blob: 59365864c9894c8f09d82d6f2250e96ee3e40a5d [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
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
9 *
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.
14 *
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.
19 *
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.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
Mike Christie8eb00532007-02-28 17:32:19 -060027#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050028#include <net/tcp.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi.h>
35#include <scsi/iscsi_proto.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_iscsi.h>
38#include <scsi/libiscsi.h>
39
Boaz Harrosh004d6532007-12-13 12:43:23 -060040static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
41 int err);
42
Mike Christie7996a772006-04-06 21:13:41 -050043struct iscsi_session *
44class_to_transport_session(struct iscsi_cls_session *cls_session)
45{
46 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
47 return iscsi_hostdata(shost->hostdata);
48}
49EXPORT_SYMBOL_GPL(class_to_transport_session);
50
Mike Christie77a23c22007-05-30 12:57:18 -050051/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
52#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050053
Mike Christie77a23c22007-05-30 12:57:18 -050054static int iscsi_sna_lt(u32 n1, u32 n2)
55{
56 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
57 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
58}
59
60/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
61static int iscsi_sna_lte(u32 n1, u32 n2)
62{
63 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
64 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
65}
66
67void
68iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050069{
70 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
71 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
72
Mike Christie77a23c22007-05-30 12:57:18 -050073 /*
74 * standard specifies this check for when to update expected and
75 * max sequence numbers
76 */
77 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
78 return;
79
80 if (exp_cmdsn != session->exp_cmdsn &&
81 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050082 session->exp_cmdsn = exp_cmdsn;
83
Mike Christie77a23c22007-05-30 12:57:18 -050084 if (max_cmdsn != session->max_cmdsn &&
85 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
86 session->max_cmdsn = max_cmdsn;
87 /*
88 * if the window closed with IO queued, then kick the
89 * xmit thread
90 */
91 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie843c0a82007-12-13 12:43:20 -060092 !list_empty(&session->leadconn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -050093 scsi_queue_work(session->host,
94 &session->leadconn->xmitwork);
95 }
Mike Christie7996a772006-04-06 21:13:41 -050096}
Mike Christie77a23c22007-05-30 12:57:18 -050097EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050098
99void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -0400100 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500101{
102 struct iscsi_conn *conn = ctask->conn;
103
104 memset(hdr, 0, sizeof(struct iscsi_data));
105 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
106 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
107 ctask->unsol_datasn++;
108 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
109 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
110
111 hdr->itt = ctask->hdr->itt;
112 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400113 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500114
115 if (ctask->unsol_count > conn->max_xmit_dlength) {
116 hton24(hdr->dlength, conn->max_xmit_dlength);
117 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -0400118 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500119 hdr->flags = 0;
120 } else {
121 hton24(hdr->dlength, ctask->unsol_count);
122 ctask->data_count = ctask->unsol_count;
123 hdr->flags = ISCSI_FLAG_CMD_FINAL;
124 }
125}
126EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
127
Boaz Harrosh004d6532007-12-13 12:43:23 -0600128static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
129{
130 unsigned exp_len = ctask->hdr_len + len;
131
132 if (exp_len > ctask->hdr_max) {
133 WARN_ON(1);
134 return -EINVAL;
135 }
136
137 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
138 ctask->hdr_len = exp_len;
139 return 0;
140}
141
Mike Christie7996a772006-04-06 21:13:41 -0500142/**
143 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
144 * @ctask: iscsi cmd task
145 *
146 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
147 * fields like dlength or final based on how much data it sends
148 */
Boaz Harrosh004d6532007-12-13 12:43:23 -0600149static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500150{
151 struct iscsi_conn *conn = ctask->conn;
152 struct iscsi_session *session = conn->session;
153 struct iscsi_cmd *hdr = ctask->hdr;
154 struct scsi_cmnd *sc = ctask->sc;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600155 unsigned hdrlength;
156 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500157
Boaz Harrosh004d6532007-12-13 12:43:23 -0600158 ctask->hdr_len = 0;
159 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
160 if (rc)
161 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500162 hdr->opcode = ISCSI_OP_SCSI_CMD;
163 hdr->flags = ISCSI_ATTR_SIMPLE;
164 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Al Virob4377352007-02-09 16:39:40 +0000165 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900166 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500167 hdr->cmdsn = cpu_to_be32(session->cmdsn);
168 session->cmdsn++;
169 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
170 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
Mike Christied473cc72007-05-30 12:57:14 -0500171 if (sc->cmd_len < MAX_COMMAND_SIZE)
172 memset(&hdr->cdb[sc->cmd_len], 0,
173 MAX_COMMAND_SIZE - sc->cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500174
Mike Christieffd04362006-08-31 18:09:24 -0400175 ctask->data_count = 0;
Mike Christie218432c2007-05-30 12:57:17 -0500176 ctask->imm_count = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500177 if (sc->sc_data_direction == DMA_TO_DEVICE) {
178 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
179 /*
180 * Write counters:
181 *
182 * imm_count bytes to be sent right after
183 * SCSI PDU Header
184 *
185 * unsol_count bytes(as Data-Out) to be sent
186 * without R2T ack right after
187 * immediate data
188 *
189 * r2t_data_count bytes to be sent via R2T ack's
190 *
191 * pad_count bytes to be sent as zero-padding
192 */
Mike Christie7996a772006-04-06 21:13:41 -0500193 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400194 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500195 ctask->unsol_datasn = 0;
196
197 if (session->imm_data_en) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900198 if (scsi_bufflen(sc) >= session->first_burst)
Mike Christie7996a772006-04-06 21:13:41 -0500199 ctask->imm_count = min(session->first_burst,
200 conn->max_xmit_dlength);
201 else
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900202 ctask->imm_count = min(scsi_bufflen(sc),
Mike Christie7996a772006-04-06 21:13:41 -0500203 conn->max_xmit_dlength);
204 hton24(ctask->hdr->dlength, ctask->imm_count);
205 } else
206 zero_data(ctask->hdr->dlength);
207
Mike Christieffd04362006-08-31 18:09:24 -0400208 if (!session->initial_r2t_en) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500209 ctask->unsol_count = min((session->first_burst),
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900210 (scsi_bufflen(sc))) - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400211 ctask->unsol_offset = ctask->imm_count;
212 }
213
Mike Christie7996a772006-04-06 21:13:41 -0500214 if (!ctask->unsol_count)
215 /* No unsolicit Data-Out's */
216 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
217 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500218 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
219 zero_data(hdr->dlength);
220
221 if (sc->sc_data_direction == DMA_FROM_DEVICE)
222 hdr->flags |= ISCSI_FLAG_CMD_READ;
223 }
224
Boaz Harrosh004d6532007-12-13 12:43:23 -0600225 /* calculate size of additional header segments (AHSs) */
226 hdrlength = ctask->hdr_len - sizeof(*hdr);
227
228 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
229 hdrlength /= ISCSI_PAD_LEN;
230
231 WARN_ON(hdrlength >= 256);
232 hdr->hlength = hdrlength & 0xFF;
233
Mike Christie7996a772006-04-06 21:13:41 -0500234 conn->scsicmd_pdus_cnt++;
Mike Christie77a23c22007-05-30 12:57:18 -0500235
236 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
237 "cmdsn %d win %d]\n",
238 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900239 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
Mike Christie77a23c22007-05-30 12:57:18 -0500240 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600241 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500242}
Mike Christie7996a772006-04-06 21:13:41 -0500243
244/**
245 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500246 * @ctask: iscsi cmd task
247 *
248 * Must be called with session lock.
249 * This function returns the scsi command to scsi-ml and returns
250 * the cmd task to the pool of available cmd tasks.
251 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400252static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500253{
Mike Christie60ecebf2006-08-31 18:09:25 -0400254 struct iscsi_session *session = ctask->conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500255 struct scsi_cmnd *sc = ctask->sc;
256
Mike Christieb6c395e2006-07-24 15:47:15 -0500257 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500258 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400259 /* SCSI eh reuses commands to verify us */
260 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500261 list_del_init(&ctask->running);
262 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
263 sc->scsi_done(sc);
264}
265
Mike Christie60ecebf2006-08-31 18:09:25 -0400266static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
267{
268 atomic_inc(&ctask->refcount);
269}
270
Mike Christie60ecebf2006-08-31 18:09:25 -0400271static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
272{
Mike Christiee648f632006-08-31 18:09:34 -0400273 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400274 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400275}
276
Mike Christie7996a772006-04-06 21:13:41 -0500277/**
278 * iscsi_cmd_rsp - SCSI Command Response processing
279 * @conn: iscsi connection
280 * @hdr: iscsi header
281 * @ctask: scsi command task
282 * @data: cmd data buffer
283 * @datalen: len of buffer
284 *
285 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
286 * then completes the command and task.
287 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500288static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
289 struct iscsi_cmd_task *ctask, char *data,
290 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500291{
Mike Christie7996a772006-04-06 21:13:41 -0500292 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
293 struct iscsi_session *session = conn->session;
294 struct scsi_cmnd *sc = ctask->sc;
295
Mike Christie77a23c22007-05-30 12:57:18 -0500296 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500297 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
298
299 sc->result = (DID_OK << 16) | rhdr->cmd_status;
300
301 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
302 sc->result = DID_ERROR << 16;
303 goto out;
304 }
305
306 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600307 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500308
309 if (datalen < 2) {
310invalid_datalen:
Or Gerlitzbe2df722006-05-02 19:46:43 -0500311 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
312 "invalid data buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500313 sc->result = DID_BAD_TARGET << 16;
314 goto out;
315 }
316
Mike Christie8eb00532007-02-28 17:32:19 -0600317 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500318 if (datalen < senselen)
319 goto invalid_datalen;
320
321 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600322 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500323 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600324 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500325 }
326
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600327 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
328 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500329 int res_count = be32_to_cpu(rhdr->residual_count);
330
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600331 if (res_count > 0 &&
332 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
333 res_count <= scsi_bufflen(sc)))
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900334 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500335 else
336 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600337 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
338 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
Mike Christie7996a772006-04-06 21:13:41 -0500339 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Mike Christie7996a772006-04-06 21:13:41 -0500340
341out:
342 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
343 (long)sc, sc->result, ctask->itt);
344 conn->scsirsp_pdus_cnt++;
345
Mike Christie60ecebf2006-08-31 18:09:25 -0400346 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500347}
348
Mike Christie7ea8b822006-07-24 15:47:22 -0500349static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
350{
351 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
352
353 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
354 conn->tmfrsp_pdus_cnt++;
355
Mike Christie843c0a82007-12-13 12:43:20 -0600356 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500357 return;
358
359 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600360 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500361 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600362 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500363 else
Mike Christie843c0a82007-12-13 12:43:20 -0600364 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500365 wake_up(&conn->ehwait);
366}
367
Mike Christie62f38302006-08-31 18:09:27 -0400368static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
369 char *data, int datalen)
370{
371 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
372 struct iscsi_hdr rejected_pdu;
373 uint32_t itt;
374
375 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
376
377 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
378 if (ntoh24(reject->dlength) > datalen)
379 return ISCSI_ERR_PROTO;
380
381 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
382 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000383 itt = get_itt(rejected_pdu.itt);
Mike Christie62f38302006-08-31 18:09:27 -0400384 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
385 "due to DataDigest error.\n", itt,
386 rejected_pdu.opcode);
387 }
388 }
389 return 0;
390}
391
Mike Christie7996a772006-04-06 21:13:41 -0500392/**
393 * __iscsi_complete_pdu - complete pdu
394 * @conn: iscsi conn
395 * @hdr: iscsi header
396 * @data: data buffer
397 * @datalen: len of data buffer
398 *
399 * Completes pdu processing by freeing any resources allocated at
400 * queuecommand or send generic. session lock must be held and verify
401 * itt must have been called.
402 */
403int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
404 char *data, int datalen)
405{
406 struct iscsi_session *session = conn->session;
407 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
408 struct iscsi_cmd_task *ctask;
409 struct iscsi_mgmt_task *mtask;
410 uint32_t itt;
411
Al Virob4377352007-02-09 16:39:40 +0000412 if (hdr->itt != RESERVED_ITT)
413 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500414 else
Al Virob4377352007-02-09 16:39:40 +0000415 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500416
417 if (itt < session->cmds_max) {
418 ctask = session->cmds[itt];
419
420 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
421 opcode, conn->id, ctask->itt, datalen);
422
423 switch(opcode) {
424 case ISCSI_OP_SCSI_CMD_RSP:
425 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500426 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
427 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500428 break;
429 case ISCSI_OP_SCSI_DATA_IN:
430 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
431 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
432 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400433 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500434 }
435 break;
436 case ISCSI_OP_R2T:
437 /* LLD handles this for now */
438 break;
439 default:
440 rc = ISCSI_ERR_BAD_OPCODE;
441 break;
442 }
443 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
444 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
445 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
446
447 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
448 opcode, conn->id, mtask->itt, datalen);
449
Mike Christie77a23c22007-05-30 12:57:18 -0500450 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500451 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500452 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500453 if (datalen) {
454 rc = ISCSI_ERR_PROTO;
455 break;
456 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500457 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
458 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500459 case ISCSI_OP_LOGIN_RSP:
460 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500461 /*
462 * login related PDU's exp_statsn is handled in
463 * userspace
464 */
Mike Christie40527af2006-07-24 15:47:45 -0500465 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
466 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -0600467 list_del_init(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500468 if (conn->login_mtask != mtask)
469 __kfifo_put(session->mgmtpool.queue,
470 (void*)&mtask, sizeof(void*));
471 break;
472 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500473 if (datalen) {
474 rc = ISCSI_ERR_PROTO;
475 break;
476 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500477
Mike Christie7ea8b822006-07-24 15:47:22 -0500478 iscsi_tmf_rsp(conn, hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500479 break;
480 case ISCSI_OP_NOOP_IN:
Al Virob4377352007-02-09 16:39:40 +0000481 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500482 rc = ISCSI_ERR_PROTO;
483 break;
484 }
Mike Christie7996a772006-04-06 21:13:41 -0500485 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
486
Mike Christie40527af2006-07-24 15:47:45 -0500487 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
488 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -0600489 list_del_init(&mtask->running);
490 __kfifo_put(session->mgmtpool.queue,
491 (void*)&mtask, sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -0500492 break;
493 default:
494 rc = ISCSI_ERR_BAD_OPCODE;
495 break;
496 }
Al Virob4377352007-02-09 16:39:40 +0000497 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500498 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400499
Mike Christie7996a772006-04-06 21:13:41 -0500500 switch(opcode) {
501 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500502 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500503 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500504 break;
505 }
506
Al Virob4377352007-02-09 16:39:40 +0000507 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500508 break;
509
510 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
511 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500512 break;
513 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400514 rc = iscsi_handle_reject(conn, hdr, data, datalen);
515 break;
Mike Christie7996a772006-04-06 21:13:41 -0500516 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500517 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400518 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
519 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500520 break;
521 default:
522 rc = ISCSI_ERR_BAD_OPCODE;
523 break;
524 }
525 } else
526 rc = ISCSI_ERR_BAD_ITT;
527
528 return rc;
529}
530EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
531
532int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
533 char *data, int datalen)
534{
535 int rc;
536
537 spin_lock(&conn->session->lock);
538 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
539 spin_unlock(&conn->session->lock);
540 return rc;
541}
542EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
543
544/* verify itt (itt encoding: age+cid+itt) */
545int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
546 uint32_t *ret_itt)
547{
548 struct iscsi_session *session = conn->session;
549 struct iscsi_cmd_task *ctask;
550 uint32_t itt;
551
Al Virob4377352007-02-09 16:39:40 +0000552 if (hdr->itt != RESERVED_ITT) {
553 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500554 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500555 printk(KERN_ERR "iscsi: received itt %x expected "
Al Virob4377352007-02-09 16:39:40 +0000556 "session age (%x)\n", (__force u32)hdr->itt,
Mike Christie7996a772006-04-06 21:13:41 -0500557 session->age & ISCSI_AGE_MASK);
558 return ISCSI_ERR_BAD_ITT;
559 }
560
Al Virob4377352007-02-09 16:39:40 +0000561 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500562 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500563 printk(KERN_ERR "iscsi: received itt %x, expected "
Al Virob4377352007-02-09 16:39:40 +0000564 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500565 return ISCSI_ERR_BAD_ITT;
566 }
Al Virob4377352007-02-09 16:39:40 +0000567 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500568 } else
Al Virob4377352007-02-09 16:39:40 +0000569 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500570
571 if (itt < session->cmds_max) {
572 ctask = session->cmds[itt];
573
574 if (!ctask->sc) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500575 printk(KERN_INFO "iscsi: dropping ctask with "
Mike Christie7996a772006-04-06 21:13:41 -0500576 "itt 0x%x\n", ctask->itt);
577 /* force drop */
578 return ISCSI_ERR_NO_SCSI_CMD;
579 }
580
581 if (ctask->sc->SCp.phase != session->age) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500582 printk(KERN_ERR "iscsi: ctask's session age %d, "
Mike Christie7996a772006-04-06 21:13:41 -0500583 "expected %d\n", ctask->sc->SCp.phase,
584 session->age);
585 return ISCSI_ERR_SESSION_FAILED;
586 }
587 }
588
589 *ret_itt = itt;
590 return 0;
591}
592EXPORT_SYMBOL_GPL(iscsi_verify_itt);
593
594void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
595{
596 struct iscsi_session *session = conn->session;
597 unsigned long flags;
598
599 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500600 if (session->state == ISCSI_STATE_FAILED) {
601 spin_unlock_irqrestore(&session->lock, flags);
602 return;
603 }
604
Mike Christie67a61112006-05-30 00:37:20 -0500605 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500606 session->state = ISCSI_STATE_FAILED;
607 spin_unlock_irqrestore(&session->lock, flags);
608 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
609 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
610 iscsi_conn_error(conn->cls_conn, err);
611}
612EXPORT_SYMBOL_GPL(iscsi_conn_failure);
613
Mike Christie77a23c22007-05-30 12:57:18 -0500614static void iscsi_prep_mtask(struct iscsi_conn *conn,
615 struct iscsi_mgmt_task *mtask)
616{
617 struct iscsi_session *session = conn->session;
618 struct iscsi_hdr *hdr = mtask->hdr;
619 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
620
621 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
622 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
623 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
624 /*
625 * pre-format CmdSN for outgoing PDU.
626 */
627 nop->cmdsn = cpu_to_be32(session->cmdsn);
628 if (hdr->itt != RESERVED_ITT) {
629 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500630 /*
631 * TODO: We always use immediate, so we never hit this.
632 * If we start to send tmfs or nops as non-immediate then
633 * we should start checking the cmdsn numbers for mgmt tasks.
634 */
Mike Christie77a23c22007-05-30 12:57:18 -0500635 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500636 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
637 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500638 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500639 }
Mike Christie77a23c22007-05-30 12:57:18 -0500640 }
641
642 if (session->tt->init_mgmt_task)
643 session->tt->init_mgmt_task(conn, mtask);
644
645 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
Mike Christie843c0a82007-12-13 12:43:20 -0600646 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
647 mtask->data_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500648}
649
Mike Christie05db8882007-02-28 17:32:16 -0600650static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400651{
652 struct iscsi_hdr *hdr = conn->mtask->hdr;
653 int rc, was_logout = 0;
654
Mike Christie77a23c22007-05-30 12:57:18 -0500655 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400656 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
657 conn->session->state = ISCSI_STATE_IN_RECOVERY;
658 iscsi_block_session(session_to_cls(conn->session));
659 was_logout = 1;
660 }
661 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500662 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400663 if (rc)
664 return rc;
665
Mike Christie05db8882007-02-28 17:32:16 -0600666 /* done with this in-progress mtask */
667 conn->mtask = NULL;
668
Mike Christieb5072ea2006-10-16 18:09:42 -0400669 if (was_logout) {
670 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
671 return -ENODATA;
672 }
673 return 0;
674}
675
Mike Christie77a23c22007-05-30 12:57:18 -0500676static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
677{
678 struct iscsi_session *session = conn->session;
679
680 /*
681 * Check for iSCSI window and take care of CmdSN wrap-around
682 */
Mike Christiee0726402007-07-26 12:46:48 -0500683 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
684 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
685 "CmdSN %u/%u\n", session->exp_cmdsn,
686 session->max_cmdsn, session->cmdsn,
687 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500688 return -ENOSPC;
689 }
690 return 0;
691}
692
693static int iscsi_xmit_ctask(struct iscsi_conn *conn)
694{
695 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600696 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500697
698 __iscsi_get_ctask(ctask);
699 spin_unlock_bh(&conn->session->lock);
700 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
701 spin_lock_bh(&conn->session->lock);
702 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500703 if (!rc)
704 /* done with this ctask */
705 conn->ctask = NULL;
706 return rc;
707}
708
Mike Christie7996a772006-04-06 21:13:41 -0500709/**
Mike Christie843c0a82007-12-13 12:43:20 -0600710 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
711 * @ctask: ctask to requeue
712 *
713 * LLDs that need to run a ctask from the session workqueue should call
714 * this. The session lock must be held.
715 */
716void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
717{
718 struct iscsi_conn *conn = ctask->conn;
719
720 list_move_tail(&ctask->running, &conn->requeue);
721 scsi_queue_work(conn->session->host, &conn->xmitwork);
722}
723EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
724
725/**
Mike Christie7996a772006-04-06 21:13:41 -0500726 * iscsi_data_xmit - xmit any command into the scheduled connection
727 * @conn: iscsi connection
728 *
729 * Notes:
730 * The function can return -EAGAIN in which case the caller must
731 * re-schedule it again later or recover. '0' return code means
732 * successful xmit.
733 **/
734static int iscsi_data_xmit(struct iscsi_conn *conn)
735{
Mike Christie3219e522006-05-30 00:37:28 -0500736 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500737
Mike Christie77a23c22007-05-30 12:57:18 -0500738 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500739 if (unlikely(conn->suspend_tx)) {
740 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500741 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500742 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500743 }
Mike Christie7996a772006-04-06 21:13:41 -0500744
745 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500746 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500747 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500748 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500749 }
Mike Christie77a23c22007-05-30 12:57:18 -0500750
Mike Christie7996a772006-04-06 21:13:41 -0500751 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600752 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500753 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500754 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500755 }
756
Mike Christie77a23c22007-05-30 12:57:18 -0500757 /*
758 * process mgmt pdus like nops before commands since we should
759 * only have one nop-out as a ping from us and targets should not
760 * overflow us with nop-ins
761 */
762check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -0600763 while (!list_empty(&conn->mgmtqueue)) {
764 conn->mtask = list_entry(conn->mgmtqueue.next,
765 struct iscsi_mgmt_task, running);
Mike Christie77a23c22007-05-30 12:57:18 -0500766 iscsi_prep_mtask(conn, conn->mtask);
Mike Christie843c0a82007-12-13 12:43:20 -0600767 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500768 rc = iscsi_xmit_mtask(conn);
769 if (rc)
770 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500771 }
772
Mike Christie843c0a82007-12-13 12:43:20 -0600773 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500774 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -0600775 if (conn->tmf_state == TMF_QUEUED)
776 break;
777
Mike Christieb6c395e2006-07-24 15:47:15 -0500778 conn->ctask = list_entry(conn->xmitqueue.next,
779 struct iscsi_cmd_task, running);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600780 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
781 fail_command(conn, conn->ctask, DID_ABORT << 16);
782 continue;
783 }
Mike Christie843c0a82007-12-13 12:43:20 -0600784 conn->session->tt->init_cmd_task(conn->ctask);
785 conn->ctask->state = ISCSI_TASK_RUNNING;
Mike Christieb6c395e2006-07-24 15:47:15 -0500786 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500787 rc = iscsi_xmit_ctask(conn);
788 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400789 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500790 /*
791 * we could continuously get new ctask requests so
792 * we need to check the mgmt queue for nops that need to
793 * be sent to aviod starvation
794 */
Mike Christie843c0a82007-12-13 12:43:20 -0600795 if (!list_empty(&conn->mgmtqueue))
796 goto check_mgmt;
797 }
798
799 while (!list_empty(&conn->requeue)) {
800 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
801 break;
802
803 conn->ctask = list_entry(conn->requeue.next,
804 struct iscsi_cmd_task, running);
805 conn->ctask->state = ISCSI_TASK_RUNNING;
806 list_move_tail(conn->requeue.next, &conn->run_list);
807 rc = iscsi_xmit_ctask(conn);
808 if (rc)
809 goto again;
810 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -0500811 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500812 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500813 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500814 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500815
816again:
817 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500818 rc = -ENODATA;
819 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500820 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500821}
822
David Howellsc4028952006-11-22 14:57:56 +0000823static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500824{
David Howellsc4028952006-11-22 14:57:56 +0000825 struct iscsi_conn *conn =
826 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500827 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500828 /*
829 * serialize Xmit worker on a per-connection basis.
830 */
Mike Christie3219e522006-05-30 00:37:28 -0500831 do {
832 rc = iscsi_data_xmit(conn);
833 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500834}
835
836enum {
837 FAILURE_BAD_HOST = 1,
838 FAILURE_SESSION_FAILED,
839 FAILURE_SESSION_FREED,
840 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400841 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500842 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500843 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500844 FAILURE_SESSION_RECOVERY_TIMEOUT,
845};
846
847int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
848{
849 struct Scsi_Host *host;
850 int reason = 0;
851 struct iscsi_session *session;
852 struct iscsi_conn *conn;
853 struct iscsi_cmd_task *ctask = NULL;
854
855 sc->scsi_done = done;
856 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400857 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500858
859 host = sc->device->host;
860 session = iscsi_hostdata(host->hostdata);
861
862 spin_lock(&session->lock);
863
Mike Christie656cffc2006-05-18 20:31:42 -0500864 /*
865 * ISCSI_STATE_FAILED is a temp. state. The recovery
866 * code will decide what is best to do with command queued
867 * during this time
868 */
869 if (session->state != ISCSI_STATE_LOGGED_IN &&
870 session->state != ISCSI_STATE_FAILED) {
871 /*
872 * to handle the race between when we set the recovery state
873 * and block the session we requeue here (commands could
874 * be entering our queuecommand while a block is starting
875 * up because the block code is not locked)
876 */
877 if (session->state == ISCSI_STATE_IN_RECOVERY) {
878 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500879 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500880 }
Mike Christie656cffc2006-05-18 20:31:42 -0500881
882 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
883 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
884 else if (session->state == ISCSI_STATE_TERMINATE)
885 reason = FAILURE_SESSION_TERMINATE;
886 else
887 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500888 goto fault;
889 }
890
Mike Christie7996a772006-04-06 21:13:41 -0500891 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400892 if (!conn) {
893 reason = FAILURE_SESSION_FREED;
894 goto fault;
895 }
Mike Christie7996a772006-04-06 21:13:41 -0500896
Mike Christie77a23c22007-05-30 12:57:18 -0500897 if (iscsi_check_cmdsn_window_closed(conn)) {
898 reason = FAILURE_WINDOW_CLOSED;
899 goto reject;
900 }
901
Mike Christie60ecebf2006-08-31 18:09:25 -0400902 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
903 sizeof(void*))) {
904 reason = FAILURE_OOM;
905 goto reject;
906 }
Mike Christiee0726402007-07-26 12:46:48 -0500907 session->queued_cmdsn++;
908
Mike Christie7996a772006-04-06 21:13:41 -0500909 sc->SCp.phase = session->age;
910 sc->SCp.ptr = (char *)ctask;
911
Mike Christie60ecebf2006-08-31 18:09:25 -0400912 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -0500913 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500914 ctask->conn = conn;
915 ctask->sc = sc;
916 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500917
Mike Christieb6c395e2006-07-24 15:47:15 -0500918 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500919 spin_unlock(&session->lock);
920
921 scsi_queue_work(host, &conn->xmitwork);
922 return 0;
923
924reject:
925 spin_unlock(&session->lock);
926 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
927 return SCSI_MLQUEUE_HOST_BUSY;
928
929fault:
930 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500931 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500932 sc->cmnd[0], reason);
933 sc->result = (DID_NO_CONNECT << 16);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900934 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500935 sc->scsi_done(sc);
936 return 0;
937}
938EXPORT_SYMBOL_GPL(iscsi_queuecommand);
939
940int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
941{
942 if (depth > ISCSI_MAX_CMD_PER_LUN)
943 depth = ISCSI_MAX_CMD_PER_LUN;
944 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
945 return sdev->queue_depth;
946}
947EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
948
Mike Christie77a23c22007-05-30 12:57:18 -0500949static struct iscsi_mgmt_task *
950__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
951 char *data, uint32_t data_size)
Mike Christie7996a772006-04-06 21:13:41 -0500952{
953 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500954 struct iscsi_mgmt_task *mtask;
955
Mike Christie77a23c22007-05-30 12:57:18 -0500956 if (session->state == ISCSI_STATE_TERMINATE)
957 return NULL;
958
Mike Christie7996a772006-04-06 21:13:41 -0500959 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
960 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
961 /*
962 * Login and Text are sent serially, in
963 * request-followed-by-response sequence.
964 * Same mtask can be used. Same ITT must be used.
965 * Note that login_mtask is preallocated at conn_create().
966 */
967 mtask = conn->login_mtask;
968 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500969 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
970 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500971
972 if (!__kfifo_get(session->mgmtpool.queue,
Mike Christie77a23c22007-05-30 12:57:18 -0500973 (void*)&mtask, sizeof(void*)))
974 return NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500975 }
976
Mike Christie7996a772006-04-06 21:13:41 -0500977 if (data_size) {
978 memcpy(mtask->data, data, data_size);
979 mtask->data_count = data_size;
980 } else
981 mtask->data_count = 0;
982
Mike Christie7996a772006-04-06 21:13:41 -0500983 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie843c0a82007-12-13 12:43:20 -0600984 INIT_LIST_HEAD(&mtask->running);
985 list_add_tail(&mtask->running, &conn->mgmtqueue);
Mike Christie77a23c22007-05-30 12:57:18 -0500986 return mtask;
Mike Christie7996a772006-04-06 21:13:41 -0500987}
988
989int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
990 char *data, uint32_t data_size)
991{
992 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie77a23c22007-05-30 12:57:18 -0500993 struct iscsi_session *session = conn->session;
994 int err = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500995
Mike Christie77a23c22007-05-30 12:57:18 -0500996 spin_lock_bh(&session->lock);
997 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
998 err = -EPERM;
999 spin_unlock_bh(&session->lock);
1000 scsi_queue_work(session->host, &conn->xmitwork);
1001 return err;
Mike Christie7996a772006-04-06 21:13:41 -05001002}
1003EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
1004
1005void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1006{
1007 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001008
1009 spin_lock_bh(&session->lock);
1010 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001011 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001012 if (session->leadconn)
1013 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001014 }
1015 spin_unlock_bh(&session->lock);
1016}
1017EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1018
1019int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1020{
1021 struct Scsi_Host *host = sc->device->host;
1022 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1023 struct iscsi_conn *conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001024
1025 spin_lock_bh(&session->lock);
1026 if (session->state == ISCSI_STATE_TERMINATE) {
1027failed:
1028 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001029 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001030 spin_unlock_bh(&session->lock);
1031 return FAILED;
1032 }
1033
Mike Christie7996a772006-04-06 21:13:41 -05001034 spin_unlock_bh(&session->lock);
1035
1036 /*
1037 * we drop the lock here but the leadconn cannot be destoyed while
1038 * we are in the scsi eh
1039 */
Mike Christie843c0a82007-12-13 12:43:20 -06001040 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001041
1042 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1043 wait_event_interruptible(conn->ehwait,
1044 session->state == ISCSI_STATE_TERMINATE ||
1045 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001046 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001047 if (signal_pending(current))
1048 flush_signals(current);
1049
1050 spin_lock_bh(&session->lock);
1051 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001052 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001053 else
1054 goto failed;
1055 spin_unlock_bh(&session->lock);
1056
1057 return SUCCESS;
1058}
1059EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1060
Mike Christie843c0a82007-12-13 12:43:20 -06001061static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001062{
Mike Christie843c0a82007-12-13 12:43:20 -06001063 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001064 struct iscsi_session *session = conn->session;
1065
1066 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001067 if (conn->tmf_state == TMF_QUEUED) {
1068 conn->tmf_state = TMF_TIMEDOUT;
1069 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001070 /* unblock eh_abort() */
1071 wake_up(&conn->ehwait);
1072 }
1073 spin_unlock(&session->lock);
1074}
1075
Mike Christie843c0a82007-12-13 12:43:20 -06001076static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1077 struct iscsi_tm *hdr, int age)
Mike Christie7996a772006-04-06 21:13:41 -05001078{
Mike Christie7996a772006-04-06 21:13:41 -05001079 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001080 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001081
Mike Christie843c0a82007-12-13 12:43:20 -06001082 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1083 NULL, 0);
1084 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001085 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001086 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001087 spin_lock_bh(&session->lock);
1088 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001089 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001090 }
Mike Christie843c0a82007-12-13 12:43:20 -06001091 conn->tmfcmd_pdus_cnt++;
1092 conn->tmf_timer.expires = 30 * HZ + jiffies;
1093 conn->tmf_timer.function = iscsi_tmf_timedout;
1094 conn->tmf_timer.data = (unsigned long)conn;
1095 add_timer(&conn->tmf_timer);
1096 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001097
Mike Christie7996a772006-04-06 21:13:41 -05001098 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001099 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001100 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001101
1102 /*
1103 * block eh thread until:
1104 *
Mike Christie843c0a82007-12-13 12:43:20 -06001105 * 1) tmf response
1106 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001107 * 3) session is terminated or restarted or userspace has
1108 * given up on recovery
1109 */
Mike Christie843c0a82007-12-13 12:43:20 -06001110 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001111 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001112 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001113 if (signal_pending(current))
1114 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001115 del_timer_sync(&conn->tmf_timer);
1116
Mike Christie6724add2007-08-15 01:38:30 -05001117 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001118 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001119 /* if the session drops it will clean up the mtask */
1120 if (age != session->age ||
1121 session->state != ISCSI_STATE_LOGGED_IN)
1122 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001123
Mike Christie843c0a82007-12-13 12:43:20 -06001124 if (!list_empty(&mtask->running)) {
1125 list_del_init(&mtask->running);
1126 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1127 sizeof(void*));
Mike Christieb6c395e2006-07-24 15:47:15 -05001128 }
Mike Christie7996a772006-04-06 21:13:41 -05001129 return 0;
1130}
1131
1132/*
Mike Christie77a23c22007-05-30 12:57:18 -05001133 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001134 */
1135static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1136 int err)
1137{
1138 struct scsi_cmnd *sc;
1139
Mike Christie7996a772006-04-06 21:13:41 -05001140 sc = ctask->sc;
1141 if (!sc)
1142 return;
Mike Christiee648f632006-08-31 18:09:34 -04001143
Mike Christiee0726402007-07-26 12:46:48 -05001144 if (ctask->state == ISCSI_TASK_PENDING)
1145 /*
1146 * cmd never made it to the xmit thread, so we should not count
1147 * the cmd in the sequencing
1148 */
1149 conn->session->queued_cmdsn--;
1150 else
Mike Christie77a23c22007-05-30 12:57:18 -05001151 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001152
Mike Christie7996a772006-04-06 21:13:41 -05001153 sc->result = err;
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001154 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie77a23c22007-05-30 12:57:18 -05001155 if (conn->ctask == ctask)
1156 conn->ctask = NULL;
Mike Christiee648f632006-08-31 18:09:34 -04001157 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001158 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001159}
1160
Mike Christie843c0a82007-12-13 12:43:20 -06001161/*
1162 * Fail commands. session lock held and recv side suspended and xmit
1163 * thread flushed
1164 */
1165static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
1166{
1167 struct iscsi_cmd_task *ctask, *tmp;
1168
1169 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1170 conn->ctask = NULL;
1171
1172 /* flush pending */
1173 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1174 if (lun == ctask->sc->device->lun || lun == -1) {
1175 debug_scsi("failing pending sc %p itt 0x%x\n",
1176 ctask->sc, ctask->itt);
1177 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1178 }
1179 }
1180
1181 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1182 if (lun == ctask->sc->device->lun || lun == -1) {
1183 debug_scsi("failing requeued sc %p itt 0x%x\n",
1184 ctask->sc, ctask->itt);
1185 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1186 }
1187 }
1188
1189 /* fail all other running */
1190 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1191 if (lun == ctask->sc->device->lun || lun == -1) {
1192 debug_scsi("failing in progress sc %p itt 0x%x\n",
1193 ctask->sc, ctask->itt);
1194 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1195 }
1196 }
1197}
1198
Mike Christie6724add2007-08-15 01:38:30 -05001199static void iscsi_suspend_tx(struct iscsi_conn *conn)
1200{
1201 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1202 scsi_flush_work(conn->session->host);
1203}
1204
1205static void iscsi_start_tx(struct iscsi_conn *conn)
1206{
1207 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1208 scsi_queue_work(conn->session->host, &conn->xmitwork);
1209}
1210
Mike Christie843c0a82007-12-13 12:43:20 -06001211static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1212 struct iscsi_tm *hdr)
1213{
1214 memset(hdr, 0, sizeof(*hdr));
1215 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1216 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1217 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1218 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1219 hdr->rtt = ctask->hdr->itt;
1220 hdr->refcmdsn = ctask->hdr->cmdsn;
1221}
1222
Mike Christie7996a772006-04-06 21:13:41 -05001223int iscsi_eh_abort(struct scsi_cmnd *sc)
1224{
Mike Christie6724add2007-08-15 01:38:30 -05001225 struct Scsi_Host *host = sc->device->host;
1226 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001227 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001228 struct iscsi_cmd_task *ctask;
1229 struct iscsi_tm *hdr;
1230 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001231
Mike Christie6724add2007-08-15 01:38:30 -05001232 mutex_lock(&session->eh_mutex);
1233 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001234 /*
1235 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1236 * got the command.
1237 */
1238 if (!sc->SCp.ptr) {
1239 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001240 spin_unlock_bh(&session->lock);
1241 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001242 return SUCCESS;
1243 }
1244
Mike Christie7996a772006-04-06 21:13:41 -05001245 /*
1246 * If we are not logged in or we have started a new session
1247 * then let the host reset code handle this
1248 */
Mike Christie843c0a82007-12-13 12:43:20 -06001249 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1250 sc->SCp.phase != session->age) {
1251 spin_unlock_bh(&session->lock);
1252 mutex_unlock(&session->eh_mutex);
1253 return FAILED;
1254 }
1255
1256 conn = session->leadconn;
1257 conn->eh_abort_cnt++;
1258 age = session->age;
1259
1260 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1261 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001262
1263 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001264 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001265 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001266 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001267 }
Mike Christie7996a772006-04-06 21:13:41 -05001268
Mike Christie77a23c22007-05-30 12:57:18 -05001269 if (ctask->state == ISCSI_TASK_PENDING) {
1270 fail_command(conn, ctask, DID_ABORT << 16);
1271 goto success;
1272 }
Mike Christie7996a772006-04-06 21:13:41 -05001273
Mike Christie843c0a82007-12-13 12:43:20 -06001274 /* only have one tmf outstanding at a time */
1275 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001276 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001277 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001278
Mike Christie843c0a82007-12-13 12:43:20 -06001279 hdr = &conn->tmhdr;
1280 iscsi_prep_abort_task_pdu(ctask, hdr);
1281
1282 if (iscsi_exec_task_mgmt_fn(conn, hdr, age)) {
1283 rc = FAILED;
1284 goto failed;
1285 }
1286
1287 switch (conn->tmf_state) {
1288 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001289 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001290 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001291 /*
1292 * clean up task if aborted. grab the recv lock as a writer
1293 */
1294 write_lock_bh(conn->recv_lock);
1295 spin_lock(&session->lock);
1296 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001297 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001298 spin_unlock(&session->lock);
1299 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001300 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001301 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001302 case TMF_TIMEDOUT:
1303 spin_unlock_bh(&session->lock);
1304 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1305 goto failed_unlocked;
1306 case TMF_NOT_FOUND:
1307 if (!sc->SCp.ptr) {
1308 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001309 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001310 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001311 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001312 }
1313 /* fall through */
1314 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001315 conn->tmf_state = TMF_INITIAL;
1316 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001317 }
1318
Mike Christie77a23c22007-05-30 12:57:18 -05001319success:
Mike Christie7996a772006-04-06 21:13:41 -05001320 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001321success_unlocked:
1322 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001323 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001324 return SUCCESS;
1325
1326failed:
1327 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001328failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001329 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1330 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001331 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001332 return FAILED;
1333}
1334EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1335
Mike Christie843c0a82007-12-13 12:43:20 -06001336static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1337{
1338 memset(hdr, 0, sizeof(*hdr));
1339 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1340 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1341 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1342 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1343 hdr->rtt = ISCSI_RESERVED_TAG;
1344}
1345
1346int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1347{
1348 struct Scsi_Host *host = sc->device->host;
1349 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1350 struct iscsi_conn *conn;
1351 struct iscsi_tm *hdr;
1352 int rc = FAILED;
1353
1354 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1355
1356 mutex_lock(&session->eh_mutex);
1357 spin_lock_bh(&session->lock);
1358 /*
1359 * Just check if we are not logged in. We cannot check for
1360 * the phase because the reset could come from a ioctl.
1361 */
1362 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1363 goto unlock;
1364 conn = session->leadconn;
1365
1366 /* only have one tmf outstanding at a time */
1367 if (conn->tmf_state != TMF_INITIAL)
1368 goto unlock;
1369 conn->tmf_state = TMF_QUEUED;
1370
1371 hdr = &conn->tmhdr;
1372 iscsi_prep_lun_reset_pdu(sc, hdr);
1373
1374 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age)) {
1375 rc = FAILED;
1376 goto unlock;
1377 }
1378
1379 switch (conn->tmf_state) {
1380 case TMF_SUCCESS:
1381 break;
1382 case TMF_TIMEDOUT:
1383 spin_unlock_bh(&session->lock);
1384 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1385 goto done;
1386 default:
1387 conn->tmf_state = TMF_INITIAL;
1388 goto unlock;
1389 }
1390
1391 rc = SUCCESS;
1392 spin_unlock_bh(&session->lock);
1393
1394 iscsi_suspend_tx(conn);
1395 /* need to grab the recv lock then session lock */
1396 write_lock_bh(conn->recv_lock);
1397 spin_lock(&session->lock);
1398 fail_all_commands(conn, sc->device->lun);
1399 conn->tmf_state = TMF_INITIAL;
1400 spin_unlock(&session->lock);
1401 write_unlock_bh(conn->recv_lock);
1402
1403 iscsi_start_tx(conn);
1404 goto done;
1405
1406unlock:
1407 spin_unlock_bh(&session->lock);
1408done:
1409 debug_scsi("iscsi_eh_device_reset %s\n",
1410 rc == SUCCESS ? "SUCCESS" : "FAILED");
1411 mutex_unlock(&session->eh_mutex);
1412 return rc;
1413}
1414EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1415
Mike Christie7996a772006-04-06 21:13:41 -05001416int
1417iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1418{
1419 int i;
1420
1421 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1422 if (*items == NULL)
1423 return -ENOMEM;
1424
1425 q->max = max;
1426 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1427 if (q->pool == NULL) {
1428 kfree(*items);
1429 return -ENOMEM;
1430 }
1431
1432 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1433 GFP_KERNEL, NULL);
1434 if (q->queue == ERR_PTR(-ENOMEM)) {
1435 kfree(q->pool);
1436 kfree(*items);
1437 return -ENOMEM;
1438 }
1439
1440 for (i = 0; i < max; i++) {
1441 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1442 if (q->pool[i] == NULL) {
1443 int j;
1444
1445 for (j = 0; j < i; j++)
1446 kfree(q->pool[j]);
1447
1448 kfifo_free(q->queue);
1449 kfree(q->pool);
1450 kfree(*items);
1451 return -ENOMEM;
1452 }
1453 memset(q->pool[i], 0, item_size);
1454 (*items)[i] = q->pool[i];
1455 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1456 }
1457 return 0;
1458}
1459EXPORT_SYMBOL_GPL(iscsi_pool_init);
1460
1461void iscsi_pool_free(struct iscsi_queue *q, void **items)
1462{
1463 int i;
1464
1465 for (i = 0; i < q->max; i++)
1466 kfree(items[i]);
1467 kfree(q->pool);
1468 kfree(items);
1469}
1470EXPORT_SYMBOL_GPL(iscsi_pool_free);
1471
1472/*
1473 * iSCSI Session's hostdata organization:
1474 *
1475 * *------------------* <== hostdata_session(host->hostdata)
1476 * | ptr to class sess|
1477 * |------------------| <== iscsi_hostdata(host->hostdata)
1478 * | iscsi_session |
1479 * *------------------*
1480 */
1481
1482#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1483 _sz % sizeof(unsigned long))
1484
1485#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1486
1487/**
1488 * iscsi_session_setup - create iscsi cls session and host and session
1489 * @scsit: scsi transport template
1490 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001491 * @cmds_max: scsi host can queue
1492 * @qdepth: scsi host cmds per lun
1493 * @cmd_task_size: LLD ctask private data size
1494 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001495 * @initial_cmdsn: initial CmdSN
1496 * @hostno: host no allocated
1497 *
1498 * This can be used by software iscsi_transports that allocate
1499 * a session per scsi host.
1500 **/
1501struct iscsi_cls_session *
1502iscsi_session_setup(struct iscsi_transport *iscsit,
1503 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001504 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001505 int cmd_task_size, int mgmt_task_size,
1506 uint32_t initial_cmdsn, uint32_t *hostno)
1507{
1508 struct Scsi_Host *shost;
1509 struct iscsi_session *session;
1510 struct iscsi_cls_session *cls_session;
1511 int cmd_i;
1512
Mike Christie15482712007-05-30 12:57:19 -05001513 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1514 if (qdepth != 0)
1515 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1516 "Queue depth must be between 1 and %d.\n",
1517 qdepth, ISCSI_MAX_CMD_PER_LUN);
1518 qdepth = ISCSI_DEF_CMD_PER_LUN;
1519 }
1520
1521 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1522 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1523 if (cmds_max != 0)
1524 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1525 "can_queue must be a power of 2 and between "
1526 "2 and %d - setting to %d.\n", cmds_max,
1527 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1528 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1529 }
1530
Mike Christie7996a772006-04-06 21:13:41 -05001531 shost = scsi_host_alloc(iscsit->host_template,
1532 hostdata_privsize(sizeof(*session)));
1533 if (!shost)
1534 return NULL;
1535
Mike Christie15482712007-05-30 12:57:19 -05001536 /* the iscsi layer takes one task for reserve */
1537 shost->can_queue = cmds_max - 1;
1538 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001539 shost->max_id = 1;
1540 shost->max_channel = 0;
1541 shost->max_lun = iscsit->max_lun;
1542 shost->max_cmd_len = iscsit->max_cmd_len;
1543 shost->transportt = scsit;
1544 shost->transportt->create_work_queue = 1;
1545 *hostno = shost->host_no;
1546
1547 session = iscsi_hostdata(shost->hostdata);
1548 memset(session, 0, sizeof(struct iscsi_session));
1549 session->host = shost;
1550 session->state = ISCSI_STATE_FREE;
1551 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001552 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001553 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001554 session->exp_cmdsn = initial_cmdsn + 1;
1555 session->max_cmdsn = initial_cmdsn + 1;
1556 session->max_r2t = 1;
1557 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001558 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001559
1560 /* initialize SCSI PDU commands pool */
1561 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1562 (void***)&session->cmds,
1563 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1564 goto cmdpool_alloc_fail;
1565
1566 /* pre-format cmds pool with ITT */
1567 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1568 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1569
1570 if (cmd_task_size)
1571 ctask->dd_data = &ctask[1];
1572 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001573 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001574 }
1575
1576 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001577
1578 /* initialize immediate command pool */
1579 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1580 (void***)&session->mgmt_cmds,
1581 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1582 goto mgmtpool_alloc_fail;
1583
1584
1585 /* pre-format immediate cmds pool with ITT */
1586 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1587 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1588
1589 if (mgmt_task_size)
1590 mtask->dd_data = &mtask[1];
1591 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001592 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001593 }
1594
1595 if (scsi_add_host(shost, NULL))
1596 goto add_host_fail;
1597
Mike Christief53a88d2006-06-28 12:00:27 -05001598 if (!try_module_get(iscsit->owner))
1599 goto cls_session_fail;
1600
Mike Christie6a8a0d32006-06-28 12:00:31 -05001601 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001602 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001603 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001604 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1605
1606 return cls_session;
1607
Mike Christief53a88d2006-06-28 12:00:27 -05001608module_put:
1609 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001610cls_session_fail:
1611 scsi_remove_host(shost);
1612add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001613 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1614mgmtpool_alloc_fail:
1615 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1616cmdpool_alloc_fail:
1617 scsi_host_put(shost);
1618 return NULL;
1619}
1620EXPORT_SYMBOL_GPL(iscsi_session_setup);
1621
1622/**
1623 * iscsi_session_teardown - destroy session, host, and cls_session
1624 * shost: scsi host
1625 *
1626 * This can be used by software iscsi_transports that allocate
1627 * a session per scsi host.
1628 **/
1629void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1630{
1631 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1632 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001633 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001634
Mike Christie464bb992007-07-26 12:46:45 -05001635 iscsi_unblock_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001636 scsi_remove_host(shost);
1637
Mike Christie7996a772006-04-06 21:13:41 -05001638 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1639 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1640
Mike Christieb2c64162007-05-30 12:57:16 -05001641 kfree(session->password);
1642 kfree(session->password_in);
1643 kfree(session->username);
1644 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001645 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001646 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001647 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001648 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001649
Mike Christie7996a772006-04-06 21:13:41 -05001650 iscsi_destroy_session(cls_session);
1651 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001652 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001653}
1654EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1655
1656/**
1657 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1658 * @cls_session: iscsi_cls_session
1659 * @conn_idx: cid
1660 **/
1661struct iscsi_cls_conn *
1662iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1663{
1664 struct iscsi_session *session = class_to_transport_session(cls_session);
1665 struct iscsi_conn *conn;
1666 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001667 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001668
1669 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1670 if (!cls_conn)
1671 return NULL;
1672 conn = cls_conn->dd_data;
1673 memset(conn, 0, sizeof(*conn));
1674
1675 conn->session = session;
1676 conn->cls_conn = cls_conn;
1677 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1678 conn->id = conn_idx;
1679 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001680 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001681 INIT_LIST_HEAD(&conn->run_list);
1682 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06001683 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05001684 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06001685 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00001686 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001687
1688 /* allocate login_mtask used for the login/text sequences */
1689 spin_lock_bh(&session->lock);
1690 if (!__kfifo_get(session->mgmtpool.queue,
1691 (void*)&conn->login_mtask,
1692 sizeof(void*))) {
1693 spin_unlock_bh(&session->lock);
1694 goto login_mtask_alloc_fail;
1695 }
1696 spin_unlock_bh(&session->lock);
1697
Mike Christiebf32ed32007-02-28 17:32:17 -06001698 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001699 if (!data)
1700 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001701 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001702
Mike Christie843c0a82007-12-13 12:43:20 -06001703 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001704 init_waitqueue_head(&conn->ehwait);
1705
1706 return cls_conn;
1707
Mike Christied36ab6f2006-05-18 20:31:34 -05001708login_mtask_data_alloc_fail:
1709 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1710 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001711login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001712 iscsi_destroy_conn(cls_conn);
1713 return NULL;
1714}
1715EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1716
1717/**
1718 * iscsi_conn_teardown - teardown iscsi connection
1719 * cls_conn: iscsi class connection
1720 *
1721 * TODO: we may need to make this into a two step process
1722 * like scsi-mls remove + put host
1723 */
1724void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1725{
1726 struct iscsi_conn *conn = cls_conn->dd_data;
1727 struct iscsi_session *session = conn->session;
1728 unsigned long flags;
1729
Mike Christie7996a772006-04-06 21:13:41 -05001730 spin_lock_bh(&session->lock);
1731 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1732 if (session->leadconn == conn) {
1733 /*
1734 * leading connection? then give up on recovery.
1735 */
1736 session->state = ISCSI_STATE_TERMINATE;
1737 wake_up(&conn->ehwait);
1738 }
1739 spin_unlock_bh(&session->lock);
1740
Mike Christie7996a772006-04-06 21:13:41 -05001741 /*
1742 * Block until all in-progress commands for this connection
1743 * time out or fail.
1744 */
1745 for (;;) {
1746 spin_lock_irqsave(session->host->host_lock, flags);
1747 if (!session->host->host_busy) { /* OK for ERL == 0 */
1748 spin_unlock_irqrestore(session->host->host_lock, flags);
1749 break;
1750 }
1751 spin_unlock_irqrestore(session->host->host_lock, flags);
1752 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001753 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1754 "host_failed %d\n", session->host->host_busy,
1755 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001756 /*
1757 * force eh_abort() to unblock
1758 */
1759 wake_up(&conn->ehwait);
1760 }
1761
Mike Christie779ea122007-02-28 17:32:15 -06001762 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06001763 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06001764
Mike Christie7996a772006-04-06 21:13:41 -05001765 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001766 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001767 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001768 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1769 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05001770 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05001771 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001772 spin_unlock_bh(&session->lock);
1773
Mike Christie7996a772006-04-06 21:13:41 -05001774 iscsi_destroy_conn(cls_conn);
1775}
1776EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1777
1778int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1779{
1780 struct iscsi_conn *conn = cls_conn->dd_data;
1781 struct iscsi_session *session = conn->session;
1782
Mike Christieffd04362006-08-31 18:09:24 -04001783 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001784 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1785 return -EPERM;
1786 }
1787
Mike Christiedb98ccd2006-08-31 18:09:31 -04001788 if ((session->imm_data_en || !session->initial_r2t_en) &&
1789 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001790 printk("iscsi: invalid burst lengths: "
1791 "first_burst %d max_burst %d\n",
1792 session->first_burst, session->max_burst);
1793 return -EINVAL;
1794 }
1795
Mike Christie7996a772006-04-06 21:13:41 -05001796 spin_lock_bh(&session->lock);
1797 conn->c_stage = ISCSI_CONN_STARTED;
1798 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05001799 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001800
1801 switch(conn->stop_stage) {
1802 case STOP_CONN_RECOVER:
1803 /*
1804 * unblock eh_abort() if it is blocked. re-try all
1805 * commands after successful recovery
1806 */
Mike Christie7996a772006-04-06 21:13:41 -05001807 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001808 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001809 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001810 spin_unlock_bh(&session->lock);
1811
1812 iscsi_unblock_session(session_to_cls(session));
1813 wake_up(&conn->ehwait);
1814 return 0;
1815 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001816 conn->stop_stage = 0;
1817 break;
Mike Christie7996a772006-04-06 21:13:41 -05001818 default:
1819 break;
1820 }
1821 spin_unlock_bh(&session->lock);
1822
1823 return 0;
1824}
1825EXPORT_SYMBOL_GPL(iscsi_conn_start);
1826
1827static void
1828flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1829{
1830 struct iscsi_mgmt_task *mtask, *tmp;
1831
1832 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06001833 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
1834 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1835 list_del_init(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001836 if (mtask == conn->login_mtask)
1837 continue;
Mike Christie7996a772006-04-06 21:13:41 -05001838 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1839 sizeof(void*));
1840 }
1841
1842 /* handle running */
1843 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1844 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christie843c0a82007-12-13 12:43:20 -06001845 list_del_init(&mtask->running);
Mike Christieed2abc72006-05-02 19:46:40 -05001846
Mike Christie7996a772006-04-06 21:13:41 -05001847 if (mtask == conn->login_mtask)
1848 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001849 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001850 sizeof(void*));
1851 }
1852
1853 conn->mtask = NULL;
1854}
1855
Mike Christie656cffc2006-05-18 20:31:42 -05001856static void iscsi_start_session_recovery(struct iscsi_session *session,
1857 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001858{
Mike Christieed2abc72006-05-02 19:46:40 -05001859 int old_stop_stage;
1860
Mike Christie6724add2007-08-15 01:38:30 -05001861 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001862 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001863 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001864 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001865 mutex_unlock(&session->eh_mutex);
1866 return;
1867 }
1868
1869 /*
1870 * The LLD either freed/unset the lock on us, or userspace called
1871 * stop but did not create a proper connection (connection was never
1872 * bound or it was unbound then stop was called).
1873 */
1874 if (!conn->recv_lock) {
1875 spin_unlock_bh(&session->lock);
1876 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001877 return;
1878 }
Mike Christieed2abc72006-05-02 19:46:40 -05001879
1880 /*
1881 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001882 * up the login task and connection. We do not need to block and set
1883 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001884 */
Mike Christie67a61112006-05-30 00:37:20 -05001885 if (flag == STOP_CONN_TERM)
1886 session->state = ISCSI_STATE_TERMINATE;
1887 else if (conn->stop_stage != STOP_CONN_RECOVER)
1888 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001889
1890 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001891 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001892 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05001893 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001894
1895 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05001896
Mike Christie1c834692006-07-24 15:47:26 -05001897 write_lock_bh(conn->recv_lock);
1898 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1899 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001900
Mike Christie7996a772006-04-06 21:13:41 -05001901 /*
1902 * for connection level recovery we should not calculate
1903 * header digest. conn->hdr_size used for optimization
1904 * in hdr_extract() and will be re-negotiated at
1905 * set_param() time.
1906 */
1907 if (flag == STOP_CONN_RECOVER) {
1908 conn->hdrdgst_en = 0;
1909 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001910 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001911 old_stop_stage != STOP_CONN_RECOVER) {
1912 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001913 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001914 }
Mike Christie7996a772006-04-06 21:13:41 -05001915 }
Mike Christie656cffc2006-05-18 20:31:42 -05001916
Mike Christie656cffc2006-05-18 20:31:42 -05001917 /*
1918 * flush queues.
1919 */
1920 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001921 fail_all_commands(conn, -1);
Mike Christie656cffc2006-05-18 20:31:42 -05001922 flush_control_queues(session, conn);
1923 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001924 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001925}
Mike Christie7996a772006-04-06 21:13:41 -05001926
1927void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1928{
1929 struct iscsi_conn *conn = cls_conn->dd_data;
1930 struct iscsi_session *session = conn->session;
1931
1932 switch (flag) {
1933 case STOP_CONN_RECOVER:
1934 case STOP_CONN_TERM:
1935 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001936 break;
Mike Christie7996a772006-04-06 21:13:41 -05001937 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001938 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001939 }
1940}
1941EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1942
1943int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1944 struct iscsi_cls_conn *cls_conn, int is_leading)
1945{
1946 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001947 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001948
Mike Christie7996a772006-04-06 21:13:41 -05001949 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001950 if (is_leading)
1951 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001952 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001953
1954 /*
1955 * Unblock xmitworker(), Login Phase will pass through.
1956 */
1957 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1958 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1959 return 0;
1960}
1961EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1962
Mike Christiea54a52c2006-06-28 12:00:23 -05001963
1964int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1965 enum iscsi_param param, char *buf, int buflen)
1966{
1967 struct iscsi_conn *conn = cls_conn->dd_data;
1968 struct iscsi_session *session = conn->session;
1969 uint32_t value;
1970
1971 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06001972 case ISCSI_PARAM_FAST_ABORT:
1973 sscanf(buf, "%d", &session->fast_abort);
1974 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05001975 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1976 sscanf(buf, "%d", &conn->max_recv_dlength);
1977 break;
1978 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1979 sscanf(buf, "%d", &conn->max_xmit_dlength);
1980 break;
1981 case ISCSI_PARAM_HDRDGST_EN:
1982 sscanf(buf, "%d", &conn->hdrdgst_en);
1983 break;
1984 case ISCSI_PARAM_DATADGST_EN:
1985 sscanf(buf, "%d", &conn->datadgst_en);
1986 break;
1987 case ISCSI_PARAM_INITIAL_R2T_EN:
1988 sscanf(buf, "%d", &session->initial_r2t_en);
1989 break;
1990 case ISCSI_PARAM_MAX_R2T:
1991 sscanf(buf, "%d", &session->max_r2t);
1992 break;
1993 case ISCSI_PARAM_IMM_DATA_EN:
1994 sscanf(buf, "%d", &session->imm_data_en);
1995 break;
1996 case ISCSI_PARAM_FIRST_BURST:
1997 sscanf(buf, "%d", &session->first_burst);
1998 break;
1999 case ISCSI_PARAM_MAX_BURST:
2000 sscanf(buf, "%d", &session->max_burst);
2001 break;
2002 case ISCSI_PARAM_PDU_INORDER_EN:
2003 sscanf(buf, "%d", &session->pdu_inorder_en);
2004 break;
2005 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2006 sscanf(buf, "%d", &session->dataseq_inorder_en);
2007 break;
2008 case ISCSI_PARAM_ERL:
2009 sscanf(buf, "%d", &session->erl);
2010 break;
2011 case ISCSI_PARAM_IFMARKER_EN:
2012 sscanf(buf, "%d", &value);
2013 BUG_ON(value);
2014 break;
2015 case ISCSI_PARAM_OFMARKER_EN:
2016 sscanf(buf, "%d", &value);
2017 BUG_ON(value);
2018 break;
2019 case ISCSI_PARAM_EXP_STATSN:
2020 sscanf(buf, "%u", &conn->exp_statsn);
2021 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002022 case ISCSI_PARAM_USERNAME:
2023 kfree(session->username);
2024 session->username = kstrdup(buf, GFP_KERNEL);
2025 if (!session->username)
2026 return -ENOMEM;
2027 break;
2028 case ISCSI_PARAM_USERNAME_IN:
2029 kfree(session->username_in);
2030 session->username_in = kstrdup(buf, GFP_KERNEL);
2031 if (!session->username_in)
2032 return -ENOMEM;
2033 break;
2034 case ISCSI_PARAM_PASSWORD:
2035 kfree(session->password);
2036 session->password = kstrdup(buf, GFP_KERNEL);
2037 if (!session->password)
2038 return -ENOMEM;
2039 break;
2040 case ISCSI_PARAM_PASSWORD_IN:
2041 kfree(session->password_in);
2042 session->password_in = kstrdup(buf, GFP_KERNEL);
2043 if (!session->password_in)
2044 return -ENOMEM;
2045 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002046 case ISCSI_PARAM_TARGET_NAME:
2047 /* this should not change between logins */
2048 if (session->targetname)
2049 break;
2050
2051 session->targetname = kstrdup(buf, GFP_KERNEL);
2052 if (!session->targetname)
2053 return -ENOMEM;
2054 break;
2055 case ISCSI_PARAM_TPGT:
2056 sscanf(buf, "%d", &session->tpgt);
2057 break;
2058 case ISCSI_PARAM_PERSISTENT_PORT:
2059 sscanf(buf, "%d", &conn->persistent_port);
2060 break;
2061 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2062 /*
2063 * this is the address returned in discovery so it should
2064 * not change between logins.
2065 */
2066 if (conn->persistent_address)
2067 break;
2068
2069 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2070 if (!conn->persistent_address)
2071 return -ENOMEM;
2072 break;
2073 default:
2074 return -ENOSYS;
2075 }
2076
2077 return 0;
2078}
2079EXPORT_SYMBOL_GPL(iscsi_set_param);
2080
2081int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2082 enum iscsi_param param, char *buf)
2083{
2084 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2085 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2086 int len;
2087
2088 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002089 case ISCSI_PARAM_FAST_ABORT:
2090 len = sprintf(buf, "%d\n", session->fast_abort);
2091 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002092 case ISCSI_PARAM_INITIAL_R2T_EN:
2093 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2094 break;
2095 case ISCSI_PARAM_MAX_R2T:
2096 len = sprintf(buf, "%hu\n", session->max_r2t);
2097 break;
2098 case ISCSI_PARAM_IMM_DATA_EN:
2099 len = sprintf(buf, "%d\n", session->imm_data_en);
2100 break;
2101 case ISCSI_PARAM_FIRST_BURST:
2102 len = sprintf(buf, "%u\n", session->first_burst);
2103 break;
2104 case ISCSI_PARAM_MAX_BURST:
2105 len = sprintf(buf, "%u\n", session->max_burst);
2106 break;
2107 case ISCSI_PARAM_PDU_INORDER_EN:
2108 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2109 break;
2110 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2111 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2112 break;
2113 case ISCSI_PARAM_ERL:
2114 len = sprintf(buf, "%d\n", session->erl);
2115 break;
2116 case ISCSI_PARAM_TARGET_NAME:
2117 len = sprintf(buf, "%s\n", session->targetname);
2118 break;
2119 case ISCSI_PARAM_TPGT:
2120 len = sprintf(buf, "%d\n", session->tpgt);
2121 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002122 case ISCSI_PARAM_USERNAME:
2123 len = sprintf(buf, "%s\n", session->username);
2124 break;
2125 case ISCSI_PARAM_USERNAME_IN:
2126 len = sprintf(buf, "%s\n", session->username_in);
2127 break;
2128 case ISCSI_PARAM_PASSWORD:
2129 len = sprintf(buf, "%s\n", session->password);
2130 break;
2131 case ISCSI_PARAM_PASSWORD_IN:
2132 len = sprintf(buf, "%s\n", session->password_in);
2133 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002134 default:
2135 return -ENOSYS;
2136 }
2137
2138 return len;
2139}
2140EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2141
2142int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2143 enum iscsi_param param, char *buf)
2144{
2145 struct iscsi_conn *conn = cls_conn->dd_data;
2146 int len;
2147
2148 switch(param) {
2149 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2150 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2151 break;
2152 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2153 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2154 break;
2155 case ISCSI_PARAM_HDRDGST_EN:
2156 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2157 break;
2158 case ISCSI_PARAM_DATADGST_EN:
2159 len = sprintf(buf, "%d\n", conn->datadgst_en);
2160 break;
2161 case ISCSI_PARAM_IFMARKER_EN:
2162 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2163 break;
2164 case ISCSI_PARAM_OFMARKER_EN:
2165 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2166 break;
2167 case ISCSI_PARAM_EXP_STATSN:
2168 len = sprintf(buf, "%u\n", conn->exp_statsn);
2169 break;
2170 case ISCSI_PARAM_PERSISTENT_PORT:
2171 len = sprintf(buf, "%d\n", conn->persistent_port);
2172 break;
2173 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2174 len = sprintf(buf, "%s\n", conn->persistent_address);
2175 break;
2176 default:
2177 return -ENOSYS;
2178 }
2179
2180 return len;
2181}
2182EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2183
Mike Christie0801c242007-05-30 12:57:12 -05002184int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2185 char *buf)
2186{
2187 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2188 int len;
2189
2190 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002191 case ISCSI_HOST_PARAM_NETDEV_NAME:
2192 if (!session->netdev)
2193 len = sprintf(buf, "%s\n", "default");
2194 else
2195 len = sprintf(buf, "%s\n", session->netdev);
2196 break;
Mike Christie0801c242007-05-30 12:57:12 -05002197 case ISCSI_HOST_PARAM_HWADDRESS:
2198 if (!session->hwaddress)
2199 len = sprintf(buf, "%s\n", "default");
2200 else
2201 len = sprintf(buf, "%s\n", session->hwaddress);
2202 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002203 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2204 if (!session->initiatorname)
2205 len = sprintf(buf, "%s\n", "unknown");
2206 else
2207 len = sprintf(buf, "%s\n", session->initiatorname);
2208 break;
2209
Mike Christie0801c242007-05-30 12:57:12 -05002210 default:
2211 return -ENOSYS;
2212 }
2213
2214 return len;
2215}
2216EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2217
2218int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2219 char *buf, int buflen)
2220{
2221 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2222
2223 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002224 case ISCSI_HOST_PARAM_NETDEV_NAME:
2225 if (!session->netdev)
2226 session->netdev = kstrdup(buf, GFP_KERNEL);
2227 break;
Mike Christie0801c242007-05-30 12:57:12 -05002228 case ISCSI_HOST_PARAM_HWADDRESS:
2229 if (!session->hwaddress)
2230 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2231 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002232 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2233 if (!session->initiatorname)
2234 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2235 break;
Mike Christie0801c242007-05-30 12:57:12 -05002236 default:
2237 return -ENOSYS;
2238 }
2239
2240 return 0;
2241}
2242EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2243
Mike Christie7996a772006-04-06 21:13:41 -05002244MODULE_AUTHOR("Mike Christie");
2245MODULE_DESCRIPTION("iSCSI library functions");
2246MODULE_LICENSE("GPL");