blob: f9539af28f028cc68996265aedf051dc0eafe84e [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Mike Christie77a23c22007-05-30 12:57:18 -050041/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050043
Mike Christie77a23c22007-05-30 12:57:18 -050044static int iscsi_sna_lt(u32 n1, u32 n2)
45{
46 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
47 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
48}
49
50/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
51static int iscsi_sna_lte(u32 n1, u32 n2)
52{
53 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55}
56
57void
58iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050059{
60 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
61 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063 /*
64 * standard specifies this check for when to update expected and
65 * max sequence numbers
66 */
67 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
68 return;
69
70 if (exp_cmdsn != session->exp_cmdsn &&
71 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050072 session->exp_cmdsn = exp_cmdsn;
73
Mike Christie77a23c22007-05-30 12:57:18 -050074 if (max_cmdsn != session->max_cmdsn &&
75 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
76 session->max_cmdsn = max_cmdsn;
77 /*
78 * if the window closed with IO queued, then kick the
79 * xmit thread
80 */
81 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie052d0142008-05-21 15:54:05 -050082 !list_empty(&session->leadconn->mgmtqueue)) {
83 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
84 scsi_queue_work(session->host,
85 &session->leadconn->xmitwork);
86 }
Mike Christie77a23c22007-05-30 12:57:18 -050087 }
Mike Christie7996a772006-04-06 21:13:41 -050088}
Mike Christie77a23c22007-05-30 12:57:18 -050089EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050090
Mike Christie9c19a7d2008-05-21 15:54:09 -050091void iscsi_prep_unsolicit_data_pdu(struct iscsi_task *task,
Mike Christieffd04362006-08-31 18:09:24 -040092 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050093{
Mike Christie9c19a7d2008-05-21 15:54:09 -050094 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -050095
96 memset(hdr, 0, sizeof(struct iscsi_data));
97 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
Mike Christie9c19a7d2008-05-21 15:54:09 -050098 hdr->datasn = cpu_to_be32(task->unsol_datasn);
99 task->unsol_datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500100 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500101 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
Mike Christie7996a772006-04-06 21:13:41 -0500102
Mike Christie9c19a7d2008-05-21 15:54:09 -0500103 hdr->itt = task->hdr->itt;
Mike Christie7996a772006-04-06 21:13:41 -0500104 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500105 hdr->offset = cpu_to_be32(task->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500106
Mike Christie9c19a7d2008-05-21 15:54:09 -0500107 if (task->unsol_count > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500108 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500109 task->data_count = conn->max_xmit_dlength;
110 task->unsol_offset += task->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500111 hdr->flags = 0;
112 } else {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500113 hton24(hdr->dlength, task->unsol_count);
114 task->data_count = task->unsol_count;
Mike Christie7996a772006-04-06 21:13:41 -0500115 hdr->flags = ISCSI_FLAG_CMD_FINAL;
116 }
117}
118EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
119
Mike Christie9c19a7d2008-05-21 15:54:09 -0500120static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600121{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500122 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600123
Mike Christie9c19a7d2008-05-21 15:54:09 -0500124 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600125 WARN_ON(1);
126 return -EINVAL;
127 }
128
129 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500130 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600131 return 0;
132}
133
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500134/*
135 * make an extended cdb AHS
136 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500137static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500138{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500139 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500140 unsigned rlen, pad_len;
141 unsigned short ahslength;
142 struct iscsi_ecdb_ahdr *ecdb_ahdr;
143 int rc;
144
Mike Christie9c19a7d2008-05-21 15:54:09 -0500145 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500146 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
147
148 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
149 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
150
151 pad_len = iscsi_padding(rlen);
152
Mike Christie9c19a7d2008-05-21 15:54:09 -0500153 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500154 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
155 if (rc)
156 return rc;
157
158 if (pad_len)
159 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
160
161 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
162 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
163 ecdb_ahdr->reserved = 0;
164 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
165
166 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
167 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500168 cmd->cmd_len, rlen, pad_len, ahslength, task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500169
170 return 0;
171}
172
Mike Christie9c19a7d2008-05-21 15:54:09 -0500173static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500174{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500175 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500176 struct iscsi_rlength_ahdr *rlen_ahdr;
177 int rc;
178
Mike Christie9c19a7d2008-05-21 15:54:09 -0500179 rlen_ahdr = iscsi_next_hdr(task);
180 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500181 if (rc)
182 return rc;
183
184 rlen_ahdr->ahslength =
185 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
186 sizeof(rlen_ahdr->reserved));
187 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
188 rlen_ahdr->reserved = 0;
189 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
190
191 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
192 "rlen_ahdr->ahslength(%d)\n",
193 be32_to_cpu(rlen_ahdr->read_length),
194 be16_to_cpu(rlen_ahdr->ahslength));
195 return 0;
196}
197
Mike Christie7996a772006-04-06 21:13:41 -0500198/**
199 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500200 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500201 *
202 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
203 * fields like dlength or final based on how much data it sends
204 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500205static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500206{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500207 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500208 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500209 struct iscsi_cmd *hdr = task->hdr;
210 struct scsi_cmnd *sc = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500211 unsigned hdrlength, cmd_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600212 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500213
Mike Christie9c19a7d2008-05-21 15:54:09 -0500214 task->hdr_len = 0;
215 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600216 if (rc)
217 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600218 hdr->opcode = ISCSI_OP_SCSI_CMD;
219 hdr->flags = ISCSI_ATTR_SIMPLE;
220 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500221 hdr->itt = build_itt(task->itt, session->age);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600222 hdr->cmdsn = cpu_to_be32(session->cmdsn);
223 session->cmdsn++;
224 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500225 cmd_len = sc->cmd_len;
226 if (cmd_len < ISCSI_CDB_SIZE)
227 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
228 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500229 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500230 if (rc)
231 return rc;
232 cmd_len = ISCSI_CDB_SIZE;
233 }
234 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500235
Mike Christie9c19a7d2008-05-21 15:54:09 -0500236 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500237 if (scsi_bidi_cmnd(sc)) {
238 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500239 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500240 if (rc)
241 return rc;
242 }
Mike Christie7996a772006-04-06 21:13:41 -0500243 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500244 unsigned out_len = scsi_out(sc)->length;
245 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500246 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
247 /*
248 * Write counters:
249 *
250 * imm_count bytes to be sent right after
251 * SCSI PDU Header
252 *
253 * unsol_count bytes(as Data-Out) to be sent
254 * without R2T ack right after
255 * immediate data
256 *
257 * r2t_data_count bytes to be sent via R2T ack's
258 *
259 * pad_count bytes to be sent as zero-padding
260 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500261 task->unsol_count = 0;
262 task->unsol_offset = 0;
263 task->unsol_datasn = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500264
265 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500266 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500267 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500268 conn->max_xmit_dlength);
269 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500270 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500271 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500272 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500273 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600274 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500275
Mike Christieffd04362006-08-31 18:09:24 -0400276 if (!session->initial_r2t_en) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500277 task->unsol_count = min(session->first_burst, out_len)
278 - task->imm_count;
279 task->unsol_offset = task->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400280 }
281
Mike Christie9c19a7d2008-05-21 15:54:09 -0500282 if (!task->unsol_count)
Mike Christie7996a772006-04-06 21:13:41 -0500283 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600284 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500285 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500286 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
287 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500288 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500289
290 if (sc->sc_data_direction == DMA_FROM_DEVICE)
291 hdr->flags |= ISCSI_FLAG_CMD_READ;
292 }
293
Boaz Harrosh004d6532007-12-13 12:43:23 -0600294 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500295 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600296
297 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
298 hdrlength /= ISCSI_PAD_LEN;
299
300 WARN_ON(hdrlength >= 256);
301 hdr->hlength = hdrlength & 0xFF;
302
Mike Christie3e5c28a2008-05-21 15:54:06 -0500303 if (conn->session->tt->init_task &&
Mike Christie9c19a7d2008-05-21 15:54:09 -0500304 conn->session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500305 return -EIO;
306
Mike Christie9c19a7d2008-05-21 15:54:09 -0500307 task->state = ISCSI_TASK_RUNNING;
308 list_move_tail(&task->running, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500309
Olaf Kircha8ac6312007-12-13 12:43:35 -0600310 conn->scsicmd_pdus_cnt++;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500311 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
312 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ?
313 "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ?
Mike Christie9c19a7d2008-05-21 15:54:09 -0500314 "write" : "read", conn->id, sc, sc->cmnd[0], task->itt,
Mike Christie3e5c28a2008-05-21 15:54:06 -0500315 scsi_bufflen(sc),
316 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
317 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600318 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500319}
Mike Christie7996a772006-04-06 21:13:41 -0500320
321/**
Mike Christie3e5c28a2008-05-21 15:54:06 -0500322 * iscsi_complete_command - finish a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500323 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500324 *
325 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500326 * This function returns the scsi command to scsi-ml or cleans
327 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500328 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500329static void iscsi_complete_command(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500330{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500331 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600332 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500333 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500334
Mike Christie9c19a7d2008-05-21 15:54:09 -0500335 list_del_init(&task->running);
336 task->state = ISCSI_TASK_COMPLETED;
337 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500338
Mike Christie9c19a7d2008-05-21 15:54:09 -0500339 if (conn->task == task)
340 conn->task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500341 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500342 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500343 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500344 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500345 return;
346
Mike Christie9c19a7d2008-05-21 15:54:09 -0500347 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500348
Mike Christie9c19a7d2008-05-21 15:54:09 -0500349 if (conn->ping_task == task)
350 conn->ping_task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500351
352 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500353 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500354 /* SCSI eh reuses commands to verify us */
355 sc->SCp.ptr = NULL;
356 /*
357 * queue command may call this to free the task, but
358 * not have setup the sc callback
359 */
360 if (sc->scsi_done)
361 sc->scsi_done(sc);
362 }
Mike Christie7996a772006-04-06 21:13:41 -0500363}
364
Mike Christie913e5bf2008-05-21 15:54:18 -0500365void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400366{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500367 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400368}
Mike Christie913e5bf2008-05-21 15:54:18 -0500369EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400370
Mike Christie9c19a7d2008-05-21 15:54:09 -0500371static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400372{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500373 if (atomic_dec_and_test(&task->refcount))
374 iscsi_complete_command(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400375}
376
Mike Christie9c19a7d2008-05-21 15:54:09 -0500377void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500378{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500379 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500380
381 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500382 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500383 spin_unlock_bh(&session->lock);
384}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500385EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500386
Mike Christieb3a7ea82007-12-13 12:43:26 -0600387/*
388 * session lock must be held
389 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500390static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600391 int err)
392{
393 struct scsi_cmnd *sc;
394
Mike Christie9c19a7d2008-05-21 15:54:09 -0500395 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600396 if (!sc)
397 return;
398
Mike Christie9c19a7d2008-05-21 15:54:09 -0500399 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600400 /*
401 * cmd never made it to the xmit thread, so we should not count
402 * the cmd in the sequencing
403 */
404 conn->session->queued_cmdsn--;
405 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500406 conn->session->tt->cleanup_task(conn, task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500407 /*
408 * Check if cleanup_task dropped the lock and the command completed,
409 */
410 if (!task->sc)
411 return;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600412
413 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500414 if (!scsi_bidi_cmnd(sc))
415 scsi_set_resid(sc, scsi_bufflen(sc));
416 else {
417 scsi_out(sc)->resid = scsi_out(sc)->length;
418 scsi_in(sc)->resid = scsi_in(sc)->length;
419 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500420
Mike Christie9c19a7d2008-05-21 15:54:09 -0500421 if (conn->task == task)
422 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600423 /* release ref from queuecommand */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500424 __iscsi_put_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600425}
426
Mike Christie3e5c28a2008-05-21 15:54:06 -0500427static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500428 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500429{
430 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431 struct iscsi_hdr *hdr = (struct iscsi_hdr *)task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500432 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
433
434 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
435 return -ENOTCONN;
436
437 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
438 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
439 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
440 /*
441 * pre-format CmdSN for outgoing PDU.
442 */
443 nop->cmdsn = cpu_to_be32(session->cmdsn);
444 if (hdr->itt != RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500445 hdr->itt = build_itt(task->itt, session->age);
Mike Christie052d0142008-05-21 15:54:05 -0500446 /*
447 * TODO: We always use immediate, so we never hit this.
448 * If we start to send tmfs or nops as non-immediate then
449 * we should start checking the cmdsn numbers for mgmt tasks.
450 */
451 if (conn->c_stage == ISCSI_CONN_STARTED &&
452 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
453 session->queued_cmdsn++;
454 session->cmdsn++;
455 }
456 }
457
Mike Christie3e5c28a2008-05-21 15:54:06 -0500458 if (session->tt->init_task)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500459 session->tt->init_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500460
461 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
462 session->state = ISCSI_STATE_LOGGING_OUT;
463
Mike Christie9c19a7d2008-05-21 15:54:09 -0500464 list_move_tail(&task->running, &conn->mgmt_run_list);
Mike Christie052d0142008-05-21 15:54:05 -0500465 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
466 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500467 task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500468 return 0;
469}
470
Mike Christie9c19a7d2008-05-21 15:54:09 -0500471static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600472__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
473 char *data, uint32_t data_size)
474{
475 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500476 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600477
478 if (session->state == ISCSI_STATE_TERMINATE)
479 return NULL;
480
481 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
482 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
483 /*
484 * Login and Text are sent serially, in
485 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500486 * Same task can be used. Same ITT must be used.
487 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600488 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500489 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600490 else {
491 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
492 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
493
Mike Christie3e5c28a2008-05-21 15:54:06 -0500494 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500495 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600496 return NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500497
498 if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
499 hdr->ttt == RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500500 conn->ping_task = task;
Mike Christie052d0142008-05-21 15:54:05 -0500501 conn->last_ping = jiffies;
502 }
Mike Christief6d51802007-12-13 12:43:30 -0600503 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500504 /*
505 * released in complete pdu for task we expect a response for, and
506 * released by the lld when it has transmitted the task for
507 * pdus we do not expect a response for.
508 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500509 atomic_set(&task->refcount, 1);
510 task->conn = conn;
511 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600512
513 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500514 memcpy(task->data, data, data_size);
515 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600516 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500517 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600518
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
520 INIT_LIST_HEAD(&task->running);
521 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500522
523 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 if (iscsi_prep_mgmt_task(conn, task)) {
525 __iscsi_put_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500526 return NULL;
527 }
528
Mike Christie9c19a7d2008-05-21 15:54:09 -0500529 if (session->tt->xmit_task(task))
530 task = NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500531
532 } else
533 scsi_queue_work(conn->session->host, &conn->xmitwork);
534
Mike Christie9c19a7d2008-05-21 15:54:09 -0500535 return task;
Mike Christief6d51802007-12-13 12:43:30 -0600536}
537
538int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
539 char *data, uint32_t data_size)
540{
541 struct iscsi_conn *conn = cls_conn->dd_data;
542 struct iscsi_session *session = conn->session;
543 int err = 0;
544
545 spin_lock_bh(&session->lock);
546 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
547 err = -EPERM;
548 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600549 return err;
550}
551EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
552
Mike Christie7996a772006-04-06 21:13:41 -0500553/**
554 * iscsi_cmd_rsp - SCSI Command Response processing
555 * @conn: iscsi connection
556 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500557 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500558 * @data: cmd data buffer
559 * @datalen: len of buffer
560 *
561 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500562 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500563 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500564static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500565 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500566 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500567{
Mike Christie7996a772006-04-06 21:13:41 -0500568 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
569 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500570 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500571
Mike Christie77a23c22007-05-30 12:57:18 -0500572 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500573 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
574
575 sc->result = (DID_OK << 16) | rhdr->cmd_status;
576
577 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
578 sc->result = DID_ERROR << 16;
579 goto out;
580 }
581
582 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600583 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500584
585 if (datalen < 2) {
586invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600587 iscsi_conn_printk(KERN_ERR, conn,
588 "Got CHECK_CONDITION but invalid data "
589 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500590 sc->result = DID_BAD_TARGET << 16;
591 goto out;
592 }
593
Harvey Harrison8f333992008-05-21 15:54:20 -0500594 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500595 if (datalen < senselen)
596 goto invalid_datalen;
597
598 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600599 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500600 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600601 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500602 }
603
Boaz Harroshc07d4442008-04-18 10:11:52 -0500604 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
605 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
606 int res_count = be32_to_cpu(rhdr->bi_residual_count);
607
608 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
609 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
610 res_count <= scsi_in(sc)->length))
611 scsi_in(sc)->resid = res_count;
612 else
613 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
614 }
615
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600616 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
617 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500618 int res_count = be32_to_cpu(rhdr->residual_count);
619
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600620 if (res_count > 0 &&
621 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
622 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500623 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900624 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500625 else
626 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500627 }
Mike Christie7996a772006-04-06 21:13:41 -0500628out:
629 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500630 (long)sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500631 conn->scsirsp_pdus_cnt++;
632
Mike Christie9c19a7d2008-05-21 15:54:09 -0500633 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500634}
635
Mike Christie1d9edf02008-09-24 11:46:09 -0500636/**
637 * iscsi_data_in_rsp - SCSI Data-In Response processing
638 * @conn: iscsi connection
639 * @hdr: iscsi pdu
640 * @task: scsi command task
641 **/
642static void
643iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
644 struct iscsi_task *task)
645{
646 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
647 struct scsi_cmnd *sc = task->sc;
648
649 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
650 return;
651
652 sc->result = (DID_OK << 16) | rhdr->cmd_status;
653 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
654 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
655 ISCSI_FLAG_DATA_OVERFLOW)) {
656 int res_count = be32_to_cpu(rhdr->residual_count);
657
658 if (res_count > 0 &&
659 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
660 res_count <= scsi_in(sc)->length))
661 scsi_in(sc)->resid = res_count;
662 else
663 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
664 }
665
666 conn->scsirsp_pdus_cnt++;
667 __iscsi_put_task(task);
668}
669
Mike Christie7ea8b822006-07-24 15:47:22 -0500670static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
671{
672 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
673
674 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
675 conn->tmfrsp_pdus_cnt++;
676
Mike Christie843c0a82007-12-13 12:43:20 -0600677 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500678 return;
679
680 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600681 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500682 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600683 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500684 else
Mike Christie843c0a82007-12-13 12:43:20 -0600685 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500686 wake_up(&conn->ehwait);
687}
688
Mike Christief6d51802007-12-13 12:43:30 -0600689static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
690{
691 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500692 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600693
Mike Christie9c19a7d2008-05-21 15:54:09 -0500694 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600695 return;
696
697 memset(&hdr, 0, sizeof(struct iscsi_nopout));
698 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
699 hdr.flags = ISCSI_FLAG_CMD_FINAL;
700
701 if (rhdr) {
702 memcpy(hdr.lun, rhdr->lun, 8);
703 hdr.ttt = rhdr->ttt;
704 hdr.itt = RESERVED_ITT;
705 } else
706 hdr.ttt = RESERVED_ITT;
707
Mike Christie9c19a7d2008-05-21 15:54:09 -0500708 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
709 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600710 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600711}
712
Mike Christie62f38302006-08-31 18:09:27 -0400713static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
714 char *data, int datalen)
715{
716 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
717 struct iscsi_hdr rejected_pdu;
718 uint32_t itt;
719
720 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
721
722 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
723 if (ntoh24(reject->dlength) > datalen)
724 return ISCSI_ERR_PROTO;
725
726 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
727 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000728 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600729 iscsi_conn_printk(KERN_ERR, conn,
730 "itt 0x%x had pdu (op 0x%x) rejected "
731 "due to DataDigest error.\n", itt,
732 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400733 }
734 }
735 return 0;
736}
737
Mike Christie7996a772006-04-06 21:13:41 -0500738/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500739 * iscsi_itt_to_task - look up task by itt
740 * @conn: iscsi connection
741 * @itt: itt
742 *
743 * This should be used for mgmt tasks like login and nops, or if
744 * the LDD's itt space does not include the session age.
745 *
746 * The session lock must be held.
747 */
748static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
749{
750 struct iscsi_session *session = conn->session;
751 uint32_t i;
752
753 if (itt == RESERVED_ITT)
754 return NULL;
755
756 i = get_itt(itt);
757 if (i >= session->cmds_max)
758 return NULL;
759
760 return session->cmds[i];
761}
762
763/**
Mike Christie7996a772006-04-06 21:13:41 -0500764 * __iscsi_complete_pdu - complete pdu
765 * @conn: iscsi conn
766 * @hdr: iscsi header
767 * @data: data buffer
768 * @datalen: len of data buffer
769 *
770 * Completes pdu processing by freeing any resources allocated at
771 * queuecommand or send generic. session lock must be held and verify
772 * itt must have been called.
773 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500774int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
775 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500776{
777 struct iscsi_session *session = conn->session;
778 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500779 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500780 uint32_t itt;
781
Mike Christief6d51802007-12-13 12:43:30 -0600782 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500783 rc = iscsi_verify_itt(conn, hdr->itt);
784 if (rc)
785 return rc;
786
Al Virob4377352007-02-09 16:39:40 +0000787 if (hdr->itt != RESERVED_ITT)
788 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500789 else
Al Virob4377352007-02-09 16:39:40 +0000790 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500791
Mike Christie3e5c28a2008-05-21 15:54:06 -0500792 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
793 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500794
Mike Christie3e5c28a2008-05-21 15:54:06 -0500795 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500796 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400797
Mike Christie7996a772006-04-06 21:13:41 -0500798 switch(opcode) {
799 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500800 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500801 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500802 break;
803 }
804
Al Virob4377352007-02-09 16:39:40 +0000805 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500806 break;
807
Mike Christief6d51802007-12-13 12:43:30 -0600808 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500809 break;
810 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400811 rc = iscsi_handle_reject(conn, hdr, data, datalen);
812 break;
Mike Christie7996a772006-04-06 21:13:41 -0500813 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500814 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400815 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
816 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500817 break;
818 default:
819 rc = ISCSI_ERR_BAD_OPCODE;
820 break;
821 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500822 goto out;
823 }
Mike Christie7996a772006-04-06 21:13:41 -0500824
Mike Christie3e5c28a2008-05-21 15:54:06 -0500825 switch(opcode) {
826 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500827 case ISCSI_OP_SCSI_DATA_IN:
828 task = iscsi_itt_to_ctask(conn, hdr->itt);
829 if (!task)
830 return ISCSI_ERR_BAD_ITT;
831 break;
832 case ISCSI_OP_R2T:
833 /*
834 * LLD handles R2Ts if they need to.
835 */
836 return 0;
837 case ISCSI_OP_LOGOUT_RSP:
838 case ISCSI_OP_LOGIN_RSP:
839 case ISCSI_OP_TEXT_RSP:
840 case ISCSI_OP_SCSI_TMFUNC_RSP:
841 case ISCSI_OP_NOOP_IN:
842 task = iscsi_itt_to_task(conn, hdr->itt);
843 if (!task)
844 return ISCSI_ERR_BAD_ITT;
845 break;
846 default:
847 return ISCSI_ERR_BAD_OPCODE;
848 }
849
850 switch(opcode) {
851 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500852 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500853 break;
854 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500855 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500856 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500857 case ISCSI_OP_LOGOUT_RSP:
858 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
859 if (datalen) {
860 rc = ISCSI_ERR_PROTO;
861 break;
862 }
863 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
864 goto recv_pdu;
865 case ISCSI_OP_LOGIN_RSP:
866 case ISCSI_OP_TEXT_RSP:
867 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
868 /*
869 * login related PDU's exp_statsn is handled in
870 * userspace
871 */
872 goto recv_pdu;
873 case ISCSI_OP_SCSI_TMFUNC_RSP:
874 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
875 if (datalen) {
876 rc = ISCSI_ERR_PROTO;
877 break;
878 }
879
880 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500881 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500882 break;
883 case ISCSI_OP_NOOP_IN:
884 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
885 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
886 rc = ISCSI_ERR_PROTO;
887 break;
888 }
889 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
890
Mike Christie9c19a7d2008-05-21 15:54:09 -0500891 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500892 /*
893 * If this is not in response to one of our
894 * nops then it must be from userspace.
895 */
896 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500897
898 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
899 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500900 break;
901 default:
902 rc = ISCSI_ERR_BAD_OPCODE;
903 break;
904 }
905
906out:
907 return rc;
908recv_pdu:
909 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
910 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500911 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500912 return rc;
913}
Mike Christie913e5bf2008-05-21 15:54:18 -0500914EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500915
916int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
917 char *data, int datalen)
918{
919 int rc;
920
921 spin_lock(&conn->session->lock);
922 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
923 spin_unlock(&conn->session->lock);
924 return rc;
925}
926EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
927
Mike Christie0af967f2008-05-21 15:54:04 -0500928int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500929{
930 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500931 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500932
Mike Christie0af967f2008-05-21 15:54:04 -0500933 if (itt == RESERVED_ITT)
934 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500935
Mike Christie0af967f2008-05-21 15:54:04 -0500936 if (((__force u32)itt & ISCSI_AGE_MASK) !=
937 (session->age << ISCSI_AGE_SHIFT)) {
938 iscsi_conn_printk(KERN_ERR, conn,
939 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -0500940 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500941 return ISCSI_ERR_BAD_ITT;
942 }
Mike Christie7996a772006-04-06 21:13:41 -0500943
Mike Christie3e5c28a2008-05-21 15:54:06 -0500944 i = get_itt(itt);
945 if (i >= session->cmds_max) {
946 iscsi_conn_printk(KERN_ERR, conn,
947 "received invalid itt index %u (max cmds "
948 "%u.\n", i, session->cmds_max);
949 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500950 }
Mike Christie7996a772006-04-06 21:13:41 -0500951 return 0;
952}
953EXPORT_SYMBOL_GPL(iscsi_verify_itt);
954
Mike Christie913e5bf2008-05-21 15:54:18 -0500955/**
956 * iscsi_itt_to_ctask - look up ctask by itt
957 * @conn: iscsi connection
958 * @itt: itt
959 *
960 * This should be used for cmd tasks.
961 *
962 * The session lock must be held.
963 */
964struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -0500965{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500966 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -0500967
968 if (iscsi_verify_itt(conn, itt))
969 return NULL;
970
Mike Christie913e5bf2008-05-21 15:54:18 -0500971 task = iscsi_itt_to_task(conn, itt);
972 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -0500973 return NULL;
974
Mike Christie913e5bf2008-05-21 15:54:18 -0500975 if (task->sc->SCp.phase != conn->session->age) {
976 iscsi_session_printk(KERN_ERR, conn->session,
977 "task's session age %d, expected %d\n",
978 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500979 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -0500980 }
Mike Christie0af967f2008-05-21 15:54:04 -0500981
Mike Christie9c19a7d2008-05-21 15:54:09 -0500982 return task;
Mike Christie0af967f2008-05-21 15:54:04 -0500983}
984EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
985
Mike Christie7996a772006-04-06 21:13:41 -0500986void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
987{
988 struct iscsi_session *session = conn->session;
989 unsigned long flags;
990
991 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500992 if (session->state == ISCSI_STATE_FAILED) {
993 spin_unlock_irqrestore(&session->lock, flags);
994 return;
995 }
996
Mike Christie67a61112006-05-30 00:37:20 -0500997 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500998 session->state = ISCSI_STATE_FAILED;
999 spin_unlock_irqrestore(&session->lock, flags);
1000 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1001 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1002 iscsi_conn_error(conn->cls_conn, err);
1003}
1004EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1005
Mike Christie77a23c22007-05-30 12:57:18 -05001006static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1007{
1008 struct iscsi_session *session = conn->session;
1009
1010 /*
1011 * Check for iSCSI window and take care of CmdSN wrap-around
1012 */
Mike Christiee0726402007-07-26 12:46:48 -05001013 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1014 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
1015 "CmdSN %u/%u\n", session->exp_cmdsn,
1016 session->max_cmdsn, session->cmdsn,
1017 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001018 return -ENOSPC;
1019 }
1020 return 0;
1021}
1022
Mike Christie9c19a7d2008-05-21 15:54:09 -05001023static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001024{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001025 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001026 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001027
Mike Christie9c19a7d2008-05-21 15:54:09 -05001028 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001029 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001030 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001031 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001032 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001033 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001034 /* done with this task */
1035 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001036 return rc;
1037}
1038
Mike Christie7996a772006-04-06 21:13:41 -05001039/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001040 * iscsi_requeue_task - requeue task to run from session workqueue
1041 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001042 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001043 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001044 * this. The session lock must be held. This should only be called
1045 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001046 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001047void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001048{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001049 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001050
Mike Christie9c19a7d2008-05-21 15:54:09 -05001051 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -06001052 scsi_queue_work(conn->session->host, &conn->xmitwork);
1053}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001054EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001055
1056/**
Mike Christie7996a772006-04-06 21:13:41 -05001057 * iscsi_data_xmit - xmit any command into the scheduled connection
1058 * @conn: iscsi connection
1059 *
1060 * Notes:
1061 * The function can return -EAGAIN in which case the caller must
1062 * re-schedule it again later or recover. '0' return code means
1063 * successful xmit.
1064 **/
1065static int iscsi_data_xmit(struct iscsi_conn *conn)
1066{
Mike Christie3219e522006-05-30 00:37:28 -05001067 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001068
Mike Christie77a23c22007-05-30 12:57:18 -05001069 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001070 if (unlikely(conn->suspend_tx)) {
1071 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001072 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001073 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001074 }
Mike Christie7996a772006-04-06 21:13:41 -05001075
Mike Christie9c19a7d2008-05-21 15:54:09 -05001076 if (conn->task) {
1077 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001078 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001079 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001080 }
1081
Mike Christie77a23c22007-05-30 12:57:18 -05001082 /*
1083 * process mgmt pdus like nops before commands since we should
1084 * only have one nop-out as a ping from us and targets should not
1085 * overflow us with nop-ins
1086 */
1087check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001088 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001089 conn->task = list_entry(conn->mgmtqueue.next,
1090 struct iscsi_task, running);
1091 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1092 __iscsi_put_task(conn->task);
1093 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001094 continue;
1095 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001096 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001097 if (rc)
1098 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001099 }
1100
Mike Christie843c0a82007-12-13 12:43:20 -06001101 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001102 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001103 if (conn->tmf_state == TMF_QUEUED)
1104 break;
1105
Mike Christie9c19a7d2008-05-21 15:54:09 -05001106 conn->task = list_entry(conn->xmitqueue.next,
1107 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001108 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001109 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001110 continue;
1111 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001112 if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
1113 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001114 continue;
1115 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001116 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001117 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001118 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001119 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001120 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001121 * we need to check the mgmt queue for nops that need to
1122 * be sent to aviod starvation
1123 */
Mike Christie843c0a82007-12-13 12:43:20 -06001124 if (!list_empty(&conn->mgmtqueue))
1125 goto check_mgmt;
1126 }
1127
1128 while (!list_empty(&conn->requeue)) {
1129 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1130 break;
1131
Mike Christieb3a7ea82007-12-13 12:43:26 -06001132 /*
1133 * we always do fastlogout - conn stop code will clean up.
1134 */
1135 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1136 break;
1137
Mike Christie9c19a7d2008-05-21 15:54:09 -05001138 conn->task = list_entry(conn->requeue.next,
1139 struct iscsi_task, running);
1140 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001141 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001142 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001143 if (rc)
1144 goto again;
1145 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001146 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001147 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001148 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001149 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001150
1151again:
1152 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001153 rc = -ENODATA;
1154 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001155 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001156}
1157
David Howellsc4028952006-11-22 14:57:56 +00001158static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001159{
David Howellsc4028952006-11-22 14:57:56 +00001160 struct iscsi_conn *conn =
1161 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001162 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001163 /*
1164 * serialize Xmit worker on a per-connection basis.
1165 */
Mike Christie3219e522006-05-30 00:37:28 -05001166 do {
1167 rc = iscsi_data_xmit(conn);
1168 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001169}
1170
1171enum {
1172 FAILURE_BAD_HOST = 1,
1173 FAILURE_SESSION_FAILED,
1174 FAILURE_SESSION_FREED,
1175 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001176 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001177 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001178 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001179 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001180 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001181 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001182};
1183
1184int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1185{
Mike Christie75613522008-05-21 15:53:59 -05001186 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001187 struct Scsi_Host *host;
1188 int reason = 0;
1189 struct iscsi_session *session;
1190 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001191 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001192
1193 sc->scsi_done = done;
1194 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001195 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001196
1197 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001198 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001199
Mike Christie75613522008-05-21 15:53:59 -05001200 cls_session = starget_to_session(scsi_target(sc->device));
1201 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001202 spin_lock(&session->lock);
1203
Mike Christie75613522008-05-21 15:53:59 -05001204 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001205 if (reason) {
1206 sc->result = reason;
1207 goto fault;
1208 }
1209
Mike Christie656cffc2006-05-18 20:31:42 -05001210 /*
1211 * ISCSI_STATE_FAILED is a temp. state. The recovery
1212 * code will decide what is best to do with command queued
1213 * during this time
1214 */
1215 if (session->state != ISCSI_STATE_LOGGED_IN &&
1216 session->state != ISCSI_STATE_FAILED) {
1217 /*
1218 * to handle the race between when we set the recovery state
1219 * and block the session we requeue here (commands could
1220 * be entering our queuecommand while a block is starting
1221 * up because the block code is not locked)
1222 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001223 switch (session->state) {
1224 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001225 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christied6d13ee2008-08-17 15:24:43 -05001226 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001227 case ISCSI_STATE_LOGGING_OUT:
1228 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christied6d13ee2008-08-17 15:24:43 -05001229 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001230 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001231 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001232 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001233 break;
1234 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001235 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001236 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001237 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001238 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001239 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001240 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001241 }
Mike Christie7996a772006-04-06 21:13:41 -05001242 goto fault;
1243 }
1244
Mike Christie7996a772006-04-06 21:13:41 -05001245 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001246 if (!conn) {
1247 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001248 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001249 goto fault;
1250 }
Mike Christie7996a772006-04-06 21:13:41 -05001251
Mike Christie77a23c22007-05-30 12:57:18 -05001252 if (iscsi_check_cmdsn_window_closed(conn)) {
1253 reason = FAILURE_WINDOW_CLOSED;
1254 goto reject;
1255 }
1256
Mike Christie9c19a7d2008-05-21 15:54:09 -05001257 if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
Mike Christie60ecebf2006-08-31 18:09:25 -04001258 sizeof(void*))) {
1259 reason = FAILURE_OOM;
1260 goto reject;
1261 }
Mike Christie7996a772006-04-06 21:13:41 -05001262 sc->SCp.phase = session->age;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001263 sc->SCp.ptr = (char *)task;
Mike Christie7996a772006-04-06 21:13:41 -05001264
Mike Christie9c19a7d2008-05-21 15:54:09 -05001265 atomic_set(&task->refcount, 1);
1266 task->state = ISCSI_TASK_PENDING;
1267 task->conn = conn;
1268 task->sc = sc;
1269 INIT_LIST_HEAD(&task->running);
1270 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001271
Mike Christie052d0142008-05-21 15:54:05 -05001272 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001273 if (iscsi_prep_scsi_cmd_pdu(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001274 sc->result = DID_ABORT << 16;
1275 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001276 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001277 goto fault;
1278 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001279 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001280 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001281 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001282 reason = FAILURE_SESSION_NOT_READY;
1283 goto reject;
1284 }
1285 } else
1286 scsi_queue_work(session->host, &conn->xmitwork);
1287
1288 session->queued_cmdsn++;
1289 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001290 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001291 return 0;
1292
1293reject:
1294 spin_unlock(&session->lock);
1295 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001296 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001297 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001298
1299fault:
1300 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001301 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001302 if (!scsi_bidi_cmnd(sc))
1303 scsi_set_resid(sc, scsi_bufflen(sc));
1304 else {
1305 scsi_out(sc)->resid = scsi_out(sc)->length;
1306 scsi_in(sc)->resid = scsi_in(sc)->length;
1307 }
Mike Christie052d0142008-05-21 15:54:05 -05001308 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001309 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001310 return 0;
1311}
1312EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1313
1314int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1315{
1316 if (depth > ISCSI_MAX_CMD_PER_LUN)
1317 depth = ISCSI_MAX_CMD_PER_LUN;
1318 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1319 return sdev->queue_depth;
1320}
1321EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1322
Mike Christie7996a772006-04-06 21:13:41 -05001323void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1324{
Mike Christie75613522008-05-21 15:53:59 -05001325 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001326
1327 spin_lock_bh(&session->lock);
1328 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001329 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001330 if (session->leadconn)
1331 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001332 }
1333 spin_unlock_bh(&session->lock);
1334}
1335EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1336
1337int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1338{
Mike Christie75613522008-05-21 15:53:59 -05001339 struct iscsi_cls_session *cls_session;
1340 struct iscsi_session *session;
1341 struct iscsi_conn *conn;
1342
1343 cls_session = starget_to_session(scsi_target(sc->device));
1344 session = cls_session->dd_data;
1345 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001346
Mike Christiebc436b22007-12-13 12:43:28 -06001347 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001348 spin_lock_bh(&session->lock);
1349 if (session->state == ISCSI_STATE_TERMINATE) {
1350failed:
1351 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001352 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001353 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001354 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001355 return FAILED;
1356 }
1357
Mike Christie7996a772006-04-06 21:13:41 -05001358 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001359 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001360 /*
1361 * we drop the lock here but the leadconn cannot be destoyed while
1362 * we are in the scsi eh
1363 */
Mike Christie843c0a82007-12-13 12:43:20 -06001364 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001365
1366 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1367 wait_event_interruptible(conn->ehwait,
1368 session->state == ISCSI_STATE_TERMINATE ||
1369 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001370 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001371 if (signal_pending(current))
1372 flush_signals(current);
1373
Mike Christiebc436b22007-12-13 12:43:28 -06001374 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001375 spin_lock_bh(&session->lock);
1376 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001377 iscsi_session_printk(KERN_INFO, session,
1378 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001379 else
1380 goto failed;
1381 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001382 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001383 return SUCCESS;
1384}
1385EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1386
Mike Christie843c0a82007-12-13 12:43:20 -06001387static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001388{
Mike Christie843c0a82007-12-13 12:43:20 -06001389 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001390 struct iscsi_session *session = conn->session;
1391
1392 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001393 if (conn->tmf_state == TMF_QUEUED) {
1394 conn->tmf_state = TMF_TIMEDOUT;
1395 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001396 /* unblock eh_abort() */
1397 wake_up(&conn->ehwait);
1398 }
1399 spin_unlock(&session->lock);
1400}
1401
Mike Christie9c19a7d2008-05-21 15:54:09 -05001402static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001403 struct iscsi_tm *hdr, int age,
1404 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001405{
Mike Christie7996a772006-04-06 21:13:41 -05001406 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001407 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001408
Mike Christie9c19a7d2008-05-21 15:54:09 -05001409 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001410 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001411 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001412 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001413 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001414 spin_lock_bh(&session->lock);
1415 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001416 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001417 }
Mike Christie843c0a82007-12-13 12:43:20 -06001418 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001419 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001420 conn->tmf_timer.function = iscsi_tmf_timedout;
1421 conn->tmf_timer.data = (unsigned long)conn;
1422 add_timer(&conn->tmf_timer);
1423 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001424
Mike Christie7996a772006-04-06 21:13:41 -05001425 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001426 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001427
1428 /*
1429 * block eh thread until:
1430 *
Mike Christie843c0a82007-12-13 12:43:20 -06001431 * 1) tmf response
1432 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001433 * 3) session is terminated or restarted or userspace has
1434 * given up on recovery
1435 */
Mike Christie843c0a82007-12-13 12:43:20 -06001436 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001437 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001438 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001439 if (signal_pending(current))
1440 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001441 del_timer_sync(&conn->tmf_timer);
1442
Mike Christie6724add2007-08-15 01:38:30 -05001443 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001444 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001445 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001446 if (age != session->age ||
1447 session->state != ISCSI_STATE_LOGGED_IN)
1448 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001449 return 0;
1450}
1451
1452/*
Mike Christie843c0a82007-12-13 12:43:20 -06001453 * Fail commands. session lock held and recv side suspended and xmit
1454 * thread flushed
1455 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001456static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1457 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001458{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001459 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001460
Mike Christie9c19a7d2008-05-21 15:54:09 -05001461 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1462 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001463
1464 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001465 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1466 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001467 debug_scsi("failing pending sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001468 task->sc, task->itt);
1469 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001470 }
1471 }
1472
Mike Christie9c19a7d2008-05-21 15:54:09 -05001473 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1474 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001475 debug_scsi("failing requeued sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001476 task->sc, task->itt);
1477 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001478 }
1479 }
1480
1481 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001482 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1483 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001484 debug_scsi("failing in progress sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001485 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001486 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001487 }
1488 }
1489}
1490
Mike Christieb40977d2008-05-21 15:54:03 -05001491void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001492{
1493 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001494 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1495 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001496}
Mike Christieb40977d2008-05-21 15:54:03 -05001497EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001498
1499static void iscsi_start_tx(struct iscsi_conn *conn)
1500{
1501 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001502 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1503 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001504}
1505
Jens Axboe242f9dc2008-09-14 05:55:09 -07001506static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001507{
1508 struct iscsi_cls_session *cls_session;
1509 struct iscsi_session *session;
1510 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001511 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001512
1513 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001514 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001515
1516 debug_scsi("scsi cmd %p timedout\n", scmd);
1517
1518 spin_lock(&session->lock);
1519 if (session->state != ISCSI_STATE_LOGGED_IN) {
1520 /*
1521 * We are probably in the middle of iscsi recovery so let
1522 * that complete and handle the error.
1523 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001524 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001525 goto done;
1526 }
1527
1528 conn = session->leadconn;
1529 if (!conn) {
1530 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001531 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001532 goto done;
1533 }
1534
1535 if (!conn->recv_timeout && !conn->ping_timeout)
1536 goto done;
1537 /*
1538 * if the ping timedout then we are in the middle of cleaning up
1539 * and can let the iscsi eh handle it
1540 */
1541 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1542 (conn->ping_timeout * HZ), jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001543 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001544 /*
1545 * if we are about to check the transport then give the command
1546 * more time
1547 */
1548 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1549 jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001550 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001551 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001552 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001553 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001554done:
1555 spin_unlock(&session->lock);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001556 debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
1557 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001558 return rc;
1559}
1560
1561static void iscsi_check_transport_timeouts(unsigned long data)
1562{
1563 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1564 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001565 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001566
1567 spin_lock(&session->lock);
1568 if (session->state != ISCSI_STATE_LOGGED_IN)
1569 goto done;
1570
Mike Christie4cf10432008-05-07 20:43:52 -05001571 recv_timeout = conn->recv_timeout;
1572 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001573 goto done;
1574
Mike Christie4cf10432008-05-07 20:43:52 -05001575 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001576 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001577 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001578 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001579 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001580 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1581 "expired, last rx %lu, last ping %lu, "
1582 "now %lu\n", conn->ping_timeout, last_recv,
1583 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001584 spin_unlock(&session->lock);
1585 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1586 return;
1587 }
1588
Mike Christie4cf10432008-05-07 20:43:52 -05001589 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001590 /* send a ping to try to provoke some traffic */
1591 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1592 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001593 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001594 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001595 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001596
Mike Christiead294e92008-01-31 13:36:50 -06001597 debug_scsi("Setting next tmo %lu\n", next_timeout);
1598 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001599done:
1600 spin_unlock(&session->lock);
1601}
1602
Mike Christie9c19a7d2008-05-21 15:54:09 -05001603static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001604 struct iscsi_tm *hdr)
1605{
1606 memset(hdr, 0, sizeof(*hdr));
1607 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1608 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1609 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001610 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1611 hdr->rtt = task->hdr->itt;
1612 hdr->refcmdsn = task->hdr->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001613}
1614
Mike Christie7996a772006-04-06 21:13:41 -05001615int iscsi_eh_abort(struct scsi_cmnd *sc)
1616{
Mike Christie75613522008-05-21 15:53:59 -05001617 struct iscsi_cls_session *cls_session;
1618 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001619 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001620 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001621 struct iscsi_tm *hdr;
1622 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001623
Mike Christie75613522008-05-21 15:53:59 -05001624 cls_session = starget_to_session(scsi_target(sc->device));
1625 session = cls_session->dd_data;
1626
Mike Christie6724add2007-08-15 01:38:30 -05001627 mutex_lock(&session->eh_mutex);
1628 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001629 /*
1630 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1631 * got the command.
1632 */
1633 if (!sc->SCp.ptr) {
1634 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001635 spin_unlock_bh(&session->lock);
1636 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001637 return SUCCESS;
1638 }
1639
Mike Christie7996a772006-04-06 21:13:41 -05001640 /*
1641 * If we are not logged in or we have started a new session
1642 * then let the host reset code handle this
1643 */
Mike Christie843c0a82007-12-13 12:43:20 -06001644 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1645 sc->SCp.phase != session->age) {
1646 spin_unlock_bh(&session->lock);
1647 mutex_unlock(&session->eh_mutex);
1648 return FAILED;
1649 }
1650
1651 conn = session->leadconn;
1652 conn->eh_abort_cnt++;
1653 age = session->age;
1654
Mike Christie9c19a7d2008-05-21 15:54:09 -05001655 task = (struct iscsi_task *)sc->SCp.ptr;
1656 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001657
Mike Christie9c19a7d2008-05-21 15:54:09 -05001658 /* task completed before time out */
1659 if (!task->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001660 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001661 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001662 }
Mike Christie7996a772006-04-06 21:13:41 -05001663
Mike Christie9c19a7d2008-05-21 15:54:09 -05001664 if (task->state == ISCSI_TASK_PENDING) {
1665 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001666 goto success;
1667 }
Mike Christie7996a772006-04-06 21:13:41 -05001668
Mike Christie843c0a82007-12-13 12:43:20 -06001669 /* only have one tmf outstanding at a time */
1670 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001671 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001672 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001673
Mike Christie843c0a82007-12-13 12:43:20 -06001674 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001675 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001676
Mike Christie9c19a7d2008-05-21 15:54:09 -05001677 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001678 rc = FAILED;
1679 goto failed;
1680 }
1681
1682 switch (conn->tmf_state) {
1683 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001684 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001685 /*
1686 * stop tx side incase the target had sent a abort rsp but
1687 * the initiator was still writing out data.
1688 */
Mike Christie6724add2007-08-15 01:38:30 -05001689 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001690 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001691 * we do not stop the recv side because targets have been
1692 * good and have never sent us a successful tmf response
1693 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001694 */
Mike Christie77a23c22007-05-30 12:57:18 -05001695 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001696 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001697 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001698 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001699 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001700 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001701 case TMF_TIMEDOUT:
1702 spin_unlock_bh(&session->lock);
1703 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1704 goto failed_unlocked;
1705 case TMF_NOT_FOUND:
1706 if (!sc->SCp.ptr) {
1707 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001708 /* task completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001709 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001710 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001711 }
1712 /* fall through */
1713 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001714 conn->tmf_state = TMF_INITIAL;
1715 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001716 }
1717
Mike Christie77a23c22007-05-30 12:57:18 -05001718success:
Mike Christie7996a772006-04-06 21:13:41 -05001719 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001720success_unlocked:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001721 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001722 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001723 return SUCCESS;
1724
1725failed:
1726 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001727failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001728 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001729 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001730 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001731 return FAILED;
1732}
1733EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1734
Mike Christie843c0a82007-12-13 12:43:20 -06001735static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1736{
1737 memset(hdr, 0, sizeof(*hdr));
1738 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1739 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1740 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1741 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001742 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001743}
1744
1745int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1746{
Mike Christie75613522008-05-21 15:53:59 -05001747 struct iscsi_cls_session *cls_session;
1748 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001749 struct iscsi_conn *conn;
1750 struct iscsi_tm *hdr;
1751 int rc = FAILED;
1752
Mike Christie75613522008-05-21 15:53:59 -05001753 cls_session = starget_to_session(scsi_target(sc->device));
1754 session = cls_session->dd_data;
1755
Mike Christie843c0a82007-12-13 12:43:20 -06001756 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1757
1758 mutex_lock(&session->eh_mutex);
1759 spin_lock_bh(&session->lock);
1760 /*
1761 * Just check if we are not logged in. We cannot check for
1762 * the phase because the reset could come from a ioctl.
1763 */
1764 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1765 goto unlock;
1766 conn = session->leadconn;
1767
1768 /* only have one tmf outstanding at a time */
1769 if (conn->tmf_state != TMF_INITIAL)
1770 goto unlock;
1771 conn->tmf_state = TMF_QUEUED;
1772
1773 hdr = &conn->tmhdr;
1774 iscsi_prep_lun_reset_pdu(sc, hdr);
1775
Mike Christie9c19a7d2008-05-21 15:54:09 -05001776 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001777 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001778 rc = FAILED;
1779 goto unlock;
1780 }
1781
1782 switch (conn->tmf_state) {
1783 case TMF_SUCCESS:
1784 break;
1785 case TMF_TIMEDOUT:
1786 spin_unlock_bh(&session->lock);
1787 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1788 goto done;
1789 default:
1790 conn->tmf_state = TMF_INITIAL;
1791 goto unlock;
1792 }
1793
1794 rc = SUCCESS;
1795 spin_unlock_bh(&session->lock);
1796
1797 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001798
Mike Christie843c0a82007-12-13 12:43:20 -06001799 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001800 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001801 conn->tmf_state = TMF_INITIAL;
1802 spin_unlock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001803
1804 iscsi_start_tx(conn);
1805 goto done;
1806
1807unlock:
1808 spin_unlock_bh(&session->lock);
1809done:
1810 debug_scsi("iscsi_eh_device_reset %s\n",
1811 rc == SUCCESS ? "SUCCESS" : "FAILED");
1812 mutex_unlock(&session->eh_mutex);
1813 return rc;
1814}
1815EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1816
Olaf Kirch63203772007-12-13 12:43:25 -06001817/*
1818 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1819 * should be accessed via kfifo_{get,put} on q->queue.
1820 * Optionally, the caller can obtain the array of object pointers
1821 * by passing in a non-NULL @items pointer
1822 */
Mike Christie7996a772006-04-06 21:13:41 -05001823int
Olaf Kirch63203772007-12-13 12:43:25 -06001824iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001825{
Olaf Kirch63203772007-12-13 12:43:25 -06001826 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001827
Olaf Kirch63203772007-12-13 12:43:25 -06001828 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001829
1830 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001831
1832 /* If the user passed an items pointer, he wants a copy of
1833 * the array. */
1834 if (items)
1835 num_arrays++;
1836 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1837 if (q->pool == NULL)
1838 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001839
1840 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1841 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001842 if (q->queue == ERR_PTR(-ENOMEM))
1843 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001844
1845 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001846 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001847 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001848 q->max = i;
1849 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001850 }
Mike Christie7996a772006-04-06 21:13:41 -05001851 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1852 }
Olaf Kirch63203772007-12-13 12:43:25 -06001853
1854 if (items) {
1855 *items = q->pool + max;
1856 memcpy(*items, q->pool, max * sizeof(void *));
1857 }
1858
Mike Christie7996a772006-04-06 21:13:41 -05001859 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001860
1861enomem:
1862 iscsi_pool_free(q);
1863 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001864}
1865EXPORT_SYMBOL_GPL(iscsi_pool_init);
1866
Olaf Kirch63203772007-12-13 12:43:25 -06001867void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001868{
1869 int i;
1870
1871 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001872 kfree(q->pool[i]);
1873 if (q->pool)
1874 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001875}
1876EXPORT_SYMBOL_GPL(iscsi_pool_free);
1877
Mike Christiea4804cd2008-05-21 15:54:00 -05001878/**
1879 * iscsi_host_add - add host to system
1880 * @shost: scsi host
1881 * @pdev: parent device
1882 *
1883 * This should be called by partial offload and software iscsi drivers
1884 * to add a host to the system.
1885 */
1886int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001887{
Mike Christie8e9a20c2008-06-16 10:11:33 -05001888 if (!shost->can_queue)
1889 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
1890
Mike Christiea4804cd2008-05-21 15:54:00 -05001891 return scsi_add_host(shost, pdev);
1892}
1893EXPORT_SYMBOL_GPL(iscsi_host_add);
1894
1895/**
1896 * iscsi_host_alloc - allocate a host and driver data
1897 * @sht: scsi host template
1898 * @dd_data_size: driver host data size
1899 * @qdepth: default device queue depth
1900 *
1901 * This should be called by partial offload and software iscsi drivers.
1902 * To access the driver specific memory use the iscsi_host_priv() macro.
1903 */
1904struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1905 int dd_data_size, uint16_t qdepth)
1906{
1907 struct Scsi_Host *shost;
1908
1909 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1910 if (!shost)
1911 return NULL;
1912 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1913
Mike Christie15482712007-05-30 12:57:19 -05001914 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1915 if (qdepth != 0)
1916 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001917 "Queue depth must be between 1 and %d.\n",
1918 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001919 qdepth = ISCSI_DEF_CMD_PER_LUN;
1920 }
Mike Christie75613522008-05-21 15:53:59 -05001921 shost->cmd_per_lun = qdepth;
Mike Christiea4804cd2008-05-21 15:54:00 -05001922 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001923}
Mike Christiea4804cd2008-05-21 15:54:00 -05001924EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001925
Mike Christiea4804cd2008-05-21 15:54:00 -05001926/**
1927 * iscsi_host_remove - remove host and sessions
1928 * @shost: scsi host
1929 *
1930 * This will also remove any sessions attached to the host, but if userspace
1931 * is managing the session at the same time this will break. TODO: add
1932 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1933 * does not remove the memory from under us.
1934 */
1935void iscsi_host_remove(struct Scsi_Host *shost)
1936{
1937 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1938 scsi_remove_host(shost);
1939}
1940EXPORT_SYMBOL_GPL(iscsi_host_remove);
1941
1942void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001943{
1944 struct iscsi_host *ihost = shost_priv(shost);
1945
1946 kfree(ihost->netdev);
1947 kfree(ihost->hwaddress);
1948 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001949 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001950}
Mike Christiea4804cd2008-05-21 15:54:00 -05001951EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05001952
1953/**
1954 * iscsi_session_setup - create iscsi cls session and host and session
1955 * @iscsit: iscsi transport template
1956 * @shost: scsi host
1957 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05001958 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05001959 * @initial_cmdsn: initial CmdSN
1960 *
1961 * This can be used by software iscsi_transports that allocate
1962 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05001963 *
1964 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
1965 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
1966 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05001967 */
1968struct iscsi_cls_session *
1969iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05001970 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05001971 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05001972{
1973 struct iscsi_session *session;
1974 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05001975 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christie8e9a20c2008-06-16 10:11:33 -05001976
1977 if (!total_cmds)
1978 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001979 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05001980 * The iscsi layer needs some tasks for nop handling and tmfs,
1981 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
1982 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05001983 */
Mike Christie3cf7b232008-05-21 15:54:17 -05001984 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
1985 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1986 "must be a power of two that is at least %d.\n",
1987 total_cmds, ISCSI_TOTAL_CMDS_MIN);
1988 return NULL;
Mike Christie15482712007-05-30 12:57:19 -05001989 }
Mike Christie3cf7b232008-05-21 15:54:17 -05001990
1991 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
1992 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1993 "must be a power of 2 less than or equal to %d.\n",
1994 cmds_max, ISCSI_TOTAL_CMDS_MAX);
1995 total_cmds = ISCSI_TOTAL_CMDS_MAX;
1996 }
1997
1998 if (!is_power_of_2(total_cmds)) {
1999 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2000 "must be a power of 2.\n", total_cmds);
2001 total_cmds = rounddown_pow_of_two(total_cmds);
2002 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2003 return NULL;
2004 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2005 total_cmds);
2006 }
2007 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002008
Mike Christie5d91e202008-05-21 15:54:01 -05002009 cls_session = iscsi_alloc_session(shost, iscsit,
2010 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002011 if (!cls_session)
Mike Christie7996a772006-04-06 21:13:41 -05002012 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002013 session = cls_session->dd_data;
2014 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002015 session->host = shost;
2016 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002017 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002018 session->lu_reset_timeout = 15;
2019 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002020 session->scsi_cmds_max = scsi_cmds;
2021 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002022 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002023 session->exp_cmdsn = initial_cmdsn + 1;
2024 session->max_cmdsn = initial_cmdsn + 1;
2025 session->max_r2t = 1;
2026 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002027 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002028 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002029
2030 /* initialize SCSI PDU commands pool */
2031 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2032 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002033 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002034 goto cmdpool_alloc_fail;
2035
2036 /* pre-format cmds pool with ITT */
2037 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002038 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002039
Mike Christie9c19a7d2008-05-21 15:54:09 -05002040 if (cmd_task_size)
2041 task->dd_data = &task[1];
2042 task->itt = cmd_i;
2043 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002044 }
2045
Mike Christief53a88d2006-06-28 12:00:27 -05002046 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002047 goto module_get_fail;
2048
Mike Christie79706342008-05-21 15:54:12 -05002049 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002050 goto cls_session_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002051 return cls_session;
2052
2053cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002054 module_put(iscsit->owner);
2055module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002056 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002057cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002058 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05002059 return NULL;
2060}
2061EXPORT_SYMBOL_GPL(iscsi_session_setup);
2062
2063/**
2064 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002065 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002066 *
Mike Christie75613522008-05-21 15:53:59 -05002067 * The driver must have called iscsi_remove_session before
2068 * calling this.
2069 */
Mike Christie7996a772006-04-06 21:13:41 -05002070void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2071{
Mike Christie75613522008-05-21 15:53:59 -05002072 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002073 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05002074
Olaf Kirch63203772007-12-13 12:43:25 -06002075 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002076
Mike Christieb2c64162007-05-30 12:57:16 -05002077 kfree(session->password);
2078 kfree(session->password_in);
2079 kfree(session->username);
2080 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002081 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002082 kfree(session->initiatorname);
2083 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002084
Mike Christie75613522008-05-21 15:53:59 -05002085 iscsi_destroy_session(cls_session);
Mike Christie63f75cc2006-07-24 15:47:29 -05002086 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002087}
2088EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2089
2090/**
2091 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2092 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002093 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002094 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002095 */
Mike Christie7996a772006-04-06 21:13:41 -05002096struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002097iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2098 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002099{
Mike Christie75613522008-05-21 15:53:59 -05002100 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002101 struct iscsi_conn *conn;
2102 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002103 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002104
Mike Christie5d91e202008-05-21 15:54:01 -05002105 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2106 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002107 if (!cls_conn)
2108 return NULL;
2109 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002110 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002111
Mike Christie5d91e202008-05-21 15:54:01 -05002112 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002113 conn->session = session;
2114 conn->cls_conn = cls_conn;
2115 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2116 conn->id = conn_idx;
2117 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002118 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002119
2120 init_timer(&conn->transport_timer);
2121 conn->transport_timer.data = (unsigned long)conn;
2122 conn->transport_timer.function = iscsi_check_transport_timeouts;
2123
Mike Christie7996a772006-04-06 21:13:41 -05002124 INIT_LIST_HEAD(&conn->run_list);
2125 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002126 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002127 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002128 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002129 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002130
Mike Christie9c19a7d2008-05-21 15:54:09 -05002131 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002132 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002133 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002134 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002135 sizeof(void*))) {
2136 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002137 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002138 }
2139 spin_unlock_bh(&session->lock);
2140
Mike Christiebf32ed32007-02-28 17:32:17 -06002141 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002142 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002143 goto login_task_data_alloc_fail;
2144 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002145
Mike Christie843c0a82007-12-13 12:43:20 -06002146 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002147 init_waitqueue_head(&conn->ehwait);
2148
2149 return cls_conn;
2150
Mike Christie9c19a7d2008-05-21 15:54:09 -05002151login_task_data_alloc_fail:
2152 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002153 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002154login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002155 iscsi_destroy_conn(cls_conn);
2156 return NULL;
2157}
2158EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2159
2160/**
2161 * iscsi_conn_teardown - teardown iscsi connection
2162 * cls_conn: iscsi class connection
2163 *
2164 * TODO: we may need to make this into a two step process
2165 * like scsi-mls remove + put host
2166 */
2167void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2168{
2169 struct iscsi_conn *conn = cls_conn->dd_data;
2170 struct iscsi_session *session = conn->session;
2171 unsigned long flags;
2172
Mike Christief6d51802007-12-13 12:43:30 -06002173 del_timer_sync(&conn->transport_timer);
2174
Mike Christie7996a772006-04-06 21:13:41 -05002175 spin_lock_bh(&session->lock);
2176 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2177 if (session->leadconn == conn) {
2178 /*
2179 * leading connection? then give up on recovery.
2180 */
2181 session->state = ISCSI_STATE_TERMINATE;
2182 wake_up(&conn->ehwait);
2183 }
2184 spin_unlock_bh(&session->lock);
2185
Mike Christie7996a772006-04-06 21:13:41 -05002186 /*
2187 * Block until all in-progress commands for this connection
2188 * time out or fail.
2189 */
2190 for (;;) {
2191 spin_lock_irqsave(session->host->host_lock, flags);
2192 if (!session->host->host_busy) { /* OK for ERL == 0 */
2193 spin_unlock_irqrestore(session->host->host_lock, flags);
2194 break;
2195 }
2196 spin_unlock_irqrestore(session->host->host_lock, flags);
2197 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002198 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2199 "host_busy %d host_failed %d\n",
2200 session->host->host_busy,
2201 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002202 /*
2203 * force eh_abort() to unblock
2204 */
2205 wake_up(&conn->ehwait);
2206 }
2207
Mike Christie779ea122007-02-28 17:32:15 -06002208 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002209 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002210
Mike Christie7996a772006-04-06 21:13:41 -05002211 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002212 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002213 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002214 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002215 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002216 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002217 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002218 spin_unlock_bh(&session->lock);
2219
Mike Christie7996a772006-04-06 21:13:41 -05002220 iscsi_destroy_conn(cls_conn);
2221}
2222EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2223
2224int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2225{
2226 struct iscsi_conn *conn = cls_conn->dd_data;
2227 struct iscsi_session *session = conn->session;
2228
Mike Christieffd04362006-08-31 18:09:24 -04002229 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002230 iscsi_conn_printk(KERN_ERR, conn,
2231 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002232 return -EPERM;
2233 }
2234
Mike Christiedb98ccd2006-08-31 18:09:31 -04002235 if ((session->imm_data_en || !session->initial_r2t_en) &&
2236 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002237 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2238 "first_burst %d max_burst %d\n",
2239 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002240 return -EINVAL;
2241 }
2242
Mike Christief6d51802007-12-13 12:43:30 -06002243 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002244 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2245 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002246 conn->recv_timeout = 5;
2247 }
2248
2249 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002250 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2251 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002252 conn->ping_timeout = 5;
2253 }
2254
Mike Christie7996a772006-04-06 21:13:41 -05002255 spin_lock_bh(&session->lock);
2256 conn->c_stage = ISCSI_CONN_STARTED;
2257 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002258 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002259
Mike Christief6d51802007-12-13 12:43:30 -06002260 conn->last_recv = jiffies;
2261 conn->last_ping = jiffies;
2262 if (conn->recv_timeout && conn->ping_timeout)
2263 mod_timer(&conn->transport_timer,
2264 jiffies + (conn->recv_timeout * HZ));
2265
Mike Christie7996a772006-04-06 21:13:41 -05002266 switch(conn->stop_stage) {
2267 case STOP_CONN_RECOVER:
2268 /*
2269 * unblock eh_abort() if it is blocked. re-try all
2270 * commands after successful recovery
2271 */
Mike Christie7996a772006-04-06 21:13:41 -05002272 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002273 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002274 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002275 if (session->age == 16)
2276 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002277 break;
Mike Christie7996a772006-04-06 21:13:41 -05002278 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002279 conn->stop_stage = 0;
2280 break;
Mike Christie7996a772006-04-06 21:13:41 -05002281 default:
2282 break;
2283 }
2284 spin_unlock_bh(&session->lock);
2285
Mike Christie75613522008-05-21 15:53:59 -05002286 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002287 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002288 return 0;
2289}
2290EXPORT_SYMBOL_GPL(iscsi_conn_start);
2291
2292static void
2293flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2294{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002295 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002296
2297 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002298 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2299 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2300 /* release ref from prep task */
2301 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002302 }
2303
2304 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002305 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2306 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2307 /* release ref from prep task */
2308 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002309 }
2310
Mike Christie9c19a7d2008-05-21 15:54:09 -05002311 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002312}
2313
Mike Christie656cffc2006-05-18 20:31:42 -05002314static void iscsi_start_session_recovery(struct iscsi_session *session,
2315 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002316{
Mike Christieed2abc72006-05-02 19:46:40 -05002317 int old_stop_stage;
2318
Mike Christief6d51802007-12-13 12:43:30 -06002319 del_timer_sync(&conn->transport_timer);
2320
Mike Christie6724add2007-08-15 01:38:30 -05002321 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002322 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002323 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002324 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002325 mutex_unlock(&session->eh_mutex);
2326 return;
2327 }
2328
2329 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002330 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002331 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002332 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002333 */
Mike Christie67a61112006-05-30 00:37:20 -05002334 if (flag == STOP_CONN_TERM)
2335 session->state = ISCSI_STATE_TERMINATE;
2336 else if (conn->stop_stage != STOP_CONN_RECOVER)
2337 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002338
2339 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002340 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002341 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002342 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002343
2344 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002345 /*
2346 * for connection level recovery we should not calculate
2347 * header digest. conn->hdr_size used for optimization
2348 * in hdr_extract() and will be re-negotiated at
2349 * set_param() time.
2350 */
2351 if (flag == STOP_CONN_RECOVER) {
2352 conn->hdrdgst_en = 0;
2353 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002354 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002355 old_stop_stage != STOP_CONN_RECOVER) {
2356 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002357 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002358 }
Mike Christie7996a772006-04-06 21:13:41 -05002359 }
Mike Christie656cffc2006-05-18 20:31:42 -05002360
Mike Christie656cffc2006-05-18 20:31:42 -05002361 /*
2362 * flush queues.
2363 */
2364 spin_lock_bh(&session->lock);
Mike Christie56d7fcf2008-08-19 18:45:26 -05002365 if (STOP_CONN_RECOVER)
2366 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2367 else
2368 fail_all_commands(conn, -1, DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002369 flush_control_queues(session, conn);
2370 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002371 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002372}
Mike Christie7996a772006-04-06 21:13:41 -05002373
2374void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2375{
2376 struct iscsi_conn *conn = cls_conn->dd_data;
2377 struct iscsi_session *session = conn->session;
2378
2379 switch (flag) {
2380 case STOP_CONN_RECOVER:
2381 case STOP_CONN_TERM:
2382 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002383 break;
Mike Christie7996a772006-04-06 21:13:41 -05002384 default:
Mike Christie322d7392008-01-31 13:36:52 -06002385 iscsi_conn_printk(KERN_ERR, conn,
2386 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002387 }
2388}
2389EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2390
2391int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2392 struct iscsi_cls_conn *cls_conn, int is_leading)
2393{
Mike Christie75613522008-05-21 15:53:59 -05002394 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002395 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002396
Mike Christie7996a772006-04-06 21:13:41 -05002397 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002398 if (is_leading)
2399 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002400 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002401
2402 /*
2403 * Unblock xmitworker(), Login Phase will pass through.
2404 */
2405 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2406 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2407 return 0;
2408}
2409EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2410
Mike Christiea54a52c2006-06-28 12:00:23 -05002411
2412int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2413 enum iscsi_param param, char *buf, int buflen)
2414{
2415 struct iscsi_conn *conn = cls_conn->dd_data;
2416 struct iscsi_session *session = conn->session;
2417 uint32_t value;
2418
2419 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002420 case ISCSI_PARAM_FAST_ABORT:
2421 sscanf(buf, "%d", &session->fast_abort);
2422 break;
Mike Christief6d51802007-12-13 12:43:30 -06002423 case ISCSI_PARAM_ABORT_TMO:
2424 sscanf(buf, "%d", &session->abort_timeout);
2425 break;
2426 case ISCSI_PARAM_LU_RESET_TMO:
2427 sscanf(buf, "%d", &session->lu_reset_timeout);
2428 break;
2429 case ISCSI_PARAM_PING_TMO:
2430 sscanf(buf, "%d", &conn->ping_timeout);
2431 break;
2432 case ISCSI_PARAM_RECV_TMO:
2433 sscanf(buf, "%d", &conn->recv_timeout);
2434 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002435 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2436 sscanf(buf, "%d", &conn->max_recv_dlength);
2437 break;
2438 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2439 sscanf(buf, "%d", &conn->max_xmit_dlength);
2440 break;
2441 case ISCSI_PARAM_HDRDGST_EN:
2442 sscanf(buf, "%d", &conn->hdrdgst_en);
2443 break;
2444 case ISCSI_PARAM_DATADGST_EN:
2445 sscanf(buf, "%d", &conn->datadgst_en);
2446 break;
2447 case ISCSI_PARAM_INITIAL_R2T_EN:
2448 sscanf(buf, "%d", &session->initial_r2t_en);
2449 break;
2450 case ISCSI_PARAM_MAX_R2T:
2451 sscanf(buf, "%d", &session->max_r2t);
2452 break;
2453 case ISCSI_PARAM_IMM_DATA_EN:
2454 sscanf(buf, "%d", &session->imm_data_en);
2455 break;
2456 case ISCSI_PARAM_FIRST_BURST:
2457 sscanf(buf, "%d", &session->first_burst);
2458 break;
2459 case ISCSI_PARAM_MAX_BURST:
2460 sscanf(buf, "%d", &session->max_burst);
2461 break;
2462 case ISCSI_PARAM_PDU_INORDER_EN:
2463 sscanf(buf, "%d", &session->pdu_inorder_en);
2464 break;
2465 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2466 sscanf(buf, "%d", &session->dataseq_inorder_en);
2467 break;
2468 case ISCSI_PARAM_ERL:
2469 sscanf(buf, "%d", &session->erl);
2470 break;
2471 case ISCSI_PARAM_IFMARKER_EN:
2472 sscanf(buf, "%d", &value);
2473 BUG_ON(value);
2474 break;
2475 case ISCSI_PARAM_OFMARKER_EN:
2476 sscanf(buf, "%d", &value);
2477 BUG_ON(value);
2478 break;
2479 case ISCSI_PARAM_EXP_STATSN:
2480 sscanf(buf, "%u", &conn->exp_statsn);
2481 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002482 case ISCSI_PARAM_USERNAME:
2483 kfree(session->username);
2484 session->username = kstrdup(buf, GFP_KERNEL);
2485 if (!session->username)
2486 return -ENOMEM;
2487 break;
2488 case ISCSI_PARAM_USERNAME_IN:
2489 kfree(session->username_in);
2490 session->username_in = kstrdup(buf, GFP_KERNEL);
2491 if (!session->username_in)
2492 return -ENOMEM;
2493 break;
2494 case ISCSI_PARAM_PASSWORD:
2495 kfree(session->password);
2496 session->password = kstrdup(buf, GFP_KERNEL);
2497 if (!session->password)
2498 return -ENOMEM;
2499 break;
2500 case ISCSI_PARAM_PASSWORD_IN:
2501 kfree(session->password_in);
2502 session->password_in = kstrdup(buf, GFP_KERNEL);
2503 if (!session->password_in)
2504 return -ENOMEM;
2505 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002506 case ISCSI_PARAM_TARGET_NAME:
2507 /* this should not change between logins */
2508 if (session->targetname)
2509 break;
2510
2511 session->targetname = kstrdup(buf, GFP_KERNEL);
2512 if (!session->targetname)
2513 return -ENOMEM;
2514 break;
2515 case ISCSI_PARAM_TPGT:
2516 sscanf(buf, "%d", &session->tpgt);
2517 break;
2518 case ISCSI_PARAM_PERSISTENT_PORT:
2519 sscanf(buf, "%d", &conn->persistent_port);
2520 break;
2521 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2522 /*
2523 * this is the address returned in discovery so it should
2524 * not change between logins.
2525 */
2526 if (conn->persistent_address)
2527 break;
2528
2529 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2530 if (!conn->persistent_address)
2531 return -ENOMEM;
2532 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002533 case ISCSI_PARAM_IFACE_NAME:
2534 if (!session->ifacename)
2535 session->ifacename = kstrdup(buf, GFP_KERNEL);
2536 break;
2537 case ISCSI_PARAM_INITIATOR_NAME:
2538 if (!session->initiatorname)
2539 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2540 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002541 default:
2542 return -ENOSYS;
2543 }
2544
2545 return 0;
2546}
2547EXPORT_SYMBOL_GPL(iscsi_set_param);
2548
2549int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2550 enum iscsi_param param, char *buf)
2551{
Mike Christie75613522008-05-21 15:53:59 -05002552 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002553 int len;
2554
2555 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002556 case ISCSI_PARAM_FAST_ABORT:
2557 len = sprintf(buf, "%d\n", session->fast_abort);
2558 break;
Mike Christief6d51802007-12-13 12:43:30 -06002559 case ISCSI_PARAM_ABORT_TMO:
2560 len = sprintf(buf, "%d\n", session->abort_timeout);
2561 break;
2562 case ISCSI_PARAM_LU_RESET_TMO:
2563 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2564 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002565 case ISCSI_PARAM_INITIAL_R2T_EN:
2566 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2567 break;
2568 case ISCSI_PARAM_MAX_R2T:
2569 len = sprintf(buf, "%hu\n", session->max_r2t);
2570 break;
2571 case ISCSI_PARAM_IMM_DATA_EN:
2572 len = sprintf(buf, "%d\n", session->imm_data_en);
2573 break;
2574 case ISCSI_PARAM_FIRST_BURST:
2575 len = sprintf(buf, "%u\n", session->first_burst);
2576 break;
2577 case ISCSI_PARAM_MAX_BURST:
2578 len = sprintf(buf, "%u\n", session->max_burst);
2579 break;
2580 case ISCSI_PARAM_PDU_INORDER_EN:
2581 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2582 break;
2583 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2584 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2585 break;
2586 case ISCSI_PARAM_ERL:
2587 len = sprintf(buf, "%d\n", session->erl);
2588 break;
2589 case ISCSI_PARAM_TARGET_NAME:
2590 len = sprintf(buf, "%s\n", session->targetname);
2591 break;
2592 case ISCSI_PARAM_TPGT:
2593 len = sprintf(buf, "%d\n", session->tpgt);
2594 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002595 case ISCSI_PARAM_USERNAME:
2596 len = sprintf(buf, "%s\n", session->username);
2597 break;
2598 case ISCSI_PARAM_USERNAME_IN:
2599 len = sprintf(buf, "%s\n", session->username_in);
2600 break;
2601 case ISCSI_PARAM_PASSWORD:
2602 len = sprintf(buf, "%s\n", session->password);
2603 break;
2604 case ISCSI_PARAM_PASSWORD_IN:
2605 len = sprintf(buf, "%s\n", session->password_in);
2606 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002607 case ISCSI_PARAM_IFACE_NAME:
2608 len = sprintf(buf, "%s\n", session->ifacename);
2609 break;
2610 case ISCSI_PARAM_INITIATOR_NAME:
2611 if (!session->initiatorname)
2612 len = sprintf(buf, "%s\n", "unknown");
2613 else
2614 len = sprintf(buf, "%s\n", session->initiatorname);
2615 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002616 default:
2617 return -ENOSYS;
2618 }
2619
2620 return len;
2621}
2622EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2623
2624int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2625 enum iscsi_param param, char *buf)
2626{
2627 struct iscsi_conn *conn = cls_conn->dd_data;
2628 int len;
2629
2630 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002631 case ISCSI_PARAM_PING_TMO:
2632 len = sprintf(buf, "%u\n", conn->ping_timeout);
2633 break;
2634 case ISCSI_PARAM_RECV_TMO:
2635 len = sprintf(buf, "%u\n", conn->recv_timeout);
2636 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002637 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2638 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2639 break;
2640 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2641 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2642 break;
2643 case ISCSI_PARAM_HDRDGST_EN:
2644 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2645 break;
2646 case ISCSI_PARAM_DATADGST_EN:
2647 len = sprintf(buf, "%d\n", conn->datadgst_en);
2648 break;
2649 case ISCSI_PARAM_IFMARKER_EN:
2650 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2651 break;
2652 case ISCSI_PARAM_OFMARKER_EN:
2653 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2654 break;
2655 case ISCSI_PARAM_EXP_STATSN:
2656 len = sprintf(buf, "%u\n", conn->exp_statsn);
2657 break;
2658 case ISCSI_PARAM_PERSISTENT_PORT:
2659 len = sprintf(buf, "%d\n", conn->persistent_port);
2660 break;
2661 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2662 len = sprintf(buf, "%s\n", conn->persistent_address);
2663 break;
2664 default:
2665 return -ENOSYS;
2666 }
2667
2668 return len;
2669}
2670EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2671
Mike Christie0801c242007-05-30 12:57:12 -05002672int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2673 char *buf)
2674{
Mike Christie75613522008-05-21 15:53:59 -05002675 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002676 int len;
2677
2678 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002679 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002680 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002681 len = sprintf(buf, "%s\n", "default");
2682 else
Mike Christie75613522008-05-21 15:53:59 -05002683 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002684 break;
Mike Christie0801c242007-05-30 12:57:12 -05002685 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002686 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002687 len = sprintf(buf, "%s\n", "default");
2688 else
Mike Christie75613522008-05-21 15:53:59 -05002689 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002690 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002691 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002692 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002693 len = sprintf(buf, "%s\n", "unknown");
2694 else
Mike Christie75613522008-05-21 15:53:59 -05002695 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002696 break;
Mike Christie75613522008-05-21 15:53:59 -05002697 case ISCSI_HOST_PARAM_IPADDRESS:
2698 if (!strlen(ihost->local_address))
2699 len = sprintf(buf, "%s\n", "unknown");
2700 else
2701 len = sprintf(buf, "%s\n",
2702 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002703 break;
Mike Christie0801c242007-05-30 12:57:12 -05002704 default:
2705 return -ENOSYS;
2706 }
2707
2708 return len;
2709}
2710EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2711
2712int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2713 char *buf, int buflen)
2714{
Mike Christie75613522008-05-21 15:53:59 -05002715 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002716
2717 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002718 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002719 if (!ihost->netdev)
2720 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002721 break;
Mike Christie0801c242007-05-30 12:57:12 -05002722 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002723 if (!ihost->hwaddress)
2724 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002725 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002726 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002727 if (!ihost->initiatorname)
2728 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002729 break;
Mike Christie0801c242007-05-30 12:57:12 -05002730 default:
2731 return -ENOSYS;
2732 }
2733
2734 return 0;
2735}
2736EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2737
Mike Christie7996a772006-04-06 21:13:41 -05002738MODULE_AUTHOR("Mike Christie");
2739MODULE_DESCRIPTION("iSCSI library functions");
2740MODULE_LICENSE("GPL");