blob: 1e605de07cffbb92cf8abe1df076b4e1ff76476a [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 Christie052d0142008-05-21 15:54:05 -050082 !list_empty(&session->leadconn->mgmtqueue)) {
83 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
84 scsi_queue_work(session->host,
85 &session->leadconn->xmitwork);
86 }
Mike Christie77a23c22007-05-30 12:57:18 -050087 }
Mike Christie7996a772006-04-06 21:13:41 -050088}
Mike Christie77a23c22007-05-30 12:57:18 -050089EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050090
91void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040092 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050093{
94 struct iscsi_conn *conn = ctask->conn;
95
96 memset(hdr, 0, sizeof(struct iscsi_data));
97 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
98 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
99 ctask->unsol_datasn++;
100 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
101 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
102
103 hdr->itt = ctask->hdr->itt;
104 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400105 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500106
107 if (ctask->unsol_count > conn->max_xmit_dlength) {
108 hton24(hdr->dlength, conn->max_xmit_dlength);
109 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -0400110 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500111 hdr->flags = 0;
112 } else {
113 hton24(hdr->dlength, ctask->unsol_count);
114 ctask->data_count = ctask->unsol_count;
115 hdr->flags = ISCSI_FLAG_CMD_FINAL;
116 }
117}
118EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
119
Boaz Harrosh004d6532007-12-13 12:43:23 -0600120static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
121{
122 unsigned exp_len = ctask->hdr_len + len;
123
124 if (exp_len > ctask->hdr_max) {
125 WARN_ON(1);
126 return -EINVAL;
127 }
128
129 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
130 ctask->hdr_len = exp_len;
131 return 0;
132}
133
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500134/*
135 * make an extended cdb AHS
136 */
137static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
138{
139 struct scsi_cmnd *cmd = ctask->sc;
140 unsigned rlen, pad_len;
141 unsigned short ahslength;
142 struct iscsi_ecdb_ahdr *ecdb_ahdr;
143 int rc;
144
145 ecdb_ahdr = iscsi_next_hdr(ctask);
146 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
147
148 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
149 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
150
151 pad_len = iscsi_padding(rlen);
152
153 rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
154 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
155 if (rc)
156 return rc;
157
158 if (pad_len)
159 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
160
161 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
162 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
163 ecdb_ahdr->reserved = 0;
164 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
165
166 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
167 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
168 cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
169
170 return 0;
171}
172
Boaz Harroshc07d4442008-04-18 10:11:52 -0500173static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
174{
175 struct scsi_cmnd *sc = ctask->sc;
176 struct iscsi_rlength_ahdr *rlen_ahdr;
177 int rc;
178
179 rlen_ahdr = iscsi_next_hdr(ctask);
180 rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
181 if (rc)
182 return rc;
183
184 rlen_ahdr->ahslength =
185 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
186 sizeof(rlen_ahdr->reserved));
187 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
188 rlen_ahdr->reserved = 0;
189 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
190
191 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
192 "rlen_ahdr->ahslength(%d)\n",
193 be32_to_cpu(rlen_ahdr->read_length),
194 be16_to_cpu(rlen_ahdr->ahslength));
195 return 0;
196}
197
Mike Christie7996a772006-04-06 21:13:41 -0500198/**
199 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
200 * @ctask: iscsi cmd task
201 *
202 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
203 * fields like dlength or final based on how much data it sends
204 */
Boaz Harrosh004d6532007-12-13 12:43:23 -0600205static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500206{
207 struct iscsi_conn *conn = ctask->conn;
208 struct iscsi_session *session = conn->session;
209 struct iscsi_cmd *hdr = ctask->hdr;
210 struct scsi_cmnd *sc = ctask->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500211 unsigned hdrlength, cmd_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600212 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500213
Boaz Harrosh004d6532007-12-13 12:43:23 -0600214 ctask->hdr_len = 0;
215 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
216 if (rc)
217 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600218 hdr->opcode = ISCSI_OP_SCSI_CMD;
219 hdr->flags = ISCSI_ATTR_SIMPLE;
220 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie8b1d0342008-01-31 13:36:53 -0600221 hdr->itt = build_itt(ctask->itt, session->age);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600222 hdr->cmdsn = cpu_to_be32(session->cmdsn);
223 session->cmdsn++;
224 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500225 cmd_len = sc->cmd_len;
226 if (cmd_len < ISCSI_CDB_SIZE)
227 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
228 else if (cmd_len > ISCSI_CDB_SIZE) {
229 rc = iscsi_prep_ecdb_ahs(ctask);
230 if (rc)
231 return rc;
232 cmd_len = ISCSI_CDB_SIZE;
233 }
234 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500235
Mike Christie218432c2007-05-30 12:57:17 -0500236 ctask->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500237 if (scsi_bidi_cmnd(sc)) {
238 hdr->flags |= ISCSI_FLAG_CMD_READ;
239 rc = iscsi_prep_bidi_ahs(ctask);
240 if (rc)
241 return rc;
242 }
Mike Christie7996a772006-04-06 21:13:41 -0500243 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500244 unsigned out_len = scsi_out(sc)->length;
245 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500246 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
247 /*
248 * Write counters:
249 *
250 * imm_count bytes to be sent right after
251 * SCSI PDU Header
252 *
253 * unsol_count bytes(as Data-Out) to be sent
254 * without R2T ack right after
255 * immediate data
256 *
257 * r2t_data_count bytes to be sent via R2T ack's
258 *
259 * pad_count bytes to be sent as zero-padding
260 */
Mike Christie7996a772006-04-06 21:13:41 -0500261 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400262 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500263 ctask->unsol_datasn = 0;
264
265 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500266 if (out_len >= session->first_burst)
Mike Christie7996a772006-04-06 21:13:41 -0500267 ctask->imm_count = min(session->first_burst,
268 conn->max_xmit_dlength);
269 else
Boaz Harroshc07d4442008-04-18 10:11:52 -0500270 ctask->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500271 conn->max_xmit_dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600272 hton24(hdr->dlength, ctask->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500273 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600274 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500275
Mike Christieffd04362006-08-31 18:09:24 -0400276 if (!session->initial_r2t_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500277 ctask->unsol_count = min(session->first_burst, out_len)
278 - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400279 ctask->unsol_offset = ctask->imm_count;
280 }
281
Mike Christie7996a772006-04-06 21:13:41 -0500282 if (!ctask->unsol_count)
283 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600284 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500285 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500286 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
287 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500288 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500289
290 if (sc->sc_data_direction == DMA_FROM_DEVICE)
291 hdr->flags |= ISCSI_FLAG_CMD_READ;
292 }
293
Boaz Harrosh004d6532007-12-13 12:43:23 -0600294 /* calculate size of additional header segments (AHSs) */
295 hdrlength = ctask->hdr_len - sizeof(*hdr);
296
297 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
298 hdrlength /= ISCSI_PAD_LEN;
299
300 WARN_ON(hdrlength >= 256);
301 hdr->hlength = hdrlength & 0xFF;
302
Mike Christie052d0142008-05-21 15:54:05 -0500303 if (conn->session->tt->init_cmd_task &&
304 conn->session->tt->init_cmd_task(ctask))
305 return -EIO;
306
307 ctask->state = ISCSI_TASK_RUNNING;
308 list_move_tail(&ctask->running, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500309
Olaf Kircha8ac6312007-12-13 12:43:35 -0600310 conn->scsicmd_pdus_cnt++;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500311 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
312 "len %d bidi_len %d cmdsn %d win %d]\n",
313 scsi_bidi_cmnd(sc) ? "bidirectional" :
314 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
315 conn->id, sc, sc->cmnd[0], ctask->itt,
316 scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600317 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600318 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500319}
Mike Christie7996a772006-04-06 21:13:41 -0500320
321/**
322 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500323 * @ctask: iscsi cmd task
324 *
325 * Must be called with session lock.
326 * This function returns the scsi command to scsi-ml and returns
327 * the cmd task to the pool of available cmd tasks.
328 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400329static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500330{
Mike Christiec1635cb2007-12-13 12:43:33 -0600331 struct iscsi_conn *conn = ctask->conn;
332 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500333 struct scsi_cmnd *sc = ctask->sc;
334
Mike Christieb6c395e2006-07-24 15:47:15 -0500335 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500336 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400337 /* SCSI eh reuses commands to verify us */
338 sc->SCp.ptr = NULL;
Mike Christiec1635cb2007-12-13 12:43:33 -0600339 if (conn->ctask == ctask)
340 conn->ctask = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500341 list_del_init(&ctask->running);
342 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500343
344 if (sc->scsi_done)
345 sc->scsi_done(sc);
Mike Christie7996a772006-04-06 21:13:41 -0500346}
347
Mike Christie60ecebf2006-08-31 18:09:25 -0400348static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
349{
350 atomic_inc(&ctask->refcount);
351}
352
Mike Christie60ecebf2006-08-31 18:09:25 -0400353static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
354{
Mike Christiee648f632006-08-31 18:09:34 -0400355 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400356 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400357}
358
Mike Christieb3a7ea82007-12-13 12:43:26 -0600359/*
360 * session lock must be held
361 */
362static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
363 int err)
364{
365 struct scsi_cmnd *sc;
366
367 sc = ctask->sc;
368 if (!sc)
369 return;
370
371 if (ctask->state == ISCSI_TASK_PENDING)
372 /*
373 * cmd never made it to the xmit thread, so we should not count
374 * the cmd in the sequencing
375 */
376 conn->session->queued_cmdsn--;
377 else
378 conn->session->tt->cleanup_cmd_task(conn, ctask);
379
380 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500381 if (!scsi_bidi_cmnd(sc))
382 scsi_set_resid(sc, scsi_bufflen(sc));
383 else {
384 scsi_out(sc)->resid = scsi_out(sc)->length;
385 scsi_in(sc)->resid = scsi_in(sc)->length;
386 }
Mike Christieb3a7ea82007-12-13 12:43:26 -0600387 if (conn->ctask == ctask)
388 conn->ctask = NULL;
389 /* release ref from queuecommand */
390 __iscsi_put_ctask(ctask);
391}
392
393/**
394 * iscsi_free_mgmt_task - return mgmt task back to pool
395 * @conn: iscsi connection
396 * @mtask: mtask
397 *
398 * Must be called with session lock.
399 */
400void iscsi_free_mgmt_task(struct iscsi_conn *conn,
401 struct iscsi_mgmt_task *mtask)
402{
403 list_del_init(&mtask->running);
404 if (conn->login_mtask == mtask)
405 return;
Mike Christief6d51802007-12-13 12:43:30 -0600406
407 if (conn->ping_mtask == mtask)
408 conn->ping_mtask = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600409 __kfifo_put(conn->session->mgmtpool.queue,
410 (void*)&mtask, sizeof(void*));
411}
412EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
413
Mike Christie052d0142008-05-21 15:54:05 -0500414static int iscsi_prep_mtask(struct iscsi_conn *conn,
415 struct iscsi_mgmt_task *mtask)
416{
417 struct iscsi_session *session = conn->session;
418 struct iscsi_hdr *hdr = mtask->hdr;
419 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
420
421 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
422 return -ENOTCONN;
423
424 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
425 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
426 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
427 /*
428 * pre-format CmdSN for outgoing PDU.
429 */
430 nop->cmdsn = cpu_to_be32(session->cmdsn);
431 if (hdr->itt != RESERVED_ITT) {
432 hdr->itt = build_itt(mtask->itt, session->age);
433 /*
434 * TODO: We always use immediate, so we never hit this.
435 * If we start to send tmfs or nops as non-immediate then
436 * we should start checking the cmdsn numbers for mgmt tasks.
437 */
438 if (conn->c_stage == ISCSI_CONN_STARTED &&
439 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
440 session->queued_cmdsn++;
441 session->cmdsn++;
442 }
443 }
444
445 if (session->tt->init_mgmt_task)
446 session->tt->init_mgmt_task(conn, mtask);
447
448 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
449 session->state = ISCSI_STATE_LOGGING_OUT;
450
451 list_move_tail(&mtask->running, &conn->mgmt_run_list);
452 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
453 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
454 mtask->data_count);
455 return 0;
456}
457
Mike Christief6d51802007-12-13 12:43:30 -0600458static struct iscsi_mgmt_task *
459__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
460 char *data, uint32_t data_size)
461{
462 struct iscsi_session *session = conn->session;
463 struct iscsi_mgmt_task *mtask;
464
465 if (session->state == ISCSI_STATE_TERMINATE)
466 return NULL;
467
468 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
469 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
470 /*
471 * Login and Text are sent serially, in
472 * request-followed-by-response sequence.
473 * Same mtask can be used. Same ITT must be used.
474 * Note that login_mtask is preallocated at conn_create().
475 */
476 mtask = conn->login_mtask;
477 else {
478 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
479 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
480
481 if (!__kfifo_get(session->mgmtpool.queue,
482 (void*)&mtask, sizeof(void*)))
483 return NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500484
485 if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
486 hdr->ttt == RESERVED_ITT) {
487 conn->ping_mtask = mtask;
488 conn->last_ping = jiffies;
489 }
Mike Christief6d51802007-12-13 12:43:30 -0600490 }
491
492 if (data_size) {
493 memcpy(mtask->data, data, data_size);
494 mtask->data_count = data_size;
495 } else
496 mtask->data_count = 0;
497
498 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
499 INIT_LIST_HEAD(&mtask->running);
500 list_add_tail(&mtask->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500501
502 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
503 if (iscsi_prep_mtask(conn, mtask)) {
504 iscsi_free_mgmt_task(conn, mtask);
505 return NULL;
506 }
507
508 if (session->tt->xmit_mgmt_task(conn, mtask))
509 mtask = NULL;
510
511 } else
512 scsi_queue_work(conn->session->host, &conn->xmitwork);
513
Mike Christief6d51802007-12-13 12:43:30 -0600514 return mtask;
515}
516
517int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
518 char *data, uint32_t data_size)
519{
520 struct iscsi_conn *conn = cls_conn->dd_data;
521 struct iscsi_session *session = conn->session;
522 int err = 0;
523
524 spin_lock_bh(&session->lock);
525 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
526 err = -EPERM;
527 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600528 return err;
529}
530EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
531
Mike Christie7996a772006-04-06 21:13:41 -0500532/**
533 * iscsi_cmd_rsp - SCSI Command Response processing
534 * @conn: iscsi connection
535 * @hdr: iscsi header
536 * @ctask: scsi command task
537 * @data: cmd data buffer
538 * @datalen: len of buffer
539 *
540 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
541 * then completes the command and task.
542 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500543static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
544 struct iscsi_cmd_task *ctask, char *data,
545 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500546{
Mike Christie7996a772006-04-06 21:13:41 -0500547 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
548 struct iscsi_session *session = conn->session;
549 struct scsi_cmnd *sc = ctask->sc;
550
Mike Christie77a23c22007-05-30 12:57:18 -0500551 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500552 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
553
554 sc->result = (DID_OK << 16) | rhdr->cmd_status;
555
556 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
557 sc->result = DID_ERROR << 16;
558 goto out;
559 }
560
561 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600562 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500563
564 if (datalen < 2) {
565invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600566 iscsi_conn_printk(KERN_ERR, conn,
567 "Got CHECK_CONDITION but invalid data "
568 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500569 sc->result = DID_BAD_TARGET << 16;
570 goto out;
571 }
572
Mike Christie8eb00532007-02-28 17:32:19 -0600573 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500574 if (datalen < senselen)
575 goto invalid_datalen;
576
577 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600578 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500579 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600580 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500581 }
582
Boaz Harroshc07d4442008-04-18 10:11:52 -0500583 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
584 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
585 int res_count = be32_to_cpu(rhdr->bi_residual_count);
586
587 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
588 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
589 res_count <= scsi_in(sc)->length))
590 scsi_in(sc)->resid = res_count;
591 else
592 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
593 }
594
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600595 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
596 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500597 int res_count = be32_to_cpu(rhdr->residual_count);
598
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600599 if (res_count > 0 &&
600 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
601 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500602 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900603 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500604 else
605 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500606 }
Mike Christie7996a772006-04-06 21:13:41 -0500607out:
608 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
609 (long)sc, sc->result, ctask->itt);
610 conn->scsirsp_pdus_cnt++;
611
Mike Christie60ecebf2006-08-31 18:09:25 -0400612 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500613}
614
Mike Christie7ea8b822006-07-24 15:47:22 -0500615static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
616{
617 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
618
619 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
620 conn->tmfrsp_pdus_cnt++;
621
Mike Christie843c0a82007-12-13 12:43:20 -0600622 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500623 return;
624
625 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600626 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500627 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600628 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500629 else
Mike Christie843c0a82007-12-13 12:43:20 -0600630 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500631 wake_up(&conn->ehwait);
632}
633
Mike Christief6d51802007-12-13 12:43:30 -0600634static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
635{
636 struct iscsi_nopout hdr;
637 struct iscsi_mgmt_task *mtask;
638
639 if (!rhdr && conn->ping_mtask)
640 return;
641
642 memset(&hdr, 0, sizeof(struct iscsi_nopout));
643 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
644 hdr.flags = ISCSI_FLAG_CMD_FINAL;
645
646 if (rhdr) {
647 memcpy(hdr.lun, rhdr->lun, 8);
648 hdr.ttt = rhdr->ttt;
649 hdr.itt = RESERVED_ITT;
650 } else
651 hdr.ttt = RESERVED_ITT;
652
653 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
Mike Christie052d0142008-05-21 15:54:05 -0500654 if (!mtask)
Mike Christie322d7392008-01-31 13:36:52 -0600655 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600656}
657
Mike Christie62f38302006-08-31 18:09:27 -0400658static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
659 char *data, int datalen)
660{
661 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
662 struct iscsi_hdr rejected_pdu;
663 uint32_t itt;
664
665 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
666
667 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
668 if (ntoh24(reject->dlength) > datalen)
669 return ISCSI_ERR_PROTO;
670
671 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
672 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000673 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600674 iscsi_conn_printk(KERN_ERR, conn,
675 "itt 0x%x had pdu (op 0x%x) rejected "
676 "due to DataDigest error.\n", itt,
677 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400678 }
679 }
680 return 0;
681}
682
Mike Christie7996a772006-04-06 21:13:41 -0500683/**
684 * __iscsi_complete_pdu - complete pdu
685 * @conn: iscsi conn
686 * @hdr: iscsi header
687 * @data: data buffer
688 * @datalen: len of data buffer
689 *
690 * Completes pdu processing by freeing any resources allocated at
691 * queuecommand or send generic. session lock must be held and verify
692 * itt must have been called.
693 */
Adrian Bunkf8d9d652008-01-29 00:11:27 +0200694static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
695 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500696{
697 struct iscsi_session *session = conn->session;
698 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
699 struct iscsi_cmd_task *ctask;
700 struct iscsi_mgmt_task *mtask;
701 uint32_t itt;
702
Mike Christief6d51802007-12-13 12:43:30 -0600703 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500704 rc = iscsi_verify_itt(conn, hdr->itt);
705 if (rc)
706 return rc;
707
Al Virob4377352007-02-09 16:39:40 +0000708 if (hdr->itt != RESERVED_ITT)
709 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500710 else
Al Virob4377352007-02-09 16:39:40 +0000711 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500712
713 if (itt < session->cmds_max) {
714 ctask = session->cmds[itt];
715
716 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
717 opcode, conn->id, ctask->itt, datalen);
718
719 switch(opcode) {
720 case ISCSI_OP_SCSI_CMD_RSP:
721 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500722 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
723 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500724 break;
725 case ISCSI_OP_SCSI_DATA_IN:
726 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
727 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
728 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400729 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500730 }
731 break;
732 case ISCSI_OP_R2T:
733 /* LLD handles this for now */
734 break;
735 default:
736 rc = ISCSI_ERR_BAD_OPCODE;
737 break;
738 }
739 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
740 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
741 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
742
743 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
744 opcode, conn->id, mtask->itt, datalen);
745
Mike Christie77a23c22007-05-30 12:57:18 -0500746 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500747 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500748 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500749 if (datalen) {
750 rc = ISCSI_ERR_PROTO;
751 break;
752 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500753 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
754 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500755 case ISCSI_OP_LOGIN_RSP:
756 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500757 /*
758 * login related PDU's exp_statsn is handled in
759 * userspace
760 */
Mike Christie40527af2006-07-24 15:47:45 -0500761 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
762 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600763 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500764 break;
765 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500766 if (datalen) {
767 rc = ISCSI_ERR_PROTO;
768 break;
769 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500770
Mike Christie7ea8b822006-07-24 15:47:22 -0500771 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600772 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500773 break;
774 case ISCSI_OP_NOOP_IN:
Mike Christief6d51802007-12-13 12:43:30 -0600775 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
776 datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500777 rc = ISCSI_ERR_PROTO;
778 break;
779 }
Mike Christie7996a772006-04-06 21:13:41 -0500780 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
781
Mike Christief6d51802007-12-13 12:43:30 -0600782 if (conn->ping_mtask != mtask) {
783 /*
784 * If this is not in response to one of our
785 * nops then it must be from userspace.
786 */
787 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
788 datalen))
789 rc = ISCSI_ERR_CONN_FAILED;
Mike Christiec8611f92008-05-08 20:15:34 -0500790 } else
791 mod_timer(&conn->transport_timer,
792 jiffies + conn->recv_timeout);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600793 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500794 break;
795 default:
796 rc = ISCSI_ERR_BAD_OPCODE;
797 break;
798 }
Al Virob4377352007-02-09 16:39:40 +0000799 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500800 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400801
Mike Christie7996a772006-04-06 21:13:41 -0500802 switch(opcode) {
803 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500804 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500805 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500806 break;
807 }
808
Al Virob4377352007-02-09 16:39:40 +0000809 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500810 break;
811
Mike Christief6d51802007-12-13 12:43:30 -0600812 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500813 break;
814 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400815 rc = iscsi_handle_reject(conn, hdr, data, datalen);
816 break;
Mike Christie7996a772006-04-06 21:13:41 -0500817 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500818 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400819 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
820 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500821 break;
822 default:
823 rc = ISCSI_ERR_BAD_OPCODE;
824 break;
825 }
826 } else
827 rc = ISCSI_ERR_BAD_ITT;
828
829 return rc;
830}
Mike Christie7996a772006-04-06 21:13:41 -0500831
832int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
833 char *data, int datalen)
834{
835 int rc;
836
837 spin_lock(&conn->session->lock);
838 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
839 spin_unlock(&conn->session->lock);
840 return rc;
841}
842EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
843
Mike Christie0af967f2008-05-21 15:54:04 -0500844int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500845{
846 struct iscsi_session *session = conn->session;
847 struct iscsi_cmd_task *ctask;
Mike Christie7996a772006-04-06 21:13:41 -0500848
Mike Christie0af967f2008-05-21 15:54:04 -0500849 if (itt == RESERVED_ITT)
850 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500851
Mike Christie0af967f2008-05-21 15:54:04 -0500852 if (((__force u32)itt & ISCSI_AGE_MASK) !=
853 (session->age << ISCSI_AGE_SHIFT)) {
854 iscsi_conn_printk(KERN_ERR, conn,
855 "received itt %x expected session age (%x)\n",
856 (__force u32)itt,
857 session->age & ISCSI_AGE_MASK);
858 return ISCSI_ERR_BAD_ITT;
859 }
Mike Christie7996a772006-04-06 21:13:41 -0500860
861 if (itt < session->cmds_max) {
862 ctask = session->cmds[itt];
863
864 if (!ctask->sc) {
Mike Christie322d7392008-01-31 13:36:52 -0600865 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
866 "with itt 0x%x\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500867 /* force drop */
868 return ISCSI_ERR_NO_SCSI_CMD;
869 }
870
871 if (ctask->sc->SCp.phase != session->age) {
Mike Christie322d7392008-01-31 13:36:52 -0600872 iscsi_conn_printk(KERN_ERR, conn,
873 "iscsi: ctask's session age %d, "
874 "expected %d\n", ctask->sc->SCp.phase,
875 session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500876 return ISCSI_ERR_SESSION_FAILED;
877 }
878 }
879
Mike Christie7996a772006-04-06 21:13:41 -0500880 return 0;
881}
882EXPORT_SYMBOL_GPL(iscsi_verify_itt);
883
Mike Christie0af967f2008-05-21 15:54:04 -0500884struct iscsi_cmd_task *
885iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
886{
887 struct iscsi_session *session = conn->session;
888 struct iscsi_cmd_task *ctask;
889 uint32_t i;
890
891 if (iscsi_verify_itt(conn, itt))
892 return NULL;
893
894 if (itt == RESERVED_ITT)
895 return NULL;
896
897 i = get_itt(itt);
898 if (i >= session->cmds_max)
899 return NULL;
900
901 ctask = session->cmds[i];
902 if (!ctask->sc)
903 return NULL;
904
905 if (ctask->sc->SCp.phase != session->age)
906 return NULL;
907
908 return ctask;
909}
910EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
911
Mike Christie7996a772006-04-06 21:13:41 -0500912void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
913{
914 struct iscsi_session *session = conn->session;
915 unsigned long flags;
916
917 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500918 if (session->state == ISCSI_STATE_FAILED) {
919 spin_unlock_irqrestore(&session->lock, flags);
920 return;
921 }
922
Mike Christie67a61112006-05-30 00:37:20 -0500923 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500924 session->state = ISCSI_STATE_FAILED;
925 spin_unlock_irqrestore(&session->lock, flags);
926 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
927 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
928 iscsi_conn_error(conn->cls_conn, err);
929}
930EXPORT_SYMBOL_GPL(iscsi_conn_failure);
931
Mike Christie05db8882007-02-28 17:32:16 -0600932static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400933{
Mike Christieb3a7ea82007-12-13 12:43:26 -0600934 int rc;
Mike Christieb5072ea2006-10-16 18:09:42 -0400935
Mike Christie77a23c22007-05-30 12:57:18 -0500936 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400937 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500938 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400939 if (rc)
940 return rc;
Mike Christie05db8882007-02-28 17:32:16 -0600941 /* done with this in-progress mtask */
942 conn->mtask = NULL;
Mike Christieb5072ea2006-10-16 18:09:42 -0400943 return 0;
944}
945
Mike Christie77a23c22007-05-30 12:57:18 -0500946static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
947{
948 struct iscsi_session *session = conn->session;
949
950 /*
951 * Check for iSCSI window and take care of CmdSN wrap-around
952 */
Mike Christiee0726402007-07-26 12:46:48 -0500953 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
954 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
955 "CmdSN %u/%u\n", session->exp_cmdsn,
956 session->max_cmdsn, session->cmdsn,
957 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500958 return -ENOSPC;
959 }
960 return 0;
961}
962
963static int iscsi_xmit_ctask(struct iscsi_conn *conn)
964{
965 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600966 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500967
968 __iscsi_get_ctask(ctask);
969 spin_unlock_bh(&conn->session->lock);
970 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
971 spin_lock_bh(&conn->session->lock);
972 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500973 if (!rc)
974 /* done with this ctask */
975 conn->ctask = NULL;
976 return rc;
977}
978
Mike Christie7996a772006-04-06 21:13:41 -0500979/**
Mike Christie843c0a82007-12-13 12:43:20 -0600980 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
981 * @ctask: ctask to requeue
982 *
983 * LLDs that need to run a ctask from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -0500984 * this. The session lock must be held. This should only be called
985 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -0600986 */
987void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
988{
989 struct iscsi_conn *conn = ctask->conn;
990
991 list_move_tail(&ctask->running, &conn->requeue);
992 scsi_queue_work(conn->session->host, &conn->xmitwork);
993}
994EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
995
996/**
Mike Christie7996a772006-04-06 21:13:41 -0500997 * iscsi_data_xmit - xmit any command into the scheduled connection
998 * @conn: iscsi connection
999 *
1000 * Notes:
1001 * The function can return -EAGAIN in which case the caller must
1002 * re-schedule it again later or recover. '0' return code means
1003 * successful xmit.
1004 **/
1005static int iscsi_data_xmit(struct iscsi_conn *conn)
1006{
Mike Christie3219e522006-05-30 00:37:28 -05001007 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001008
Mike Christie77a23c22007-05-30 12:57:18 -05001009 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001010 if (unlikely(conn->suspend_tx)) {
1011 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001012 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001013 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001014 }
Mike Christie7996a772006-04-06 21:13:41 -05001015
1016 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -05001017 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001018 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001019 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001020 }
Mike Christie77a23c22007-05-30 12:57:18 -05001021
Mike Christie7996a772006-04-06 21:13:41 -05001022 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -06001023 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001024 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001025 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001026 }
1027
Mike Christie77a23c22007-05-30 12:57:18 -05001028 /*
1029 * process mgmt pdus like nops before commands since we should
1030 * only have one nop-out as a ping from us and targets should not
1031 * overflow us with nop-ins
1032 */
1033check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001034 while (!list_empty(&conn->mgmtqueue)) {
1035 conn->mtask = list_entry(conn->mgmtqueue.next,
1036 struct iscsi_mgmt_task, running);
Mike Christie052d0142008-05-21 15:54:05 -05001037 if (iscsi_prep_mtask(conn, conn->mtask)) {
Mike Christieb3a7ea82007-12-13 12:43:26 -06001038 iscsi_free_mgmt_task(conn, conn->mtask);
1039 conn->mtask = NULL;
1040 continue;
1041 }
Mike Christie77a23c22007-05-30 12:57:18 -05001042 rc = iscsi_xmit_mtask(conn);
1043 if (rc)
1044 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001045 }
1046
Mike Christie843c0a82007-12-13 12:43:20 -06001047 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001048 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001049 if (conn->tmf_state == TMF_QUEUED)
1050 break;
1051
Mike Christieb6c395e2006-07-24 15:47:15 -05001052 conn->ctask = list_entry(conn->xmitqueue.next,
1053 struct iscsi_cmd_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001054 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9000bcd2007-12-13 12:43:32 -06001055 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001056 continue;
1057 }
Boaz Harrosh004d6532007-12-13 12:43:23 -06001058 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1059 fail_command(conn, conn->ctask, DID_ABORT << 16);
1060 continue;
1061 }
Mike Christie77a23c22007-05-30 12:57:18 -05001062 rc = iscsi_xmit_ctask(conn);
1063 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001064 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001065 /*
1066 * we could continuously get new ctask requests so
1067 * we need to check the mgmt queue for nops that need to
1068 * be sent to aviod starvation
1069 */
Mike Christie843c0a82007-12-13 12:43:20 -06001070 if (!list_empty(&conn->mgmtqueue))
1071 goto check_mgmt;
1072 }
1073
1074 while (!list_empty(&conn->requeue)) {
1075 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1076 break;
1077
Mike Christieb3a7ea82007-12-13 12:43:26 -06001078 /*
1079 * we always do fastlogout - conn stop code will clean up.
1080 */
1081 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1082 break;
1083
Mike Christie843c0a82007-12-13 12:43:20 -06001084 conn->ctask = list_entry(conn->requeue.next,
1085 struct iscsi_cmd_task, running);
1086 conn->ctask->state = ISCSI_TASK_RUNNING;
1087 list_move_tail(conn->requeue.next, &conn->run_list);
1088 rc = iscsi_xmit_ctask(conn);
1089 if (rc)
1090 goto again;
1091 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001092 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001093 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001094 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001095 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001096
1097again:
1098 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001099 rc = -ENODATA;
1100 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001101 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001102}
1103
David Howellsc4028952006-11-22 14:57:56 +00001104static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001105{
David Howellsc4028952006-11-22 14:57:56 +00001106 struct iscsi_conn *conn =
1107 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001108 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001109 /*
1110 * serialize Xmit worker on a per-connection basis.
1111 */
Mike Christie3219e522006-05-30 00:37:28 -05001112 do {
1113 rc = iscsi_data_xmit(conn);
1114 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001115}
1116
1117enum {
1118 FAILURE_BAD_HOST = 1,
1119 FAILURE_SESSION_FAILED,
1120 FAILURE_SESSION_FREED,
1121 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001122 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001123 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001124 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001125 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001126 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001127 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001128};
1129
1130int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1131{
Mike Christie75613522008-05-21 15:53:59 -05001132 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001133 struct Scsi_Host *host;
1134 int reason = 0;
1135 struct iscsi_session *session;
1136 struct iscsi_conn *conn;
1137 struct iscsi_cmd_task *ctask = NULL;
1138
1139 sc->scsi_done = done;
1140 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001141 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001142
1143 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001144 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001145
Mike Christie75613522008-05-21 15:53:59 -05001146 cls_session = starget_to_session(scsi_target(sc->device));
1147 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001148 spin_lock(&session->lock);
1149
Mike Christie75613522008-05-21 15:53:59 -05001150 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001151 if (reason) {
1152 sc->result = reason;
1153 goto fault;
1154 }
1155
Mike Christie656cffc2006-05-18 20:31:42 -05001156 /*
1157 * ISCSI_STATE_FAILED is a temp. state. The recovery
1158 * code will decide what is best to do with command queued
1159 * during this time
1160 */
1161 if (session->state != ISCSI_STATE_LOGGED_IN &&
1162 session->state != ISCSI_STATE_FAILED) {
1163 /*
1164 * to handle the race between when we set the recovery state
1165 * and block the session we requeue here (commands could
1166 * be entering our queuecommand while a block is starting
1167 * up because the block code is not locked)
1168 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001169 switch (session->state) {
1170 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001171 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001172 sc->result = DID_IMM_RETRY << 16;
1173 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001174 case ISCSI_STATE_LOGGING_OUT:
1175 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001176 sc->result = DID_IMM_RETRY << 16;
1177 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001178 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001179 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001180 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001181 break;
1182 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001183 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001184 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001185 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001186 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001187 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001188 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001189 }
Mike Christie7996a772006-04-06 21:13:41 -05001190 goto fault;
1191 }
1192
Mike Christie7996a772006-04-06 21:13:41 -05001193 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001194 if (!conn) {
1195 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001196 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001197 goto fault;
1198 }
Mike Christie7996a772006-04-06 21:13:41 -05001199
Mike Christie77a23c22007-05-30 12:57:18 -05001200 if (iscsi_check_cmdsn_window_closed(conn)) {
1201 reason = FAILURE_WINDOW_CLOSED;
1202 goto reject;
1203 }
1204
Mike Christie60ecebf2006-08-31 18:09:25 -04001205 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1206 sizeof(void*))) {
1207 reason = FAILURE_OOM;
1208 goto reject;
1209 }
Mike Christie7996a772006-04-06 21:13:41 -05001210 sc->SCp.phase = session->age;
1211 sc->SCp.ptr = (char *)ctask;
1212
Mike Christie60ecebf2006-08-31 18:09:25 -04001213 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -05001214 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -05001215 ctask->conn = conn;
1216 ctask->sc = sc;
1217 INIT_LIST_HEAD(&ctask->running);
Mike Christieb6c395e2006-07-24 15:47:15 -05001218 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001219
Mike Christie052d0142008-05-21 15:54:05 -05001220 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
1221 if (iscsi_prep_scsi_cmd_pdu(ctask)) {
1222 sc->result = DID_ABORT << 16;
1223 sc->scsi_done = NULL;
1224 iscsi_complete_command(ctask);
1225 goto fault;
1226 }
1227 if (session->tt->xmit_cmd_task(conn, ctask)) {
1228 sc->scsi_done = NULL;
1229 iscsi_complete_command(ctask);
1230 reason = FAILURE_SESSION_NOT_READY;
1231 goto reject;
1232 }
1233 } else
1234 scsi_queue_work(session->host, &conn->xmitwork);
1235
1236 session->queued_cmdsn++;
1237 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001238 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001239 return 0;
1240
1241reject:
1242 spin_unlock(&session->lock);
1243 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001244 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001245 return SCSI_MLQUEUE_HOST_BUSY;
1246
1247fault:
1248 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001249 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001250 if (!scsi_bidi_cmnd(sc))
1251 scsi_set_resid(sc, scsi_bufflen(sc));
1252 else {
1253 scsi_out(sc)->resid = scsi_out(sc)->length;
1254 scsi_in(sc)->resid = scsi_in(sc)->length;
1255 }
Mike Christie052d0142008-05-21 15:54:05 -05001256 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001257 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001258 return 0;
1259}
1260EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1261
1262int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1263{
1264 if (depth > ISCSI_MAX_CMD_PER_LUN)
1265 depth = ISCSI_MAX_CMD_PER_LUN;
1266 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1267 return sdev->queue_depth;
1268}
1269EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1270
Mike Christie7996a772006-04-06 21:13:41 -05001271void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1272{
Mike Christie75613522008-05-21 15:53:59 -05001273 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001274
1275 spin_lock_bh(&session->lock);
1276 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001277 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001278 if (session->leadconn)
1279 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001280 }
1281 spin_unlock_bh(&session->lock);
1282}
1283EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1284
1285int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1286{
Mike Christie75613522008-05-21 15:53:59 -05001287 struct iscsi_cls_session *cls_session;
1288 struct iscsi_session *session;
1289 struct iscsi_conn *conn;
1290
1291 cls_session = starget_to_session(scsi_target(sc->device));
1292 session = cls_session->dd_data;
1293 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001294
Mike Christiebc436b22007-12-13 12:43:28 -06001295 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001296 spin_lock_bh(&session->lock);
1297 if (session->state == ISCSI_STATE_TERMINATE) {
1298failed:
1299 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001300 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001301 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001302 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001303 return FAILED;
1304 }
1305
Mike Christie7996a772006-04-06 21:13:41 -05001306 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001307 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001308 /*
1309 * we drop the lock here but the leadconn cannot be destoyed while
1310 * we are in the scsi eh
1311 */
Mike Christie843c0a82007-12-13 12:43:20 -06001312 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001313
1314 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1315 wait_event_interruptible(conn->ehwait,
1316 session->state == ISCSI_STATE_TERMINATE ||
1317 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001318 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001319 if (signal_pending(current))
1320 flush_signals(current);
1321
Mike Christiebc436b22007-12-13 12:43:28 -06001322 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001323 spin_lock_bh(&session->lock);
1324 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001325 iscsi_session_printk(KERN_INFO, session,
1326 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001327 else
1328 goto failed;
1329 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001330 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001331 return SUCCESS;
1332}
1333EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1334
Mike Christie843c0a82007-12-13 12:43:20 -06001335static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001336{
Mike Christie843c0a82007-12-13 12:43:20 -06001337 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001338 struct iscsi_session *session = conn->session;
1339
1340 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001341 if (conn->tmf_state == TMF_QUEUED) {
1342 conn->tmf_state = TMF_TIMEDOUT;
1343 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001344 /* unblock eh_abort() */
1345 wake_up(&conn->ehwait);
1346 }
1347 spin_unlock(&session->lock);
1348}
1349
Mike Christie843c0a82007-12-13 12:43:20 -06001350static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001351 struct iscsi_tm *hdr, int age,
1352 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001353{
Mike Christie7996a772006-04-06 21:13:41 -05001354 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001355 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001356
Mike Christie843c0a82007-12-13 12:43:20 -06001357 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1358 NULL, 0);
1359 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001360 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001361 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001362 spin_lock_bh(&session->lock);
1363 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001364 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001365 }
Mike Christie843c0a82007-12-13 12:43:20 -06001366 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001367 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001368 conn->tmf_timer.function = iscsi_tmf_timedout;
1369 conn->tmf_timer.data = (unsigned long)conn;
1370 add_timer(&conn->tmf_timer);
1371 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001372
Mike Christie7996a772006-04-06 21:13:41 -05001373 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001374 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001375
1376 /*
1377 * block eh thread until:
1378 *
Mike Christie843c0a82007-12-13 12:43:20 -06001379 * 1) tmf response
1380 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001381 * 3) session is terminated or restarted or userspace has
1382 * given up on recovery
1383 */
Mike Christie843c0a82007-12-13 12:43:20 -06001384 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001385 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001386 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001387 if (signal_pending(current))
1388 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001389 del_timer_sync(&conn->tmf_timer);
1390
Mike Christie6724add2007-08-15 01:38:30 -05001391 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001392 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001393 /* if the session drops it will clean up the mtask */
1394 if (age != session->age ||
1395 session->state != ISCSI_STATE_LOGGED_IN)
1396 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001397 return 0;
1398}
1399
1400/*
Mike Christie843c0a82007-12-13 12:43:20 -06001401 * Fail commands. session lock held and recv side suspended and xmit
1402 * thread flushed
1403 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001404static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1405 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001406{
1407 struct iscsi_cmd_task *ctask, *tmp;
1408
1409 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1410 conn->ctask = NULL;
1411
1412 /* flush pending */
1413 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1414 if (lun == ctask->sc->device->lun || lun == -1) {
1415 debug_scsi("failing pending sc %p itt 0x%x\n",
1416 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001417 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001418 }
1419 }
1420
1421 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1422 if (lun == ctask->sc->device->lun || lun == -1) {
1423 debug_scsi("failing requeued sc %p itt 0x%x\n",
1424 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001425 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001426 }
1427 }
1428
1429 /* fail all other running */
1430 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1431 if (lun == ctask->sc->device->lun || lun == -1) {
1432 debug_scsi("failing in progress sc %p itt 0x%x\n",
1433 ctask->sc, ctask->itt);
1434 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1435 }
1436 }
1437}
1438
Mike Christieb40977d2008-05-21 15:54:03 -05001439void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001440{
1441 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001442 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1443 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001444}
Mike Christieb40977d2008-05-21 15:54:03 -05001445EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001446
1447static void iscsi_start_tx(struct iscsi_conn *conn)
1448{
1449 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001450 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1451 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001452}
1453
Mike Christief6d51802007-12-13 12:43:30 -06001454static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1455{
1456 struct iscsi_cls_session *cls_session;
1457 struct iscsi_session *session;
1458 struct iscsi_conn *conn;
1459 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1460
1461 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001462 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001463
1464 debug_scsi("scsi cmd %p timedout\n", scmd);
1465
1466 spin_lock(&session->lock);
1467 if (session->state != ISCSI_STATE_LOGGED_IN) {
1468 /*
1469 * We are probably in the middle of iscsi recovery so let
1470 * that complete and handle the error.
1471 */
1472 rc = EH_RESET_TIMER;
1473 goto done;
1474 }
1475
1476 conn = session->leadconn;
1477 if (!conn) {
1478 /* In the middle of shuting down */
1479 rc = EH_RESET_TIMER;
1480 goto done;
1481 }
1482
1483 if (!conn->recv_timeout && !conn->ping_timeout)
1484 goto done;
1485 /*
1486 * if the ping timedout then we are in the middle of cleaning up
1487 * and can let the iscsi eh handle it
1488 */
1489 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1490 (conn->ping_timeout * HZ), jiffies))
1491 rc = EH_RESET_TIMER;
1492 /*
1493 * if we are about to check the transport then give the command
1494 * more time
1495 */
1496 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1497 jiffies))
1498 rc = EH_RESET_TIMER;
1499 /* if in the middle of checking the transport then give us more time */
1500 if (conn->ping_mtask)
1501 rc = EH_RESET_TIMER;
1502done:
1503 spin_unlock(&session->lock);
1504 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1505 return rc;
1506}
1507
1508static void iscsi_check_transport_timeouts(unsigned long data)
1509{
1510 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1511 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001512 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001513
1514 spin_lock(&session->lock);
1515 if (session->state != ISCSI_STATE_LOGGED_IN)
1516 goto done;
1517
Mike Christie4cf10432008-05-07 20:43:52 -05001518 recv_timeout = conn->recv_timeout;
1519 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001520 goto done;
1521
Mike Christie4cf10432008-05-07 20:43:52 -05001522 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001523 last_recv = conn->last_recv;
Mike Christie4cf10432008-05-07 20:43:52 -05001524 if (conn->ping_mtask &&
1525 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001526 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001527 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1528 "expired, last rx %lu, last ping %lu, "
1529 "now %lu\n", conn->ping_timeout, last_recv,
1530 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001531 spin_unlock(&session->lock);
1532 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1533 return;
1534 }
1535
Mike Christie4cf10432008-05-07 20:43:52 -05001536 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001537 /* send a ping to try to provoke some traffic */
1538 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1539 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001540 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001541 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001542 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001543
Mike Christiead294e92008-01-31 13:36:50 -06001544 debug_scsi("Setting next tmo %lu\n", next_timeout);
1545 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001546done:
1547 spin_unlock(&session->lock);
1548}
1549
Mike Christie843c0a82007-12-13 12:43:20 -06001550static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1551 struct iscsi_tm *hdr)
1552{
1553 memset(hdr, 0, sizeof(*hdr));
1554 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1555 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1556 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1557 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1558 hdr->rtt = ctask->hdr->itt;
1559 hdr->refcmdsn = ctask->hdr->cmdsn;
1560}
1561
Mike Christie7996a772006-04-06 21:13:41 -05001562int iscsi_eh_abort(struct scsi_cmnd *sc)
1563{
Mike Christie75613522008-05-21 15:53:59 -05001564 struct iscsi_cls_session *cls_session;
1565 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001566 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001567 struct iscsi_cmd_task *ctask;
1568 struct iscsi_tm *hdr;
1569 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001570
Mike Christie75613522008-05-21 15:53:59 -05001571 cls_session = starget_to_session(scsi_target(sc->device));
1572 session = cls_session->dd_data;
1573
Mike Christie6724add2007-08-15 01:38:30 -05001574 mutex_lock(&session->eh_mutex);
1575 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001576 /*
1577 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1578 * got the command.
1579 */
1580 if (!sc->SCp.ptr) {
1581 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001582 spin_unlock_bh(&session->lock);
1583 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001584 return SUCCESS;
1585 }
1586
Mike Christie7996a772006-04-06 21:13:41 -05001587 /*
1588 * If we are not logged in or we have started a new session
1589 * then let the host reset code handle this
1590 */
Mike Christie843c0a82007-12-13 12:43:20 -06001591 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1592 sc->SCp.phase != session->age) {
1593 spin_unlock_bh(&session->lock);
1594 mutex_unlock(&session->eh_mutex);
1595 return FAILED;
1596 }
1597
1598 conn = session->leadconn;
1599 conn->eh_abort_cnt++;
1600 age = session->age;
1601
1602 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1603 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001604
1605 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001606 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001607 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001608 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001609 }
Mike Christie7996a772006-04-06 21:13:41 -05001610
Mike Christie77a23c22007-05-30 12:57:18 -05001611 if (ctask->state == ISCSI_TASK_PENDING) {
1612 fail_command(conn, ctask, DID_ABORT << 16);
1613 goto success;
1614 }
Mike Christie7996a772006-04-06 21:13:41 -05001615
Mike Christie843c0a82007-12-13 12:43:20 -06001616 /* only have one tmf outstanding at a time */
1617 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001618 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001619 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001620
Mike Christie843c0a82007-12-13 12:43:20 -06001621 hdr = &conn->tmhdr;
1622 iscsi_prep_abort_task_pdu(ctask, hdr);
1623
Mike Christief6d51802007-12-13 12:43:30 -06001624 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001625 rc = FAILED;
1626 goto failed;
1627 }
1628
1629 switch (conn->tmf_state) {
1630 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001631 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001632 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001633 /*
1634 * clean up task if aborted. grab the recv lock as a writer
1635 */
1636 write_lock_bh(conn->recv_lock);
1637 spin_lock(&session->lock);
1638 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001639 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001640 spin_unlock(&session->lock);
1641 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001642 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001643 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001644 case TMF_TIMEDOUT:
1645 spin_unlock_bh(&session->lock);
1646 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1647 goto failed_unlocked;
1648 case TMF_NOT_FOUND:
1649 if (!sc->SCp.ptr) {
1650 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001651 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001652 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001653 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001654 }
1655 /* fall through */
1656 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001657 conn->tmf_state = TMF_INITIAL;
1658 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001659 }
1660
Mike Christie77a23c22007-05-30 12:57:18 -05001661success:
Mike Christie7996a772006-04-06 21:13:41 -05001662 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001663success_unlocked:
1664 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001665 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001666 return SUCCESS;
1667
1668failed:
1669 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001670failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001671 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1672 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001673 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001674 return FAILED;
1675}
1676EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1677
Mike Christie843c0a82007-12-13 12:43:20 -06001678static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1679{
1680 memset(hdr, 0, sizeof(*hdr));
1681 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1682 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1683 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1684 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001685 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001686}
1687
1688int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1689{
Mike Christie75613522008-05-21 15:53:59 -05001690 struct iscsi_cls_session *cls_session;
1691 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001692 struct iscsi_conn *conn;
1693 struct iscsi_tm *hdr;
1694 int rc = FAILED;
1695
Mike Christie75613522008-05-21 15:53:59 -05001696 cls_session = starget_to_session(scsi_target(sc->device));
1697 session = cls_session->dd_data;
1698
Mike Christie843c0a82007-12-13 12:43:20 -06001699 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1700
1701 mutex_lock(&session->eh_mutex);
1702 spin_lock_bh(&session->lock);
1703 /*
1704 * Just check if we are not logged in. We cannot check for
1705 * the phase because the reset could come from a ioctl.
1706 */
1707 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1708 goto unlock;
1709 conn = session->leadconn;
1710
1711 /* only have one tmf outstanding at a time */
1712 if (conn->tmf_state != TMF_INITIAL)
1713 goto unlock;
1714 conn->tmf_state = TMF_QUEUED;
1715
1716 hdr = &conn->tmhdr;
1717 iscsi_prep_lun_reset_pdu(sc, hdr);
1718
Mike Christief6d51802007-12-13 12:43:30 -06001719 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1720 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001721 rc = FAILED;
1722 goto unlock;
1723 }
1724
1725 switch (conn->tmf_state) {
1726 case TMF_SUCCESS:
1727 break;
1728 case TMF_TIMEDOUT:
1729 spin_unlock_bh(&session->lock);
1730 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1731 goto done;
1732 default:
1733 conn->tmf_state = TMF_INITIAL;
1734 goto unlock;
1735 }
1736
1737 rc = SUCCESS;
1738 spin_unlock_bh(&session->lock);
1739
1740 iscsi_suspend_tx(conn);
1741 /* need to grab the recv lock then session lock */
1742 write_lock_bh(conn->recv_lock);
1743 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001744 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001745 conn->tmf_state = TMF_INITIAL;
1746 spin_unlock(&session->lock);
1747 write_unlock_bh(conn->recv_lock);
1748
1749 iscsi_start_tx(conn);
1750 goto done;
1751
1752unlock:
1753 spin_unlock_bh(&session->lock);
1754done:
1755 debug_scsi("iscsi_eh_device_reset %s\n",
1756 rc == SUCCESS ? "SUCCESS" : "FAILED");
1757 mutex_unlock(&session->eh_mutex);
1758 return rc;
1759}
1760EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1761
Olaf Kirch63203772007-12-13 12:43:25 -06001762/*
1763 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1764 * should be accessed via kfifo_{get,put} on q->queue.
1765 * Optionally, the caller can obtain the array of object pointers
1766 * by passing in a non-NULL @items pointer
1767 */
Mike Christie7996a772006-04-06 21:13:41 -05001768int
Olaf Kirch63203772007-12-13 12:43:25 -06001769iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001770{
Olaf Kirch63203772007-12-13 12:43:25 -06001771 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001772
Olaf Kirch63203772007-12-13 12:43:25 -06001773 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001774
1775 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001776
1777 /* If the user passed an items pointer, he wants a copy of
1778 * the array. */
1779 if (items)
1780 num_arrays++;
1781 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1782 if (q->pool == NULL)
1783 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001784
1785 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1786 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001787 if (q->queue == ERR_PTR(-ENOMEM))
1788 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001789
1790 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001791 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001792 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001793 q->max = i;
1794 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001795 }
Mike Christie7996a772006-04-06 21:13:41 -05001796 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1797 }
Olaf Kirch63203772007-12-13 12:43:25 -06001798
1799 if (items) {
1800 *items = q->pool + max;
1801 memcpy(*items, q->pool, max * sizeof(void *));
1802 }
1803
Mike Christie7996a772006-04-06 21:13:41 -05001804 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001805
1806enomem:
1807 iscsi_pool_free(q);
1808 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001809}
1810EXPORT_SYMBOL_GPL(iscsi_pool_init);
1811
Olaf Kirch63203772007-12-13 12:43:25 -06001812void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001813{
1814 int i;
1815
1816 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001817 kfree(q->pool[i]);
1818 if (q->pool)
1819 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001820}
1821EXPORT_SYMBOL_GPL(iscsi_pool_free);
1822
Mike Christiea4804cd2008-05-21 15:54:00 -05001823/**
1824 * iscsi_host_add - add host to system
1825 * @shost: scsi host
1826 * @pdev: parent device
1827 *
1828 * This should be called by partial offload and software iscsi drivers
1829 * to add a host to the system.
1830 */
1831int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001832{
Mike Christiea4804cd2008-05-21 15:54:00 -05001833 return scsi_add_host(shost, pdev);
1834}
1835EXPORT_SYMBOL_GPL(iscsi_host_add);
1836
1837/**
1838 * iscsi_host_alloc - allocate a host and driver data
1839 * @sht: scsi host template
1840 * @dd_data_size: driver host data size
1841 * @qdepth: default device queue depth
1842 *
1843 * This should be called by partial offload and software iscsi drivers.
1844 * To access the driver specific memory use the iscsi_host_priv() macro.
1845 */
1846struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1847 int dd_data_size, uint16_t qdepth)
1848{
1849 struct Scsi_Host *shost;
1850
1851 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1852 if (!shost)
1853 return NULL;
1854 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1855
Mike Christie15482712007-05-30 12:57:19 -05001856 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1857 if (qdepth != 0)
1858 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001859 "Queue depth must be between 1 and %d.\n",
1860 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001861 qdepth = ISCSI_DEF_CMD_PER_LUN;
1862 }
Mike Christie75613522008-05-21 15:53:59 -05001863 shost->cmd_per_lun = qdepth;
Mike Christiea4804cd2008-05-21 15:54:00 -05001864 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001865}
Mike Christiea4804cd2008-05-21 15:54:00 -05001866EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001867
Mike Christiea4804cd2008-05-21 15:54:00 -05001868/**
1869 * iscsi_host_remove - remove host and sessions
1870 * @shost: scsi host
1871 *
1872 * This will also remove any sessions attached to the host, but if userspace
1873 * is managing the session at the same time this will break. TODO: add
1874 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1875 * does not remove the memory from under us.
1876 */
1877void iscsi_host_remove(struct Scsi_Host *shost)
1878{
1879 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1880 scsi_remove_host(shost);
1881}
1882EXPORT_SYMBOL_GPL(iscsi_host_remove);
1883
1884void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001885{
1886 struct iscsi_host *ihost = shost_priv(shost);
1887
1888 kfree(ihost->netdev);
1889 kfree(ihost->hwaddress);
1890 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001891 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001892}
Mike Christiea4804cd2008-05-21 15:54:00 -05001893EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05001894
1895/**
1896 * iscsi_session_setup - create iscsi cls session and host and session
1897 * @iscsit: iscsi transport template
1898 * @shost: scsi host
1899 * @cmds_max: session can queue
1900 * @cmd_task_size: LLD ctask private data size
1901 * @mgmt_task_size: LLD mtask private data size
1902 * @initial_cmdsn: initial CmdSN
1903 *
1904 * This can be used by software iscsi_transports that allocate
1905 * a session per scsi host.
1906 */
1907struct iscsi_cls_session *
1908iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
1909 uint16_t cmds_max, int cmd_task_size, int mgmt_task_size,
1910 uint32_t initial_cmdsn)
1911{
1912 struct iscsi_session *session;
1913 struct iscsi_cls_session *cls_session;
1914 int cmd_i;
1915
Mike Christie31ed0bf2008-02-26 12:35:23 -06001916 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1917 cmds_max < 2) {
Mike Christie15482712007-05-30 12:57:19 -05001918 if (cmds_max != 0)
1919 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1920 "can_queue must be a power of 2 and between "
1921 "2 and %d - setting to %d.\n", cmds_max,
1922 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1923 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1924 }
1925
Mike Christie5d91e202008-05-21 15:54:01 -05001926 cls_session = iscsi_alloc_session(shost, iscsit,
1927 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05001928 if (!cls_session)
Mike Christie7996a772006-04-06 21:13:41 -05001929 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001930 session = cls_session->dd_data;
1931 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001932 session->host = shost;
1933 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001934 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001935 session->lu_reset_timeout = 15;
1936 session->abort_timeout = 10;
Mike Christie7996a772006-04-06 21:13:41 -05001937 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001938 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001939 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001940 session->exp_cmdsn = initial_cmdsn + 1;
1941 session->max_cmdsn = initial_cmdsn + 1;
1942 session->max_r2t = 1;
1943 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001944 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05001945 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001946
1947 /* initialize SCSI PDU commands pool */
1948 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1949 (void***)&session->cmds,
1950 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1951 goto cmdpool_alloc_fail;
1952
1953 /* pre-format cmds pool with ITT */
1954 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1955 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1956
1957 if (cmd_task_size)
1958 ctask->dd_data = &ctask[1];
1959 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001960 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001961 }
1962
Mike Christie7996a772006-04-06 21:13:41 -05001963 /* initialize immediate command pool */
1964 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1965 (void***)&session->mgmt_cmds,
1966 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1967 goto mgmtpool_alloc_fail;
1968
1969
1970 /* pre-format immediate cmds pool with ITT */
1971 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1972 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1973
1974 if (mgmt_task_size)
1975 mtask->dd_data = &mtask[1];
1976 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001977 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001978 }
1979
Mike Christief53a88d2006-06-28 12:00:27 -05001980 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05001981 goto module_get_fail;
1982
1983 if (iscsi_add_session(cls_session, 0))
Mike Christief53a88d2006-06-28 12:00:27 -05001984 goto cls_session_fail;
Mike Christie7996a772006-04-06 21:13:41 -05001985 return cls_session;
1986
1987cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05001988 module_put(iscsit->owner);
1989module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001990 iscsi_pool_free(&session->mgmtpool);
Mike Christie7996a772006-04-06 21:13:41 -05001991mgmtpool_alloc_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001992 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001993cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05001994 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001995 return NULL;
1996}
1997EXPORT_SYMBOL_GPL(iscsi_session_setup);
1998
1999/**
2000 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002001 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002002 *
Mike Christie75613522008-05-21 15:53:59 -05002003 * The driver must have called iscsi_remove_session before
2004 * calling this.
2005 */
Mike Christie7996a772006-04-06 21:13:41 -05002006void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2007{
Mike Christie75613522008-05-21 15:53:59 -05002008 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002009 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05002010
Olaf Kirch63203772007-12-13 12:43:25 -06002011 iscsi_pool_free(&session->mgmtpool);
2012 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002013
Mike Christieb2c64162007-05-30 12:57:16 -05002014 kfree(session->password);
2015 kfree(session->password_in);
2016 kfree(session->username);
2017 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002018 kfree(session->targetname);
2019
Mike Christie75613522008-05-21 15:53:59 -05002020 iscsi_destroy_session(cls_session);
Mike Christie63f75cc2006-07-24 15:47:29 -05002021 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002022}
2023EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2024
2025/**
2026 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2027 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002028 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002029 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002030 */
Mike Christie7996a772006-04-06 21:13:41 -05002031struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002032iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2033 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002034{
Mike Christie75613522008-05-21 15:53:59 -05002035 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002036 struct iscsi_conn *conn;
2037 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002038 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002039
Mike Christie5d91e202008-05-21 15:54:01 -05002040 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2041 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002042 if (!cls_conn)
2043 return NULL;
2044 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002045 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002046
Mike Christie5d91e202008-05-21 15:54:01 -05002047 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002048 conn->session = session;
2049 conn->cls_conn = cls_conn;
2050 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2051 conn->id = conn_idx;
2052 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002053 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002054
2055 init_timer(&conn->transport_timer);
2056 conn->transport_timer.data = (unsigned long)conn;
2057 conn->transport_timer.function = iscsi_check_transport_timeouts;
2058
Mike Christie7996a772006-04-06 21:13:41 -05002059 INIT_LIST_HEAD(&conn->run_list);
2060 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002061 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002062 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002063 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002064 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002065
2066 /* allocate login_mtask used for the login/text sequences */
2067 spin_lock_bh(&session->lock);
2068 if (!__kfifo_get(session->mgmtpool.queue,
2069 (void*)&conn->login_mtask,
2070 sizeof(void*))) {
2071 spin_unlock_bh(&session->lock);
2072 goto login_mtask_alloc_fail;
2073 }
2074 spin_unlock_bh(&session->lock);
2075
Mike Christiebf32ed32007-02-28 17:32:17 -06002076 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002077 if (!data)
2078 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05002079 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002080
Mike Christie843c0a82007-12-13 12:43:20 -06002081 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002082 init_waitqueue_head(&conn->ehwait);
2083
2084 return cls_conn;
2085
Mike Christied36ab6f2006-05-18 20:31:34 -05002086login_mtask_data_alloc_fail:
2087 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2088 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002089login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002090 iscsi_destroy_conn(cls_conn);
2091 return NULL;
2092}
2093EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2094
2095/**
2096 * iscsi_conn_teardown - teardown iscsi connection
2097 * cls_conn: iscsi class connection
2098 *
2099 * TODO: we may need to make this into a two step process
2100 * like scsi-mls remove + put host
2101 */
2102void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2103{
2104 struct iscsi_conn *conn = cls_conn->dd_data;
2105 struct iscsi_session *session = conn->session;
2106 unsigned long flags;
2107
Mike Christief6d51802007-12-13 12:43:30 -06002108 del_timer_sync(&conn->transport_timer);
2109
Mike Christie7996a772006-04-06 21:13:41 -05002110 spin_lock_bh(&session->lock);
2111 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2112 if (session->leadconn == conn) {
2113 /*
2114 * leading connection? then give up on recovery.
2115 */
2116 session->state = ISCSI_STATE_TERMINATE;
2117 wake_up(&conn->ehwait);
2118 }
2119 spin_unlock_bh(&session->lock);
2120
Mike Christie7996a772006-04-06 21:13:41 -05002121 /*
2122 * Block until all in-progress commands for this connection
2123 * time out or fail.
2124 */
2125 for (;;) {
2126 spin_lock_irqsave(session->host->host_lock, flags);
2127 if (!session->host->host_busy) { /* OK for ERL == 0 */
2128 spin_unlock_irqrestore(session->host->host_lock, flags);
2129 break;
2130 }
2131 spin_unlock_irqrestore(session->host->host_lock, flags);
2132 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002133 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2134 "host_busy %d host_failed %d\n",
2135 session->host->host_busy,
2136 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002137 /*
2138 * force eh_abort() to unblock
2139 */
2140 wake_up(&conn->ehwait);
2141 }
2142
Mike Christie779ea122007-02-28 17:32:15 -06002143 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002144 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002145
Mike Christie7996a772006-04-06 21:13:41 -05002146 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002147 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002148 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05002149 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2150 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002151 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002152 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002153 spin_unlock_bh(&session->lock);
2154
Mike Christie7996a772006-04-06 21:13:41 -05002155 iscsi_destroy_conn(cls_conn);
2156}
2157EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2158
2159int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2160{
2161 struct iscsi_conn *conn = cls_conn->dd_data;
2162 struct iscsi_session *session = conn->session;
2163
Mike Christieffd04362006-08-31 18:09:24 -04002164 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002165 iscsi_conn_printk(KERN_ERR, conn,
2166 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002167 return -EPERM;
2168 }
2169
Mike Christiedb98ccd2006-08-31 18:09:31 -04002170 if ((session->imm_data_en || !session->initial_r2t_en) &&
2171 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002172 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2173 "first_burst %d max_burst %d\n",
2174 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002175 return -EINVAL;
2176 }
2177
Mike Christief6d51802007-12-13 12:43:30 -06002178 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002179 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2180 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002181 conn->recv_timeout = 5;
2182 }
2183
2184 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002185 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2186 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002187 conn->ping_timeout = 5;
2188 }
2189
Mike Christie7996a772006-04-06 21:13:41 -05002190 spin_lock_bh(&session->lock);
2191 conn->c_stage = ISCSI_CONN_STARTED;
2192 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002193 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002194
Mike Christief6d51802007-12-13 12:43:30 -06002195 conn->last_recv = jiffies;
2196 conn->last_ping = jiffies;
2197 if (conn->recv_timeout && conn->ping_timeout)
2198 mod_timer(&conn->transport_timer,
2199 jiffies + (conn->recv_timeout * HZ));
2200
Mike Christie7996a772006-04-06 21:13:41 -05002201 switch(conn->stop_stage) {
2202 case STOP_CONN_RECOVER:
2203 /*
2204 * unblock eh_abort() if it is blocked. re-try all
2205 * commands after successful recovery
2206 */
Mike Christie7996a772006-04-06 21:13:41 -05002207 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002208 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002209 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002210 if (session->age == 16)
2211 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002212 break;
Mike Christie7996a772006-04-06 21:13:41 -05002213 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002214 conn->stop_stage = 0;
2215 break;
Mike Christie7996a772006-04-06 21:13:41 -05002216 default:
2217 break;
2218 }
2219 spin_unlock_bh(&session->lock);
2220
Mike Christie75613522008-05-21 15:53:59 -05002221 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002222 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002223 return 0;
2224}
2225EXPORT_SYMBOL_GPL(iscsi_conn_start);
2226
2227static void
2228flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2229{
2230 struct iscsi_mgmt_task *mtask, *tmp;
2231
2232 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06002233 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2234 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002235 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002236 }
2237
2238 /* handle running */
2239 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2240 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002241 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002242 }
2243
2244 conn->mtask = NULL;
2245}
2246
Mike Christie656cffc2006-05-18 20:31:42 -05002247static void iscsi_start_session_recovery(struct iscsi_session *session,
2248 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002249{
Mike Christieed2abc72006-05-02 19:46:40 -05002250 int old_stop_stage;
2251
Mike Christief6d51802007-12-13 12:43:30 -06002252 del_timer_sync(&conn->transport_timer);
2253
Mike Christie6724add2007-08-15 01:38:30 -05002254 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002255 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002256 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002257 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002258 mutex_unlock(&session->eh_mutex);
2259 return;
2260 }
2261
2262 /*
2263 * The LLD either freed/unset the lock on us, or userspace called
2264 * stop but did not create a proper connection (connection was never
2265 * bound or it was unbound then stop was called).
2266 */
2267 if (!conn->recv_lock) {
2268 spin_unlock_bh(&session->lock);
2269 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002270 return;
2271 }
Mike Christieed2abc72006-05-02 19:46:40 -05002272
2273 /*
2274 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05002275 * up the login task and connection. We do not need to block and set
2276 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002277 */
Mike Christie67a61112006-05-30 00:37:20 -05002278 if (flag == STOP_CONN_TERM)
2279 session->state = ISCSI_STATE_TERMINATE;
2280 else if (conn->stop_stage != STOP_CONN_RECOVER)
2281 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002282
2283 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002284 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002285 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002286 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002287
2288 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002289
Mike Christie1c834692006-07-24 15:47:26 -05002290 write_lock_bh(conn->recv_lock);
2291 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2292 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002293
Mike Christie7996a772006-04-06 21:13:41 -05002294 /*
2295 * for connection level recovery we should not calculate
2296 * header digest. conn->hdr_size used for optimization
2297 * in hdr_extract() and will be re-negotiated at
2298 * set_param() time.
2299 */
2300 if (flag == STOP_CONN_RECOVER) {
2301 conn->hdrdgst_en = 0;
2302 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002303 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002304 old_stop_stage != STOP_CONN_RECOVER) {
2305 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002306 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002307 }
Mike Christie7996a772006-04-06 21:13:41 -05002308 }
Mike Christie656cffc2006-05-18 20:31:42 -05002309
Mike Christie656cffc2006-05-18 20:31:42 -05002310 /*
2311 * flush queues.
2312 */
2313 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002314 fail_all_commands(conn, -1,
2315 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002316 flush_control_queues(session, conn);
2317 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002318 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002319}
Mike Christie7996a772006-04-06 21:13:41 -05002320
2321void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2322{
2323 struct iscsi_conn *conn = cls_conn->dd_data;
2324 struct iscsi_session *session = conn->session;
2325
2326 switch (flag) {
2327 case STOP_CONN_RECOVER:
2328 case STOP_CONN_TERM:
2329 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002330 break;
Mike Christie7996a772006-04-06 21:13:41 -05002331 default:
Mike Christie322d7392008-01-31 13:36:52 -06002332 iscsi_conn_printk(KERN_ERR, conn,
2333 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002334 }
2335}
2336EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2337
2338int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2339 struct iscsi_cls_conn *cls_conn, int is_leading)
2340{
Mike Christie75613522008-05-21 15:53:59 -05002341 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002342 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002343
Mike Christie7996a772006-04-06 21:13:41 -05002344 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002345 if (is_leading)
2346 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002347 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002348
2349 /*
2350 * Unblock xmitworker(), Login Phase will pass through.
2351 */
2352 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2353 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2354 return 0;
2355}
2356EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2357
Mike Christiea54a52c2006-06-28 12:00:23 -05002358
2359int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2360 enum iscsi_param param, char *buf, int buflen)
2361{
2362 struct iscsi_conn *conn = cls_conn->dd_data;
2363 struct iscsi_session *session = conn->session;
2364 uint32_t value;
2365
2366 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002367 case ISCSI_PARAM_FAST_ABORT:
2368 sscanf(buf, "%d", &session->fast_abort);
2369 break;
Mike Christief6d51802007-12-13 12:43:30 -06002370 case ISCSI_PARAM_ABORT_TMO:
2371 sscanf(buf, "%d", &session->abort_timeout);
2372 break;
2373 case ISCSI_PARAM_LU_RESET_TMO:
2374 sscanf(buf, "%d", &session->lu_reset_timeout);
2375 break;
2376 case ISCSI_PARAM_PING_TMO:
2377 sscanf(buf, "%d", &conn->ping_timeout);
2378 break;
2379 case ISCSI_PARAM_RECV_TMO:
2380 sscanf(buf, "%d", &conn->recv_timeout);
2381 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002382 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2383 sscanf(buf, "%d", &conn->max_recv_dlength);
2384 break;
2385 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2386 sscanf(buf, "%d", &conn->max_xmit_dlength);
2387 break;
2388 case ISCSI_PARAM_HDRDGST_EN:
2389 sscanf(buf, "%d", &conn->hdrdgst_en);
2390 break;
2391 case ISCSI_PARAM_DATADGST_EN:
2392 sscanf(buf, "%d", &conn->datadgst_en);
2393 break;
2394 case ISCSI_PARAM_INITIAL_R2T_EN:
2395 sscanf(buf, "%d", &session->initial_r2t_en);
2396 break;
2397 case ISCSI_PARAM_MAX_R2T:
2398 sscanf(buf, "%d", &session->max_r2t);
2399 break;
2400 case ISCSI_PARAM_IMM_DATA_EN:
2401 sscanf(buf, "%d", &session->imm_data_en);
2402 break;
2403 case ISCSI_PARAM_FIRST_BURST:
2404 sscanf(buf, "%d", &session->first_burst);
2405 break;
2406 case ISCSI_PARAM_MAX_BURST:
2407 sscanf(buf, "%d", &session->max_burst);
2408 break;
2409 case ISCSI_PARAM_PDU_INORDER_EN:
2410 sscanf(buf, "%d", &session->pdu_inorder_en);
2411 break;
2412 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2413 sscanf(buf, "%d", &session->dataseq_inorder_en);
2414 break;
2415 case ISCSI_PARAM_ERL:
2416 sscanf(buf, "%d", &session->erl);
2417 break;
2418 case ISCSI_PARAM_IFMARKER_EN:
2419 sscanf(buf, "%d", &value);
2420 BUG_ON(value);
2421 break;
2422 case ISCSI_PARAM_OFMARKER_EN:
2423 sscanf(buf, "%d", &value);
2424 BUG_ON(value);
2425 break;
2426 case ISCSI_PARAM_EXP_STATSN:
2427 sscanf(buf, "%u", &conn->exp_statsn);
2428 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002429 case ISCSI_PARAM_USERNAME:
2430 kfree(session->username);
2431 session->username = kstrdup(buf, GFP_KERNEL);
2432 if (!session->username)
2433 return -ENOMEM;
2434 break;
2435 case ISCSI_PARAM_USERNAME_IN:
2436 kfree(session->username_in);
2437 session->username_in = kstrdup(buf, GFP_KERNEL);
2438 if (!session->username_in)
2439 return -ENOMEM;
2440 break;
2441 case ISCSI_PARAM_PASSWORD:
2442 kfree(session->password);
2443 session->password = kstrdup(buf, GFP_KERNEL);
2444 if (!session->password)
2445 return -ENOMEM;
2446 break;
2447 case ISCSI_PARAM_PASSWORD_IN:
2448 kfree(session->password_in);
2449 session->password_in = kstrdup(buf, GFP_KERNEL);
2450 if (!session->password_in)
2451 return -ENOMEM;
2452 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002453 case ISCSI_PARAM_TARGET_NAME:
2454 /* this should not change between logins */
2455 if (session->targetname)
2456 break;
2457
2458 session->targetname = kstrdup(buf, GFP_KERNEL);
2459 if (!session->targetname)
2460 return -ENOMEM;
2461 break;
2462 case ISCSI_PARAM_TPGT:
2463 sscanf(buf, "%d", &session->tpgt);
2464 break;
2465 case ISCSI_PARAM_PERSISTENT_PORT:
2466 sscanf(buf, "%d", &conn->persistent_port);
2467 break;
2468 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2469 /*
2470 * this is the address returned in discovery so it should
2471 * not change between logins.
2472 */
2473 if (conn->persistent_address)
2474 break;
2475
2476 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2477 if (!conn->persistent_address)
2478 return -ENOMEM;
2479 break;
2480 default:
2481 return -ENOSYS;
2482 }
2483
2484 return 0;
2485}
2486EXPORT_SYMBOL_GPL(iscsi_set_param);
2487
2488int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2489 enum iscsi_param param, char *buf)
2490{
Mike Christie75613522008-05-21 15:53:59 -05002491 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002492 int len;
2493
2494 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002495 case ISCSI_PARAM_FAST_ABORT:
2496 len = sprintf(buf, "%d\n", session->fast_abort);
2497 break;
Mike Christief6d51802007-12-13 12:43:30 -06002498 case ISCSI_PARAM_ABORT_TMO:
2499 len = sprintf(buf, "%d\n", session->abort_timeout);
2500 break;
2501 case ISCSI_PARAM_LU_RESET_TMO:
2502 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2503 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002504 case ISCSI_PARAM_INITIAL_R2T_EN:
2505 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2506 break;
2507 case ISCSI_PARAM_MAX_R2T:
2508 len = sprintf(buf, "%hu\n", session->max_r2t);
2509 break;
2510 case ISCSI_PARAM_IMM_DATA_EN:
2511 len = sprintf(buf, "%d\n", session->imm_data_en);
2512 break;
2513 case ISCSI_PARAM_FIRST_BURST:
2514 len = sprintf(buf, "%u\n", session->first_burst);
2515 break;
2516 case ISCSI_PARAM_MAX_BURST:
2517 len = sprintf(buf, "%u\n", session->max_burst);
2518 break;
2519 case ISCSI_PARAM_PDU_INORDER_EN:
2520 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2521 break;
2522 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2523 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2524 break;
2525 case ISCSI_PARAM_ERL:
2526 len = sprintf(buf, "%d\n", session->erl);
2527 break;
2528 case ISCSI_PARAM_TARGET_NAME:
2529 len = sprintf(buf, "%s\n", session->targetname);
2530 break;
2531 case ISCSI_PARAM_TPGT:
2532 len = sprintf(buf, "%d\n", session->tpgt);
2533 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002534 case ISCSI_PARAM_USERNAME:
2535 len = sprintf(buf, "%s\n", session->username);
2536 break;
2537 case ISCSI_PARAM_USERNAME_IN:
2538 len = sprintf(buf, "%s\n", session->username_in);
2539 break;
2540 case ISCSI_PARAM_PASSWORD:
2541 len = sprintf(buf, "%s\n", session->password);
2542 break;
2543 case ISCSI_PARAM_PASSWORD_IN:
2544 len = sprintf(buf, "%s\n", session->password_in);
2545 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002546 default:
2547 return -ENOSYS;
2548 }
2549
2550 return len;
2551}
2552EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2553
2554int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2555 enum iscsi_param param, char *buf)
2556{
2557 struct iscsi_conn *conn = cls_conn->dd_data;
2558 int len;
2559
2560 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002561 case ISCSI_PARAM_PING_TMO:
2562 len = sprintf(buf, "%u\n", conn->ping_timeout);
2563 break;
2564 case ISCSI_PARAM_RECV_TMO:
2565 len = sprintf(buf, "%u\n", conn->recv_timeout);
2566 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002567 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2568 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2569 break;
2570 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2571 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2572 break;
2573 case ISCSI_PARAM_HDRDGST_EN:
2574 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2575 break;
2576 case ISCSI_PARAM_DATADGST_EN:
2577 len = sprintf(buf, "%d\n", conn->datadgst_en);
2578 break;
2579 case ISCSI_PARAM_IFMARKER_EN:
2580 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2581 break;
2582 case ISCSI_PARAM_OFMARKER_EN:
2583 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2584 break;
2585 case ISCSI_PARAM_EXP_STATSN:
2586 len = sprintf(buf, "%u\n", conn->exp_statsn);
2587 break;
2588 case ISCSI_PARAM_PERSISTENT_PORT:
2589 len = sprintf(buf, "%d\n", conn->persistent_port);
2590 break;
2591 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2592 len = sprintf(buf, "%s\n", conn->persistent_address);
2593 break;
2594 default:
2595 return -ENOSYS;
2596 }
2597
2598 return len;
2599}
2600EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2601
Mike Christie0801c242007-05-30 12:57:12 -05002602int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2603 char *buf)
2604{
Mike Christie75613522008-05-21 15:53:59 -05002605 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002606 int len;
2607
2608 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002609 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002610 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002611 len = sprintf(buf, "%s\n", "default");
2612 else
Mike Christie75613522008-05-21 15:53:59 -05002613 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002614 break;
Mike Christie0801c242007-05-30 12:57:12 -05002615 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002616 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002617 len = sprintf(buf, "%s\n", "default");
2618 else
Mike Christie75613522008-05-21 15:53:59 -05002619 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002620 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002621 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002622 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002623 len = sprintf(buf, "%s\n", "unknown");
2624 else
Mike Christie75613522008-05-21 15:53:59 -05002625 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002626 break;
Mike Christie75613522008-05-21 15:53:59 -05002627 case ISCSI_HOST_PARAM_IPADDRESS:
2628 if (!strlen(ihost->local_address))
2629 len = sprintf(buf, "%s\n", "unknown");
2630 else
2631 len = sprintf(buf, "%s\n",
2632 ihost->local_address);
Mike Christie0801c242007-05-30 12:57:12 -05002633 default:
2634 return -ENOSYS;
2635 }
2636
2637 return len;
2638}
2639EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2640
2641int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2642 char *buf, int buflen)
2643{
Mike Christie75613522008-05-21 15:53:59 -05002644 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002645
2646 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002647 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002648 if (!ihost->netdev)
2649 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002650 break;
Mike Christie0801c242007-05-30 12:57:12 -05002651 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002652 if (!ihost->hwaddress)
2653 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002654 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002655 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002656 if (!ihost->initiatorname)
2657 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002658 break;
Mike Christie0801c242007-05-30 12:57:12 -05002659 default:
2660 return -ENOSYS;
2661 }
2662
2663 return 0;
2664}
2665EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2666
Mike Christie7996a772006-04-06 21:13:41 -05002667MODULE_AUTHOR("Mike Christie");
2668MODULE_DESCRIPTION("iSCSI library functions");
2669MODULE_LICENSE("GPL");