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 <asm/unaligned.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_eh.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi.h>
35 #include <scsi/iscsi_proto.h>
36 #include <scsi/scsi_transport.h>
37 #include <scsi/scsi_transport_iscsi.h>
38 #include <scsi/libiscsi.h>
40 struct iscsi_session *
41 class_to_transport_session(struct iscsi_cls_session *cls_session)
43 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 return iscsi_hostdata(shost->hostdata);
46 EXPORT_SYMBOL_GPL(class_to_transport_session);
48 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49 #define SNA32_CHECK 2147483648UL
51 static int iscsi_sna_lt(u32 n1, u32 n2)
53 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
57 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
58 static int iscsi_sna_lte(u32 n1, u32 n2)
60 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
61 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
65 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
67 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
68 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
71 * standard specifies this check for when to update expected and
72 * max sequence numbers
74 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
77 if (exp_cmdsn != session->exp_cmdsn &&
78 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
79 session->exp_cmdsn = exp_cmdsn;
81 if (max_cmdsn != session->max_cmdsn &&
82 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
83 session->max_cmdsn = max_cmdsn;
85 * if the window closed with IO queued, then kick the
88 if (!list_empty(&session->leadconn->xmitqueue) ||
89 !list_empty(&session->leadconn->mgmtqueue))
90 scsi_queue_work(session->host,
91 &session->leadconn->xmitwork);
94 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
96 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
97 struct iscsi_data *hdr)
99 struct iscsi_conn *conn = ctask->conn;
101 memset(hdr, 0, sizeof(struct iscsi_data));
102 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
103 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
104 ctask->unsol_datasn++;
105 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
106 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
108 hdr->itt = ctask->hdr->itt;
109 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
110 hdr->offset = cpu_to_be32(ctask->unsol_offset);
112 if (ctask->unsol_count > conn->max_xmit_dlength) {
113 hton24(hdr->dlength, conn->max_xmit_dlength);
114 ctask->data_count = conn->max_xmit_dlength;
115 ctask->unsol_offset += ctask->data_count;
118 hton24(hdr->dlength, ctask->unsol_count);
119 ctask->data_count = ctask->unsol_count;
120 hdr->flags = ISCSI_FLAG_CMD_FINAL;
123 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
125 static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
127 unsigned exp_len = ctask->hdr_len + len;
129 if (exp_len > ctask->hdr_max) {
134 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
135 ctask->hdr_len = exp_len;
140 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
141 * @ctask: iscsi cmd task
143 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
144 * fields like dlength or final based on how much data it sends
146 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
148 struct iscsi_conn *conn = ctask->conn;
149 struct iscsi_session *session = conn->session;
150 struct iscsi_cmd *hdr = ctask->hdr;
151 struct scsi_cmnd *sc = ctask->sc;
156 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
159 hdr->opcode = ISCSI_OP_SCSI_CMD;
160 hdr->flags = ISCSI_ATTR_SIMPLE;
161 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
162 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
163 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
164 hdr->cmdsn = cpu_to_be32(session->cmdsn);
166 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
167 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
168 if (sc->cmd_len < MAX_COMMAND_SIZE)
169 memset(&hdr->cdb[sc->cmd_len], 0,
170 MAX_COMMAND_SIZE - sc->cmd_len);
172 ctask->data_count = 0;
173 ctask->imm_count = 0;
174 if (sc->sc_data_direction == DMA_TO_DEVICE) {
175 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
179 * imm_count bytes to be sent right after
182 * unsol_count bytes(as Data-Out) to be sent
183 * without R2T ack right after
186 * r2t_data_count bytes to be sent via R2T ack's
188 * pad_count bytes to be sent as zero-padding
190 ctask->unsol_count = 0;
191 ctask->unsol_offset = 0;
192 ctask->unsol_datasn = 0;
194 if (session->imm_data_en) {
195 if (scsi_bufflen(sc) >= session->first_burst)
196 ctask->imm_count = min(session->first_burst,
197 conn->max_xmit_dlength);
199 ctask->imm_count = min(scsi_bufflen(sc),
200 conn->max_xmit_dlength);
201 hton24(ctask->hdr->dlength, ctask->imm_count);
203 zero_data(ctask->hdr->dlength);
205 if (!session->initial_r2t_en) {
206 ctask->unsol_count = min((session->first_burst),
207 (scsi_bufflen(sc))) - ctask->imm_count;
208 ctask->unsol_offset = ctask->imm_count;
211 if (!ctask->unsol_count)
212 /* No unsolicit Data-Out's */
213 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
215 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
216 zero_data(hdr->dlength);
218 if (sc->sc_data_direction == DMA_FROM_DEVICE)
219 hdr->flags |= ISCSI_FLAG_CMD_READ;
222 /* calculate size of additional header segments (AHSs) */
223 hdrlength = ctask->hdr_len - sizeof(*hdr);
225 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
226 hdrlength /= ISCSI_PAD_LEN;
228 WARN_ON(hdrlength >= 256);
229 hdr->hlength = hdrlength & 0xFF;
231 conn->scsicmd_pdus_cnt++;
233 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
234 "cmdsn %d win %d]\n",
235 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
236 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
237 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
242 * iscsi_complete_command - return command back to scsi-ml
243 * @ctask: iscsi cmd task
245 * Must be called with session lock.
246 * This function returns the scsi command to scsi-ml and returns
247 * the cmd task to the pool of available cmd tasks.
249 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
251 struct iscsi_session *session = ctask->conn->session;
252 struct scsi_cmnd *sc = ctask->sc;
254 ctask->state = ISCSI_TASK_COMPLETED;
256 /* SCSI eh reuses commands to verify us */
258 list_del_init(&ctask->running);
259 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
263 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
265 atomic_inc(&ctask->refcount);
268 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
270 if (atomic_dec_and_test(&ctask->refcount))
271 iscsi_complete_command(ctask);
275 * session lock must be held
277 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
280 struct scsi_cmnd *sc;
286 if (ctask->state == ISCSI_TASK_PENDING)
288 * cmd never made it to the xmit thread, so we should not count
289 * the cmd in the sequencing
291 conn->session->queued_cmdsn--;
293 conn->session->tt->cleanup_cmd_task(conn, ctask);
296 scsi_set_resid(sc, scsi_bufflen(sc));
297 if (conn->ctask == ctask)
299 /* release ref from queuecommand */
300 __iscsi_put_ctask(ctask);
304 * iscsi_free_mgmt_task - return mgmt task back to pool
305 * @conn: iscsi connection
308 * Must be called with session lock.
310 void iscsi_free_mgmt_task(struct iscsi_conn *conn,
311 struct iscsi_mgmt_task *mtask)
313 list_del_init(&mtask->running);
314 if (conn->login_mtask == mtask)
316 __kfifo_put(conn->session->mgmtpool.queue,
317 (void*)&mtask, sizeof(void*));
319 EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
322 * iscsi_cmd_rsp - SCSI Command Response processing
323 * @conn: iscsi connection
325 * @ctask: scsi command task
326 * @data: cmd data buffer
327 * @datalen: len of buffer
329 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
330 * then completes the command and task.
332 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
333 struct iscsi_cmd_task *ctask, char *data,
336 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
337 struct iscsi_session *session = conn->session;
338 struct scsi_cmnd *sc = ctask->sc;
340 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
341 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
343 sc->result = (DID_OK << 16) | rhdr->cmd_status;
345 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
346 sc->result = DID_ERROR << 16;
350 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
355 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
356 "invalid data buffer size of %d\n", datalen);
357 sc->result = DID_BAD_TARGET << 16;
361 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
362 if (datalen < senselen)
363 goto invalid_datalen;
365 memcpy(sc->sense_buffer, data + 2,
366 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
367 debug_scsi("copied %d bytes of sense\n",
368 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
371 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
372 ISCSI_FLAG_CMD_OVERFLOW)) {
373 int res_count = be32_to_cpu(rhdr->residual_count);
376 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
377 res_count <= scsi_bufflen(sc)))
378 scsi_set_resid(sc, res_count);
380 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
381 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
382 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
383 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
386 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
387 (long)sc, sc->result, ctask->itt);
388 conn->scsirsp_pdus_cnt++;
390 __iscsi_put_ctask(ctask);
393 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
395 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
397 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
398 conn->tmfrsp_pdus_cnt++;
400 if (conn->tmf_state != TMF_QUEUED)
403 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
404 conn->tmf_state = TMF_SUCCESS;
405 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
406 conn->tmf_state = TMF_NOT_FOUND;
408 conn->tmf_state = TMF_FAILED;
409 wake_up(&conn->ehwait);
412 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
413 char *data, int datalen)
415 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
416 struct iscsi_hdr rejected_pdu;
419 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
421 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
422 if (ntoh24(reject->dlength) > datalen)
423 return ISCSI_ERR_PROTO;
425 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
426 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
427 itt = get_itt(rejected_pdu.itt);
428 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
429 "due to DataDigest error.\n", itt,
430 rejected_pdu.opcode);
437 * __iscsi_complete_pdu - complete pdu
441 * @datalen: len of data buffer
443 * Completes pdu processing by freeing any resources allocated at
444 * queuecommand or send generic. session lock must be held and verify
445 * itt must have been called.
447 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
448 char *data, int datalen)
450 struct iscsi_session *session = conn->session;
451 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
452 struct iscsi_cmd_task *ctask;
453 struct iscsi_mgmt_task *mtask;
456 if (hdr->itt != RESERVED_ITT)
457 itt = get_itt(hdr->itt);
461 if (itt < session->cmds_max) {
462 ctask = session->cmds[itt];
464 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
465 opcode, conn->id, ctask->itt, datalen);
468 case ISCSI_OP_SCSI_CMD_RSP:
469 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
470 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
473 case ISCSI_OP_SCSI_DATA_IN:
474 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
475 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
476 conn->scsirsp_pdus_cnt++;
477 __iscsi_put_ctask(ctask);
481 /* LLD handles this for now */
484 rc = ISCSI_ERR_BAD_OPCODE;
487 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
488 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
489 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
491 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
492 opcode, conn->id, mtask->itt, datalen);
494 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
496 case ISCSI_OP_LOGOUT_RSP:
498 rc = ISCSI_ERR_PROTO;
501 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
503 case ISCSI_OP_LOGIN_RSP:
504 case ISCSI_OP_TEXT_RSP:
506 * login related PDU's exp_statsn is handled in
509 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
510 rc = ISCSI_ERR_CONN_FAILED;
511 iscsi_free_mgmt_task(conn, mtask);
513 case ISCSI_OP_SCSI_TMFUNC_RSP:
515 rc = ISCSI_ERR_PROTO;
519 iscsi_tmf_rsp(conn, hdr);
520 iscsi_free_mgmt_task(conn, mtask);
522 case ISCSI_OP_NOOP_IN:
523 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
524 rc = ISCSI_ERR_PROTO;
527 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
529 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
530 rc = ISCSI_ERR_CONN_FAILED;
531 iscsi_free_mgmt_task(conn, mtask);
534 rc = ISCSI_ERR_BAD_OPCODE;
537 } else if (itt == ~0U) {
538 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
541 case ISCSI_OP_NOOP_IN:
543 rc = ISCSI_ERR_PROTO;
547 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
550 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
551 rc = ISCSI_ERR_CONN_FAILED;
553 case ISCSI_OP_REJECT:
554 rc = iscsi_handle_reject(conn, hdr, data, datalen);
556 case ISCSI_OP_ASYNC_EVENT:
557 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
558 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
559 rc = ISCSI_ERR_CONN_FAILED;
562 rc = ISCSI_ERR_BAD_OPCODE;
566 rc = ISCSI_ERR_BAD_ITT;
570 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
572 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
573 char *data, int datalen)
577 spin_lock(&conn->session->lock);
578 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
579 spin_unlock(&conn->session->lock);
582 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
584 /* verify itt (itt encoding: age+cid+itt) */
585 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
588 struct iscsi_session *session = conn->session;
589 struct iscsi_cmd_task *ctask;
592 if (hdr->itt != RESERVED_ITT) {
593 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
594 (session->age << ISCSI_AGE_SHIFT)) {
595 printk(KERN_ERR "iscsi: received itt %x expected "
596 "session age (%x)\n", (__force u32)hdr->itt,
597 session->age & ISCSI_AGE_MASK);
598 return ISCSI_ERR_BAD_ITT;
601 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
602 (conn->id << ISCSI_CID_SHIFT)) {
603 printk(KERN_ERR "iscsi: received itt %x, expected "
604 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
605 return ISCSI_ERR_BAD_ITT;
607 itt = get_itt(hdr->itt);
611 if (itt < session->cmds_max) {
612 ctask = session->cmds[itt];
615 printk(KERN_INFO "iscsi: dropping ctask with "
616 "itt 0x%x\n", ctask->itt);
618 return ISCSI_ERR_NO_SCSI_CMD;
621 if (ctask->sc->SCp.phase != session->age) {
622 printk(KERN_ERR "iscsi: ctask's session age %d, "
623 "expected %d\n", ctask->sc->SCp.phase,
625 return ISCSI_ERR_SESSION_FAILED;
632 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
634 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
636 struct iscsi_session *session = conn->session;
639 spin_lock_irqsave(&session->lock, flags);
640 if (session->state == ISCSI_STATE_FAILED) {
641 spin_unlock_irqrestore(&session->lock, flags);
645 if (conn->stop_stage == 0)
646 session->state = ISCSI_STATE_FAILED;
647 spin_unlock_irqrestore(&session->lock, flags);
648 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
649 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
650 iscsi_conn_error(conn->cls_conn, err);
652 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
654 static void iscsi_prep_mtask(struct iscsi_conn *conn,
655 struct iscsi_mgmt_task *mtask)
657 struct iscsi_session *session = conn->session;
658 struct iscsi_hdr *hdr = mtask->hdr;
659 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
661 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
662 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
663 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
665 * pre-format CmdSN for outgoing PDU.
667 nop->cmdsn = cpu_to_be32(session->cmdsn);
668 if (hdr->itt != RESERVED_ITT) {
669 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
671 * TODO: We always use immediate, so we never hit this.
672 * If we start to send tmfs or nops as non-immediate then
673 * we should start checking the cmdsn numbers for mgmt tasks.
675 if (conn->c_stage == ISCSI_CONN_STARTED &&
676 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
677 session->queued_cmdsn++;
682 if (session->tt->init_mgmt_task)
683 session->tt->init_mgmt_task(conn, mtask);
685 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
686 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
690 static int iscsi_xmit_mtask(struct iscsi_conn *conn)
692 struct iscsi_hdr *hdr = conn->mtask->hdr;
695 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
696 conn->session->state = ISCSI_STATE_LOGGING_OUT;
697 spin_unlock_bh(&conn->session->lock);
699 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
700 spin_lock_bh(&conn->session->lock);
704 /* done with this in-progress mtask */
709 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
711 struct iscsi_session *session = conn->session;
714 * Check for iSCSI window and take care of CmdSN wrap-around
716 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
717 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
718 "CmdSN %u/%u\n", session->exp_cmdsn,
719 session->max_cmdsn, session->cmdsn,
720 session->queued_cmdsn);
726 static int iscsi_xmit_ctask(struct iscsi_conn *conn)
728 struct iscsi_cmd_task *ctask = conn->ctask;
731 __iscsi_get_ctask(ctask);
732 spin_unlock_bh(&conn->session->lock);
733 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
734 spin_lock_bh(&conn->session->lock);
735 __iscsi_put_ctask(ctask);
737 /* done with this ctask */
743 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
744 * @ctask: ctask to requeue
746 * LLDs that need to run a ctask from the session workqueue should call
747 * this. The session lock must be held.
749 void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
751 struct iscsi_conn *conn = ctask->conn;
753 list_move_tail(&ctask->running, &conn->requeue);
754 scsi_queue_work(conn->session->host, &conn->xmitwork);
756 EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
759 * iscsi_data_xmit - xmit any command into the scheduled connection
760 * @conn: iscsi connection
763 * The function can return -EAGAIN in which case the caller must
764 * re-schedule it again later or recover. '0' return code means
767 static int iscsi_data_xmit(struct iscsi_conn *conn)
771 spin_lock_bh(&conn->session->lock);
772 if (unlikely(conn->suspend_tx)) {
773 debug_scsi("conn %d Tx suspended!\n", conn->id);
774 spin_unlock_bh(&conn->session->lock);
779 rc = iscsi_xmit_ctask(conn);
785 rc = iscsi_xmit_mtask(conn);
791 * process mgmt pdus like nops before commands since we should
792 * only have one nop-out as a ping from us and targets should not
793 * overflow us with nop-ins
796 while (!list_empty(&conn->mgmtqueue)) {
797 conn->mtask = list_entry(conn->mgmtqueue.next,
798 struct iscsi_mgmt_task, running);
799 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
800 iscsi_free_mgmt_task(conn, conn->mtask);
805 iscsi_prep_mtask(conn, conn->mtask);
806 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
807 rc = iscsi_xmit_mtask(conn);
812 /* process pending command queue */
813 while (!list_empty(&conn->xmitqueue)) {
814 if (conn->tmf_state == TMF_QUEUED)
817 conn->ctask = list_entry(conn->xmitqueue.next,
818 struct iscsi_cmd_task, running);
819 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
820 fail_command(conn, conn->ctask, DID_NO_CONNECT << 16);
823 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
824 fail_command(conn, conn->ctask, DID_ABORT << 16);
827 conn->session->tt->init_cmd_task(conn->ctask);
828 conn->ctask->state = ISCSI_TASK_RUNNING;
829 list_move_tail(conn->xmitqueue.next, &conn->run_list);
830 rc = iscsi_xmit_ctask(conn);
834 * we could continuously get new ctask requests so
835 * we need to check the mgmt queue for nops that need to
836 * be sent to aviod starvation
838 if (!list_empty(&conn->mgmtqueue))
842 while (!list_empty(&conn->requeue)) {
843 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
847 * we always do fastlogout - conn stop code will clean up.
849 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
852 conn->ctask = list_entry(conn->requeue.next,
853 struct iscsi_cmd_task, running);
854 conn->ctask->state = ISCSI_TASK_RUNNING;
855 list_move_tail(conn->requeue.next, &conn->run_list);
856 rc = iscsi_xmit_ctask(conn);
859 if (!list_empty(&conn->mgmtqueue))
862 spin_unlock_bh(&conn->session->lock);
866 if (unlikely(conn->suspend_tx))
868 spin_unlock_bh(&conn->session->lock);
872 static void iscsi_xmitworker(struct work_struct *work)
874 struct iscsi_conn *conn =
875 container_of(work, struct iscsi_conn, xmitwork);
878 * serialize Xmit worker on a per-connection basis.
881 rc = iscsi_data_xmit(conn);
882 } while (rc >= 0 || rc == -EAGAIN);
886 FAILURE_BAD_HOST = 1,
887 FAILURE_SESSION_FAILED,
888 FAILURE_SESSION_FREED,
889 FAILURE_WINDOW_CLOSED,
891 FAILURE_SESSION_TERMINATE,
892 FAILURE_SESSION_IN_RECOVERY,
893 FAILURE_SESSION_RECOVERY_TIMEOUT,
894 FAILURE_SESSION_LOGGING_OUT,
897 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
899 struct Scsi_Host *host;
901 struct iscsi_session *session;
902 struct iscsi_conn *conn;
903 struct iscsi_cmd_task *ctask = NULL;
905 sc->scsi_done = done;
909 host = sc->device->host;
910 session = iscsi_hostdata(host->hostdata);
912 spin_lock(&session->lock);
915 * ISCSI_STATE_FAILED is a temp. state. The recovery
916 * code will decide what is best to do with command queued
919 if (session->state != ISCSI_STATE_LOGGED_IN &&
920 session->state != ISCSI_STATE_FAILED) {
922 * to handle the race between when we set the recovery state
923 * and block the session we requeue here (commands could
924 * be entering our queuecommand while a block is starting
925 * up because the block code is not locked)
927 if (session->state == ISCSI_STATE_IN_RECOVERY) {
928 reason = FAILURE_SESSION_IN_RECOVERY;
932 switch (session->state) {
933 case ISCSI_STATE_RECOVERY_FAILED:
934 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
936 case ISCSI_STATE_TERMINATE:
937 reason = FAILURE_SESSION_TERMINATE;
939 case ISCSI_STATE_LOGGING_OUT:
940 reason = FAILURE_SESSION_LOGGING_OUT;
943 reason = FAILURE_SESSION_FREED;
948 conn = session->leadconn;
950 reason = FAILURE_SESSION_FREED;
954 if (iscsi_check_cmdsn_window_closed(conn)) {
955 reason = FAILURE_WINDOW_CLOSED;
959 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
961 reason = FAILURE_OOM;
964 session->queued_cmdsn++;
966 sc->SCp.phase = session->age;
967 sc->SCp.ptr = (char *)ctask;
969 atomic_set(&ctask->refcount, 1);
970 ctask->state = ISCSI_TASK_PENDING;
973 INIT_LIST_HEAD(&ctask->running);
975 list_add_tail(&ctask->running, &conn->xmitqueue);
976 spin_unlock(&session->lock);
978 scsi_queue_work(host, &conn->xmitwork);
982 spin_unlock(&session->lock);
983 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
984 return SCSI_MLQUEUE_HOST_BUSY;
987 spin_unlock(&session->lock);
988 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
989 sc->cmnd[0], reason);
990 sc->result = (DID_NO_CONNECT << 16);
991 scsi_set_resid(sc, scsi_bufflen(sc));
995 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
997 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
999 if (depth > ISCSI_MAX_CMD_PER_LUN)
1000 depth = ISCSI_MAX_CMD_PER_LUN;
1001 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1002 return sdev->queue_depth;
1004 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1006 static struct iscsi_mgmt_task *
1007 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1008 char *data, uint32_t data_size)
1010 struct iscsi_session *session = conn->session;
1011 struct iscsi_mgmt_task *mtask;
1013 if (session->state == ISCSI_STATE_TERMINATE)
1016 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
1017 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
1019 * Login and Text are sent serially, in
1020 * request-followed-by-response sequence.
1021 * Same mtask can be used. Same ITT must be used.
1022 * Note that login_mtask is preallocated at conn_create().
1024 mtask = conn->login_mtask;
1026 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
1027 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
1029 if (!__kfifo_get(session->mgmtpool.queue,
1030 (void*)&mtask, sizeof(void*)))
1035 memcpy(mtask->data, data, data_size);
1036 mtask->data_count = data_size;
1038 mtask->data_count = 0;
1040 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
1041 INIT_LIST_HEAD(&mtask->running);
1042 list_add_tail(&mtask->running, &conn->mgmtqueue);
1046 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
1047 char *data, uint32_t data_size)
1049 struct iscsi_conn *conn = cls_conn->dd_data;
1050 struct iscsi_session *session = conn->session;
1053 spin_lock_bh(&session->lock);
1054 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
1056 spin_unlock_bh(&session->lock);
1057 scsi_queue_work(session->host, &conn->xmitwork);
1060 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
1062 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1064 struct iscsi_session *session = class_to_transport_session(cls_session);
1066 spin_lock_bh(&session->lock);
1067 if (session->state != ISCSI_STATE_LOGGED_IN) {
1068 session->state = ISCSI_STATE_RECOVERY_FAILED;
1069 if (session->leadconn)
1070 wake_up(&session->leadconn->ehwait);
1072 spin_unlock_bh(&session->lock);
1074 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1076 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1078 struct Scsi_Host *host = sc->device->host;
1079 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1080 struct iscsi_conn *conn = session->leadconn;
1082 mutex_lock(&session->eh_mutex);
1083 spin_lock_bh(&session->lock);
1084 if (session->state == ISCSI_STATE_TERMINATE) {
1086 debug_scsi("failing host reset: session terminated "
1087 "[CID %d age %d]\n", conn->id, session->age);
1088 spin_unlock_bh(&session->lock);
1089 mutex_unlock(&session->eh_mutex);
1093 spin_unlock_bh(&session->lock);
1094 mutex_unlock(&session->eh_mutex);
1096 * we drop the lock here but the leadconn cannot be destoyed while
1097 * we are in the scsi eh
1099 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1101 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1102 wait_event_interruptible(conn->ehwait,
1103 session->state == ISCSI_STATE_TERMINATE ||
1104 session->state == ISCSI_STATE_LOGGED_IN ||
1105 session->state == ISCSI_STATE_RECOVERY_FAILED);
1106 if (signal_pending(current))
1107 flush_signals(current);
1109 mutex_lock(&session->eh_mutex);
1110 spin_lock_bh(&session->lock);
1111 if (session->state == ISCSI_STATE_LOGGED_IN)
1112 printk(KERN_INFO "iscsi: host reset succeeded\n");
1115 spin_unlock_bh(&session->lock);
1116 mutex_unlock(&session->eh_mutex);
1119 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1121 static void iscsi_tmf_timedout(unsigned long data)
1123 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1124 struct iscsi_session *session = conn->session;
1126 spin_lock(&session->lock);
1127 if (conn->tmf_state == TMF_QUEUED) {
1128 conn->tmf_state = TMF_TIMEDOUT;
1129 debug_scsi("tmf timedout\n");
1130 /* unblock eh_abort() */
1131 wake_up(&conn->ehwait);
1133 spin_unlock(&session->lock);
1136 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1137 struct iscsi_tm *hdr, int age)
1139 struct iscsi_session *session = conn->session;
1140 struct iscsi_mgmt_task *mtask;
1142 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1145 spin_unlock_bh(&session->lock);
1146 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1147 spin_lock_bh(&session->lock);
1148 debug_scsi("tmf exec failure\n");
1151 conn->tmfcmd_pdus_cnt++;
1152 conn->tmf_timer.expires = 30 * HZ + jiffies;
1153 conn->tmf_timer.function = iscsi_tmf_timedout;
1154 conn->tmf_timer.data = (unsigned long)conn;
1155 add_timer(&conn->tmf_timer);
1156 debug_scsi("tmf set timeout\n");
1158 spin_unlock_bh(&session->lock);
1159 mutex_unlock(&session->eh_mutex);
1160 scsi_queue_work(session->host, &conn->xmitwork);
1163 * block eh thread until:
1167 * 3) session is terminated or restarted or userspace has
1168 * given up on recovery
1170 wait_event_interruptible(conn->ehwait, age != session->age ||
1171 session->state != ISCSI_STATE_LOGGED_IN ||
1172 conn->tmf_state != TMF_QUEUED);
1173 if (signal_pending(current))
1174 flush_signals(current);
1175 del_timer_sync(&conn->tmf_timer);
1177 mutex_lock(&session->eh_mutex);
1178 spin_lock_bh(&session->lock);
1179 /* if the session drops it will clean up the mtask */
1180 if (age != session->age ||
1181 session->state != ISCSI_STATE_LOGGED_IN)
1187 * Fail commands. session lock held and recv side suspended and xmit
1190 static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
1192 struct iscsi_cmd_task *ctask, *tmp;
1194 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1198 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1199 if (lun == ctask->sc->device->lun || lun == -1) {
1200 debug_scsi("failing pending sc %p itt 0x%x\n",
1201 ctask->sc, ctask->itt);
1202 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1206 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1207 if (lun == ctask->sc->device->lun || lun == -1) {
1208 debug_scsi("failing requeued sc %p itt 0x%x\n",
1209 ctask->sc, ctask->itt);
1210 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1214 /* fail all other running */
1215 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1216 if (lun == ctask->sc->device->lun || lun == -1) {
1217 debug_scsi("failing in progress sc %p itt 0x%x\n",
1218 ctask->sc, ctask->itt);
1219 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1224 static void iscsi_suspend_tx(struct iscsi_conn *conn)
1226 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1227 scsi_flush_work(conn->session->host);
1230 static void iscsi_start_tx(struct iscsi_conn *conn)
1232 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1233 scsi_queue_work(conn->session->host, &conn->xmitwork);
1236 static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1237 struct iscsi_tm *hdr)
1239 memset(hdr, 0, sizeof(*hdr));
1240 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1241 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1242 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1243 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1244 hdr->rtt = ctask->hdr->itt;
1245 hdr->refcmdsn = ctask->hdr->cmdsn;
1248 int iscsi_eh_abort(struct scsi_cmnd *sc)
1250 struct Scsi_Host *host = sc->device->host;
1251 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1252 struct iscsi_conn *conn;
1253 struct iscsi_cmd_task *ctask;
1254 struct iscsi_tm *hdr;
1257 mutex_lock(&session->eh_mutex);
1258 spin_lock_bh(&session->lock);
1260 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1264 debug_scsi("sc never reached iscsi layer or it completed.\n");
1265 spin_unlock_bh(&session->lock);
1266 mutex_unlock(&session->eh_mutex);
1271 * If we are not logged in or we have started a new session
1272 * then let the host reset code handle this
1274 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1275 sc->SCp.phase != session->age) {
1276 spin_unlock_bh(&session->lock);
1277 mutex_unlock(&session->eh_mutex);
1281 conn = session->leadconn;
1282 conn->eh_abort_cnt++;
1285 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1286 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1288 /* ctask completed before time out */
1290 debug_scsi("sc completed while abort in progress\n");
1294 if (ctask->state == ISCSI_TASK_PENDING) {
1295 fail_command(conn, ctask, DID_ABORT << 16);
1299 /* only have one tmf outstanding at a time */
1300 if (conn->tmf_state != TMF_INITIAL)
1302 conn->tmf_state = TMF_QUEUED;
1305 iscsi_prep_abort_task_pdu(ctask, hdr);
1307 if (iscsi_exec_task_mgmt_fn(conn, hdr, age)) {
1312 switch (conn->tmf_state) {
1314 spin_unlock_bh(&session->lock);
1315 iscsi_suspend_tx(conn);
1317 * clean up task if aborted. grab the recv lock as a writer
1319 write_lock_bh(conn->recv_lock);
1320 spin_lock(&session->lock);
1321 fail_command(conn, ctask, DID_ABORT << 16);
1322 conn->tmf_state = TMF_INITIAL;
1323 spin_unlock(&session->lock);
1324 write_unlock_bh(conn->recv_lock);
1325 iscsi_start_tx(conn);
1326 goto success_unlocked;
1328 spin_unlock_bh(&session->lock);
1329 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1330 goto failed_unlocked;
1333 conn->tmf_state = TMF_INITIAL;
1334 /* ctask completed before tmf abort response */
1335 debug_scsi("sc completed while abort in progress\n");
1340 conn->tmf_state = TMF_INITIAL;
1345 spin_unlock_bh(&session->lock);
1347 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1348 mutex_unlock(&session->eh_mutex);
1352 spin_unlock_bh(&session->lock);
1354 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1355 ctask ? ctask->itt : 0);
1356 mutex_unlock(&session->eh_mutex);
1359 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1361 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1363 memset(hdr, 0, sizeof(*hdr));
1364 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1365 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1366 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1367 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1368 hdr->rtt = ISCSI_RESERVED_TAG;
1371 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1373 struct Scsi_Host *host = sc->device->host;
1374 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1375 struct iscsi_conn *conn;
1376 struct iscsi_tm *hdr;
1379 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1381 mutex_lock(&session->eh_mutex);
1382 spin_lock_bh(&session->lock);
1384 * Just check if we are not logged in. We cannot check for
1385 * the phase because the reset could come from a ioctl.
1387 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1389 conn = session->leadconn;
1391 /* only have one tmf outstanding at a time */
1392 if (conn->tmf_state != TMF_INITIAL)
1394 conn->tmf_state = TMF_QUEUED;
1397 iscsi_prep_lun_reset_pdu(sc, hdr);
1399 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age)) {
1404 switch (conn->tmf_state) {
1408 spin_unlock_bh(&session->lock);
1409 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1412 conn->tmf_state = TMF_INITIAL;
1417 spin_unlock_bh(&session->lock);
1419 iscsi_suspend_tx(conn);
1420 /* need to grab the recv lock then session lock */
1421 write_lock_bh(conn->recv_lock);
1422 spin_lock(&session->lock);
1423 fail_all_commands(conn, sc->device->lun);
1424 conn->tmf_state = TMF_INITIAL;
1425 spin_unlock(&session->lock);
1426 write_unlock_bh(conn->recv_lock);
1428 iscsi_start_tx(conn);
1432 spin_unlock_bh(&session->lock);
1434 debug_scsi("iscsi_eh_device_reset %s\n",
1435 rc == SUCCESS ? "SUCCESS" : "FAILED");
1436 mutex_unlock(&session->eh_mutex);
1439 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1442 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1443 * should be accessed via kfifo_{get,put} on q->queue.
1444 * Optionally, the caller can obtain the array of object pointers
1445 * by passing in a non-NULL @items pointer
1448 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1450 int i, num_arrays = 1;
1452 memset(q, 0, sizeof(*q));
1456 /* If the user passed an items pointer, he wants a copy of
1460 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1461 if (q->pool == NULL)
1464 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1466 if (q->queue == ERR_PTR(-ENOMEM))
1469 for (i = 0; i < max; i++) {
1470 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
1471 if (q->pool[i] == NULL) {
1475 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1479 *items = q->pool + max;
1480 memcpy(*items, q->pool, max * sizeof(void *));
1489 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1491 void iscsi_pool_free(struct iscsi_pool *q)
1495 for (i = 0; i < q->max; i++)
1500 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1503 * iSCSI Session's hostdata organization:
1505 * *------------------* <== hostdata_session(host->hostdata)
1506 * | ptr to class sess|
1507 * |------------------| <== iscsi_hostdata(host->hostdata)
1509 * *------------------*
1512 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1513 _sz % sizeof(unsigned long))
1515 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1518 * iscsi_session_setup - create iscsi cls session and host and session
1519 * @scsit: scsi transport template
1520 * @iscsit: iscsi transport template
1521 * @cmds_max: scsi host can queue
1522 * @qdepth: scsi host cmds per lun
1523 * @cmd_task_size: LLD ctask private data size
1524 * @mgmt_task_size: LLD mtask private data size
1525 * @initial_cmdsn: initial CmdSN
1526 * @hostno: host no allocated
1528 * This can be used by software iscsi_transports that allocate
1529 * a session per scsi host.
1531 struct iscsi_cls_session *
1532 iscsi_session_setup(struct iscsi_transport *iscsit,
1533 struct scsi_transport_template *scsit,
1534 uint16_t cmds_max, uint16_t qdepth,
1535 int cmd_task_size, int mgmt_task_size,
1536 uint32_t initial_cmdsn, uint32_t *hostno)
1538 struct Scsi_Host *shost;
1539 struct iscsi_session *session;
1540 struct iscsi_cls_session *cls_session;
1543 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1545 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1546 "Queue depth must be between 1 and %d.\n",
1547 qdepth, ISCSI_MAX_CMD_PER_LUN);
1548 qdepth = ISCSI_DEF_CMD_PER_LUN;
1551 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1552 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1554 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1555 "can_queue must be a power of 2 and between "
1556 "2 and %d - setting to %d.\n", cmds_max,
1557 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1558 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1561 shost = scsi_host_alloc(iscsit->host_template,
1562 hostdata_privsize(sizeof(*session)));
1566 /* the iscsi layer takes one task for reserve */
1567 shost->can_queue = cmds_max - 1;
1568 shost->cmd_per_lun = qdepth;
1570 shost->max_channel = 0;
1571 shost->max_lun = iscsit->max_lun;
1572 shost->max_cmd_len = iscsit->max_cmd_len;
1573 shost->transportt = scsit;
1574 shost->transportt->create_work_queue = 1;
1575 *hostno = shost->host_no;
1577 session = iscsi_hostdata(shost->hostdata);
1578 memset(session, 0, sizeof(struct iscsi_session));
1579 session->host = shost;
1580 session->state = ISCSI_STATE_FREE;
1581 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1582 session->cmds_max = cmds_max;
1583 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
1584 session->exp_cmdsn = initial_cmdsn + 1;
1585 session->max_cmdsn = initial_cmdsn + 1;
1586 session->max_r2t = 1;
1587 session->tt = iscsit;
1588 mutex_init(&session->eh_mutex);
1590 /* initialize SCSI PDU commands pool */
1591 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1592 (void***)&session->cmds,
1593 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1594 goto cmdpool_alloc_fail;
1596 /* pre-format cmds pool with ITT */
1597 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1598 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1601 ctask->dd_data = &ctask[1];
1603 INIT_LIST_HEAD(&ctask->running);
1606 spin_lock_init(&session->lock);
1608 /* initialize immediate command pool */
1609 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1610 (void***)&session->mgmt_cmds,
1611 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1612 goto mgmtpool_alloc_fail;
1615 /* pre-format immediate cmds pool with ITT */
1616 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1617 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1620 mtask->dd_data = &mtask[1];
1621 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1622 INIT_LIST_HEAD(&mtask->running);
1625 if (scsi_add_host(shost, NULL))
1628 if (!try_module_get(iscsit->owner))
1629 goto cls_session_fail;
1631 cls_session = iscsi_create_session(shost, iscsit, 0);
1634 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1639 module_put(iscsit->owner);
1641 scsi_remove_host(shost);
1643 iscsi_pool_free(&session->mgmtpool);
1644 mgmtpool_alloc_fail:
1645 iscsi_pool_free(&session->cmdpool);
1647 scsi_host_put(shost);
1650 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1653 * iscsi_session_teardown - destroy session, host, and cls_session
1656 * This can be used by software iscsi_transports that allocate
1657 * a session per scsi host.
1659 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1661 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1662 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1663 struct module *owner = cls_session->transport->owner;
1665 iscsi_unblock_session(cls_session);
1666 scsi_remove_host(shost);
1668 iscsi_pool_free(&session->mgmtpool);
1669 iscsi_pool_free(&session->cmdpool);
1671 kfree(session->password);
1672 kfree(session->password_in);
1673 kfree(session->username);
1674 kfree(session->username_in);
1675 kfree(session->targetname);
1676 kfree(session->netdev);
1677 kfree(session->hwaddress);
1678 kfree(session->initiatorname);
1680 iscsi_destroy_session(cls_session);
1681 scsi_host_put(shost);
1684 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1687 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1688 * @cls_session: iscsi_cls_session
1691 struct iscsi_cls_conn *
1692 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1694 struct iscsi_session *session = class_to_transport_session(cls_session);
1695 struct iscsi_conn *conn;
1696 struct iscsi_cls_conn *cls_conn;
1699 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1702 conn = cls_conn->dd_data;
1703 memset(conn, 0, sizeof(*conn));
1705 conn->session = session;
1706 conn->cls_conn = cls_conn;
1707 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1708 conn->id = conn_idx;
1709 conn->exp_statsn = 0;
1710 conn->tmf_state = TMF_INITIAL;
1711 INIT_LIST_HEAD(&conn->run_list);
1712 INIT_LIST_HEAD(&conn->mgmt_run_list);
1713 INIT_LIST_HEAD(&conn->mgmtqueue);
1714 INIT_LIST_HEAD(&conn->xmitqueue);
1715 INIT_LIST_HEAD(&conn->requeue);
1716 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
1718 /* allocate login_mtask used for the login/text sequences */
1719 spin_lock_bh(&session->lock);
1720 if (!__kfifo_get(session->mgmtpool.queue,
1721 (void*)&conn->login_mtask,
1723 spin_unlock_bh(&session->lock);
1724 goto login_mtask_alloc_fail;
1726 spin_unlock_bh(&session->lock);
1728 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
1730 goto login_mtask_data_alloc_fail;
1731 conn->login_mtask->data = conn->data = data;
1733 init_timer(&conn->tmf_timer);
1734 init_waitqueue_head(&conn->ehwait);
1738 login_mtask_data_alloc_fail:
1739 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1741 login_mtask_alloc_fail:
1742 iscsi_destroy_conn(cls_conn);
1745 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1748 * iscsi_conn_teardown - teardown iscsi connection
1749 * cls_conn: iscsi class connection
1751 * TODO: we may need to make this into a two step process
1752 * like scsi-mls remove + put host
1754 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1756 struct iscsi_conn *conn = cls_conn->dd_data;
1757 struct iscsi_session *session = conn->session;
1758 unsigned long flags;
1760 spin_lock_bh(&session->lock);
1761 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1762 if (session->leadconn == conn) {
1764 * leading connection? then give up on recovery.
1766 session->state = ISCSI_STATE_TERMINATE;
1767 wake_up(&conn->ehwait);
1769 spin_unlock_bh(&session->lock);
1772 * Block until all in-progress commands for this connection
1776 spin_lock_irqsave(session->host->host_lock, flags);
1777 if (!session->host->host_busy) { /* OK for ERL == 0 */
1778 spin_unlock_irqrestore(session->host->host_lock, flags);
1781 spin_unlock_irqrestore(session->host->host_lock, flags);
1782 msleep_interruptible(500);
1783 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1784 "host_failed %d\n", session->host->host_busy,
1785 session->host->host_failed);
1787 * force eh_abort() to unblock
1789 wake_up(&conn->ehwait);
1792 /* flush queued up work because we free the connection below */
1793 iscsi_suspend_tx(conn);
1795 spin_lock_bh(&session->lock);
1797 kfree(conn->persistent_address);
1798 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1800 if (session->leadconn == conn)
1801 session->leadconn = NULL;
1802 spin_unlock_bh(&session->lock);
1804 iscsi_destroy_conn(cls_conn);
1806 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1808 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1810 struct iscsi_conn *conn = cls_conn->dd_data;
1811 struct iscsi_session *session = conn->session;
1814 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1818 if ((session->imm_data_en || !session->initial_r2t_en) &&
1819 session->first_burst > session->max_burst) {
1820 printk("iscsi: invalid burst lengths: "
1821 "first_burst %d max_burst %d\n",
1822 session->first_burst, session->max_burst);
1826 spin_lock_bh(&session->lock);
1827 conn->c_stage = ISCSI_CONN_STARTED;
1828 session->state = ISCSI_STATE_LOGGED_IN;
1829 session->queued_cmdsn = session->cmdsn;
1831 switch(conn->stop_stage) {
1832 case STOP_CONN_RECOVER:
1834 * unblock eh_abort() if it is blocked. re-try all
1835 * commands after successful recovery
1837 conn->stop_stage = 0;
1838 conn->tmf_state = TMF_INITIAL;
1840 spin_unlock_bh(&session->lock);
1842 iscsi_unblock_session(session_to_cls(session));
1843 wake_up(&conn->ehwait);
1845 case STOP_CONN_TERM:
1846 conn->stop_stage = 0;
1851 spin_unlock_bh(&session->lock);
1855 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1858 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1860 struct iscsi_mgmt_task *mtask, *tmp;
1862 /* handle pending */
1863 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
1864 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1865 iscsi_free_mgmt_task(conn, mtask);
1868 /* handle running */
1869 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1870 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1871 iscsi_free_mgmt_task(conn, mtask);
1877 static void iscsi_start_session_recovery(struct iscsi_session *session,
1878 struct iscsi_conn *conn, int flag)
1882 mutex_lock(&session->eh_mutex);
1883 spin_lock_bh(&session->lock);
1884 if (conn->stop_stage == STOP_CONN_TERM) {
1885 spin_unlock_bh(&session->lock);
1886 mutex_unlock(&session->eh_mutex);
1891 * The LLD either freed/unset the lock on us, or userspace called
1892 * stop but did not create a proper connection (connection was never
1893 * bound or it was unbound then stop was called).
1895 if (!conn->recv_lock) {
1896 spin_unlock_bh(&session->lock);
1897 mutex_unlock(&session->eh_mutex);
1902 * When this is called for the in_login state, we only want to clean
1903 * up the login task and connection. We do not need to block and set
1904 * the recovery state again
1906 if (flag == STOP_CONN_TERM)
1907 session->state = ISCSI_STATE_TERMINATE;
1908 else if (conn->stop_stage != STOP_CONN_RECOVER)
1909 session->state = ISCSI_STATE_IN_RECOVERY;
1911 old_stop_stage = conn->stop_stage;
1912 conn->stop_stage = flag;
1913 conn->c_stage = ISCSI_CONN_STOPPED;
1914 spin_unlock_bh(&session->lock);
1916 iscsi_suspend_tx(conn);
1918 write_lock_bh(conn->recv_lock);
1919 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1920 write_unlock_bh(conn->recv_lock);
1923 * for connection level recovery we should not calculate
1924 * header digest. conn->hdr_size used for optimization
1925 * in hdr_extract() and will be re-negotiated at
1928 if (flag == STOP_CONN_RECOVER) {
1929 conn->hdrdgst_en = 0;
1930 conn->datadgst_en = 0;
1931 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1932 old_stop_stage != STOP_CONN_RECOVER) {
1933 debug_scsi("blocking session\n");
1934 iscsi_block_session(session_to_cls(session));
1941 spin_lock_bh(&session->lock);
1942 fail_all_commands(conn, -1);
1943 flush_control_queues(session, conn);
1944 spin_unlock_bh(&session->lock);
1945 mutex_unlock(&session->eh_mutex);
1948 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1950 struct iscsi_conn *conn = cls_conn->dd_data;
1951 struct iscsi_session *session = conn->session;
1954 case STOP_CONN_RECOVER:
1955 case STOP_CONN_TERM:
1956 iscsi_start_session_recovery(session, conn, flag);
1959 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1962 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1964 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1965 struct iscsi_cls_conn *cls_conn, int is_leading)
1967 struct iscsi_session *session = class_to_transport_session(cls_session);
1968 struct iscsi_conn *conn = cls_conn->dd_data;
1970 spin_lock_bh(&session->lock);
1972 session->leadconn = conn;
1973 spin_unlock_bh(&session->lock);
1976 * Unblock xmitworker(), Login Phase will pass through.
1978 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1979 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1982 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1985 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1986 enum iscsi_param param, char *buf, int buflen)
1988 struct iscsi_conn *conn = cls_conn->dd_data;
1989 struct iscsi_session *session = conn->session;
1993 case ISCSI_PARAM_FAST_ABORT:
1994 sscanf(buf, "%d", &session->fast_abort);
1996 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1997 sscanf(buf, "%d", &conn->max_recv_dlength);
1999 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2000 sscanf(buf, "%d", &conn->max_xmit_dlength);
2002 case ISCSI_PARAM_HDRDGST_EN:
2003 sscanf(buf, "%d", &conn->hdrdgst_en);
2005 case ISCSI_PARAM_DATADGST_EN:
2006 sscanf(buf, "%d", &conn->datadgst_en);
2008 case ISCSI_PARAM_INITIAL_R2T_EN:
2009 sscanf(buf, "%d", &session->initial_r2t_en);
2011 case ISCSI_PARAM_MAX_R2T:
2012 sscanf(buf, "%d", &session->max_r2t);
2014 case ISCSI_PARAM_IMM_DATA_EN:
2015 sscanf(buf, "%d", &session->imm_data_en);
2017 case ISCSI_PARAM_FIRST_BURST:
2018 sscanf(buf, "%d", &session->first_burst);
2020 case ISCSI_PARAM_MAX_BURST:
2021 sscanf(buf, "%d", &session->max_burst);
2023 case ISCSI_PARAM_PDU_INORDER_EN:
2024 sscanf(buf, "%d", &session->pdu_inorder_en);
2026 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2027 sscanf(buf, "%d", &session->dataseq_inorder_en);
2029 case ISCSI_PARAM_ERL:
2030 sscanf(buf, "%d", &session->erl);
2032 case ISCSI_PARAM_IFMARKER_EN:
2033 sscanf(buf, "%d", &value);
2036 case ISCSI_PARAM_OFMARKER_EN:
2037 sscanf(buf, "%d", &value);
2040 case ISCSI_PARAM_EXP_STATSN:
2041 sscanf(buf, "%u", &conn->exp_statsn);
2043 case ISCSI_PARAM_USERNAME:
2044 kfree(session->username);
2045 session->username = kstrdup(buf, GFP_KERNEL);
2046 if (!session->username)
2049 case ISCSI_PARAM_USERNAME_IN:
2050 kfree(session->username_in);
2051 session->username_in = kstrdup(buf, GFP_KERNEL);
2052 if (!session->username_in)
2055 case ISCSI_PARAM_PASSWORD:
2056 kfree(session->password);
2057 session->password = kstrdup(buf, GFP_KERNEL);
2058 if (!session->password)
2061 case ISCSI_PARAM_PASSWORD_IN:
2062 kfree(session->password_in);
2063 session->password_in = kstrdup(buf, GFP_KERNEL);
2064 if (!session->password_in)
2067 case ISCSI_PARAM_TARGET_NAME:
2068 /* this should not change between logins */
2069 if (session->targetname)
2072 session->targetname = kstrdup(buf, GFP_KERNEL);
2073 if (!session->targetname)
2076 case ISCSI_PARAM_TPGT:
2077 sscanf(buf, "%d", &session->tpgt);
2079 case ISCSI_PARAM_PERSISTENT_PORT:
2080 sscanf(buf, "%d", &conn->persistent_port);
2082 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2084 * this is the address returned in discovery so it should
2085 * not change between logins.
2087 if (conn->persistent_address)
2090 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2091 if (!conn->persistent_address)
2100 EXPORT_SYMBOL_GPL(iscsi_set_param);
2102 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2103 enum iscsi_param param, char *buf)
2105 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2106 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2110 case ISCSI_PARAM_FAST_ABORT:
2111 len = sprintf(buf, "%d\n", session->fast_abort);
2113 case ISCSI_PARAM_INITIAL_R2T_EN:
2114 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2116 case ISCSI_PARAM_MAX_R2T:
2117 len = sprintf(buf, "%hu\n", session->max_r2t);
2119 case ISCSI_PARAM_IMM_DATA_EN:
2120 len = sprintf(buf, "%d\n", session->imm_data_en);
2122 case ISCSI_PARAM_FIRST_BURST:
2123 len = sprintf(buf, "%u\n", session->first_burst);
2125 case ISCSI_PARAM_MAX_BURST:
2126 len = sprintf(buf, "%u\n", session->max_burst);
2128 case ISCSI_PARAM_PDU_INORDER_EN:
2129 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2131 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2132 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2134 case ISCSI_PARAM_ERL:
2135 len = sprintf(buf, "%d\n", session->erl);
2137 case ISCSI_PARAM_TARGET_NAME:
2138 len = sprintf(buf, "%s\n", session->targetname);
2140 case ISCSI_PARAM_TPGT:
2141 len = sprintf(buf, "%d\n", session->tpgt);
2143 case ISCSI_PARAM_USERNAME:
2144 len = sprintf(buf, "%s\n", session->username);
2146 case ISCSI_PARAM_USERNAME_IN:
2147 len = sprintf(buf, "%s\n", session->username_in);
2149 case ISCSI_PARAM_PASSWORD:
2150 len = sprintf(buf, "%s\n", session->password);
2152 case ISCSI_PARAM_PASSWORD_IN:
2153 len = sprintf(buf, "%s\n", session->password_in);
2161 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2163 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2164 enum iscsi_param param, char *buf)
2166 struct iscsi_conn *conn = cls_conn->dd_data;
2170 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2171 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2173 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2174 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2176 case ISCSI_PARAM_HDRDGST_EN:
2177 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2179 case ISCSI_PARAM_DATADGST_EN:
2180 len = sprintf(buf, "%d\n", conn->datadgst_en);
2182 case ISCSI_PARAM_IFMARKER_EN:
2183 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2185 case ISCSI_PARAM_OFMARKER_EN:
2186 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2188 case ISCSI_PARAM_EXP_STATSN:
2189 len = sprintf(buf, "%u\n", conn->exp_statsn);
2191 case ISCSI_PARAM_PERSISTENT_PORT:
2192 len = sprintf(buf, "%d\n", conn->persistent_port);
2194 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2195 len = sprintf(buf, "%s\n", conn->persistent_address);
2203 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2205 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2208 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2212 case ISCSI_HOST_PARAM_NETDEV_NAME:
2213 if (!session->netdev)
2214 len = sprintf(buf, "%s\n", "default");
2216 len = sprintf(buf, "%s\n", session->netdev);
2218 case ISCSI_HOST_PARAM_HWADDRESS:
2219 if (!session->hwaddress)
2220 len = sprintf(buf, "%s\n", "default");
2222 len = sprintf(buf, "%s\n", session->hwaddress);
2224 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2225 if (!session->initiatorname)
2226 len = sprintf(buf, "%s\n", "unknown");
2228 len = sprintf(buf, "%s\n", session->initiatorname);
2237 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2239 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2240 char *buf, int buflen)
2242 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2245 case ISCSI_HOST_PARAM_NETDEV_NAME:
2246 if (!session->netdev)
2247 session->netdev = kstrdup(buf, GFP_KERNEL);
2249 case ISCSI_HOST_PARAM_HWADDRESS:
2250 if (!session->hwaddress)
2251 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2253 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2254 if (!session->initiatorname)
2255 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2263 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2265 MODULE_AUTHOR("Mike Christie");
2266 MODULE_DESCRIPTION("iSCSI library functions");
2267 MODULE_LICENSE("GPL");