blob: 57eb3af40e96e2f2057662ad12c451c5fad58335 [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 Christie1b2c7af2009-03-05 14:45:58 -060041static int iscsi_dbg_lib;
42module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
48 do { \
49 if (iscsi_dbg_lib) \
50 iscsi_conn_printk(KERN_INFO, _conn, \
51 "%s " dbg_fmt, \
52 __func__, ##arg); \
53 } while (0);
54
55#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (iscsi_dbg_lib) \
58 iscsi_session_printk(KERN_INFO, _session, \
59 "%s " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
64#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050065
Mike Christie77a23c22007-05-30 12:57:18 -050066static int iscsi_sna_lt(u32 n1, u32 n2)
67{
68 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
69 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
70}
71
72/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
73static int iscsi_sna_lte(u32 n1, u32 n2)
74{
75 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
76 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
77}
78
Mike Christie32ae7632009-03-05 14:46:03 -060079inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
80{
81 struct Scsi_Host *shost = conn->session->host;
82 struct iscsi_host *ihost = shost_priv(shost);
83
84 queue_work(ihost->workq, &conn->xmitwork);
85}
86EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
87
Mike Christie77a23c22007-05-30 12:57:18 -050088void
89iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050090{
91 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
92 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
93
Mike Christie77a23c22007-05-30 12:57:18 -050094 /*
95 * standard specifies this check for when to update expected and
96 * max sequence numbers
97 */
98 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
99 return;
100
101 if (exp_cmdsn != session->exp_cmdsn &&
102 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500103 session->exp_cmdsn = exp_cmdsn;
104
Mike Christie77a23c22007-05-30 12:57:18 -0500105 if (max_cmdsn != session->max_cmdsn &&
106 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
107 session->max_cmdsn = max_cmdsn;
108 /*
109 * if the window closed with IO queued, then kick the
110 * xmit thread
111 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500112 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie052d0142008-05-21 15:54:05 -0500113 !list_empty(&session->leadconn->mgmtqueue)) {
114 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
Mike Christie32ae7632009-03-05 14:46:03 -0600115 iscsi_conn_queue_work(session->leadconn);
Mike Christie052d0142008-05-21 15:54:05 -0500116 }
Mike Christie77a23c22007-05-30 12:57:18 -0500117 }
Mike Christie7996a772006-04-06 21:13:41 -0500118}
Mike Christie77a23c22007-05-30 12:57:18 -0500119EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500120
Mike Christie577577d2008-12-02 00:32:05 -0600121/**
122 * iscsi_prep_data_out_pdu - initialize Data-Out
123 * @task: scsi command task
124 * @r2t: R2T info
125 * @hdr: iscsi data in pdu
126 *
127 * Notes:
128 * Initialize Data-Out within this R2T sequence and finds
129 * proper data_offset within this SCSI command.
130 *
131 * This function is called with connection lock taken.
132 **/
133void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
134 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500135{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500136 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600137 unsigned int left = r2t->data_length - r2t->sent;
138
139 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500140
141 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600142 hdr->ttt = r2t->ttt;
143 hdr->datasn = cpu_to_be32(r2t->datasn);
144 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500145 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600146 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
147 hdr->itt = task->hdr_itt;
148 hdr->exp_statsn = r2t->exp_statsn;
149 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
150 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500151 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600152 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500153 hdr->flags = 0;
154 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600155 hton24(hdr->dlength, left);
156 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500157 hdr->flags = ISCSI_FLAG_CMD_FINAL;
158 }
Mike Christie577577d2008-12-02 00:32:05 -0600159 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500160}
Mike Christie577577d2008-12-02 00:32:05 -0600161EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500162
Mike Christie9c19a7d2008-05-21 15:54:09 -0500163static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600164{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500165 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600166
Mike Christie9c19a7d2008-05-21 15:54:09 -0500167 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600168 WARN_ON(1);
169 return -EINVAL;
170 }
171
172 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500173 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600174 return 0;
175}
176
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500177/*
178 * make an extended cdb AHS
179 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500180static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500181{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500182 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500183 unsigned rlen, pad_len;
184 unsigned short ahslength;
185 struct iscsi_ecdb_ahdr *ecdb_ahdr;
186 int rc;
187
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500189 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
190
191 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
192 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
193
194 pad_len = iscsi_padding(rlen);
195
Mike Christie9c19a7d2008-05-21 15:54:09 -0500196 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500197 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
198 if (rc)
199 return rc;
200
201 if (pad_len)
202 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
203
204 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
205 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
206 ecdb_ahdr->reserved = 0;
207 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
208
Mike Christie1b2c7af2009-03-05 14:45:58 -0600209 ISCSI_DBG_SESSION(task->conn->session,
210 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
211 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
212 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
213 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500214 return 0;
215}
216
Mike Christie9c19a7d2008-05-21 15:54:09 -0500217static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500218{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500219 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500220 struct iscsi_rlength_ahdr *rlen_ahdr;
221 int rc;
222
Mike Christie9c19a7d2008-05-21 15:54:09 -0500223 rlen_ahdr = iscsi_next_hdr(task);
224 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500225 if (rc)
226 return rc;
227
228 rlen_ahdr->ahslength =
229 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
230 sizeof(rlen_ahdr->reserved));
231 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
232 rlen_ahdr->reserved = 0;
233 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
234
Mike Christie1b2c7af2009-03-05 14:45:58 -0600235 ISCSI_DBG_SESSION(task->conn->session,
236 "bidi-in rlen_ahdr->read_length(%d) "
237 "rlen_ahdr->ahslength(%d)\n",
238 be32_to_cpu(rlen_ahdr->read_length),
239 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500240 return 0;
241}
242
Mike Christie7996a772006-04-06 21:13:41 -0500243/**
244 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500245 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500246 *
247 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
248 * fields like dlength or final based on how much data it sends
249 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500250static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500251{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500252 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500253 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500254 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600255 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500256 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600257 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600258 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500259
Mike Christie184b57c2009-05-13 17:57:39 -0500260 if (conn->session->tt->alloc_pdu) {
261 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
262 if (rc)
263 return rc;
264 }
Mike Christie577577d2008-12-02 00:32:05 -0600265 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600266 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600267 memset(hdr, 0, sizeof(*hdr));
268
Mike Christie2ff79d52008-12-02 00:32:14 -0600269 if (session->tt->parse_pdu_itt)
270 hdr->itt = task->hdr_itt = itt;
271 else
272 hdr->itt = task->hdr_itt = build_itt(task->itt,
273 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500274 task->hdr_len = 0;
275 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600276 if (rc)
277 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600278 hdr->opcode = ISCSI_OP_SCSI_CMD;
279 hdr->flags = ISCSI_ATTR_SIMPLE;
280 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600281 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600282 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600283 session->cmdsn++;
284 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500285 cmd_len = sc->cmd_len;
286 if (cmd_len < ISCSI_CDB_SIZE)
287 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
288 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500289 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500290 if (rc)
291 return rc;
292 cmd_len = ISCSI_CDB_SIZE;
293 }
294 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500295
Mike Christie9c19a7d2008-05-21 15:54:09 -0500296 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500297 if (scsi_bidi_cmnd(sc)) {
298 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500299 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500300 if (rc)
301 return rc;
302 }
Mike Christie7996a772006-04-06 21:13:41 -0500303 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500304 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600305 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
306
Boaz Harroshc07d4442008-04-18 10:11:52 -0500307 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500308 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
309 /*
310 * Write counters:
311 *
312 * imm_count bytes to be sent right after
313 * SCSI PDU Header
314 *
315 * unsol_count bytes(as Data-Out) to be sent
316 * without R2T ack right after
317 * immediate data
318 *
Mike Christie577577d2008-12-02 00:32:05 -0600319 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500320 *
321 * pad_count bytes to be sent as zero-padding
322 */
Mike Christie577577d2008-12-02 00:32:05 -0600323 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500324
325 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500326 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500327 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500328 conn->max_xmit_dlength);
329 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500330 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500331 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500332 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500333 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600334 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500335
Mike Christieffd04362006-08-31 18:09:24 -0400336 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600337 r2t->data_length = min(session->first_burst, out_len) -
338 task->imm_count;
339 r2t->data_offset = task->imm_count;
340 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
341 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400342 }
343
Mike Christie577577d2008-12-02 00:32:05 -0600344 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500345 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600346 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500347 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500348 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
349 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500350 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500351
352 if (sc->sc_data_direction == DMA_FROM_DEVICE)
353 hdr->flags |= ISCSI_FLAG_CMD_READ;
354 }
355
Boaz Harrosh004d6532007-12-13 12:43:23 -0600356 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500357 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600358
359 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
360 hdrlength /= ISCSI_PAD_LEN;
361
362 WARN_ON(hdrlength >= 256);
363 hdr->hlength = hdrlength & 0xFF;
364
Mike Christie577577d2008-12-02 00:32:05 -0600365 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500366 return -EIO;
367
Mike Christie9c19a7d2008-05-21 15:54:09 -0500368 task->state = ISCSI_TASK_RUNNING;
Mike Christie77a23c22007-05-30 12:57:18 -0500369
Olaf Kircha8ac6312007-12-13 12:43:35 -0600370 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600371 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
372 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
373 scsi_bidi_cmnd(sc) ? "bidirectional" :
374 sc->sc_data_direction == DMA_TO_DEVICE ?
375 "write" : "read", conn->id, sc, sc->cmnd[0],
376 task->itt, scsi_bufflen(sc),
377 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
378 session->cmdsn,
379 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600380 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500381}
Mike Christie7996a772006-04-06 21:13:41 -0500382
383/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500384 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500385 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500386 *
387 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500388 * This function returns the scsi command to scsi-ml or cleans
389 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500390 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500391static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500392{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500393 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600394 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500395 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500396
Mike Christie577577d2008-12-02 00:32:05 -0600397 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500398 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500399 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500400
Mike Christie3e5c28a2008-05-21 15:54:06 -0500401 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500402 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500403 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500404 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500405 return;
406
Mike Christie9c19a7d2008-05-21 15:54:09 -0500407 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500408
Mike Christie3e5c28a2008-05-21 15:54:06 -0500409 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500410 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500411 /* SCSI eh reuses commands to verify us */
412 sc->SCp.ptr = NULL;
413 /*
414 * queue command may call this to free the task, but
415 * not have setup the sc callback
416 */
417 if (sc->scsi_done)
418 sc->scsi_done(sc);
419 }
Mike Christie7996a772006-04-06 21:13:41 -0500420}
421
Mike Christie913e5bf2008-05-21 15:54:18 -0500422void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400423{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500424 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400425}
Mike Christie913e5bf2008-05-21 15:54:18 -0500426EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400427
Mike Christie9c19a7d2008-05-21 15:54:09 -0500428static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400429{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500430 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500431 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400432}
433
Mike Christie9c19a7d2008-05-21 15:54:09 -0500434void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500435{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500436 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500437
438 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500439 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500440 spin_unlock_bh(&session->lock);
441}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500442EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500443
Mike Christie3bbaaad2009-05-13 17:57:46 -0500444/**
445 * iscsi_complete_task - finish a task
446 * @task: iscsi cmd task
447 *
448 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600449 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500450static void iscsi_complete_task(struct iscsi_task *task)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600451{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500452 struct iscsi_conn *conn = task->conn;
453
454 if (task->state == ISCSI_TASK_COMPLETED)
455 return;
456 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
457
458 task->state = ISCSI_TASK_COMPLETED;
459
460 if (!list_empty(&task->running))
461 list_del_init(&task->running);
462
463 if (conn->task == task)
464 conn->task = NULL;
465
466 if (conn->ping_task == task)
467 conn->ping_task = NULL;
468
469 /* release get from queueing */
470 __iscsi_put_task(task);
471}
472
473/*
474 * session lock must be held and if not called for a task that is
475 * still pending or from the xmit thread, then xmit thread must
476 * be suspended.
477 */
478static void fail_scsi_task(struct iscsi_task *task, int err)
479{
480 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600481 struct scsi_cmnd *sc;
482
Mike Christie3bbaaad2009-05-13 17:57:46 -0500483 /*
484 * if a command completes and we get a successful tmf response
485 * we will hit this because the scsi eh abort code does not take
486 * a ref to the task.
487 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500488 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600489 if (!sc)
490 return;
491
Mike Christie9c19a7d2008-05-21 15:54:09 -0500492 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600493 /*
494 * cmd never made it to the xmit thread, so we should not count
495 * the cmd in the sequencing
496 */
497 conn->session->queued_cmdsn--;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600498
499 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500500 if (!scsi_bidi_cmnd(sc))
501 scsi_set_resid(sc, scsi_bufflen(sc));
502 else {
503 scsi_out(sc)->resid = scsi_out(sc)->length;
504 scsi_in(sc)->resid = scsi_in(sc)->length;
505 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500506
Mike Christie3bbaaad2009-05-13 17:57:46 -0500507 iscsi_complete_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600508}
509
Mike Christie3e5c28a2008-05-21 15:54:06 -0500510static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500511 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500512{
513 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600514 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500515 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
516
517 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
518 return -ENOTCONN;
519
520 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
521 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
522 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
523 /*
524 * pre-format CmdSN for outgoing PDU.
525 */
526 nop->cmdsn = cpu_to_be32(session->cmdsn);
527 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500528 /*
529 * TODO: We always use immediate, so we never hit this.
530 * If we start to send tmfs or nops as non-immediate then
531 * we should start checking the cmdsn numbers for mgmt tasks.
532 */
533 if (conn->c_stage == ISCSI_CONN_STARTED &&
534 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
535 session->queued_cmdsn++;
536 session->cmdsn++;
537 }
538 }
539
Mike Christieae15f802008-12-02 00:32:15 -0600540 if (session->tt->init_task && session->tt->init_task(task))
541 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500542
543 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
544 session->state = ISCSI_STATE_LOGGING_OUT;
545
Mike Christie577577d2008-12-02 00:32:05 -0600546 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600547 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
548 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
549 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500550 return 0;
551}
552
Mike Christie9c19a7d2008-05-21 15:54:09 -0500553static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600554__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
555 char *data, uint32_t data_size)
556{
557 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500558 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600559 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600560
561 if (session->state == ISCSI_STATE_TERMINATE)
562 return NULL;
563
564 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
565 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
566 /*
567 * Login and Text are sent serially, in
568 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500569 * Same task can be used. Same ITT must be used.
570 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600571 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500572 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600573 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500574 if (session->state != ISCSI_STATE_LOGGED_IN)
575 return NULL;
576
Mike Christief6d51802007-12-13 12:43:30 -0600577 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
578 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
579
Mike Christie3e5c28a2008-05-21 15:54:06 -0500580 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500581 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600582 return NULL;
583 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500584 /*
585 * released in complete pdu for task we expect a response for, and
586 * released by the lld when it has transmitted the task for
587 * pdus we do not expect a response for.
588 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500589 atomic_set(&task->refcount, 1);
590 task->conn = conn;
591 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500592 INIT_LIST_HEAD(&task->running);
593 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600594
595 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500596 memcpy(task->data, data, data_size);
597 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600598 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500599 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600600
Mike Christie184b57c2009-05-13 17:57:39 -0500601 if (conn->session->tt->alloc_pdu) {
602 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
603 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
604 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500605 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500606 }
Mike Christie577577d2008-12-02 00:32:05 -0600607 }
Mike Christie184b57c2009-05-13 17:57:39 -0500608
Mike Christie262ef632008-12-02 00:32:13 -0600609 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600610 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500611 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600612
613 if (hdr->itt != RESERVED_ITT) {
614 if (session->tt->parse_pdu_itt)
615 task->hdr->itt = itt;
616 else
617 task->hdr->itt = build_itt(task->itt,
618 task->conn->session->age);
619 }
620
Mike Christie052d0142008-05-21 15:54:05 -0500621 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -0600622 if (iscsi_prep_mgmt_task(conn, task))
623 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500624
Mike Christie9c19a7d2008-05-21 15:54:09 -0500625 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600626 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500627 } else {
628 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600629 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500630 }
Mike Christie052d0142008-05-21 15:54:05 -0500631
Mike Christie9c19a7d2008-05-21 15:54:09 -0500632 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600633
634free_task:
635 __iscsi_put_task(task);
636 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600637}
638
639int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
640 char *data, uint32_t data_size)
641{
642 struct iscsi_conn *conn = cls_conn->dd_data;
643 struct iscsi_session *session = conn->session;
644 int err = 0;
645
646 spin_lock_bh(&session->lock);
647 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
648 err = -EPERM;
649 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600650 return err;
651}
652EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
653
Mike Christie7996a772006-04-06 21:13:41 -0500654/**
655 * iscsi_cmd_rsp - SCSI Command Response processing
656 * @conn: iscsi connection
657 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500658 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500659 * @data: cmd data buffer
660 * @datalen: len of buffer
661 *
662 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500663 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500664 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500665static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500666 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500667 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500668{
Mike Christie7996a772006-04-06 21:13:41 -0500669 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
670 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500671 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500672
Mike Christie77a23c22007-05-30 12:57:18 -0500673 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500674 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
675
676 sc->result = (DID_OK << 16) | rhdr->cmd_status;
677
678 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
679 sc->result = DID_ERROR << 16;
680 goto out;
681 }
682
683 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600684 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500685
686 if (datalen < 2) {
687invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600688 iscsi_conn_printk(KERN_ERR, conn,
689 "Got CHECK_CONDITION but invalid data "
690 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500691 sc->result = DID_BAD_TARGET << 16;
692 goto out;
693 }
694
Harvey Harrison8f3339912008-05-21 15:54:20 -0500695 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500696 if (datalen < senselen)
697 goto invalid_datalen;
698
699 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600700 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600701 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
702 min_t(uint16_t, senselen,
703 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500704 }
705
Boaz Harroshc07d4442008-04-18 10:11:52 -0500706 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
707 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
708 int res_count = be32_to_cpu(rhdr->bi_residual_count);
709
710 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
711 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
712 res_count <= scsi_in(sc)->length))
713 scsi_in(sc)->resid = res_count;
714 else
715 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
716 }
717
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600718 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
719 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500720 int res_count = be32_to_cpu(rhdr->residual_count);
721
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600722 if (res_count > 0 &&
723 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
724 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500725 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900726 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500727 else
728 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500729 }
Mike Christie7996a772006-04-06 21:13:41 -0500730out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500731 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600732 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500733 conn->scsirsp_pdus_cnt++;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500734 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500735}
736
Mike Christie1d9edf02008-09-24 11:46:09 -0500737/**
738 * iscsi_data_in_rsp - SCSI Data-In Response processing
739 * @conn: iscsi connection
740 * @hdr: iscsi pdu
741 * @task: scsi command task
742 **/
743static void
744iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
745 struct iscsi_task *task)
746{
747 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
748 struct scsi_cmnd *sc = task->sc;
749
750 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
751 return;
752
Mike Christieedbc9aa052009-05-13 17:57:42 -0500753 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500754 sc->result = (DID_OK << 16) | rhdr->cmd_status;
755 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
756 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
757 ISCSI_FLAG_DATA_OVERFLOW)) {
758 int res_count = be32_to_cpu(rhdr->residual_count);
759
760 if (res_count > 0 &&
761 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
762 res_count <= scsi_in(sc)->length))
763 scsi_in(sc)->resid = res_count;
764 else
765 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
766 }
767
Mike Christie3bbaaad2009-05-13 17:57:46 -0500768 ISCSI_DBG_SESSION(conn->session, "data in with status done "
769 "[sc %p res %d itt 0x%x]\n",
770 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500771 conn->scsirsp_pdus_cnt++;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500772 iscsi_complete_task(task);
Mike Christie1d9edf02008-09-24 11:46:09 -0500773}
774
Mike Christie7ea8b822006-07-24 15:47:22 -0500775static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
776{
777 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
778
779 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
780 conn->tmfrsp_pdus_cnt++;
781
Mike Christie843c0a82007-12-13 12:43:20 -0600782 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500783 return;
784
785 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600786 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500787 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600788 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500789 else
Mike Christie843c0a82007-12-13 12:43:20 -0600790 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500791 wake_up(&conn->ehwait);
792}
793
Mike Christief6d51802007-12-13 12:43:30 -0600794static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
795{
796 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500797 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600798
Mike Christie9c19a7d2008-05-21 15:54:09 -0500799 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600800 return;
801
802 memset(&hdr, 0, sizeof(struct iscsi_nopout));
803 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
804 hdr.flags = ISCSI_FLAG_CMD_FINAL;
805
806 if (rhdr) {
807 memcpy(hdr.lun, rhdr->lun, 8);
808 hdr.ttt = rhdr->ttt;
809 hdr.itt = RESERVED_ITT;
810 } else
811 hdr.ttt = RESERVED_ITT;
812
Mike Christie9c19a7d2008-05-21 15:54:09 -0500813 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
814 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600815 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600816 else if (!rhdr) {
817 /* only track our nops */
818 conn->ping_task = task;
819 conn->last_ping = jiffies;
820 }
Mike Christief6d51802007-12-13 12:43:30 -0600821}
822
Mike Christie62f38302006-08-31 18:09:27 -0400823static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
824 char *data, int datalen)
825{
826 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
827 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400828
829 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
830
831 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
832 if (ntoh24(reject->dlength) > datalen)
833 return ISCSI_ERR_PROTO;
834
835 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
836 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600837 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600838 "pdu (op 0x%x) rejected "
839 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600840 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400841 }
842 }
843 return 0;
844}
845
Mike Christie7996a772006-04-06 21:13:41 -0500846/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500847 * iscsi_itt_to_task - look up task by itt
848 * @conn: iscsi connection
849 * @itt: itt
850 *
851 * This should be used for mgmt tasks like login and nops, or if
852 * the LDD's itt space does not include the session age.
853 *
854 * The session lock must be held.
855 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500856struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500857{
858 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600859 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500860
861 if (itt == RESERVED_ITT)
862 return NULL;
863
Mike Christie262ef632008-12-02 00:32:13 -0600864 if (session->tt->parse_pdu_itt)
865 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
866 else
867 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500868 if (i >= session->cmds_max)
869 return NULL;
870
871 return session->cmds[i];
872}
Mike Christie8f9256c2009-05-13 17:57:41 -0500873EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500874
875/**
Mike Christie7996a772006-04-06 21:13:41 -0500876 * __iscsi_complete_pdu - complete pdu
877 * @conn: iscsi conn
878 * @hdr: iscsi header
879 * @data: data buffer
880 * @datalen: len of data buffer
881 *
882 * Completes pdu processing by freeing any resources allocated at
883 * queuecommand or send generic. session lock must be held and verify
884 * itt must have been called.
885 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500886int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
887 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500888{
889 struct iscsi_session *session = conn->session;
890 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500891 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500892 uint32_t itt;
893
Mike Christief6d51802007-12-13 12:43:30 -0600894 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500895 rc = iscsi_verify_itt(conn, hdr->itt);
896 if (rc)
897 return rc;
898
Al Virob4377352007-02-09 16:39:40 +0000899 if (hdr->itt != RESERVED_ITT)
900 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500901 else
Al Virob4377352007-02-09 16:39:40 +0000902 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500903
Mike Christie1b2c7af2009-03-05 14:45:58 -0600904 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
905 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500906
Mike Christie3e5c28a2008-05-21 15:54:06 -0500907 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500908 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400909
Mike Christie7996a772006-04-06 21:13:41 -0500910 switch(opcode) {
911 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500912 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500913 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500914 break;
915 }
916
Al Virob4377352007-02-09 16:39:40 +0000917 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500918 break;
919
Mike Christief6d51802007-12-13 12:43:30 -0600920 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500921 break;
922 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400923 rc = iscsi_handle_reject(conn, hdr, data, datalen);
924 break;
Mike Christie7996a772006-04-06 21:13:41 -0500925 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500926 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400927 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
928 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500929 break;
930 default:
931 rc = ISCSI_ERR_BAD_OPCODE;
932 break;
933 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500934 goto out;
935 }
Mike Christie7996a772006-04-06 21:13:41 -0500936
Mike Christie3e5c28a2008-05-21 15:54:06 -0500937 switch(opcode) {
938 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500939 case ISCSI_OP_SCSI_DATA_IN:
940 task = iscsi_itt_to_ctask(conn, hdr->itt);
941 if (!task)
942 return ISCSI_ERR_BAD_ITT;
943 break;
944 case ISCSI_OP_R2T:
945 /*
946 * LLD handles R2Ts if they need to.
947 */
948 return 0;
949 case ISCSI_OP_LOGOUT_RSP:
950 case ISCSI_OP_LOGIN_RSP:
951 case ISCSI_OP_TEXT_RSP:
952 case ISCSI_OP_SCSI_TMFUNC_RSP:
953 case ISCSI_OP_NOOP_IN:
954 task = iscsi_itt_to_task(conn, hdr->itt);
955 if (!task)
956 return ISCSI_ERR_BAD_ITT;
957 break;
958 default:
959 return ISCSI_ERR_BAD_OPCODE;
960 }
961
962 switch(opcode) {
963 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500964 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500965 break;
966 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500967 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500968 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500969 case ISCSI_OP_LOGOUT_RSP:
970 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
971 if (datalen) {
972 rc = ISCSI_ERR_PROTO;
973 break;
974 }
975 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
976 goto recv_pdu;
977 case ISCSI_OP_LOGIN_RSP:
978 case ISCSI_OP_TEXT_RSP:
979 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
980 /*
981 * login related PDU's exp_statsn is handled in
982 * userspace
983 */
984 goto recv_pdu;
985 case ISCSI_OP_SCSI_TMFUNC_RSP:
986 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
987 if (datalen) {
988 rc = ISCSI_ERR_PROTO;
989 break;
990 }
991
992 iscsi_tmf_rsp(conn, hdr);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500993 iscsi_complete_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500994 break;
995 case ISCSI_OP_NOOP_IN:
996 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
997 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
998 rc = ISCSI_ERR_PROTO;
999 break;
1000 }
1001 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1002
Mike Christie9c19a7d2008-05-21 15:54:09 -05001003 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -05001004 /*
1005 * If this is not in response to one of our
1006 * nops then it must be from userspace.
1007 */
1008 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001009
1010 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001011 iscsi_complete_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001012 break;
1013 default:
1014 rc = ISCSI_ERR_BAD_OPCODE;
1015 break;
1016 }
1017
1018out:
1019 return rc;
1020recv_pdu:
1021 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1022 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001023 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05001024 return rc;
1025}
Mike Christie913e5bf2008-05-21 15:54:18 -05001026EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001027
1028int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1029 char *data, int datalen)
1030{
1031 int rc;
1032
1033 spin_lock(&conn->session->lock);
1034 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1035 spin_unlock(&conn->session->lock);
1036 return rc;
1037}
1038EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1039
Mike Christie0af967f2008-05-21 15:54:04 -05001040int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001041{
1042 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001043 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001044
Mike Christie0af967f2008-05-21 15:54:04 -05001045 if (itt == RESERVED_ITT)
1046 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001047
Mike Christie262ef632008-12-02 00:32:13 -06001048 if (session->tt->parse_pdu_itt)
1049 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1050 else {
1051 i = get_itt(itt);
1052 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1053 }
1054
1055 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001056 iscsi_conn_printk(KERN_ERR, conn,
1057 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001058 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001059 return ISCSI_ERR_BAD_ITT;
1060 }
Mike Christie7996a772006-04-06 21:13:41 -05001061
Mike Christie3e5c28a2008-05-21 15:54:06 -05001062 if (i >= session->cmds_max) {
1063 iscsi_conn_printk(KERN_ERR, conn,
1064 "received invalid itt index %u (max cmds "
1065 "%u.\n", i, session->cmds_max);
1066 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001067 }
Mike Christie7996a772006-04-06 21:13:41 -05001068 return 0;
1069}
1070EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1071
Mike Christie913e5bf2008-05-21 15:54:18 -05001072/**
1073 * iscsi_itt_to_ctask - look up ctask by itt
1074 * @conn: iscsi connection
1075 * @itt: itt
1076 *
1077 * This should be used for cmd tasks.
1078 *
1079 * The session lock must be held.
1080 */
1081struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001082{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001083 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001084
1085 if (iscsi_verify_itt(conn, itt))
1086 return NULL;
1087
Mike Christie913e5bf2008-05-21 15:54:18 -05001088 task = iscsi_itt_to_task(conn, itt);
1089 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001090 return NULL;
1091
Mike Christie913e5bf2008-05-21 15:54:18 -05001092 if (task->sc->SCp.phase != conn->session->age) {
1093 iscsi_session_printk(KERN_ERR, conn->session,
1094 "task's session age %d, expected %d\n",
1095 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001096 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001097 }
Mike Christie0af967f2008-05-21 15:54:04 -05001098
Mike Christie9c19a7d2008-05-21 15:54:09 -05001099 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001100}
1101EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1102
Mike Christie40a06e72009-03-05 14:46:05 -06001103void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001104 enum iscsi_err err)
1105{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001106 struct iscsi_conn *conn;
1107 struct device *dev;
1108 unsigned long flags;
1109
1110 spin_lock_irqsave(&session->lock, flags);
1111 conn = session->leadconn;
1112 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1113 spin_unlock_irqrestore(&session->lock, flags);
1114 return;
1115 }
1116
1117 dev = get_device(&conn->cls_conn->dev);
1118 spin_unlock_irqrestore(&session->lock, flags);
1119 if (!dev)
1120 return;
1121 /*
1122 * if the host is being removed bypass the connection
1123 * recovery initialization because we are going to kill
1124 * the session.
1125 */
1126 if (err == ISCSI_ERR_INVALID_HOST)
1127 iscsi_conn_error_event(conn->cls_conn, err);
1128 else
1129 iscsi_conn_failure(conn, err);
1130 put_device(dev);
1131}
1132EXPORT_SYMBOL_GPL(iscsi_session_failure);
1133
Mike Christie7996a772006-04-06 21:13:41 -05001134void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1135{
1136 struct iscsi_session *session = conn->session;
1137 unsigned long flags;
1138
1139 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001140 if (session->state == ISCSI_STATE_FAILED) {
1141 spin_unlock_irqrestore(&session->lock, flags);
1142 return;
1143 }
1144
Mike Christie67a61112006-05-30 00:37:20 -05001145 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001146 session->state = ISCSI_STATE_FAILED;
1147 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001148
Mike Christie7996a772006-04-06 21:13:41 -05001149 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1150 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001151 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001152}
1153EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1154
Mike Christie77a23c22007-05-30 12:57:18 -05001155static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1156{
1157 struct iscsi_session *session = conn->session;
1158
1159 /*
1160 * Check for iSCSI window and take care of CmdSN wrap-around
1161 */
Mike Christiee0726402007-07-26 12:46:48 -05001162 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001163 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1164 "%u MaxCmdSN %u CmdSN %u/%u\n",
1165 session->exp_cmdsn, session->max_cmdsn,
1166 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001167 return -ENOSPC;
1168 }
1169 return 0;
1170}
1171
Mike Christie9c19a7d2008-05-21 15:54:09 -05001172static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001173{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001174 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001175 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001176
Mike Christie9c19a7d2008-05-21 15:54:09 -05001177 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001178 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001179 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001180 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001181 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001182 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001183 /* done with this task */
1184 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001185 return rc;
1186}
1187
Mike Christie7996a772006-04-06 21:13:41 -05001188/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001189 * iscsi_requeue_task - requeue task to run from session workqueue
1190 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001191 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001192 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001193 * this. The session lock must be held. This should only be called
1194 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001195 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001196void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001197{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001198 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001199
Mike Christie3bbaaad2009-05-13 17:57:46 -05001200 /*
1201 * this may be on the requeue list already if the xmit_task callout
1202 * is handling the r2ts while we are adding new ones
1203 */
1204 if (list_empty(&task->running))
1205 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001206 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001207}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001208EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001209
1210/**
Mike Christie7996a772006-04-06 21:13:41 -05001211 * iscsi_data_xmit - xmit any command into the scheduled connection
1212 * @conn: iscsi connection
1213 *
1214 * Notes:
1215 * The function can return -EAGAIN in which case the caller must
1216 * re-schedule it again later or recover. '0' return code means
1217 * successful xmit.
1218 **/
1219static int iscsi_data_xmit(struct iscsi_conn *conn)
1220{
Mike Christie3219e522006-05-30 00:37:28 -05001221 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001222
Mike Christie77a23c22007-05-30 12:57:18 -05001223 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001224 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001225 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001226 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001227 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001228 }
Mike Christie7996a772006-04-06 21:13:41 -05001229
Mike Christie9c19a7d2008-05-21 15:54:09 -05001230 if (conn->task) {
1231 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001232 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001233 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001234 }
1235
Mike Christie77a23c22007-05-30 12:57:18 -05001236 /*
1237 * process mgmt pdus like nops before commands since we should
1238 * only have one nop-out as a ping from us and targets should not
1239 * overflow us with nop-ins
1240 */
1241check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001242 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001243 conn->task = list_entry(conn->mgmtqueue.next,
1244 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001245 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001246 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1247 __iscsi_put_task(conn->task);
1248 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001249 continue;
1250 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001251 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001252 if (rc)
1253 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001254 }
1255
Mike Christie843c0a82007-12-13 12:43:20 -06001256 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001257 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001258 if (conn->tmf_state == TMF_QUEUED)
1259 break;
1260
Mike Christie3bbaaad2009-05-13 17:57:46 -05001261 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001262 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001263 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001264 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001265 fail_scsi_task(conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001266 continue;
1267 }
Mike Christie577577d2008-12-02 00:32:05 -06001268 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1269 if (rc) {
1270 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001271 list_add_tail(&conn->task->running,
1272 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001273 conn->task = NULL;
1274 goto again;
1275 } else
Mike Christie3bbaaad2009-05-13 17:57:46 -05001276 fail_scsi_task(conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001277 continue;
1278 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001279 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001280 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001281 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001282 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001283 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001284 * we need to check the mgmt queue for nops that need to
1285 * be sent to aviod starvation
1286 */
Mike Christie843c0a82007-12-13 12:43:20 -06001287 if (!list_empty(&conn->mgmtqueue))
1288 goto check_mgmt;
1289 }
1290
1291 while (!list_empty(&conn->requeue)) {
1292 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1293 break;
1294
Mike Christieb3a7ea82007-12-13 12:43:26 -06001295 /*
1296 * we always do fastlogout - conn stop code will clean up.
1297 */
1298 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1299 break;
1300
Mike Christie9c19a7d2008-05-21 15:54:09 -05001301 conn->task = list_entry(conn->requeue.next,
1302 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001303 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001304 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001305 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001306 if (rc)
1307 goto again;
1308 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001309 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001310 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001311 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001312 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001313
1314again:
1315 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001316 rc = -ENODATA;
1317 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001318 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001319}
1320
David Howellsc4028952006-11-22 14:57:56 +00001321static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001322{
David Howellsc4028952006-11-22 14:57:56 +00001323 struct iscsi_conn *conn =
1324 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001325 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001326 /*
1327 * serialize Xmit worker on a per-connection basis.
1328 */
Mike Christie3219e522006-05-30 00:37:28 -05001329 do {
1330 rc = iscsi_data_xmit(conn);
1331 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001332}
1333
Mike Christie577577d2008-12-02 00:32:05 -06001334static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1335 struct scsi_cmnd *sc)
1336{
1337 struct iscsi_task *task;
1338
1339 if (!__kfifo_get(conn->session->cmdpool.queue,
1340 (void *) &task, sizeof(void *)))
1341 return NULL;
1342
1343 sc->SCp.phase = conn->session->age;
1344 sc->SCp.ptr = (char *) task;
1345
1346 atomic_set(&task->refcount, 1);
1347 task->state = ISCSI_TASK_PENDING;
1348 task->conn = conn;
1349 task->sc = sc;
1350 INIT_LIST_HEAD(&task->running);
1351 return task;
1352}
1353
Mike Christie7996a772006-04-06 21:13:41 -05001354enum {
1355 FAILURE_BAD_HOST = 1,
1356 FAILURE_SESSION_FAILED,
1357 FAILURE_SESSION_FREED,
1358 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001359 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001360 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001361 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001362 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001363 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001364 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001365};
1366
1367int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1368{
Mike Christie75613522008-05-21 15:53:59 -05001369 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001370 struct Scsi_Host *host;
1371 int reason = 0;
1372 struct iscsi_session *session;
1373 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001374 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001375
1376 sc->scsi_done = done;
1377 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001378 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001379
1380 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001381 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001382
Mike Christie75613522008-05-21 15:53:59 -05001383 cls_session = starget_to_session(scsi_target(sc->device));
1384 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001385 spin_lock(&session->lock);
1386
Mike Christie75613522008-05-21 15:53:59 -05001387 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001388 if (reason) {
1389 sc->result = reason;
1390 goto fault;
1391 }
1392
Mike Christie301e0f72009-05-13 17:57:47 -05001393 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001394 /*
1395 * to handle the race between when we set the recovery state
1396 * and block the session we requeue here (commands could
1397 * be entering our queuecommand while a block is starting
1398 * up because the block code is not locked)
1399 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001400 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001401 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001402 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001403 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001404 sc->result = DID_IMM_RETRY << 16;
1405 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001406 case ISCSI_STATE_LOGGING_OUT:
1407 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001408 sc->result = DID_IMM_RETRY << 16;
1409 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001410 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001411 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001412 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001413 break;
1414 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001415 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001416 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001417 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001418 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001419 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001420 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001421 }
Mike Christie7996a772006-04-06 21:13:41 -05001422 goto fault;
1423 }
1424
Mike Christie7996a772006-04-06 21:13:41 -05001425 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001426 if (!conn) {
1427 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001428 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001429 goto fault;
1430 }
Mike Christie7996a772006-04-06 21:13:41 -05001431
Mike Christie77a23c22007-05-30 12:57:18 -05001432 if (iscsi_check_cmdsn_window_closed(conn)) {
1433 reason = FAILURE_WINDOW_CLOSED;
1434 goto reject;
1435 }
1436
Mike Christie577577d2008-12-02 00:32:05 -06001437 task = iscsi_alloc_task(conn, sc);
1438 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001439 reason = FAILURE_OOM;
1440 goto reject;
1441 }
Mike Christie7996a772006-04-06 21:13:41 -05001442
Mike Christie052d0142008-05-21 15:54:05 -05001443 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -06001444 reason = iscsi_prep_scsi_cmd_pdu(task);
1445 if (reason) {
1446 if (reason == -ENOMEM) {
1447 reason = FAILURE_OOM;
1448 goto prepd_reject;
1449 } else {
1450 sc->result = DID_ABORT << 16;
1451 goto prepd_fault;
1452 }
Mike Christie052d0142008-05-21 15:54:05 -05001453 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001454 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001455 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001456 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001457 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001458 } else {
1459 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001460 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001461 }
Mike Christie052d0142008-05-21 15:54:05 -05001462
1463 session->queued_cmdsn++;
1464 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001465 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001466 return 0;
1467
Mike Christie577577d2008-12-02 00:32:05 -06001468prepd_reject:
1469 sc->scsi_done = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001470 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05001471reject:
1472 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001473 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1474 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001475 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001476 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001477
Mike Christie577577d2008-12-02 00:32:05 -06001478prepd_fault:
1479 sc->scsi_done = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001480 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05001481fault:
1482 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001483 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1484 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001485 if (!scsi_bidi_cmnd(sc))
1486 scsi_set_resid(sc, scsi_bufflen(sc));
1487 else {
1488 scsi_out(sc)->resid = scsi_out(sc)->length;
1489 scsi_in(sc)->resid = scsi_in(sc)->length;
1490 }
Mike Christie052d0142008-05-21 15:54:05 -05001491 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001492 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001493 return 0;
1494}
1495EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1496
1497int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1498{
Mike Christie7996a772006-04-06 21:13:41 -05001499 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1500 return sdev->queue_depth;
1501}
1502EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1503
Mike Christie6b5d6c42009-04-21 15:32:32 -05001504int iscsi_target_alloc(struct scsi_target *starget)
1505{
1506 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1507 struct iscsi_session *session = cls_session->dd_data;
1508
1509 starget->can_queue = session->scsi_cmds_max;
1510 return 0;
1511}
1512EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1513
Mike Christie7996a772006-04-06 21:13:41 -05001514void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1515{
Mike Christie75613522008-05-21 15:53:59 -05001516 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001517
1518 spin_lock_bh(&session->lock);
1519 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001520 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001521 if (session->leadconn)
1522 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001523 }
1524 spin_unlock_bh(&session->lock);
1525}
1526EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1527
Mike Christie8e124522008-09-24 11:46:12 -05001528int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001529{
Mike Christie75613522008-05-21 15:53:59 -05001530 struct iscsi_cls_session *cls_session;
1531 struct iscsi_session *session;
1532 struct iscsi_conn *conn;
1533
1534 cls_session = starget_to_session(scsi_target(sc->device));
1535 session = cls_session->dd_data;
1536 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001537
Mike Christiebc436b22007-12-13 12:43:28 -06001538 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001539 spin_lock_bh(&session->lock);
1540 if (session->state == ISCSI_STATE_TERMINATE) {
1541failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001542 iscsi_session_printk(KERN_INFO, session,
1543 "failing target reset: Could not log "
1544 "back into target [age %d]\n",
1545 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001546 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001547 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001548 return FAILED;
1549 }
1550
Mike Christie7996a772006-04-06 21:13:41 -05001551 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001552 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001553 /*
1554 * we drop the lock here but the leadconn cannot be destoyed while
1555 * we are in the scsi eh
1556 */
Mike Christie843c0a82007-12-13 12:43:20 -06001557 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001558
Mike Christie1b2c7af2009-03-05 14:45:58 -06001559 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001560 wait_event_interruptible(conn->ehwait,
1561 session->state == ISCSI_STATE_TERMINATE ||
1562 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001563 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001564 if (signal_pending(current))
1565 flush_signals(current);
1566
Mike Christiebc436b22007-12-13 12:43:28 -06001567 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001568 spin_lock_bh(&session->lock);
1569 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001570 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001571 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001572 else
1573 goto failed;
1574 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001575 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001576 return SUCCESS;
1577}
Mike Christie8e124522008-09-24 11:46:12 -05001578EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001579
Mike Christie843c0a82007-12-13 12:43:20 -06001580static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001581{
Mike Christie843c0a82007-12-13 12:43:20 -06001582 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001583 struct iscsi_session *session = conn->session;
1584
1585 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001586 if (conn->tmf_state == TMF_QUEUED) {
1587 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001588 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001589 /* unblock eh_abort() */
1590 wake_up(&conn->ehwait);
1591 }
1592 spin_unlock(&session->lock);
1593}
1594
Mike Christie9c19a7d2008-05-21 15:54:09 -05001595static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001596 struct iscsi_tm *hdr, int age,
1597 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001598{
Mike Christie7996a772006-04-06 21:13:41 -05001599 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001600 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001601
Mike Christie9c19a7d2008-05-21 15:54:09 -05001602 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001603 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001604 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001605 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001606 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001607 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001608 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001609 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001610 }
Mike Christie843c0a82007-12-13 12:43:20 -06001611 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001612 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001613 conn->tmf_timer.function = iscsi_tmf_timedout;
1614 conn->tmf_timer.data = (unsigned long)conn;
1615 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001616 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001617
Mike Christie7996a772006-04-06 21:13:41 -05001618 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001619 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001620
1621 /*
1622 * block eh thread until:
1623 *
Mike Christie843c0a82007-12-13 12:43:20 -06001624 * 1) tmf response
1625 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001626 * 3) session is terminated or restarted or userspace has
1627 * given up on recovery
1628 */
Mike Christie843c0a82007-12-13 12:43:20 -06001629 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001630 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001631 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001632 if (signal_pending(current))
1633 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001634 del_timer_sync(&conn->tmf_timer);
1635
Mike Christie6724add2007-08-15 01:38:30 -05001636 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001637 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001638 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001639 if (age != session->age ||
1640 session->state != ISCSI_STATE_LOGGED_IN)
1641 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001642 return 0;
1643}
1644
1645/*
Mike Christie843c0a82007-12-13 12:43:20 -06001646 * Fail commands. session lock held and recv side suspended and xmit
1647 * thread flushed
1648 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001649static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1650 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001651{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001652 struct iscsi_task *task;
1653 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001654
Mike Christie3bbaaad2009-05-13 17:57:46 -05001655 for (i = 0; i < conn->session->cmds_max; i++) {
1656 task = conn->session->cmds[i];
1657 if (!task->sc || task->state == ISCSI_TASK_FREE)
1658 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001659
Mike Christie3bbaaad2009-05-13 17:57:46 -05001660 if (lun != -1 && lun != task->sc->device->lun)
1661 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001662
Mike Christie3bbaaad2009-05-13 17:57:46 -05001663 ISCSI_DBG_SESSION(conn->session,
1664 "failing sc %p itt 0x%x state %d\n",
1665 task->sc, task->itt, task->state);
1666 fail_scsi_task(task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001667 }
1668}
1669
Mike Christieb40977d2008-05-21 15:54:03 -05001670void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001671{
Mike Christie32ae7632009-03-05 14:46:03 -06001672 struct Scsi_Host *shost = conn->session->host;
1673 struct iscsi_host *ihost = shost_priv(shost);
1674
Mike Christie6724add2007-08-15 01:38:30 -05001675 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001676 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
Mike Christie32ae7632009-03-05 14:46:03 -06001677 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001678}
Mike Christieb40977d2008-05-21 15:54:03 -05001679EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001680
1681static void iscsi_start_tx(struct iscsi_conn *conn)
1682{
1683 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001684 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
Mike Christie32ae7632009-03-05 14:46:03 -06001685 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001686}
1687
Mike Christie4c48a822009-05-13 17:57:45 -05001688/*
1689 * We want to make sure a ping is in flight. It has timed out.
1690 * And we are not busy processing a pdu that is making
1691 * progress but got started before the ping and is taking a while
1692 * to complete so the ping is just stuck behind it in a queue.
1693 */
1694static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1695{
1696 if (conn->ping_task &&
1697 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1698 (conn->ping_timeout * HZ), jiffies))
1699 return 1;
1700 else
1701 return 0;
1702}
1703
Jens Axboe242f9dc2008-09-14 05:55:09 -07001704static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001705{
1706 struct iscsi_cls_session *cls_session;
1707 struct iscsi_session *session;
1708 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001709 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001710
1711 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001712 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001713
Mike Christie1b2c7af2009-03-05 14:45:58 -06001714 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
Mike Christief6d51802007-12-13 12:43:30 -06001715
1716 spin_lock(&session->lock);
1717 if (session->state != ISCSI_STATE_LOGGED_IN) {
1718 /*
1719 * We are probably in the middle of iscsi recovery so let
1720 * that complete and handle the error.
1721 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001722 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001723 goto done;
1724 }
1725
1726 conn = session->leadconn;
1727 if (!conn) {
1728 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001729 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001730 goto done;
1731 }
1732
1733 if (!conn->recv_timeout && !conn->ping_timeout)
1734 goto done;
1735 /*
1736 * if the ping timedout then we are in the middle of cleaning up
1737 * and can let the iscsi eh handle it
1738 */
Mike Christie4c48a822009-05-13 17:57:45 -05001739 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001740 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001741 goto done;
1742 }
Mike Christief6d51802007-12-13 12:43:30 -06001743 /*
1744 * if we are about to check the transport then give the command
1745 * more time
1746 */
1747 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
Mike Christie4c48a822009-05-13 17:57:45 -05001748 jiffies)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001749 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001750 goto done;
1751 }
1752
Mike Christief6d51802007-12-13 12:43:30 -06001753 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001754 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001755 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001756done:
1757 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001758 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1759 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001760 return rc;
1761}
1762
1763static void iscsi_check_transport_timeouts(unsigned long data)
1764{
1765 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1766 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001767 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001768
1769 spin_lock(&session->lock);
1770 if (session->state != ISCSI_STATE_LOGGED_IN)
1771 goto done;
1772
Mike Christie4cf10432008-05-07 20:43:52 -05001773 recv_timeout = conn->recv_timeout;
1774 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001775 goto done;
1776
Mike Christie4cf10432008-05-07 20:43:52 -05001777 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001778 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001779
1780 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001781 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001782 "expired, recv timeout %d, last rx %lu, "
1783 "last ping %lu, now %lu\n",
1784 conn->ping_timeout, conn->recv_timeout,
1785 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001786 spin_unlock(&session->lock);
1787 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1788 return;
1789 }
1790
Mike Christie4cf10432008-05-07 20:43:52 -05001791 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001792 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001793 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001794 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001795 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001796 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001797 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001798
Mike Christie1b2c7af2009-03-05 14:45:58 -06001799 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001800 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001801done:
1802 spin_unlock(&session->lock);
1803}
1804
Mike Christie9c19a7d2008-05-21 15:54:09 -05001805static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001806 struct iscsi_tm *hdr)
1807{
1808 memset(hdr, 0, sizeof(*hdr));
1809 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1810 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1811 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001812 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1813 hdr->rtt = task->hdr_itt;
1814 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001815}
1816
Mike Christie7996a772006-04-06 21:13:41 -05001817int iscsi_eh_abort(struct scsi_cmnd *sc)
1818{
Mike Christie75613522008-05-21 15:53:59 -05001819 struct iscsi_cls_session *cls_session;
1820 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001821 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001822 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001823 struct iscsi_tm *hdr;
1824 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001825
Mike Christie75613522008-05-21 15:53:59 -05001826 cls_session = starget_to_session(scsi_target(sc->device));
1827 session = cls_session->dd_data;
1828
Mike Christie6724add2007-08-15 01:38:30 -05001829 mutex_lock(&session->eh_mutex);
1830 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001831 /*
1832 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1833 * got the command.
1834 */
1835 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001836 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1837 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001838 spin_unlock_bh(&session->lock);
1839 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001840 return SUCCESS;
1841 }
1842
Mike Christie7996a772006-04-06 21:13:41 -05001843 /*
1844 * If we are not logged in or we have started a new session
1845 * then let the host reset code handle this
1846 */
Mike Christie843c0a82007-12-13 12:43:20 -06001847 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1848 sc->SCp.phase != session->age) {
1849 spin_unlock_bh(&session->lock);
1850 mutex_unlock(&session->eh_mutex);
1851 return FAILED;
1852 }
1853
1854 conn = session->leadconn;
1855 conn->eh_abort_cnt++;
1856 age = session->age;
1857
Mike Christie9c19a7d2008-05-21 15:54:09 -05001858 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001859 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1860 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001861
Mike Christie9c19a7d2008-05-21 15:54:09 -05001862 /* task completed before time out */
1863 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001864 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1865 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001866 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001867 }
Mike Christie7996a772006-04-06 21:13:41 -05001868
Mike Christie9c19a7d2008-05-21 15:54:09 -05001869 if (task->state == ISCSI_TASK_PENDING) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001870 fail_scsi_task(task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001871 goto success;
1872 }
Mike Christie7996a772006-04-06 21:13:41 -05001873
Mike Christie843c0a82007-12-13 12:43:20 -06001874 /* only have one tmf outstanding at a time */
1875 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001876 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001877 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001878
Mike Christie843c0a82007-12-13 12:43:20 -06001879 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001880 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001881
Mike Christie9c19a7d2008-05-21 15:54:09 -05001882 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001883 rc = FAILED;
1884 goto failed;
1885 }
1886
1887 switch (conn->tmf_state) {
1888 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001889 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001890 /*
1891 * stop tx side incase the target had sent a abort rsp but
1892 * the initiator was still writing out data.
1893 */
Mike Christie6724add2007-08-15 01:38:30 -05001894 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001895 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001896 * we do not stop the recv side because targets have been
1897 * good and have never sent us a successful tmf response
1898 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001899 */
Mike Christie77a23c22007-05-30 12:57:18 -05001900 spin_lock(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001901 fail_scsi_task(task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001902 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001903 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001904 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001905 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001906 case TMF_TIMEDOUT:
1907 spin_unlock_bh(&session->lock);
1908 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1909 goto failed_unlocked;
1910 case TMF_NOT_FOUND:
1911 if (!sc->SCp.ptr) {
1912 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001913 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001914 ISCSI_DBG_SESSION(session, "sc completed while abort "
1915 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001916 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001917 }
1918 /* fall through */
1919 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001920 conn->tmf_state = TMF_INITIAL;
1921 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001922 }
1923
Mike Christie77a23c22007-05-30 12:57:18 -05001924success:
Mike Christie7996a772006-04-06 21:13:41 -05001925 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001926success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001927 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1928 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001929 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001930 return SUCCESS;
1931
1932failed:
1933 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001934failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001935 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1936 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001937 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001938 return FAILED;
1939}
1940EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1941
Mike Christie843c0a82007-12-13 12:43:20 -06001942static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1943{
1944 memset(hdr, 0, sizeof(*hdr));
1945 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1946 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1947 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1948 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001949 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001950}
1951
1952int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1953{
Mike Christie75613522008-05-21 15:53:59 -05001954 struct iscsi_cls_session *cls_session;
1955 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001956 struct iscsi_conn *conn;
1957 struct iscsi_tm *hdr;
1958 int rc = FAILED;
1959
Mike Christie75613522008-05-21 15:53:59 -05001960 cls_session = starget_to_session(scsi_target(sc->device));
1961 session = cls_session->dd_data;
1962
Mike Christie1b2c7af2009-03-05 14:45:58 -06001963 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1964 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06001965
1966 mutex_lock(&session->eh_mutex);
1967 spin_lock_bh(&session->lock);
1968 /*
1969 * Just check if we are not logged in. We cannot check for
1970 * the phase because the reset could come from a ioctl.
1971 */
1972 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1973 goto unlock;
1974 conn = session->leadconn;
1975
1976 /* only have one tmf outstanding at a time */
1977 if (conn->tmf_state != TMF_INITIAL)
1978 goto unlock;
1979 conn->tmf_state = TMF_QUEUED;
1980
1981 hdr = &conn->tmhdr;
1982 iscsi_prep_lun_reset_pdu(sc, hdr);
1983
Mike Christie9c19a7d2008-05-21 15:54:09 -05001984 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001985 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001986 rc = FAILED;
1987 goto unlock;
1988 }
1989
1990 switch (conn->tmf_state) {
1991 case TMF_SUCCESS:
1992 break;
1993 case TMF_TIMEDOUT:
1994 spin_unlock_bh(&session->lock);
1995 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1996 goto done;
1997 default:
1998 conn->tmf_state = TMF_INITIAL;
1999 goto unlock;
2000 }
2001
2002 rc = SUCCESS;
2003 spin_unlock_bh(&session->lock);
2004
2005 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002006
Mike Christiea3439142008-09-24 11:46:15 -05002007 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002008 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002009 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002010 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002011
2012 iscsi_start_tx(conn);
2013 goto done;
2014
2015unlock:
2016 spin_unlock_bh(&session->lock);
2017done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06002018 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
2019 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002020 mutex_unlock(&session->eh_mutex);
2021 return rc;
2022}
2023EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2024
Olaf Kirch63203772007-12-13 12:43:25 -06002025/*
2026 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2027 * should be accessed via kfifo_{get,put} on q->queue.
2028 * Optionally, the caller can obtain the array of object pointers
2029 * by passing in a non-NULL @items pointer
2030 */
Mike Christie7996a772006-04-06 21:13:41 -05002031int
Olaf Kirch63203772007-12-13 12:43:25 -06002032iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002033{
Olaf Kirch63203772007-12-13 12:43:25 -06002034 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002035
Olaf Kirch63203772007-12-13 12:43:25 -06002036 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002037
2038 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002039
2040 /* If the user passed an items pointer, he wants a copy of
2041 * the array. */
2042 if (items)
2043 num_arrays++;
2044 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2045 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002046 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002047
2048 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2049 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002050 if (IS_ERR(q->queue)) {
2051 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002052 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002053 }
Mike Christie7996a772006-04-06 21:13:41 -05002054
2055 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002056 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002057 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002058 q->max = i;
2059 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002060 }
Mike Christie7996a772006-04-06 21:13:41 -05002061 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2062 }
Olaf Kirch63203772007-12-13 12:43:25 -06002063
2064 if (items) {
2065 *items = q->pool + max;
2066 memcpy(*items, q->pool, max * sizeof(void *));
2067 }
2068
Mike Christie7996a772006-04-06 21:13:41 -05002069 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002070
2071enomem:
2072 iscsi_pool_free(q);
2073 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002074}
2075EXPORT_SYMBOL_GPL(iscsi_pool_init);
2076
Olaf Kirch63203772007-12-13 12:43:25 -06002077void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002078{
2079 int i;
2080
2081 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002082 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002083 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002084 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002085}
2086EXPORT_SYMBOL_GPL(iscsi_pool_free);
2087
Mike Christiea4804cd2008-05-21 15:54:00 -05002088/**
2089 * iscsi_host_add - add host to system
2090 * @shost: scsi host
2091 * @pdev: parent device
2092 *
2093 * This should be called by partial offload and software iscsi drivers
2094 * to add a host to the system.
2095 */
2096int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002097{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002098 if (!shost->can_queue)
2099 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2100
Mike Christie4d108352009-03-05 14:46:04 -06002101 if (!shost->cmd_per_lun)
2102 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2103
Mike Christie308cec12009-02-06 12:06:20 -06002104 if (!shost->transportt->eh_timed_out)
2105 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002106 return scsi_add_host(shost, pdev);
2107}
2108EXPORT_SYMBOL_GPL(iscsi_host_add);
2109
2110/**
2111 * iscsi_host_alloc - allocate a host and driver data
2112 * @sht: scsi host template
2113 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002114 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002115 *
2116 * This should be called by partial offload and software iscsi drivers.
2117 * To access the driver specific memory use the iscsi_host_priv() macro.
2118 */
2119struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002120 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002121{
2122 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002123 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002124
2125 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2126 if (!shost)
2127 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002128 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002129
2130 if (xmit_can_sleep) {
2131 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2132 "iscsi_q_%d", shost->host_no);
2133 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2134 if (!ihost->workq)
2135 goto free_host;
2136 }
2137
Mike Christiee5bd7b52008-09-24 11:46:10 -05002138 spin_lock_init(&ihost->lock);
2139 ihost->state = ISCSI_HOST_SETUP;
2140 ihost->num_sessions = 0;
2141 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002142 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002143
2144free_host:
2145 scsi_host_put(shost);
2146 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002147}
Mike Christiea4804cd2008-05-21 15:54:00 -05002148EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002149
Mike Christiee5bd7b52008-09-24 11:46:10 -05002150static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2151{
Mike Christie40a06e72009-03-05 14:46:05 -06002152 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002153}
2154
Mike Christiea4804cd2008-05-21 15:54:00 -05002155/**
2156 * iscsi_host_remove - remove host and sessions
2157 * @shost: scsi host
2158 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002159 * If there are any sessions left, this will initiate the removal and wait
2160 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002161 */
2162void iscsi_host_remove(struct Scsi_Host *shost)
2163{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002164 struct iscsi_host *ihost = shost_priv(shost);
2165 unsigned long flags;
2166
2167 spin_lock_irqsave(&ihost->lock, flags);
2168 ihost->state = ISCSI_HOST_REMOVED;
2169 spin_unlock_irqrestore(&ihost->lock, flags);
2170
2171 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2172 wait_event_interruptible(ihost->session_removal_wq,
2173 ihost->num_sessions == 0);
2174 if (signal_pending(current))
2175 flush_signals(current);
2176
Mike Christiea4804cd2008-05-21 15:54:00 -05002177 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002178 if (ihost->workq)
2179 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002180}
2181EXPORT_SYMBOL_GPL(iscsi_host_remove);
2182
2183void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002184{
2185 struct iscsi_host *ihost = shost_priv(shost);
2186
2187 kfree(ihost->netdev);
2188 kfree(ihost->hwaddress);
2189 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002190 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002191}
Mike Christiea4804cd2008-05-21 15:54:00 -05002192EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002193
Mike Christiee5bd7b52008-09-24 11:46:10 -05002194static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2195{
2196 struct iscsi_host *ihost = shost_priv(shost);
2197 unsigned long flags;
2198
2199 shost = scsi_host_get(shost);
2200 if (!shost) {
2201 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2202 "of session teardown event because host already "
2203 "removed.\n");
2204 return;
2205 }
2206
2207 spin_lock_irqsave(&ihost->lock, flags);
2208 ihost->num_sessions--;
2209 if (ihost->num_sessions == 0)
2210 wake_up(&ihost->session_removal_wq);
2211 spin_unlock_irqrestore(&ihost->lock, flags);
2212 scsi_host_put(shost);
2213}
2214
Mike Christie75613522008-05-21 15:53:59 -05002215/**
2216 * iscsi_session_setup - create iscsi cls session and host and session
2217 * @iscsit: iscsi transport template
2218 * @shost: scsi host
2219 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002220 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002221 * @initial_cmdsn: initial CmdSN
2222 *
2223 * This can be used by software iscsi_transports that allocate
2224 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002225 *
2226 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2227 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2228 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002229 */
2230struct iscsi_cls_session *
2231iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002232 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002233 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002234{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002235 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002236 struct iscsi_session *session;
2237 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002238 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002239 unsigned long flags;
2240
2241 spin_lock_irqsave(&ihost->lock, flags);
2242 if (ihost->state == ISCSI_HOST_REMOVED) {
2243 spin_unlock_irqrestore(&ihost->lock, flags);
2244 return NULL;
2245 }
2246 ihost->num_sessions++;
2247 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002248
2249 if (!total_cmds)
2250 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002251 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002252 * The iscsi layer needs some tasks for nop handling and tmfs,
2253 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2254 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002255 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002256 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2257 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2258 "must be a power of two that is at least %d.\n",
2259 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002260 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002261 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002262
2263 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2264 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2265 "must be a power of 2 less than or equal to %d.\n",
2266 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2267 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2268 }
2269
2270 if (!is_power_of_2(total_cmds)) {
2271 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2272 "must be a power of 2.\n", total_cmds);
2273 total_cmds = rounddown_pow_of_two(total_cmds);
2274 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2275 return NULL;
2276 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2277 total_cmds);
2278 }
2279 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002280
Mike Christie5d91e202008-05-21 15:54:01 -05002281 cls_session = iscsi_alloc_session(shost, iscsit,
2282 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002283 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002284 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002285 session = cls_session->dd_data;
2286 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002287 session->host = shost;
2288 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002289 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002290 session->lu_reset_timeout = 15;
2291 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002292 session->scsi_cmds_max = scsi_cmds;
2293 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002294 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002295 session->exp_cmdsn = initial_cmdsn + 1;
2296 session->max_cmdsn = initial_cmdsn + 1;
2297 session->max_r2t = 1;
2298 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002299 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002300 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002301
2302 /* initialize SCSI PDU commands pool */
2303 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2304 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002305 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002306 goto cmdpool_alloc_fail;
2307
2308 /* pre-format cmds pool with ITT */
2309 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002310 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002311
Mike Christie9c19a7d2008-05-21 15:54:09 -05002312 if (cmd_task_size)
2313 task->dd_data = &task[1];
2314 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002315 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002316 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002317 }
2318
Mike Christief53a88d2006-06-28 12:00:27 -05002319 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002320 goto module_get_fail;
2321
Mike Christie79706342008-05-21 15:54:12 -05002322 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002323 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002324
Mike Christie7996a772006-04-06 21:13:41 -05002325 return cls_session;
2326
2327cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002328 module_put(iscsit->owner);
2329module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002330 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002331cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002332 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002333dec_session_count:
2334 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002335 return NULL;
2336}
2337EXPORT_SYMBOL_GPL(iscsi_session_setup);
2338
2339/**
2340 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002341 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002342 *
Mike Christie75613522008-05-21 15:53:59 -05002343 * The driver must have called iscsi_remove_session before
2344 * calling this.
2345 */
Mike Christie7996a772006-04-06 21:13:41 -05002346void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2347{
Mike Christie75613522008-05-21 15:53:59 -05002348 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002349 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002350 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002351
Olaf Kirch63203772007-12-13 12:43:25 -06002352 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002353
Mike Christieb2c64162007-05-30 12:57:16 -05002354 kfree(session->password);
2355 kfree(session->password_in);
2356 kfree(session->username);
2357 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002358 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002359 kfree(session->initiatorname);
2360 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002361
Mike Christie75613522008-05-21 15:53:59 -05002362 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002363 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002364 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002365}
2366EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2367
2368/**
2369 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2370 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002371 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002372 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002373 */
Mike Christie7996a772006-04-06 21:13:41 -05002374struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002375iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2376 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002377{
Mike Christie75613522008-05-21 15:53:59 -05002378 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002379 struct iscsi_conn *conn;
2380 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002381 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002382
Mike Christie5d91e202008-05-21 15:54:01 -05002383 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2384 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002385 if (!cls_conn)
2386 return NULL;
2387 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002388 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002389
Mike Christie5d91e202008-05-21 15:54:01 -05002390 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002391 conn->session = session;
2392 conn->cls_conn = cls_conn;
2393 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2394 conn->id = conn_idx;
2395 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002396 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002397
2398 init_timer(&conn->transport_timer);
2399 conn->transport_timer.data = (unsigned long)conn;
2400 conn->transport_timer.function = iscsi_check_transport_timeouts;
2401
Mike Christie843c0a82007-12-13 12:43:20 -06002402 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002403 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002404 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002405 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002406
Mike Christie9c19a7d2008-05-21 15:54:09 -05002407 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002408 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002409 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002410 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002411 sizeof(void*))) {
2412 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002413 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002414 }
2415 spin_unlock_bh(&session->lock);
2416
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002417 data = (char *) __get_free_pages(GFP_KERNEL,
2418 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002419 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002420 goto login_task_data_alloc_fail;
2421 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002422
Mike Christie843c0a82007-12-13 12:43:20 -06002423 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002424 init_waitqueue_head(&conn->ehwait);
2425
2426 return cls_conn;
2427
Mike Christie9c19a7d2008-05-21 15:54:09 -05002428login_task_data_alloc_fail:
2429 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002430 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002431login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002432 iscsi_destroy_conn(cls_conn);
2433 return NULL;
2434}
2435EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2436
2437/**
2438 * iscsi_conn_teardown - teardown iscsi connection
2439 * cls_conn: iscsi class connection
2440 *
2441 * TODO: we may need to make this into a two step process
2442 * like scsi-mls remove + put host
2443 */
2444void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2445{
2446 struct iscsi_conn *conn = cls_conn->dd_data;
2447 struct iscsi_session *session = conn->session;
2448 unsigned long flags;
2449
Mike Christief6d51802007-12-13 12:43:30 -06002450 del_timer_sync(&conn->transport_timer);
2451
Mike Christie7996a772006-04-06 21:13:41 -05002452 spin_lock_bh(&session->lock);
2453 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2454 if (session->leadconn == conn) {
2455 /*
2456 * leading connection? then give up on recovery.
2457 */
2458 session->state = ISCSI_STATE_TERMINATE;
2459 wake_up(&conn->ehwait);
2460 }
2461 spin_unlock_bh(&session->lock);
2462
Mike Christie7996a772006-04-06 21:13:41 -05002463 /*
2464 * Block until all in-progress commands for this connection
2465 * time out or fail.
2466 */
2467 for (;;) {
2468 spin_lock_irqsave(session->host->host_lock, flags);
2469 if (!session->host->host_busy) { /* OK for ERL == 0 */
2470 spin_unlock_irqrestore(session->host->host_lock, flags);
2471 break;
2472 }
2473 spin_unlock_irqrestore(session->host->host_lock, flags);
2474 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002475 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2476 "host_busy %d host_failed %d\n",
2477 session->host->host_busy,
2478 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002479 /*
2480 * force eh_abort() to unblock
2481 */
2482 wake_up(&conn->ehwait);
2483 }
2484
Mike Christie779ea122007-02-28 17:32:15 -06002485 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002486 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002487
Mike Christie7996a772006-04-06 21:13:41 -05002488 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002489 free_pages((unsigned long) conn->data,
2490 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002491 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002492 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002493 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002494 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002495 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002496 spin_unlock_bh(&session->lock);
2497
Mike Christie7996a772006-04-06 21:13:41 -05002498 iscsi_destroy_conn(cls_conn);
2499}
2500EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2501
2502int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2503{
2504 struct iscsi_conn *conn = cls_conn->dd_data;
2505 struct iscsi_session *session = conn->session;
2506
Mike Christieffd04362006-08-31 18:09:24 -04002507 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002508 iscsi_conn_printk(KERN_ERR, conn,
2509 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002510 return -EPERM;
2511 }
2512
Mike Christiedb98ccd2006-08-31 18:09:31 -04002513 if ((session->imm_data_en || !session->initial_r2t_en) &&
2514 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002515 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2516 "first_burst %d max_burst %d\n",
2517 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002518 return -EINVAL;
2519 }
2520
Mike Christief6d51802007-12-13 12:43:30 -06002521 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002522 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2523 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002524 conn->recv_timeout = 5;
2525 }
2526
2527 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002528 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2529 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002530 conn->ping_timeout = 5;
2531 }
2532
Mike Christie7996a772006-04-06 21:13:41 -05002533 spin_lock_bh(&session->lock);
2534 conn->c_stage = ISCSI_CONN_STARTED;
2535 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002536 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002537
Mike Christief6d51802007-12-13 12:43:30 -06002538 conn->last_recv = jiffies;
2539 conn->last_ping = jiffies;
2540 if (conn->recv_timeout && conn->ping_timeout)
2541 mod_timer(&conn->transport_timer,
2542 jiffies + (conn->recv_timeout * HZ));
2543
Mike Christie7996a772006-04-06 21:13:41 -05002544 switch(conn->stop_stage) {
2545 case STOP_CONN_RECOVER:
2546 /*
2547 * unblock eh_abort() if it is blocked. re-try all
2548 * commands after successful recovery
2549 */
Mike Christie7996a772006-04-06 21:13:41 -05002550 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002551 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002552 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002553 if (session->age == 16)
2554 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002555 break;
Mike Christie7996a772006-04-06 21:13:41 -05002556 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002557 conn->stop_stage = 0;
2558 break;
Mike Christie7996a772006-04-06 21:13:41 -05002559 default:
2560 break;
2561 }
2562 spin_unlock_bh(&session->lock);
2563
Mike Christie75613522008-05-21 15:53:59 -05002564 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002565 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002566 return 0;
2567}
2568EXPORT_SYMBOL_GPL(iscsi_conn_start);
2569
2570static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002571fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002572{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002573 struct iscsi_task *task;
2574 int i;
Mike Christie7996a772006-04-06 21:13:41 -05002575
Mike Christie3bbaaad2009-05-13 17:57:46 -05002576 for (i = 0; i < conn->session->cmds_max; i++) {
2577 task = conn->session->cmds[i];
2578 if (task->sc)
2579 continue;
2580
2581 if (task->state == ISCSI_TASK_FREE)
2582 continue;
2583
2584 ISCSI_DBG_SESSION(conn->session,
2585 "failing mgmt itt 0x%x state %d\n",
2586 task->itt, task->state);
2587 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002588 }
Mike Christie7996a772006-04-06 21:13:41 -05002589}
2590
Mike Christie656cffc2006-05-18 20:31:42 -05002591static void iscsi_start_session_recovery(struct iscsi_session *session,
2592 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002593{
Mike Christieed2abc72006-05-02 19:46:40 -05002594 int old_stop_stage;
2595
Mike Christie6724add2007-08-15 01:38:30 -05002596 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002597 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002598 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002599 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002600 mutex_unlock(&session->eh_mutex);
2601 return;
2602 }
2603
2604 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002605 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002606 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002607 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002608 */
Mike Christie67a61112006-05-30 00:37:20 -05002609 if (flag == STOP_CONN_TERM)
2610 session->state = ISCSI_STATE_TERMINATE;
2611 else if (conn->stop_stage != STOP_CONN_RECOVER)
2612 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002613 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002614
Mike Christie26013ad2009-05-13 17:57:43 -05002615 del_timer_sync(&conn->transport_timer);
2616 iscsi_suspend_tx(conn);
2617
2618 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002619 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002620 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002621 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002622 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002623
Mike Christie7996a772006-04-06 21:13:41 -05002624 /*
2625 * for connection level recovery we should not calculate
2626 * header digest. conn->hdr_size used for optimization
2627 * in hdr_extract() and will be re-negotiated at
2628 * set_param() time.
2629 */
2630 if (flag == STOP_CONN_RECOVER) {
2631 conn->hdrdgst_en = 0;
2632 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002633 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002634 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002635 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002636 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002637 }
Mike Christie7996a772006-04-06 21:13:41 -05002638 }
Mike Christie656cffc2006-05-18 20:31:42 -05002639
Mike Christie656cffc2006-05-18 20:31:42 -05002640 /*
2641 * flush queues.
2642 */
2643 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002644 if (flag == STOP_CONN_RECOVER)
Mike Christie3bbaaad2009-05-13 17:57:46 -05002645 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie56d7fcf2008-08-19 18:45:26 -05002646 else
Mike Christie3bbaaad2009-05-13 17:57:46 -05002647 fail_scsi_tasks(conn, -1, DID_ERROR);
2648 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002649 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002650 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002651}
Mike Christie7996a772006-04-06 21:13:41 -05002652
2653void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2654{
2655 struct iscsi_conn *conn = cls_conn->dd_data;
2656 struct iscsi_session *session = conn->session;
2657
2658 switch (flag) {
2659 case STOP_CONN_RECOVER:
2660 case STOP_CONN_TERM:
2661 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002662 break;
Mike Christie7996a772006-04-06 21:13:41 -05002663 default:
Mike Christie322d7392008-01-31 13:36:52 -06002664 iscsi_conn_printk(KERN_ERR, conn,
2665 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002666 }
2667}
2668EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2669
2670int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2671 struct iscsi_cls_conn *cls_conn, int is_leading)
2672{
Mike Christie75613522008-05-21 15:53:59 -05002673 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002674 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002675
Mike Christie7996a772006-04-06 21:13:41 -05002676 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002677 if (is_leading)
2678 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002679 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002680
2681 /*
2682 * Unblock xmitworker(), Login Phase will pass through.
2683 */
2684 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2685 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2686 return 0;
2687}
2688EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2689
Mike Christie5700b1a2009-05-13 17:57:40 -05002690static int iscsi_switch_str_param(char **param, char *new_val_buf)
2691{
2692 char *new_val;
2693
2694 if (*param) {
2695 if (!strcmp(*param, new_val_buf))
2696 return 0;
2697 }
2698
2699 new_val = kstrdup(new_val_buf, GFP_NOIO);
2700 if (!new_val)
2701 return -ENOMEM;
2702
2703 kfree(*param);
2704 *param = new_val;
2705 return 0;
2706}
Mike Christiea54a52c2006-06-28 12:00:23 -05002707
2708int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2709 enum iscsi_param param, char *buf, int buflen)
2710{
2711 struct iscsi_conn *conn = cls_conn->dd_data;
2712 struct iscsi_session *session = conn->session;
2713 uint32_t value;
2714
2715 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002716 case ISCSI_PARAM_FAST_ABORT:
2717 sscanf(buf, "%d", &session->fast_abort);
2718 break;
Mike Christief6d51802007-12-13 12:43:30 -06002719 case ISCSI_PARAM_ABORT_TMO:
2720 sscanf(buf, "%d", &session->abort_timeout);
2721 break;
2722 case ISCSI_PARAM_LU_RESET_TMO:
2723 sscanf(buf, "%d", &session->lu_reset_timeout);
2724 break;
2725 case ISCSI_PARAM_PING_TMO:
2726 sscanf(buf, "%d", &conn->ping_timeout);
2727 break;
2728 case ISCSI_PARAM_RECV_TMO:
2729 sscanf(buf, "%d", &conn->recv_timeout);
2730 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002731 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2732 sscanf(buf, "%d", &conn->max_recv_dlength);
2733 break;
2734 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2735 sscanf(buf, "%d", &conn->max_xmit_dlength);
2736 break;
2737 case ISCSI_PARAM_HDRDGST_EN:
2738 sscanf(buf, "%d", &conn->hdrdgst_en);
2739 break;
2740 case ISCSI_PARAM_DATADGST_EN:
2741 sscanf(buf, "%d", &conn->datadgst_en);
2742 break;
2743 case ISCSI_PARAM_INITIAL_R2T_EN:
2744 sscanf(buf, "%d", &session->initial_r2t_en);
2745 break;
2746 case ISCSI_PARAM_MAX_R2T:
2747 sscanf(buf, "%d", &session->max_r2t);
2748 break;
2749 case ISCSI_PARAM_IMM_DATA_EN:
2750 sscanf(buf, "%d", &session->imm_data_en);
2751 break;
2752 case ISCSI_PARAM_FIRST_BURST:
2753 sscanf(buf, "%d", &session->first_burst);
2754 break;
2755 case ISCSI_PARAM_MAX_BURST:
2756 sscanf(buf, "%d", &session->max_burst);
2757 break;
2758 case ISCSI_PARAM_PDU_INORDER_EN:
2759 sscanf(buf, "%d", &session->pdu_inorder_en);
2760 break;
2761 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2762 sscanf(buf, "%d", &session->dataseq_inorder_en);
2763 break;
2764 case ISCSI_PARAM_ERL:
2765 sscanf(buf, "%d", &session->erl);
2766 break;
2767 case ISCSI_PARAM_IFMARKER_EN:
2768 sscanf(buf, "%d", &value);
2769 BUG_ON(value);
2770 break;
2771 case ISCSI_PARAM_OFMARKER_EN:
2772 sscanf(buf, "%d", &value);
2773 BUG_ON(value);
2774 break;
2775 case ISCSI_PARAM_EXP_STATSN:
2776 sscanf(buf, "%u", &conn->exp_statsn);
2777 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002778 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002779 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002780 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002781 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002782 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002783 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002784 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002785 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002786 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002787 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002788 case ISCSI_PARAM_TPGT:
2789 sscanf(buf, "%d", &session->tpgt);
2790 break;
2791 case ISCSI_PARAM_PERSISTENT_PORT:
2792 sscanf(buf, "%d", &conn->persistent_port);
2793 break;
2794 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002795 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002796 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002797 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002798 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002799 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002800 default:
2801 return -ENOSYS;
2802 }
2803
2804 return 0;
2805}
2806EXPORT_SYMBOL_GPL(iscsi_set_param);
2807
2808int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2809 enum iscsi_param param, char *buf)
2810{
Mike Christie75613522008-05-21 15:53:59 -05002811 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002812 int len;
2813
2814 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002815 case ISCSI_PARAM_FAST_ABORT:
2816 len = sprintf(buf, "%d\n", session->fast_abort);
2817 break;
Mike Christief6d51802007-12-13 12:43:30 -06002818 case ISCSI_PARAM_ABORT_TMO:
2819 len = sprintf(buf, "%d\n", session->abort_timeout);
2820 break;
2821 case ISCSI_PARAM_LU_RESET_TMO:
2822 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2823 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002824 case ISCSI_PARAM_INITIAL_R2T_EN:
2825 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2826 break;
2827 case ISCSI_PARAM_MAX_R2T:
2828 len = sprintf(buf, "%hu\n", session->max_r2t);
2829 break;
2830 case ISCSI_PARAM_IMM_DATA_EN:
2831 len = sprintf(buf, "%d\n", session->imm_data_en);
2832 break;
2833 case ISCSI_PARAM_FIRST_BURST:
2834 len = sprintf(buf, "%u\n", session->first_burst);
2835 break;
2836 case ISCSI_PARAM_MAX_BURST:
2837 len = sprintf(buf, "%u\n", session->max_burst);
2838 break;
2839 case ISCSI_PARAM_PDU_INORDER_EN:
2840 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2841 break;
2842 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2843 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2844 break;
2845 case ISCSI_PARAM_ERL:
2846 len = sprintf(buf, "%d\n", session->erl);
2847 break;
2848 case ISCSI_PARAM_TARGET_NAME:
2849 len = sprintf(buf, "%s\n", session->targetname);
2850 break;
2851 case ISCSI_PARAM_TPGT:
2852 len = sprintf(buf, "%d\n", session->tpgt);
2853 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002854 case ISCSI_PARAM_USERNAME:
2855 len = sprintf(buf, "%s\n", session->username);
2856 break;
2857 case ISCSI_PARAM_USERNAME_IN:
2858 len = sprintf(buf, "%s\n", session->username_in);
2859 break;
2860 case ISCSI_PARAM_PASSWORD:
2861 len = sprintf(buf, "%s\n", session->password);
2862 break;
2863 case ISCSI_PARAM_PASSWORD_IN:
2864 len = sprintf(buf, "%s\n", session->password_in);
2865 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002866 case ISCSI_PARAM_IFACE_NAME:
2867 len = sprintf(buf, "%s\n", session->ifacename);
2868 break;
2869 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002870 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05002871 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002872 default:
2873 return -ENOSYS;
2874 }
2875
2876 return len;
2877}
2878EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2879
2880int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2881 enum iscsi_param param, char *buf)
2882{
2883 struct iscsi_conn *conn = cls_conn->dd_data;
2884 int len;
2885
2886 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002887 case ISCSI_PARAM_PING_TMO:
2888 len = sprintf(buf, "%u\n", conn->ping_timeout);
2889 break;
2890 case ISCSI_PARAM_RECV_TMO:
2891 len = sprintf(buf, "%u\n", conn->recv_timeout);
2892 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002893 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2894 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2895 break;
2896 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2897 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2898 break;
2899 case ISCSI_PARAM_HDRDGST_EN:
2900 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2901 break;
2902 case ISCSI_PARAM_DATADGST_EN:
2903 len = sprintf(buf, "%d\n", conn->datadgst_en);
2904 break;
2905 case ISCSI_PARAM_IFMARKER_EN:
2906 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2907 break;
2908 case ISCSI_PARAM_OFMARKER_EN:
2909 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2910 break;
2911 case ISCSI_PARAM_EXP_STATSN:
2912 len = sprintf(buf, "%u\n", conn->exp_statsn);
2913 break;
2914 case ISCSI_PARAM_PERSISTENT_PORT:
2915 len = sprintf(buf, "%d\n", conn->persistent_port);
2916 break;
2917 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2918 len = sprintf(buf, "%s\n", conn->persistent_address);
2919 break;
2920 default:
2921 return -ENOSYS;
2922 }
2923
2924 return len;
2925}
2926EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2927
Mike Christie0801c242007-05-30 12:57:12 -05002928int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2929 char *buf)
2930{
Mike Christie75613522008-05-21 15:53:59 -05002931 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002932 int len;
2933
2934 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002935 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002936 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002937 break;
Mike Christie0801c242007-05-30 12:57:12 -05002938 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002939 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002940 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002941 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002942 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002943 break;
Mike Christie75613522008-05-21 15:53:59 -05002944 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002945 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002946 break;
Mike Christie0801c242007-05-30 12:57:12 -05002947 default:
2948 return -ENOSYS;
2949 }
2950
2951 return len;
2952}
2953EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2954
2955int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2956 char *buf, int buflen)
2957{
Mike Christie75613522008-05-21 15:53:59 -05002958 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002959
2960 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002961 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002962 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002963 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002964 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05002965 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002966 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002967 default:
2968 return -ENOSYS;
2969 }
2970
2971 return 0;
2972}
2973EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2974
Mike Christie7996a772006-04-06 21:13:41 -05002975MODULE_AUTHOR("Mike Christie");
2976MODULE_DESCRIPTION("iSCSI library functions");
2977MODULE_LICENSE("GPL");