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 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1372 struct iscsi_session *session = conn->session;
1374 spin_lock_bh(&session->frwd_lock);
1375 if (session->state == ISCSI_STATE_FAILED) {
1376 spin_unlock_bh(&session->frwd_lock);
1380 if (conn->stop_stage == 0)
1381 session->state = ISCSI_STATE_FAILED;
1382 spin_unlock_bh(&session->frwd_lock);
1384 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1385 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1386 iscsi_conn_error_event(conn->cls_conn, err);
1388 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1390 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1392 struct iscsi_session *session = conn->session;
1395 * Check for iSCSI window and take care of CmdSN wrap-around
1397 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1398 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1399 "%u MaxCmdSN %u CmdSN %u/%u\n",
1400 session->exp_cmdsn, session->max_cmdsn,
1401 session->cmdsn, session->queued_cmdsn);
1407 static int iscsi_xmit_task(struct iscsi_conn *conn)
1409 struct iscsi_task *task = conn->task;
1412 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1415 spin_lock_bh(&conn->session->back_lock);
1416 if (conn->task == NULL) {
1417 spin_unlock_bh(&conn->session->back_lock);
1420 __iscsi_get_task(task);
1421 spin_unlock_bh(&conn->session->back_lock);
1422 spin_unlock_bh(&conn->session->frwd_lock);
1423 rc = conn->session->tt->xmit_task(task);
1424 spin_lock_bh(&conn->session->frwd_lock);
1426 /* done with this task */
1427 task->last_xfer = jiffies;
1430 /* regular RX path uses back_lock */
1431 spin_lock(&conn->session->back_lock);
1432 __iscsi_put_task(task);
1433 spin_unlock(&conn->session->back_lock);
1438 * iscsi_requeue_task - requeue task to run from session workqueue
1439 * @task: task to requeue
1441 * LLDs that need to run a task from the session workqueue should call
1442 * this. The session frwd_lock must be held. This should only be called
1443 * by software drivers.
1445 void iscsi_requeue_task(struct iscsi_task *task)
1447 struct iscsi_conn *conn = task->conn;
1450 * this may be on the requeue list already if the xmit_task callout
1451 * is handling the r2ts while we are adding new ones
1453 spin_lock_bh(&conn->taskqueuelock);
1454 if (list_empty(&task->running))
1455 list_add_tail(&task->running, &conn->requeue);
1456 spin_unlock_bh(&conn->taskqueuelock);
1457 iscsi_conn_queue_work(conn);
1459 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1462 * iscsi_data_xmit - xmit any command into the scheduled connection
1463 * @conn: iscsi connection
1466 * The function can return -EAGAIN in which case the caller must
1467 * re-schedule it again later or recover. '0' return code means
1470 static int iscsi_data_xmit(struct iscsi_conn *conn)
1472 struct iscsi_task *task;
1475 spin_lock_bh(&conn->session->frwd_lock);
1476 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1477 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1478 spin_unlock_bh(&conn->session->frwd_lock);
1483 rc = iscsi_xmit_task(conn);
1489 * process mgmt pdus like nops before commands since we should
1490 * only have one nop-out as a ping from us and targets should not
1491 * overflow us with nop-ins
1493 spin_lock_bh(&conn->taskqueuelock);
1495 while (!list_empty(&conn->mgmtqueue)) {
1496 conn->task = list_entry(conn->mgmtqueue.next,
1497 struct iscsi_task, running);
1498 list_del_init(&conn->task->running);
1499 spin_unlock_bh(&conn->taskqueuelock);
1500 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1501 /* regular RX path uses back_lock */
1502 spin_lock_bh(&conn->session->back_lock);
1503 __iscsi_put_task(conn->task);
1504 spin_unlock_bh(&conn->session->back_lock);
1506 spin_lock_bh(&conn->taskqueuelock);
1509 rc = iscsi_xmit_task(conn);
1512 spin_lock_bh(&conn->taskqueuelock);
1515 /* process pending command queue */
1516 while (!list_empty(&conn->cmdqueue)) {
1517 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1519 list_del_init(&conn->task->running);
1520 spin_unlock_bh(&conn->taskqueuelock);
1521 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1522 fail_scsi_task(conn->task, DID_IMM_RETRY);
1523 spin_lock_bh(&conn->taskqueuelock);
1526 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1528 if (rc == -ENOMEM || rc == -EACCES)
1529 fail_scsi_task(conn->task, DID_IMM_RETRY);
1531 fail_scsi_task(conn->task, DID_ABORT);
1532 spin_lock_bh(&conn->taskqueuelock);
1535 rc = iscsi_xmit_task(conn);
1539 * we could continuously get new task requests so
1540 * we need to check the mgmt queue for nops that need to
1541 * be sent to aviod starvation
1543 spin_lock_bh(&conn->taskqueuelock);
1544 if (!list_empty(&conn->mgmtqueue))
1548 while (!list_empty(&conn->requeue)) {
1550 * we always do fastlogout - conn stop code will clean up.
1552 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1555 task = list_entry(conn->requeue.next, struct iscsi_task,
1557 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1561 list_del_init(&conn->task->running);
1562 conn->task->state = ISCSI_TASK_RUNNING;
1563 spin_unlock_bh(&conn->taskqueuelock);
1564 rc = iscsi_xmit_task(conn);
1567 spin_lock_bh(&conn->taskqueuelock);
1568 if (!list_empty(&conn->mgmtqueue))
1571 spin_unlock_bh(&conn->taskqueuelock);
1572 spin_unlock_bh(&conn->session->frwd_lock);
1576 spin_unlock_bh(&conn->session->frwd_lock);
1580 static void iscsi_xmitworker(struct work_struct *work)
1582 struct iscsi_conn *conn =
1583 container_of(work, struct iscsi_conn, xmitwork);
1586 * serialize Xmit worker on a per-connection basis.
1589 rc = iscsi_data_xmit(conn);
1590 } while (rc >= 0 || rc == -EAGAIN);
1593 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1594 struct scsi_cmnd *sc)
1596 struct iscsi_task *task;
1598 if (!kfifo_out(&conn->session->cmdpool.queue,
1599 (void *) &task, sizeof(void *)))
1602 sc->SCp.phase = conn->session->age;
1603 sc->SCp.ptr = (char *) task;
1605 refcount_set(&task->refcount, 1);
1606 task->state = ISCSI_TASK_PENDING;
1609 task->have_checked_conn = false;
1610 task->last_timeout = jiffies;
1611 task->last_xfer = jiffies;
1612 task->protected = false;
1613 INIT_LIST_HEAD(&task->running);
1618 FAILURE_BAD_HOST = 1,
1619 FAILURE_SESSION_FAILED,
1620 FAILURE_SESSION_FREED,
1621 FAILURE_WINDOW_CLOSED,
1623 FAILURE_SESSION_TERMINATE,
1624 FAILURE_SESSION_IN_RECOVERY,
1625 FAILURE_SESSION_RECOVERY_TIMEOUT,
1626 FAILURE_SESSION_LOGGING_OUT,
1627 FAILURE_SESSION_NOT_READY,
1630 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1632 struct iscsi_cls_session *cls_session;
1633 struct iscsi_host *ihost;
1635 struct iscsi_session *session;
1636 struct iscsi_conn *conn;
1637 struct iscsi_task *task = NULL;
1642 ihost = shost_priv(host);
1644 cls_session = starget_to_session(scsi_target(sc->device));
1645 session = cls_session->dd_data;
1646 spin_lock_bh(&session->frwd_lock);
1648 reason = iscsi_session_chkready(cls_session);
1650 sc->result = reason;
1654 if (session->state != ISCSI_STATE_LOGGED_IN) {
1656 * to handle the race between when we set the recovery state
1657 * and block the session we requeue here (commands could
1658 * be entering our queuecommand while a block is starting
1659 * up because the block code is not locked)
1661 switch (session->state) {
1662 case ISCSI_STATE_FAILED:
1664 * cmds should fail during shutdown, if the session
1665 * state is bad, allowing completion to happen
1667 if (unlikely(system_state != SYSTEM_RUNNING)) {
1668 reason = FAILURE_SESSION_FAILED;
1669 sc->result = DID_NO_CONNECT << 16;
1673 case ISCSI_STATE_IN_RECOVERY:
1674 reason = FAILURE_SESSION_IN_RECOVERY;
1675 sc->result = DID_IMM_RETRY << 16;
1677 case ISCSI_STATE_LOGGING_OUT:
1678 reason = FAILURE_SESSION_LOGGING_OUT;
1679 sc->result = DID_IMM_RETRY << 16;
1681 case ISCSI_STATE_RECOVERY_FAILED:
1682 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1683 sc->result = DID_TRANSPORT_FAILFAST << 16;
1685 case ISCSI_STATE_TERMINATE:
1686 reason = FAILURE_SESSION_TERMINATE;
1687 sc->result = DID_NO_CONNECT << 16;
1690 reason = FAILURE_SESSION_FREED;
1691 sc->result = DID_NO_CONNECT << 16;
1696 conn = session->leadconn;
1698 reason = FAILURE_SESSION_FREED;
1699 sc->result = DID_NO_CONNECT << 16;
1703 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1704 reason = FAILURE_SESSION_IN_RECOVERY;
1705 sc->result = DID_REQUEUE << 16;
1709 if (iscsi_check_cmdsn_window_closed(conn)) {
1710 reason = FAILURE_WINDOW_CLOSED;
1714 task = iscsi_alloc_task(conn, sc);
1716 reason = FAILURE_OOM;
1720 if (!ihost->workq) {
1721 reason = iscsi_prep_scsi_cmd_pdu(task);
1723 if (reason == -ENOMEM || reason == -EACCES) {
1724 reason = FAILURE_OOM;
1727 sc->result = DID_ABORT << 16;
1731 if (session->tt->xmit_task(task)) {
1733 reason = FAILURE_SESSION_NOT_READY;
1737 spin_lock_bh(&conn->taskqueuelock);
1738 list_add_tail(&task->running, &conn->cmdqueue);
1739 spin_unlock_bh(&conn->taskqueuelock);
1740 iscsi_conn_queue_work(conn);
1743 session->queued_cmdsn++;
1744 spin_unlock_bh(&session->frwd_lock);
1748 spin_lock_bh(&session->back_lock);
1749 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1750 spin_unlock_bh(&session->back_lock);
1752 spin_unlock_bh(&session->frwd_lock);
1753 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1754 sc->cmnd[0], reason);
1755 return SCSI_MLQUEUE_TARGET_BUSY;
1758 spin_lock_bh(&session->back_lock);
1759 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1760 spin_unlock_bh(&session->back_lock);
1762 spin_unlock_bh(&session->frwd_lock);
1763 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1764 sc->cmnd[0], reason);
1765 scsi_set_resid(sc, scsi_bufflen(sc));
1769 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1771 int iscsi_target_alloc(struct scsi_target *starget)
1773 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1774 struct iscsi_session *session = cls_session->dd_data;
1776 starget->can_queue = session->scsi_cmds_max;
1779 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1781 static void iscsi_tmf_timedout(struct timer_list *t)
1783 struct iscsi_session *session = from_timer(session, t, tmf_timer);
1785 spin_lock(&session->frwd_lock);
1786 if (session->tmf_state == TMF_QUEUED) {
1787 session->tmf_state = TMF_TIMEDOUT;
1788 ISCSI_DBG_EH(session, "tmf timedout\n");
1789 /* unblock eh_abort() */
1790 wake_up(&session->ehwait);
1792 spin_unlock(&session->frwd_lock);
1795 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1796 struct iscsi_tm *hdr, int age,
1798 __must_hold(&session->frwd_lock)
1800 struct iscsi_session *session = conn->session;
1801 struct iscsi_task *task;
1803 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1806 spin_unlock_bh(&session->frwd_lock);
1807 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1808 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1809 spin_lock_bh(&session->frwd_lock);
1812 conn->tmfcmd_pdus_cnt++;
1813 session->tmf_timer.expires = timeout * HZ + jiffies;
1814 add_timer(&session->tmf_timer);
1815 ISCSI_DBG_EH(session, "tmf set timeout\n");
1817 spin_unlock_bh(&session->frwd_lock);
1818 mutex_unlock(&session->eh_mutex);
1821 * block eh thread until:
1825 * 3) session is terminated or restarted or userspace has
1826 * given up on recovery
1828 wait_event_interruptible(session->ehwait, age != session->age ||
1829 session->state != ISCSI_STATE_LOGGED_IN ||
1830 session->tmf_state != TMF_QUEUED);
1831 if (signal_pending(current))
1832 flush_signals(current);
1833 del_timer_sync(&session->tmf_timer);
1835 mutex_lock(&session->eh_mutex);
1836 spin_lock_bh(&session->frwd_lock);
1837 /* if the session drops it will clean up the task */
1838 if (age != session->age ||
1839 session->state != ISCSI_STATE_LOGGED_IN)
1845 * Fail commands. session lock held and recv side suspended and xmit
1848 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1850 struct iscsi_task *task;
1853 for (i = 0; i < conn->session->cmds_max; i++) {
1854 task = conn->session->cmds[i];
1855 if (!task->sc || task->state == ISCSI_TASK_FREE)
1858 if (lun != -1 && lun != task->sc->device->lun)
1861 ISCSI_DBG_SESSION(conn->session,
1862 "failing sc %p itt 0x%x state %d\n",
1863 task->sc, task->itt, task->state);
1864 fail_scsi_task(task, error);
1869 * iscsi_suspend_queue - suspend iscsi_queuecommand
1870 * @conn: iscsi conn to stop queueing IO on
1872 * This grabs the session frwd_lock to make sure no one is in
1873 * xmit_task/queuecommand, and then sets suspend to prevent
1874 * new commands from being queued. This only needs to be called
1875 * by offload drivers that need to sync a path like ep disconnect
1876 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1877 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1879 void iscsi_suspend_queue(struct iscsi_conn *conn)
1881 spin_lock_bh(&conn->session->frwd_lock);
1882 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1883 spin_unlock_bh(&conn->session->frwd_lock);
1885 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1888 * iscsi_suspend_tx - suspend iscsi_data_xmit
1889 * @conn: iscsi conn tp stop processing IO on.
1891 * This function sets the suspend bit to prevent iscsi_data_xmit
1892 * from sending new IO, and if work is queued on the xmit thread
1893 * it will wait for it to be completed.
1895 void iscsi_suspend_tx(struct iscsi_conn *conn)
1897 struct Scsi_Host *shost = conn->session->host;
1898 struct iscsi_host *ihost = shost_priv(shost);
1900 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1902 flush_workqueue(ihost->workq);
1904 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1906 static void iscsi_start_tx(struct iscsi_conn *conn)
1908 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1909 iscsi_conn_queue_work(conn);
1913 * We want to make sure a ping is in flight. It has timed out.
1914 * And we are not busy processing a pdu that is making
1915 * progress but got started before the ping and is taking a while
1916 * to complete so the ping is just stuck behind it in a queue.
1918 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1920 if (READ_ONCE(conn->ping_task) &&
1921 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1922 (conn->ping_timeout * HZ), jiffies))
1928 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1930 enum blk_eh_timer_return rc = BLK_EH_DONE;
1931 struct iscsi_task *task = NULL, *running_task;
1932 struct iscsi_cls_session *cls_session;
1933 struct iscsi_session *session;
1934 struct iscsi_conn *conn;
1937 cls_session = starget_to_session(scsi_target(sc->device));
1938 session = cls_session->dd_data;
1940 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1942 spin_lock_bh(&session->frwd_lock);
1943 task = (struct iscsi_task *)sc->SCp.ptr;
1946 * Raced with completion. Blk layer has taken ownership
1947 * so let timeout code complete it now.
1953 if (session->state != ISCSI_STATE_LOGGED_IN) {
1955 * During shutdown, if session is prematurely disconnected,
1956 * recovery won't happen and there will be hung cmds. Not
1957 * handling cmds would trigger EH, also bad in this case.
1958 * Instead, handle cmd, allow completion to happen and let
1959 * upper layer to deal with the result.
1961 if (unlikely(system_state != SYSTEM_RUNNING)) {
1962 sc->result = DID_NO_CONNECT << 16;
1963 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
1968 * We are probably in the middle of iscsi recovery so let
1969 * that complete and handle the error.
1971 rc = BLK_EH_RESET_TIMER;
1975 conn = session->leadconn;
1977 /* In the middle of shuting down */
1978 rc = BLK_EH_RESET_TIMER;
1983 * If we have sent (at least queued to the network layer) a pdu or
1984 * recvd one for the task since the last timeout ask for
1985 * more time. If on the next timeout we have not made progress
1986 * we can check if it is the task or connection when we send the
1989 if (time_after(task->last_xfer, task->last_timeout)) {
1990 ISCSI_DBG_EH(session, "Command making progress. Asking "
1991 "scsi-ml for more time to complete. "
1992 "Last data xfer at %lu. Last timeout was at "
1993 "%lu\n.", task->last_xfer, task->last_timeout);
1994 task->have_checked_conn = false;
1995 rc = BLK_EH_RESET_TIMER;
1999 if (!conn->recv_timeout && !conn->ping_timeout)
2002 * if the ping timedout then we are in the middle of cleaning up
2003 * and can let the iscsi eh handle it
2005 if (iscsi_has_ping_timed_out(conn)) {
2006 rc = BLK_EH_RESET_TIMER;
2010 for (i = 0; i < conn->session->cmds_max; i++) {
2011 running_task = conn->session->cmds[i];
2012 if (!running_task->sc || running_task == task ||
2013 running_task->state != ISCSI_TASK_RUNNING)
2017 * Only check if cmds started before this one have made
2018 * progress, or this could never fail
2020 if (time_after(running_task->sc->jiffies_at_alloc,
2021 task->sc->jiffies_at_alloc))
2024 if (time_after(running_task->last_xfer, task->last_timeout)) {
2026 * This task has not made progress, but a task
2027 * started before us has transferred data since
2028 * we started/last-checked. We could be queueing
2029 * too many tasks or the LU is bad.
2031 * If the device is bad the cmds ahead of us on
2032 * other devs will complete, and this loop will
2033 * eventually fail starting the scsi eh.
2035 ISCSI_DBG_EH(session, "Command has not made progress "
2036 "but commands ahead of it have. "
2037 "Asking scsi-ml for more time to "
2038 "complete. Our last xfer vs running task "
2039 "last xfer %lu/%lu. Last check %lu.\n",
2040 task->last_xfer, running_task->last_xfer,
2041 task->last_timeout);
2042 rc = BLK_EH_RESET_TIMER;
2047 /* Assumes nop timeout is shorter than scsi cmd timeout */
2048 if (task->have_checked_conn)
2052 * Checking the transport already or nop from a cmd timeout still
2055 if (READ_ONCE(conn->ping_task)) {
2056 task->have_checked_conn = true;
2057 rc = BLK_EH_RESET_TIMER;
2061 /* Make sure there is a transport check done */
2062 iscsi_send_nopout(conn, NULL);
2063 task->have_checked_conn = true;
2064 rc = BLK_EH_RESET_TIMER;
2068 task->last_timeout = jiffies;
2069 spin_unlock_bh(&session->frwd_lock);
2070 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2071 "timer reset" : "shutdown or nh");
2074 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2076 static void iscsi_check_transport_timeouts(struct timer_list *t)
2078 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2079 struct iscsi_session *session = conn->session;
2080 unsigned long recv_timeout, next_timeout = 0, last_recv;
2082 spin_lock(&session->frwd_lock);
2083 if (session->state != ISCSI_STATE_LOGGED_IN)
2086 recv_timeout = conn->recv_timeout;
2091 last_recv = conn->last_recv;
2093 if (iscsi_has_ping_timed_out(conn)) {
2094 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2095 "expired, recv timeout %d, last rx %lu, "
2096 "last ping %lu, now %lu\n",
2097 conn->ping_timeout, conn->recv_timeout,
2098 last_recv, conn->last_ping, jiffies);
2099 spin_unlock(&session->frwd_lock);
2100 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2104 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2105 /* send a ping to try to provoke some traffic */
2106 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2107 if (iscsi_send_nopout(conn, NULL))
2108 next_timeout = jiffies + (1 * HZ);
2110 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2112 next_timeout = last_recv + recv_timeout;
2114 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2115 mod_timer(&conn->transport_timer, next_timeout);
2117 spin_unlock(&session->frwd_lock);
2120 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2121 struct iscsi_tm *hdr)
2123 memset(hdr, 0, sizeof(*hdr));
2124 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2125 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2126 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2127 hdr->lun = task->lun;
2128 hdr->rtt = task->hdr_itt;
2129 hdr->refcmdsn = task->cmdsn;
2132 int iscsi_eh_abort(struct scsi_cmnd *sc)
2134 struct iscsi_cls_session *cls_session;
2135 struct iscsi_session *session;
2136 struct iscsi_conn *conn;
2137 struct iscsi_task *task;
2138 struct iscsi_tm *hdr;
2141 cls_session = starget_to_session(scsi_target(sc->device));
2142 session = cls_session->dd_data;
2144 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2146 mutex_lock(&session->eh_mutex);
2147 spin_lock_bh(&session->frwd_lock);
2149 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2153 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2155 spin_unlock_bh(&session->frwd_lock);
2156 mutex_unlock(&session->eh_mutex);
2161 * If we are not logged in or we have started a new session
2162 * then let the host reset code handle this
2164 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2165 sc->SCp.phase != session->age) {
2166 spin_unlock_bh(&session->frwd_lock);
2167 mutex_unlock(&session->eh_mutex);
2168 ISCSI_DBG_EH(session, "failing abort due to dropped "
2173 conn = session->leadconn;
2174 conn->eh_abort_cnt++;
2177 task = (struct iscsi_task *)sc->SCp.ptr;
2178 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2181 /* task completed before time out */
2183 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2187 if (task->state == ISCSI_TASK_PENDING) {
2188 fail_scsi_task(task, DID_ABORT);
2192 /* only have one tmf outstanding at a time */
2193 if (session->tmf_state != TMF_INITIAL)
2195 session->tmf_state = TMF_QUEUED;
2197 hdr = &session->tmhdr;
2198 iscsi_prep_abort_task_pdu(task, hdr);
2200 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2203 switch (session->tmf_state) {
2205 spin_unlock_bh(&session->frwd_lock);
2207 * stop tx side incase the target had sent a abort rsp but
2208 * the initiator was still writing out data.
2210 iscsi_suspend_tx(conn);
2212 * we do not stop the recv side because targets have been
2213 * good and have never sent us a successful tmf response
2214 * then sent more data for the cmd.
2216 spin_lock_bh(&session->frwd_lock);
2217 fail_scsi_task(task, DID_ABORT);
2218 session->tmf_state = TMF_INITIAL;
2219 memset(hdr, 0, sizeof(*hdr));
2220 spin_unlock_bh(&session->frwd_lock);
2221 iscsi_start_tx(conn);
2222 goto success_unlocked;
2224 spin_unlock_bh(&session->frwd_lock);
2225 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2226 goto failed_unlocked;
2229 session->tmf_state = TMF_INITIAL;
2230 memset(hdr, 0, sizeof(*hdr));
2231 /* task completed before tmf abort response */
2232 ISCSI_DBG_EH(session, "sc completed while abort in "
2238 session->tmf_state = TMF_INITIAL;
2243 spin_unlock_bh(&session->frwd_lock);
2245 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2247 mutex_unlock(&session->eh_mutex);
2251 spin_unlock_bh(&session->frwd_lock);
2253 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2254 task ? task->itt : 0);
2255 mutex_unlock(&session->eh_mutex);
2258 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2260 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2262 memset(hdr, 0, sizeof(*hdr));
2263 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2264 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2265 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2266 int_to_scsilun(sc->device->lun, &hdr->lun);
2267 hdr->rtt = RESERVED_ITT;
2270 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2272 struct iscsi_cls_session *cls_session;
2273 struct iscsi_session *session;
2274 struct iscsi_conn *conn;
2275 struct iscsi_tm *hdr;
2278 cls_session = starget_to_session(scsi_target(sc->device));
2279 session = cls_session->dd_data;
2281 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2284 mutex_lock(&session->eh_mutex);
2285 spin_lock_bh(&session->frwd_lock);
2287 * Just check if we are not logged in. We cannot check for
2288 * the phase because the reset could come from a ioctl.
2290 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2292 conn = session->leadconn;
2294 /* only have one tmf outstanding at a time */
2295 if (session->tmf_state != TMF_INITIAL)
2297 session->tmf_state = TMF_QUEUED;
2299 hdr = &session->tmhdr;
2300 iscsi_prep_lun_reset_pdu(sc, hdr);
2302 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2303 session->lu_reset_timeout)) {
2308 switch (session->tmf_state) {
2312 spin_unlock_bh(&session->frwd_lock);
2313 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2316 session->tmf_state = TMF_INITIAL;
2321 spin_unlock_bh(&session->frwd_lock);
2323 iscsi_suspend_tx(conn);
2325 spin_lock_bh(&session->frwd_lock);
2326 memset(hdr, 0, sizeof(*hdr));
2327 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2328 session->tmf_state = TMF_INITIAL;
2329 spin_unlock_bh(&session->frwd_lock);
2331 iscsi_start_tx(conn);
2335 spin_unlock_bh(&session->frwd_lock);
2337 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2338 rc == SUCCESS ? "SUCCESS" : "FAILED");
2339 mutex_unlock(&session->eh_mutex);
2342 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2344 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2346 struct iscsi_session *session = cls_session->dd_data;
2348 spin_lock_bh(&session->frwd_lock);
2349 if (session->state != ISCSI_STATE_LOGGED_IN) {
2350 session->state = ISCSI_STATE_RECOVERY_FAILED;
2351 wake_up(&session->ehwait);
2353 spin_unlock_bh(&session->frwd_lock);
2355 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2358 * iscsi_eh_session_reset - drop session and attempt relogin
2361 * This function will wait for a relogin, session termination from
2362 * userspace, or a recovery/replacement timeout.
2364 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2366 struct iscsi_cls_session *cls_session;
2367 struct iscsi_session *session;
2368 struct iscsi_conn *conn;
2370 cls_session = starget_to_session(scsi_target(sc->device));
2371 session = cls_session->dd_data;
2372 conn = session->leadconn;
2374 mutex_lock(&session->eh_mutex);
2375 spin_lock_bh(&session->frwd_lock);
2376 if (session->state == ISCSI_STATE_TERMINATE) {
2378 ISCSI_DBG_EH(session,
2379 "failing session reset: Could not log back into "
2380 "%s [age %d]\n", session->targetname,
2382 spin_unlock_bh(&session->frwd_lock);
2383 mutex_unlock(&session->eh_mutex);
2387 spin_unlock_bh(&session->frwd_lock);
2388 mutex_unlock(&session->eh_mutex);
2390 * we drop the lock here but the leadconn cannot be destoyed while
2391 * we are in the scsi eh
2393 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2395 ISCSI_DBG_EH(session, "wait for relogin\n");
2396 wait_event_interruptible(session->ehwait,
2397 session->state == ISCSI_STATE_TERMINATE ||
2398 session->state == ISCSI_STATE_LOGGED_IN ||
2399 session->state == ISCSI_STATE_RECOVERY_FAILED);
2400 if (signal_pending(current))
2401 flush_signals(current);
2403 mutex_lock(&session->eh_mutex);
2404 spin_lock_bh(&session->frwd_lock);
2405 if (session->state == ISCSI_STATE_LOGGED_IN) {
2406 ISCSI_DBG_EH(session,
2407 "session reset succeeded for %s,%s\n",
2408 session->targetname, conn->persistent_address);
2411 spin_unlock_bh(&session->frwd_lock);
2412 mutex_unlock(&session->eh_mutex);
2415 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2417 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2419 memset(hdr, 0, sizeof(*hdr));
2420 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2421 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2422 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2423 hdr->rtt = RESERVED_ITT;
2427 * iscsi_eh_target_reset - reset target
2430 * This will attempt to send a warm target reset.
2432 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2434 struct iscsi_cls_session *cls_session;
2435 struct iscsi_session *session;
2436 struct iscsi_conn *conn;
2437 struct iscsi_tm *hdr;
2440 cls_session = starget_to_session(scsi_target(sc->device));
2441 session = cls_session->dd_data;
2443 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2444 session->targetname);
2446 mutex_lock(&session->eh_mutex);
2447 spin_lock_bh(&session->frwd_lock);
2449 * Just check if we are not logged in. We cannot check for
2450 * the phase because the reset could come from a ioctl.
2452 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2454 conn = session->leadconn;
2456 /* only have one tmf outstanding at a time */
2457 if (session->tmf_state != TMF_INITIAL)
2459 session->tmf_state = TMF_QUEUED;
2461 hdr = &session->tmhdr;
2462 iscsi_prep_tgt_reset_pdu(sc, hdr);
2464 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2465 session->tgt_reset_timeout)) {
2470 switch (session->tmf_state) {
2474 spin_unlock_bh(&session->frwd_lock);
2475 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2478 session->tmf_state = TMF_INITIAL;
2483 spin_unlock_bh(&session->frwd_lock);
2485 iscsi_suspend_tx(conn);
2487 spin_lock_bh(&session->frwd_lock);
2488 memset(hdr, 0, sizeof(*hdr));
2489 fail_scsi_tasks(conn, -1, DID_ERROR);
2490 session->tmf_state = TMF_INITIAL;
2491 spin_unlock_bh(&session->frwd_lock);
2493 iscsi_start_tx(conn);
2497 spin_unlock_bh(&session->frwd_lock);
2499 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2500 rc == SUCCESS ? "SUCCESS" : "FAILED");
2501 mutex_unlock(&session->eh_mutex);
2506 * iscsi_eh_recover_target - reset target and possibly the session
2509 * This will attempt to send a warm target reset. If that fails,
2510 * we will escalate to ERL0 session recovery.
2512 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2516 rc = iscsi_eh_target_reset(sc);
2518 rc = iscsi_eh_session_reset(sc);
2521 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2524 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2525 * should be accessed via kfifo_{get,put} on q->queue.
2526 * Optionally, the caller can obtain the array of object pointers
2527 * by passing in a non-NULL @items pointer
2530 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2532 int i, num_arrays = 1;
2534 memset(q, 0, sizeof(*q));
2538 /* If the user passed an items pointer, he wants a copy of
2542 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2543 if (q->pool == NULL)
2546 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2548 for (i = 0; i < max; i++) {
2549 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2550 if (q->pool[i] == NULL) {
2554 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2558 *items = q->pool + max;
2559 memcpy(*items, q->pool, max * sizeof(void *));
2568 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2570 void iscsi_pool_free(struct iscsi_pool *q)
2574 for (i = 0; i < q->max; i++)
2578 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2581 * iscsi_host_add - add host to system
2583 * @pdev: parent device
2585 * This should be called by partial offload and software iscsi drivers
2586 * to add a host to the system.
2588 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2590 if (!shost->can_queue)
2591 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2593 if (!shost->cmd_per_lun)
2594 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2596 return scsi_add_host(shost, pdev);
2598 EXPORT_SYMBOL_GPL(iscsi_host_add);
2601 * iscsi_host_alloc - allocate a host and driver data
2602 * @sht: scsi host template
2603 * @dd_data_size: driver host data size
2604 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2606 * This should be called by partial offload and software iscsi drivers.
2607 * To access the driver specific memory use the iscsi_host_priv() macro.
2609 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2610 int dd_data_size, bool xmit_can_sleep)
2612 struct Scsi_Host *shost;
2613 struct iscsi_host *ihost;
2615 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2618 ihost = shost_priv(shost);
2620 if (xmit_can_sleep) {
2621 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2622 "iscsi_q_%d", shost->host_no);
2623 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2628 spin_lock_init(&ihost->lock);
2629 ihost->state = ISCSI_HOST_SETUP;
2630 ihost->num_sessions = 0;
2631 init_waitqueue_head(&ihost->session_removal_wq);
2635 scsi_host_put(shost);
2638 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2640 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2642 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2646 * iscsi_host_remove - remove host and sessions
2649 * If there are any sessions left, this will initiate the removal and wait
2650 * for the completion.
2652 void iscsi_host_remove(struct Scsi_Host *shost)
2654 struct iscsi_host *ihost = shost_priv(shost);
2655 unsigned long flags;
2657 spin_lock_irqsave(&ihost->lock, flags);
2658 ihost->state = ISCSI_HOST_REMOVED;
2659 spin_unlock_irqrestore(&ihost->lock, flags);
2661 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2662 wait_event_interruptible(ihost->session_removal_wq,
2663 ihost->num_sessions == 0);
2664 if (signal_pending(current))
2665 flush_signals(current);
2667 scsi_remove_host(shost);
2669 destroy_workqueue(ihost->workq);
2671 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2673 void iscsi_host_free(struct Scsi_Host *shost)
2675 struct iscsi_host *ihost = shost_priv(shost);
2677 kfree(ihost->netdev);
2678 kfree(ihost->hwaddress);
2679 kfree(ihost->initiatorname);
2680 scsi_host_put(shost);
2682 EXPORT_SYMBOL_GPL(iscsi_host_free);
2684 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2686 struct iscsi_host *ihost = shost_priv(shost);
2687 unsigned long flags;
2689 shost = scsi_host_get(shost);
2691 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2692 "of session teardown event because host already "
2697 spin_lock_irqsave(&ihost->lock, flags);
2698 ihost->num_sessions--;
2699 if (ihost->num_sessions == 0)
2700 wake_up(&ihost->session_removal_wq);
2701 spin_unlock_irqrestore(&ihost->lock, flags);
2702 scsi_host_put(shost);
2706 * iscsi_session_setup - create iscsi cls session and host and session
2707 * @iscsit: iscsi transport template
2709 * @cmds_max: session can queue
2710 * @dd_size: private driver data size, added to session allocation size
2711 * @cmd_task_size: LLD task private data size
2712 * @initial_cmdsn: initial CmdSN
2713 * @id: target ID to add to this session
2715 * This can be used by software iscsi_transports that allocate
2716 * a session per scsi host.
2718 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2719 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2720 * for nop handling and login/logout requests.
2722 struct iscsi_cls_session *
2723 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2724 uint16_t cmds_max, int dd_size, int cmd_task_size,
2725 uint32_t initial_cmdsn, unsigned int id)
2727 struct iscsi_host *ihost = shost_priv(shost);
2728 struct iscsi_session *session;
2729 struct iscsi_cls_session *cls_session;
2730 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2731 unsigned long flags;
2733 spin_lock_irqsave(&ihost->lock, flags);
2734 if (ihost->state == ISCSI_HOST_REMOVED) {
2735 spin_unlock_irqrestore(&ihost->lock, flags);
2738 ihost->num_sessions++;
2739 spin_unlock_irqrestore(&ihost->lock, flags);
2742 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2744 * The iscsi layer needs some tasks for nop handling and tmfs,
2745 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2746 * + 1 command for scsi IO.
2748 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2749 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2750 "must be a power of two that is at least %d.\n",
2751 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2752 goto dec_session_count;
2755 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2756 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2757 "must be a power of 2 less than or equal to %d.\n",
2758 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2759 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2762 if (!is_power_of_2(total_cmds)) {
2763 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2764 "must be a power of 2.\n", total_cmds);
2765 total_cmds = rounddown_pow_of_two(total_cmds);
2766 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2768 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2771 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2773 cls_session = iscsi_alloc_session(shost, iscsit,
2774 sizeof(struct iscsi_session) +
2777 goto dec_session_count;
2778 session = cls_session->dd_data;
2779 session->cls_session = cls_session;
2780 session->host = shost;
2781 session->state = ISCSI_STATE_FREE;
2782 session->fast_abort = 1;
2783 session->tgt_reset_timeout = 30;
2784 session->lu_reset_timeout = 15;
2785 session->abort_timeout = 10;
2786 session->scsi_cmds_max = scsi_cmds;
2787 session->cmds_max = total_cmds;
2788 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2789 session->exp_cmdsn = initial_cmdsn + 1;
2790 session->max_cmdsn = initial_cmdsn + 1;
2791 session->max_r2t = 1;
2792 session->tt = iscsit;
2793 session->dd_data = cls_session->dd_data + sizeof(*session);
2795 session->tmf_state = TMF_INITIAL;
2796 timer_setup(&session->tmf_timer, iscsi_tmf_timedout, 0);
2797 mutex_init(&session->eh_mutex);
2799 spin_lock_init(&session->frwd_lock);
2800 spin_lock_init(&session->back_lock);
2802 /* initialize SCSI PDU commands pool */
2803 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2804 (void***)&session->cmds,
2805 cmd_task_size + sizeof(struct iscsi_task)))
2806 goto cmdpool_alloc_fail;
2808 /* pre-format cmds pool with ITT */
2809 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2810 struct iscsi_task *task = session->cmds[cmd_i];
2813 task->dd_data = &task[1];
2815 task->state = ISCSI_TASK_FREE;
2816 INIT_LIST_HEAD(&task->running);
2819 if (!try_module_get(iscsit->owner))
2820 goto module_get_fail;
2822 if (iscsi_add_session(cls_session, id))
2823 goto cls_session_fail;
2828 module_put(iscsit->owner);
2830 iscsi_pool_free(&session->cmdpool);
2832 iscsi_free_session(cls_session);
2834 iscsi_host_dec_session_cnt(shost);
2837 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2840 * iscsi_session_teardown - destroy session, host, and cls_session
2841 * @cls_session: iscsi session
2843 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2845 struct iscsi_session *session = cls_session->dd_data;
2846 struct module *owner = cls_session->transport->owner;
2847 struct Scsi_Host *shost = session->host;
2849 iscsi_pool_free(&session->cmdpool);
2851 iscsi_remove_session(cls_session);
2853 kfree(session->password);
2854 kfree(session->password_in);
2855 kfree(session->username);
2856 kfree(session->username_in);
2857 kfree(session->targetname);
2858 kfree(session->targetalias);
2859 kfree(session->initiatorname);
2860 kfree(session->boot_root);
2861 kfree(session->boot_nic);
2862 kfree(session->boot_target);
2863 kfree(session->ifacename);
2864 kfree(session->portal_type);
2865 kfree(session->discovery_parent_type);
2867 iscsi_free_session(cls_session);
2869 iscsi_host_dec_session_cnt(shost);
2872 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2875 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2876 * @cls_session: iscsi_cls_session
2877 * @dd_size: private driver data size
2880 struct iscsi_cls_conn *
2881 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2884 struct iscsi_session *session = cls_session->dd_data;
2885 struct iscsi_conn *conn;
2886 struct iscsi_cls_conn *cls_conn;
2889 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2893 conn = cls_conn->dd_data;
2894 memset(conn, 0, sizeof(*conn) + dd_size);
2896 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2897 conn->session = session;
2898 conn->cls_conn = cls_conn;
2899 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2900 conn->id = conn_idx;
2901 conn->exp_statsn = 0;
2903 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2905 INIT_LIST_HEAD(&conn->mgmtqueue);
2906 INIT_LIST_HEAD(&conn->cmdqueue);
2907 INIT_LIST_HEAD(&conn->requeue);
2908 spin_lock_init(&conn->taskqueuelock);
2909 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2911 /* allocate login_task used for the login/text sequences */
2912 spin_lock_bh(&session->frwd_lock);
2913 if (!kfifo_out(&session->cmdpool.queue,
2914 (void*)&conn->login_task,
2916 spin_unlock_bh(&session->frwd_lock);
2917 goto login_task_alloc_fail;
2919 spin_unlock_bh(&session->frwd_lock);
2921 data = (char *) __get_free_pages(GFP_KERNEL,
2922 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2924 goto login_task_data_alloc_fail;
2925 conn->login_task->data = conn->data = data;
2927 init_waitqueue_head(&session->ehwait);
2931 login_task_data_alloc_fail:
2932 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2934 login_task_alloc_fail:
2935 iscsi_destroy_conn(cls_conn);
2938 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2941 * iscsi_conn_teardown - teardown iscsi connection
2942 * @cls_conn: iscsi class connection
2944 * TODO: we may need to make this into a two step process
2945 * like scsi-mls remove + put host
2947 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2949 struct iscsi_conn *conn = cls_conn->dd_data;
2950 struct iscsi_session *session = conn->session;
2951 char *tmp_persistent_address = conn->persistent_address;
2952 char *tmp_local_ipaddr = conn->local_ipaddr;
2954 del_timer_sync(&conn->transport_timer);
2956 mutex_lock(&session->eh_mutex);
2957 spin_lock_bh(&session->frwd_lock);
2958 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2959 if (session->leadconn == conn) {
2961 * leading connection? then give up on recovery.
2963 session->state = ISCSI_STATE_TERMINATE;
2964 wake_up(&session->ehwait);
2966 spin_unlock_bh(&session->frwd_lock);
2968 /* flush queued up work because we free the connection below */
2969 iscsi_suspend_tx(conn);
2971 spin_lock_bh(&session->frwd_lock);
2972 free_pages((unsigned long) conn->data,
2973 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2974 /* regular RX path uses back_lock */
2975 spin_lock_bh(&session->back_lock);
2976 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2978 spin_unlock_bh(&session->back_lock);
2979 if (session->leadconn == conn)
2980 session->leadconn = NULL;
2981 spin_unlock_bh(&session->frwd_lock);
2982 mutex_unlock(&session->eh_mutex);
2984 iscsi_destroy_conn(cls_conn);
2985 kfree(tmp_persistent_address);
2986 kfree(tmp_local_ipaddr);
2988 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2990 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2992 struct iscsi_conn *conn = cls_conn->dd_data;
2993 struct iscsi_session *session = conn->session;
2996 iscsi_conn_printk(KERN_ERR, conn,
2997 "can't start unbound connection\n");
3001 if ((session->imm_data_en || !session->initial_r2t_en) &&
3002 session->first_burst > session->max_burst) {
3003 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3004 "first_burst %d max_burst %d\n",
3005 session->first_burst, session->max_burst);
3009 if (conn->ping_timeout && !conn->recv_timeout) {
3010 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3011 "zero. Using 5 seconds\n.");
3012 conn->recv_timeout = 5;
3015 if (conn->recv_timeout && !conn->ping_timeout) {
3016 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3017 "zero. Using 5 seconds.\n");
3018 conn->ping_timeout = 5;
3021 spin_lock_bh(&session->frwd_lock);
3022 conn->c_stage = ISCSI_CONN_STARTED;
3023 session->state = ISCSI_STATE_LOGGED_IN;
3024 session->queued_cmdsn = session->cmdsn;
3026 conn->last_recv = jiffies;
3027 conn->last_ping = jiffies;
3028 if (conn->recv_timeout && conn->ping_timeout)
3029 mod_timer(&conn->transport_timer,
3030 jiffies + (conn->recv_timeout * HZ));
3032 switch(conn->stop_stage) {
3033 case STOP_CONN_RECOVER:
3035 * unblock eh_abort() if it is blocked. re-try all
3036 * commands after successful recovery
3038 conn->stop_stage = 0;
3039 session->tmf_state = TMF_INITIAL;
3041 if (session->age == 16)
3044 case STOP_CONN_TERM:
3045 conn->stop_stage = 0;
3050 spin_unlock_bh(&session->frwd_lock);
3052 iscsi_unblock_session(session->cls_session);
3053 wake_up(&session->ehwait);
3056 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3059 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3061 struct iscsi_task *task;
3064 for (i = 0; i < conn->session->cmds_max; i++) {
3065 task = conn->session->cmds[i];
3069 if (task->state == ISCSI_TASK_FREE)
3072 ISCSI_DBG_SESSION(conn->session,
3073 "failing mgmt itt 0x%x state %d\n",
3074 task->itt, task->state);
3075 state = ISCSI_TASK_ABRT_SESS_RECOV;
3076 if (task->state == ISCSI_TASK_PENDING)
3077 state = ISCSI_TASK_COMPLETED;
3078 spin_lock_bh(&session->back_lock);
3079 iscsi_complete_task(task, state);
3080 spin_unlock_bh(&session->back_lock);
3084 static void iscsi_start_session_recovery(struct iscsi_session *session,
3085 struct iscsi_conn *conn, int flag)
3089 mutex_lock(&session->eh_mutex);
3090 spin_lock_bh(&session->frwd_lock);
3091 if (conn->stop_stage == STOP_CONN_TERM) {
3092 spin_unlock_bh(&session->frwd_lock);
3093 mutex_unlock(&session->eh_mutex);
3098 * When this is called for the in_login state, we only want to clean
3099 * up the login task and connection. We do not need to block and set
3100 * the recovery state again
3102 if (flag == STOP_CONN_TERM)
3103 session->state = ISCSI_STATE_TERMINATE;
3104 else if (conn->stop_stage != STOP_CONN_RECOVER)
3105 session->state = ISCSI_STATE_IN_RECOVERY;
3107 old_stop_stage = conn->stop_stage;
3108 conn->stop_stage = flag;
3109 spin_unlock_bh(&session->frwd_lock);
3111 del_timer_sync(&conn->transport_timer);
3112 iscsi_suspend_tx(conn);
3114 spin_lock_bh(&session->frwd_lock);
3115 conn->c_stage = ISCSI_CONN_STOPPED;
3116 spin_unlock_bh(&session->frwd_lock);
3119 * for connection level recovery we should not calculate
3120 * header digest. conn->hdr_size used for optimization
3121 * in hdr_extract() and will be re-negotiated at
3124 if (flag == STOP_CONN_RECOVER) {
3125 conn->hdrdgst_en = 0;
3126 conn->datadgst_en = 0;
3127 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3128 old_stop_stage != STOP_CONN_RECOVER) {
3129 ISCSI_DBG_SESSION(session, "blocking session\n");
3130 iscsi_block_session(session->cls_session);
3137 spin_lock_bh(&session->frwd_lock);
3138 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3139 fail_mgmt_tasks(session, conn);
3140 memset(&session->tmhdr, 0, sizeof(session->tmhdr));
3141 spin_unlock_bh(&session->frwd_lock);
3142 mutex_unlock(&session->eh_mutex);
3145 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3147 struct iscsi_conn *conn = cls_conn->dd_data;
3148 struct iscsi_session *session = conn->session;
3151 case STOP_CONN_RECOVER:
3152 case STOP_CONN_TERM:
3153 iscsi_start_session_recovery(session, conn, flag);
3156 iscsi_conn_printk(KERN_ERR, conn,
3157 "invalid stop flag %d\n", flag);
3160 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3162 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3163 struct iscsi_cls_conn *cls_conn, int is_leading)
3165 struct iscsi_session *session = cls_session->dd_data;
3166 struct iscsi_conn *conn = cls_conn->dd_data;
3168 spin_lock_bh(&session->frwd_lock);
3170 session->leadconn = conn;
3171 spin_unlock_bh(&session->frwd_lock);
3174 * Unblock xmitworker(), Login Phase will pass through.
3176 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3177 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3180 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3182 int iscsi_switch_str_param(char **param, char *new_val_buf)
3187 if (!strcmp(*param, new_val_buf))
3191 new_val = kstrdup(new_val_buf, GFP_NOIO);
3199 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3201 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3202 enum iscsi_param param, char *buf, int buflen)
3204 struct iscsi_conn *conn = cls_conn->dd_data;
3205 struct iscsi_session *session = conn->session;
3209 case ISCSI_PARAM_FAST_ABORT:
3210 sscanf(buf, "%d", &session->fast_abort);
3212 case ISCSI_PARAM_ABORT_TMO:
3213 sscanf(buf, "%d", &session->abort_timeout);
3215 case ISCSI_PARAM_LU_RESET_TMO:
3216 sscanf(buf, "%d", &session->lu_reset_timeout);
3218 case ISCSI_PARAM_TGT_RESET_TMO:
3219 sscanf(buf, "%d", &session->tgt_reset_timeout);
3221 case ISCSI_PARAM_PING_TMO:
3222 sscanf(buf, "%d", &conn->ping_timeout);
3224 case ISCSI_PARAM_RECV_TMO:
3225 sscanf(buf, "%d", &conn->recv_timeout);
3227 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3228 sscanf(buf, "%d", &conn->max_recv_dlength);
3230 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3231 sscanf(buf, "%d", &conn->max_xmit_dlength);
3233 case ISCSI_PARAM_HDRDGST_EN:
3234 sscanf(buf, "%d", &conn->hdrdgst_en);
3236 case ISCSI_PARAM_DATADGST_EN:
3237 sscanf(buf, "%d", &conn->datadgst_en);
3239 case ISCSI_PARAM_INITIAL_R2T_EN:
3240 sscanf(buf, "%d", &session->initial_r2t_en);
3242 case ISCSI_PARAM_MAX_R2T:
3243 sscanf(buf, "%hu", &session->max_r2t);
3245 case ISCSI_PARAM_IMM_DATA_EN:
3246 sscanf(buf, "%d", &session->imm_data_en);
3248 case ISCSI_PARAM_FIRST_BURST:
3249 sscanf(buf, "%d", &session->first_burst);
3251 case ISCSI_PARAM_MAX_BURST:
3252 sscanf(buf, "%d", &session->max_burst);
3254 case ISCSI_PARAM_PDU_INORDER_EN:
3255 sscanf(buf, "%d", &session->pdu_inorder_en);
3257 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3258 sscanf(buf, "%d", &session->dataseq_inorder_en);
3260 case ISCSI_PARAM_ERL:
3261 sscanf(buf, "%d", &session->erl);
3263 case ISCSI_PARAM_EXP_STATSN:
3264 sscanf(buf, "%u", &conn->exp_statsn);
3266 case ISCSI_PARAM_USERNAME:
3267 return iscsi_switch_str_param(&session->username, buf);
3268 case ISCSI_PARAM_USERNAME_IN:
3269 return iscsi_switch_str_param(&session->username_in, buf);
3270 case ISCSI_PARAM_PASSWORD:
3271 return iscsi_switch_str_param(&session->password, buf);
3272 case ISCSI_PARAM_PASSWORD_IN:
3273 return iscsi_switch_str_param(&session->password_in, buf);
3274 case ISCSI_PARAM_TARGET_NAME:
3275 return iscsi_switch_str_param(&session->targetname, buf);
3276 case ISCSI_PARAM_TARGET_ALIAS:
3277 return iscsi_switch_str_param(&session->targetalias, buf);
3278 case ISCSI_PARAM_TPGT:
3279 sscanf(buf, "%d", &session->tpgt);
3281 case ISCSI_PARAM_PERSISTENT_PORT:
3282 sscanf(buf, "%d", &conn->persistent_port);
3284 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3285 return iscsi_switch_str_param(&conn->persistent_address, buf);
3286 case ISCSI_PARAM_IFACE_NAME:
3287 return iscsi_switch_str_param(&session->ifacename, buf);
3288 case ISCSI_PARAM_INITIATOR_NAME:
3289 return iscsi_switch_str_param(&session->initiatorname, buf);
3290 case ISCSI_PARAM_BOOT_ROOT:
3291 return iscsi_switch_str_param(&session->boot_root, buf);
3292 case ISCSI_PARAM_BOOT_NIC:
3293 return iscsi_switch_str_param(&session->boot_nic, buf);
3294 case ISCSI_PARAM_BOOT_TARGET:
3295 return iscsi_switch_str_param(&session->boot_target, buf);
3296 case ISCSI_PARAM_PORTAL_TYPE:
3297 return iscsi_switch_str_param(&session->portal_type, buf);
3298 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3299 return iscsi_switch_str_param(&session->discovery_parent_type,
3301 case ISCSI_PARAM_DISCOVERY_SESS:
3302 sscanf(buf, "%d", &val);
3303 session->discovery_sess = !!val;
3305 case ISCSI_PARAM_LOCAL_IPADDR:
3306 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3313 EXPORT_SYMBOL_GPL(iscsi_set_param);
3315 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3316 enum iscsi_param param, char *buf)
3318 struct iscsi_session *session = cls_session->dd_data;
3322 case ISCSI_PARAM_FAST_ABORT:
3323 len = sysfs_emit(buf, "%d\n", session->fast_abort);
3325 case ISCSI_PARAM_ABORT_TMO:
3326 len = sysfs_emit(buf, "%d\n", session->abort_timeout);
3328 case ISCSI_PARAM_LU_RESET_TMO:
3329 len = sysfs_emit(buf, "%d\n", session->lu_reset_timeout);
3331 case ISCSI_PARAM_TGT_RESET_TMO:
3332 len = sysfs_emit(buf, "%d\n", session->tgt_reset_timeout);
3334 case ISCSI_PARAM_INITIAL_R2T_EN:
3335 len = sysfs_emit(buf, "%d\n", session->initial_r2t_en);
3337 case ISCSI_PARAM_MAX_R2T:
3338 len = sysfs_emit(buf, "%hu\n", session->max_r2t);
3340 case ISCSI_PARAM_IMM_DATA_EN:
3341 len = sysfs_emit(buf, "%d\n", session->imm_data_en);
3343 case ISCSI_PARAM_FIRST_BURST:
3344 len = sysfs_emit(buf, "%u\n", session->first_burst);
3346 case ISCSI_PARAM_MAX_BURST:
3347 len = sysfs_emit(buf, "%u\n", session->max_burst);
3349 case ISCSI_PARAM_PDU_INORDER_EN:
3350 len = sysfs_emit(buf, "%d\n", session->pdu_inorder_en);
3352 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3353 len = sysfs_emit(buf, "%d\n", session->dataseq_inorder_en);
3355 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3356 len = sysfs_emit(buf, "%d\n", session->def_taskmgmt_tmo);
3358 case ISCSI_PARAM_ERL:
3359 len = sysfs_emit(buf, "%d\n", session->erl);
3361 case ISCSI_PARAM_TARGET_NAME:
3362 len = sysfs_emit(buf, "%s\n", session->targetname);
3364 case ISCSI_PARAM_TARGET_ALIAS:
3365 len = sysfs_emit(buf, "%s\n", session->targetalias);
3367 case ISCSI_PARAM_TPGT:
3368 len = sysfs_emit(buf, "%d\n", session->tpgt);
3370 case ISCSI_PARAM_USERNAME:
3371 len = sysfs_emit(buf, "%s\n", session->username);
3373 case ISCSI_PARAM_USERNAME_IN:
3374 len = sysfs_emit(buf, "%s\n", session->username_in);
3376 case ISCSI_PARAM_PASSWORD:
3377 len = sysfs_emit(buf, "%s\n", session->password);
3379 case ISCSI_PARAM_PASSWORD_IN:
3380 len = sysfs_emit(buf, "%s\n", session->password_in);
3382 case ISCSI_PARAM_IFACE_NAME:
3383 len = sysfs_emit(buf, "%s\n", session->ifacename);
3385 case ISCSI_PARAM_INITIATOR_NAME:
3386 len = sysfs_emit(buf, "%s\n", session->initiatorname);
3388 case ISCSI_PARAM_BOOT_ROOT:
3389 len = sysfs_emit(buf, "%s\n", session->boot_root);
3391 case ISCSI_PARAM_BOOT_NIC:
3392 len = sysfs_emit(buf, "%s\n", session->boot_nic);
3394 case ISCSI_PARAM_BOOT_TARGET:
3395 len = sysfs_emit(buf, "%s\n", session->boot_target);
3397 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3398 len = sysfs_emit(buf, "%u\n", session->auto_snd_tgt_disable);
3400 case ISCSI_PARAM_DISCOVERY_SESS:
3401 len = sysfs_emit(buf, "%u\n", session->discovery_sess);
3403 case ISCSI_PARAM_PORTAL_TYPE:
3404 len = sysfs_emit(buf, "%s\n", session->portal_type);
3406 case ISCSI_PARAM_CHAP_AUTH_EN:
3407 len = sysfs_emit(buf, "%u\n", session->chap_auth_en);
3409 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3410 len = sysfs_emit(buf, "%u\n", session->discovery_logout_en);
3412 case ISCSI_PARAM_BIDI_CHAP_EN:
3413 len = sysfs_emit(buf, "%u\n", session->bidi_chap_en);
3415 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3416 len = sysfs_emit(buf, "%u\n", session->discovery_auth_optional);
3418 case ISCSI_PARAM_DEF_TIME2WAIT:
3419 len = sysfs_emit(buf, "%d\n", session->time2wait);
3421 case ISCSI_PARAM_DEF_TIME2RETAIN:
3422 len = sysfs_emit(buf, "%d\n", session->time2retain);
3424 case ISCSI_PARAM_TSID:
3425 len = sysfs_emit(buf, "%u\n", session->tsid);
3427 case ISCSI_PARAM_ISID:
3428 len = sysfs_emit(buf, "%02x%02x%02x%02x%02x%02x\n",
3429 session->isid[0], session->isid[1],
3430 session->isid[2], session->isid[3],
3431 session->isid[4], session->isid[5]);
3433 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3434 len = sysfs_emit(buf, "%u\n", session->discovery_parent_idx);
3436 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3437 if (session->discovery_parent_type)
3438 len = sysfs_emit(buf, "%s\n",
3439 session->discovery_parent_type);
3441 len = sysfs_emit(buf, "\n");
3449 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3451 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3452 enum iscsi_param param, char *buf)
3454 struct sockaddr_in6 *sin6 = NULL;
3455 struct sockaddr_in *sin = NULL;
3458 switch (addr->ss_family) {
3460 sin = (struct sockaddr_in *)addr;
3463 sin6 = (struct sockaddr_in6 *)addr;
3470 case ISCSI_PARAM_CONN_ADDRESS:
3471 case ISCSI_HOST_PARAM_IPADDRESS:
3473 len = sysfs_emit(buf, "%pI4\n", &sin->sin_addr.s_addr);
3475 len = sysfs_emit(buf, "%pI6\n", &sin6->sin6_addr);
3477 case ISCSI_PARAM_CONN_PORT:
3478 case ISCSI_PARAM_LOCAL_PORT:
3480 len = sysfs_emit(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3482 len = sysfs_emit(buf, "%hu\n",
3483 be16_to_cpu(sin6->sin6_port));
3491 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3493 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3494 enum iscsi_param param, char *buf)
3496 struct iscsi_conn *conn = cls_conn->dd_data;
3500 case ISCSI_PARAM_PING_TMO:
3501 len = sysfs_emit(buf, "%u\n", conn->ping_timeout);
3503 case ISCSI_PARAM_RECV_TMO:
3504 len = sysfs_emit(buf, "%u\n", conn->recv_timeout);
3506 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3507 len = sysfs_emit(buf, "%u\n", conn->max_recv_dlength);
3509 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3510 len = sysfs_emit(buf, "%u\n", conn->max_xmit_dlength);
3512 case ISCSI_PARAM_HDRDGST_EN:
3513 len = sysfs_emit(buf, "%d\n", conn->hdrdgst_en);
3515 case ISCSI_PARAM_DATADGST_EN:
3516 len = sysfs_emit(buf, "%d\n", conn->datadgst_en);
3518 case ISCSI_PARAM_IFMARKER_EN:
3519 len = sysfs_emit(buf, "%d\n", conn->ifmarker_en);
3521 case ISCSI_PARAM_OFMARKER_EN:
3522 len = sysfs_emit(buf, "%d\n", conn->ofmarker_en);
3524 case ISCSI_PARAM_EXP_STATSN:
3525 len = sysfs_emit(buf, "%u\n", conn->exp_statsn);
3527 case ISCSI_PARAM_PERSISTENT_PORT:
3528 len = sysfs_emit(buf, "%d\n", conn->persistent_port);
3530 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3531 len = sysfs_emit(buf, "%s\n", conn->persistent_address);
3533 case ISCSI_PARAM_STATSN:
3534 len = sysfs_emit(buf, "%u\n", conn->statsn);
3536 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3537 len = sysfs_emit(buf, "%u\n", conn->max_segment_size);
3539 case ISCSI_PARAM_KEEPALIVE_TMO:
3540 len = sysfs_emit(buf, "%u\n", conn->keepalive_tmo);
3542 case ISCSI_PARAM_LOCAL_PORT:
3543 len = sysfs_emit(buf, "%u\n", conn->local_port);
3545 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3546 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_stat);
3548 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3549 len = sysfs_emit(buf, "%u\n", conn->tcp_nagle_disable);
3551 case ISCSI_PARAM_TCP_WSF_DISABLE:
3552 len = sysfs_emit(buf, "%u\n", conn->tcp_wsf_disable);
3554 case ISCSI_PARAM_TCP_TIMER_SCALE:
3555 len = sysfs_emit(buf, "%u\n", conn->tcp_timer_scale);
3557 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3558 len = sysfs_emit(buf, "%u\n", conn->tcp_timestamp_en);
3560 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3561 len = sysfs_emit(buf, "%u\n", conn->fragment_disable);
3563 case ISCSI_PARAM_IPV4_TOS:
3564 len = sysfs_emit(buf, "%u\n", conn->ipv4_tos);
3566 case ISCSI_PARAM_IPV6_TC:
3567 len = sysfs_emit(buf, "%u\n", conn->ipv6_traffic_class);
3569 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3570 len = sysfs_emit(buf, "%u\n", conn->ipv6_flow_label);
3572 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3573 len = sysfs_emit(buf, "%u\n", conn->is_fw_assigned_ipv6);
3575 case ISCSI_PARAM_TCP_XMIT_WSF:
3576 len = sysfs_emit(buf, "%u\n", conn->tcp_xmit_wsf);
3578 case ISCSI_PARAM_TCP_RECV_WSF:
3579 len = sysfs_emit(buf, "%u\n", conn->tcp_recv_wsf);
3581 case ISCSI_PARAM_LOCAL_IPADDR:
3582 len = sysfs_emit(buf, "%s\n", conn->local_ipaddr);
3590 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3592 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3595 struct iscsi_host *ihost = shost_priv(shost);
3599 case ISCSI_HOST_PARAM_NETDEV_NAME:
3600 len = sysfs_emit(buf, "%s\n", ihost->netdev);
3602 case ISCSI_HOST_PARAM_HWADDRESS:
3603 len = sysfs_emit(buf, "%s\n", ihost->hwaddress);
3605 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3606 len = sysfs_emit(buf, "%s\n", ihost->initiatorname);
3614 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3616 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3617 char *buf, int buflen)
3619 struct iscsi_host *ihost = shost_priv(shost);
3622 case ISCSI_HOST_PARAM_NETDEV_NAME:
3623 return iscsi_switch_str_param(&ihost->netdev, buf);
3624 case ISCSI_HOST_PARAM_HWADDRESS:
3625 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3626 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3627 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3634 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3636 MODULE_AUTHOR("Mike Christie");
3637 MODULE_DESCRIPTION("iSCSI library functions");
3638 MODULE_LICENSE("GPL");