blob: 79bc49fd7f12c05e622b3ec3c17f804e0e3077c5 [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>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Mike Christie77a23c22007-05-30 12:57:18 -050041/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050043
Mike Christie77a23c22007-05-30 12:57:18 -050044static int iscsi_sna_lt(u32 n1, u32 n2)
45{
46 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
47 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
48}
49
50/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
51static int iscsi_sna_lte(u32 n1, u32 n2)
52{
53 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55}
56
57void
58iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050059{
60 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
61 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063 /*
64 * standard specifies this check for when to update expected and
65 * max sequence numbers
66 */
67 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
68 return;
69
70 if (exp_cmdsn != session->exp_cmdsn &&
71 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050072 session->exp_cmdsn = exp_cmdsn;
73
Mike Christie77a23c22007-05-30 12:57:18 -050074 if (max_cmdsn != session->max_cmdsn &&
75 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
76 session->max_cmdsn = max_cmdsn;
77 /*
78 * if the window closed with IO queued, then kick the
79 * xmit thread
80 */
81 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie843c0a82007-12-13 12:43:20 -060082 !list_empty(&session->leadconn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -050083 scsi_queue_work(session->host,
84 &session->leadconn->xmitwork);
85 }
Mike Christie7996a772006-04-06 21:13:41 -050086}
Mike Christie77a23c22007-05-30 12:57:18 -050087EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050088
89void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040090 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050091{
92 struct iscsi_conn *conn = ctask->conn;
93
94 memset(hdr, 0, sizeof(struct iscsi_data));
95 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
96 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
97 ctask->unsol_datasn++;
98 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
99 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
100
101 hdr->itt = ctask->hdr->itt;
102 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400103 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500104
105 if (ctask->unsol_count > conn->max_xmit_dlength) {
106 hton24(hdr->dlength, conn->max_xmit_dlength);
107 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -0400108 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500109 hdr->flags = 0;
110 } else {
111 hton24(hdr->dlength, ctask->unsol_count);
112 ctask->data_count = ctask->unsol_count;
113 hdr->flags = ISCSI_FLAG_CMD_FINAL;
114 }
115}
116EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
117
Boaz Harrosh004d6532007-12-13 12:43:23 -0600118static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
119{
120 unsigned exp_len = ctask->hdr_len + len;
121
122 if (exp_len > ctask->hdr_max) {
123 WARN_ON(1);
124 return -EINVAL;
125 }
126
127 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
128 ctask->hdr_len = exp_len;
129 return 0;
130}
131
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500132/*
133 * make an extended cdb AHS
134 */
135static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
136{
137 struct scsi_cmnd *cmd = ctask->sc;
138 unsigned rlen, pad_len;
139 unsigned short ahslength;
140 struct iscsi_ecdb_ahdr *ecdb_ahdr;
141 int rc;
142
143 ecdb_ahdr = iscsi_next_hdr(ctask);
144 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
145
146 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
147 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
148
149 pad_len = iscsi_padding(rlen);
150
151 rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
152 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
153 if (rc)
154 return rc;
155
156 if (pad_len)
157 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
158
159 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
160 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
161 ecdb_ahdr->reserved = 0;
162 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
163
164 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
165 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
166 cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
167
168 return 0;
169}
170
Boaz Harroshc07d4442008-04-18 10:11:52 -0500171static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
172{
173 struct scsi_cmnd *sc = ctask->sc;
174 struct iscsi_rlength_ahdr *rlen_ahdr;
175 int rc;
176
177 rlen_ahdr = iscsi_next_hdr(ctask);
178 rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
179 if (rc)
180 return rc;
181
182 rlen_ahdr->ahslength =
183 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
184 sizeof(rlen_ahdr->reserved));
185 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
186 rlen_ahdr->reserved = 0;
187 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
188
189 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
190 "rlen_ahdr->ahslength(%d)\n",
191 be32_to_cpu(rlen_ahdr->read_length),
192 be16_to_cpu(rlen_ahdr->ahslength));
193 return 0;
194}
195
Mike Christie7996a772006-04-06 21:13:41 -0500196/**
197 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
198 * @ctask: iscsi cmd task
199 *
200 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
201 * fields like dlength or final based on how much data it sends
202 */
Boaz Harrosh004d6532007-12-13 12:43:23 -0600203static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500204{
205 struct iscsi_conn *conn = ctask->conn;
206 struct iscsi_session *session = conn->session;
207 struct iscsi_cmd *hdr = ctask->hdr;
208 struct scsi_cmnd *sc = ctask->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500209 unsigned hdrlength, cmd_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600210 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500211
Boaz Harrosh004d6532007-12-13 12:43:23 -0600212 ctask->hdr_len = 0;
213 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
214 if (rc)
215 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600216 hdr->opcode = ISCSI_OP_SCSI_CMD;
217 hdr->flags = ISCSI_ATTR_SIMPLE;
218 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie8b1d0342008-01-31 13:36:53 -0600219 hdr->itt = build_itt(ctask->itt, session->age);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600220 hdr->cmdsn = cpu_to_be32(session->cmdsn);
221 session->cmdsn++;
222 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500223 cmd_len = sc->cmd_len;
224 if (cmd_len < ISCSI_CDB_SIZE)
225 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
226 else if (cmd_len > ISCSI_CDB_SIZE) {
227 rc = iscsi_prep_ecdb_ahs(ctask);
228 if (rc)
229 return rc;
230 cmd_len = ISCSI_CDB_SIZE;
231 }
232 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500233
Mike Christie218432c2007-05-30 12:57:17 -0500234 ctask->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500235 if (scsi_bidi_cmnd(sc)) {
236 hdr->flags |= ISCSI_FLAG_CMD_READ;
237 rc = iscsi_prep_bidi_ahs(ctask);
238 if (rc)
239 return rc;
240 }
Mike Christie7996a772006-04-06 21:13:41 -0500241 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500242 unsigned out_len = scsi_out(sc)->length;
243 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500244 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
245 /*
246 * Write counters:
247 *
248 * imm_count bytes to be sent right after
249 * SCSI PDU Header
250 *
251 * unsol_count bytes(as Data-Out) to be sent
252 * without R2T ack right after
253 * immediate data
254 *
255 * r2t_data_count bytes to be sent via R2T ack's
256 *
257 * pad_count bytes to be sent as zero-padding
258 */
Mike Christie7996a772006-04-06 21:13:41 -0500259 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400260 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500261 ctask->unsol_datasn = 0;
262
263 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500264 if (out_len >= session->first_burst)
Mike Christie7996a772006-04-06 21:13:41 -0500265 ctask->imm_count = min(session->first_burst,
266 conn->max_xmit_dlength);
267 else
Boaz Harroshc07d4442008-04-18 10:11:52 -0500268 ctask->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500269 conn->max_xmit_dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600270 hton24(hdr->dlength, ctask->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500271 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600272 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500273
Mike Christieffd04362006-08-31 18:09:24 -0400274 if (!session->initial_r2t_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500275 ctask->unsol_count = min(session->first_burst, out_len)
276 - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400277 ctask->unsol_offset = ctask->imm_count;
278 }
279
Mike Christie7996a772006-04-06 21:13:41 -0500280 if (!ctask->unsol_count)
281 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600282 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500283 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500284 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
285 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500286 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500287
288 if (sc->sc_data_direction == DMA_FROM_DEVICE)
289 hdr->flags |= ISCSI_FLAG_CMD_READ;
290 }
291
Boaz Harrosh004d6532007-12-13 12:43:23 -0600292 /* calculate size of additional header segments (AHSs) */
293 hdrlength = ctask->hdr_len - sizeof(*hdr);
294
295 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
296 hdrlength /= ISCSI_PAD_LEN;
297
298 WARN_ON(hdrlength >= 256);
299 hdr->hlength = hdrlength & 0xFF;
300
Olaf Kircha8ac6312007-12-13 12:43:35 -0600301 if (conn->session->tt->init_cmd_task(conn->ctask))
302 return EIO;
Mike Christie77a23c22007-05-30 12:57:18 -0500303
Olaf Kircha8ac6312007-12-13 12:43:35 -0600304 conn->scsicmd_pdus_cnt++;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500305 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
306 "len %d bidi_len %d cmdsn %d win %d]\n",
307 scsi_bidi_cmnd(sc) ? "bidirectional" :
308 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
309 conn->id, sc, sc->cmnd[0], ctask->itt,
310 scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600311 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600312 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500313}
Mike Christie7996a772006-04-06 21:13:41 -0500314
315/**
316 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500317 * @ctask: iscsi cmd task
318 *
319 * Must be called with session lock.
320 * This function returns the scsi command to scsi-ml and returns
321 * the cmd task to the pool of available cmd tasks.
322 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400323static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500324{
Mike Christiec1635cb2007-12-13 12:43:33 -0600325 struct iscsi_conn *conn = ctask->conn;
326 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500327 struct scsi_cmnd *sc = ctask->sc;
328
Mike Christieb6c395e2006-07-24 15:47:15 -0500329 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500330 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400331 /* SCSI eh reuses commands to verify us */
332 sc->SCp.ptr = NULL;
Mike Christiec1635cb2007-12-13 12:43:33 -0600333 if (conn->ctask == ctask)
334 conn->ctask = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500335 list_del_init(&ctask->running);
336 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
337 sc->scsi_done(sc);
338}
339
Mike Christie60ecebf2006-08-31 18:09:25 -0400340static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
341{
342 atomic_inc(&ctask->refcount);
343}
344
Mike Christie60ecebf2006-08-31 18:09:25 -0400345static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
346{
Mike Christiee648f632006-08-31 18:09:34 -0400347 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400348 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400349}
350
Mike Christieb3a7ea82007-12-13 12:43:26 -0600351/*
352 * session lock must be held
353 */
354static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
355 int err)
356{
357 struct scsi_cmnd *sc;
358
359 sc = ctask->sc;
360 if (!sc)
361 return;
362
363 if (ctask->state == ISCSI_TASK_PENDING)
364 /*
365 * cmd never made it to the xmit thread, so we should not count
366 * the cmd in the sequencing
367 */
368 conn->session->queued_cmdsn--;
369 else
370 conn->session->tt->cleanup_cmd_task(conn, ctask);
371
372 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500373 if (!scsi_bidi_cmnd(sc))
374 scsi_set_resid(sc, scsi_bufflen(sc));
375 else {
376 scsi_out(sc)->resid = scsi_out(sc)->length;
377 scsi_in(sc)->resid = scsi_in(sc)->length;
378 }
Mike Christieb3a7ea82007-12-13 12:43:26 -0600379 if (conn->ctask == ctask)
380 conn->ctask = NULL;
381 /* release ref from queuecommand */
382 __iscsi_put_ctask(ctask);
383}
384
385/**
386 * iscsi_free_mgmt_task - return mgmt task back to pool
387 * @conn: iscsi connection
388 * @mtask: mtask
389 *
390 * Must be called with session lock.
391 */
392void iscsi_free_mgmt_task(struct iscsi_conn *conn,
393 struct iscsi_mgmt_task *mtask)
394{
395 list_del_init(&mtask->running);
396 if (conn->login_mtask == mtask)
397 return;
Mike Christief6d51802007-12-13 12:43:30 -0600398
399 if (conn->ping_mtask == mtask)
400 conn->ping_mtask = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600401 __kfifo_put(conn->session->mgmtpool.queue,
402 (void*)&mtask, sizeof(void*));
403}
404EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
405
Mike Christief6d51802007-12-13 12:43:30 -0600406static struct iscsi_mgmt_task *
407__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
408 char *data, uint32_t data_size)
409{
410 struct iscsi_session *session = conn->session;
411 struct iscsi_mgmt_task *mtask;
412
413 if (session->state == ISCSI_STATE_TERMINATE)
414 return NULL;
415
416 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
417 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
418 /*
419 * Login and Text are sent serially, in
420 * request-followed-by-response sequence.
421 * Same mtask can be used. Same ITT must be used.
422 * Note that login_mtask is preallocated at conn_create().
423 */
424 mtask = conn->login_mtask;
425 else {
426 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
427 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
428
429 if (!__kfifo_get(session->mgmtpool.queue,
430 (void*)&mtask, sizeof(void*)))
431 return NULL;
432 }
433
434 if (data_size) {
435 memcpy(mtask->data, data, data_size);
436 mtask->data_count = data_size;
437 } else
438 mtask->data_count = 0;
439
440 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
441 INIT_LIST_HEAD(&mtask->running);
442 list_add_tail(&mtask->running, &conn->mgmtqueue);
443 return mtask;
444}
445
446int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
447 char *data, uint32_t data_size)
448{
449 struct iscsi_conn *conn = cls_conn->dd_data;
450 struct iscsi_session *session = conn->session;
451 int err = 0;
452
453 spin_lock_bh(&session->lock);
454 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
455 err = -EPERM;
456 spin_unlock_bh(&session->lock);
457 scsi_queue_work(session->host, &conn->xmitwork);
458 return err;
459}
460EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
461
Mike Christie7996a772006-04-06 21:13:41 -0500462/**
463 * iscsi_cmd_rsp - SCSI Command Response processing
464 * @conn: iscsi connection
465 * @hdr: iscsi header
466 * @ctask: scsi command task
467 * @data: cmd data buffer
468 * @datalen: len of buffer
469 *
470 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
471 * then completes the command and task.
472 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500473static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
474 struct iscsi_cmd_task *ctask, char *data,
475 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500476{
Mike Christie7996a772006-04-06 21:13:41 -0500477 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
478 struct iscsi_session *session = conn->session;
479 struct scsi_cmnd *sc = ctask->sc;
480
Mike Christie77a23c22007-05-30 12:57:18 -0500481 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500482 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
483
484 sc->result = (DID_OK << 16) | rhdr->cmd_status;
485
486 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
487 sc->result = DID_ERROR << 16;
488 goto out;
489 }
490
491 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600492 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500493
494 if (datalen < 2) {
495invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600496 iscsi_conn_printk(KERN_ERR, conn,
497 "Got CHECK_CONDITION but invalid data "
498 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500499 sc->result = DID_BAD_TARGET << 16;
500 goto out;
501 }
502
Mike Christie8eb00532007-02-28 17:32:19 -0600503 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500504 if (datalen < senselen)
505 goto invalid_datalen;
506
507 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600508 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500509 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600510 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500511 }
512
Boaz Harroshc07d4442008-04-18 10:11:52 -0500513 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
514 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
515 int res_count = be32_to_cpu(rhdr->bi_residual_count);
516
517 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
518 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
519 res_count <= scsi_in(sc)->length))
520 scsi_in(sc)->resid = res_count;
521 else
522 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
523 }
524
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600525 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
526 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500527 int res_count = be32_to_cpu(rhdr->residual_count);
528
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600529 if (res_count > 0 &&
530 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
531 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500532 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900533 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500534 else
535 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500536 }
Mike Christie7996a772006-04-06 21:13:41 -0500537out:
538 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
539 (long)sc, sc->result, ctask->itt);
540 conn->scsirsp_pdus_cnt++;
541
Mike Christie60ecebf2006-08-31 18:09:25 -0400542 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500543}
544
Mike Christie7ea8b822006-07-24 15:47:22 -0500545static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
546{
547 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
548
549 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
550 conn->tmfrsp_pdus_cnt++;
551
Mike Christie843c0a82007-12-13 12:43:20 -0600552 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500553 return;
554
555 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600556 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500557 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600558 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500559 else
Mike Christie843c0a82007-12-13 12:43:20 -0600560 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500561 wake_up(&conn->ehwait);
562}
563
Mike Christief6d51802007-12-13 12:43:30 -0600564static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
565{
566 struct iscsi_nopout hdr;
567 struct iscsi_mgmt_task *mtask;
568
569 if (!rhdr && conn->ping_mtask)
570 return;
571
572 memset(&hdr, 0, sizeof(struct iscsi_nopout));
573 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
574 hdr.flags = ISCSI_FLAG_CMD_FINAL;
575
576 if (rhdr) {
577 memcpy(hdr.lun, rhdr->lun, 8);
578 hdr.ttt = rhdr->ttt;
579 hdr.itt = RESERVED_ITT;
580 } else
581 hdr.ttt = RESERVED_ITT;
582
583 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
584 if (!mtask) {
Mike Christie322d7392008-01-31 13:36:52 -0600585 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600586 return;
587 }
588
589 /* only track our nops */
590 if (!rhdr) {
591 conn->ping_mtask = mtask;
592 conn->last_ping = jiffies;
593 }
594 scsi_queue_work(conn->session->host, &conn->xmitwork);
595}
596
Mike Christie62f38302006-08-31 18:09:27 -0400597static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
598 char *data, int datalen)
599{
600 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
601 struct iscsi_hdr rejected_pdu;
602 uint32_t itt;
603
604 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
605
606 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
607 if (ntoh24(reject->dlength) > datalen)
608 return ISCSI_ERR_PROTO;
609
610 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
611 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000612 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600613 iscsi_conn_printk(KERN_ERR, conn,
614 "itt 0x%x had pdu (op 0x%x) rejected "
615 "due to DataDigest error.\n", itt,
616 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400617 }
618 }
619 return 0;
620}
621
Mike Christie7996a772006-04-06 21:13:41 -0500622/**
623 * __iscsi_complete_pdu - complete pdu
624 * @conn: iscsi conn
625 * @hdr: iscsi header
626 * @data: data buffer
627 * @datalen: len of data buffer
628 *
629 * Completes pdu processing by freeing any resources allocated at
630 * queuecommand or send generic. session lock must be held and verify
631 * itt must have been called.
632 */
Adrian Bunkf8d9d652008-01-29 00:11:27 +0200633static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
634 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500635{
636 struct iscsi_session *session = conn->session;
637 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
638 struct iscsi_cmd_task *ctask;
639 struct iscsi_mgmt_task *mtask;
640 uint32_t itt;
641
Mike Christief6d51802007-12-13 12:43:30 -0600642 conn->last_recv = jiffies;
Al Virob4377352007-02-09 16:39:40 +0000643 if (hdr->itt != RESERVED_ITT)
644 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500645 else
Al Virob4377352007-02-09 16:39:40 +0000646 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500647
648 if (itt < session->cmds_max) {
649 ctask = session->cmds[itt];
650
651 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
652 opcode, conn->id, ctask->itt, datalen);
653
654 switch(opcode) {
655 case ISCSI_OP_SCSI_CMD_RSP:
656 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500657 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
658 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500659 break;
660 case ISCSI_OP_SCSI_DATA_IN:
661 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
662 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
663 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400664 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500665 }
666 break;
667 case ISCSI_OP_R2T:
668 /* LLD handles this for now */
669 break;
670 default:
671 rc = ISCSI_ERR_BAD_OPCODE;
672 break;
673 }
674 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
675 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
676 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
677
678 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
679 opcode, conn->id, mtask->itt, datalen);
680
Mike Christie77a23c22007-05-30 12:57:18 -0500681 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500682 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500683 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500684 if (datalen) {
685 rc = ISCSI_ERR_PROTO;
686 break;
687 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500688 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
689 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500690 case ISCSI_OP_LOGIN_RSP:
691 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500692 /*
693 * login related PDU's exp_statsn is handled in
694 * userspace
695 */
Mike Christie40527af2006-07-24 15:47:45 -0500696 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
697 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600698 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500699 break;
700 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500701 if (datalen) {
702 rc = ISCSI_ERR_PROTO;
703 break;
704 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500705
Mike Christie7ea8b822006-07-24 15:47:22 -0500706 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600707 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500708 break;
709 case ISCSI_OP_NOOP_IN:
Mike Christief6d51802007-12-13 12:43:30 -0600710 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
711 datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500712 rc = ISCSI_ERR_PROTO;
713 break;
714 }
Mike Christie7996a772006-04-06 21:13:41 -0500715 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
716
Mike Christief6d51802007-12-13 12:43:30 -0600717 if (conn->ping_mtask != mtask) {
718 /*
719 * If this is not in response to one of our
720 * nops then it must be from userspace.
721 */
722 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
723 datalen))
724 rc = ISCSI_ERR_CONN_FAILED;
Mike Christiec8611f92008-05-08 20:15:34 -0500725 } else
726 mod_timer(&conn->transport_timer,
727 jiffies + conn->recv_timeout);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600728 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500729 break;
730 default:
731 rc = ISCSI_ERR_BAD_OPCODE;
732 break;
733 }
Al Virob4377352007-02-09 16:39:40 +0000734 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500735 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400736
Mike Christie7996a772006-04-06 21:13:41 -0500737 switch(opcode) {
738 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500739 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500740 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500741 break;
742 }
743
Al Virob4377352007-02-09 16:39:40 +0000744 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500745 break;
746
Mike Christief6d51802007-12-13 12:43:30 -0600747 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500748 break;
749 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400750 rc = iscsi_handle_reject(conn, hdr, data, datalen);
751 break;
Mike Christie7996a772006-04-06 21:13:41 -0500752 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500753 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400754 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
755 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500756 break;
757 default:
758 rc = ISCSI_ERR_BAD_OPCODE;
759 break;
760 }
761 } else
762 rc = ISCSI_ERR_BAD_ITT;
763
764 return rc;
765}
Mike Christie7996a772006-04-06 21:13:41 -0500766
767int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
768 char *data, int datalen)
769{
770 int rc;
771
772 spin_lock(&conn->session->lock);
773 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
774 spin_unlock(&conn->session->lock);
775 return rc;
776}
777EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
778
779/* verify itt (itt encoding: age+cid+itt) */
780int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
781 uint32_t *ret_itt)
782{
783 struct iscsi_session *session = conn->session;
784 struct iscsi_cmd_task *ctask;
785 uint32_t itt;
786
Al Virob4377352007-02-09 16:39:40 +0000787 if (hdr->itt != RESERVED_ITT) {
788 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500789 (session->age << ISCSI_AGE_SHIFT)) {
Mike Christie322d7392008-01-31 13:36:52 -0600790 iscsi_conn_printk(KERN_ERR, conn,
791 "received itt %x expected session "
792 "age (%x)\n", (__force u32)hdr->itt,
793 session->age & ISCSI_AGE_MASK);
Mike Christie7996a772006-04-06 21:13:41 -0500794 return ISCSI_ERR_BAD_ITT;
795 }
796
Al Virob4377352007-02-09 16:39:40 +0000797 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500798 } else
Al Virob4377352007-02-09 16:39:40 +0000799 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500800
801 if (itt < session->cmds_max) {
802 ctask = session->cmds[itt];
803
804 if (!ctask->sc) {
Mike Christie322d7392008-01-31 13:36:52 -0600805 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
806 "with itt 0x%x\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500807 /* force drop */
808 return ISCSI_ERR_NO_SCSI_CMD;
809 }
810
811 if (ctask->sc->SCp.phase != session->age) {
Mike Christie322d7392008-01-31 13:36:52 -0600812 iscsi_conn_printk(KERN_ERR, conn,
813 "iscsi: ctask's session age %d, "
814 "expected %d\n", ctask->sc->SCp.phase,
815 session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500816 return ISCSI_ERR_SESSION_FAILED;
817 }
818 }
819
820 *ret_itt = itt;
821 return 0;
822}
823EXPORT_SYMBOL_GPL(iscsi_verify_itt);
824
825void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
826{
827 struct iscsi_session *session = conn->session;
828 unsigned long flags;
829
830 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500831 if (session->state == ISCSI_STATE_FAILED) {
832 spin_unlock_irqrestore(&session->lock, flags);
833 return;
834 }
835
Mike Christie67a61112006-05-30 00:37:20 -0500836 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500837 session->state = ISCSI_STATE_FAILED;
838 spin_unlock_irqrestore(&session->lock, flags);
839 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
840 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
841 iscsi_conn_error(conn->cls_conn, err);
842}
843EXPORT_SYMBOL_GPL(iscsi_conn_failure);
844
Mike Christie77a23c22007-05-30 12:57:18 -0500845static void iscsi_prep_mtask(struct iscsi_conn *conn,
846 struct iscsi_mgmt_task *mtask)
847{
848 struct iscsi_session *session = conn->session;
849 struct iscsi_hdr *hdr = mtask->hdr;
850 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
851
852 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
853 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
854 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
855 /*
856 * pre-format CmdSN for outgoing PDU.
857 */
858 nop->cmdsn = cpu_to_be32(session->cmdsn);
859 if (hdr->itt != RESERVED_ITT) {
Mike Christie8b1d0342008-01-31 13:36:53 -0600860 hdr->itt = build_itt(mtask->itt, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500861 /*
862 * TODO: We always use immediate, so we never hit this.
863 * If we start to send tmfs or nops as non-immediate then
864 * we should start checking the cmdsn numbers for mgmt tasks.
865 */
Mike Christie77a23c22007-05-30 12:57:18 -0500866 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500867 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
868 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500869 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500870 }
Mike Christie77a23c22007-05-30 12:57:18 -0500871 }
872
873 if (session->tt->init_mgmt_task)
874 session->tt->init_mgmt_task(conn, mtask);
875
876 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
Mike Christie843c0a82007-12-13 12:43:20 -0600877 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
878 mtask->data_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500879}
880
Mike Christie05db8882007-02-28 17:32:16 -0600881static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400882{
883 struct iscsi_hdr *hdr = conn->mtask->hdr;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600884 int rc;
Mike Christieb5072ea2006-10-16 18:09:42 -0400885
Mike Christieb3a7ea82007-12-13 12:43:26 -0600886 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
887 conn->session->state = ISCSI_STATE_LOGGING_OUT;
Mike Christie77a23c22007-05-30 12:57:18 -0500888 spin_unlock_bh(&conn->session->lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600889
Mike Christieb5072ea2006-10-16 18:09:42 -0400890 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500891 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400892 if (rc)
893 return rc;
894
Mike Christie05db8882007-02-28 17:32:16 -0600895 /* done with this in-progress mtask */
896 conn->mtask = NULL;
Mike Christieb5072ea2006-10-16 18:09:42 -0400897 return 0;
898}
899
Mike Christie77a23c22007-05-30 12:57:18 -0500900static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
901{
902 struct iscsi_session *session = conn->session;
903
904 /*
905 * Check for iSCSI window and take care of CmdSN wrap-around
906 */
Mike Christiee0726402007-07-26 12:46:48 -0500907 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
908 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
909 "CmdSN %u/%u\n", session->exp_cmdsn,
910 session->max_cmdsn, session->cmdsn,
911 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500912 return -ENOSPC;
913 }
914 return 0;
915}
916
917static int iscsi_xmit_ctask(struct iscsi_conn *conn)
918{
919 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600920 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500921
922 __iscsi_get_ctask(ctask);
923 spin_unlock_bh(&conn->session->lock);
924 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
925 spin_lock_bh(&conn->session->lock);
926 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500927 if (!rc)
928 /* done with this ctask */
929 conn->ctask = NULL;
930 return rc;
931}
932
Mike Christie7996a772006-04-06 21:13:41 -0500933/**
Mike Christie843c0a82007-12-13 12:43:20 -0600934 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
935 * @ctask: ctask to requeue
936 *
937 * LLDs that need to run a ctask from the session workqueue should call
938 * this. The session lock must be held.
939 */
940void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
941{
942 struct iscsi_conn *conn = ctask->conn;
943
944 list_move_tail(&ctask->running, &conn->requeue);
945 scsi_queue_work(conn->session->host, &conn->xmitwork);
946}
947EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
948
949/**
Mike Christie7996a772006-04-06 21:13:41 -0500950 * iscsi_data_xmit - xmit any command into the scheduled connection
951 * @conn: iscsi connection
952 *
953 * Notes:
954 * The function can return -EAGAIN in which case the caller must
955 * re-schedule it again later or recover. '0' return code means
956 * successful xmit.
957 **/
958static int iscsi_data_xmit(struct iscsi_conn *conn)
959{
Mike Christie3219e522006-05-30 00:37:28 -0500960 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500961
Mike Christie77a23c22007-05-30 12:57:18 -0500962 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500963 if (unlikely(conn->suspend_tx)) {
964 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500965 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500966 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500967 }
Mike Christie7996a772006-04-06 21:13:41 -0500968
969 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500970 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500971 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500972 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500973 }
Mike Christie77a23c22007-05-30 12:57:18 -0500974
Mike Christie7996a772006-04-06 21:13:41 -0500975 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600976 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500977 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500978 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500979 }
980
Mike Christie77a23c22007-05-30 12:57:18 -0500981 /*
982 * process mgmt pdus like nops before commands since we should
983 * only have one nop-out as a ping from us and targets should not
984 * overflow us with nop-ins
985 */
986check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -0600987 while (!list_empty(&conn->mgmtqueue)) {
988 conn->mtask = list_entry(conn->mgmtqueue.next,
989 struct iscsi_mgmt_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600990 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
991 iscsi_free_mgmt_task(conn, conn->mtask);
992 conn->mtask = NULL;
993 continue;
994 }
995
Mike Christie77a23c22007-05-30 12:57:18 -0500996 iscsi_prep_mtask(conn, conn->mtask);
Mike Christie843c0a82007-12-13 12:43:20 -0600997 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500998 rc = iscsi_xmit_mtask(conn);
999 if (rc)
1000 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001001 }
1002
Mike Christie843c0a82007-12-13 12:43:20 -06001003 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001004 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001005 if (conn->tmf_state == TMF_QUEUED)
1006 break;
1007
Mike Christieb6c395e2006-07-24 15:47:15 -05001008 conn->ctask = list_entry(conn->xmitqueue.next,
1009 struct iscsi_cmd_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001010 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9000bcd2007-12-13 12:43:32 -06001011 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001012 continue;
1013 }
Boaz Harrosh004d6532007-12-13 12:43:23 -06001014 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1015 fail_command(conn, conn->ctask, DID_ABORT << 16);
1016 continue;
1017 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001018
Mike Christie843c0a82007-12-13 12:43:20 -06001019 conn->ctask->state = ISCSI_TASK_RUNNING;
Mike Christieb6c395e2006-07-24 15:47:15 -05001020 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -05001021 rc = iscsi_xmit_ctask(conn);
1022 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001023 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001024 /*
1025 * we could continuously get new ctask requests so
1026 * we need to check the mgmt queue for nops that need to
1027 * be sent to aviod starvation
1028 */
Mike Christie843c0a82007-12-13 12:43:20 -06001029 if (!list_empty(&conn->mgmtqueue))
1030 goto check_mgmt;
1031 }
1032
1033 while (!list_empty(&conn->requeue)) {
1034 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1035 break;
1036
Mike Christieb3a7ea82007-12-13 12:43:26 -06001037 /*
1038 * we always do fastlogout - conn stop code will clean up.
1039 */
1040 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1041 break;
1042
Mike Christie843c0a82007-12-13 12:43:20 -06001043 conn->ctask = list_entry(conn->requeue.next,
1044 struct iscsi_cmd_task, running);
1045 conn->ctask->state = ISCSI_TASK_RUNNING;
1046 list_move_tail(conn->requeue.next, &conn->run_list);
1047 rc = iscsi_xmit_ctask(conn);
1048 if (rc)
1049 goto again;
1050 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001051 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001052 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001053 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001054 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001055
1056again:
1057 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001058 rc = -ENODATA;
1059 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001060 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001061}
1062
David Howellsc4028952006-11-22 14:57:56 +00001063static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001064{
David Howellsc4028952006-11-22 14:57:56 +00001065 struct iscsi_conn *conn =
1066 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001067 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001068 /*
1069 * serialize Xmit worker on a per-connection basis.
1070 */
Mike Christie3219e522006-05-30 00:37:28 -05001071 do {
1072 rc = iscsi_data_xmit(conn);
1073 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001074}
1075
1076enum {
1077 FAILURE_BAD_HOST = 1,
1078 FAILURE_SESSION_FAILED,
1079 FAILURE_SESSION_FREED,
1080 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001081 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001082 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001083 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001084 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001085 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001086 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001087};
1088
1089int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1090{
Mike Christie75613522008-05-21 15:53:59 -05001091 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001092 struct Scsi_Host *host;
1093 int reason = 0;
1094 struct iscsi_session *session;
1095 struct iscsi_conn *conn;
1096 struct iscsi_cmd_task *ctask = NULL;
1097
1098 sc->scsi_done = done;
1099 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001100 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001101
1102 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001103 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001104
Mike Christie75613522008-05-21 15:53:59 -05001105 cls_session = starget_to_session(scsi_target(sc->device));
1106 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001107 spin_lock(&session->lock);
1108
Mike Christie75613522008-05-21 15:53:59 -05001109 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001110 if (reason) {
1111 sc->result = reason;
1112 goto fault;
1113 }
1114
Mike Christie656cffc2006-05-18 20:31:42 -05001115 /*
1116 * ISCSI_STATE_FAILED is a temp. state. The recovery
1117 * code will decide what is best to do with command queued
1118 * during this time
1119 */
1120 if (session->state != ISCSI_STATE_LOGGED_IN &&
1121 session->state != ISCSI_STATE_FAILED) {
1122 /*
1123 * to handle the race between when we set the recovery state
1124 * and block the session we requeue here (commands could
1125 * be entering our queuecommand while a block is starting
1126 * up because the block code is not locked)
1127 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001128 switch (session->state) {
1129 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001130 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001131 sc->result = DID_IMM_RETRY << 16;
1132 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001133 case ISCSI_STATE_LOGGING_OUT:
1134 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001135 sc->result = DID_IMM_RETRY << 16;
1136 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001137 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001138 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001139 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001140 break;
1141 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001142 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001143 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001144 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001145 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001146 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001147 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001148 }
Mike Christie7996a772006-04-06 21:13:41 -05001149 goto fault;
1150 }
1151
Mike Christie7996a772006-04-06 21:13:41 -05001152 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001153 if (!conn) {
1154 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001155 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001156 goto fault;
1157 }
Mike Christie7996a772006-04-06 21:13:41 -05001158
Mike Christie77a23c22007-05-30 12:57:18 -05001159 if (iscsi_check_cmdsn_window_closed(conn)) {
1160 reason = FAILURE_WINDOW_CLOSED;
1161 goto reject;
1162 }
1163
Mike Christie60ecebf2006-08-31 18:09:25 -04001164 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1165 sizeof(void*))) {
1166 reason = FAILURE_OOM;
1167 goto reject;
1168 }
Mike Christiee0726402007-07-26 12:46:48 -05001169 session->queued_cmdsn++;
1170
Mike Christie7996a772006-04-06 21:13:41 -05001171 sc->SCp.phase = session->age;
1172 sc->SCp.ptr = (char *)ctask;
1173
Mike Christie60ecebf2006-08-31 18:09:25 -04001174 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -05001175 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -05001176 ctask->conn = conn;
1177 ctask->sc = sc;
1178 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001179
Mike Christieb6c395e2006-07-24 15:47:15 -05001180 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001181 spin_unlock(&session->lock);
1182
1183 scsi_queue_work(host, &conn->xmitwork);
Mike Christie1040c992007-12-13 12:43:34 -06001184 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001185 return 0;
1186
1187reject:
1188 spin_unlock(&session->lock);
1189 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001190 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001191 return SCSI_MLQUEUE_HOST_BUSY;
1192
1193fault:
1194 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001195 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001196 if (!scsi_bidi_cmnd(sc))
1197 scsi_set_resid(sc, scsi_bufflen(sc));
1198 else {
1199 scsi_out(sc)->resid = scsi_out(sc)->length;
1200 scsi_in(sc)->resid = scsi_in(sc)->length;
1201 }
Mike Christie7996a772006-04-06 21:13:41 -05001202 sc->scsi_done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001203 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001204 return 0;
1205}
1206EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1207
1208int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1209{
1210 if (depth > ISCSI_MAX_CMD_PER_LUN)
1211 depth = ISCSI_MAX_CMD_PER_LUN;
1212 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1213 return sdev->queue_depth;
1214}
1215EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1216
Mike Christie7996a772006-04-06 21:13:41 -05001217void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1218{
Mike Christie75613522008-05-21 15:53:59 -05001219 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001220
1221 spin_lock_bh(&session->lock);
1222 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001223 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001224 if (session->leadconn)
1225 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001226 }
1227 spin_unlock_bh(&session->lock);
1228}
1229EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1230
1231int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1232{
Mike Christie75613522008-05-21 15:53:59 -05001233 struct iscsi_cls_session *cls_session;
1234 struct iscsi_session *session;
1235 struct iscsi_conn *conn;
1236
1237 cls_session = starget_to_session(scsi_target(sc->device));
1238 session = cls_session->dd_data;
1239 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001240
Mike Christiebc436b22007-12-13 12:43:28 -06001241 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001242 spin_lock_bh(&session->lock);
1243 if (session->state == ISCSI_STATE_TERMINATE) {
1244failed:
1245 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001246 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001247 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001248 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001249 return FAILED;
1250 }
1251
Mike Christie7996a772006-04-06 21:13:41 -05001252 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001253 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001254 /*
1255 * we drop the lock here but the leadconn cannot be destoyed while
1256 * we are in the scsi eh
1257 */
Mike Christie843c0a82007-12-13 12:43:20 -06001258 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001259
1260 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1261 wait_event_interruptible(conn->ehwait,
1262 session->state == ISCSI_STATE_TERMINATE ||
1263 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001264 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001265 if (signal_pending(current))
1266 flush_signals(current);
1267
Mike Christiebc436b22007-12-13 12:43:28 -06001268 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001269 spin_lock_bh(&session->lock);
1270 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001271 iscsi_session_printk(KERN_INFO, session,
1272 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001273 else
1274 goto failed;
1275 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001276 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001277 return SUCCESS;
1278}
1279EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1280
Mike Christie843c0a82007-12-13 12:43:20 -06001281static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001282{
Mike Christie843c0a82007-12-13 12:43:20 -06001283 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001284 struct iscsi_session *session = conn->session;
1285
1286 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001287 if (conn->tmf_state == TMF_QUEUED) {
1288 conn->tmf_state = TMF_TIMEDOUT;
1289 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001290 /* unblock eh_abort() */
1291 wake_up(&conn->ehwait);
1292 }
1293 spin_unlock(&session->lock);
1294}
1295
Mike Christie843c0a82007-12-13 12:43:20 -06001296static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001297 struct iscsi_tm *hdr, int age,
1298 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001299{
Mike Christie7996a772006-04-06 21:13:41 -05001300 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001301 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001302
Mike Christie843c0a82007-12-13 12:43:20 -06001303 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1304 NULL, 0);
1305 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001306 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001307 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001308 spin_lock_bh(&session->lock);
1309 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001310 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001311 }
Mike Christie843c0a82007-12-13 12:43:20 -06001312 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001313 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001314 conn->tmf_timer.function = iscsi_tmf_timedout;
1315 conn->tmf_timer.data = (unsigned long)conn;
1316 add_timer(&conn->tmf_timer);
1317 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001318
Mike Christie7996a772006-04-06 21:13:41 -05001319 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001320 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001321 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001322
1323 /*
1324 * block eh thread until:
1325 *
Mike Christie843c0a82007-12-13 12:43:20 -06001326 * 1) tmf response
1327 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001328 * 3) session is terminated or restarted or userspace has
1329 * given up on recovery
1330 */
Mike Christie843c0a82007-12-13 12:43:20 -06001331 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001332 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001333 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001334 if (signal_pending(current))
1335 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001336 del_timer_sync(&conn->tmf_timer);
1337
Mike Christie6724add2007-08-15 01:38:30 -05001338 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001339 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001340 /* if the session drops it will clean up the mtask */
1341 if (age != session->age ||
1342 session->state != ISCSI_STATE_LOGGED_IN)
1343 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001344 return 0;
1345}
1346
1347/*
Mike Christie843c0a82007-12-13 12:43:20 -06001348 * Fail commands. session lock held and recv side suspended and xmit
1349 * thread flushed
1350 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001351static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1352 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001353{
1354 struct iscsi_cmd_task *ctask, *tmp;
1355
1356 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1357 conn->ctask = NULL;
1358
1359 /* flush pending */
1360 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1361 if (lun == ctask->sc->device->lun || lun == -1) {
1362 debug_scsi("failing pending sc %p itt 0x%x\n",
1363 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001364 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001365 }
1366 }
1367
1368 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1369 if (lun == ctask->sc->device->lun || lun == -1) {
1370 debug_scsi("failing requeued sc %p itt 0x%x\n",
1371 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001372 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001373 }
1374 }
1375
1376 /* fail all other running */
1377 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1378 if (lun == ctask->sc->device->lun || lun == -1) {
1379 debug_scsi("failing in progress sc %p itt 0x%x\n",
1380 ctask->sc, ctask->itt);
1381 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1382 }
1383 }
1384}
1385
Mike Christieb40977d2008-05-21 15:54:03 -05001386void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001387{
1388 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1389 scsi_flush_work(conn->session->host);
1390}
Mike Christieb40977d2008-05-21 15:54:03 -05001391EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001392
1393static void iscsi_start_tx(struct iscsi_conn *conn)
1394{
1395 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1396 scsi_queue_work(conn->session->host, &conn->xmitwork);
1397}
1398
Mike Christief6d51802007-12-13 12:43:30 -06001399static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1400{
1401 struct iscsi_cls_session *cls_session;
1402 struct iscsi_session *session;
1403 struct iscsi_conn *conn;
1404 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1405
1406 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001407 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001408
1409 debug_scsi("scsi cmd %p timedout\n", scmd);
1410
1411 spin_lock(&session->lock);
1412 if (session->state != ISCSI_STATE_LOGGED_IN) {
1413 /*
1414 * We are probably in the middle of iscsi recovery so let
1415 * that complete and handle the error.
1416 */
1417 rc = EH_RESET_TIMER;
1418 goto done;
1419 }
1420
1421 conn = session->leadconn;
1422 if (!conn) {
1423 /* In the middle of shuting down */
1424 rc = EH_RESET_TIMER;
1425 goto done;
1426 }
1427
1428 if (!conn->recv_timeout && !conn->ping_timeout)
1429 goto done;
1430 /*
1431 * if the ping timedout then we are in the middle of cleaning up
1432 * and can let the iscsi eh handle it
1433 */
1434 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1435 (conn->ping_timeout * HZ), jiffies))
1436 rc = EH_RESET_TIMER;
1437 /*
1438 * if we are about to check the transport then give the command
1439 * more time
1440 */
1441 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1442 jiffies))
1443 rc = EH_RESET_TIMER;
1444 /* if in the middle of checking the transport then give us more time */
1445 if (conn->ping_mtask)
1446 rc = EH_RESET_TIMER;
1447done:
1448 spin_unlock(&session->lock);
1449 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1450 return rc;
1451}
1452
1453static void iscsi_check_transport_timeouts(unsigned long data)
1454{
1455 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1456 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001457 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001458
1459 spin_lock(&session->lock);
1460 if (session->state != ISCSI_STATE_LOGGED_IN)
1461 goto done;
1462
Mike Christie4cf10432008-05-07 20:43:52 -05001463 recv_timeout = conn->recv_timeout;
1464 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001465 goto done;
1466
Mike Christie4cf10432008-05-07 20:43:52 -05001467 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001468 last_recv = conn->last_recv;
Mike Christie4cf10432008-05-07 20:43:52 -05001469 if (conn->ping_mtask &&
1470 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001471 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001472 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1473 "expired, last rx %lu, last ping %lu, "
1474 "now %lu\n", conn->ping_timeout, last_recv,
1475 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001476 spin_unlock(&session->lock);
1477 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1478 return;
1479 }
1480
Mike Christie4cf10432008-05-07 20:43:52 -05001481 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001482 /* send a ping to try to provoke some traffic */
1483 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1484 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001485 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001486 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001487 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001488
Mike Christiead294e92008-01-31 13:36:50 -06001489 debug_scsi("Setting next tmo %lu\n", next_timeout);
1490 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001491done:
1492 spin_unlock(&session->lock);
1493}
1494
Mike Christie843c0a82007-12-13 12:43:20 -06001495static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1496 struct iscsi_tm *hdr)
1497{
1498 memset(hdr, 0, sizeof(*hdr));
1499 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1500 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1501 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1502 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1503 hdr->rtt = ctask->hdr->itt;
1504 hdr->refcmdsn = ctask->hdr->cmdsn;
1505}
1506
Mike Christie7996a772006-04-06 21:13:41 -05001507int iscsi_eh_abort(struct scsi_cmnd *sc)
1508{
Mike Christie75613522008-05-21 15:53:59 -05001509 struct iscsi_cls_session *cls_session;
1510 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001511 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001512 struct iscsi_cmd_task *ctask;
1513 struct iscsi_tm *hdr;
1514 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001515
Mike Christie75613522008-05-21 15:53:59 -05001516 cls_session = starget_to_session(scsi_target(sc->device));
1517 session = cls_session->dd_data;
1518
Mike Christie6724add2007-08-15 01:38:30 -05001519 mutex_lock(&session->eh_mutex);
1520 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001521 /*
1522 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1523 * got the command.
1524 */
1525 if (!sc->SCp.ptr) {
1526 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001527 spin_unlock_bh(&session->lock);
1528 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001529 return SUCCESS;
1530 }
1531
Mike Christie7996a772006-04-06 21:13:41 -05001532 /*
1533 * If we are not logged in or we have started a new session
1534 * then let the host reset code handle this
1535 */
Mike Christie843c0a82007-12-13 12:43:20 -06001536 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1537 sc->SCp.phase != session->age) {
1538 spin_unlock_bh(&session->lock);
1539 mutex_unlock(&session->eh_mutex);
1540 return FAILED;
1541 }
1542
1543 conn = session->leadconn;
1544 conn->eh_abort_cnt++;
1545 age = session->age;
1546
1547 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1548 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001549
1550 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001551 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001552 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001553 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001554 }
Mike Christie7996a772006-04-06 21:13:41 -05001555
Mike Christie77a23c22007-05-30 12:57:18 -05001556 if (ctask->state == ISCSI_TASK_PENDING) {
1557 fail_command(conn, ctask, DID_ABORT << 16);
1558 goto success;
1559 }
Mike Christie7996a772006-04-06 21:13:41 -05001560
Mike Christie843c0a82007-12-13 12:43:20 -06001561 /* only have one tmf outstanding at a time */
1562 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001563 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001564 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001565
Mike Christie843c0a82007-12-13 12:43:20 -06001566 hdr = &conn->tmhdr;
1567 iscsi_prep_abort_task_pdu(ctask, hdr);
1568
Mike Christief6d51802007-12-13 12:43:30 -06001569 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001570 rc = FAILED;
1571 goto failed;
1572 }
1573
1574 switch (conn->tmf_state) {
1575 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001576 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001577 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001578 /*
1579 * clean up task if aborted. grab the recv lock as a writer
1580 */
1581 write_lock_bh(conn->recv_lock);
1582 spin_lock(&session->lock);
1583 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001584 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001585 spin_unlock(&session->lock);
1586 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001587 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001588 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001589 case TMF_TIMEDOUT:
1590 spin_unlock_bh(&session->lock);
1591 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1592 goto failed_unlocked;
1593 case TMF_NOT_FOUND:
1594 if (!sc->SCp.ptr) {
1595 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001596 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001597 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001598 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001599 }
1600 /* fall through */
1601 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001602 conn->tmf_state = TMF_INITIAL;
1603 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001604 }
1605
Mike Christie77a23c22007-05-30 12:57:18 -05001606success:
Mike Christie7996a772006-04-06 21:13:41 -05001607 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001608success_unlocked:
1609 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001610 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001611 return SUCCESS;
1612
1613failed:
1614 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001615failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001616 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1617 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001618 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001619 return FAILED;
1620}
1621EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1622
Mike Christie843c0a82007-12-13 12:43:20 -06001623static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1624{
1625 memset(hdr, 0, sizeof(*hdr));
1626 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1627 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1628 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1629 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001630 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001631}
1632
1633int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1634{
Mike Christie75613522008-05-21 15:53:59 -05001635 struct iscsi_cls_session *cls_session;
1636 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001637 struct iscsi_conn *conn;
1638 struct iscsi_tm *hdr;
1639 int rc = FAILED;
1640
Mike Christie75613522008-05-21 15:53:59 -05001641 cls_session = starget_to_session(scsi_target(sc->device));
1642 session = cls_session->dd_data;
1643
Mike Christie843c0a82007-12-13 12:43:20 -06001644 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1645
1646 mutex_lock(&session->eh_mutex);
1647 spin_lock_bh(&session->lock);
1648 /*
1649 * Just check if we are not logged in. We cannot check for
1650 * the phase because the reset could come from a ioctl.
1651 */
1652 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1653 goto unlock;
1654 conn = session->leadconn;
1655
1656 /* only have one tmf outstanding at a time */
1657 if (conn->tmf_state != TMF_INITIAL)
1658 goto unlock;
1659 conn->tmf_state = TMF_QUEUED;
1660
1661 hdr = &conn->tmhdr;
1662 iscsi_prep_lun_reset_pdu(sc, hdr);
1663
Mike Christief6d51802007-12-13 12:43:30 -06001664 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1665 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001666 rc = FAILED;
1667 goto unlock;
1668 }
1669
1670 switch (conn->tmf_state) {
1671 case TMF_SUCCESS:
1672 break;
1673 case TMF_TIMEDOUT:
1674 spin_unlock_bh(&session->lock);
1675 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1676 goto done;
1677 default:
1678 conn->tmf_state = TMF_INITIAL;
1679 goto unlock;
1680 }
1681
1682 rc = SUCCESS;
1683 spin_unlock_bh(&session->lock);
1684
1685 iscsi_suspend_tx(conn);
1686 /* need to grab the recv lock then session lock */
1687 write_lock_bh(conn->recv_lock);
1688 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001689 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001690 conn->tmf_state = TMF_INITIAL;
1691 spin_unlock(&session->lock);
1692 write_unlock_bh(conn->recv_lock);
1693
1694 iscsi_start_tx(conn);
1695 goto done;
1696
1697unlock:
1698 spin_unlock_bh(&session->lock);
1699done:
1700 debug_scsi("iscsi_eh_device_reset %s\n",
1701 rc == SUCCESS ? "SUCCESS" : "FAILED");
1702 mutex_unlock(&session->eh_mutex);
1703 return rc;
1704}
1705EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1706
Olaf Kirch63203772007-12-13 12:43:25 -06001707/*
1708 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1709 * should be accessed via kfifo_{get,put} on q->queue.
1710 * Optionally, the caller can obtain the array of object pointers
1711 * by passing in a non-NULL @items pointer
1712 */
Mike Christie7996a772006-04-06 21:13:41 -05001713int
Olaf Kirch63203772007-12-13 12:43:25 -06001714iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001715{
Olaf Kirch63203772007-12-13 12:43:25 -06001716 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001717
Olaf Kirch63203772007-12-13 12:43:25 -06001718 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001719
1720 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001721
1722 /* If the user passed an items pointer, he wants a copy of
1723 * the array. */
1724 if (items)
1725 num_arrays++;
1726 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1727 if (q->pool == NULL)
1728 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001729
1730 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1731 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001732 if (q->queue == ERR_PTR(-ENOMEM))
1733 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001734
1735 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001736 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001737 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001738 q->max = i;
1739 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001740 }
Mike Christie7996a772006-04-06 21:13:41 -05001741 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1742 }
Olaf Kirch63203772007-12-13 12:43:25 -06001743
1744 if (items) {
1745 *items = q->pool + max;
1746 memcpy(*items, q->pool, max * sizeof(void *));
1747 }
1748
Mike Christie7996a772006-04-06 21:13:41 -05001749 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001750
1751enomem:
1752 iscsi_pool_free(q);
1753 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001754}
1755EXPORT_SYMBOL_GPL(iscsi_pool_init);
1756
Olaf Kirch63203772007-12-13 12:43:25 -06001757void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001758{
1759 int i;
1760
1761 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001762 kfree(q->pool[i]);
1763 if (q->pool)
1764 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001765}
1766EXPORT_SYMBOL_GPL(iscsi_pool_free);
1767
Mike Christiea4804cd2008-05-21 15:54:00 -05001768/**
1769 * iscsi_host_add - add host to system
1770 * @shost: scsi host
1771 * @pdev: parent device
1772 *
1773 * This should be called by partial offload and software iscsi drivers
1774 * to add a host to the system.
1775 */
1776int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001777{
Mike Christiea4804cd2008-05-21 15:54:00 -05001778 return scsi_add_host(shost, pdev);
1779}
1780EXPORT_SYMBOL_GPL(iscsi_host_add);
1781
1782/**
1783 * iscsi_host_alloc - allocate a host and driver data
1784 * @sht: scsi host template
1785 * @dd_data_size: driver host data size
1786 * @qdepth: default device queue depth
1787 *
1788 * This should be called by partial offload and software iscsi drivers.
1789 * To access the driver specific memory use the iscsi_host_priv() macro.
1790 */
1791struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1792 int dd_data_size, uint16_t qdepth)
1793{
1794 struct Scsi_Host *shost;
1795
1796 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1797 if (!shost)
1798 return NULL;
1799 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1800
Mike Christie15482712007-05-30 12:57:19 -05001801 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1802 if (qdepth != 0)
1803 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001804 "Queue depth must be between 1 and %d.\n",
1805 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001806 qdepth = ISCSI_DEF_CMD_PER_LUN;
1807 }
Mike Christie75613522008-05-21 15:53:59 -05001808 shost->cmd_per_lun = qdepth;
Mike Christiea4804cd2008-05-21 15:54:00 -05001809 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001810}
Mike Christiea4804cd2008-05-21 15:54:00 -05001811EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001812
Mike Christiea4804cd2008-05-21 15:54:00 -05001813/**
1814 * iscsi_host_remove - remove host and sessions
1815 * @shost: scsi host
1816 *
1817 * This will also remove any sessions attached to the host, but if userspace
1818 * is managing the session at the same time this will break. TODO: add
1819 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1820 * does not remove the memory from under us.
1821 */
1822void iscsi_host_remove(struct Scsi_Host *shost)
1823{
1824 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1825 scsi_remove_host(shost);
1826}
1827EXPORT_SYMBOL_GPL(iscsi_host_remove);
1828
1829void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001830{
1831 struct iscsi_host *ihost = shost_priv(shost);
1832
1833 kfree(ihost->netdev);
1834 kfree(ihost->hwaddress);
1835 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001836 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001837}
Mike Christiea4804cd2008-05-21 15:54:00 -05001838EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05001839
1840/**
1841 * iscsi_session_setup - create iscsi cls session and host and session
1842 * @iscsit: iscsi transport template
1843 * @shost: scsi host
1844 * @cmds_max: session can queue
1845 * @cmd_task_size: LLD ctask private data size
1846 * @mgmt_task_size: LLD mtask private data size
1847 * @initial_cmdsn: initial CmdSN
1848 *
1849 * This can be used by software iscsi_transports that allocate
1850 * a session per scsi host.
1851 */
1852struct iscsi_cls_session *
1853iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
1854 uint16_t cmds_max, int cmd_task_size, int mgmt_task_size,
1855 uint32_t initial_cmdsn)
1856{
1857 struct iscsi_session *session;
1858 struct iscsi_cls_session *cls_session;
1859 int cmd_i;
1860
Mike Christie31ed0bf2008-02-26 12:35:23 -06001861 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1862 cmds_max < 2) {
Mike Christie15482712007-05-30 12:57:19 -05001863 if (cmds_max != 0)
1864 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1865 "can_queue must be a power of 2 and between "
1866 "2 and %d - setting to %d.\n", cmds_max,
1867 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1868 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1869 }
1870
Mike Christie5d91e202008-05-21 15:54:01 -05001871 cls_session = iscsi_alloc_session(shost, iscsit,
1872 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05001873 if (!cls_session)
Mike Christie7996a772006-04-06 21:13:41 -05001874 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001875 session = cls_session->dd_data;
1876 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001877 session->host = shost;
1878 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001879 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001880 session->lu_reset_timeout = 15;
1881 session->abort_timeout = 10;
Mike Christie7996a772006-04-06 21:13:41 -05001882 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001883 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001884 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001885 session->exp_cmdsn = initial_cmdsn + 1;
1886 session->max_cmdsn = initial_cmdsn + 1;
1887 session->max_r2t = 1;
1888 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001889 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05001890 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001891
1892 /* initialize SCSI PDU commands pool */
1893 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1894 (void***)&session->cmds,
1895 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1896 goto cmdpool_alloc_fail;
1897
1898 /* pre-format cmds pool with ITT */
1899 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1900 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1901
1902 if (cmd_task_size)
1903 ctask->dd_data = &ctask[1];
1904 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001905 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001906 }
1907
Mike Christie7996a772006-04-06 21:13:41 -05001908 /* initialize immediate command pool */
1909 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1910 (void***)&session->mgmt_cmds,
1911 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1912 goto mgmtpool_alloc_fail;
1913
1914
1915 /* pre-format immediate cmds pool with ITT */
1916 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1917 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1918
1919 if (mgmt_task_size)
1920 mtask->dd_data = &mtask[1];
1921 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001922 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001923 }
1924
Mike Christief53a88d2006-06-28 12:00:27 -05001925 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05001926 goto module_get_fail;
1927
1928 if (iscsi_add_session(cls_session, 0))
Mike Christief53a88d2006-06-28 12:00:27 -05001929 goto cls_session_fail;
Mike Christie7996a772006-04-06 21:13:41 -05001930 return cls_session;
1931
1932cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05001933 module_put(iscsit->owner);
1934module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001935 iscsi_pool_free(&session->mgmtpool);
Mike Christie7996a772006-04-06 21:13:41 -05001936mgmtpool_alloc_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001937 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001938cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05001939 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001940 return NULL;
1941}
1942EXPORT_SYMBOL_GPL(iscsi_session_setup);
1943
1944/**
1945 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05001946 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05001947 *
Mike Christie75613522008-05-21 15:53:59 -05001948 * The driver must have called iscsi_remove_session before
1949 * calling this.
1950 */
Mike Christie7996a772006-04-06 21:13:41 -05001951void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1952{
Mike Christie75613522008-05-21 15:53:59 -05001953 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05001954 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001955
Olaf Kirch63203772007-12-13 12:43:25 -06001956 iscsi_pool_free(&session->mgmtpool);
1957 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001958
Mike Christieb2c64162007-05-30 12:57:16 -05001959 kfree(session->password);
1960 kfree(session->password_in);
1961 kfree(session->username);
1962 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001963 kfree(session->targetname);
1964
Mike Christie75613522008-05-21 15:53:59 -05001965 iscsi_destroy_session(cls_session);
Mike Christie63f75cc2006-07-24 15:47:29 -05001966 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001967}
1968EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1969
1970/**
1971 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1972 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05001973 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05001974 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05001975 */
Mike Christie7996a772006-04-06 21:13:41 -05001976struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05001977iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
1978 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05001979{
Mike Christie75613522008-05-21 15:53:59 -05001980 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001981 struct iscsi_conn *conn;
1982 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001983 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001984
Mike Christie5d91e202008-05-21 15:54:01 -05001985 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
1986 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05001987 if (!cls_conn)
1988 return NULL;
1989 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05001990 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05001991
Mike Christie5d91e202008-05-21 15:54:01 -05001992 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05001993 conn->session = session;
1994 conn->cls_conn = cls_conn;
1995 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1996 conn->id = conn_idx;
1997 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001998 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06001999
2000 init_timer(&conn->transport_timer);
2001 conn->transport_timer.data = (unsigned long)conn;
2002 conn->transport_timer.function = iscsi_check_transport_timeouts;
2003
Mike Christie7996a772006-04-06 21:13:41 -05002004 INIT_LIST_HEAD(&conn->run_list);
2005 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002006 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002007 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002008 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002009 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002010
2011 /* allocate login_mtask used for the login/text sequences */
2012 spin_lock_bh(&session->lock);
2013 if (!__kfifo_get(session->mgmtpool.queue,
2014 (void*)&conn->login_mtask,
2015 sizeof(void*))) {
2016 spin_unlock_bh(&session->lock);
2017 goto login_mtask_alloc_fail;
2018 }
2019 spin_unlock_bh(&session->lock);
2020
Mike Christiebf32ed32007-02-28 17:32:17 -06002021 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002022 if (!data)
2023 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05002024 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002025
Mike Christie843c0a82007-12-13 12:43:20 -06002026 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002027 init_waitqueue_head(&conn->ehwait);
2028
2029 return cls_conn;
2030
Mike Christied36ab6f2006-05-18 20:31:34 -05002031login_mtask_data_alloc_fail:
2032 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2033 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002034login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002035 iscsi_destroy_conn(cls_conn);
2036 return NULL;
2037}
2038EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2039
2040/**
2041 * iscsi_conn_teardown - teardown iscsi connection
2042 * cls_conn: iscsi class connection
2043 *
2044 * TODO: we may need to make this into a two step process
2045 * like scsi-mls remove + put host
2046 */
2047void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2048{
2049 struct iscsi_conn *conn = cls_conn->dd_data;
2050 struct iscsi_session *session = conn->session;
2051 unsigned long flags;
2052
Mike Christief6d51802007-12-13 12:43:30 -06002053 del_timer_sync(&conn->transport_timer);
2054
Mike Christie7996a772006-04-06 21:13:41 -05002055 spin_lock_bh(&session->lock);
2056 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2057 if (session->leadconn == conn) {
2058 /*
2059 * leading connection? then give up on recovery.
2060 */
2061 session->state = ISCSI_STATE_TERMINATE;
2062 wake_up(&conn->ehwait);
2063 }
2064 spin_unlock_bh(&session->lock);
2065
Mike Christie7996a772006-04-06 21:13:41 -05002066 /*
2067 * Block until all in-progress commands for this connection
2068 * time out or fail.
2069 */
2070 for (;;) {
2071 spin_lock_irqsave(session->host->host_lock, flags);
2072 if (!session->host->host_busy) { /* OK for ERL == 0 */
2073 spin_unlock_irqrestore(session->host->host_lock, flags);
2074 break;
2075 }
2076 spin_unlock_irqrestore(session->host->host_lock, flags);
2077 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002078 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2079 "host_busy %d host_failed %d\n",
2080 session->host->host_busy,
2081 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002082 /*
2083 * force eh_abort() to unblock
2084 */
2085 wake_up(&conn->ehwait);
2086 }
2087
Mike Christie779ea122007-02-28 17:32:15 -06002088 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002089 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002090
Mike Christie7996a772006-04-06 21:13:41 -05002091 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002092 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002093 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05002094 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2095 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002096 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002097 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002098 spin_unlock_bh(&session->lock);
2099
Mike Christie7996a772006-04-06 21:13:41 -05002100 iscsi_destroy_conn(cls_conn);
2101}
2102EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2103
2104int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2105{
2106 struct iscsi_conn *conn = cls_conn->dd_data;
2107 struct iscsi_session *session = conn->session;
2108
Mike Christieffd04362006-08-31 18:09:24 -04002109 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002110 iscsi_conn_printk(KERN_ERR, conn,
2111 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002112 return -EPERM;
2113 }
2114
Mike Christiedb98ccd2006-08-31 18:09:31 -04002115 if ((session->imm_data_en || !session->initial_r2t_en) &&
2116 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002117 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2118 "first_burst %d max_burst %d\n",
2119 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002120 return -EINVAL;
2121 }
2122
Mike Christief6d51802007-12-13 12:43:30 -06002123 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002124 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2125 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002126 conn->recv_timeout = 5;
2127 }
2128
2129 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002130 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2131 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002132 conn->ping_timeout = 5;
2133 }
2134
Mike Christie7996a772006-04-06 21:13:41 -05002135 spin_lock_bh(&session->lock);
2136 conn->c_stage = ISCSI_CONN_STARTED;
2137 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002138 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002139
Mike Christief6d51802007-12-13 12:43:30 -06002140 conn->last_recv = jiffies;
2141 conn->last_ping = jiffies;
2142 if (conn->recv_timeout && conn->ping_timeout)
2143 mod_timer(&conn->transport_timer,
2144 jiffies + (conn->recv_timeout * HZ));
2145
Mike Christie7996a772006-04-06 21:13:41 -05002146 switch(conn->stop_stage) {
2147 case STOP_CONN_RECOVER:
2148 /*
2149 * unblock eh_abort() if it is blocked. re-try all
2150 * commands after successful recovery
2151 */
Mike Christie7996a772006-04-06 21:13:41 -05002152 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002153 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002154 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002155 if (session->age == 16)
2156 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002157 break;
Mike Christie7996a772006-04-06 21:13:41 -05002158 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002159 conn->stop_stage = 0;
2160 break;
Mike Christie7996a772006-04-06 21:13:41 -05002161 default:
2162 break;
2163 }
2164 spin_unlock_bh(&session->lock);
2165
Mike Christie75613522008-05-21 15:53:59 -05002166 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002167 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002168 return 0;
2169}
2170EXPORT_SYMBOL_GPL(iscsi_conn_start);
2171
2172static void
2173flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2174{
2175 struct iscsi_mgmt_task *mtask, *tmp;
2176
2177 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06002178 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2179 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002180 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002181 }
2182
2183 /* handle running */
2184 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2185 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002186 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002187 }
2188
2189 conn->mtask = NULL;
2190}
2191
Mike Christie656cffc2006-05-18 20:31:42 -05002192static void iscsi_start_session_recovery(struct iscsi_session *session,
2193 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002194{
Mike Christieed2abc72006-05-02 19:46:40 -05002195 int old_stop_stage;
2196
Mike Christief6d51802007-12-13 12:43:30 -06002197 del_timer_sync(&conn->transport_timer);
2198
Mike Christie6724add2007-08-15 01:38:30 -05002199 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002200 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002201 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002202 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002203 mutex_unlock(&session->eh_mutex);
2204 return;
2205 }
2206
2207 /*
2208 * The LLD either freed/unset the lock on us, or userspace called
2209 * stop but did not create a proper connection (connection was never
2210 * bound or it was unbound then stop was called).
2211 */
2212 if (!conn->recv_lock) {
2213 spin_unlock_bh(&session->lock);
2214 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002215 return;
2216 }
Mike Christieed2abc72006-05-02 19:46:40 -05002217
2218 /*
2219 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05002220 * up the login task and connection. We do not need to block and set
2221 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002222 */
Mike Christie67a61112006-05-30 00:37:20 -05002223 if (flag == STOP_CONN_TERM)
2224 session->state = ISCSI_STATE_TERMINATE;
2225 else if (conn->stop_stage != STOP_CONN_RECOVER)
2226 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002227
2228 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002229 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002230 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002231 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002232
2233 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002234
Mike Christie1c834692006-07-24 15:47:26 -05002235 write_lock_bh(conn->recv_lock);
2236 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2237 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002238
Mike Christie7996a772006-04-06 21:13:41 -05002239 /*
2240 * for connection level recovery we should not calculate
2241 * header digest. conn->hdr_size used for optimization
2242 * in hdr_extract() and will be re-negotiated at
2243 * set_param() time.
2244 */
2245 if (flag == STOP_CONN_RECOVER) {
2246 conn->hdrdgst_en = 0;
2247 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002248 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002249 old_stop_stage != STOP_CONN_RECOVER) {
2250 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002251 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002252 }
Mike Christie7996a772006-04-06 21:13:41 -05002253 }
Mike Christie656cffc2006-05-18 20:31:42 -05002254
Mike Christie656cffc2006-05-18 20:31:42 -05002255 /*
2256 * flush queues.
2257 */
2258 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002259 fail_all_commands(conn, -1,
2260 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002261 flush_control_queues(session, conn);
2262 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002263 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002264}
Mike Christie7996a772006-04-06 21:13:41 -05002265
2266void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2267{
2268 struct iscsi_conn *conn = cls_conn->dd_data;
2269 struct iscsi_session *session = conn->session;
2270
2271 switch (flag) {
2272 case STOP_CONN_RECOVER:
2273 case STOP_CONN_TERM:
2274 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002275 break;
Mike Christie7996a772006-04-06 21:13:41 -05002276 default:
Mike Christie322d7392008-01-31 13:36:52 -06002277 iscsi_conn_printk(KERN_ERR, conn,
2278 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002279 }
2280}
2281EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2282
2283int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2284 struct iscsi_cls_conn *cls_conn, int is_leading)
2285{
Mike Christie75613522008-05-21 15:53:59 -05002286 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002287 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002288
Mike Christie7996a772006-04-06 21:13:41 -05002289 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002290 if (is_leading)
2291 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002292 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002293
2294 /*
2295 * Unblock xmitworker(), Login Phase will pass through.
2296 */
2297 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2298 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2299 return 0;
2300}
2301EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2302
Mike Christiea54a52c2006-06-28 12:00:23 -05002303
2304int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2305 enum iscsi_param param, char *buf, int buflen)
2306{
2307 struct iscsi_conn *conn = cls_conn->dd_data;
2308 struct iscsi_session *session = conn->session;
2309 uint32_t value;
2310
2311 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002312 case ISCSI_PARAM_FAST_ABORT:
2313 sscanf(buf, "%d", &session->fast_abort);
2314 break;
Mike Christief6d51802007-12-13 12:43:30 -06002315 case ISCSI_PARAM_ABORT_TMO:
2316 sscanf(buf, "%d", &session->abort_timeout);
2317 break;
2318 case ISCSI_PARAM_LU_RESET_TMO:
2319 sscanf(buf, "%d", &session->lu_reset_timeout);
2320 break;
2321 case ISCSI_PARAM_PING_TMO:
2322 sscanf(buf, "%d", &conn->ping_timeout);
2323 break;
2324 case ISCSI_PARAM_RECV_TMO:
2325 sscanf(buf, "%d", &conn->recv_timeout);
2326 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002327 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2328 sscanf(buf, "%d", &conn->max_recv_dlength);
2329 break;
2330 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2331 sscanf(buf, "%d", &conn->max_xmit_dlength);
2332 break;
2333 case ISCSI_PARAM_HDRDGST_EN:
2334 sscanf(buf, "%d", &conn->hdrdgst_en);
2335 break;
2336 case ISCSI_PARAM_DATADGST_EN:
2337 sscanf(buf, "%d", &conn->datadgst_en);
2338 break;
2339 case ISCSI_PARAM_INITIAL_R2T_EN:
2340 sscanf(buf, "%d", &session->initial_r2t_en);
2341 break;
2342 case ISCSI_PARAM_MAX_R2T:
2343 sscanf(buf, "%d", &session->max_r2t);
2344 break;
2345 case ISCSI_PARAM_IMM_DATA_EN:
2346 sscanf(buf, "%d", &session->imm_data_en);
2347 break;
2348 case ISCSI_PARAM_FIRST_BURST:
2349 sscanf(buf, "%d", &session->first_burst);
2350 break;
2351 case ISCSI_PARAM_MAX_BURST:
2352 sscanf(buf, "%d", &session->max_burst);
2353 break;
2354 case ISCSI_PARAM_PDU_INORDER_EN:
2355 sscanf(buf, "%d", &session->pdu_inorder_en);
2356 break;
2357 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2358 sscanf(buf, "%d", &session->dataseq_inorder_en);
2359 break;
2360 case ISCSI_PARAM_ERL:
2361 sscanf(buf, "%d", &session->erl);
2362 break;
2363 case ISCSI_PARAM_IFMARKER_EN:
2364 sscanf(buf, "%d", &value);
2365 BUG_ON(value);
2366 break;
2367 case ISCSI_PARAM_OFMARKER_EN:
2368 sscanf(buf, "%d", &value);
2369 BUG_ON(value);
2370 break;
2371 case ISCSI_PARAM_EXP_STATSN:
2372 sscanf(buf, "%u", &conn->exp_statsn);
2373 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002374 case ISCSI_PARAM_USERNAME:
2375 kfree(session->username);
2376 session->username = kstrdup(buf, GFP_KERNEL);
2377 if (!session->username)
2378 return -ENOMEM;
2379 break;
2380 case ISCSI_PARAM_USERNAME_IN:
2381 kfree(session->username_in);
2382 session->username_in = kstrdup(buf, GFP_KERNEL);
2383 if (!session->username_in)
2384 return -ENOMEM;
2385 break;
2386 case ISCSI_PARAM_PASSWORD:
2387 kfree(session->password);
2388 session->password = kstrdup(buf, GFP_KERNEL);
2389 if (!session->password)
2390 return -ENOMEM;
2391 break;
2392 case ISCSI_PARAM_PASSWORD_IN:
2393 kfree(session->password_in);
2394 session->password_in = kstrdup(buf, GFP_KERNEL);
2395 if (!session->password_in)
2396 return -ENOMEM;
2397 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002398 case ISCSI_PARAM_TARGET_NAME:
2399 /* this should not change between logins */
2400 if (session->targetname)
2401 break;
2402
2403 session->targetname = kstrdup(buf, GFP_KERNEL);
2404 if (!session->targetname)
2405 return -ENOMEM;
2406 break;
2407 case ISCSI_PARAM_TPGT:
2408 sscanf(buf, "%d", &session->tpgt);
2409 break;
2410 case ISCSI_PARAM_PERSISTENT_PORT:
2411 sscanf(buf, "%d", &conn->persistent_port);
2412 break;
2413 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2414 /*
2415 * this is the address returned in discovery so it should
2416 * not change between logins.
2417 */
2418 if (conn->persistent_address)
2419 break;
2420
2421 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2422 if (!conn->persistent_address)
2423 return -ENOMEM;
2424 break;
2425 default:
2426 return -ENOSYS;
2427 }
2428
2429 return 0;
2430}
2431EXPORT_SYMBOL_GPL(iscsi_set_param);
2432
2433int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2434 enum iscsi_param param, char *buf)
2435{
Mike Christie75613522008-05-21 15:53:59 -05002436 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002437 int len;
2438
2439 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002440 case ISCSI_PARAM_FAST_ABORT:
2441 len = sprintf(buf, "%d\n", session->fast_abort);
2442 break;
Mike Christief6d51802007-12-13 12:43:30 -06002443 case ISCSI_PARAM_ABORT_TMO:
2444 len = sprintf(buf, "%d\n", session->abort_timeout);
2445 break;
2446 case ISCSI_PARAM_LU_RESET_TMO:
2447 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2448 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002449 case ISCSI_PARAM_INITIAL_R2T_EN:
2450 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2451 break;
2452 case ISCSI_PARAM_MAX_R2T:
2453 len = sprintf(buf, "%hu\n", session->max_r2t);
2454 break;
2455 case ISCSI_PARAM_IMM_DATA_EN:
2456 len = sprintf(buf, "%d\n", session->imm_data_en);
2457 break;
2458 case ISCSI_PARAM_FIRST_BURST:
2459 len = sprintf(buf, "%u\n", session->first_burst);
2460 break;
2461 case ISCSI_PARAM_MAX_BURST:
2462 len = sprintf(buf, "%u\n", session->max_burst);
2463 break;
2464 case ISCSI_PARAM_PDU_INORDER_EN:
2465 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2466 break;
2467 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2468 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2469 break;
2470 case ISCSI_PARAM_ERL:
2471 len = sprintf(buf, "%d\n", session->erl);
2472 break;
2473 case ISCSI_PARAM_TARGET_NAME:
2474 len = sprintf(buf, "%s\n", session->targetname);
2475 break;
2476 case ISCSI_PARAM_TPGT:
2477 len = sprintf(buf, "%d\n", session->tpgt);
2478 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002479 case ISCSI_PARAM_USERNAME:
2480 len = sprintf(buf, "%s\n", session->username);
2481 break;
2482 case ISCSI_PARAM_USERNAME_IN:
2483 len = sprintf(buf, "%s\n", session->username_in);
2484 break;
2485 case ISCSI_PARAM_PASSWORD:
2486 len = sprintf(buf, "%s\n", session->password);
2487 break;
2488 case ISCSI_PARAM_PASSWORD_IN:
2489 len = sprintf(buf, "%s\n", session->password_in);
2490 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002491 default:
2492 return -ENOSYS;
2493 }
2494
2495 return len;
2496}
2497EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2498
2499int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2500 enum iscsi_param param, char *buf)
2501{
2502 struct iscsi_conn *conn = cls_conn->dd_data;
2503 int len;
2504
2505 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002506 case ISCSI_PARAM_PING_TMO:
2507 len = sprintf(buf, "%u\n", conn->ping_timeout);
2508 break;
2509 case ISCSI_PARAM_RECV_TMO:
2510 len = sprintf(buf, "%u\n", conn->recv_timeout);
2511 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002512 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2513 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2514 break;
2515 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2516 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2517 break;
2518 case ISCSI_PARAM_HDRDGST_EN:
2519 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2520 break;
2521 case ISCSI_PARAM_DATADGST_EN:
2522 len = sprintf(buf, "%d\n", conn->datadgst_en);
2523 break;
2524 case ISCSI_PARAM_IFMARKER_EN:
2525 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2526 break;
2527 case ISCSI_PARAM_OFMARKER_EN:
2528 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2529 break;
2530 case ISCSI_PARAM_EXP_STATSN:
2531 len = sprintf(buf, "%u\n", conn->exp_statsn);
2532 break;
2533 case ISCSI_PARAM_PERSISTENT_PORT:
2534 len = sprintf(buf, "%d\n", conn->persistent_port);
2535 break;
2536 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2537 len = sprintf(buf, "%s\n", conn->persistent_address);
2538 break;
2539 default:
2540 return -ENOSYS;
2541 }
2542
2543 return len;
2544}
2545EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2546
Mike Christie0801c242007-05-30 12:57:12 -05002547int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2548 char *buf)
2549{
Mike Christie75613522008-05-21 15:53:59 -05002550 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002551 int len;
2552
2553 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002554 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002555 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002556 len = sprintf(buf, "%s\n", "default");
2557 else
Mike Christie75613522008-05-21 15:53:59 -05002558 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002559 break;
Mike Christie0801c242007-05-30 12:57:12 -05002560 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002561 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002562 len = sprintf(buf, "%s\n", "default");
2563 else
Mike Christie75613522008-05-21 15:53:59 -05002564 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002565 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002566 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002567 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002568 len = sprintf(buf, "%s\n", "unknown");
2569 else
Mike Christie75613522008-05-21 15:53:59 -05002570 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002571 break;
Mike Christie75613522008-05-21 15:53:59 -05002572 case ISCSI_HOST_PARAM_IPADDRESS:
2573 if (!strlen(ihost->local_address))
2574 len = sprintf(buf, "%s\n", "unknown");
2575 else
2576 len = sprintf(buf, "%s\n",
2577 ihost->local_address);
Mike Christie0801c242007-05-30 12:57:12 -05002578 default:
2579 return -ENOSYS;
2580 }
2581
2582 return len;
2583}
2584EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2585
2586int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2587 char *buf, int buflen)
2588{
Mike Christie75613522008-05-21 15:53:59 -05002589 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002590
2591 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002592 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002593 if (!ihost->netdev)
2594 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002595 break;
Mike Christie0801c242007-05-30 12:57:12 -05002596 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002597 if (!ihost->hwaddress)
2598 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002599 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002600 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002601 if (!ihost->initiatorname)
2602 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002603 break;
Mike Christie0801c242007-05-30 12:57:12 -05002604 default:
2605 return -ENOSYS;
2606 }
2607
2608 return 0;
2609}
2610EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2611
Mike Christie7996a772006-04-06 21:13:41 -05002612MODULE_AUTHOR("Mike Christie");
2613MODULE_DESCRIPTION("iSCSI library functions");
2614MODULE_LICENSE("GPL");