1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
6 * Copyright (C) 2004 - 2006 Mike Christie
7 * Copyright (C) 2004 - 2005 Dmitry Yusupov
8 * Copyright (C) 2004 - 2005 Alex Aizman
9 * maintained by open-iscsi@googlegroups.com
11 #include <linux/types.h>
12 #include <linux/kfifo.h>
13 #include <linux/delay.h>
14 #include <linux/log2.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/module.h>
18 #include <asm/unaligned.h>
20 #include <scsi/scsi_cmnd.h>
21 #include <scsi/scsi_device.h>
22 #include <scsi/scsi_eh.h>
23 #include <scsi/scsi_tcq.h>
24 #include <scsi/scsi_host.h>
25 #include <scsi/scsi.h>
26 #include <scsi/iscsi_proto.h>
27 #include <scsi/scsi_transport.h>
28 #include <scsi/scsi_transport_iscsi.h>
29 #include <scsi/libiscsi.h>
30 #include <trace/events/iscsi.h>
32 static int iscsi_dbg_lib_conn;
33 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
35 MODULE_PARM_DESC(debug_libiscsi_conn,
36 "Turn on debugging for connections in libiscsi module. "
37 "Set to 1 to turn on, and zero to turn off. Default is off.");
39 static int iscsi_dbg_lib_session;
40 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
42 MODULE_PARM_DESC(debug_libiscsi_session,
43 "Turn on debugging for sessions in libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default is off.");
46 static int iscsi_dbg_lib_eh;
47 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
49 MODULE_PARM_DESC(debug_libiscsi_eh,
50 "Turn on debugging for error handling in libiscsi module. "
51 "Set to 1 to turn on, and zero to turn off. Default is off.");
53 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
55 if (iscsi_dbg_lib_conn) \
56 iscsi_conn_printk(KERN_INFO, _conn, \
59 iscsi_dbg_trace(trace_iscsi_dbg_conn, \
60 &(_conn)->cls_conn->dev, \
61 "%s " dbg_fmt, __func__, ##arg);\
64 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
66 if (iscsi_dbg_lib_session) \
67 iscsi_session_printk(KERN_INFO, _session, \
70 iscsi_dbg_trace(trace_iscsi_dbg_session, \
71 &(_session)->cls_session->dev, \
72 "%s " dbg_fmt, __func__, ##arg); \
75 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
77 if (iscsi_dbg_lib_eh) \
78 iscsi_session_printk(KERN_INFO, _session, \
81 iscsi_dbg_trace(trace_iscsi_dbg_eh, \
82 &(_session)->cls_session->dev, \
83 "%s " dbg_fmt, __func__, ##arg); \
86 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
88 struct Scsi_Host *shost = conn->session->host;
89 struct iscsi_host *ihost = shost_priv(shost);
92 queue_work(ihost->workq, &conn->xmitwork);
94 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
96 static void __iscsi_update_cmdsn(struct iscsi_session *session,
97 uint32_t exp_cmdsn, uint32_t max_cmdsn)
100 * standard specifies this check for when to update expected and
101 * max sequence numbers
103 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
106 if (exp_cmdsn != session->exp_cmdsn &&
107 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
108 session->exp_cmdsn = exp_cmdsn;
110 if (max_cmdsn != session->max_cmdsn &&
111 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
112 session->max_cmdsn = max_cmdsn;
115 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
117 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
118 be32_to_cpu(hdr->max_cmdsn));
120 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
123 * iscsi_prep_data_out_pdu - initialize Data-Out
124 * @task: scsi command task
126 * @hdr: iscsi data in pdu
129 * Initialize Data-Out within this R2T sequence and finds
130 * proper data_offset within this SCSI command.
132 * This function is called with connection lock taken.
134 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
135 struct iscsi_data *hdr)
137 struct iscsi_conn *conn = task->conn;
138 unsigned int left = r2t->data_length - r2t->sent;
140 task->hdr_len = sizeof(struct iscsi_data);
142 memset(hdr, 0, sizeof(struct iscsi_data));
144 hdr->datasn = cpu_to_be32(r2t->datasn);
146 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
147 hdr->lun = task->lun;
148 hdr->itt = task->hdr_itt;
149 hdr->exp_statsn = r2t->exp_statsn;
150 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
151 if (left > conn->max_xmit_dlength) {
152 hton24(hdr->dlength, conn->max_xmit_dlength);
153 r2t->data_count = conn->max_xmit_dlength;
156 hton24(hdr->dlength, left);
157 r2t->data_count = left;
158 hdr->flags = ISCSI_FLAG_CMD_FINAL;
160 conn->dataout_pdus_cnt++;
162 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
164 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
166 unsigned exp_len = task->hdr_len + len;
168 if (exp_len > task->hdr_max) {
173 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
174 task->hdr_len = exp_len;
179 * make an extended cdb AHS
181 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
183 struct scsi_cmnd *cmd = task->sc;
184 unsigned rlen, pad_len;
185 unsigned short ahslength;
186 struct iscsi_ecdb_ahdr *ecdb_ahdr;
189 ecdb_ahdr = iscsi_next_hdr(task);
190 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
192 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
193 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
195 pad_len = iscsi_padding(rlen);
197 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
198 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
203 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
205 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
206 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
207 ecdb_ahdr->reserved = 0;
208 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
210 ISCSI_DBG_SESSION(task->conn->session,
211 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
212 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
213 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
219 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
221 * @opcode: opcode to check for
223 * During TMF a task has to be checked if it's affected.
224 * All unrelated I/O can be passed through, but I/O to the
225 * affected LUN should be restricted.
226 * If 'fast_abort' is set we won't be sending any I/O to the
228 * Otherwise the target is waiting for all TTTs to be completed,
229 * so we have to send all outstanding Data-Out PDUs to the target.
231 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
233 struct iscsi_session *session = task->conn->session;
234 struct iscsi_tm *tmf = &session->tmhdr;
237 if (session->tmf_state == TMF_INITIAL)
240 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
243 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
244 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
246 * Allow PDUs for unrelated LUNs
248 hdr_lun = scsilun_to_int(&tmf->lun);
249 if (hdr_lun != task->sc->device->lun)
252 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
254 * Fail all SCSI cmd PDUs
256 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
257 iscsi_session_printk(KERN_INFO, session,
258 "task [op %x itt 0x%x/0x%x] rejected.\n",
259 opcode, task->itt, task->hdr_itt);
263 * And also all data-out PDUs in response to R2T
264 * if fast_abort is set.
266 if (session->fast_abort) {
267 iscsi_session_printk(KERN_INFO, session,
268 "task [op %x itt 0x%x/0x%x] fast abort.\n",
269 opcode, task->itt, task->hdr_itt);
273 case ISCSI_TM_FUNC_ABORT_TASK:
275 * the caller has already checked if the task
276 * they want to abort was in the pending queue so if
277 * we are here the cmd pdu has gone out already, and
278 * we will only hit this for data-outs
280 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
281 task->hdr_itt == tmf->rtt) {
282 ISCSI_DBG_SESSION(session,
283 "Preventing task %x/%x from sending "
284 "data-out due to abort task in "
285 "progress\n", task->itt,
296 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
299 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
300 * fields like dlength or final based on how much data it sends
302 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
304 struct iscsi_conn *conn = task->conn;
305 struct iscsi_session *session = conn->session;
306 struct scsi_cmnd *sc = task->sc;
307 struct iscsi_scsi_req *hdr;
308 unsigned hdrlength, cmd_len, transfer_length;
312 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
316 if (conn->session->tt->alloc_pdu) {
317 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
321 hdr = (struct iscsi_scsi_req *)task->hdr;
323 memset(hdr, 0, sizeof(*hdr));
325 if (session->tt->parse_pdu_itt)
326 hdr->itt = task->hdr_itt = itt;
328 hdr->itt = task->hdr_itt = build_itt(task->itt,
329 task->conn->session->age);
331 rc = iscsi_add_hdr(task, sizeof(*hdr));
334 hdr->opcode = ISCSI_OP_SCSI_CMD;
335 hdr->flags = ISCSI_ATTR_SIMPLE;
336 int_to_scsilun(sc->device->lun, &hdr->lun);
337 task->lun = hdr->lun;
338 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
339 cmd_len = sc->cmd_len;
340 if (cmd_len < ISCSI_CDB_SIZE)
341 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
342 else if (cmd_len > ISCSI_CDB_SIZE) {
343 rc = iscsi_prep_ecdb_ahs(task);
346 cmd_len = ISCSI_CDB_SIZE;
348 memcpy(hdr->cdb, sc->cmnd, cmd_len);
351 if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
352 task->protected = true;
354 transfer_length = scsi_transfer_length(sc);
355 hdr->data_length = cpu_to_be32(transfer_length);
356 if (sc->sc_data_direction == DMA_TO_DEVICE) {
357 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
359 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
363 * imm_count bytes to be sent right after
366 * unsol_count bytes(as Data-Out) to be sent
367 * without R2T ack right after
370 * r2t data_length bytes to be sent via R2T ack's
372 * pad_count bytes to be sent as zero-padding
374 memset(r2t, 0, sizeof(*r2t));
376 if (session->imm_data_en) {
377 if (transfer_length >= session->first_burst)
378 task->imm_count = min(session->first_burst,
379 conn->max_xmit_dlength);
381 task->imm_count = min(transfer_length,
382 conn->max_xmit_dlength);
383 hton24(hdr->dlength, task->imm_count);
385 zero_data(hdr->dlength);
387 if (!session->initial_r2t_en) {
388 r2t->data_length = min(session->first_burst,
391 r2t->data_offset = task->imm_count;
392 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
393 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
396 if (!task->unsol_r2t.data_length)
397 /* No unsolicit Data-Out's */
398 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
400 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
401 zero_data(hdr->dlength);
403 if (sc->sc_data_direction == DMA_FROM_DEVICE)
404 hdr->flags |= ISCSI_FLAG_CMD_READ;
407 /* calculate size of additional header segments (AHSs) */
408 hdrlength = task->hdr_len - sizeof(*hdr);
410 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
411 hdrlength /= ISCSI_PAD_LEN;
413 WARN_ON(hdrlength >= 256);
414 hdr->hlength = hdrlength & 0xFF;
415 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
417 if (session->tt->init_task && session->tt->init_task(task))
420 task->state = ISCSI_TASK_RUNNING;
423 conn->scsicmd_pdus_cnt++;
424 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
425 "itt 0x%x len %d cmdsn %d win %d]\n",
426 sc->sc_data_direction == DMA_TO_DEVICE ?
427 "write" : "read", conn->id, sc, sc->cmnd[0],
428 task->itt, transfer_length,
430 session->max_cmdsn - session->exp_cmdsn + 1);
435 * iscsi_free_task - free a task
436 * @task: iscsi cmd task
438 * Must be called with session back_lock.
439 * This function returns the scsi command to scsi-ml or cleans
440 * up mgmt tasks then returns the task to the pool.
442 static void iscsi_free_task(struct iscsi_task *task)
444 struct iscsi_conn *conn = task->conn;
445 struct iscsi_session *session = conn->session;
446 struct scsi_cmnd *sc = task->sc;
447 int oldstate = task->state;
449 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
450 task->itt, task->state, task->sc);
452 session->tt->cleanup_task(task);
453 task->state = ISCSI_TASK_FREE;
456 * login task is preallocated so do not free
458 if (conn->login_task == task)
461 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
464 /* SCSI eh reuses commands to verify us */
467 * queue command may call this to free the task, so
468 * it will decide how to return sc to scsi-ml.
470 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
475 void __iscsi_get_task(struct iscsi_task *task)
477 refcount_inc(&task->refcount);
479 EXPORT_SYMBOL_GPL(__iscsi_get_task);
481 void __iscsi_put_task(struct iscsi_task *task)
483 if (refcount_dec_and_test(&task->refcount))
484 iscsi_free_task(task);
486 EXPORT_SYMBOL_GPL(__iscsi_put_task);
488 void iscsi_put_task(struct iscsi_task *task)
490 struct iscsi_session *session = task->conn->session;
492 /* regular RX path uses back_lock */
493 spin_lock_bh(&session->back_lock);
494 __iscsi_put_task(task);
495 spin_unlock_bh(&session->back_lock);
497 EXPORT_SYMBOL_GPL(iscsi_put_task);
500 * iscsi_complete_task - finish a task
501 * @task: iscsi cmd task
502 * @state: state to complete task with
504 * Must be called with session back_lock.
506 static void iscsi_complete_task(struct iscsi_task *task, int state)
508 struct iscsi_conn *conn = task->conn;
510 ISCSI_DBG_SESSION(conn->session,
511 "complete task itt 0x%x state %d sc %p\n",
512 task->itt, task->state, task->sc);
513 if (task->state == ISCSI_TASK_COMPLETED ||
514 task->state == ISCSI_TASK_ABRT_TMF ||
515 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
516 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
518 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
521 spin_lock_bh(&conn->taskqueuelock);
522 if (!list_empty(&task->running)) {
523 pr_debug_once("%s while task on list", __func__);
524 list_del_init(&task->running);
526 spin_unlock_bh(&conn->taskqueuelock);
528 if (conn->task == task)
531 if (READ_ONCE(conn->ping_task) == task)
532 WRITE_ONCE(conn->ping_task, NULL);
534 /* release get from queueing */
535 __iscsi_put_task(task);
539 * iscsi_complete_scsi_task - finish scsi task normally
540 * @task: iscsi task for scsi cmd
541 * @exp_cmdsn: expected cmd sn in cpu format
542 * @max_cmdsn: max cmd sn in cpu format
544 * This is used when drivers do not need or cannot perform
545 * lower level pdu processing.
547 * Called with session back_lock
549 void iscsi_complete_scsi_task(struct iscsi_task *task,
550 uint32_t exp_cmdsn, uint32_t max_cmdsn)
552 struct iscsi_conn *conn = task->conn;
554 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
556 conn->last_recv = jiffies;
557 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
558 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
560 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
564 * session back_lock must be held and if not called for a task that is
565 * still pending or from the xmit thread, then xmit thread must
568 static void fail_scsi_task(struct iscsi_task *task, int err)
570 struct iscsi_conn *conn = task->conn;
571 struct scsi_cmnd *sc;
575 * if a command completes and we get a successful tmf response
576 * we will hit this because the scsi eh abort code does not take
583 if (task->state == ISCSI_TASK_PENDING) {
585 * cmd never made it to the xmit thread, so we should not count
586 * the cmd in the sequencing
588 conn->session->queued_cmdsn--;
589 /* it was never sent so just complete like normal */
590 state = ISCSI_TASK_COMPLETED;
591 } else if (err == DID_TRANSPORT_DISRUPTED)
592 state = ISCSI_TASK_ABRT_SESS_RECOV;
594 state = ISCSI_TASK_ABRT_TMF;
596 sc->result = err << 16;
597 scsi_set_resid(sc, scsi_bufflen(sc));
599 /* regular RX path uses back_lock */
600 spin_lock_bh(&conn->session->back_lock);
601 iscsi_complete_task(task, state);
602 spin_unlock_bh(&conn->session->back_lock);
605 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
606 struct iscsi_task *task)
608 struct iscsi_session *session = conn->session;
609 struct iscsi_hdr *hdr = task->hdr;
610 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
611 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
613 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
616 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
617 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
619 * pre-format CmdSN for outgoing PDU.
621 nop->cmdsn = cpu_to_be32(session->cmdsn);
622 if (hdr->itt != RESERVED_ITT) {
624 * TODO: We always use immediate for normal session pdus.
625 * If we start to send tmfs or nops as non-immediate then
626 * we should start checking the cmdsn numbers for mgmt tasks.
628 * During discovery sessions iscsid sends TEXT as non immediate,
629 * but we always only send one PDU at a time.
631 if (conn->c_stage == ISCSI_CONN_STARTED &&
632 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
633 session->queued_cmdsn++;
638 if (session->tt->init_task && session->tt->init_task(task))
641 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
642 session->state = ISCSI_STATE_LOGGING_OUT;
644 task->state = ISCSI_TASK_RUNNING;
645 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
646 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
647 hdr->itt, task->data_count);
651 static struct iscsi_task *
652 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
653 char *data, uint32_t data_size)
655 struct iscsi_session *session = conn->session;
656 struct iscsi_host *ihost = shost_priv(session->host);
657 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
658 struct iscsi_task *task;
661 if (session->state == ISCSI_STATE_TERMINATE)
664 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
666 * Login and Text are sent serially, in
667 * request-followed-by-response sequence.
668 * Same task can be used. Same ITT must be used.
669 * Note that login_task is preallocated at conn_create().
671 if (conn->login_task->state != ISCSI_TASK_FREE) {
672 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
673 "progress. Cannot start new task.\n");
677 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
678 iscsi_conn_printk(KERN_ERR, conn, "Invalid buffer len of %u for login task. Max len is %u\n", data_size, ISCSI_DEF_MAX_RECV_SEG_LEN);
682 task = conn->login_task;
684 if (session->state != ISCSI_STATE_LOGGED_IN)
687 if (data_size != 0) {
688 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
692 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
693 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
695 if (!kfifo_out(&session->cmdpool.queue,
696 (void*)&task, sizeof(void*)))
700 * released in complete pdu for task we expect a response for, and
701 * released by the lld when it has transmitted the task for
702 * pdus we do not expect a response for.
704 refcount_set(&task->refcount, 1);
707 INIT_LIST_HEAD(&task->running);
708 task->state = ISCSI_TASK_PENDING;
711 memcpy(task->data, data, data_size);
712 task->data_count = data_size;
714 task->data_count = 0;
716 if (conn->session->tt->alloc_pdu) {
717 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
718 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
719 "pdu for mgmt task.\n");
724 itt = task->hdr->itt;
725 task->hdr_len = sizeof(struct iscsi_hdr);
726 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
728 if (hdr->itt != RESERVED_ITT) {
729 if (session->tt->parse_pdu_itt)
730 task->hdr->itt = itt;
732 task->hdr->itt = build_itt(task->itt,
733 task->conn->session->age);
736 if (unlikely(READ_ONCE(conn->ping_task) == INVALID_SCSI_TASK))
737 WRITE_ONCE(conn->ping_task, task);
740 if (iscsi_prep_mgmt_task(conn, task))
743 if (session->tt->xmit_task(task))
746 spin_lock_bh(&conn->taskqueuelock);
747 list_add_tail(&task->running, &conn->mgmtqueue);
748 spin_unlock_bh(&conn->taskqueuelock);
749 iscsi_conn_queue_work(conn);
755 /* regular RX path uses back_lock */
756 spin_lock(&session->back_lock);
757 __iscsi_put_task(task);
758 spin_unlock(&session->back_lock);
762 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
763 char *data, uint32_t data_size)
765 struct iscsi_conn *conn = cls_conn->dd_data;
766 struct iscsi_session *session = conn->session;
769 spin_lock_bh(&session->frwd_lock);
770 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
772 spin_unlock_bh(&session->frwd_lock);
775 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
778 * iscsi_cmd_rsp - SCSI Command Response processing
779 * @conn: iscsi connection
781 * @task: scsi command task
782 * @data: cmd data buffer
783 * @datalen: len of buffer
785 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
786 * then completes the command and task. called under back_lock
788 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
789 struct iscsi_task *task, char *data,
792 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
793 struct iscsi_session *session = conn->session;
794 struct scsi_cmnd *sc = task->sc;
796 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
797 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
799 sc->result = (DID_OK << 16) | rhdr->cmd_status;
801 if (task->protected) {
806 * Transports that didn't implement check_protection
807 * callback but still published T10-PI support to scsi-mid
808 * deserve this BUG_ON.
810 BUG_ON(!session->tt->check_protection);
812 ascq = session->tt->check_protection(task, §or);
814 sc->result = DRIVER_SENSE << 24 |
815 SAM_STAT_CHECK_CONDITION;
816 scsi_build_sense_buffer(1, sc->sense_buffer,
817 ILLEGAL_REQUEST, 0x10, ascq);
818 scsi_set_sense_information(sc->sense_buffer,
819 SCSI_SENSE_BUFFERSIZE,
825 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
826 sc->result = DID_ERROR << 16;
830 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
835 iscsi_conn_printk(KERN_ERR, conn,
836 "Got CHECK_CONDITION but invalid data "
837 "buffer size of %d\n", datalen);
838 sc->result = DID_BAD_TARGET << 16;
842 senselen = get_unaligned_be16(data);
843 if (datalen < senselen)
844 goto invalid_datalen;
846 memcpy(sc->sense_buffer, data + 2,
847 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
848 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
849 min_t(uint16_t, senselen,
850 SCSI_SENSE_BUFFERSIZE));
853 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
854 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
855 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
858 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
859 ISCSI_FLAG_CMD_OVERFLOW)) {
860 int res_count = be32_to_cpu(rhdr->residual_count);
863 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
864 res_count <= scsi_bufflen(sc)))
865 /* write side for bidi or uni-io set_resid */
866 scsi_set_resid(sc, res_count);
868 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
871 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
872 sc, sc->result, task->itt);
873 conn->scsirsp_pdus_cnt++;
874 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
878 * iscsi_data_in_rsp - SCSI Data-In Response processing
879 * @conn: iscsi connection
881 * @task: scsi command task
883 * iscsi_data_in_rsp sets up the scsi_cmnd fields based on the data received
884 * then completes the command and task. called under back_lock
887 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
888 struct iscsi_task *task)
890 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
891 struct scsi_cmnd *sc = task->sc;
893 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
896 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
897 sc->result = (DID_OK << 16) | rhdr->cmd_status;
898 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
899 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
900 ISCSI_FLAG_DATA_OVERFLOW)) {
901 int res_count = be32_to_cpu(rhdr->residual_count);
904 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
905 res_count <= sc->sdb.length))
906 scsi_set_resid(sc, res_count);
908 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
911 ISCSI_DBG_SESSION(conn->session, "data in with status done "
912 "[sc %p res %d itt 0x%x]\n",
913 sc, sc->result, task->itt);
914 conn->scsirsp_pdus_cnt++;
915 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
918 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
920 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
921 struct iscsi_session *session = conn->session;
923 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
924 conn->tmfrsp_pdus_cnt++;
926 if (session->tmf_state != TMF_QUEUED)
929 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
930 session->tmf_state = TMF_SUCCESS;
931 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
932 session->tmf_state = TMF_NOT_FOUND;
934 session->tmf_state = TMF_FAILED;
935 wake_up(&session->ehwait);
938 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
940 struct iscsi_nopout hdr;
941 struct iscsi_task *task;
944 if (READ_ONCE(conn->ping_task))
946 WRITE_ONCE(conn->ping_task, INVALID_SCSI_TASK);
949 memset(&hdr, 0, sizeof(struct iscsi_nopout));
950 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
951 hdr.flags = ISCSI_FLAG_CMD_FINAL;
956 hdr.itt = RESERVED_ITT;
958 hdr.ttt = RESERVED_ITT;
960 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
963 WRITE_ONCE(conn->ping_task, NULL);
964 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
967 /* only track our nops */
968 conn->last_ping = jiffies;
975 * iscsi_nop_out_rsp - SCSI NOP Response processing
976 * @task: scsi command task
977 * @nop: the nop structure
978 * @data: where to put the data
979 * @datalen: length of data
981 * iscsi_nop_out_rsp handles nop response from use or
982 * from user space. called under back_lock
984 static int iscsi_nop_out_rsp(struct iscsi_task *task,
985 struct iscsi_nopin *nop, char *data, int datalen)
987 struct iscsi_conn *conn = task->conn;
990 if (READ_ONCE(conn->ping_task) != task) {
992 * If this is not in response to one of our
993 * nops then it must be from userspace.
995 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
997 rc = ISCSI_ERR_CONN_FAILED;
999 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1000 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1004 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1005 char *data, int datalen)
1007 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1008 struct iscsi_hdr rejected_pdu;
1011 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1013 if (ntoh24(reject->dlength) > datalen ||
1014 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1015 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1016 "pdu. Invalid data length (pdu dlength "
1017 "%u, datalen %d\n", ntoh24(reject->dlength),
1019 return ISCSI_ERR_PROTO;
1021 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1022 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1024 switch (reject->reason) {
1025 case ISCSI_REASON_DATA_DIGEST_ERROR:
1026 iscsi_conn_printk(KERN_ERR, conn,
1027 "pdu (op 0x%x itt 0x%x) rejected "
1028 "due to DataDigest error.\n",
1029 opcode, rejected_pdu.itt);
1031 case ISCSI_REASON_IMM_CMD_REJECT:
1032 iscsi_conn_printk(KERN_ERR, conn,
1033 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1034 "immediate commands.\n",
1035 opcode, rejected_pdu.itt);
1037 * We only send one TMF at a time so if the target could not
1038 * handle it, then it should get fixed (RFC mandates that
1039 * a target can handle one immediate TMF per conn).
1041 * For nops-outs, we could have sent more than one if
1042 * the target is sending us lots of nop-ins
1044 if (opcode != ISCSI_OP_NOOP_OUT)
1047 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1049 * nop-out in response to target's nop-out rejected.
1052 /* In RX path we are under back lock */
1053 spin_unlock(&conn->session->back_lock);
1054 spin_lock(&conn->session->frwd_lock);
1055 iscsi_send_nopout(conn,
1056 (struct iscsi_nopin*)&rejected_pdu);
1057 spin_unlock(&conn->session->frwd_lock);
1058 spin_lock(&conn->session->back_lock);
1060 struct iscsi_task *task;
1062 * Our nop as ping got dropped. We know the target
1063 * and transport are ok so just clean up
1065 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1067 iscsi_conn_printk(KERN_ERR, conn,
1068 "Invalid pdu reject. Could "
1069 "not lookup rejected task.\n");
1070 rc = ISCSI_ERR_BAD_ITT;
1072 rc = iscsi_nop_out_rsp(task,
1073 (struct iscsi_nopin*)&rejected_pdu,
1078 iscsi_conn_printk(KERN_ERR, conn,
1079 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1080 "code 0x%x\n", rejected_pdu.opcode,
1081 rejected_pdu.itt, reject->reason);
1088 * iscsi_itt_to_task - look up task by itt
1089 * @conn: iscsi connection
1092 * This should be used for mgmt tasks like login and nops, or if
1093 * the LDD's itt space does not include the session age.
1095 * The session back_lock must be held.
1097 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1099 struct iscsi_session *session = conn->session;
1102 if (itt == RESERVED_ITT)
1105 if (session->tt->parse_pdu_itt)
1106 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1109 if (i >= session->cmds_max)
1112 return session->cmds[i];
1114 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1117 * __iscsi_complete_pdu - complete pdu
1119 * @hdr: iscsi header
1120 * @data: data buffer
1121 * @datalen: len of data buffer
1123 * Completes pdu processing by freeing any resources allocated at
1124 * queuecommand or send generic. session back_lock must be held and verify
1125 * itt must have been called.
1127 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1128 char *data, int datalen)
1130 struct iscsi_session *session = conn->session;
1131 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1132 struct iscsi_task *task;
1135 conn->last_recv = jiffies;
1136 rc = iscsi_verify_itt(conn, hdr->itt);
1140 if (hdr->itt != RESERVED_ITT)
1141 itt = get_itt(hdr->itt);
1145 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1146 opcode, conn->id, itt, datalen);
1149 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1152 case ISCSI_OP_NOOP_IN:
1154 rc = ISCSI_ERR_PROTO;
1158 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1161 /* In RX path we are under back lock */
1162 spin_unlock(&session->back_lock);
1163 spin_lock(&session->frwd_lock);
1164 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1165 spin_unlock(&session->frwd_lock);
1166 spin_lock(&session->back_lock);
1168 case ISCSI_OP_REJECT:
1169 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1171 case ISCSI_OP_ASYNC_EVENT:
1172 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1173 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1174 rc = ISCSI_ERR_CONN_FAILED;
1177 rc = ISCSI_ERR_BAD_OPCODE;
1184 case ISCSI_OP_SCSI_CMD_RSP:
1185 case ISCSI_OP_SCSI_DATA_IN:
1186 task = iscsi_itt_to_ctask(conn, hdr->itt);
1188 return ISCSI_ERR_BAD_ITT;
1189 task->last_xfer = jiffies;
1193 * LLD handles R2Ts if they need to.
1196 case ISCSI_OP_LOGOUT_RSP:
1197 case ISCSI_OP_LOGIN_RSP:
1198 case ISCSI_OP_TEXT_RSP:
1199 case ISCSI_OP_SCSI_TMFUNC_RSP:
1200 case ISCSI_OP_NOOP_IN:
1201 task = iscsi_itt_to_task(conn, hdr->itt);
1203 return ISCSI_ERR_BAD_ITT;
1206 return ISCSI_ERR_BAD_OPCODE;
1210 case ISCSI_OP_SCSI_CMD_RSP:
1211 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1213 case ISCSI_OP_SCSI_DATA_IN:
1214 iscsi_data_in_rsp(conn, hdr, task);
1216 case ISCSI_OP_LOGOUT_RSP:
1217 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1219 rc = ISCSI_ERR_PROTO;
1222 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1224 case ISCSI_OP_LOGIN_RSP:
1225 case ISCSI_OP_TEXT_RSP:
1226 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1228 * login related PDU's exp_statsn is handled in
1232 case ISCSI_OP_SCSI_TMFUNC_RSP:
1233 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1235 rc = ISCSI_ERR_PROTO;
1239 iscsi_tmf_rsp(conn, hdr);
1240 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1242 case ISCSI_OP_NOOP_IN:
1243 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1244 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1245 rc = ISCSI_ERR_PROTO;
1248 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1250 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1254 rc = ISCSI_ERR_BAD_OPCODE;
1261 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1262 rc = ISCSI_ERR_CONN_FAILED;
1263 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1266 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1268 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1269 char *data, int datalen)
1273 spin_lock(&conn->session->back_lock);
1274 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1275 spin_unlock(&conn->session->back_lock);
1278 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1280 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1282 struct iscsi_session *session = conn->session;
1285 if (itt == RESERVED_ITT)
1288 if (session->tt->parse_pdu_itt)
1289 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1292 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1295 if (age != session->age) {
1296 iscsi_conn_printk(KERN_ERR, conn,
1297 "received itt %x expected session age (%x)\n",
1298 (__force u32)itt, session->age);
1299 return ISCSI_ERR_BAD_ITT;
1302 if (i >= session->cmds_max) {
1303 iscsi_conn_printk(KERN_ERR, conn,
1304 "received invalid itt index %u (max cmds "
1305 "%u.\n", i, session->cmds_max);
1306 return ISCSI_ERR_BAD_ITT;
1310 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1313 * iscsi_itt_to_ctask - look up ctask by itt
1314 * @conn: iscsi connection
1317 * This should be used for cmd tasks.
1319 * The session back_lock must be held.
1321 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1323 struct iscsi_task *task;
1325 if (iscsi_verify_itt(conn, itt))
1328 task = iscsi_itt_to_task(conn, itt);
1329 if (!task || !task->sc)
1332 if (task->sc->SCp.phase != conn->session->age) {
1333 iscsi_session_printk(KERN_ERR, conn->session,
1334 "task's session age %d, expected %d\n",
1335 task->sc->SCp.phase, conn->session->age);
1341 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1343 void iscsi_session_failure(struct iscsi_session *session,
1346 struct iscsi_conn *conn;
1348 spin_lock_bh(&session->frwd_lock);
1349 conn = session->leadconn;
1350 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1351 spin_unlock_bh(&session->frwd_lock);
1355 iscsi_get_conn(conn->cls_conn);
1356 spin_unlock_bh(&session->frwd_lock);
1358 * if the host is being removed bypass the connection
1359 * recovery initialization because we are going to kill
1362 if (err == ISCSI_ERR_INVALID_HOST)
1363 iscsi_conn_error_event(conn->cls_conn, err);
1365 iscsi_conn_failure(conn, err);
1366 iscsi_put_conn(conn->cls_conn);
1368 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1370 static bool iscsi_set_conn_failed(struct iscsi_conn *conn)
1372 struct iscsi_session *session = conn->session;
1374 if (session->state == ISCSI_STATE_FAILED)
1377 if (conn->stop_stage == 0)
1378 session->state = ISCSI_STATE_FAILED;
1380 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1381 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1385 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1387 struct iscsi_session *session = conn->session;
1390 spin_lock_bh(&session->frwd_lock);
1391 needs_evt = iscsi_set_conn_failed(conn);
1392 spin_unlock_bh(&session->frwd_lock);
1395 iscsi_conn_error_event(conn->cls_conn, err);
1397 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1399 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1401 struct iscsi_session *session = conn->session;
1404 * Check for iSCSI window and take care of CmdSN wrap-around
1406 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1407 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1408 "%u MaxCmdSN %u CmdSN %u/%u\n",
1409 session->exp_cmdsn, session->max_cmdsn,
1410 session->cmdsn, session->queued_cmdsn);
1416 static int iscsi_xmit_task(struct iscsi_conn *conn)
1418 struct iscsi_task *task = conn->task;
1421 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1424 spin_lock_bh(&conn->session->back_lock);
1425 if (conn->task == NULL) {
1426 spin_unlock_bh(&conn->session->back_lock);
1429 __iscsi_get_task(task);
1430 spin_unlock_bh(&conn->session->back_lock);
1431 spin_unlock_bh(&conn->session->frwd_lock);
1432 rc = conn->session->tt->xmit_task(task);
1433 spin_lock_bh(&conn->session->frwd_lock);
1435 /* done with this task */
1436 task->last_xfer = jiffies;
1439 /* regular RX path uses back_lock */
1440 spin_lock(&conn->session->back_lock);
1441 __iscsi_put_task(task);
1442 spin_unlock(&conn->session->back_lock);
1447 * iscsi_requeue_task - requeue task to run from session workqueue
1448 * @task: task to requeue
1450 * LLDs that need to run a task from the session workqueue should call
1451 * this. The session frwd_lock must be held. This should only be called
1452 * by software drivers.
1454 void iscsi_requeue_task(struct iscsi_task *task)
1456 struct iscsi_conn *conn = task->conn;
1459 * this may be on the requeue list already if the xmit_task callout
1460 * is handling the r2ts while we are adding new ones
1462 spin_lock_bh(&conn->taskqueuelock);
1463 if (list_empty(&task->running))
1464 list_add_tail(&task->running, &conn->requeue);
1465 spin_unlock_bh(&conn->taskqueuelock);
1466 iscsi_conn_queue_work(conn);
1468 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1471 * iscsi_data_xmit - xmit any command into the scheduled connection
1472 * @conn: iscsi connection
1475 * The function can return -EAGAIN in which case the caller must
1476 * re-schedule it again later or recover. '0' return code means
1479 static int iscsi_data_xmit(struct iscsi_conn *conn)
1481 struct iscsi_task *task;
1484 spin_lock_bh(&conn->session->frwd_lock);
1485 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1486 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1487 spin_unlock_bh(&conn->session->frwd_lock);
1492 rc = iscsi_xmit_task(conn);
1498 * process mgmt pdus like nops before commands since we should
1499 * only have one nop-out as a ping from us and targets should not
1500 * overflow us with nop-ins
1502 spin_lock_bh(&conn->taskqueuelock);
1504 while (!list_empty(&conn->mgmtqueue)) {
1505 conn->task = list_entry(conn->mgmtqueue.next,
1506 struct iscsi_task, running);
1507 list_del_init(&conn->task->running);
1508 spin_unlock_bh(&conn->taskqueuelock);
1509 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1510 /* regular RX path uses back_lock */
1511 spin_lock_bh(&conn->session->back_lock);
1512 __iscsi_put_task(conn->task);
1513 spin_unlock_bh(&conn->session->back_lock);
1515 spin_lock_bh(&conn->taskqueuelock);
1518 rc = iscsi_xmit_task(conn);
1521 spin_lock_bh(&conn->taskqueuelock);
1524 /* process pending command queue */
1525 while (!list_empty(&conn->cmdqueue)) {
1526 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1528 list_del_init(&conn->task->running);
1529 spin_unlock_bh(&conn->taskqueuelock);
1530 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1531 fail_scsi_task(conn->task, DID_IMM_RETRY);
1532 spin_lock_bh(&conn->taskqueuelock);
1535 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1537 if (rc == -ENOMEM || rc == -EACCES)
1538 fail_scsi_task(conn->task, DID_IMM_RETRY);
1540 fail_scsi_task(conn->task, DID_ABORT);
1541 spin_lock_bh(&conn->taskqueuelock);
1544 rc = iscsi_xmit_task(conn);
1548 * we could continuously get new task requests so
1549 * we need to check the mgmt queue for nops that need to
1550 * be sent to aviod starvation
1552 spin_lock_bh(&conn->taskqueuelock);
1553 if (!list_empty(&conn->mgmtqueue))
1557 while (!list_empty(&conn->requeue)) {
1559 * we always do fastlogout - conn stop code will clean up.
1561 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1564 task = list_entry(conn->requeue.next, struct iscsi_task,
1566 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1570 list_del_init(&conn->task->running);
1571 conn->task->state = ISCSI_TASK_RUNNING;
1572 spin_unlock_bh(&conn->taskqueuelock);
1573 rc = iscsi_xmit_task(conn);
1576 spin_lock_bh(&conn->taskqueuelock);
1577 if (!list_empty(&conn->mgmtqueue))
1580 spin_unlock_bh(&conn->taskqueuelock);
1581 spin_unlock_bh(&conn->session->frwd_lock);
1585 spin_unlock_bh(&conn->session->frwd_lock);
1589 static void iscsi_xmitworker(struct work_struct *work)
1591 struct iscsi_conn *conn =
1592 container_of(work, struct iscsi_conn, xmitwork);
1595 * serialize Xmit worker on a per-connection basis.
1598 rc = iscsi_data_xmit(conn);
1599 } while (rc >= 0 || rc == -EAGAIN);
1602 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1603 struct scsi_cmnd *sc)
1605 struct iscsi_task *task;
1607 if (!kfifo_out(&conn->session->cmdpool.queue,
1608 (void *) &task, sizeof(void *)))
1611 sc->SCp.phase = conn->session->age;
1612 sc->SCp.ptr = (char *) task;
1614 refcount_set(&task->refcount, 1);
1615 task->state = ISCSI_TASK_PENDING;
1618 task->have_checked_conn = false;
1619 task->last_timeout = jiffies;
1620 task->last_xfer = jiffies;
1621 task->protected = false;
1622 INIT_LIST_HEAD(&task->running);
1627 FAILURE_BAD_HOST = 1,
1628 FAILURE_SESSION_FAILED,
1629 FAILURE_SESSION_FREED,
1630 FAILURE_WINDOW_CLOSED,
1632 FAILURE_SESSION_TERMINATE,
1633 FAILURE_SESSION_IN_RECOVERY,
1634 FAILURE_SESSION_RECOVERY_TIMEOUT,
1635 FAILURE_SESSION_LOGGING_OUT,
1636 FAILURE_SESSION_NOT_READY,
1639 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1641 struct iscsi_cls_session *cls_session;
1642 struct iscsi_host *ihost;
1644 struct iscsi_session *session;
1645 struct iscsi_conn *conn;
1646 struct iscsi_task *task = NULL;
1651 ihost = shost_priv(host);
1653 cls_session = starget_to_session(scsi_target(sc->device));
1654 session = cls_session->dd_data;
1655 spin_lock_bh(&session->frwd_lock);
1657 reason = iscsi_session_chkready(cls_session);
1659 sc->result = reason;
1663 if (session->state != ISCSI_STATE_LOGGED_IN) {
1665 * to handle the race between when we set the recovery state
1666 * and block the session we requeue here (commands could
1667 * be entering our queuecommand while a block is starting
1668 * up because the block code is not locked)
1670 switch (session->state) {
1671 case ISCSI_STATE_FAILED:
1673 * cmds should fail during shutdown, if the session
1674 * state is bad, allowing completion to happen
1676 if (unlikely(system_state != SYSTEM_RUNNING)) {
1677 reason = FAILURE_SESSION_FAILED;
1678 sc->result = DID_NO_CONNECT << 16;
1682 case ISCSI_STATE_IN_RECOVERY:
1683 reason = FAILURE_SESSION_IN_RECOVERY;
1684 sc->result = DID_IMM_RETRY << 16;
1686 case ISCSI_STATE_LOGGING_OUT:
1687 reason = FAILURE_SESSION_LOGGING_OUT;
1688 sc->result = DID_IMM_RETRY << 16;
1690 case ISCSI_STATE_RECOVERY_FAILED:
1691 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1692 sc->result = DID_TRANSPORT_FAILFAST << 16;
1694 case ISCSI_STATE_TERMINATE:
1695 reason = FAILURE_SESSION_TERMINATE;
1696 sc->result = DID_NO_CONNECT << 16;
1699 reason = FAILURE_SESSION_FREED;
1700 sc->result = DID_NO_CONNECT << 16;
1705 conn = session->leadconn;
1707 reason = FAILURE_SESSION_FREED;
1708 sc->result = DID_NO_CONNECT << 16;
1712 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1713 reason = FAILURE_SESSION_IN_RECOVERY;
1714 sc->result = DID_REQUEUE << 16;
1718 if (iscsi_check_cmdsn_window_closed(conn)) {
1719 reason = FAILURE_WINDOW_CLOSED;
1723 task = iscsi_alloc_task(conn, sc);
1725 reason = FAILURE_OOM;
1729 if (!ihost->workq) {
1730 reason = iscsi_prep_scsi_cmd_pdu(task);
1732 if (reason == -ENOMEM || reason == -EACCES) {
1733 reason = FAILURE_OOM;
1736 sc->result = DID_ABORT << 16;
1740 if (session->tt->xmit_task(task)) {
1742 reason = FAILURE_SESSION_NOT_READY;
1746 spin_lock_bh(&conn->taskqueuelock);
1747 list_add_tail(&task->running, &conn->cmdqueue);
1748 spin_unlock_bh(&conn->taskqueuelock);
1749 iscsi_conn_queue_work(conn);
1752 session->queued_cmdsn++;
1753 spin_unlock_bh(&session->frwd_lock);
1757 spin_lock_bh(&session->back_lock);
1758 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1759 spin_unlock_bh(&session->back_lock);
1761 spin_unlock_bh(&session->frwd_lock);
1762 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1763 sc->cmnd[0], reason);
1764 return SCSI_MLQUEUE_TARGET_BUSY;
1767 spin_lock_bh(&session->back_lock);
1768 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1769 spin_unlock_bh(&session->back_lock);
1771 spin_unlock_bh(&session->frwd_lock);
1772 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1773 sc->cmnd[0], reason);
1774 scsi_set_resid(sc, scsi_bufflen(sc));
1778 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1780 int iscsi_target_alloc(struct scsi_target *starget)
1782 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1783 struct iscsi_session *session = cls_session->dd_data;
1785 starget->can_queue = session->scsi_cmds_max;
1788 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1790 static void iscsi_tmf_timedout(struct timer_list *t)
1792 struct iscsi_session *session = from_timer(session, t, tmf_timer);
1794 spin_lock(&session->frwd_lock);
1795 if (session->tmf_state == TMF_QUEUED) {
1796 session->tmf_state = TMF_TIMEDOUT;
1797 ISCSI_DBG_EH(session, "tmf timedout\n");
1798 /* unblock eh_abort() */
1799 wake_up(&session->ehwait);
1801 spin_unlock(&session->frwd_lock);
1804 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1805 struct iscsi_tm *hdr, int age,
1807 __must_hold(&session->frwd_lock)
1809 struct iscsi_session *session = conn->session;
1810 struct iscsi_task *task;
1812 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1815 spin_unlock_bh(&session->frwd_lock);
1816 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1817 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1818 spin_lock_bh(&session->frwd_lock);
1821 conn->tmfcmd_pdus_cnt++;
1822 session->tmf_timer.expires = timeout * HZ + jiffies;
1823 add_timer(&session->tmf_timer);
1824 ISCSI_DBG_EH(session, "tmf set timeout\n");
1826 spin_unlock_bh(&session->frwd_lock);
1827 mutex_unlock(&session->eh_mutex);
1830 * block eh thread until:
1834 * 3) session is terminated or restarted or userspace has
1835 * given up on recovery
1837 wait_event_interruptible(session->ehwait, age != session->age ||
1838 session->state != ISCSI_STATE_LOGGED_IN ||
1839 session->tmf_state != TMF_QUEUED);
1840 if (signal_pending(current))
1841 flush_signals(current);
1842 del_timer_sync(&session->tmf_timer);
1844 mutex_lock(&session->eh_mutex);
1845 spin_lock_bh(&session->frwd_lock);
1846 /* if the session drops it will clean up the task */
1847 if (age != session->age ||
1848 session->state != ISCSI_STATE_LOGGED_IN)
1854 * Fail commands. session lock held and recv side suspended and xmit
1857 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1859 struct iscsi_task *task;
1862 for (i = 0; i < conn->session->cmds_max; i++) {
1863 task = conn->session->cmds[i];
1864 if (!task->sc || task->state == ISCSI_TASK_FREE)
1867 if (lun != -1 && lun != task->sc->device->lun)
1870 ISCSI_DBG_SESSION(conn->session,
1871 "failing sc %p itt 0x%x state %d\n",
1872 task->sc, task->itt, task->state);
1873 fail_scsi_task(task, error);
1878 * iscsi_suspend_queue - suspend iscsi_queuecommand
1879 * @conn: iscsi conn to stop queueing IO on
1881 * This grabs the session frwd_lock to make sure no one is in
1882 * xmit_task/queuecommand, and then sets suspend to prevent
1883 * new commands from being queued. This only needs to be called
1884 * by offload drivers that need to sync a path like ep disconnect
1885 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1886 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1888 void iscsi_suspend_queue(struct iscsi_conn *conn)
1890 spin_lock_bh(&conn->session->frwd_lock);
1891 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1892 spin_unlock_bh(&conn->session->frwd_lock);
1894 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1897 * iscsi_suspend_tx - suspend iscsi_data_xmit
1898 * @conn: iscsi conn tp stop processing IO on.
1900 * This function sets the suspend bit to prevent iscsi_data_xmit
1901 * from sending new IO, and if work is queued on the xmit thread
1902 * it will wait for it to be completed.
1904 void iscsi_suspend_tx(struct iscsi_conn *conn)
1906 struct Scsi_Host *shost = conn->session->host;
1907 struct iscsi_host *ihost = shost_priv(shost);
1909 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1911 flush_workqueue(ihost->workq);
1913 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1915 static void iscsi_start_tx(struct iscsi_conn *conn)
1917 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1918 iscsi_conn_queue_work(conn);
1922 * We want to make sure a ping is in flight. It has timed out.
1923 * And we are not busy processing a pdu that is making
1924 * progress but got started before the ping and is taking a while
1925 * to complete so the ping is just stuck behind it in a queue.
1927 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1929 if (READ_ONCE(conn->ping_task) &&
1930 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1931 (conn->ping_timeout * HZ), jiffies))
1937 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1939 enum blk_eh_timer_return rc = BLK_EH_DONE;
1940 struct iscsi_task *task = NULL, *running_task;
1941 struct iscsi_cls_session *cls_session;
1942 struct iscsi_session *session;
1943 struct iscsi_conn *conn;
1946 cls_session = starget_to_session(scsi_target(sc->device));
1947 session = cls_session->dd_data;
1949 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1951 spin_lock_bh(&session->frwd_lock);
1952 task = (struct iscsi_task *)sc->SCp.ptr;
1955 * Raced with completion. Blk layer has taken ownership
1956 * so let timeout code complete it now.
1962 if (session->state != ISCSI_STATE_LOGGED_IN) {
1964 * During shutdown, if session is prematurely disconnected,
1965 * recovery won't happen and there will be hung cmds. Not
1966 * handling cmds would trigger EH, also bad in this case.
1967 * Instead, handle cmd, allow completion to happen and let
1968 * upper layer to deal with the result.
1970 if (unlikely(system_state != SYSTEM_RUNNING)) {
1971 sc->result = DID_NO_CONNECT << 16;
1972 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
1977 * We are probably in the middle of iscsi recovery so let
1978 * that complete and handle the error.
1980 rc = BLK_EH_RESET_TIMER;
1984 conn = session->leadconn;
1986 /* In the middle of shuting down */
1987 rc = BLK_EH_RESET_TIMER;
1992 * If we have sent (at least queued to the network layer) a pdu or
1993 * recvd one for the task since the last timeout ask for
1994 * more time. If on the next timeout we have not made progress
1995 * we can check if it is the task or connection when we send the
1998 if (time_after(task->last_xfer, task->last_timeout)) {
1999 ISCSI_DBG_EH(session, "Command making progress. Asking "
2000 "scsi-ml for more time to complete. "
2001 "Last data xfer at %lu. Last timeout was at "
2002 "%lu\n.", task->last_xfer, task->last_timeout);
2003 task->have_checked_conn = false;
2004 rc = BLK_EH_RESET_TIMER;
2008 if (!conn->recv_timeout && !conn->ping_timeout)
2011 * if the ping timedout then we are in the middle of cleaning up
2012 * and can let the iscsi eh handle it
2014 if (iscsi_has_ping_timed_out(conn)) {
2015 rc = BLK_EH_RESET_TIMER;
2019 for (i = 0; i < conn->session->cmds_max; i++) {
2020 running_task = conn->session->cmds[i];
2021 if (!running_task->sc || running_task == task ||
2022 running_task->state != ISCSI_TASK_RUNNING)
2026 * Only check if cmds started before this one have made
2027 * progress, or this could never fail
2029 if (time_after(running_task->sc->jiffies_at_alloc,
2030 task->sc->jiffies_at_alloc))
2033 if (time_after(running_task->last_xfer, task->last_timeout)) {
2035 * This task has not made progress, but a task
2036 * started before us has transferred data since
2037 * we started/last-checked. We could be queueing
2038 * too many tasks or the LU is bad.
2040 * If the device is bad the cmds ahead of us on
2041 * other devs will complete, and this loop will
2042 * eventually fail starting the scsi eh.
2044 ISCSI_DBG_EH(session, "Command has not made progress "
2045 "but commands ahead of it have. "
2046 "Asking scsi-ml for more time to "
2047 "complete. Our last xfer vs running task "
2048 "last xfer %lu/%lu. Last check %lu.\n",
2049 task->last_xfer, running_task->last_xfer,
2050 task->last_timeout);
2051 rc = BLK_EH_RESET_TIMER;
2056 /* Assumes nop timeout is shorter than scsi cmd timeout */
2057 if (task->have_checked_conn)
2061 * Checking the transport already or nop from a cmd timeout still
2064 if (READ_ONCE(conn->ping_task)) {
2065 task->have_checked_conn = true;
2066 rc = BLK_EH_RESET_TIMER;
2070 /* Make sure there is a transport check done */
2071 iscsi_send_nopout(conn, NULL);
2072 task->have_checked_conn = true;
2073 rc = BLK_EH_RESET_TIMER;
2077 task->last_timeout = jiffies;
2078 spin_unlock_bh(&session->frwd_lock);
2079 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2080 "timer reset" : "shutdown or nh");
2083 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2085 static void iscsi_check_transport_timeouts(struct timer_list *t)
2087 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2088 struct iscsi_session *session = conn->session;
2089 unsigned long recv_timeout, next_timeout = 0, last_recv;
2091 spin_lock(&session->frwd_lock);
2092 if (session->state != ISCSI_STATE_LOGGED_IN)
2095 recv_timeout = conn->recv_timeout;
2100 last_recv = conn->last_recv;
2102 if (iscsi_has_ping_timed_out(conn)) {
2103 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2104 "expired, recv timeout %d, last rx %lu, "
2105 "last ping %lu, now %lu\n",
2106 conn->ping_timeout, conn->recv_timeout,
2107 last_recv, conn->last_ping, jiffies);
2108 spin_unlock(&session->frwd_lock);
2109 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2113 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2114 /* send a ping to try to provoke some traffic */
2115 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2116 if (iscsi_send_nopout(conn, NULL))
2117 next_timeout = jiffies + (1 * HZ);
2119 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2121 next_timeout = last_recv + recv_timeout;
2123 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2124 mod_timer(&conn->transport_timer, next_timeout);
2126 spin_unlock(&session->frwd_lock);
2130 * iscsi_conn_unbind - prevent queueing to conn.
2131 * @cls_conn: iscsi conn ep is bound to.
2132 * @is_active: is the conn in use for boot or is this for EH/termination
2134 * This must be called by drivers implementing the ep_disconnect callout.
2135 * It disables queueing to the connection from libiscsi in preparation for
2136 * an ep_disconnect call.
2138 void iscsi_conn_unbind(struct iscsi_cls_conn *cls_conn, bool is_active)
2140 struct iscsi_session *session;
2141 struct iscsi_conn *conn;
2146 conn = cls_conn->dd_data;
2147 session = conn->session;
2149 * Wait for iscsi_eh calls to exit. We don't wait for the tmf to
2150 * complete or timeout. The caller just wants to know what's running
2151 * is everything that needs to be cleaned up, and no cmds will be
2154 mutex_lock(&session->eh_mutex);
2156 iscsi_suspend_queue(conn);
2157 iscsi_suspend_tx(conn);
2159 spin_lock_bh(&session->frwd_lock);
2162 * if logout timed out before userspace could even send a PDU
2163 * the state might still be in ISCSI_STATE_LOGGED_IN and
2164 * allowing new cmds and TMFs.
2166 if (session->state == ISCSI_STATE_LOGGED_IN)
2167 iscsi_set_conn_failed(conn);
2169 spin_unlock_bh(&session->frwd_lock);
2170 mutex_unlock(&session->eh_mutex);
2172 EXPORT_SYMBOL_GPL(iscsi_conn_unbind);
2174 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2175 struct iscsi_tm *hdr)
2177 memset(hdr, 0, sizeof(*hdr));
2178 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2179 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2180 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2181 hdr->lun = task->lun;
2182 hdr->rtt = task->hdr_itt;
2183 hdr->refcmdsn = task->cmdsn;
2186 int iscsi_eh_abort(struct scsi_cmnd *sc)
2188 struct iscsi_cls_session *cls_session;
2189 struct iscsi_session *session;
2190 struct iscsi_conn *conn;
2191 struct iscsi_task *task;
2192 struct iscsi_tm *hdr;
2195 cls_session = starget_to_session(scsi_target(sc->device));
2196 session = cls_session->dd_data;
2198 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2200 mutex_lock(&session->eh_mutex);
2201 spin_lock_bh(&session->frwd_lock);
2203 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2207 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2209 spin_unlock_bh(&session->frwd_lock);
2210 mutex_unlock(&session->eh_mutex);
2215 * If we are not logged in or we have started a new session
2216 * then let the host reset code handle this
2218 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2219 sc->SCp.phase != session->age) {
2220 spin_unlock_bh(&session->frwd_lock);
2221 mutex_unlock(&session->eh_mutex);
2222 ISCSI_DBG_EH(session, "failing abort due to dropped "
2227 conn = session->leadconn;
2228 conn->eh_abort_cnt++;
2231 task = (struct iscsi_task *)sc->SCp.ptr;
2232 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2235 /* task completed before time out */
2237 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2241 if (task->state == ISCSI_TASK_PENDING) {
2242 fail_scsi_task(task, DID_ABORT);
2246 /* only have one tmf outstanding at a time */
2247 if (session->tmf_state != TMF_INITIAL)
2249 session->tmf_state = TMF_QUEUED;
2251 hdr = &session->tmhdr;
2252 iscsi_prep_abort_task_pdu(task, hdr);
2254 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2257 switch (session->tmf_state) {
2259 spin_unlock_bh(&session->frwd_lock);
2261 * stop tx side incase the target had sent a abort rsp but
2262 * the initiator was still writing out data.
2264 iscsi_suspend_tx(conn);
2266 * we do not stop the recv side because targets have been
2267 * good and have never sent us a successful tmf response
2268 * then sent more data for the cmd.
2270 spin_lock_bh(&session->frwd_lock);
2271 fail_scsi_task(task, DID_ABORT);
2272 session->tmf_state = TMF_INITIAL;
2273 memset(hdr, 0, sizeof(*hdr));
2274 spin_unlock_bh(&session->frwd_lock);
2275 iscsi_start_tx(conn);
2276 goto success_unlocked;
2278 spin_unlock_bh(&session->frwd_lock);
2279 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2280 goto failed_unlocked;
2283 session->tmf_state = TMF_INITIAL;
2284 memset(hdr, 0, sizeof(*hdr));
2285 /* task completed before tmf abort response */
2286 ISCSI_DBG_EH(session, "sc completed while abort in "
2292 session->tmf_state = TMF_INITIAL;
2297 spin_unlock_bh(&session->frwd_lock);
2299 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2301 mutex_unlock(&session->eh_mutex);
2305 spin_unlock_bh(&session->frwd_lock);
2307 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2308 task ? task->itt : 0);
2309 mutex_unlock(&session->eh_mutex);
2312 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2314 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2316 memset(hdr, 0, sizeof(*hdr));
2317 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2318 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2319 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2320 int_to_scsilun(sc->device->lun, &hdr->lun);
2321 hdr->rtt = RESERVED_ITT;
2324 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2326 struct iscsi_cls_session *cls_session;
2327 struct iscsi_session *session;
2328 struct iscsi_conn *conn;
2329 struct iscsi_tm *hdr;
2332 cls_session = starget_to_session(scsi_target(sc->device));
2333 session = cls_session->dd_data;
2335 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2338 mutex_lock(&session->eh_mutex);
2339 spin_lock_bh(&session->frwd_lock);
2341 * Just check if we are not logged in. We cannot check for
2342 * the phase because the reset could come from a ioctl.
2344 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2346 conn = session->leadconn;
2348 /* only have one tmf outstanding at a time */
2349 if (session->tmf_state != TMF_INITIAL)
2351 session->tmf_state = TMF_QUEUED;
2353 hdr = &session->tmhdr;
2354 iscsi_prep_lun_reset_pdu(sc, hdr);
2356 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2357 session->lu_reset_timeout)) {
2362 switch (session->tmf_state) {
2366 spin_unlock_bh(&session->frwd_lock);
2367 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2370 session->tmf_state = TMF_INITIAL;
2375 spin_unlock_bh(&session->frwd_lock);
2377 iscsi_suspend_tx(conn);
2379 spin_lock_bh(&session->frwd_lock);
2380 memset(hdr, 0, sizeof(*hdr));
2381 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2382 session->tmf_state = TMF_INITIAL;
2383 spin_unlock_bh(&session->frwd_lock);
2385 iscsi_start_tx(conn);
2389 spin_unlock_bh(&session->frwd_lock);
2391 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2392 rc == SUCCESS ? "SUCCESS" : "FAILED");
2393 mutex_unlock(&session->eh_mutex);
2396 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2398 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2400 struct iscsi_session *session = cls_session->dd_data;
2402 spin_lock_bh(&session->frwd_lock);
2403 if (session->state != ISCSI_STATE_LOGGED_IN) {
2404 session->state = ISCSI_STATE_RECOVERY_FAILED;
2405 wake_up(&session->ehwait);
2407 spin_unlock_bh(&session->frwd_lock);
2409 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2412 * iscsi_eh_session_reset - drop session and attempt relogin
2415 * This function will wait for a relogin, session termination from
2416 * userspace, or a recovery/replacement timeout.
2418 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2420 struct iscsi_cls_session *cls_session;
2421 struct iscsi_session *session;
2422 struct iscsi_conn *conn;
2424 cls_session = starget_to_session(scsi_target(sc->device));
2425 session = cls_session->dd_data;
2426 conn = session->leadconn;
2428 mutex_lock(&session->eh_mutex);
2429 spin_lock_bh(&session->frwd_lock);
2430 if (session->state == ISCSI_STATE_TERMINATE) {
2432 ISCSI_DBG_EH(session,
2433 "failing session reset: Could not log back into "
2434 "%s [age %d]\n", session->targetname,
2436 spin_unlock_bh(&session->frwd_lock);
2437 mutex_unlock(&session->eh_mutex);
2441 spin_unlock_bh(&session->frwd_lock);
2442 mutex_unlock(&session->eh_mutex);
2444 * we drop the lock here but the leadconn cannot be destoyed while
2445 * we are in the scsi eh
2447 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2449 ISCSI_DBG_EH(session, "wait for relogin\n");
2450 wait_event_interruptible(session->ehwait,
2451 session->state == ISCSI_STATE_TERMINATE ||
2452 session->state == ISCSI_STATE_LOGGED_IN ||
2453 session->state == ISCSI_STATE_RECOVERY_FAILED);
2454 if (signal_pending(current))
2455 flush_signals(current);
2457 mutex_lock(&session->eh_mutex);
2458 spin_lock_bh(&session->frwd_lock);
2459 if (session->state == ISCSI_STATE_LOGGED_IN) {
2460 ISCSI_DBG_EH(session,
2461 "session reset succeeded for %s,%s\n",
2462 session->targetname, conn->persistent_address);
2465 spin_unlock_bh(&session->frwd_lock);
2466 mutex_unlock(&session->eh_mutex);
2469 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2471 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2473 memset(hdr, 0, sizeof(*hdr));
2474 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2475 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2476 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2477 hdr->rtt = RESERVED_ITT;
2481 * iscsi_eh_target_reset - reset target
2484 * This will attempt to send a warm target reset.
2486 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2488 struct iscsi_cls_session *cls_session;
2489 struct iscsi_session *session;
2490 struct iscsi_conn *conn;
2491 struct iscsi_tm *hdr;
2494 cls_session = starget_to_session(scsi_target(sc->device));
2495 session = cls_session->dd_data;
2497 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2498 session->targetname);
2500 mutex_lock(&session->eh_mutex);
2501 spin_lock_bh(&session->frwd_lock);
2503 * Just check if we are not logged in. We cannot check for
2504 * the phase because the reset could come from a ioctl.
2506 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2508 conn = session->leadconn;
2510 /* only have one tmf outstanding at a time */
2511 if (session->tmf_state != TMF_INITIAL)
2513 session->tmf_state = TMF_QUEUED;
2515 hdr = &session->tmhdr;
2516 iscsi_prep_tgt_reset_pdu(sc, hdr);
2518 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2519 session->tgt_reset_timeout)) {
2524 switch (session->tmf_state) {
2528 spin_unlock_bh(&session->frwd_lock);
2529 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2532 session->tmf_state = TMF_INITIAL;
2537 spin_unlock_bh(&session->frwd_lock);
2539 iscsi_suspend_tx(conn);
2541 spin_lock_bh(&session->frwd_lock);
2542 memset(hdr, 0, sizeof(*hdr));
2543 fail_scsi_tasks(conn, -1, DID_ERROR);
2544 session->tmf_state = TMF_INITIAL;
2545 spin_unlock_bh(&session->frwd_lock);
2547 iscsi_start_tx(conn);
2551 spin_unlock_bh(&session->frwd_lock);
2553 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2554 rc == SUCCESS ? "SUCCESS" : "FAILED");
2555 mutex_unlock(&session->eh_mutex);
2560 * iscsi_eh_recover_target - reset target and possibly the session
2563 * This will attempt to send a warm target reset. If that fails,
2564 * we will escalate to ERL0 session recovery.
2566 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2570 rc = iscsi_eh_target_reset(sc);
2572 rc = iscsi_eh_session_reset(sc);
2575 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2578 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2579 * should be accessed via kfifo_{get,put} on q->queue.
2580 * Optionally, the caller can obtain the array of object pointers
2581 * by passing in a non-NULL @items pointer
2584 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2586 int i, num_arrays = 1;
2588 memset(q, 0, sizeof(*q));
2592 /* If the user passed an items pointer, he wants a copy of
2596 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2597 if (q->pool == NULL)
2600 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2602 for (i = 0; i < max; i++) {
2603 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2604 if (q->pool[i] == NULL) {
2608 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2612 *items = q->pool + max;
2613 memcpy(*items, q->pool, max * sizeof(void *));
2622 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2624 void iscsi_pool_free(struct iscsi_pool *q)
2628 for (i = 0; i < q->max; i++)
2632 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2635 * iscsi_host_add - add host to system
2637 * @pdev: parent device
2639 * This should be called by partial offload and software iscsi drivers
2640 * to add a host to the system.
2642 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2644 if (!shost->can_queue)
2645 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2647 if (!shost->cmd_per_lun)
2648 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2650 return scsi_add_host(shost, pdev);
2652 EXPORT_SYMBOL_GPL(iscsi_host_add);
2655 * iscsi_host_alloc - allocate a host and driver data
2656 * @sht: scsi host template
2657 * @dd_data_size: driver host data size
2658 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2660 * This should be called by partial offload and software iscsi drivers.
2661 * To access the driver specific memory use the iscsi_host_priv() macro.
2663 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2664 int dd_data_size, bool xmit_can_sleep)
2666 struct Scsi_Host *shost;
2667 struct iscsi_host *ihost;
2669 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2672 ihost = shost_priv(shost);
2674 if (xmit_can_sleep) {
2675 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2676 "iscsi_q_%d", shost->host_no);
2677 ihost->workq = alloc_workqueue("%s",
2678 WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
2679 1, ihost->workq_name);
2684 spin_lock_init(&ihost->lock);
2685 ihost->state = ISCSI_HOST_SETUP;
2686 ihost->num_sessions = 0;
2687 init_waitqueue_head(&ihost->session_removal_wq);
2691 scsi_host_put(shost);
2694 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2696 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2698 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2702 * iscsi_host_remove - remove host and sessions
2705 * If there are any sessions left, this will initiate the removal and wait
2706 * for the completion.
2708 void iscsi_host_remove(struct Scsi_Host *shost)
2710 struct iscsi_host *ihost = shost_priv(shost);
2711 unsigned long flags;
2713 spin_lock_irqsave(&ihost->lock, flags);
2714 ihost->state = ISCSI_HOST_REMOVED;
2715 spin_unlock_irqrestore(&ihost->lock, flags);
2717 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2718 wait_event_interruptible(ihost->session_removal_wq,
2719 ihost->num_sessions == 0);
2720 if (signal_pending(current))
2721 flush_signals(current);
2723 scsi_remove_host(shost);
2725 destroy_workqueue(ihost->workq);
2727 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2729 void iscsi_host_free(struct Scsi_Host *shost)
2731 struct iscsi_host *ihost = shost_priv(shost);
2733 kfree(ihost->netdev);
2734 kfree(ihost->hwaddress);
2735 kfree(ihost->initiatorname);
2736 scsi_host_put(shost);
2738 EXPORT_SYMBOL_GPL(iscsi_host_free);
2740 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2742 struct iscsi_host *ihost = shost_priv(shost);
2743 unsigned long flags;
2745 shost = scsi_host_get(shost);
2747 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2748 "of session teardown event because host already "
2753 spin_lock_irqsave(&ihost->lock, flags);
2754 ihost->num_sessions--;
2755 if (ihost->num_sessions == 0)
2756 wake_up(&ihost->session_removal_wq);
2757 spin_unlock_irqrestore(&ihost->lock, flags);
2758 scsi_host_put(shost);
2762 * iscsi_session_setup - create iscsi cls session and host and session
2763 * @iscsit: iscsi transport template
2765 * @cmds_max: session can queue
2766 * @dd_size: private driver data size, added to session allocation size
2767 * @cmd_task_size: LLD task private data size
2768 * @initial_cmdsn: initial CmdSN
2769 * @id: target ID to add to this session
2771 * This can be used by software iscsi_transports that allocate
2772 * a session per scsi host.
2774 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2775 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2776 * for nop handling and login/logout requests.
2778 struct iscsi_cls_session *
2779 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2780 uint16_t cmds_max, int dd_size, int cmd_task_size,
2781 uint32_t initial_cmdsn, unsigned int id)
2783 struct iscsi_host *ihost = shost_priv(shost);
2784 struct iscsi_session *session;
2785 struct iscsi_cls_session *cls_session;
2786 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2787 unsigned long flags;
2789 spin_lock_irqsave(&ihost->lock, flags);
2790 if (ihost->state == ISCSI_HOST_REMOVED) {
2791 spin_unlock_irqrestore(&ihost->lock, flags);
2794 ihost->num_sessions++;
2795 spin_unlock_irqrestore(&ihost->lock, flags);
2798 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2800 * The iscsi layer needs some tasks for nop handling and tmfs,
2801 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2802 * + 1 command for scsi IO.
2804 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2805 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2806 "must be a power of two that is at least %d.\n",
2807 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2808 goto dec_session_count;
2811 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2812 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2813 "must be a power of 2 less than or equal to %d.\n",
2814 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2815 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2818 if (!is_power_of_2(total_cmds)) {
2819 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2820 "must be a power of 2.\n", total_cmds);
2821 total_cmds = rounddown_pow_of_two(total_cmds);
2822 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2823 goto dec_session_count;
2824 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2827 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2829 cls_session = iscsi_alloc_session(shost, iscsit,
2830 sizeof(struct iscsi_session) +
2833 goto dec_session_count;
2834 session = cls_session->dd_data;
2835 session->cls_session = cls_session;
2836 session->host = shost;
2837 session->state = ISCSI_STATE_FREE;
2838 session->fast_abort = 1;
2839 session->tgt_reset_timeout = 30;
2840 session->lu_reset_timeout = 15;
2841 session->abort_timeout = 10;
2842 session->scsi_cmds_max = scsi_cmds;
2843 session->cmds_max = total_cmds;
2844 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2845 session->exp_cmdsn = initial_cmdsn + 1;
2846 session->max_cmdsn = initial_cmdsn + 1;
2847 session->max_r2t = 1;
2848 session->tt = iscsit;
2849 session->dd_data = cls_session->dd_data + sizeof(*session);
2851 session->tmf_state = TMF_INITIAL;
2852 timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
2853 mutex_init(&session->eh_mutex);
2855 spin_lock_init(&session->frwd_lock);
2856 spin_lock_init(&session->back_lock);
2858 /* initialize SCSI PDU commands pool */
2859 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2860 (void***)&session->cmds,
2861 cmd_task_size + sizeof(struct iscsi_task)))
2862 goto cmdpool_alloc_fail;
2864 /* pre-format cmds pool with ITT */
2865 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2866 struct iscsi_task *task = session->cmds[cmd_i];
2869 task->dd_data = &task[1];
2871 task->state = ISCSI_TASK_FREE;
2872 INIT_LIST_HEAD(&task->running);
2875 if (!try_module_get(iscsit->owner))
2876 goto module_get_fail;
2878 if (iscsi_add_session(cls_session, id))
2879 goto cls_session_fail;
2884 module_put(iscsit->owner);
2886 iscsi_pool_free(&session->cmdpool);
2888 iscsi_free_session(cls_session);
2890 iscsi_host_dec_session_cnt(shost);
2893 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2896 * iscsi_session_teardown - destroy session, host, and cls_session
2897 * @cls_session: iscsi session
2899 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2901 struct iscsi_session *session = cls_session->dd_data;
2902 struct module *owner = cls_session->transport->owner;
2903 struct Scsi_Host *shost = session->host;
2905 iscsi_pool_free(&session->cmdpool);
2907 iscsi_remove_session(cls_session);
2909 kfree(session->password);
2910 kfree(session->password_in);
2911 kfree(session->username);
2912 kfree(session->username_in);
2913 kfree(session->targetname);
2914 kfree(session->targetalias);
2915 kfree(session->initiatorname);
2916 kfree(session->boot_root);
2917 kfree(session->boot_nic);
2918 kfree(session->boot_target);
2919 kfree(session->ifacename);
2920 kfree(session->portal_type);
2921 kfree(session->discovery_parent_type);
2923 iscsi_free_session(cls_session);
2925 iscsi_host_dec_session_cnt(shost);
2928 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2931 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2932 * @cls_session: iscsi_cls_session
2933 * @dd_size: private driver data size
2936 struct iscsi_cls_conn *
2937 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2940 struct iscsi_session *session = cls_session->dd_data;
2941 struct iscsi_conn *conn;
2942 struct iscsi_cls_conn *cls_conn;
2945 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2949 conn = cls_conn->dd_data;
2950 memset(conn, 0, sizeof(*conn) + dd_size);
2952 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2953 conn->session = session;
2954 conn->cls_conn = cls_conn;
2955 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2956 conn->id = conn_idx;
2957 conn->exp_statsn = 0;
2959 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2961 INIT_LIST_HEAD(&conn->mgmtqueue);
2962 INIT_LIST_HEAD(&conn->cmdqueue);
2963 INIT_LIST_HEAD(&conn->requeue);
2964 spin_lock_init(&conn->taskqueuelock);
2965 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2967 /* allocate login_task used for the login/text sequences */
2968 spin_lock_bh(&session->frwd_lock);
2969 if (!kfifo_out(&session->cmdpool.queue,
2970 (void*)&conn->login_task,
2972 spin_unlock_bh(&session->frwd_lock);
2973 goto login_task_alloc_fail;
2975 spin_unlock_bh(&session->frwd_lock);
2977 data = (char *) __get_free_pages(GFP_KERNEL,
2978 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2980 goto login_task_data_alloc_fail;
2981 conn->login_task->data = conn->data = data;
2983 init_waitqueue_head(&session->ehwait);
2987 login_task_data_alloc_fail:
2988 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2990 login_task_alloc_fail:
2991 iscsi_destroy_conn(cls_conn);
2994 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2997 * iscsi_conn_teardown - teardown iscsi connection
2998 * @cls_conn: iscsi class connection
3000 * TODO: we may need to make this into a two step process
3001 * like scsi-mls remove + put host
3003 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
3005 struct iscsi_conn *conn = cls_conn->dd_data;
3006 struct iscsi_session *session = conn->session;
3007 char *tmp_persistent_address = conn->persistent_address;
3008 char *tmp_local_ipaddr = conn->local_ipaddr;
3010 del_timer_sync(&conn->transport_timer);
3012 mutex_lock(&session->eh_mutex);
3013 spin_lock_bh(&session->frwd_lock);
3014 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
3015 if (session->leadconn == conn) {
3017 * leading connection? then give up on recovery.
3019 session->state = ISCSI_STATE_TERMINATE;
3020 wake_up(&session->ehwait);
3022 spin_unlock_bh(&session->frwd_lock);
3024 /* flush queued up work because we free the connection below */
3025 iscsi_suspend_tx(conn);
3027 spin_lock_bh(&session->frwd_lock);
3028 free_pages((unsigned long) conn->data,
3029 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3030 /* regular RX path uses back_lock */
3031 spin_lock_bh(&session->back_lock);
3032 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3034 spin_unlock_bh(&session->back_lock);
3035 if (session->leadconn == conn)
3036 session->leadconn = NULL;
3037 spin_unlock_bh(&session->frwd_lock);
3038 mutex_unlock(&session->eh_mutex);
3040 iscsi_destroy_conn(cls_conn);
3041 kfree(tmp_persistent_address);
3042 kfree(tmp_local_ipaddr);
3044 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
3046 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
3048 struct iscsi_conn *conn = cls_conn->dd_data;
3049 struct iscsi_session *session = conn->session;
3052 iscsi_conn_printk(KERN_ERR, conn,
3053 "can't start unbound connection\n");
3057 if ((session->imm_data_en || !session->initial_r2t_en) &&
3058 session->first_burst > session->max_burst) {
3059 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3060 "first_burst %d max_burst %d\n",
3061 session->first_burst, session->max_burst);
3065 if (conn->ping_timeout && !conn->recv_timeout) {
3066 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3067 "zero. Using 5 seconds\n.");
3068 conn->recv_timeout = 5;
3071 if (conn->recv_timeout && !conn->ping_timeout) {
3072 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3073 "zero. Using 5 seconds.\n");
3074 conn->ping_timeout = 5;
3077 spin_lock_bh(&session->frwd_lock);
3078 conn->c_stage = ISCSI_CONN_STARTED;
3079 session->state = ISCSI_STATE_LOGGED_IN;
3080 session->queued_cmdsn = session->cmdsn;
3082 conn->last_recv = jiffies;
3083 conn->last_ping = jiffies;
3084 if (conn->recv_timeout && conn->ping_timeout)
3085 mod_timer(&conn->transport_timer,
3086 jiffies + (conn->recv_timeout * HZ));
3088 switch(conn->stop_stage) {
3089 case STOP_CONN_RECOVER:
3091 * unblock eh_abort() if it is blocked. re-try all
3092 * commands after successful recovery
3094 conn->stop_stage = 0;
3095 session->tmf_state = TMF_INITIAL;
3097 if (session->age == 16)
3100 case STOP_CONN_TERM:
3101 conn->stop_stage = 0;
3106 spin_unlock_bh(&session->frwd_lock);
3108 iscsi_unblock_session(session->cls_session);
3109 wake_up(&session->ehwait);
3112 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3115 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3117 struct iscsi_task *task;
3120 for (i = 0; i < conn->session->cmds_max; i++) {
3121 task = conn->session->cmds[i];
3125 if (task->state == ISCSI_TASK_FREE)
3128 ISCSI_DBG_SESSION(conn->session,
3129 "failing mgmt itt 0x%x state %d\n",
3130 task->itt, task->state);
3131 state = ISCSI_TASK_ABRT_SESS_RECOV;
3132 if (task->state == ISCSI_TASK_PENDING)
3133 state = ISCSI_TASK_COMPLETED;
3134 spin_lock_bh(&session->back_lock);
3135 iscsi_complete_task(task, state);
3136 spin_unlock_bh(&session->back_lock);
3140 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3142 struct iscsi_conn *conn = cls_conn->dd_data;
3143 struct iscsi_session *session = conn->session;
3146 mutex_lock(&session->eh_mutex);
3147 spin_lock_bh(&session->frwd_lock);
3148 if (conn->stop_stage == STOP_CONN_TERM) {
3149 spin_unlock_bh(&session->frwd_lock);
3150 mutex_unlock(&session->eh_mutex);
3155 * When this is called for the in_login state, we only want to clean
3156 * up the login task and connection. We do not need to block and set
3157 * the recovery state again
3159 if (flag == STOP_CONN_TERM)
3160 session->state = ISCSI_STATE_TERMINATE;
3161 else if (conn->stop_stage != STOP_CONN_RECOVER)
3162 session->state = ISCSI_STATE_IN_RECOVERY;
3164 old_stop_stage = conn->stop_stage;
3165 conn->stop_stage = flag;
3166 spin_unlock_bh(&session->frwd_lock);
3168 del_timer_sync(&conn->transport_timer);
3169 iscsi_suspend_tx(conn);
3171 spin_lock_bh(&session->frwd_lock);
3172 conn->c_stage = ISCSI_CONN_STOPPED;
3173 spin_unlock_bh(&session->frwd_lock);
3176 * for connection level recovery we should not calculate
3177 * header digest. conn->hdr_size used for optimization
3178 * in hdr_extract() and will be re-negotiated at
3181 if (flag == STOP_CONN_RECOVER) {
3182 conn->hdrdgst_en = 0;
3183 conn->datadgst_en = 0;
3184 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3185 old_stop_stage != STOP_CONN_RECOVER) {
3186 ISCSI_DBG_SESSION(session, "blocking session\n");
3187 iscsi_block_session(session->cls_session);
3194 spin_lock_bh(&session->frwd_lock);
3195 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3196 fail_mgmt_tasks(session, conn);
3197 memset(&session->tmhdr, 0, sizeof(session->tmhdr));
3198 spin_unlock_bh(&session->frwd_lock);
3199 mutex_unlock(&session->eh_mutex);
3201 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3203 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3204 struct iscsi_cls_conn *cls_conn, int is_leading)
3206 struct iscsi_session *session = cls_session->dd_data;
3207 struct iscsi_conn *conn = cls_conn->dd_data;
3209 spin_lock_bh(&session->frwd_lock);
3211 session->leadconn = conn;
3212 spin_unlock_bh(&session->frwd_lock);
3215 * Unblock xmitworker(), Login Phase will pass through.
3217 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3218 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3221 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3223 int iscsi_switch_str_param(char **param, char *new_val_buf)
3228 if (!strcmp(*param, new_val_buf))
3232 new_val = kstrdup(new_val_buf, GFP_NOIO);
3240 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3242 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3243 enum iscsi_param param, char *buf, int buflen)
3245 struct iscsi_conn *conn = cls_conn->dd_data;
3246 struct iscsi_session *session = conn->session;
3250 case ISCSI_PARAM_FAST_ABORT:
3251 sscanf(buf, "%d", &session->fast_abort);
3253 case ISCSI_PARAM_ABORT_TMO:
3254 sscanf(buf, "%d", &session->abort_timeout);
3256 case ISCSI_PARAM_LU_RESET_TMO:
3257 sscanf(buf, "%d", &session->lu_reset_timeout);
3259 case ISCSI_PARAM_TGT_RESET_TMO:
3260 sscanf(buf, "%d", &session->tgt_reset_timeout);
3262 case ISCSI_PARAM_PING_TMO:
3263 sscanf(buf, "%d", &conn->ping_timeout);
3265 case ISCSI_PARAM_RECV_TMO:
3266 sscanf(buf, "%d", &conn->recv_timeout);
3268 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3269 sscanf(buf, "%d", &conn->max_recv_dlength);
3271 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3272 sscanf(buf, "%d", &conn->max_xmit_dlength);
3274 case ISCSI_PARAM_HDRDGST_EN:
3275 sscanf(buf, "%d", &conn->hdrdgst_en);
3277 case ISCSI_PARAM_DATADGST_EN:
3278 sscanf(buf, "%d", &conn->datadgst_en);
3280 case ISCSI_PARAM_INITIAL_R2T_EN:
3281 sscanf(buf, "%d", &session->initial_r2t_en);
3283 case ISCSI_PARAM_MAX_R2T:
3284 sscanf(buf, "%hu", &session->max_r2t);
3286 case ISCSI_PARAM_IMM_DATA_EN:
3287 sscanf(buf, "%d", &session->imm_data_en);
3289 case ISCSI_PARAM_FIRST_BURST:
3290 sscanf(buf, "%d", &session->first_burst);
3292 case ISCSI_PARAM_MAX_BURST:
3293 sscanf(buf, "%d", &session->max_burst);
3295 case ISCSI_PARAM_PDU_INORDER_EN:
3296 sscanf(buf, "%d", &session->pdu_inorder_en);
3298 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3299 sscanf(buf, "%d", &session->dataseq_inorder_en);
3301 case ISCSI_PARAM_ERL:
3302 sscanf(buf, "%d", &session->erl);
3304 case ISCSI_PARAM_EXP_STATSN:
3305 sscanf(buf, "%u", &conn->exp_statsn);
3307 case ISCSI_PARAM_USERNAME:
3308 return iscsi_switch_str_param(&session->username, buf);
3309 case ISCSI_PARAM_USERNAME_IN:
3310 return iscsi_switch_str_param(&session->username_in, buf);
3311 case ISCSI_PARAM_PASSWORD:
3312 return iscsi_switch_str_param(&session->password, buf);
3313 case ISCSI_PARAM_PASSWORD_IN:
3314 return iscsi_switch_str_param(&session->password_in, buf);
3315 case ISCSI_PARAM_TARGET_NAME:
3316 return iscsi_switch_str_param(&session->targetname, buf);
3317 case ISCSI_PARAM_TARGET_ALIAS:
3318 return iscsi_switch_str_param(&session->targetalias, buf);
3319 case ISCSI_PARAM_TPGT:
3320 sscanf(buf, "%d", &session->tpgt);
3322 case ISCSI_PARAM_PERSISTENT_PORT:
3323 sscanf(buf, "%d", &conn->persistent_port);
3325 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3326 return iscsi_switch_str_param(&conn->persistent_address, buf);
3327 case ISCSI_PARAM_IFACE_NAME:
3328 return iscsi_switch_str_param(&session->ifacename, buf);
3329 case ISCSI_PARAM_INITIATOR_NAME:
3330 return iscsi_switch_str_param(&session->initiatorname, buf);
3331 case ISCSI_PARAM_BOOT_ROOT:
3332 return iscsi_switch_str_param(&session->boot_root, buf);
3333 case ISCSI_PARAM_BOOT_NIC:
3334 return iscsi_switch_str_param(&session->boot_nic, buf);
3335 case ISCSI_PARAM_BOOT_TARGET:
3336 return iscsi_switch_str_param(&session->boot_target, buf);
3337 case ISCSI_PARAM_PORTAL_TYPE:
3338 return iscsi_switch_str_param(&session->portal_type, buf);
3339 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3340 return iscsi_switch_str_param(&session->discovery_parent_type,
3342 case ISCSI_PARAM_DISCOVERY_SESS:
3343 sscanf(buf, "%d", &val);
3344 session->discovery_sess = !!val;
3346 case ISCSI_PARAM_LOCAL_IPADDR:
3347 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3354 EXPORT_SYMBOL_GPL(iscsi_set_param);
3356 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3357 enum iscsi_param param, char *buf)
3359 struct iscsi_session *session = cls_session->dd_data;
3363 case ISCSI_PARAM_FAST_ABORT:
3364 len = sysfs_emit(buf, "%d\n", session->fast_abort);
3366 case ISCSI_PARAM_ABORT_TMO:
3367 len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3369 case ISCSI_PARAM_LU_RESET_TMO:
3370 len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3372 case ISCSI_PARAM_TGT_RESET_TMO:
3373 len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
3375 case ISCSI_PARAM_INITIAL_R2T_EN:
3376 len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3378 case ISCSI_PARAM_MAX_R2T:
3379 len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3381 case ISCSI_PARAM_IMM_DATA_EN:
3382 len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3384 case ISCSI_PARAM_FIRST_BURST:
3385 len = sysfs_emit(buf, "%u\n", session->first_burst);
3387 case ISCSI_PARAM_MAX_BURST:
3388 len = sysfs_emit(buf, "%u\n", session->max_burst);
3390 case ISCSI_PARAM_PDU_INORDER_EN:
3391 len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3393 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3394 len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3396 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3397 len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
3399 case ISCSI_PARAM_ERL:
3400 len = sysfs_emit(buf, "%d\n", session->erl);
3402 case ISCSI_PARAM_TARGET_NAME:
3403 len = sysfs_emit(buf, "%s\n", session->targetname);
3405 case ISCSI_PARAM_TARGET_ALIAS:
3406 len = sysfs_emit(buf, "%s\n", session->targetalias);
3408 case ISCSI_PARAM_TPGT:
3409 len = sysfs_emit(buf, "%d\n", session->tpgt);
3411 case ISCSI_PARAM_USERNAME:
3412 len = sysfs_emit(buf, "%s\n", session->username);
3414 case ISCSI_PARAM_USERNAME_IN:
3415 len = sysfs_emit(buf, "%s\n", session->username_in);
3417 case ISCSI_PARAM_PASSWORD:
3418 len = sysfs_emit(buf, "%s\n", session->password);
3420 case ISCSI_PARAM_PASSWORD_IN:
3421 len = sysfs_emit(buf, "%s\n", session->password_in);
3423 case ISCSI_PARAM_IFACE_NAME:
3424 len = sysfs_emit(buf, "%s\n", session->ifacename);
3426 case ISCSI_PARAM_INITIATOR_NAME:
3427 len = sysfs_emit(buf, "%s\n", session->initiatorname);
3429 case ISCSI_PARAM_BOOT_ROOT:
3430 len = sysfs_emit(buf, "%s\n", session->boot_root);
3432 case ISCSI_PARAM_BOOT_NIC:
3433 len = sysfs_emit(buf, "%s\n", session->boot_nic);
3435 case ISCSI_PARAM_BOOT_TARGET:
3436 len = sysfs_emit(buf, "%s\n", session->boot_target);
3438 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3439 len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3441 case ISCSI_PARAM_DISCOVERY_SESS:
3442 len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3444 case ISCSI_PARAM_PORTAL_TYPE:
3445 len = sysfs_emit(buf, "%s\n", session->portal_type);
3447 case ISCSI_PARAM_CHAP_AUTH_EN:
3448 len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3450 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3451 len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3453 case ISCSI_PARAM_BIDI_CHAP_EN:
3454 len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3456 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3457 len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3459 case ISCSI_PARAM_DEF_TIME2WAIT:
3460 len = sysfs_emit(buf, "%d\n", session->time2wait);
3462 case ISCSI_PARAM_DEF_TIME2RETAIN:
3463 len = sysfs_emit(buf, "%d\n", session->time2retain);
3465 case ISCSI_PARAM_TSID:
3466 len = sysfs_emit(buf, "%u\n", session->tsid);
3468 case ISCSI_PARAM_ISID:
3469 len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3470 session->isid[0], session->isid[1],
3471 session->isid[2], session->isid[3],
3472 session->isid[4], session->isid[5]);
3474 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3475 len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3477 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3478 if (session->discovery_parent_type)
3479 len = sysfs_emit(buf, "%s\n",
3480 session->discovery_parent_type);
3482 len = sysfs_emit(buf, "\n");
3490 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3492 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3493 enum iscsi_param param, char *buf)
3495 struct sockaddr_in6 *sin6 = NULL;
3496 struct sockaddr_in *sin = NULL;
3499 switch (addr->ss_family) {
3501 sin = (struct sockaddr_in *)addr;
3504 sin6 = (struct sockaddr_in6 *)addr;
3511 case ISCSI_PARAM_CONN_ADDRESS:
3512 case ISCSI_HOST_PARAM_IPADDRESS:
3514 len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
3516 len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
3518 case ISCSI_PARAM_CONN_PORT:
3519 case ISCSI_PARAM_LOCAL_PORT:
3521 len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3523 len = sysfs_emit(buf, "%hu\n",
3524 be16_to_cpu(sin6->sin6_port));
3532 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3534 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3535 enum iscsi_param param, char *buf)
3537 struct iscsi_conn *conn = cls_conn->dd_data;
3541 case ISCSI_PARAM_PING_TMO:
3542 len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3544 case ISCSI_PARAM_RECV_TMO:
3545 len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3547 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3548 len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3550 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3551 len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3553 case ISCSI_PARAM_HDRDGST_EN:
3554 len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3556 case ISCSI_PARAM_DATADGST_EN:
3557 len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3559 case ISCSI_PARAM_IFMARKER_EN:
3560 len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3562 case ISCSI_PARAM_OFMARKER_EN:
3563 len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3565 case ISCSI_PARAM_EXP_STATSN:
3566 len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3568 case ISCSI_PARAM_PERSISTENT_PORT:
3569 len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3571 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3572 len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3574 case ISCSI_PARAM_STATSN:
3575 len = sysfs_emit(buf, "%u\n", conn->statsn);
3577 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3578 len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3580 case ISCSI_PARAM_KEEPALIVE_TMO:
3581 len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3583 case ISCSI_PARAM_LOCAL_PORT:
3584 len = sysfs_emit(buf, "%u\n", conn->local_port);
3586 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3587 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3589 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3590 len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3592 case ISCSI_PARAM_TCP_WSF_DISABLE:
3593 len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3595 case ISCSI_PARAM_TCP_TIMER_SCALE:
3596 len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3598 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3599 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3601 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3602 len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3604 case ISCSI_PARAM_IPV4_TOS:
3605 len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3607 case ISCSI_PARAM_IPV6_TC:
3608 len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3610 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3611 len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
3613 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3614 len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3616 case ISCSI_PARAM_TCP_XMIT_WSF:
3617 len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3619 case ISCSI_PARAM_TCP_RECV_WSF:
3620 len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3622 case ISCSI_PARAM_LOCAL_IPADDR:
3623 len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3631 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3633 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3636 struct iscsi_host *ihost = shost_priv(shost);
3640 case ISCSI_HOST_PARAM_NETDEV_NAME:
3641 len = sysfs_emit(buf, "%s\n", ihost->netdev);
3643 case ISCSI_HOST_PARAM_HWADDRESS:
3644 len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
3646 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3647 len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
3655 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3657 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3658 char *buf, int buflen)
3660 struct iscsi_host *ihost = shost_priv(shost);
3663 case ISCSI_HOST_PARAM_NETDEV_NAME:
3664 return iscsi_switch_str_param(&ihost->netdev, buf);
3665 case ISCSI_HOST_PARAM_HWADDRESS:
3666 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3667 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3668 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3675 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3677 MODULE_AUTHOR("Mike Christie");
3678 MODULE_DESCRIPTION("iSCSI library functions");
3679 MODULE_LICENSE("GPL");