added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / scsi / libiscsi.c
blobca4467cb5df214347a4f31e864ab56ab4572a07e
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 /* 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)));
57 void
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))
68 return;
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
79 * xmit thread
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);
91 /**
92 * iscsi_prep_data_out_pdu - initialize Data-Out
93 * @task: scsi command task
94 * @r2t: R2T info
95 * @hdr: iscsi data in pdu
97 * Notes:
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));
112 hdr->ttt = r2t->ttt;
113 hdr->datasn = cpu_to_be32(r2t->datasn);
114 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;
123 hdr->flags = 0;
124 } else {
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) {
138 WARN_ON(1);
139 return -EINVAL;
142 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
143 task->hdr_len = exp_len;
144 return 0;
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;
156 int rc;
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);
168 if (rc)
169 return rc;
171 if (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);
183 return 0;
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;
190 int rc;
192 rlen_ahdr = iscsi_next_hdr(task);
193 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
194 if (rc)
195 return rc;
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));
208 return 0;
212 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
213 * @task: iscsi task
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;
225 itt_t itt;
226 int rc;
228 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
229 if (rc)
230 return rc;
231 hdr = (struct iscsi_cmd *) task->hdr;
232 itt = hdr->itt;
233 memset(hdr, 0, sizeof(*hdr));
235 if (session->tt->parse_pdu_itt)
236 hdr->itt = task->hdr_itt = itt;
237 else
238 hdr->itt = task->hdr_itt = build_itt(task->itt,
239 task->conn->session->age);
240 task->hdr_len = 0;
241 rc = iscsi_add_hdr(task, sizeof(*hdr));
242 if (rc)
243 return rc;
244 hdr->opcode = ISCSI_OP_SCSI_CMD;
245 hdr->flags = ISCSI_ATTR_SIMPLE;
246 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
247 memcpy(task->lun, hdr->lun, sizeof(task->lun));
248 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
249 session->cmdsn++;
250 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
251 cmd_len = sc->cmd_len;
252 if (cmd_len < ISCSI_CDB_SIZE)
253 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
254 else if (cmd_len > ISCSI_CDB_SIZE) {
255 rc = iscsi_prep_ecdb_ahs(task);
256 if (rc)
257 return rc;
258 cmd_len = ISCSI_CDB_SIZE;
260 memcpy(hdr->cdb, sc->cmnd, cmd_len);
262 task->imm_count = 0;
263 if (scsi_bidi_cmnd(sc)) {
264 hdr->flags |= ISCSI_FLAG_CMD_READ;
265 rc = iscsi_prep_bidi_ahs(task);
266 if (rc)
267 return rc;
269 if (sc->sc_data_direction == DMA_TO_DEVICE) {
270 unsigned out_len = scsi_out(sc)->length;
271 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
273 hdr->data_length = cpu_to_be32(out_len);
274 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
276 * Write counters:
278 * imm_count bytes to be sent right after
279 * SCSI PDU Header
281 * unsol_count bytes(as Data-Out) to be sent
282 * without R2T ack right after
283 * immediate data
285 * r2t data_length bytes to be sent via R2T ack's
287 * pad_count bytes to be sent as zero-padding
289 memset(r2t, 0, sizeof(*r2t));
291 if (session->imm_data_en) {
292 if (out_len >= session->first_burst)
293 task->imm_count = min(session->first_burst,
294 conn->max_xmit_dlength);
295 else
296 task->imm_count = min(out_len,
297 conn->max_xmit_dlength);
298 hton24(hdr->dlength, task->imm_count);
299 } else
300 zero_data(hdr->dlength);
302 if (!session->initial_r2t_en) {
303 r2t->data_length = min(session->first_burst, out_len) -
304 task->imm_count;
305 r2t->data_offset = task->imm_count;
306 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
307 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
310 if (!task->unsol_r2t.data_length)
311 /* No unsolicit Data-Out's */
312 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
313 } else {
314 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
315 zero_data(hdr->dlength);
316 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
318 if (sc->sc_data_direction == DMA_FROM_DEVICE)
319 hdr->flags |= ISCSI_FLAG_CMD_READ;
322 /* calculate size of additional header segments (AHSs) */
323 hdrlength = task->hdr_len - sizeof(*hdr);
325 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
326 hdrlength /= ISCSI_PAD_LEN;
328 WARN_ON(hdrlength >= 256);
329 hdr->hlength = hdrlength & 0xFF;
331 if (session->tt->init_task && session->tt->init_task(task))
332 return -EIO;
334 task->state = ISCSI_TASK_RUNNING;
335 list_move_tail(&task->running, &conn->run_list);
337 conn->scsicmd_pdus_cnt++;
338 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
339 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ?
340 "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ?
341 "write" : "read", conn->id, sc, sc->cmnd[0], task->itt,
342 scsi_bufflen(sc),
343 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
344 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
345 return 0;
349 * iscsi_complete_command - finish a task
350 * @task: iscsi cmd task
352 * Must be called with session lock.
353 * This function returns the scsi command to scsi-ml or cleans
354 * up mgmt tasks then returns the task to the pool.
356 static void iscsi_complete_command(struct iscsi_task *task)
358 struct iscsi_conn *conn = task->conn;
359 struct iscsi_session *session = conn->session;
360 struct scsi_cmnd *sc = task->sc;
362 session->tt->cleanup_task(task);
363 list_del_init(&task->running);
364 task->state = ISCSI_TASK_COMPLETED;
365 task->sc = NULL;
367 if (conn->task == task)
368 conn->task = NULL;
370 * login task is preallocated so do not free
372 if (conn->login_task == task)
373 return;
375 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
377 if (conn->ping_task == task)
378 conn->ping_task = NULL;
380 if (sc) {
381 task->sc = NULL;
382 /* SCSI eh reuses commands to verify us */
383 sc->SCp.ptr = NULL;
385 * queue command may call this to free the task, but
386 * not have setup the sc callback
388 if (sc->scsi_done)
389 sc->scsi_done(sc);
393 void __iscsi_get_task(struct iscsi_task *task)
395 atomic_inc(&task->refcount);
397 EXPORT_SYMBOL_GPL(__iscsi_get_task);
399 static void __iscsi_put_task(struct iscsi_task *task)
401 if (atomic_dec_and_test(&task->refcount))
402 iscsi_complete_command(task);
405 void iscsi_put_task(struct iscsi_task *task)
407 struct iscsi_session *session = task->conn->session;
409 spin_lock_bh(&session->lock);
410 __iscsi_put_task(task);
411 spin_unlock_bh(&session->lock);
413 EXPORT_SYMBOL_GPL(iscsi_put_task);
416 * session lock must be held
418 static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
419 int err)
421 struct scsi_cmnd *sc;
423 sc = task->sc;
424 if (!sc)
425 return;
427 if (task->state == ISCSI_TASK_PENDING)
429 * cmd never made it to the xmit thread, so we should not count
430 * the cmd in the sequencing
432 conn->session->queued_cmdsn--;
434 sc->result = err;
435 if (!scsi_bidi_cmnd(sc))
436 scsi_set_resid(sc, scsi_bufflen(sc));
437 else {
438 scsi_out(sc)->resid = scsi_out(sc)->length;
439 scsi_in(sc)->resid = scsi_in(sc)->length;
442 if (conn->task == task)
443 conn->task = NULL;
444 /* release ref from queuecommand */
445 __iscsi_put_task(task);
448 static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
449 struct iscsi_task *task)
451 struct iscsi_session *session = conn->session;
452 struct iscsi_hdr *hdr = task->hdr;
453 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
455 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
456 return -ENOTCONN;
458 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
459 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
460 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
462 * pre-format CmdSN for outgoing PDU.
464 nop->cmdsn = cpu_to_be32(session->cmdsn);
465 if (hdr->itt != RESERVED_ITT) {
467 * TODO: We always use immediate, so we never hit this.
468 * If we start to send tmfs or nops as non-immediate then
469 * we should start checking the cmdsn numbers for mgmt tasks.
471 if (conn->c_stage == ISCSI_CONN_STARTED &&
472 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
473 session->queued_cmdsn++;
474 session->cmdsn++;
478 if (session->tt->init_task && session->tt->init_task(task))
479 return -EIO;
481 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
482 session->state = ISCSI_STATE_LOGGING_OUT;
484 task->state = ISCSI_TASK_RUNNING;
485 list_move_tail(&task->running, &conn->mgmt_run_list);
486 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
487 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
488 task->data_count);
489 return 0;
492 static struct iscsi_task *
493 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
494 char *data, uint32_t data_size)
496 struct iscsi_session *session = conn->session;
497 struct iscsi_task *task;
498 itt_t itt;
500 if (session->state == ISCSI_STATE_TERMINATE)
501 return NULL;
503 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
504 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
506 * Login and Text are sent serially, in
507 * request-followed-by-response sequence.
508 * Same task can be used. Same ITT must be used.
509 * Note that login_task is preallocated at conn_create().
511 task = conn->login_task;
512 else {
513 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
514 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
516 if (!__kfifo_get(session->cmdpool.queue,
517 (void*)&task, sizeof(void*)))
518 return NULL;
521 * released in complete pdu for task we expect a response for, and
522 * released by the lld when it has transmitted the task for
523 * pdus we do not expect a response for.
525 atomic_set(&task->refcount, 1);
526 task->conn = conn;
527 task->sc = NULL;
529 if (data_size) {
530 memcpy(task->data, data, data_size);
531 task->data_count = data_size;
532 } else
533 task->data_count = 0;
535 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
536 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
537 "pdu for mgmt task.\n");
538 goto requeue_task;
540 itt = task->hdr->itt;
541 task->hdr_len = sizeof(struct iscsi_hdr);
542 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
544 if (hdr->itt != RESERVED_ITT) {
545 if (session->tt->parse_pdu_itt)
546 task->hdr->itt = itt;
547 else
548 task->hdr->itt = build_itt(task->itt,
549 task->conn->session->age);
552 INIT_LIST_HEAD(&task->running);
553 list_add_tail(&task->running, &conn->mgmtqueue);
555 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
556 if (iscsi_prep_mgmt_task(conn, task))
557 goto free_task;
559 if (session->tt->xmit_task(task))
560 goto free_task;
562 } else
563 scsi_queue_work(conn->session->host, &conn->xmitwork);
565 return task;
567 free_task:
568 __iscsi_put_task(task);
569 return NULL;
571 requeue_task:
572 if (task != conn->login_task)
573 __kfifo_put(session->cmdpool.queue, (void*)&task,
574 sizeof(void*));
575 return NULL;
578 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
579 char *data, uint32_t data_size)
581 struct iscsi_conn *conn = cls_conn->dd_data;
582 struct iscsi_session *session = conn->session;
583 int err = 0;
585 spin_lock_bh(&session->lock);
586 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
587 err = -EPERM;
588 spin_unlock_bh(&session->lock);
589 return err;
591 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
594 * iscsi_cmd_rsp - SCSI Command Response processing
595 * @conn: iscsi connection
596 * @hdr: iscsi header
597 * @task: scsi command task
598 * @data: cmd data buffer
599 * @datalen: len of buffer
601 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
602 * then completes the command and task.
604 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
605 struct iscsi_task *task, char *data,
606 int datalen)
608 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
609 struct iscsi_session *session = conn->session;
610 struct scsi_cmnd *sc = task->sc;
612 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
613 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
615 sc->result = (DID_OK << 16) | rhdr->cmd_status;
617 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
618 sc->result = DID_ERROR << 16;
619 goto out;
622 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
623 uint16_t senselen;
625 if (datalen < 2) {
626 invalid_datalen:
627 iscsi_conn_printk(KERN_ERR, conn,
628 "Got CHECK_CONDITION but invalid data "
629 "buffer size of %d\n", datalen);
630 sc->result = DID_BAD_TARGET << 16;
631 goto out;
634 senselen = get_unaligned_be16(data);
635 if (datalen < senselen)
636 goto invalid_datalen;
638 memcpy(sc->sense_buffer, data + 2,
639 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
640 debug_scsi("copied %d bytes of sense\n",
641 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
644 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
645 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
646 int res_count = be32_to_cpu(rhdr->bi_residual_count);
648 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
649 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
650 res_count <= scsi_in(sc)->length))
651 scsi_in(sc)->resid = res_count;
652 else
653 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
656 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
657 ISCSI_FLAG_CMD_OVERFLOW)) {
658 int res_count = be32_to_cpu(rhdr->residual_count);
660 if (res_count > 0 &&
661 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
662 res_count <= scsi_bufflen(sc)))
663 /* write side for bidi or uni-io set_resid */
664 scsi_set_resid(sc, res_count);
665 else
666 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
668 out:
669 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
670 (long)sc, sc->result, task->itt);
671 conn->scsirsp_pdus_cnt++;
673 __iscsi_put_task(task);
677 * iscsi_data_in_rsp - SCSI Data-In Response processing
678 * @conn: iscsi connection
679 * @hdr: iscsi pdu
680 * @task: scsi command task
682 static void
683 iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
684 struct iscsi_task *task)
686 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
687 struct scsi_cmnd *sc = task->sc;
689 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
690 return;
692 sc->result = (DID_OK << 16) | rhdr->cmd_status;
693 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
694 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
695 ISCSI_FLAG_DATA_OVERFLOW)) {
696 int res_count = be32_to_cpu(rhdr->residual_count);
698 if (res_count > 0 &&
699 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
700 res_count <= scsi_in(sc)->length))
701 scsi_in(sc)->resid = res_count;
702 else
703 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
706 conn->scsirsp_pdus_cnt++;
707 __iscsi_put_task(task);
710 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
712 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
714 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
715 conn->tmfrsp_pdus_cnt++;
717 if (conn->tmf_state != TMF_QUEUED)
718 return;
720 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
721 conn->tmf_state = TMF_SUCCESS;
722 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
723 conn->tmf_state = TMF_NOT_FOUND;
724 else
725 conn->tmf_state = TMF_FAILED;
726 wake_up(&conn->ehwait);
729 static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
731 struct iscsi_nopout hdr;
732 struct iscsi_task *task;
734 if (!rhdr && conn->ping_task)
735 return;
737 memset(&hdr, 0, sizeof(struct iscsi_nopout));
738 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
739 hdr.flags = ISCSI_FLAG_CMD_FINAL;
741 if (rhdr) {
742 memcpy(hdr.lun, rhdr->lun, 8);
743 hdr.ttt = rhdr->ttt;
744 hdr.itt = RESERVED_ITT;
745 } else
746 hdr.ttt = RESERVED_ITT;
748 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
749 if (!task)
750 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
751 else if (!rhdr) {
752 /* only track our nops */
753 conn->ping_task = task;
754 conn->last_ping = jiffies;
758 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
759 char *data, int datalen)
761 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
762 struct iscsi_hdr rejected_pdu;
764 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
766 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
767 if (ntoh24(reject->dlength) > datalen)
768 return ISCSI_ERR_PROTO;
770 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
771 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
772 iscsi_conn_printk(KERN_ERR, conn,
773 "pdu (op 0x%x) rejected "
774 "due to DataDigest error.\n",
775 rejected_pdu.opcode);
778 return 0;
782 * iscsi_itt_to_task - look up task by itt
783 * @conn: iscsi connection
784 * @itt: itt
786 * This should be used for mgmt tasks like login and nops, or if
787 * the LDD's itt space does not include the session age.
789 * The session lock must be held.
791 static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
793 struct iscsi_session *session = conn->session;
794 int i;
796 if (itt == RESERVED_ITT)
797 return NULL;
799 if (session->tt->parse_pdu_itt)
800 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
801 else
802 i = get_itt(itt);
803 if (i >= session->cmds_max)
804 return NULL;
806 return session->cmds[i];
810 * __iscsi_complete_pdu - complete pdu
811 * @conn: iscsi conn
812 * @hdr: iscsi header
813 * @data: data buffer
814 * @datalen: len of data buffer
816 * Completes pdu processing by freeing any resources allocated at
817 * queuecommand or send generic. session lock must be held and verify
818 * itt must have been called.
820 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
821 char *data, int datalen)
823 struct iscsi_session *session = conn->session;
824 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
825 struct iscsi_task *task;
826 uint32_t itt;
828 conn->last_recv = jiffies;
829 rc = iscsi_verify_itt(conn, hdr->itt);
830 if (rc)
831 return rc;
833 if (hdr->itt != RESERVED_ITT)
834 itt = get_itt(hdr->itt);
835 else
836 itt = ~0U;
838 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
839 opcode, conn->id, itt, datalen);
841 if (itt == ~0U) {
842 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
844 switch(opcode) {
845 case ISCSI_OP_NOOP_IN:
846 if (datalen) {
847 rc = ISCSI_ERR_PROTO;
848 break;
851 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
852 break;
854 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
855 break;
856 case ISCSI_OP_REJECT:
857 rc = iscsi_handle_reject(conn, hdr, data, datalen);
858 break;
859 case ISCSI_OP_ASYNC_EVENT:
860 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
861 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
862 rc = ISCSI_ERR_CONN_FAILED;
863 break;
864 default:
865 rc = ISCSI_ERR_BAD_OPCODE;
866 break;
868 goto out;
871 switch(opcode) {
872 case ISCSI_OP_SCSI_CMD_RSP:
873 case ISCSI_OP_SCSI_DATA_IN:
874 task = iscsi_itt_to_ctask(conn, hdr->itt);
875 if (!task)
876 return ISCSI_ERR_BAD_ITT;
877 break;
878 case ISCSI_OP_R2T:
880 * LLD handles R2Ts if they need to.
882 return 0;
883 case ISCSI_OP_LOGOUT_RSP:
884 case ISCSI_OP_LOGIN_RSP:
885 case ISCSI_OP_TEXT_RSP:
886 case ISCSI_OP_SCSI_TMFUNC_RSP:
887 case ISCSI_OP_NOOP_IN:
888 task = iscsi_itt_to_task(conn, hdr->itt);
889 if (!task)
890 return ISCSI_ERR_BAD_ITT;
891 break;
892 default:
893 return ISCSI_ERR_BAD_OPCODE;
896 switch(opcode) {
897 case ISCSI_OP_SCSI_CMD_RSP:
898 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
899 break;
900 case ISCSI_OP_SCSI_DATA_IN:
901 iscsi_data_in_rsp(conn, hdr, task);
902 break;
903 case ISCSI_OP_LOGOUT_RSP:
904 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
905 if (datalen) {
906 rc = ISCSI_ERR_PROTO;
907 break;
909 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
910 goto recv_pdu;
911 case ISCSI_OP_LOGIN_RSP:
912 case ISCSI_OP_TEXT_RSP:
913 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
915 * login related PDU's exp_statsn is handled in
916 * userspace
918 goto recv_pdu;
919 case ISCSI_OP_SCSI_TMFUNC_RSP:
920 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
921 if (datalen) {
922 rc = ISCSI_ERR_PROTO;
923 break;
926 iscsi_tmf_rsp(conn, hdr);
927 __iscsi_put_task(task);
928 break;
929 case ISCSI_OP_NOOP_IN:
930 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
931 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
932 rc = ISCSI_ERR_PROTO;
933 break;
935 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
937 if (conn->ping_task != task)
939 * If this is not in response to one of our
940 * nops then it must be from userspace.
942 goto recv_pdu;
944 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
945 __iscsi_put_task(task);
946 break;
947 default:
948 rc = ISCSI_ERR_BAD_OPCODE;
949 break;
952 out:
953 return rc;
954 recv_pdu:
955 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
956 rc = ISCSI_ERR_CONN_FAILED;
957 __iscsi_put_task(task);
958 return rc;
960 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
962 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
963 char *data, int datalen)
965 int rc;
967 spin_lock(&conn->session->lock);
968 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
969 spin_unlock(&conn->session->lock);
970 return rc;
972 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
974 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
976 struct iscsi_session *session = conn->session;
977 int age = 0, i = 0;
979 if (itt == RESERVED_ITT)
980 return 0;
982 if (session->tt->parse_pdu_itt)
983 session->tt->parse_pdu_itt(conn, itt, &i, &age);
984 else {
985 i = get_itt(itt);
986 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
989 if (age != session->age) {
990 iscsi_conn_printk(KERN_ERR, conn,
991 "received itt %x expected session age (%x)\n",
992 (__force u32)itt, session->age);
993 return ISCSI_ERR_BAD_ITT;
996 if (i >= session->cmds_max) {
997 iscsi_conn_printk(KERN_ERR, conn,
998 "received invalid itt index %u (max cmds "
999 "%u.\n", i, session->cmds_max);
1000 return ISCSI_ERR_BAD_ITT;
1002 return 0;
1004 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1007 * iscsi_itt_to_ctask - look up ctask by itt
1008 * @conn: iscsi connection
1009 * @itt: itt
1011 * This should be used for cmd tasks.
1013 * The session lock must be held.
1015 struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1017 struct iscsi_task *task;
1019 if (iscsi_verify_itt(conn, itt))
1020 return NULL;
1022 task = iscsi_itt_to_task(conn, itt);
1023 if (!task || !task->sc)
1024 return NULL;
1026 if (task->sc->SCp.phase != conn->session->age) {
1027 iscsi_session_printk(KERN_ERR, conn->session,
1028 "task's session age %d, expected %d\n",
1029 task->sc->SCp.phase, conn->session->age);
1030 return NULL;
1033 return task;
1035 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1037 void iscsi_session_failure(struct iscsi_cls_session *cls_session,
1038 enum iscsi_err err)
1040 struct iscsi_session *session = cls_session->dd_data;
1041 struct iscsi_conn *conn;
1042 struct device *dev;
1043 unsigned long flags;
1045 spin_lock_irqsave(&session->lock, flags);
1046 conn = session->leadconn;
1047 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1048 spin_unlock_irqrestore(&session->lock, flags);
1049 return;
1052 dev = get_device(&conn->cls_conn->dev);
1053 spin_unlock_irqrestore(&session->lock, flags);
1054 if (!dev)
1055 return;
1057 * if the host is being removed bypass the connection
1058 * recovery initialization because we are going to kill
1059 * the session.
1061 if (err == ISCSI_ERR_INVALID_HOST)
1062 iscsi_conn_error_event(conn->cls_conn, err);
1063 else
1064 iscsi_conn_failure(conn, err);
1065 put_device(dev);
1067 EXPORT_SYMBOL_GPL(iscsi_session_failure);
1069 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1071 struct iscsi_session *session = conn->session;
1072 unsigned long flags;
1074 spin_lock_irqsave(&session->lock, flags);
1075 if (session->state == ISCSI_STATE_FAILED) {
1076 spin_unlock_irqrestore(&session->lock, flags);
1077 return;
1080 if (conn->stop_stage == 0)
1081 session->state = ISCSI_STATE_FAILED;
1082 spin_unlock_irqrestore(&session->lock, flags);
1084 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1085 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1086 iscsi_conn_error_event(conn->cls_conn, err);
1088 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1090 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1092 struct iscsi_session *session = conn->session;
1095 * Check for iSCSI window and take care of CmdSN wrap-around
1097 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1098 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
1099 "CmdSN %u/%u\n", session->exp_cmdsn,
1100 session->max_cmdsn, session->cmdsn,
1101 session->queued_cmdsn);
1102 return -ENOSPC;
1104 return 0;
1107 static int iscsi_xmit_task(struct iscsi_conn *conn)
1109 struct iscsi_task *task = conn->task;
1110 int rc;
1112 __iscsi_get_task(task);
1113 spin_unlock_bh(&conn->session->lock);
1114 rc = conn->session->tt->xmit_task(task);
1115 spin_lock_bh(&conn->session->lock);
1116 __iscsi_put_task(task);
1117 if (!rc)
1118 /* done with this task */
1119 conn->task = NULL;
1120 return rc;
1124 * iscsi_requeue_task - requeue task to run from session workqueue
1125 * @task: task to requeue
1127 * LLDs that need to run a task from the session workqueue should call
1128 * this. The session lock must be held. This should only be called
1129 * by software drivers.
1131 void iscsi_requeue_task(struct iscsi_task *task)
1133 struct iscsi_conn *conn = task->conn;
1135 list_move_tail(&task->running, &conn->requeue);
1136 scsi_queue_work(conn->session->host, &conn->xmitwork);
1138 EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1141 * iscsi_data_xmit - xmit any command into the scheduled connection
1142 * @conn: iscsi connection
1144 * Notes:
1145 * The function can return -EAGAIN in which case the caller must
1146 * re-schedule it again later or recover. '0' return code means
1147 * successful xmit.
1149 static int iscsi_data_xmit(struct iscsi_conn *conn)
1151 int rc = 0;
1153 spin_lock_bh(&conn->session->lock);
1154 if (unlikely(conn->suspend_tx)) {
1155 debug_scsi("conn %d Tx suspended!\n", conn->id);
1156 spin_unlock_bh(&conn->session->lock);
1157 return -ENODATA;
1160 if (conn->task) {
1161 rc = iscsi_xmit_task(conn);
1162 if (rc)
1163 goto again;
1167 * process mgmt pdus like nops before commands since we should
1168 * only have one nop-out as a ping from us and targets should not
1169 * overflow us with nop-ins
1171 check_mgmt:
1172 while (!list_empty(&conn->mgmtqueue)) {
1173 conn->task = list_entry(conn->mgmtqueue.next,
1174 struct iscsi_task, running);
1175 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1176 __iscsi_put_task(conn->task);
1177 conn->task = NULL;
1178 continue;
1180 rc = iscsi_xmit_task(conn);
1181 if (rc)
1182 goto again;
1185 /* process pending command queue */
1186 while (!list_empty(&conn->xmitqueue)) {
1187 if (conn->tmf_state == TMF_QUEUED)
1188 break;
1190 conn->task = list_entry(conn->xmitqueue.next,
1191 struct iscsi_task, running);
1192 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1193 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
1194 continue;
1196 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1197 if (rc) {
1198 if (rc == -ENOMEM) {
1199 conn->task = NULL;
1200 goto again;
1201 } else
1202 fail_command(conn, conn->task, DID_ABORT << 16);
1203 continue;
1205 rc = iscsi_xmit_task(conn);
1206 if (rc)
1207 goto again;
1209 * we could continuously get new task requests so
1210 * we need to check the mgmt queue for nops that need to
1211 * be sent to aviod starvation
1213 if (!list_empty(&conn->mgmtqueue))
1214 goto check_mgmt;
1217 while (!list_empty(&conn->requeue)) {
1218 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1219 break;
1222 * we always do fastlogout - conn stop code will clean up.
1224 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1225 break;
1227 conn->task = list_entry(conn->requeue.next,
1228 struct iscsi_task, running);
1229 conn->task->state = ISCSI_TASK_RUNNING;
1230 list_move_tail(conn->requeue.next, &conn->run_list);
1231 rc = iscsi_xmit_task(conn);
1232 if (rc)
1233 goto again;
1234 if (!list_empty(&conn->mgmtqueue))
1235 goto check_mgmt;
1237 spin_unlock_bh(&conn->session->lock);
1238 return -ENODATA;
1240 again:
1241 if (unlikely(conn->suspend_tx))
1242 rc = -ENODATA;
1243 spin_unlock_bh(&conn->session->lock);
1244 return rc;
1247 static void iscsi_xmitworker(struct work_struct *work)
1249 struct iscsi_conn *conn =
1250 container_of(work, struct iscsi_conn, xmitwork);
1251 int rc;
1253 * serialize Xmit worker on a per-connection basis.
1255 do {
1256 rc = iscsi_data_xmit(conn);
1257 } while (rc >= 0 || rc == -EAGAIN);
1260 static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1261 struct scsi_cmnd *sc)
1263 struct iscsi_task *task;
1265 if (!__kfifo_get(conn->session->cmdpool.queue,
1266 (void *) &task, sizeof(void *)))
1267 return NULL;
1269 sc->SCp.phase = conn->session->age;
1270 sc->SCp.ptr = (char *) task;
1272 atomic_set(&task->refcount, 1);
1273 task->state = ISCSI_TASK_PENDING;
1274 task->conn = conn;
1275 task->sc = sc;
1276 INIT_LIST_HEAD(&task->running);
1277 return task;
1280 enum {
1281 FAILURE_BAD_HOST = 1,
1282 FAILURE_SESSION_FAILED,
1283 FAILURE_SESSION_FREED,
1284 FAILURE_WINDOW_CLOSED,
1285 FAILURE_OOM,
1286 FAILURE_SESSION_TERMINATE,
1287 FAILURE_SESSION_IN_RECOVERY,
1288 FAILURE_SESSION_RECOVERY_TIMEOUT,
1289 FAILURE_SESSION_LOGGING_OUT,
1290 FAILURE_SESSION_NOT_READY,
1293 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1295 struct iscsi_cls_session *cls_session;
1296 struct Scsi_Host *host;
1297 int reason = 0;
1298 struct iscsi_session *session;
1299 struct iscsi_conn *conn;
1300 struct iscsi_task *task = NULL;
1302 sc->scsi_done = done;
1303 sc->result = 0;
1304 sc->SCp.ptr = NULL;
1306 host = sc->device->host;
1307 spin_unlock(host->host_lock);
1309 cls_session = starget_to_session(scsi_target(sc->device));
1310 session = cls_session->dd_data;
1311 spin_lock(&session->lock);
1313 reason = iscsi_session_chkready(cls_session);
1314 if (reason) {
1315 sc->result = reason;
1316 goto fault;
1320 * ISCSI_STATE_FAILED is a temp. state. The recovery
1321 * code will decide what is best to do with command queued
1322 * during this time
1324 if (session->state != ISCSI_STATE_LOGGED_IN &&
1325 session->state != ISCSI_STATE_FAILED) {
1327 * to handle the race between when we set the recovery state
1328 * and block the session we requeue here (commands could
1329 * be entering our queuecommand while a block is starting
1330 * up because the block code is not locked)
1332 switch (session->state) {
1333 case ISCSI_STATE_IN_RECOVERY:
1334 reason = FAILURE_SESSION_IN_RECOVERY;
1335 goto reject;
1336 case ISCSI_STATE_LOGGING_OUT:
1337 reason = FAILURE_SESSION_LOGGING_OUT;
1338 goto reject;
1339 case ISCSI_STATE_RECOVERY_FAILED:
1340 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1341 sc->result = DID_TRANSPORT_FAILFAST << 16;
1342 break;
1343 case ISCSI_STATE_TERMINATE:
1344 reason = FAILURE_SESSION_TERMINATE;
1345 sc->result = DID_NO_CONNECT << 16;
1346 break;
1347 default:
1348 reason = FAILURE_SESSION_FREED;
1349 sc->result = DID_NO_CONNECT << 16;
1351 goto fault;
1354 conn = session->leadconn;
1355 if (!conn) {
1356 reason = FAILURE_SESSION_FREED;
1357 sc->result = DID_NO_CONNECT << 16;
1358 goto fault;
1361 if (iscsi_check_cmdsn_window_closed(conn)) {
1362 reason = FAILURE_WINDOW_CLOSED;
1363 goto reject;
1366 task = iscsi_alloc_task(conn, sc);
1367 if (!task) {
1368 reason = FAILURE_OOM;
1369 goto reject;
1371 list_add_tail(&task->running, &conn->xmitqueue);
1373 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
1374 reason = iscsi_prep_scsi_cmd_pdu(task);
1375 if (reason) {
1376 if (reason == -ENOMEM) {
1377 reason = FAILURE_OOM;
1378 goto prepd_reject;
1379 } else {
1380 sc->result = DID_ABORT << 16;
1381 goto prepd_fault;
1384 if (session->tt->xmit_task(task)) {
1385 reason = FAILURE_SESSION_NOT_READY;
1386 goto prepd_reject;
1388 } else
1389 scsi_queue_work(session->host, &conn->xmitwork);
1391 session->queued_cmdsn++;
1392 spin_unlock(&session->lock);
1393 spin_lock(host->host_lock);
1394 return 0;
1396 prepd_reject:
1397 sc->scsi_done = NULL;
1398 iscsi_complete_command(task);
1399 reject:
1400 spin_unlock(&session->lock);
1401 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1402 spin_lock(host->host_lock);
1403 return SCSI_MLQUEUE_TARGET_BUSY;
1405 prepd_fault:
1406 sc->scsi_done = NULL;
1407 iscsi_complete_command(task);
1408 fault:
1409 spin_unlock(&session->lock);
1410 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
1411 if (!scsi_bidi_cmnd(sc))
1412 scsi_set_resid(sc, scsi_bufflen(sc));
1413 else {
1414 scsi_out(sc)->resid = scsi_out(sc)->length;
1415 scsi_in(sc)->resid = scsi_in(sc)->length;
1417 done(sc);
1418 spin_lock(host->host_lock);
1419 return 0;
1421 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1423 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1425 if (depth > ISCSI_MAX_CMD_PER_LUN)
1426 depth = ISCSI_MAX_CMD_PER_LUN;
1427 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1428 return sdev->queue_depth;
1430 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1432 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1434 struct iscsi_session *session = cls_session->dd_data;
1436 spin_lock_bh(&session->lock);
1437 if (session->state != ISCSI_STATE_LOGGED_IN) {
1438 session->state = ISCSI_STATE_RECOVERY_FAILED;
1439 if (session->leadconn)
1440 wake_up(&session->leadconn->ehwait);
1442 spin_unlock_bh(&session->lock);
1444 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1446 int iscsi_eh_target_reset(struct scsi_cmnd *sc)
1448 struct iscsi_cls_session *cls_session;
1449 struct iscsi_session *session;
1450 struct iscsi_conn *conn;
1452 cls_session = starget_to_session(scsi_target(sc->device));
1453 session = cls_session->dd_data;
1454 conn = session->leadconn;
1456 mutex_lock(&session->eh_mutex);
1457 spin_lock_bh(&session->lock);
1458 if (session->state == ISCSI_STATE_TERMINATE) {
1459 failed:
1460 debug_scsi("failing target reset: session terminated "
1461 "[CID %d age %d]\n", conn->id, session->age);
1462 spin_unlock_bh(&session->lock);
1463 mutex_unlock(&session->eh_mutex);
1464 return FAILED;
1467 spin_unlock_bh(&session->lock);
1468 mutex_unlock(&session->eh_mutex);
1470 * we drop the lock here but the leadconn cannot be destoyed while
1471 * we are in the scsi eh
1473 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1475 debug_scsi("iscsi_eh_target_reset wait for relogin\n");
1476 wait_event_interruptible(conn->ehwait,
1477 session->state == ISCSI_STATE_TERMINATE ||
1478 session->state == ISCSI_STATE_LOGGED_IN ||
1479 session->state == ISCSI_STATE_RECOVERY_FAILED);
1480 if (signal_pending(current))
1481 flush_signals(current);
1483 mutex_lock(&session->eh_mutex);
1484 spin_lock_bh(&session->lock);
1485 if (session->state == ISCSI_STATE_LOGGED_IN)
1486 iscsi_session_printk(KERN_INFO, session,
1487 "target reset succeeded\n");
1488 else
1489 goto failed;
1490 spin_unlock_bh(&session->lock);
1491 mutex_unlock(&session->eh_mutex);
1492 return SUCCESS;
1494 EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
1496 static void iscsi_tmf_timedout(unsigned long data)
1498 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1499 struct iscsi_session *session = conn->session;
1501 spin_lock(&session->lock);
1502 if (conn->tmf_state == TMF_QUEUED) {
1503 conn->tmf_state = TMF_TIMEDOUT;
1504 debug_scsi("tmf timedout\n");
1505 /* unblock eh_abort() */
1506 wake_up(&conn->ehwait);
1508 spin_unlock(&session->lock);
1511 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1512 struct iscsi_tm *hdr, int age,
1513 int timeout)
1515 struct iscsi_session *session = conn->session;
1516 struct iscsi_task *task;
1518 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1519 NULL, 0);
1520 if (!task) {
1521 spin_unlock_bh(&session->lock);
1522 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1523 spin_lock_bh(&session->lock);
1524 debug_scsi("tmf exec failure\n");
1525 return -EPERM;
1527 conn->tmfcmd_pdus_cnt++;
1528 conn->tmf_timer.expires = timeout * HZ + jiffies;
1529 conn->tmf_timer.function = iscsi_tmf_timedout;
1530 conn->tmf_timer.data = (unsigned long)conn;
1531 add_timer(&conn->tmf_timer);
1532 debug_scsi("tmf set timeout\n");
1534 spin_unlock_bh(&session->lock);
1535 mutex_unlock(&session->eh_mutex);
1538 * block eh thread until:
1540 * 1) tmf response
1541 * 2) tmf timeout
1542 * 3) session is terminated or restarted or userspace has
1543 * given up on recovery
1545 wait_event_interruptible(conn->ehwait, age != session->age ||
1546 session->state != ISCSI_STATE_LOGGED_IN ||
1547 conn->tmf_state != TMF_QUEUED);
1548 if (signal_pending(current))
1549 flush_signals(current);
1550 del_timer_sync(&conn->tmf_timer);
1552 mutex_lock(&session->eh_mutex);
1553 spin_lock_bh(&session->lock);
1554 /* if the session drops it will clean up the task */
1555 if (age != session->age ||
1556 session->state != ISCSI_STATE_LOGGED_IN)
1557 return -ENOTCONN;
1558 return 0;
1562 * Fail commands. session lock held and recv side suspended and xmit
1563 * thread flushed
1565 static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1566 int error)
1568 struct iscsi_task *task, *tmp;
1570 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1571 conn->task = NULL;
1573 /* flush pending */
1574 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1575 if (lun == task->sc->device->lun || lun == -1) {
1576 debug_scsi("failing pending sc %p itt 0x%x\n",
1577 task->sc, task->itt);
1578 fail_command(conn, task, error << 16);
1582 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1583 if (lun == task->sc->device->lun || lun == -1) {
1584 debug_scsi("failing requeued sc %p itt 0x%x\n",
1585 task->sc, task->itt);
1586 fail_command(conn, task, error << 16);
1590 /* fail all other running */
1591 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1592 if (lun == task->sc->device->lun || lun == -1) {
1593 debug_scsi("failing in progress sc %p itt 0x%x\n",
1594 task->sc, task->itt);
1595 fail_command(conn, task, error << 16);
1600 void iscsi_suspend_tx(struct iscsi_conn *conn)
1602 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1603 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1604 scsi_flush_work(conn->session->host);
1606 EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1608 static void iscsi_start_tx(struct iscsi_conn *conn)
1610 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1611 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1612 scsi_queue_work(conn->session->host, &conn->xmitwork);
1615 static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1617 struct iscsi_cls_session *cls_session;
1618 struct iscsi_session *session;
1619 struct iscsi_conn *conn;
1620 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1622 cls_session = starget_to_session(scsi_target(scmd->device));
1623 session = cls_session->dd_data;
1625 debug_scsi("scsi cmd %p timedout\n", scmd);
1627 spin_lock(&session->lock);
1628 if (session->state != ISCSI_STATE_LOGGED_IN) {
1630 * We are probably in the middle of iscsi recovery so let
1631 * that complete and handle the error.
1633 rc = BLK_EH_RESET_TIMER;
1634 goto done;
1637 conn = session->leadconn;
1638 if (!conn) {
1639 /* In the middle of shuting down */
1640 rc = BLK_EH_RESET_TIMER;
1641 goto done;
1644 if (!conn->recv_timeout && !conn->ping_timeout)
1645 goto done;
1647 * if the ping timedout then we are in the middle of cleaning up
1648 * and can let the iscsi eh handle it
1650 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1651 (conn->ping_timeout * HZ), jiffies))
1652 rc = BLK_EH_RESET_TIMER;
1654 * if we are about to check the transport then give the command
1655 * more time
1657 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1658 jiffies))
1659 rc = BLK_EH_RESET_TIMER;
1660 /* if in the middle of checking the transport then give us more time */
1661 if (conn->ping_task)
1662 rc = BLK_EH_RESET_TIMER;
1663 done:
1664 spin_unlock(&session->lock);
1665 debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
1666 "timer reset" : "nh");
1667 return rc;
1670 static void iscsi_check_transport_timeouts(unsigned long data)
1672 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1673 struct iscsi_session *session = conn->session;
1674 unsigned long recv_timeout, next_timeout = 0, last_recv;
1676 spin_lock(&session->lock);
1677 if (session->state != ISCSI_STATE_LOGGED_IN)
1678 goto done;
1680 recv_timeout = conn->recv_timeout;
1681 if (!recv_timeout)
1682 goto done;
1684 recv_timeout *= HZ;
1685 last_recv = conn->last_recv;
1686 if (conn->ping_task &&
1687 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
1688 jiffies)) {
1689 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1690 "expired, last rx %lu, last ping %lu, "
1691 "now %lu\n", conn->ping_timeout, last_recv,
1692 conn->last_ping, jiffies);
1693 spin_unlock(&session->lock);
1694 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1695 return;
1698 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
1699 /* send a ping to try to provoke some traffic */
1700 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1701 iscsi_send_nopout(conn, NULL);
1702 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
1703 } else
1704 next_timeout = last_recv + recv_timeout;
1706 debug_scsi("Setting next tmo %lu\n", next_timeout);
1707 mod_timer(&conn->transport_timer, next_timeout);
1708 done:
1709 spin_unlock(&session->lock);
1712 static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
1713 struct iscsi_tm *hdr)
1715 memset(hdr, 0, sizeof(*hdr));
1716 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1717 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1718 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1719 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1720 hdr->rtt = task->hdr_itt;
1721 hdr->refcmdsn = task->cmdsn;
1724 int iscsi_eh_abort(struct scsi_cmnd *sc)
1726 struct iscsi_cls_session *cls_session;
1727 struct iscsi_session *session;
1728 struct iscsi_conn *conn;
1729 struct iscsi_task *task;
1730 struct iscsi_tm *hdr;
1731 int rc, age;
1733 cls_session = starget_to_session(scsi_target(sc->device));
1734 session = cls_session->dd_data;
1736 mutex_lock(&session->eh_mutex);
1737 spin_lock_bh(&session->lock);
1739 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1740 * got the command.
1742 if (!sc->SCp.ptr) {
1743 debug_scsi("sc never reached iscsi layer or it completed.\n");
1744 spin_unlock_bh(&session->lock);
1745 mutex_unlock(&session->eh_mutex);
1746 return SUCCESS;
1750 * If we are not logged in or we have started a new session
1751 * then let the host reset code handle this
1753 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1754 sc->SCp.phase != session->age) {
1755 spin_unlock_bh(&session->lock);
1756 mutex_unlock(&session->eh_mutex);
1757 return FAILED;
1760 conn = session->leadconn;
1761 conn->eh_abort_cnt++;
1762 age = session->age;
1764 task = (struct iscsi_task *)sc->SCp.ptr;
1765 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
1767 /* task completed before time out */
1768 if (!task->sc) {
1769 debug_scsi("sc completed while abort in progress\n");
1770 goto success;
1773 if (task->state == ISCSI_TASK_PENDING) {
1774 fail_command(conn, task, DID_ABORT << 16);
1775 goto success;
1778 /* only have one tmf outstanding at a time */
1779 if (conn->tmf_state != TMF_INITIAL)
1780 goto failed;
1781 conn->tmf_state = TMF_QUEUED;
1783 hdr = &conn->tmhdr;
1784 iscsi_prep_abort_task_pdu(task, hdr);
1786 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
1787 rc = FAILED;
1788 goto failed;
1791 switch (conn->tmf_state) {
1792 case TMF_SUCCESS:
1793 spin_unlock_bh(&session->lock);
1795 * stop tx side incase the target had sent a abort rsp but
1796 * the initiator was still writing out data.
1798 iscsi_suspend_tx(conn);
1800 * we do not stop the recv side because targets have been
1801 * good and have never sent us a successful tmf response
1802 * then sent more data for the cmd.
1804 spin_lock(&session->lock);
1805 fail_command(conn, task, DID_ABORT << 16);
1806 conn->tmf_state = TMF_INITIAL;
1807 spin_unlock(&session->lock);
1808 iscsi_start_tx(conn);
1809 goto success_unlocked;
1810 case TMF_TIMEDOUT:
1811 spin_unlock_bh(&session->lock);
1812 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1813 goto failed_unlocked;
1814 case TMF_NOT_FOUND:
1815 if (!sc->SCp.ptr) {
1816 conn->tmf_state = TMF_INITIAL;
1817 /* task completed before tmf abort response */
1818 debug_scsi("sc completed while abort in progress\n");
1819 goto success;
1821 /* fall through */
1822 default:
1823 conn->tmf_state = TMF_INITIAL;
1824 goto failed;
1827 success:
1828 spin_unlock_bh(&session->lock);
1829 success_unlocked:
1830 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
1831 mutex_unlock(&session->eh_mutex);
1832 return SUCCESS;
1834 failed:
1835 spin_unlock_bh(&session->lock);
1836 failed_unlocked:
1837 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1838 task ? task->itt : 0);
1839 mutex_unlock(&session->eh_mutex);
1840 return FAILED;
1842 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1844 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1846 memset(hdr, 0, sizeof(*hdr));
1847 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1848 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1849 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1850 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1851 hdr->rtt = RESERVED_ITT;
1854 int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1856 struct iscsi_cls_session *cls_session;
1857 struct iscsi_session *session;
1858 struct iscsi_conn *conn;
1859 struct iscsi_tm *hdr;
1860 int rc = FAILED;
1862 cls_session = starget_to_session(scsi_target(sc->device));
1863 session = cls_session->dd_data;
1865 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1867 mutex_lock(&session->eh_mutex);
1868 spin_lock_bh(&session->lock);
1870 * Just check if we are not logged in. We cannot check for
1871 * the phase because the reset could come from a ioctl.
1873 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1874 goto unlock;
1875 conn = session->leadconn;
1877 /* only have one tmf outstanding at a time */
1878 if (conn->tmf_state != TMF_INITIAL)
1879 goto unlock;
1880 conn->tmf_state = TMF_QUEUED;
1882 hdr = &conn->tmhdr;
1883 iscsi_prep_lun_reset_pdu(sc, hdr);
1885 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1886 session->lu_reset_timeout)) {
1887 rc = FAILED;
1888 goto unlock;
1891 switch (conn->tmf_state) {
1892 case TMF_SUCCESS:
1893 break;
1894 case TMF_TIMEDOUT:
1895 spin_unlock_bh(&session->lock);
1896 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1897 goto done;
1898 default:
1899 conn->tmf_state = TMF_INITIAL;
1900 goto unlock;
1903 rc = SUCCESS;
1904 spin_unlock_bh(&session->lock);
1906 iscsi_suspend_tx(conn);
1908 spin_lock_bh(&session->lock);
1909 fail_all_commands(conn, sc->device->lun, DID_ERROR);
1910 conn->tmf_state = TMF_INITIAL;
1911 spin_unlock_bh(&session->lock);
1913 iscsi_start_tx(conn);
1914 goto done;
1916 unlock:
1917 spin_unlock_bh(&session->lock);
1918 done:
1919 debug_scsi("iscsi_eh_device_reset %s\n",
1920 rc == SUCCESS ? "SUCCESS" : "FAILED");
1921 mutex_unlock(&session->eh_mutex);
1922 return rc;
1924 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1927 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1928 * should be accessed via kfifo_{get,put} on q->queue.
1929 * Optionally, the caller can obtain the array of object pointers
1930 * by passing in a non-NULL @items pointer
1933 iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
1935 int i, num_arrays = 1;
1937 memset(q, 0, sizeof(*q));
1939 q->max = max;
1941 /* If the user passed an items pointer, he wants a copy of
1942 * the array. */
1943 if (items)
1944 num_arrays++;
1945 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1946 if (q->pool == NULL)
1947 return -ENOMEM;
1949 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1950 GFP_KERNEL, NULL);
1951 if (IS_ERR(q->queue)) {
1952 q->queue = NULL;
1953 goto enomem;
1956 for (i = 0; i < max; i++) {
1957 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
1958 if (q->pool[i] == NULL) {
1959 q->max = i;
1960 goto enomem;
1962 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1965 if (items) {
1966 *items = q->pool + max;
1967 memcpy(*items, q->pool, max * sizeof(void *));
1970 return 0;
1972 enomem:
1973 iscsi_pool_free(q);
1974 return -ENOMEM;
1976 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1978 void iscsi_pool_free(struct iscsi_pool *q)
1980 int i;
1982 for (i = 0; i < q->max; i++)
1983 kfree(q->pool[i]);
1984 kfree(q->pool);
1985 kfree(q->queue);
1987 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1990 * iscsi_host_add - add host to system
1991 * @shost: scsi host
1992 * @pdev: parent device
1994 * This should be called by partial offload and software iscsi drivers
1995 * to add a host to the system.
1997 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
1999 if (!shost->can_queue)
2000 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2002 if (!shost->transportt->eh_timed_out)
2003 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2004 return scsi_add_host(shost, pdev);
2006 EXPORT_SYMBOL_GPL(iscsi_host_add);
2009 * iscsi_host_alloc - allocate a host and driver data
2010 * @sht: scsi host template
2011 * @dd_data_size: driver host data size
2012 * @qdepth: default device queue depth
2014 * This should be called by partial offload and software iscsi drivers.
2015 * To access the driver specific memory use the iscsi_host_priv() macro.
2017 struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2018 int dd_data_size, uint16_t qdepth)
2020 struct Scsi_Host *shost;
2021 struct iscsi_host *ihost;
2023 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2024 if (!shost)
2025 return NULL;
2027 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
2028 if (qdepth != 0)
2029 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
2030 "Queue depth must be between 1 and %d.\n",
2031 qdepth, ISCSI_MAX_CMD_PER_LUN);
2032 qdepth = ISCSI_DEF_CMD_PER_LUN;
2034 shost->cmd_per_lun = qdepth;
2036 ihost = shost_priv(shost);
2037 spin_lock_init(&ihost->lock);
2038 ihost->state = ISCSI_HOST_SETUP;
2039 ihost->num_sessions = 0;
2040 init_waitqueue_head(&ihost->session_removal_wq);
2041 return shost;
2043 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2045 static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2047 iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
2051 * iscsi_host_remove - remove host and sessions
2052 * @shost: scsi host
2054 * If there are any sessions left, this will initiate the removal and wait
2055 * for the completion.
2057 void iscsi_host_remove(struct Scsi_Host *shost)
2059 struct iscsi_host *ihost = shost_priv(shost);
2060 unsigned long flags;
2062 spin_lock_irqsave(&ihost->lock, flags);
2063 ihost->state = ISCSI_HOST_REMOVED;
2064 spin_unlock_irqrestore(&ihost->lock, flags);
2066 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2067 wait_event_interruptible(ihost->session_removal_wq,
2068 ihost->num_sessions == 0);
2069 if (signal_pending(current))
2070 flush_signals(current);
2072 scsi_remove_host(shost);
2074 EXPORT_SYMBOL_GPL(iscsi_host_remove);
2076 void iscsi_host_free(struct Scsi_Host *shost)
2078 struct iscsi_host *ihost = shost_priv(shost);
2080 kfree(ihost->netdev);
2081 kfree(ihost->hwaddress);
2082 kfree(ihost->initiatorname);
2083 scsi_host_put(shost);
2085 EXPORT_SYMBOL_GPL(iscsi_host_free);
2087 static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2089 struct iscsi_host *ihost = shost_priv(shost);
2090 unsigned long flags;
2092 shost = scsi_host_get(shost);
2093 if (!shost) {
2094 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2095 "of session teardown event because host already "
2096 "removed.\n");
2097 return;
2100 spin_lock_irqsave(&ihost->lock, flags);
2101 ihost->num_sessions--;
2102 if (ihost->num_sessions == 0)
2103 wake_up(&ihost->session_removal_wq);
2104 spin_unlock_irqrestore(&ihost->lock, flags);
2105 scsi_host_put(shost);
2109 * iscsi_session_setup - create iscsi cls session and host and session
2110 * @iscsit: iscsi transport template
2111 * @shost: scsi host
2112 * @cmds_max: session can queue
2113 * @cmd_task_size: LLD task private data size
2114 * @initial_cmdsn: initial CmdSN
2116 * This can be used by software iscsi_transports that allocate
2117 * a session per scsi host.
2119 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2120 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2121 * for nop handling and login/logout requests.
2123 struct iscsi_cls_session *
2124 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2125 uint16_t cmds_max, int cmd_task_size,
2126 uint32_t initial_cmdsn, unsigned int id)
2128 struct iscsi_host *ihost = shost_priv(shost);
2129 struct iscsi_session *session;
2130 struct iscsi_cls_session *cls_session;
2131 int cmd_i, scsi_cmds, total_cmds = cmds_max;
2132 unsigned long flags;
2134 spin_lock_irqsave(&ihost->lock, flags);
2135 if (ihost->state == ISCSI_HOST_REMOVED) {
2136 spin_unlock_irqrestore(&ihost->lock, flags);
2137 return NULL;
2139 ihost->num_sessions++;
2140 spin_unlock_irqrestore(&ihost->lock, flags);
2142 if (!total_cmds)
2143 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2145 * The iscsi layer needs some tasks for nop handling and tmfs,
2146 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2147 * + 1 command for scsi IO.
2149 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2150 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2151 "must be a power of two that is at least %d.\n",
2152 total_cmds, ISCSI_TOTAL_CMDS_MIN);
2153 goto dec_session_count;
2156 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2157 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2158 "must be a power of 2 less than or equal to %d.\n",
2159 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2160 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2163 if (!is_power_of_2(total_cmds)) {
2164 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2165 "must be a power of 2.\n", total_cmds);
2166 total_cmds = rounddown_pow_of_two(total_cmds);
2167 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2168 return NULL;
2169 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2170 total_cmds);
2172 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2174 cls_session = iscsi_alloc_session(shost, iscsit,
2175 sizeof(struct iscsi_session));
2176 if (!cls_session)
2177 goto dec_session_count;
2178 session = cls_session->dd_data;
2179 session->cls_session = cls_session;
2180 session->host = shost;
2181 session->state = ISCSI_STATE_FREE;
2182 session->fast_abort = 1;
2183 session->lu_reset_timeout = 15;
2184 session->abort_timeout = 10;
2185 session->scsi_cmds_max = scsi_cmds;
2186 session->cmds_max = total_cmds;
2187 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2188 session->exp_cmdsn = initial_cmdsn + 1;
2189 session->max_cmdsn = initial_cmdsn + 1;
2190 session->max_r2t = 1;
2191 session->tt = iscsit;
2192 mutex_init(&session->eh_mutex);
2193 spin_lock_init(&session->lock);
2195 /* initialize SCSI PDU commands pool */
2196 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2197 (void***)&session->cmds,
2198 cmd_task_size + sizeof(struct iscsi_task)))
2199 goto cmdpool_alloc_fail;
2201 /* pre-format cmds pool with ITT */
2202 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
2203 struct iscsi_task *task = session->cmds[cmd_i];
2205 if (cmd_task_size)
2206 task->dd_data = &task[1];
2207 task->itt = cmd_i;
2208 INIT_LIST_HEAD(&task->running);
2211 if (!try_module_get(iscsit->owner))
2212 goto module_get_fail;
2214 if (iscsi_add_session(cls_session, id))
2215 goto cls_session_fail;
2217 return cls_session;
2219 cls_session_fail:
2220 module_put(iscsit->owner);
2221 module_get_fail:
2222 iscsi_pool_free(&session->cmdpool);
2223 cmdpool_alloc_fail:
2224 iscsi_free_session(cls_session);
2225 dec_session_count:
2226 iscsi_host_dec_session_cnt(shost);
2227 return NULL;
2229 EXPORT_SYMBOL_GPL(iscsi_session_setup);
2232 * iscsi_session_teardown - destroy session, host, and cls_session
2233 * @cls_session: iscsi session
2235 * The driver must have called iscsi_remove_session before
2236 * calling this.
2238 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2240 struct iscsi_session *session = cls_session->dd_data;
2241 struct module *owner = cls_session->transport->owner;
2242 struct Scsi_Host *shost = session->host;
2244 iscsi_pool_free(&session->cmdpool);
2246 kfree(session->password);
2247 kfree(session->password_in);
2248 kfree(session->username);
2249 kfree(session->username_in);
2250 kfree(session->targetname);
2251 kfree(session->initiatorname);
2252 kfree(session->ifacename);
2254 iscsi_destroy_session(cls_session);
2255 iscsi_host_dec_session_cnt(shost);
2256 module_put(owner);
2258 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2261 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2262 * @cls_session: iscsi_cls_session
2263 * @dd_size: private driver data size
2264 * @conn_idx: cid
2266 struct iscsi_cls_conn *
2267 iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2268 uint32_t conn_idx)
2270 struct iscsi_session *session = cls_session->dd_data;
2271 struct iscsi_conn *conn;
2272 struct iscsi_cls_conn *cls_conn;
2273 char *data;
2275 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2276 conn_idx);
2277 if (!cls_conn)
2278 return NULL;
2279 conn = cls_conn->dd_data;
2280 memset(conn, 0, sizeof(*conn) + dd_size);
2282 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
2283 conn->session = session;
2284 conn->cls_conn = cls_conn;
2285 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2286 conn->id = conn_idx;
2287 conn->exp_statsn = 0;
2288 conn->tmf_state = TMF_INITIAL;
2290 init_timer(&conn->transport_timer);
2291 conn->transport_timer.data = (unsigned long)conn;
2292 conn->transport_timer.function = iscsi_check_transport_timeouts;
2294 INIT_LIST_HEAD(&conn->run_list);
2295 INIT_LIST_HEAD(&conn->mgmt_run_list);
2296 INIT_LIST_HEAD(&conn->mgmtqueue);
2297 INIT_LIST_HEAD(&conn->xmitqueue);
2298 INIT_LIST_HEAD(&conn->requeue);
2299 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2301 /* allocate login_task used for the login/text sequences */
2302 spin_lock_bh(&session->lock);
2303 if (!__kfifo_get(session->cmdpool.queue,
2304 (void*)&conn->login_task,
2305 sizeof(void*))) {
2306 spin_unlock_bh(&session->lock);
2307 goto login_task_alloc_fail;
2309 spin_unlock_bh(&session->lock);
2311 data = (char *) __get_free_pages(GFP_KERNEL,
2312 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2313 if (!data)
2314 goto login_task_data_alloc_fail;
2315 conn->login_task->data = conn->data = data;
2317 init_timer(&conn->tmf_timer);
2318 init_waitqueue_head(&conn->ehwait);
2320 return cls_conn;
2322 login_task_data_alloc_fail:
2323 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2324 sizeof(void*));
2325 login_task_alloc_fail:
2326 iscsi_destroy_conn(cls_conn);
2327 return NULL;
2329 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2332 * iscsi_conn_teardown - teardown iscsi connection
2333 * cls_conn: iscsi class connection
2335 * TODO: we may need to make this into a two step process
2336 * like scsi-mls remove + put host
2338 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2340 struct iscsi_conn *conn = cls_conn->dd_data;
2341 struct iscsi_session *session = conn->session;
2342 unsigned long flags;
2344 del_timer_sync(&conn->transport_timer);
2346 spin_lock_bh(&session->lock);
2347 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2348 if (session->leadconn == conn) {
2350 * leading connection? then give up on recovery.
2352 session->state = ISCSI_STATE_TERMINATE;
2353 wake_up(&conn->ehwait);
2355 spin_unlock_bh(&session->lock);
2358 * Block until all in-progress commands for this connection
2359 * time out or fail.
2361 for (;;) {
2362 spin_lock_irqsave(session->host->host_lock, flags);
2363 if (!session->host->host_busy) { /* OK for ERL == 0 */
2364 spin_unlock_irqrestore(session->host->host_lock, flags);
2365 break;
2367 spin_unlock_irqrestore(session->host->host_lock, flags);
2368 msleep_interruptible(500);
2369 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2370 "host_busy %d host_failed %d\n",
2371 session->host->host_busy,
2372 session->host->host_failed);
2374 * force eh_abort() to unblock
2376 wake_up(&conn->ehwait);
2379 /* flush queued up work because we free the connection below */
2380 iscsi_suspend_tx(conn);
2382 spin_lock_bh(&session->lock);
2383 free_pages((unsigned long) conn->data,
2384 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2385 kfree(conn->persistent_address);
2386 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
2387 sizeof(void*));
2388 if (session->leadconn == conn)
2389 session->leadconn = NULL;
2390 spin_unlock_bh(&session->lock);
2392 iscsi_destroy_conn(cls_conn);
2394 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2396 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2398 struct iscsi_conn *conn = cls_conn->dd_data;
2399 struct iscsi_session *session = conn->session;
2401 if (!session) {
2402 iscsi_conn_printk(KERN_ERR, conn,
2403 "can't start unbound connection\n");
2404 return -EPERM;
2407 if ((session->imm_data_en || !session->initial_r2t_en) &&
2408 session->first_burst > session->max_burst) {
2409 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2410 "first_burst %d max_burst %d\n",
2411 session->first_burst, session->max_burst);
2412 return -EINVAL;
2415 if (conn->ping_timeout && !conn->recv_timeout) {
2416 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2417 "zero. Using 5 seconds\n.");
2418 conn->recv_timeout = 5;
2421 if (conn->recv_timeout && !conn->ping_timeout) {
2422 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2423 "zero. Using 5 seconds.\n");
2424 conn->ping_timeout = 5;
2427 spin_lock_bh(&session->lock);
2428 conn->c_stage = ISCSI_CONN_STARTED;
2429 session->state = ISCSI_STATE_LOGGED_IN;
2430 session->queued_cmdsn = session->cmdsn;
2432 conn->last_recv = jiffies;
2433 conn->last_ping = jiffies;
2434 if (conn->recv_timeout && conn->ping_timeout)
2435 mod_timer(&conn->transport_timer,
2436 jiffies + (conn->recv_timeout * HZ));
2438 switch(conn->stop_stage) {
2439 case STOP_CONN_RECOVER:
2441 * unblock eh_abort() if it is blocked. re-try all
2442 * commands after successful recovery
2444 conn->stop_stage = 0;
2445 conn->tmf_state = TMF_INITIAL;
2446 session->age++;
2447 if (session->age == 16)
2448 session->age = 0;
2449 break;
2450 case STOP_CONN_TERM:
2451 conn->stop_stage = 0;
2452 break;
2453 default:
2454 break;
2456 spin_unlock_bh(&session->lock);
2458 iscsi_unblock_session(session->cls_session);
2459 wake_up(&conn->ehwait);
2460 return 0;
2462 EXPORT_SYMBOL_GPL(iscsi_conn_start);
2464 static void
2465 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2467 struct iscsi_task *task, *tmp;
2469 /* handle pending */
2470 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2471 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2472 /* release ref from prep task */
2473 __iscsi_put_task(task);
2476 /* handle running */
2477 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2478 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2479 /* release ref from prep task */
2480 __iscsi_put_task(task);
2483 conn->task = NULL;
2486 static void iscsi_start_session_recovery(struct iscsi_session *session,
2487 struct iscsi_conn *conn, int flag)
2489 int old_stop_stage;
2491 del_timer_sync(&conn->transport_timer);
2493 mutex_lock(&session->eh_mutex);
2494 spin_lock_bh(&session->lock);
2495 if (conn->stop_stage == STOP_CONN_TERM) {
2496 spin_unlock_bh(&session->lock);
2497 mutex_unlock(&session->eh_mutex);
2498 return;
2502 * When this is called for the in_login state, we only want to clean
2503 * up the login task and connection. We do not need to block and set
2504 * the recovery state again
2506 if (flag == STOP_CONN_TERM)
2507 session->state = ISCSI_STATE_TERMINATE;
2508 else if (conn->stop_stage != STOP_CONN_RECOVER)
2509 session->state = ISCSI_STATE_IN_RECOVERY;
2511 old_stop_stage = conn->stop_stage;
2512 conn->stop_stage = flag;
2513 conn->c_stage = ISCSI_CONN_STOPPED;
2514 spin_unlock_bh(&session->lock);
2516 iscsi_suspend_tx(conn);
2518 * for connection level recovery we should not calculate
2519 * header digest. conn->hdr_size used for optimization
2520 * in hdr_extract() and will be re-negotiated at
2521 * set_param() time.
2523 if (flag == STOP_CONN_RECOVER) {
2524 conn->hdrdgst_en = 0;
2525 conn->datadgst_en = 0;
2526 if (session->state == ISCSI_STATE_IN_RECOVERY &&
2527 old_stop_stage != STOP_CONN_RECOVER) {
2528 debug_scsi("blocking session\n");
2529 iscsi_block_session(session->cls_session);
2534 * flush queues.
2536 spin_lock_bh(&session->lock);
2537 if (flag == STOP_CONN_RECOVER)
2538 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2539 else
2540 fail_all_commands(conn, -1, DID_ERROR);
2541 flush_control_queues(session, conn);
2542 spin_unlock_bh(&session->lock);
2543 mutex_unlock(&session->eh_mutex);
2546 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2548 struct iscsi_conn *conn = cls_conn->dd_data;
2549 struct iscsi_session *session = conn->session;
2551 switch (flag) {
2552 case STOP_CONN_RECOVER:
2553 case STOP_CONN_TERM:
2554 iscsi_start_session_recovery(session, conn, flag);
2555 break;
2556 default:
2557 iscsi_conn_printk(KERN_ERR, conn,
2558 "invalid stop flag %d\n", flag);
2561 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2563 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2564 struct iscsi_cls_conn *cls_conn, int is_leading)
2566 struct iscsi_session *session = cls_session->dd_data;
2567 struct iscsi_conn *conn = cls_conn->dd_data;
2569 spin_lock_bh(&session->lock);
2570 if (is_leading)
2571 session->leadconn = conn;
2572 spin_unlock_bh(&session->lock);
2575 * Unblock xmitworker(), Login Phase will pass through.
2577 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2578 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2579 return 0;
2581 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2584 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2585 enum iscsi_param param, char *buf, int buflen)
2587 struct iscsi_conn *conn = cls_conn->dd_data;
2588 struct iscsi_session *session = conn->session;
2589 uint32_t value;
2591 switch(param) {
2592 case ISCSI_PARAM_FAST_ABORT:
2593 sscanf(buf, "%d", &session->fast_abort);
2594 break;
2595 case ISCSI_PARAM_ABORT_TMO:
2596 sscanf(buf, "%d", &session->abort_timeout);
2597 break;
2598 case ISCSI_PARAM_LU_RESET_TMO:
2599 sscanf(buf, "%d", &session->lu_reset_timeout);
2600 break;
2601 case ISCSI_PARAM_PING_TMO:
2602 sscanf(buf, "%d", &conn->ping_timeout);
2603 break;
2604 case ISCSI_PARAM_RECV_TMO:
2605 sscanf(buf, "%d", &conn->recv_timeout);
2606 break;
2607 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2608 sscanf(buf, "%d", &conn->max_recv_dlength);
2609 break;
2610 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2611 sscanf(buf, "%d", &conn->max_xmit_dlength);
2612 break;
2613 case ISCSI_PARAM_HDRDGST_EN:
2614 sscanf(buf, "%d", &conn->hdrdgst_en);
2615 break;
2616 case ISCSI_PARAM_DATADGST_EN:
2617 sscanf(buf, "%d", &conn->datadgst_en);
2618 break;
2619 case ISCSI_PARAM_INITIAL_R2T_EN:
2620 sscanf(buf, "%d", &session->initial_r2t_en);
2621 break;
2622 case ISCSI_PARAM_MAX_R2T:
2623 sscanf(buf, "%d", &session->max_r2t);
2624 break;
2625 case ISCSI_PARAM_IMM_DATA_EN:
2626 sscanf(buf, "%d", &session->imm_data_en);
2627 break;
2628 case ISCSI_PARAM_FIRST_BURST:
2629 sscanf(buf, "%d", &session->first_burst);
2630 break;
2631 case ISCSI_PARAM_MAX_BURST:
2632 sscanf(buf, "%d", &session->max_burst);
2633 break;
2634 case ISCSI_PARAM_PDU_INORDER_EN:
2635 sscanf(buf, "%d", &session->pdu_inorder_en);
2636 break;
2637 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2638 sscanf(buf, "%d", &session->dataseq_inorder_en);
2639 break;
2640 case ISCSI_PARAM_ERL:
2641 sscanf(buf, "%d", &session->erl);
2642 break;
2643 case ISCSI_PARAM_IFMARKER_EN:
2644 sscanf(buf, "%d", &value);
2645 BUG_ON(value);
2646 break;
2647 case ISCSI_PARAM_OFMARKER_EN:
2648 sscanf(buf, "%d", &value);
2649 BUG_ON(value);
2650 break;
2651 case ISCSI_PARAM_EXP_STATSN:
2652 sscanf(buf, "%u", &conn->exp_statsn);
2653 break;
2654 case ISCSI_PARAM_USERNAME:
2655 kfree(session->username);
2656 session->username = kstrdup(buf, GFP_KERNEL);
2657 if (!session->username)
2658 return -ENOMEM;
2659 break;
2660 case ISCSI_PARAM_USERNAME_IN:
2661 kfree(session->username_in);
2662 session->username_in = kstrdup(buf, GFP_KERNEL);
2663 if (!session->username_in)
2664 return -ENOMEM;
2665 break;
2666 case ISCSI_PARAM_PASSWORD:
2667 kfree(session->password);
2668 session->password = kstrdup(buf, GFP_KERNEL);
2669 if (!session->password)
2670 return -ENOMEM;
2671 break;
2672 case ISCSI_PARAM_PASSWORD_IN:
2673 kfree(session->password_in);
2674 session->password_in = kstrdup(buf, GFP_KERNEL);
2675 if (!session->password_in)
2676 return -ENOMEM;
2677 break;
2678 case ISCSI_PARAM_TARGET_NAME:
2679 /* this should not change between logins */
2680 if (session->targetname)
2681 break;
2683 session->targetname = kstrdup(buf, GFP_KERNEL);
2684 if (!session->targetname)
2685 return -ENOMEM;
2686 break;
2687 case ISCSI_PARAM_TPGT:
2688 sscanf(buf, "%d", &session->tpgt);
2689 break;
2690 case ISCSI_PARAM_PERSISTENT_PORT:
2691 sscanf(buf, "%d", &conn->persistent_port);
2692 break;
2693 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2695 * this is the address returned in discovery so it should
2696 * not change between logins.
2698 if (conn->persistent_address)
2699 break;
2701 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2702 if (!conn->persistent_address)
2703 return -ENOMEM;
2704 break;
2705 case ISCSI_PARAM_IFACE_NAME:
2706 if (!session->ifacename)
2707 session->ifacename = kstrdup(buf, GFP_KERNEL);
2708 break;
2709 case ISCSI_PARAM_INITIATOR_NAME:
2710 if (!session->initiatorname)
2711 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2712 break;
2713 default:
2714 return -ENOSYS;
2717 return 0;
2719 EXPORT_SYMBOL_GPL(iscsi_set_param);
2721 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2722 enum iscsi_param param, char *buf)
2724 struct iscsi_session *session = cls_session->dd_data;
2725 int len;
2727 switch(param) {
2728 case ISCSI_PARAM_FAST_ABORT:
2729 len = sprintf(buf, "%d\n", session->fast_abort);
2730 break;
2731 case ISCSI_PARAM_ABORT_TMO:
2732 len = sprintf(buf, "%d\n", session->abort_timeout);
2733 break;
2734 case ISCSI_PARAM_LU_RESET_TMO:
2735 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2736 break;
2737 case ISCSI_PARAM_INITIAL_R2T_EN:
2738 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2739 break;
2740 case ISCSI_PARAM_MAX_R2T:
2741 len = sprintf(buf, "%hu\n", session->max_r2t);
2742 break;
2743 case ISCSI_PARAM_IMM_DATA_EN:
2744 len = sprintf(buf, "%d\n", session->imm_data_en);
2745 break;
2746 case ISCSI_PARAM_FIRST_BURST:
2747 len = sprintf(buf, "%u\n", session->first_burst);
2748 break;
2749 case ISCSI_PARAM_MAX_BURST:
2750 len = sprintf(buf, "%u\n", session->max_burst);
2751 break;
2752 case ISCSI_PARAM_PDU_INORDER_EN:
2753 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2754 break;
2755 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2756 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2757 break;
2758 case ISCSI_PARAM_ERL:
2759 len = sprintf(buf, "%d\n", session->erl);
2760 break;
2761 case ISCSI_PARAM_TARGET_NAME:
2762 len = sprintf(buf, "%s\n", session->targetname);
2763 break;
2764 case ISCSI_PARAM_TPGT:
2765 len = sprintf(buf, "%d\n", session->tpgt);
2766 break;
2767 case ISCSI_PARAM_USERNAME:
2768 len = sprintf(buf, "%s\n", session->username);
2769 break;
2770 case ISCSI_PARAM_USERNAME_IN:
2771 len = sprintf(buf, "%s\n", session->username_in);
2772 break;
2773 case ISCSI_PARAM_PASSWORD:
2774 len = sprintf(buf, "%s\n", session->password);
2775 break;
2776 case ISCSI_PARAM_PASSWORD_IN:
2777 len = sprintf(buf, "%s\n", session->password_in);
2778 break;
2779 case ISCSI_PARAM_IFACE_NAME:
2780 len = sprintf(buf, "%s\n", session->ifacename);
2781 break;
2782 case ISCSI_PARAM_INITIATOR_NAME:
2783 if (!session->initiatorname)
2784 len = sprintf(buf, "%s\n", "unknown");
2785 else
2786 len = sprintf(buf, "%s\n", session->initiatorname);
2787 break;
2788 default:
2789 return -ENOSYS;
2792 return len;
2794 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2796 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2797 enum iscsi_param param, char *buf)
2799 struct iscsi_conn *conn = cls_conn->dd_data;
2800 int len;
2802 switch(param) {
2803 case ISCSI_PARAM_PING_TMO:
2804 len = sprintf(buf, "%u\n", conn->ping_timeout);
2805 break;
2806 case ISCSI_PARAM_RECV_TMO:
2807 len = sprintf(buf, "%u\n", conn->recv_timeout);
2808 break;
2809 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2810 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2811 break;
2812 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2813 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2814 break;
2815 case ISCSI_PARAM_HDRDGST_EN:
2816 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2817 break;
2818 case ISCSI_PARAM_DATADGST_EN:
2819 len = sprintf(buf, "%d\n", conn->datadgst_en);
2820 break;
2821 case ISCSI_PARAM_IFMARKER_EN:
2822 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2823 break;
2824 case ISCSI_PARAM_OFMARKER_EN:
2825 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2826 break;
2827 case ISCSI_PARAM_EXP_STATSN:
2828 len = sprintf(buf, "%u\n", conn->exp_statsn);
2829 break;
2830 case ISCSI_PARAM_PERSISTENT_PORT:
2831 len = sprintf(buf, "%d\n", conn->persistent_port);
2832 break;
2833 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2834 len = sprintf(buf, "%s\n", conn->persistent_address);
2835 break;
2836 default:
2837 return -ENOSYS;
2840 return len;
2842 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2844 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2845 char *buf)
2847 struct iscsi_host *ihost = shost_priv(shost);
2848 int len;
2850 switch (param) {
2851 case ISCSI_HOST_PARAM_NETDEV_NAME:
2852 if (!ihost->netdev)
2853 len = sprintf(buf, "%s\n", "default");
2854 else
2855 len = sprintf(buf, "%s\n", ihost->netdev);
2856 break;
2857 case ISCSI_HOST_PARAM_HWADDRESS:
2858 if (!ihost->hwaddress)
2859 len = sprintf(buf, "%s\n", "default");
2860 else
2861 len = sprintf(buf, "%s\n", ihost->hwaddress);
2862 break;
2863 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2864 if (!ihost->initiatorname)
2865 len = sprintf(buf, "%s\n", "unknown");
2866 else
2867 len = sprintf(buf, "%s\n", ihost->initiatorname);
2868 break;
2869 case ISCSI_HOST_PARAM_IPADDRESS:
2870 if (!strlen(ihost->local_address))
2871 len = sprintf(buf, "%s\n", "unknown");
2872 else
2873 len = sprintf(buf, "%s\n",
2874 ihost->local_address);
2875 break;
2876 default:
2877 return -ENOSYS;
2880 return len;
2882 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2884 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2885 char *buf, int buflen)
2887 struct iscsi_host *ihost = shost_priv(shost);
2889 switch (param) {
2890 case ISCSI_HOST_PARAM_NETDEV_NAME:
2891 if (!ihost->netdev)
2892 ihost->netdev = kstrdup(buf, GFP_KERNEL);
2893 break;
2894 case ISCSI_HOST_PARAM_HWADDRESS:
2895 if (!ihost->hwaddress)
2896 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
2897 break;
2898 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2899 if (!ihost->initiatorname)
2900 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
2901 break;
2902 default:
2903 return -ENOSYS;
2906 return 0;
2908 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2910 MODULE_AUTHOR("Mike Christie");
2911 MODULE_DESCRIPTION("iSCSI library functions");
2912 MODULE_LICENSE("GPL");