4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <linux/log2.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi.h>
36 #include <scsi/iscsi_proto.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_iscsi.h>
39 #include <scsi/libiscsi.h>
41 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42 #define SNA32_CHECK 2147483648UL
44 static int iscsi_sna_lt(u32 n1, u32 n2)
46 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
47 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
50 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
51 static int iscsi_sna_lte(u32 n1, u32 n2)
53 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
58 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
60 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
61 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
64 * standard specifies this check for when to update expected and
65 * max sequence numbers
67 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
70 if (exp_cmdsn != session->exp_cmdsn &&
71 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
72 session->exp_cmdsn = exp_cmdsn;
74 if (max_cmdsn != session->max_cmdsn &&
75 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
76 session->max_cmdsn = max_cmdsn;
78 * if the window closed with IO queued, then kick the
81 if (!list_empty(&session->leadconn->xmitqueue) ||
82 !list_empty(&session->leadconn->mgmtqueue)) {
83 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
84 scsi_queue_work(session->host,
85 &session->leadconn->xmitwork);
89 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
92 * iscsi_prep_data_out_pdu - initialize Data-Out
93 * @task: scsi command task
95 * @hdr: iscsi data in pdu
98 * Initialize Data-Out within this R2T sequence and finds
99 * proper data_offset within this SCSI command.
101 * This function is called with connection lock taken.
103 void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
104 struct iscsi_data *hdr)
106 struct iscsi_conn *conn = task->conn;
107 unsigned int left = r2t->data_length - r2t->sent;
109 task->hdr_len = sizeof(struct iscsi_data);
111 memset(hdr, 0, sizeof(struct iscsi_data));
113 hdr->datasn = cpu_to_be32(r2t->datasn);
115 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
116 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
117 hdr->itt = task->hdr_itt;
118 hdr->exp_statsn = r2t->exp_statsn;
119 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
120 if (left > conn->max_xmit_dlength) {
121 hton24(hdr->dlength, conn->max_xmit_dlength);
122 r2t->data_count = conn->max_xmit_dlength;
125 hton24(hdr->dlength, left);
126 r2t->data_count = left;
127 hdr->flags = ISCSI_FLAG_CMD_FINAL;
129 conn->dataout_pdus_cnt++;
131 EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
133 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
135 unsigned exp_len = task->hdr_len + len;
137 if (exp_len > task->hdr_max) {
142 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
143 task->hdr_len = exp_len;
148 * make an extended cdb AHS
150 static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
152 struct scsi_cmnd *cmd = task->sc;
153 unsigned rlen, pad_len;
154 unsigned short ahslength;
155 struct iscsi_ecdb_ahdr *ecdb_ahdr;
158 ecdb_ahdr = iscsi_next_hdr(task);
159 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
161 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
162 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
164 pad_len = iscsi_padding(rlen);
166 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
167 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
172 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
174 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
175 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
176 ecdb_ahdr->reserved = 0;
177 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
179 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
180 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
181 cmd->cmd_len, rlen, pad_len, ahslength, task->hdr_len);
186 static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
188 struct scsi_cmnd *sc = task->sc;
189 struct iscsi_rlength_ahdr *rlen_ahdr;
192 rlen_ahdr = iscsi_next_hdr(task);
193 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
197 rlen_ahdr->ahslength =
198 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
199 sizeof(rlen_ahdr->reserved));
200 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
201 rlen_ahdr->reserved = 0;
202 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
204 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
205 "rlen_ahdr->ahslength(%d)\n",
206 be32_to_cpu(rlen_ahdr->read_length),
207 be16_to_cpu(rlen_ahdr->ahslength));
212 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
215 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
216 * fields like dlength or final based on how much data it sends
218 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
220 struct iscsi_conn *conn = task->conn;
221 struct iscsi_session *session = conn->session;
222 struct scsi_cmnd *sc = task->sc;
223 struct iscsi_cmd *hdr;
224 unsigned hdrlength, cmd_len;
227 rc = conn->session->tt->alloc_pdu(task);
230 hdr = (struct iscsi_cmd *) task->hdr;
231 memset(hdr, 0, sizeof(*hdr));
234 rc = iscsi_add_hdr(task, sizeof(*hdr));
237 hdr->opcode = ISCSI_OP_SCSI_CMD;
238 hdr->flags = ISCSI_ATTR_SIMPLE;
239 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
240 memcpy(task->lun, hdr->lun, sizeof(task->lun));
241 hdr->itt = task->hdr_itt = build_itt(task->itt, session->age);
242 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
244 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
245 cmd_len = sc->cmd_len;
246 if (cmd_len < ISCSI_CDB_SIZE)
247 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
248 else if (cmd_len > ISCSI_CDB_SIZE) {
249 rc = iscsi_prep_ecdb_ahs(task);
252 cmd_len = ISCSI_CDB_SIZE;
254 memcpy(hdr->cdb, sc->cmnd, cmd_len);
257 if (scsi_bidi_cmnd(sc)) {
258 hdr->flags |= ISCSI_FLAG_CMD_READ;
259 rc = iscsi_prep_bidi_ahs(task);
263 if (sc->sc_data_direction == DMA_TO_DEVICE) {
264 unsigned out_len = scsi_out(sc)->length;
265 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
267 hdr->data_length = cpu_to_be32(out_len);
268 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
272 * imm_count bytes to be sent right after
275 * unsol_count bytes(as Data-Out) to be sent
276 * without R2T ack right after
279 * r2t data_length bytes to be sent via R2T ack's
281 * pad_count bytes to be sent as zero-padding
283 memset(r2t, 0, sizeof(*r2t));
285 if (session->imm_data_en) {
286 if (out_len >= session->first_burst)
287 task->imm_count = min(session->first_burst,
288 conn->max_xmit_dlength);
290 task->imm_count = min(out_len,
291 conn->max_xmit_dlength);
292 hton24(hdr->dlength, task->imm_count);
294 zero_data(hdr->dlength);
296 if (!session->initial_r2t_en) {
297 r2t->data_length = min(session->first_burst, out_len) -
299 r2t->data_offset = task->imm_count;
300 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
301 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
304 if (!task->unsol_r2t.data_length)
305 /* No unsolicit Data-Out's */
306 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
308 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
309 zero_data(hdr->dlength);
310 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
312 if (sc->sc_data_direction == DMA_FROM_DEVICE)
313 hdr->flags |= ISCSI_FLAG_CMD_READ;
316 /* calculate size of additional header segments (AHSs) */
317 hdrlength = task->hdr_len - sizeof(*hdr);
319 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
320 hdrlength /= ISCSI_PAD_LEN;
322 WARN_ON(hdrlength >= 256);
323 hdr->hlength = hdrlength & 0xFF;
325 if (session->tt->init_task && session->tt->init_task(task))
328 task->state = ISCSI_TASK_RUNNING;
329 list_move_tail(&task->running, &conn->run_list);
331 conn->scsicmd_pdus_cnt++;
332 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
333 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ?
334 "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ?
335 "write" : "read", conn->id, sc, sc->cmnd[0], task->itt,
337 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
338 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
343 * iscsi_complete_command - finish a task
344 * @task: iscsi cmd task
346 * Must be called with session lock.
347 * This function returns the scsi command to scsi-ml or cleans
348 * up mgmt tasks then returns the task to the pool.
350 static void iscsi_complete_command(struct iscsi_task *task)
352 struct iscsi_conn *conn = task->conn;
353 struct iscsi_session *session = conn->session;
354 struct scsi_cmnd *sc = task->sc;
356 session->tt->cleanup_task(task);
357 list_del_init(&task->running);
358 task->state = ISCSI_TASK_COMPLETED;
361 if (conn->task == task)
364 * login task is preallocated so do not free
366 if (conn->login_task == task)
369 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
371 if (conn->ping_task == task)
372 conn->ping_task = NULL;
376 /* SCSI eh reuses commands to verify us */
379 * queue command may call this to free the task, but
380 * not have setup the sc callback
387 void __iscsi_get_task(struct iscsi_task *task)
389 atomic_inc(&task->refcount);
391 EXPORT_SYMBOL_GPL(__iscsi_get_task);
393 static void __iscsi_put_task(struct iscsi_task *task)
395 if (atomic_dec_and_test(&task->refcount))
396 iscsi_complete_command(task);
399 void iscsi_put_task(struct iscsi_task *task)
401 struct iscsi_session *session = task->conn->session;
403 spin_lock_bh(&session->lock);
404 __iscsi_put_task(task);
405 spin_unlock_bh(&session->lock);
407 EXPORT_SYMBOL_GPL(iscsi_put_task);
410 * session lock must be held
412 static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
415 struct scsi_cmnd *sc;
421 if (task->state == ISCSI_TASK_PENDING)
423 * cmd never made it to the xmit thread, so we should not count
424 * the cmd in the sequencing
426 conn->session->queued_cmdsn--;
429 if (!scsi_bidi_cmnd(sc))
430 scsi_set_resid(sc, scsi_bufflen(sc));
432 scsi_out(sc)->resid = scsi_out(sc)->length;
433 scsi_in(sc)->resid = scsi_in(sc)->length;
436 if (conn->task == task)
438 /* release ref from queuecommand */
439 __iscsi_put_task(task);
442 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
443 struct iscsi_task *task)
445 struct iscsi_session *session = conn->session;
446 struct iscsi_hdr *hdr = task->hdr;
447 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
449 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
452 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
453 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
454 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
456 * pre-format CmdSN for outgoing PDU.
458 nop->cmdsn = cpu_to_be32(session->cmdsn);
459 if (hdr->itt != RESERVED_ITT) {
460 hdr->itt = build_itt(task->itt, session->age);
462 * TODO: We always use immediate, so we never hit this.
463 * If we start to send tmfs or nops as non-immediate then
464 * we should start checking the cmdsn numbers for mgmt tasks.
466 if (conn->c_stage == ISCSI_CONN_STARTED &&
467 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
468 session->queued_cmdsn++;
473 if (session->tt->init_task)
474 session->tt->init_task(task);
476 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
477 session->state = ISCSI_STATE_LOGGING_OUT;
479 task->state = ISCSI_TASK_RUNNING;
480 list_move_tail(&task->running, &conn->mgmt_run_list);
481 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
482 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
487 static struct iscsi_task *
488 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
489 char *data, uint32_t data_size)
491 struct iscsi_session *session = conn->session;
492 struct iscsi_task *task;
494 if (session->state == ISCSI_STATE_TERMINATE)
497 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
498 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
500 * Login and Text are sent serially, in
501 * request-followed-by-response sequence.
502 * Same task can be used. Same ITT must be used.
503 * Note that login_task is preallocated at conn_create().
505 task = conn->login_task;
507 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
508 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
510 if (!__kfifo_get(session->cmdpool.queue,
511 (void*)&task, sizeof(void*)))
515 * released in complete pdu for task we expect a response for, and
516 * released by the lld when it has transmitted the task for
517 * pdus we do not expect a response for.
519 atomic_set(&task->refcount, 1);
524 memcpy(task->data, data, data_size);
525 task->data_count = data_size;
527 task->data_count = 0;
529 if (conn->session->tt->alloc_pdu(task)) {
530 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
531 "pdu for mgmt task.\n");
534 task->hdr_len = sizeof(struct iscsi_hdr);
536 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
537 INIT_LIST_HEAD(&task->running);
538 list_add_tail(&task->running, &conn->mgmtqueue);
540 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
541 if (iscsi_prep_mgmt_task(conn, task))
544 if (session->tt->xmit_task(task))
548 scsi_queue_work(conn->session->host, &conn->xmitwork);
553 __iscsi_put_task(task);
557 if (task != conn->login_task)
558 __kfifo_put(session->cmdpool.queue, (void*)&task,
563 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
564 char *data, uint32_t data_size)
566 struct iscsi_conn *conn = cls_conn->dd_data;
567 struct iscsi_session *session = conn->session;
570 spin_lock_bh(&session->lock);
571 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
573 spin_unlock_bh(&session->lock);
576 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
579 * iscsi_cmd_rsp - SCSI Command Response processing
580 * @conn: iscsi connection
582 * @task: scsi command task
583 * @data: cmd data buffer
584 * @datalen: len of buffer
586 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
587 * then completes the command and task.
589 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
590 struct iscsi_task *task, char *data,
593 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
594 struct iscsi_session *session = conn->session;
595 struct scsi_cmnd *sc = task->sc;
597 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
598 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
600 sc->result = (DID_OK << 16) | rhdr->cmd_status;
602 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
603 sc->result = DID_ERROR << 16;
607 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
612 iscsi_conn_printk(KERN_ERR, conn,
613 "Got CHECK_CONDITION but invalid data "
614 "buffer size of %d\n", datalen);
615 sc->result = DID_BAD_TARGET << 16;
619 senselen = get_unaligned_be16(data);
620 if (datalen < senselen)
621 goto invalid_datalen;
623 memcpy(sc->sense_buffer, data + 2,
624 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
625 debug_scsi("copied %d bytes of sense\n",
626 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
629 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
630 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
631 int res_count = be32_to_cpu(rhdr->bi_residual_count);
633 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
634 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
635 res_count <= scsi_in(sc)->length))
636 scsi_in(sc)->resid = res_count;
638 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
641 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
642 ISCSI_FLAG_CMD_OVERFLOW)) {
643 int res_count = be32_to_cpu(rhdr->residual_count);
646 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
647 res_count <= scsi_bufflen(sc)))
648 /* write side for bidi or uni-io set_resid */
649 scsi_set_resid(sc, res_count);
651 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
654 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
655 (long)sc, sc->result, task->itt);
656 conn->scsirsp_pdus_cnt++;
658 __iscsi_put_task(task);
662 * iscsi_data_in_rsp - SCSI Data-In Response processing
663 * @conn: iscsi connection
665 * @task: scsi command task
668 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
669 struct iscsi_task *task)
671 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
672 struct scsi_cmnd *sc = task->sc;
674 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
677 sc->result = (DID_OK << 16) | rhdr->cmd_status;
678 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
679 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
680 ISCSI_FLAG_DATA_OVERFLOW)) {
681 int res_count = be32_to_cpu(rhdr->residual_count);
684 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
685 res_count <= scsi_in(sc)->length))
686 scsi_in(sc)->resid = res_count;
688 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
691 conn->scsirsp_pdus_cnt++;
692 __iscsi_put_task(task);
695 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
697 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
699 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
700 conn->tmfrsp_pdus_cnt++;
702 if (conn->tmf_state != TMF_QUEUED)
705 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
706 conn->tmf_state = TMF_SUCCESS;
707 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
708 conn->tmf_state = TMF_NOT_FOUND;
710 conn->tmf_state = TMF_FAILED;
711 wake_up(&conn->ehwait);
714 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
716 struct iscsi_nopout hdr;
717 struct iscsi_task *task;
719 if (!rhdr && conn->ping_task)
722 memset(&hdr, 0, sizeof(struct iscsi_nopout));
723 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
724 hdr.flags = ISCSI_FLAG_CMD_FINAL;
727 memcpy(hdr.lun, rhdr->lun, 8);
729 hdr.itt = RESERVED_ITT;
731 hdr.ttt = RESERVED_ITT;
733 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
735 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
737 /* only track our nops */
738 conn->ping_task = task;
739 conn->last_ping = jiffies;
743 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
744 char *data, int datalen)
746 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
747 struct iscsi_hdr rejected_pdu;
750 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
752 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
753 if (ntoh24(reject->dlength) > datalen)
754 return ISCSI_ERR_PROTO;
756 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
757 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
758 itt = get_itt(rejected_pdu.itt);
759 iscsi_conn_printk(KERN_ERR, conn,
760 "itt 0x%x had pdu (op 0x%x) rejected "
761 "due to DataDigest error.\n", itt,
762 rejected_pdu.opcode);
769 * iscsi_itt_to_task - look up task by itt
770 * @conn: iscsi connection
773 * This should be used for mgmt tasks like login and nops, or if
774 * the LDD's itt space does not include the session age.
776 * The session lock must be held.
778 static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
780 struct iscsi_session *session = conn->session;
783 if (itt == RESERVED_ITT)
787 if (i >= session->cmds_max)
790 return session->cmds[i];
794 * __iscsi_complete_pdu - complete pdu
798 * @datalen: len of data buffer
800 * Completes pdu processing by freeing any resources allocated at
801 * queuecommand or send generic. session lock must be held and verify
802 * itt must have been called.
804 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
805 char *data, int datalen)
807 struct iscsi_session *session = conn->session;
808 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
809 struct iscsi_task *task;
812 conn->last_recv = jiffies;
813 rc = iscsi_verify_itt(conn, hdr->itt);
817 if (hdr->itt != RESERVED_ITT)
818 itt = get_itt(hdr->itt);
822 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
823 opcode, conn->id, itt, datalen);
826 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
829 case ISCSI_OP_NOOP_IN:
831 rc = ISCSI_ERR_PROTO;
835 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
838 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
840 case ISCSI_OP_REJECT:
841 rc = iscsi_handle_reject(conn, hdr, data, datalen);
843 case ISCSI_OP_ASYNC_EVENT:
844 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
845 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
846 rc = ISCSI_ERR_CONN_FAILED;
849 rc = ISCSI_ERR_BAD_OPCODE;
856 case ISCSI_OP_SCSI_CMD_RSP:
857 case ISCSI_OP_SCSI_DATA_IN:
858 task = iscsi_itt_to_ctask(conn, hdr->itt);
860 return ISCSI_ERR_BAD_ITT;
864 * LLD handles R2Ts if they need to.
867 case ISCSI_OP_LOGOUT_RSP:
868 case ISCSI_OP_LOGIN_RSP:
869 case ISCSI_OP_TEXT_RSP:
870 case ISCSI_OP_SCSI_TMFUNC_RSP:
871 case ISCSI_OP_NOOP_IN:
872 task = iscsi_itt_to_task(conn, hdr->itt);
874 return ISCSI_ERR_BAD_ITT;
877 return ISCSI_ERR_BAD_OPCODE;
881 case ISCSI_OP_SCSI_CMD_RSP:
882 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
884 case ISCSI_OP_SCSI_DATA_IN:
885 iscsi_data_in_rsp(conn, hdr, task);
887 case ISCSI_OP_LOGOUT_RSP:
888 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
890 rc = ISCSI_ERR_PROTO;
893 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
895 case ISCSI_OP_LOGIN_RSP:
896 case ISCSI_OP_TEXT_RSP:
897 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
899 * login related PDU's exp_statsn is handled in
903 case ISCSI_OP_SCSI_TMFUNC_RSP:
904 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
906 rc = ISCSI_ERR_PROTO;
910 iscsi_tmf_rsp(conn, hdr);
911 __iscsi_put_task(task);
913 case ISCSI_OP_NOOP_IN:
914 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
915 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
916 rc = ISCSI_ERR_PROTO;
919 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
921 if (conn->ping_task != task)
923 * If this is not in response to one of our
924 * nops then it must be from userspace.
928 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
929 __iscsi_put_task(task);
932 rc = ISCSI_ERR_BAD_OPCODE;
939 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
940 rc = ISCSI_ERR_CONN_FAILED;
941 __iscsi_put_task(task);
944 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
946 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
947 char *data, int datalen)
951 spin_lock(&conn->session->lock);
952 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
953 spin_unlock(&conn->session->lock);
956 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
958 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
960 struct iscsi_session *session = conn->session;
963 if (itt == RESERVED_ITT)
966 if (((__force u32)itt & ISCSI_AGE_MASK) !=
967 (session->age << ISCSI_AGE_SHIFT)) {
968 iscsi_conn_printk(KERN_ERR, conn,
969 "received itt %x expected session age (%x)\n",
970 (__force u32)itt, session->age);
971 return ISCSI_ERR_BAD_ITT;
975 if (i >= session->cmds_max) {
976 iscsi_conn_printk(KERN_ERR, conn,
977 "received invalid itt index %u (max cmds "
978 "%u.\n", i, session->cmds_max);
979 return ISCSI_ERR_BAD_ITT;
983 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
986 * iscsi_itt_to_ctask - look up ctask by itt
987 * @conn: iscsi connection
990 * This should be used for cmd tasks.
992 * The session lock must be held.
994 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
996 struct iscsi_task *task;
998 if (iscsi_verify_itt(conn, itt))
1001 task = iscsi_itt_to_task(conn, itt);
1002 if (!task || !task->sc)
1005 if (task->sc->SCp.phase != conn->session->age) {
1006 iscsi_session_printk(KERN_ERR, conn->session,
1007 "task's session age %d, expected %d\n",
1008 task->sc->SCp.phase, conn->session->age);
1014 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1016 void iscsi_session_failure(struct iscsi_cls_session *cls_session,
1019 struct iscsi_session *session = cls_session->dd_data;
1020 struct iscsi_conn *conn;
1022 unsigned long flags;
1024 spin_lock_irqsave(&session->lock, flags);
1025 conn = session->leadconn;
1026 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1027 spin_unlock_irqrestore(&session->lock, flags);
1031 dev = get_device(&conn->cls_conn->dev);
1032 spin_unlock_irqrestore(&session->lock, flags);
1036 * if the host is being removed bypass the connection
1037 * recovery initialization because we are going to kill
1040 if (err == ISCSI_ERR_INVALID_HOST)
1041 iscsi_conn_error_event(conn->cls_conn, err);
1043 iscsi_conn_failure(conn, err);
1046 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1048 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1050 struct iscsi_session *session = conn->session;
1051 unsigned long flags;
1053 spin_lock_irqsave(&session->lock, flags);
1054 if (session->state == ISCSI_STATE_FAILED) {
1055 spin_unlock_irqrestore(&session->lock, flags);
1059 if (conn->stop_stage == 0)
1060 session->state = ISCSI_STATE_FAILED;
1061 spin_unlock_irqrestore(&session->lock, flags);
1063 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1064 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1065 iscsi_conn_error_event(conn->cls_conn, err);
1067 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1069 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1071 struct iscsi_session *session = conn->session;
1074 * Check for iSCSI window and take care of CmdSN wrap-around
1076 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1077 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
1078 "CmdSN %u/%u\n", session->exp_cmdsn,
1079 session->max_cmdsn, session->cmdsn,
1080 session->queued_cmdsn);
1086 static int iscsi_xmit_task(struct iscsi_conn *conn)
1088 struct iscsi_task *task = conn->task;
1091 __iscsi_get_task(task);
1092 spin_unlock_bh(&conn->session->lock);
1093 rc = conn->session->tt->xmit_task(task);
1094 spin_lock_bh(&conn->session->lock);
1095 __iscsi_put_task(task);
1097 /* done with this task */
1103 * iscsi_requeue_task - requeue task to run from session workqueue
1104 * @task: task to requeue
1106 * LLDs that need to run a task from the session workqueue should call
1107 * this. The session lock must be held. This should only be called
1108 * by software drivers.
1110 void iscsi_requeue_task(struct iscsi_task *task)
1112 struct iscsi_conn *conn = task->conn;
1114 list_move_tail(&task->running, &conn->requeue);
1115 scsi_queue_work(conn->session->host, &conn->xmitwork);
1117 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1120 * iscsi_data_xmit - xmit any command into the scheduled connection
1121 * @conn: iscsi connection
1124 * The function can return -EAGAIN in which case the caller must
1125 * re-schedule it again later or recover. '0' return code means
1128 static int iscsi_data_xmit(struct iscsi_conn *conn)
1132 spin_lock_bh(&conn->session->lock);
1133 if (unlikely(conn->suspend_tx)) {
1134 debug_scsi("conn %d Tx suspended!\n", conn->id);
1135 spin_unlock_bh(&conn->session->lock);
1140 rc = iscsi_xmit_task(conn);
1146 * process mgmt pdus like nops before commands since we should
1147 * only have one nop-out as a ping from us and targets should not
1148 * overflow us with nop-ins
1151 while (!list_empty(&conn->mgmtqueue)) {
1152 conn->task = list_entry(conn->mgmtqueue.next,
1153 struct iscsi_task, running);
1154 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1155 __iscsi_put_task(conn->task);
1159 rc = iscsi_xmit_task(conn);
1164 /* process pending command queue */
1165 while (!list_empty(&conn->xmitqueue)) {
1166 if (conn->tmf_state == TMF_QUEUED)
1169 conn->task = list_entry(conn->xmitqueue.next,
1170 struct iscsi_task, running);
1171 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1172 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
1175 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1177 if (rc == -ENOMEM) {
1181 fail_command(conn, conn->task, DID_ABORT << 16);
1184 rc = iscsi_xmit_task(conn);
1188 * we could continuously get new task requests so
1189 * we need to check the mgmt queue for nops that need to
1190 * be sent to aviod starvation
1192 if (!list_empty(&conn->mgmtqueue))
1196 while (!list_empty(&conn->requeue)) {
1197 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1201 * we always do fastlogout - conn stop code will clean up.
1203 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1206 conn->task = list_entry(conn->requeue.next,
1207 struct iscsi_task, running);
1208 conn->task->state = ISCSI_TASK_RUNNING;
1209 list_move_tail(conn->requeue.next, &conn->run_list);
1210 rc = iscsi_xmit_task(conn);
1213 if (!list_empty(&conn->mgmtqueue))
1216 spin_unlock_bh(&conn->session->lock);
1220 if (unlikely(conn->suspend_tx))
1222 spin_unlock_bh(&conn->session->lock);
1226 static void iscsi_xmitworker(struct work_struct *work)
1228 struct iscsi_conn *conn =
1229 container_of(work, struct iscsi_conn, xmitwork);
1232 * serialize Xmit worker on a per-connection basis.
1235 rc = iscsi_data_xmit(conn);
1236 } while (rc >= 0 || rc == -EAGAIN);
1239 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1240 struct scsi_cmnd *sc)
1242 struct iscsi_task *task;
1244 if (!__kfifo_get(conn->session->cmdpool.queue,
1245 (void *) &task, sizeof(void *)))
1248 sc->SCp.phase = conn->session->age;
1249 sc->SCp.ptr = (char *) task;
1251 atomic_set(&task->refcount, 1);
1252 task->state = ISCSI_TASK_PENDING;
1255 INIT_LIST_HEAD(&task->running);
1260 FAILURE_BAD_HOST = 1,
1261 FAILURE_SESSION_FAILED,
1262 FAILURE_SESSION_FREED,
1263 FAILURE_WINDOW_CLOSED,
1265 FAILURE_SESSION_TERMINATE,
1266 FAILURE_SESSION_IN_RECOVERY,
1267 FAILURE_SESSION_RECOVERY_TIMEOUT,
1268 FAILURE_SESSION_LOGGING_OUT,
1269 FAILURE_SESSION_NOT_READY,
1272 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1274 struct iscsi_cls_session *cls_session;
1275 struct Scsi_Host *host;
1277 struct iscsi_session *session;
1278 struct iscsi_conn *conn;
1279 struct iscsi_task *task = NULL;
1281 sc->scsi_done = done;
1285 host = sc->device->host;
1286 spin_unlock(host->host_lock);
1288 cls_session = starget_to_session(scsi_target(sc->device));
1289 session = cls_session->dd_data;
1290 spin_lock(&session->lock);
1292 reason = iscsi_session_chkready(cls_session);
1294 sc->result = reason;
1299 * ISCSI_STATE_FAILED is a temp. state. The recovery
1300 * code will decide what is best to do with command queued
1303 if (session->state != ISCSI_STATE_LOGGED_IN &&
1304 session->state != ISCSI_STATE_FAILED) {
1306 * to handle the race between when we set the recovery state
1307 * and block the session we requeue here (commands could
1308 * be entering our queuecommand while a block is starting
1309 * up because the block code is not locked)
1311 switch (session->state) {
1312 case ISCSI_STATE_IN_RECOVERY:
1313 reason = FAILURE_SESSION_IN_RECOVERY;
1315 case ISCSI_STATE_LOGGING_OUT:
1316 reason = FAILURE_SESSION_LOGGING_OUT;
1318 case ISCSI_STATE_RECOVERY_FAILED:
1319 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1320 sc->result = DID_TRANSPORT_FAILFAST << 16;
1322 case ISCSI_STATE_TERMINATE:
1323 reason = FAILURE_SESSION_TERMINATE;
1324 sc->result = DID_NO_CONNECT << 16;
1327 reason = FAILURE_SESSION_FREED;
1328 sc->result = DID_NO_CONNECT << 16;
1333 conn = session->leadconn;
1335 reason = FAILURE_SESSION_FREED;
1336 sc->result = DID_NO_CONNECT << 16;
1340 if (iscsi_check_cmdsn_window_closed(conn)) {
1341 reason = FAILURE_WINDOW_CLOSED;
1345 task = iscsi_alloc_task(conn, sc);
1347 reason = FAILURE_OOM;
1350 list_add_tail(&task->running, &conn->xmitqueue);
1352 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
1353 reason = iscsi_prep_scsi_cmd_pdu(task);
1355 if (reason == -ENOMEM) {
1356 reason = FAILURE_OOM;
1359 sc->result = DID_ABORT << 16;
1363 if (session->tt->xmit_task(task)) {
1364 reason = FAILURE_SESSION_NOT_READY;
1368 scsi_queue_work(session->host, &conn->xmitwork);
1370 session->queued_cmdsn++;
1371 spin_unlock(&session->lock);
1372 spin_lock(host->host_lock);
1376 sc->scsi_done = NULL;
1377 iscsi_complete_command(task);
1379 spin_unlock(&session->lock);
1380 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1381 spin_lock(host->host_lock);
1382 return SCSI_MLQUEUE_TARGET_BUSY;
1385 sc->scsi_done = NULL;
1386 iscsi_complete_command(task);
1388 spin_unlock(&session->lock);
1389 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
1390 if (!scsi_bidi_cmnd(sc))
1391 scsi_set_resid(sc, scsi_bufflen(sc));
1393 scsi_out(sc)->resid = scsi_out(sc)->length;
1394 scsi_in(sc)->resid = scsi_in(sc)->length;
1397 spin_lock(host->host_lock);
1400 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1402 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1404 if (depth > ISCSI_MAX_CMD_PER_LUN)
1405 depth = ISCSI_MAX_CMD_PER_LUN;
1406 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1407 return sdev->queue_depth;
1409 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1411 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1413 struct iscsi_session *session = cls_session->dd_data;
1415 spin_lock_bh(&session->lock);
1416 if (session->state != ISCSI_STATE_LOGGED_IN) {
1417 session->state = ISCSI_STATE_RECOVERY_FAILED;
1418 if (session->leadconn)
1419 wake_up(&session->leadconn->ehwait);
1421 spin_unlock_bh(&session->lock);
1423 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1425 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
1427 struct iscsi_cls_session *cls_session;
1428 struct iscsi_session *session;
1429 struct iscsi_conn *conn;
1431 cls_session = starget_to_session(scsi_target(sc->device));
1432 session = cls_session->dd_data;
1433 conn = session->leadconn;
1435 mutex_lock(&session->eh_mutex);
1436 spin_lock_bh(&session->lock);
1437 if (session->state == ISCSI_STATE_TERMINATE) {
1439 debug_scsi("failing target reset: session terminated "
1440 "[CID %d age %d]\n", conn->id, session->age);
1441 spin_unlock_bh(&session->lock);
1442 mutex_unlock(&session->eh_mutex);
1446 spin_unlock_bh(&session->lock);
1447 mutex_unlock(&session->eh_mutex);
1449 * we drop the lock here but the leadconn cannot be destoyed while
1450 * we are in the scsi eh
1452 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1454 debug_scsi("iscsi_eh_target_reset wait for relogin\n");
1455 wait_event_interruptible(conn->ehwait,
1456 session->state == ISCSI_STATE_TERMINATE ||
1457 session->state == ISCSI_STATE_LOGGED_IN ||
1458 session->state == ISCSI_STATE_RECOVERY_FAILED);
1459 if (signal_pending(current))
1460 flush_signals(current);
1462 mutex_lock(&session->eh_mutex);
1463 spin_lock_bh(&session->lock);
1464 if (session->state == ISCSI_STATE_LOGGED_IN)
1465 iscsi_session_printk(KERN_INFO, session,
1466 "target reset succeeded\n");
1469 spin_unlock_bh(&session->lock);
1470 mutex_unlock(&session->eh_mutex);
1473 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
1475 static void iscsi_tmf_timedout(unsigned long data)
1477 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1478 struct iscsi_session *session = conn->session;
1480 spin_lock(&session->lock);
1481 if (conn->tmf_state == TMF_QUEUED) {
1482 conn->tmf_state = TMF_TIMEDOUT;
1483 debug_scsi("tmf timedout\n");
1484 /* unblock eh_abort() */
1485 wake_up(&conn->ehwait);
1487 spin_unlock(&session->lock);
1490 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1491 struct iscsi_tm *hdr, int age,
1494 struct iscsi_session *session = conn->session;
1495 struct iscsi_task *task;
1497 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1500 spin_unlock_bh(&session->lock);
1501 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1502 spin_lock_bh(&session->lock);
1503 debug_scsi("tmf exec failure\n");
1506 conn->tmfcmd_pdus_cnt++;
1507 conn->tmf_timer.expires = timeout * HZ + jiffies;
1508 conn->tmf_timer.function = iscsi_tmf_timedout;
1509 conn->tmf_timer.data = (unsigned long)conn;
1510 add_timer(&conn->tmf_timer);
1511 debug_scsi("tmf set timeout\n");
1513 spin_unlock_bh(&session->lock);
1514 mutex_unlock(&session->eh_mutex);
1517 * block eh thread until:
1521 * 3) session is terminated or restarted or userspace has
1522 * given up on recovery
1524 wait_event_interruptible(conn->ehwait, age != session->age ||
1525 session->state != ISCSI_STATE_LOGGED_IN ||
1526 conn->tmf_state != TMF_QUEUED);
1527 if (signal_pending(current))
1528 flush_signals(current);
1529 del_timer_sync(&conn->tmf_timer);
1531 mutex_lock(&session->eh_mutex);
1532 spin_lock_bh(&session->lock);
1533 /* if the session drops it will clean up the task */
1534 if (age != session->age ||
1535 session->state != ISCSI_STATE_LOGGED_IN)
1541 * Fail commands. session lock held and recv side suspended and xmit
1544 static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1547 struct iscsi_task *task, *tmp;
1549 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1553 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1554 if (lun == task->sc->device->lun || lun == -1) {
1555 debug_scsi("failing pending sc %p itt 0x%x\n",
1556 task->sc, task->itt);
1557 fail_command(conn, task, error << 16);
1561 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1562 if (lun == task->sc->device->lun || lun == -1) {
1563 debug_scsi("failing requeued sc %p itt 0x%x\n",
1564 task->sc, task->itt);
1565 fail_command(conn, task, error << 16);
1569 /* fail all other running */
1570 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1571 if (lun == task->sc->device->lun || lun == -1) {
1572 debug_scsi("failing in progress sc %p itt 0x%x\n",
1573 task->sc, task->itt);
1574 fail_command(conn, task, error << 16);
1579 void iscsi_suspend_tx(struct iscsi_conn *conn)
1581 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1582 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1583 scsi_flush_work(conn->session->host);
1585 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1587 static void iscsi_start_tx(struct iscsi_conn *conn)
1589 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1590 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1591 scsi_queue_work(conn->session->host, &conn->xmitwork);
1594 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1596 struct iscsi_cls_session *cls_session;
1597 struct iscsi_session *session;
1598 struct iscsi_conn *conn;
1599 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1601 cls_session = starget_to_session(scsi_target(scmd->device));
1602 session = cls_session->dd_data;
1604 debug_scsi("scsi cmd %p timedout\n", scmd);
1606 spin_lock(&session->lock);
1607 if (session->state != ISCSI_STATE_LOGGED_IN) {
1609 * We are probably in the middle of iscsi recovery so let
1610 * that complete and handle the error.
1612 rc = BLK_EH_RESET_TIMER;
1616 conn = session->leadconn;
1618 /* In the middle of shuting down */
1619 rc = BLK_EH_RESET_TIMER;
1623 if (!conn->recv_timeout && !conn->ping_timeout)
1626 * if the ping timedout then we are in the middle of cleaning up
1627 * and can let the iscsi eh handle it
1629 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1630 (conn->ping_timeout * HZ), jiffies))
1631 rc = BLK_EH_RESET_TIMER;
1633 * if we are about to check the transport then give the command
1636 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1638 rc = BLK_EH_RESET_TIMER;
1639 /* if in the middle of checking the transport then give us more time */
1640 if (conn->ping_task)
1641 rc = BLK_EH_RESET_TIMER;
1643 spin_unlock(&session->lock);
1644 debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
1645 "timer reset" : "nh");
1649 static void iscsi_check_transport_timeouts(unsigned long data)
1651 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1652 struct iscsi_session *session = conn->session;
1653 unsigned long recv_timeout, next_timeout = 0, last_recv;
1655 spin_lock(&session->lock);
1656 if (session->state != ISCSI_STATE_LOGGED_IN)
1659 recv_timeout = conn->recv_timeout;
1664 last_recv = conn->last_recv;
1665 if (conn->ping_task &&
1666 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
1668 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1669 "expired, last rx %lu, last ping %lu, "
1670 "now %lu\n", conn->ping_timeout, last_recv,
1671 conn->last_ping, jiffies);
1672 spin_unlock(&session->lock);
1673 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1677 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1678 /* send a ping to try to provoke some traffic */
1679 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1680 iscsi_send_nopout(conn, NULL);
1681 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1683 next_timeout = last_recv + recv_timeout;
1685 debug_scsi("Setting next tmo %lu\n", next_timeout);
1686 mod_timer(&conn->transport_timer, next_timeout);
1688 spin_unlock(&session->lock);
1691 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
1692 struct iscsi_tm *hdr)
1694 memset(hdr, 0, sizeof(*hdr));
1695 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1696 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1697 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1698 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1699 hdr->rtt = task->hdr_itt;
1700 hdr->refcmdsn = task->cmdsn;
1703 int iscsi_eh_abort(struct scsi_cmnd *sc)
1705 struct iscsi_cls_session *cls_session;
1706 struct iscsi_session *session;
1707 struct iscsi_conn *conn;
1708 struct iscsi_task *task;
1709 struct iscsi_tm *hdr;
1712 cls_session = starget_to_session(scsi_target(sc->device));
1713 session = cls_session->dd_data;
1715 mutex_lock(&session->eh_mutex);
1716 spin_lock_bh(&session->lock);
1718 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1722 debug_scsi("sc never reached iscsi layer or it completed.\n");
1723 spin_unlock_bh(&session->lock);
1724 mutex_unlock(&session->eh_mutex);
1729 * If we are not logged in or we have started a new session
1730 * then let the host reset code handle this
1732 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1733 sc->SCp.phase != session->age) {
1734 spin_unlock_bh(&session->lock);
1735 mutex_unlock(&session->eh_mutex);
1739 conn = session->leadconn;
1740 conn->eh_abort_cnt++;
1743 task = (struct iscsi_task *)sc->SCp.ptr;
1744 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
1746 /* task completed before time out */
1748 debug_scsi("sc completed while abort in progress\n");
1752 if (task->state == ISCSI_TASK_PENDING) {
1753 fail_command(conn, task, DID_ABORT << 16);
1757 /* only have one tmf outstanding at a time */
1758 if (conn->tmf_state != TMF_INITIAL)
1760 conn->tmf_state = TMF_QUEUED;
1763 iscsi_prep_abort_task_pdu(task, hdr);
1765 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1770 switch (conn->tmf_state) {
1772 spin_unlock_bh(&session->lock);
1774 * stop tx side incase the target had sent a abort rsp but
1775 * the initiator was still writing out data.
1777 iscsi_suspend_tx(conn);
1779 * we do not stop the recv side because targets have been
1780 * good and have never sent us a successful tmf response
1781 * then sent more data for the cmd.
1783 spin_lock(&session->lock);
1784 fail_command(conn, task, DID_ABORT << 16);
1785 conn->tmf_state = TMF_INITIAL;
1786 spin_unlock(&session->lock);
1787 iscsi_start_tx(conn);
1788 goto success_unlocked;
1790 spin_unlock_bh(&session->lock);
1791 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1792 goto failed_unlocked;
1795 conn->tmf_state = TMF_INITIAL;
1796 /* task completed before tmf abort response */
1797 debug_scsi("sc completed while abort in progress\n");
1802 conn->tmf_state = TMF_INITIAL;
1807 spin_unlock_bh(&session->lock);
1809 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
1810 mutex_unlock(&session->eh_mutex);
1814 spin_unlock_bh(&session->lock);
1816 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1817 task ? task->itt : 0);
1818 mutex_unlock(&session->eh_mutex);
1821 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1823 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1825 memset(hdr, 0, sizeof(*hdr));
1826 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1827 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1828 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1829 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1830 hdr->rtt = RESERVED_ITT;
1833 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1835 struct iscsi_cls_session *cls_session;
1836 struct iscsi_session *session;
1837 struct iscsi_conn *conn;
1838 struct iscsi_tm *hdr;
1841 cls_session = starget_to_session(scsi_target(sc->device));
1842 session = cls_session->dd_data;
1844 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1846 mutex_lock(&session->eh_mutex);
1847 spin_lock_bh(&session->lock);
1849 * Just check if we are not logged in. We cannot check for
1850 * the phase because the reset could come from a ioctl.
1852 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1854 conn = session->leadconn;
1856 /* only have one tmf outstanding at a time */
1857 if (conn->tmf_state != TMF_INITIAL)
1859 conn->tmf_state = TMF_QUEUED;
1862 iscsi_prep_lun_reset_pdu(sc, hdr);
1864 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1865 session->lu_reset_timeout)) {
1870 switch (conn->tmf_state) {
1874 spin_unlock_bh(&session->lock);
1875 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1878 conn->tmf_state = TMF_INITIAL;
1883 spin_unlock_bh(&session->lock);
1885 iscsi_suspend_tx(conn);
1887 spin_lock_bh(&session->lock);
1888 fail_all_commands(conn, sc->device->lun, DID_ERROR);
1889 conn->tmf_state = TMF_INITIAL;
1890 spin_unlock_bh(&session->lock);
1892 iscsi_start_tx(conn);
1896 spin_unlock_bh(&session->lock);
1898 debug_scsi("iscsi_eh_device_reset %s\n",
1899 rc == SUCCESS ? "SUCCESS" : "FAILED");
1900 mutex_unlock(&session->eh_mutex);
1903 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1906 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1907 * should be accessed via kfifo_{get,put} on q->queue.
1908 * Optionally, the caller can obtain the array of object pointers
1909 * by passing in a non-NULL @items pointer
1912 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1914 int i, num_arrays = 1;
1916 memset(q, 0, sizeof(*q));
1920 /* If the user passed an items pointer, he wants a copy of
1924 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1925 if (q->pool == NULL)
1928 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1930 if (q->queue == ERR_PTR(-ENOMEM))
1933 for (i = 0; i < max; i++) {
1934 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
1935 if (q->pool[i] == NULL) {
1939 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1943 *items = q->pool + max;
1944 memcpy(*items, q->pool, max * sizeof(void *));
1953 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1955 void iscsi_pool_free(struct iscsi_pool *q)
1959 for (i = 0; i < q->max; i++)
1964 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1967 * iscsi_host_add - add host to system
1969 * @pdev: parent device
1971 * This should be called by partial offload and software iscsi drivers
1972 * to add a host to the system.
1974 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
1976 if (!shost->can_queue)
1977 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
1979 return scsi_add_host(shost, pdev);
1981 EXPORT_SYMBOL_GPL(iscsi_host_add);
1984 * iscsi_host_alloc - allocate a host and driver data
1985 * @sht: scsi host template
1986 * @dd_data_size: driver host data size
1987 * @qdepth: default device queue depth
1989 * This should be called by partial offload and software iscsi drivers.
1990 * To access the driver specific memory use the iscsi_host_priv() macro.
1992 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1993 int dd_data_size, uint16_t qdepth)
1995 struct Scsi_Host *shost;
1996 struct iscsi_host *ihost;
1998 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2001 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2003 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
2005 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
2006 "Queue depth must be between 1 and %d.\n",
2007 qdepth, ISCSI_MAX_CMD_PER_LUN);
2008 qdepth = ISCSI_DEF_CMD_PER_LUN;
2010 shost->cmd_per_lun = qdepth;
2012 ihost = shost_priv(shost);
2013 spin_lock_init(&ihost->lock);
2014 ihost->state = ISCSI_HOST_SETUP;
2015 ihost->num_sessions = 0;
2016 init_waitqueue_head(&ihost->session_removal_wq);
2019 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2021 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2023 iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
2027 * iscsi_host_remove - remove host and sessions
2030 * If there are any sessions left, this will initiate the removal and wait
2031 * for the completion.
2033 void iscsi_host_remove(struct Scsi_Host *shost)
2035 struct iscsi_host *ihost = shost_priv(shost);
2036 unsigned long flags;
2038 spin_lock_irqsave(&ihost->lock, flags);
2039 ihost->state = ISCSI_HOST_REMOVED;
2040 spin_unlock_irqrestore(&ihost->lock, flags);
2042 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2043 wait_event_interruptible(ihost->session_removal_wq,
2044 ihost->num_sessions == 0);
2045 if (signal_pending(current))
2046 flush_signals(current);
2048 scsi_remove_host(shost);
2050 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2052 void iscsi_host_free(struct Scsi_Host *shost)
2054 struct iscsi_host *ihost = shost_priv(shost);
2056 kfree(ihost->netdev);
2057 kfree(ihost->hwaddress);
2058 kfree(ihost->initiatorname);
2059 scsi_host_put(shost);
2061 EXPORT_SYMBOL_GPL(iscsi_host_free);
2063 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2065 struct iscsi_host *ihost = shost_priv(shost);
2066 unsigned long flags;
2068 shost = scsi_host_get(shost);
2070 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2071 "of session teardown event because host already "
2076 spin_lock_irqsave(&ihost->lock, flags);
2077 ihost->num_sessions--;
2078 if (ihost->num_sessions == 0)
2079 wake_up(&ihost->session_removal_wq);
2080 spin_unlock_irqrestore(&ihost->lock, flags);
2081 scsi_host_put(shost);
2085 * iscsi_session_setup - create iscsi cls session and host and session
2086 * @iscsit: iscsi transport template
2088 * @cmds_max: session can queue
2089 * @cmd_task_size: LLD task private data size
2090 * @initial_cmdsn: initial CmdSN
2092 * This can be used by software iscsi_transports that allocate
2093 * a session per scsi host.
2095 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2096 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2097 * for nop handling and login/logout requests.
2099 struct iscsi_cls_session *
2100 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2101 uint16_t cmds_max, int cmd_task_size,
2102 uint32_t initial_cmdsn, unsigned int id)
2104 struct iscsi_host *ihost = shost_priv(shost);
2105 struct iscsi_session *session;
2106 struct iscsi_cls_session *cls_session;
2107 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2108 unsigned long flags;
2110 spin_lock_irqsave(&ihost->lock, flags);
2111 if (ihost->state == ISCSI_HOST_REMOVED) {
2112 spin_unlock_irqrestore(&ihost->lock, flags);
2115 ihost->num_sessions++;
2116 spin_unlock_irqrestore(&ihost->lock, flags);
2119 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2121 * The iscsi layer needs some tasks for nop handling and tmfs,
2122 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2123 * + 1 command for scsi IO.
2125 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2126 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2127 "must be a power of two that is at least %d.\n",
2128 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2129 goto dec_session_count;
2132 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2133 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2134 "must be a power of 2 less than or equal to %d.\n",
2135 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2136 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2139 if (!is_power_of_2(total_cmds)) {
2140 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2141 "must be a power of 2.\n", total_cmds);
2142 total_cmds = rounddown_pow_of_two(total_cmds);
2143 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2145 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2148 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2150 cls_session = iscsi_alloc_session(shost, iscsit,
2151 sizeof(struct iscsi_session));
2153 goto dec_session_count;
2154 session = cls_session->dd_data;
2155 session->cls_session = cls_session;
2156 session->host = shost;
2157 session->state = ISCSI_STATE_FREE;
2158 session->fast_abort = 1;
2159 session->lu_reset_timeout = 15;
2160 session->abort_timeout = 10;
2161 session->scsi_cmds_max = scsi_cmds;
2162 session->cmds_max = total_cmds;
2163 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2164 session->exp_cmdsn = initial_cmdsn + 1;
2165 session->max_cmdsn = initial_cmdsn + 1;
2166 session->max_r2t = 1;
2167 session->tt = iscsit;
2168 mutex_init(&session->eh_mutex);
2169 spin_lock_init(&session->lock);
2171 /* initialize SCSI PDU commands pool */
2172 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2173 (void***)&session->cmds,
2174 cmd_task_size + sizeof(struct iscsi_task)))
2175 goto cmdpool_alloc_fail;
2177 /* pre-format cmds pool with ITT */
2178 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2179 struct iscsi_task *task = session->cmds[cmd_i];
2182 task->dd_data = &task[1];
2184 INIT_LIST_HEAD(&task->running);
2187 if (!try_module_get(iscsit->owner))
2188 goto module_get_fail;
2190 if (iscsi_add_session(cls_session, id))
2191 goto cls_session_fail;
2196 module_put(iscsit->owner);
2198 iscsi_pool_free(&session->cmdpool);
2200 iscsi_free_session(cls_session);
2202 iscsi_host_dec_session_cnt(shost);
2205 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2208 * iscsi_session_teardown - destroy session, host, and cls_session
2209 * @cls_session: iscsi session
2211 * The driver must have called iscsi_remove_session before
2214 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2216 struct iscsi_session *session = cls_session->dd_data;
2217 struct module *owner = cls_session->transport->owner;
2218 struct Scsi_Host *shost = session->host;
2220 iscsi_pool_free(&session->cmdpool);
2222 kfree(session->password);
2223 kfree(session->password_in);
2224 kfree(session->username);
2225 kfree(session->username_in);
2226 kfree(session->targetname);
2227 kfree(session->initiatorname);
2228 kfree(session->ifacename);
2230 iscsi_destroy_session(cls_session);
2231 iscsi_host_dec_session_cnt(shost);
2234 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2237 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2238 * @cls_session: iscsi_cls_session
2239 * @dd_size: private driver data size
2242 struct iscsi_cls_conn *
2243 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2246 struct iscsi_session *session = cls_session->dd_data;
2247 struct iscsi_conn *conn;
2248 struct iscsi_cls_conn *cls_conn;
2251 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2255 conn = cls_conn->dd_data;
2256 memset(conn, 0, sizeof(*conn) + dd_size);
2258 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2259 conn->session = session;
2260 conn->cls_conn = cls_conn;
2261 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2262 conn->id = conn_idx;
2263 conn->exp_statsn = 0;
2264 conn->tmf_state = TMF_INITIAL;
2266 init_timer(&conn->transport_timer);
2267 conn->transport_timer.data = (unsigned long)conn;
2268 conn->transport_timer.function = iscsi_check_transport_timeouts;
2270 INIT_LIST_HEAD(&conn->run_list);
2271 INIT_LIST_HEAD(&conn->mgmt_run_list);
2272 INIT_LIST_HEAD(&conn->mgmtqueue);
2273 INIT_LIST_HEAD(&conn->xmitqueue);
2274 INIT_LIST_HEAD(&conn->requeue);
2275 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2277 /* allocate login_task used for the login/text sequences */
2278 spin_lock_bh(&session->lock);
2279 if (!__kfifo_get(session->cmdpool.queue,
2280 (void*)&conn->login_task,
2282 spin_unlock_bh(&session->lock);
2283 goto login_task_alloc_fail;
2285 spin_unlock_bh(&session->lock);
2287 data = (char *) __get_free_pages(GFP_KERNEL,
2288 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2290 goto login_task_data_alloc_fail;
2291 conn->login_task->data = conn->data = data;
2293 init_timer(&conn->tmf_timer);
2294 init_waitqueue_head(&conn->ehwait);
2298 login_task_data_alloc_fail:
2299 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2301 login_task_alloc_fail:
2302 iscsi_destroy_conn(cls_conn);
2305 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2308 * iscsi_conn_teardown - teardown iscsi connection
2309 * cls_conn: iscsi class connection
2311 * TODO: we may need to make this into a two step process
2312 * like scsi-mls remove + put host
2314 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2316 struct iscsi_conn *conn = cls_conn->dd_data;
2317 struct iscsi_session *session = conn->session;
2318 unsigned long flags;
2320 del_timer_sync(&conn->transport_timer);
2322 spin_lock_bh(&session->lock);
2323 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2324 if (session->leadconn == conn) {
2326 * leading connection? then give up on recovery.
2328 session->state = ISCSI_STATE_TERMINATE;
2329 wake_up(&conn->ehwait);
2331 spin_unlock_bh(&session->lock);
2334 * Block until all in-progress commands for this connection
2338 spin_lock_irqsave(session->host->host_lock, flags);
2339 if (!session->host->host_busy) { /* OK for ERL == 0 */
2340 spin_unlock_irqrestore(session->host->host_lock, flags);
2343 spin_unlock_irqrestore(session->host->host_lock, flags);
2344 msleep_interruptible(500);
2345 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2346 "host_busy %d host_failed %d\n",
2347 session->host->host_busy,
2348 session->host->host_failed);
2350 * force eh_abort() to unblock
2352 wake_up(&conn->ehwait);
2355 /* flush queued up work because we free the connection below */
2356 iscsi_suspend_tx(conn);
2358 spin_lock_bh(&session->lock);
2359 free_pages((unsigned long) conn->data,
2360 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2361 kfree(conn->persistent_address);
2362 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2364 if (session->leadconn == conn)
2365 session->leadconn = NULL;
2366 spin_unlock_bh(&session->lock);
2368 iscsi_destroy_conn(cls_conn);
2370 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2372 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2374 struct iscsi_conn *conn = cls_conn->dd_data;
2375 struct iscsi_session *session = conn->session;
2378 iscsi_conn_printk(KERN_ERR, conn,
2379 "can't start unbound connection\n");
2383 if ((session->imm_data_en || !session->initial_r2t_en) &&
2384 session->first_burst > session->max_burst) {
2385 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2386 "first_burst %d max_burst %d\n",
2387 session->first_burst, session->max_burst);
2391 if (conn->ping_timeout && !conn->recv_timeout) {
2392 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2393 "zero. Using 5 seconds\n.");
2394 conn->recv_timeout = 5;
2397 if (conn->recv_timeout && !conn->ping_timeout) {
2398 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2399 "zero. Using 5 seconds.\n");
2400 conn->ping_timeout = 5;
2403 spin_lock_bh(&session->lock);
2404 conn->c_stage = ISCSI_CONN_STARTED;
2405 session->state = ISCSI_STATE_LOGGED_IN;
2406 session->queued_cmdsn = session->cmdsn;
2408 conn->last_recv = jiffies;
2409 conn->last_ping = jiffies;
2410 if (conn->recv_timeout && conn->ping_timeout)
2411 mod_timer(&conn->transport_timer,
2412 jiffies + (conn->recv_timeout * HZ));
2414 switch(conn->stop_stage) {
2415 case STOP_CONN_RECOVER:
2417 * unblock eh_abort() if it is blocked. re-try all
2418 * commands after successful recovery
2420 conn->stop_stage = 0;
2421 conn->tmf_state = TMF_INITIAL;
2423 if (session->age == 16)
2426 case STOP_CONN_TERM:
2427 conn->stop_stage = 0;
2432 spin_unlock_bh(&session->lock);
2434 iscsi_unblock_session(session->cls_session);
2435 wake_up(&conn->ehwait);
2438 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2441 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2443 struct iscsi_task *task, *tmp;
2445 /* handle pending */
2446 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2447 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2448 /* release ref from prep task */
2449 __iscsi_put_task(task);
2452 /* handle running */
2453 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2454 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2455 /* release ref from prep task */
2456 __iscsi_put_task(task);
2462 static void iscsi_start_session_recovery(struct iscsi_session *session,
2463 struct iscsi_conn *conn, int flag)
2467 del_timer_sync(&conn->transport_timer);
2469 mutex_lock(&session->eh_mutex);
2470 spin_lock_bh(&session->lock);
2471 if (conn->stop_stage == STOP_CONN_TERM) {
2472 spin_unlock_bh(&session->lock);
2473 mutex_unlock(&session->eh_mutex);
2478 * When this is called for the in_login state, we only want to clean
2479 * up the login task and connection. We do not need to block and set
2480 * the recovery state again
2482 if (flag == STOP_CONN_TERM)
2483 session->state = ISCSI_STATE_TERMINATE;
2484 else if (conn->stop_stage != STOP_CONN_RECOVER)
2485 session->state = ISCSI_STATE_IN_RECOVERY;
2487 old_stop_stage = conn->stop_stage;
2488 conn->stop_stage = flag;
2489 conn->c_stage = ISCSI_CONN_STOPPED;
2490 spin_unlock_bh(&session->lock);
2492 iscsi_suspend_tx(conn);
2494 * for connection level recovery we should not calculate
2495 * header digest. conn->hdr_size used for optimization
2496 * in hdr_extract() and will be re-negotiated at
2499 if (flag == STOP_CONN_RECOVER) {
2500 conn->hdrdgst_en = 0;
2501 conn->datadgst_en = 0;
2502 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2503 old_stop_stage != STOP_CONN_RECOVER) {
2504 debug_scsi("blocking session\n");
2505 iscsi_block_session(session->cls_session);
2512 spin_lock_bh(&session->lock);
2513 if (flag == STOP_CONN_RECOVER)
2514 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2516 fail_all_commands(conn, -1, DID_ERROR);
2517 flush_control_queues(session, conn);
2518 spin_unlock_bh(&session->lock);
2519 mutex_unlock(&session->eh_mutex);
2522 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2524 struct iscsi_conn *conn = cls_conn->dd_data;
2525 struct iscsi_session *session = conn->session;
2528 case STOP_CONN_RECOVER:
2529 case STOP_CONN_TERM:
2530 iscsi_start_session_recovery(session, conn, flag);
2533 iscsi_conn_printk(KERN_ERR, conn,
2534 "invalid stop flag %d\n", flag);
2537 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2539 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2540 struct iscsi_cls_conn *cls_conn, int is_leading)
2542 struct iscsi_session *session = cls_session->dd_data;
2543 struct iscsi_conn *conn = cls_conn->dd_data;
2545 spin_lock_bh(&session->lock);
2547 session->leadconn = conn;
2548 spin_unlock_bh(&session->lock);
2551 * Unblock xmitworker(), Login Phase will pass through.
2553 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2554 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2557 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2560 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2561 enum iscsi_param param, char *buf, int buflen)
2563 struct iscsi_conn *conn = cls_conn->dd_data;
2564 struct iscsi_session *session = conn->session;
2568 case ISCSI_PARAM_FAST_ABORT:
2569 sscanf(buf, "%d", &session->fast_abort);
2571 case ISCSI_PARAM_ABORT_TMO:
2572 sscanf(buf, "%d", &session->abort_timeout);
2574 case ISCSI_PARAM_LU_RESET_TMO:
2575 sscanf(buf, "%d", &session->lu_reset_timeout);
2577 case ISCSI_PARAM_PING_TMO:
2578 sscanf(buf, "%d", &conn->ping_timeout);
2580 case ISCSI_PARAM_RECV_TMO:
2581 sscanf(buf, "%d", &conn->recv_timeout);
2583 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2584 sscanf(buf, "%d", &conn->max_recv_dlength);
2586 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2587 sscanf(buf, "%d", &conn->max_xmit_dlength);
2589 case ISCSI_PARAM_HDRDGST_EN:
2590 sscanf(buf, "%d", &conn->hdrdgst_en);
2592 case ISCSI_PARAM_DATADGST_EN:
2593 sscanf(buf, "%d", &conn->datadgst_en);
2595 case ISCSI_PARAM_INITIAL_R2T_EN:
2596 sscanf(buf, "%d", &session->initial_r2t_en);
2598 case ISCSI_PARAM_MAX_R2T:
2599 sscanf(buf, "%d", &session->max_r2t);
2601 case ISCSI_PARAM_IMM_DATA_EN:
2602 sscanf(buf, "%d", &session->imm_data_en);
2604 case ISCSI_PARAM_FIRST_BURST:
2605 sscanf(buf, "%d", &session->first_burst);
2607 case ISCSI_PARAM_MAX_BURST:
2608 sscanf(buf, "%d", &session->max_burst);
2610 case ISCSI_PARAM_PDU_INORDER_EN:
2611 sscanf(buf, "%d", &session->pdu_inorder_en);
2613 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2614 sscanf(buf, "%d", &session->dataseq_inorder_en);
2616 case ISCSI_PARAM_ERL:
2617 sscanf(buf, "%d", &session->erl);
2619 case ISCSI_PARAM_IFMARKER_EN:
2620 sscanf(buf, "%d", &value);
2623 case ISCSI_PARAM_OFMARKER_EN:
2624 sscanf(buf, "%d", &value);
2627 case ISCSI_PARAM_EXP_STATSN:
2628 sscanf(buf, "%u", &conn->exp_statsn);
2630 case ISCSI_PARAM_USERNAME:
2631 kfree(session->username);
2632 session->username = kstrdup(buf, GFP_KERNEL);
2633 if (!session->username)
2636 case ISCSI_PARAM_USERNAME_IN:
2637 kfree(session->username_in);
2638 session->username_in = kstrdup(buf, GFP_KERNEL);
2639 if (!session->username_in)
2642 case ISCSI_PARAM_PASSWORD:
2643 kfree(session->password);
2644 session->password = kstrdup(buf, GFP_KERNEL);
2645 if (!session->password)
2648 case ISCSI_PARAM_PASSWORD_IN:
2649 kfree(session->password_in);
2650 session->password_in = kstrdup(buf, GFP_KERNEL);
2651 if (!session->password_in)
2654 case ISCSI_PARAM_TARGET_NAME:
2655 /* this should not change between logins */
2656 if (session->targetname)
2659 session->targetname = kstrdup(buf, GFP_KERNEL);
2660 if (!session->targetname)
2663 case ISCSI_PARAM_TPGT:
2664 sscanf(buf, "%d", &session->tpgt);
2666 case ISCSI_PARAM_PERSISTENT_PORT:
2667 sscanf(buf, "%d", &conn->persistent_port);
2669 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2671 * this is the address returned in discovery so it should
2672 * not change between logins.
2674 if (conn->persistent_address)
2677 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2678 if (!conn->persistent_address)
2681 case ISCSI_PARAM_IFACE_NAME:
2682 if (!session->ifacename)
2683 session->ifacename = kstrdup(buf, GFP_KERNEL);
2685 case ISCSI_PARAM_INITIATOR_NAME:
2686 if (!session->initiatorname)
2687 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2695 EXPORT_SYMBOL_GPL(iscsi_set_param);
2697 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2698 enum iscsi_param param, char *buf)
2700 struct iscsi_session *session = cls_session->dd_data;
2704 case ISCSI_PARAM_FAST_ABORT:
2705 len = sprintf(buf, "%d\n", session->fast_abort);
2707 case ISCSI_PARAM_ABORT_TMO:
2708 len = sprintf(buf, "%d\n", session->abort_timeout);
2710 case ISCSI_PARAM_LU_RESET_TMO:
2711 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2713 case ISCSI_PARAM_INITIAL_R2T_EN:
2714 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2716 case ISCSI_PARAM_MAX_R2T:
2717 len = sprintf(buf, "%hu\n", session->max_r2t);
2719 case ISCSI_PARAM_IMM_DATA_EN:
2720 len = sprintf(buf, "%d\n", session->imm_data_en);
2722 case ISCSI_PARAM_FIRST_BURST:
2723 len = sprintf(buf, "%u\n", session->first_burst);
2725 case ISCSI_PARAM_MAX_BURST:
2726 len = sprintf(buf, "%u\n", session->max_burst);
2728 case ISCSI_PARAM_PDU_INORDER_EN:
2729 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2731 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2732 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2734 case ISCSI_PARAM_ERL:
2735 len = sprintf(buf, "%d\n", session->erl);
2737 case ISCSI_PARAM_TARGET_NAME:
2738 len = sprintf(buf, "%s\n", session->targetname);
2740 case ISCSI_PARAM_TPGT:
2741 len = sprintf(buf, "%d\n", session->tpgt);
2743 case ISCSI_PARAM_USERNAME:
2744 len = sprintf(buf, "%s\n", session->username);
2746 case ISCSI_PARAM_USERNAME_IN:
2747 len = sprintf(buf, "%s\n", session->username_in);
2749 case ISCSI_PARAM_PASSWORD:
2750 len = sprintf(buf, "%s\n", session->password);
2752 case ISCSI_PARAM_PASSWORD_IN:
2753 len = sprintf(buf, "%s\n", session->password_in);
2755 case ISCSI_PARAM_IFACE_NAME:
2756 len = sprintf(buf, "%s\n", session->ifacename);
2758 case ISCSI_PARAM_INITIATOR_NAME:
2759 if (!session->initiatorname)
2760 len = sprintf(buf, "%s\n", "unknown");
2762 len = sprintf(buf, "%s\n", session->initiatorname);
2770 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2772 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2773 enum iscsi_param param, char *buf)
2775 struct iscsi_conn *conn = cls_conn->dd_data;
2779 case ISCSI_PARAM_PING_TMO:
2780 len = sprintf(buf, "%u\n", conn->ping_timeout);
2782 case ISCSI_PARAM_RECV_TMO:
2783 len = sprintf(buf, "%u\n", conn->recv_timeout);
2785 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2786 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2788 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2789 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2791 case ISCSI_PARAM_HDRDGST_EN:
2792 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2794 case ISCSI_PARAM_DATADGST_EN:
2795 len = sprintf(buf, "%d\n", conn->datadgst_en);
2797 case ISCSI_PARAM_IFMARKER_EN:
2798 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2800 case ISCSI_PARAM_OFMARKER_EN:
2801 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2803 case ISCSI_PARAM_EXP_STATSN:
2804 len = sprintf(buf, "%u\n", conn->exp_statsn);
2806 case ISCSI_PARAM_PERSISTENT_PORT:
2807 len = sprintf(buf, "%d\n", conn->persistent_port);
2809 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2810 len = sprintf(buf, "%s\n", conn->persistent_address);
2818 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2820 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2823 struct iscsi_host *ihost = shost_priv(shost);
2827 case ISCSI_HOST_PARAM_NETDEV_NAME:
2829 len = sprintf(buf, "%s\n", "default");
2831 len = sprintf(buf, "%s\n", ihost->netdev);
2833 case ISCSI_HOST_PARAM_HWADDRESS:
2834 if (!ihost->hwaddress)
2835 len = sprintf(buf, "%s\n", "default");
2837 len = sprintf(buf, "%s\n", ihost->hwaddress);
2839 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2840 if (!ihost->initiatorname)
2841 len = sprintf(buf, "%s\n", "unknown");
2843 len = sprintf(buf, "%s\n", ihost->initiatorname);
2845 case ISCSI_HOST_PARAM_IPADDRESS:
2846 if (!strlen(ihost->local_address))
2847 len = sprintf(buf, "%s\n", "unknown");
2849 len = sprintf(buf, "%s\n",
2850 ihost->local_address);
2858 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2860 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2861 char *buf, int buflen)
2863 struct iscsi_host *ihost = shost_priv(shost);
2866 case ISCSI_HOST_PARAM_NETDEV_NAME:
2868 ihost->netdev = kstrdup(buf, GFP_KERNEL);
2870 case ISCSI_HOST_PARAM_HWADDRESS:
2871 if (!ihost->hwaddress)
2872 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
2874 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2875 if (!ihost->initiatorname)
2876 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
2884 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2886 MODULE_AUTHOR("Mike Christie");
2887 MODULE_DESCRIPTION("iSCSI library functions");
2888 MODULE_LICENSE("GPL");