blob: ef92b1b0f16edb7ba20279a6037b9f5432bb5bbf [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
Mike Christie3e5c28a2008-05-21 15:54:06 -0500200 * @ctask: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500201 *
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 Christie3e5c28a2008-05-21 15:54:06 -0500303 if (conn->session->tt->init_task &&
304 conn->session->tt->init_task(ctask))
Mike Christie052d0142008-05-21 15:54:05 -0500305 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++;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500311 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
312 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ?
313 "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ?
314 "write" : "read", conn->id, sc, sc->cmnd[0], ctask->itt,
315 scsi_bufflen(sc),
316 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
317 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/**
Mike Christie3e5c28a2008-05-21 15:54:06 -0500322 * iscsi_complete_command - finish a task
Mike Christie7996a772006-04-06 21:13:41 -0500323 * @ctask: iscsi cmd task
324 *
325 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500326 * This function returns the scsi command to scsi-ml or cleans
327 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500328 */
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 Christie3e5c28a2008-05-21 15:54:06 -0500335 list_del_init(&ctask->running);
Mike Christieb6c395e2006-07-24 15:47:15 -0500336 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500337 ctask->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500338
Mike Christiec1635cb2007-12-13 12:43:33 -0600339 if (conn->ctask == ctask)
340 conn->ctask = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500341 /*
342 * login ctask is preallocated so do not free
343 */
344 if (conn->login_ctask == ctask)
345 return;
346
Mike Christie7996a772006-04-06 21:13:41 -0500347 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500348
Mike Christie3e5c28a2008-05-21 15:54:06 -0500349 if (conn->ping_ctask == ctask)
350 conn->ping_ctask = NULL;
351
352 if (sc) {
353 ctask->sc = NULL;
354 /* SCSI eh reuses commands to verify us */
355 sc->SCp.ptr = NULL;
356 /*
357 * queue command may call this to free the task, but
358 * not have setup the sc callback
359 */
360 if (sc->scsi_done)
361 sc->scsi_done(sc);
362 }
Mike Christie7996a772006-04-06 21:13:41 -0500363}
364
Mike Christie60ecebf2006-08-31 18:09:25 -0400365static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
366{
367 atomic_inc(&ctask->refcount);
368}
369
Mike Christie60ecebf2006-08-31 18:09:25 -0400370static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
371{
Mike Christiee648f632006-08-31 18:09:34 -0400372 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400373 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400374}
375
Mike Christie3e5c28a2008-05-21 15:54:06 -0500376void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
377{
378 struct iscsi_session *session = ctask->conn->session;
379
380 spin_lock_bh(&session->lock);
381 __iscsi_put_ctask(ctask);
382 spin_unlock_bh(&session->lock);
383}
384EXPORT_SYMBOL_GPL(iscsi_put_ctask);
385
Mike Christieb3a7ea82007-12-13 12:43:26 -0600386/*
387 * session lock must be held
388 */
389static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
390 int err)
391{
392 struct scsi_cmnd *sc;
393
394 sc = ctask->sc;
395 if (!sc)
396 return;
397
398 if (ctask->state == ISCSI_TASK_PENDING)
399 /*
400 * cmd never made it to the xmit thread, so we should not count
401 * the cmd in the sequencing
402 */
403 conn->session->queued_cmdsn--;
404 else
Mike Christie3e5c28a2008-05-21 15:54:06 -0500405 conn->session->tt->cleanup_task(conn, ctask);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600406
407 sc->result = err;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500408
Boaz Harroshc07d4442008-04-18 10:11:52 -0500409 if (!scsi_bidi_cmnd(sc))
410 scsi_set_resid(sc, scsi_bufflen(sc));
411 else {
412 scsi_out(sc)->resid = scsi_out(sc)->length;
413 scsi_in(sc)->resid = scsi_in(sc)->length;
414 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500415
Mike Christieb3a7ea82007-12-13 12:43:26 -0600416 if (conn->ctask == ctask)
417 conn->ctask = NULL;
418 /* release ref from queuecommand */
419 __iscsi_put_ctask(ctask);
420}
421
Mike Christie3e5c28a2008-05-21 15:54:06 -0500422static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
423 struct iscsi_cmd_task *ctask)
Mike Christie052d0142008-05-21 15:54:05 -0500424{
425 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500426 struct iscsi_hdr *hdr = (struct iscsi_hdr *)ctask->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500427 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
428
429 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
430 return -ENOTCONN;
431
432 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
433 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
434 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
435 /*
436 * pre-format CmdSN for outgoing PDU.
437 */
438 nop->cmdsn = cpu_to_be32(session->cmdsn);
439 if (hdr->itt != RESERVED_ITT) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500440 hdr->itt = build_itt(ctask->itt, session->age);
Mike Christie052d0142008-05-21 15:54:05 -0500441 /*
442 * TODO: We always use immediate, so we never hit this.
443 * If we start to send tmfs or nops as non-immediate then
444 * we should start checking the cmdsn numbers for mgmt tasks.
445 */
446 if (conn->c_stage == ISCSI_CONN_STARTED &&
447 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
448 session->queued_cmdsn++;
449 session->cmdsn++;
450 }
451 }
452
Mike Christie3e5c28a2008-05-21 15:54:06 -0500453 if (session->tt->init_task)
454 session->tt->init_task(ctask);
Mike Christie052d0142008-05-21 15:54:05 -0500455
456 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
457 session->state = ISCSI_STATE_LOGGING_OUT;
458
Mike Christie3e5c28a2008-05-21 15:54:06 -0500459 list_move_tail(&ctask->running, &conn->mgmt_run_list);
Mike Christie052d0142008-05-21 15:54:05 -0500460 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
461 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
Mike Christie3e5c28a2008-05-21 15:54:06 -0500462 ctask->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500463 return 0;
464}
465
Mike Christie3e5c28a2008-05-21 15:54:06 -0500466static struct iscsi_cmd_task *
Mike Christief6d51802007-12-13 12:43:30 -0600467__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
468 char *data, uint32_t data_size)
469{
470 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500471 struct iscsi_cmd_task *ctask;
Mike Christief6d51802007-12-13 12:43:30 -0600472
473 if (session->state == ISCSI_STATE_TERMINATE)
474 return NULL;
475
476 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
477 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
478 /*
479 * Login and Text are sent serially, in
480 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500481 * Same task can be used. Same ITT must be used.
482 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600483 */
Mike Christie3e5c28a2008-05-21 15:54:06 -0500484 ctask = conn->login_ctask;
Mike Christief6d51802007-12-13 12:43:30 -0600485 else {
486 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
487 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
488
Mike Christie3e5c28a2008-05-21 15:54:06 -0500489 if (!__kfifo_get(session->cmdpool.queue,
490 (void*)&ctask, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600491 return NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500492
493 if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
494 hdr->ttt == RESERVED_ITT) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500495 conn->ping_ctask = ctask;
Mike Christie052d0142008-05-21 15:54:05 -0500496 conn->last_ping = jiffies;
497 }
Mike Christief6d51802007-12-13 12:43:30 -0600498 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500499 /*
500 * released in complete pdu for task we expect a response for, and
501 * released by the lld when it has transmitted the task for
502 * pdus we do not expect a response for.
503 */
504 atomic_set(&ctask->refcount, 1);
505 ctask->conn = conn;
506 ctask->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600507
508 if (data_size) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500509 memcpy(ctask->data, data, data_size);
510 ctask->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600511 } else
Mike Christie3e5c28a2008-05-21 15:54:06 -0500512 ctask->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600513
Mike Christie3e5c28a2008-05-21 15:54:06 -0500514 memcpy(ctask->hdr, hdr, sizeof(struct iscsi_hdr));
515 INIT_LIST_HEAD(&ctask->running);
516 list_add_tail(&ctask->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500517
518 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500519 if (iscsi_prep_mgmt_task(conn, ctask)) {
520 __iscsi_put_ctask(ctask);
Mike Christie052d0142008-05-21 15:54:05 -0500521 return NULL;
522 }
523
Mike Christie3e5c28a2008-05-21 15:54:06 -0500524 if (session->tt->xmit_task(ctask))
525 ctask = NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500526
527 } else
528 scsi_queue_work(conn->session->host, &conn->xmitwork);
529
Mike Christie3e5c28a2008-05-21 15:54:06 -0500530 return ctask;
Mike Christief6d51802007-12-13 12:43:30 -0600531}
532
533int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
534 char *data, uint32_t data_size)
535{
536 struct iscsi_conn *conn = cls_conn->dd_data;
537 struct iscsi_session *session = conn->session;
538 int err = 0;
539
540 spin_lock_bh(&session->lock);
541 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
542 err = -EPERM;
543 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600544 return err;
545}
546EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
547
Mike Christie7996a772006-04-06 21:13:41 -0500548/**
549 * iscsi_cmd_rsp - SCSI Command Response processing
550 * @conn: iscsi connection
551 * @hdr: iscsi header
552 * @ctask: scsi command task
553 * @data: cmd data buffer
554 * @datalen: len of buffer
555 *
556 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie3e5c28a2008-05-21 15:54:06 -0500557 * then completes the command and ctask.
Mike Christie7996a772006-04-06 21:13:41 -0500558 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500559static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
560 struct iscsi_cmd_task *ctask, char *data,
561 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500562{
Mike Christie7996a772006-04-06 21:13:41 -0500563 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
564 struct iscsi_session *session = conn->session;
565 struct scsi_cmnd *sc = ctask->sc;
566
Mike Christie77a23c22007-05-30 12:57:18 -0500567 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500568 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
569
570 sc->result = (DID_OK << 16) | rhdr->cmd_status;
571
572 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
573 sc->result = DID_ERROR << 16;
574 goto out;
575 }
576
577 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600578 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500579
580 if (datalen < 2) {
581invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600582 iscsi_conn_printk(KERN_ERR, conn,
583 "Got CHECK_CONDITION but invalid data "
584 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500585 sc->result = DID_BAD_TARGET << 16;
586 goto out;
587 }
588
Mike Christie8eb00532007-02-28 17:32:19 -0600589 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500590 if (datalen < senselen)
591 goto invalid_datalen;
592
593 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600594 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500595 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600596 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500597 }
598
Boaz Harroshc07d4442008-04-18 10:11:52 -0500599 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
600 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
601 int res_count = be32_to_cpu(rhdr->bi_residual_count);
602
603 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
604 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
605 res_count <= scsi_in(sc)->length))
606 scsi_in(sc)->resid = res_count;
607 else
608 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
609 }
610
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600611 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
612 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500613 int res_count = be32_to_cpu(rhdr->residual_count);
614
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600615 if (res_count > 0 &&
616 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
617 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500618 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900619 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500620 else
621 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500622 }
Mike Christie7996a772006-04-06 21:13:41 -0500623out:
624 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
625 (long)sc, sc->result, ctask->itt);
626 conn->scsirsp_pdus_cnt++;
627
Mike Christie60ecebf2006-08-31 18:09:25 -0400628 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500629}
630
Mike Christie7ea8b822006-07-24 15:47:22 -0500631static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
632{
633 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
634
635 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
636 conn->tmfrsp_pdus_cnt++;
637
Mike Christie843c0a82007-12-13 12:43:20 -0600638 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500639 return;
640
641 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600642 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500643 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600644 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500645 else
Mike Christie843c0a82007-12-13 12:43:20 -0600646 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500647 wake_up(&conn->ehwait);
648}
649
Mike Christief6d51802007-12-13 12:43:30 -0600650static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
651{
652 struct iscsi_nopout hdr;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500653 struct iscsi_cmd_task *ctask;
Mike Christief6d51802007-12-13 12:43:30 -0600654
Mike Christie3e5c28a2008-05-21 15:54:06 -0500655 if (!rhdr && conn->ping_ctask)
Mike Christief6d51802007-12-13 12:43:30 -0600656 return;
657
658 memset(&hdr, 0, sizeof(struct iscsi_nopout));
659 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
660 hdr.flags = ISCSI_FLAG_CMD_FINAL;
661
662 if (rhdr) {
663 memcpy(hdr.lun, rhdr->lun, 8);
664 hdr.ttt = rhdr->ttt;
665 hdr.itt = RESERVED_ITT;
666 } else
667 hdr.ttt = RESERVED_ITT;
668
Mike Christie3e5c28a2008-05-21 15:54:06 -0500669 ctask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
670 if (!ctask)
Mike Christie322d7392008-01-31 13:36:52 -0600671 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600672}
673
Mike Christie62f38302006-08-31 18:09:27 -0400674static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
675 char *data, int datalen)
676{
677 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
678 struct iscsi_hdr rejected_pdu;
679 uint32_t itt;
680
681 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
682
683 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
684 if (ntoh24(reject->dlength) > datalen)
685 return ISCSI_ERR_PROTO;
686
687 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
688 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000689 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600690 iscsi_conn_printk(KERN_ERR, conn,
691 "itt 0x%x had pdu (op 0x%x) rejected "
692 "due to DataDigest error.\n", itt,
693 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400694 }
695 }
696 return 0;
697}
698
Mike Christie7996a772006-04-06 21:13:41 -0500699/**
700 * __iscsi_complete_pdu - complete pdu
701 * @conn: iscsi conn
702 * @hdr: iscsi header
703 * @data: data buffer
704 * @datalen: len of data buffer
705 *
706 * Completes pdu processing by freeing any resources allocated at
707 * queuecommand or send generic. session lock must be held and verify
708 * itt must have been called.
709 */
Adrian Bunkf8d9d652008-01-29 00:11:27 +0200710static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
711 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500712{
713 struct iscsi_session *session = conn->session;
714 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
715 struct iscsi_cmd_task *ctask;
Mike Christie7996a772006-04-06 21:13:41 -0500716 uint32_t itt;
717
Mike Christief6d51802007-12-13 12:43:30 -0600718 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500719 rc = iscsi_verify_itt(conn, hdr->itt);
720 if (rc)
721 return rc;
722
Al Virob4377352007-02-09 16:39:40 +0000723 if (hdr->itt != RESERVED_ITT)
724 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500725 else
Al Virob4377352007-02-09 16:39:40 +0000726 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500727
Mike Christie3e5c28a2008-05-21 15:54:06 -0500728 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
729 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500730
Mike Christie3e5c28a2008-05-21 15:54:06 -0500731 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500732 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400733
Mike Christie7996a772006-04-06 21:13:41 -0500734 switch(opcode) {
735 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500736 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500737 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500738 break;
739 }
740
Al Virob4377352007-02-09 16:39:40 +0000741 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500742 break;
743
Mike Christief6d51802007-12-13 12:43:30 -0600744 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500745 break;
746 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400747 rc = iscsi_handle_reject(conn, hdr, data, datalen);
748 break;
Mike Christie7996a772006-04-06 21:13:41 -0500749 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500750 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400751 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
752 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500753 break;
754 default:
755 rc = ISCSI_ERR_BAD_OPCODE;
756 break;
757 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500758 goto out;
759 }
Mike Christie7996a772006-04-06 21:13:41 -0500760
Mike Christie3e5c28a2008-05-21 15:54:06 -0500761 ctask = session->cmds[itt];
762 switch(opcode) {
763 case ISCSI_OP_SCSI_CMD_RSP:
764 if (!ctask->sc) {
765 rc = ISCSI_ERR_NO_SCSI_CMD;
766 break;
767 }
768 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
769 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data, datalen);
770 break;
771 case ISCSI_OP_SCSI_DATA_IN:
772 if (!ctask->sc) {
773 rc = ISCSI_ERR_NO_SCSI_CMD;
774 break;
775 }
776 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
777 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
778 conn->scsirsp_pdus_cnt++;
779 iscsi_update_cmdsn(session,
780 (struct iscsi_nopin*) hdr);
781 __iscsi_put_ctask(ctask);
782 }
783 break;
784 case ISCSI_OP_R2T:
785 /* LLD handles this for now */
786 break;
787 case ISCSI_OP_LOGOUT_RSP:
788 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
789 if (datalen) {
790 rc = ISCSI_ERR_PROTO;
791 break;
792 }
793 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
794 goto recv_pdu;
795 case ISCSI_OP_LOGIN_RSP:
796 case ISCSI_OP_TEXT_RSP:
797 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
798 /*
799 * login related PDU's exp_statsn is handled in
800 * userspace
801 */
802 goto recv_pdu;
803 case ISCSI_OP_SCSI_TMFUNC_RSP:
804 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
805 if (datalen) {
806 rc = ISCSI_ERR_PROTO;
807 break;
808 }
809
810 iscsi_tmf_rsp(conn, hdr);
811 __iscsi_put_ctask(ctask);
812 break;
813 case ISCSI_OP_NOOP_IN:
814 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
815 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
816 rc = ISCSI_ERR_PROTO;
817 break;
818 }
819 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
820
821 if (conn->ping_ctask != ctask)
822 /*
823 * If this is not in response to one of our
824 * nops then it must be from userspace.
825 */
826 goto recv_pdu;
827 __iscsi_put_ctask(ctask);
828 break;
829 default:
830 rc = ISCSI_ERR_BAD_OPCODE;
831 break;
832 }
833
834out:
835 return rc;
836recv_pdu:
837 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
838 rc = ISCSI_ERR_CONN_FAILED;
839 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500840 return rc;
841}
Mike Christie7996a772006-04-06 21:13:41 -0500842
843int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
844 char *data, int datalen)
845{
846 int rc;
847
848 spin_lock(&conn->session->lock);
849 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
850 spin_unlock(&conn->session->lock);
851 return rc;
852}
853EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
854
Mike Christie0af967f2008-05-21 15:54:04 -0500855int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500856{
857 struct iscsi_session *session = conn->session;
858 struct iscsi_cmd_task *ctask;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500859 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500860
Mike Christie0af967f2008-05-21 15:54:04 -0500861 if (itt == RESERVED_ITT)
862 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500863
Mike Christie0af967f2008-05-21 15:54:04 -0500864 if (((__force u32)itt & ISCSI_AGE_MASK) !=
865 (session->age << ISCSI_AGE_SHIFT)) {
866 iscsi_conn_printk(KERN_ERR, conn,
867 "received itt %x expected session age (%x)\n",
868 (__force u32)itt,
869 session->age & ISCSI_AGE_MASK);
870 return ISCSI_ERR_BAD_ITT;
871 }
Mike Christie7996a772006-04-06 21:13:41 -0500872
Mike Christie3e5c28a2008-05-21 15:54:06 -0500873 i = get_itt(itt);
874 if (i >= session->cmds_max) {
875 iscsi_conn_printk(KERN_ERR, conn,
876 "received invalid itt index %u (max cmds "
877 "%u.\n", i, session->cmds_max);
878 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500879 }
880
Mike Christie3e5c28a2008-05-21 15:54:06 -0500881 ctask = session->cmds[i];
882 if (ctask->sc && ctask->sc->SCp.phase != session->age) {
883 iscsi_conn_printk(KERN_ERR, conn,
884 "iscsi: ctask's session age %d, "
885 "expected %d\n", ctask->sc->SCp.phase,
886 session->age);
887 return ISCSI_ERR_SESSION_FAILED;
888 }
Mike Christie7996a772006-04-06 21:13:41 -0500889 return 0;
890}
891EXPORT_SYMBOL_GPL(iscsi_verify_itt);
892
Mike Christie0af967f2008-05-21 15:54:04 -0500893struct iscsi_cmd_task *
894iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
895{
896 struct iscsi_session *session = conn->session;
897 struct iscsi_cmd_task *ctask;
898 uint32_t i;
899
900 if (iscsi_verify_itt(conn, itt))
901 return NULL;
902
903 if (itt == RESERVED_ITT)
904 return NULL;
905
906 i = get_itt(itt);
907 if (i >= session->cmds_max)
908 return NULL;
909
910 ctask = session->cmds[i];
911 if (!ctask->sc)
912 return NULL;
913
914 if (ctask->sc->SCp.phase != session->age)
915 return NULL;
916
917 return ctask;
918}
919EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
920
Mike Christie7996a772006-04-06 21:13:41 -0500921void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
922{
923 struct iscsi_session *session = conn->session;
924 unsigned long flags;
925
926 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500927 if (session->state == ISCSI_STATE_FAILED) {
928 spin_unlock_irqrestore(&session->lock, flags);
929 return;
930 }
931
Mike Christie67a61112006-05-30 00:37:20 -0500932 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500933 session->state = ISCSI_STATE_FAILED;
934 spin_unlock_irqrestore(&session->lock, flags);
935 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
936 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
937 iscsi_conn_error(conn->cls_conn, err);
938}
939EXPORT_SYMBOL_GPL(iscsi_conn_failure);
940
Mike Christie77a23c22007-05-30 12:57:18 -0500941static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
942{
943 struct iscsi_session *session = conn->session;
944
945 /*
946 * Check for iSCSI window and take care of CmdSN wrap-around
947 */
Mike Christiee0726402007-07-26 12:46:48 -0500948 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
949 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
950 "CmdSN %u/%u\n", session->exp_cmdsn,
951 session->max_cmdsn, session->cmdsn,
952 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500953 return -ENOSPC;
954 }
955 return 0;
956}
957
958static int iscsi_xmit_ctask(struct iscsi_conn *conn)
959{
960 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600961 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500962
963 __iscsi_get_ctask(ctask);
964 spin_unlock_bh(&conn->session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500965 rc = conn->session->tt->xmit_task(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500966 spin_lock_bh(&conn->session->lock);
967 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500968 if (!rc)
969 /* done with this ctask */
970 conn->ctask = NULL;
971 return rc;
972}
973
Mike Christie7996a772006-04-06 21:13:41 -0500974/**
Mike Christie843c0a82007-12-13 12:43:20 -0600975 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
976 * @ctask: ctask to requeue
977 *
978 * LLDs that need to run a ctask from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -0500979 * this. The session lock must be held. This should only be called
980 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -0600981 */
982void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
983{
984 struct iscsi_conn *conn = ctask->conn;
985
986 list_move_tail(&ctask->running, &conn->requeue);
987 scsi_queue_work(conn->session->host, &conn->xmitwork);
988}
989EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
990
991/**
Mike Christie7996a772006-04-06 21:13:41 -0500992 * iscsi_data_xmit - xmit any command into the scheduled connection
993 * @conn: iscsi connection
994 *
995 * Notes:
996 * The function can return -EAGAIN in which case the caller must
997 * re-schedule it again later or recover. '0' return code means
998 * successful xmit.
999 **/
1000static int iscsi_data_xmit(struct iscsi_conn *conn)
1001{
Mike Christie3219e522006-05-30 00:37:28 -05001002 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001003
Mike Christie77a23c22007-05-30 12:57:18 -05001004 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001005 if (unlikely(conn->suspend_tx)) {
1006 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001007 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001008 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001009 }
Mike Christie7996a772006-04-06 21:13:41 -05001010
1011 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -05001012 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001013 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001014 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001015 }
1016
Mike Christie77a23c22007-05-30 12:57:18 -05001017 /*
1018 * process mgmt pdus like nops before commands since we should
1019 * only have one nop-out as a ping from us and targets should not
1020 * overflow us with nop-ins
1021 */
1022check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001023 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie3e5c28a2008-05-21 15:54:06 -05001024 conn->ctask = list_entry(conn->mgmtqueue.next,
1025 struct iscsi_cmd_task, running);
1026 if (iscsi_prep_mgmt_task(conn, conn->ctask)) {
1027 __iscsi_put_ctask(conn->ctask);
1028 conn->ctask = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001029 continue;
1030 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001031 rc = iscsi_xmit_ctask(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001032 if (rc)
1033 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001034 }
1035
Mike Christie843c0a82007-12-13 12:43:20 -06001036 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001037 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001038 if (conn->tmf_state == TMF_QUEUED)
1039 break;
1040
Mike Christieb6c395e2006-07-24 15:47:15 -05001041 conn->ctask = list_entry(conn->xmitqueue.next,
1042 struct iscsi_cmd_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001043 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9000bcd2007-12-13 12:43:32 -06001044 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001045 continue;
1046 }
Boaz Harrosh004d6532007-12-13 12:43:23 -06001047 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1048 fail_command(conn, conn->ctask, DID_ABORT << 16);
1049 continue;
1050 }
Mike Christie77a23c22007-05-30 12:57:18 -05001051 rc = iscsi_xmit_ctask(conn);
1052 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001053 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001054 /*
1055 * we could continuously get new ctask requests so
1056 * we need to check the mgmt queue for nops that need to
1057 * be sent to aviod starvation
1058 */
Mike Christie843c0a82007-12-13 12:43:20 -06001059 if (!list_empty(&conn->mgmtqueue))
1060 goto check_mgmt;
1061 }
1062
1063 while (!list_empty(&conn->requeue)) {
1064 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1065 break;
1066
Mike Christieb3a7ea82007-12-13 12:43:26 -06001067 /*
1068 * we always do fastlogout - conn stop code will clean up.
1069 */
1070 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1071 break;
1072
Mike Christie843c0a82007-12-13 12:43:20 -06001073 conn->ctask = list_entry(conn->requeue.next,
1074 struct iscsi_cmd_task, running);
1075 conn->ctask->state = ISCSI_TASK_RUNNING;
1076 list_move_tail(conn->requeue.next, &conn->run_list);
1077 rc = iscsi_xmit_ctask(conn);
1078 if (rc)
1079 goto again;
1080 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001081 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001082 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001083 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001084 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001085
1086again:
1087 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001088 rc = -ENODATA;
1089 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001090 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001091}
1092
David Howellsc4028952006-11-22 14:57:56 +00001093static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001094{
David Howellsc4028952006-11-22 14:57:56 +00001095 struct iscsi_conn *conn =
1096 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001097 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001098 /*
1099 * serialize Xmit worker on a per-connection basis.
1100 */
Mike Christie3219e522006-05-30 00:37:28 -05001101 do {
1102 rc = iscsi_data_xmit(conn);
1103 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001104}
1105
1106enum {
1107 FAILURE_BAD_HOST = 1,
1108 FAILURE_SESSION_FAILED,
1109 FAILURE_SESSION_FREED,
1110 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001111 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001112 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001113 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001114 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001115 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001116 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001117};
1118
1119int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1120{
Mike Christie75613522008-05-21 15:53:59 -05001121 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001122 struct Scsi_Host *host;
1123 int reason = 0;
1124 struct iscsi_session *session;
1125 struct iscsi_conn *conn;
1126 struct iscsi_cmd_task *ctask = NULL;
1127
1128 sc->scsi_done = done;
1129 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001130 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001131
1132 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001133 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001134
Mike Christie75613522008-05-21 15:53:59 -05001135 cls_session = starget_to_session(scsi_target(sc->device));
1136 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001137 spin_lock(&session->lock);
1138
Mike Christie75613522008-05-21 15:53:59 -05001139 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001140 if (reason) {
1141 sc->result = reason;
1142 goto fault;
1143 }
1144
Mike Christie656cffc2006-05-18 20:31:42 -05001145 /*
1146 * ISCSI_STATE_FAILED is a temp. state. The recovery
1147 * code will decide what is best to do with command queued
1148 * during this time
1149 */
1150 if (session->state != ISCSI_STATE_LOGGED_IN &&
1151 session->state != ISCSI_STATE_FAILED) {
1152 /*
1153 * to handle the race between when we set the recovery state
1154 * and block the session we requeue here (commands could
1155 * be entering our queuecommand while a block is starting
1156 * up because the block code is not locked)
1157 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001158 switch (session->state) {
1159 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001160 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001161 sc->result = DID_IMM_RETRY << 16;
1162 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001163 case ISCSI_STATE_LOGGING_OUT:
1164 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001165 sc->result = DID_IMM_RETRY << 16;
1166 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001167 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001168 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001169 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001170 break;
1171 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001172 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001173 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001174 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001175 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001176 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001177 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001178 }
Mike Christie7996a772006-04-06 21:13:41 -05001179 goto fault;
1180 }
1181
Mike Christie7996a772006-04-06 21:13:41 -05001182 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001183 if (!conn) {
1184 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001185 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001186 goto fault;
1187 }
Mike Christie7996a772006-04-06 21:13:41 -05001188
Mike Christie77a23c22007-05-30 12:57:18 -05001189 if (iscsi_check_cmdsn_window_closed(conn)) {
1190 reason = FAILURE_WINDOW_CLOSED;
1191 goto reject;
1192 }
1193
Mike Christie60ecebf2006-08-31 18:09:25 -04001194 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1195 sizeof(void*))) {
1196 reason = FAILURE_OOM;
1197 goto reject;
1198 }
Mike Christie7996a772006-04-06 21:13:41 -05001199 sc->SCp.phase = session->age;
1200 sc->SCp.ptr = (char *)ctask;
1201
Mike Christie60ecebf2006-08-31 18:09:25 -04001202 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -05001203 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -05001204 ctask->conn = conn;
1205 ctask->sc = sc;
1206 INIT_LIST_HEAD(&ctask->running);
Mike Christieb6c395e2006-07-24 15:47:15 -05001207 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001208
Mike Christie052d0142008-05-21 15:54:05 -05001209 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
1210 if (iscsi_prep_scsi_cmd_pdu(ctask)) {
1211 sc->result = DID_ABORT << 16;
1212 sc->scsi_done = NULL;
1213 iscsi_complete_command(ctask);
1214 goto fault;
1215 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001216 if (session->tt->xmit_task(ctask)) {
Mike Christie052d0142008-05-21 15:54:05 -05001217 sc->scsi_done = NULL;
1218 iscsi_complete_command(ctask);
1219 reason = FAILURE_SESSION_NOT_READY;
1220 goto reject;
1221 }
1222 } else
1223 scsi_queue_work(session->host, &conn->xmitwork);
1224
1225 session->queued_cmdsn++;
1226 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001227 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001228 return 0;
1229
1230reject:
1231 spin_unlock(&session->lock);
1232 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001233 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001234 return SCSI_MLQUEUE_HOST_BUSY;
1235
1236fault:
1237 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001238 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001239 if (!scsi_bidi_cmnd(sc))
1240 scsi_set_resid(sc, scsi_bufflen(sc));
1241 else {
1242 scsi_out(sc)->resid = scsi_out(sc)->length;
1243 scsi_in(sc)->resid = scsi_in(sc)->length;
1244 }
Mike Christie052d0142008-05-21 15:54:05 -05001245 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001246 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001247 return 0;
1248}
1249EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1250
1251int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1252{
1253 if (depth > ISCSI_MAX_CMD_PER_LUN)
1254 depth = ISCSI_MAX_CMD_PER_LUN;
1255 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1256 return sdev->queue_depth;
1257}
1258EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1259
Mike Christie7996a772006-04-06 21:13:41 -05001260void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1261{
Mike Christie75613522008-05-21 15:53:59 -05001262 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001263
1264 spin_lock_bh(&session->lock);
1265 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001266 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001267 if (session->leadconn)
1268 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001269 }
1270 spin_unlock_bh(&session->lock);
1271}
1272EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1273
1274int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1275{
Mike Christie75613522008-05-21 15:53:59 -05001276 struct iscsi_cls_session *cls_session;
1277 struct iscsi_session *session;
1278 struct iscsi_conn *conn;
1279
1280 cls_session = starget_to_session(scsi_target(sc->device));
1281 session = cls_session->dd_data;
1282 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001283
Mike Christiebc436b22007-12-13 12:43:28 -06001284 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001285 spin_lock_bh(&session->lock);
1286 if (session->state == ISCSI_STATE_TERMINATE) {
1287failed:
1288 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001289 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001290 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001291 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001292 return FAILED;
1293 }
1294
Mike Christie7996a772006-04-06 21:13:41 -05001295 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001296 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001297 /*
1298 * we drop the lock here but the leadconn cannot be destoyed while
1299 * we are in the scsi eh
1300 */
Mike Christie843c0a82007-12-13 12:43:20 -06001301 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001302
1303 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1304 wait_event_interruptible(conn->ehwait,
1305 session->state == ISCSI_STATE_TERMINATE ||
1306 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001307 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001308 if (signal_pending(current))
1309 flush_signals(current);
1310
Mike Christiebc436b22007-12-13 12:43:28 -06001311 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001312 spin_lock_bh(&session->lock);
1313 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001314 iscsi_session_printk(KERN_INFO, session,
1315 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001316 else
1317 goto failed;
1318 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001319 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001320 return SUCCESS;
1321}
1322EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1323
Mike Christie843c0a82007-12-13 12:43:20 -06001324static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001325{
Mike Christie843c0a82007-12-13 12:43:20 -06001326 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001327 struct iscsi_session *session = conn->session;
1328
1329 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001330 if (conn->tmf_state == TMF_QUEUED) {
1331 conn->tmf_state = TMF_TIMEDOUT;
1332 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001333 /* unblock eh_abort() */
1334 wake_up(&conn->ehwait);
1335 }
1336 spin_unlock(&session->lock);
1337}
1338
Mike Christie3e5c28a2008-05-21 15:54:06 -05001339static int iscsi_exec_ctask_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001340 struct iscsi_tm *hdr, int age,
1341 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001342{
Mike Christie7996a772006-04-06 21:13:41 -05001343 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001344 struct iscsi_cmd_task *ctask;
Mike Christie7996a772006-04-06 21:13:41 -05001345
Mike Christie3e5c28a2008-05-21 15:54:06 -05001346 ctask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001347 NULL, 0);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001348 if (!ctask) {
Mike Christie6724add2007-08-15 01:38:30 -05001349 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001350 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001351 spin_lock_bh(&session->lock);
1352 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001353 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001354 }
Mike Christie843c0a82007-12-13 12:43:20 -06001355 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001356 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001357 conn->tmf_timer.function = iscsi_tmf_timedout;
1358 conn->tmf_timer.data = (unsigned long)conn;
1359 add_timer(&conn->tmf_timer);
1360 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001361
Mike Christie7996a772006-04-06 21:13:41 -05001362 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001363 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001364
1365 /*
1366 * block eh thread until:
1367 *
Mike Christie843c0a82007-12-13 12:43:20 -06001368 * 1) tmf response
1369 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001370 * 3) session is terminated or restarted or userspace has
1371 * given up on recovery
1372 */
Mike Christie843c0a82007-12-13 12:43:20 -06001373 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001374 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001375 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001376 if (signal_pending(current))
1377 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001378 del_timer_sync(&conn->tmf_timer);
1379
Mike Christie6724add2007-08-15 01:38:30 -05001380 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001381 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001382 /* if the session drops it will clean up the ctask */
Mike Christie843c0a82007-12-13 12:43:20 -06001383 if (age != session->age ||
1384 session->state != ISCSI_STATE_LOGGED_IN)
1385 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001386 return 0;
1387}
1388
1389/*
Mike Christie843c0a82007-12-13 12:43:20 -06001390 * Fail commands. session lock held and recv side suspended and xmit
1391 * thread flushed
1392 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001393static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1394 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001395{
1396 struct iscsi_cmd_task *ctask, *tmp;
1397
1398 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1399 conn->ctask = NULL;
1400
1401 /* flush pending */
1402 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1403 if (lun == ctask->sc->device->lun || lun == -1) {
1404 debug_scsi("failing pending sc %p itt 0x%x\n",
1405 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001406 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001407 }
1408 }
1409
1410 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1411 if (lun == ctask->sc->device->lun || lun == -1) {
1412 debug_scsi("failing requeued sc %p itt 0x%x\n",
1413 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001414 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001415 }
1416 }
1417
1418 /* fail all other running */
1419 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1420 if (lun == ctask->sc->device->lun || lun == -1) {
1421 debug_scsi("failing in progress sc %p itt 0x%x\n",
1422 ctask->sc, ctask->itt);
1423 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1424 }
1425 }
1426}
1427
Mike Christieb40977d2008-05-21 15:54:03 -05001428void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001429{
1430 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001431 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1432 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001433}
Mike Christieb40977d2008-05-21 15:54:03 -05001434EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001435
1436static void iscsi_start_tx(struct iscsi_conn *conn)
1437{
1438 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001439 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1440 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001441}
1442
Mike Christief6d51802007-12-13 12:43:30 -06001443static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1444{
1445 struct iscsi_cls_session *cls_session;
1446 struct iscsi_session *session;
1447 struct iscsi_conn *conn;
1448 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1449
1450 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001451 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001452
1453 debug_scsi("scsi cmd %p timedout\n", scmd);
1454
1455 spin_lock(&session->lock);
1456 if (session->state != ISCSI_STATE_LOGGED_IN) {
1457 /*
1458 * We are probably in the middle of iscsi recovery so let
1459 * that complete and handle the error.
1460 */
1461 rc = EH_RESET_TIMER;
1462 goto done;
1463 }
1464
1465 conn = session->leadconn;
1466 if (!conn) {
1467 /* In the middle of shuting down */
1468 rc = EH_RESET_TIMER;
1469 goto done;
1470 }
1471
1472 if (!conn->recv_timeout && !conn->ping_timeout)
1473 goto done;
1474 /*
1475 * if the ping timedout then we are in the middle of cleaning up
1476 * and can let the iscsi eh handle it
1477 */
1478 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1479 (conn->ping_timeout * HZ), jiffies))
1480 rc = EH_RESET_TIMER;
1481 /*
1482 * if we are about to check the transport then give the command
1483 * more time
1484 */
1485 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1486 jiffies))
1487 rc = EH_RESET_TIMER;
1488 /* if in the middle of checking the transport then give us more time */
Mike Christie3e5c28a2008-05-21 15:54:06 -05001489 if (conn->ping_ctask)
Mike Christief6d51802007-12-13 12:43:30 -06001490 rc = EH_RESET_TIMER;
1491done:
1492 spin_unlock(&session->lock);
1493 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1494 return rc;
1495}
1496
1497static void iscsi_check_transport_timeouts(unsigned long data)
1498{
1499 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1500 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001501 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001502
1503 spin_lock(&session->lock);
1504 if (session->state != ISCSI_STATE_LOGGED_IN)
1505 goto done;
1506
Mike Christie4cf10432008-05-07 20:43:52 -05001507 recv_timeout = conn->recv_timeout;
1508 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001509 goto done;
1510
Mike Christie4cf10432008-05-07 20:43:52 -05001511 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001512 last_recv = conn->last_recv;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001513 if (conn->ping_ctask &&
Mike Christie4cf10432008-05-07 20:43:52 -05001514 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001515 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001516 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1517 "expired, last rx %lu, last ping %lu, "
1518 "now %lu\n", conn->ping_timeout, last_recv,
1519 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001520 spin_unlock(&session->lock);
1521 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1522 return;
1523 }
1524
Mike Christie4cf10432008-05-07 20:43:52 -05001525 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001526 /* send a ping to try to provoke some traffic */
1527 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1528 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001529 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001530 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001531 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001532
Mike Christiead294e92008-01-31 13:36:50 -06001533 debug_scsi("Setting next tmo %lu\n", next_timeout);
1534 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001535done:
1536 spin_unlock(&session->lock);
1537}
1538
Mike Christie3e5c28a2008-05-21 15:54:06 -05001539static void iscsi_prep_abort_ctask_pdu(struct iscsi_cmd_task *ctask,
Mike Christie843c0a82007-12-13 12:43:20 -06001540 struct iscsi_tm *hdr)
1541{
1542 memset(hdr, 0, sizeof(*hdr));
1543 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1544 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1545 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1546 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1547 hdr->rtt = ctask->hdr->itt;
1548 hdr->refcmdsn = ctask->hdr->cmdsn;
1549}
1550
Mike Christie7996a772006-04-06 21:13:41 -05001551int iscsi_eh_abort(struct scsi_cmnd *sc)
1552{
Mike Christie75613522008-05-21 15:53:59 -05001553 struct iscsi_cls_session *cls_session;
1554 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001555 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001556 struct iscsi_cmd_task *ctask;
1557 struct iscsi_tm *hdr;
1558 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001559
Mike Christie75613522008-05-21 15:53:59 -05001560 cls_session = starget_to_session(scsi_target(sc->device));
1561 session = cls_session->dd_data;
1562
Mike Christie6724add2007-08-15 01:38:30 -05001563 mutex_lock(&session->eh_mutex);
1564 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001565 /*
1566 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1567 * got the command.
1568 */
1569 if (!sc->SCp.ptr) {
1570 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001571 spin_unlock_bh(&session->lock);
1572 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001573 return SUCCESS;
1574 }
1575
Mike Christie7996a772006-04-06 21:13:41 -05001576 /*
1577 * If we are not logged in or we have started a new session
1578 * then let the host reset code handle this
1579 */
Mike Christie843c0a82007-12-13 12:43:20 -06001580 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1581 sc->SCp.phase != session->age) {
1582 spin_unlock_bh(&session->lock);
1583 mutex_unlock(&session->eh_mutex);
1584 return FAILED;
1585 }
1586
1587 conn = session->leadconn;
1588 conn->eh_abort_cnt++;
1589 age = session->age;
1590
1591 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1592 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001593
1594 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001595 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001596 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001597 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001598 }
Mike Christie7996a772006-04-06 21:13:41 -05001599
Mike Christie77a23c22007-05-30 12:57:18 -05001600 if (ctask->state == ISCSI_TASK_PENDING) {
1601 fail_command(conn, ctask, DID_ABORT << 16);
1602 goto success;
1603 }
Mike Christie7996a772006-04-06 21:13:41 -05001604
Mike Christie843c0a82007-12-13 12:43:20 -06001605 /* only have one tmf outstanding at a time */
1606 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001607 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001608 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001609
Mike Christie843c0a82007-12-13 12:43:20 -06001610 hdr = &conn->tmhdr;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001611 iscsi_prep_abort_ctask_pdu(ctask, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001612
Mike Christie3e5c28a2008-05-21 15:54:06 -05001613 if (iscsi_exec_ctask_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001614 rc = FAILED;
1615 goto failed;
1616 }
1617
1618 switch (conn->tmf_state) {
1619 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001620 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001621 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001622 /*
Mike Christie3e5c28a2008-05-21 15:54:06 -05001623 * clean up ctask if aborted. grab the recv lock as a writer
Mike Christie77a23c22007-05-30 12:57:18 -05001624 */
1625 write_lock_bh(conn->recv_lock);
1626 spin_lock(&session->lock);
1627 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001628 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001629 spin_unlock(&session->lock);
1630 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001631 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001632 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001633 case TMF_TIMEDOUT:
1634 spin_unlock_bh(&session->lock);
1635 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1636 goto failed_unlocked;
1637 case TMF_NOT_FOUND:
1638 if (!sc->SCp.ptr) {
1639 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001640 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001641 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001642 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001643 }
1644 /* fall through */
1645 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001646 conn->tmf_state = TMF_INITIAL;
1647 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001648 }
1649
Mike Christie77a23c22007-05-30 12:57:18 -05001650success:
Mike Christie7996a772006-04-06 21:13:41 -05001651 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001652success_unlocked:
1653 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001654 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001655 return SUCCESS;
1656
1657failed:
1658 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001659failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001660 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1661 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001662 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001663 return FAILED;
1664}
1665EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1666
Mike Christie843c0a82007-12-13 12:43:20 -06001667static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1668{
1669 memset(hdr, 0, sizeof(*hdr));
1670 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1671 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1672 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1673 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001674 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001675}
1676
1677int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1678{
Mike Christie75613522008-05-21 15:53:59 -05001679 struct iscsi_cls_session *cls_session;
1680 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001681 struct iscsi_conn *conn;
1682 struct iscsi_tm *hdr;
1683 int rc = FAILED;
1684
Mike Christie75613522008-05-21 15:53:59 -05001685 cls_session = starget_to_session(scsi_target(sc->device));
1686 session = cls_session->dd_data;
1687
Mike Christie843c0a82007-12-13 12:43:20 -06001688 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1689
1690 mutex_lock(&session->eh_mutex);
1691 spin_lock_bh(&session->lock);
1692 /*
1693 * Just check if we are not logged in. We cannot check for
1694 * the phase because the reset could come from a ioctl.
1695 */
1696 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1697 goto unlock;
1698 conn = session->leadconn;
1699
1700 /* only have one tmf outstanding at a time */
1701 if (conn->tmf_state != TMF_INITIAL)
1702 goto unlock;
1703 conn->tmf_state = TMF_QUEUED;
1704
1705 hdr = &conn->tmhdr;
1706 iscsi_prep_lun_reset_pdu(sc, hdr);
1707
Mike Christie3e5c28a2008-05-21 15:54:06 -05001708 if (iscsi_exec_ctask_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001709 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001710 rc = FAILED;
1711 goto unlock;
1712 }
1713
1714 switch (conn->tmf_state) {
1715 case TMF_SUCCESS:
1716 break;
1717 case TMF_TIMEDOUT:
1718 spin_unlock_bh(&session->lock);
1719 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1720 goto done;
1721 default:
1722 conn->tmf_state = TMF_INITIAL;
1723 goto unlock;
1724 }
1725
1726 rc = SUCCESS;
1727 spin_unlock_bh(&session->lock);
1728
1729 iscsi_suspend_tx(conn);
1730 /* need to grab the recv lock then session lock */
1731 write_lock_bh(conn->recv_lock);
1732 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001733 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001734 conn->tmf_state = TMF_INITIAL;
1735 spin_unlock(&session->lock);
1736 write_unlock_bh(conn->recv_lock);
1737
1738 iscsi_start_tx(conn);
1739 goto done;
1740
1741unlock:
1742 spin_unlock_bh(&session->lock);
1743done:
1744 debug_scsi("iscsi_eh_device_reset %s\n",
1745 rc == SUCCESS ? "SUCCESS" : "FAILED");
1746 mutex_unlock(&session->eh_mutex);
1747 return rc;
1748}
1749EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1750
Olaf Kirch63203772007-12-13 12:43:25 -06001751/*
1752 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1753 * should be accessed via kfifo_{get,put} on q->queue.
1754 * Optionally, the caller can obtain the array of object pointers
1755 * by passing in a non-NULL @items pointer
1756 */
Mike Christie7996a772006-04-06 21:13:41 -05001757int
Olaf Kirch63203772007-12-13 12:43:25 -06001758iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001759{
Olaf Kirch63203772007-12-13 12:43:25 -06001760 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001761
Olaf Kirch63203772007-12-13 12:43:25 -06001762 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001763
1764 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001765
1766 /* If the user passed an items pointer, he wants a copy of
1767 * the array. */
1768 if (items)
1769 num_arrays++;
1770 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1771 if (q->pool == NULL)
1772 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001773
1774 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1775 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001776 if (q->queue == ERR_PTR(-ENOMEM))
1777 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001778
1779 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001780 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001781 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001782 q->max = i;
1783 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001784 }
Mike Christie7996a772006-04-06 21:13:41 -05001785 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1786 }
Olaf Kirch63203772007-12-13 12:43:25 -06001787
1788 if (items) {
1789 *items = q->pool + max;
1790 memcpy(*items, q->pool, max * sizeof(void *));
1791 }
1792
Mike Christie7996a772006-04-06 21:13:41 -05001793 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001794
1795enomem:
1796 iscsi_pool_free(q);
1797 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001798}
1799EXPORT_SYMBOL_GPL(iscsi_pool_init);
1800
Olaf Kirch63203772007-12-13 12:43:25 -06001801void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001802{
1803 int i;
1804
1805 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001806 kfree(q->pool[i]);
1807 if (q->pool)
1808 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001809}
1810EXPORT_SYMBOL_GPL(iscsi_pool_free);
1811
Mike Christiea4804cd2008-05-21 15:54:00 -05001812/**
1813 * iscsi_host_add - add host to system
1814 * @shost: scsi host
1815 * @pdev: parent device
1816 *
1817 * This should be called by partial offload and software iscsi drivers
1818 * to add a host to the system.
1819 */
1820int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001821{
Mike Christiea4804cd2008-05-21 15:54:00 -05001822 return scsi_add_host(shost, pdev);
1823}
1824EXPORT_SYMBOL_GPL(iscsi_host_add);
1825
1826/**
1827 * iscsi_host_alloc - allocate a host and driver data
1828 * @sht: scsi host template
1829 * @dd_data_size: driver host data size
1830 * @qdepth: default device queue depth
1831 *
1832 * This should be called by partial offload and software iscsi drivers.
1833 * To access the driver specific memory use the iscsi_host_priv() macro.
1834 */
1835struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1836 int dd_data_size, uint16_t qdepth)
1837{
1838 struct Scsi_Host *shost;
1839
1840 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1841 if (!shost)
1842 return NULL;
1843 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1844
Mike Christie15482712007-05-30 12:57:19 -05001845 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1846 if (qdepth != 0)
1847 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001848 "Queue depth must be between 1 and %d.\n",
1849 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001850 qdepth = ISCSI_DEF_CMD_PER_LUN;
1851 }
Mike Christie75613522008-05-21 15:53:59 -05001852 shost->cmd_per_lun = qdepth;
Mike Christiea4804cd2008-05-21 15:54:00 -05001853 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001854}
Mike Christiea4804cd2008-05-21 15:54:00 -05001855EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001856
Mike Christiea4804cd2008-05-21 15:54:00 -05001857/**
1858 * iscsi_host_remove - remove host and sessions
1859 * @shost: scsi host
1860 *
1861 * This will also remove any sessions attached to the host, but if userspace
1862 * is managing the session at the same time this will break. TODO: add
1863 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1864 * does not remove the memory from under us.
1865 */
1866void iscsi_host_remove(struct Scsi_Host *shost)
1867{
1868 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1869 scsi_remove_host(shost);
1870}
1871EXPORT_SYMBOL_GPL(iscsi_host_remove);
1872
1873void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001874{
1875 struct iscsi_host *ihost = shost_priv(shost);
1876
1877 kfree(ihost->netdev);
1878 kfree(ihost->hwaddress);
1879 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001880 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001881}
Mike Christiea4804cd2008-05-21 15:54:00 -05001882EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05001883
1884/**
1885 * iscsi_session_setup - create iscsi cls session and host and session
1886 * @iscsit: iscsi transport template
1887 * @shost: scsi host
1888 * @cmds_max: session can queue
Mike Christie3e5c28a2008-05-21 15:54:06 -05001889 * @cmd_ctask_size: LLD ctask private data size
Mike Christie75613522008-05-21 15:53:59 -05001890 * @initial_cmdsn: initial CmdSN
1891 *
1892 * This can be used by software iscsi_transports that allocate
1893 * a session per scsi host.
1894 */
1895struct iscsi_cls_session *
1896iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3e5c28a2008-05-21 15:54:06 -05001897 uint16_t scsi_cmds_max, int cmd_ctask_size,
Mike Christie75613522008-05-21 15:53:59 -05001898 uint32_t initial_cmdsn)
1899{
1900 struct iscsi_session *session;
1901 struct iscsi_cls_session *cls_session;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001902 int cmd_i, cmds_max;
Mike Christie75613522008-05-21 15:53:59 -05001903
Mike Christie3e5c28a2008-05-21 15:54:06 -05001904 /*
1905 * The iscsi layer needs some ctasks for nop handling and tmfs.
1906 */
1907 if (scsi_cmds_max < 1)
1908 scsi_cmds_max = ISCSI_MGMT_CMDS_MAX;
1909 if ((scsi_cmds_max + ISCSI_MGMT_CMDS_MAX) >= ISCSI_MGMT_ITT_OFFSET) {
1910 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1911 "can_queue must be less than %d.\n",
1912 scsi_cmds_max,
1913 ISCSI_MGMT_ITT_OFFSET - ISCSI_MGMT_CMDS_MAX);
1914 scsi_cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001915 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001916 cmds_max = roundup_pow_of_two(scsi_cmds_max + ISCSI_MGMT_CMDS_MAX);
Mike Christie15482712007-05-30 12:57:19 -05001917
Mike Christie5d91e202008-05-21 15:54:01 -05001918 cls_session = iscsi_alloc_session(shost, iscsit,
1919 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05001920 if (!cls_session)
Mike Christie7996a772006-04-06 21:13:41 -05001921 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001922 session = cls_session->dd_data;
1923 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001924 session->host = shost;
1925 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001926 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001927 session->lu_reset_timeout = 15;
1928 session->abort_timeout = 10;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001929 session->scsi_cmds_max = scsi_cmds_max;
Mike Christie15482712007-05-30 12:57:19 -05001930 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001931 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001932 session->exp_cmdsn = initial_cmdsn + 1;
1933 session->max_cmdsn = initial_cmdsn + 1;
1934 session->max_r2t = 1;
1935 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001936 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05001937 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001938
1939 /* initialize SCSI PDU commands pool */
1940 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1941 (void***)&session->cmds,
Mike Christie3e5c28a2008-05-21 15:54:06 -05001942 cmd_ctask_size + sizeof(struct iscsi_cmd_task)))
Mike Christie7996a772006-04-06 21:13:41 -05001943 goto cmdpool_alloc_fail;
1944
1945 /* pre-format cmds pool with ITT */
1946 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1947 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1948
Mike Christie3e5c28a2008-05-21 15:54:06 -05001949 if (cmd_ctask_size)
Mike Christie7996a772006-04-06 21:13:41 -05001950 ctask->dd_data = &ctask[1];
1951 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001952 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001953 }
1954
Mike Christief53a88d2006-06-28 12:00:27 -05001955 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05001956 goto module_get_fail;
1957
1958 if (iscsi_add_session(cls_session, 0))
Mike Christief53a88d2006-06-28 12:00:27 -05001959 goto cls_session_fail;
Mike Christie7996a772006-04-06 21:13:41 -05001960 return cls_session;
1961
1962cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05001963 module_put(iscsit->owner);
1964module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001965 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001966cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05001967 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001968 return NULL;
1969}
1970EXPORT_SYMBOL_GPL(iscsi_session_setup);
1971
1972/**
1973 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05001974 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05001975 *
Mike Christie75613522008-05-21 15:53:59 -05001976 * The driver must have called iscsi_remove_session before
1977 * calling this.
1978 */
Mike Christie7996a772006-04-06 21:13:41 -05001979void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1980{
Mike Christie75613522008-05-21 15:53:59 -05001981 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05001982 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001983
Olaf Kirch63203772007-12-13 12:43:25 -06001984 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001985
Mike Christieb2c64162007-05-30 12:57:16 -05001986 kfree(session->password);
1987 kfree(session->password_in);
1988 kfree(session->username);
1989 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001990 kfree(session->targetname);
1991
Mike Christie75613522008-05-21 15:53:59 -05001992 iscsi_destroy_session(cls_session);
Mike Christie63f75cc2006-07-24 15:47:29 -05001993 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001994}
1995EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1996
1997/**
1998 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1999 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002000 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002001 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002002 */
Mike Christie7996a772006-04-06 21:13:41 -05002003struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002004iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2005 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002006{
Mike Christie75613522008-05-21 15:53:59 -05002007 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002008 struct iscsi_conn *conn;
2009 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002010 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002011
Mike Christie5d91e202008-05-21 15:54:01 -05002012 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2013 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002014 if (!cls_conn)
2015 return NULL;
2016 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002017 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002018
Mike Christie5d91e202008-05-21 15:54:01 -05002019 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002020 conn->session = session;
2021 conn->cls_conn = cls_conn;
2022 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2023 conn->id = conn_idx;
2024 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002025 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002026
2027 init_timer(&conn->transport_timer);
2028 conn->transport_timer.data = (unsigned long)conn;
2029 conn->transport_timer.function = iscsi_check_transport_timeouts;
2030
Mike Christie7996a772006-04-06 21:13:41 -05002031 INIT_LIST_HEAD(&conn->run_list);
2032 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002033 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002034 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002035 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002036 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002037
Mike Christie3e5c28a2008-05-21 15:54:06 -05002038 /* allocate login_ctask used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002039 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002040 if (!__kfifo_get(session->cmdpool.queue,
2041 (void*)&conn->login_ctask,
Mike Christie7996a772006-04-06 21:13:41 -05002042 sizeof(void*))) {
2043 spin_unlock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002044 goto login_ctask_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002045 }
2046 spin_unlock_bh(&session->lock);
2047
Mike Christiebf32ed32007-02-28 17:32:17 -06002048 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002049 if (!data)
Mike Christie3e5c28a2008-05-21 15:54:06 -05002050 goto login_ctask_data_alloc_fail;
2051 conn->login_ctask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002052
Mike Christie843c0a82007-12-13 12:43:20 -06002053 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002054 init_waitqueue_head(&conn->ehwait);
2055
2056 return cls_conn;
2057
Mike Christie3e5c28a2008-05-21 15:54:06 -05002058login_ctask_data_alloc_fail:
2059 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_ctask,
Mike Christied36ab6f2006-05-18 20:31:34 -05002060 sizeof(void*));
Mike Christie3e5c28a2008-05-21 15:54:06 -05002061login_ctask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002062 iscsi_destroy_conn(cls_conn);
2063 return NULL;
2064}
2065EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2066
2067/**
2068 * iscsi_conn_teardown - teardown iscsi connection
2069 * cls_conn: iscsi class connection
2070 *
2071 * TODO: we may need to make this into a two step process
2072 * like scsi-mls remove + put host
2073 */
2074void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2075{
2076 struct iscsi_conn *conn = cls_conn->dd_data;
2077 struct iscsi_session *session = conn->session;
2078 unsigned long flags;
2079
Mike Christief6d51802007-12-13 12:43:30 -06002080 del_timer_sync(&conn->transport_timer);
2081
Mike Christie7996a772006-04-06 21:13:41 -05002082 spin_lock_bh(&session->lock);
2083 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2084 if (session->leadconn == conn) {
2085 /*
2086 * leading connection? then give up on recovery.
2087 */
2088 session->state = ISCSI_STATE_TERMINATE;
2089 wake_up(&conn->ehwait);
2090 }
2091 spin_unlock_bh(&session->lock);
2092
Mike Christie7996a772006-04-06 21:13:41 -05002093 /*
2094 * Block until all in-progress commands for this connection
2095 * time out or fail.
2096 */
2097 for (;;) {
2098 spin_lock_irqsave(session->host->host_lock, flags);
2099 if (!session->host->host_busy) { /* OK for ERL == 0 */
2100 spin_unlock_irqrestore(session->host->host_lock, flags);
2101 break;
2102 }
2103 spin_unlock_irqrestore(session->host->host_lock, flags);
2104 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002105 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2106 "host_busy %d host_failed %d\n",
2107 session->host->host_busy,
2108 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002109 /*
2110 * force eh_abort() to unblock
2111 */
2112 wake_up(&conn->ehwait);
2113 }
2114
Mike Christie779ea122007-02-28 17:32:15 -06002115 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002116 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002117
Mike Christie7996a772006-04-06 21:13:41 -05002118 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002119 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002120 kfree(conn->persistent_address);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002121 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_ctask,
Mike Christie7996a772006-04-06 21:13:41 -05002122 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002123 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002124 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002125 spin_unlock_bh(&session->lock);
2126
Mike Christie7996a772006-04-06 21:13:41 -05002127 iscsi_destroy_conn(cls_conn);
2128}
2129EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2130
2131int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2132{
2133 struct iscsi_conn *conn = cls_conn->dd_data;
2134 struct iscsi_session *session = conn->session;
2135
Mike Christieffd04362006-08-31 18:09:24 -04002136 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002137 iscsi_conn_printk(KERN_ERR, conn,
2138 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002139 return -EPERM;
2140 }
2141
Mike Christiedb98ccd2006-08-31 18:09:31 -04002142 if ((session->imm_data_en || !session->initial_r2t_en) &&
2143 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002144 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2145 "first_burst %d max_burst %d\n",
2146 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002147 return -EINVAL;
2148 }
2149
Mike Christief6d51802007-12-13 12:43:30 -06002150 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002151 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2152 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002153 conn->recv_timeout = 5;
2154 }
2155
2156 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002157 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2158 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002159 conn->ping_timeout = 5;
2160 }
2161
Mike Christie7996a772006-04-06 21:13:41 -05002162 spin_lock_bh(&session->lock);
2163 conn->c_stage = ISCSI_CONN_STARTED;
2164 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002165 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002166
Mike Christief6d51802007-12-13 12:43:30 -06002167 conn->last_recv = jiffies;
2168 conn->last_ping = jiffies;
2169 if (conn->recv_timeout && conn->ping_timeout)
2170 mod_timer(&conn->transport_timer,
2171 jiffies + (conn->recv_timeout * HZ));
2172
Mike Christie7996a772006-04-06 21:13:41 -05002173 switch(conn->stop_stage) {
2174 case STOP_CONN_RECOVER:
2175 /*
2176 * unblock eh_abort() if it is blocked. re-try all
2177 * commands after successful recovery
2178 */
Mike Christie7996a772006-04-06 21:13:41 -05002179 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002180 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002181 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002182 if (session->age == 16)
2183 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002184 break;
Mike Christie7996a772006-04-06 21:13:41 -05002185 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002186 conn->stop_stage = 0;
2187 break;
Mike Christie7996a772006-04-06 21:13:41 -05002188 default:
2189 break;
2190 }
2191 spin_unlock_bh(&session->lock);
2192
Mike Christie75613522008-05-21 15:53:59 -05002193 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002194 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002195 return 0;
2196}
2197EXPORT_SYMBOL_GPL(iscsi_conn_start);
2198
2199static void
2200flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2201{
Mike Christie3e5c28a2008-05-21 15:54:06 -05002202 struct iscsi_cmd_task *ctask, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002203
2204 /* handle pending */
Mike Christie3e5c28a2008-05-21 15:54:06 -05002205 list_for_each_entry_safe(ctask, tmp, &conn->mgmtqueue, running) {
2206 debug_scsi("flushing pending mgmt ctask itt 0x%x\n", ctask->itt);
2207 /* release ref from prep ctask */
2208 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05002209 }
2210
2211 /* handle running */
Mike Christie3e5c28a2008-05-21 15:54:06 -05002212 list_for_each_entry_safe(ctask, tmp, &conn->mgmt_run_list, running) {
2213 debug_scsi("flushing running mgmt ctask itt 0x%x\n", ctask->itt);
2214 /* release ref from prep ctask */
2215 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05002216 }
2217
Mike Christie3e5c28a2008-05-21 15:54:06 -05002218 conn->ctask = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002219}
2220
Mike Christie656cffc2006-05-18 20:31:42 -05002221static void iscsi_start_session_recovery(struct iscsi_session *session,
2222 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002223{
Mike Christieed2abc72006-05-02 19:46:40 -05002224 int old_stop_stage;
2225
Mike Christief6d51802007-12-13 12:43:30 -06002226 del_timer_sync(&conn->transport_timer);
2227
Mike Christie6724add2007-08-15 01:38:30 -05002228 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002229 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002230 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002231 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002232 mutex_unlock(&session->eh_mutex);
2233 return;
2234 }
2235
2236 /*
2237 * The LLD either freed/unset the lock on us, or userspace called
2238 * stop but did not create a proper connection (connection was never
2239 * bound or it was unbound then stop was called).
2240 */
2241 if (!conn->recv_lock) {
2242 spin_unlock_bh(&session->lock);
2243 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002244 return;
2245 }
Mike Christieed2abc72006-05-02 19:46:40 -05002246
2247 /*
2248 * When this is called for the in_login state, we only want to clean
Mike Christie3e5c28a2008-05-21 15:54:06 -05002249 * up the login ctask and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002250 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002251 */
Mike Christie67a61112006-05-30 00:37:20 -05002252 if (flag == STOP_CONN_TERM)
2253 session->state = ISCSI_STATE_TERMINATE;
2254 else if (conn->stop_stage != STOP_CONN_RECOVER)
2255 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002256
2257 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002258 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002259 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002260 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002261
2262 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002263
Mike Christie1c834692006-07-24 15:47:26 -05002264 write_lock_bh(conn->recv_lock);
2265 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2266 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002267
Mike Christie7996a772006-04-06 21:13:41 -05002268 /*
2269 * for connection level recovery we should not calculate
2270 * header digest. conn->hdr_size used for optimization
2271 * in hdr_extract() and will be re-negotiated at
2272 * set_param() time.
2273 */
2274 if (flag == STOP_CONN_RECOVER) {
2275 conn->hdrdgst_en = 0;
2276 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002277 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002278 old_stop_stage != STOP_CONN_RECOVER) {
2279 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002280 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002281 }
Mike Christie7996a772006-04-06 21:13:41 -05002282 }
Mike Christie656cffc2006-05-18 20:31:42 -05002283
Mike Christie656cffc2006-05-18 20:31:42 -05002284 /*
2285 * flush queues.
2286 */
2287 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002288 fail_all_commands(conn, -1,
2289 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002290 flush_control_queues(session, conn);
2291 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002292 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002293}
Mike Christie7996a772006-04-06 21:13:41 -05002294
2295void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2296{
2297 struct iscsi_conn *conn = cls_conn->dd_data;
2298 struct iscsi_session *session = conn->session;
2299
2300 switch (flag) {
2301 case STOP_CONN_RECOVER:
2302 case STOP_CONN_TERM:
2303 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002304 break;
Mike Christie7996a772006-04-06 21:13:41 -05002305 default:
Mike Christie322d7392008-01-31 13:36:52 -06002306 iscsi_conn_printk(KERN_ERR, conn,
2307 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002308 }
2309}
2310EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2311
2312int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2313 struct iscsi_cls_conn *cls_conn, int is_leading)
2314{
Mike Christie75613522008-05-21 15:53:59 -05002315 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002316 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002317
Mike Christie7996a772006-04-06 21:13:41 -05002318 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002319 if (is_leading)
2320 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002321 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002322
2323 /*
2324 * Unblock xmitworker(), Login Phase will pass through.
2325 */
2326 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2327 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2328 return 0;
2329}
2330EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2331
Mike Christiea54a52c2006-06-28 12:00:23 -05002332
2333int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2334 enum iscsi_param param, char *buf, int buflen)
2335{
2336 struct iscsi_conn *conn = cls_conn->dd_data;
2337 struct iscsi_session *session = conn->session;
2338 uint32_t value;
2339
2340 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002341 case ISCSI_PARAM_FAST_ABORT:
2342 sscanf(buf, "%d", &session->fast_abort);
2343 break;
Mike Christief6d51802007-12-13 12:43:30 -06002344 case ISCSI_PARAM_ABORT_TMO:
2345 sscanf(buf, "%d", &session->abort_timeout);
2346 break;
2347 case ISCSI_PARAM_LU_RESET_TMO:
2348 sscanf(buf, "%d", &session->lu_reset_timeout);
2349 break;
2350 case ISCSI_PARAM_PING_TMO:
2351 sscanf(buf, "%d", &conn->ping_timeout);
2352 break;
2353 case ISCSI_PARAM_RECV_TMO:
2354 sscanf(buf, "%d", &conn->recv_timeout);
2355 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002356 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2357 sscanf(buf, "%d", &conn->max_recv_dlength);
2358 break;
2359 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2360 sscanf(buf, "%d", &conn->max_xmit_dlength);
2361 break;
2362 case ISCSI_PARAM_HDRDGST_EN:
2363 sscanf(buf, "%d", &conn->hdrdgst_en);
2364 break;
2365 case ISCSI_PARAM_DATADGST_EN:
2366 sscanf(buf, "%d", &conn->datadgst_en);
2367 break;
2368 case ISCSI_PARAM_INITIAL_R2T_EN:
2369 sscanf(buf, "%d", &session->initial_r2t_en);
2370 break;
2371 case ISCSI_PARAM_MAX_R2T:
2372 sscanf(buf, "%d", &session->max_r2t);
2373 break;
2374 case ISCSI_PARAM_IMM_DATA_EN:
2375 sscanf(buf, "%d", &session->imm_data_en);
2376 break;
2377 case ISCSI_PARAM_FIRST_BURST:
2378 sscanf(buf, "%d", &session->first_burst);
2379 break;
2380 case ISCSI_PARAM_MAX_BURST:
2381 sscanf(buf, "%d", &session->max_burst);
2382 break;
2383 case ISCSI_PARAM_PDU_INORDER_EN:
2384 sscanf(buf, "%d", &session->pdu_inorder_en);
2385 break;
2386 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2387 sscanf(buf, "%d", &session->dataseq_inorder_en);
2388 break;
2389 case ISCSI_PARAM_ERL:
2390 sscanf(buf, "%d", &session->erl);
2391 break;
2392 case ISCSI_PARAM_IFMARKER_EN:
2393 sscanf(buf, "%d", &value);
2394 BUG_ON(value);
2395 break;
2396 case ISCSI_PARAM_OFMARKER_EN:
2397 sscanf(buf, "%d", &value);
2398 BUG_ON(value);
2399 break;
2400 case ISCSI_PARAM_EXP_STATSN:
2401 sscanf(buf, "%u", &conn->exp_statsn);
2402 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002403 case ISCSI_PARAM_USERNAME:
2404 kfree(session->username);
2405 session->username = kstrdup(buf, GFP_KERNEL);
2406 if (!session->username)
2407 return -ENOMEM;
2408 break;
2409 case ISCSI_PARAM_USERNAME_IN:
2410 kfree(session->username_in);
2411 session->username_in = kstrdup(buf, GFP_KERNEL);
2412 if (!session->username_in)
2413 return -ENOMEM;
2414 break;
2415 case ISCSI_PARAM_PASSWORD:
2416 kfree(session->password);
2417 session->password = kstrdup(buf, GFP_KERNEL);
2418 if (!session->password)
2419 return -ENOMEM;
2420 break;
2421 case ISCSI_PARAM_PASSWORD_IN:
2422 kfree(session->password_in);
2423 session->password_in = kstrdup(buf, GFP_KERNEL);
2424 if (!session->password_in)
2425 return -ENOMEM;
2426 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002427 case ISCSI_PARAM_TARGET_NAME:
2428 /* this should not change between logins */
2429 if (session->targetname)
2430 break;
2431
2432 session->targetname = kstrdup(buf, GFP_KERNEL);
2433 if (!session->targetname)
2434 return -ENOMEM;
2435 break;
2436 case ISCSI_PARAM_TPGT:
2437 sscanf(buf, "%d", &session->tpgt);
2438 break;
2439 case ISCSI_PARAM_PERSISTENT_PORT:
2440 sscanf(buf, "%d", &conn->persistent_port);
2441 break;
2442 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2443 /*
2444 * this is the address returned in discovery so it should
2445 * not change between logins.
2446 */
2447 if (conn->persistent_address)
2448 break;
2449
2450 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2451 if (!conn->persistent_address)
2452 return -ENOMEM;
2453 break;
2454 default:
2455 return -ENOSYS;
2456 }
2457
2458 return 0;
2459}
2460EXPORT_SYMBOL_GPL(iscsi_set_param);
2461
2462int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2463 enum iscsi_param param, char *buf)
2464{
Mike Christie75613522008-05-21 15:53:59 -05002465 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002466 int len;
2467
2468 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002469 case ISCSI_PARAM_FAST_ABORT:
2470 len = sprintf(buf, "%d\n", session->fast_abort);
2471 break;
Mike Christief6d51802007-12-13 12:43:30 -06002472 case ISCSI_PARAM_ABORT_TMO:
2473 len = sprintf(buf, "%d\n", session->abort_timeout);
2474 break;
2475 case ISCSI_PARAM_LU_RESET_TMO:
2476 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2477 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002478 case ISCSI_PARAM_INITIAL_R2T_EN:
2479 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2480 break;
2481 case ISCSI_PARAM_MAX_R2T:
2482 len = sprintf(buf, "%hu\n", session->max_r2t);
2483 break;
2484 case ISCSI_PARAM_IMM_DATA_EN:
2485 len = sprintf(buf, "%d\n", session->imm_data_en);
2486 break;
2487 case ISCSI_PARAM_FIRST_BURST:
2488 len = sprintf(buf, "%u\n", session->first_burst);
2489 break;
2490 case ISCSI_PARAM_MAX_BURST:
2491 len = sprintf(buf, "%u\n", session->max_burst);
2492 break;
2493 case ISCSI_PARAM_PDU_INORDER_EN:
2494 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2495 break;
2496 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2497 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2498 break;
2499 case ISCSI_PARAM_ERL:
2500 len = sprintf(buf, "%d\n", session->erl);
2501 break;
2502 case ISCSI_PARAM_TARGET_NAME:
2503 len = sprintf(buf, "%s\n", session->targetname);
2504 break;
2505 case ISCSI_PARAM_TPGT:
2506 len = sprintf(buf, "%d\n", session->tpgt);
2507 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002508 case ISCSI_PARAM_USERNAME:
2509 len = sprintf(buf, "%s\n", session->username);
2510 break;
2511 case ISCSI_PARAM_USERNAME_IN:
2512 len = sprintf(buf, "%s\n", session->username_in);
2513 break;
2514 case ISCSI_PARAM_PASSWORD:
2515 len = sprintf(buf, "%s\n", session->password);
2516 break;
2517 case ISCSI_PARAM_PASSWORD_IN:
2518 len = sprintf(buf, "%s\n", session->password_in);
2519 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002520 default:
2521 return -ENOSYS;
2522 }
2523
2524 return len;
2525}
2526EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2527
2528int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2529 enum iscsi_param param, char *buf)
2530{
2531 struct iscsi_conn *conn = cls_conn->dd_data;
2532 int len;
2533
2534 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002535 case ISCSI_PARAM_PING_TMO:
2536 len = sprintf(buf, "%u\n", conn->ping_timeout);
2537 break;
2538 case ISCSI_PARAM_RECV_TMO:
2539 len = sprintf(buf, "%u\n", conn->recv_timeout);
2540 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002541 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2542 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2543 break;
2544 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2545 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2546 break;
2547 case ISCSI_PARAM_HDRDGST_EN:
2548 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2549 break;
2550 case ISCSI_PARAM_DATADGST_EN:
2551 len = sprintf(buf, "%d\n", conn->datadgst_en);
2552 break;
2553 case ISCSI_PARAM_IFMARKER_EN:
2554 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2555 break;
2556 case ISCSI_PARAM_OFMARKER_EN:
2557 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2558 break;
2559 case ISCSI_PARAM_EXP_STATSN:
2560 len = sprintf(buf, "%u\n", conn->exp_statsn);
2561 break;
2562 case ISCSI_PARAM_PERSISTENT_PORT:
2563 len = sprintf(buf, "%d\n", conn->persistent_port);
2564 break;
2565 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2566 len = sprintf(buf, "%s\n", conn->persistent_address);
2567 break;
2568 default:
2569 return -ENOSYS;
2570 }
2571
2572 return len;
2573}
2574EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2575
Mike Christie0801c242007-05-30 12:57:12 -05002576int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2577 char *buf)
2578{
Mike Christie75613522008-05-21 15:53:59 -05002579 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002580 int len;
2581
2582 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002583 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002584 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002585 len = sprintf(buf, "%s\n", "default");
2586 else
Mike Christie75613522008-05-21 15:53:59 -05002587 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002588 break;
Mike Christie0801c242007-05-30 12:57:12 -05002589 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002590 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002591 len = sprintf(buf, "%s\n", "default");
2592 else
Mike Christie75613522008-05-21 15:53:59 -05002593 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002594 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002595 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002596 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002597 len = sprintf(buf, "%s\n", "unknown");
2598 else
Mike Christie75613522008-05-21 15:53:59 -05002599 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002600 break;
Mike Christie75613522008-05-21 15:53:59 -05002601 case ISCSI_HOST_PARAM_IPADDRESS:
2602 if (!strlen(ihost->local_address))
2603 len = sprintf(buf, "%s\n", "unknown");
2604 else
2605 len = sprintf(buf, "%s\n",
2606 ihost->local_address);
Mike Christie0801c242007-05-30 12:57:12 -05002607 default:
2608 return -ENOSYS;
2609 }
2610
2611 return len;
2612}
2613EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2614
2615int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2616 char *buf, int buflen)
2617{
Mike Christie75613522008-05-21 15:53:59 -05002618 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002619
2620 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002621 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002622 if (!ihost->netdev)
2623 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002624 break;
Mike Christie0801c242007-05-30 12:57:12 -05002625 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002626 if (!ihost->hwaddress)
2627 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002628 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002629 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002630 if (!ihost->initiatorname)
2631 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002632 break;
Mike Christie0801c242007-05-30 12:57:12 -05002633 default:
2634 return -ENOSYS;
2635 }
2636
2637 return 0;
2638}
2639EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2640
Mike Christie7996a772006-04-06 21:13:41 -05002641MODULE_AUTHOR("Mike Christie");
2642MODULE_DESCRIPTION("iSCSI library functions");
2643MODULE_LICENSE("GPL");