blob: c723e60f02b0c467703d2511c90288412550b7fc [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 Christie9c19a7d2008-05-21 15:54:09 -0500365static void __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}
369
Mike Christie9c19a7d2008-05-21 15:54:09 -0500370static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400371{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500372 if (atomic_dec_and_test(&task->refcount))
373 iscsi_complete_command(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400374}
375
Mike Christie9c19a7d2008-05-21 15:54:09 -0500376void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500377{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500378 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500379
380 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500381 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500382 spin_unlock_bh(&session->lock);
383}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500385
Mike Christieb3a7ea82007-12-13 12:43:26 -0600386/*
387 * session lock must be held
388 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500389static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600390 int err)
391{
392 struct scsi_cmnd *sc;
393
Mike Christie9c19a7d2008-05-21 15:54:09 -0500394 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600395 if (!sc)
396 return;
397
Mike Christie9c19a7d2008-05-21 15:54:09 -0500398 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600399 /*
400 * cmd never made it to the xmit thread, so we should not count
401 * the cmd in the sequencing
402 */
403 conn->session->queued_cmdsn--;
404 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500405 conn->session->tt->cleanup_task(conn, task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600406
407 sc->result = err;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500408
Boaz Harroshc07d4442008-04-18 10:11:52 -0500409 if (!scsi_bidi_cmnd(sc))
410 scsi_set_resid(sc, scsi_bufflen(sc));
411 else {
412 scsi_out(sc)->resid = scsi_out(sc)->length;
413 scsi_in(sc)->resid = scsi_in(sc)->length;
414 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500415
Mike Christie9c19a7d2008-05-21 15:54:09 -0500416 if (conn->task == task)
417 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600418 /* release ref from queuecommand */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500419 __iscsi_put_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600420}
421
Mike Christie3e5c28a2008-05-21 15:54:06 -0500422static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500423 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500424{
425 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500426 struct iscsi_hdr *hdr = (struct iscsi_hdr *)task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500427 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
428
429 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
430 return -ENOTCONN;
431
432 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
433 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
434 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
435 /*
436 * pre-format CmdSN for outgoing PDU.
437 */
438 nop->cmdsn = cpu_to_be32(session->cmdsn);
439 if (hdr->itt != RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500440 hdr->itt = build_itt(task->itt, session->age);
Mike Christie052d0142008-05-21 15:54:05 -0500441 /*
442 * TODO: We always use immediate, so we never hit this.
443 * If we start to send tmfs or nops as non-immediate then
444 * we should start checking the cmdsn numbers for mgmt tasks.
445 */
446 if (conn->c_stage == ISCSI_CONN_STARTED &&
447 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
448 session->queued_cmdsn++;
449 session->cmdsn++;
450 }
451 }
452
Mike Christie3e5c28a2008-05-21 15:54:06 -0500453 if (session->tt->init_task)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500454 session->tt->init_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500455
456 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
457 session->state = ISCSI_STATE_LOGGING_OUT;
458
Mike Christie9c19a7d2008-05-21 15:54:09 -0500459 list_move_tail(&task->running, &conn->mgmt_run_list);
Mike Christie052d0142008-05-21 15:54:05 -0500460 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
461 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500462 task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500463 return 0;
464}
465
Mike Christie9c19a7d2008-05-21 15:54:09 -0500466static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600467__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
468 char *data, uint32_t data_size)
469{
470 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500471 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600472
473 if (session->state == ISCSI_STATE_TERMINATE)
474 return NULL;
475
476 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
477 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
478 /*
479 * Login and Text are sent serially, in
480 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500481 * Same task can be used. Same ITT must be used.
482 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600483 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500484 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600485 else {
486 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
487 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
488
Mike Christie3e5c28a2008-05-21 15:54:06 -0500489 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500490 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600491 return NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500492
493 if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
494 hdr->ttt == RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500495 conn->ping_task = task;
Mike Christie052d0142008-05-21 15:54:05 -0500496 conn->last_ping = jiffies;
497 }
Mike Christief6d51802007-12-13 12:43:30 -0600498 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500499 /*
500 * released in complete pdu for task we expect a response for, and
501 * released by the lld when it has transmitted the task for
502 * pdus we do not expect a response for.
503 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500504 atomic_set(&task->refcount, 1);
505 task->conn = conn;
506 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600507
508 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500509 memcpy(task->data, data, data_size);
510 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600511 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500512 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600513
Mike Christie9c19a7d2008-05-21 15:54:09 -0500514 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
515 INIT_LIST_HEAD(&task->running);
516 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500517
518 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 if (iscsi_prep_mgmt_task(conn, task)) {
520 __iscsi_put_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500521 return NULL;
522 }
523
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 if (session->tt->xmit_task(task))
525 task = NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500526
527 } else
528 scsi_queue_work(conn->session->host, &conn->xmitwork);
529
Mike Christie9c19a7d2008-05-21 15:54:09 -0500530 return task;
Mike Christief6d51802007-12-13 12:43:30 -0600531}
532
533int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
534 char *data, uint32_t data_size)
535{
536 struct iscsi_conn *conn = cls_conn->dd_data;
537 struct iscsi_session *session = conn->session;
538 int err = 0;
539
540 spin_lock_bh(&session->lock);
541 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
542 err = -EPERM;
543 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600544 return err;
545}
546EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
547
Mike Christie7996a772006-04-06 21:13:41 -0500548/**
549 * iscsi_cmd_rsp - SCSI Command Response processing
550 * @conn: iscsi connection
551 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500552 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500553 * @data: cmd data buffer
554 * @datalen: len of buffer
555 *
556 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500557 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500558 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500559static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500560 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500561 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500562{
Mike Christie7996a772006-04-06 21:13:41 -0500563 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
564 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500565 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500566
Mike Christie77a23c22007-05-30 12:57:18 -0500567 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500568 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
569
570 sc->result = (DID_OK << 16) | rhdr->cmd_status;
571
572 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
573 sc->result = DID_ERROR << 16;
574 goto out;
575 }
576
577 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600578 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500579
580 if (datalen < 2) {
581invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600582 iscsi_conn_printk(KERN_ERR, conn,
583 "Got CHECK_CONDITION but invalid data "
584 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500585 sc->result = DID_BAD_TARGET << 16;
586 goto out;
587 }
588
Mike Christie8eb00532007-02-28 17:32:19 -0600589 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500590 if (datalen < senselen)
591 goto invalid_datalen;
592
593 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600594 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500595 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600596 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500597 }
598
Boaz Harroshc07d4442008-04-18 10:11:52 -0500599 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
600 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
601 int res_count = be32_to_cpu(rhdr->bi_residual_count);
602
603 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
604 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
605 res_count <= scsi_in(sc)->length))
606 scsi_in(sc)->resid = res_count;
607 else
608 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
609 }
610
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600611 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
612 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500613 int res_count = be32_to_cpu(rhdr->residual_count);
614
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600615 if (res_count > 0 &&
616 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
617 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500618 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900619 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500620 else
621 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500622 }
Mike Christie7996a772006-04-06 21:13:41 -0500623out:
624 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500625 (long)sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500626 conn->scsirsp_pdus_cnt++;
627
Mike Christie9c19a7d2008-05-21 15:54:09 -0500628 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500629}
630
Mike Christie7ea8b822006-07-24 15:47:22 -0500631static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
632{
633 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
634
635 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
636 conn->tmfrsp_pdus_cnt++;
637
Mike Christie843c0a82007-12-13 12:43:20 -0600638 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500639 return;
640
641 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600642 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500643 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600644 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500645 else
Mike Christie843c0a82007-12-13 12:43:20 -0600646 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500647 wake_up(&conn->ehwait);
648}
649
Mike Christief6d51802007-12-13 12:43:30 -0600650static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
651{
652 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500653 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600654
Mike Christie9c19a7d2008-05-21 15:54:09 -0500655 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600656 return;
657
658 memset(&hdr, 0, sizeof(struct iscsi_nopout));
659 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
660 hdr.flags = ISCSI_FLAG_CMD_FINAL;
661
662 if (rhdr) {
663 memcpy(hdr.lun, rhdr->lun, 8);
664 hdr.ttt = rhdr->ttt;
665 hdr.itt = RESERVED_ITT;
666 } else
667 hdr.ttt = RESERVED_ITT;
668
Mike Christie9c19a7d2008-05-21 15:54:09 -0500669 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
670 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600671 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600672}
673
Mike Christie62f38302006-08-31 18:09:27 -0400674static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
675 char *data, int datalen)
676{
677 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
678 struct iscsi_hdr rejected_pdu;
679 uint32_t itt;
680
681 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
682
683 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
684 if (ntoh24(reject->dlength) > datalen)
685 return ISCSI_ERR_PROTO;
686
687 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
688 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000689 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600690 iscsi_conn_printk(KERN_ERR, conn,
691 "itt 0x%x had pdu (op 0x%x) rejected "
692 "due to DataDigest error.\n", itt,
693 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400694 }
695 }
696 return 0;
697}
698
Mike Christie7996a772006-04-06 21:13:41 -0500699/**
700 * __iscsi_complete_pdu - complete pdu
701 * @conn: iscsi conn
702 * @hdr: iscsi header
703 * @data: data buffer
704 * @datalen: len of data buffer
705 *
706 * Completes pdu processing by freeing any resources allocated at
707 * queuecommand or send generic. session lock must be held and verify
708 * itt must have been called.
709 */
Adrian Bunkf8d9d652008-01-29 00:11:27 +0200710static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
711 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500712{
713 struct iscsi_session *session = conn->session;
714 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500715 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500716 uint32_t itt;
717
Mike Christief6d51802007-12-13 12:43:30 -0600718 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500719 rc = iscsi_verify_itt(conn, hdr->itt);
720 if (rc)
721 return rc;
722
Al Virob4377352007-02-09 16:39:40 +0000723 if (hdr->itt != RESERVED_ITT)
724 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500725 else
Al Virob4377352007-02-09 16:39:40 +0000726 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500727
Mike Christie3e5c28a2008-05-21 15:54:06 -0500728 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
729 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500730
Mike Christie3e5c28a2008-05-21 15:54:06 -0500731 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500732 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400733
Mike Christie7996a772006-04-06 21:13:41 -0500734 switch(opcode) {
735 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500736 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500737 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500738 break;
739 }
740
Al Virob4377352007-02-09 16:39:40 +0000741 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500742 break;
743
Mike Christief6d51802007-12-13 12:43:30 -0600744 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500745 break;
746 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400747 rc = iscsi_handle_reject(conn, hdr, data, datalen);
748 break;
Mike Christie7996a772006-04-06 21:13:41 -0500749 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500750 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400751 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
752 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500753 break;
754 default:
755 rc = ISCSI_ERR_BAD_OPCODE;
756 break;
757 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500758 goto out;
759 }
Mike Christie7996a772006-04-06 21:13:41 -0500760
Mike Christie9c19a7d2008-05-21 15:54:09 -0500761 task = session->cmds[itt];
Mike Christie3e5c28a2008-05-21 15:54:06 -0500762 switch(opcode) {
763 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500764 if (!task->sc) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500765 rc = ISCSI_ERR_NO_SCSI_CMD;
766 break;
767 }
Mike Christie9c19a7d2008-05-21 15:54:09 -0500768 BUG_ON((void*)task != task->sc->SCp.ptr);
769 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500770 break;
771 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500772 if (!task->sc) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500773 rc = ISCSI_ERR_NO_SCSI_CMD;
774 break;
775 }
Mike Christie9c19a7d2008-05-21 15:54:09 -0500776 BUG_ON((void*)task != task->sc->SCp.ptr);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500777 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
778 conn->scsirsp_pdus_cnt++;
779 iscsi_update_cmdsn(session,
780 (struct iscsi_nopin*) hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500781 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500782 }
783 break;
784 case ISCSI_OP_R2T:
785 /* LLD handles this for now */
786 break;
787 case ISCSI_OP_LOGOUT_RSP:
788 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
789 if (datalen) {
790 rc = ISCSI_ERR_PROTO;
791 break;
792 }
793 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
794 goto recv_pdu;
795 case ISCSI_OP_LOGIN_RSP:
796 case ISCSI_OP_TEXT_RSP:
797 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
798 /*
799 * login related PDU's exp_statsn is handled in
800 * userspace
801 */
802 goto recv_pdu;
803 case ISCSI_OP_SCSI_TMFUNC_RSP:
804 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
805 if (datalen) {
806 rc = ISCSI_ERR_PROTO;
807 break;
808 }
809
810 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500811 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500812 break;
813 case ISCSI_OP_NOOP_IN:
814 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
815 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
816 rc = ISCSI_ERR_PROTO;
817 break;
818 }
819 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
820
Mike Christie9c19a7d2008-05-21 15:54:09 -0500821 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500822 /*
823 * If this is not in response to one of our
824 * nops then it must be from userspace.
825 */
826 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500827
828 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
829 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500830 break;
831 default:
832 rc = ISCSI_ERR_BAD_OPCODE;
833 break;
834 }
835
836out:
837 return rc;
838recv_pdu:
839 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
840 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500841 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500842 return rc;
843}
Mike Christie7996a772006-04-06 21:13:41 -0500844
845int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
846 char *data, int datalen)
847{
848 int rc;
849
850 spin_lock(&conn->session->lock);
851 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
852 spin_unlock(&conn->session->lock);
853 return rc;
854}
855EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
856
Mike Christie0af967f2008-05-21 15:54:04 -0500857int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500858{
859 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500860 struct iscsi_task *task;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500861 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500862
Mike Christie0af967f2008-05-21 15:54:04 -0500863 if (itt == RESERVED_ITT)
864 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500865
Mike Christie0af967f2008-05-21 15:54:04 -0500866 if (((__force u32)itt & ISCSI_AGE_MASK) !=
867 (session->age << ISCSI_AGE_SHIFT)) {
868 iscsi_conn_printk(KERN_ERR, conn,
869 "received itt %x expected session age (%x)\n",
870 (__force u32)itt,
871 session->age & ISCSI_AGE_MASK);
872 return ISCSI_ERR_BAD_ITT;
873 }
Mike Christie7996a772006-04-06 21:13:41 -0500874
Mike Christie3e5c28a2008-05-21 15:54:06 -0500875 i = get_itt(itt);
876 if (i >= session->cmds_max) {
877 iscsi_conn_printk(KERN_ERR, conn,
878 "received invalid itt index %u (max cmds "
879 "%u.\n", i, session->cmds_max);
880 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500881 }
882
Mike Christie9c19a7d2008-05-21 15:54:09 -0500883 task = session->cmds[i];
884 if (task->sc && task->sc->SCp.phase != session->age) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500885 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500886 "iscsi: task's session age %d, "
887 "expected %d\n", task->sc->SCp.phase,
Mike Christie3e5c28a2008-05-21 15:54:06 -0500888 session->age);
889 return ISCSI_ERR_SESSION_FAILED;
890 }
Mike Christie7996a772006-04-06 21:13:41 -0500891 return 0;
892}
893EXPORT_SYMBOL_GPL(iscsi_verify_itt);
894
Mike Christie9c19a7d2008-05-21 15:54:09 -0500895struct iscsi_task *
Mike Christie0af967f2008-05-21 15:54:04 -0500896iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
897{
898 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500899 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -0500900 uint32_t i;
901
902 if (iscsi_verify_itt(conn, itt))
903 return NULL;
904
905 if (itt == RESERVED_ITT)
906 return NULL;
907
908 i = get_itt(itt);
909 if (i >= session->cmds_max)
910 return NULL;
911
Mike Christie9c19a7d2008-05-21 15:54:09 -0500912 task = session->cmds[i];
913 if (!task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -0500914 return NULL;
915
Mike Christie9c19a7d2008-05-21 15:54:09 -0500916 if (task->sc->SCp.phase != session->age)
Mike Christie0af967f2008-05-21 15:54:04 -0500917 return NULL;
918
Mike Christie9c19a7d2008-05-21 15:54:09 -0500919 return task;
Mike Christie0af967f2008-05-21 15:54:04 -0500920}
921EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
922
Mike Christie7996a772006-04-06 21:13:41 -0500923void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
924{
925 struct iscsi_session *session = conn->session;
926 unsigned long flags;
927
928 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500929 if (session->state == ISCSI_STATE_FAILED) {
930 spin_unlock_irqrestore(&session->lock, flags);
931 return;
932 }
933
Mike Christie67a61112006-05-30 00:37:20 -0500934 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500935 session->state = ISCSI_STATE_FAILED;
936 spin_unlock_irqrestore(&session->lock, flags);
937 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
938 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
939 iscsi_conn_error(conn->cls_conn, err);
940}
941EXPORT_SYMBOL_GPL(iscsi_conn_failure);
942
Mike Christie77a23c22007-05-30 12:57:18 -0500943static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
944{
945 struct iscsi_session *session = conn->session;
946
947 /*
948 * Check for iSCSI window and take care of CmdSN wrap-around
949 */
Mike Christiee0726402007-07-26 12:46:48 -0500950 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
951 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
952 "CmdSN %u/%u\n", session->exp_cmdsn,
953 session->max_cmdsn, session->cmdsn,
954 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500955 return -ENOSPC;
956 }
957 return 0;
958}
959
Mike Christie9c19a7d2008-05-21 15:54:09 -0500960static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -0500961{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500962 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -0600963 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500964
Mike Christie9c19a7d2008-05-21 15:54:09 -0500965 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -0500966 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500967 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -0500968 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500969 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -0500970 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500971 /* done with this task */
972 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -0500973 return rc;
974}
975
Mike Christie7996a772006-04-06 21:13:41 -0500976/**
Mike Christie9c19a7d2008-05-21 15:54:09 -0500977 * iscsi_requeue_task - requeue task to run from session workqueue
978 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -0600979 *
Mike Christie9c19a7d2008-05-21 15:54:09 -0500980 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -0500981 * this. The session lock must be held. This should only be called
982 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -0600983 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500984void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -0600985{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500986 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -0600987
Mike Christie9c19a7d2008-05-21 15:54:09 -0500988 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -0600989 scsi_queue_work(conn->session->host, &conn->xmitwork);
990}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500991EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -0600992
993/**
Mike Christie7996a772006-04-06 21:13:41 -0500994 * iscsi_data_xmit - xmit any command into the scheduled connection
995 * @conn: iscsi connection
996 *
997 * Notes:
998 * The function can return -EAGAIN in which case the caller must
999 * re-schedule it again later or recover. '0' return code means
1000 * successful xmit.
1001 **/
1002static int iscsi_data_xmit(struct iscsi_conn *conn)
1003{
Mike Christie3219e522006-05-30 00:37:28 -05001004 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001005
Mike Christie77a23c22007-05-30 12:57:18 -05001006 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001007 if (unlikely(conn->suspend_tx)) {
1008 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001009 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001010 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001011 }
Mike Christie7996a772006-04-06 21:13:41 -05001012
Mike Christie9c19a7d2008-05-21 15:54:09 -05001013 if (conn->task) {
1014 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001015 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001016 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001017 }
1018
Mike Christie77a23c22007-05-30 12:57:18 -05001019 /*
1020 * process mgmt pdus like nops before commands since we should
1021 * only have one nop-out as a ping from us and targets should not
1022 * overflow us with nop-ins
1023 */
1024check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001025 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001026 conn->task = list_entry(conn->mgmtqueue.next,
1027 struct iscsi_task, running);
1028 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1029 __iscsi_put_task(conn->task);
1030 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001031 continue;
1032 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001033 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001034 if (rc)
1035 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001036 }
1037
Mike Christie843c0a82007-12-13 12:43:20 -06001038 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001039 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001040 if (conn->tmf_state == TMF_QUEUED)
1041 break;
1042
Mike Christie9c19a7d2008-05-21 15:54:09 -05001043 conn->task = list_entry(conn->xmitqueue.next,
1044 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001045 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001046 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001047 continue;
1048 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001049 if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
1050 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001051 continue;
1052 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001053 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001054 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001055 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001056 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001057 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001058 * we need to check the mgmt queue for nops that need to
1059 * be sent to aviod starvation
1060 */
Mike Christie843c0a82007-12-13 12:43:20 -06001061 if (!list_empty(&conn->mgmtqueue))
1062 goto check_mgmt;
1063 }
1064
1065 while (!list_empty(&conn->requeue)) {
1066 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1067 break;
1068
Mike Christieb3a7ea82007-12-13 12:43:26 -06001069 /*
1070 * we always do fastlogout - conn stop code will clean up.
1071 */
1072 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1073 break;
1074
Mike Christie9c19a7d2008-05-21 15:54:09 -05001075 conn->task = list_entry(conn->requeue.next,
1076 struct iscsi_task, running);
1077 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001078 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001079 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001080 if (rc)
1081 goto again;
1082 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001083 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001084 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001085 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001086 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001087
1088again:
1089 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001090 rc = -ENODATA;
1091 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001092 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001093}
1094
David Howellsc4028952006-11-22 14:57:56 +00001095static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001096{
David Howellsc4028952006-11-22 14:57:56 +00001097 struct iscsi_conn *conn =
1098 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001099 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001100 /*
1101 * serialize Xmit worker on a per-connection basis.
1102 */
Mike Christie3219e522006-05-30 00:37:28 -05001103 do {
1104 rc = iscsi_data_xmit(conn);
1105 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001106}
1107
1108enum {
1109 FAILURE_BAD_HOST = 1,
1110 FAILURE_SESSION_FAILED,
1111 FAILURE_SESSION_FREED,
1112 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001113 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001114 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001115 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001116 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001117 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001118 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001119};
1120
1121int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1122{
Mike Christie75613522008-05-21 15:53:59 -05001123 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001124 struct Scsi_Host *host;
1125 int reason = 0;
1126 struct iscsi_session *session;
1127 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001128 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001129
1130 sc->scsi_done = done;
1131 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001132 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001133
1134 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001135 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001136
Mike Christie75613522008-05-21 15:53:59 -05001137 cls_session = starget_to_session(scsi_target(sc->device));
1138 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001139 spin_lock(&session->lock);
1140
Mike Christie75613522008-05-21 15:53:59 -05001141 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001142 if (reason) {
1143 sc->result = reason;
1144 goto fault;
1145 }
1146
Mike Christie656cffc2006-05-18 20:31:42 -05001147 /*
1148 * ISCSI_STATE_FAILED is a temp. state. The recovery
1149 * code will decide what is best to do with command queued
1150 * during this time
1151 */
1152 if (session->state != ISCSI_STATE_LOGGED_IN &&
1153 session->state != ISCSI_STATE_FAILED) {
1154 /*
1155 * to handle the race between when we set the recovery state
1156 * and block the session we requeue here (commands could
1157 * be entering our queuecommand while a block is starting
1158 * up because the block code is not locked)
1159 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001160 switch (session->state) {
1161 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001162 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001163 sc->result = DID_IMM_RETRY << 16;
1164 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001165 case ISCSI_STATE_LOGGING_OUT:
1166 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001167 sc->result = DID_IMM_RETRY << 16;
1168 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001169 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001170 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001171 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001172 break;
1173 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001174 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001175 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001176 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001177 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001178 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001179 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001180 }
Mike Christie7996a772006-04-06 21:13:41 -05001181 goto fault;
1182 }
1183
Mike Christie7996a772006-04-06 21:13:41 -05001184 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001185 if (!conn) {
1186 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001187 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001188 goto fault;
1189 }
Mike Christie7996a772006-04-06 21:13:41 -05001190
Mike Christie77a23c22007-05-30 12:57:18 -05001191 if (iscsi_check_cmdsn_window_closed(conn)) {
1192 reason = FAILURE_WINDOW_CLOSED;
1193 goto reject;
1194 }
1195
Mike Christie9c19a7d2008-05-21 15:54:09 -05001196 if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
Mike Christie60ecebf2006-08-31 18:09:25 -04001197 sizeof(void*))) {
1198 reason = FAILURE_OOM;
1199 goto reject;
1200 }
Mike Christie7996a772006-04-06 21:13:41 -05001201 sc->SCp.phase = session->age;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001202 sc->SCp.ptr = (char *)task;
Mike Christie7996a772006-04-06 21:13:41 -05001203
Mike Christie9c19a7d2008-05-21 15:54:09 -05001204 atomic_set(&task->refcount, 1);
1205 task->state = ISCSI_TASK_PENDING;
1206 task->conn = conn;
1207 task->sc = sc;
1208 INIT_LIST_HEAD(&task->running);
1209 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001210
Mike Christie052d0142008-05-21 15:54:05 -05001211 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001212 if (iscsi_prep_scsi_cmd_pdu(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001213 sc->result = DID_ABORT << 16;
1214 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001215 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001216 goto fault;
1217 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001218 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001219 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001220 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001221 reason = FAILURE_SESSION_NOT_READY;
1222 goto reject;
1223 }
1224 } else
1225 scsi_queue_work(session->host, &conn->xmitwork);
1226
1227 session->queued_cmdsn++;
1228 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001229 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001230 return 0;
1231
1232reject:
1233 spin_unlock(&session->lock);
1234 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001235 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001236 return SCSI_MLQUEUE_HOST_BUSY;
1237
1238fault:
1239 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001240 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001241 if (!scsi_bidi_cmnd(sc))
1242 scsi_set_resid(sc, scsi_bufflen(sc));
1243 else {
1244 scsi_out(sc)->resid = scsi_out(sc)->length;
1245 scsi_in(sc)->resid = scsi_in(sc)->length;
1246 }
Mike Christie052d0142008-05-21 15:54:05 -05001247 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001248 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001249 return 0;
1250}
1251EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1252
1253int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1254{
1255 if (depth > ISCSI_MAX_CMD_PER_LUN)
1256 depth = ISCSI_MAX_CMD_PER_LUN;
1257 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1258 return sdev->queue_depth;
1259}
1260EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1261
Mike Christie7996a772006-04-06 21:13:41 -05001262void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1263{
Mike Christie75613522008-05-21 15:53:59 -05001264 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001265
1266 spin_lock_bh(&session->lock);
1267 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001268 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001269 if (session->leadconn)
1270 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001271 }
1272 spin_unlock_bh(&session->lock);
1273}
1274EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1275
1276int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1277{
Mike Christie75613522008-05-21 15:53:59 -05001278 struct iscsi_cls_session *cls_session;
1279 struct iscsi_session *session;
1280 struct iscsi_conn *conn;
1281
1282 cls_session = starget_to_session(scsi_target(sc->device));
1283 session = cls_session->dd_data;
1284 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001285
Mike Christiebc436b22007-12-13 12:43:28 -06001286 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001287 spin_lock_bh(&session->lock);
1288 if (session->state == ISCSI_STATE_TERMINATE) {
1289failed:
1290 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001291 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001292 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001293 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001294 return FAILED;
1295 }
1296
Mike Christie7996a772006-04-06 21:13:41 -05001297 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001298 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001299 /*
1300 * we drop the lock here but the leadconn cannot be destoyed while
1301 * we are in the scsi eh
1302 */
Mike Christie843c0a82007-12-13 12:43:20 -06001303 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001304
1305 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1306 wait_event_interruptible(conn->ehwait,
1307 session->state == ISCSI_STATE_TERMINATE ||
1308 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001309 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001310 if (signal_pending(current))
1311 flush_signals(current);
1312
Mike Christiebc436b22007-12-13 12:43:28 -06001313 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001314 spin_lock_bh(&session->lock);
1315 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001316 iscsi_session_printk(KERN_INFO, session,
1317 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001318 else
1319 goto failed;
1320 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001321 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001322 return SUCCESS;
1323}
1324EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1325
Mike Christie843c0a82007-12-13 12:43:20 -06001326static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001327{
Mike Christie843c0a82007-12-13 12:43:20 -06001328 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001329 struct iscsi_session *session = conn->session;
1330
1331 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001332 if (conn->tmf_state == TMF_QUEUED) {
1333 conn->tmf_state = TMF_TIMEDOUT;
1334 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001335 /* unblock eh_abort() */
1336 wake_up(&conn->ehwait);
1337 }
1338 spin_unlock(&session->lock);
1339}
1340
Mike Christie9c19a7d2008-05-21 15:54:09 -05001341static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001342 struct iscsi_tm *hdr, int age,
1343 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001344{
Mike Christie7996a772006-04-06 21:13:41 -05001345 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001346 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001347
Mike Christie9c19a7d2008-05-21 15:54:09 -05001348 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001349 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001350 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001351 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001352 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001353 spin_lock_bh(&session->lock);
1354 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001355 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001356 }
Mike Christie843c0a82007-12-13 12:43:20 -06001357 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001358 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001359 conn->tmf_timer.function = iscsi_tmf_timedout;
1360 conn->tmf_timer.data = (unsigned long)conn;
1361 add_timer(&conn->tmf_timer);
1362 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001363
Mike Christie7996a772006-04-06 21:13:41 -05001364 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001365 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001366
1367 /*
1368 * block eh thread until:
1369 *
Mike Christie843c0a82007-12-13 12:43:20 -06001370 * 1) tmf response
1371 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001372 * 3) session is terminated or restarted or userspace has
1373 * given up on recovery
1374 */
Mike Christie843c0a82007-12-13 12:43:20 -06001375 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001376 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001377 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001378 if (signal_pending(current))
1379 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001380 del_timer_sync(&conn->tmf_timer);
1381
Mike Christie6724add2007-08-15 01:38:30 -05001382 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001383 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001384 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001385 if (age != session->age ||
1386 session->state != ISCSI_STATE_LOGGED_IN)
1387 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001388 return 0;
1389}
1390
1391/*
Mike Christie843c0a82007-12-13 12:43:20 -06001392 * Fail commands. session lock held and recv side suspended and xmit
1393 * thread flushed
1394 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001395static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1396 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001397{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001398 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001399
Mike Christie9c19a7d2008-05-21 15:54:09 -05001400 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1401 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001402
1403 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001404 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1405 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001406 debug_scsi("failing pending sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001407 task->sc, task->itt);
1408 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001409 }
1410 }
1411
Mike Christie9c19a7d2008-05-21 15:54:09 -05001412 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1413 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001414 debug_scsi("failing requeued sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001415 task->sc, task->itt);
1416 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001417 }
1418 }
1419
1420 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001421 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1422 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001423 debug_scsi("failing in progress sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001424 task->sc, task->itt);
1425 fail_command(conn, task, DID_BUS_BUSY << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001426 }
1427 }
1428}
1429
Mike Christieb40977d2008-05-21 15:54:03 -05001430void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001431{
1432 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001433 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1434 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001435}
Mike Christieb40977d2008-05-21 15:54:03 -05001436EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001437
1438static void iscsi_start_tx(struct iscsi_conn *conn)
1439{
1440 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001441 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1442 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001443}
1444
Mike Christief6d51802007-12-13 12:43:30 -06001445static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1446{
1447 struct iscsi_cls_session *cls_session;
1448 struct iscsi_session *session;
1449 struct iscsi_conn *conn;
1450 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1451
1452 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001453 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001454
1455 debug_scsi("scsi cmd %p timedout\n", scmd);
1456
1457 spin_lock(&session->lock);
1458 if (session->state != ISCSI_STATE_LOGGED_IN) {
1459 /*
1460 * We are probably in the middle of iscsi recovery so let
1461 * that complete and handle the error.
1462 */
1463 rc = EH_RESET_TIMER;
1464 goto done;
1465 }
1466
1467 conn = session->leadconn;
1468 if (!conn) {
1469 /* In the middle of shuting down */
1470 rc = EH_RESET_TIMER;
1471 goto done;
1472 }
1473
1474 if (!conn->recv_timeout && !conn->ping_timeout)
1475 goto done;
1476 /*
1477 * if the ping timedout then we are in the middle of cleaning up
1478 * and can let the iscsi eh handle it
1479 */
1480 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1481 (conn->ping_timeout * HZ), jiffies))
1482 rc = EH_RESET_TIMER;
1483 /*
1484 * if we are about to check the transport then give the command
1485 * more time
1486 */
1487 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1488 jiffies))
1489 rc = EH_RESET_TIMER;
1490 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001491 if (conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -06001492 rc = EH_RESET_TIMER;
1493done:
1494 spin_unlock(&session->lock);
1495 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1496 return rc;
1497}
1498
1499static void iscsi_check_transport_timeouts(unsigned long data)
1500{
1501 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1502 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001503 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001504
1505 spin_lock(&session->lock);
1506 if (session->state != ISCSI_STATE_LOGGED_IN)
1507 goto done;
1508
Mike Christie4cf10432008-05-07 20:43:52 -05001509 recv_timeout = conn->recv_timeout;
1510 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001511 goto done;
1512
Mike Christie4cf10432008-05-07 20:43:52 -05001513 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001514 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001515 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001516 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001517 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001518 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1519 "expired, last rx %lu, last ping %lu, "
1520 "now %lu\n", conn->ping_timeout, last_recv,
1521 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001522 spin_unlock(&session->lock);
1523 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1524 return;
1525 }
1526
Mike Christie4cf10432008-05-07 20:43:52 -05001527 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001528 /* send a ping to try to provoke some traffic */
1529 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1530 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001531 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001532 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001533 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001534
Mike Christiead294e92008-01-31 13:36:50 -06001535 debug_scsi("Setting next tmo %lu\n", next_timeout);
1536 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001537done:
1538 spin_unlock(&session->lock);
1539}
1540
Mike Christie9c19a7d2008-05-21 15:54:09 -05001541static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001542 struct iscsi_tm *hdr)
1543{
1544 memset(hdr, 0, sizeof(*hdr));
1545 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1546 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1547 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001548 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1549 hdr->rtt = task->hdr->itt;
1550 hdr->refcmdsn = task->hdr->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001551}
1552
Mike Christie7996a772006-04-06 21:13:41 -05001553int iscsi_eh_abort(struct scsi_cmnd *sc)
1554{
Mike Christie75613522008-05-21 15:53:59 -05001555 struct iscsi_cls_session *cls_session;
1556 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001557 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001558 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001559 struct iscsi_tm *hdr;
1560 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001561
Mike Christie75613522008-05-21 15:53:59 -05001562 cls_session = starget_to_session(scsi_target(sc->device));
1563 session = cls_session->dd_data;
1564
Mike Christie6724add2007-08-15 01:38:30 -05001565 mutex_lock(&session->eh_mutex);
1566 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001567 /*
1568 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1569 * got the command.
1570 */
1571 if (!sc->SCp.ptr) {
1572 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001573 spin_unlock_bh(&session->lock);
1574 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001575 return SUCCESS;
1576 }
1577
Mike Christie7996a772006-04-06 21:13:41 -05001578 /*
1579 * If we are not logged in or we have started a new session
1580 * then let the host reset code handle this
1581 */
Mike Christie843c0a82007-12-13 12:43:20 -06001582 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1583 sc->SCp.phase != session->age) {
1584 spin_unlock_bh(&session->lock);
1585 mutex_unlock(&session->eh_mutex);
1586 return FAILED;
1587 }
1588
1589 conn = session->leadconn;
1590 conn->eh_abort_cnt++;
1591 age = session->age;
1592
Mike Christie9c19a7d2008-05-21 15:54:09 -05001593 task = (struct iscsi_task *)sc->SCp.ptr;
1594 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001595
Mike Christie9c19a7d2008-05-21 15:54:09 -05001596 /* task completed before time out */
1597 if (!task->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001598 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001599 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001600 }
Mike Christie7996a772006-04-06 21:13:41 -05001601
Mike Christie9c19a7d2008-05-21 15:54:09 -05001602 if (task->state == ISCSI_TASK_PENDING) {
1603 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001604 goto success;
1605 }
Mike Christie7996a772006-04-06 21:13:41 -05001606
Mike Christie843c0a82007-12-13 12:43:20 -06001607 /* only have one tmf outstanding at a time */
1608 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001609 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001610 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001611
Mike Christie843c0a82007-12-13 12:43:20 -06001612 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001613 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001614
Mike Christie9c19a7d2008-05-21 15:54:09 -05001615 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001616 rc = FAILED;
1617 goto failed;
1618 }
1619
1620 switch (conn->tmf_state) {
1621 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001622 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001623 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001624 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001625 * clean up task if aborted. grab the recv lock as a writer
Mike Christie77a23c22007-05-30 12:57:18 -05001626 */
1627 write_lock_bh(conn->recv_lock);
1628 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001629 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001630 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001631 spin_unlock(&session->lock);
1632 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001633 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001634 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001635 case TMF_TIMEDOUT:
1636 spin_unlock_bh(&session->lock);
1637 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1638 goto failed_unlocked;
1639 case TMF_NOT_FOUND:
1640 if (!sc->SCp.ptr) {
1641 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001642 /* task completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001643 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001644 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001645 }
1646 /* fall through */
1647 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001648 conn->tmf_state = TMF_INITIAL;
1649 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001650 }
1651
Mike Christie77a23c22007-05-30 12:57:18 -05001652success:
Mike Christie7996a772006-04-06 21:13:41 -05001653 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001654success_unlocked:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001655 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001656 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001657 return SUCCESS;
1658
1659failed:
1660 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001661failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001662 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001663 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001664 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001665 return FAILED;
1666}
1667EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1668
Mike Christie843c0a82007-12-13 12:43:20 -06001669static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1670{
1671 memset(hdr, 0, sizeof(*hdr));
1672 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1673 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1674 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1675 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001676 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001677}
1678
1679int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1680{
Mike Christie75613522008-05-21 15:53:59 -05001681 struct iscsi_cls_session *cls_session;
1682 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001683 struct iscsi_conn *conn;
1684 struct iscsi_tm *hdr;
1685 int rc = FAILED;
1686
Mike Christie75613522008-05-21 15:53:59 -05001687 cls_session = starget_to_session(scsi_target(sc->device));
1688 session = cls_session->dd_data;
1689
Mike Christie843c0a82007-12-13 12:43:20 -06001690 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1691
1692 mutex_lock(&session->eh_mutex);
1693 spin_lock_bh(&session->lock);
1694 /*
1695 * Just check if we are not logged in. We cannot check for
1696 * the phase because the reset could come from a ioctl.
1697 */
1698 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1699 goto unlock;
1700 conn = session->leadconn;
1701
1702 /* only have one tmf outstanding at a time */
1703 if (conn->tmf_state != TMF_INITIAL)
1704 goto unlock;
1705 conn->tmf_state = TMF_QUEUED;
1706
1707 hdr = &conn->tmhdr;
1708 iscsi_prep_lun_reset_pdu(sc, hdr);
1709
Mike Christie9c19a7d2008-05-21 15:54:09 -05001710 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001711 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001712 rc = FAILED;
1713 goto unlock;
1714 }
1715
1716 switch (conn->tmf_state) {
1717 case TMF_SUCCESS:
1718 break;
1719 case TMF_TIMEDOUT:
1720 spin_unlock_bh(&session->lock);
1721 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1722 goto done;
1723 default:
1724 conn->tmf_state = TMF_INITIAL;
1725 goto unlock;
1726 }
1727
1728 rc = SUCCESS;
1729 spin_unlock_bh(&session->lock);
1730
1731 iscsi_suspend_tx(conn);
1732 /* need to grab the recv lock then session lock */
1733 write_lock_bh(conn->recv_lock);
1734 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001735 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001736 conn->tmf_state = TMF_INITIAL;
1737 spin_unlock(&session->lock);
1738 write_unlock_bh(conn->recv_lock);
1739
1740 iscsi_start_tx(conn);
1741 goto done;
1742
1743unlock:
1744 spin_unlock_bh(&session->lock);
1745done:
1746 debug_scsi("iscsi_eh_device_reset %s\n",
1747 rc == SUCCESS ? "SUCCESS" : "FAILED");
1748 mutex_unlock(&session->eh_mutex);
1749 return rc;
1750}
1751EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1752
Olaf Kirch63203772007-12-13 12:43:25 -06001753/*
1754 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1755 * should be accessed via kfifo_{get,put} on q->queue.
1756 * Optionally, the caller can obtain the array of object pointers
1757 * by passing in a non-NULL @items pointer
1758 */
Mike Christie7996a772006-04-06 21:13:41 -05001759int
Olaf Kirch63203772007-12-13 12:43:25 -06001760iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001761{
Olaf Kirch63203772007-12-13 12:43:25 -06001762 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001763
Olaf Kirch63203772007-12-13 12:43:25 -06001764 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001765
1766 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001767
1768 /* If the user passed an items pointer, he wants a copy of
1769 * the array. */
1770 if (items)
1771 num_arrays++;
1772 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1773 if (q->pool == NULL)
1774 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001775
1776 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1777 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001778 if (q->queue == ERR_PTR(-ENOMEM))
1779 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001780
1781 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001782 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001783 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001784 q->max = i;
1785 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001786 }
Mike Christie7996a772006-04-06 21:13:41 -05001787 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1788 }
Olaf Kirch63203772007-12-13 12:43:25 -06001789
1790 if (items) {
1791 *items = q->pool + max;
1792 memcpy(*items, q->pool, max * sizeof(void *));
1793 }
1794
Mike Christie7996a772006-04-06 21:13:41 -05001795 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001796
1797enomem:
1798 iscsi_pool_free(q);
1799 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001800}
1801EXPORT_SYMBOL_GPL(iscsi_pool_init);
1802
Olaf Kirch63203772007-12-13 12:43:25 -06001803void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001804{
1805 int i;
1806
1807 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001808 kfree(q->pool[i]);
1809 if (q->pool)
1810 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001811}
1812EXPORT_SYMBOL_GPL(iscsi_pool_free);
1813
Mike Christiea4804cd2008-05-21 15:54:00 -05001814/**
1815 * iscsi_host_add - add host to system
1816 * @shost: scsi host
1817 * @pdev: parent device
1818 *
1819 * This should be called by partial offload and software iscsi drivers
1820 * to add a host to the system.
1821 */
1822int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001823{
Mike Christiea4804cd2008-05-21 15:54:00 -05001824 return scsi_add_host(shost, pdev);
1825}
1826EXPORT_SYMBOL_GPL(iscsi_host_add);
1827
1828/**
1829 * iscsi_host_alloc - allocate a host and driver data
1830 * @sht: scsi host template
1831 * @dd_data_size: driver host data size
1832 * @qdepth: default device queue depth
1833 *
1834 * This should be called by partial offload and software iscsi drivers.
1835 * To access the driver specific memory use the iscsi_host_priv() macro.
1836 */
1837struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1838 int dd_data_size, uint16_t qdepth)
1839{
1840 struct Scsi_Host *shost;
1841
1842 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1843 if (!shost)
1844 return NULL;
1845 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1846
Mike Christie15482712007-05-30 12:57:19 -05001847 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1848 if (qdepth != 0)
1849 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001850 "Queue depth must be between 1 and %d.\n",
1851 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001852 qdepth = ISCSI_DEF_CMD_PER_LUN;
1853 }
Mike Christie75613522008-05-21 15:53:59 -05001854 shost->cmd_per_lun = qdepth;
Mike Christiea4804cd2008-05-21 15:54:00 -05001855 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001856}
Mike Christiea4804cd2008-05-21 15:54:00 -05001857EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001858
Mike Christiea4804cd2008-05-21 15:54:00 -05001859/**
1860 * iscsi_host_remove - remove host and sessions
1861 * @shost: scsi host
1862 *
1863 * This will also remove any sessions attached to the host, but if userspace
1864 * is managing the session at the same time this will break. TODO: add
1865 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1866 * does not remove the memory from under us.
1867 */
1868void iscsi_host_remove(struct Scsi_Host *shost)
1869{
1870 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1871 scsi_remove_host(shost);
1872}
1873EXPORT_SYMBOL_GPL(iscsi_host_remove);
1874
1875void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001876{
1877 struct iscsi_host *ihost = shost_priv(shost);
1878
1879 kfree(ihost->netdev);
1880 kfree(ihost->hwaddress);
1881 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001882 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001883}
Mike Christiea4804cd2008-05-21 15:54:00 -05001884EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05001885
1886/**
1887 * iscsi_session_setup - create iscsi cls session and host and session
1888 * @iscsit: iscsi transport template
1889 * @shost: scsi host
1890 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05001891 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05001892 * @initial_cmdsn: initial CmdSN
1893 *
1894 * This can be used by software iscsi_transports that allocate
1895 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05001896 *
1897 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
1898 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
1899 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05001900 */
1901struct iscsi_cls_session *
1902iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05001903 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05001904 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05001905{
1906 struct iscsi_session *session;
1907 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05001908 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001909 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05001910 * The iscsi layer needs some tasks for nop handling and tmfs,
1911 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
1912 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05001913 */
Mike Christie3cf7b232008-05-21 15:54:17 -05001914 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
1915 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1916 "must be a power of two that is at least %d.\n",
1917 total_cmds, ISCSI_TOTAL_CMDS_MIN);
1918 return NULL;
Mike Christie15482712007-05-30 12:57:19 -05001919 }
Mike Christie3cf7b232008-05-21 15:54:17 -05001920
1921 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
1922 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1923 "must be a power of 2 less than or equal to %d.\n",
1924 cmds_max, ISCSI_TOTAL_CMDS_MAX);
1925 total_cmds = ISCSI_TOTAL_CMDS_MAX;
1926 }
1927
1928 if (!is_power_of_2(total_cmds)) {
1929 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1930 "must be a power of 2.\n", total_cmds);
1931 total_cmds = rounddown_pow_of_two(total_cmds);
1932 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
1933 return NULL;
1934 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
1935 total_cmds);
1936 }
1937 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001938
Mike Christie5d91e202008-05-21 15:54:01 -05001939 cls_session = iscsi_alloc_session(shost, iscsit,
1940 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05001941 if (!cls_session)
Mike Christie7996a772006-04-06 21:13:41 -05001942 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05001943 session = cls_session->dd_data;
1944 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001945 session->host = shost;
1946 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001947 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001948 session->lu_reset_timeout = 15;
1949 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05001950 session->scsi_cmds_max = scsi_cmds;
1951 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05001952 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001953 session->exp_cmdsn = initial_cmdsn + 1;
1954 session->max_cmdsn = initial_cmdsn + 1;
1955 session->max_r2t = 1;
1956 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001957 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05001958 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001959
1960 /* initialize SCSI PDU commands pool */
1961 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1962 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001963 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05001964 goto cmdpool_alloc_fail;
1965
1966 /* pre-format cmds pool with ITT */
1967 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001968 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05001969
Mike Christie9c19a7d2008-05-21 15:54:09 -05001970 if (cmd_task_size)
1971 task->dd_data = &task[1];
1972 task->itt = cmd_i;
1973 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05001974 }
1975
Mike Christief53a88d2006-06-28 12:00:27 -05001976 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05001977 goto module_get_fail;
1978
Mike Christie79706342008-05-21 15:54:12 -05001979 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05001980 goto cls_session_fail;
Mike Christie7996a772006-04-06 21:13:41 -05001981 return cls_session;
1982
1983cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05001984 module_put(iscsit->owner);
1985module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001986 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001987cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05001988 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001989 return NULL;
1990}
1991EXPORT_SYMBOL_GPL(iscsi_session_setup);
1992
1993/**
1994 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05001995 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05001996 *
Mike Christie75613522008-05-21 15:53:59 -05001997 * The driver must have called iscsi_remove_session before
1998 * calling this.
1999 */
Mike Christie7996a772006-04-06 21:13:41 -05002000void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2001{
Mike Christie75613522008-05-21 15:53:59 -05002002 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002003 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05002004
Olaf Kirch63203772007-12-13 12:43:25 -06002005 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002006
Mike Christieb2c64162007-05-30 12:57:16 -05002007 kfree(session->password);
2008 kfree(session->password_in);
2009 kfree(session->username);
2010 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002011 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002012 kfree(session->initiatorname);
2013 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002014
Mike Christie75613522008-05-21 15:53:59 -05002015 iscsi_destroy_session(cls_session);
Mike Christie63f75cc2006-07-24 15:47:29 -05002016 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002017}
2018EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2019
2020/**
2021 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2022 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002023 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002024 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002025 */
Mike Christie7996a772006-04-06 21:13:41 -05002026struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002027iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2028 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002029{
Mike Christie75613522008-05-21 15:53:59 -05002030 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002031 struct iscsi_conn *conn;
2032 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002033 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002034
Mike Christie5d91e202008-05-21 15:54:01 -05002035 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2036 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002037 if (!cls_conn)
2038 return NULL;
2039 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002040 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002041
Mike Christie5d91e202008-05-21 15:54:01 -05002042 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002043 conn->session = session;
2044 conn->cls_conn = cls_conn;
2045 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2046 conn->id = conn_idx;
2047 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002048 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002049
2050 init_timer(&conn->transport_timer);
2051 conn->transport_timer.data = (unsigned long)conn;
2052 conn->transport_timer.function = iscsi_check_transport_timeouts;
2053
Mike Christie7996a772006-04-06 21:13:41 -05002054 INIT_LIST_HEAD(&conn->run_list);
2055 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002056 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002057 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002058 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002059 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002060
Mike Christie9c19a7d2008-05-21 15:54:09 -05002061 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002062 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002063 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002064 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002065 sizeof(void*))) {
2066 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002067 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002068 }
2069 spin_unlock_bh(&session->lock);
2070
Mike Christiebf32ed32007-02-28 17:32:17 -06002071 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002072 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002073 goto login_task_data_alloc_fail;
2074 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002075
Mike Christie843c0a82007-12-13 12:43:20 -06002076 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002077 init_waitqueue_head(&conn->ehwait);
2078
2079 return cls_conn;
2080
Mike Christie9c19a7d2008-05-21 15:54:09 -05002081login_task_data_alloc_fail:
2082 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002083 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002084login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002085 iscsi_destroy_conn(cls_conn);
2086 return NULL;
2087}
2088EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2089
2090/**
2091 * iscsi_conn_teardown - teardown iscsi connection
2092 * cls_conn: iscsi class connection
2093 *
2094 * TODO: we may need to make this into a two step process
2095 * like scsi-mls remove + put host
2096 */
2097void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2098{
2099 struct iscsi_conn *conn = cls_conn->dd_data;
2100 struct iscsi_session *session = conn->session;
2101 unsigned long flags;
2102
Mike Christief6d51802007-12-13 12:43:30 -06002103 del_timer_sync(&conn->transport_timer);
2104
Mike Christie7996a772006-04-06 21:13:41 -05002105 spin_lock_bh(&session->lock);
2106 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2107 if (session->leadconn == conn) {
2108 /*
2109 * leading connection? then give up on recovery.
2110 */
2111 session->state = ISCSI_STATE_TERMINATE;
2112 wake_up(&conn->ehwait);
2113 }
2114 spin_unlock_bh(&session->lock);
2115
Mike Christie7996a772006-04-06 21:13:41 -05002116 /*
2117 * Block until all in-progress commands for this connection
2118 * time out or fail.
2119 */
2120 for (;;) {
2121 spin_lock_irqsave(session->host->host_lock, flags);
2122 if (!session->host->host_busy) { /* OK for ERL == 0 */
2123 spin_unlock_irqrestore(session->host->host_lock, flags);
2124 break;
2125 }
2126 spin_unlock_irqrestore(session->host->host_lock, flags);
2127 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002128 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2129 "host_busy %d host_failed %d\n",
2130 session->host->host_busy,
2131 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002132 /*
2133 * force eh_abort() to unblock
2134 */
2135 wake_up(&conn->ehwait);
2136 }
2137
Mike Christie779ea122007-02-28 17:32:15 -06002138 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002139 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002140
Mike Christie7996a772006-04-06 21:13:41 -05002141 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002142 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002143 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002144 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002145 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002146 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002147 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002148 spin_unlock_bh(&session->lock);
2149
Mike Christie7996a772006-04-06 21:13:41 -05002150 iscsi_destroy_conn(cls_conn);
2151}
2152EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2153
2154int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2155{
2156 struct iscsi_conn *conn = cls_conn->dd_data;
2157 struct iscsi_session *session = conn->session;
2158
Mike Christieffd04362006-08-31 18:09:24 -04002159 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002160 iscsi_conn_printk(KERN_ERR, conn,
2161 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002162 return -EPERM;
2163 }
2164
Mike Christiedb98ccd2006-08-31 18:09:31 -04002165 if ((session->imm_data_en || !session->initial_r2t_en) &&
2166 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002167 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2168 "first_burst %d max_burst %d\n",
2169 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002170 return -EINVAL;
2171 }
2172
Mike Christief6d51802007-12-13 12:43:30 -06002173 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002174 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2175 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002176 conn->recv_timeout = 5;
2177 }
2178
2179 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002180 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2181 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002182 conn->ping_timeout = 5;
2183 }
2184
Mike Christie7996a772006-04-06 21:13:41 -05002185 spin_lock_bh(&session->lock);
2186 conn->c_stage = ISCSI_CONN_STARTED;
2187 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002188 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002189
Mike Christief6d51802007-12-13 12:43:30 -06002190 conn->last_recv = jiffies;
2191 conn->last_ping = jiffies;
2192 if (conn->recv_timeout && conn->ping_timeout)
2193 mod_timer(&conn->transport_timer,
2194 jiffies + (conn->recv_timeout * HZ));
2195
Mike Christie7996a772006-04-06 21:13:41 -05002196 switch(conn->stop_stage) {
2197 case STOP_CONN_RECOVER:
2198 /*
2199 * unblock eh_abort() if it is blocked. re-try all
2200 * commands after successful recovery
2201 */
Mike Christie7996a772006-04-06 21:13:41 -05002202 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002203 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002204 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002205 if (session->age == 16)
2206 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002207 break;
Mike Christie7996a772006-04-06 21:13:41 -05002208 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002209 conn->stop_stage = 0;
2210 break;
Mike Christie7996a772006-04-06 21:13:41 -05002211 default:
2212 break;
2213 }
2214 spin_unlock_bh(&session->lock);
2215
Mike Christie75613522008-05-21 15:53:59 -05002216 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002217 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002218 return 0;
2219}
2220EXPORT_SYMBOL_GPL(iscsi_conn_start);
2221
2222static void
2223flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2224{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002225 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002226
2227 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002228 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2229 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2230 /* release ref from prep task */
2231 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002232 }
2233
2234 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002235 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2236 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2237 /* release ref from prep task */
2238 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002239 }
2240
Mike Christie9c19a7d2008-05-21 15:54:09 -05002241 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002242}
2243
Mike Christie656cffc2006-05-18 20:31:42 -05002244static void iscsi_start_session_recovery(struct iscsi_session *session,
2245 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002246{
Mike Christieed2abc72006-05-02 19:46:40 -05002247 int old_stop_stage;
2248
Mike Christief6d51802007-12-13 12:43:30 -06002249 del_timer_sync(&conn->transport_timer);
2250
Mike Christie6724add2007-08-15 01:38:30 -05002251 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002252 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002253 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002254 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002255 mutex_unlock(&session->eh_mutex);
2256 return;
2257 }
2258
2259 /*
2260 * The LLD either freed/unset the lock on us, or userspace called
2261 * stop but did not create a proper connection (connection was never
2262 * bound or it was unbound then stop was called).
2263 */
2264 if (!conn->recv_lock) {
2265 spin_unlock_bh(&session->lock);
2266 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002267 return;
2268 }
Mike Christieed2abc72006-05-02 19:46:40 -05002269
2270 /*
2271 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002272 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002273 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002274 */
Mike Christie67a61112006-05-30 00:37:20 -05002275 if (flag == STOP_CONN_TERM)
2276 session->state = ISCSI_STATE_TERMINATE;
2277 else if (conn->stop_stage != STOP_CONN_RECOVER)
2278 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002279
2280 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002281 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002282 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002283 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002284
2285 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002286
Mike Christie1c834692006-07-24 15:47:26 -05002287 write_lock_bh(conn->recv_lock);
2288 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2289 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002290
Mike Christie7996a772006-04-06 21:13:41 -05002291 /*
2292 * for connection level recovery we should not calculate
2293 * header digest. conn->hdr_size used for optimization
2294 * in hdr_extract() and will be re-negotiated at
2295 * set_param() time.
2296 */
2297 if (flag == STOP_CONN_RECOVER) {
2298 conn->hdrdgst_en = 0;
2299 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002300 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002301 old_stop_stage != STOP_CONN_RECOVER) {
2302 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002303 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002304 }
Mike Christie7996a772006-04-06 21:13:41 -05002305 }
Mike Christie656cffc2006-05-18 20:31:42 -05002306
Mike Christie656cffc2006-05-18 20:31:42 -05002307 /*
2308 * flush queues.
2309 */
2310 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002311 fail_all_commands(conn, -1,
2312 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002313 flush_control_queues(session, conn);
2314 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002315 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002316}
Mike Christie7996a772006-04-06 21:13:41 -05002317
2318void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2319{
2320 struct iscsi_conn *conn = cls_conn->dd_data;
2321 struct iscsi_session *session = conn->session;
2322
2323 switch (flag) {
2324 case STOP_CONN_RECOVER:
2325 case STOP_CONN_TERM:
2326 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002327 break;
Mike Christie7996a772006-04-06 21:13:41 -05002328 default:
Mike Christie322d7392008-01-31 13:36:52 -06002329 iscsi_conn_printk(KERN_ERR, conn,
2330 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002331 }
2332}
2333EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2334
2335int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2336 struct iscsi_cls_conn *cls_conn, int is_leading)
2337{
Mike Christie75613522008-05-21 15:53:59 -05002338 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002339 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002340
Mike Christie7996a772006-04-06 21:13:41 -05002341 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002342 if (is_leading)
2343 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002344 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002345
2346 /*
2347 * Unblock xmitworker(), Login Phase will pass through.
2348 */
2349 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2350 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2351 return 0;
2352}
2353EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2354
Mike Christiea54a52c2006-06-28 12:00:23 -05002355
2356int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2357 enum iscsi_param param, char *buf, int buflen)
2358{
2359 struct iscsi_conn *conn = cls_conn->dd_data;
2360 struct iscsi_session *session = conn->session;
2361 uint32_t value;
2362
2363 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002364 case ISCSI_PARAM_FAST_ABORT:
2365 sscanf(buf, "%d", &session->fast_abort);
2366 break;
Mike Christief6d51802007-12-13 12:43:30 -06002367 case ISCSI_PARAM_ABORT_TMO:
2368 sscanf(buf, "%d", &session->abort_timeout);
2369 break;
2370 case ISCSI_PARAM_LU_RESET_TMO:
2371 sscanf(buf, "%d", &session->lu_reset_timeout);
2372 break;
2373 case ISCSI_PARAM_PING_TMO:
2374 sscanf(buf, "%d", &conn->ping_timeout);
2375 break;
2376 case ISCSI_PARAM_RECV_TMO:
2377 sscanf(buf, "%d", &conn->recv_timeout);
2378 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002379 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2380 sscanf(buf, "%d", &conn->max_recv_dlength);
2381 break;
2382 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2383 sscanf(buf, "%d", &conn->max_xmit_dlength);
2384 break;
2385 case ISCSI_PARAM_HDRDGST_EN:
2386 sscanf(buf, "%d", &conn->hdrdgst_en);
2387 break;
2388 case ISCSI_PARAM_DATADGST_EN:
2389 sscanf(buf, "%d", &conn->datadgst_en);
2390 break;
2391 case ISCSI_PARAM_INITIAL_R2T_EN:
2392 sscanf(buf, "%d", &session->initial_r2t_en);
2393 break;
2394 case ISCSI_PARAM_MAX_R2T:
2395 sscanf(buf, "%d", &session->max_r2t);
2396 break;
2397 case ISCSI_PARAM_IMM_DATA_EN:
2398 sscanf(buf, "%d", &session->imm_data_en);
2399 break;
2400 case ISCSI_PARAM_FIRST_BURST:
2401 sscanf(buf, "%d", &session->first_burst);
2402 break;
2403 case ISCSI_PARAM_MAX_BURST:
2404 sscanf(buf, "%d", &session->max_burst);
2405 break;
2406 case ISCSI_PARAM_PDU_INORDER_EN:
2407 sscanf(buf, "%d", &session->pdu_inorder_en);
2408 break;
2409 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2410 sscanf(buf, "%d", &session->dataseq_inorder_en);
2411 break;
2412 case ISCSI_PARAM_ERL:
2413 sscanf(buf, "%d", &session->erl);
2414 break;
2415 case ISCSI_PARAM_IFMARKER_EN:
2416 sscanf(buf, "%d", &value);
2417 BUG_ON(value);
2418 break;
2419 case ISCSI_PARAM_OFMARKER_EN:
2420 sscanf(buf, "%d", &value);
2421 BUG_ON(value);
2422 break;
2423 case ISCSI_PARAM_EXP_STATSN:
2424 sscanf(buf, "%u", &conn->exp_statsn);
2425 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002426 case ISCSI_PARAM_USERNAME:
2427 kfree(session->username);
2428 session->username = kstrdup(buf, GFP_KERNEL);
2429 if (!session->username)
2430 return -ENOMEM;
2431 break;
2432 case ISCSI_PARAM_USERNAME_IN:
2433 kfree(session->username_in);
2434 session->username_in = kstrdup(buf, GFP_KERNEL);
2435 if (!session->username_in)
2436 return -ENOMEM;
2437 break;
2438 case ISCSI_PARAM_PASSWORD:
2439 kfree(session->password);
2440 session->password = kstrdup(buf, GFP_KERNEL);
2441 if (!session->password)
2442 return -ENOMEM;
2443 break;
2444 case ISCSI_PARAM_PASSWORD_IN:
2445 kfree(session->password_in);
2446 session->password_in = kstrdup(buf, GFP_KERNEL);
2447 if (!session->password_in)
2448 return -ENOMEM;
2449 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002450 case ISCSI_PARAM_TARGET_NAME:
2451 /* this should not change between logins */
2452 if (session->targetname)
2453 break;
2454
2455 session->targetname = kstrdup(buf, GFP_KERNEL);
2456 if (!session->targetname)
2457 return -ENOMEM;
2458 break;
2459 case ISCSI_PARAM_TPGT:
2460 sscanf(buf, "%d", &session->tpgt);
2461 break;
2462 case ISCSI_PARAM_PERSISTENT_PORT:
2463 sscanf(buf, "%d", &conn->persistent_port);
2464 break;
2465 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2466 /*
2467 * this is the address returned in discovery so it should
2468 * not change between logins.
2469 */
2470 if (conn->persistent_address)
2471 break;
2472
2473 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2474 if (!conn->persistent_address)
2475 return -ENOMEM;
2476 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002477 case ISCSI_PARAM_IFACE_NAME:
2478 if (!session->ifacename)
2479 session->ifacename = kstrdup(buf, GFP_KERNEL);
2480 break;
2481 case ISCSI_PARAM_INITIATOR_NAME:
2482 if (!session->initiatorname)
2483 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2484 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002485 default:
2486 return -ENOSYS;
2487 }
2488
2489 return 0;
2490}
2491EXPORT_SYMBOL_GPL(iscsi_set_param);
2492
2493int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2494 enum iscsi_param param, char *buf)
2495{
Mike Christie75613522008-05-21 15:53:59 -05002496 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002497 int len;
2498
2499 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002500 case ISCSI_PARAM_FAST_ABORT:
2501 len = sprintf(buf, "%d\n", session->fast_abort);
2502 break;
Mike Christief6d51802007-12-13 12:43:30 -06002503 case ISCSI_PARAM_ABORT_TMO:
2504 len = sprintf(buf, "%d\n", session->abort_timeout);
2505 break;
2506 case ISCSI_PARAM_LU_RESET_TMO:
2507 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2508 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002509 case ISCSI_PARAM_INITIAL_R2T_EN:
2510 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2511 break;
2512 case ISCSI_PARAM_MAX_R2T:
2513 len = sprintf(buf, "%hu\n", session->max_r2t);
2514 break;
2515 case ISCSI_PARAM_IMM_DATA_EN:
2516 len = sprintf(buf, "%d\n", session->imm_data_en);
2517 break;
2518 case ISCSI_PARAM_FIRST_BURST:
2519 len = sprintf(buf, "%u\n", session->first_burst);
2520 break;
2521 case ISCSI_PARAM_MAX_BURST:
2522 len = sprintf(buf, "%u\n", session->max_burst);
2523 break;
2524 case ISCSI_PARAM_PDU_INORDER_EN:
2525 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2526 break;
2527 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2528 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2529 break;
2530 case ISCSI_PARAM_ERL:
2531 len = sprintf(buf, "%d\n", session->erl);
2532 break;
2533 case ISCSI_PARAM_TARGET_NAME:
2534 len = sprintf(buf, "%s\n", session->targetname);
2535 break;
2536 case ISCSI_PARAM_TPGT:
2537 len = sprintf(buf, "%d\n", session->tpgt);
2538 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002539 case ISCSI_PARAM_USERNAME:
2540 len = sprintf(buf, "%s\n", session->username);
2541 break;
2542 case ISCSI_PARAM_USERNAME_IN:
2543 len = sprintf(buf, "%s\n", session->username_in);
2544 break;
2545 case ISCSI_PARAM_PASSWORD:
2546 len = sprintf(buf, "%s\n", session->password);
2547 break;
2548 case ISCSI_PARAM_PASSWORD_IN:
2549 len = sprintf(buf, "%s\n", session->password_in);
2550 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002551 case ISCSI_PARAM_IFACE_NAME:
2552 len = sprintf(buf, "%s\n", session->ifacename);
2553 break;
2554 case ISCSI_PARAM_INITIATOR_NAME:
2555 if (!session->initiatorname)
2556 len = sprintf(buf, "%s\n", "unknown");
2557 else
2558 len = sprintf(buf, "%s\n", session->initiatorname);
2559 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002560 default:
2561 return -ENOSYS;
2562 }
2563
2564 return len;
2565}
2566EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2567
2568int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2569 enum iscsi_param param, char *buf)
2570{
2571 struct iscsi_conn *conn = cls_conn->dd_data;
2572 int len;
2573
2574 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002575 case ISCSI_PARAM_PING_TMO:
2576 len = sprintf(buf, "%u\n", conn->ping_timeout);
2577 break;
2578 case ISCSI_PARAM_RECV_TMO:
2579 len = sprintf(buf, "%u\n", conn->recv_timeout);
2580 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002581 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2582 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2583 break;
2584 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2585 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2586 break;
2587 case ISCSI_PARAM_HDRDGST_EN:
2588 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2589 break;
2590 case ISCSI_PARAM_DATADGST_EN:
2591 len = sprintf(buf, "%d\n", conn->datadgst_en);
2592 break;
2593 case ISCSI_PARAM_IFMARKER_EN:
2594 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2595 break;
2596 case ISCSI_PARAM_OFMARKER_EN:
2597 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2598 break;
2599 case ISCSI_PARAM_EXP_STATSN:
2600 len = sprintf(buf, "%u\n", conn->exp_statsn);
2601 break;
2602 case ISCSI_PARAM_PERSISTENT_PORT:
2603 len = sprintf(buf, "%d\n", conn->persistent_port);
2604 break;
2605 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2606 len = sprintf(buf, "%s\n", conn->persistent_address);
2607 break;
2608 default:
2609 return -ENOSYS;
2610 }
2611
2612 return len;
2613}
2614EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2615
Mike Christie0801c242007-05-30 12:57:12 -05002616int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2617 char *buf)
2618{
Mike Christie75613522008-05-21 15:53:59 -05002619 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002620 int len;
2621
2622 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002623 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002624 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002625 len = sprintf(buf, "%s\n", "default");
2626 else
Mike Christie75613522008-05-21 15:53:59 -05002627 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002628 break;
Mike Christie0801c242007-05-30 12:57:12 -05002629 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002630 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002631 len = sprintf(buf, "%s\n", "default");
2632 else
Mike Christie75613522008-05-21 15:53:59 -05002633 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002634 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002635 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002636 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002637 len = sprintf(buf, "%s\n", "unknown");
2638 else
Mike Christie75613522008-05-21 15:53:59 -05002639 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002640 break;
Mike Christie75613522008-05-21 15:53:59 -05002641 case ISCSI_HOST_PARAM_IPADDRESS:
2642 if (!strlen(ihost->local_address))
2643 len = sprintf(buf, "%s\n", "unknown");
2644 else
2645 len = sprintf(buf, "%s\n",
2646 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002647 break;
Mike Christie0801c242007-05-30 12:57:12 -05002648 default:
2649 return -ENOSYS;
2650 }
2651
2652 return len;
2653}
2654EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2655
2656int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2657 char *buf, int buflen)
2658{
Mike Christie75613522008-05-21 15:53:59 -05002659 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002660
2661 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002662 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002663 if (!ihost->netdev)
2664 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002665 break;
Mike Christie0801c242007-05-30 12:57:12 -05002666 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002667 if (!ihost->hwaddress)
2668 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002669 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002670 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002671 if (!ihost->initiatorname)
2672 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002673 break;
Mike Christie0801c242007-05-30 12:57:12 -05002674 default:
2675 return -ENOSYS;
2676 }
2677
2678 return 0;
2679}
2680EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2681
Mike Christie7996a772006-04-06 21:13:41 -05002682MODULE_AUTHOR("Mike Christie");
2683MODULE_DESCRIPTION("iSCSI library functions");
2684MODULE_LICENSE("GPL");