blob: 0b7457d558f7ba7c182220d71cc4909def077a5c [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 Harrison8f3339912008-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 Christie7ea8b822006-07-24 15:47:22 -0500636static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
637{
638 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
639
640 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
641 conn->tmfrsp_pdus_cnt++;
642
Mike Christie843c0a82007-12-13 12:43:20 -0600643 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500644 return;
645
646 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600647 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500648 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600649 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500650 else
Mike Christie843c0a82007-12-13 12:43:20 -0600651 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500652 wake_up(&conn->ehwait);
653}
654
Mike Christief6d51802007-12-13 12:43:30 -0600655static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
656{
657 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500658 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600659
Mike Christie9c19a7d2008-05-21 15:54:09 -0500660 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600661 return;
662
663 memset(&hdr, 0, sizeof(struct iscsi_nopout));
664 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
665 hdr.flags = ISCSI_FLAG_CMD_FINAL;
666
667 if (rhdr) {
668 memcpy(hdr.lun, rhdr->lun, 8);
669 hdr.ttt = rhdr->ttt;
670 hdr.itt = RESERVED_ITT;
671 } else
672 hdr.ttt = RESERVED_ITT;
673
Mike Christie9c19a7d2008-05-21 15:54:09 -0500674 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
675 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600676 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600677}
678
Mike Christie62f38302006-08-31 18:09:27 -0400679static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
680 char *data, int datalen)
681{
682 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
683 struct iscsi_hdr rejected_pdu;
684 uint32_t itt;
685
686 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
687
688 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
689 if (ntoh24(reject->dlength) > datalen)
690 return ISCSI_ERR_PROTO;
691
692 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
693 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000694 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600695 iscsi_conn_printk(KERN_ERR, conn,
696 "itt 0x%x had pdu (op 0x%x) rejected "
697 "due to DataDigest error.\n", itt,
698 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400699 }
700 }
701 return 0;
702}
703
Mike Christie7996a772006-04-06 21:13:41 -0500704/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500705 * iscsi_itt_to_task - look up task by itt
706 * @conn: iscsi connection
707 * @itt: itt
708 *
709 * This should be used for mgmt tasks like login and nops, or if
710 * the LDD's itt space does not include the session age.
711 *
712 * The session lock must be held.
713 */
714static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
715{
716 struct iscsi_session *session = conn->session;
717 uint32_t i;
718
719 if (itt == RESERVED_ITT)
720 return NULL;
721
722 i = get_itt(itt);
723 if (i >= session->cmds_max)
724 return NULL;
725
726 return session->cmds[i];
727}
728
729/**
Mike Christie7996a772006-04-06 21:13:41 -0500730 * __iscsi_complete_pdu - complete pdu
731 * @conn: iscsi conn
732 * @hdr: iscsi header
733 * @data: data buffer
734 * @datalen: len of data buffer
735 *
736 * Completes pdu processing by freeing any resources allocated at
737 * queuecommand or send generic. session lock must be held and verify
738 * itt must have been called.
739 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500740int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
741 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500742{
743 struct iscsi_session *session = conn->session;
744 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500745 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500746 uint32_t itt;
747
Mike Christief6d51802007-12-13 12:43:30 -0600748 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500749 rc = iscsi_verify_itt(conn, hdr->itt);
750 if (rc)
751 return rc;
752
Al Virob4377352007-02-09 16:39:40 +0000753 if (hdr->itt != RESERVED_ITT)
754 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500755 else
Al Virob4377352007-02-09 16:39:40 +0000756 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500757
Mike Christie3e5c28a2008-05-21 15:54:06 -0500758 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
759 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500760
Mike Christie3e5c28a2008-05-21 15:54:06 -0500761 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500762 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400763
Mike Christie7996a772006-04-06 21:13:41 -0500764 switch(opcode) {
765 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500766 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500767 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500768 break;
769 }
770
Al Virob4377352007-02-09 16:39:40 +0000771 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500772 break;
773
Mike Christief6d51802007-12-13 12:43:30 -0600774 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500775 break;
776 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400777 rc = iscsi_handle_reject(conn, hdr, data, datalen);
778 break;
Mike Christie7996a772006-04-06 21:13:41 -0500779 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500780 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400781 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
782 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500783 break;
784 default:
785 rc = ISCSI_ERR_BAD_OPCODE;
786 break;
787 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500788 goto out;
789 }
Mike Christie7996a772006-04-06 21:13:41 -0500790
Mike Christie3e5c28a2008-05-21 15:54:06 -0500791 switch(opcode) {
792 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500793 case ISCSI_OP_SCSI_DATA_IN:
794 task = iscsi_itt_to_ctask(conn, hdr->itt);
795 if (!task)
796 return ISCSI_ERR_BAD_ITT;
797 break;
798 case ISCSI_OP_R2T:
799 /*
800 * LLD handles R2Ts if they need to.
801 */
802 return 0;
803 case ISCSI_OP_LOGOUT_RSP:
804 case ISCSI_OP_LOGIN_RSP:
805 case ISCSI_OP_TEXT_RSP:
806 case ISCSI_OP_SCSI_TMFUNC_RSP:
807 case ISCSI_OP_NOOP_IN:
808 task = iscsi_itt_to_task(conn, hdr->itt);
809 if (!task)
810 return ISCSI_ERR_BAD_ITT;
811 break;
812 default:
813 return ISCSI_ERR_BAD_OPCODE;
814 }
815
816 switch(opcode) {
817 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500818 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500819 break;
820 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie3e5c28a2008-05-21 15:54:06 -0500821 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
822 conn->scsirsp_pdus_cnt++;
823 iscsi_update_cmdsn(session,
824 (struct iscsi_nopin*) hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500825 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500826 }
827 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500828 case ISCSI_OP_LOGOUT_RSP:
829 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
830 if (datalen) {
831 rc = ISCSI_ERR_PROTO;
832 break;
833 }
834 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
835 goto recv_pdu;
836 case ISCSI_OP_LOGIN_RSP:
837 case ISCSI_OP_TEXT_RSP:
838 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
839 /*
840 * login related PDU's exp_statsn is handled in
841 * userspace
842 */
843 goto recv_pdu;
844 case ISCSI_OP_SCSI_TMFUNC_RSP:
845 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
846 if (datalen) {
847 rc = ISCSI_ERR_PROTO;
848 break;
849 }
850
851 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500852 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500853 break;
854 case ISCSI_OP_NOOP_IN:
855 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
856 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
857 rc = ISCSI_ERR_PROTO;
858 break;
859 }
860 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
861
Mike Christie9c19a7d2008-05-21 15:54:09 -0500862 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500863 /*
864 * If this is not in response to one of our
865 * nops then it must be from userspace.
866 */
867 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500868
869 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
870 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500871 break;
872 default:
873 rc = ISCSI_ERR_BAD_OPCODE;
874 break;
875 }
876
877out:
878 return rc;
879recv_pdu:
880 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
881 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500882 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500883 return rc;
884}
Mike Christie913e5bf2008-05-21 15:54:18 -0500885EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500886
887int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
888 char *data, int datalen)
889{
890 int rc;
891
892 spin_lock(&conn->session->lock);
893 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
894 spin_unlock(&conn->session->lock);
895 return rc;
896}
897EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
898
Mike Christie0af967f2008-05-21 15:54:04 -0500899int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500900{
901 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500902 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500903
Mike Christie0af967f2008-05-21 15:54:04 -0500904 if (itt == RESERVED_ITT)
905 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500906
Mike Christie0af967f2008-05-21 15:54:04 -0500907 if (((__force u32)itt & ISCSI_AGE_MASK) !=
908 (session->age << ISCSI_AGE_SHIFT)) {
909 iscsi_conn_printk(KERN_ERR, conn,
910 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -0500911 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500912 return ISCSI_ERR_BAD_ITT;
913 }
Mike Christie7996a772006-04-06 21:13:41 -0500914
Mike Christie3e5c28a2008-05-21 15:54:06 -0500915 i = get_itt(itt);
916 if (i >= session->cmds_max) {
917 iscsi_conn_printk(KERN_ERR, conn,
918 "received invalid itt index %u (max cmds "
919 "%u.\n", i, session->cmds_max);
920 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500921 }
Mike Christie7996a772006-04-06 21:13:41 -0500922 return 0;
923}
924EXPORT_SYMBOL_GPL(iscsi_verify_itt);
925
Mike Christie913e5bf2008-05-21 15:54:18 -0500926/**
927 * iscsi_itt_to_ctask - look up ctask by itt
928 * @conn: iscsi connection
929 * @itt: itt
930 *
931 * This should be used for cmd tasks.
932 *
933 * The session lock must be held.
934 */
935struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -0500936{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500937 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -0500938
939 if (iscsi_verify_itt(conn, itt))
940 return NULL;
941
Mike Christie913e5bf2008-05-21 15:54:18 -0500942 task = iscsi_itt_to_task(conn, itt);
943 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -0500944 return NULL;
945
Mike Christie913e5bf2008-05-21 15:54:18 -0500946 if (task->sc->SCp.phase != conn->session->age) {
947 iscsi_session_printk(KERN_ERR, conn->session,
948 "task's session age %d, expected %d\n",
949 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500950 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -0500951 }
Mike Christie0af967f2008-05-21 15:54:04 -0500952
Mike Christie9c19a7d2008-05-21 15:54:09 -0500953 return task;
Mike Christie0af967f2008-05-21 15:54:04 -0500954}
955EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
956
Mike Christie7996a772006-04-06 21:13:41 -0500957void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
958{
959 struct iscsi_session *session = conn->session;
960 unsigned long flags;
961
962 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500963 if (session->state == ISCSI_STATE_FAILED) {
964 spin_unlock_irqrestore(&session->lock, flags);
965 return;
966 }
967
Mike Christie67a61112006-05-30 00:37:20 -0500968 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500969 session->state = ISCSI_STATE_FAILED;
970 spin_unlock_irqrestore(&session->lock, flags);
971 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
972 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
973 iscsi_conn_error(conn->cls_conn, err);
974}
975EXPORT_SYMBOL_GPL(iscsi_conn_failure);
976
Mike Christie77a23c22007-05-30 12:57:18 -0500977static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
978{
979 struct iscsi_session *session = conn->session;
980
981 /*
982 * Check for iSCSI window and take care of CmdSN wrap-around
983 */
Mike Christiee0726402007-07-26 12:46:48 -0500984 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
985 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
986 "CmdSN %u/%u\n", session->exp_cmdsn,
987 session->max_cmdsn, session->cmdsn,
988 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500989 return -ENOSPC;
990 }
991 return 0;
992}
993
Mike Christie9c19a7d2008-05-21 15:54:09 -0500994static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -0500995{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500996 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -0600997 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500998
Mike Christie9c19a7d2008-05-21 15:54:09 -0500999 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001000 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001001 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001002 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001003 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001004 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001005 /* done with this task */
1006 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001007 return rc;
1008}
1009
Mike Christie7996a772006-04-06 21:13:41 -05001010/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001011 * iscsi_requeue_task - requeue task to run from session workqueue
1012 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001013 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001014 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001015 * this. The session lock must be held. This should only be called
1016 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001017 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001018void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001019{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001020 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001021
Mike Christie9c19a7d2008-05-21 15:54:09 -05001022 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -06001023 scsi_queue_work(conn->session->host, &conn->xmitwork);
1024}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001025EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001026
1027/**
Mike Christie7996a772006-04-06 21:13:41 -05001028 * iscsi_data_xmit - xmit any command into the scheduled connection
1029 * @conn: iscsi connection
1030 *
1031 * Notes:
1032 * The function can return -EAGAIN in which case the caller must
1033 * re-schedule it again later or recover. '0' return code means
1034 * successful xmit.
1035 **/
1036static int iscsi_data_xmit(struct iscsi_conn *conn)
1037{
Mike Christie3219e522006-05-30 00:37:28 -05001038 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001039
Mike Christie77a23c22007-05-30 12:57:18 -05001040 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001041 if (unlikely(conn->suspend_tx)) {
1042 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001043 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001044 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001045 }
Mike Christie7996a772006-04-06 21:13:41 -05001046
Mike Christie9c19a7d2008-05-21 15:54:09 -05001047 if (conn->task) {
1048 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001049 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001050 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001051 }
1052
Mike Christie77a23c22007-05-30 12:57:18 -05001053 /*
1054 * process mgmt pdus like nops before commands since we should
1055 * only have one nop-out as a ping from us and targets should not
1056 * overflow us with nop-ins
1057 */
1058check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001059 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001060 conn->task = list_entry(conn->mgmtqueue.next,
1061 struct iscsi_task, running);
1062 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1063 __iscsi_put_task(conn->task);
1064 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001065 continue;
1066 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001067 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001068 if (rc)
1069 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001070 }
1071
Mike Christie843c0a82007-12-13 12:43:20 -06001072 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001073 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001074 if (conn->tmf_state == TMF_QUEUED)
1075 break;
1076
Mike Christie9c19a7d2008-05-21 15:54:09 -05001077 conn->task = list_entry(conn->xmitqueue.next,
1078 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001079 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001080 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001081 continue;
1082 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001083 if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
1084 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001085 continue;
1086 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001087 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001088 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001089 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001090 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001091 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001092 * we need to check the mgmt queue for nops that need to
1093 * be sent to aviod starvation
1094 */
Mike Christie843c0a82007-12-13 12:43:20 -06001095 if (!list_empty(&conn->mgmtqueue))
1096 goto check_mgmt;
1097 }
1098
1099 while (!list_empty(&conn->requeue)) {
1100 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1101 break;
1102
Mike Christieb3a7ea82007-12-13 12:43:26 -06001103 /*
1104 * we always do fastlogout - conn stop code will clean up.
1105 */
1106 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1107 break;
1108
Mike Christie9c19a7d2008-05-21 15:54:09 -05001109 conn->task = list_entry(conn->requeue.next,
1110 struct iscsi_task, running);
1111 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001112 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001113 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001114 if (rc)
1115 goto again;
1116 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001117 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001118 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001119 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001120 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001121
1122again:
1123 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001124 rc = -ENODATA;
1125 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001126 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001127}
1128
David Howellsc4028952006-11-22 14:57:56 +00001129static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001130{
David Howellsc4028952006-11-22 14:57:56 +00001131 struct iscsi_conn *conn =
1132 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001133 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001134 /*
1135 * serialize Xmit worker on a per-connection basis.
1136 */
Mike Christie3219e522006-05-30 00:37:28 -05001137 do {
1138 rc = iscsi_data_xmit(conn);
1139 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001140}
1141
1142enum {
1143 FAILURE_BAD_HOST = 1,
1144 FAILURE_SESSION_FAILED,
1145 FAILURE_SESSION_FREED,
1146 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001147 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001148 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001149 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001150 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001151 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001152 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001153};
1154
1155int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1156{
Mike Christie75613522008-05-21 15:53:59 -05001157 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001158 struct Scsi_Host *host;
1159 int reason = 0;
1160 struct iscsi_session *session;
1161 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001162 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001163
1164 sc->scsi_done = done;
1165 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001166 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001167
1168 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001169 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001170
Mike Christie75613522008-05-21 15:53:59 -05001171 cls_session = starget_to_session(scsi_target(sc->device));
1172 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001173 spin_lock(&session->lock);
1174
Mike Christie75613522008-05-21 15:53:59 -05001175 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001176 if (reason) {
1177 sc->result = reason;
1178 goto fault;
1179 }
1180
Mike Christie656cffc2006-05-18 20:31:42 -05001181 /*
1182 * ISCSI_STATE_FAILED is a temp. state. The recovery
1183 * code will decide what is best to do with command queued
1184 * during this time
1185 */
1186 if (session->state != ISCSI_STATE_LOGGED_IN &&
1187 session->state != ISCSI_STATE_FAILED) {
1188 /*
1189 * to handle the race between when we set the recovery state
1190 * and block the session we requeue here (commands could
1191 * be entering our queuecommand while a block is starting
1192 * up because the block code is not locked)
1193 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001194 switch (session->state) {
1195 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001196 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001197 sc->result = DID_IMM_RETRY << 16;
1198 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001199 case ISCSI_STATE_LOGGING_OUT:
1200 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001201 sc->result = DID_IMM_RETRY << 16;
1202 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001203 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001204 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001205 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001206 break;
1207 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001208 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001209 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001210 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001211 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001212 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001213 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001214 }
Mike Christie7996a772006-04-06 21:13:41 -05001215 goto fault;
1216 }
1217
Mike Christie7996a772006-04-06 21:13:41 -05001218 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001219 if (!conn) {
1220 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001221 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001222 goto fault;
1223 }
Mike Christie7996a772006-04-06 21:13:41 -05001224
Mike Christie77a23c22007-05-30 12:57:18 -05001225 if (iscsi_check_cmdsn_window_closed(conn)) {
1226 reason = FAILURE_WINDOW_CLOSED;
1227 goto reject;
1228 }
1229
Mike Christie9c19a7d2008-05-21 15:54:09 -05001230 if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
Mike Christie60ecebf2006-08-31 18:09:25 -04001231 sizeof(void*))) {
1232 reason = FAILURE_OOM;
1233 goto reject;
1234 }
Mike Christie7996a772006-04-06 21:13:41 -05001235 sc->SCp.phase = session->age;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001236 sc->SCp.ptr = (char *)task;
Mike Christie7996a772006-04-06 21:13:41 -05001237
Mike Christie9c19a7d2008-05-21 15:54:09 -05001238 atomic_set(&task->refcount, 1);
1239 task->state = ISCSI_TASK_PENDING;
1240 task->conn = conn;
1241 task->sc = sc;
1242 INIT_LIST_HEAD(&task->running);
1243 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001244
Mike Christie052d0142008-05-21 15:54:05 -05001245 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001246 if (iscsi_prep_scsi_cmd_pdu(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001247 sc->result = DID_ABORT << 16;
1248 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001249 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001250 goto fault;
1251 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001252 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001253 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001254 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001255 reason = FAILURE_SESSION_NOT_READY;
1256 goto reject;
1257 }
1258 } else
1259 scsi_queue_work(session->host, &conn->xmitwork);
1260
1261 session->queued_cmdsn++;
1262 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001263 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001264 return 0;
1265
1266reject:
1267 spin_unlock(&session->lock);
1268 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001269 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001270 return SCSI_MLQUEUE_HOST_BUSY;
1271
1272fault:
1273 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001274 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001275 if (!scsi_bidi_cmnd(sc))
1276 scsi_set_resid(sc, scsi_bufflen(sc));
1277 else {
1278 scsi_out(sc)->resid = scsi_out(sc)->length;
1279 scsi_in(sc)->resid = scsi_in(sc)->length;
1280 }
Mike Christie052d0142008-05-21 15:54:05 -05001281 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001282 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001283 return 0;
1284}
1285EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1286
1287int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1288{
1289 if (depth > ISCSI_MAX_CMD_PER_LUN)
1290 depth = ISCSI_MAX_CMD_PER_LUN;
1291 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1292 return sdev->queue_depth;
1293}
1294EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1295
Mike Christie7996a772006-04-06 21:13:41 -05001296void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1297{
Mike Christie75613522008-05-21 15:53:59 -05001298 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001299
1300 spin_lock_bh(&session->lock);
1301 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001302 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001303 if (session->leadconn)
1304 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001305 }
1306 spin_unlock_bh(&session->lock);
1307}
1308EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1309
1310int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1311{
Mike Christie75613522008-05-21 15:53:59 -05001312 struct iscsi_cls_session *cls_session;
1313 struct iscsi_session *session;
1314 struct iscsi_conn *conn;
1315
1316 cls_session = starget_to_session(scsi_target(sc->device));
1317 session = cls_session->dd_data;
1318 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001319
Mike Christiebc436b22007-12-13 12:43:28 -06001320 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001321 spin_lock_bh(&session->lock);
1322 if (session->state == ISCSI_STATE_TERMINATE) {
1323failed:
1324 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001325 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001326 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001327 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001328 return FAILED;
1329 }
1330
Mike Christie7996a772006-04-06 21:13:41 -05001331 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001332 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001333 /*
1334 * we drop the lock here but the leadconn cannot be destoyed while
1335 * we are in the scsi eh
1336 */
Mike Christie843c0a82007-12-13 12:43:20 -06001337 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001338
1339 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1340 wait_event_interruptible(conn->ehwait,
1341 session->state == ISCSI_STATE_TERMINATE ||
1342 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001343 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001344 if (signal_pending(current))
1345 flush_signals(current);
1346
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_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001350 iscsi_session_printk(KERN_INFO, session,
1351 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001352 else
1353 goto failed;
1354 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001355 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001356 return SUCCESS;
1357}
1358EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1359
Mike Christie843c0a82007-12-13 12:43:20 -06001360static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001361{
Mike Christie843c0a82007-12-13 12:43:20 -06001362 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001363 struct iscsi_session *session = conn->session;
1364
1365 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001366 if (conn->tmf_state == TMF_QUEUED) {
1367 conn->tmf_state = TMF_TIMEDOUT;
1368 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001369 /* unblock eh_abort() */
1370 wake_up(&conn->ehwait);
1371 }
1372 spin_unlock(&session->lock);
1373}
1374
Mike Christie9c19a7d2008-05-21 15:54:09 -05001375static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001376 struct iscsi_tm *hdr, int age,
1377 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001378{
Mike Christie7996a772006-04-06 21:13:41 -05001379 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001380 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001381
Mike Christie9c19a7d2008-05-21 15:54:09 -05001382 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001383 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001384 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001385 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001386 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001387 spin_lock_bh(&session->lock);
1388 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001389 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001390 }
Mike Christie843c0a82007-12-13 12:43:20 -06001391 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001392 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001393 conn->tmf_timer.function = iscsi_tmf_timedout;
1394 conn->tmf_timer.data = (unsigned long)conn;
1395 add_timer(&conn->tmf_timer);
1396 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001397
Mike Christie7996a772006-04-06 21:13:41 -05001398 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001399 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001400
1401 /*
1402 * block eh thread until:
1403 *
Mike Christie843c0a82007-12-13 12:43:20 -06001404 * 1) tmf response
1405 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001406 * 3) session is terminated or restarted or userspace has
1407 * given up on recovery
1408 */
Mike Christie843c0a82007-12-13 12:43:20 -06001409 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001410 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001411 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001412 if (signal_pending(current))
1413 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001414 del_timer_sync(&conn->tmf_timer);
1415
Mike Christie6724add2007-08-15 01:38:30 -05001416 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001417 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001418 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001419 if (age != session->age ||
1420 session->state != ISCSI_STATE_LOGGED_IN)
1421 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001422 return 0;
1423}
1424
1425/*
Mike Christie843c0a82007-12-13 12:43:20 -06001426 * Fail commands. session lock held and recv side suspended and xmit
1427 * thread flushed
1428 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001429static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1430 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001431{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001432 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001433
Mike Christie9c19a7d2008-05-21 15:54:09 -05001434 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1435 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001436
1437 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001438 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1439 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001440 debug_scsi("failing pending sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001441 task->sc, task->itt);
1442 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001443 }
1444 }
1445
Mike Christie9c19a7d2008-05-21 15:54:09 -05001446 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1447 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001448 debug_scsi("failing requeued sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001449 task->sc, task->itt);
1450 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001451 }
1452 }
1453
1454 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001455 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1456 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001457 debug_scsi("failing in progress sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001458 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001459 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001460 }
1461 }
1462}
1463
Mike Christieb40977d2008-05-21 15:54:03 -05001464void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001465{
1466 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001467 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1468 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001469}
Mike Christieb40977d2008-05-21 15:54:03 -05001470EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001471
1472static void iscsi_start_tx(struct iscsi_conn *conn)
1473{
1474 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001475 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1476 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001477}
1478
Mike Christief6d51802007-12-13 12:43:30 -06001479static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1480{
1481 struct iscsi_cls_session *cls_session;
1482 struct iscsi_session *session;
1483 struct iscsi_conn *conn;
1484 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1485
1486 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001487 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001488
1489 debug_scsi("scsi cmd %p timedout\n", scmd);
1490
1491 spin_lock(&session->lock);
1492 if (session->state != ISCSI_STATE_LOGGED_IN) {
1493 /*
1494 * We are probably in the middle of iscsi recovery so let
1495 * that complete and handle the error.
1496 */
1497 rc = EH_RESET_TIMER;
1498 goto done;
1499 }
1500
1501 conn = session->leadconn;
1502 if (!conn) {
1503 /* In the middle of shuting down */
1504 rc = EH_RESET_TIMER;
1505 goto done;
1506 }
1507
1508 if (!conn->recv_timeout && !conn->ping_timeout)
1509 goto done;
1510 /*
1511 * if the ping timedout then we are in the middle of cleaning up
1512 * and can let the iscsi eh handle it
1513 */
1514 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1515 (conn->ping_timeout * HZ), jiffies))
1516 rc = EH_RESET_TIMER;
1517 /*
1518 * if we are about to check the transport then give the command
1519 * more time
1520 */
1521 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1522 jiffies))
1523 rc = EH_RESET_TIMER;
1524 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001525 if (conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -06001526 rc = EH_RESET_TIMER;
1527done:
1528 spin_unlock(&session->lock);
1529 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1530 return rc;
1531}
1532
1533static void iscsi_check_transport_timeouts(unsigned long data)
1534{
1535 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1536 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001537 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001538
1539 spin_lock(&session->lock);
1540 if (session->state != ISCSI_STATE_LOGGED_IN)
1541 goto done;
1542
Mike Christie4cf10432008-05-07 20:43:52 -05001543 recv_timeout = conn->recv_timeout;
1544 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001545 goto done;
1546
Mike Christie4cf10432008-05-07 20:43:52 -05001547 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001548 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001549 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001550 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001551 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001552 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1553 "expired, last rx %lu, last ping %lu, "
1554 "now %lu\n", conn->ping_timeout, last_recv,
1555 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001556 spin_unlock(&session->lock);
1557 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1558 return;
1559 }
1560
Mike Christie4cf10432008-05-07 20:43:52 -05001561 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001562 /* send a ping to try to provoke some traffic */
1563 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1564 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001565 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001566 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001567 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001568
Mike Christiead294e92008-01-31 13:36:50 -06001569 debug_scsi("Setting next tmo %lu\n", next_timeout);
1570 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001571done:
1572 spin_unlock(&session->lock);
1573}
1574
Mike Christie9c19a7d2008-05-21 15:54:09 -05001575static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001576 struct iscsi_tm *hdr)
1577{
1578 memset(hdr, 0, sizeof(*hdr));
1579 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1580 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1581 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001582 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1583 hdr->rtt = task->hdr->itt;
1584 hdr->refcmdsn = task->hdr->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001585}
1586
Mike Christie7996a772006-04-06 21:13:41 -05001587int iscsi_eh_abort(struct scsi_cmnd *sc)
1588{
Mike Christie75613522008-05-21 15:53:59 -05001589 struct iscsi_cls_session *cls_session;
1590 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001591 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001592 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001593 struct iscsi_tm *hdr;
1594 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001595
Mike Christie75613522008-05-21 15:53:59 -05001596 cls_session = starget_to_session(scsi_target(sc->device));
1597 session = cls_session->dd_data;
1598
Mike Christie6724add2007-08-15 01:38:30 -05001599 mutex_lock(&session->eh_mutex);
1600 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001601 /*
1602 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1603 * got the command.
1604 */
1605 if (!sc->SCp.ptr) {
1606 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001607 spin_unlock_bh(&session->lock);
1608 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001609 return SUCCESS;
1610 }
1611
Mike Christie7996a772006-04-06 21:13:41 -05001612 /*
1613 * If we are not logged in or we have started a new session
1614 * then let the host reset code handle this
1615 */
Mike Christie843c0a82007-12-13 12:43:20 -06001616 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1617 sc->SCp.phase != session->age) {
1618 spin_unlock_bh(&session->lock);
1619 mutex_unlock(&session->eh_mutex);
1620 return FAILED;
1621 }
1622
1623 conn = session->leadconn;
1624 conn->eh_abort_cnt++;
1625 age = session->age;
1626
Mike Christie9c19a7d2008-05-21 15:54:09 -05001627 task = (struct iscsi_task *)sc->SCp.ptr;
1628 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001629
Mike Christie9c19a7d2008-05-21 15:54:09 -05001630 /* task completed before time out */
1631 if (!task->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001632 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001633 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001634 }
Mike Christie7996a772006-04-06 21:13:41 -05001635
Mike Christie9c19a7d2008-05-21 15:54:09 -05001636 if (task->state == ISCSI_TASK_PENDING) {
1637 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001638 goto success;
1639 }
Mike Christie7996a772006-04-06 21:13:41 -05001640
Mike Christie843c0a82007-12-13 12:43:20 -06001641 /* only have one tmf outstanding at a time */
1642 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001643 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001644 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001645
Mike Christie843c0a82007-12-13 12:43:20 -06001646 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001647 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001648
Mike Christie9c19a7d2008-05-21 15:54:09 -05001649 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001650 rc = FAILED;
1651 goto failed;
1652 }
1653
1654 switch (conn->tmf_state) {
1655 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001656 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001657 /*
1658 * stop tx side incase the target had sent a abort rsp but
1659 * the initiator was still writing out data.
1660 */
Mike Christie6724add2007-08-15 01:38:30 -05001661 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001662 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001663 * we do not stop the recv side because targets have been
1664 * good and have never sent us a successful tmf response
1665 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001666 */
Mike Christie77a23c22007-05-30 12:57:18 -05001667 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001668 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001669 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001670 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001671 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001672 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001673 case TMF_TIMEDOUT:
1674 spin_unlock_bh(&session->lock);
1675 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1676 goto failed_unlocked;
1677 case TMF_NOT_FOUND:
1678 if (!sc->SCp.ptr) {
1679 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001680 /* task completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001681 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001682 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001683 }
1684 /* fall through */
1685 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001686 conn->tmf_state = TMF_INITIAL;
1687 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001688 }
1689
Mike Christie77a23c22007-05-30 12:57:18 -05001690success:
Mike Christie7996a772006-04-06 21:13:41 -05001691 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001692success_unlocked:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001693 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001694 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001695 return SUCCESS;
1696
1697failed:
1698 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001699failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001700 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001701 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001702 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001703 return FAILED;
1704}
1705EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1706
Mike Christie843c0a82007-12-13 12:43:20 -06001707static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1708{
1709 memset(hdr, 0, sizeof(*hdr));
1710 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1711 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1712 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1713 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001714 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001715}
1716
1717int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1718{
Mike Christie75613522008-05-21 15:53:59 -05001719 struct iscsi_cls_session *cls_session;
1720 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001721 struct iscsi_conn *conn;
1722 struct iscsi_tm *hdr;
1723 int rc = FAILED;
1724
Mike Christie75613522008-05-21 15:53:59 -05001725 cls_session = starget_to_session(scsi_target(sc->device));
1726 session = cls_session->dd_data;
1727
Mike Christie843c0a82007-12-13 12:43:20 -06001728 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1729
1730 mutex_lock(&session->eh_mutex);
1731 spin_lock_bh(&session->lock);
1732 /*
1733 * Just check if we are not logged in. We cannot check for
1734 * the phase because the reset could come from a ioctl.
1735 */
1736 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1737 goto unlock;
1738 conn = session->leadconn;
1739
1740 /* only have one tmf outstanding at a time */
1741 if (conn->tmf_state != TMF_INITIAL)
1742 goto unlock;
1743 conn->tmf_state = TMF_QUEUED;
1744
1745 hdr = &conn->tmhdr;
1746 iscsi_prep_lun_reset_pdu(sc, hdr);
1747
Mike Christie9c19a7d2008-05-21 15:54:09 -05001748 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001749 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001750 rc = FAILED;
1751 goto unlock;
1752 }
1753
1754 switch (conn->tmf_state) {
1755 case TMF_SUCCESS:
1756 break;
1757 case TMF_TIMEDOUT:
1758 spin_unlock_bh(&session->lock);
1759 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1760 goto done;
1761 default:
1762 conn->tmf_state = TMF_INITIAL;
1763 goto unlock;
1764 }
1765
1766 rc = SUCCESS;
1767 spin_unlock_bh(&session->lock);
1768
1769 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001770
Mike Christie843c0a82007-12-13 12:43:20 -06001771 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001772 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001773 conn->tmf_state = TMF_INITIAL;
1774 spin_unlock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001775
1776 iscsi_start_tx(conn);
1777 goto done;
1778
1779unlock:
1780 spin_unlock_bh(&session->lock);
1781done:
1782 debug_scsi("iscsi_eh_device_reset %s\n",
1783 rc == SUCCESS ? "SUCCESS" : "FAILED");
1784 mutex_unlock(&session->eh_mutex);
1785 return rc;
1786}
1787EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1788
Olaf Kirch63203772007-12-13 12:43:25 -06001789/*
1790 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1791 * should be accessed via kfifo_{get,put} on q->queue.
1792 * Optionally, the caller can obtain the array of object pointers
1793 * by passing in a non-NULL @items pointer
1794 */
Mike Christie7996a772006-04-06 21:13:41 -05001795int
Olaf Kirch63203772007-12-13 12:43:25 -06001796iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001797{
Olaf Kirch63203772007-12-13 12:43:25 -06001798 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001799
Olaf Kirch63203772007-12-13 12:43:25 -06001800 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001801
1802 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001803
1804 /* If the user passed an items pointer, he wants a copy of
1805 * the array. */
1806 if (items)
1807 num_arrays++;
1808 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1809 if (q->pool == NULL)
1810 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001811
1812 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1813 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001814 if (q->queue == ERR_PTR(-ENOMEM))
1815 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001816
1817 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001818 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001819 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001820 q->max = i;
1821 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001822 }
Mike Christie7996a772006-04-06 21:13:41 -05001823 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1824 }
Olaf Kirch63203772007-12-13 12:43:25 -06001825
1826 if (items) {
1827 *items = q->pool + max;
1828 memcpy(*items, q->pool, max * sizeof(void *));
1829 }
1830
Mike Christie7996a772006-04-06 21:13:41 -05001831 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001832
1833enomem:
1834 iscsi_pool_free(q);
1835 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001836}
1837EXPORT_SYMBOL_GPL(iscsi_pool_init);
1838
Olaf Kirch63203772007-12-13 12:43:25 -06001839void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001840{
1841 int i;
1842
1843 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001844 kfree(q->pool[i]);
1845 if (q->pool)
1846 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001847}
1848EXPORT_SYMBOL_GPL(iscsi_pool_free);
1849
Mike Christiea4804cd2008-05-21 15:54:00 -05001850/**
1851 * iscsi_host_add - add host to system
1852 * @shost: scsi host
1853 * @pdev: parent device
1854 *
1855 * This should be called by partial offload and software iscsi drivers
1856 * to add a host to the system.
1857 */
1858int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001859{
Mike Christie8e9a20c2008-06-16 10:11:33 -05001860 if (!shost->can_queue)
1861 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
1862
Mike Christiea4804cd2008-05-21 15:54:00 -05001863 return scsi_add_host(shost, pdev);
1864}
1865EXPORT_SYMBOL_GPL(iscsi_host_add);
1866
1867/**
1868 * iscsi_host_alloc - allocate a host and driver data
1869 * @sht: scsi host template
1870 * @dd_data_size: driver host data size
1871 * @qdepth: default device queue depth
1872 *
1873 * This should be called by partial offload and software iscsi drivers.
1874 * To access the driver specific memory use the iscsi_host_priv() macro.
1875 */
1876struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1877 int dd_data_size, uint16_t qdepth)
1878{
1879 struct Scsi_Host *shost;
1880
1881 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1882 if (!shost)
1883 return NULL;
1884 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1885
Mike Christie15482712007-05-30 12:57:19 -05001886 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1887 if (qdepth != 0)
1888 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001889 "Queue depth must be between 1 and %d.\n",
1890 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001891 qdepth = ISCSI_DEF_CMD_PER_LUN;
1892 }
Mike Christie75613522008-05-21 15:53:59 -05001893 shost->cmd_per_lun = qdepth;
Mike Christiea4804cd2008-05-21 15:54:00 -05001894 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001895}
Mike Christiea4804cd2008-05-21 15:54:00 -05001896EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001897
Mike Christiea4804cd2008-05-21 15:54:00 -05001898/**
1899 * iscsi_host_remove - remove host and sessions
1900 * @shost: scsi host
1901 *
1902 * This will also remove any sessions attached to the host, but if userspace
1903 * is managing the session at the same time this will break. TODO: add
1904 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1905 * does not remove the memory from under us.
1906 */
1907void iscsi_host_remove(struct Scsi_Host *shost)
1908{
1909 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1910 scsi_remove_host(shost);
1911}
1912EXPORT_SYMBOL_GPL(iscsi_host_remove);
1913
1914void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001915{
1916 struct iscsi_host *ihost = shost_priv(shost);
1917
1918 kfree(ihost->netdev);
1919 kfree(ihost->hwaddress);
1920 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001921 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001922}
Mike Christiea4804cd2008-05-21 15:54:00 -05001923EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05001924
1925/**
1926 * iscsi_session_setup - create iscsi cls session and host and session
1927 * @iscsit: iscsi transport template
1928 * @shost: scsi host
1929 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05001930 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05001931 * @initial_cmdsn: initial CmdSN
1932 *
1933 * This can be used by software iscsi_transports that allocate
1934 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05001935 *
1936 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
1937 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
1938 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05001939 */
1940struct iscsi_cls_session *
1941iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05001942 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05001943 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05001944{
1945 struct iscsi_session *session;
1946 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05001947 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christie8e9a20c2008-06-16 10:11:33 -05001948
1949 if (!total_cmds)
1950 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001951 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05001952 * The iscsi layer needs some tasks for nop handling and tmfs,
1953 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
1954 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05001955 */
Mike Christie3cf7b232008-05-21 15:54:17 -05001956 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
1957 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1958 "must be a power of two that is at least %d.\n",
1959 total_cmds, ISCSI_TOTAL_CMDS_MIN);
1960 return NULL;
Mike Christie15482712007-05-30 12:57:19 -05001961 }
Mike Christie3cf7b232008-05-21 15:54:17 -05001962
1963 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
1964 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1965 "must be a power of 2 less than or equal to %d.\n",
1966 cmds_max, ISCSI_TOTAL_CMDS_MAX);
1967 total_cmds = ISCSI_TOTAL_CMDS_MAX;
1968 }
1969
1970 if (!is_power_of_2(total_cmds)) {
1971 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1972 "must be a power of 2.\n", total_cmds);
1973 total_cmds = rounddown_pow_of_two(total_cmds);
1974 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
1975 return NULL;
1976 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
1977 total_cmds);
1978 }
1979 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001980
Mike Christie5d91e202008-05-21 15:54:01 -05001981 cls_session = iscsi_alloc_session(shost, iscsit,
1982 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05001983 if (!cls_session)
Mike Christie7996a772006-04-06 21:13:41 -05001984 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001985 session = cls_session->dd_data;
1986 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001987 session->host = shost;
1988 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001989 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001990 session->lu_reset_timeout = 15;
1991 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05001992 session->scsi_cmds_max = scsi_cmds;
1993 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05001994 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001995 session->exp_cmdsn = initial_cmdsn + 1;
1996 session->max_cmdsn = initial_cmdsn + 1;
1997 session->max_r2t = 1;
1998 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001999 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002000 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002001
2002 /* initialize SCSI PDU commands pool */
2003 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2004 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002005 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002006 goto cmdpool_alloc_fail;
2007
2008 /* pre-format cmds pool with ITT */
2009 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002010 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002011
Mike Christie9c19a7d2008-05-21 15:54:09 -05002012 if (cmd_task_size)
2013 task->dd_data = &task[1];
2014 task->itt = cmd_i;
2015 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002016 }
2017
Mike Christief53a88d2006-06-28 12:00:27 -05002018 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002019 goto module_get_fail;
2020
Mike Christie79706342008-05-21 15:54:12 -05002021 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002022 goto cls_session_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002023 return cls_session;
2024
2025cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002026 module_put(iscsit->owner);
2027module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002028 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002029cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002030 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05002031 return NULL;
2032}
2033EXPORT_SYMBOL_GPL(iscsi_session_setup);
2034
2035/**
2036 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002037 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002038 *
Mike Christie75613522008-05-21 15:53:59 -05002039 * The driver must have called iscsi_remove_session before
2040 * calling this.
2041 */
Mike Christie7996a772006-04-06 21:13:41 -05002042void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2043{
Mike Christie75613522008-05-21 15:53:59 -05002044 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002045 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05002046
Olaf Kirch63203772007-12-13 12:43:25 -06002047 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002048
Mike Christieb2c64162007-05-30 12:57:16 -05002049 kfree(session->password);
2050 kfree(session->password_in);
2051 kfree(session->username);
2052 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002053 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002054 kfree(session->initiatorname);
2055 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002056
Mike Christie75613522008-05-21 15:53:59 -05002057 iscsi_destroy_session(cls_session);
Mike Christie63f75cc2006-07-24 15:47:29 -05002058 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002059}
2060EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2061
2062/**
2063 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2064 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002065 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002066 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002067 */
Mike Christie7996a772006-04-06 21:13:41 -05002068struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002069iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2070 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002071{
Mike Christie75613522008-05-21 15:53:59 -05002072 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002073 struct iscsi_conn *conn;
2074 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002075 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002076
Mike Christie5d91e202008-05-21 15:54:01 -05002077 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2078 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002079 if (!cls_conn)
2080 return NULL;
2081 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002082 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002083
Mike Christie5d91e202008-05-21 15:54:01 -05002084 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002085 conn->session = session;
2086 conn->cls_conn = cls_conn;
2087 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2088 conn->id = conn_idx;
2089 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002090 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002091
2092 init_timer(&conn->transport_timer);
2093 conn->transport_timer.data = (unsigned long)conn;
2094 conn->transport_timer.function = iscsi_check_transport_timeouts;
2095
Mike Christie7996a772006-04-06 21:13:41 -05002096 INIT_LIST_HEAD(&conn->run_list);
2097 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002098 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002099 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002100 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002101 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002102
Mike Christie9c19a7d2008-05-21 15:54:09 -05002103 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002104 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002105 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002106 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002107 sizeof(void*))) {
2108 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002109 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002110 }
2111 spin_unlock_bh(&session->lock);
2112
Mike Christiebf32ed32007-02-28 17:32:17 -06002113 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002114 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002115 goto login_task_data_alloc_fail;
2116 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002117
Mike Christie843c0a82007-12-13 12:43:20 -06002118 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002119 init_waitqueue_head(&conn->ehwait);
2120
2121 return cls_conn;
2122
Mike Christie9c19a7d2008-05-21 15:54:09 -05002123login_task_data_alloc_fail:
2124 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002125 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002126login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002127 iscsi_destroy_conn(cls_conn);
2128 return NULL;
2129}
2130EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2131
2132/**
2133 * iscsi_conn_teardown - teardown iscsi connection
2134 * cls_conn: iscsi class connection
2135 *
2136 * TODO: we may need to make this into a two step process
2137 * like scsi-mls remove + put host
2138 */
2139void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2140{
2141 struct iscsi_conn *conn = cls_conn->dd_data;
2142 struct iscsi_session *session = conn->session;
2143 unsigned long flags;
2144
Mike Christief6d51802007-12-13 12:43:30 -06002145 del_timer_sync(&conn->transport_timer);
2146
Mike Christie7996a772006-04-06 21:13:41 -05002147 spin_lock_bh(&session->lock);
2148 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2149 if (session->leadconn == conn) {
2150 /*
2151 * leading connection? then give up on recovery.
2152 */
2153 session->state = ISCSI_STATE_TERMINATE;
2154 wake_up(&conn->ehwait);
2155 }
2156 spin_unlock_bh(&session->lock);
2157
Mike Christie7996a772006-04-06 21:13:41 -05002158 /*
2159 * Block until all in-progress commands for this connection
2160 * time out or fail.
2161 */
2162 for (;;) {
2163 spin_lock_irqsave(session->host->host_lock, flags);
2164 if (!session->host->host_busy) { /* OK for ERL == 0 */
2165 spin_unlock_irqrestore(session->host->host_lock, flags);
2166 break;
2167 }
2168 spin_unlock_irqrestore(session->host->host_lock, flags);
2169 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002170 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2171 "host_busy %d host_failed %d\n",
2172 session->host->host_busy,
2173 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002174 /*
2175 * force eh_abort() to unblock
2176 */
2177 wake_up(&conn->ehwait);
2178 }
2179
Mike Christie779ea122007-02-28 17:32:15 -06002180 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002181 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002182
Mike Christie7996a772006-04-06 21:13:41 -05002183 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002184 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002185 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002186 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002187 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002188 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002189 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002190 spin_unlock_bh(&session->lock);
2191
Mike Christie7996a772006-04-06 21:13:41 -05002192 iscsi_destroy_conn(cls_conn);
2193}
2194EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2195
2196int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2197{
2198 struct iscsi_conn *conn = cls_conn->dd_data;
2199 struct iscsi_session *session = conn->session;
2200
Mike Christieffd04362006-08-31 18:09:24 -04002201 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002202 iscsi_conn_printk(KERN_ERR, conn,
2203 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002204 return -EPERM;
2205 }
2206
Mike Christiedb98ccd2006-08-31 18:09:31 -04002207 if ((session->imm_data_en || !session->initial_r2t_en) &&
2208 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002209 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2210 "first_burst %d max_burst %d\n",
2211 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002212 return -EINVAL;
2213 }
2214
Mike Christief6d51802007-12-13 12:43:30 -06002215 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002216 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2217 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002218 conn->recv_timeout = 5;
2219 }
2220
2221 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002222 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2223 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002224 conn->ping_timeout = 5;
2225 }
2226
Mike Christie7996a772006-04-06 21:13:41 -05002227 spin_lock_bh(&session->lock);
2228 conn->c_stage = ISCSI_CONN_STARTED;
2229 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002230 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002231
Mike Christief6d51802007-12-13 12:43:30 -06002232 conn->last_recv = jiffies;
2233 conn->last_ping = jiffies;
2234 if (conn->recv_timeout && conn->ping_timeout)
2235 mod_timer(&conn->transport_timer,
2236 jiffies + (conn->recv_timeout * HZ));
2237
Mike Christie7996a772006-04-06 21:13:41 -05002238 switch(conn->stop_stage) {
2239 case STOP_CONN_RECOVER:
2240 /*
2241 * unblock eh_abort() if it is blocked. re-try all
2242 * commands after successful recovery
2243 */
Mike Christie7996a772006-04-06 21:13:41 -05002244 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002245 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002246 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002247 if (session->age == 16)
2248 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002249 break;
Mike Christie7996a772006-04-06 21:13:41 -05002250 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002251 conn->stop_stage = 0;
2252 break;
Mike Christie7996a772006-04-06 21:13:41 -05002253 default:
2254 break;
2255 }
2256 spin_unlock_bh(&session->lock);
2257
Mike Christie75613522008-05-21 15:53:59 -05002258 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002259 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002260 return 0;
2261}
2262EXPORT_SYMBOL_GPL(iscsi_conn_start);
2263
2264static void
2265flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2266{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002267 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002268
2269 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002270 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2271 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2272 /* release ref from prep task */
2273 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002274 }
2275
2276 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002277 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2278 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2279 /* release ref from prep task */
2280 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002281 }
2282
Mike Christie9c19a7d2008-05-21 15:54:09 -05002283 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002284}
2285
Mike Christie656cffc2006-05-18 20:31:42 -05002286static void iscsi_start_session_recovery(struct iscsi_session *session,
2287 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002288{
Mike Christieed2abc72006-05-02 19:46:40 -05002289 int old_stop_stage;
2290
Mike Christief6d51802007-12-13 12:43:30 -06002291 del_timer_sync(&conn->transport_timer);
2292
Mike Christie6724add2007-08-15 01:38:30 -05002293 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002294 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002295 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002296 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002297 mutex_unlock(&session->eh_mutex);
2298 return;
2299 }
2300
2301 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002302 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002303 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002304 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002305 */
Mike Christie67a61112006-05-30 00:37:20 -05002306 if (flag == STOP_CONN_TERM)
2307 session->state = ISCSI_STATE_TERMINATE;
2308 else if (conn->stop_stage != STOP_CONN_RECOVER)
2309 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002310
2311 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002312 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002313 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002314 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002315
2316 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002317 /*
2318 * for connection level recovery we should not calculate
2319 * header digest. conn->hdr_size used for optimization
2320 * in hdr_extract() and will be re-negotiated at
2321 * set_param() time.
2322 */
2323 if (flag == STOP_CONN_RECOVER) {
2324 conn->hdrdgst_en = 0;
2325 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002326 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002327 old_stop_stage != STOP_CONN_RECOVER) {
2328 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002329 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002330 }
Mike Christie7996a772006-04-06 21:13:41 -05002331 }
Mike Christie656cffc2006-05-18 20:31:42 -05002332
Mike Christie656cffc2006-05-18 20:31:42 -05002333 /*
2334 * flush queues.
2335 */
2336 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002337 fail_all_commands(conn, -1,
2338 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002339 flush_control_queues(session, conn);
2340 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002341 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002342}
Mike Christie7996a772006-04-06 21:13:41 -05002343
2344void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2345{
2346 struct iscsi_conn *conn = cls_conn->dd_data;
2347 struct iscsi_session *session = conn->session;
2348
2349 switch (flag) {
2350 case STOP_CONN_RECOVER:
2351 case STOP_CONN_TERM:
2352 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002353 break;
Mike Christie7996a772006-04-06 21:13:41 -05002354 default:
Mike Christie322d7392008-01-31 13:36:52 -06002355 iscsi_conn_printk(KERN_ERR, conn,
2356 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002357 }
2358}
2359EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2360
2361int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2362 struct iscsi_cls_conn *cls_conn, int is_leading)
2363{
Mike Christie75613522008-05-21 15:53:59 -05002364 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002365 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002366
Mike Christie7996a772006-04-06 21:13:41 -05002367 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002368 if (is_leading)
2369 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002370 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002371
2372 /*
2373 * Unblock xmitworker(), Login Phase will pass through.
2374 */
2375 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2376 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2377 return 0;
2378}
2379EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2380
Mike Christiea54a52c2006-06-28 12:00:23 -05002381
2382int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2383 enum iscsi_param param, char *buf, int buflen)
2384{
2385 struct iscsi_conn *conn = cls_conn->dd_data;
2386 struct iscsi_session *session = conn->session;
2387 uint32_t value;
2388
2389 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002390 case ISCSI_PARAM_FAST_ABORT:
2391 sscanf(buf, "%d", &session->fast_abort);
2392 break;
Mike Christief6d51802007-12-13 12:43:30 -06002393 case ISCSI_PARAM_ABORT_TMO:
2394 sscanf(buf, "%d", &session->abort_timeout);
2395 break;
2396 case ISCSI_PARAM_LU_RESET_TMO:
2397 sscanf(buf, "%d", &session->lu_reset_timeout);
2398 break;
2399 case ISCSI_PARAM_PING_TMO:
2400 sscanf(buf, "%d", &conn->ping_timeout);
2401 break;
2402 case ISCSI_PARAM_RECV_TMO:
2403 sscanf(buf, "%d", &conn->recv_timeout);
2404 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002405 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2406 sscanf(buf, "%d", &conn->max_recv_dlength);
2407 break;
2408 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2409 sscanf(buf, "%d", &conn->max_xmit_dlength);
2410 break;
2411 case ISCSI_PARAM_HDRDGST_EN:
2412 sscanf(buf, "%d", &conn->hdrdgst_en);
2413 break;
2414 case ISCSI_PARAM_DATADGST_EN:
2415 sscanf(buf, "%d", &conn->datadgst_en);
2416 break;
2417 case ISCSI_PARAM_INITIAL_R2T_EN:
2418 sscanf(buf, "%d", &session->initial_r2t_en);
2419 break;
2420 case ISCSI_PARAM_MAX_R2T:
2421 sscanf(buf, "%d", &session->max_r2t);
2422 break;
2423 case ISCSI_PARAM_IMM_DATA_EN:
2424 sscanf(buf, "%d", &session->imm_data_en);
2425 break;
2426 case ISCSI_PARAM_FIRST_BURST:
2427 sscanf(buf, "%d", &session->first_burst);
2428 break;
2429 case ISCSI_PARAM_MAX_BURST:
2430 sscanf(buf, "%d", &session->max_burst);
2431 break;
2432 case ISCSI_PARAM_PDU_INORDER_EN:
2433 sscanf(buf, "%d", &session->pdu_inorder_en);
2434 break;
2435 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2436 sscanf(buf, "%d", &session->dataseq_inorder_en);
2437 break;
2438 case ISCSI_PARAM_ERL:
2439 sscanf(buf, "%d", &session->erl);
2440 break;
2441 case ISCSI_PARAM_IFMARKER_EN:
2442 sscanf(buf, "%d", &value);
2443 BUG_ON(value);
2444 break;
2445 case ISCSI_PARAM_OFMARKER_EN:
2446 sscanf(buf, "%d", &value);
2447 BUG_ON(value);
2448 break;
2449 case ISCSI_PARAM_EXP_STATSN:
2450 sscanf(buf, "%u", &conn->exp_statsn);
2451 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002452 case ISCSI_PARAM_USERNAME:
2453 kfree(session->username);
2454 session->username = kstrdup(buf, GFP_KERNEL);
2455 if (!session->username)
2456 return -ENOMEM;
2457 break;
2458 case ISCSI_PARAM_USERNAME_IN:
2459 kfree(session->username_in);
2460 session->username_in = kstrdup(buf, GFP_KERNEL);
2461 if (!session->username_in)
2462 return -ENOMEM;
2463 break;
2464 case ISCSI_PARAM_PASSWORD:
2465 kfree(session->password);
2466 session->password = kstrdup(buf, GFP_KERNEL);
2467 if (!session->password)
2468 return -ENOMEM;
2469 break;
2470 case ISCSI_PARAM_PASSWORD_IN:
2471 kfree(session->password_in);
2472 session->password_in = kstrdup(buf, GFP_KERNEL);
2473 if (!session->password_in)
2474 return -ENOMEM;
2475 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002476 case ISCSI_PARAM_TARGET_NAME:
2477 /* this should not change between logins */
2478 if (session->targetname)
2479 break;
2480
2481 session->targetname = kstrdup(buf, GFP_KERNEL);
2482 if (!session->targetname)
2483 return -ENOMEM;
2484 break;
2485 case ISCSI_PARAM_TPGT:
2486 sscanf(buf, "%d", &session->tpgt);
2487 break;
2488 case ISCSI_PARAM_PERSISTENT_PORT:
2489 sscanf(buf, "%d", &conn->persistent_port);
2490 break;
2491 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2492 /*
2493 * this is the address returned in discovery so it should
2494 * not change between logins.
2495 */
2496 if (conn->persistent_address)
2497 break;
2498
2499 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2500 if (!conn->persistent_address)
2501 return -ENOMEM;
2502 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002503 case ISCSI_PARAM_IFACE_NAME:
2504 if (!session->ifacename)
2505 session->ifacename = kstrdup(buf, GFP_KERNEL);
2506 break;
2507 case ISCSI_PARAM_INITIATOR_NAME:
2508 if (!session->initiatorname)
2509 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2510 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002511 default:
2512 return -ENOSYS;
2513 }
2514
2515 return 0;
2516}
2517EXPORT_SYMBOL_GPL(iscsi_set_param);
2518
2519int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2520 enum iscsi_param param, char *buf)
2521{
Mike Christie75613522008-05-21 15:53:59 -05002522 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002523 int len;
2524
2525 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002526 case ISCSI_PARAM_FAST_ABORT:
2527 len = sprintf(buf, "%d\n", session->fast_abort);
2528 break;
Mike Christief6d51802007-12-13 12:43:30 -06002529 case ISCSI_PARAM_ABORT_TMO:
2530 len = sprintf(buf, "%d\n", session->abort_timeout);
2531 break;
2532 case ISCSI_PARAM_LU_RESET_TMO:
2533 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2534 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002535 case ISCSI_PARAM_INITIAL_R2T_EN:
2536 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2537 break;
2538 case ISCSI_PARAM_MAX_R2T:
2539 len = sprintf(buf, "%hu\n", session->max_r2t);
2540 break;
2541 case ISCSI_PARAM_IMM_DATA_EN:
2542 len = sprintf(buf, "%d\n", session->imm_data_en);
2543 break;
2544 case ISCSI_PARAM_FIRST_BURST:
2545 len = sprintf(buf, "%u\n", session->first_burst);
2546 break;
2547 case ISCSI_PARAM_MAX_BURST:
2548 len = sprintf(buf, "%u\n", session->max_burst);
2549 break;
2550 case ISCSI_PARAM_PDU_INORDER_EN:
2551 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2552 break;
2553 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2554 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2555 break;
2556 case ISCSI_PARAM_ERL:
2557 len = sprintf(buf, "%d\n", session->erl);
2558 break;
2559 case ISCSI_PARAM_TARGET_NAME:
2560 len = sprintf(buf, "%s\n", session->targetname);
2561 break;
2562 case ISCSI_PARAM_TPGT:
2563 len = sprintf(buf, "%d\n", session->tpgt);
2564 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002565 case ISCSI_PARAM_USERNAME:
2566 len = sprintf(buf, "%s\n", session->username);
2567 break;
2568 case ISCSI_PARAM_USERNAME_IN:
2569 len = sprintf(buf, "%s\n", session->username_in);
2570 break;
2571 case ISCSI_PARAM_PASSWORD:
2572 len = sprintf(buf, "%s\n", session->password);
2573 break;
2574 case ISCSI_PARAM_PASSWORD_IN:
2575 len = sprintf(buf, "%s\n", session->password_in);
2576 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002577 case ISCSI_PARAM_IFACE_NAME:
2578 len = sprintf(buf, "%s\n", session->ifacename);
2579 break;
2580 case ISCSI_PARAM_INITIATOR_NAME:
2581 if (!session->initiatorname)
2582 len = sprintf(buf, "%s\n", "unknown");
2583 else
2584 len = sprintf(buf, "%s\n", session->initiatorname);
2585 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002586 default:
2587 return -ENOSYS;
2588 }
2589
2590 return len;
2591}
2592EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2593
2594int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2595 enum iscsi_param param, char *buf)
2596{
2597 struct iscsi_conn *conn = cls_conn->dd_data;
2598 int len;
2599
2600 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002601 case ISCSI_PARAM_PING_TMO:
2602 len = sprintf(buf, "%u\n", conn->ping_timeout);
2603 break;
2604 case ISCSI_PARAM_RECV_TMO:
2605 len = sprintf(buf, "%u\n", conn->recv_timeout);
2606 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002607 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2608 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2609 break;
2610 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2611 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2612 break;
2613 case ISCSI_PARAM_HDRDGST_EN:
2614 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2615 break;
2616 case ISCSI_PARAM_DATADGST_EN:
2617 len = sprintf(buf, "%d\n", conn->datadgst_en);
2618 break;
2619 case ISCSI_PARAM_IFMARKER_EN:
2620 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2621 break;
2622 case ISCSI_PARAM_OFMARKER_EN:
2623 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2624 break;
2625 case ISCSI_PARAM_EXP_STATSN:
2626 len = sprintf(buf, "%u\n", conn->exp_statsn);
2627 break;
2628 case ISCSI_PARAM_PERSISTENT_PORT:
2629 len = sprintf(buf, "%d\n", conn->persistent_port);
2630 break;
2631 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2632 len = sprintf(buf, "%s\n", conn->persistent_address);
2633 break;
2634 default:
2635 return -ENOSYS;
2636 }
2637
2638 return len;
2639}
2640EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2641
Mike Christie0801c242007-05-30 12:57:12 -05002642int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2643 char *buf)
2644{
Mike Christie75613522008-05-21 15:53:59 -05002645 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002646 int len;
2647
2648 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002649 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002650 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002651 len = sprintf(buf, "%s\n", "default");
2652 else
Mike Christie75613522008-05-21 15:53:59 -05002653 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002654 break;
Mike Christie0801c242007-05-30 12:57:12 -05002655 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002656 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002657 len = sprintf(buf, "%s\n", "default");
2658 else
Mike Christie75613522008-05-21 15:53:59 -05002659 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002660 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002661 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002662 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002663 len = sprintf(buf, "%s\n", "unknown");
2664 else
Mike Christie75613522008-05-21 15:53:59 -05002665 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002666 break;
Mike Christie75613522008-05-21 15:53:59 -05002667 case ISCSI_HOST_PARAM_IPADDRESS:
2668 if (!strlen(ihost->local_address))
2669 len = sprintf(buf, "%s\n", "unknown");
2670 else
2671 len = sprintf(buf, "%s\n",
2672 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002673 break;
Mike Christie0801c242007-05-30 12:57:12 -05002674 default:
2675 return -ENOSYS;
2676 }
2677
2678 return len;
2679}
2680EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2681
2682int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2683 char *buf, int buflen)
2684{
Mike Christie75613522008-05-21 15:53:59 -05002685 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002686
2687 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002688 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002689 if (!ihost->netdev)
2690 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002691 break;
Mike Christie0801c242007-05-30 12:57:12 -05002692 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002693 if (!ihost->hwaddress)
2694 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002695 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002696 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002697 if (!ihost->initiatorname)
2698 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002699 break;
Mike Christie0801c242007-05-30 12:57:12 -05002700 default:
2701 return -ENOSYS;
2702 }
2703
2704 return 0;
2705}
2706EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2707
Mike Christie7996a772006-04-06 21:13:41 -05002708MODULE_AUTHOR("Mike Christie");
2709MODULE_DESCRIPTION("iSCSI library functions");
2710MODULE_LICENSE("GPL");