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->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
306 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
307 cmd_len = sc->cmd_len;
308 if (cmd_len < ISCSI_CDB_SIZE)
309 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
310 else if (cmd_len > ISCSI_CDB_SIZE) {
311 rc = iscsi_prep_ecdb_ahs(task);
314 cmd_len = ISCSI_CDB_SIZE;
316 memcpy(hdr->cdb, sc->cmnd, cmd_len);
319 if (scsi_bidi_cmnd(sc)) {
320 hdr->flags |= ISCSI_FLAG_CMD_READ;
321 rc = iscsi_prep_bidi_ahs(task);
325 if (sc->sc_data_direction == DMA_TO_DEVICE) {
326 unsigned out_len = scsi_out(sc)->length;
327 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
329 hdr->data_length = cpu_to_be32(out_len);
330 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
334 * imm_count bytes to be sent right after
337 * unsol_count bytes(as Data-Out) to be sent
338 * without R2T ack right after
341 * r2t data_length bytes to be sent via R2T ack's
343 * pad_count bytes to be sent as zero-padding
345 memset(r2t, 0, sizeof(*r2t));
347 if (session->imm_data_en) {
348 if (out_len >= session->first_burst)
349 task->imm_count = min(session->first_burst,
350 conn->max_xmit_dlength);
352 task->imm_count = min(out_len,
353 conn->max_xmit_dlength);
354 hton24(hdr->dlength, task->imm_count);
356 zero_data(hdr->dlength);
358 if (!session->initial_r2t_en) {
359 r2t->data_length = min(session->first_burst, out_len) -
361 r2t->data_offset = task->imm_count;
362 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
363 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
366 if (!task->unsol_r2t.data_length)
367 /* No unsolicit Data-Out's */
368 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
370 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
371 zero_data(hdr->dlength);
372 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
374 if (sc->sc_data_direction == DMA_FROM_DEVICE)
375 hdr->flags |= ISCSI_FLAG_CMD_READ;
378 /* calculate size of additional header segments (AHSs) */
379 hdrlength = task->hdr_len - sizeof(*hdr);
381 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
382 hdrlength /= ISCSI_PAD_LEN;
384 WARN_ON(hdrlength >= 256);
385 hdr->hlength = hdrlength & 0xFF;
387 if (session->tt->init_task && session->tt->init_task(task))
390 task->state = ISCSI_TASK_RUNNING;
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_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
861 char *data, int datalen)
863 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
864 struct iscsi_hdr rejected_pdu;
866 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
868 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
869 if (ntoh24(reject->dlength) > datalen)
870 return ISCSI_ERR_PROTO;
872 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
873 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
874 iscsi_conn_printk(KERN_ERR, conn,
875 "pdu (op 0x%x) rejected "
876 "due to DataDigest error.\n",
877 rejected_pdu.opcode);
884 * iscsi_itt_to_task - look up task by itt
885 * @conn: iscsi connection
888 * This should be used for mgmt tasks like login and nops, or if
889 * the LDD's itt space does not include the session age.
891 * The session lock must be held.
893 struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
895 struct iscsi_session *session = conn->session;
898 if (itt == RESERVED_ITT)
901 if (session->tt->parse_pdu_itt)
902 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
905 if (i >= session->cmds_max)
908 return session->cmds[i];
910 EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
913 * __iscsi_complete_pdu - complete pdu
917 * @datalen: len of data buffer
919 * Completes pdu processing by freeing any resources allocated at
920 * queuecommand or send generic. session lock must be held and verify
921 * itt must have been called.
923 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
924 char *data, int datalen)
926 struct iscsi_session *session = conn->session;
927 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
928 struct iscsi_task *task;
931 conn->last_recv = jiffies;
932 rc = iscsi_verify_itt(conn, hdr->itt);
936 if (hdr->itt != RESERVED_ITT)
937 itt = get_itt(hdr->itt);
941 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
942 opcode, conn->id, itt, datalen);
945 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
948 case ISCSI_OP_NOOP_IN:
950 rc = ISCSI_ERR_PROTO;
954 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
957 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
959 case ISCSI_OP_REJECT:
960 rc = iscsi_handle_reject(conn, hdr, data, datalen);
962 case ISCSI_OP_ASYNC_EVENT:
963 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
964 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
965 rc = ISCSI_ERR_CONN_FAILED;
968 rc = ISCSI_ERR_BAD_OPCODE;
975 case ISCSI_OP_SCSI_CMD_RSP:
976 case ISCSI_OP_SCSI_DATA_IN:
977 task = iscsi_itt_to_ctask(conn, hdr->itt);
979 return ISCSI_ERR_BAD_ITT;
980 task->last_xfer = jiffies;
984 * LLD handles R2Ts if they need to.
987 case ISCSI_OP_LOGOUT_RSP:
988 case ISCSI_OP_LOGIN_RSP:
989 case ISCSI_OP_TEXT_RSP:
990 case ISCSI_OP_SCSI_TMFUNC_RSP:
991 case ISCSI_OP_NOOP_IN:
992 task = iscsi_itt_to_task(conn, hdr->itt);
994 return ISCSI_ERR_BAD_ITT;
997 return ISCSI_ERR_BAD_OPCODE;
1001 case ISCSI_OP_SCSI_CMD_RSP:
1002 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1004 case ISCSI_OP_SCSI_DATA_IN:
1005 iscsi_data_in_rsp(conn, hdr, task);
1007 case ISCSI_OP_LOGOUT_RSP:
1008 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1010 rc = ISCSI_ERR_PROTO;
1013 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1015 case ISCSI_OP_LOGIN_RSP:
1016 case ISCSI_OP_TEXT_RSP:
1017 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1019 * login related PDU's exp_statsn is handled in
1023 case ISCSI_OP_SCSI_TMFUNC_RSP:
1024 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1026 rc = ISCSI_ERR_PROTO;
1030 iscsi_tmf_rsp(conn, hdr);
1031 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1033 case ISCSI_OP_NOOP_IN:
1034 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1035 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1036 rc = ISCSI_ERR_PROTO;
1039 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1041 if (conn->ping_task != task)
1043 * If this is not in response to one of our
1044 * nops then it must be from userspace.
1048 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1049 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1052 rc = ISCSI_ERR_BAD_OPCODE;
1059 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1060 rc = ISCSI_ERR_CONN_FAILED;
1061 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1064 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1066 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1067 char *data, int datalen)
1071 spin_lock(&conn->session->lock);
1072 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1073 spin_unlock(&conn->session->lock);
1076 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1078 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1080 struct iscsi_session *session = conn->session;
1083 if (itt == RESERVED_ITT)
1086 if (session->tt->parse_pdu_itt)
1087 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1090 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1093 if (age != session->age) {
1094 iscsi_conn_printk(KERN_ERR, conn,
1095 "received itt %x expected session age (%x)\n",
1096 (__force u32)itt, session->age);
1097 return ISCSI_ERR_BAD_ITT;
1100 if (i >= session->cmds_max) {
1101 iscsi_conn_printk(KERN_ERR, conn,
1102 "received invalid itt index %u (max cmds "
1103 "%u.\n", i, session->cmds_max);
1104 return ISCSI_ERR_BAD_ITT;
1108 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1111 * iscsi_itt_to_ctask - look up ctask by itt
1112 * @conn: iscsi connection
1115 * This should be used for cmd tasks.
1117 * The session lock must be held.
1119 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1121 struct iscsi_task *task;
1123 if (iscsi_verify_itt(conn, itt))
1126 task = iscsi_itt_to_task(conn, itt);
1127 if (!task || !task->sc)
1130 if (task->sc->SCp.phase != conn->session->age) {
1131 iscsi_session_printk(KERN_ERR, conn->session,
1132 "task's session age %d, expected %d\n",
1133 task->sc->SCp.phase, conn->session->age);
1139 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1141 void iscsi_session_failure(struct iscsi_session *session,
1144 struct iscsi_conn *conn;
1146 unsigned long flags;
1148 spin_lock_irqsave(&session->lock, flags);
1149 conn = session->leadconn;
1150 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1151 spin_unlock_irqrestore(&session->lock, flags);
1155 dev = get_device(&conn->cls_conn->dev);
1156 spin_unlock_irqrestore(&session->lock, flags);
1160 * if the host is being removed bypass the connection
1161 * recovery initialization because we are going to kill
1164 if (err == ISCSI_ERR_INVALID_HOST)
1165 iscsi_conn_error_event(conn->cls_conn, err);
1167 iscsi_conn_failure(conn, err);
1170 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1172 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1174 struct iscsi_session *session = conn->session;
1175 unsigned long flags;
1177 spin_lock_irqsave(&session->lock, flags);
1178 if (session->state == ISCSI_STATE_FAILED) {
1179 spin_unlock_irqrestore(&session->lock, flags);
1183 if (conn->stop_stage == 0)
1184 session->state = ISCSI_STATE_FAILED;
1185 spin_unlock_irqrestore(&session->lock, flags);
1187 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1188 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1189 iscsi_conn_error_event(conn->cls_conn, err);
1191 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1193 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1195 struct iscsi_session *session = conn->session;
1198 * Check for iSCSI window and take care of CmdSN wrap-around
1200 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1201 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1202 "%u MaxCmdSN %u CmdSN %u/%u\n",
1203 session->exp_cmdsn, session->max_cmdsn,
1204 session->cmdsn, session->queued_cmdsn);
1210 static int iscsi_xmit_task(struct iscsi_conn *conn)
1212 struct iscsi_task *task = conn->task;
1215 __iscsi_get_task(task);
1216 spin_unlock_bh(&conn->session->lock);
1217 rc = conn->session->tt->xmit_task(task);
1218 spin_lock_bh(&conn->session->lock);
1220 /* done with this task */
1221 task->last_xfer = jiffies;
1224 __iscsi_put_task(task);
1229 * iscsi_requeue_task - requeue task to run from session workqueue
1230 * @task: task to requeue
1232 * LLDs that need to run a task from the session workqueue should call
1233 * this. The session lock must be held. This should only be called
1234 * by software drivers.
1236 void iscsi_requeue_task(struct iscsi_task *task)
1238 struct iscsi_conn *conn = task->conn;
1241 * this may be on the requeue list already if the xmit_task callout
1242 * is handling the r2ts while we are adding new ones
1244 if (list_empty(&task->running))
1245 list_add_tail(&task->running, &conn->requeue);
1246 iscsi_conn_queue_work(conn);
1248 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1251 * iscsi_data_xmit - xmit any command into the scheduled connection
1252 * @conn: iscsi connection
1255 * The function can return -EAGAIN in which case the caller must
1256 * re-schedule it again later or recover. '0' return code means
1259 static int iscsi_data_xmit(struct iscsi_conn *conn)
1263 spin_lock_bh(&conn->session->lock);
1264 if (unlikely(conn->suspend_tx)) {
1265 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1266 spin_unlock_bh(&conn->session->lock);
1271 rc = iscsi_xmit_task(conn);
1277 * process mgmt pdus like nops before commands since we should
1278 * only have one nop-out as a ping from us and targets should not
1279 * overflow us with nop-ins
1282 while (!list_empty(&conn->mgmtqueue)) {
1283 conn->task = list_entry(conn->mgmtqueue.next,
1284 struct iscsi_task, running);
1285 list_del_init(&conn->task->running);
1286 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1287 __iscsi_put_task(conn->task);
1291 rc = iscsi_xmit_task(conn);
1296 /* process pending command queue */
1297 while (!list_empty(&conn->cmdqueue)) {
1298 if (conn->tmf_state == TMF_QUEUED)
1301 conn->task = list_entry(conn->cmdqueue.next,
1302 struct iscsi_task, running);
1303 list_del_init(&conn->task->running);
1304 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1305 fail_scsi_task(conn->task, DID_IMM_RETRY);
1308 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1310 if (rc == -ENOMEM) {
1311 list_add_tail(&conn->task->running,
1316 fail_scsi_task(conn->task, DID_ABORT);
1319 rc = iscsi_xmit_task(conn);
1323 * we could continuously get new task requests so
1324 * we need to check the mgmt queue for nops that need to
1325 * be sent to aviod starvation
1327 if (!list_empty(&conn->mgmtqueue))
1331 while (!list_empty(&conn->requeue)) {
1332 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1336 * we always do fastlogout - conn stop code will clean up.
1338 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1341 conn->task = list_entry(conn->requeue.next,
1342 struct iscsi_task, running);
1343 list_del_init(&conn->task->running);
1344 conn->task->state = ISCSI_TASK_RUNNING;
1345 rc = iscsi_xmit_task(conn);
1348 if (!list_empty(&conn->mgmtqueue))
1351 spin_unlock_bh(&conn->session->lock);
1355 if (unlikely(conn->suspend_tx))
1357 spin_unlock_bh(&conn->session->lock);
1361 static void iscsi_xmitworker(struct work_struct *work)
1363 struct iscsi_conn *conn =
1364 container_of(work, struct iscsi_conn, xmitwork);
1367 * serialize Xmit worker on a per-connection basis.
1370 rc = iscsi_data_xmit(conn);
1371 } while (rc >= 0 || rc == -EAGAIN);
1374 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1375 struct scsi_cmnd *sc)
1377 struct iscsi_task *task;
1379 if (!__kfifo_get(conn->session->cmdpool.queue,
1380 (void *) &task, sizeof(void *)))
1383 sc->SCp.phase = conn->session->age;
1384 sc->SCp.ptr = (char *) task;
1386 atomic_set(&task->refcount, 1);
1387 task->state = ISCSI_TASK_PENDING;
1390 task->have_checked_conn = false;
1391 task->last_timeout = jiffies;
1392 task->last_xfer = jiffies;
1393 INIT_LIST_HEAD(&task->running);
1398 FAILURE_BAD_HOST = 1,
1399 FAILURE_SESSION_FAILED,
1400 FAILURE_SESSION_FREED,
1401 FAILURE_WINDOW_CLOSED,
1403 FAILURE_SESSION_TERMINATE,
1404 FAILURE_SESSION_IN_RECOVERY,
1405 FAILURE_SESSION_RECOVERY_TIMEOUT,
1406 FAILURE_SESSION_LOGGING_OUT,
1407 FAILURE_SESSION_NOT_READY,
1410 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1412 struct iscsi_cls_session *cls_session;
1413 struct Scsi_Host *host;
1414 struct iscsi_host *ihost;
1416 struct iscsi_session *session;
1417 struct iscsi_conn *conn;
1418 struct iscsi_task *task = NULL;
1420 sc->scsi_done = done;
1424 host = sc->device->host;
1425 ihost = shost_priv(host);
1426 spin_unlock(host->host_lock);
1428 cls_session = starget_to_session(scsi_target(sc->device));
1429 session = cls_session->dd_data;
1430 spin_lock(&session->lock);
1432 reason = iscsi_session_chkready(cls_session);
1434 sc->result = reason;
1438 if (session->state != ISCSI_STATE_LOGGED_IN) {
1440 * to handle the race between when we set the recovery state
1441 * and block the session we requeue here (commands could
1442 * be entering our queuecommand while a block is starting
1443 * up because the block code is not locked)
1445 switch (session->state) {
1446 case ISCSI_STATE_FAILED:
1447 case ISCSI_STATE_IN_RECOVERY:
1448 reason = FAILURE_SESSION_IN_RECOVERY;
1449 sc->result = DID_IMM_RETRY << 16;
1451 case ISCSI_STATE_LOGGING_OUT:
1452 reason = FAILURE_SESSION_LOGGING_OUT;
1453 sc->result = DID_IMM_RETRY << 16;
1455 case ISCSI_STATE_RECOVERY_FAILED:
1456 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1457 sc->result = DID_TRANSPORT_FAILFAST << 16;
1459 case ISCSI_STATE_TERMINATE:
1460 reason = FAILURE_SESSION_TERMINATE;
1461 sc->result = DID_NO_CONNECT << 16;
1464 reason = FAILURE_SESSION_FREED;
1465 sc->result = DID_NO_CONNECT << 16;
1470 conn = session->leadconn;
1472 reason = FAILURE_SESSION_FREED;
1473 sc->result = DID_NO_CONNECT << 16;
1477 if (iscsi_check_cmdsn_window_closed(conn)) {
1478 reason = FAILURE_WINDOW_CLOSED;
1482 task = iscsi_alloc_task(conn, sc);
1484 reason = FAILURE_OOM;
1488 if (!ihost->workq) {
1489 reason = iscsi_prep_scsi_cmd_pdu(task);
1491 if (reason == -ENOMEM) {
1492 reason = FAILURE_OOM;
1495 sc->result = DID_ABORT << 16;
1499 if (session->tt->xmit_task(task)) {
1500 reason = FAILURE_SESSION_NOT_READY;
1504 list_add_tail(&task->running, &conn->cmdqueue);
1505 iscsi_conn_queue_work(conn);
1508 session->queued_cmdsn++;
1509 spin_unlock(&session->lock);
1510 spin_lock(host->host_lock);
1514 sc->scsi_done = NULL;
1515 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1517 spin_unlock(&session->lock);
1518 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1519 sc->cmnd[0], reason);
1520 spin_lock(host->host_lock);
1521 return SCSI_MLQUEUE_TARGET_BUSY;
1524 sc->scsi_done = NULL;
1525 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1527 spin_unlock(&session->lock);
1528 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1529 sc->cmnd[0], reason);
1530 if (!scsi_bidi_cmnd(sc))
1531 scsi_set_resid(sc, scsi_bufflen(sc));
1533 scsi_out(sc)->resid = scsi_out(sc)->length;
1534 scsi_in(sc)->resid = scsi_in(sc)->length;
1537 spin_lock(host->host_lock);
1540 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1542 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1544 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1545 return sdev->queue_depth;
1547 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1549 int iscsi_target_alloc(struct scsi_target *starget)
1551 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1552 struct iscsi_session *session = cls_session->dd_data;
1554 starget->can_queue = session->scsi_cmds_max;
1557 EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1559 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1561 struct iscsi_session *session = cls_session->dd_data;
1563 spin_lock_bh(&session->lock);
1564 if (session->state != ISCSI_STATE_LOGGED_IN) {
1565 session->state = ISCSI_STATE_RECOVERY_FAILED;
1566 if (session->leadconn)
1567 wake_up(&session->leadconn->ehwait);
1569 spin_unlock_bh(&session->lock);
1571 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1573 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
1575 struct iscsi_cls_session *cls_session;
1576 struct iscsi_session *session;
1577 struct iscsi_conn *conn;
1579 cls_session = starget_to_session(scsi_target(sc->device));
1580 session = cls_session->dd_data;
1581 conn = session->leadconn;
1583 mutex_lock(&session->eh_mutex);
1584 spin_lock_bh(&session->lock);
1585 if (session->state == ISCSI_STATE_TERMINATE) {
1587 ISCSI_DBG_EH(session,
1588 "failing target reset: Could not log back into "
1589 "target [age %d]\n",
1591 spin_unlock_bh(&session->lock);
1592 mutex_unlock(&session->eh_mutex);
1596 spin_unlock_bh(&session->lock);
1597 mutex_unlock(&session->eh_mutex);
1599 * we drop the lock here but the leadconn cannot be destoyed while
1600 * we are in the scsi eh
1602 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1604 ISCSI_DBG_EH(session, "wait for relogin\n");
1605 wait_event_interruptible(conn->ehwait,
1606 session->state == ISCSI_STATE_TERMINATE ||
1607 session->state == ISCSI_STATE_LOGGED_IN ||
1608 session->state == ISCSI_STATE_RECOVERY_FAILED);
1609 if (signal_pending(current))
1610 flush_signals(current);
1612 mutex_lock(&session->eh_mutex);
1613 spin_lock_bh(&session->lock);
1614 if (session->state == ISCSI_STATE_LOGGED_IN) {
1615 ISCSI_DBG_EH(session,
1616 "target reset succeeded\n");
1619 spin_unlock_bh(&session->lock);
1620 mutex_unlock(&session->eh_mutex);
1623 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
1625 static void iscsi_tmf_timedout(unsigned long data)
1627 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1628 struct iscsi_session *session = conn->session;
1630 spin_lock(&session->lock);
1631 if (conn->tmf_state == TMF_QUEUED) {
1632 conn->tmf_state = TMF_TIMEDOUT;
1633 ISCSI_DBG_EH(session, "tmf timedout\n");
1634 /* unblock eh_abort() */
1635 wake_up(&conn->ehwait);
1637 spin_unlock(&session->lock);
1640 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1641 struct iscsi_tm *hdr, int age,
1644 struct iscsi_session *session = conn->session;
1645 struct iscsi_task *task;
1647 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1650 spin_unlock_bh(&session->lock);
1651 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1652 spin_lock_bh(&session->lock);
1653 ISCSI_DBG_EH(session, "tmf exec failure\n");
1656 conn->tmfcmd_pdus_cnt++;
1657 conn->tmf_timer.expires = timeout * HZ + jiffies;
1658 conn->tmf_timer.function = iscsi_tmf_timedout;
1659 conn->tmf_timer.data = (unsigned long)conn;
1660 add_timer(&conn->tmf_timer);
1661 ISCSI_DBG_EH(session, "tmf set timeout\n");
1663 spin_unlock_bh(&session->lock);
1664 mutex_unlock(&session->eh_mutex);
1667 * block eh thread until:
1671 * 3) session is terminated or restarted or userspace has
1672 * given up on recovery
1674 wait_event_interruptible(conn->ehwait, age != session->age ||
1675 session->state != ISCSI_STATE_LOGGED_IN ||
1676 conn->tmf_state != TMF_QUEUED);
1677 if (signal_pending(current))
1678 flush_signals(current);
1679 del_timer_sync(&conn->tmf_timer);
1681 mutex_lock(&session->eh_mutex);
1682 spin_lock_bh(&session->lock);
1683 /* if the session drops it will clean up the task */
1684 if (age != session->age ||
1685 session->state != ISCSI_STATE_LOGGED_IN)
1691 * Fail commands. session lock held and recv side suspended and xmit
1694 static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1697 struct iscsi_task *task;
1700 for (i = 0; i < conn->session->cmds_max; i++) {
1701 task = conn->session->cmds[i];
1702 if (!task->sc || task->state == ISCSI_TASK_FREE)
1705 if (lun != -1 && lun != task->sc->device->lun)
1708 ISCSI_DBG_SESSION(conn->session,
1709 "failing sc %p itt 0x%x state %d\n",
1710 task->sc, task->itt, task->state);
1711 fail_scsi_task(task, error);
1715 void iscsi_suspend_tx(struct iscsi_conn *conn)
1717 struct Scsi_Host *shost = conn->session->host;
1718 struct iscsi_host *ihost = shost_priv(shost);
1720 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1722 flush_workqueue(ihost->workq);
1724 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1726 static void iscsi_start_tx(struct iscsi_conn *conn)
1728 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1729 iscsi_conn_queue_work(conn);
1733 * We want to make sure a ping is in flight. It has timed out.
1734 * And we are not busy processing a pdu that is making
1735 * progress but got started before the ping and is taking a while
1736 * to complete so the ping is just stuck behind it in a queue.
1738 static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1740 if (conn->ping_task &&
1741 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1742 (conn->ping_timeout * HZ), jiffies))
1748 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1750 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1751 struct iscsi_task *task = NULL;
1752 struct iscsi_cls_session *cls_session;
1753 struct iscsi_session *session;
1754 struct iscsi_conn *conn;
1756 cls_session = starget_to_session(scsi_target(sc->device));
1757 session = cls_session->dd_data;
1759 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1761 spin_lock(&session->lock);
1762 if (session->state != ISCSI_STATE_LOGGED_IN) {
1764 * We are probably in the middle of iscsi recovery so let
1765 * that complete and handle the error.
1767 rc = BLK_EH_RESET_TIMER;
1771 conn = session->leadconn;
1773 /* In the middle of shuting down */
1774 rc = BLK_EH_RESET_TIMER;
1778 task = (struct iscsi_task *)sc->SCp.ptr;
1782 * If we have sent (at least queued to the network layer) a pdu or
1783 * recvd one for the task since the last timeout ask for
1784 * more time. If on the next timeout we have not made progress
1785 * we can check if it is the task or connection when we send the
1788 if (time_after_eq(task->last_xfer, task->last_timeout)) {
1789 ISCSI_DBG_EH(session, "Command making progress. Asking "
1790 "scsi-ml for more time to complete. "
1791 "Last data recv at %lu. Last timeout was at "
1792 "%lu\n.", task->last_xfer, task->last_timeout);
1793 task->have_checked_conn = false;
1794 rc = BLK_EH_RESET_TIMER;
1798 if (!conn->recv_timeout && !conn->ping_timeout)
1801 * if the ping timedout then we are in the middle of cleaning up
1802 * and can let the iscsi eh handle it
1804 if (iscsi_has_ping_timed_out(conn)) {
1805 rc = BLK_EH_RESET_TIMER;
1809 /* Assumes nop timeout is shorter than scsi cmd timeout */
1810 if (task->have_checked_conn)
1814 * Checking the transport already or nop from a cmd timeout still
1817 if (conn->ping_task) {
1818 task->have_checked_conn = true;
1819 rc = BLK_EH_RESET_TIMER;
1823 /* Make sure there is a transport check done */
1824 iscsi_send_nopout(conn, NULL);
1825 task->have_checked_conn = true;
1826 rc = BLK_EH_RESET_TIMER;
1830 task->last_timeout = jiffies;
1831 spin_unlock(&session->lock);
1832 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1833 "timer reset" : "nh");
1837 static void iscsi_check_transport_timeouts(unsigned long data)
1839 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1840 struct iscsi_session *session = conn->session;
1841 unsigned long recv_timeout, next_timeout = 0, last_recv;
1843 spin_lock(&session->lock);
1844 if (session->state != ISCSI_STATE_LOGGED_IN)
1847 recv_timeout = conn->recv_timeout;
1852 last_recv = conn->last_recv;
1854 if (iscsi_has_ping_timed_out(conn)) {
1855 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1856 "expired, recv timeout %d, last rx %lu, "
1857 "last ping %lu, now %lu\n",
1858 conn->ping_timeout, conn->recv_timeout,
1859 last_recv, conn->last_ping, jiffies);
1860 spin_unlock(&session->lock);
1861 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1865 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1866 /* send a ping to try to provoke some traffic */
1867 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
1868 iscsi_send_nopout(conn, NULL);
1869 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1871 next_timeout = last_recv + recv_timeout;
1873 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
1874 mod_timer(&conn->transport_timer, next_timeout);
1876 spin_unlock(&session->lock);
1879 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
1880 struct iscsi_tm *hdr)
1882 memset(hdr, 0, sizeof(*hdr));
1883 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1884 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1885 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1886 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1887 hdr->rtt = task->hdr_itt;
1888 hdr->refcmdsn = task->cmdsn;
1891 int iscsi_eh_abort(struct scsi_cmnd *sc)
1893 struct iscsi_cls_session *cls_session;
1894 struct iscsi_session *session;
1895 struct iscsi_conn *conn;
1896 struct iscsi_task *task;
1897 struct iscsi_tm *hdr;
1900 cls_session = starget_to_session(scsi_target(sc->device));
1901 session = cls_session->dd_data;
1903 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
1905 mutex_lock(&session->eh_mutex);
1906 spin_lock_bh(&session->lock);
1908 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1912 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
1914 spin_unlock_bh(&session->lock);
1915 mutex_unlock(&session->eh_mutex);
1920 * If we are not logged in or we have started a new session
1921 * then let the host reset code handle this
1923 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1924 sc->SCp.phase != session->age) {
1925 spin_unlock_bh(&session->lock);
1926 mutex_unlock(&session->eh_mutex);
1927 ISCSI_DBG_EH(session, "failing abort due to dropped "
1932 conn = session->leadconn;
1933 conn->eh_abort_cnt++;
1936 task = (struct iscsi_task *)sc->SCp.ptr;
1937 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
1940 /* task completed before time out */
1942 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
1946 if (task->state == ISCSI_TASK_PENDING) {
1947 fail_scsi_task(task, DID_ABORT);
1951 /* only have one tmf outstanding at a time */
1952 if (conn->tmf_state != TMF_INITIAL)
1954 conn->tmf_state = TMF_QUEUED;
1957 iscsi_prep_abort_task_pdu(task, hdr);
1959 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1964 switch (conn->tmf_state) {
1966 spin_unlock_bh(&session->lock);
1968 * stop tx side incase the target had sent a abort rsp but
1969 * the initiator was still writing out data.
1971 iscsi_suspend_tx(conn);
1973 * we do not stop the recv side because targets have been
1974 * good and have never sent us a successful tmf response
1975 * then sent more data for the cmd.
1977 spin_lock(&session->lock);
1978 fail_scsi_task(task, DID_ABORT);
1979 conn->tmf_state = TMF_INITIAL;
1980 spin_unlock(&session->lock);
1981 iscsi_start_tx(conn);
1982 goto success_unlocked;
1984 spin_unlock_bh(&session->lock);
1985 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1986 goto failed_unlocked;
1989 conn->tmf_state = TMF_INITIAL;
1990 /* task completed before tmf abort response */
1991 ISCSI_DBG_EH(session, "sc completed while abort in "
1997 conn->tmf_state = TMF_INITIAL;
2002 spin_unlock_bh(&session->lock);
2004 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2006 mutex_unlock(&session->eh_mutex);
2010 spin_unlock_bh(&session->lock);
2012 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2013 task ? task->itt : 0);
2014 mutex_unlock(&session->eh_mutex);
2017 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2019 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2021 memset(hdr, 0, sizeof(*hdr));
2022 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2023 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2024 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2025 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
2026 hdr->rtt = RESERVED_ITT;
2029 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2031 struct iscsi_cls_session *cls_session;
2032 struct iscsi_session *session;
2033 struct iscsi_conn *conn;
2034 struct iscsi_tm *hdr;
2037 cls_session = starget_to_session(scsi_target(sc->device));
2038 session = cls_session->dd_data;
2040 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2042 mutex_lock(&session->eh_mutex);
2043 spin_lock_bh(&session->lock);
2045 * Just check if we are not logged in. We cannot check for
2046 * the phase because the reset could come from a ioctl.
2048 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2050 conn = session->leadconn;
2052 /* only have one tmf outstanding at a time */
2053 if (conn->tmf_state != TMF_INITIAL)
2055 conn->tmf_state = TMF_QUEUED;
2058 iscsi_prep_lun_reset_pdu(sc, hdr);
2060 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2061 session->lu_reset_timeout)) {
2066 switch (conn->tmf_state) {
2070 spin_unlock_bh(&session->lock);
2071 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2074 conn->tmf_state = TMF_INITIAL;
2079 spin_unlock_bh(&session->lock);
2081 iscsi_suspend_tx(conn);
2083 spin_lock_bh(&session->lock);
2084 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2085 conn->tmf_state = TMF_INITIAL;
2086 spin_unlock_bh(&session->lock);
2088 iscsi_start_tx(conn);
2092 spin_unlock_bh(&session->lock);
2094 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2095 rc == SUCCESS ? "SUCCESS" : "FAILED");
2096 mutex_unlock(&session->eh_mutex);
2099 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2102 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2103 * should be accessed via kfifo_{get,put} on q->queue.
2104 * Optionally, the caller can obtain the array of object pointers
2105 * by passing in a non-NULL @items pointer
2108 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2110 int i, num_arrays = 1;
2112 memset(q, 0, sizeof(*q));
2116 /* If the user passed an items pointer, he wants a copy of
2120 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2121 if (q->pool == NULL)
2124 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2126 if (IS_ERR(q->queue)) {
2131 for (i = 0; i < max; i++) {
2132 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2133 if (q->pool[i] == NULL) {
2137 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2141 *items = q->pool + max;
2142 memcpy(*items, q->pool, max * sizeof(void *));
2151 EXPORT_SYMBOL_GPL(iscsi_pool_init);
2153 void iscsi_pool_free(struct iscsi_pool *q)
2157 for (i = 0; i < q->max; i++)
2162 EXPORT_SYMBOL_GPL(iscsi_pool_free);
2165 * iscsi_host_add - add host to system
2167 * @pdev: parent device
2169 * This should be called by partial offload and software iscsi drivers
2170 * to add a host to the system.
2172 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2174 if (!shost->can_queue)
2175 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2177 if (!shost->cmd_per_lun)
2178 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2180 if (!shost->transportt->eh_timed_out)
2181 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2182 return scsi_add_host(shost, pdev);
2184 EXPORT_SYMBOL_GPL(iscsi_host_add);
2187 * iscsi_host_alloc - allocate a host and driver data
2188 * @sht: scsi host template
2189 * @dd_data_size: driver host data size
2190 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
2192 * This should be called by partial offload and software iscsi drivers.
2193 * To access the driver specific memory use the iscsi_host_priv() macro.
2195 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2196 int dd_data_size, bool xmit_can_sleep)
2198 struct Scsi_Host *shost;
2199 struct iscsi_host *ihost;
2201 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2204 ihost = shost_priv(shost);
2206 if (xmit_can_sleep) {
2207 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2208 "iscsi_q_%d", shost->host_no);
2209 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2214 spin_lock_init(&ihost->lock);
2215 ihost->state = ISCSI_HOST_SETUP;
2216 ihost->num_sessions = 0;
2217 init_waitqueue_head(&ihost->session_removal_wq);
2221 scsi_host_put(shost);
2224 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2226 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2228 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2232 * iscsi_host_remove - remove host and sessions
2235 * If there are any sessions left, this will initiate the removal and wait
2236 * for the completion.
2238 void iscsi_host_remove(struct Scsi_Host *shost)
2240 struct iscsi_host *ihost = shost_priv(shost);
2241 unsigned long flags;
2243 spin_lock_irqsave(&ihost->lock, flags);
2244 ihost->state = ISCSI_HOST_REMOVED;
2245 spin_unlock_irqrestore(&ihost->lock, flags);
2247 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2248 wait_event_interruptible(ihost->session_removal_wq,
2249 ihost->num_sessions == 0);
2250 if (signal_pending(current))
2251 flush_signals(current);
2253 scsi_remove_host(shost);
2255 destroy_workqueue(ihost->workq);
2257 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2259 void iscsi_host_free(struct Scsi_Host *shost)
2261 struct iscsi_host *ihost = shost_priv(shost);
2263 kfree(ihost->netdev);
2264 kfree(ihost->hwaddress);
2265 kfree(ihost->initiatorname);
2266 scsi_host_put(shost);
2268 EXPORT_SYMBOL_GPL(iscsi_host_free);
2270 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2272 struct iscsi_host *ihost = shost_priv(shost);
2273 unsigned long flags;
2275 shost = scsi_host_get(shost);
2277 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2278 "of session teardown event because host already "
2283 spin_lock_irqsave(&ihost->lock, flags);
2284 ihost->num_sessions--;
2285 if (ihost->num_sessions == 0)
2286 wake_up(&ihost->session_removal_wq);
2287 spin_unlock_irqrestore(&ihost->lock, flags);
2288 scsi_host_put(shost);
2292 * iscsi_session_setup - create iscsi cls session and host and session
2293 * @iscsit: iscsi transport template
2295 * @cmds_max: session can queue
2296 * @cmd_task_size: LLD task private data size
2297 * @initial_cmdsn: initial CmdSN
2299 * This can be used by software iscsi_transports that allocate
2300 * a session per scsi host.
2302 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2303 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2304 * for nop handling and login/logout requests.
2306 struct iscsi_cls_session *
2307 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2308 uint16_t cmds_max, int cmd_task_size,
2309 uint32_t initial_cmdsn, unsigned int id)
2311 struct iscsi_host *ihost = shost_priv(shost);
2312 struct iscsi_session *session;
2313 struct iscsi_cls_session *cls_session;
2314 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2315 unsigned long flags;
2317 spin_lock_irqsave(&ihost->lock, flags);
2318 if (ihost->state == ISCSI_HOST_REMOVED) {
2319 spin_unlock_irqrestore(&ihost->lock, flags);
2322 ihost->num_sessions++;
2323 spin_unlock_irqrestore(&ihost->lock, flags);
2326 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2328 * The iscsi layer needs some tasks for nop handling and tmfs,
2329 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2330 * + 1 command for scsi IO.
2332 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2333 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2334 "must be a power of two that is at least %d.\n",
2335 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2336 goto dec_session_count;
2339 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2340 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2341 "must be a power of 2 less than or equal to %d.\n",
2342 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2343 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2346 if (!is_power_of_2(total_cmds)) {
2347 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2348 "must be a power of 2.\n", total_cmds);
2349 total_cmds = rounddown_pow_of_two(total_cmds);
2350 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2352 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2355 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2357 cls_session = iscsi_alloc_session(shost, iscsit,
2358 sizeof(struct iscsi_session));
2360 goto dec_session_count;
2361 session = cls_session->dd_data;
2362 session->cls_session = cls_session;
2363 session->host = shost;
2364 session->state = ISCSI_STATE_FREE;
2365 session->fast_abort = 1;
2366 session->lu_reset_timeout = 15;
2367 session->abort_timeout = 10;
2368 session->scsi_cmds_max = scsi_cmds;
2369 session->cmds_max = total_cmds;
2370 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2371 session->exp_cmdsn = initial_cmdsn + 1;
2372 session->max_cmdsn = initial_cmdsn + 1;
2373 session->max_r2t = 1;
2374 session->tt = iscsit;
2375 mutex_init(&session->eh_mutex);
2376 spin_lock_init(&session->lock);
2378 /* initialize SCSI PDU commands pool */
2379 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2380 (void***)&session->cmds,
2381 cmd_task_size + sizeof(struct iscsi_task)))
2382 goto cmdpool_alloc_fail;
2384 /* pre-format cmds pool with ITT */
2385 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2386 struct iscsi_task *task = session->cmds[cmd_i];
2389 task->dd_data = &task[1];
2391 task->state = ISCSI_TASK_FREE;
2392 INIT_LIST_HEAD(&task->running);
2395 if (!try_module_get(iscsit->owner))
2396 goto module_get_fail;
2398 if (iscsi_add_session(cls_session, id))
2399 goto cls_session_fail;
2404 module_put(iscsit->owner);
2406 iscsi_pool_free(&session->cmdpool);
2408 iscsi_free_session(cls_session);
2410 iscsi_host_dec_session_cnt(shost);
2413 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2416 * iscsi_session_teardown - destroy session, host, and cls_session
2417 * @cls_session: iscsi session
2419 * The driver must have called iscsi_remove_session before
2422 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2424 struct iscsi_session *session = cls_session->dd_data;
2425 struct module *owner = cls_session->transport->owner;
2426 struct Scsi_Host *shost = session->host;
2428 iscsi_pool_free(&session->cmdpool);
2430 kfree(session->password);
2431 kfree(session->password_in);
2432 kfree(session->username);
2433 kfree(session->username_in);
2434 kfree(session->targetname);
2435 kfree(session->initiatorname);
2436 kfree(session->ifacename);
2438 iscsi_destroy_session(cls_session);
2439 iscsi_host_dec_session_cnt(shost);
2442 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2445 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2446 * @cls_session: iscsi_cls_session
2447 * @dd_size: private driver data size
2450 struct iscsi_cls_conn *
2451 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2454 struct iscsi_session *session = cls_session->dd_data;
2455 struct iscsi_conn *conn;
2456 struct iscsi_cls_conn *cls_conn;
2459 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2463 conn = cls_conn->dd_data;
2464 memset(conn, 0, sizeof(*conn) + dd_size);
2466 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2467 conn->session = session;
2468 conn->cls_conn = cls_conn;
2469 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2470 conn->id = conn_idx;
2471 conn->exp_statsn = 0;
2472 conn->tmf_state = TMF_INITIAL;
2474 init_timer(&conn->transport_timer);
2475 conn->transport_timer.data = (unsigned long)conn;
2476 conn->transport_timer.function = iscsi_check_transport_timeouts;
2478 INIT_LIST_HEAD(&conn->mgmtqueue);
2479 INIT_LIST_HEAD(&conn->cmdqueue);
2480 INIT_LIST_HEAD(&conn->requeue);
2481 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2483 /* allocate login_task used for the login/text sequences */
2484 spin_lock_bh(&session->lock);
2485 if (!__kfifo_get(session->cmdpool.queue,
2486 (void*)&conn->login_task,
2488 spin_unlock_bh(&session->lock);
2489 goto login_task_alloc_fail;
2491 spin_unlock_bh(&session->lock);
2493 data = (char *) __get_free_pages(GFP_KERNEL,
2494 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2496 goto login_task_data_alloc_fail;
2497 conn->login_task->data = conn->data = data;
2499 init_timer(&conn->tmf_timer);
2500 init_waitqueue_head(&conn->ehwait);
2504 login_task_data_alloc_fail:
2505 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2507 login_task_alloc_fail:
2508 iscsi_destroy_conn(cls_conn);
2511 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2514 * iscsi_conn_teardown - teardown iscsi connection
2515 * cls_conn: iscsi class connection
2517 * TODO: we may need to make this into a two step process
2518 * like scsi-mls remove + put host
2520 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2522 struct iscsi_conn *conn = cls_conn->dd_data;
2523 struct iscsi_session *session = conn->session;
2524 unsigned long flags;
2526 del_timer_sync(&conn->transport_timer);
2528 spin_lock_bh(&session->lock);
2529 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2530 if (session->leadconn == conn) {
2532 * leading connection? then give up on recovery.
2534 session->state = ISCSI_STATE_TERMINATE;
2535 wake_up(&conn->ehwait);
2537 spin_unlock_bh(&session->lock);
2540 * Block until all in-progress commands for this connection
2544 spin_lock_irqsave(session->host->host_lock, flags);
2545 if (!session->host->host_busy) { /* OK for ERL == 0 */
2546 spin_unlock_irqrestore(session->host->host_lock, flags);
2549 spin_unlock_irqrestore(session->host->host_lock, flags);
2550 msleep_interruptible(500);
2551 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2552 "host_busy %d host_failed %d\n",
2553 session->host->host_busy,
2554 session->host->host_failed);
2556 * force eh_abort() to unblock
2558 wake_up(&conn->ehwait);
2561 /* flush queued up work because we free the connection below */
2562 iscsi_suspend_tx(conn);
2564 spin_lock_bh(&session->lock);
2565 free_pages((unsigned long) conn->data,
2566 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2567 kfree(conn->persistent_address);
2568 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2570 if (session->leadconn == conn)
2571 session->leadconn = NULL;
2572 spin_unlock_bh(&session->lock);
2574 iscsi_destroy_conn(cls_conn);
2576 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2578 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2580 struct iscsi_conn *conn = cls_conn->dd_data;
2581 struct iscsi_session *session = conn->session;
2584 iscsi_conn_printk(KERN_ERR, conn,
2585 "can't start unbound connection\n");
2589 if ((session->imm_data_en || !session->initial_r2t_en) &&
2590 session->first_burst > session->max_burst) {
2591 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2592 "first_burst %d max_burst %d\n",
2593 session->first_burst, session->max_burst);
2597 if (conn->ping_timeout && !conn->recv_timeout) {
2598 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2599 "zero. Using 5 seconds\n.");
2600 conn->recv_timeout = 5;
2603 if (conn->recv_timeout && !conn->ping_timeout) {
2604 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2605 "zero. Using 5 seconds.\n");
2606 conn->ping_timeout = 5;
2609 spin_lock_bh(&session->lock);
2610 conn->c_stage = ISCSI_CONN_STARTED;
2611 session->state = ISCSI_STATE_LOGGED_IN;
2612 session->queued_cmdsn = session->cmdsn;
2614 conn->last_recv = jiffies;
2615 conn->last_ping = jiffies;
2616 if (conn->recv_timeout && conn->ping_timeout)
2617 mod_timer(&conn->transport_timer,
2618 jiffies + (conn->recv_timeout * HZ));
2620 switch(conn->stop_stage) {
2621 case STOP_CONN_RECOVER:
2623 * unblock eh_abort() if it is blocked. re-try all
2624 * commands after successful recovery
2626 conn->stop_stage = 0;
2627 conn->tmf_state = TMF_INITIAL;
2629 if (session->age == 16)
2632 case STOP_CONN_TERM:
2633 conn->stop_stage = 0;
2638 spin_unlock_bh(&session->lock);
2640 iscsi_unblock_session(session->cls_session);
2641 wake_up(&conn->ehwait);
2644 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2647 fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
2649 struct iscsi_task *task;
2652 for (i = 0; i < conn->session->cmds_max; i++) {
2653 task = conn->session->cmds[i];
2657 if (task->state == ISCSI_TASK_FREE)
2660 ISCSI_DBG_SESSION(conn->session,
2661 "failing mgmt itt 0x%x state %d\n",
2662 task->itt, task->state);
2663 state = ISCSI_TASK_ABRT_SESS_RECOV;
2664 if (task->state == ISCSI_TASK_PENDING)
2665 state = ISCSI_TASK_COMPLETED;
2666 iscsi_complete_task(task, state);
2671 static void iscsi_start_session_recovery(struct iscsi_session *session,
2672 struct iscsi_conn *conn, int flag)
2676 mutex_lock(&session->eh_mutex);
2677 spin_lock_bh(&session->lock);
2678 if (conn->stop_stage == STOP_CONN_TERM) {
2679 spin_unlock_bh(&session->lock);
2680 mutex_unlock(&session->eh_mutex);
2685 * When this is called for the in_login state, we only want to clean
2686 * up the login task and connection. We do not need to block and set
2687 * the recovery state again
2689 if (flag == STOP_CONN_TERM)
2690 session->state = ISCSI_STATE_TERMINATE;
2691 else if (conn->stop_stage != STOP_CONN_RECOVER)
2692 session->state = ISCSI_STATE_IN_RECOVERY;
2693 spin_unlock_bh(&session->lock);
2695 del_timer_sync(&conn->transport_timer);
2696 iscsi_suspend_tx(conn);
2698 spin_lock_bh(&session->lock);
2699 old_stop_stage = conn->stop_stage;
2700 conn->stop_stage = flag;
2701 conn->c_stage = ISCSI_CONN_STOPPED;
2702 spin_unlock_bh(&session->lock);
2705 * for connection level recovery we should not calculate
2706 * header digest. conn->hdr_size used for optimization
2707 * in hdr_extract() and will be re-negotiated at
2710 if (flag == STOP_CONN_RECOVER) {
2711 conn->hdrdgst_en = 0;
2712 conn->datadgst_en = 0;
2713 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2714 old_stop_stage != STOP_CONN_RECOVER) {
2715 ISCSI_DBG_SESSION(session, "blocking session\n");
2716 iscsi_block_session(session->cls_session);
2723 spin_lock_bh(&session->lock);
2724 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
2725 fail_mgmt_tasks(session, conn);
2726 spin_unlock_bh(&session->lock);
2727 mutex_unlock(&session->eh_mutex);
2730 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2732 struct iscsi_conn *conn = cls_conn->dd_data;
2733 struct iscsi_session *session = conn->session;
2736 case STOP_CONN_RECOVER:
2737 case STOP_CONN_TERM:
2738 iscsi_start_session_recovery(session, conn, flag);
2741 iscsi_conn_printk(KERN_ERR, conn,
2742 "invalid stop flag %d\n", flag);
2745 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2747 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2748 struct iscsi_cls_conn *cls_conn, int is_leading)
2750 struct iscsi_session *session = cls_session->dd_data;
2751 struct iscsi_conn *conn = cls_conn->dd_data;
2753 spin_lock_bh(&session->lock);
2755 session->leadconn = conn;
2756 spin_unlock_bh(&session->lock);
2759 * Unblock xmitworker(), Login Phase will pass through.
2761 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2762 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2765 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2767 static int iscsi_switch_str_param(char **param, char *new_val_buf)
2772 if (!strcmp(*param, new_val_buf))
2776 new_val = kstrdup(new_val_buf, GFP_NOIO);
2785 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2786 enum iscsi_param param, char *buf, int buflen)
2788 struct iscsi_conn *conn = cls_conn->dd_data;
2789 struct iscsi_session *session = conn->session;
2793 case ISCSI_PARAM_FAST_ABORT:
2794 sscanf(buf, "%d", &session->fast_abort);
2796 case ISCSI_PARAM_ABORT_TMO:
2797 sscanf(buf, "%d", &session->abort_timeout);
2799 case ISCSI_PARAM_LU_RESET_TMO:
2800 sscanf(buf, "%d", &session->lu_reset_timeout);
2802 case ISCSI_PARAM_PING_TMO:
2803 sscanf(buf, "%d", &conn->ping_timeout);
2805 case ISCSI_PARAM_RECV_TMO:
2806 sscanf(buf, "%d", &conn->recv_timeout);
2808 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2809 sscanf(buf, "%d", &conn->max_recv_dlength);
2811 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2812 sscanf(buf, "%d", &conn->max_xmit_dlength);
2814 case ISCSI_PARAM_HDRDGST_EN:
2815 sscanf(buf, "%d", &conn->hdrdgst_en);
2817 case ISCSI_PARAM_DATADGST_EN:
2818 sscanf(buf, "%d", &conn->datadgst_en);
2820 case ISCSI_PARAM_INITIAL_R2T_EN:
2821 sscanf(buf, "%d", &session->initial_r2t_en);
2823 case ISCSI_PARAM_MAX_R2T:
2824 sscanf(buf, "%d", &session->max_r2t);
2826 case ISCSI_PARAM_IMM_DATA_EN:
2827 sscanf(buf, "%d", &session->imm_data_en);
2829 case ISCSI_PARAM_FIRST_BURST:
2830 sscanf(buf, "%d", &session->first_burst);
2832 case ISCSI_PARAM_MAX_BURST:
2833 sscanf(buf, "%d", &session->max_burst);
2835 case ISCSI_PARAM_PDU_INORDER_EN:
2836 sscanf(buf, "%d", &session->pdu_inorder_en);
2838 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2839 sscanf(buf, "%d", &session->dataseq_inorder_en);
2841 case ISCSI_PARAM_ERL:
2842 sscanf(buf, "%d", &session->erl);
2844 case ISCSI_PARAM_IFMARKER_EN:
2845 sscanf(buf, "%d", &value);
2848 case ISCSI_PARAM_OFMARKER_EN:
2849 sscanf(buf, "%d", &value);
2852 case ISCSI_PARAM_EXP_STATSN:
2853 sscanf(buf, "%u", &conn->exp_statsn);
2855 case ISCSI_PARAM_USERNAME:
2856 return iscsi_switch_str_param(&session->username, buf);
2857 case ISCSI_PARAM_USERNAME_IN:
2858 return iscsi_switch_str_param(&session->username_in, buf);
2859 case ISCSI_PARAM_PASSWORD:
2860 return iscsi_switch_str_param(&session->password, buf);
2861 case ISCSI_PARAM_PASSWORD_IN:
2862 return iscsi_switch_str_param(&session->password_in, buf);
2863 case ISCSI_PARAM_TARGET_NAME:
2864 return iscsi_switch_str_param(&session->targetname, buf);
2865 case ISCSI_PARAM_TPGT:
2866 sscanf(buf, "%d", &session->tpgt);
2868 case ISCSI_PARAM_PERSISTENT_PORT:
2869 sscanf(buf, "%d", &conn->persistent_port);
2871 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2872 return iscsi_switch_str_param(&conn->persistent_address, buf);
2873 case ISCSI_PARAM_IFACE_NAME:
2874 return iscsi_switch_str_param(&session->ifacename, buf);
2875 case ISCSI_PARAM_INITIATOR_NAME:
2876 return iscsi_switch_str_param(&session->initiatorname, buf);
2883 EXPORT_SYMBOL_GPL(iscsi_set_param);
2885 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2886 enum iscsi_param param, char *buf)
2888 struct iscsi_session *session = cls_session->dd_data;
2892 case ISCSI_PARAM_FAST_ABORT:
2893 len = sprintf(buf, "%d\n", session->fast_abort);
2895 case ISCSI_PARAM_ABORT_TMO:
2896 len = sprintf(buf, "%d\n", session->abort_timeout);
2898 case ISCSI_PARAM_LU_RESET_TMO:
2899 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2901 case ISCSI_PARAM_INITIAL_R2T_EN:
2902 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2904 case ISCSI_PARAM_MAX_R2T:
2905 len = sprintf(buf, "%hu\n", session->max_r2t);
2907 case ISCSI_PARAM_IMM_DATA_EN:
2908 len = sprintf(buf, "%d\n", session->imm_data_en);
2910 case ISCSI_PARAM_FIRST_BURST:
2911 len = sprintf(buf, "%u\n", session->first_burst);
2913 case ISCSI_PARAM_MAX_BURST:
2914 len = sprintf(buf, "%u\n", session->max_burst);
2916 case ISCSI_PARAM_PDU_INORDER_EN:
2917 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2919 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2920 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2922 case ISCSI_PARAM_ERL:
2923 len = sprintf(buf, "%d\n", session->erl);
2925 case ISCSI_PARAM_TARGET_NAME:
2926 len = sprintf(buf, "%s\n", session->targetname);
2928 case ISCSI_PARAM_TPGT:
2929 len = sprintf(buf, "%d\n", session->tpgt);
2931 case ISCSI_PARAM_USERNAME:
2932 len = sprintf(buf, "%s\n", session->username);
2934 case ISCSI_PARAM_USERNAME_IN:
2935 len = sprintf(buf, "%s\n", session->username_in);
2937 case ISCSI_PARAM_PASSWORD:
2938 len = sprintf(buf, "%s\n", session->password);
2940 case ISCSI_PARAM_PASSWORD_IN:
2941 len = sprintf(buf, "%s\n", session->password_in);
2943 case ISCSI_PARAM_IFACE_NAME:
2944 len = sprintf(buf, "%s\n", session->ifacename);
2946 case ISCSI_PARAM_INITIATOR_NAME:
2947 len = sprintf(buf, "%s\n", session->initiatorname);
2955 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2957 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2958 enum iscsi_param param, char *buf)
2960 struct iscsi_conn *conn = cls_conn->dd_data;
2964 case ISCSI_PARAM_PING_TMO:
2965 len = sprintf(buf, "%u\n", conn->ping_timeout);
2967 case ISCSI_PARAM_RECV_TMO:
2968 len = sprintf(buf, "%u\n", conn->recv_timeout);
2970 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2971 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2973 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2974 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2976 case ISCSI_PARAM_HDRDGST_EN:
2977 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2979 case ISCSI_PARAM_DATADGST_EN:
2980 len = sprintf(buf, "%d\n", conn->datadgst_en);
2982 case ISCSI_PARAM_IFMARKER_EN:
2983 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2985 case ISCSI_PARAM_OFMARKER_EN:
2986 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2988 case ISCSI_PARAM_EXP_STATSN:
2989 len = sprintf(buf, "%u\n", conn->exp_statsn);
2991 case ISCSI_PARAM_PERSISTENT_PORT:
2992 len = sprintf(buf, "%d\n", conn->persistent_port);
2994 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2995 len = sprintf(buf, "%s\n", conn->persistent_address);
3003 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3005 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3008 struct iscsi_host *ihost = shost_priv(shost);
3012 case ISCSI_HOST_PARAM_NETDEV_NAME:
3013 len = sprintf(buf, "%s\n", ihost->netdev);
3015 case ISCSI_HOST_PARAM_HWADDRESS:
3016 len = sprintf(buf, "%s\n", ihost->hwaddress);
3018 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3019 len = sprintf(buf, "%s\n", ihost->initiatorname);
3021 case ISCSI_HOST_PARAM_IPADDRESS:
3022 len = sprintf(buf, "%s\n", ihost->local_address);
3030 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3032 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3033 char *buf, int buflen)
3035 struct iscsi_host *ihost = shost_priv(shost);
3038 case ISCSI_HOST_PARAM_NETDEV_NAME:
3039 return iscsi_switch_str_param(&ihost->netdev, buf);
3040 case ISCSI_HOST_PARAM_HWADDRESS:
3041 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3042 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3043 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3050 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3052 MODULE_AUTHOR("Mike Christie");
3053 MODULE_DESCRIPTION("iSCSI library functions");
3054 MODULE_LICENSE("GPL");