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 __kfifo_len(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);
126 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
127 * @ctask: iscsi cmd task
129 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
130 * fields like dlength or final based on how much data it sends
132 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
134 struct iscsi_conn *conn = ctask->conn;
135 struct iscsi_session *session = conn->session;
136 struct iscsi_cmd *hdr = ctask->hdr;
137 struct scsi_cmnd *sc = ctask->sc;
139 hdr->opcode = ISCSI_OP_SCSI_CMD;
140 hdr->flags = ISCSI_ATTR_SIMPLE;
141 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
142 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
143 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
144 hdr->cmdsn = cpu_to_be32(session->cmdsn);
146 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
147 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
148 if (sc->cmd_len < MAX_COMMAND_SIZE)
149 memset(&hdr->cdb[sc->cmd_len], 0,
150 MAX_COMMAND_SIZE - sc->cmd_len);
152 ctask->data_count = 0;
153 ctask->imm_count = 0;
154 if (sc->sc_data_direction == DMA_TO_DEVICE) {
155 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
159 * imm_count bytes to be sent right after
162 * unsol_count bytes(as Data-Out) to be sent
163 * without R2T ack right after
166 * r2t_data_count bytes to be sent via R2T ack's
168 * pad_count bytes to be sent as zero-padding
170 ctask->unsol_count = 0;
171 ctask->unsol_offset = 0;
172 ctask->unsol_datasn = 0;
174 if (session->imm_data_en) {
175 if (scsi_bufflen(sc) >= session->first_burst)
176 ctask->imm_count = min(session->first_burst,
177 conn->max_xmit_dlength);
179 ctask->imm_count = min(scsi_bufflen(sc),
180 conn->max_xmit_dlength);
181 hton24(ctask->hdr->dlength, ctask->imm_count);
183 zero_data(ctask->hdr->dlength);
185 if (!session->initial_r2t_en) {
186 ctask->unsol_count = min((session->first_burst),
187 (scsi_bufflen(sc))) - ctask->imm_count;
188 ctask->unsol_offset = ctask->imm_count;
191 if (!ctask->unsol_count)
192 /* No unsolicit Data-Out's */
193 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
195 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
196 zero_data(hdr->dlength);
198 if (sc->sc_data_direction == DMA_FROM_DEVICE)
199 hdr->flags |= ISCSI_FLAG_CMD_READ;
202 conn->scsicmd_pdus_cnt++;
204 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
205 "cmdsn %d win %d]\n",
206 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
207 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
208 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
212 * iscsi_complete_command - return command back to scsi-ml
213 * @ctask: iscsi cmd task
215 * Must be called with session lock.
216 * This function returns the scsi command to scsi-ml and returns
217 * the cmd task to the pool of available cmd tasks.
219 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
221 struct iscsi_session *session = ctask->conn->session;
222 struct scsi_cmnd *sc = ctask->sc;
224 ctask->state = ISCSI_TASK_COMPLETED;
226 /* SCSI eh reuses commands to verify us */
228 list_del_init(&ctask->running);
229 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
233 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
235 atomic_inc(&ctask->refcount);
238 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
240 if (atomic_dec_and_test(&ctask->refcount))
241 iscsi_complete_command(ctask);
245 * iscsi_cmd_rsp - SCSI Command Response processing
246 * @conn: iscsi connection
248 * @ctask: scsi command task
249 * @data: cmd data buffer
250 * @datalen: len of buffer
252 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
253 * then completes the command and task.
255 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
256 struct iscsi_cmd_task *ctask, char *data,
259 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
260 struct iscsi_session *session = conn->session;
261 struct scsi_cmnd *sc = ctask->sc;
263 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
264 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
266 sc->result = (DID_OK << 16) | rhdr->cmd_status;
268 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
269 sc->result = DID_ERROR << 16;
273 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
278 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
279 "invalid data buffer size of %d\n", datalen);
280 sc->result = DID_BAD_TARGET << 16;
284 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
285 if (datalen < senselen)
286 goto invalid_datalen;
288 memcpy(sc->sense_buffer, data + 2,
289 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
290 debug_scsi("copied %d bytes of sense\n",
291 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
294 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
295 int res_count = be32_to_cpu(rhdr->residual_count);
297 if (res_count > 0 && res_count <= scsi_bufflen(sc))
298 scsi_set_resid(sc, res_count);
300 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
301 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
302 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
303 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
304 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
307 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
308 (long)sc, sc->result, ctask->itt);
309 conn->scsirsp_pdus_cnt++;
311 __iscsi_put_ctask(ctask);
314 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
316 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
318 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
319 conn->tmfrsp_pdus_cnt++;
321 if (conn->tmabort_state != TMABORT_INITIAL)
324 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
325 conn->tmabort_state = TMABORT_SUCCESS;
326 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
327 conn->tmabort_state = TMABORT_NOT_FOUND;
329 conn->tmabort_state = TMABORT_FAILED;
330 wake_up(&conn->ehwait);
333 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
334 char *data, int datalen)
336 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
337 struct iscsi_hdr rejected_pdu;
340 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
342 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
343 if (ntoh24(reject->dlength) > datalen)
344 return ISCSI_ERR_PROTO;
346 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
347 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
348 itt = get_itt(rejected_pdu.itt);
349 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
350 "due to DataDigest error.\n", itt,
351 rejected_pdu.opcode);
358 * __iscsi_complete_pdu - complete pdu
362 * @datalen: len of data buffer
364 * Completes pdu processing by freeing any resources allocated at
365 * queuecommand or send generic. session lock must be held and verify
366 * itt must have been called.
368 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
369 char *data, int datalen)
371 struct iscsi_session *session = conn->session;
372 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
373 struct iscsi_cmd_task *ctask;
374 struct iscsi_mgmt_task *mtask;
377 if (hdr->itt != RESERVED_ITT)
378 itt = get_itt(hdr->itt);
382 if (itt < session->cmds_max) {
383 ctask = session->cmds[itt];
385 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
386 opcode, conn->id, ctask->itt, datalen);
389 case ISCSI_OP_SCSI_CMD_RSP:
390 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
391 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
394 case ISCSI_OP_SCSI_DATA_IN:
395 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
396 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
397 conn->scsirsp_pdus_cnt++;
398 __iscsi_put_ctask(ctask);
402 /* LLD handles this for now */
405 rc = ISCSI_ERR_BAD_OPCODE;
408 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
409 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
410 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
412 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
413 opcode, conn->id, mtask->itt, datalen);
415 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
417 case ISCSI_OP_LOGOUT_RSP:
419 rc = ISCSI_ERR_PROTO;
422 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
424 case ISCSI_OP_LOGIN_RSP:
425 case ISCSI_OP_TEXT_RSP:
427 * login related PDU's exp_statsn is handled in
430 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
431 rc = ISCSI_ERR_CONN_FAILED;
432 list_del(&mtask->running);
433 if (conn->login_mtask != mtask)
434 __kfifo_put(session->mgmtpool.queue,
435 (void*)&mtask, sizeof(void*));
437 case ISCSI_OP_SCSI_TMFUNC_RSP:
439 rc = ISCSI_ERR_PROTO;
443 iscsi_tmf_rsp(conn, hdr);
445 case ISCSI_OP_NOOP_IN:
446 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
447 rc = ISCSI_ERR_PROTO;
450 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
452 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
453 rc = ISCSI_ERR_CONN_FAILED;
454 list_del(&mtask->running);
455 if (conn->login_mtask != mtask)
456 __kfifo_put(session->mgmtpool.queue,
457 (void*)&mtask, sizeof(void*));
460 rc = ISCSI_ERR_BAD_OPCODE;
463 } else if (itt == ~0U) {
464 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
467 case ISCSI_OP_NOOP_IN:
469 rc = ISCSI_ERR_PROTO;
473 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
476 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
477 rc = ISCSI_ERR_CONN_FAILED;
479 case ISCSI_OP_REJECT:
480 rc = iscsi_handle_reject(conn, hdr, data, datalen);
482 case ISCSI_OP_ASYNC_EVENT:
483 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
484 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
485 rc = ISCSI_ERR_CONN_FAILED;
488 rc = ISCSI_ERR_BAD_OPCODE;
492 rc = ISCSI_ERR_BAD_ITT;
496 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
498 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
499 char *data, int datalen)
503 spin_lock(&conn->session->lock);
504 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
505 spin_unlock(&conn->session->lock);
508 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
510 /* verify itt (itt encoding: age+cid+itt) */
511 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
514 struct iscsi_session *session = conn->session;
515 struct iscsi_cmd_task *ctask;
518 if (hdr->itt != RESERVED_ITT) {
519 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
520 (session->age << ISCSI_AGE_SHIFT)) {
521 printk(KERN_ERR "iscsi: received itt %x expected "
522 "session age (%x)\n", (__force u32)hdr->itt,
523 session->age & ISCSI_AGE_MASK);
524 return ISCSI_ERR_BAD_ITT;
527 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
528 (conn->id << ISCSI_CID_SHIFT)) {
529 printk(KERN_ERR "iscsi: received itt %x, expected "
530 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
531 return ISCSI_ERR_BAD_ITT;
533 itt = get_itt(hdr->itt);
537 if (itt < session->cmds_max) {
538 ctask = session->cmds[itt];
541 printk(KERN_INFO "iscsi: dropping ctask with "
542 "itt 0x%x\n", ctask->itt);
544 return ISCSI_ERR_NO_SCSI_CMD;
547 if (ctask->sc->SCp.phase != session->age) {
548 printk(KERN_ERR "iscsi: ctask's session age %d, "
549 "expected %d\n", ctask->sc->SCp.phase,
551 return ISCSI_ERR_SESSION_FAILED;
558 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
560 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
562 struct iscsi_session *session = conn->session;
565 spin_lock_irqsave(&session->lock, flags);
566 if (session->state == ISCSI_STATE_FAILED) {
567 spin_unlock_irqrestore(&session->lock, flags);
571 if (conn->stop_stage == 0)
572 session->state = ISCSI_STATE_FAILED;
573 spin_unlock_irqrestore(&session->lock, flags);
574 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
575 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
576 iscsi_conn_error(conn->cls_conn, err);
578 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
580 static void iscsi_prep_mtask(struct iscsi_conn *conn,
581 struct iscsi_mgmt_task *mtask)
583 struct iscsi_session *session = conn->session;
584 struct iscsi_hdr *hdr = mtask->hdr;
585 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
587 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
588 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
589 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
591 * pre-format CmdSN for outgoing PDU.
593 nop->cmdsn = cpu_to_be32(session->cmdsn);
594 if (hdr->itt != RESERVED_ITT) {
595 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
597 * TODO: We always use immediate, so we never hit this.
598 * If we start to send tmfs or nops as non-immediate then
599 * we should start checking the cmdsn numbers for mgmt tasks.
601 if (conn->c_stage == ISCSI_CONN_STARTED &&
602 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
603 session->queued_cmdsn++;
608 if (session->tt->init_mgmt_task)
609 session->tt->init_mgmt_task(conn, mtask);
611 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
612 hdr->opcode, hdr->itt, mtask->data_count);
615 static int iscsi_xmit_mtask(struct iscsi_conn *conn)
617 struct iscsi_hdr *hdr = conn->mtask->hdr;
618 int rc, was_logout = 0;
620 spin_unlock_bh(&conn->session->lock);
621 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
622 conn->session->state = ISCSI_STATE_IN_RECOVERY;
623 iscsi_block_session(session_to_cls(conn->session));
626 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
627 spin_lock_bh(&conn->session->lock);
631 /* done with this in-progress mtask */
635 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
641 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
643 struct iscsi_session *session = conn->session;
646 * Check for iSCSI window and take care of CmdSN wrap-around
648 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
649 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
650 "CmdSN %u/%u\n", session->exp_cmdsn,
651 session->max_cmdsn, session->cmdsn,
652 session->queued_cmdsn);
658 static int iscsi_xmit_ctask(struct iscsi_conn *conn)
660 struct iscsi_cmd_task *ctask = conn->ctask;
664 * serialize with TMF AbortTask
666 if (ctask->state == ISCSI_TASK_ABORTING)
669 __iscsi_get_ctask(ctask);
670 spin_unlock_bh(&conn->session->lock);
671 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
672 spin_lock_bh(&conn->session->lock);
673 __iscsi_put_ctask(ctask);
677 /* done with this ctask */
683 * iscsi_data_xmit - xmit any command into the scheduled connection
684 * @conn: iscsi connection
687 * The function can return -EAGAIN in which case the caller must
688 * re-schedule it again later or recover. '0' return code means
691 static int iscsi_data_xmit(struct iscsi_conn *conn)
695 spin_lock_bh(&conn->session->lock);
696 if (unlikely(conn->suspend_tx)) {
697 debug_scsi("conn %d Tx suspended!\n", conn->id);
698 spin_unlock_bh(&conn->session->lock);
703 rc = iscsi_xmit_ctask(conn);
709 rc = iscsi_xmit_mtask(conn);
715 * process mgmt pdus like nops before commands since we should
716 * only have one nop-out as a ping from us and targets should not
717 * overflow us with nop-ins
720 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
722 iscsi_prep_mtask(conn, conn->mtask);
723 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
724 rc = iscsi_xmit_mtask(conn);
729 /* process command queue */
730 while (!list_empty(&conn->xmitqueue)) {
732 * iscsi tcp may readd the task to the xmitqueue to send
735 conn->ctask = list_entry(conn->xmitqueue.next,
736 struct iscsi_cmd_task, running);
737 switch (conn->ctask->state) {
738 case ISCSI_TASK_ABORTING:
740 case ISCSI_TASK_PENDING:
741 iscsi_prep_scsi_cmd_pdu(conn->ctask);
742 conn->session->tt->init_cmd_task(conn->ctask);
745 conn->ctask->state = ISCSI_TASK_RUNNING;
748 list_move_tail(conn->xmitqueue.next, &conn->run_list);
750 rc = iscsi_xmit_ctask(conn);
754 * we could continuously get new ctask requests so
755 * we need to check the mgmt queue for nops that need to
756 * be sent to aviod starvation
758 if (__kfifo_len(conn->mgmtqueue))
761 spin_unlock_bh(&conn->session->lock);
765 if (unlikely(conn->suspend_tx))
767 spin_unlock_bh(&conn->session->lock);
771 static void iscsi_xmitworker(struct work_struct *work)
773 struct iscsi_conn *conn =
774 container_of(work, struct iscsi_conn, xmitwork);
777 * serialize Xmit worker on a per-connection basis.
780 rc = iscsi_data_xmit(conn);
781 } while (rc >= 0 || rc == -EAGAIN);
785 FAILURE_BAD_HOST = 1,
786 FAILURE_SESSION_FAILED,
787 FAILURE_SESSION_FREED,
788 FAILURE_WINDOW_CLOSED,
790 FAILURE_SESSION_TERMINATE,
791 FAILURE_SESSION_IN_RECOVERY,
792 FAILURE_SESSION_RECOVERY_TIMEOUT,
795 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
797 struct Scsi_Host *host;
799 struct iscsi_session *session;
800 struct iscsi_conn *conn;
801 struct iscsi_cmd_task *ctask = NULL;
803 sc->scsi_done = done;
807 host = sc->device->host;
808 session = iscsi_hostdata(host->hostdata);
810 spin_lock(&session->lock);
813 * ISCSI_STATE_FAILED is a temp. state. The recovery
814 * code will decide what is best to do with command queued
817 if (session->state != ISCSI_STATE_LOGGED_IN &&
818 session->state != ISCSI_STATE_FAILED) {
820 * to handle the race between when we set the recovery state
821 * and block the session we requeue here (commands could
822 * be entering our queuecommand while a block is starting
823 * up because the block code is not locked)
825 if (session->state == ISCSI_STATE_IN_RECOVERY) {
826 reason = FAILURE_SESSION_IN_RECOVERY;
830 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
831 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
832 else if (session->state == ISCSI_STATE_TERMINATE)
833 reason = FAILURE_SESSION_TERMINATE;
835 reason = FAILURE_SESSION_FREED;
839 conn = session->leadconn;
841 reason = FAILURE_SESSION_FREED;
845 if (iscsi_check_cmdsn_window_closed(conn)) {
846 reason = FAILURE_WINDOW_CLOSED;
850 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
852 reason = FAILURE_OOM;
855 session->queued_cmdsn++;
857 sc->SCp.phase = session->age;
858 sc->SCp.ptr = (char *)ctask;
860 atomic_set(&ctask->refcount, 1);
861 ctask->state = ISCSI_TASK_PENDING;
865 INIT_LIST_HEAD(&ctask->running);
867 list_add_tail(&ctask->running, &conn->xmitqueue);
868 spin_unlock(&session->lock);
870 scsi_queue_work(host, &conn->xmitwork);
874 spin_unlock(&session->lock);
875 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
876 return SCSI_MLQUEUE_HOST_BUSY;
879 spin_unlock(&session->lock);
880 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
881 sc->cmnd[0], reason);
882 sc->result = (DID_NO_CONNECT << 16);
883 scsi_set_resid(sc, scsi_bufflen(sc));
887 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
889 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
891 if (depth > ISCSI_MAX_CMD_PER_LUN)
892 depth = ISCSI_MAX_CMD_PER_LUN;
893 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
894 return sdev->queue_depth;
896 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
898 static struct iscsi_mgmt_task *
899 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
900 char *data, uint32_t data_size)
902 struct iscsi_session *session = conn->session;
903 struct iscsi_mgmt_task *mtask;
905 if (session->state == ISCSI_STATE_TERMINATE)
908 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
909 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
911 * Login and Text are sent serially, in
912 * request-followed-by-response sequence.
913 * Same mtask can be used. Same ITT must be used.
914 * Note that login_mtask is preallocated at conn_create().
916 mtask = conn->login_mtask;
918 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
919 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
921 if (!__kfifo_get(session->mgmtpool.queue,
922 (void*)&mtask, sizeof(void*)))
927 memcpy(mtask->data, data, data_size);
928 mtask->data_count = data_size;
930 mtask->data_count = 0;
932 INIT_LIST_HEAD(&mtask->running);
933 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
934 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
938 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
939 char *data, uint32_t data_size)
941 struct iscsi_conn *conn = cls_conn->dd_data;
942 struct iscsi_session *session = conn->session;
945 spin_lock_bh(&session->lock);
946 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
948 spin_unlock_bh(&session->lock);
949 scsi_queue_work(session->host, &conn->xmitwork);
952 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
954 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
956 struct iscsi_session *session = class_to_transport_session(cls_session);
957 struct iscsi_conn *conn = session->leadconn;
959 spin_lock_bh(&session->lock);
960 if (session->state != ISCSI_STATE_LOGGED_IN) {
961 session->state = ISCSI_STATE_RECOVERY_FAILED;
963 wake_up(&conn->ehwait);
965 spin_unlock_bh(&session->lock);
967 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
969 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
971 struct Scsi_Host *host = sc->device->host;
972 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
973 struct iscsi_conn *conn = session->leadconn;
974 int fail_session = 0;
976 spin_lock_bh(&session->lock);
977 if (session->state == ISCSI_STATE_TERMINATE) {
979 debug_scsi("failing host reset: session terminated "
980 "[CID %d age %d]\n", conn->id, session->age);
981 spin_unlock_bh(&session->lock);
985 if (sc->SCp.phase == session->age) {
986 debug_scsi("failing connection CID %d due to SCSI host reset\n",
990 spin_unlock_bh(&session->lock);
993 * we drop the lock here but the leadconn cannot be destoyed while
994 * we are in the scsi eh
997 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
999 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1000 wait_event_interruptible(conn->ehwait,
1001 session->state == ISCSI_STATE_TERMINATE ||
1002 session->state == ISCSI_STATE_LOGGED_IN ||
1003 session->state == ISCSI_STATE_RECOVERY_FAILED);
1004 if (signal_pending(current))
1005 flush_signals(current);
1007 spin_lock_bh(&session->lock);
1008 if (session->state == ISCSI_STATE_LOGGED_IN)
1009 printk(KERN_INFO "iscsi: host reset succeeded\n");
1012 spin_unlock_bh(&session->lock);
1016 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1018 static void iscsi_tmabort_timedout(unsigned long data)
1020 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1021 struct iscsi_conn *conn = ctask->conn;
1022 struct iscsi_session *session = conn->session;
1024 spin_lock(&session->lock);
1025 if (conn->tmabort_state == TMABORT_INITIAL) {
1026 conn->tmabort_state = TMABORT_TIMEDOUT;
1027 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1028 ctask->sc, ctask->itt);
1029 /* unblock eh_abort() */
1030 wake_up(&conn->ehwait);
1032 spin_unlock(&session->lock);
1035 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1036 struct iscsi_cmd_task *ctask)
1038 struct iscsi_conn *conn = ctask->conn;
1039 struct iscsi_session *session = conn->session;
1040 struct iscsi_tm *hdr = &conn->tmhdr;
1043 * ctask timed out but session is OK requests must be serialized.
1045 memset(hdr, 0, sizeof(struct iscsi_tm));
1046 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1047 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1048 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1049 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1050 hdr->rtt = ctask->hdr->itt;
1051 hdr->refcmdsn = ctask->hdr->cmdsn;
1053 ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1055 if (!ctask->mtask) {
1056 spin_unlock_bh(&session->lock);
1057 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1058 spin_lock_bh(&session->lock)
1059 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1062 ctask->state = ISCSI_TASK_ABORTING;
1064 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1066 if (conn->tmabort_state == TMABORT_INITIAL) {
1067 conn->tmfcmd_pdus_cnt++;
1068 conn->tmabort_timer.expires = 20*HZ + jiffies;
1069 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1070 conn->tmabort_timer.data = (unsigned long)ctask;
1071 add_timer(&conn->tmabort_timer);
1072 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
1074 spin_unlock_bh(&session->lock);
1075 mutex_unlock(&session->eh_mutex);
1076 scsi_queue_work(session->host, &conn->xmitwork);
1079 * block eh thread until:
1083 * 3) session is terminated or restarted or userspace has
1084 * given up on recovery
1086 wait_event_interruptible(conn->ehwait,
1087 sc->SCp.phase != session->age ||
1088 session->state != ISCSI_STATE_LOGGED_IN ||
1089 conn->tmabort_state != TMABORT_INITIAL);
1090 if (signal_pending(current))
1091 flush_signals(current);
1092 del_timer_sync(&conn->tmabort_timer);
1093 mutex_lock(&session->eh_mutex);
1094 spin_lock_bh(&session->lock);
1099 * session lock must be held
1101 static struct iscsi_mgmt_task *
1102 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1104 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1105 struct iscsi_mgmt_task *task;
1107 debug_scsi("searching %d tasks\n", nr_tasks);
1109 for (i = 0; i < nr_tasks; i++) {
1110 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1111 debug_scsi("check task %u\n", task->itt);
1113 if (task->itt == itt) {
1114 debug_scsi("matched task\n");
1118 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1123 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1125 struct iscsi_conn *conn = ctask->conn;
1126 struct iscsi_session *session = conn->session;
1131 if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
1132 list_del(&ctask->mtask->running);
1133 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1135 ctask->mtask = NULL;
1140 * session lock must be held
1142 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1145 struct scsi_cmnd *sc;
1151 if (ctask->state == ISCSI_TASK_PENDING)
1153 * cmd never made it to the xmit thread, so we should not count
1154 * the cmd in the sequencing
1156 conn->session->queued_cmdsn--;
1158 conn->session->tt->cleanup_cmd_task(conn, ctask);
1159 iscsi_ctask_mtask_cleanup(ctask);
1162 scsi_set_resid(sc, scsi_bufflen(sc));
1163 if (conn->ctask == ctask)
1165 /* release ref from queuecommand */
1166 __iscsi_put_ctask(ctask);
1169 static void iscsi_suspend_tx(struct iscsi_conn *conn)
1171 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1172 scsi_flush_work(conn->session->host);
1175 static void iscsi_start_tx(struct iscsi_conn *conn)
1177 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1178 scsi_queue_work(conn->session->host, &conn->xmitwork);
1181 int iscsi_eh_abort(struct scsi_cmnd *sc)
1183 struct Scsi_Host *host = sc->device->host;
1184 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1185 struct iscsi_cmd_task *ctask;
1186 struct iscsi_conn *conn;
1189 mutex_lock(&session->eh_mutex);
1190 spin_lock_bh(&session->lock);
1192 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1196 debug_scsi("sc never reached iscsi layer or it completed.\n");
1197 spin_unlock_bh(&session->lock);
1198 mutex_unlock(&session->eh_mutex);
1202 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1205 conn->eh_abort_cnt++;
1206 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1209 * If we are not logged in or we have started a new session
1210 * then let the host reset code handle this
1212 if (session->state != ISCSI_STATE_LOGGED_IN ||
1213 sc->SCp.phase != session->age)
1216 /* ctask completed before time out */
1218 debug_scsi("sc completed while abort in progress\n");
1222 /* what should we do here ? */
1223 if (conn->ctask == ctask) {
1224 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1225 "Failing abort\n", sc, ctask->itt);
1229 if (ctask->state == ISCSI_TASK_PENDING) {
1230 fail_command(conn, ctask, DID_ABORT << 16);
1234 conn->tmabort_state = TMABORT_INITIAL;
1235 rc = iscsi_exec_abort_task(sc, ctask);
1236 if (rc || sc->SCp.phase != session->age ||
1237 session->state != ISCSI_STATE_LOGGED_IN)
1239 iscsi_ctask_mtask_cleanup(ctask);
1241 switch (conn->tmabort_state) {
1242 case TMABORT_SUCCESS:
1243 spin_unlock_bh(&session->lock);
1244 iscsi_suspend_tx(conn);
1246 * clean up task if aborted. grab the recv lock as a writer
1248 write_lock_bh(conn->recv_lock);
1249 spin_lock(&session->lock);
1250 fail_command(conn, ctask, DID_ABORT << 16);
1251 spin_unlock(&session->lock);
1252 write_unlock_bh(conn->recv_lock);
1253 iscsi_start_tx(conn);
1254 goto success_unlocked;
1255 case TMABORT_NOT_FOUND:
1257 /* ctask completed before tmf abort response */
1258 debug_scsi("sc completed while abort in progress\n");
1263 /* timedout or failed */
1264 spin_unlock_bh(&session->lock);
1265 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1266 goto failed_unlocked;
1270 spin_unlock_bh(&session->lock);
1272 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1273 mutex_unlock(&session->eh_mutex);
1277 spin_unlock_bh(&session->lock);
1279 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1280 mutex_unlock(&session->eh_mutex);
1283 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1286 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1290 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1295 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1296 if (q->pool == NULL) {
1301 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1303 if (q->queue == ERR_PTR(-ENOMEM)) {
1309 for (i = 0; i < max; i++) {
1310 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1311 if (q->pool[i] == NULL) {
1314 for (j = 0; j < i; j++)
1317 kfifo_free(q->queue);
1322 memset(q->pool[i], 0, item_size);
1323 (*items)[i] = q->pool[i];
1324 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1328 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1330 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1334 for (i = 0; i < q->max; i++)
1339 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1342 * iSCSI Session's hostdata organization:
1344 * *------------------* <== hostdata_session(host->hostdata)
1345 * | ptr to class sess|
1346 * |------------------| <== iscsi_hostdata(host->hostdata)
1348 * *------------------*
1351 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1352 _sz % sizeof(unsigned long))
1354 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1357 * iscsi_session_setup - create iscsi cls session and host and session
1358 * @scsit: scsi transport template
1359 * @iscsit: iscsi transport template
1360 * @cmds_max: scsi host can queue
1361 * @qdepth: scsi host cmds per lun
1362 * @cmd_task_size: LLD ctask private data size
1363 * @mgmt_task_size: LLD mtask private data size
1364 * @initial_cmdsn: initial CmdSN
1365 * @hostno: host no allocated
1367 * This can be used by software iscsi_transports that allocate
1368 * a session per scsi host.
1370 struct iscsi_cls_session *
1371 iscsi_session_setup(struct iscsi_transport *iscsit,
1372 struct scsi_transport_template *scsit,
1373 uint16_t cmds_max, uint16_t qdepth,
1374 int cmd_task_size, int mgmt_task_size,
1375 uint32_t initial_cmdsn, uint32_t *hostno)
1377 struct Scsi_Host *shost;
1378 struct iscsi_session *session;
1379 struct iscsi_cls_session *cls_session;
1382 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1384 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1385 "Queue depth must be between 1 and %d.\n",
1386 qdepth, ISCSI_MAX_CMD_PER_LUN);
1387 qdepth = ISCSI_DEF_CMD_PER_LUN;
1390 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1391 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1393 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1394 "can_queue must be a power of 2 and between "
1395 "2 and %d - setting to %d.\n", cmds_max,
1396 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1397 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1400 shost = scsi_host_alloc(iscsit->host_template,
1401 hostdata_privsize(sizeof(*session)));
1405 /* the iscsi layer takes one task for reserve */
1406 shost->can_queue = cmds_max - 1;
1407 shost->cmd_per_lun = qdepth;
1409 shost->max_channel = 0;
1410 shost->max_lun = iscsit->max_lun;
1411 shost->max_cmd_len = iscsit->max_cmd_len;
1412 shost->transportt = scsit;
1413 shost->transportt->create_work_queue = 1;
1414 *hostno = shost->host_no;
1416 session = iscsi_hostdata(shost->hostdata);
1417 memset(session, 0, sizeof(struct iscsi_session));
1418 session->host = shost;
1419 session->state = ISCSI_STATE_FREE;
1420 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1421 session->cmds_max = cmds_max;
1422 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
1423 session->exp_cmdsn = initial_cmdsn + 1;
1424 session->max_cmdsn = initial_cmdsn + 1;
1425 session->max_r2t = 1;
1426 session->tt = iscsit;
1427 mutex_init(&session->eh_mutex);
1429 /* initialize SCSI PDU commands pool */
1430 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1431 (void***)&session->cmds,
1432 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1433 goto cmdpool_alloc_fail;
1435 /* pre-format cmds pool with ITT */
1436 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1437 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1440 ctask->dd_data = &ctask[1];
1442 INIT_LIST_HEAD(&ctask->running);
1445 spin_lock_init(&session->lock);
1447 /* initialize immediate command pool */
1448 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1449 (void***)&session->mgmt_cmds,
1450 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1451 goto mgmtpool_alloc_fail;
1454 /* pre-format immediate cmds pool with ITT */
1455 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1456 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1459 mtask->dd_data = &mtask[1];
1460 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1461 INIT_LIST_HEAD(&mtask->running);
1464 if (scsi_add_host(shost, NULL))
1467 if (!try_module_get(iscsit->owner))
1468 goto cls_session_fail;
1470 cls_session = iscsi_create_session(shost, iscsit, 0);
1473 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1478 module_put(iscsit->owner);
1480 scsi_remove_host(shost);
1482 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1483 mgmtpool_alloc_fail:
1484 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1486 scsi_host_put(shost);
1489 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1492 * iscsi_session_teardown - destroy session, host, and cls_session
1495 * This can be used by software iscsi_transports that allocate
1496 * a session per scsi host.
1498 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1500 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1501 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1502 struct module *owner = cls_session->transport->owner;
1504 iscsi_unblock_session(cls_session);
1505 scsi_remove_host(shost);
1507 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1508 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1510 kfree(session->password);
1511 kfree(session->password_in);
1512 kfree(session->username);
1513 kfree(session->username_in);
1514 kfree(session->targetname);
1515 kfree(session->netdev);
1516 kfree(session->hwaddress);
1517 kfree(session->initiatorname);
1519 iscsi_destroy_session(cls_session);
1520 scsi_host_put(shost);
1523 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1526 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1527 * @cls_session: iscsi_cls_session
1530 struct iscsi_cls_conn *
1531 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1533 struct iscsi_session *session = class_to_transport_session(cls_session);
1534 struct iscsi_conn *conn;
1535 struct iscsi_cls_conn *cls_conn;
1538 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1541 conn = cls_conn->dd_data;
1542 memset(conn, 0, sizeof(*conn));
1544 conn->session = session;
1545 conn->cls_conn = cls_conn;
1546 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1547 conn->id = conn_idx;
1548 conn->exp_statsn = 0;
1549 conn->tmabort_state = TMABORT_INITIAL;
1550 INIT_LIST_HEAD(&conn->run_list);
1551 INIT_LIST_HEAD(&conn->mgmt_run_list);
1552 INIT_LIST_HEAD(&conn->xmitqueue);
1554 /* initialize general immediate & non-immediate PDU commands queue */
1555 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1557 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1558 goto mgmtqueue_alloc_fail;
1560 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
1562 /* allocate login_mtask used for the login/text sequences */
1563 spin_lock_bh(&session->lock);
1564 if (!__kfifo_get(session->mgmtpool.queue,
1565 (void*)&conn->login_mtask,
1567 spin_unlock_bh(&session->lock);
1568 goto login_mtask_alloc_fail;
1570 spin_unlock_bh(&session->lock);
1572 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
1574 goto login_mtask_data_alloc_fail;
1575 conn->login_mtask->data = conn->data = data;
1577 init_timer(&conn->tmabort_timer);
1578 init_waitqueue_head(&conn->ehwait);
1582 login_mtask_data_alloc_fail:
1583 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1585 login_mtask_alloc_fail:
1586 kfifo_free(conn->mgmtqueue);
1587 mgmtqueue_alloc_fail:
1588 iscsi_destroy_conn(cls_conn);
1591 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1594 * iscsi_conn_teardown - teardown iscsi connection
1595 * cls_conn: iscsi class connection
1597 * TODO: we may need to make this into a two step process
1598 * like scsi-mls remove + put host
1600 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1602 struct iscsi_conn *conn = cls_conn->dd_data;
1603 struct iscsi_session *session = conn->session;
1604 unsigned long flags;
1606 spin_lock_bh(&session->lock);
1607 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1608 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1609 if (session->leadconn == conn) {
1611 * leading connection? then give up on recovery.
1613 session->state = ISCSI_STATE_TERMINATE;
1614 wake_up(&conn->ehwait);
1616 spin_unlock_bh(&session->lock);
1619 * Block until all in-progress commands for this connection
1623 spin_lock_irqsave(session->host->host_lock, flags);
1624 if (!session->host->host_busy) { /* OK for ERL == 0 */
1625 spin_unlock_irqrestore(session->host->host_lock, flags);
1628 spin_unlock_irqrestore(session->host->host_lock, flags);
1629 msleep_interruptible(500);
1630 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1631 "host_failed %d\n", session->host->host_busy,
1632 session->host->host_failed);
1634 * force eh_abort() to unblock
1636 wake_up(&conn->ehwait);
1639 /* flush queued up work because we free the connection below */
1640 scsi_flush_work(session->host);
1642 spin_lock_bh(&session->lock);
1644 kfree(conn->persistent_address);
1645 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1647 if (session->leadconn == conn)
1648 session->leadconn = NULL;
1649 spin_unlock_bh(&session->lock);
1651 kfifo_free(conn->mgmtqueue);
1653 iscsi_destroy_conn(cls_conn);
1655 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1657 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1659 struct iscsi_conn *conn = cls_conn->dd_data;
1660 struct iscsi_session *session = conn->session;
1663 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1667 if ((session->imm_data_en || !session->initial_r2t_en) &&
1668 session->first_burst > session->max_burst) {
1669 printk("iscsi: invalid burst lengths: "
1670 "first_burst %d max_burst %d\n",
1671 session->first_burst, session->max_burst);
1675 spin_lock_bh(&session->lock);
1676 conn->c_stage = ISCSI_CONN_STARTED;
1677 session->state = ISCSI_STATE_LOGGED_IN;
1678 session->queued_cmdsn = session->cmdsn;
1680 switch(conn->stop_stage) {
1681 case STOP_CONN_RECOVER:
1683 * unblock eh_abort() if it is blocked. re-try all
1684 * commands after successful recovery
1686 conn->stop_stage = 0;
1687 conn->tmabort_state = TMABORT_INITIAL;
1689 spin_unlock_bh(&session->lock);
1691 iscsi_unblock_session(session_to_cls(session));
1692 wake_up(&conn->ehwait);
1694 case STOP_CONN_TERM:
1695 conn->stop_stage = 0;
1700 spin_unlock_bh(&session->lock);
1704 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1707 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1709 struct iscsi_mgmt_task *mtask, *tmp;
1711 /* handle pending */
1712 while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1713 if (mtask == conn->login_mtask)
1715 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1716 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1720 /* handle running */
1721 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1722 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1723 list_del(&mtask->running);
1725 if (mtask == conn->login_mtask)
1727 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1734 /* Fail commands. Mutex and session lock held and recv side suspended */
1735 static void fail_all_commands(struct iscsi_conn *conn)
1737 struct iscsi_cmd_task *ctask, *tmp;
1740 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1741 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1743 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1746 /* fail all other running */
1747 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1748 debug_scsi("failing in progress sc %p itt 0x%x\n",
1749 ctask->sc, ctask->itt);
1750 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1756 static void iscsi_start_session_recovery(struct iscsi_session *session,
1757 struct iscsi_conn *conn, int flag)
1761 mutex_lock(&session->eh_mutex);
1762 spin_lock_bh(&session->lock);
1763 if (conn->stop_stage == STOP_CONN_TERM) {
1764 spin_unlock_bh(&session->lock);
1765 mutex_unlock(&session->eh_mutex);
1770 * The LLD either freed/unset the lock on us, or userspace called
1771 * stop but did not create a proper connection (connection was never
1772 * bound or it was unbound then stop was called).
1774 if (!conn->recv_lock) {
1775 spin_unlock_bh(&session->lock);
1776 mutex_unlock(&session->eh_mutex);
1781 * When this is called for the in_login state, we only want to clean
1782 * up the login task and connection. We do not need to block and set
1783 * the recovery state again
1785 if (flag == STOP_CONN_TERM)
1786 session->state = ISCSI_STATE_TERMINATE;
1787 else if (conn->stop_stage != STOP_CONN_RECOVER)
1788 session->state = ISCSI_STATE_IN_RECOVERY;
1790 old_stop_stage = conn->stop_stage;
1791 conn->stop_stage = flag;
1792 conn->c_stage = ISCSI_CONN_STOPPED;
1793 spin_unlock_bh(&session->lock);
1795 iscsi_suspend_tx(conn);
1797 write_lock_bh(conn->recv_lock);
1798 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1799 write_unlock_bh(conn->recv_lock);
1802 * for connection level recovery we should not calculate
1803 * header digest. conn->hdr_size used for optimization
1804 * in hdr_extract() and will be re-negotiated at
1807 if (flag == STOP_CONN_RECOVER) {
1808 conn->hdrdgst_en = 0;
1809 conn->datadgst_en = 0;
1810 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1811 old_stop_stage != STOP_CONN_RECOVER) {
1812 debug_scsi("blocking session\n");
1813 iscsi_block_session(session_to_cls(session));
1820 spin_lock_bh(&session->lock);
1821 fail_all_commands(conn);
1822 flush_control_queues(session, conn);
1823 spin_unlock_bh(&session->lock);
1824 mutex_unlock(&session->eh_mutex);
1827 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1829 struct iscsi_conn *conn = cls_conn->dd_data;
1830 struct iscsi_session *session = conn->session;
1833 case STOP_CONN_RECOVER:
1834 case STOP_CONN_TERM:
1835 iscsi_start_session_recovery(session, conn, flag);
1838 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1841 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1843 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1844 struct iscsi_cls_conn *cls_conn, int is_leading)
1846 struct iscsi_session *session = class_to_transport_session(cls_session);
1847 struct iscsi_conn *conn = cls_conn->dd_data;
1849 spin_lock_bh(&session->lock);
1851 session->leadconn = conn;
1852 spin_unlock_bh(&session->lock);
1855 * Unblock xmitworker(), Login Phase will pass through.
1857 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1858 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1861 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1864 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1865 enum iscsi_param param, char *buf, int buflen)
1867 struct iscsi_conn *conn = cls_conn->dd_data;
1868 struct iscsi_session *session = conn->session;
1872 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1873 sscanf(buf, "%d", &conn->max_recv_dlength);
1875 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1876 sscanf(buf, "%d", &conn->max_xmit_dlength);
1878 case ISCSI_PARAM_HDRDGST_EN:
1879 sscanf(buf, "%d", &conn->hdrdgst_en);
1881 case ISCSI_PARAM_DATADGST_EN:
1882 sscanf(buf, "%d", &conn->datadgst_en);
1884 case ISCSI_PARAM_INITIAL_R2T_EN:
1885 sscanf(buf, "%d", &session->initial_r2t_en);
1887 case ISCSI_PARAM_MAX_R2T:
1888 sscanf(buf, "%d", &session->max_r2t);
1890 case ISCSI_PARAM_IMM_DATA_EN:
1891 sscanf(buf, "%d", &session->imm_data_en);
1893 case ISCSI_PARAM_FIRST_BURST:
1894 sscanf(buf, "%d", &session->first_burst);
1896 case ISCSI_PARAM_MAX_BURST:
1897 sscanf(buf, "%d", &session->max_burst);
1899 case ISCSI_PARAM_PDU_INORDER_EN:
1900 sscanf(buf, "%d", &session->pdu_inorder_en);
1902 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1903 sscanf(buf, "%d", &session->dataseq_inorder_en);
1905 case ISCSI_PARAM_ERL:
1906 sscanf(buf, "%d", &session->erl);
1908 case ISCSI_PARAM_IFMARKER_EN:
1909 sscanf(buf, "%d", &value);
1912 case ISCSI_PARAM_OFMARKER_EN:
1913 sscanf(buf, "%d", &value);
1916 case ISCSI_PARAM_EXP_STATSN:
1917 sscanf(buf, "%u", &conn->exp_statsn);
1919 case ISCSI_PARAM_USERNAME:
1920 kfree(session->username);
1921 session->username = kstrdup(buf, GFP_KERNEL);
1922 if (!session->username)
1925 case ISCSI_PARAM_USERNAME_IN:
1926 kfree(session->username_in);
1927 session->username_in = kstrdup(buf, GFP_KERNEL);
1928 if (!session->username_in)
1931 case ISCSI_PARAM_PASSWORD:
1932 kfree(session->password);
1933 session->password = kstrdup(buf, GFP_KERNEL);
1934 if (!session->password)
1937 case ISCSI_PARAM_PASSWORD_IN:
1938 kfree(session->password_in);
1939 session->password_in = kstrdup(buf, GFP_KERNEL);
1940 if (!session->password_in)
1943 case ISCSI_PARAM_TARGET_NAME:
1944 /* this should not change between logins */
1945 if (session->targetname)
1948 session->targetname = kstrdup(buf, GFP_KERNEL);
1949 if (!session->targetname)
1952 case ISCSI_PARAM_TPGT:
1953 sscanf(buf, "%d", &session->tpgt);
1955 case ISCSI_PARAM_PERSISTENT_PORT:
1956 sscanf(buf, "%d", &conn->persistent_port);
1958 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1960 * this is the address returned in discovery so it should
1961 * not change between logins.
1963 if (conn->persistent_address)
1966 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1967 if (!conn->persistent_address)
1976 EXPORT_SYMBOL_GPL(iscsi_set_param);
1978 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1979 enum iscsi_param param, char *buf)
1981 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1982 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1986 case ISCSI_PARAM_INITIAL_R2T_EN:
1987 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1989 case ISCSI_PARAM_MAX_R2T:
1990 len = sprintf(buf, "%hu\n", session->max_r2t);
1992 case ISCSI_PARAM_IMM_DATA_EN:
1993 len = sprintf(buf, "%d\n", session->imm_data_en);
1995 case ISCSI_PARAM_FIRST_BURST:
1996 len = sprintf(buf, "%u\n", session->first_burst);
1998 case ISCSI_PARAM_MAX_BURST:
1999 len = sprintf(buf, "%u\n", session->max_burst);
2001 case ISCSI_PARAM_PDU_INORDER_EN:
2002 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2004 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2005 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2007 case ISCSI_PARAM_ERL:
2008 len = sprintf(buf, "%d\n", session->erl);
2010 case ISCSI_PARAM_TARGET_NAME:
2011 len = sprintf(buf, "%s\n", session->targetname);
2013 case ISCSI_PARAM_TPGT:
2014 len = sprintf(buf, "%d\n", session->tpgt);
2016 case ISCSI_PARAM_USERNAME:
2017 len = sprintf(buf, "%s\n", session->username);
2019 case ISCSI_PARAM_USERNAME_IN:
2020 len = sprintf(buf, "%s\n", session->username_in);
2022 case ISCSI_PARAM_PASSWORD:
2023 len = sprintf(buf, "%s\n", session->password);
2025 case ISCSI_PARAM_PASSWORD_IN:
2026 len = sprintf(buf, "%s\n", session->password_in);
2034 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2036 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2037 enum iscsi_param param, char *buf)
2039 struct iscsi_conn *conn = cls_conn->dd_data;
2043 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2044 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2046 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2047 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2049 case ISCSI_PARAM_HDRDGST_EN:
2050 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2052 case ISCSI_PARAM_DATADGST_EN:
2053 len = sprintf(buf, "%d\n", conn->datadgst_en);
2055 case ISCSI_PARAM_IFMARKER_EN:
2056 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2058 case ISCSI_PARAM_OFMARKER_EN:
2059 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2061 case ISCSI_PARAM_EXP_STATSN:
2062 len = sprintf(buf, "%u\n", conn->exp_statsn);
2064 case ISCSI_PARAM_PERSISTENT_PORT:
2065 len = sprintf(buf, "%d\n", conn->persistent_port);
2067 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2068 len = sprintf(buf, "%s\n", conn->persistent_address);
2076 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2078 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2081 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2085 case ISCSI_HOST_PARAM_NETDEV_NAME:
2086 if (!session->netdev)
2087 len = sprintf(buf, "%s\n", "default");
2089 len = sprintf(buf, "%s\n", session->netdev);
2091 case ISCSI_HOST_PARAM_HWADDRESS:
2092 if (!session->hwaddress)
2093 len = sprintf(buf, "%s\n", "default");
2095 len = sprintf(buf, "%s\n", session->hwaddress);
2097 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2098 if (!session->initiatorname)
2099 len = sprintf(buf, "%s\n", "unknown");
2101 len = sprintf(buf, "%s\n", session->initiatorname);
2110 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2112 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2113 char *buf, int buflen)
2115 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2118 case ISCSI_HOST_PARAM_NETDEV_NAME:
2119 if (!session->netdev)
2120 session->netdev = kstrdup(buf, GFP_KERNEL);
2122 case ISCSI_HOST_PARAM_HWADDRESS:
2123 if (!session->hwaddress)
2124 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2126 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2127 if (!session->initiatorname)
2128 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2136 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2138 MODULE_AUTHOR("Mike Christie");
2139 MODULE_DESCRIPTION("iSCSI library functions");
2140 MODULE_LICENSE("GPL");