Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / scsi / libiscsi.c
blob9975095b46bc1d5bc62d798e07dd2f128613f833
1 /*
2 * iSCSI lib functions
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <asm/unaligned.h>
29 #include <net/tcp.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi.h>
36 #include <scsi/iscsi_proto.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_iscsi.h>
39 #include <scsi/libiscsi.h>
41 struct iscsi_session *
42 class_to_transport_session(struct iscsi_cls_session *cls_session)
44 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
45 return iscsi_hostdata(shost->hostdata);
47 EXPORT_SYMBOL_GPL(class_to_transport_session);
49 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
50 #define SNA32_CHECK 2147483648UL
52 static int iscsi_sna_lt(u32 n1, u32 n2)
54 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
55 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
58 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
59 static int iscsi_sna_lte(u32 n1, u32 n2)
61 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
62 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
65 void
66 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
68 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
69 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
72 * standard specifies this check for when to update expected and
73 * max sequence numbers
75 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
76 return;
78 if (exp_cmdsn != session->exp_cmdsn &&
79 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
80 session->exp_cmdsn = exp_cmdsn;
82 if (max_cmdsn != session->max_cmdsn &&
83 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
84 session->max_cmdsn = max_cmdsn;
86 * if the window closed with IO queued, then kick the
87 * xmit thread
89 if (!list_empty(&session->leadconn->xmitqueue) ||
90 !list_empty(&session->leadconn->mgmtqueue))
91 scsi_queue_work(session->host,
92 &session->leadconn->xmitwork);
95 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
97 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
98 struct iscsi_data *hdr)
100 struct iscsi_conn *conn = ctask->conn;
102 memset(hdr, 0, sizeof(struct iscsi_data));
103 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
104 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
105 ctask->unsol_datasn++;
106 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
107 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
109 hdr->itt = ctask->hdr->itt;
110 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
111 hdr->offset = cpu_to_be32(ctask->unsol_offset);
113 if (ctask->unsol_count > conn->max_xmit_dlength) {
114 hton24(hdr->dlength, conn->max_xmit_dlength);
115 ctask->data_count = conn->max_xmit_dlength;
116 ctask->unsol_offset += ctask->data_count;
117 hdr->flags = 0;
118 } else {
119 hton24(hdr->dlength, ctask->unsol_count);
120 ctask->data_count = ctask->unsol_count;
121 hdr->flags = ISCSI_FLAG_CMD_FINAL;
124 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
126 static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
128 unsigned exp_len = ctask->hdr_len + len;
130 if (exp_len > ctask->hdr_max) {
131 WARN_ON(1);
132 return -EINVAL;
135 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
136 ctask->hdr_len = exp_len;
137 return 0;
141 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
142 * @ctask: iscsi cmd task
144 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
145 * fields like dlength or final based on how much data it sends
147 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
149 struct iscsi_conn *conn = ctask->conn;
150 struct iscsi_session *session = conn->session;
151 struct iscsi_cmd *hdr = ctask->hdr;
152 struct scsi_cmnd *sc = ctask->sc;
153 unsigned hdrlength;
154 int rc;
156 ctask->hdr_len = 0;
157 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
158 if (rc)
159 return rc;
160 hdr->opcode = ISCSI_OP_SCSI_CMD;
161 hdr->flags = ISCSI_ATTR_SIMPLE;
162 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
163 hdr->itt = build_itt(ctask->itt, session->age);
164 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
165 hdr->cmdsn = cpu_to_be32(session->cmdsn);
166 session->cmdsn++;
167 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
168 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
169 if (sc->cmd_len < MAX_COMMAND_SIZE)
170 memset(&hdr->cdb[sc->cmd_len], 0,
171 MAX_COMMAND_SIZE - sc->cmd_len);
173 ctask->imm_count = 0;
174 if (sc->sc_data_direction == DMA_TO_DEVICE) {
175 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
177 * Write counters:
179 * imm_count bytes to be sent right after
180 * SCSI PDU Header
182 * unsol_count bytes(as Data-Out) to be sent
183 * without R2T ack right after
184 * immediate data
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);
198 else
199 ctask->imm_count = min(scsi_bufflen(sc),
200 conn->max_xmit_dlength);
201 hton24(hdr->dlength, ctask->imm_count);
202 } else
203 zero_data(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 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
214 } else {
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 if (conn->session->tt->init_cmd_task(conn->ctask))
232 return EIO;
234 conn->scsicmd_pdus_cnt++;
235 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
236 "cmdsn %d win %d]\n",
237 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
238 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
239 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
240 return 0;
244 * iscsi_complete_command - return command back to scsi-ml
245 * @ctask: iscsi cmd task
247 * Must be called with session lock.
248 * This function returns the scsi command to scsi-ml and returns
249 * the cmd task to the pool of available cmd tasks.
251 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
253 struct iscsi_conn *conn = ctask->conn;
254 struct iscsi_session *session = conn->session;
255 struct scsi_cmnd *sc = ctask->sc;
257 ctask->state = ISCSI_TASK_COMPLETED;
258 ctask->sc = NULL;
259 /* SCSI eh reuses commands to verify us */
260 sc->SCp.ptr = NULL;
261 if (conn->ctask == ctask)
262 conn->ctask = NULL;
263 list_del_init(&ctask->running);
264 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
265 sc->scsi_done(sc);
268 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
270 atomic_inc(&ctask->refcount);
273 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
275 if (atomic_dec_and_test(&ctask->refcount))
276 iscsi_complete_command(ctask);
280 * session lock must be held
282 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 int err)
285 struct scsi_cmnd *sc;
287 sc = ctask->sc;
288 if (!sc)
289 return;
291 if (ctask->state == ISCSI_TASK_PENDING)
293 * cmd never made it to the xmit thread, so we should not count
294 * the cmd in the sequencing
296 conn->session->queued_cmdsn--;
297 else
298 conn->session->tt->cleanup_cmd_task(conn, ctask);
300 sc->result = err;
301 scsi_set_resid(sc, scsi_bufflen(sc));
302 if (conn->ctask == ctask)
303 conn->ctask = NULL;
304 /* release ref from queuecommand */
305 __iscsi_put_ctask(ctask);
309 * iscsi_free_mgmt_task - return mgmt task back to pool
310 * @conn: iscsi connection
311 * @mtask: mtask
313 * Must be called with session lock.
315 void iscsi_free_mgmt_task(struct iscsi_conn *conn,
316 struct iscsi_mgmt_task *mtask)
318 list_del_init(&mtask->running);
319 if (conn->login_mtask == mtask)
320 return;
322 if (conn->ping_mtask == mtask)
323 conn->ping_mtask = NULL;
324 __kfifo_put(conn->session->mgmtpool.queue,
325 (void*)&mtask, sizeof(void*));
327 EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
329 static struct iscsi_mgmt_task *
330 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
331 char *data, uint32_t data_size)
333 struct iscsi_session *session = conn->session;
334 struct iscsi_mgmt_task *mtask;
336 if (session->state == ISCSI_STATE_TERMINATE)
337 return NULL;
339 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
340 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
342 * Login and Text are sent serially, in
343 * request-followed-by-response sequence.
344 * Same mtask can be used. Same ITT must be used.
345 * Note that login_mtask is preallocated at conn_create().
347 mtask = conn->login_mtask;
348 else {
349 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
350 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
352 if (!__kfifo_get(session->mgmtpool.queue,
353 (void*)&mtask, sizeof(void*)))
354 return NULL;
357 if (data_size) {
358 memcpy(mtask->data, data, data_size);
359 mtask->data_count = data_size;
360 } else
361 mtask->data_count = 0;
363 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
364 INIT_LIST_HEAD(&mtask->running);
365 list_add_tail(&mtask->running, &conn->mgmtqueue);
366 return mtask;
369 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
370 char *data, uint32_t data_size)
372 struct iscsi_conn *conn = cls_conn->dd_data;
373 struct iscsi_session *session = conn->session;
374 int err = 0;
376 spin_lock_bh(&session->lock);
377 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
378 err = -EPERM;
379 spin_unlock_bh(&session->lock);
380 scsi_queue_work(session->host, &conn->xmitwork);
381 return err;
383 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
386 * iscsi_cmd_rsp - SCSI Command Response processing
387 * @conn: iscsi connection
388 * @hdr: iscsi header
389 * @ctask: scsi command task
390 * @data: cmd data buffer
391 * @datalen: len of buffer
393 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
394 * then completes the command and task.
396 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
397 struct iscsi_cmd_task *ctask, char *data,
398 int datalen)
400 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
401 struct iscsi_session *session = conn->session;
402 struct scsi_cmnd *sc = ctask->sc;
404 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
405 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
407 sc->result = (DID_OK << 16) | rhdr->cmd_status;
409 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
410 sc->result = DID_ERROR << 16;
411 goto out;
414 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
415 uint16_t senselen;
417 if (datalen < 2) {
418 invalid_datalen:
419 iscsi_conn_printk(KERN_ERR, conn,
420 "Got CHECK_CONDITION but invalid data "
421 "buffer size of %d\n", datalen);
422 sc->result = DID_BAD_TARGET << 16;
423 goto out;
426 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
427 if (datalen < senselen)
428 goto invalid_datalen;
430 memcpy(sc->sense_buffer, data + 2,
431 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
432 debug_scsi("copied %d bytes of sense\n",
433 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
436 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
437 ISCSI_FLAG_CMD_OVERFLOW)) {
438 int res_count = be32_to_cpu(rhdr->residual_count);
440 if (res_count > 0 &&
441 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
442 res_count <= scsi_bufflen(sc)))
443 scsi_set_resid(sc, res_count);
444 else
445 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
446 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
447 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
448 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
450 out:
451 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
452 (long)sc, sc->result, ctask->itt);
453 conn->scsirsp_pdus_cnt++;
455 __iscsi_put_ctask(ctask);
458 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
460 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
462 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
463 conn->tmfrsp_pdus_cnt++;
465 if (conn->tmf_state != TMF_QUEUED)
466 return;
468 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
469 conn->tmf_state = TMF_SUCCESS;
470 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
471 conn->tmf_state = TMF_NOT_FOUND;
472 else
473 conn->tmf_state = TMF_FAILED;
474 wake_up(&conn->ehwait);
477 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
479 struct iscsi_nopout hdr;
480 struct iscsi_mgmt_task *mtask;
482 if (!rhdr && conn->ping_mtask)
483 return;
485 memset(&hdr, 0, sizeof(struct iscsi_nopout));
486 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
487 hdr.flags = ISCSI_FLAG_CMD_FINAL;
489 if (rhdr) {
490 memcpy(hdr.lun, rhdr->lun, 8);
491 hdr.ttt = rhdr->ttt;
492 hdr.itt = RESERVED_ITT;
493 } else
494 hdr.ttt = RESERVED_ITT;
496 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
497 if (!mtask) {
498 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
499 return;
502 /* only track our nops */
503 if (!rhdr) {
504 conn->ping_mtask = mtask;
505 conn->last_ping = jiffies;
507 scsi_queue_work(conn->session->host, &conn->xmitwork);
510 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
511 char *data, int datalen)
513 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
514 struct iscsi_hdr rejected_pdu;
515 uint32_t itt;
517 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
519 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
520 if (ntoh24(reject->dlength) > datalen)
521 return ISCSI_ERR_PROTO;
523 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
524 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
525 itt = get_itt(rejected_pdu.itt);
526 iscsi_conn_printk(KERN_ERR, conn,
527 "itt 0x%x had pdu (op 0x%x) rejected "
528 "due to DataDigest error.\n", itt,
529 rejected_pdu.opcode);
532 return 0;
536 * __iscsi_complete_pdu - complete pdu
537 * @conn: iscsi conn
538 * @hdr: iscsi header
539 * @data: data buffer
540 * @datalen: len of data buffer
542 * Completes pdu processing by freeing any resources allocated at
543 * queuecommand or send generic. session lock must be held and verify
544 * itt must have been called.
546 static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
547 char *data, int datalen)
549 struct iscsi_session *session = conn->session;
550 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
551 struct iscsi_cmd_task *ctask;
552 struct iscsi_mgmt_task *mtask;
553 uint32_t itt;
555 conn->last_recv = jiffies;
556 if (hdr->itt != RESERVED_ITT)
557 itt = get_itt(hdr->itt);
558 else
559 itt = ~0U;
561 if (itt < session->cmds_max) {
562 ctask = session->cmds[itt];
564 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
565 opcode, conn->id, ctask->itt, datalen);
567 switch(opcode) {
568 case ISCSI_OP_SCSI_CMD_RSP:
569 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
570 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
571 datalen);
572 break;
573 case ISCSI_OP_SCSI_DATA_IN:
574 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
575 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
576 conn->scsirsp_pdus_cnt++;
577 __iscsi_put_ctask(ctask);
579 break;
580 case ISCSI_OP_R2T:
581 /* LLD handles this for now */
582 break;
583 default:
584 rc = ISCSI_ERR_BAD_OPCODE;
585 break;
587 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
588 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
589 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
591 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
592 opcode, conn->id, mtask->itt, datalen);
594 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
595 switch(opcode) {
596 case ISCSI_OP_LOGOUT_RSP:
597 if (datalen) {
598 rc = ISCSI_ERR_PROTO;
599 break;
601 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
602 /* fall through */
603 case ISCSI_OP_LOGIN_RSP:
604 case ISCSI_OP_TEXT_RSP:
606 * login related PDU's exp_statsn is handled in
607 * userspace
609 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
610 rc = ISCSI_ERR_CONN_FAILED;
611 iscsi_free_mgmt_task(conn, mtask);
612 break;
613 case ISCSI_OP_SCSI_TMFUNC_RSP:
614 if (datalen) {
615 rc = ISCSI_ERR_PROTO;
616 break;
619 iscsi_tmf_rsp(conn, hdr);
620 iscsi_free_mgmt_task(conn, mtask);
621 break;
622 case ISCSI_OP_NOOP_IN:
623 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
624 datalen) {
625 rc = ISCSI_ERR_PROTO;
626 break;
628 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
630 if (conn->ping_mtask != mtask) {
632 * If this is not in response to one of our
633 * nops then it must be from userspace.
635 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
636 datalen))
637 rc = ISCSI_ERR_CONN_FAILED;
638 } else
639 mod_timer(&conn->transport_timer,
640 jiffies + conn->recv_timeout);
641 iscsi_free_mgmt_task(conn, mtask);
642 break;
643 default:
644 rc = ISCSI_ERR_BAD_OPCODE;
645 break;
647 } else if (itt == ~0U) {
648 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
650 switch(opcode) {
651 case ISCSI_OP_NOOP_IN:
652 if (datalen) {
653 rc = ISCSI_ERR_PROTO;
654 break;
657 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
658 break;
660 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
661 break;
662 case ISCSI_OP_REJECT:
663 rc = iscsi_handle_reject(conn, hdr, data, datalen);
664 break;
665 case ISCSI_OP_ASYNC_EVENT:
666 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
667 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
668 rc = ISCSI_ERR_CONN_FAILED;
669 break;
670 default:
671 rc = ISCSI_ERR_BAD_OPCODE;
672 break;
674 } else
675 rc = ISCSI_ERR_BAD_ITT;
677 return rc;
680 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
681 char *data, int datalen)
683 int rc;
685 spin_lock(&conn->session->lock);
686 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
687 spin_unlock(&conn->session->lock);
688 return rc;
690 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
692 /* verify itt (itt encoding: age+cid+itt) */
693 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
694 uint32_t *ret_itt)
696 struct iscsi_session *session = conn->session;
697 struct iscsi_cmd_task *ctask;
698 uint32_t itt;
700 if (hdr->itt != RESERVED_ITT) {
701 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
702 (session->age << ISCSI_AGE_SHIFT)) {
703 iscsi_conn_printk(KERN_ERR, conn,
704 "received itt %x expected session "
705 "age (%x)\n", (__force u32)hdr->itt,
706 session->age & ISCSI_AGE_MASK);
707 return ISCSI_ERR_BAD_ITT;
710 itt = get_itt(hdr->itt);
711 } else
712 itt = ~0U;
714 if (itt < session->cmds_max) {
715 ctask = session->cmds[itt];
717 if (!ctask->sc) {
718 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
719 "with itt 0x%x\n", ctask->itt);
720 /* force drop */
721 return ISCSI_ERR_NO_SCSI_CMD;
724 if (ctask->sc->SCp.phase != session->age) {
725 iscsi_conn_printk(KERN_ERR, conn,
726 "iscsi: ctask's session age %d, "
727 "expected %d\n", ctask->sc->SCp.phase,
728 session->age);
729 return ISCSI_ERR_SESSION_FAILED;
733 *ret_itt = itt;
734 return 0;
736 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
738 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
740 struct iscsi_session *session = conn->session;
741 unsigned long flags;
743 spin_lock_irqsave(&session->lock, flags);
744 if (session->state == ISCSI_STATE_FAILED) {
745 spin_unlock_irqrestore(&session->lock, flags);
746 return;
749 if (conn->stop_stage == 0)
750 session->state = ISCSI_STATE_FAILED;
751 spin_unlock_irqrestore(&session->lock, flags);
752 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
753 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
754 iscsi_conn_error(conn->cls_conn, err);
756 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
758 static void iscsi_prep_mtask(struct iscsi_conn *conn,
759 struct iscsi_mgmt_task *mtask)
761 struct iscsi_session *session = conn->session;
762 struct iscsi_hdr *hdr = mtask->hdr;
763 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
765 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
766 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
767 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
769 * pre-format CmdSN for outgoing PDU.
771 nop->cmdsn = cpu_to_be32(session->cmdsn);
772 if (hdr->itt != RESERVED_ITT) {
773 hdr->itt = build_itt(mtask->itt, session->age);
775 * TODO: We always use immediate, so we never hit this.
776 * If we start to send tmfs or nops as non-immediate then
777 * we should start checking the cmdsn numbers for mgmt tasks.
779 if (conn->c_stage == ISCSI_CONN_STARTED &&
780 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
781 session->queued_cmdsn++;
782 session->cmdsn++;
786 if (session->tt->init_mgmt_task)
787 session->tt->init_mgmt_task(conn, mtask);
789 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
790 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
791 mtask->data_count);
794 static int iscsi_xmit_mtask(struct iscsi_conn *conn)
796 struct iscsi_hdr *hdr = conn->mtask->hdr;
797 int rc;
799 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
800 conn->session->state = ISCSI_STATE_LOGGING_OUT;
801 spin_unlock_bh(&conn->session->lock);
803 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
804 spin_lock_bh(&conn->session->lock);
805 if (rc)
806 return rc;
808 /* done with this in-progress mtask */
809 conn->mtask = NULL;
810 return 0;
813 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
815 struct iscsi_session *session = conn->session;
818 * Check for iSCSI window and take care of CmdSN wrap-around
820 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
821 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
822 "CmdSN %u/%u\n", session->exp_cmdsn,
823 session->max_cmdsn, session->cmdsn,
824 session->queued_cmdsn);
825 return -ENOSPC;
827 return 0;
830 static int iscsi_xmit_ctask(struct iscsi_conn *conn)
832 struct iscsi_cmd_task *ctask = conn->ctask;
833 int rc;
835 __iscsi_get_ctask(ctask);
836 spin_unlock_bh(&conn->session->lock);
837 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
838 spin_lock_bh(&conn->session->lock);
839 __iscsi_put_ctask(ctask);
840 if (!rc)
841 /* done with this ctask */
842 conn->ctask = NULL;
843 return rc;
847 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
848 * @ctask: ctask to requeue
850 * LLDs that need to run a ctask from the session workqueue should call
851 * this. The session lock must be held.
853 void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
855 struct iscsi_conn *conn = ctask->conn;
857 list_move_tail(&ctask->running, &conn->requeue);
858 scsi_queue_work(conn->session->host, &conn->xmitwork);
860 EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
863 * iscsi_data_xmit - xmit any command into the scheduled connection
864 * @conn: iscsi connection
866 * Notes:
867 * The function can return -EAGAIN in which case the caller must
868 * re-schedule it again later or recover. '0' return code means
869 * successful xmit.
871 static int iscsi_data_xmit(struct iscsi_conn *conn)
873 int rc = 0;
875 spin_lock_bh(&conn->session->lock);
876 if (unlikely(conn->suspend_tx)) {
877 debug_scsi("conn %d Tx suspended!\n", conn->id);
878 spin_unlock_bh(&conn->session->lock);
879 return -ENODATA;
882 if (conn->ctask) {
883 rc = iscsi_xmit_ctask(conn);
884 if (rc)
885 goto again;
888 if (conn->mtask) {
889 rc = iscsi_xmit_mtask(conn);
890 if (rc)
891 goto again;
895 * process mgmt pdus like nops before commands since we should
896 * only have one nop-out as a ping from us and targets should not
897 * overflow us with nop-ins
899 check_mgmt:
900 while (!list_empty(&conn->mgmtqueue)) {
901 conn->mtask = list_entry(conn->mgmtqueue.next,
902 struct iscsi_mgmt_task, running);
903 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
904 iscsi_free_mgmt_task(conn, conn->mtask);
905 conn->mtask = NULL;
906 continue;
909 iscsi_prep_mtask(conn, conn->mtask);
910 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
911 rc = iscsi_xmit_mtask(conn);
912 if (rc)
913 goto again;
916 /* process pending command queue */
917 while (!list_empty(&conn->xmitqueue)) {
918 if (conn->tmf_state == TMF_QUEUED)
919 break;
921 conn->ctask = list_entry(conn->xmitqueue.next,
922 struct iscsi_cmd_task, running);
923 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
924 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
925 continue;
927 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
928 fail_command(conn, conn->ctask, DID_ABORT << 16);
929 continue;
932 conn->ctask->state = ISCSI_TASK_RUNNING;
933 list_move_tail(conn->xmitqueue.next, &conn->run_list);
934 rc = iscsi_xmit_ctask(conn);
935 if (rc)
936 goto again;
938 * we could continuously get new ctask requests so
939 * we need to check the mgmt queue for nops that need to
940 * be sent to aviod starvation
942 if (!list_empty(&conn->mgmtqueue))
943 goto check_mgmt;
946 while (!list_empty(&conn->requeue)) {
947 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
948 break;
951 * we always do fastlogout - conn stop code will clean up.
953 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
954 break;
956 conn->ctask = list_entry(conn->requeue.next,
957 struct iscsi_cmd_task, running);
958 conn->ctask->state = ISCSI_TASK_RUNNING;
959 list_move_tail(conn->requeue.next, &conn->run_list);
960 rc = iscsi_xmit_ctask(conn);
961 if (rc)
962 goto again;
963 if (!list_empty(&conn->mgmtqueue))
964 goto check_mgmt;
966 spin_unlock_bh(&conn->session->lock);
967 return -ENODATA;
969 again:
970 if (unlikely(conn->suspend_tx))
971 rc = -ENODATA;
972 spin_unlock_bh(&conn->session->lock);
973 return rc;
976 static void iscsi_xmitworker(struct work_struct *work)
978 struct iscsi_conn *conn =
979 container_of(work, struct iscsi_conn, xmitwork);
980 int rc;
982 * serialize Xmit worker on a per-connection basis.
984 do {
985 rc = iscsi_data_xmit(conn);
986 } while (rc >= 0 || rc == -EAGAIN);
989 enum {
990 FAILURE_BAD_HOST = 1,
991 FAILURE_SESSION_FAILED,
992 FAILURE_SESSION_FREED,
993 FAILURE_WINDOW_CLOSED,
994 FAILURE_OOM,
995 FAILURE_SESSION_TERMINATE,
996 FAILURE_SESSION_IN_RECOVERY,
997 FAILURE_SESSION_RECOVERY_TIMEOUT,
998 FAILURE_SESSION_LOGGING_OUT,
999 FAILURE_SESSION_NOT_READY,
1002 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1004 struct Scsi_Host *host;
1005 int reason = 0;
1006 struct iscsi_session *session;
1007 struct iscsi_conn *conn;
1008 struct iscsi_cmd_task *ctask = NULL;
1010 sc->scsi_done = done;
1011 sc->result = 0;
1012 sc->SCp.ptr = NULL;
1014 host = sc->device->host;
1015 spin_unlock(host->host_lock);
1017 session = iscsi_hostdata(host->hostdata);
1018 spin_lock(&session->lock);
1020 reason = iscsi_session_chkready(session_to_cls(session));
1021 if (reason) {
1022 sc->result = reason;
1023 goto fault;
1027 * ISCSI_STATE_FAILED is a temp. state. The recovery
1028 * code will decide what is best to do with command queued
1029 * during this time
1031 if (session->state != ISCSI_STATE_LOGGED_IN &&
1032 session->state != ISCSI_STATE_FAILED) {
1034 * to handle the race between when we set the recovery state
1035 * and block the session we requeue here (commands could
1036 * be entering our queuecommand while a block is starting
1037 * up because the block code is not locked)
1039 switch (session->state) {
1040 case ISCSI_STATE_IN_RECOVERY:
1041 reason = FAILURE_SESSION_IN_RECOVERY;
1042 sc->result = DID_IMM_RETRY << 16;
1043 break;
1044 case ISCSI_STATE_LOGGING_OUT:
1045 reason = FAILURE_SESSION_LOGGING_OUT;
1046 sc->result = DID_IMM_RETRY << 16;
1047 break;
1048 case ISCSI_STATE_RECOVERY_FAILED:
1049 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1050 sc->result = DID_NO_CONNECT << 16;
1051 break;
1052 case ISCSI_STATE_TERMINATE:
1053 reason = FAILURE_SESSION_TERMINATE;
1054 sc->result = DID_NO_CONNECT << 16;
1055 break;
1056 default:
1057 reason = FAILURE_SESSION_FREED;
1058 sc->result = DID_NO_CONNECT << 16;
1060 goto fault;
1063 conn = session->leadconn;
1064 if (!conn) {
1065 reason = FAILURE_SESSION_FREED;
1066 sc->result = DID_NO_CONNECT << 16;
1067 goto fault;
1070 if (iscsi_check_cmdsn_window_closed(conn)) {
1071 reason = FAILURE_WINDOW_CLOSED;
1072 goto reject;
1075 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1076 sizeof(void*))) {
1077 reason = FAILURE_OOM;
1078 goto reject;
1080 session->queued_cmdsn++;
1082 sc->SCp.phase = session->age;
1083 sc->SCp.ptr = (char *)ctask;
1085 atomic_set(&ctask->refcount, 1);
1086 ctask->state = ISCSI_TASK_PENDING;
1087 ctask->conn = conn;
1088 ctask->sc = sc;
1089 INIT_LIST_HEAD(&ctask->running);
1091 list_add_tail(&ctask->running, &conn->xmitqueue);
1092 spin_unlock(&session->lock);
1094 scsi_queue_work(host, &conn->xmitwork);
1095 spin_lock(host->host_lock);
1096 return 0;
1098 reject:
1099 spin_unlock(&session->lock);
1100 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1101 spin_lock(host->host_lock);
1102 return SCSI_MLQUEUE_HOST_BUSY;
1104 fault:
1105 spin_unlock(&session->lock);
1106 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
1107 scsi_set_resid(sc, scsi_bufflen(sc));
1108 sc->scsi_done(sc);
1109 spin_lock(host->host_lock);
1110 return 0;
1112 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1114 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1116 if (depth > ISCSI_MAX_CMD_PER_LUN)
1117 depth = ISCSI_MAX_CMD_PER_LUN;
1118 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1119 return sdev->queue_depth;
1121 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1123 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1125 struct iscsi_session *session = class_to_transport_session(cls_session);
1127 spin_lock_bh(&session->lock);
1128 if (session->state != ISCSI_STATE_LOGGED_IN) {
1129 session->state = ISCSI_STATE_RECOVERY_FAILED;
1130 if (session->leadconn)
1131 wake_up(&session->leadconn->ehwait);
1133 spin_unlock_bh(&session->lock);
1135 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1137 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1139 struct Scsi_Host *host = sc->device->host;
1140 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1141 struct iscsi_conn *conn = session->leadconn;
1143 mutex_lock(&session->eh_mutex);
1144 spin_lock_bh(&session->lock);
1145 if (session->state == ISCSI_STATE_TERMINATE) {
1146 failed:
1147 debug_scsi("failing host reset: session terminated "
1148 "[CID %d age %d]\n", conn->id, session->age);
1149 spin_unlock_bh(&session->lock);
1150 mutex_unlock(&session->eh_mutex);
1151 return FAILED;
1154 spin_unlock_bh(&session->lock);
1155 mutex_unlock(&session->eh_mutex);
1157 * we drop the lock here but the leadconn cannot be destoyed while
1158 * we are in the scsi eh
1160 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1162 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1163 wait_event_interruptible(conn->ehwait,
1164 session->state == ISCSI_STATE_TERMINATE ||
1165 session->state == ISCSI_STATE_LOGGED_IN ||
1166 session->state == ISCSI_STATE_RECOVERY_FAILED);
1167 if (signal_pending(current))
1168 flush_signals(current);
1170 mutex_lock(&session->eh_mutex);
1171 spin_lock_bh(&session->lock);
1172 if (session->state == ISCSI_STATE_LOGGED_IN)
1173 iscsi_session_printk(KERN_INFO, session,
1174 "host reset succeeded\n");
1175 else
1176 goto failed;
1177 spin_unlock_bh(&session->lock);
1178 mutex_unlock(&session->eh_mutex);
1179 return SUCCESS;
1181 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1183 static void iscsi_tmf_timedout(unsigned long data)
1185 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1186 struct iscsi_session *session = conn->session;
1188 spin_lock(&session->lock);
1189 if (conn->tmf_state == TMF_QUEUED) {
1190 conn->tmf_state = TMF_TIMEDOUT;
1191 debug_scsi("tmf timedout\n");
1192 /* unblock eh_abort() */
1193 wake_up(&conn->ehwait);
1195 spin_unlock(&session->lock);
1198 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1199 struct iscsi_tm *hdr, int age,
1200 int timeout)
1202 struct iscsi_session *session = conn->session;
1203 struct iscsi_mgmt_task *mtask;
1205 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1206 NULL, 0);
1207 if (!mtask) {
1208 spin_unlock_bh(&session->lock);
1209 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1210 spin_lock_bh(&session->lock);
1211 debug_scsi("tmf exec failure\n");
1212 return -EPERM;
1214 conn->tmfcmd_pdus_cnt++;
1215 conn->tmf_timer.expires = timeout * HZ + jiffies;
1216 conn->tmf_timer.function = iscsi_tmf_timedout;
1217 conn->tmf_timer.data = (unsigned long)conn;
1218 add_timer(&conn->tmf_timer);
1219 debug_scsi("tmf set timeout\n");
1221 spin_unlock_bh(&session->lock);
1222 mutex_unlock(&session->eh_mutex);
1223 scsi_queue_work(session->host, &conn->xmitwork);
1226 * block eh thread until:
1228 * 1) tmf response
1229 * 2) tmf timeout
1230 * 3) session is terminated or restarted or userspace has
1231 * given up on recovery
1233 wait_event_interruptible(conn->ehwait, age != session->age ||
1234 session->state != ISCSI_STATE_LOGGED_IN ||
1235 conn->tmf_state != TMF_QUEUED);
1236 if (signal_pending(current))
1237 flush_signals(current);
1238 del_timer_sync(&conn->tmf_timer);
1240 mutex_lock(&session->eh_mutex);
1241 spin_lock_bh(&session->lock);
1242 /* if the session drops it will clean up the mtask */
1243 if (age != session->age ||
1244 session->state != ISCSI_STATE_LOGGED_IN)
1245 return -ENOTCONN;
1246 return 0;
1250 * Fail commands. session lock held and recv side suspended and xmit
1251 * thread flushed
1253 static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1254 int error)
1256 struct iscsi_cmd_task *ctask, *tmp;
1258 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1259 conn->ctask = NULL;
1261 /* flush pending */
1262 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1263 if (lun == ctask->sc->device->lun || lun == -1) {
1264 debug_scsi("failing pending sc %p itt 0x%x\n",
1265 ctask->sc, ctask->itt);
1266 fail_command(conn, ctask, error << 16);
1270 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1271 if (lun == ctask->sc->device->lun || lun == -1) {
1272 debug_scsi("failing requeued sc %p itt 0x%x\n",
1273 ctask->sc, ctask->itt);
1274 fail_command(conn, ctask, error << 16);
1278 /* fail all other running */
1279 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1280 if (lun == ctask->sc->device->lun || lun == -1) {
1281 debug_scsi("failing in progress sc %p itt 0x%x\n",
1282 ctask->sc, ctask->itt);
1283 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1288 static void iscsi_suspend_tx(struct iscsi_conn *conn)
1290 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1291 scsi_flush_work(conn->session->host);
1294 static void iscsi_start_tx(struct iscsi_conn *conn)
1296 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1297 scsi_queue_work(conn->session->host, &conn->xmitwork);
1300 static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1302 struct iscsi_cls_session *cls_session;
1303 struct iscsi_session *session;
1304 struct iscsi_conn *conn;
1305 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1307 cls_session = starget_to_session(scsi_target(scmd->device));
1308 session = class_to_transport_session(cls_session);
1310 debug_scsi("scsi cmd %p timedout\n", scmd);
1312 spin_lock(&session->lock);
1313 if (session->state != ISCSI_STATE_LOGGED_IN) {
1315 * We are probably in the middle of iscsi recovery so let
1316 * that complete and handle the error.
1318 rc = EH_RESET_TIMER;
1319 goto done;
1322 conn = session->leadconn;
1323 if (!conn) {
1324 /* In the middle of shuting down */
1325 rc = EH_RESET_TIMER;
1326 goto done;
1329 if (!conn->recv_timeout && !conn->ping_timeout)
1330 goto done;
1332 * if the ping timedout then we are in the middle of cleaning up
1333 * and can let the iscsi eh handle it
1335 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1336 (conn->ping_timeout * HZ), jiffies))
1337 rc = EH_RESET_TIMER;
1339 * if we are about to check the transport then give the command
1340 * more time
1342 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1343 jiffies))
1344 rc = EH_RESET_TIMER;
1345 /* if in the middle of checking the transport then give us more time */
1346 if (conn->ping_mtask)
1347 rc = EH_RESET_TIMER;
1348 done:
1349 spin_unlock(&session->lock);
1350 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1351 return rc;
1354 static void iscsi_check_transport_timeouts(unsigned long data)
1356 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1357 struct iscsi_session *session = conn->session;
1358 unsigned long recv_timeout, next_timeout = 0, last_recv;
1360 spin_lock(&session->lock);
1361 if (session->state != ISCSI_STATE_LOGGED_IN)
1362 goto done;
1364 recv_timeout = conn->recv_timeout;
1365 if (!recv_timeout)
1366 goto done;
1368 recv_timeout *= HZ;
1369 last_recv = conn->last_recv;
1370 if (conn->ping_mtask &&
1371 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
1372 jiffies)) {
1373 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1374 "expired, last rx %lu, last ping %lu, "
1375 "now %lu\n", conn->ping_timeout, last_recv,
1376 conn->last_ping, jiffies);
1377 spin_unlock(&session->lock);
1378 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1379 return;
1382 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1383 /* send a ping to try to provoke some traffic */
1384 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1385 iscsi_send_nopout(conn, NULL);
1386 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1387 } else
1388 next_timeout = last_recv + recv_timeout;
1390 debug_scsi("Setting next tmo %lu\n", next_timeout);
1391 mod_timer(&conn->transport_timer, next_timeout);
1392 done:
1393 spin_unlock(&session->lock);
1396 static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1397 struct iscsi_tm *hdr)
1399 memset(hdr, 0, sizeof(*hdr));
1400 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1401 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1402 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1403 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1404 hdr->rtt = ctask->hdr->itt;
1405 hdr->refcmdsn = ctask->hdr->cmdsn;
1408 int iscsi_eh_abort(struct scsi_cmnd *sc)
1410 struct Scsi_Host *host = sc->device->host;
1411 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1412 struct iscsi_conn *conn;
1413 struct iscsi_cmd_task *ctask;
1414 struct iscsi_tm *hdr;
1415 int rc, age;
1417 mutex_lock(&session->eh_mutex);
1418 spin_lock_bh(&session->lock);
1420 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1421 * got the command.
1423 if (!sc->SCp.ptr) {
1424 debug_scsi("sc never reached iscsi layer or it completed.\n");
1425 spin_unlock_bh(&session->lock);
1426 mutex_unlock(&session->eh_mutex);
1427 return SUCCESS;
1431 * If we are not logged in or we have started a new session
1432 * then let the host reset code handle this
1434 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1435 sc->SCp.phase != session->age) {
1436 spin_unlock_bh(&session->lock);
1437 mutex_unlock(&session->eh_mutex);
1438 return FAILED;
1441 conn = session->leadconn;
1442 conn->eh_abort_cnt++;
1443 age = session->age;
1445 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1446 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1448 /* ctask completed before time out */
1449 if (!ctask->sc) {
1450 debug_scsi("sc completed while abort in progress\n");
1451 goto success;
1454 if (ctask->state == ISCSI_TASK_PENDING) {
1455 fail_command(conn, ctask, DID_ABORT << 16);
1456 goto success;
1459 /* only have one tmf outstanding at a time */
1460 if (conn->tmf_state != TMF_INITIAL)
1461 goto failed;
1462 conn->tmf_state = TMF_QUEUED;
1464 hdr = &conn->tmhdr;
1465 iscsi_prep_abort_task_pdu(ctask, hdr);
1467 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1468 rc = FAILED;
1469 goto failed;
1472 switch (conn->tmf_state) {
1473 case TMF_SUCCESS:
1474 spin_unlock_bh(&session->lock);
1475 iscsi_suspend_tx(conn);
1477 * clean up task if aborted. grab the recv lock as a writer
1479 write_lock_bh(conn->recv_lock);
1480 spin_lock(&session->lock);
1481 fail_command(conn, ctask, DID_ABORT << 16);
1482 conn->tmf_state = TMF_INITIAL;
1483 spin_unlock(&session->lock);
1484 write_unlock_bh(conn->recv_lock);
1485 iscsi_start_tx(conn);
1486 goto success_unlocked;
1487 case TMF_TIMEDOUT:
1488 spin_unlock_bh(&session->lock);
1489 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1490 goto failed_unlocked;
1491 case TMF_NOT_FOUND:
1492 if (!sc->SCp.ptr) {
1493 conn->tmf_state = TMF_INITIAL;
1494 /* ctask completed before tmf abort response */
1495 debug_scsi("sc completed while abort in progress\n");
1496 goto success;
1498 /* fall through */
1499 default:
1500 conn->tmf_state = TMF_INITIAL;
1501 goto failed;
1504 success:
1505 spin_unlock_bh(&session->lock);
1506 success_unlocked:
1507 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1508 mutex_unlock(&session->eh_mutex);
1509 return SUCCESS;
1511 failed:
1512 spin_unlock_bh(&session->lock);
1513 failed_unlocked:
1514 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1515 ctask ? ctask->itt : 0);
1516 mutex_unlock(&session->eh_mutex);
1517 return FAILED;
1519 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1521 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1523 memset(hdr, 0, sizeof(*hdr));
1524 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1525 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1526 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1527 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1528 hdr->rtt = RESERVED_ITT;
1531 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1533 struct Scsi_Host *host = sc->device->host;
1534 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1535 struct iscsi_conn *conn;
1536 struct iscsi_tm *hdr;
1537 int rc = FAILED;
1539 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1541 mutex_lock(&session->eh_mutex);
1542 spin_lock_bh(&session->lock);
1544 * Just check if we are not logged in. We cannot check for
1545 * the phase because the reset could come from a ioctl.
1547 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1548 goto unlock;
1549 conn = session->leadconn;
1551 /* only have one tmf outstanding at a time */
1552 if (conn->tmf_state != TMF_INITIAL)
1553 goto unlock;
1554 conn->tmf_state = TMF_QUEUED;
1556 hdr = &conn->tmhdr;
1557 iscsi_prep_lun_reset_pdu(sc, hdr);
1559 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1560 session->lu_reset_timeout)) {
1561 rc = FAILED;
1562 goto unlock;
1565 switch (conn->tmf_state) {
1566 case TMF_SUCCESS:
1567 break;
1568 case TMF_TIMEDOUT:
1569 spin_unlock_bh(&session->lock);
1570 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1571 goto done;
1572 default:
1573 conn->tmf_state = TMF_INITIAL;
1574 goto unlock;
1577 rc = SUCCESS;
1578 spin_unlock_bh(&session->lock);
1580 iscsi_suspend_tx(conn);
1581 /* need to grab the recv lock then session lock */
1582 write_lock_bh(conn->recv_lock);
1583 spin_lock(&session->lock);
1584 fail_all_commands(conn, sc->device->lun, DID_ERROR);
1585 conn->tmf_state = TMF_INITIAL;
1586 spin_unlock(&session->lock);
1587 write_unlock_bh(conn->recv_lock);
1589 iscsi_start_tx(conn);
1590 goto done;
1592 unlock:
1593 spin_unlock_bh(&session->lock);
1594 done:
1595 debug_scsi("iscsi_eh_device_reset %s\n",
1596 rc == SUCCESS ? "SUCCESS" : "FAILED");
1597 mutex_unlock(&session->eh_mutex);
1598 return rc;
1600 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1603 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1604 * should be accessed via kfifo_{get,put} on q->queue.
1605 * Optionally, the caller can obtain the array of object pointers
1606 * by passing in a non-NULL @items pointer
1609 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1611 int i, num_arrays = 1;
1613 memset(q, 0, sizeof(*q));
1615 q->max = max;
1617 /* If the user passed an items pointer, he wants a copy of
1618 * the array. */
1619 if (items)
1620 num_arrays++;
1621 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1622 if (q->pool == NULL)
1623 goto enomem;
1625 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1626 GFP_KERNEL, NULL);
1627 if (q->queue == ERR_PTR(-ENOMEM))
1628 goto enomem;
1630 for (i = 0; i < max; i++) {
1631 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
1632 if (q->pool[i] == NULL) {
1633 q->max = i;
1634 goto enomem;
1636 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1639 if (items) {
1640 *items = q->pool + max;
1641 memcpy(*items, q->pool, max * sizeof(void *));
1644 return 0;
1646 enomem:
1647 iscsi_pool_free(q);
1648 return -ENOMEM;
1650 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1652 void iscsi_pool_free(struct iscsi_pool *q)
1654 int i;
1656 for (i = 0; i < q->max; i++)
1657 kfree(q->pool[i]);
1658 if (q->pool)
1659 kfree(q->pool);
1661 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1664 * iSCSI Session's hostdata organization:
1666 * *------------------* <== hostdata_session(host->hostdata)
1667 * | ptr to class sess|
1668 * |------------------| <== iscsi_hostdata(host->hostdata)
1669 * | iscsi_session |
1670 * *------------------*
1673 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1674 _sz % sizeof(unsigned long))
1676 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1679 * iscsi_session_setup - create iscsi cls session and host and session
1680 * @scsit: scsi transport template
1681 * @iscsit: iscsi transport template
1682 * @cmds_max: scsi host can queue
1683 * @qdepth: scsi host cmds per lun
1684 * @cmd_task_size: LLD ctask private data size
1685 * @mgmt_task_size: LLD mtask private data size
1686 * @initial_cmdsn: initial CmdSN
1687 * @hostno: host no allocated
1689 * This can be used by software iscsi_transports that allocate
1690 * a session per scsi host.
1692 struct iscsi_cls_session *
1693 iscsi_session_setup(struct iscsi_transport *iscsit,
1694 struct scsi_transport_template *scsit,
1695 uint16_t cmds_max, uint16_t qdepth,
1696 int cmd_task_size, int mgmt_task_size,
1697 uint32_t initial_cmdsn, uint32_t *hostno)
1699 struct Scsi_Host *shost;
1700 struct iscsi_session *session;
1701 struct iscsi_cls_session *cls_session;
1702 int cmd_i;
1704 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1705 if (qdepth != 0)
1706 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1707 "Queue depth must be between 1 and %d.\n",
1708 qdepth, ISCSI_MAX_CMD_PER_LUN);
1709 qdepth = ISCSI_DEF_CMD_PER_LUN;
1712 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1713 cmds_max < 2) {
1714 if (cmds_max != 0)
1715 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1716 "can_queue must be a power of 2 and between "
1717 "2 and %d - setting to %d.\n", cmds_max,
1718 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1719 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1722 shost = scsi_host_alloc(iscsit->host_template,
1723 hostdata_privsize(sizeof(*session)));
1724 if (!shost)
1725 return NULL;
1727 /* the iscsi layer takes one task for reserve */
1728 shost->can_queue = cmds_max - 1;
1729 shost->cmd_per_lun = qdepth;
1730 shost->max_id = 1;
1731 shost->max_channel = 0;
1732 shost->max_lun = iscsit->max_lun;
1733 shost->max_cmd_len = iscsit->max_cmd_len;
1734 shost->transportt = scsit;
1735 shost->transportt->create_work_queue = 1;
1736 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1737 *hostno = shost->host_no;
1739 session = iscsi_hostdata(shost->hostdata);
1740 memset(session, 0, sizeof(struct iscsi_session));
1741 session->host = shost;
1742 session->state = ISCSI_STATE_FREE;
1743 session->fast_abort = 1;
1744 session->lu_reset_timeout = 15;
1745 session->abort_timeout = 10;
1746 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1747 session->cmds_max = cmds_max;
1748 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
1749 session->exp_cmdsn = initial_cmdsn + 1;
1750 session->max_cmdsn = initial_cmdsn + 1;
1751 session->max_r2t = 1;
1752 session->tt = iscsit;
1753 mutex_init(&session->eh_mutex);
1755 /* initialize SCSI PDU commands pool */
1756 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1757 (void***)&session->cmds,
1758 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1759 goto cmdpool_alloc_fail;
1761 /* pre-format cmds pool with ITT */
1762 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1763 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1765 if (cmd_task_size)
1766 ctask->dd_data = &ctask[1];
1767 ctask->itt = cmd_i;
1768 INIT_LIST_HEAD(&ctask->running);
1771 spin_lock_init(&session->lock);
1773 /* initialize immediate command pool */
1774 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1775 (void***)&session->mgmt_cmds,
1776 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1777 goto mgmtpool_alloc_fail;
1780 /* pre-format immediate cmds pool with ITT */
1781 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1782 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1784 if (mgmt_task_size)
1785 mtask->dd_data = &mtask[1];
1786 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1787 INIT_LIST_HEAD(&mtask->running);
1790 if (scsi_add_host(shost, NULL))
1791 goto add_host_fail;
1793 if (!try_module_get(iscsit->owner))
1794 goto cls_session_fail;
1796 cls_session = iscsi_create_session(shost, iscsit, 0);
1797 if (!cls_session)
1798 goto module_put;
1799 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1801 return cls_session;
1803 module_put:
1804 module_put(iscsit->owner);
1805 cls_session_fail:
1806 scsi_remove_host(shost);
1807 add_host_fail:
1808 iscsi_pool_free(&session->mgmtpool);
1809 mgmtpool_alloc_fail:
1810 iscsi_pool_free(&session->cmdpool);
1811 cmdpool_alloc_fail:
1812 scsi_host_put(shost);
1813 return NULL;
1815 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1818 * iscsi_session_teardown - destroy session, host, and cls_session
1819 * shost: scsi host
1821 * This can be used by software iscsi_transports that allocate
1822 * a session per scsi host.
1824 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1826 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1827 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1828 struct module *owner = cls_session->transport->owner;
1830 iscsi_remove_session(cls_session);
1831 scsi_remove_host(shost);
1833 iscsi_pool_free(&session->mgmtpool);
1834 iscsi_pool_free(&session->cmdpool);
1836 kfree(session->password);
1837 kfree(session->password_in);
1838 kfree(session->username);
1839 kfree(session->username_in);
1840 kfree(session->targetname);
1841 kfree(session->netdev);
1842 kfree(session->hwaddress);
1843 kfree(session->initiatorname);
1845 iscsi_free_session(cls_session);
1846 scsi_host_put(shost);
1847 module_put(owner);
1849 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1852 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1853 * @cls_session: iscsi_cls_session
1854 * @conn_idx: cid
1856 struct iscsi_cls_conn *
1857 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1859 struct iscsi_session *session = class_to_transport_session(cls_session);
1860 struct iscsi_conn *conn;
1861 struct iscsi_cls_conn *cls_conn;
1862 char *data;
1864 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1865 if (!cls_conn)
1866 return NULL;
1867 conn = cls_conn->dd_data;
1868 memset(conn, 0, sizeof(*conn));
1870 conn->session = session;
1871 conn->cls_conn = cls_conn;
1872 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1873 conn->id = conn_idx;
1874 conn->exp_statsn = 0;
1875 conn->tmf_state = TMF_INITIAL;
1877 init_timer(&conn->transport_timer);
1878 conn->transport_timer.data = (unsigned long)conn;
1879 conn->transport_timer.function = iscsi_check_transport_timeouts;
1881 INIT_LIST_HEAD(&conn->run_list);
1882 INIT_LIST_HEAD(&conn->mgmt_run_list);
1883 INIT_LIST_HEAD(&conn->mgmtqueue);
1884 INIT_LIST_HEAD(&conn->xmitqueue);
1885 INIT_LIST_HEAD(&conn->requeue);
1886 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
1888 /* allocate login_mtask used for the login/text sequences */
1889 spin_lock_bh(&session->lock);
1890 if (!__kfifo_get(session->mgmtpool.queue,
1891 (void*)&conn->login_mtask,
1892 sizeof(void*))) {
1893 spin_unlock_bh(&session->lock);
1894 goto login_mtask_alloc_fail;
1896 spin_unlock_bh(&session->lock);
1898 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
1899 if (!data)
1900 goto login_mtask_data_alloc_fail;
1901 conn->login_mtask->data = conn->data = data;
1903 init_timer(&conn->tmf_timer);
1904 init_waitqueue_head(&conn->ehwait);
1906 return cls_conn;
1908 login_mtask_data_alloc_fail:
1909 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1910 sizeof(void*));
1911 login_mtask_alloc_fail:
1912 iscsi_destroy_conn(cls_conn);
1913 return NULL;
1915 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1918 * iscsi_conn_teardown - teardown iscsi connection
1919 * cls_conn: iscsi class connection
1921 * TODO: we may need to make this into a two step process
1922 * like scsi-mls remove + put host
1924 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1926 struct iscsi_conn *conn = cls_conn->dd_data;
1927 struct iscsi_session *session = conn->session;
1928 unsigned long flags;
1930 del_timer_sync(&conn->transport_timer);
1932 spin_lock_bh(&session->lock);
1933 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1934 if (session->leadconn == conn) {
1936 * leading connection? then give up on recovery.
1938 session->state = ISCSI_STATE_TERMINATE;
1939 wake_up(&conn->ehwait);
1941 spin_unlock_bh(&session->lock);
1944 * Block until all in-progress commands for this connection
1945 * time out or fail.
1947 for (;;) {
1948 spin_lock_irqsave(session->host->host_lock, flags);
1949 if (!session->host->host_busy) { /* OK for ERL == 0 */
1950 spin_unlock_irqrestore(session->host->host_lock, flags);
1951 break;
1953 spin_unlock_irqrestore(session->host->host_lock, flags);
1954 msleep_interruptible(500);
1955 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
1956 "host_busy %d host_failed %d\n",
1957 session->host->host_busy,
1958 session->host->host_failed);
1960 * force eh_abort() to unblock
1962 wake_up(&conn->ehwait);
1965 /* flush queued up work because we free the connection below */
1966 iscsi_suspend_tx(conn);
1968 spin_lock_bh(&session->lock);
1969 kfree(conn->data);
1970 kfree(conn->persistent_address);
1971 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1972 sizeof(void*));
1973 if (session->leadconn == conn)
1974 session->leadconn = NULL;
1975 spin_unlock_bh(&session->lock);
1977 iscsi_destroy_conn(cls_conn);
1979 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1981 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1983 struct iscsi_conn *conn = cls_conn->dd_data;
1984 struct iscsi_session *session = conn->session;
1986 if (!session) {
1987 iscsi_conn_printk(KERN_ERR, conn,
1988 "can't start unbound connection\n");
1989 return -EPERM;
1992 if ((session->imm_data_en || !session->initial_r2t_en) &&
1993 session->first_burst > session->max_burst) {
1994 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
1995 "first_burst %d max_burst %d\n",
1996 session->first_burst, session->max_burst);
1997 return -EINVAL;
2000 if (conn->ping_timeout && !conn->recv_timeout) {
2001 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2002 "zero. Using 5 seconds\n.");
2003 conn->recv_timeout = 5;
2006 if (conn->recv_timeout && !conn->ping_timeout) {
2007 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2008 "zero. Using 5 seconds.\n");
2009 conn->ping_timeout = 5;
2012 spin_lock_bh(&session->lock);
2013 conn->c_stage = ISCSI_CONN_STARTED;
2014 session->state = ISCSI_STATE_LOGGED_IN;
2015 session->queued_cmdsn = session->cmdsn;
2017 conn->last_recv = jiffies;
2018 conn->last_ping = jiffies;
2019 if (conn->recv_timeout && conn->ping_timeout)
2020 mod_timer(&conn->transport_timer,
2021 jiffies + (conn->recv_timeout * HZ));
2023 switch(conn->stop_stage) {
2024 case STOP_CONN_RECOVER:
2026 * unblock eh_abort() if it is blocked. re-try all
2027 * commands after successful recovery
2029 conn->stop_stage = 0;
2030 conn->tmf_state = TMF_INITIAL;
2031 session->age++;
2032 if (session->age == 16)
2033 session->age = 0;
2034 break;
2035 case STOP_CONN_TERM:
2036 conn->stop_stage = 0;
2037 break;
2038 default:
2039 break;
2041 spin_unlock_bh(&session->lock);
2043 iscsi_unblock_session(session_to_cls(session));
2044 wake_up(&conn->ehwait);
2045 return 0;
2047 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2049 static void
2050 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2052 struct iscsi_mgmt_task *mtask, *tmp;
2054 /* handle pending */
2055 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2056 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
2057 iscsi_free_mgmt_task(conn, mtask);
2060 /* handle running */
2061 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2062 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
2063 iscsi_free_mgmt_task(conn, mtask);
2066 conn->mtask = NULL;
2069 static void iscsi_start_session_recovery(struct iscsi_session *session,
2070 struct iscsi_conn *conn, int flag)
2072 int old_stop_stage;
2074 del_timer_sync(&conn->transport_timer);
2076 mutex_lock(&session->eh_mutex);
2077 spin_lock_bh(&session->lock);
2078 if (conn->stop_stage == STOP_CONN_TERM) {
2079 spin_unlock_bh(&session->lock);
2080 mutex_unlock(&session->eh_mutex);
2081 return;
2085 * The LLD either freed/unset the lock on us, or userspace called
2086 * stop but did not create a proper connection (connection was never
2087 * bound or it was unbound then stop was called).
2089 if (!conn->recv_lock) {
2090 spin_unlock_bh(&session->lock);
2091 mutex_unlock(&session->eh_mutex);
2092 return;
2096 * When this is called for the in_login state, we only want to clean
2097 * up the login task and connection. We do not need to block and set
2098 * the recovery state again
2100 if (flag == STOP_CONN_TERM)
2101 session->state = ISCSI_STATE_TERMINATE;
2102 else if (conn->stop_stage != STOP_CONN_RECOVER)
2103 session->state = ISCSI_STATE_IN_RECOVERY;
2105 old_stop_stage = conn->stop_stage;
2106 conn->stop_stage = flag;
2107 conn->c_stage = ISCSI_CONN_STOPPED;
2108 spin_unlock_bh(&session->lock);
2110 iscsi_suspend_tx(conn);
2112 write_lock_bh(conn->recv_lock);
2113 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2114 write_unlock_bh(conn->recv_lock);
2117 * for connection level recovery we should not calculate
2118 * header digest. conn->hdr_size used for optimization
2119 * in hdr_extract() and will be re-negotiated at
2120 * set_param() time.
2122 if (flag == STOP_CONN_RECOVER) {
2123 conn->hdrdgst_en = 0;
2124 conn->datadgst_en = 0;
2125 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2126 old_stop_stage != STOP_CONN_RECOVER) {
2127 debug_scsi("blocking session\n");
2128 iscsi_block_session(session_to_cls(session));
2133 * flush queues.
2135 spin_lock_bh(&session->lock);
2136 fail_all_commands(conn, -1,
2137 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
2138 flush_control_queues(session, conn);
2139 spin_unlock_bh(&session->lock);
2140 mutex_unlock(&session->eh_mutex);
2143 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2145 struct iscsi_conn *conn = cls_conn->dd_data;
2146 struct iscsi_session *session = conn->session;
2148 switch (flag) {
2149 case STOP_CONN_RECOVER:
2150 case STOP_CONN_TERM:
2151 iscsi_start_session_recovery(session, conn, flag);
2152 break;
2153 default:
2154 iscsi_conn_printk(KERN_ERR, conn,
2155 "invalid stop flag %d\n", flag);
2158 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2160 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2161 struct iscsi_cls_conn *cls_conn, int is_leading)
2163 struct iscsi_session *session = class_to_transport_session(cls_session);
2164 struct iscsi_conn *conn = cls_conn->dd_data;
2166 spin_lock_bh(&session->lock);
2167 if (is_leading)
2168 session->leadconn = conn;
2169 spin_unlock_bh(&session->lock);
2172 * Unblock xmitworker(), Login Phase will pass through.
2174 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2175 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2176 return 0;
2178 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2181 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2182 enum iscsi_param param, char *buf, int buflen)
2184 struct iscsi_conn *conn = cls_conn->dd_data;
2185 struct iscsi_session *session = conn->session;
2186 uint32_t value;
2188 switch(param) {
2189 case ISCSI_PARAM_FAST_ABORT:
2190 sscanf(buf, "%d", &session->fast_abort);
2191 break;
2192 case ISCSI_PARAM_ABORT_TMO:
2193 sscanf(buf, "%d", &session->abort_timeout);
2194 break;
2195 case ISCSI_PARAM_LU_RESET_TMO:
2196 sscanf(buf, "%d", &session->lu_reset_timeout);
2197 break;
2198 case ISCSI_PARAM_PING_TMO:
2199 sscanf(buf, "%d", &conn->ping_timeout);
2200 break;
2201 case ISCSI_PARAM_RECV_TMO:
2202 sscanf(buf, "%d", &conn->recv_timeout);
2203 break;
2204 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2205 sscanf(buf, "%d", &conn->max_recv_dlength);
2206 break;
2207 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2208 sscanf(buf, "%d", &conn->max_xmit_dlength);
2209 break;
2210 case ISCSI_PARAM_HDRDGST_EN:
2211 sscanf(buf, "%d", &conn->hdrdgst_en);
2212 break;
2213 case ISCSI_PARAM_DATADGST_EN:
2214 sscanf(buf, "%d", &conn->datadgst_en);
2215 break;
2216 case ISCSI_PARAM_INITIAL_R2T_EN:
2217 sscanf(buf, "%d", &session->initial_r2t_en);
2218 break;
2219 case ISCSI_PARAM_MAX_R2T:
2220 sscanf(buf, "%d", &session->max_r2t);
2221 break;
2222 case ISCSI_PARAM_IMM_DATA_EN:
2223 sscanf(buf, "%d", &session->imm_data_en);
2224 break;
2225 case ISCSI_PARAM_FIRST_BURST:
2226 sscanf(buf, "%d", &session->first_burst);
2227 break;
2228 case ISCSI_PARAM_MAX_BURST:
2229 sscanf(buf, "%d", &session->max_burst);
2230 break;
2231 case ISCSI_PARAM_PDU_INORDER_EN:
2232 sscanf(buf, "%d", &session->pdu_inorder_en);
2233 break;
2234 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2235 sscanf(buf, "%d", &session->dataseq_inorder_en);
2236 break;
2237 case ISCSI_PARAM_ERL:
2238 sscanf(buf, "%d", &session->erl);
2239 break;
2240 case ISCSI_PARAM_IFMARKER_EN:
2241 sscanf(buf, "%d", &value);
2242 BUG_ON(value);
2243 break;
2244 case ISCSI_PARAM_OFMARKER_EN:
2245 sscanf(buf, "%d", &value);
2246 BUG_ON(value);
2247 break;
2248 case ISCSI_PARAM_EXP_STATSN:
2249 sscanf(buf, "%u", &conn->exp_statsn);
2250 break;
2251 case ISCSI_PARAM_USERNAME:
2252 kfree(session->username);
2253 session->username = kstrdup(buf, GFP_KERNEL);
2254 if (!session->username)
2255 return -ENOMEM;
2256 break;
2257 case ISCSI_PARAM_USERNAME_IN:
2258 kfree(session->username_in);
2259 session->username_in = kstrdup(buf, GFP_KERNEL);
2260 if (!session->username_in)
2261 return -ENOMEM;
2262 break;
2263 case ISCSI_PARAM_PASSWORD:
2264 kfree(session->password);
2265 session->password = kstrdup(buf, GFP_KERNEL);
2266 if (!session->password)
2267 return -ENOMEM;
2268 break;
2269 case ISCSI_PARAM_PASSWORD_IN:
2270 kfree(session->password_in);
2271 session->password_in = kstrdup(buf, GFP_KERNEL);
2272 if (!session->password_in)
2273 return -ENOMEM;
2274 break;
2275 case ISCSI_PARAM_TARGET_NAME:
2276 /* this should not change between logins */
2277 if (session->targetname)
2278 break;
2280 session->targetname = kstrdup(buf, GFP_KERNEL);
2281 if (!session->targetname)
2282 return -ENOMEM;
2283 break;
2284 case ISCSI_PARAM_TPGT:
2285 sscanf(buf, "%d", &session->tpgt);
2286 break;
2287 case ISCSI_PARAM_PERSISTENT_PORT:
2288 sscanf(buf, "%d", &conn->persistent_port);
2289 break;
2290 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2292 * this is the address returned in discovery so it should
2293 * not change between logins.
2295 if (conn->persistent_address)
2296 break;
2298 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2299 if (!conn->persistent_address)
2300 return -ENOMEM;
2301 break;
2302 default:
2303 return -ENOSYS;
2306 return 0;
2308 EXPORT_SYMBOL_GPL(iscsi_set_param);
2310 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2311 enum iscsi_param param, char *buf)
2313 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2314 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2315 int len;
2317 switch(param) {
2318 case ISCSI_PARAM_FAST_ABORT:
2319 len = sprintf(buf, "%d\n", session->fast_abort);
2320 break;
2321 case ISCSI_PARAM_ABORT_TMO:
2322 len = sprintf(buf, "%d\n", session->abort_timeout);
2323 break;
2324 case ISCSI_PARAM_LU_RESET_TMO:
2325 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2326 break;
2327 case ISCSI_PARAM_INITIAL_R2T_EN:
2328 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2329 break;
2330 case ISCSI_PARAM_MAX_R2T:
2331 len = sprintf(buf, "%hu\n", session->max_r2t);
2332 break;
2333 case ISCSI_PARAM_IMM_DATA_EN:
2334 len = sprintf(buf, "%d\n", session->imm_data_en);
2335 break;
2336 case ISCSI_PARAM_FIRST_BURST:
2337 len = sprintf(buf, "%u\n", session->first_burst);
2338 break;
2339 case ISCSI_PARAM_MAX_BURST:
2340 len = sprintf(buf, "%u\n", session->max_burst);
2341 break;
2342 case ISCSI_PARAM_PDU_INORDER_EN:
2343 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2344 break;
2345 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2346 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2347 break;
2348 case ISCSI_PARAM_ERL:
2349 len = sprintf(buf, "%d\n", session->erl);
2350 break;
2351 case ISCSI_PARAM_TARGET_NAME:
2352 len = sprintf(buf, "%s\n", session->targetname);
2353 break;
2354 case ISCSI_PARAM_TPGT:
2355 len = sprintf(buf, "%d\n", session->tpgt);
2356 break;
2357 case ISCSI_PARAM_USERNAME:
2358 len = sprintf(buf, "%s\n", session->username);
2359 break;
2360 case ISCSI_PARAM_USERNAME_IN:
2361 len = sprintf(buf, "%s\n", session->username_in);
2362 break;
2363 case ISCSI_PARAM_PASSWORD:
2364 len = sprintf(buf, "%s\n", session->password);
2365 break;
2366 case ISCSI_PARAM_PASSWORD_IN:
2367 len = sprintf(buf, "%s\n", session->password_in);
2368 break;
2369 default:
2370 return -ENOSYS;
2373 return len;
2375 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2377 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2378 enum iscsi_param param, char *buf)
2380 struct iscsi_conn *conn = cls_conn->dd_data;
2381 int len;
2383 switch(param) {
2384 case ISCSI_PARAM_PING_TMO:
2385 len = sprintf(buf, "%u\n", conn->ping_timeout);
2386 break;
2387 case ISCSI_PARAM_RECV_TMO:
2388 len = sprintf(buf, "%u\n", conn->recv_timeout);
2389 break;
2390 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2391 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2392 break;
2393 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2394 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2395 break;
2396 case ISCSI_PARAM_HDRDGST_EN:
2397 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2398 break;
2399 case ISCSI_PARAM_DATADGST_EN:
2400 len = sprintf(buf, "%d\n", conn->datadgst_en);
2401 break;
2402 case ISCSI_PARAM_IFMARKER_EN:
2403 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2404 break;
2405 case ISCSI_PARAM_OFMARKER_EN:
2406 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2407 break;
2408 case ISCSI_PARAM_EXP_STATSN:
2409 len = sprintf(buf, "%u\n", conn->exp_statsn);
2410 break;
2411 case ISCSI_PARAM_PERSISTENT_PORT:
2412 len = sprintf(buf, "%d\n", conn->persistent_port);
2413 break;
2414 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2415 len = sprintf(buf, "%s\n", conn->persistent_address);
2416 break;
2417 default:
2418 return -ENOSYS;
2421 return len;
2423 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2425 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2426 char *buf)
2428 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2429 int len;
2431 switch (param) {
2432 case ISCSI_HOST_PARAM_NETDEV_NAME:
2433 if (!session->netdev)
2434 len = sprintf(buf, "%s\n", "default");
2435 else
2436 len = sprintf(buf, "%s\n", session->netdev);
2437 break;
2438 case ISCSI_HOST_PARAM_HWADDRESS:
2439 if (!session->hwaddress)
2440 len = sprintf(buf, "%s\n", "default");
2441 else
2442 len = sprintf(buf, "%s\n", session->hwaddress);
2443 break;
2444 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2445 if (!session->initiatorname)
2446 len = sprintf(buf, "%s\n", "unknown");
2447 else
2448 len = sprintf(buf, "%s\n", session->initiatorname);
2449 break;
2451 default:
2452 return -ENOSYS;
2455 return len;
2457 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2459 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2460 char *buf, int buflen)
2462 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2464 switch (param) {
2465 case ISCSI_HOST_PARAM_NETDEV_NAME:
2466 if (!session->netdev)
2467 session->netdev = kstrdup(buf, GFP_KERNEL);
2468 break;
2469 case ISCSI_HOST_PARAM_HWADDRESS:
2470 if (!session->hwaddress)
2471 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2472 break;
2473 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2474 if (!session->initiatorname)
2475 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2476 break;
2477 default:
2478 return -ENOSYS;
2481 return 0;
2483 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2485 MODULE_AUTHOR("Mike Christie");
2486 MODULE_DESCRIPTION("iSCSI library functions");
2487 MODULE_LICENSE("GPL");