blob: 010c1b9b178cb5ae5ea0e9f54e3a631c210f0a7e [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
41struct iscsi_session *
42class_to_transport_session(struct iscsi_cls_session *cls_session)
43{
44 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
45 return iscsi_hostdata(shost->hostdata);
46}
47EXPORT_SYMBOL_GPL(class_to_transport_session);
48
Mike Christie77a23c22007-05-30 12:57:18 -050049/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
50#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050051
Mike Christie77a23c22007-05-30 12:57:18 -050052static int iscsi_sna_lt(u32 n1, u32 n2)
53{
54 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
55 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
56}
57
58/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
59static int iscsi_sna_lte(u32 n1, u32 n2)
60{
61 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
62 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
63}
64
65void
66iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050067{
68 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
69 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
70
Mike Christie77a23c22007-05-30 12:57:18 -050071 /*
72 * standard specifies this check for when to update expected and
73 * max sequence numbers
74 */
75 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
76 return;
77
78 if (exp_cmdsn != session->exp_cmdsn &&
79 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050080 session->exp_cmdsn = exp_cmdsn;
81
Mike Christie77a23c22007-05-30 12:57:18 -050082 if (max_cmdsn != session->max_cmdsn &&
83 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
84 session->max_cmdsn = max_cmdsn;
85 /*
86 * if the window closed with IO queued, then kick the
87 * xmit thread
88 */
89 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie843c0a82007-12-13 12:43:20 -060090 !list_empty(&session->leadconn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -050091 scsi_queue_work(session->host,
92 &session->leadconn->xmitwork);
93 }
Mike Christie7996a772006-04-06 21:13:41 -050094}
Mike Christie77a23c22007-05-30 12:57:18 -050095EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050096
97void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040098 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050099{
100 struct iscsi_conn *conn = ctask->conn;
101
102 memset(hdr, 0, sizeof(struct iscsi_data));
103 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
104 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
105 ctask->unsol_datasn++;
106 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
107 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
108
109 hdr->itt = ctask->hdr->itt;
110 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400111 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500112
113 if (ctask->unsol_count > conn->max_xmit_dlength) {
114 hton24(hdr->dlength, conn->max_xmit_dlength);
115 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -0400116 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500117 hdr->flags = 0;
118 } else {
119 hton24(hdr->dlength, ctask->unsol_count);
120 ctask->data_count = ctask->unsol_count;
121 hdr->flags = ISCSI_FLAG_CMD_FINAL;
122 }
123}
124EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
125
Boaz Harrosh004d6532007-12-13 12:43:23 -0600126static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
127{
128 unsigned exp_len = ctask->hdr_len + len;
129
130 if (exp_len > ctask->hdr_max) {
131 WARN_ON(1);
132 return -EINVAL;
133 }
134
135 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
136 ctask->hdr_len = exp_len;
137 return 0;
138}
139
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500140/*
141 * make an extended cdb AHS
142 */
143static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask)
144{
145 struct scsi_cmnd *cmd = ctask->sc;
146 unsigned rlen, pad_len;
147 unsigned short ahslength;
148 struct iscsi_ecdb_ahdr *ecdb_ahdr;
149 int rc;
150
151 ecdb_ahdr = iscsi_next_hdr(ctask);
152 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
153
154 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
155 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
156
157 pad_len = iscsi_padding(rlen);
158
159 rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) +
160 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
161 if (rc)
162 return rc;
163
164 if (pad_len)
165 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
166
167 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
168 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
169 ecdb_ahdr->reserved = 0;
170 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
171
172 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
173 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
174 cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len);
175
176 return 0;
177}
178
Boaz Harroshc07d4442008-04-18 10:11:52 -0500179static int iscsi_prep_bidi_ahs(struct iscsi_cmd_task *ctask)
180{
181 struct scsi_cmnd *sc = ctask->sc;
182 struct iscsi_rlength_ahdr *rlen_ahdr;
183 int rc;
184
185 rlen_ahdr = iscsi_next_hdr(ctask);
186 rc = iscsi_add_hdr(ctask, sizeof(*rlen_ahdr));
187 if (rc)
188 return rc;
189
190 rlen_ahdr->ahslength =
191 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
192 sizeof(rlen_ahdr->reserved));
193 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
194 rlen_ahdr->reserved = 0;
195 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
196
197 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
198 "rlen_ahdr->ahslength(%d)\n",
199 be32_to_cpu(rlen_ahdr->read_length),
200 be16_to_cpu(rlen_ahdr->ahslength));
201 return 0;
202}
203
Mike Christie7996a772006-04-06 21:13:41 -0500204/**
205 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
206 * @ctask: iscsi cmd task
207 *
208 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
209 * fields like dlength or final based on how much data it sends
210 */
Boaz Harrosh004d6532007-12-13 12:43:23 -0600211static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500212{
213 struct iscsi_conn *conn = ctask->conn;
214 struct iscsi_session *session = conn->session;
215 struct iscsi_cmd *hdr = ctask->hdr;
216 struct scsi_cmnd *sc = ctask->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500217 unsigned hdrlength, cmd_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600218 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500219
Boaz Harrosh004d6532007-12-13 12:43:23 -0600220 ctask->hdr_len = 0;
221 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
222 if (rc)
223 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600224 hdr->opcode = ISCSI_OP_SCSI_CMD;
225 hdr->flags = ISCSI_ATTR_SIMPLE;
226 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie8b1d0342008-01-31 13:36:53 -0600227 hdr->itt = build_itt(ctask->itt, session->age);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600228 hdr->cmdsn = cpu_to_be32(session->cmdsn);
229 session->cmdsn++;
230 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500231 cmd_len = sc->cmd_len;
232 if (cmd_len < ISCSI_CDB_SIZE)
233 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
234 else if (cmd_len > ISCSI_CDB_SIZE) {
235 rc = iscsi_prep_ecdb_ahs(ctask);
236 if (rc)
237 return rc;
238 cmd_len = ISCSI_CDB_SIZE;
239 }
240 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500241
Mike Christie218432c2007-05-30 12:57:17 -0500242 ctask->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500243 if (scsi_bidi_cmnd(sc)) {
244 hdr->flags |= ISCSI_FLAG_CMD_READ;
245 rc = iscsi_prep_bidi_ahs(ctask);
246 if (rc)
247 return rc;
248 }
Mike Christie7996a772006-04-06 21:13:41 -0500249 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500250 unsigned out_len = scsi_out(sc)->length;
251 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500252 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
253 /*
254 * Write counters:
255 *
256 * imm_count bytes to be sent right after
257 * SCSI PDU Header
258 *
259 * unsol_count bytes(as Data-Out) to be sent
260 * without R2T ack right after
261 * immediate data
262 *
263 * r2t_data_count bytes to be sent via R2T ack's
264 *
265 * pad_count bytes to be sent as zero-padding
266 */
Mike Christie7996a772006-04-06 21:13:41 -0500267 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400268 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500269 ctask->unsol_datasn = 0;
270
271 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500272 if (out_len >= session->first_burst)
Mike Christie7996a772006-04-06 21:13:41 -0500273 ctask->imm_count = min(session->first_burst,
274 conn->max_xmit_dlength);
275 else
Boaz Harroshc07d4442008-04-18 10:11:52 -0500276 ctask->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500277 conn->max_xmit_dlength);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600278 hton24(hdr->dlength, ctask->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500279 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600280 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500281
Mike Christieffd04362006-08-31 18:09:24 -0400282 if (!session->initial_r2t_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500283 ctask->unsol_count = min(session->first_burst, out_len)
284 - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400285 ctask->unsol_offset = ctask->imm_count;
286 }
287
Mike Christie7996a772006-04-06 21:13:41 -0500288 if (!ctask->unsol_count)
289 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600290 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500291 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500292 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
293 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500294 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500295
296 if (sc->sc_data_direction == DMA_FROM_DEVICE)
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
298 }
299
Boaz Harrosh004d6532007-12-13 12:43:23 -0600300 /* calculate size of additional header segments (AHSs) */
301 hdrlength = ctask->hdr_len - sizeof(*hdr);
302
303 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
304 hdrlength /= ISCSI_PAD_LEN;
305
306 WARN_ON(hdrlength >= 256);
307 hdr->hlength = hdrlength & 0xFF;
308
Olaf Kircha8ac6312007-12-13 12:43:35 -0600309 if (conn->session->tt->init_cmd_task(conn->ctask))
310 return EIO;
Mike Christie77a23c22007-05-30 12:57:18 -0500311
Olaf Kircha8ac6312007-12-13 12:43:35 -0600312 conn->scsicmd_pdus_cnt++;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500313 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x "
314 "len %d bidi_len %d cmdsn %d win %d]\n",
315 scsi_bidi_cmnd(sc) ? "bidirectional" :
316 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
317 conn->id, sc, sc->cmnd[0], ctask->itt,
318 scsi_bufflen(sc), scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
Olaf Kircha8ac6312007-12-13 12:43:35 -0600319 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600320 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500321}
Mike Christie7996a772006-04-06 21:13:41 -0500322
323/**
324 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500325 * @ctask: iscsi cmd task
326 *
327 * Must be called with session lock.
328 * This function returns the scsi command to scsi-ml and returns
329 * the cmd task to the pool of available cmd tasks.
330 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400331static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500332{
Mike Christiec1635cb2007-12-13 12:43:33 -0600333 struct iscsi_conn *conn = ctask->conn;
334 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500335 struct scsi_cmnd *sc = ctask->sc;
336
Mike Christieb6c395e2006-07-24 15:47:15 -0500337 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500338 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400339 /* SCSI eh reuses commands to verify us */
340 sc->SCp.ptr = NULL;
Mike Christiec1635cb2007-12-13 12:43:33 -0600341 if (conn->ctask == ctask)
342 conn->ctask = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500343 list_del_init(&ctask->running);
344 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
345 sc->scsi_done(sc);
346}
347
Mike Christie60ecebf2006-08-31 18:09:25 -0400348static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
349{
350 atomic_inc(&ctask->refcount);
351}
352
Mike Christie60ecebf2006-08-31 18:09:25 -0400353static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
354{
Mike Christiee648f632006-08-31 18:09:34 -0400355 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400356 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400357}
358
Mike Christieb3a7ea82007-12-13 12:43:26 -0600359/*
360 * session lock must be held
361 */
362static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
363 int err)
364{
365 struct scsi_cmnd *sc;
366
367 sc = ctask->sc;
368 if (!sc)
369 return;
370
371 if (ctask->state == ISCSI_TASK_PENDING)
372 /*
373 * cmd never made it to the xmit thread, so we should not count
374 * the cmd in the sequencing
375 */
376 conn->session->queued_cmdsn--;
377 else
378 conn->session->tt->cleanup_cmd_task(conn, ctask);
379
380 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500381 if (!scsi_bidi_cmnd(sc))
382 scsi_set_resid(sc, scsi_bufflen(sc));
383 else {
384 scsi_out(sc)->resid = scsi_out(sc)->length;
385 scsi_in(sc)->resid = scsi_in(sc)->length;
386 }
Mike Christieb3a7ea82007-12-13 12:43:26 -0600387 if (conn->ctask == ctask)
388 conn->ctask = NULL;
389 /* release ref from queuecommand */
390 __iscsi_put_ctask(ctask);
391}
392
393/**
394 * iscsi_free_mgmt_task - return mgmt task back to pool
395 * @conn: iscsi connection
396 * @mtask: mtask
397 *
398 * Must be called with session lock.
399 */
400void iscsi_free_mgmt_task(struct iscsi_conn *conn,
401 struct iscsi_mgmt_task *mtask)
402{
403 list_del_init(&mtask->running);
404 if (conn->login_mtask == mtask)
405 return;
Mike Christief6d51802007-12-13 12:43:30 -0600406
407 if (conn->ping_mtask == mtask)
408 conn->ping_mtask = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600409 __kfifo_put(conn->session->mgmtpool.queue,
410 (void*)&mtask, sizeof(void*));
411}
412EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
413
Mike Christief6d51802007-12-13 12:43:30 -0600414static struct iscsi_mgmt_task *
415__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
416 char *data, uint32_t data_size)
417{
418 struct iscsi_session *session = conn->session;
419 struct iscsi_mgmt_task *mtask;
420
421 if (session->state == ISCSI_STATE_TERMINATE)
422 return NULL;
423
424 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
425 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
426 /*
427 * Login and Text are sent serially, in
428 * request-followed-by-response sequence.
429 * Same mtask can be used. Same ITT must be used.
430 * Note that login_mtask is preallocated at conn_create().
431 */
432 mtask = conn->login_mtask;
433 else {
434 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
435 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
436
437 if (!__kfifo_get(session->mgmtpool.queue,
438 (void*)&mtask, sizeof(void*)))
439 return NULL;
440 }
441
442 if (data_size) {
443 memcpy(mtask->data, data, data_size);
444 mtask->data_count = data_size;
445 } else
446 mtask->data_count = 0;
447
448 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
449 INIT_LIST_HEAD(&mtask->running);
450 list_add_tail(&mtask->running, &conn->mgmtqueue);
451 return mtask;
452}
453
454int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
455 char *data, uint32_t data_size)
456{
457 struct iscsi_conn *conn = cls_conn->dd_data;
458 struct iscsi_session *session = conn->session;
459 int err = 0;
460
461 spin_lock_bh(&session->lock);
462 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
463 err = -EPERM;
464 spin_unlock_bh(&session->lock);
465 scsi_queue_work(session->host, &conn->xmitwork);
466 return err;
467}
468EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
469
Mike Christie7996a772006-04-06 21:13:41 -0500470/**
471 * iscsi_cmd_rsp - SCSI Command Response processing
472 * @conn: iscsi connection
473 * @hdr: iscsi header
474 * @ctask: scsi command task
475 * @data: cmd data buffer
476 * @datalen: len of buffer
477 *
478 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
479 * then completes the command and task.
480 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500481static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
482 struct iscsi_cmd_task *ctask, char *data,
483 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500484{
Mike Christie7996a772006-04-06 21:13:41 -0500485 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
486 struct iscsi_session *session = conn->session;
487 struct scsi_cmnd *sc = ctask->sc;
488
Mike Christie77a23c22007-05-30 12:57:18 -0500489 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500490 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
491
492 sc->result = (DID_OK << 16) | rhdr->cmd_status;
493
494 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
495 sc->result = DID_ERROR << 16;
496 goto out;
497 }
498
499 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600500 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500501
502 if (datalen < 2) {
503invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600504 iscsi_conn_printk(KERN_ERR, conn,
505 "Got CHECK_CONDITION but invalid data "
506 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500507 sc->result = DID_BAD_TARGET << 16;
508 goto out;
509 }
510
Mike Christie8eb00532007-02-28 17:32:19 -0600511 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500512 if (datalen < senselen)
513 goto invalid_datalen;
514
515 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600516 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500517 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600518 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500519 }
520
Boaz Harroshc07d4442008-04-18 10:11:52 -0500521 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
522 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
523 int res_count = be32_to_cpu(rhdr->bi_residual_count);
524
525 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
526 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
527 res_count <= scsi_in(sc)->length))
528 scsi_in(sc)->resid = res_count;
529 else
530 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
531 }
532
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600533 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
534 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500535 int res_count = be32_to_cpu(rhdr->residual_count);
536
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600537 if (res_count > 0 &&
538 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
539 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500540 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900541 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500542 else
543 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500544 }
Mike Christie7996a772006-04-06 21:13:41 -0500545out:
546 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
547 (long)sc, sc->result, ctask->itt);
548 conn->scsirsp_pdus_cnt++;
549
Mike Christie60ecebf2006-08-31 18:09:25 -0400550 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500551}
552
Mike Christie7ea8b822006-07-24 15:47:22 -0500553static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
554{
555 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
556
557 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
558 conn->tmfrsp_pdus_cnt++;
559
Mike Christie843c0a82007-12-13 12:43:20 -0600560 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500561 return;
562
563 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600564 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500565 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600566 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500567 else
Mike Christie843c0a82007-12-13 12:43:20 -0600568 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500569 wake_up(&conn->ehwait);
570}
571
Mike Christief6d51802007-12-13 12:43:30 -0600572static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
573{
574 struct iscsi_nopout hdr;
575 struct iscsi_mgmt_task *mtask;
576
577 if (!rhdr && conn->ping_mtask)
578 return;
579
580 memset(&hdr, 0, sizeof(struct iscsi_nopout));
581 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
582 hdr.flags = ISCSI_FLAG_CMD_FINAL;
583
584 if (rhdr) {
585 memcpy(hdr.lun, rhdr->lun, 8);
586 hdr.ttt = rhdr->ttt;
587 hdr.itt = RESERVED_ITT;
588 } else
589 hdr.ttt = RESERVED_ITT;
590
591 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
592 if (!mtask) {
Mike Christie322d7392008-01-31 13:36:52 -0600593 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600594 return;
595 }
596
597 /* only track our nops */
598 if (!rhdr) {
599 conn->ping_mtask = mtask;
600 conn->last_ping = jiffies;
601 }
602 scsi_queue_work(conn->session->host, &conn->xmitwork);
603}
604
Mike Christie62f38302006-08-31 18:09:27 -0400605static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
606 char *data, int datalen)
607{
608 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
609 struct iscsi_hdr rejected_pdu;
610 uint32_t itt;
611
612 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
613
614 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
615 if (ntoh24(reject->dlength) > datalen)
616 return ISCSI_ERR_PROTO;
617
618 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
619 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000620 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600621 iscsi_conn_printk(KERN_ERR, conn,
622 "itt 0x%x had pdu (op 0x%x) rejected "
623 "due to DataDigest error.\n", itt,
624 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400625 }
626 }
627 return 0;
628}
629
Mike Christie7996a772006-04-06 21:13:41 -0500630/**
631 * __iscsi_complete_pdu - complete pdu
632 * @conn: iscsi conn
633 * @hdr: iscsi header
634 * @data: data buffer
635 * @datalen: len of data buffer
636 *
637 * Completes pdu processing by freeing any resources allocated at
638 * queuecommand or send generic. session lock must be held and verify
639 * itt must have been called.
640 */
Adrian Bunkf8d9d652008-01-29 00:11:27 +0200641static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
642 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500643{
644 struct iscsi_session *session = conn->session;
645 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
646 struct iscsi_cmd_task *ctask;
647 struct iscsi_mgmt_task *mtask;
648 uint32_t itt;
649
Mike Christief6d51802007-12-13 12:43:30 -0600650 conn->last_recv = jiffies;
Al Virob4377352007-02-09 16:39:40 +0000651 if (hdr->itt != RESERVED_ITT)
652 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500653 else
Al Virob4377352007-02-09 16:39:40 +0000654 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500655
656 if (itt < session->cmds_max) {
657 ctask = session->cmds[itt];
658
659 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
660 opcode, conn->id, ctask->itt, datalen);
661
662 switch(opcode) {
663 case ISCSI_OP_SCSI_CMD_RSP:
664 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500665 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
666 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500667 break;
668 case ISCSI_OP_SCSI_DATA_IN:
669 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
670 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
671 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400672 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500673 }
674 break;
675 case ISCSI_OP_R2T:
676 /* LLD handles this for now */
677 break;
678 default:
679 rc = ISCSI_ERR_BAD_OPCODE;
680 break;
681 }
682 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
683 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
684 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
685
686 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
687 opcode, conn->id, mtask->itt, datalen);
688
Mike Christie77a23c22007-05-30 12:57:18 -0500689 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500690 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500691 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500692 if (datalen) {
693 rc = ISCSI_ERR_PROTO;
694 break;
695 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500696 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
697 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500698 case ISCSI_OP_LOGIN_RSP:
699 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500700 /*
701 * login related PDU's exp_statsn is handled in
702 * userspace
703 */
Mike Christie40527af2006-07-24 15:47:45 -0500704 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
705 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600706 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500707 break;
708 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500709 if (datalen) {
710 rc = ISCSI_ERR_PROTO;
711 break;
712 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500713
Mike Christie7ea8b822006-07-24 15:47:22 -0500714 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600715 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500716 break;
717 case ISCSI_OP_NOOP_IN:
Mike Christief6d51802007-12-13 12:43:30 -0600718 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
719 datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500720 rc = ISCSI_ERR_PROTO;
721 break;
722 }
Mike Christie7996a772006-04-06 21:13:41 -0500723 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
724
Mike Christief6d51802007-12-13 12:43:30 -0600725 if (conn->ping_mtask != mtask) {
726 /*
727 * If this is not in response to one of our
728 * nops then it must be from userspace.
729 */
730 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
731 datalen))
732 rc = ISCSI_ERR_CONN_FAILED;
733 }
Mike Christieb3a7ea82007-12-13 12:43:26 -0600734 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500735 break;
736 default:
737 rc = ISCSI_ERR_BAD_OPCODE;
738 break;
739 }
Al Virob4377352007-02-09 16:39:40 +0000740 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500741 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400742
Mike Christie7996a772006-04-06 21:13:41 -0500743 switch(opcode) {
744 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500745 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500746 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500747 break;
748 }
749
Al Virob4377352007-02-09 16:39:40 +0000750 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500751 break;
752
Mike Christief6d51802007-12-13 12:43:30 -0600753 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500754 break;
755 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400756 rc = iscsi_handle_reject(conn, hdr, data, datalen);
757 break;
Mike Christie7996a772006-04-06 21:13:41 -0500758 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500759 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400760 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
761 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500762 break;
763 default:
764 rc = ISCSI_ERR_BAD_OPCODE;
765 break;
766 }
767 } else
768 rc = ISCSI_ERR_BAD_ITT;
769
770 return rc;
771}
Mike Christie7996a772006-04-06 21:13:41 -0500772
773int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
774 char *data, int datalen)
775{
776 int rc;
777
778 spin_lock(&conn->session->lock);
779 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
780 spin_unlock(&conn->session->lock);
781 return rc;
782}
783EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
784
785/* verify itt (itt encoding: age+cid+itt) */
786int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
787 uint32_t *ret_itt)
788{
789 struct iscsi_session *session = conn->session;
790 struct iscsi_cmd_task *ctask;
791 uint32_t itt;
792
Al Virob4377352007-02-09 16:39:40 +0000793 if (hdr->itt != RESERVED_ITT) {
794 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500795 (session->age << ISCSI_AGE_SHIFT)) {
Mike Christie322d7392008-01-31 13:36:52 -0600796 iscsi_conn_printk(KERN_ERR, conn,
797 "received itt %x expected session "
798 "age (%x)\n", (__force u32)hdr->itt,
799 session->age & ISCSI_AGE_MASK);
Mike Christie7996a772006-04-06 21:13:41 -0500800 return ISCSI_ERR_BAD_ITT;
801 }
802
Al Virob4377352007-02-09 16:39:40 +0000803 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500804 } else
Al Virob4377352007-02-09 16:39:40 +0000805 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500806
807 if (itt < session->cmds_max) {
808 ctask = session->cmds[itt];
809
810 if (!ctask->sc) {
Mike Christie322d7392008-01-31 13:36:52 -0600811 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
812 "with itt 0x%x\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500813 /* force drop */
814 return ISCSI_ERR_NO_SCSI_CMD;
815 }
816
817 if (ctask->sc->SCp.phase != session->age) {
Mike Christie322d7392008-01-31 13:36:52 -0600818 iscsi_conn_printk(KERN_ERR, conn,
819 "iscsi: ctask's session age %d, "
820 "expected %d\n", ctask->sc->SCp.phase,
821 session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500822 return ISCSI_ERR_SESSION_FAILED;
823 }
824 }
825
826 *ret_itt = itt;
827 return 0;
828}
829EXPORT_SYMBOL_GPL(iscsi_verify_itt);
830
831void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
832{
833 struct iscsi_session *session = conn->session;
834 unsigned long flags;
835
836 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500837 if (session->state == ISCSI_STATE_FAILED) {
838 spin_unlock_irqrestore(&session->lock, flags);
839 return;
840 }
841
Mike Christie67a61112006-05-30 00:37:20 -0500842 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500843 session->state = ISCSI_STATE_FAILED;
844 spin_unlock_irqrestore(&session->lock, flags);
845 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
846 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
847 iscsi_conn_error(conn->cls_conn, err);
848}
849EXPORT_SYMBOL_GPL(iscsi_conn_failure);
850
Mike Christie77a23c22007-05-30 12:57:18 -0500851static void iscsi_prep_mtask(struct iscsi_conn *conn,
852 struct iscsi_mgmt_task *mtask)
853{
854 struct iscsi_session *session = conn->session;
855 struct iscsi_hdr *hdr = mtask->hdr;
856 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
857
858 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
859 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
860 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
861 /*
862 * pre-format CmdSN for outgoing PDU.
863 */
864 nop->cmdsn = cpu_to_be32(session->cmdsn);
865 if (hdr->itt != RESERVED_ITT) {
Mike Christie8b1d0342008-01-31 13:36:53 -0600866 hdr->itt = build_itt(mtask->itt, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500867 /*
868 * TODO: We always use immediate, so we never hit this.
869 * If we start to send tmfs or nops as non-immediate then
870 * we should start checking the cmdsn numbers for mgmt tasks.
871 */
Mike Christie77a23c22007-05-30 12:57:18 -0500872 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500873 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
874 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500875 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500876 }
Mike Christie77a23c22007-05-30 12:57:18 -0500877 }
878
879 if (session->tt->init_mgmt_task)
880 session->tt->init_mgmt_task(conn, mtask);
881
882 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
Mike Christie843c0a82007-12-13 12:43:20 -0600883 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
884 mtask->data_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500885}
886
Mike Christie05db8882007-02-28 17:32:16 -0600887static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400888{
889 struct iscsi_hdr *hdr = conn->mtask->hdr;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600890 int rc;
Mike Christieb5072ea2006-10-16 18:09:42 -0400891
Mike Christieb3a7ea82007-12-13 12:43:26 -0600892 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
893 conn->session->state = ISCSI_STATE_LOGGING_OUT;
Mike Christie77a23c22007-05-30 12:57:18 -0500894 spin_unlock_bh(&conn->session->lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600895
Mike Christieb5072ea2006-10-16 18:09:42 -0400896 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500897 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400898 if (rc)
899 return rc;
900
Mike Christie05db8882007-02-28 17:32:16 -0600901 /* done with this in-progress mtask */
902 conn->mtask = NULL;
Mike Christieb5072ea2006-10-16 18:09:42 -0400903 return 0;
904}
905
Mike Christie77a23c22007-05-30 12:57:18 -0500906static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
907{
908 struct iscsi_session *session = conn->session;
909
910 /*
911 * Check for iSCSI window and take care of CmdSN wrap-around
912 */
Mike Christiee0726402007-07-26 12:46:48 -0500913 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
914 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
915 "CmdSN %u/%u\n", session->exp_cmdsn,
916 session->max_cmdsn, session->cmdsn,
917 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500918 return -ENOSPC;
919 }
920 return 0;
921}
922
923static int iscsi_xmit_ctask(struct iscsi_conn *conn)
924{
925 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600926 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500927
928 __iscsi_get_ctask(ctask);
929 spin_unlock_bh(&conn->session->lock);
930 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
931 spin_lock_bh(&conn->session->lock);
932 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500933 if (!rc)
934 /* done with this ctask */
935 conn->ctask = NULL;
936 return rc;
937}
938
Mike Christie7996a772006-04-06 21:13:41 -0500939/**
Mike Christie843c0a82007-12-13 12:43:20 -0600940 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
941 * @ctask: ctask to requeue
942 *
943 * LLDs that need to run a ctask from the session workqueue should call
944 * this. The session lock must be held.
945 */
946void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
947{
948 struct iscsi_conn *conn = ctask->conn;
949
950 list_move_tail(&ctask->running, &conn->requeue);
951 scsi_queue_work(conn->session->host, &conn->xmitwork);
952}
953EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
954
955/**
Mike Christie7996a772006-04-06 21:13:41 -0500956 * iscsi_data_xmit - xmit any command into the scheduled connection
957 * @conn: iscsi connection
958 *
959 * Notes:
960 * The function can return -EAGAIN in which case the caller must
961 * re-schedule it again later or recover. '0' return code means
962 * successful xmit.
963 **/
964static int iscsi_data_xmit(struct iscsi_conn *conn)
965{
Mike Christie3219e522006-05-30 00:37:28 -0500966 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500967
Mike Christie77a23c22007-05-30 12:57:18 -0500968 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500969 if (unlikely(conn->suspend_tx)) {
970 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500971 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500972 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500973 }
Mike Christie7996a772006-04-06 21:13:41 -0500974
975 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500976 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500977 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500978 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500979 }
Mike Christie77a23c22007-05-30 12:57:18 -0500980
Mike Christie7996a772006-04-06 21:13:41 -0500981 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600982 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500983 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500984 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500985 }
986
Mike Christie77a23c22007-05-30 12:57:18 -0500987 /*
988 * process mgmt pdus like nops before commands since we should
989 * only have one nop-out as a ping from us and targets should not
990 * overflow us with nop-ins
991 */
992check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -0600993 while (!list_empty(&conn->mgmtqueue)) {
994 conn->mtask = list_entry(conn->mgmtqueue.next,
995 struct iscsi_mgmt_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600996 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
997 iscsi_free_mgmt_task(conn, conn->mtask);
998 conn->mtask = NULL;
999 continue;
1000 }
1001
Mike Christie77a23c22007-05-30 12:57:18 -05001002 iscsi_prep_mtask(conn, conn->mtask);
Mike Christie843c0a82007-12-13 12:43:20 -06001003 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
Mike Christie77a23c22007-05-30 12:57:18 -05001004 rc = iscsi_xmit_mtask(conn);
1005 if (rc)
1006 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001007 }
1008
Mike Christie843c0a82007-12-13 12:43:20 -06001009 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001010 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001011 if (conn->tmf_state == TMF_QUEUED)
1012 break;
1013
Mike Christieb6c395e2006-07-24 15:47:15 -05001014 conn->ctask = list_entry(conn->xmitqueue.next,
1015 struct iscsi_cmd_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001016 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9000bcd2007-12-13 12:43:32 -06001017 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001018 continue;
1019 }
Boaz Harrosh004d6532007-12-13 12:43:23 -06001020 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
1021 fail_command(conn, conn->ctask, DID_ABORT << 16);
1022 continue;
1023 }
Olaf Kircha8ac6312007-12-13 12:43:35 -06001024
Mike Christie843c0a82007-12-13 12:43:20 -06001025 conn->ctask->state = ISCSI_TASK_RUNNING;
Mike Christieb6c395e2006-07-24 15:47:15 -05001026 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -05001027 rc = iscsi_xmit_ctask(conn);
1028 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001029 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001030 /*
1031 * we could continuously get new ctask requests so
1032 * we need to check the mgmt queue for nops that need to
1033 * be sent to aviod starvation
1034 */
Mike Christie843c0a82007-12-13 12:43:20 -06001035 if (!list_empty(&conn->mgmtqueue))
1036 goto check_mgmt;
1037 }
1038
1039 while (!list_empty(&conn->requeue)) {
1040 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1041 break;
1042
Mike Christieb3a7ea82007-12-13 12:43:26 -06001043 /*
1044 * we always do fastlogout - conn stop code will clean up.
1045 */
1046 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1047 break;
1048
Mike Christie843c0a82007-12-13 12:43:20 -06001049 conn->ctask = list_entry(conn->requeue.next,
1050 struct iscsi_cmd_task, running);
1051 conn->ctask->state = ISCSI_TASK_RUNNING;
1052 list_move_tail(conn->requeue.next, &conn->run_list);
1053 rc = iscsi_xmit_ctask(conn);
1054 if (rc)
1055 goto again;
1056 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001057 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001058 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001059 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001060 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001061
1062again:
1063 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001064 rc = -ENODATA;
1065 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001066 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001067}
1068
David Howellsc4028952006-11-22 14:57:56 +00001069static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001070{
David Howellsc4028952006-11-22 14:57:56 +00001071 struct iscsi_conn *conn =
1072 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001073 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001074 /*
1075 * serialize Xmit worker on a per-connection basis.
1076 */
Mike Christie3219e522006-05-30 00:37:28 -05001077 do {
1078 rc = iscsi_data_xmit(conn);
1079 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001080}
1081
1082enum {
1083 FAILURE_BAD_HOST = 1,
1084 FAILURE_SESSION_FAILED,
1085 FAILURE_SESSION_FREED,
1086 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001087 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001088 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001089 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001090 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001091 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001092 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001093};
1094
1095int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1096{
1097 struct Scsi_Host *host;
1098 int reason = 0;
1099 struct iscsi_session *session;
1100 struct iscsi_conn *conn;
1101 struct iscsi_cmd_task *ctask = NULL;
1102
1103 sc->scsi_done = done;
1104 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001105 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001106
1107 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001108 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001109
Mike Christie1040c992007-12-13 12:43:34 -06001110 session = iscsi_hostdata(host->hostdata);
Mike Christie7996a772006-04-06 21:13:41 -05001111 spin_lock(&session->lock);
1112
Mike Christie6eabafb2008-01-31 13:36:43 -06001113 reason = iscsi_session_chkready(session_to_cls(session));
1114 if (reason) {
1115 sc->result = reason;
1116 goto fault;
1117 }
1118
Mike Christie656cffc2006-05-18 20:31:42 -05001119 /*
1120 * ISCSI_STATE_FAILED is a temp. state. The recovery
1121 * code will decide what is best to do with command queued
1122 * during this time
1123 */
1124 if (session->state != ISCSI_STATE_LOGGED_IN &&
1125 session->state != ISCSI_STATE_FAILED) {
1126 /*
1127 * to handle the race between when we set the recovery state
1128 * and block the session we requeue here (commands could
1129 * be entering our queuecommand while a block is starting
1130 * up because the block code is not locked)
1131 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001132 switch (session->state) {
1133 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001134 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001135 sc->result = DID_IMM_RETRY << 16;
1136 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001137 case ISCSI_STATE_LOGGING_OUT:
1138 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001139 sc->result = DID_IMM_RETRY << 16;
1140 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001141 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001142 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001143 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001144 break;
1145 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001146 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001147 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001148 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001149 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001150 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001151 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001152 }
Mike Christie7996a772006-04-06 21:13:41 -05001153 goto fault;
1154 }
1155
Mike Christie7996a772006-04-06 21:13:41 -05001156 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001157 if (!conn) {
1158 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001159 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001160 goto fault;
1161 }
Mike Christie7996a772006-04-06 21:13:41 -05001162
Mike Christie77a23c22007-05-30 12:57:18 -05001163 if (iscsi_check_cmdsn_window_closed(conn)) {
1164 reason = FAILURE_WINDOW_CLOSED;
1165 goto reject;
1166 }
1167
Mike Christie60ecebf2006-08-31 18:09:25 -04001168 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1169 sizeof(void*))) {
1170 reason = FAILURE_OOM;
1171 goto reject;
1172 }
Mike Christiee0726402007-07-26 12:46:48 -05001173 session->queued_cmdsn++;
1174
Mike Christie7996a772006-04-06 21:13:41 -05001175 sc->SCp.phase = session->age;
1176 sc->SCp.ptr = (char *)ctask;
1177
Mike Christie60ecebf2006-08-31 18:09:25 -04001178 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -05001179 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -05001180 ctask->conn = conn;
1181 ctask->sc = sc;
1182 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001183
Mike Christieb6c395e2006-07-24 15:47:15 -05001184 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001185 spin_unlock(&session->lock);
1186
1187 scsi_queue_work(host, &conn->xmitwork);
Mike Christie1040c992007-12-13 12:43:34 -06001188 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001189 return 0;
1190
1191reject:
1192 spin_unlock(&session->lock);
1193 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001194 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001195 return SCSI_MLQUEUE_HOST_BUSY;
1196
1197fault:
1198 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001199 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001200 if (!scsi_bidi_cmnd(sc))
1201 scsi_set_resid(sc, scsi_bufflen(sc));
1202 else {
1203 scsi_out(sc)->resid = scsi_out(sc)->length;
1204 scsi_in(sc)->resid = scsi_in(sc)->length;
1205 }
Mike Christie7996a772006-04-06 21:13:41 -05001206 sc->scsi_done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001207 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001208 return 0;
1209}
1210EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1211
1212int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1213{
1214 if (depth > ISCSI_MAX_CMD_PER_LUN)
1215 depth = ISCSI_MAX_CMD_PER_LUN;
1216 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1217 return sdev->queue_depth;
1218}
1219EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1220
Mike Christie7996a772006-04-06 21:13:41 -05001221void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1222{
1223 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001224
1225 spin_lock_bh(&session->lock);
1226 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001227 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001228 if (session->leadconn)
1229 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001230 }
1231 spin_unlock_bh(&session->lock);
1232}
1233EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1234
1235int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1236{
1237 struct Scsi_Host *host = sc->device->host;
1238 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1239 struct iscsi_conn *conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001240
Mike Christiebc436b22007-12-13 12:43:28 -06001241 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001242 spin_lock_bh(&session->lock);
1243 if (session->state == ISCSI_STATE_TERMINATE) {
1244failed:
1245 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001246 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001247 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001248 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001249 return FAILED;
1250 }
1251
Mike Christie7996a772006-04-06 21:13:41 -05001252 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001253 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001254 /*
1255 * we drop the lock here but the leadconn cannot be destoyed while
1256 * we are in the scsi eh
1257 */
Mike Christie843c0a82007-12-13 12:43:20 -06001258 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001259
1260 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1261 wait_event_interruptible(conn->ehwait,
1262 session->state == ISCSI_STATE_TERMINATE ||
1263 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001264 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001265 if (signal_pending(current))
1266 flush_signals(current);
1267
Mike Christiebc436b22007-12-13 12:43:28 -06001268 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001269 spin_lock_bh(&session->lock);
1270 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001271 iscsi_session_printk(KERN_INFO, session,
1272 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001273 else
1274 goto failed;
1275 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001276 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001277 return SUCCESS;
1278}
1279EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1280
Mike Christie843c0a82007-12-13 12:43:20 -06001281static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001282{
Mike Christie843c0a82007-12-13 12:43:20 -06001283 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001284 struct iscsi_session *session = conn->session;
1285
1286 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001287 if (conn->tmf_state == TMF_QUEUED) {
1288 conn->tmf_state = TMF_TIMEDOUT;
1289 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001290 /* unblock eh_abort() */
1291 wake_up(&conn->ehwait);
1292 }
1293 spin_unlock(&session->lock);
1294}
1295
Mike Christie843c0a82007-12-13 12:43:20 -06001296static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001297 struct iscsi_tm *hdr, int age,
1298 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001299{
Mike Christie7996a772006-04-06 21:13:41 -05001300 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001301 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001302
Mike Christie843c0a82007-12-13 12:43:20 -06001303 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1304 NULL, 0);
1305 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001306 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001307 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001308 spin_lock_bh(&session->lock);
1309 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001310 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001311 }
Mike Christie843c0a82007-12-13 12:43:20 -06001312 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001313 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001314 conn->tmf_timer.function = iscsi_tmf_timedout;
1315 conn->tmf_timer.data = (unsigned long)conn;
1316 add_timer(&conn->tmf_timer);
1317 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001318
Mike Christie7996a772006-04-06 21:13:41 -05001319 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001320 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001321 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001322
1323 /*
1324 * block eh thread until:
1325 *
Mike Christie843c0a82007-12-13 12:43:20 -06001326 * 1) tmf response
1327 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001328 * 3) session is terminated or restarted or userspace has
1329 * given up on recovery
1330 */
Mike Christie843c0a82007-12-13 12:43:20 -06001331 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001332 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001333 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001334 if (signal_pending(current))
1335 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001336 del_timer_sync(&conn->tmf_timer);
1337
Mike Christie6724add2007-08-15 01:38:30 -05001338 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001339 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001340 /* if the session drops it will clean up the mtask */
1341 if (age != session->age ||
1342 session->state != ISCSI_STATE_LOGGED_IN)
1343 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001344 return 0;
1345}
1346
1347/*
Mike Christie843c0a82007-12-13 12:43:20 -06001348 * Fail commands. session lock held and recv side suspended and xmit
1349 * thread flushed
1350 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001351static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1352 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001353{
1354 struct iscsi_cmd_task *ctask, *tmp;
1355
1356 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1357 conn->ctask = NULL;
1358
1359 /* flush pending */
1360 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1361 if (lun == ctask->sc->device->lun || lun == -1) {
1362 debug_scsi("failing pending sc %p itt 0x%x\n",
1363 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001364 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001365 }
1366 }
1367
1368 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1369 if (lun == ctask->sc->device->lun || lun == -1) {
1370 debug_scsi("failing requeued sc %p itt 0x%x\n",
1371 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001372 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001373 }
1374 }
1375
1376 /* fail all other running */
1377 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1378 if (lun == ctask->sc->device->lun || lun == -1) {
1379 debug_scsi("failing in progress sc %p itt 0x%x\n",
1380 ctask->sc, ctask->itt);
1381 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1382 }
1383 }
1384}
1385
Mike Christie6724add2007-08-15 01:38:30 -05001386static void iscsi_suspend_tx(struct iscsi_conn *conn)
1387{
1388 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1389 scsi_flush_work(conn->session->host);
1390}
1391
1392static void iscsi_start_tx(struct iscsi_conn *conn)
1393{
1394 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1395 scsi_queue_work(conn->session->host, &conn->xmitwork);
1396}
1397
Mike Christief6d51802007-12-13 12:43:30 -06001398static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1399{
1400 struct iscsi_cls_session *cls_session;
1401 struct iscsi_session *session;
1402 struct iscsi_conn *conn;
1403 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1404
1405 cls_session = starget_to_session(scsi_target(scmd->device));
1406 session = class_to_transport_session(cls_session);
1407
1408 debug_scsi("scsi cmd %p timedout\n", scmd);
1409
1410 spin_lock(&session->lock);
1411 if (session->state != ISCSI_STATE_LOGGED_IN) {
1412 /*
1413 * We are probably in the middle of iscsi recovery so let
1414 * that complete and handle the error.
1415 */
1416 rc = EH_RESET_TIMER;
1417 goto done;
1418 }
1419
1420 conn = session->leadconn;
1421 if (!conn) {
1422 /* In the middle of shuting down */
1423 rc = EH_RESET_TIMER;
1424 goto done;
1425 }
1426
1427 if (!conn->recv_timeout && !conn->ping_timeout)
1428 goto done;
1429 /*
1430 * if the ping timedout then we are in the middle of cleaning up
1431 * and can let the iscsi eh handle it
1432 */
1433 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1434 (conn->ping_timeout * HZ), jiffies))
1435 rc = EH_RESET_TIMER;
1436 /*
1437 * if we are about to check the transport then give the command
1438 * more time
1439 */
1440 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1441 jiffies))
1442 rc = EH_RESET_TIMER;
1443 /* if in the middle of checking the transport then give us more time */
1444 if (conn->ping_mtask)
1445 rc = EH_RESET_TIMER;
1446done:
1447 spin_unlock(&session->lock);
1448 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1449 return rc;
1450}
1451
1452static void iscsi_check_transport_timeouts(unsigned long data)
1453{
1454 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1455 struct iscsi_session *session = conn->session;
1456 unsigned long timeout, next_timeout = 0, last_recv;
1457
1458 spin_lock(&session->lock);
1459 if (session->state != ISCSI_STATE_LOGGED_IN)
1460 goto done;
1461
1462 timeout = conn->recv_timeout;
1463 if (!timeout)
1464 goto done;
1465
1466 timeout *= HZ;
1467 last_recv = conn->last_recv;
1468 if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
1469 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001470 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1471 "expired, last rx %lu, last ping %lu, "
1472 "now %lu\n", conn->ping_timeout, last_recv,
1473 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001474 spin_unlock(&session->lock);
1475 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1476 return;
1477 }
1478
1479 if (time_before_eq(last_recv + timeout, jiffies)) {
1480 if (time_before_eq(conn->last_ping, last_recv)) {
1481 /* send a ping to try to provoke some traffic */
1482 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1483 iscsi_send_nopout(conn, NULL);
1484 }
1485 next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001486 } else
Mike Christief6d51802007-12-13 12:43:30 -06001487 next_timeout = last_recv + timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001488
Mike Christiead294e92008-01-31 13:36:50 -06001489 debug_scsi("Setting next tmo %lu\n", next_timeout);
1490 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001491done:
1492 spin_unlock(&session->lock);
1493}
1494
Mike Christie843c0a82007-12-13 12:43:20 -06001495static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1496 struct iscsi_tm *hdr)
1497{
1498 memset(hdr, 0, sizeof(*hdr));
1499 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1500 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1501 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1502 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1503 hdr->rtt = ctask->hdr->itt;
1504 hdr->refcmdsn = ctask->hdr->cmdsn;
1505}
1506
Mike Christie7996a772006-04-06 21:13:41 -05001507int iscsi_eh_abort(struct scsi_cmnd *sc)
1508{
Mike Christie6724add2007-08-15 01:38:30 -05001509 struct Scsi_Host *host = sc->device->host;
1510 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001511 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001512 struct iscsi_cmd_task *ctask;
1513 struct iscsi_tm *hdr;
1514 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001515
Mike Christie6724add2007-08-15 01:38:30 -05001516 mutex_lock(&session->eh_mutex);
1517 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001518 /*
1519 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1520 * got the command.
1521 */
1522 if (!sc->SCp.ptr) {
1523 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001524 spin_unlock_bh(&session->lock);
1525 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001526 return SUCCESS;
1527 }
1528
Mike Christie7996a772006-04-06 21:13:41 -05001529 /*
1530 * If we are not logged in or we have started a new session
1531 * then let the host reset code handle this
1532 */
Mike Christie843c0a82007-12-13 12:43:20 -06001533 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1534 sc->SCp.phase != session->age) {
1535 spin_unlock_bh(&session->lock);
1536 mutex_unlock(&session->eh_mutex);
1537 return FAILED;
1538 }
1539
1540 conn = session->leadconn;
1541 conn->eh_abort_cnt++;
1542 age = session->age;
1543
1544 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1545 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001546
1547 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001548 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001549 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001550 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001551 }
Mike Christie7996a772006-04-06 21:13:41 -05001552
Mike Christie77a23c22007-05-30 12:57:18 -05001553 if (ctask->state == ISCSI_TASK_PENDING) {
1554 fail_command(conn, ctask, DID_ABORT << 16);
1555 goto success;
1556 }
Mike Christie7996a772006-04-06 21:13:41 -05001557
Mike Christie843c0a82007-12-13 12:43:20 -06001558 /* only have one tmf outstanding at a time */
1559 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001560 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001561 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001562
Mike Christie843c0a82007-12-13 12:43:20 -06001563 hdr = &conn->tmhdr;
1564 iscsi_prep_abort_task_pdu(ctask, hdr);
1565
Mike Christief6d51802007-12-13 12:43:30 -06001566 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001567 rc = FAILED;
1568 goto failed;
1569 }
1570
1571 switch (conn->tmf_state) {
1572 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001573 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001574 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001575 /*
1576 * clean up task if aborted. grab the recv lock as a writer
1577 */
1578 write_lock_bh(conn->recv_lock);
1579 spin_lock(&session->lock);
1580 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001581 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001582 spin_unlock(&session->lock);
1583 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001584 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001585 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001586 case TMF_TIMEDOUT:
1587 spin_unlock_bh(&session->lock);
1588 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1589 goto failed_unlocked;
1590 case TMF_NOT_FOUND:
1591 if (!sc->SCp.ptr) {
1592 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001593 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001594 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001595 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001596 }
1597 /* fall through */
1598 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001599 conn->tmf_state = TMF_INITIAL;
1600 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001601 }
1602
Mike Christie77a23c22007-05-30 12:57:18 -05001603success:
Mike Christie7996a772006-04-06 21:13:41 -05001604 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001605success_unlocked:
1606 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001607 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001608 return SUCCESS;
1609
1610failed:
1611 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001612failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001613 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1614 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001615 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001616 return FAILED;
1617}
1618EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1619
Mike Christie843c0a82007-12-13 12:43:20 -06001620static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1621{
1622 memset(hdr, 0, sizeof(*hdr));
1623 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1624 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1625 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1626 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001627 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001628}
1629
1630int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1631{
1632 struct Scsi_Host *host = sc->device->host;
1633 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1634 struct iscsi_conn *conn;
1635 struct iscsi_tm *hdr;
1636 int rc = FAILED;
1637
1638 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1639
1640 mutex_lock(&session->eh_mutex);
1641 spin_lock_bh(&session->lock);
1642 /*
1643 * Just check if we are not logged in. We cannot check for
1644 * the phase because the reset could come from a ioctl.
1645 */
1646 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1647 goto unlock;
1648 conn = session->leadconn;
1649
1650 /* only have one tmf outstanding at a time */
1651 if (conn->tmf_state != TMF_INITIAL)
1652 goto unlock;
1653 conn->tmf_state = TMF_QUEUED;
1654
1655 hdr = &conn->tmhdr;
1656 iscsi_prep_lun_reset_pdu(sc, hdr);
1657
Mike Christief6d51802007-12-13 12:43:30 -06001658 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1659 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001660 rc = FAILED;
1661 goto unlock;
1662 }
1663
1664 switch (conn->tmf_state) {
1665 case TMF_SUCCESS:
1666 break;
1667 case TMF_TIMEDOUT:
1668 spin_unlock_bh(&session->lock);
1669 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1670 goto done;
1671 default:
1672 conn->tmf_state = TMF_INITIAL;
1673 goto unlock;
1674 }
1675
1676 rc = SUCCESS;
1677 spin_unlock_bh(&session->lock);
1678
1679 iscsi_suspend_tx(conn);
1680 /* need to grab the recv lock then session lock */
1681 write_lock_bh(conn->recv_lock);
1682 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001683 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001684 conn->tmf_state = TMF_INITIAL;
1685 spin_unlock(&session->lock);
1686 write_unlock_bh(conn->recv_lock);
1687
1688 iscsi_start_tx(conn);
1689 goto done;
1690
1691unlock:
1692 spin_unlock_bh(&session->lock);
1693done:
1694 debug_scsi("iscsi_eh_device_reset %s\n",
1695 rc == SUCCESS ? "SUCCESS" : "FAILED");
1696 mutex_unlock(&session->eh_mutex);
1697 return rc;
1698}
1699EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1700
Olaf Kirch63203772007-12-13 12:43:25 -06001701/*
1702 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1703 * should be accessed via kfifo_{get,put} on q->queue.
1704 * Optionally, the caller can obtain the array of object pointers
1705 * by passing in a non-NULL @items pointer
1706 */
Mike Christie7996a772006-04-06 21:13:41 -05001707int
Olaf Kirch63203772007-12-13 12:43:25 -06001708iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001709{
Olaf Kirch63203772007-12-13 12:43:25 -06001710 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001711
Olaf Kirch63203772007-12-13 12:43:25 -06001712 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001713
1714 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001715
1716 /* If the user passed an items pointer, he wants a copy of
1717 * the array. */
1718 if (items)
1719 num_arrays++;
1720 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1721 if (q->pool == NULL)
1722 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001723
1724 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1725 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001726 if (q->queue == ERR_PTR(-ENOMEM))
1727 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001728
1729 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001730 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001731 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001732 q->max = i;
1733 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001734 }
Mike Christie7996a772006-04-06 21:13:41 -05001735 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1736 }
Olaf Kirch63203772007-12-13 12:43:25 -06001737
1738 if (items) {
1739 *items = q->pool + max;
1740 memcpy(*items, q->pool, max * sizeof(void *));
1741 }
1742
Mike Christie7996a772006-04-06 21:13:41 -05001743 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001744
1745enomem:
1746 iscsi_pool_free(q);
1747 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001748}
1749EXPORT_SYMBOL_GPL(iscsi_pool_init);
1750
Olaf Kirch63203772007-12-13 12:43:25 -06001751void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001752{
1753 int i;
1754
1755 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001756 kfree(q->pool[i]);
1757 if (q->pool)
1758 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001759}
1760EXPORT_SYMBOL_GPL(iscsi_pool_free);
1761
1762/*
1763 * iSCSI Session's hostdata organization:
1764 *
1765 * *------------------* <== hostdata_session(host->hostdata)
1766 * | ptr to class sess|
1767 * |------------------| <== iscsi_hostdata(host->hostdata)
1768 * | iscsi_session |
1769 * *------------------*
1770 */
1771
1772#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1773 _sz % sizeof(unsigned long))
1774
1775#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1776
1777/**
1778 * iscsi_session_setup - create iscsi cls session and host and session
1779 * @scsit: scsi transport template
1780 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001781 * @cmds_max: scsi host can queue
1782 * @qdepth: scsi host cmds per lun
1783 * @cmd_task_size: LLD ctask private data size
1784 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001785 * @initial_cmdsn: initial CmdSN
1786 * @hostno: host no allocated
1787 *
1788 * This can be used by software iscsi_transports that allocate
1789 * a session per scsi host.
1790 **/
1791struct iscsi_cls_session *
1792iscsi_session_setup(struct iscsi_transport *iscsit,
1793 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001794 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001795 int cmd_task_size, int mgmt_task_size,
1796 uint32_t initial_cmdsn, uint32_t *hostno)
1797{
1798 struct Scsi_Host *shost;
1799 struct iscsi_session *session;
1800 struct iscsi_cls_session *cls_session;
1801 int cmd_i;
1802
Mike Christie15482712007-05-30 12:57:19 -05001803 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1804 if (qdepth != 0)
1805 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1806 "Queue depth must be between 1 and %d.\n",
1807 qdepth, ISCSI_MAX_CMD_PER_LUN);
1808 qdepth = ISCSI_DEF_CMD_PER_LUN;
1809 }
1810
Mike Christie31ed0bf2008-02-26 12:35:23 -06001811 if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET ||
1812 cmds_max < 2) {
Mike Christie15482712007-05-30 12:57:19 -05001813 if (cmds_max != 0)
1814 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1815 "can_queue must be a power of 2 and between "
1816 "2 and %d - setting to %d.\n", cmds_max,
1817 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1818 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1819 }
1820
Mike Christie7996a772006-04-06 21:13:41 -05001821 shost = scsi_host_alloc(iscsit->host_template,
1822 hostdata_privsize(sizeof(*session)));
1823 if (!shost)
1824 return NULL;
1825
Mike Christie15482712007-05-30 12:57:19 -05001826 /* the iscsi layer takes one task for reserve */
1827 shost->can_queue = cmds_max - 1;
1828 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001829 shost->max_id = 1;
1830 shost->max_channel = 0;
1831 shost->max_lun = iscsit->max_lun;
1832 shost->max_cmd_len = iscsit->max_cmd_len;
1833 shost->transportt = scsit;
1834 shost->transportt->create_work_queue = 1;
Mike Christief6d51802007-12-13 12:43:30 -06001835 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christie7996a772006-04-06 21:13:41 -05001836 *hostno = shost->host_no;
1837
1838 session = iscsi_hostdata(shost->hostdata);
1839 memset(session, 0, sizeof(struct iscsi_session));
1840 session->host = shost;
1841 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001842 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001843 session->lu_reset_timeout = 15;
1844 session->abort_timeout = 10;
Mike Christie7996a772006-04-06 21:13:41 -05001845 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001846 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001847 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001848 session->exp_cmdsn = initial_cmdsn + 1;
1849 session->max_cmdsn = initial_cmdsn + 1;
1850 session->max_r2t = 1;
1851 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001852 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001853
1854 /* initialize SCSI PDU commands pool */
1855 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1856 (void***)&session->cmds,
1857 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1858 goto cmdpool_alloc_fail;
1859
1860 /* pre-format cmds pool with ITT */
1861 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1862 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1863
1864 if (cmd_task_size)
1865 ctask->dd_data = &ctask[1];
1866 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001867 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001868 }
1869
1870 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001871
1872 /* initialize immediate command pool */
1873 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1874 (void***)&session->mgmt_cmds,
1875 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1876 goto mgmtpool_alloc_fail;
1877
1878
1879 /* pre-format immediate cmds pool with ITT */
1880 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1881 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1882
1883 if (mgmt_task_size)
1884 mtask->dd_data = &mtask[1];
1885 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001886 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001887 }
1888
1889 if (scsi_add_host(shost, NULL))
1890 goto add_host_fail;
1891
Mike Christief53a88d2006-06-28 12:00:27 -05001892 if (!try_module_get(iscsit->owner))
1893 goto cls_session_fail;
1894
Mike Christie6a8a0d32006-06-28 12:00:31 -05001895 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001896 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001897 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001898 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1899
1900 return cls_session;
1901
Mike Christief53a88d2006-06-28 12:00:27 -05001902module_put:
1903 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001904cls_session_fail:
1905 scsi_remove_host(shost);
1906add_host_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001907 iscsi_pool_free(&session->mgmtpool);
Mike Christie7996a772006-04-06 21:13:41 -05001908mgmtpool_alloc_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001909 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001910cmdpool_alloc_fail:
1911 scsi_host_put(shost);
1912 return NULL;
1913}
1914EXPORT_SYMBOL_GPL(iscsi_session_setup);
1915
1916/**
1917 * iscsi_session_teardown - destroy session, host, and cls_session
1918 * shost: scsi host
1919 *
1920 * This can be used by software iscsi_transports that allocate
1921 * a session per scsi host.
1922 **/
1923void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1924{
1925 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1926 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001927 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001928
Mike Christie26974782007-12-13 12:43:29 -06001929 iscsi_remove_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001930 scsi_remove_host(shost);
1931
Olaf Kirch63203772007-12-13 12:43:25 -06001932 iscsi_pool_free(&session->mgmtpool);
1933 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001934
Mike Christieb2c64162007-05-30 12:57:16 -05001935 kfree(session->password);
1936 kfree(session->password_in);
1937 kfree(session->username);
1938 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001939 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001940 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001941 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001942 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001943
Mike Christie26974782007-12-13 12:43:29 -06001944 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001945 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001946 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001947}
1948EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1949
1950/**
1951 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1952 * @cls_session: iscsi_cls_session
1953 * @conn_idx: cid
1954 **/
1955struct iscsi_cls_conn *
1956iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1957{
1958 struct iscsi_session *session = class_to_transport_session(cls_session);
1959 struct iscsi_conn *conn;
1960 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001961 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001962
1963 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1964 if (!cls_conn)
1965 return NULL;
1966 conn = cls_conn->dd_data;
1967 memset(conn, 0, sizeof(*conn));
1968
1969 conn->session = session;
1970 conn->cls_conn = cls_conn;
1971 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1972 conn->id = conn_idx;
1973 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001974 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06001975
1976 init_timer(&conn->transport_timer);
1977 conn->transport_timer.data = (unsigned long)conn;
1978 conn->transport_timer.function = iscsi_check_transport_timeouts;
1979
Mike Christie7996a772006-04-06 21:13:41 -05001980 INIT_LIST_HEAD(&conn->run_list);
1981 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06001982 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05001983 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06001984 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00001985 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001986
1987 /* allocate login_mtask used for the login/text sequences */
1988 spin_lock_bh(&session->lock);
1989 if (!__kfifo_get(session->mgmtpool.queue,
1990 (void*)&conn->login_mtask,
1991 sizeof(void*))) {
1992 spin_unlock_bh(&session->lock);
1993 goto login_mtask_alloc_fail;
1994 }
1995 spin_unlock_bh(&session->lock);
1996
Mike Christiebf32ed32007-02-28 17:32:17 -06001997 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001998 if (!data)
1999 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05002000 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002001
Mike Christie843c0a82007-12-13 12:43:20 -06002002 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002003 init_waitqueue_head(&conn->ehwait);
2004
2005 return cls_conn;
2006
Mike Christied36ab6f2006-05-18 20:31:34 -05002007login_mtask_data_alloc_fail:
2008 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2009 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002010login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002011 iscsi_destroy_conn(cls_conn);
2012 return NULL;
2013}
2014EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2015
2016/**
2017 * iscsi_conn_teardown - teardown iscsi connection
2018 * cls_conn: iscsi class connection
2019 *
2020 * TODO: we may need to make this into a two step process
2021 * like scsi-mls remove + put host
2022 */
2023void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2024{
2025 struct iscsi_conn *conn = cls_conn->dd_data;
2026 struct iscsi_session *session = conn->session;
2027 unsigned long flags;
2028
Mike Christief6d51802007-12-13 12:43:30 -06002029 del_timer_sync(&conn->transport_timer);
2030
Mike Christie7996a772006-04-06 21:13:41 -05002031 spin_lock_bh(&session->lock);
2032 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2033 if (session->leadconn == conn) {
2034 /*
2035 * leading connection? then give up on recovery.
2036 */
2037 session->state = ISCSI_STATE_TERMINATE;
2038 wake_up(&conn->ehwait);
2039 }
2040 spin_unlock_bh(&session->lock);
2041
Mike Christie7996a772006-04-06 21:13:41 -05002042 /*
2043 * Block until all in-progress commands for this connection
2044 * time out or fail.
2045 */
2046 for (;;) {
2047 spin_lock_irqsave(session->host->host_lock, flags);
2048 if (!session->host->host_busy) { /* OK for ERL == 0 */
2049 spin_unlock_irqrestore(session->host->host_lock, flags);
2050 break;
2051 }
2052 spin_unlock_irqrestore(session->host->host_lock, flags);
2053 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002054 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2055 "host_busy %d host_failed %d\n",
2056 session->host->host_busy,
2057 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002058 /*
2059 * force eh_abort() to unblock
2060 */
2061 wake_up(&conn->ehwait);
2062 }
2063
Mike Christie779ea122007-02-28 17:32:15 -06002064 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002065 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002066
Mike Christie7996a772006-04-06 21:13:41 -05002067 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002068 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002069 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05002070 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
2071 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002072 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002073 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002074 spin_unlock_bh(&session->lock);
2075
Mike Christie7996a772006-04-06 21:13:41 -05002076 iscsi_destroy_conn(cls_conn);
2077}
2078EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2079
2080int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2081{
2082 struct iscsi_conn *conn = cls_conn->dd_data;
2083 struct iscsi_session *session = conn->session;
2084
Mike Christieffd04362006-08-31 18:09:24 -04002085 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002086 iscsi_conn_printk(KERN_ERR, conn,
2087 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002088 return -EPERM;
2089 }
2090
Mike Christiedb98ccd2006-08-31 18:09:31 -04002091 if ((session->imm_data_en || !session->initial_r2t_en) &&
2092 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002093 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2094 "first_burst %d max_burst %d\n",
2095 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002096 return -EINVAL;
2097 }
2098
Mike Christief6d51802007-12-13 12:43:30 -06002099 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002100 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2101 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002102 conn->recv_timeout = 5;
2103 }
2104
2105 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002106 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2107 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002108 conn->ping_timeout = 5;
2109 }
2110
Mike Christie7996a772006-04-06 21:13:41 -05002111 spin_lock_bh(&session->lock);
2112 conn->c_stage = ISCSI_CONN_STARTED;
2113 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002114 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002115
Mike Christief6d51802007-12-13 12:43:30 -06002116 conn->last_recv = jiffies;
2117 conn->last_ping = jiffies;
2118 if (conn->recv_timeout && conn->ping_timeout)
2119 mod_timer(&conn->transport_timer,
2120 jiffies + (conn->recv_timeout * HZ));
2121
Mike Christie7996a772006-04-06 21:13:41 -05002122 switch(conn->stop_stage) {
2123 case STOP_CONN_RECOVER:
2124 /*
2125 * unblock eh_abort() if it is blocked. re-try all
2126 * commands after successful recovery
2127 */
Mike Christie7996a772006-04-06 21:13:41 -05002128 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002129 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002130 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002131 if (session->age == 16)
2132 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002133 break;
Mike Christie7996a772006-04-06 21:13:41 -05002134 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002135 conn->stop_stage = 0;
2136 break;
Mike Christie7996a772006-04-06 21:13:41 -05002137 default:
2138 break;
2139 }
2140 spin_unlock_bh(&session->lock);
2141
Mike Christie6eabafb2008-01-31 13:36:43 -06002142 iscsi_unblock_session(session_to_cls(session));
2143 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002144 return 0;
2145}
2146EXPORT_SYMBOL_GPL(iscsi_conn_start);
2147
2148static void
2149flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2150{
2151 struct iscsi_mgmt_task *mtask, *tmp;
2152
2153 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06002154 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2155 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002156 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002157 }
2158
2159 /* handle running */
2160 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2161 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002162 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002163 }
2164
2165 conn->mtask = NULL;
2166}
2167
Mike Christie656cffc2006-05-18 20:31:42 -05002168static void iscsi_start_session_recovery(struct iscsi_session *session,
2169 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002170{
Mike Christieed2abc72006-05-02 19:46:40 -05002171 int old_stop_stage;
2172
Mike Christief6d51802007-12-13 12:43:30 -06002173 del_timer_sync(&conn->transport_timer);
2174
Mike Christie6724add2007-08-15 01:38:30 -05002175 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002176 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002177 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002178 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002179 mutex_unlock(&session->eh_mutex);
2180 return;
2181 }
2182
2183 /*
2184 * The LLD either freed/unset the lock on us, or userspace called
2185 * stop but did not create a proper connection (connection was never
2186 * bound or it was unbound then stop was called).
2187 */
2188 if (!conn->recv_lock) {
2189 spin_unlock_bh(&session->lock);
2190 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002191 return;
2192 }
Mike Christieed2abc72006-05-02 19:46:40 -05002193
2194 /*
2195 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05002196 * up the login task and connection. We do not need to block and set
2197 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002198 */
Mike Christie67a61112006-05-30 00:37:20 -05002199 if (flag == STOP_CONN_TERM)
2200 session->state = ISCSI_STATE_TERMINATE;
2201 else if (conn->stop_stage != STOP_CONN_RECOVER)
2202 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002203
2204 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002205 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002206 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002207 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002208
2209 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002210
Mike Christie1c834692006-07-24 15:47:26 -05002211 write_lock_bh(conn->recv_lock);
2212 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2213 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002214
Mike Christie7996a772006-04-06 21:13:41 -05002215 /*
2216 * for connection level recovery we should not calculate
2217 * header digest. conn->hdr_size used for optimization
2218 * in hdr_extract() and will be re-negotiated at
2219 * set_param() time.
2220 */
2221 if (flag == STOP_CONN_RECOVER) {
2222 conn->hdrdgst_en = 0;
2223 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002224 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002225 old_stop_stage != STOP_CONN_RECOVER) {
2226 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05002227 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05002228 }
Mike Christie7996a772006-04-06 21:13:41 -05002229 }
Mike Christie656cffc2006-05-18 20:31:42 -05002230
Mike Christie656cffc2006-05-18 20:31:42 -05002231 /*
2232 * flush queues.
2233 */
2234 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002235 fail_all_commands(conn, -1,
2236 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002237 flush_control_queues(session, conn);
2238 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002239 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002240}
Mike Christie7996a772006-04-06 21:13:41 -05002241
2242void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2243{
2244 struct iscsi_conn *conn = cls_conn->dd_data;
2245 struct iscsi_session *session = conn->session;
2246
2247 switch (flag) {
2248 case STOP_CONN_RECOVER:
2249 case STOP_CONN_TERM:
2250 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002251 break;
Mike Christie7996a772006-04-06 21:13:41 -05002252 default:
Mike Christie322d7392008-01-31 13:36:52 -06002253 iscsi_conn_printk(KERN_ERR, conn,
2254 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002255 }
2256}
2257EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2258
2259int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2260 struct iscsi_cls_conn *cls_conn, int is_leading)
2261{
2262 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04002263 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002264
Mike Christie7996a772006-04-06 21:13:41 -05002265 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002266 if (is_leading)
2267 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002268 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002269
2270 /*
2271 * Unblock xmitworker(), Login Phase will pass through.
2272 */
2273 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2274 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2275 return 0;
2276}
2277EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2278
Mike Christiea54a52c2006-06-28 12:00:23 -05002279
2280int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2281 enum iscsi_param param, char *buf, int buflen)
2282{
2283 struct iscsi_conn *conn = cls_conn->dd_data;
2284 struct iscsi_session *session = conn->session;
2285 uint32_t value;
2286
2287 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002288 case ISCSI_PARAM_FAST_ABORT:
2289 sscanf(buf, "%d", &session->fast_abort);
2290 break;
Mike Christief6d51802007-12-13 12:43:30 -06002291 case ISCSI_PARAM_ABORT_TMO:
2292 sscanf(buf, "%d", &session->abort_timeout);
2293 break;
2294 case ISCSI_PARAM_LU_RESET_TMO:
2295 sscanf(buf, "%d", &session->lu_reset_timeout);
2296 break;
2297 case ISCSI_PARAM_PING_TMO:
2298 sscanf(buf, "%d", &conn->ping_timeout);
2299 break;
2300 case ISCSI_PARAM_RECV_TMO:
2301 sscanf(buf, "%d", &conn->recv_timeout);
2302 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002303 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2304 sscanf(buf, "%d", &conn->max_recv_dlength);
2305 break;
2306 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2307 sscanf(buf, "%d", &conn->max_xmit_dlength);
2308 break;
2309 case ISCSI_PARAM_HDRDGST_EN:
2310 sscanf(buf, "%d", &conn->hdrdgst_en);
2311 break;
2312 case ISCSI_PARAM_DATADGST_EN:
2313 sscanf(buf, "%d", &conn->datadgst_en);
2314 break;
2315 case ISCSI_PARAM_INITIAL_R2T_EN:
2316 sscanf(buf, "%d", &session->initial_r2t_en);
2317 break;
2318 case ISCSI_PARAM_MAX_R2T:
2319 sscanf(buf, "%d", &session->max_r2t);
2320 break;
2321 case ISCSI_PARAM_IMM_DATA_EN:
2322 sscanf(buf, "%d", &session->imm_data_en);
2323 break;
2324 case ISCSI_PARAM_FIRST_BURST:
2325 sscanf(buf, "%d", &session->first_burst);
2326 break;
2327 case ISCSI_PARAM_MAX_BURST:
2328 sscanf(buf, "%d", &session->max_burst);
2329 break;
2330 case ISCSI_PARAM_PDU_INORDER_EN:
2331 sscanf(buf, "%d", &session->pdu_inorder_en);
2332 break;
2333 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2334 sscanf(buf, "%d", &session->dataseq_inorder_en);
2335 break;
2336 case ISCSI_PARAM_ERL:
2337 sscanf(buf, "%d", &session->erl);
2338 break;
2339 case ISCSI_PARAM_IFMARKER_EN:
2340 sscanf(buf, "%d", &value);
2341 BUG_ON(value);
2342 break;
2343 case ISCSI_PARAM_OFMARKER_EN:
2344 sscanf(buf, "%d", &value);
2345 BUG_ON(value);
2346 break;
2347 case ISCSI_PARAM_EXP_STATSN:
2348 sscanf(buf, "%u", &conn->exp_statsn);
2349 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002350 case ISCSI_PARAM_USERNAME:
2351 kfree(session->username);
2352 session->username = kstrdup(buf, GFP_KERNEL);
2353 if (!session->username)
2354 return -ENOMEM;
2355 break;
2356 case ISCSI_PARAM_USERNAME_IN:
2357 kfree(session->username_in);
2358 session->username_in = kstrdup(buf, GFP_KERNEL);
2359 if (!session->username_in)
2360 return -ENOMEM;
2361 break;
2362 case ISCSI_PARAM_PASSWORD:
2363 kfree(session->password);
2364 session->password = kstrdup(buf, GFP_KERNEL);
2365 if (!session->password)
2366 return -ENOMEM;
2367 break;
2368 case ISCSI_PARAM_PASSWORD_IN:
2369 kfree(session->password_in);
2370 session->password_in = kstrdup(buf, GFP_KERNEL);
2371 if (!session->password_in)
2372 return -ENOMEM;
2373 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002374 case ISCSI_PARAM_TARGET_NAME:
2375 /* this should not change between logins */
2376 if (session->targetname)
2377 break;
2378
2379 session->targetname = kstrdup(buf, GFP_KERNEL);
2380 if (!session->targetname)
2381 return -ENOMEM;
2382 break;
2383 case ISCSI_PARAM_TPGT:
2384 sscanf(buf, "%d", &session->tpgt);
2385 break;
2386 case ISCSI_PARAM_PERSISTENT_PORT:
2387 sscanf(buf, "%d", &conn->persistent_port);
2388 break;
2389 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2390 /*
2391 * this is the address returned in discovery so it should
2392 * not change between logins.
2393 */
2394 if (conn->persistent_address)
2395 break;
2396
2397 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2398 if (!conn->persistent_address)
2399 return -ENOMEM;
2400 break;
2401 default:
2402 return -ENOSYS;
2403 }
2404
2405 return 0;
2406}
2407EXPORT_SYMBOL_GPL(iscsi_set_param);
2408
2409int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2410 enum iscsi_param param, char *buf)
2411{
2412 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2413 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2414 int len;
2415
2416 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002417 case ISCSI_PARAM_FAST_ABORT:
2418 len = sprintf(buf, "%d\n", session->fast_abort);
2419 break;
Mike Christief6d51802007-12-13 12:43:30 -06002420 case ISCSI_PARAM_ABORT_TMO:
2421 len = sprintf(buf, "%d\n", session->abort_timeout);
2422 break;
2423 case ISCSI_PARAM_LU_RESET_TMO:
2424 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2425 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002426 case ISCSI_PARAM_INITIAL_R2T_EN:
2427 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2428 break;
2429 case ISCSI_PARAM_MAX_R2T:
2430 len = sprintf(buf, "%hu\n", session->max_r2t);
2431 break;
2432 case ISCSI_PARAM_IMM_DATA_EN:
2433 len = sprintf(buf, "%d\n", session->imm_data_en);
2434 break;
2435 case ISCSI_PARAM_FIRST_BURST:
2436 len = sprintf(buf, "%u\n", session->first_burst);
2437 break;
2438 case ISCSI_PARAM_MAX_BURST:
2439 len = sprintf(buf, "%u\n", session->max_burst);
2440 break;
2441 case ISCSI_PARAM_PDU_INORDER_EN:
2442 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2443 break;
2444 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2445 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2446 break;
2447 case ISCSI_PARAM_ERL:
2448 len = sprintf(buf, "%d\n", session->erl);
2449 break;
2450 case ISCSI_PARAM_TARGET_NAME:
2451 len = sprintf(buf, "%s\n", session->targetname);
2452 break;
2453 case ISCSI_PARAM_TPGT:
2454 len = sprintf(buf, "%d\n", session->tpgt);
2455 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002456 case ISCSI_PARAM_USERNAME:
2457 len = sprintf(buf, "%s\n", session->username);
2458 break;
2459 case ISCSI_PARAM_USERNAME_IN:
2460 len = sprintf(buf, "%s\n", session->username_in);
2461 break;
2462 case ISCSI_PARAM_PASSWORD:
2463 len = sprintf(buf, "%s\n", session->password);
2464 break;
2465 case ISCSI_PARAM_PASSWORD_IN:
2466 len = sprintf(buf, "%s\n", session->password_in);
2467 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002468 default:
2469 return -ENOSYS;
2470 }
2471
2472 return len;
2473}
2474EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2475
2476int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2477 enum iscsi_param param, char *buf)
2478{
2479 struct iscsi_conn *conn = cls_conn->dd_data;
2480 int len;
2481
2482 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002483 case ISCSI_PARAM_PING_TMO:
2484 len = sprintf(buf, "%u\n", conn->ping_timeout);
2485 break;
2486 case ISCSI_PARAM_RECV_TMO:
2487 len = sprintf(buf, "%u\n", conn->recv_timeout);
2488 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002489 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2490 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2491 break;
2492 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2493 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2494 break;
2495 case ISCSI_PARAM_HDRDGST_EN:
2496 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2497 break;
2498 case ISCSI_PARAM_DATADGST_EN:
2499 len = sprintf(buf, "%d\n", conn->datadgst_en);
2500 break;
2501 case ISCSI_PARAM_IFMARKER_EN:
2502 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2503 break;
2504 case ISCSI_PARAM_OFMARKER_EN:
2505 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2506 break;
2507 case ISCSI_PARAM_EXP_STATSN:
2508 len = sprintf(buf, "%u\n", conn->exp_statsn);
2509 break;
2510 case ISCSI_PARAM_PERSISTENT_PORT:
2511 len = sprintf(buf, "%d\n", conn->persistent_port);
2512 break;
2513 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2514 len = sprintf(buf, "%s\n", conn->persistent_address);
2515 break;
2516 default:
2517 return -ENOSYS;
2518 }
2519
2520 return len;
2521}
2522EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2523
Mike Christie0801c242007-05-30 12:57:12 -05002524int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2525 char *buf)
2526{
2527 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2528 int len;
2529
2530 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002531 case ISCSI_HOST_PARAM_NETDEV_NAME:
2532 if (!session->netdev)
2533 len = sprintf(buf, "%s\n", "default");
2534 else
2535 len = sprintf(buf, "%s\n", session->netdev);
2536 break;
Mike Christie0801c242007-05-30 12:57:12 -05002537 case ISCSI_HOST_PARAM_HWADDRESS:
2538 if (!session->hwaddress)
2539 len = sprintf(buf, "%s\n", "default");
2540 else
2541 len = sprintf(buf, "%s\n", session->hwaddress);
2542 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002543 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2544 if (!session->initiatorname)
2545 len = sprintf(buf, "%s\n", "unknown");
2546 else
2547 len = sprintf(buf, "%s\n", session->initiatorname);
2548 break;
2549
Mike Christie0801c242007-05-30 12:57:12 -05002550 default:
2551 return -ENOSYS;
2552 }
2553
2554 return len;
2555}
2556EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2557
2558int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2559 char *buf, int buflen)
2560{
2561 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2562
2563 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002564 case ISCSI_HOST_PARAM_NETDEV_NAME:
2565 if (!session->netdev)
2566 session->netdev = kstrdup(buf, GFP_KERNEL);
2567 break;
Mike Christie0801c242007-05-30 12:57:12 -05002568 case ISCSI_HOST_PARAM_HWADDRESS:
2569 if (!session->hwaddress)
2570 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2571 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002572 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2573 if (!session->initiatorname)
2574 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2575 break;
Mike Christie0801c242007-05-30 12:57:12 -05002576 default:
2577 return -ENOSYS;
2578 }
2579
2580 return 0;
2581}
2582EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2583
Mike Christie7996a772006-04-06 21:13:41 -05002584MODULE_AUTHOR("Mike Christie");
2585MODULE_DESCRIPTION("iSCSI library functions");
2586MODULE_LICENSE("GPL");