4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <linux/slab.h>
29 #include <linux/sched/signal.h>
30 #include <linux/module.h>
31 #include <asm/unaligned.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_eh.h>
36 #include <scsi/scsi_tcq.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi.h>
39 #include <scsi/iscsi_proto.h>
40 #include <scsi/scsi_transport.h>
41 #include <scsi/scsi_transport_iscsi.h>
42 #include <scsi/libiscsi.h>
43 #include <trace/events/iscsi.h>
45 static int iscsi_dbg_lib_conn;
46 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
48 MODULE_PARM_DESC(debug_libiscsi_conn,
49 "Turn on debugging for connections in libiscsi module. "
50 "Set to 1 to turn on, and zero to turn off. Default is off.");
52 static int iscsi_dbg_lib_session;
53 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
55 MODULE_PARM_DESC(debug_libiscsi_session,
56 "Turn on debugging for sessions in libiscsi module. "
57 "Set to 1 to turn on, and zero to turn off. Default is off.");
59 static int iscsi_dbg_lib_eh;
60 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
62 MODULE_PARM_DESC(debug_libiscsi_eh,
63 "Turn on debugging for error handling in libiscsi module. "
64 "Set to 1 to turn on, and zero to turn off. Default is off.");
66 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
68 if (iscsi_dbg_lib_conn) \
69 iscsi_conn_printk(KERN_INFO, _conn, \
72 iscsi_dbg_trace(trace_iscsi_dbg_conn, \
73 &(_conn)->cls_conn->dev, \
74 "%s " dbg_fmt, __func__, ##arg);\
77 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
79 if (iscsi_dbg_lib_session) \
80 iscsi_session_printk(KERN_INFO, _session, \
83 iscsi_dbg_trace(trace_iscsi_dbg_session, \
84 &(_session)->cls_session->dev, \
85 "%s " dbg_fmt, __func__, ##arg); \
88 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
90 if (iscsi_dbg_lib_eh) \
91 iscsi_session_printk(KERN_INFO, _session, \
94 iscsi_dbg_trace(trace_iscsi_dbg_eh, \
95 &(_session)->cls_session->dev, \
96 "%s " dbg_fmt, __func__, ##arg); \
99 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
101 struct Scsi_Host *shost = conn->session->host;
102 struct iscsi_host *ihost = shost_priv(shost);
105 queue_work(ihost->workq, &conn->xmitwork);
107 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
109 static void __iscsi_update_cmdsn(struct iscsi_session *session,
110 uint32_t exp_cmdsn, uint32_t max_cmdsn)
113 * standard specifies this check for when to update expected and
114 * max sequence numbers
116 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
119 if (exp_cmdsn != session->exp_cmdsn &&
120 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
121 session->exp_cmdsn = exp_cmdsn;
123 if (max_cmdsn != session->max_cmdsn &&
124 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn))
125 session->max_cmdsn = max_cmdsn;
128 void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
130 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
131 be32_to_cpu(hdr->max_cmdsn));
133 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
136 * iscsi_prep_data_out_pdu - initialize Data-Out
137 * @task: scsi command task
139 * @hdr: iscsi data in pdu
142 * Initialize Data-Out within this R2T sequence and finds
143 * proper data_offset within this SCSI command.
145 * This function is called with connection lock taken.
147 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
148 struct iscsi_data *hdr)
150 struct iscsi_conn *conn = task->conn;
151 unsigned int left = r2t->data_length - r2t->sent;
153 task->hdr_len = sizeof(struct iscsi_data);
155 memset(hdr, 0, sizeof(struct iscsi_data));
157 hdr->datasn = cpu_to_be32(r2t->datasn);
159 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
160 hdr->lun = task->lun;
161 hdr->itt = task->hdr_itt;
162 hdr->exp_statsn = r2t->exp_statsn;
163 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
164 if (left > conn->max_xmit_dlength) {
165 hton24(hdr->dlength, conn->max_xmit_dlength);
166 r2t->data_count = conn->max_xmit_dlength;
169 hton24(hdr->dlength, left);
170 r2t->data_count = left;
171 hdr->flags = ISCSI_FLAG_CMD_FINAL;
173 conn->dataout_pdus_cnt++;
175 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
177 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
179 unsigned exp_len = task->hdr_len + len;
181 if (exp_len > task->hdr_max) {
186 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
187 task->hdr_len = exp_len;
192 * make an extended cdb AHS
194 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
196 struct scsi_cmnd *cmd = task->sc;
197 unsigned rlen, pad_len;
198 unsigned short ahslength;
199 struct iscsi_ecdb_ahdr *ecdb_ahdr;
202 ecdb_ahdr = iscsi_next_hdr(task);
203 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
205 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
206 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
208 pad_len = iscsi_padding(rlen);
210 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
211 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
216 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
218 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
219 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
220 ecdb_ahdr->reserved = 0;
221 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
223 ISCSI_DBG_SESSION(task->conn->session,
224 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
225 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
226 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
231 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
233 struct scsi_cmnd *sc = task->sc;
234 struct iscsi_rlength_ahdr *rlen_ahdr;
237 rlen_ahdr = iscsi_next_hdr(task);
238 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
242 rlen_ahdr->ahslength =
243 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
244 sizeof(rlen_ahdr->reserved));
245 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
246 rlen_ahdr->reserved = 0;
247 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
249 ISCSI_DBG_SESSION(task->conn->session,
250 "bidi-in rlen_ahdr->read_length(%d) "
251 "rlen_ahdr->ahslength(%d)\n",
252 be32_to_cpu(rlen_ahdr->read_length),
253 be16_to_cpu(rlen_ahdr->ahslength));
258 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
260 * @opcode: opcode to check for
262 * During TMF a task has to be checked if it's affected.
263 * All unrelated I/O can be passed through, but I/O to the
264 * affected LUN should be restricted.
265 * If 'fast_abort' is set we won't be sending any I/O to the
267 * Otherwise the target is waiting for all TTTs to be completed,
268 * so we have to send all outstanding Data-Out PDUs to the target.
270 static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
272 struct iscsi_conn *conn = task->conn;
273 struct iscsi_tm *tmf = &conn->tmhdr;
276 if (conn->tmf_state == TMF_INITIAL)
279 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
282 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
283 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
285 * Allow PDUs for unrelated LUNs
287 hdr_lun = scsilun_to_int(&tmf->lun);
288 if (hdr_lun != task->sc->device->lun)
291 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
293 * Fail all SCSI cmd PDUs
295 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
296 iscsi_conn_printk(KERN_INFO, conn,
305 * And also all data-out PDUs in response to R2T
306 * if fast_abort is set.
308 if (conn->session->fast_abort) {
309 iscsi_conn_printk(KERN_INFO, conn,
311 "0x%x/0x%x] fast abort.\n",
317 case ISCSI_TM_FUNC_ABORT_TASK:
319 * the caller has already checked if the task
320 * they want to abort was in the pending queue so if
321 * we are here the cmd pdu has gone out already, and
322 * we will only hit this for data-outs
324 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
325 task->hdr_itt == tmf->rtt) {
326 ISCSI_DBG_SESSION(conn->session,
327 "Preventing task %x/%x from sending "
328 "data-out due to abort task in "
329 "progress\n", task->itt,
340 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
343 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
344 * fields like dlength or final based on how much data it sends
346 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
348 struct iscsi_conn *conn = task->conn;
349 struct iscsi_session *session = conn->session;
350 struct scsi_cmnd *sc = task->sc;
351 struct iscsi_scsi_req *hdr;
352 unsigned hdrlength, cmd_len, transfer_length;
356 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
360 if (conn->session->tt->alloc_pdu) {
361 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
365 hdr = (struct iscsi_scsi_req *)task->hdr;
367 memset(hdr, 0, sizeof(*hdr));
369 if (session->tt->parse_pdu_itt)
370 hdr->itt = task->hdr_itt = itt;
372 hdr->itt = task->hdr_itt = build_itt(task->itt,
373 task->conn->session->age);
375 rc = iscsi_add_hdr(task, sizeof(*hdr));
378 hdr->opcode = ISCSI_OP_SCSI_CMD;
379 hdr->flags = ISCSI_ATTR_SIMPLE;
380 int_to_scsilun(sc->device->lun, &hdr->lun);
381 task->lun = hdr->lun;
382 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
383 cmd_len = sc->cmd_len;
384 if (cmd_len < ISCSI_CDB_SIZE)
385 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
386 else if (cmd_len > ISCSI_CDB_SIZE) {
387 rc = iscsi_prep_ecdb_ahs(task);
390 cmd_len = ISCSI_CDB_SIZE;
392 memcpy(hdr->cdb, sc->cmnd, cmd_len);
395 if (scsi_bidi_cmnd(sc)) {
396 hdr->flags |= ISCSI_FLAG_CMD_READ;
397 rc = iscsi_prep_bidi_ahs(task);
402 if (scsi_get_prot_op(sc) != SCSI_PROT_NORMAL)
403 task->protected = true;
405 transfer_length = scsi_transfer_length(sc);
406 hdr->data_length = cpu_to_be32(transfer_length);
407 if (sc->sc_data_direction == DMA_TO_DEVICE) {
408 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
410 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
414 * imm_count bytes to be sent right after
417 * unsol_count bytes(as Data-Out) to be sent
418 * without R2T ack right after
421 * r2t data_length bytes to be sent via R2T ack's
423 * pad_count bytes to be sent as zero-padding
425 memset(r2t, 0, sizeof(*r2t));
427 if (session->imm_data_en) {
428 if (transfer_length >= session->first_burst)
429 task->imm_count = min(session->first_burst,
430 conn->max_xmit_dlength);
432 task->imm_count = min(transfer_length,
433 conn->max_xmit_dlength);
434 hton24(hdr->dlength, task->imm_count);
436 zero_data(hdr->dlength);
438 if (!session->initial_r2t_en) {
439 r2t->data_length = min(session->first_burst,
442 r2t->data_offset = task->imm_count;
443 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
444 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
447 if (!task->unsol_r2t.data_length)
448 /* No unsolicit Data-Out's */
449 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
451 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
452 zero_data(hdr->dlength);
454 if (sc->sc_data_direction == DMA_FROM_DEVICE)
455 hdr->flags |= ISCSI_FLAG_CMD_READ;
458 /* calculate size of additional header segments (AHSs) */
459 hdrlength = task->hdr_len - sizeof(*hdr);
461 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
462 hdrlength /= ISCSI_PAD_LEN;
464 WARN_ON(hdrlength >= 256);
465 hdr->hlength = hdrlength & 0xFF;
466 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
468 if (session->tt->init_task && session->tt->init_task(task))
471 task->state = ISCSI_TASK_RUNNING;
474 conn->scsicmd_pdus_cnt++;
475 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
476 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
477 scsi_bidi_cmnd(sc) ? "bidirectional" :
478 sc->sc_data_direction == DMA_TO_DEVICE ?
479 "write" : "read", conn->id, sc, sc->cmnd[0],
480 task->itt, transfer_length,
481 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
483 session->max_cmdsn - session->exp_cmdsn + 1);
488 * iscsi_free_task - free a task
489 * @task: iscsi cmd task
491 * Must be called with session back_lock.
492 * This function returns the scsi command to scsi-ml or cleans
493 * up mgmt tasks then returns the task to the pool.
495 static void iscsi_free_task(struct iscsi_task *task)
497 struct iscsi_conn *conn = task->conn;
498 struct iscsi_session *session = conn->session;
499 struct scsi_cmnd *sc = task->sc;
500 int oldstate = task->state;
502 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
503 task->itt, task->state, task->sc);
505 session->tt->cleanup_task(task);
506 task->state = ISCSI_TASK_FREE;
509 * login task is preallocated so do not free
511 if (conn->login_task == task)
514 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
517 /* SCSI eh reuses commands to verify us */
520 * queue command may call this to free the task, so
521 * it will decide how to return sc to scsi-ml.
523 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
528 void __iscsi_get_task(struct iscsi_task *task)
530 refcount_inc(&task->refcount);
532 EXPORT_SYMBOL_GPL(__iscsi_get_task);
534 void __iscsi_put_task(struct iscsi_task *task)
536 if (refcount_dec_and_test(&task->refcount))
537 iscsi_free_task(task);
539 EXPORT_SYMBOL_GPL(__iscsi_put_task);
541 void iscsi_put_task(struct iscsi_task *task)
543 struct iscsi_session *session = task->conn->session;
545 /* regular RX path uses back_lock */
546 spin_lock_bh(&session->back_lock);
547 __iscsi_put_task(task);
548 spin_unlock_bh(&session->back_lock);
550 EXPORT_SYMBOL_GPL(iscsi_put_task);
553 * iscsi_complete_task - finish a task
554 * @task: iscsi cmd task
555 * @state: state to complete task with
557 * Must be called with session back_lock.
559 static void iscsi_complete_task(struct iscsi_task *task, int state)
561 struct iscsi_conn *conn = task->conn;
563 ISCSI_DBG_SESSION(conn->session,
564 "complete task itt 0x%x state %d sc %p\n",
565 task->itt, task->state, task->sc);
566 if (task->state == ISCSI_TASK_COMPLETED ||
567 task->state == ISCSI_TASK_ABRT_TMF ||
568 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
569 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
571 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
574 spin_lock_bh(&conn->taskqueuelock);
575 if (!list_empty(&task->running)) {
576 pr_debug_once("%s while task on list", __func__);
577 list_del_init(&task->running);
579 spin_unlock_bh(&conn->taskqueuelock);
581 if (conn->task == task)
584 if (conn->ping_task == task)
585 conn->ping_task = NULL;
587 /* release get from queueing */
588 __iscsi_put_task(task);
592 * iscsi_complete_scsi_task - finish scsi task normally
593 * @task: iscsi task for scsi cmd
594 * @exp_cmdsn: expected cmd sn in cpu format
595 * @max_cmdsn: max cmd sn in cpu format
597 * This is used when drivers do not need or cannot perform
598 * lower level pdu processing.
600 * Called with session back_lock
602 void iscsi_complete_scsi_task(struct iscsi_task *task,
603 uint32_t exp_cmdsn, uint32_t max_cmdsn)
605 struct iscsi_conn *conn = task->conn;
607 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
609 conn->last_recv = jiffies;
610 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
611 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
613 EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
617 * session back_lock must be held and if not called for a task that is
618 * still pending or from the xmit thread, then xmit thread must
621 static void fail_scsi_task(struct iscsi_task *task, int err)
623 struct iscsi_conn *conn = task->conn;
624 struct scsi_cmnd *sc;
628 * if a command completes and we get a successful tmf response
629 * we will hit this because the scsi eh abort code does not take
636 if (task->state == ISCSI_TASK_PENDING) {
638 * cmd never made it to the xmit thread, so we should not count
639 * the cmd in the sequencing
641 conn->session->queued_cmdsn--;
642 /* it was never sent so just complete like normal */
643 state = ISCSI_TASK_COMPLETED;
644 } else if (err == DID_TRANSPORT_DISRUPTED)
645 state = ISCSI_TASK_ABRT_SESS_RECOV;
647 state = ISCSI_TASK_ABRT_TMF;
649 sc->result = err << 16;
650 if (!scsi_bidi_cmnd(sc))
651 scsi_set_resid(sc, scsi_bufflen(sc));
653 scsi_out(sc)->resid = scsi_out(sc)->length;
654 scsi_in(sc)->resid = scsi_in(sc)->length;
657 /* regular RX path uses back_lock */
658 spin_lock_bh(&conn->session->back_lock);
659 iscsi_complete_task(task, state);
660 spin_unlock_bh(&conn->session->back_lock);
663 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
664 struct iscsi_task *task)
666 struct iscsi_session *session = conn->session;
667 struct iscsi_hdr *hdr = task->hdr;
668 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
669 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
671 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
674 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
675 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
677 * pre-format CmdSN for outgoing PDU.
679 nop->cmdsn = cpu_to_be32(session->cmdsn);
680 if (hdr->itt != RESERVED_ITT) {
682 * TODO: We always use immediate for normal session pdus.
683 * If we start to send tmfs or nops as non-immediate then
684 * we should start checking the cmdsn numbers for mgmt tasks.
686 * During discovery sessions iscsid sends TEXT as non immediate,
687 * but we always only send one PDU at a time.
689 if (conn->c_stage == ISCSI_CONN_STARTED &&
690 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
691 session->queued_cmdsn++;
696 if (session->tt->init_task && session->tt->init_task(task))
699 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
700 session->state = ISCSI_STATE_LOGGING_OUT;
702 task->state = ISCSI_TASK_RUNNING;
703 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
704 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
705 hdr->itt, task->data_count);
709 static struct iscsi_task *
710 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
711 char *data, uint32_t data_size)
713 struct iscsi_session *session = conn->session;
714 struct iscsi_host *ihost = shost_priv(session->host);
715 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
716 struct iscsi_task *task;
719 if (session->state == ISCSI_STATE_TERMINATE)
722 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
724 * Login and Text are sent serially, in
725 * request-followed-by-response sequence.
726 * Same task can be used. Same ITT must be used.
727 * Note that login_task is preallocated at conn_create().
729 if (conn->login_task->state != ISCSI_TASK_FREE) {
730 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
731 "progress. Cannot start new task.\n");
735 if (data_size > ISCSI_DEF_MAX_RECV_SEG_LEN) {
736 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);
740 task = conn->login_task;
742 if (session->state != ISCSI_STATE_LOGGED_IN)
745 if (data_size != 0) {
746 iscsi_conn_printk(KERN_ERR, conn, "Can not send data buffer of len %u for op 0x%x\n", data_size, opcode);
750 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
751 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
753 if (!kfifo_out(&session->cmdpool.queue,
754 (void*)&task, sizeof(void*)))
758 * released in complete pdu for task we expect a response for, and
759 * released by the lld when it has transmitted the task for
760 * pdus we do not expect a response for.
762 refcount_set(&task->refcount, 1);
765 INIT_LIST_HEAD(&task->running);
766 task->state = ISCSI_TASK_PENDING;
769 memcpy(task->data, data, data_size);
770 task->data_count = data_size;
772 task->data_count = 0;
774 if (conn->session->tt->alloc_pdu) {
775 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
776 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
777 "pdu for mgmt task.\n");
782 itt = task->hdr->itt;
783 task->hdr_len = sizeof(struct iscsi_hdr);
784 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
786 if (hdr->itt != RESERVED_ITT) {
787 if (session->tt->parse_pdu_itt)
788 task->hdr->itt = itt;
790 task->hdr->itt = build_itt(task->itt,
791 task->conn->session->age);
795 if (iscsi_prep_mgmt_task(conn, task))
798 if (session->tt->xmit_task(task))
801 spin_lock_bh(&conn->taskqueuelock);
802 list_add_tail(&task->running, &conn->mgmtqueue);
803 spin_unlock_bh(&conn->taskqueuelock);
804 iscsi_conn_queue_work(conn);
810 /* regular RX path uses back_lock */
811 spin_lock(&session->back_lock);
812 __iscsi_put_task(task);
813 spin_unlock(&session->back_lock);
817 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
818 char *data, uint32_t data_size)
820 struct iscsi_conn *conn = cls_conn->dd_data;
821 struct iscsi_session *session = conn->session;
824 spin_lock_bh(&session->frwd_lock);
825 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
827 spin_unlock_bh(&session->frwd_lock);
830 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
833 * iscsi_cmd_rsp - SCSI Command Response processing
834 * @conn: iscsi connection
836 * @task: scsi command task
837 * @data: cmd data buffer
838 * @datalen: len of buffer
840 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
841 * then completes the command and task.
843 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
844 struct iscsi_task *task, char *data,
847 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
848 struct iscsi_session *session = conn->session;
849 struct scsi_cmnd *sc = task->sc;
851 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
852 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
854 sc->result = (DID_OK << 16) | rhdr->cmd_status;
856 if (task->protected) {
861 * Transports that didn't implement check_protection
862 * callback but still published T10-PI support to scsi-mid
863 * deserve this BUG_ON.
865 BUG_ON(!session->tt->check_protection);
867 ascq = session->tt->check_protection(task, §or);
869 sc->result = DRIVER_SENSE << 24 |
870 SAM_STAT_CHECK_CONDITION;
871 scsi_build_sense_buffer(1, sc->sense_buffer,
872 ILLEGAL_REQUEST, 0x10, ascq);
873 scsi_set_sense_information(sc->sense_buffer,
874 SCSI_SENSE_BUFFERSIZE,
880 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
881 sc->result = DID_ERROR << 16;
885 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
890 iscsi_conn_printk(KERN_ERR, conn,
891 "Got CHECK_CONDITION but invalid data "
892 "buffer size of %d\n", datalen);
893 sc->result = DID_BAD_TARGET << 16;
897 senselen = get_unaligned_be16(data);
898 if (datalen < senselen)
899 goto invalid_datalen;
901 memcpy(sc->sense_buffer, data + 2,
902 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
903 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
904 min_t(uint16_t, senselen,
905 SCSI_SENSE_BUFFERSIZE));
908 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
909 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
910 int res_count = be32_to_cpu(rhdr->bi_residual_count);
912 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
913 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
914 res_count <= scsi_in(sc)->length))
915 scsi_in(sc)->resid = res_count;
917 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
920 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
921 ISCSI_FLAG_CMD_OVERFLOW)) {
922 int res_count = be32_to_cpu(rhdr->residual_count);
925 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
926 res_count <= scsi_bufflen(sc)))
927 /* write side for bidi or uni-io set_resid */
928 scsi_set_resid(sc, res_count);
930 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
933 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
934 sc, sc->result, task->itt);
935 conn->scsirsp_pdus_cnt++;
936 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
940 * iscsi_data_in_rsp - SCSI Data-In Response processing
941 * @conn: iscsi connection
943 * @task: scsi command task
946 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
947 struct iscsi_task *task)
949 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
950 struct scsi_cmnd *sc = task->sc;
952 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
955 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
956 sc->result = (DID_OK << 16) | rhdr->cmd_status;
957 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
958 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
959 ISCSI_FLAG_DATA_OVERFLOW)) {
960 int res_count = be32_to_cpu(rhdr->residual_count);
963 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
964 res_count <= scsi_in(sc)->length))
965 scsi_in(sc)->resid = res_count;
967 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
970 ISCSI_DBG_SESSION(conn->session, "data in with status done "
971 "[sc %p res %d itt 0x%x]\n",
972 sc, sc->result, task->itt);
973 conn->scsirsp_pdus_cnt++;
974 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
977 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
979 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
981 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
982 conn->tmfrsp_pdus_cnt++;
984 if (conn->tmf_state != TMF_QUEUED)
987 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
988 conn->tmf_state = TMF_SUCCESS;
989 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
990 conn->tmf_state = TMF_NOT_FOUND;
992 conn->tmf_state = TMF_FAILED;
993 wake_up(&conn->ehwait);
996 static int iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
998 struct iscsi_nopout hdr;
999 struct iscsi_task *task;
1001 if (!rhdr && conn->ping_task)
1004 memset(&hdr, 0, sizeof(struct iscsi_nopout));
1005 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
1006 hdr.flags = ISCSI_FLAG_CMD_FINAL;
1009 hdr.lun = rhdr->lun;
1010 hdr.ttt = rhdr->ttt;
1011 hdr.itt = RESERVED_ITT;
1013 hdr.ttt = RESERVED_ITT;
1015 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
1017 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
1020 /* only track our nops */
1021 conn->ping_task = task;
1022 conn->last_ping = jiffies;
1028 static int iscsi_nop_out_rsp(struct iscsi_task *task,
1029 struct iscsi_nopin *nop, char *data, int datalen)
1031 struct iscsi_conn *conn = task->conn;
1034 if (conn->ping_task != task) {
1036 * If this is not in response to one of our
1037 * nops then it must be from userspace.
1039 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
1041 rc = ISCSI_ERR_CONN_FAILED;
1043 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1044 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1048 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1049 char *data, int datalen)
1051 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1052 struct iscsi_hdr rejected_pdu;
1055 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1057 if (ntoh24(reject->dlength) > datalen ||
1058 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1059 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1060 "pdu. Invalid data length (pdu dlength "
1061 "%u, datalen %d\n", ntoh24(reject->dlength),
1063 return ISCSI_ERR_PROTO;
1065 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1066 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1068 switch (reject->reason) {
1069 case ISCSI_REASON_DATA_DIGEST_ERROR:
1070 iscsi_conn_printk(KERN_ERR, conn,
1071 "pdu (op 0x%x itt 0x%x) rejected "
1072 "due to DataDigest error.\n",
1073 opcode, rejected_pdu.itt);
1075 case ISCSI_REASON_IMM_CMD_REJECT:
1076 iscsi_conn_printk(KERN_ERR, conn,
1077 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1078 "immediate commands.\n",
1079 opcode, rejected_pdu.itt);
1081 * We only send one TMF at a time so if the target could not
1082 * handle it, then it should get fixed (RFC mandates that
1083 * a target can handle one immediate TMF per conn).
1085 * For nops-outs, we could have sent more than one if
1086 * the target is sending us lots of nop-ins
1088 if (opcode != ISCSI_OP_NOOP_OUT)
1091 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1093 * nop-out in response to target's nop-out rejected.
1096 /* In RX path we are under back lock */
1097 spin_unlock(&conn->session->back_lock);
1098 spin_lock(&conn->session->frwd_lock);
1099 iscsi_send_nopout(conn,
1100 (struct iscsi_nopin*)&rejected_pdu);
1101 spin_unlock(&conn->session->frwd_lock);
1102 spin_lock(&conn->session->back_lock);
1104 struct iscsi_task *task;
1106 * Our nop as ping got dropped. We know the target
1107 * and transport are ok so just clean up
1109 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1111 iscsi_conn_printk(KERN_ERR, conn,
1112 "Invalid pdu reject. Could "
1113 "not lookup rejected task.\n");
1114 rc = ISCSI_ERR_BAD_ITT;
1116 rc = iscsi_nop_out_rsp(task,
1117 (struct iscsi_nopin*)&rejected_pdu,
1122 iscsi_conn_printk(KERN_ERR, conn,
1123 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1124 "code 0x%x\n", rejected_pdu.opcode,
1125 rejected_pdu.itt, reject->reason);
1132 * iscsi_itt_to_task - look up task by itt
1133 * @conn: iscsi connection
1136 * This should be used for mgmt tasks like login and nops, or if
1137 * the LDD's itt space does not include the session age.
1139 * The session back_lock must be held.
1141 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1143 struct iscsi_session *session = conn->session;
1146 if (itt == RESERVED_ITT)
1149 if (session->tt->parse_pdu_itt)
1150 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1153 if (i >= session->cmds_max)
1156 return session->cmds[i];
1158 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1161 * __iscsi_complete_pdu - complete pdu
1163 * @hdr: iscsi header
1164 * @data: data buffer
1165 * @datalen: len of data buffer
1167 * Completes pdu processing by freeing any resources allocated at
1168 * queuecommand or send generic. session back_lock must be held and verify
1169 * itt must have been called.
1171 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1172 char *data, int datalen)
1174 struct iscsi_session *session = conn->session;
1175 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1176 struct iscsi_task *task;
1179 conn->last_recv = jiffies;
1180 rc = iscsi_verify_itt(conn, hdr->itt);
1184 if (hdr->itt != RESERVED_ITT)
1185 itt = get_itt(hdr->itt);
1189 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1190 opcode, conn->id, itt, datalen);
1193 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1196 case ISCSI_OP_NOOP_IN:
1198 rc = ISCSI_ERR_PROTO;
1202 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1205 /* In RX path we are under back lock */
1206 spin_unlock(&session->back_lock);
1207 spin_lock(&session->frwd_lock);
1208 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1209 spin_unlock(&session->frwd_lock);
1210 spin_lock(&session->back_lock);
1212 case ISCSI_OP_REJECT:
1213 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1215 case ISCSI_OP_ASYNC_EVENT:
1216 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1217 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1218 rc = ISCSI_ERR_CONN_FAILED;
1221 rc = ISCSI_ERR_BAD_OPCODE;
1228 case ISCSI_OP_SCSI_CMD_RSP:
1229 case ISCSI_OP_SCSI_DATA_IN:
1230 task = iscsi_itt_to_ctask(conn, hdr->itt);
1232 return ISCSI_ERR_BAD_ITT;
1233 task->last_xfer = jiffies;
1237 * LLD handles R2Ts if they need to.
1240 case ISCSI_OP_LOGOUT_RSP:
1241 case ISCSI_OP_LOGIN_RSP:
1242 case ISCSI_OP_TEXT_RSP:
1243 case ISCSI_OP_SCSI_TMFUNC_RSP:
1244 case ISCSI_OP_NOOP_IN:
1245 task = iscsi_itt_to_task(conn, hdr->itt);
1247 return ISCSI_ERR_BAD_ITT;
1250 return ISCSI_ERR_BAD_OPCODE;
1254 case ISCSI_OP_SCSI_CMD_RSP:
1255 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1257 case ISCSI_OP_SCSI_DATA_IN:
1258 iscsi_data_in_rsp(conn, hdr, task);
1260 case ISCSI_OP_LOGOUT_RSP:
1261 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1263 rc = ISCSI_ERR_PROTO;
1266 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1268 case ISCSI_OP_LOGIN_RSP:
1269 case ISCSI_OP_TEXT_RSP:
1270 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1272 * login related PDU's exp_statsn is handled in
1276 case ISCSI_OP_SCSI_TMFUNC_RSP:
1277 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1279 rc = ISCSI_ERR_PROTO;
1283 iscsi_tmf_rsp(conn, hdr);
1284 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1286 case ISCSI_OP_NOOP_IN:
1287 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1288 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1289 rc = ISCSI_ERR_PROTO;
1292 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1294 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1298 rc = ISCSI_ERR_BAD_OPCODE;
1305 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1306 rc = ISCSI_ERR_CONN_FAILED;
1307 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1310 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1312 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1313 char *data, int datalen)
1317 spin_lock(&conn->session->back_lock);
1318 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1319 spin_unlock(&conn->session->back_lock);
1322 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1324 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1326 struct iscsi_session *session = conn->session;
1329 if (itt == RESERVED_ITT)
1332 if (session->tt->parse_pdu_itt)
1333 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1336 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1339 if (age != session->age) {
1340 iscsi_conn_printk(KERN_ERR, conn,
1341 "received itt %x expected session age (%x)\n",
1342 (__force u32)itt, session->age);
1343 return ISCSI_ERR_BAD_ITT;
1346 if (i >= session->cmds_max) {
1347 iscsi_conn_printk(KERN_ERR, conn,
1348 "received invalid itt index %u (max cmds "
1349 "%u.\n", i, session->cmds_max);
1350 return ISCSI_ERR_BAD_ITT;
1354 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1357 * iscsi_itt_to_ctask - look up ctask by itt
1358 * @conn: iscsi connection
1361 * This should be used for cmd tasks.
1363 * The session back_lock must be held.
1365 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1367 struct iscsi_task *task;
1369 if (iscsi_verify_itt(conn, itt))
1372 task = iscsi_itt_to_task(conn, itt);
1373 if (!task || !task->sc)
1376 if (task->sc->SCp.phase != conn->session->age) {
1377 iscsi_session_printk(KERN_ERR, conn->session,
1378 "task's session age %d, expected %d\n",
1379 task->sc->SCp.phase, conn->session->age);
1385 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1387 void iscsi_session_failure(struct iscsi_session *session,
1390 struct iscsi_conn *conn;
1393 spin_lock_bh(&session->frwd_lock);
1394 conn = session->leadconn;
1395 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1396 spin_unlock_bh(&session->frwd_lock);
1400 dev = get_device(&conn->cls_conn->dev);
1401 spin_unlock_bh(&session->frwd_lock);
1405 * if the host is being removed bypass the connection
1406 * recovery initialization because we are going to kill
1409 if (err == ISCSI_ERR_INVALID_HOST)
1410 iscsi_conn_error_event(conn->cls_conn, err);
1412 iscsi_conn_failure(conn, err);
1415 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1417 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1419 struct iscsi_session *session = conn->session;
1421 spin_lock_bh(&session->frwd_lock);
1422 if (session->state == ISCSI_STATE_FAILED) {
1423 spin_unlock_bh(&session->frwd_lock);
1427 if (conn->stop_stage == 0)
1428 session->state = ISCSI_STATE_FAILED;
1429 spin_unlock_bh(&session->frwd_lock);
1431 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1432 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1433 iscsi_conn_error_event(conn->cls_conn, err);
1435 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1437 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1439 struct iscsi_session *session = conn->session;
1442 * Check for iSCSI window and take care of CmdSN wrap-around
1444 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1445 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1446 "%u MaxCmdSN %u CmdSN %u/%u\n",
1447 session->exp_cmdsn, session->max_cmdsn,
1448 session->cmdsn, session->queued_cmdsn);
1454 static int iscsi_xmit_task(struct iscsi_conn *conn)
1456 struct iscsi_task *task = conn->task;
1459 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1462 spin_lock_bh(&conn->session->back_lock);
1463 if (conn->task == NULL) {
1464 spin_unlock_bh(&conn->session->back_lock);
1467 __iscsi_get_task(task);
1468 spin_unlock_bh(&conn->session->back_lock);
1469 spin_unlock_bh(&conn->session->frwd_lock);
1470 rc = conn->session->tt->xmit_task(task);
1471 spin_lock_bh(&conn->session->frwd_lock);
1473 /* done with this task */
1474 task->last_xfer = jiffies;
1477 /* regular RX path uses back_lock */
1478 spin_lock(&conn->session->back_lock);
1479 __iscsi_put_task(task);
1480 spin_unlock(&conn->session->back_lock);
1485 * iscsi_requeue_task - requeue task to run from session workqueue
1486 * @task: task to requeue
1488 * LLDs that need to run a task from the session workqueue should call
1489 * this. The session frwd_lock must be held. This should only be called
1490 * by software drivers.
1492 void iscsi_requeue_task(struct iscsi_task *task)
1494 struct iscsi_conn *conn = task->conn;
1497 * this may be on the requeue list already if the xmit_task callout
1498 * is handling the r2ts while we are adding new ones
1500 spin_lock_bh(&conn->taskqueuelock);
1501 if (list_empty(&task->running))
1502 list_add_tail(&task->running, &conn->requeue);
1503 spin_unlock_bh(&conn->taskqueuelock);
1504 iscsi_conn_queue_work(conn);
1506 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1509 * iscsi_data_xmit - xmit any command into the scheduled connection
1510 * @conn: iscsi connection
1513 * The function can return -EAGAIN in which case the caller must
1514 * re-schedule it again later or recover. '0' return code means
1517 static int iscsi_data_xmit(struct iscsi_conn *conn)
1519 struct iscsi_task *task;
1522 spin_lock_bh(&conn->session->frwd_lock);
1523 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1524 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1525 spin_unlock_bh(&conn->session->frwd_lock);
1530 rc = iscsi_xmit_task(conn);
1536 * process mgmt pdus like nops before commands since we should
1537 * only have one nop-out as a ping from us and targets should not
1538 * overflow us with nop-ins
1540 spin_lock_bh(&conn->taskqueuelock);
1542 while (!list_empty(&conn->mgmtqueue)) {
1543 conn->task = list_entry(conn->mgmtqueue.next,
1544 struct iscsi_task, running);
1545 list_del_init(&conn->task->running);
1546 spin_unlock_bh(&conn->taskqueuelock);
1547 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1548 /* regular RX path uses back_lock */
1549 spin_lock_bh(&conn->session->back_lock);
1550 __iscsi_put_task(conn->task);
1551 spin_unlock_bh(&conn->session->back_lock);
1553 spin_lock_bh(&conn->taskqueuelock);
1556 rc = iscsi_xmit_task(conn);
1559 spin_lock_bh(&conn->taskqueuelock);
1562 /* process pending command queue */
1563 while (!list_empty(&conn->cmdqueue)) {
1564 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1566 list_del_init(&conn->task->running);
1567 spin_unlock_bh(&conn->taskqueuelock);
1568 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1569 fail_scsi_task(conn->task, DID_IMM_RETRY);
1570 spin_lock_bh(&conn->taskqueuelock);
1573 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1575 if (rc == -ENOMEM || rc == -EACCES) {
1576 spin_lock_bh(&conn->taskqueuelock);
1577 list_add_tail(&conn->task->running,
1580 spin_unlock_bh(&conn->taskqueuelock);
1583 fail_scsi_task(conn->task, DID_ABORT);
1584 spin_lock_bh(&conn->taskqueuelock);
1587 rc = iscsi_xmit_task(conn);
1591 * we could continuously get new task requests so
1592 * we need to check the mgmt queue for nops that need to
1593 * be sent to aviod starvation
1595 spin_lock_bh(&conn->taskqueuelock);
1596 if (!list_empty(&conn->mgmtqueue))
1600 while (!list_empty(&conn->requeue)) {
1602 * we always do fastlogout - conn stop code will clean up.
1604 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1607 task = list_entry(conn->requeue.next, struct iscsi_task,
1609 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1613 list_del_init(&conn->task->running);
1614 conn->task->state = ISCSI_TASK_RUNNING;
1615 spin_unlock_bh(&conn->taskqueuelock);
1616 rc = iscsi_xmit_task(conn);
1619 spin_lock_bh(&conn->taskqueuelock);
1620 if (!list_empty(&conn->mgmtqueue))
1623 spin_unlock_bh(&conn->taskqueuelock);
1624 spin_unlock_bh(&conn->session->frwd_lock);
1628 spin_unlock_bh(&conn->session->frwd_lock);
1632 static void iscsi_xmitworker(struct work_struct *work)
1634 struct iscsi_conn *conn =
1635 container_of(work, struct iscsi_conn, xmitwork);
1638 * serialize Xmit worker on a per-connection basis.
1641 rc = iscsi_data_xmit(conn);
1642 } while (rc >= 0 || rc == -EAGAIN);
1645 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1646 struct scsi_cmnd *sc)
1648 struct iscsi_task *task;
1650 if (!kfifo_out(&conn->session->cmdpool.queue,
1651 (void *) &task, sizeof(void *)))
1654 sc->SCp.phase = conn->session->age;
1655 sc->SCp.ptr = (char *) task;
1657 refcount_set(&task->refcount, 1);
1658 task->state = ISCSI_TASK_PENDING;
1661 task->have_checked_conn = false;
1662 task->last_timeout = jiffies;
1663 task->last_xfer = jiffies;
1664 task->protected = false;
1665 INIT_LIST_HEAD(&task->running);
1670 FAILURE_BAD_HOST = 1,
1671 FAILURE_SESSION_FAILED,
1672 FAILURE_SESSION_FREED,
1673 FAILURE_WINDOW_CLOSED,
1675 FAILURE_SESSION_TERMINATE,
1676 FAILURE_SESSION_IN_RECOVERY,
1677 FAILURE_SESSION_RECOVERY_TIMEOUT,
1678 FAILURE_SESSION_LOGGING_OUT,
1679 FAILURE_SESSION_NOT_READY,
1682 int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1684 struct iscsi_cls_session *cls_session;
1685 struct iscsi_host *ihost;
1687 struct iscsi_session *session;
1688 struct iscsi_conn *conn;
1689 struct iscsi_task *task = NULL;
1694 ihost = shost_priv(host);
1696 cls_session = starget_to_session(scsi_target(sc->device));
1697 session = cls_session->dd_data;
1698 spin_lock_bh(&session->frwd_lock);
1700 reason = iscsi_session_chkready(cls_session);
1702 sc->result = reason;
1706 if (session->state != ISCSI_STATE_LOGGED_IN) {
1708 * to handle the race between when we set the recovery state
1709 * and block the session we requeue here (commands could
1710 * be entering our queuecommand while a block is starting
1711 * up because the block code is not locked)
1713 switch (session->state) {
1714 case ISCSI_STATE_FAILED:
1716 * cmds should fail during shutdown, if the session
1717 * state is bad, allowing completion to happen
1719 if (unlikely(system_state != SYSTEM_RUNNING)) {
1720 reason = FAILURE_SESSION_FAILED;
1721 sc->result = DID_NO_CONNECT << 16;
1725 case ISCSI_STATE_IN_RECOVERY:
1726 reason = FAILURE_SESSION_IN_RECOVERY;
1727 sc->result = DID_IMM_RETRY << 16;
1729 case ISCSI_STATE_LOGGING_OUT:
1730 reason = FAILURE_SESSION_LOGGING_OUT;
1731 sc->result = DID_IMM_RETRY << 16;
1733 case ISCSI_STATE_RECOVERY_FAILED:
1734 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1735 sc->result = DID_TRANSPORT_FAILFAST << 16;
1737 case ISCSI_STATE_TERMINATE:
1738 reason = FAILURE_SESSION_TERMINATE;
1739 sc->result = DID_NO_CONNECT << 16;
1742 reason = FAILURE_SESSION_FREED;
1743 sc->result = DID_NO_CONNECT << 16;
1748 conn = session->leadconn;
1750 reason = FAILURE_SESSION_FREED;
1751 sc->result = DID_NO_CONNECT << 16;
1755 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1756 reason = FAILURE_SESSION_IN_RECOVERY;
1757 sc->result = DID_REQUEUE << 16;
1761 if (iscsi_check_cmdsn_window_closed(conn)) {
1762 reason = FAILURE_WINDOW_CLOSED;
1766 task = iscsi_alloc_task(conn, sc);
1768 reason = FAILURE_OOM;
1772 if (!ihost->workq) {
1773 reason = iscsi_prep_scsi_cmd_pdu(task);
1775 if (reason == -ENOMEM || reason == -EACCES) {
1776 reason = FAILURE_OOM;
1779 sc->result = DID_ABORT << 16;
1783 if (session->tt->xmit_task(task)) {
1785 reason = FAILURE_SESSION_NOT_READY;
1789 spin_lock_bh(&conn->taskqueuelock);
1790 list_add_tail(&task->running, &conn->cmdqueue);
1791 spin_unlock_bh(&conn->taskqueuelock);
1792 iscsi_conn_queue_work(conn);
1795 session->queued_cmdsn++;
1796 spin_unlock_bh(&session->frwd_lock);
1800 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1802 spin_unlock_bh(&session->frwd_lock);
1803 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1804 sc->cmnd[0], reason);
1805 return SCSI_MLQUEUE_TARGET_BUSY;
1808 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1810 spin_unlock_bh(&session->frwd_lock);
1811 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1812 sc->cmnd[0], reason);
1813 if (!scsi_bidi_cmnd(sc))
1814 scsi_set_resid(sc, scsi_bufflen(sc));
1816 scsi_out(sc)->resid = scsi_out(sc)->length;
1817 scsi_in(sc)->resid = scsi_in(sc)->length;
1822 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1824 int iscsi_target_alloc(struct scsi_target *starget)
1826 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1827 struct iscsi_session *session = cls_session->dd_data;
1829 starget->can_queue = session->scsi_cmds_max;
1832 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1834 static void iscsi_tmf_timedout(struct timer_list *t)
1836 struct iscsi_conn *conn = from_timer(conn, t, tmf_timer);
1837 struct iscsi_session *session = conn->session;
1839 spin_lock(&session->frwd_lock);
1840 if (conn->tmf_state == TMF_QUEUED) {
1841 conn->tmf_state = TMF_TIMEDOUT;
1842 ISCSI_DBG_EH(session, "tmf timedout\n");
1843 /* unblock eh_abort() */
1844 wake_up(&conn->ehwait);
1846 spin_unlock(&session->frwd_lock);
1849 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1850 struct iscsi_tm *hdr, int age,
1852 __must_hold(&session->frwd_lock)
1854 struct iscsi_session *session = conn->session;
1855 struct iscsi_task *task;
1857 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1860 spin_unlock_bh(&session->frwd_lock);
1861 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1862 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1863 spin_lock_bh(&session->frwd_lock);
1866 conn->tmfcmd_pdus_cnt++;
1867 conn->tmf_timer.expires = timeout * HZ + jiffies;
1868 add_timer(&conn->tmf_timer);
1869 ISCSI_DBG_EH(session, "tmf set timeout\n");
1871 spin_unlock_bh(&session->frwd_lock);
1872 mutex_unlock(&session->eh_mutex);
1875 * block eh thread until:
1879 * 3) session is terminated or restarted or userspace has
1880 * given up on recovery
1882 wait_event_interruptible(conn->ehwait, age != session->age ||
1883 session->state != ISCSI_STATE_LOGGED_IN ||
1884 conn->tmf_state != TMF_QUEUED);
1885 if (signal_pending(current))
1886 flush_signals(current);
1887 del_timer_sync(&conn->tmf_timer);
1889 mutex_lock(&session->eh_mutex);
1890 spin_lock_bh(&session->frwd_lock);
1891 /* if the session drops it will clean up the task */
1892 if (age != session->age ||
1893 session->state != ISCSI_STATE_LOGGED_IN)
1899 * Fail commands. session lock held and recv side suspended and xmit
1902 static void fail_scsi_tasks(struct iscsi_conn *conn, u64 lun, int error)
1904 struct iscsi_task *task;
1907 for (i = 0; i < conn->session->cmds_max; i++) {
1908 task = conn->session->cmds[i];
1909 if (!task->sc || task->state == ISCSI_TASK_FREE)
1912 if (lun != -1 && lun != task->sc->device->lun)
1915 ISCSI_DBG_SESSION(conn->session,
1916 "failing sc %p itt 0x%x state %d\n",
1917 task->sc, task->itt, task->state);
1918 fail_scsi_task(task, error);
1923 * iscsi_suspend_queue - suspend iscsi_queuecommand
1924 * @conn: iscsi conn to stop queueing IO on
1926 * This grabs the session frwd_lock to make sure no one is in
1927 * xmit_task/queuecommand, and then sets suspend to prevent
1928 * new commands from being queued. This only needs to be called
1929 * by offload drivers that need to sync a path like ep disconnect
1930 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1931 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1933 void iscsi_suspend_queue(struct iscsi_conn *conn)
1935 spin_lock_bh(&conn->session->frwd_lock);
1936 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1937 spin_unlock_bh(&conn->session->frwd_lock);
1939 EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1942 * iscsi_suspend_tx - suspend iscsi_data_xmit
1943 * @conn: iscsi conn tp stop processing IO on.
1945 * This function sets the suspend bit to prevent iscsi_data_xmit
1946 * from sending new IO, and if work is queued on the xmit thread
1947 * it will wait for it to be completed.
1949 void iscsi_suspend_tx(struct iscsi_conn *conn)
1951 struct Scsi_Host *shost = conn->session->host;
1952 struct iscsi_host *ihost = shost_priv(shost);
1954 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1956 flush_workqueue(ihost->workq);
1958 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1960 static void iscsi_start_tx(struct iscsi_conn *conn)
1962 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1963 iscsi_conn_queue_work(conn);
1967 * We want to make sure a ping is in flight. It has timed out.
1968 * And we are not busy processing a pdu that is making
1969 * progress but got started before the ping and is taking a while
1970 * to complete so the ping is just stuck behind it in a queue.
1972 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1974 if (conn->ping_task &&
1975 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1976 (conn->ping_timeout * HZ), jiffies))
1982 enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1984 enum blk_eh_timer_return rc = BLK_EH_DONE;
1985 struct iscsi_task *task = NULL, *running_task;
1986 struct iscsi_cls_session *cls_session;
1987 struct iscsi_session *session;
1988 struct iscsi_conn *conn;
1991 cls_session = starget_to_session(scsi_target(sc->device));
1992 session = cls_session->dd_data;
1994 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1996 spin_lock(&session->frwd_lock);
1997 task = (struct iscsi_task *)sc->SCp.ptr;
2000 * Raced with completion. Blk layer has taken ownership
2001 * so let timeout code complete it now.
2007 if (session->state != ISCSI_STATE_LOGGED_IN) {
2009 * During shutdown, if session is prematurely disconnected,
2010 * recovery won't happen and there will be hung cmds. Not
2011 * handling cmds would trigger EH, also bad in this case.
2012 * Instead, handle cmd, allow completion to happen and let
2013 * upper layer to deal with the result.
2015 if (unlikely(system_state != SYSTEM_RUNNING)) {
2016 sc->result = DID_NO_CONNECT << 16;
2017 ISCSI_DBG_EH(session, "sc on shutdown, handled\n");
2022 * We are probably in the middle of iscsi recovery so let
2023 * that complete and handle the error.
2025 rc = BLK_EH_RESET_TIMER;
2029 conn = session->leadconn;
2031 /* In the middle of shuting down */
2032 rc = BLK_EH_RESET_TIMER;
2037 * If we have sent (at least queued to the network layer) a pdu or
2038 * recvd one for the task since the last timeout ask for
2039 * more time. If on the next timeout we have not made progress
2040 * we can check if it is the task or connection when we send the
2043 if (time_after(task->last_xfer, task->last_timeout)) {
2044 ISCSI_DBG_EH(session, "Command making progress. Asking "
2045 "scsi-ml for more time to complete. "
2046 "Last data xfer at %lu. Last timeout was at "
2047 "%lu\n.", task->last_xfer, task->last_timeout);
2048 task->have_checked_conn = false;
2049 rc = BLK_EH_RESET_TIMER;
2053 if (!conn->recv_timeout && !conn->ping_timeout)
2056 * if the ping timedout then we are in the middle of cleaning up
2057 * and can let the iscsi eh handle it
2059 if (iscsi_has_ping_timed_out(conn)) {
2060 rc = BLK_EH_RESET_TIMER;
2064 for (i = 0; i < conn->session->cmds_max; i++) {
2065 running_task = conn->session->cmds[i];
2066 if (!running_task->sc || running_task == task ||
2067 running_task->state != ISCSI_TASK_RUNNING)
2071 * Only check if cmds started before this one have made
2072 * progress, or this could never fail
2074 if (time_after(running_task->sc->jiffies_at_alloc,
2075 task->sc->jiffies_at_alloc))
2078 if (time_after(running_task->last_xfer, task->last_timeout)) {
2080 * This task has not made progress, but a task
2081 * started before us has transferred data since
2082 * we started/last-checked. We could be queueing
2083 * too many tasks or the LU is bad.
2085 * If the device is bad the cmds ahead of us on
2086 * other devs will complete, and this loop will
2087 * eventually fail starting the scsi eh.
2089 ISCSI_DBG_EH(session, "Command has not made progress "
2090 "but commands ahead of it have. "
2091 "Asking scsi-ml for more time to "
2092 "complete. Our last xfer vs running task "
2093 "last xfer %lu/%lu. Last check %lu.\n",
2094 task->last_xfer, running_task->last_xfer,
2095 task->last_timeout);
2096 rc = BLK_EH_RESET_TIMER;
2101 /* Assumes nop timeout is shorter than scsi cmd timeout */
2102 if (task->have_checked_conn)
2106 * Checking the transport already or nop from a cmd timeout still
2109 if (conn->ping_task) {
2110 task->have_checked_conn = true;
2111 rc = BLK_EH_RESET_TIMER;
2115 /* Make sure there is a transport check done */
2116 iscsi_send_nopout(conn, NULL);
2117 task->have_checked_conn = true;
2118 rc = BLK_EH_RESET_TIMER;
2122 task->last_timeout = jiffies;
2123 spin_unlock(&session->frwd_lock);
2124 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2125 "timer reset" : "shutdown or nh");
2128 EXPORT_SYMBOL_GPL(iscsi_eh_cmd_timed_out);
2130 static void iscsi_check_transport_timeouts(struct timer_list *t)
2132 struct iscsi_conn *conn = from_timer(conn, t, transport_timer);
2133 struct iscsi_session *session = conn->session;
2134 unsigned long recv_timeout, next_timeout = 0, last_recv;
2136 spin_lock(&session->frwd_lock);
2137 if (session->state != ISCSI_STATE_LOGGED_IN)
2140 recv_timeout = conn->recv_timeout;
2145 last_recv = conn->last_recv;
2147 if (iscsi_has_ping_timed_out(conn)) {
2148 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
2149 "expired, recv timeout %d, last rx %lu, "
2150 "last ping %lu, now %lu\n",
2151 conn->ping_timeout, conn->recv_timeout,
2152 last_recv, conn->last_ping, jiffies);
2153 spin_unlock(&session->frwd_lock);
2154 iscsi_conn_failure(conn, ISCSI_ERR_NOP_TIMEDOUT);
2158 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2159 /* send a ping to try to provoke some traffic */
2160 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2161 if (iscsi_send_nopout(conn, NULL))
2162 next_timeout = jiffies + (1 * HZ);
2164 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2166 next_timeout = last_recv + recv_timeout;
2168 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2169 mod_timer(&conn->transport_timer, next_timeout);
2171 spin_unlock(&session->frwd_lock);
2174 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
2175 struct iscsi_tm *hdr)
2177 memset(hdr, 0, sizeof(*hdr));
2178 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2179 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2180 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2181 hdr->lun = task->lun;
2182 hdr->rtt = task->hdr_itt;
2183 hdr->refcmdsn = task->cmdsn;
2186 int iscsi_eh_abort(struct scsi_cmnd *sc)
2188 struct iscsi_cls_session *cls_session;
2189 struct iscsi_session *session;
2190 struct iscsi_conn *conn;
2191 struct iscsi_task *task;
2192 struct iscsi_tm *hdr;
2195 cls_session = starget_to_session(scsi_target(sc->device));
2196 session = cls_session->dd_data;
2198 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2200 mutex_lock(&session->eh_mutex);
2201 spin_lock_bh(&session->frwd_lock);
2203 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2207 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2209 spin_unlock_bh(&session->frwd_lock);
2210 mutex_unlock(&session->eh_mutex);
2215 * If we are not logged in or we have started a new session
2216 * then let the host reset code handle this
2218 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2219 sc->SCp.phase != session->age) {
2220 spin_unlock_bh(&session->frwd_lock);
2221 mutex_unlock(&session->eh_mutex);
2222 ISCSI_DBG_EH(session, "failing abort due to dropped "
2227 conn = session->leadconn;
2228 conn->eh_abort_cnt++;
2231 task = (struct iscsi_task *)sc->SCp.ptr;
2232 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2235 /* task completed before time out */
2237 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2241 if (task->state == ISCSI_TASK_PENDING) {
2242 fail_scsi_task(task, DID_ABORT);
2246 /* only have one tmf outstanding at a time */
2247 if (conn->tmf_state != TMF_INITIAL)
2249 conn->tmf_state = TMF_QUEUED;
2252 iscsi_prep_abort_task_pdu(task, hdr);
2254 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout))
2257 switch (conn->tmf_state) {
2259 spin_unlock_bh(&session->frwd_lock);
2261 * stop tx side incase the target had sent a abort rsp but
2262 * the initiator was still writing out data.
2264 iscsi_suspend_tx(conn);
2266 * we do not stop the recv side because targets have been
2267 * good and have never sent us a successful tmf response
2268 * then sent more data for the cmd.
2270 spin_lock_bh(&session->frwd_lock);
2271 fail_scsi_task(task, DID_ABORT);
2272 conn->tmf_state = TMF_INITIAL;
2273 memset(hdr, 0, sizeof(*hdr));
2274 spin_unlock_bh(&session->frwd_lock);
2275 iscsi_start_tx(conn);
2276 goto success_unlocked;
2278 spin_unlock_bh(&session->frwd_lock);
2279 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2280 goto failed_unlocked;
2283 conn->tmf_state = TMF_INITIAL;
2284 memset(hdr, 0, sizeof(*hdr));
2285 /* task completed before tmf abort response */
2286 ISCSI_DBG_EH(session, "sc completed while abort in "
2292 conn->tmf_state = TMF_INITIAL;
2297 spin_unlock_bh(&session->frwd_lock);
2299 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2301 mutex_unlock(&session->eh_mutex);
2305 spin_unlock_bh(&session->frwd_lock);
2307 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2308 task ? task->itt : 0);
2309 mutex_unlock(&session->eh_mutex);
2312 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2314 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2316 memset(hdr, 0, sizeof(*hdr));
2317 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2318 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2319 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2320 int_to_scsilun(sc->device->lun, &hdr->lun);
2321 hdr->rtt = RESERVED_ITT;
2324 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2326 struct iscsi_cls_session *cls_session;
2327 struct iscsi_session *session;
2328 struct iscsi_conn *conn;
2329 struct iscsi_tm *hdr;
2332 cls_session = starget_to_session(scsi_target(sc->device));
2333 session = cls_session->dd_data;
2335 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %llu]\n", sc,
2338 mutex_lock(&session->eh_mutex);
2339 spin_lock_bh(&session->frwd_lock);
2341 * Just check if we are not logged in. We cannot check for
2342 * the phase because the reset could come from a ioctl.
2344 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2346 conn = session->leadconn;
2348 /* only have one tmf outstanding at a time */
2349 if (conn->tmf_state != TMF_INITIAL)
2351 conn->tmf_state = TMF_QUEUED;
2354 iscsi_prep_lun_reset_pdu(sc, hdr);
2356 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2357 session->lu_reset_timeout)) {
2362 switch (conn->tmf_state) {
2366 spin_unlock_bh(&session->frwd_lock);
2367 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2370 conn->tmf_state = TMF_INITIAL;
2375 spin_unlock_bh(&session->frwd_lock);
2377 iscsi_suspend_tx(conn);
2379 spin_lock_bh(&session->frwd_lock);
2380 memset(hdr, 0, sizeof(*hdr));
2381 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2382 conn->tmf_state = TMF_INITIAL;
2383 spin_unlock_bh(&session->frwd_lock);
2385 iscsi_start_tx(conn);
2389 spin_unlock_bh(&session->frwd_lock);
2391 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2392 rc == SUCCESS ? "SUCCESS" : "FAILED");
2393 mutex_unlock(&session->eh_mutex);
2396 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2398 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2400 struct iscsi_session *session = cls_session->dd_data;
2402 spin_lock_bh(&session->frwd_lock);
2403 if (session->state != ISCSI_STATE_LOGGED_IN) {
2404 session->state = ISCSI_STATE_RECOVERY_FAILED;
2405 if (session->leadconn)
2406 wake_up(&session->leadconn->ehwait);
2408 spin_unlock_bh(&session->frwd_lock);
2410 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2413 * iscsi_eh_session_reset - drop session and attempt relogin
2416 * This function will wait for a relogin, session termination from
2417 * userspace, or a recovery/replacement timeout.
2419 int iscsi_eh_session_reset(struct scsi_cmnd *sc)
2421 struct iscsi_cls_session *cls_session;
2422 struct iscsi_session *session;
2423 struct iscsi_conn *conn;
2425 cls_session = starget_to_session(scsi_target(sc->device));
2426 session = cls_session->dd_data;
2427 conn = session->leadconn;
2429 mutex_lock(&session->eh_mutex);
2430 spin_lock_bh(&session->frwd_lock);
2431 if (session->state == ISCSI_STATE_TERMINATE) {
2433 ISCSI_DBG_EH(session,
2434 "failing session reset: Could not log back into "
2435 "%s [age %d]\n", session->targetname,
2437 spin_unlock_bh(&session->frwd_lock);
2438 mutex_unlock(&session->eh_mutex);
2442 spin_unlock_bh(&session->frwd_lock);
2443 mutex_unlock(&session->eh_mutex);
2445 * we drop the lock here but the leadconn cannot be destoyed while
2446 * we are in the scsi eh
2448 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2450 ISCSI_DBG_EH(session, "wait for relogin\n");
2451 wait_event_interruptible(conn->ehwait,
2452 session->state == ISCSI_STATE_TERMINATE ||
2453 session->state == ISCSI_STATE_LOGGED_IN ||
2454 session->state == ISCSI_STATE_RECOVERY_FAILED);
2455 if (signal_pending(current))
2456 flush_signals(current);
2458 mutex_lock(&session->eh_mutex);
2459 spin_lock_bh(&session->frwd_lock);
2460 if (session->state == ISCSI_STATE_LOGGED_IN) {
2461 ISCSI_DBG_EH(session,
2462 "session reset succeeded for %s,%s\n",
2463 session->targetname, conn->persistent_address);
2466 spin_unlock_bh(&session->frwd_lock);
2467 mutex_unlock(&session->eh_mutex);
2470 EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
2472 static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2474 memset(hdr, 0, sizeof(*hdr));
2475 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2476 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2477 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2478 hdr->rtt = RESERVED_ITT;
2482 * iscsi_eh_target_reset - reset target
2485 * This will attempt to send a warm target reset.
2487 static int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2489 struct iscsi_cls_session *cls_session;
2490 struct iscsi_session *session;
2491 struct iscsi_conn *conn;
2492 struct iscsi_tm *hdr;
2495 cls_session = starget_to_session(scsi_target(sc->device));
2496 session = cls_session->dd_data;
2498 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2499 session->targetname);
2501 mutex_lock(&session->eh_mutex);
2502 spin_lock_bh(&session->frwd_lock);
2504 * Just check if we are not logged in. We cannot check for
2505 * the phase because the reset could come from a ioctl.
2507 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2509 conn = session->leadconn;
2511 /* only have one tmf outstanding at a time */
2512 if (conn->tmf_state != TMF_INITIAL)
2514 conn->tmf_state = TMF_QUEUED;
2517 iscsi_prep_tgt_reset_pdu(sc, hdr);
2519 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2520 session->tgt_reset_timeout)) {
2525 switch (conn->tmf_state) {
2529 spin_unlock_bh(&session->frwd_lock);
2530 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2533 conn->tmf_state = TMF_INITIAL;
2538 spin_unlock_bh(&session->frwd_lock);
2540 iscsi_suspend_tx(conn);
2542 spin_lock_bh(&session->frwd_lock);
2543 memset(hdr, 0, sizeof(*hdr));
2544 fail_scsi_tasks(conn, -1, DID_ERROR);
2545 conn->tmf_state = TMF_INITIAL;
2546 spin_unlock_bh(&session->frwd_lock);
2548 iscsi_start_tx(conn);
2552 spin_unlock_bh(&session->frwd_lock);
2554 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2555 rc == SUCCESS ? "SUCCESS" : "FAILED");
2556 mutex_unlock(&session->eh_mutex);
2561 * iscsi_eh_recover_target - reset target and possibly the session
2564 * This will attempt to send a warm target reset. If that fails,
2565 * we will escalate to ERL0 session recovery.
2567 int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2571 rc = iscsi_eh_target_reset(sc);
2573 rc = iscsi_eh_session_reset(sc);
2576 EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2579 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2580 * should be accessed via kfifo_{get,put} on q->queue.
2581 * Optionally, the caller can obtain the array of object pointers
2582 * by passing in a non-NULL @items pointer
2585 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2587 int i, num_arrays = 1;
2589 memset(q, 0, sizeof(*q));
2593 /* If the user passed an items pointer, he wants a copy of
2597 q->pool = kvcalloc(num_arrays * max, sizeof(void *), GFP_KERNEL);
2598 if (q->pool == NULL)
2601 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2603 for (i = 0; i < max; i++) {
2604 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2605 if (q->pool[i] == NULL) {
2609 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2613 *items = q->pool + max;
2614 memcpy(*items, q->pool, max * sizeof(void *));
2623 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2625 void iscsi_pool_free(struct iscsi_pool *q)
2629 for (i = 0; i < q->max; i++)
2633 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2636 * iscsi_host_add - add host to system
2638 * @pdev: parent device
2640 * This should be called by partial offload and software iscsi drivers
2641 * to add a host to the system.
2643 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2645 if (!shost->can_queue)
2646 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2648 if (!shost->cmd_per_lun)
2649 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2651 return scsi_add_host(shost, pdev);
2653 EXPORT_SYMBOL_GPL(iscsi_host_add);
2656 * iscsi_host_alloc - allocate a host and driver data
2657 * @sht: scsi host template
2658 * @dd_data_size: driver host data size
2659 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2661 * This should be called by partial offload and software iscsi drivers.
2662 * To access the driver specific memory use the iscsi_host_priv() macro.
2664 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2665 int dd_data_size, bool xmit_can_sleep)
2667 struct Scsi_Host *shost;
2668 struct iscsi_host *ihost;
2670 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2673 ihost = shost_priv(shost);
2675 if (xmit_can_sleep) {
2676 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2677 "iscsi_q_%d", shost->host_no);
2678 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2683 spin_lock_init(&ihost->lock);
2684 ihost->state = ISCSI_HOST_SETUP;
2685 ihost->num_sessions = 0;
2686 init_waitqueue_head(&ihost->session_removal_wq);
2690 scsi_host_put(shost);
2693 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2695 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2697 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2701 * iscsi_host_remove - remove host and sessions
2704 * If there are any sessions left, this will initiate the removal and wait
2705 * for the completion.
2707 void iscsi_host_remove(struct Scsi_Host *shost)
2709 struct iscsi_host *ihost = shost_priv(shost);
2710 unsigned long flags;
2712 spin_lock_irqsave(&ihost->lock, flags);
2713 ihost->state = ISCSI_HOST_REMOVED;
2714 spin_unlock_irqrestore(&ihost->lock, flags);
2716 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2717 wait_event_interruptible(ihost->session_removal_wq,
2718 ihost->num_sessions == 0);
2719 if (signal_pending(current))
2720 flush_signals(current);
2722 scsi_remove_host(shost);
2724 destroy_workqueue(ihost->workq);
2726 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2728 void iscsi_host_free(struct Scsi_Host *shost)
2730 struct iscsi_host *ihost = shost_priv(shost);
2732 kfree(ihost->netdev);
2733 kfree(ihost->hwaddress);
2734 kfree(ihost->initiatorname);
2735 scsi_host_put(shost);
2737 EXPORT_SYMBOL_GPL(iscsi_host_free);
2739 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2741 struct iscsi_host *ihost = shost_priv(shost);
2742 unsigned long flags;
2744 shost = scsi_host_get(shost);
2746 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2747 "of session teardown event because host already "
2752 spin_lock_irqsave(&ihost->lock, flags);
2753 ihost->num_sessions--;
2754 if (ihost->num_sessions == 0)
2755 wake_up(&ihost->session_removal_wq);
2756 spin_unlock_irqrestore(&ihost->lock, flags);
2757 scsi_host_put(shost);
2761 * iscsi_session_setup - create iscsi cls session and host and session
2762 * @iscsit: iscsi transport template
2764 * @cmds_max: session can queue
2765 * @dd_size: private driver data size, added to session allocation size
2766 * @cmd_task_size: LLD task private data size
2767 * @initial_cmdsn: initial CmdSN
2768 * @id: target ID to add to this session
2770 * This can be used by software iscsi_transports that allocate
2771 * a session per scsi host.
2773 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2774 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2775 * for nop handling and login/logout requests.
2777 struct iscsi_cls_session *
2778 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2779 uint16_t cmds_max, int dd_size, int cmd_task_size,
2780 uint32_t initial_cmdsn, unsigned int id)
2782 struct iscsi_host *ihost = shost_priv(shost);
2783 struct iscsi_session *session;
2784 struct iscsi_cls_session *cls_session;
2785 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2786 unsigned long flags;
2788 spin_lock_irqsave(&ihost->lock, flags);
2789 if (ihost->state == ISCSI_HOST_REMOVED) {
2790 spin_unlock_irqrestore(&ihost->lock, flags);
2793 ihost->num_sessions++;
2794 spin_unlock_irqrestore(&ihost->lock, flags);
2797 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2799 * The iscsi layer needs some tasks for nop handling and tmfs,
2800 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2801 * + 1 command for scsi IO.
2803 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2804 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2805 "must be a power of two that is at least %d.\n",
2806 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2807 goto dec_session_count;
2810 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2811 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2812 "must be a power of 2 less than or equal to %d.\n",
2813 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2814 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2817 if (!is_power_of_2(total_cmds)) {
2818 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2819 "must be a power of 2.\n", total_cmds);
2820 total_cmds = rounddown_pow_of_two(total_cmds);
2821 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2823 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2826 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2828 cls_session = iscsi_alloc_session(shost, iscsit,
2829 sizeof(struct iscsi_session) +
2832 goto dec_session_count;
2833 session = cls_session->dd_data;
2834 session->cls_session = cls_session;
2835 session->host = shost;
2836 session->state = ISCSI_STATE_FREE;
2837 session->fast_abort = 1;
2838 session->tgt_reset_timeout = 30;
2839 session->lu_reset_timeout = 15;
2840 session->abort_timeout = 10;
2841 session->scsi_cmds_max = scsi_cmds;
2842 session->cmds_max = total_cmds;
2843 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2844 session->exp_cmdsn = initial_cmdsn + 1;
2845 session->max_cmdsn = initial_cmdsn + 1;
2846 session->max_r2t = 1;
2847 session->tt = iscsit;
2848 session->dd_data = cls_session->dd_data + sizeof(*session);
2850 mutex_init(&session->eh_mutex);
2851 spin_lock_init(&session->frwd_lock);
2852 spin_lock_init(&session->back_lock);
2854 /* initialize SCSI PDU commands pool */
2855 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2856 (void***)&session->cmds,
2857 cmd_task_size + sizeof(struct iscsi_task)))
2858 goto cmdpool_alloc_fail;
2860 /* pre-format cmds pool with ITT */
2861 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2862 struct iscsi_task *task = session->cmds[cmd_i];
2865 task->dd_data = &task[1];
2867 task->state = ISCSI_TASK_FREE;
2868 INIT_LIST_HEAD(&task->running);
2871 if (!try_module_get(iscsit->owner))
2872 goto module_get_fail;
2874 if (iscsi_add_session(cls_session, id))
2875 goto cls_session_fail;
2880 module_put(iscsit->owner);
2882 iscsi_pool_free(&session->cmdpool);
2884 iscsi_free_session(cls_session);
2886 iscsi_host_dec_session_cnt(shost);
2889 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2892 * iscsi_session_teardown - destroy session, host, and cls_session
2893 * @cls_session: iscsi session
2895 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2897 struct iscsi_session *session = cls_session->dd_data;
2898 struct module *owner = cls_session->transport->owner;
2899 struct Scsi_Host *shost = session->host;
2901 iscsi_pool_free(&session->cmdpool);
2903 iscsi_remove_session(cls_session);
2905 kfree(session->password);
2906 kfree(session->password_in);
2907 kfree(session->username);
2908 kfree(session->username_in);
2909 kfree(session->targetname);
2910 kfree(session->targetalias);
2911 kfree(session->initiatorname);
2912 kfree(session->boot_root);
2913 kfree(session->boot_nic);
2914 kfree(session->boot_target);
2915 kfree(session->ifacename);
2916 kfree(session->portal_type);
2917 kfree(session->discovery_parent_type);
2919 iscsi_free_session(cls_session);
2921 iscsi_host_dec_session_cnt(shost);
2924 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2927 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2928 * @cls_session: iscsi_cls_session
2929 * @dd_size: private driver data size
2932 struct iscsi_cls_conn *
2933 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2936 struct iscsi_session *session = cls_session->dd_data;
2937 struct iscsi_conn *conn;
2938 struct iscsi_cls_conn *cls_conn;
2941 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2945 conn = cls_conn->dd_data;
2946 memset(conn, 0, sizeof(*conn) + dd_size);
2948 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2949 conn->session = session;
2950 conn->cls_conn = cls_conn;
2951 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2952 conn->id = conn_idx;
2953 conn->exp_statsn = 0;
2954 conn->tmf_state = TMF_INITIAL;
2956 timer_setup(&conn->transport_timer, iscsi_check_transport_timeouts, 0);
2958 INIT_LIST_HEAD(&conn->mgmtqueue);
2959 INIT_LIST_HEAD(&conn->cmdqueue);
2960 INIT_LIST_HEAD(&conn->requeue);
2961 spin_lock_init(&conn->taskqueuelock);
2962 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2964 /* allocate login_task used for the login/text sequences */
2965 spin_lock_bh(&session->frwd_lock);
2966 if (!kfifo_out(&session->cmdpool.queue,
2967 (void*)&conn->login_task,
2969 spin_unlock_bh(&session->frwd_lock);
2970 goto login_task_alloc_fail;
2972 spin_unlock_bh(&session->frwd_lock);
2974 data = (char *) __get_free_pages(GFP_KERNEL,
2975 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2977 goto login_task_data_alloc_fail;
2978 conn->login_task->data = conn->data = data;
2980 timer_setup(&conn->tmf_timer, iscsi_tmf_timedout, 0);
2981 init_waitqueue_head(&conn->ehwait);
2985 login_task_data_alloc_fail:
2986 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2988 login_task_alloc_fail:
2989 iscsi_destroy_conn(cls_conn);
2992 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2995 * iscsi_conn_teardown - teardown iscsi connection
2996 * @cls_conn: iscsi class connection
2998 * TODO: we may need to make this into a two step process
2999 * like scsi-mls remove + put host
3001 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
3003 struct iscsi_conn *conn = cls_conn->dd_data;
3004 struct iscsi_session *session = conn->session;
3006 del_timer_sync(&conn->transport_timer);
3008 mutex_lock(&session->eh_mutex);
3009 spin_lock_bh(&session->frwd_lock);
3010 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
3011 if (session->leadconn == conn) {
3013 * leading connection? then give up on recovery.
3015 session->state = ISCSI_STATE_TERMINATE;
3016 wake_up(&conn->ehwait);
3018 spin_unlock_bh(&session->frwd_lock);
3020 /* flush queued up work because we free the connection below */
3021 iscsi_suspend_tx(conn);
3023 spin_lock_bh(&session->frwd_lock);
3024 free_pages((unsigned long) conn->data,
3025 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
3026 kfree(conn->persistent_address);
3027 kfree(conn->local_ipaddr);
3028 /* regular RX path uses back_lock */
3029 spin_lock_bh(&session->back_lock);
3030 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
3032 spin_unlock_bh(&session->back_lock);
3033 if (session->leadconn == conn)
3034 session->leadconn = NULL;
3035 spin_unlock_bh(&session->frwd_lock);
3036 mutex_unlock(&session->eh_mutex);
3038 iscsi_destroy_conn(cls_conn);
3040 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
3042 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
3044 struct iscsi_conn *conn = cls_conn->dd_data;
3045 struct iscsi_session *session = conn->session;
3048 iscsi_conn_printk(KERN_ERR, conn,
3049 "can't start unbound connection\n");
3053 if ((session->imm_data_en || !session->initial_r2t_en) &&
3054 session->first_burst > session->max_burst) {
3055 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3056 "first_burst %d max_burst %d\n",
3057 session->first_burst, session->max_burst);
3061 if (conn->ping_timeout && !conn->recv_timeout) {
3062 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3063 "zero. Using 5 seconds\n.");
3064 conn->recv_timeout = 5;
3067 if (conn->recv_timeout && !conn->ping_timeout) {
3068 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3069 "zero. Using 5 seconds.\n");
3070 conn->ping_timeout = 5;
3073 spin_lock_bh(&session->frwd_lock);
3074 conn->c_stage = ISCSI_CONN_STARTED;
3075 session->state = ISCSI_STATE_LOGGED_IN;
3076 session->queued_cmdsn = session->cmdsn;
3078 conn->last_recv = jiffies;
3079 conn->last_ping = jiffies;
3080 if (conn->recv_timeout && conn->ping_timeout)
3081 mod_timer(&conn->transport_timer,
3082 jiffies + (conn->recv_timeout * HZ));
3084 switch(conn->stop_stage) {
3085 case STOP_CONN_RECOVER:
3087 * unblock eh_abort() if it is blocked. re-try all
3088 * commands after successful recovery
3090 conn->stop_stage = 0;
3091 conn->tmf_state = TMF_INITIAL;
3093 if (session->age == 16)
3096 case STOP_CONN_TERM:
3097 conn->stop_stage = 0;
3102 spin_unlock_bh(&session->frwd_lock);
3104 iscsi_unblock_session(session->cls_session);
3105 wake_up(&conn->ehwait);
3108 EXPORT_SYMBOL_GPL(iscsi_conn_start);
3111 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3113 struct iscsi_task *task;
3116 for (i = 0; i < conn->session->cmds_max; i++) {
3117 task = conn->session->cmds[i];
3121 if (task->state == ISCSI_TASK_FREE)
3124 ISCSI_DBG_SESSION(conn->session,
3125 "failing mgmt itt 0x%x state %d\n",
3126 task->itt, task->state);
3127 state = ISCSI_TASK_ABRT_SESS_RECOV;
3128 if (task->state == ISCSI_TASK_PENDING)
3129 state = ISCSI_TASK_COMPLETED;
3130 iscsi_complete_task(task, state);
3135 static void iscsi_start_session_recovery(struct iscsi_session *session,
3136 struct iscsi_conn *conn, int flag)
3140 mutex_lock(&session->eh_mutex);
3141 spin_lock_bh(&session->frwd_lock);
3142 if (conn->stop_stage == STOP_CONN_TERM) {
3143 spin_unlock_bh(&session->frwd_lock);
3144 mutex_unlock(&session->eh_mutex);
3149 * When this is called for the in_login state, we only want to clean
3150 * up the login task and connection. We do not need to block and set
3151 * the recovery state again
3153 if (flag == STOP_CONN_TERM)
3154 session->state = ISCSI_STATE_TERMINATE;
3155 else if (conn->stop_stage != STOP_CONN_RECOVER)
3156 session->state = ISCSI_STATE_IN_RECOVERY;
3158 old_stop_stage = conn->stop_stage;
3159 conn->stop_stage = flag;
3160 spin_unlock_bh(&session->frwd_lock);
3162 del_timer_sync(&conn->transport_timer);
3163 iscsi_suspend_tx(conn);
3165 spin_lock_bh(&session->frwd_lock);
3166 conn->c_stage = ISCSI_CONN_STOPPED;
3167 spin_unlock_bh(&session->frwd_lock);
3170 * for connection level recovery we should not calculate
3171 * header digest. conn->hdr_size used for optimization
3172 * in hdr_extract() and will be re-negotiated at
3175 if (flag == STOP_CONN_RECOVER) {
3176 conn->hdrdgst_en = 0;
3177 conn->datadgst_en = 0;
3178 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3179 old_stop_stage != STOP_CONN_RECOVER) {
3180 ISCSI_DBG_SESSION(session, "blocking session\n");
3181 iscsi_block_session(session->cls_session);
3188 spin_lock_bh(&session->frwd_lock);
3189 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3190 fail_mgmt_tasks(session, conn);
3191 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3192 spin_unlock_bh(&session->frwd_lock);
3193 mutex_unlock(&session->eh_mutex);
3196 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3198 struct iscsi_conn *conn = cls_conn->dd_data;
3199 struct iscsi_session *session = conn->session;
3202 case STOP_CONN_RECOVER:
3203 case STOP_CONN_TERM:
3204 iscsi_start_session_recovery(session, conn, flag);
3207 iscsi_conn_printk(KERN_ERR, conn,
3208 "invalid stop flag %d\n", flag);
3211 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3213 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3214 struct iscsi_cls_conn *cls_conn, int is_leading)
3216 struct iscsi_session *session = cls_session->dd_data;
3217 struct iscsi_conn *conn = cls_conn->dd_data;
3219 spin_lock_bh(&session->frwd_lock);
3221 session->leadconn = conn;
3222 spin_unlock_bh(&session->frwd_lock);
3225 * Unblock xmitworker(), Login Phase will pass through.
3227 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3228 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3231 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3233 int iscsi_switch_str_param(char **param, char *new_val_buf)
3238 if (!strcmp(*param, new_val_buf))
3242 new_val = kstrdup(new_val_buf, GFP_NOIO);
3250 EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
3252 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3253 enum iscsi_param param, char *buf, int buflen)
3255 struct iscsi_conn *conn = cls_conn->dd_data;
3256 struct iscsi_session *session = conn->session;
3260 case ISCSI_PARAM_FAST_ABORT:
3261 sscanf(buf, "%d", &session->fast_abort);
3263 case ISCSI_PARAM_ABORT_TMO:
3264 sscanf(buf, "%d", &session->abort_timeout);
3266 case ISCSI_PARAM_LU_RESET_TMO:
3267 sscanf(buf, "%d", &session->lu_reset_timeout);
3269 case ISCSI_PARAM_TGT_RESET_TMO:
3270 sscanf(buf, "%d", &session->tgt_reset_timeout);
3272 case ISCSI_PARAM_PING_TMO:
3273 sscanf(buf, "%d", &conn->ping_timeout);
3275 case ISCSI_PARAM_RECV_TMO:
3276 sscanf(buf, "%d", &conn->recv_timeout);
3278 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3279 sscanf(buf, "%d", &conn->max_recv_dlength);
3281 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3282 sscanf(buf, "%d", &conn->max_xmit_dlength);
3284 case ISCSI_PARAM_HDRDGST_EN:
3285 sscanf(buf, "%d", &conn->hdrdgst_en);
3287 case ISCSI_PARAM_DATADGST_EN:
3288 sscanf(buf, "%d", &conn->datadgst_en);
3290 case ISCSI_PARAM_INITIAL_R2T_EN:
3291 sscanf(buf, "%d", &session->initial_r2t_en);
3293 case ISCSI_PARAM_MAX_R2T:
3294 sscanf(buf, "%hu", &session->max_r2t);
3296 case ISCSI_PARAM_IMM_DATA_EN:
3297 sscanf(buf, "%d", &session->imm_data_en);
3299 case ISCSI_PARAM_FIRST_BURST:
3300 sscanf(buf, "%d", &session->first_burst);
3302 case ISCSI_PARAM_MAX_BURST:
3303 sscanf(buf, "%d", &session->max_burst);
3305 case ISCSI_PARAM_PDU_INORDER_EN:
3306 sscanf(buf, "%d", &session->pdu_inorder_en);
3308 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3309 sscanf(buf, "%d", &session->dataseq_inorder_en);
3311 case ISCSI_PARAM_ERL:
3312 sscanf(buf, "%d", &session->erl);
3314 case ISCSI_PARAM_EXP_STATSN:
3315 sscanf(buf, "%u", &conn->exp_statsn);
3317 case ISCSI_PARAM_USERNAME:
3318 return iscsi_switch_str_param(&session->username, buf);
3319 case ISCSI_PARAM_USERNAME_IN:
3320 return iscsi_switch_str_param(&session->username_in, buf);
3321 case ISCSI_PARAM_PASSWORD:
3322 return iscsi_switch_str_param(&session->password, buf);
3323 case ISCSI_PARAM_PASSWORD_IN:
3324 return iscsi_switch_str_param(&session->password_in, buf);
3325 case ISCSI_PARAM_TARGET_NAME:
3326 return iscsi_switch_str_param(&session->targetname, buf);
3327 case ISCSI_PARAM_TARGET_ALIAS:
3328 return iscsi_switch_str_param(&session->targetalias, buf);
3329 case ISCSI_PARAM_TPGT:
3330 sscanf(buf, "%d", &session->tpgt);
3332 case ISCSI_PARAM_PERSISTENT_PORT:
3333 sscanf(buf, "%d", &conn->persistent_port);
3335 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3336 return iscsi_switch_str_param(&conn->persistent_address, buf);
3337 case ISCSI_PARAM_IFACE_NAME:
3338 return iscsi_switch_str_param(&session->ifacename, buf);
3339 case ISCSI_PARAM_INITIATOR_NAME:
3340 return iscsi_switch_str_param(&session->initiatorname, buf);
3341 case ISCSI_PARAM_BOOT_ROOT:
3342 return iscsi_switch_str_param(&session->boot_root, buf);
3343 case ISCSI_PARAM_BOOT_NIC:
3344 return iscsi_switch_str_param(&session->boot_nic, buf);
3345 case ISCSI_PARAM_BOOT_TARGET:
3346 return iscsi_switch_str_param(&session->boot_target, buf);
3347 case ISCSI_PARAM_PORTAL_TYPE:
3348 return iscsi_switch_str_param(&session->portal_type, buf);
3349 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3350 return iscsi_switch_str_param(&session->discovery_parent_type,
3352 case ISCSI_PARAM_DISCOVERY_SESS:
3353 sscanf(buf, "%d", &val);
3354 session->discovery_sess = !!val;
3356 case ISCSI_PARAM_LOCAL_IPADDR:
3357 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
3364 EXPORT_SYMBOL_GPL(iscsi_set_param);
3366 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3367 enum iscsi_param param, char *buf)
3369 struct iscsi_session *session = cls_session->dd_data;
3373 case ISCSI_PARAM_FAST_ABORT:
3374 len = sprintf(buf, "%d\n", session->fast_abort);
3376 case ISCSI_PARAM_ABORT_TMO:
3377 len = sprintf(buf, "%d\n", session->abort_timeout);
3379 case ISCSI_PARAM_LU_RESET_TMO:
3380 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3382 case ISCSI_PARAM_TGT_RESET_TMO:
3383 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3385 case ISCSI_PARAM_INITIAL_R2T_EN:
3386 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3388 case ISCSI_PARAM_MAX_R2T:
3389 len = sprintf(buf, "%hu\n", session->max_r2t);
3391 case ISCSI_PARAM_IMM_DATA_EN:
3392 len = sprintf(buf, "%d\n", session->imm_data_en);
3394 case ISCSI_PARAM_FIRST_BURST:
3395 len = sprintf(buf, "%u\n", session->first_burst);
3397 case ISCSI_PARAM_MAX_BURST:
3398 len = sprintf(buf, "%u\n", session->max_burst);
3400 case ISCSI_PARAM_PDU_INORDER_EN:
3401 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3403 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3404 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3406 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3407 len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
3409 case ISCSI_PARAM_ERL:
3410 len = sprintf(buf, "%d\n", session->erl);
3412 case ISCSI_PARAM_TARGET_NAME:
3413 len = sprintf(buf, "%s\n", session->targetname);
3415 case ISCSI_PARAM_TARGET_ALIAS:
3416 len = sprintf(buf, "%s\n", session->targetalias);
3418 case ISCSI_PARAM_TPGT:
3419 len = sprintf(buf, "%d\n", session->tpgt);
3421 case ISCSI_PARAM_USERNAME:
3422 len = sprintf(buf, "%s\n", session->username);
3424 case ISCSI_PARAM_USERNAME_IN:
3425 len = sprintf(buf, "%s\n", session->username_in);
3427 case ISCSI_PARAM_PASSWORD:
3428 len = sprintf(buf, "%s\n", session->password);
3430 case ISCSI_PARAM_PASSWORD_IN:
3431 len = sprintf(buf, "%s\n", session->password_in);
3433 case ISCSI_PARAM_IFACE_NAME:
3434 len = sprintf(buf, "%s\n", session->ifacename);
3436 case ISCSI_PARAM_INITIATOR_NAME:
3437 len = sprintf(buf, "%s\n", session->initiatorname);
3439 case ISCSI_PARAM_BOOT_ROOT:
3440 len = sprintf(buf, "%s\n", session->boot_root);
3442 case ISCSI_PARAM_BOOT_NIC:
3443 len = sprintf(buf, "%s\n", session->boot_nic);
3445 case ISCSI_PARAM_BOOT_TARGET:
3446 len = sprintf(buf, "%s\n", session->boot_target);
3448 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3449 len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
3451 case ISCSI_PARAM_DISCOVERY_SESS:
3452 len = sprintf(buf, "%u\n", session->discovery_sess);
3454 case ISCSI_PARAM_PORTAL_TYPE:
3455 len = sprintf(buf, "%s\n", session->portal_type);
3457 case ISCSI_PARAM_CHAP_AUTH_EN:
3458 len = sprintf(buf, "%u\n", session->chap_auth_en);
3460 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3461 len = sprintf(buf, "%u\n", session->discovery_logout_en);
3463 case ISCSI_PARAM_BIDI_CHAP_EN:
3464 len = sprintf(buf, "%u\n", session->bidi_chap_en);
3466 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3467 len = sprintf(buf, "%u\n", session->discovery_auth_optional);
3469 case ISCSI_PARAM_DEF_TIME2WAIT:
3470 len = sprintf(buf, "%d\n", session->time2wait);
3472 case ISCSI_PARAM_DEF_TIME2RETAIN:
3473 len = sprintf(buf, "%d\n", session->time2retain);
3475 case ISCSI_PARAM_TSID:
3476 len = sprintf(buf, "%u\n", session->tsid);
3478 case ISCSI_PARAM_ISID:
3479 len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
3480 session->isid[0], session->isid[1],
3481 session->isid[2], session->isid[3],
3482 session->isid[4], session->isid[5]);
3484 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3485 len = sprintf(buf, "%u\n", session->discovery_parent_idx);
3487 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3488 if (session->discovery_parent_type)
3489 len = sprintf(buf, "%s\n",
3490 session->discovery_parent_type);
3492 len = sprintf(buf, "\n");
3500 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3502 int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3503 enum iscsi_param param, char *buf)
3505 struct sockaddr_in6 *sin6 = NULL;
3506 struct sockaddr_in *sin = NULL;
3509 switch (addr->ss_family) {
3511 sin = (struct sockaddr_in *)addr;
3514 sin6 = (struct sockaddr_in6 *)addr;
3521 case ISCSI_PARAM_CONN_ADDRESS:
3522 case ISCSI_HOST_PARAM_IPADDRESS:
3524 len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
3526 len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
3528 case ISCSI_PARAM_CONN_PORT:
3529 case ISCSI_PARAM_LOCAL_PORT:
3531 len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3533 len = sprintf(buf, "%hu\n",
3534 be16_to_cpu(sin6->sin6_port));
3542 EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3544 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3545 enum iscsi_param param, char *buf)
3547 struct iscsi_conn *conn = cls_conn->dd_data;
3551 case ISCSI_PARAM_PING_TMO:
3552 len = sprintf(buf, "%u\n", conn->ping_timeout);
3554 case ISCSI_PARAM_RECV_TMO:
3555 len = sprintf(buf, "%u\n", conn->recv_timeout);
3557 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3558 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3560 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3561 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3563 case ISCSI_PARAM_HDRDGST_EN:
3564 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3566 case ISCSI_PARAM_DATADGST_EN:
3567 len = sprintf(buf, "%d\n", conn->datadgst_en);
3569 case ISCSI_PARAM_IFMARKER_EN:
3570 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3572 case ISCSI_PARAM_OFMARKER_EN:
3573 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3575 case ISCSI_PARAM_EXP_STATSN:
3576 len = sprintf(buf, "%u\n", conn->exp_statsn);
3578 case ISCSI_PARAM_PERSISTENT_PORT:
3579 len = sprintf(buf, "%d\n", conn->persistent_port);
3581 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3582 len = sprintf(buf, "%s\n", conn->persistent_address);
3584 case ISCSI_PARAM_STATSN:
3585 len = sprintf(buf, "%u\n", conn->statsn);
3587 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3588 len = sprintf(buf, "%u\n", conn->max_segment_size);
3590 case ISCSI_PARAM_KEEPALIVE_TMO:
3591 len = sprintf(buf, "%u\n", conn->keepalive_tmo);
3593 case ISCSI_PARAM_LOCAL_PORT:
3594 len = sprintf(buf, "%u\n", conn->local_port);
3596 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3597 len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
3599 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3600 len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
3602 case ISCSI_PARAM_TCP_WSF_DISABLE:
3603 len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
3605 case ISCSI_PARAM_TCP_TIMER_SCALE:
3606 len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
3608 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3609 len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
3611 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3612 len = sprintf(buf, "%u\n", conn->fragment_disable);
3614 case ISCSI_PARAM_IPV4_TOS:
3615 len = sprintf(buf, "%u\n", conn->ipv4_tos);
3617 case ISCSI_PARAM_IPV6_TC:
3618 len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
3620 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3621 len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
3623 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3624 len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
3626 case ISCSI_PARAM_TCP_XMIT_WSF:
3627 len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
3629 case ISCSI_PARAM_TCP_RECV_WSF:
3630 len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
3632 case ISCSI_PARAM_LOCAL_IPADDR:
3633 len = sprintf(buf, "%s\n", conn->local_ipaddr);
3641 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3643 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3646 struct iscsi_host *ihost = shost_priv(shost);
3650 case ISCSI_HOST_PARAM_NETDEV_NAME:
3651 len = sprintf(buf, "%s\n", ihost->netdev);
3653 case ISCSI_HOST_PARAM_HWADDRESS:
3654 len = sprintf(buf, "%s\n", ihost->hwaddress);
3656 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3657 len = sprintf(buf, "%s\n", ihost->initiatorname);
3665 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3667 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3668 char *buf, int buflen)
3670 struct iscsi_host *ihost = shost_priv(shost);
3673 case ISCSI_HOST_PARAM_NETDEV_NAME:
3674 return iscsi_switch_str_param(&ihost->netdev, buf);
3675 case ISCSI_HOST_PARAM_HWADDRESS:
3676 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3677 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3678 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3685 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3687 MODULE_AUTHOR("Mike Christie");
3688 MODULE_DESCRIPTION("iSCSI library functions");
3689 MODULE_LICENSE("GPL");