4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi.h>
36 #include <scsi/iscsi_proto.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_iscsi.h>
39 #include <scsi/libiscsi.h>
41 static int iscsi_dbg_lib_conn;
42 module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
44 MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
48 static int iscsi_dbg_lib_session;
49 module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
51 MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
55 static int iscsi_dbg_lib_eh;
56 module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
58 MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
62 #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
64 if (iscsi_dbg_lib_conn) \
65 iscsi_conn_printk(KERN_INFO, _conn, \
70 #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
72 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
78 #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
80 if (iscsi_dbg_lib_eh) \
81 iscsi_session_printk(KERN_INFO, _session, \
86 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87 #define SNA32_CHECK 2147483648UL
89 static int iscsi_sna_lt(u32 n1, u32 n2)
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
95 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96 static int iscsi_sna_lte(u32 n1, u32 n2)
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
102 inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
108 queue_work(ihost->workq, &conn->xmitwork);
110 EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
113 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
115 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
116 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
119 * standard specifies this check for when to update expected and
120 * max sequence numbers
122 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
125 if (exp_cmdsn != session->exp_cmdsn &&
126 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
127 session->exp_cmdsn = exp_cmdsn;
129 if (max_cmdsn != session->max_cmdsn &&
130 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
131 session->max_cmdsn = max_cmdsn;
133 * if the window closed with IO queued, then kick the
136 if (!list_empty(&session->leadconn->cmdqueue) ||
137 !list_empty(&session->leadconn->mgmtqueue))
138 iscsi_conn_queue_work(session->leadconn);
141 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
144 * iscsi_prep_data_out_pdu - initialize Data-Out
145 * @task: scsi command task
147 * @hdr: iscsi data in pdu
150 * Initialize Data-Out within this R2T sequence and finds
151 * proper data_offset within this SCSI command.
153 * This function is called with connection lock taken.
155 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
156 struct iscsi_data *hdr)
158 struct iscsi_conn *conn = task->conn;
159 unsigned int left = r2t->data_length - r2t->sent;
161 task->hdr_len = sizeof(struct iscsi_data);
163 memset(hdr, 0, sizeof(struct iscsi_data));
165 hdr->datasn = cpu_to_be32(r2t->datasn);
167 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
168 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
169 hdr->itt = task->hdr_itt;
170 hdr->exp_statsn = r2t->exp_statsn;
171 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
172 if (left > conn->max_xmit_dlength) {
173 hton24(hdr->dlength, conn->max_xmit_dlength);
174 r2t->data_count = conn->max_xmit_dlength;
177 hton24(hdr->dlength, left);
178 r2t->data_count = left;
179 hdr->flags = ISCSI_FLAG_CMD_FINAL;
181 conn->dataout_pdus_cnt++;
183 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
185 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
187 unsigned exp_len = task->hdr_len + len;
189 if (exp_len > task->hdr_max) {
194 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
195 task->hdr_len = exp_len;
200 * make an extended cdb AHS
202 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
204 struct scsi_cmnd *cmd = task->sc;
205 unsigned rlen, pad_len;
206 unsigned short ahslength;
207 struct iscsi_ecdb_ahdr *ecdb_ahdr;
210 ecdb_ahdr = iscsi_next_hdr(task);
211 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
213 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
214 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
216 pad_len = iscsi_padding(rlen);
218 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
219 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
224 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
226 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
227 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
228 ecdb_ahdr->reserved = 0;
229 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
231 ISCSI_DBG_SESSION(task->conn->session,
232 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
233 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
234 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
239 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
241 struct scsi_cmnd *sc = task->sc;
242 struct iscsi_rlength_ahdr *rlen_ahdr;
245 rlen_ahdr = iscsi_next_hdr(task);
246 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
250 rlen_ahdr->ahslength =
251 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
252 sizeof(rlen_ahdr->reserved));
253 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
254 rlen_ahdr->reserved = 0;
255 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
257 ISCSI_DBG_SESSION(task->conn->session,
258 "bidi-in rlen_ahdr->read_length(%d) "
259 "rlen_ahdr->ahslength(%d)\n",
260 be32_to_cpu(rlen_ahdr->read_length),
261 be16_to_cpu(rlen_ahdr->ahslength));
266 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
269 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
270 * fields like dlength or final based on how much data it sends
272 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
274 struct iscsi_conn *conn = task->conn;
275 struct iscsi_session *session = conn->session;
276 struct scsi_cmnd *sc = task->sc;
277 struct iscsi_cmd *hdr;
278 unsigned hdrlength, cmd_len;
282 if (conn->session->tt->alloc_pdu) {
283 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
287 hdr = (struct iscsi_cmd *) task->hdr;
289 memset(hdr, 0, sizeof(*hdr));
291 if (session->tt->parse_pdu_itt)
292 hdr->itt = task->hdr_itt = itt;
294 hdr->itt = task->hdr_itt = build_itt(task->itt,
295 task->conn->session->age);
297 rc = iscsi_add_hdr(task, sizeof(*hdr));
300 hdr->opcode = ISCSI_OP_SCSI_CMD;
301 hdr->flags = ISCSI_ATTR_SIMPLE;
302 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
303 memcpy(task->lun, hdr->lun, sizeof(task->lun));
304 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
305 cmd_len = sc->cmd_len;
306 if (cmd_len < ISCSI_CDB_SIZE)
307 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
308 else if (cmd_len > ISCSI_CDB_SIZE) {
309 rc = iscsi_prep_ecdb_ahs(task);
312 cmd_len = ISCSI_CDB_SIZE;
314 memcpy(hdr->cdb, sc->cmnd, cmd_len);
317 if (scsi_bidi_cmnd(sc)) {
318 hdr->flags |= ISCSI_FLAG_CMD_READ;
319 rc = iscsi_prep_bidi_ahs(task);
323 if (sc->sc_data_direction == DMA_TO_DEVICE) {
324 unsigned out_len = scsi_out(sc)->length;
325 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
327 hdr->data_length = cpu_to_be32(out_len);
328 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
332 * imm_count bytes to be sent right after
335 * unsol_count bytes(as Data-Out) to be sent
336 * without R2T ack right after
339 * r2t data_length bytes to be sent via R2T ack's
341 * pad_count bytes to be sent as zero-padding
343 memset(r2t, 0, sizeof(*r2t));
345 if (session->imm_data_en) {
346 if (out_len >= session->first_burst)
347 task->imm_count = min(session->first_burst,
348 conn->max_xmit_dlength);
350 task->imm_count = min(out_len,
351 conn->max_xmit_dlength);
352 hton24(hdr->dlength, task->imm_count);
354 zero_data(hdr->dlength);
356 if (!session->initial_r2t_en) {
357 r2t->data_length = min(session->first_burst, out_len) -
359 r2t->data_offset = task->imm_count;
360 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
361 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
364 if (!task->unsol_r2t.data_length)
365 /* No unsolicit Data-Out's */
366 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
368 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
369 zero_data(hdr->dlength);
370 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
372 if (sc->sc_data_direction == DMA_FROM_DEVICE)
373 hdr->flags |= ISCSI_FLAG_CMD_READ;
376 /* calculate size of additional header segments (AHSs) */
377 hdrlength = task->hdr_len - sizeof(*hdr);
379 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
380 hdrlength /= ISCSI_PAD_LEN;
382 WARN_ON(hdrlength >= 256);
383 hdr->hlength = hdrlength & 0xFF;
385 if (session->tt->init_task && session->tt->init_task(task))
388 task->state = ISCSI_TASK_RUNNING;
389 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
392 conn->scsicmd_pdus_cnt++;
393 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
394 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
395 scsi_bidi_cmnd(sc) ? "bidirectional" :
396 sc->sc_data_direction == DMA_TO_DEVICE ?
397 "write" : "read", conn->id, sc, sc->cmnd[0],
398 task->itt, scsi_bufflen(sc),
399 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
401 session->max_cmdsn - session->exp_cmdsn + 1);
406 * iscsi_free_task - free a task
407 * @task: iscsi cmd task
409 * Must be called with session lock.
410 * This function returns the scsi command to scsi-ml or cleans
411 * up mgmt tasks then returns the task to the pool.
413 static void iscsi_free_task(struct iscsi_task *task)
415 struct iscsi_conn *conn = task->conn;
416 struct iscsi_session *session = conn->session;
417 struct scsi_cmnd *sc = task->sc;
419 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
420 task->itt, task->state, task->sc);
422 session->tt->cleanup_task(task);
423 task->state = ISCSI_TASK_FREE;
426 * login task is preallocated so do not free
428 if (conn->login_task == task)
431 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
435 /* SCSI eh reuses commands to verify us */
438 * queue command may call this to free the task, but
439 * not have setup the sc callback
446 void __iscsi_get_task(struct iscsi_task *task)
448 atomic_inc(&task->refcount);
450 EXPORT_SYMBOL_GPL(__iscsi_get_task);
452 static void __iscsi_put_task(struct iscsi_task *task)
454 if (atomic_dec_and_test(&task->refcount))
455 iscsi_free_task(task);
458 void iscsi_put_task(struct iscsi_task *task)
460 struct iscsi_session *session = task->conn->session;
462 spin_lock_bh(&session->lock);
463 __iscsi_put_task(task);
464 spin_unlock_bh(&session->lock);
466 EXPORT_SYMBOL_GPL(iscsi_put_task);
469 * iscsi_complete_task - finish a task
470 * @task: iscsi cmd task
471 * @state: state to complete task with
473 * Must be called with session lock.
475 static void iscsi_complete_task(struct iscsi_task *task, int state)
477 struct iscsi_conn *conn = task->conn;
479 ISCSI_DBG_SESSION(conn->session,
480 "complete task itt 0x%x state %d sc %p\n",
481 task->itt, task->state, task->sc);
482 if (task->state == ISCSI_TASK_COMPLETED ||
483 task->state == ISCSI_TASK_ABRT_TMF ||
484 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
486 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
489 if (!list_empty(&task->running))
490 list_del_init(&task->running);
492 if (conn->task == task)
495 if (conn->ping_task == task)
496 conn->ping_task = NULL;
498 /* release get from queueing */
499 __iscsi_put_task(task);
503 * session lock must be held and if not called for a task that is
504 * still pending or from the xmit thread, then xmit thread must
507 static void fail_scsi_task(struct iscsi_task *task, int err)
509 struct iscsi_conn *conn = task->conn;
510 struct scsi_cmnd *sc;
514 * if a command completes and we get a successful tmf response
515 * we will hit this because the scsi eh abort code does not take
522 if (task->state == ISCSI_TASK_PENDING) {
524 * cmd never made it to the xmit thread, so we should not count
525 * the cmd in the sequencing
527 conn->session->queued_cmdsn--;
528 /* it was never sent so just complete like normal */
529 state = ISCSI_TASK_COMPLETED;
530 } else if (err == DID_TRANSPORT_DISRUPTED)
531 state = ISCSI_TASK_ABRT_SESS_RECOV;
533 state = ISCSI_TASK_ABRT_TMF;
535 sc->result = err << 16;
536 if (!scsi_bidi_cmnd(sc))
537 scsi_set_resid(sc, scsi_bufflen(sc));
539 scsi_out(sc)->resid = scsi_out(sc)->length;
540 scsi_in(sc)->resid = scsi_in(sc)->length;
543 iscsi_complete_task(task, state);
546 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
547 struct iscsi_task *task)
549 struct iscsi_session *session = conn->session;
550 struct iscsi_hdr *hdr = task->hdr;
551 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
553 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
556 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
557 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
558 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
560 * pre-format CmdSN for outgoing PDU.
562 nop->cmdsn = cpu_to_be32(session->cmdsn);
563 if (hdr->itt != RESERVED_ITT) {
565 * TODO: We always use immediate, so we never hit this.
566 * If we start to send tmfs or nops as non-immediate then
567 * we should start checking the cmdsn numbers for mgmt tasks.
569 if (conn->c_stage == ISCSI_CONN_STARTED &&
570 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
571 session->queued_cmdsn++;
576 if (session->tt->init_task && session->tt->init_task(task))
579 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
580 session->state = ISCSI_STATE_LOGGING_OUT;
582 task->state = ISCSI_TASK_RUNNING;
583 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
584 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
585 hdr->itt, task->data_count);
589 static struct iscsi_task *
590 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
591 char *data, uint32_t data_size)
593 struct iscsi_session *session = conn->session;
594 struct iscsi_host *ihost = shost_priv(session->host);
595 struct iscsi_task *task;
598 if (session->state == ISCSI_STATE_TERMINATE)
601 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
602 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
604 * Login and Text are sent serially, in
605 * request-followed-by-response sequence.
606 * Same task can be used. Same ITT must be used.
607 * Note that login_task is preallocated at conn_create().
609 task = conn->login_task;
611 if (session->state != ISCSI_STATE_LOGGED_IN)
614 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
615 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
617 if (!__kfifo_get(session->cmdpool.queue,
618 (void*)&task, sizeof(void*)))
622 * released in complete pdu for task we expect a response for, and
623 * released by the lld when it has transmitted the task for
624 * pdus we do not expect a response for.
626 atomic_set(&task->refcount, 1);
629 INIT_LIST_HEAD(&task->running);
630 task->state = ISCSI_TASK_PENDING;
633 memcpy(task->data, data, data_size);
634 task->data_count = data_size;
636 task->data_count = 0;
638 if (conn->session->tt->alloc_pdu) {
639 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
640 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
641 "pdu for mgmt task.\n");
646 itt = task->hdr->itt;
647 task->hdr_len = sizeof(struct iscsi_hdr);
648 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
650 if (hdr->itt != RESERVED_ITT) {
651 if (session->tt->parse_pdu_itt)
652 task->hdr->itt = itt;
654 task->hdr->itt = build_itt(task->itt,
655 task->conn->session->age);
659 if (iscsi_prep_mgmt_task(conn, task))
662 if (session->tt->xmit_task(task))
665 list_add_tail(&task->running, &conn->mgmtqueue);
666 iscsi_conn_queue_work(conn);
672 __iscsi_put_task(task);
676 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
677 char *data, uint32_t data_size)
679 struct iscsi_conn *conn = cls_conn->dd_data;
680 struct iscsi_session *session = conn->session;
683 spin_lock_bh(&session->lock);
684 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
686 spin_unlock_bh(&session->lock);
689 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
692 * iscsi_cmd_rsp - SCSI Command Response processing
693 * @conn: iscsi connection
695 * @task: scsi command task
696 * @data: cmd data buffer
697 * @datalen: len of buffer
699 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
700 * then completes the command and task.
702 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
703 struct iscsi_task *task, char *data,
706 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
707 struct iscsi_session *session = conn->session;
708 struct scsi_cmnd *sc = task->sc;
710 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
711 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
713 sc->result = (DID_OK << 16) | rhdr->cmd_status;
715 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
716 sc->result = DID_ERROR << 16;
720 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
725 iscsi_conn_printk(KERN_ERR, conn,
726 "Got CHECK_CONDITION but invalid data "
727 "buffer size of %d\n", datalen);
728 sc->result = DID_BAD_TARGET << 16;
732 senselen = get_unaligned_be16(data);
733 if (datalen < senselen)
734 goto invalid_datalen;
736 memcpy(sc->sense_buffer, data + 2,
737 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
738 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
739 min_t(uint16_t, senselen,
740 SCSI_SENSE_BUFFERSIZE));
743 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
744 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
745 int res_count = be32_to_cpu(rhdr->bi_residual_count);
747 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
748 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
749 res_count <= scsi_in(sc)->length))
750 scsi_in(sc)->resid = res_count;
752 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
755 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
756 ISCSI_FLAG_CMD_OVERFLOW)) {
757 int res_count = be32_to_cpu(rhdr->residual_count);
760 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
761 res_count <= scsi_bufflen(sc)))
762 /* write side for bidi or uni-io set_resid */
763 scsi_set_resid(sc, res_count);
765 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
768 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
769 sc, sc->result, task->itt);
770 conn->scsirsp_pdus_cnt++;
771 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
775 * iscsi_data_in_rsp - SCSI Data-In Response processing
776 * @conn: iscsi connection
778 * @task: scsi command task
781 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
782 struct iscsi_task *task)
784 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
785 struct scsi_cmnd *sc = task->sc;
787 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
790 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
791 sc->result = (DID_OK << 16) | rhdr->cmd_status;
792 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
793 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
794 ISCSI_FLAG_DATA_OVERFLOW)) {
795 int res_count = be32_to_cpu(rhdr->residual_count);
798 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
799 res_count <= scsi_in(sc)->length))
800 scsi_in(sc)->resid = res_count;
802 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
805 ISCSI_DBG_SESSION(conn->session, "data in with status done "
806 "[sc %p res %d itt 0x%x]\n",
807 sc, sc->result, task->itt);
808 conn->scsirsp_pdus_cnt++;
809 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
812 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
814 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
816 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
817 conn->tmfrsp_pdus_cnt++;
819 if (conn->tmf_state != TMF_QUEUED)
822 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
823 conn->tmf_state = TMF_SUCCESS;
824 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
825 conn->tmf_state = TMF_NOT_FOUND;
827 conn->tmf_state = TMF_FAILED;
828 wake_up(&conn->ehwait);
831 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
833 struct iscsi_nopout hdr;
834 struct iscsi_task *task;
836 if (!rhdr && conn->ping_task)
839 memset(&hdr, 0, sizeof(struct iscsi_nopout));
840 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
841 hdr.flags = ISCSI_FLAG_CMD_FINAL;
844 memcpy(hdr.lun, rhdr->lun, 8);
846 hdr.itt = RESERVED_ITT;
848 hdr.ttt = RESERVED_ITT;
850 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
852 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
854 /* only track our nops */
855 conn->ping_task = task;
856 conn->last_ping = jiffies;
860 static int iscsi_nop_out_rsp(struct iscsi_task *task,
861 struct iscsi_nopin *nop, char *data, int datalen)
863 struct iscsi_conn *conn = task->conn;
866 if (conn->ping_task != task) {
868 * If this is not in response to one of our
869 * nops then it must be from userspace.
871 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
873 rc = ISCSI_ERR_CONN_FAILED;
875 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
876 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
880 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
881 char *data, int datalen)
883 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
884 struct iscsi_hdr rejected_pdu;
887 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
889 if (ntoh24(reject->dlength) > datalen ||
890 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
891 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
892 "pdu. Invalid data length (pdu dlength "
893 "%u, datalen %d\n", ntoh24(reject->dlength),
895 return ISCSI_ERR_PROTO;
897 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
898 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
900 switch (reject->reason) {
901 case ISCSI_REASON_DATA_DIGEST_ERROR:
902 iscsi_conn_printk(KERN_ERR, conn,
903 "pdu (op 0x%x itt 0x%x) rejected "
904 "due to DataDigest error.\n",
905 rejected_pdu.itt, opcode);
907 case ISCSI_REASON_IMM_CMD_REJECT:
908 iscsi_conn_printk(KERN_ERR, conn,
909 "pdu (op 0x%x itt 0x%x) rejected. Too many "
910 "immediate commands.\n",
911 rejected_pdu.itt, opcode);
913 * We only send one TMF at a time so if the target could not
914 * handle it, then it should get fixed (RFC mandates that
915 * a target can handle one immediate TMF per conn).
917 * For nops-outs, we could have sent more than one if
918 * the target is sending us lots of nop-ins
920 if (opcode != ISCSI_OP_NOOP_OUT)
923 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
925 * nop-out in response to target's nop-out rejected.
928 iscsi_send_nopout(conn,
929 (struct iscsi_nopin*)&rejected_pdu);
931 struct iscsi_task *task;
933 * Our nop as ping got dropped. We know the target
934 * and transport are ok so just clean up
936 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
938 iscsi_conn_printk(KERN_ERR, conn,
939 "Invalid pdu reject. Could "
940 "not lookup rejected task.\n");
941 rc = ISCSI_ERR_BAD_ITT;
943 rc = iscsi_nop_out_rsp(task,
944 (struct iscsi_nopin*)&rejected_pdu,
949 iscsi_conn_printk(KERN_ERR, conn,
950 "pdu (op 0x%x itt 0x%x) rejected. Reason "
951 "code 0x%x\n", rejected_pdu.itt,
952 rejected_pdu.opcode, reject->reason);
959 * iscsi_itt_to_task - look up task by itt
960 * @conn: iscsi connection
963 * This should be used for mgmt tasks like login and nops, or if
964 * the LDD's itt space does not include the session age.
966 * The session lock must be held.
968 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
970 struct iscsi_session *session = conn->session;
973 if (itt == RESERVED_ITT)
976 if (session->tt->parse_pdu_itt)
977 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
980 if (i >= session->cmds_max)
983 return session->cmds[i];
985 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
988 * __iscsi_complete_pdu - complete pdu
992 * @datalen: len of data buffer
994 * Completes pdu processing by freeing any resources allocated at
995 * queuecommand or send generic. session lock must be held and verify
996 * itt must have been called.
998 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
999 char *data, int datalen)
1001 struct iscsi_session *session = conn->session;
1002 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1003 struct iscsi_task *task;
1006 conn->last_recv = jiffies;
1007 rc = iscsi_verify_itt(conn, hdr->itt);
1011 if (hdr->itt != RESERVED_ITT)
1012 itt = get_itt(hdr->itt);
1016 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1017 opcode, conn->id, itt, datalen);
1020 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1023 case ISCSI_OP_NOOP_IN:
1025 rc = ISCSI_ERR_PROTO;
1029 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1032 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1034 case ISCSI_OP_REJECT:
1035 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1037 case ISCSI_OP_ASYNC_EVENT:
1038 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1039 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1040 rc = ISCSI_ERR_CONN_FAILED;
1043 rc = ISCSI_ERR_BAD_OPCODE;
1050 case ISCSI_OP_SCSI_CMD_RSP:
1051 case ISCSI_OP_SCSI_DATA_IN:
1052 task = iscsi_itt_to_ctask(conn, hdr->itt);
1054 return ISCSI_ERR_BAD_ITT;
1055 task->last_xfer = jiffies;
1059 * LLD handles R2Ts if they need to.
1062 case ISCSI_OP_LOGOUT_RSP:
1063 case ISCSI_OP_LOGIN_RSP:
1064 case ISCSI_OP_TEXT_RSP:
1065 case ISCSI_OP_SCSI_TMFUNC_RSP:
1066 case ISCSI_OP_NOOP_IN:
1067 task = iscsi_itt_to_task(conn, hdr->itt);
1069 return ISCSI_ERR_BAD_ITT;
1072 return ISCSI_ERR_BAD_OPCODE;
1076 case ISCSI_OP_SCSI_CMD_RSP:
1077 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1079 case ISCSI_OP_SCSI_DATA_IN:
1080 iscsi_data_in_rsp(conn, hdr, task);
1082 case ISCSI_OP_LOGOUT_RSP:
1083 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1085 rc = ISCSI_ERR_PROTO;
1088 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1090 case ISCSI_OP_LOGIN_RSP:
1091 case ISCSI_OP_TEXT_RSP:
1092 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1094 * login related PDU's exp_statsn is handled in
1098 case ISCSI_OP_SCSI_TMFUNC_RSP:
1099 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1101 rc = ISCSI_ERR_PROTO;
1105 iscsi_tmf_rsp(conn, hdr);
1106 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1108 case ISCSI_OP_NOOP_IN:
1109 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1110 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1111 rc = ISCSI_ERR_PROTO;
1114 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1116 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1120 rc = ISCSI_ERR_BAD_OPCODE;
1127 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1128 rc = ISCSI_ERR_CONN_FAILED;
1129 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1132 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1134 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1135 char *data, int datalen)
1139 spin_lock(&conn->session->lock);
1140 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1141 spin_unlock(&conn->session->lock);
1144 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1146 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1148 struct iscsi_session *session = conn->session;
1151 if (itt == RESERVED_ITT)
1154 if (session->tt->parse_pdu_itt)
1155 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1158 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1161 if (age != session->age) {
1162 iscsi_conn_printk(KERN_ERR, conn,
1163 "received itt %x expected session age (%x)\n",
1164 (__force u32)itt, session->age);
1165 return ISCSI_ERR_BAD_ITT;
1168 if (i >= session->cmds_max) {
1169 iscsi_conn_printk(KERN_ERR, conn,
1170 "received invalid itt index %u (max cmds "
1171 "%u.\n", i, session->cmds_max);
1172 return ISCSI_ERR_BAD_ITT;
1176 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1179 * iscsi_itt_to_ctask - look up ctask by itt
1180 * @conn: iscsi connection
1183 * This should be used for cmd tasks.
1185 * The session lock must be held.
1187 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1189 struct iscsi_task *task;
1191 if (iscsi_verify_itt(conn, itt))
1194 task = iscsi_itt_to_task(conn, itt);
1195 if (!task || !task->sc)
1198 if (task->sc->SCp.phase != conn->session->age) {
1199 iscsi_session_printk(KERN_ERR, conn->session,
1200 "task's session age %d, expected %d\n",
1201 task->sc->SCp.phase, conn->session->age);
1207 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1209 void iscsi_session_failure(struct iscsi_session *session,
1212 struct iscsi_conn *conn;
1214 unsigned long flags;
1216 spin_lock_irqsave(&session->lock, flags);
1217 conn = session->leadconn;
1218 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1219 spin_unlock_irqrestore(&session->lock, flags);
1223 dev = get_device(&conn->cls_conn->dev);
1224 spin_unlock_irqrestore(&session->lock, flags);
1228 * if the host is being removed bypass the connection
1229 * recovery initialization because we are going to kill
1232 if (err == ISCSI_ERR_INVALID_HOST)
1233 iscsi_conn_error_event(conn->cls_conn, err);
1235 iscsi_conn_failure(conn, err);
1238 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1240 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1242 struct iscsi_session *session = conn->session;
1243 unsigned long flags;
1245 spin_lock_irqsave(&session->lock, flags);
1246 if (session->state == ISCSI_STATE_FAILED) {
1247 spin_unlock_irqrestore(&session->lock, flags);
1251 if (conn->stop_stage == 0)
1252 session->state = ISCSI_STATE_FAILED;
1253 spin_unlock_irqrestore(&session->lock, flags);
1255 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1256 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1257 iscsi_conn_error_event(conn->cls_conn, err);
1259 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1261 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1263 struct iscsi_session *session = conn->session;
1266 * Check for iSCSI window and take care of CmdSN wrap-around
1268 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1269 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1270 "%u MaxCmdSN %u CmdSN %u/%u\n",
1271 session->exp_cmdsn, session->max_cmdsn,
1272 session->cmdsn, session->queued_cmdsn);
1278 static int iscsi_xmit_task(struct iscsi_conn *conn)
1280 struct iscsi_task *task = conn->task;
1283 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1286 __iscsi_get_task(task);
1287 spin_unlock_bh(&conn->session->lock);
1288 rc = conn->session->tt->xmit_task(task);
1289 spin_lock_bh(&conn->session->lock);
1291 /* done with this task */
1292 task->last_xfer = jiffies;
1295 __iscsi_put_task(task);
1300 * iscsi_requeue_task - requeue task to run from session workqueue
1301 * @task: task to requeue
1303 * LLDs that need to run a task from the session workqueue should call
1304 * this. The session lock must be held. This should only be called
1305 * by software drivers.
1307 void iscsi_requeue_task(struct iscsi_task *task)
1309 struct iscsi_conn *conn = task->conn;
1312 * this may be on the requeue list already if the xmit_task callout
1313 * is handling the r2ts while we are adding new ones
1315 if (list_empty(&task->running))
1316 list_add_tail(&task->running, &conn->requeue);
1317 iscsi_conn_queue_work(conn);
1319 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1322 * iscsi_data_xmit - xmit any command into the scheduled connection
1323 * @conn: iscsi connection
1326 * The function can return -EAGAIN in which case the caller must
1327 * re-schedule it again later or recover. '0' return code means
1330 static int iscsi_data_xmit(struct iscsi_conn *conn)
1334 spin_lock_bh(&conn->session->lock);
1335 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1336 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1337 spin_unlock_bh(&conn->session->lock);
1342 rc = iscsi_xmit_task(conn);
1348 * process mgmt pdus like nops before commands since we should
1349 * only have one nop-out as a ping from us and targets should not
1350 * overflow us with nop-ins
1353 while (!list_empty(&conn->mgmtqueue)) {
1354 conn->task = list_entry(conn->mgmtqueue.next,
1355 struct iscsi_task, running);
1356 list_del_init(&conn->task->running);
1357 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1358 __iscsi_put_task(conn->task);
1362 rc = iscsi_xmit_task(conn);
1367 /* process pending command queue */
1368 while (!list_empty(&conn->cmdqueue)) {
1369 if (conn->tmf_state == TMF_QUEUED)
1372 conn->task = list_entry(conn->cmdqueue.next,
1373 struct iscsi_task, running);
1374 list_del_init(&conn->task->running);
1375 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1376 fail_scsi_task(conn->task, DID_IMM_RETRY);
1379 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1381 if (rc == -ENOMEM) {
1382 list_add_tail(&conn->task->running,
1387 fail_scsi_task(conn->task, DID_ABORT);
1390 rc = iscsi_xmit_task(conn);
1394 * we could continuously get new task requests so
1395 * we need to check the mgmt queue for nops that need to
1396 * be sent to aviod starvation
1398 if (!list_empty(&conn->mgmtqueue))
1402 while (!list_empty(&conn->requeue)) {
1403 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1407 * we always do fastlogout - conn stop code will clean up.
1409 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1412 conn->task = list_entry(conn->requeue.next,
1413 struct iscsi_task, running);
1414 list_del_init(&conn->task->running);
1415 conn->task->state = ISCSI_TASK_RUNNING;
1416 rc = iscsi_xmit_task(conn);
1419 if (!list_empty(&conn->mgmtqueue))
1422 spin_unlock_bh(&conn->session->lock);
1426 spin_unlock_bh(&conn->session->lock);
1430 static void iscsi_xmitworker(struct work_struct *work)
1432 struct iscsi_conn *conn =
1433 container_of(work, struct iscsi_conn, xmitwork);
1436 * serialize Xmit worker on a per-connection basis.
1439 rc = iscsi_data_xmit(conn);
1440 } while (rc >= 0 || rc == -EAGAIN);
1443 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1444 struct scsi_cmnd *sc)
1446 struct iscsi_task *task;
1448 if (!__kfifo_get(conn->session->cmdpool.queue,
1449 (void *) &task, sizeof(void *)))
1452 sc->SCp.phase = conn->session->age;
1453 sc->SCp.ptr = (char *) task;
1455 atomic_set(&task->refcount, 1);
1456 task->state = ISCSI_TASK_PENDING;
1459 task->have_checked_conn = false;
1460 task->last_timeout = jiffies;
1461 task->last_xfer = jiffies;
1462 INIT_LIST_HEAD(&task->running);
1467 FAILURE_BAD_HOST = 1,
1468 FAILURE_SESSION_FAILED,
1469 FAILURE_SESSION_FREED,
1470 FAILURE_WINDOW_CLOSED,
1472 FAILURE_SESSION_TERMINATE,
1473 FAILURE_SESSION_IN_RECOVERY,
1474 FAILURE_SESSION_RECOVERY_TIMEOUT,
1475 FAILURE_SESSION_LOGGING_OUT,
1476 FAILURE_SESSION_NOT_READY,
1479 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1481 struct iscsi_cls_session *cls_session;
1482 struct Scsi_Host *host;
1483 struct iscsi_host *ihost;
1485 struct iscsi_session *session;
1486 struct iscsi_conn *conn;
1487 struct iscsi_task *task = NULL;
1489 sc->scsi_done = done;
1493 host = sc->device->host;
1494 ihost = shost_priv(host);
1495 spin_unlock(host->host_lock);
1497 cls_session = starget_to_session(scsi_target(sc->device));
1498 session = cls_session->dd_data;
1499 spin_lock(&session->lock);
1501 reason = iscsi_session_chkready(cls_session);
1503 sc->result = reason;
1507 if (session->state != ISCSI_STATE_LOGGED_IN) {
1509 * to handle the race between when we set the recovery state
1510 * and block the session we requeue here (commands could
1511 * be entering our queuecommand while a block is starting
1512 * up because the block code is not locked)
1514 switch (session->state) {
1515 case ISCSI_STATE_FAILED:
1516 case ISCSI_STATE_IN_RECOVERY:
1517 reason = FAILURE_SESSION_IN_RECOVERY;
1518 sc->result = DID_IMM_RETRY << 16;
1520 case ISCSI_STATE_LOGGING_OUT:
1521 reason = FAILURE_SESSION_LOGGING_OUT;
1522 sc->result = DID_IMM_RETRY << 16;
1524 case ISCSI_STATE_RECOVERY_FAILED:
1525 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1526 sc->result = DID_TRANSPORT_FAILFAST << 16;
1528 case ISCSI_STATE_TERMINATE:
1529 reason = FAILURE_SESSION_TERMINATE;
1530 sc->result = DID_NO_CONNECT << 16;
1533 reason = FAILURE_SESSION_FREED;
1534 sc->result = DID_NO_CONNECT << 16;
1539 conn = session->leadconn;
1541 reason = FAILURE_SESSION_FREED;
1542 sc->result = DID_NO_CONNECT << 16;
1546 if (iscsi_check_cmdsn_window_closed(conn)) {
1547 reason = FAILURE_WINDOW_CLOSED;
1551 task = iscsi_alloc_task(conn, sc);
1553 reason = FAILURE_OOM;
1557 if (!ihost->workq) {
1558 reason = iscsi_prep_scsi_cmd_pdu(task);
1560 if (reason == -ENOMEM) {
1561 reason = FAILURE_OOM;
1564 sc->result = DID_ABORT << 16;
1568 if (session->tt->xmit_task(task)) {
1570 reason = FAILURE_SESSION_NOT_READY;
1574 list_add_tail(&task->running, &conn->cmdqueue);
1575 iscsi_conn_queue_work(conn);
1578 session->queued_cmdsn++;
1579 spin_unlock(&session->lock);
1580 spin_lock(host->host_lock);
1584 sc->scsi_done = NULL;
1585 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1587 spin_unlock(&session->lock);
1588 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1589 sc->cmnd[0], reason);
1590 spin_lock(host->host_lock);
1591 return SCSI_MLQUEUE_TARGET_BUSY;
1594 sc->scsi_done = NULL;
1595 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1597 spin_unlock(&session->lock);
1598 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1599 sc->cmnd[0], reason);
1600 if (!scsi_bidi_cmnd(sc))
1601 scsi_set_resid(sc, scsi_bufflen(sc));
1603 scsi_out(sc)->resid = scsi_out(sc)->length;
1604 scsi_in(sc)->resid = scsi_in(sc)->length;
1607 spin_lock(host->host_lock);
1610 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1612 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1614 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1615 return sdev->queue_depth;
1617 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1619 int iscsi_target_alloc(struct scsi_target *starget)
1621 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1622 struct iscsi_session *session = cls_session->dd_data;
1624 starget->can_queue = session->scsi_cmds_max;
1627 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1629 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1631 struct iscsi_session *session = cls_session->dd_data;
1633 spin_lock_bh(&session->lock);
1634 if (session->state != ISCSI_STATE_LOGGED_IN) {
1635 session->state = ISCSI_STATE_RECOVERY_FAILED;
1636 if (session->leadconn)
1637 wake_up(&session->leadconn->ehwait);
1639 spin_unlock_bh(&session->lock);
1641 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1643 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
1645 struct iscsi_cls_session *cls_session;
1646 struct iscsi_session *session;
1647 struct iscsi_conn *conn;
1649 cls_session = starget_to_session(scsi_target(sc->device));
1650 session = cls_session->dd_data;
1651 conn = session->leadconn;
1653 mutex_lock(&session->eh_mutex);
1654 spin_lock_bh(&session->lock);
1655 if (session->state == ISCSI_STATE_TERMINATE) {
1657 ISCSI_DBG_EH(session,
1658 "failing target reset: Could not log back into "
1659 "target [age %d]\n",
1661 spin_unlock_bh(&session->lock);
1662 mutex_unlock(&session->eh_mutex);
1666 spin_unlock_bh(&session->lock);
1667 mutex_unlock(&session->eh_mutex);
1669 * we drop the lock here but the leadconn cannot be destoyed while
1670 * we are in the scsi eh
1672 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1674 ISCSI_DBG_EH(session, "wait for relogin\n");
1675 wait_event_interruptible(conn->ehwait,
1676 session->state == ISCSI_STATE_TERMINATE ||
1677 session->state == ISCSI_STATE_LOGGED_IN ||
1678 session->state == ISCSI_STATE_RECOVERY_FAILED);
1679 if (signal_pending(current))
1680 flush_signals(current);
1682 mutex_lock(&session->eh_mutex);
1683 spin_lock_bh(&session->lock);
1684 if (session->state == ISCSI_STATE_LOGGED_IN) {
1685 ISCSI_DBG_EH(session,
1686 "target reset succeeded\n");
1689 spin_unlock_bh(&session->lock);
1690 mutex_unlock(&session->eh_mutex);
1693 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
1695 static void iscsi_tmf_timedout(unsigned long data)
1697 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1698 struct iscsi_session *session = conn->session;
1700 spin_lock(&session->lock);
1701 if (conn->tmf_state == TMF_QUEUED) {
1702 conn->tmf_state = TMF_TIMEDOUT;
1703 ISCSI_DBG_EH(session, "tmf timedout\n");
1704 /* unblock eh_abort() */
1705 wake_up(&conn->ehwait);
1707 spin_unlock(&session->lock);
1710 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1711 struct iscsi_tm *hdr, int age,
1714 struct iscsi_session *session = conn->session;
1715 struct iscsi_task *task;
1717 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1720 spin_unlock_bh(&session->lock);
1721 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1722 spin_lock_bh(&session->lock);
1723 ISCSI_DBG_EH(session, "tmf exec failure\n");
1726 conn->tmfcmd_pdus_cnt++;
1727 conn->tmf_timer.expires = timeout * HZ + jiffies;
1728 conn->tmf_timer.function = iscsi_tmf_timedout;
1729 conn->tmf_timer.data = (unsigned long)conn;
1730 add_timer(&conn->tmf_timer);
1731 ISCSI_DBG_EH(session, "tmf set timeout\n");
1733 spin_unlock_bh(&session->lock);
1734 mutex_unlock(&session->eh_mutex);
1737 * block eh thread until:
1741 * 3) session is terminated or restarted or userspace has
1742 * given up on recovery
1744 wait_event_interruptible(conn->ehwait, age != session->age ||
1745 session->state != ISCSI_STATE_LOGGED_IN ||
1746 conn->tmf_state != TMF_QUEUED);
1747 if (signal_pending(current))
1748 flush_signals(current);
1749 del_timer_sync(&conn->tmf_timer);
1751 mutex_lock(&session->eh_mutex);
1752 spin_lock_bh(&session->lock);
1753 /* if the session drops it will clean up the task */
1754 if (age != session->age ||
1755 session->state != ISCSI_STATE_LOGGED_IN)
1761 * Fail commands. session lock held and recv side suspended and xmit
1764 static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1767 struct iscsi_task *task;
1770 for (i = 0; i < conn->session->cmds_max; i++) {
1771 task = conn->session->cmds[i];
1772 if (!task->sc || task->state == ISCSI_TASK_FREE)
1775 if (lun != -1 && lun != task->sc->device->lun)
1778 ISCSI_DBG_SESSION(conn->session,
1779 "failing sc %p itt 0x%x state %d\n",
1780 task->sc, task->itt, task->state);
1781 fail_scsi_task(task, error);
1785 void iscsi_suspend_tx(struct iscsi_conn *conn)
1787 struct Scsi_Host *shost = conn->session->host;
1788 struct iscsi_host *ihost = shost_priv(shost);
1790 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1792 flush_workqueue(ihost->workq);
1794 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1796 static void iscsi_start_tx(struct iscsi_conn *conn)
1798 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1799 iscsi_conn_queue_work(conn);
1803 * We want to make sure a ping is in flight. It has timed out.
1804 * And we are not busy processing a pdu that is making
1805 * progress but got started before the ping and is taking a while
1806 * to complete so the ping is just stuck behind it in a queue.
1808 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1810 if (conn->ping_task &&
1811 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1812 (conn->ping_timeout * HZ), jiffies))
1818 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1820 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1821 struct iscsi_task *task = NULL;
1822 struct iscsi_cls_session *cls_session;
1823 struct iscsi_session *session;
1824 struct iscsi_conn *conn;
1826 cls_session = starget_to_session(scsi_target(sc->device));
1827 session = cls_session->dd_data;
1829 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1831 spin_lock(&session->lock);
1832 if (session->state != ISCSI_STATE_LOGGED_IN) {
1834 * We are probably in the middle of iscsi recovery so let
1835 * that complete and handle the error.
1837 rc = BLK_EH_RESET_TIMER;
1841 conn = session->leadconn;
1843 /* In the middle of shuting down */
1844 rc = BLK_EH_RESET_TIMER;
1848 task = (struct iscsi_task *)sc->SCp.ptr;
1852 * If we have sent (at least queued to the network layer) a pdu or
1853 * recvd one for the task since the last timeout ask for
1854 * more time. If on the next timeout we have not made progress
1855 * we can check if it is the task or connection when we send the
1858 if (time_after_eq(task->last_xfer, task->last_timeout)) {
1859 ISCSI_DBG_EH(session, "Command making progress. Asking "
1860 "scsi-ml for more time to complete. "
1861 "Last data recv at %lu. Last timeout was at "
1862 "%lu\n.", task->last_xfer, task->last_timeout);
1863 task->have_checked_conn = false;
1864 rc = BLK_EH_RESET_TIMER;
1868 if (!conn->recv_timeout && !conn->ping_timeout)
1871 * if the ping timedout then we are in the middle of cleaning up
1872 * and can let the iscsi eh handle it
1874 if (iscsi_has_ping_timed_out(conn)) {
1875 rc = BLK_EH_RESET_TIMER;
1879 /* Assumes nop timeout is shorter than scsi cmd timeout */
1880 if (task->have_checked_conn)
1884 * Checking the transport already or nop from a cmd timeout still
1887 if (conn->ping_task) {
1888 task->have_checked_conn = true;
1889 rc = BLK_EH_RESET_TIMER;
1893 /* Make sure there is a transport check done */
1894 iscsi_send_nopout(conn, NULL);
1895 task->have_checked_conn = true;
1896 rc = BLK_EH_RESET_TIMER;
1900 task->last_timeout = jiffies;
1901 spin_unlock(&session->lock);
1902 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1903 "timer reset" : "nh");
1907 static void iscsi_check_transport_timeouts(unsigned long data)
1909 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1910 struct iscsi_session *session = conn->session;
1911 unsigned long recv_timeout, next_timeout = 0, last_recv;
1913 spin_lock(&session->lock);
1914 if (session->state != ISCSI_STATE_LOGGED_IN)
1917 recv_timeout = conn->recv_timeout;
1922 last_recv = conn->last_recv;
1924 if (iscsi_has_ping_timed_out(conn)) {
1925 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1926 "expired, recv timeout %d, last rx %lu, "
1927 "last ping %lu, now %lu\n",
1928 conn->ping_timeout, conn->recv_timeout,
1929 last_recv, conn->last_ping, jiffies);
1930 spin_unlock(&session->lock);
1931 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1935 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1936 /* send a ping to try to provoke some traffic */
1937 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
1938 iscsi_send_nopout(conn, NULL);
1939 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1941 next_timeout = last_recv + recv_timeout;
1943 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
1944 mod_timer(&conn->transport_timer, next_timeout);
1946 spin_unlock(&session->lock);
1949 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
1950 struct iscsi_tm *hdr)
1952 memset(hdr, 0, sizeof(*hdr));
1953 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1954 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1955 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1956 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1957 hdr->rtt = task->hdr_itt;
1958 hdr->refcmdsn = task->cmdsn;
1961 int iscsi_eh_abort(struct scsi_cmnd *sc)
1963 struct iscsi_cls_session *cls_session;
1964 struct iscsi_session *session;
1965 struct iscsi_conn *conn;
1966 struct iscsi_task *task;
1967 struct iscsi_tm *hdr;
1970 cls_session = starget_to_session(scsi_target(sc->device));
1971 session = cls_session->dd_data;
1973 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
1975 mutex_lock(&session->eh_mutex);
1976 spin_lock_bh(&session->lock);
1978 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1982 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
1984 spin_unlock_bh(&session->lock);
1985 mutex_unlock(&session->eh_mutex);
1990 * If we are not logged in or we have started a new session
1991 * then let the host reset code handle this
1993 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1994 sc->SCp.phase != session->age) {
1995 spin_unlock_bh(&session->lock);
1996 mutex_unlock(&session->eh_mutex);
1997 ISCSI_DBG_EH(session, "failing abort due to dropped "
2002 conn = session->leadconn;
2003 conn->eh_abort_cnt++;
2006 task = (struct iscsi_task *)sc->SCp.ptr;
2007 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2010 /* task completed before time out */
2012 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2016 if (task->state == ISCSI_TASK_PENDING) {
2017 fail_scsi_task(task, DID_ABORT);
2021 /* only have one tmf outstanding at a time */
2022 if (conn->tmf_state != TMF_INITIAL)
2024 conn->tmf_state = TMF_QUEUED;
2027 iscsi_prep_abort_task_pdu(task, hdr);
2029 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
2034 switch (conn->tmf_state) {
2036 spin_unlock_bh(&session->lock);
2038 * stop tx side incase the target had sent a abort rsp but
2039 * the initiator was still writing out data.
2041 iscsi_suspend_tx(conn);
2043 * we do not stop the recv side because targets have been
2044 * good and have never sent us a successful tmf response
2045 * then sent more data for the cmd.
2047 spin_lock_bh(&session->lock);
2048 fail_scsi_task(task, DID_ABORT);
2049 conn->tmf_state = TMF_INITIAL;
2050 spin_unlock_bh(&session->lock);
2051 iscsi_start_tx(conn);
2052 goto success_unlocked;
2054 spin_unlock_bh(&session->lock);
2055 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2056 goto failed_unlocked;
2059 conn->tmf_state = TMF_INITIAL;
2060 /* task completed before tmf abort response */
2061 ISCSI_DBG_EH(session, "sc completed while abort in "
2067 conn->tmf_state = TMF_INITIAL;
2072 spin_unlock_bh(&session->lock);
2074 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2076 mutex_unlock(&session->eh_mutex);
2080 spin_unlock_bh(&session->lock);
2082 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2083 task ? task->itt : 0);
2084 mutex_unlock(&session->eh_mutex);
2087 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2089 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2091 memset(hdr, 0, sizeof(*hdr));
2092 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2093 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2094 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2095 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
2096 hdr->rtt = RESERVED_ITT;
2099 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2101 struct iscsi_cls_session *cls_session;
2102 struct iscsi_session *session;
2103 struct iscsi_conn *conn;
2104 struct iscsi_tm *hdr;
2107 cls_session = starget_to_session(scsi_target(sc->device));
2108 session = cls_session->dd_data;
2110 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2112 mutex_lock(&session->eh_mutex);
2113 spin_lock_bh(&session->lock);
2115 * Just check if we are not logged in. We cannot check for
2116 * the phase because the reset could come from a ioctl.
2118 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2120 conn = session->leadconn;
2122 /* only have one tmf outstanding at a time */
2123 if (conn->tmf_state != TMF_INITIAL)
2125 conn->tmf_state = TMF_QUEUED;
2128 iscsi_prep_lun_reset_pdu(sc, hdr);
2130 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2131 session->lu_reset_timeout)) {
2136 switch (conn->tmf_state) {
2140 spin_unlock_bh(&session->lock);
2141 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2144 conn->tmf_state = TMF_INITIAL;
2149 spin_unlock_bh(&session->lock);
2151 iscsi_suspend_tx(conn);
2153 spin_lock_bh(&session->lock);
2154 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2155 conn->tmf_state = TMF_INITIAL;
2156 spin_unlock_bh(&session->lock);
2158 iscsi_start_tx(conn);
2162 spin_unlock_bh(&session->lock);
2164 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2165 rc == SUCCESS ? "SUCCESS" : "FAILED");
2166 mutex_unlock(&session->eh_mutex);
2169 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2172 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2173 * should be accessed via kfifo_{get,put} on q->queue.
2174 * Optionally, the caller can obtain the array of object pointers
2175 * by passing in a non-NULL @items pointer
2178 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2180 int i, num_arrays = 1;
2182 memset(q, 0, sizeof(*q));
2186 /* If the user passed an items pointer, he wants a copy of
2190 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2191 if (q->pool == NULL)
2194 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2196 if (IS_ERR(q->queue)) {
2201 for (i = 0; i < max; i++) {
2202 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2203 if (q->pool[i] == NULL) {
2207 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2211 *items = q->pool + max;
2212 memcpy(*items, q->pool, max * sizeof(void *));
2221 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2223 void iscsi_pool_free(struct iscsi_pool *q)
2227 for (i = 0; i < q->max; i++)
2232 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2235 * iscsi_host_add - add host to system
2237 * @pdev: parent device
2239 * This should be called by partial offload and software iscsi drivers
2240 * to add a host to the system.
2242 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2244 if (!shost->can_queue)
2245 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2247 if (!shost->cmd_per_lun)
2248 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2250 if (!shost->transportt->eh_timed_out)
2251 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2252 return scsi_add_host(shost, pdev);
2254 EXPORT_SYMBOL_GPL(iscsi_host_add);
2257 * iscsi_host_alloc - allocate a host and driver data
2258 * @sht: scsi host template
2259 * @dd_data_size: driver host data size
2260 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2262 * This should be called by partial offload and software iscsi drivers.
2263 * To access the driver specific memory use the iscsi_host_priv() macro.
2265 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2266 int dd_data_size, bool xmit_can_sleep)
2268 struct Scsi_Host *shost;
2269 struct iscsi_host *ihost;
2271 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2274 ihost = shost_priv(shost);
2276 if (xmit_can_sleep) {
2277 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2278 "iscsi_q_%d", shost->host_no);
2279 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2284 spin_lock_init(&ihost->lock);
2285 ihost->state = ISCSI_HOST_SETUP;
2286 ihost->num_sessions = 0;
2287 init_waitqueue_head(&ihost->session_removal_wq);
2291 scsi_host_put(shost);
2294 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2296 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2298 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2302 * iscsi_host_remove - remove host and sessions
2305 * If there are any sessions left, this will initiate the removal and wait
2306 * for the completion.
2308 void iscsi_host_remove(struct Scsi_Host *shost)
2310 struct iscsi_host *ihost = shost_priv(shost);
2311 unsigned long flags;
2313 spin_lock_irqsave(&ihost->lock, flags);
2314 ihost->state = ISCSI_HOST_REMOVED;
2315 spin_unlock_irqrestore(&ihost->lock, flags);
2317 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2318 wait_event_interruptible(ihost->session_removal_wq,
2319 ihost->num_sessions == 0);
2320 if (signal_pending(current))
2321 flush_signals(current);
2323 scsi_remove_host(shost);
2325 destroy_workqueue(ihost->workq);
2327 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2329 void iscsi_host_free(struct Scsi_Host *shost)
2331 struct iscsi_host *ihost = shost_priv(shost);
2333 kfree(ihost->netdev);
2334 kfree(ihost->hwaddress);
2335 kfree(ihost->initiatorname);
2336 scsi_host_put(shost);
2338 EXPORT_SYMBOL_GPL(iscsi_host_free);
2340 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2342 struct iscsi_host *ihost = shost_priv(shost);
2343 unsigned long flags;
2345 shost = scsi_host_get(shost);
2347 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2348 "of session teardown event because host already "
2353 spin_lock_irqsave(&ihost->lock, flags);
2354 ihost->num_sessions--;
2355 if (ihost->num_sessions == 0)
2356 wake_up(&ihost->session_removal_wq);
2357 spin_unlock_irqrestore(&ihost->lock, flags);
2358 scsi_host_put(shost);
2362 * iscsi_session_setup - create iscsi cls session and host and session
2363 * @iscsit: iscsi transport template
2365 * @cmds_max: session can queue
2366 * @cmd_task_size: LLD task private data size
2367 * @initial_cmdsn: initial CmdSN
2369 * This can be used by software iscsi_transports that allocate
2370 * a session per scsi host.
2372 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2373 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2374 * for nop handling and login/logout requests.
2376 struct iscsi_cls_session *
2377 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2378 uint16_t cmds_max, int cmd_task_size,
2379 uint32_t initial_cmdsn, unsigned int id)
2381 struct iscsi_host *ihost = shost_priv(shost);
2382 struct iscsi_session *session;
2383 struct iscsi_cls_session *cls_session;
2384 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2385 unsigned long flags;
2387 spin_lock_irqsave(&ihost->lock, flags);
2388 if (ihost->state == ISCSI_HOST_REMOVED) {
2389 spin_unlock_irqrestore(&ihost->lock, flags);
2392 ihost->num_sessions++;
2393 spin_unlock_irqrestore(&ihost->lock, flags);
2396 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2398 * The iscsi layer needs some tasks for nop handling and tmfs,
2399 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2400 * + 1 command for scsi IO.
2402 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2403 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2404 "must be a power of two that is at least %d.\n",
2405 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2406 goto dec_session_count;
2409 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2410 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2411 "must be a power of 2 less than or equal to %d.\n",
2412 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2413 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2416 if (!is_power_of_2(total_cmds)) {
2417 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2418 "must be a power of 2.\n", total_cmds);
2419 total_cmds = rounddown_pow_of_two(total_cmds);
2420 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2422 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2425 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2427 cls_session = iscsi_alloc_session(shost, iscsit,
2428 sizeof(struct iscsi_session));
2430 goto dec_session_count;
2431 session = cls_session->dd_data;
2432 session->cls_session = cls_session;
2433 session->host = shost;
2434 session->state = ISCSI_STATE_FREE;
2435 session->fast_abort = 1;
2436 session->lu_reset_timeout = 15;
2437 session->abort_timeout = 10;
2438 session->scsi_cmds_max = scsi_cmds;
2439 session->cmds_max = total_cmds;
2440 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2441 session->exp_cmdsn = initial_cmdsn + 1;
2442 session->max_cmdsn = initial_cmdsn + 1;
2443 session->max_r2t = 1;
2444 session->tt = iscsit;
2445 mutex_init(&session->eh_mutex);
2446 spin_lock_init(&session->lock);
2448 /* initialize SCSI PDU commands pool */
2449 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2450 (void***)&session->cmds,
2451 cmd_task_size + sizeof(struct iscsi_task)))
2452 goto cmdpool_alloc_fail;
2454 /* pre-format cmds pool with ITT */
2455 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2456 struct iscsi_task *task = session->cmds[cmd_i];
2459 task->dd_data = &task[1];
2461 task->state = ISCSI_TASK_FREE;
2462 INIT_LIST_HEAD(&task->running);
2465 if (!try_module_get(iscsit->owner))
2466 goto module_get_fail;
2468 if (iscsi_add_session(cls_session, id))
2469 goto cls_session_fail;
2474 module_put(iscsit->owner);
2476 iscsi_pool_free(&session->cmdpool);
2478 iscsi_free_session(cls_session);
2480 iscsi_host_dec_session_cnt(shost);
2483 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2486 * iscsi_session_teardown - destroy session, host, and cls_session
2487 * @cls_session: iscsi session
2489 * The driver must have called iscsi_remove_session before
2492 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2494 struct iscsi_session *session = cls_session->dd_data;
2495 struct module *owner = cls_session->transport->owner;
2496 struct Scsi_Host *shost = session->host;
2498 iscsi_pool_free(&session->cmdpool);
2500 kfree(session->password);
2501 kfree(session->password_in);
2502 kfree(session->username);
2503 kfree(session->username_in);
2504 kfree(session->targetname);
2505 kfree(session->initiatorname);
2506 kfree(session->ifacename);
2508 iscsi_destroy_session(cls_session);
2509 iscsi_host_dec_session_cnt(shost);
2512 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2515 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2516 * @cls_session: iscsi_cls_session
2517 * @dd_size: private driver data size
2520 struct iscsi_cls_conn *
2521 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2524 struct iscsi_session *session = cls_session->dd_data;
2525 struct iscsi_conn *conn;
2526 struct iscsi_cls_conn *cls_conn;
2529 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2533 conn = cls_conn->dd_data;
2534 memset(conn, 0, sizeof(*conn) + dd_size);
2536 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2537 conn->session = session;
2538 conn->cls_conn = cls_conn;
2539 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2540 conn->id = conn_idx;
2541 conn->exp_statsn = 0;
2542 conn->tmf_state = TMF_INITIAL;
2544 init_timer(&conn->transport_timer);
2545 conn->transport_timer.data = (unsigned long)conn;
2546 conn->transport_timer.function = iscsi_check_transport_timeouts;
2548 INIT_LIST_HEAD(&conn->mgmtqueue);
2549 INIT_LIST_HEAD(&conn->cmdqueue);
2550 INIT_LIST_HEAD(&conn->requeue);
2551 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2553 /* allocate login_task used for the login/text sequences */
2554 spin_lock_bh(&session->lock);
2555 if (!__kfifo_get(session->cmdpool.queue,
2556 (void*)&conn->login_task,
2558 spin_unlock_bh(&session->lock);
2559 goto login_task_alloc_fail;
2561 spin_unlock_bh(&session->lock);
2563 data = (char *) __get_free_pages(GFP_KERNEL,
2564 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2566 goto login_task_data_alloc_fail;
2567 conn->login_task->data = conn->data = data;
2569 init_timer(&conn->tmf_timer);
2570 init_waitqueue_head(&conn->ehwait);
2574 login_task_data_alloc_fail:
2575 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2577 login_task_alloc_fail:
2578 iscsi_destroy_conn(cls_conn);
2581 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2584 * iscsi_conn_teardown - teardown iscsi connection
2585 * cls_conn: iscsi class connection
2587 * TODO: we may need to make this into a two step process
2588 * like scsi-mls remove + put host
2590 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2592 struct iscsi_conn *conn = cls_conn->dd_data;
2593 struct iscsi_session *session = conn->session;
2594 unsigned long flags;
2596 del_timer_sync(&conn->transport_timer);
2598 spin_lock_bh(&session->lock);
2599 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2600 if (session->leadconn == conn) {
2602 * leading connection? then give up on recovery.
2604 session->state = ISCSI_STATE_TERMINATE;
2605 wake_up(&conn->ehwait);
2607 spin_unlock_bh(&session->lock);
2610 * Block until all in-progress commands for this connection
2614 spin_lock_irqsave(session->host->host_lock, flags);
2615 if (!session->host->host_busy) { /* OK for ERL == 0 */
2616 spin_unlock_irqrestore(session->host->host_lock, flags);
2619 spin_unlock_irqrestore(session->host->host_lock, flags);
2620 msleep_interruptible(500);
2621 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2622 "host_busy %d host_failed %d\n",
2623 session->host->host_busy,
2624 session->host->host_failed);
2626 * force eh_abort() to unblock
2628 wake_up(&conn->ehwait);
2631 /* flush queued up work because we free the connection below */
2632 iscsi_suspend_tx(conn);
2634 spin_lock_bh(&session->lock);
2635 free_pages((unsigned long) conn->data,
2636 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2637 kfree(conn->persistent_address);
2638 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2640 if (session->leadconn == conn)
2641 session->leadconn = NULL;
2642 spin_unlock_bh(&session->lock);
2644 iscsi_destroy_conn(cls_conn);
2646 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2648 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2650 struct iscsi_conn *conn = cls_conn->dd_data;
2651 struct iscsi_session *session = conn->session;
2654 iscsi_conn_printk(KERN_ERR, conn,
2655 "can't start unbound connection\n");
2659 if ((session->imm_data_en || !session->initial_r2t_en) &&
2660 session->first_burst > session->max_burst) {
2661 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2662 "first_burst %d max_burst %d\n",
2663 session->first_burst, session->max_burst);
2667 if (conn->ping_timeout && !conn->recv_timeout) {
2668 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2669 "zero. Using 5 seconds\n.");
2670 conn->recv_timeout = 5;
2673 if (conn->recv_timeout && !conn->ping_timeout) {
2674 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2675 "zero. Using 5 seconds.\n");
2676 conn->ping_timeout = 5;
2679 spin_lock_bh(&session->lock);
2680 conn->c_stage = ISCSI_CONN_STARTED;
2681 session->state = ISCSI_STATE_LOGGED_IN;
2682 session->queued_cmdsn = session->cmdsn;
2684 conn->last_recv = jiffies;
2685 conn->last_ping = jiffies;
2686 if (conn->recv_timeout && conn->ping_timeout)
2687 mod_timer(&conn->transport_timer,
2688 jiffies + (conn->recv_timeout * HZ));
2690 switch(conn->stop_stage) {
2691 case STOP_CONN_RECOVER:
2693 * unblock eh_abort() if it is blocked. re-try all
2694 * commands after successful recovery
2696 conn->stop_stage = 0;
2697 conn->tmf_state = TMF_INITIAL;
2699 if (session->age == 16)
2702 case STOP_CONN_TERM:
2703 conn->stop_stage = 0;
2708 spin_unlock_bh(&session->lock);
2710 iscsi_unblock_session(session->cls_session);
2711 wake_up(&conn->ehwait);
2714 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2717 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
2719 struct iscsi_task *task;
2722 for (i = 0; i < conn->session->cmds_max; i++) {
2723 task = conn->session->cmds[i];
2727 if (task->state == ISCSI_TASK_FREE)
2730 ISCSI_DBG_SESSION(conn->session,
2731 "failing mgmt itt 0x%x state %d\n",
2732 task->itt, task->state);
2733 state = ISCSI_TASK_ABRT_SESS_RECOV;
2734 if (task->state == ISCSI_TASK_PENDING)
2735 state = ISCSI_TASK_COMPLETED;
2736 iscsi_complete_task(task, state);
2741 static void iscsi_start_session_recovery(struct iscsi_session *session,
2742 struct iscsi_conn *conn, int flag)
2746 mutex_lock(&session->eh_mutex);
2747 spin_lock_bh(&session->lock);
2748 if (conn->stop_stage == STOP_CONN_TERM) {
2749 spin_unlock_bh(&session->lock);
2750 mutex_unlock(&session->eh_mutex);
2755 * When this is called for the in_login state, we only want to clean
2756 * up the login task and connection. We do not need to block and set
2757 * the recovery state again
2759 if (flag == STOP_CONN_TERM)
2760 session->state = ISCSI_STATE_TERMINATE;
2761 else if (conn->stop_stage != STOP_CONN_RECOVER)
2762 session->state = ISCSI_STATE_IN_RECOVERY;
2763 spin_unlock_bh(&session->lock);
2765 del_timer_sync(&conn->transport_timer);
2766 iscsi_suspend_tx(conn);
2768 spin_lock_bh(&session->lock);
2769 old_stop_stage = conn->stop_stage;
2770 conn->stop_stage = flag;
2771 conn->c_stage = ISCSI_CONN_STOPPED;
2772 spin_unlock_bh(&session->lock);
2775 * for connection level recovery we should not calculate
2776 * header digest. conn->hdr_size used for optimization
2777 * in hdr_extract() and will be re-negotiated at
2780 if (flag == STOP_CONN_RECOVER) {
2781 conn->hdrdgst_en = 0;
2782 conn->datadgst_en = 0;
2783 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2784 old_stop_stage != STOP_CONN_RECOVER) {
2785 ISCSI_DBG_SESSION(session, "blocking session\n");
2786 iscsi_block_session(session->cls_session);
2793 spin_lock_bh(&session->lock);
2794 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
2795 fail_mgmt_tasks(session, conn);
2796 spin_unlock_bh(&session->lock);
2797 mutex_unlock(&session->eh_mutex);
2800 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2802 struct iscsi_conn *conn = cls_conn->dd_data;
2803 struct iscsi_session *session = conn->session;
2806 case STOP_CONN_RECOVER:
2807 case STOP_CONN_TERM:
2808 iscsi_start_session_recovery(session, conn, flag);
2811 iscsi_conn_printk(KERN_ERR, conn,
2812 "invalid stop flag %d\n", flag);
2815 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2817 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2818 struct iscsi_cls_conn *cls_conn, int is_leading)
2820 struct iscsi_session *session = cls_session->dd_data;
2821 struct iscsi_conn *conn = cls_conn->dd_data;
2823 spin_lock_bh(&session->lock);
2825 session->leadconn = conn;
2826 spin_unlock_bh(&session->lock);
2829 * Unblock xmitworker(), Login Phase will pass through.
2831 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2832 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2835 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2837 static int iscsi_switch_str_param(char **param, char *new_val_buf)
2842 if (!strcmp(*param, new_val_buf))
2846 new_val = kstrdup(new_val_buf, GFP_NOIO);
2855 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2856 enum iscsi_param param, char *buf, int buflen)
2858 struct iscsi_conn *conn = cls_conn->dd_data;
2859 struct iscsi_session *session = conn->session;
2863 case ISCSI_PARAM_FAST_ABORT:
2864 sscanf(buf, "%d", &session->fast_abort);
2866 case ISCSI_PARAM_ABORT_TMO:
2867 sscanf(buf, "%d", &session->abort_timeout);
2869 case ISCSI_PARAM_LU_RESET_TMO:
2870 sscanf(buf, "%d", &session->lu_reset_timeout);
2872 case ISCSI_PARAM_PING_TMO:
2873 sscanf(buf, "%d", &conn->ping_timeout);
2875 case ISCSI_PARAM_RECV_TMO:
2876 sscanf(buf, "%d", &conn->recv_timeout);
2878 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2879 sscanf(buf, "%d", &conn->max_recv_dlength);
2881 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2882 sscanf(buf, "%d", &conn->max_xmit_dlength);
2884 case ISCSI_PARAM_HDRDGST_EN:
2885 sscanf(buf, "%d", &conn->hdrdgst_en);
2887 case ISCSI_PARAM_DATADGST_EN:
2888 sscanf(buf, "%d", &conn->datadgst_en);
2890 case ISCSI_PARAM_INITIAL_R2T_EN:
2891 sscanf(buf, "%d", &session->initial_r2t_en);
2893 case ISCSI_PARAM_MAX_R2T:
2894 sscanf(buf, "%d", &session->max_r2t);
2896 case ISCSI_PARAM_IMM_DATA_EN:
2897 sscanf(buf, "%d", &session->imm_data_en);
2899 case ISCSI_PARAM_FIRST_BURST:
2900 sscanf(buf, "%d", &session->first_burst);
2902 case ISCSI_PARAM_MAX_BURST:
2903 sscanf(buf, "%d", &session->max_burst);
2905 case ISCSI_PARAM_PDU_INORDER_EN:
2906 sscanf(buf, "%d", &session->pdu_inorder_en);
2908 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2909 sscanf(buf, "%d", &session->dataseq_inorder_en);
2911 case ISCSI_PARAM_ERL:
2912 sscanf(buf, "%d", &session->erl);
2914 case ISCSI_PARAM_IFMARKER_EN:
2915 sscanf(buf, "%d", &value);
2918 case ISCSI_PARAM_OFMARKER_EN:
2919 sscanf(buf, "%d", &value);
2922 case ISCSI_PARAM_EXP_STATSN:
2923 sscanf(buf, "%u", &conn->exp_statsn);
2925 case ISCSI_PARAM_USERNAME:
2926 return iscsi_switch_str_param(&session->username, buf);
2927 case ISCSI_PARAM_USERNAME_IN:
2928 return iscsi_switch_str_param(&session->username_in, buf);
2929 case ISCSI_PARAM_PASSWORD:
2930 return iscsi_switch_str_param(&session->password, buf);
2931 case ISCSI_PARAM_PASSWORD_IN:
2932 return iscsi_switch_str_param(&session->password_in, buf);
2933 case ISCSI_PARAM_TARGET_NAME:
2934 return iscsi_switch_str_param(&session->targetname, buf);
2935 case ISCSI_PARAM_TPGT:
2936 sscanf(buf, "%d", &session->tpgt);
2938 case ISCSI_PARAM_PERSISTENT_PORT:
2939 sscanf(buf, "%d", &conn->persistent_port);
2941 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2942 return iscsi_switch_str_param(&conn->persistent_address, buf);
2943 case ISCSI_PARAM_IFACE_NAME:
2944 return iscsi_switch_str_param(&session->ifacename, buf);
2945 case ISCSI_PARAM_INITIATOR_NAME:
2946 return iscsi_switch_str_param(&session->initiatorname, buf);
2953 EXPORT_SYMBOL_GPL(iscsi_set_param);
2955 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2956 enum iscsi_param param, char *buf)
2958 struct iscsi_session *session = cls_session->dd_data;
2962 case ISCSI_PARAM_FAST_ABORT:
2963 len = sprintf(buf, "%d\n", session->fast_abort);
2965 case ISCSI_PARAM_ABORT_TMO:
2966 len = sprintf(buf, "%d\n", session->abort_timeout);
2968 case ISCSI_PARAM_LU_RESET_TMO:
2969 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2971 case ISCSI_PARAM_INITIAL_R2T_EN:
2972 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2974 case ISCSI_PARAM_MAX_R2T:
2975 len = sprintf(buf, "%hu\n", session->max_r2t);
2977 case ISCSI_PARAM_IMM_DATA_EN:
2978 len = sprintf(buf, "%d\n", session->imm_data_en);
2980 case ISCSI_PARAM_FIRST_BURST:
2981 len = sprintf(buf, "%u\n", session->first_burst);
2983 case ISCSI_PARAM_MAX_BURST:
2984 len = sprintf(buf, "%u\n", session->max_burst);
2986 case ISCSI_PARAM_PDU_INORDER_EN:
2987 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2989 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2990 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2992 case ISCSI_PARAM_ERL:
2993 len = sprintf(buf, "%d\n", session->erl);
2995 case ISCSI_PARAM_TARGET_NAME:
2996 len = sprintf(buf, "%s\n", session->targetname);
2998 case ISCSI_PARAM_TPGT:
2999 len = sprintf(buf, "%d\n", session->tpgt);
3001 case ISCSI_PARAM_USERNAME:
3002 len = sprintf(buf, "%s\n", session->username);
3004 case ISCSI_PARAM_USERNAME_IN:
3005 len = sprintf(buf, "%s\n", session->username_in);
3007 case ISCSI_PARAM_PASSWORD:
3008 len = sprintf(buf, "%s\n", session->password);
3010 case ISCSI_PARAM_PASSWORD_IN:
3011 len = sprintf(buf, "%s\n", session->password_in);
3013 case ISCSI_PARAM_IFACE_NAME:
3014 len = sprintf(buf, "%s\n", session->ifacename);
3016 case ISCSI_PARAM_INITIATOR_NAME:
3017 len = sprintf(buf, "%s\n", session->initiatorname);
3025 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3027 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3028 enum iscsi_param param, char *buf)
3030 struct iscsi_conn *conn = cls_conn->dd_data;
3034 case ISCSI_PARAM_PING_TMO:
3035 len = sprintf(buf, "%u\n", conn->ping_timeout);
3037 case ISCSI_PARAM_RECV_TMO:
3038 len = sprintf(buf, "%u\n", conn->recv_timeout);
3040 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3041 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3043 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3044 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3046 case ISCSI_PARAM_HDRDGST_EN:
3047 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3049 case ISCSI_PARAM_DATADGST_EN:
3050 len = sprintf(buf, "%d\n", conn->datadgst_en);
3052 case ISCSI_PARAM_IFMARKER_EN:
3053 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3055 case ISCSI_PARAM_OFMARKER_EN:
3056 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3058 case ISCSI_PARAM_EXP_STATSN:
3059 len = sprintf(buf, "%u\n", conn->exp_statsn);
3061 case ISCSI_PARAM_PERSISTENT_PORT:
3062 len = sprintf(buf, "%d\n", conn->persistent_port);
3064 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3065 len = sprintf(buf, "%s\n", conn->persistent_address);
3073 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3075 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3078 struct iscsi_host *ihost = shost_priv(shost);
3082 case ISCSI_HOST_PARAM_NETDEV_NAME:
3083 len = sprintf(buf, "%s\n", ihost->netdev);
3085 case ISCSI_HOST_PARAM_HWADDRESS:
3086 len = sprintf(buf, "%s\n", ihost->hwaddress);
3088 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3089 len = sprintf(buf, "%s\n", ihost->initiatorname);
3091 case ISCSI_HOST_PARAM_IPADDRESS:
3092 len = sprintf(buf, "%s\n", ihost->local_address);
3100 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3102 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3103 char *buf, int buflen)
3105 struct iscsi_host *ihost = shost_priv(shost);
3108 case ISCSI_HOST_PARAM_NETDEV_NAME:
3109 return iscsi_switch_str_param(&ihost->netdev, buf);
3110 case ISCSI_HOST_PARAM_HWADDRESS:
3111 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3112 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3113 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3120 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3122 MODULE_AUTHOR("Mike Christie");
3123 MODULE_DESCRIPTION("iSCSI library functions");
3124 MODULE_LICENSE("GPL");