blob: 716cc344c5dfb5253eb5436cd0e19e5fbaa82b66 [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
Erez Zilberbd2199d2009-06-15 22:11:10 -050041static int iscsi_dbg_lib_conn;
42module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
47
48static int iscsi_dbg_lib_session;
49module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
50 S_IRUGO | S_IWUSR);
51MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
54
55static int iscsi_dbg_lib_eh;
56module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
57 S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
Mike Christie1b2c7af2009-03-05 14:45:58 -060061
62#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
63 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050064 if (iscsi_dbg_lib_conn) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060065 iscsi_conn_printk(KERN_INFO, _conn, \
66 "%s " dbg_fmt, \
67 __func__, ##arg); \
68 } while (0);
69
70#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
71 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050072 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
74 "%s " dbg_fmt, \
75 __func__, ##arg); \
76 } while (0);
77
78#define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
79 do { \
80 if (iscsi_dbg_lib_eh) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060081 iscsi_session_printk(KERN_INFO, _session, \
82 "%s " dbg_fmt, \
83 __func__, ##arg); \
84 } while (0);
85
Mike Christie77a23c22007-05-30 12:57:18 -050086/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050088
Mike Christie77a23c22007-05-30 12:57:18 -050089static int iscsi_sna_lt(u32 n1, u32 n2)
90{
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
93}
94
95/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96static int iscsi_sna_lte(u32 n1, u32 n2)
97{
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
100}
101
Mike Christie32ae7632009-03-05 14:46:03 -0600102inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
103{
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
106
Mike Christie1336aed2009-05-13 17:57:48 -0500107 if (ihost->workq)
108 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -0600109}
110EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
111
Mike Christie77a23c22007-05-30 12:57:18 -0500112void
113iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500114{
115 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
116 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
117
Mike Christie77a23c22007-05-30 12:57:18 -0500118 /*
119 * standard specifies this check for when to update expected and
120 * max sequence numbers
121 */
122 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
123 return;
124
125 if (exp_cmdsn != session->exp_cmdsn &&
126 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500127 session->exp_cmdsn = exp_cmdsn;
128
Mike Christie77a23c22007-05-30 12:57:18 -0500129 if (max_cmdsn != session->max_cmdsn &&
130 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
131 session->max_cmdsn = max_cmdsn;
132 /*
133 * if the window closed with IO queued, then kick the
134 * xmit thread
135 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500136 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500137 !list_empty(&session->leadconn->mgmtqueue))
138 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500139 }
Mike Christie7996a772006-04-06 21:13:41 -0500140}
Mike Christie77a23c22007-05-30 12:57:18 -0500141EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500142
Mike Christie577577d2008-12-02 00:32:05 -0600143/**
144 * iscsi_prep_data_out_pdu - initialize Data-Out
145 * @task: scsi command task
146 * @r2t: R2T info
147 * @hdr: iscsi data in pdu
148 *
149 * Notes:
150 * Initialize Data-Out within this R2T sequence and finds
151 * proper data_offset within this SCSI command.
152 *
153 * This function is called with connection lock taken.
154 **/
155void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
156 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500157{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500158 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600159 unsigned int left = r2t->data_length - r2t->sent;
160
161 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500162
163 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600164 hdr->ttt = r2t->ttt;
165 hdr->datasn = cpu_to_be32(r2t->datasn);
166 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500167 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600168 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
169 hdr->itt = task->hdr_itt;
170 hdr->exp_statsn = r2t->exp_statsn;
171 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
172 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500173 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600174 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500175 hdr->flags = 0;
176 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600177 hton24(hdr->dlength, left);
178 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500179 hdr->flags = ISCSI_FLAG_CMD_FINAL;
180 }
Mike Christie577577d2008-12-02 00:32:05 -0600181 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500182}
Mike Christie577577d2008-12-02 00:32:05 -0600183EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500184
Mike Christie9c19a7d2008-05-21 15:54:09 -0500185static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600186{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500187 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600188
Mike Christie9c19a7d2008-05-21 15:54:09 -0500189 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600190 WARN_ON(1);
191 return -EINVAL;
192 }
193
194 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500195 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600196 return 0;
197}
198
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500199/*
200 * make an extended cdb AHS
201 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500202static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500203{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500204 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500205 unsigned rlen, pad_len;
206 unsigned short ahslength;
207 struct iscsi_ecdb_ahdr *ecdb_ahdr;
208 int rc;
209
Mike Christie9c19a7d2008-05-21 15:54:09 -0500210 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500211 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
212
213 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
214 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
215
216 pad_len = iscsi_padding(rlen);
217
Mike Christie9c19a7d2008-05-21 15:54:09 -0500218 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500219 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
220 if (rc)
221 return rc;
222
223 if (pad_len)
224 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
225
226 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
227 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
228 ecdb_ahdr->reserved = 0;
229 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
230
Mike Christie1b2c7af2009-03-05 14:45:58 -0600231 ISCSI_DBG_SESSION(task->conn->session,
232 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
233 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
234 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
235 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500236 return 0;
237}
238
Mike Christie9c19a7d2008-05-21 15:54:09 -0500239static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500240{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500241 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500242 struct iscsi_rlength_ahdr *rlen_ahdr;
243 int rc;
244
Mike Christie9c19a7d2008-05-21 15:54:09 -0500245 rlen_ahdr = iscsi_next_hdr(task);
246 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500247 if (rc)
248 return rc;
249
250 rlen_ahdr->ahslength =
251 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
252 sizeof(rlen_ahdr->reserved));
253 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
254 rlen_ahdr->reserved = 0;
255 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
256
Mike Christie1b2c7af2009-03-05 14:45:58 -0600257 ISCSI_DBG_SESSION(task->conn->session,
258 "bidi-in rlen_ahdr->read_length(%d) "
259 "rlen_ahdr->ahslength(%d)\n",
260 be32_to_cpu(rlen_ahdr->read_length),
261 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500262 return 0;
263}
264
Mike Christie7996a772006-04-06 21:13:41 -0500265/**
266 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500267 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500268 *
269 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
270 * fields like dlength or final based on how much data it sends
271 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500272static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500273{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500274 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500275 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500276 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600277 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500278 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600279 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600280 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500281
Mike Christie184b57c2009-05-13 17:57:39 -0500282 if (conn->session->tt->alloc_pdu) {
283 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
284 if (rc)
285 return rc;
286 }
Mike Christie577577d2008-12-02 00:32:05 -0600287 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600288 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600289 memset(hdr, 0, sizeof(*hdr));
290
Mike Christie2ff79d52008-12-02 00:32:14 -0600291 if (session->tt->parse_pdu_itt)
292 hdr->itt = task->hdr_itt = itt;
293 else
294 hdr->itt = task->hdr_itt = build_itt(task->itt,
295 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500296 task->hdr_len = 0;
297 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600298 if (rc)
299 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600300 hdr->opcode = ISCSI_OP_SCSI_CMD;
301 hdr->flags = ISCSI_ATTR_SIMPLE;
302 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600303 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600304 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600305 session->cmdsn++;
306 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500307 cmd_len = sc->cmd_len;
308 if (cmd_len < ISCSI_CDB_SIZE)
309 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
310 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500311 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500312 if (rc)
313 return rc;
314 cmd_len = ISCSI_CDB_SIZE;
315 }
316 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500317
Mike Christie9c19a7d2008-05-21 15:54:09 -0500318 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500319 if (scsi_bidi_cmnd(sc)) {
320 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500321 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500322 if (rc)
323 return rc;
324 }
Mike Christie7996a772006-04-06 21:13:41 -0500325 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500326 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600327 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
328
Boaz Harroshc07d4442008-04-18 10:11:52 -0500329 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500330 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
331 /*
332 * Write counters:
333 *
334 * imm_count bytes to be sent right after
335 * SCSI PDU Header
336 *
337 * unsol_count bytes(as Data-Out) to be sent
338 * without R2T ack right after
339 * immediate data
340 *
Mike Christie577577d2008-12-02 00:32:05 -0600341 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500342 *
343 * pad_count bytes to be sent as zero-padding
344 */
Mike Christie577577d2008-12-02 00:32:05 -0600345 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500346
347 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500348 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500349 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500350 conn->max_xmit_dlength);
351 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500352 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500353 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500354 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500355 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600356 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500357
Mike Christieffd04362006-08-31 18:09:24 -0400358 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600359 r2t->data_length = min(session->first_burst, out_len) -
360 task->imm_count;
361 r2t->data_offset = task->imm_count;
362 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
363 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400364 }
365
Mike Christie577577d2008-12-02 00:32:05 -0600366 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500367 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600368 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500369 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500370 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
371 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500372 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500373
374 if (sc->sc_data_direction == DMA_FROM_DEVICE)
375 hdr->flags |= ISCSI_FLAG_CMD_READ;
376 }
377
Boaz Harrosh004d6532007-12-13 12:43:23 -0600378 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500379 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600380
381 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
382 hdrlength /= ISCSI_PAD_LEN;
383
384 WARN_ON(hdrlength >= 256);
385 hdr->hlength = hdrlength & 0xFF;
386
Mike Christie577577d2008-12-02 00:32:05 -0600387 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500388 return -EIO;
389
Mike Christie9c19a7d2008-05-21 15:54:09 -0500390 task->state = ISCSI_TASK_RUNNING;
Mike Christie77a23c22007-05-30 12:57:18 -0500391
Olaf Kircha8ac6312007-12-13 12:43:35 -0600392 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600393 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
394 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
395 scsi_bidi_cmnd(sc) ? "bidirectional" :
396 sc->sc_data_direction == DMA_TO_DEVICE ?
397 "write" : "read", conn->id, sc, sc->cmnd[0],
398 task->itt, scsi_bufflen(sc),
399 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
400 session->cmdsn,
401 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600402 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500403}
Mike Christie7996a772006-04-06 21:13:41 -0500404
405/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500406 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500407 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500408 *
409 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500410 * This function returns the scsi command to scsi-ml or cleans
411 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500412 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500413static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500414{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500415 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600416 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500417 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500418
Mike Christie4421c9e2009-05-13 17:57:50 -0500419 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
420 task->itt, task->state, task->sc);
421
Mike Christie577577d2008-12-02 00:32:05 -0600422 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500423 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500424 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500425 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500426 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500427 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500428 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500429 return;
430
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500432
Mike Christie3e5c28a2008-05-21 15:54:06 -0500433 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500434 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500435 /* SCSI eh reuses commands to verify us */
436 sc->SCp.ptr = NULL;
437 /*
438 * queue command may call this to free the task, but
439 * not have setup the sc callback
440 */
441 if (sc->scsi_done)
442 sc->scsi_done(sc);
443 }
Mike Christie7996a772006-04-06 21:13:41 -0500444}
445
Mike Christie913e5bf2008-05-21 15:54:18 -0500446void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400447{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500448 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400449}
Mike Christie913e5bf2008-05-21 15:54:18 -0500450EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400451
Mike Christie9c19a7d2008-05-21 15:54:09 -0500452static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400453{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500454 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500455 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400456}
457
Mike Christie9c19a7d2008-05-21 15:54:09 -0500458void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500459{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500460 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500461
462 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500463 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500464 spin_unlock_bh(&session->lock);
465}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500466EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500467
Mike Christie3bbaaad2009-05-13 17:57:46 -0500468/**
469 * iscsi_complete_task - finish a task
470 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500471 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500472 *
473 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600474 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500475static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600476{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500477 struct iscsi_conn *conn = task->conn;
478
Mike Christie4421c9e2009-05-13 17:57:50 -0500479 ISCSI_DBG_SESSION(conn->session,
480 "complete task itt 0x%x state %d sc %p\n",
481 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500482 if (task->state == ISCSI_TASK_COMPLETED ||
483 task->state == ISCSI_TASK_ABRT_TMF ||
484 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500485 return;
486 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500487 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500488
489 if (!list_empty(&task->running))
490 list_del_init(&task->running);
491
492 if (conn->task == task)
493 conn->task = NULL;
494
495 if (conn->ping_task == task)
496 conn->ping_task = NULL;
497
498 /* release get from queueing */
499 __iscsi_put_task(task);
500}
501
502/*
503 * session lock must be held and if not called for a task that is
504 * still pending or from the xmit thread, then xmit thread must
505 * be suspended.
506 */
507static void fail_scsi_task(struct iscsi_task *task, int err)
508{
509 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600510 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500511 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600512
Mike Christie3bbaaad2009-05-13 17:57:46 -0500513 /*
514 * if a command completes and we get a successful tmf response
515 * we will hit this because the scsi eh abort code does not take
516 * a ref to the task.
517 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500518 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600519 if (!sc)
520 return;
521
Mike Christieb3cd5052009-05-13 17:57:49 -0500522 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600523 /*
524 * cmd never made it to the xmit thread, so we should not count
525 * the cmd in the sequencing
526 */
527 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500528 /* it was never sent so just complete like normal */
529 state = ISCSI_TASK_COMPLETED;
530 } else if (err == DID_TRANSPORT_DISRUPTED)
531 state = ISCSI_TASK_ABRT_SESS_RECOV;
532 else
533 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600534
Mike Christieb3cd5052009-05-13 17:57:49 -0500535 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500536 if (!scsi_bidi_cmnd(sc))
537 scsi_set_resid(sc, scsi_bufflen(sc));
538 else {
539 scsi_out(sc)->resid = scsi_out(sc)->length;
540 scsi_in(sc)->resid = scsi_in(sc)->length;
541 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500542
Mike Christieb3cd5052009-05-13 17:57:49 -0500543 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600544}
545
Mike Christie3e5c28a2008-05-21 15:54:06 -0500546static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500547 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500548{
549 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600550 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500551 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
552
553 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
554 return -ENOTCONN;
555
556 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
557 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
558 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
559 /*
560 * pre-format CmdSN for outgoing PDU.
561 */
562 nop->cmdsn = cpu_to_be32(session->cmdsn);
563 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500564 /*
565 * TODO: We always use immediate, so we never hit this.
566 * If we start to send tmfs or nops as non-immediate then
567 * we should start checking the cmdsn numbers for mgmt tasks.
568 */
569 if (conn->c_stage == ISCSI_CONN_STARTED &&
570 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
571 session->queued_cmdsn++;
572 session->cmdsn++;
573 }
574 }
575
Mike Christieae15f802008-12-02 00:32:15 -0600576 if (session->tt->init_task && session->tt->init_task(task))
577 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500578
579 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
580 session->state = ISCSI_STATE_LOGGING_OUT;
581
Mike Christie577577d2008-12-02 00:32:05 -0600582 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600583 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
584 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
585 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500586 return 0;
587}
588
Mike Christie9c19a7d2008-05-21 15:54:09 -0500589static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600590__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
591 char *data, uint32_t data_size)
592{
593 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500594 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500595 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600596 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600597
598 if (session->state == ISCSI_STATE_TERMINATE)
599 return NULL;
600
601 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
602 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
603 /*
604 * Login and Text are sent serially, in
605 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500606 * Same task can be used. Same ITT must be used.
607 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600608 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500609 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600610 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500611 if (session->state != ISCSI_STATE_LOGGED_IN)
612 return NULL;
613
Mike Christief6d51802007-12-13 12:43:30 -0600614 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
615 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
616
Mike Christie3e5c28a2008-05-21 15:54:06 -0500617 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500618 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600619 return NULL;
620 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500621 /*
622 * released in complete pdu for task we expect a response for, and
623 * released by the lld when it has transmitted the task for
624 * pdus we do not expect a response for.
625 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500626 atomic_set(&task->refcount, 1);
627 task->conn = conn;
628 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500629 INIT_LIST_HEAD(&task->running);
630 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600631
632 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500633 memcpy(task->data, data, data_size);
634 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600635 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500636 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600637
Mike Christie184b57c2009-05-13 17:57:39 -0500638 if (conn->session->tt->alloc_pdu) {
639 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
640 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
641 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500642 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500643 }
Mike Christie577577d2008-12-02 00:32:05 -0600644 }
Mike Christie184b57c2009-05-13 17:57:39 -0500645
Mike Christie262ef632008-12-02 00:32:13 -0600646 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600647 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500648 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600649
650 if (hdr->itt != RESERVED_ITT) {
651 if (session->tt->parse_pdu_itt)
652 task->hdr->itt = itt;
653 else
654 task->hdr->itt = build_itt(task->itt,
655 task->conn->session->age);
656 }
657
Mike Christie1336aed2009-05-13 17:57:48 -0500658 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600659 if (iscsi_prep_mgmt_task(conn, task))
660 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500661
Mike Christie9c19a7d2008-05-21 15:54:09 -0500662 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600663 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500664 } else {
665 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600666 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500667 }
Mike Christie052d0142008-05-21 15:54:05 -0500668
Mike Christie9c19a7d2008-05-21 15:54:09 -0500669 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600670
671free_task:
672 __iscsi_put_task(task);
673 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600674}
675
676int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
677 char *data, uint32_t data_size)
678{
679 struct iscsi_conn *conn = cls_conn->dd_data;
680 struct iscsi_session *session = conn->session;
681 int err = 0;
682
683 spin_lock_bh(&session->lock);
684 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
685 err = -EPERM;
686 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600687 return err;
688}
689EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
690
Mike Christie7996a772006-04-06 21:13:41 -0500691/**
692 * iscsi_cmd_rsp - SCSI Command Response processing
693 * @conn: iscsi connection
694 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500695 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500696 * @data: cmd data buffer
697 * @datalen: len of buffer
698 *
699 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500700 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500701 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500702static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500703 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500704 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500705{
Mike Christie7996a772006-04-06 21:13:41 -0500706 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
707 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500708 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500709
Mike Christie77a23c22007-05-30 12:57:18 -0500710 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500711 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
712
713 sc->result = (DID_OK << 16) | rhdr->cmd_status;
714
715 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
716 sc->result = DID_ERROR << 16;
717 goto out;
718 }
719
720 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600721 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500722
723 if (datalen < 2) {
724invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600725 iscsi_conn_printk(KERN_ERR, conn,
726 "Got CHECK_CONDITION but invalid data "
727 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500728 sc->result = DID_BAD_TARGET << 16;
729 goto out;
730 }
731
Harvey Harrison8f3339912008-05-21 15:54:20 -0500732 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500733 if (datalen < senselen)
734 goto invalid_datalen;
735
736 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600737 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600738 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
739 min_t(uint16_t, senselen,
740 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500741 }
742
Boaz Harroshc07d4442008-04-18 10:11:52 -0500743 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
744 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
745 int res_count = be32_to_cpu(rhdr->bi_residual_count);
746
747 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
748 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
749 res_count <= scsi_in(sc)->length))
750 scsi_in(sc)->resid = res_count;
751 else
752 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
753 }
754
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600755 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
756 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500757 int res_count = be32_to_cpu(rhdr->residual_count);
758
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600759 if (res_count > 0 &&
760 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
761 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500762 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900763 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500764 else
765 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500766 }
Mike Christie7996a772006-04-06 21:13:41 -0500767out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500768 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600769 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500770 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500771 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500772}
773
Mike Christie1d9edf02008-09-24 11:46:09 -0500774/**
775 * iscsi_data_in_rsp - SCSI Data-In Response processing
776 * @conn: iscsi connection
777 * @hdr: iscsi pdu
778 * @task: scsi command task
779 **/
780static void
781iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
782 struct iscsi_task *task)
783{
784 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
785 struct scsi_cmnd *sc = task->sc;
786
787 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
788 return;
789
Mike Christieedbc9aa052009-05-13 17:57:42 -0500790 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500791 sc->result = (DID_OK << 16) | rhdr->cmd_status;
792 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
793 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
794 ISCSI_FLAG_DATA_OVERFLOW)) {
795 int res_count = be32_to_cpu(rhdr->residual_count);
796
797 if (res_count > 0 &&
798 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
799 res_count <= scsi_in(sc)->length))
800 scsi_in(sc)->resid = res_count;
801 else
802 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
803 }
804
Mike Christie3bbaaad2009-05-13 17:57:46 -0500805 ISCSI_DBG_SESSION(conn->session, "data in with status done "
806 "[sc %p res %d itt 0x%x]\n",
807 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500808 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500809 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500810}
811
Mike Christie7ea8b822006-07-24 15:47:22 -0500812static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
813{
814 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
815
816 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
817 conn->tmfrsp_pdus_cnt++;
818
Mike Christie843c0a82007-12-13 12:43:20 -0600819 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500820 return;
821
822 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600823 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500824 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600825 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500826 else
Mike Christie843c0a82007-12-13 12:43:20 -0600827 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500828 wake_up(&conn->ehwait);
829}
830
Mike Christief6d51802007-12-13 12:43:30 -0600831static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
832{
833 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500834 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600835
Mike Christie9c19a7d2008-05-21 15:54:09 -0500836 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600837 return;
838
839 memset(&hdr, 0, sizeof(struct iscsi_nopout));
840 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
841 hdr.flags = ISCSI_FLAG_CMD_FINAL;
842
843 if (rhdr) {
844 memcpy(hdr.lun, rhdr->lun, 8);
845 hdr.ttt = rhdr->ttt;
846 hdr.itt = RESERVED_ITT;
847 } else
848 hdr.ttt = RESERVED_ITT;
849
Mike Christie9c19a7d2008-05-21 15:54:09 -0500850 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
851 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600852 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600853 else if (!rhdr) {
854 /* only track our nops */
855 conn->ping_task = task;
856 conn->last_ping = jiffies;
857 }
Mike Christief6d51802007-12-13 12:43:30 -0600858}
859
Mike Christie62f38302006-08-31 18:09:27 -0400860static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
861 char *data, int datalen)
862{
863 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
864 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400865
866 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
867
868 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
869 if (ntoh24(reject->dlength) > datalen)
870 return ISCSI_ERR_PROTO;
871
872 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
873 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600874 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600875 "pdu (op 0x%x) rejected "
876 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600877 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400878 }
879 }
880 return 0;
881}
882
Mike Christie7996a772006-04-06 21:13:41 -0500883/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500884 * iscsi_itt_to_task - look up task by itt
885 * @conn: iscsi connection
886 * @itt: itt
887 *
888 * This should be used for mgmt tasks like login and nops, or if
889 * the LDD's itt space does not include the session age.
890 *
891 * The session lock must be held.
892 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500893struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500894{
895 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600896 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500897
898 if (itt == RESERVED_ITT)
899 return NULL;
900
Mike Christie262ef632008-12-02 00:32:13 -0600901 if (session->tt->parse_pdu_itt)
902 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
903 else
904 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500905 if (i >= session->cmds_max)
906 return NULL;
907
908 return session->cmds[i];
909}
Mike Christie8f9256c2009-05-13 17:57:41 -0500910EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500911
912/**
Mike Christie7996a772006-04-06 21:13:41 -0500913 * __iscsi_complete_pdu - complete pdu
914 * @conn: iscsi conn
915 * @hdr: iscsi header
916 * @data: data buffer
917 * @datalen: len of data buffer
918 *
919 * Completes pdu processing by freeing any resources allocated at
920 * queuecommand or send generic. session lock must be held and verify
921 * itt must have been called.
922 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500923int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
924 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500925{
926 struct iscsi_session *session = conn->session;
927 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500928 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500929 uint32_t itt;
930
Mike Christief6d51802007-12-13 12:43:30 -0600931 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500932 rc = iscsi_verify_itt(conn, hdr->itt);
933 if (rc)
934 return rc;
935
Al Virob4377352007-02-09 16:39:40 +0000936 if (hdr->itt != RESERVED_ITT)
937 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500938 else
Al Virob4377352007-02-09 16:39:40 +0000939 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500940
Mike Christie1b2c7af2009-03-05 14:45:58 -0600941 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
942 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500943
Mike Christie3e5c28a2008-05-21 15:54:06 -0500944 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500945 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400946
Mike Christie7996a772006-04-06 21:13:41 -0500947 switch(opcode) {
948 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500949 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500950 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500951 break;
952 }
953
Al Virob4377352007-02-09 16:39:40 +0000954 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500955 break;
956
Mike Christief6d51802007-12-13 12:43:30 -0600957 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500958 break;
959 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400960 rc = iscsi_handle_reject(conn, hdr, data, datalen);
961 break;
Mike Christie7996a772006-04-06 21:13:41 -0500962 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500963 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400964 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
965 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500966 break;
967 default:
968 rc = ISCSI_ERR_BAD_OPCODE;
969 break;
970 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500971 goto out;
972 }
Mike Christie7996a772006-04-06 21:13:41 -0500973
Mike Christie3e5c28a2008-05-21 15:54:06 -0500974 switch(opcode) {
975 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500976 case ISCSI_OP_SCSI_DATA_IN:
977 task = iscsi_itt_to_ctask(conn, hdr->itt);
978 if (!task)
979 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -0500980 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -0500981 break;
982 case ISCSI_OP_R2T:
983 /*
984 * LLD handles R2Ts if they need to.
985 */
986 return 0;
987 case ISCSI_OP_LOGOUT_RSP:
988 case ISCSI_OP_LOGIN_RSP:
989 case ISCSI_OP_TEXT_RSP:
990 case ISCSI_OP_SCSI_TMFUNC_RSP:
991 case ISCSI_OP_NOOP_IN:
992 task = iscsi_itt_to_task(conn, hdr->itt);
993 if (!task)
994 return ISCSI_ERR_BAD_ITT;
995 break;
996 default:
997 return ISCSI_ERR_BAD_OPCODE;
998 }
999
1000 switch(opcode) {
1001 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001002 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001003 break;
1004 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001005 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001006 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001007 case ISCSI_OP_LOGOUT_RSP:
1008 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1009 if (datalen) {
1010 rc = ISCSI_ERR_PROTO;
1011 break;
1012 }
1013 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1014 goto recv_pdu;
1015 case ISCSI_OP_LOGIN_RSP:
1016 case ISCSI_OP_TEXT_RSP:
1017 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1018 /*
1019 * login related PDU's exp_statsn is handled in
1020 * userspace
1021 */
1022 goto recv_pdu;
1023 case ISCSI_OP_SCSI_TMFUNC_RSP:
1024 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1025 if (datalen) {
1026 rc = ISCSI_ERR_PROTO;
1027 break;
1028 }
1029
1030 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001031 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001032 break;
1033 case ISCSI_OP_NOOP_IN:
1034 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1035 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1036 rc = ISCSI_ERR_PROTO;
1037 break;
1038 }
1039 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1040
Mike Christie9c19a7d2008-05-21 15:54:09 -05001041 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -05001042 /*
1043 * If this is not in response to one of our
1044 * nops then it must be from userspace.
1045 */
1046 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001047
1048 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
Mike Christieb3cd5052009-05-13 17:57:49 -05001049 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001050 break;
1051 default:
1052 rc = ISCSI_ERR_BAD_OPCODE;
1053 break;
1054 }
1055
1056out:
1057 return rc;
1058recv_pdu:
1059 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1060 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001061 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001062 return rc;
1063}
Mike Christie913e5bf2008-05-21 15:54:18 -05001064EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001065
1066int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1067 char *data, int datalen)
1068{
1069 int rc;
1070
1071 spin_lock(&conn->session->lock);
1072 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1073 spin_unlock(&conn->session->lock);
1074 return rc;
1075}
1076EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1077
Mike Christie0af967f2008-05-21 15:54:04 -05001078int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001079{
1080 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001081 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001082
Mike Christie0af967f2008-05-21 15:54:04 -05001083 if (itt == RESERVED_ITT)
1084 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001085
Mike Christie262ef632008-12-02 00:32:13 -06001086 if (session->tt->parse_pdu_itt)
1087 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1088 else {
1089 i = get_itt(itt);
1090 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1091 }
1092
1093 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001094 iscsi_conn_printk(KERN_ERR, conn,
1095 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001096 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001097 return ISCSI_ERR_BAD_ITT;
1098 }
Mike Christie7996a772006-04-06 21:13:41 -05001099
Mike Christie3e5c28a2008-05-21 15:54:06 -05001100 if (i >= session->cmds_max) {
1101 iscsi_conn_printk(KERN_ERR, conn,
1102 "received invalid itt index %u (max cmds "
1103 "%u.\n", i, session->cmds_max);
1104 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001105 }
Mike Christie7996a772006-04-06 21:13:41 -05001106 return 0;
1107}
1108EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1109
Mike Christie913e5bf2008-05-21 15:54:18 -05001110/**
1111 * iscsi_itt_to_ctask - look up ctask by itt
1112 * @conn: iscsi connection
1113 * @itt: itt
1114 *
1115 * This should be used for cmd tasks.
1116 *
1117 * The session lock must be held.
1118 */
1119struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001120{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001121 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001122
1123 if (iscsi_verify_itt(conn, itt))
1124 return NULL;
1125
Mike Christie913e5bf2008-05-21 15:54:18 -05001126 task = iscsi_itt_to_task(conn, itt);
1127 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001128 return NULL;
1129
Mike Christie913e5bf2008-05-21 15:54:18 -05001130 if (task->sc->SCp.phase != conn->session->age) {
1131 iscsi_session_printk(KERN_ERR, conn->session,
1132 "task's session age %d, expected %d\n",
1133 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001134 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001135 }
Mike Christie0af967f2008-05-21 15:54:04 -05001136
Mike Christie9c19a7d2008-05-21 15:54:09 -05001137 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001138}
1139EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1140
Mike Christie40a06e72009-03-05 14:46:05 -06001141void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001142 enum iscsi_err err)
1143{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001144 struct iscsi_conn *conn;
1145 struct device *dev;
1146 unsigned long flags;
1147
1148 spin_lock_irqsave(&session->lock, flags);
1149 conn = session->leadconn;
1150 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1151 spin_unlock_irqrestore(&session->lock, flags);
1152 return;
1153 }
1154
1155 dev = get_device(&conn->cls_conn->dev);
1156 spin_unlock_irqrestore(&session->lock, flags);
1157 if (!dev)
1158 return;
1159 /*
1160 * if the host is being removed bypass the connection
1161 * recovery initialization because we are going to kill
1162 * the session.
1163 */
1164 if (err == ISCSI_ERR_INVALID_HOST)
1165 iscsi_conn_error_event(conn->cls_conn, err);
1166 else
1167 iscsi_conn_failure(conn, err);
1168 put_device(dev);
1169}
1170EXPORT_SYMBOL_GPL(iscsi_session_failure);
1171
Mike Christie7996a772006-04-06 21:13:41 -05001172void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1173{
1174 struct iscsi_session *session = conn->session;
1175 unsigned long flags;
1176
1177 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001178 if (session->state == ISCSI_STATE_FAILED) {
1179 spin_unlock_irqrestore(&session->lock, flags);
1180 return;
1181 }
1182
Mike Christie67a61112006-05-30 00:37:20 -05001183 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001184 session->state = ISCSI_STATE_FAILED;
1185 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001186
Mike Christie7996a772006-04-06 21:13:41 -05001187 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1188 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001189 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001190}
1191EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1192
Mike Christie77a23c22007-05-30 12:57:18 -05001193static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1194{
1195 struct iscsi_session *session = conn->session;
1196
1197 /*
1198 * Check for iSCSI window and take care of CmdSN wrap-around
1199 */
Mike Christiee0726402007-07-26 12:46:48 -05001200 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001201 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1202 "%u MaxCmdSN %u CmdSN %u/%u\n",
1203 session->exp_cmdsn, session->max_cmdsn,
1204 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001205 return -ENOSPC;
1206 }
1207 return 0;
1208}
1209
Mike Christie9c19a7d2008-05-21 15:54:09 -05001210static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001211{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001212 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001213 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001214
Mike Christie9c19a7d2008-05-21 15:54:09 -05001215 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001216 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001217 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001218 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001219 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001220 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001221 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001222 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001223 }
1224 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001225 return rc;
1226}
1227
Mike Christie7996a772006-04-06 21:13:41 -05001228/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001229 * iscsi_requeue_task - requeue task to run from session workqueue
1230 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001231 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001232 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001233 * this. The session lock must be held. This should only be called
1234 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001235 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001236void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001237{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001238 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001239
Mike Christie3bbaaad2009-05-13 17:57:46 -05001240 /*
1241 * this may be on the requeue list already if the xmit_task callout
1242 * is handling the r2ts while we are adding new ones
1243 */
1244 if (list_empty(&task->running))
1245 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001246 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001247}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001248EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001249
1250/**
Mike Christie7996a772006-04-06 21:13:41 -05001251 * iscsi_data_xmit - xmit any command into the scheduled connection
1252 * @conn: iscsi connection
1253 *
1254 * Notes:
1255 * The function can return -EAGAIN in which case the caller must
1256 * re-schedule it again later or recover. '0' return code means
1257 * successful xmit.
1258 **/
1259static int iscsi_data_xmit(struct iscsi_conn *conn)
1260{
Mike Christie3219e522006-05-30 00:37:28 -05001261 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001262
Mike Christie77a23c22007-05-30 12:57:18 -05001263 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001264 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001265 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001266 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001267 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001268 }
Mike Christie7996a772006-04-06 21:13:41 -05001269
Mike Christie9c19a7d2008-05-21 15:54:09 -05001270 if (conn->task) {
1271 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001272 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001273 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001274 }
1275
Mike Christie77a23c22007-05-30 12:57:18 -05001276 /*
1277 * process mgmt pdus like nops before commands since we should
1278 * only have one nop-out as a ping from us and targets should not
1279 * overflow us with nop-ins
1280 */
1281check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001282 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001283 conn->task = list_entry(conn->mgmtqueue.next,
1284 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001285 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001286 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1287 __iscsi_put_task(conn->task);
1288 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001289 continue;
1290 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001291 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001292 if (rc)
1293 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001294 }
1295
Mike Christie843c0a82007-12-13 12:43:20 -06001296 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001297 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001298 if (conn->tmf_state == TMF_QUEUED)
1299 break;
1300
Mike Christie3bbaaad2009-05-13 17:57:46 -05001301 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001302 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001303 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001304 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001305 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001306 continue;
1307 }
Mike Christie577577d2008-12-02 00:32:05 -06001308 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1309 if (rc) {
1310 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001311 list_add_tail(&conn->task->running,
1312 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001313 conn->task = NULL;
1314 goto again;
1315 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001316 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001317 continue;
1318 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001319 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001320 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001321 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001322 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001323 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001324 * we need to check the mgmt queue for nops that need to
1325 * be sent to aviod starvation
1326 */
Mike Christie843c0a82007-12-13 12:43:20 -06001327 if (!list_empty(&conn->mgmtqueue))
1328 goto check_mgmt;
1329 }
1330
1331 while (!list_empty(&conn->requeue)) {
1332 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1333 break;
1334
Mike Christieb3a7ea82007-12-13 12:43:26 -06001335 /*
1336 * we always do fastlogout - conn stop code will clean up.
1337 */
1338 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1339 break;
1340
Mike Christie9c19a7d2008-05-21 15:54:09 -05001341 conn->task = list_entry(conn->requeue.next,
1342 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001343 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001344 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001345 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001346 if (rc)
1347 goto again;
1348 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001349 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001350 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001351 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001352 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001353
1354again:
1355 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001356 rc = -ENODATA;
1357 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001358 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001359}
1360
David Howellsc4028952006-11-22 14:57:56 +00001361static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001362{
David Howellsc4028952006-11-22 14:57:56 +00001363 struct iscsi_conn *conn =
1364 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001365 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001366 /*
1367 * serialize Xmit worker on a per-connection basis.
1368 */
Mike Christie3219e522006-05-30 00:37:28 -05001369 do {
1370 rc = iscsi_data_xmit(conn);
1371 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001372}
1373
Mike Christie577577d2008-12-02 00:32:05 -06001374static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1375 struct scsi_cmnd *sc)
1376{
1377 struct iscsi_task *task;
1378
1379 if (!__kfifo_get(conn->session->cmdpool.queue,
1380 (void *) &task, sizeof(void *)))
1381 return NULL;
1382
1383 sc->SCp.phase = conn->session->age;
1384 sc->SCp.ptr = (char *) task;
1385
1386 atomic_set(&task->refcount, 1);
1387 task->state = ISCSI_TASK_PENDING;
1388 task->conn = conn;
1389 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001390 task->have_checked_conn = false;
1391 task->last_timeout = jiffies;
1392 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001393 INIT_LIST_HEAD(&task->running);
1394 return task;
1395}
1396
Mike Christie7996a772006-04-06 21:13:41 -05001397enum {
1398 FAILURE_BAD_HOST = 1,
1399 FAILURE_SESSION_FAILED,
1400 FAILURE_SESSION_FREED,
1401 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001402 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001403 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001404 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001405 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001406 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001407 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001408};
1409
1410int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1411{
Mike Christie75613522008-05-21 15:53:59 -05001412 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001413 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001414 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001415 int reason = 0;
1416 struct iscsi_session *session;
1417 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001418 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001419
1420 sc->scsi_done = done;
1421 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001422 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001423
1424 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001425 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001426 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001427
Mike Christie75613522008-05-21 15:53:59 -05001428 cls_session = starget_to_session(scsi_target(sc->device));
1429 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001430 spin_lock(&session->lock);
1431
Mike Christie75613522008-05-21 15:53:59 -05001432 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001433 if (reason) {
1434 sc->result = reason;
1435 goto fault;
1436 }
1437
Mike Christie301e0f72009-05-13 17:57:47 -05001438 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001439 /*
1440 * to handle the race between when we set the recovery state
1441 * and block the session we requeue here (commands could
1442 * be entering our queuecommand while a block is starting
1443 * up because the block code is not locked)
1444 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001445 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001446 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001447 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001448 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001449 sc->result = DID_IMM_RETRY << 16;
1450 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001451 case ISCSI_STATE_LOGGING_OUT:
1452 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001453 sc->result = DID_IMM_RETRY << 16;
1454 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001455 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001456 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001457 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001458 break;
1459 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001460 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001461 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001462 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001463 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001464 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001465 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001466 }
Mike Christie7996a772006-04-06 21:13:41 -05001467 goto fault;
1468 }
1469
Mike Christie7996a772006-04-06 21:13:41 -05001470 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001471 if (!conn) {
1472 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001473 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001474 goto fault;
1475 }
Mike Christie7996a772006-04-06 21:13:41 -05001476
Mike Christie77a23c22007-05-30 12:57:18 -05001477 if (iscsi_check_cmdsn_window_closed(conn)) {
1478 reason = FAILURE_WINDOW_CLOSED;
1479 goto reject;
1480 }
1481
Mike Christie577577d2008-12-02 00:32:05 -06001482 task = iscsi_alloc_task(conn, sc);
1483 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001484 reason = FAILURE_OOM;
1485 goto reject;
1486 }
Mike Christie7996a772006-04-06 21:13:41 -05001487
Mike Christie1336aed2009-05-13 17:57:48 -05001488 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001489 reason = iscsi_prep_scsi_cmd_pdu(task);
1490 if (reason) {
1491 if (reason == -ENOMEM) {
1492 reason = FAILURE_OOM;
1493 goto prepd_reject;
1494 } else {
1495 sc->result = DID_ABORT << 16;
1496 goto prepd_fault;
1497 }
Mike Christie052d0142008-05-21 15:54:05 -05001498 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001499 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001500 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001501 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001502 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001503 } else {
1504 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001505 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001506 }
Mike Christie052d0142008-05-21 15:54:05 -05001507
1508 session->queued_cmdsn++;
1509 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001510 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001511 return 0;
1512
Mike Christie577577d2008-12-02 00:32:05 -06001513prepd_reject:
1514 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001515 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001516reject:
1517 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001518 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1519 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001520 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001521 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001522
Mike Christie577577d2008-12-02 00:32:05 -06001523prepd_fault:
1524 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001525 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001526fault:
1527 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001528 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1529 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001530 if (!scsi_bidi_cmnd(sc))
1531 scsi_set_resid(sc, scsi_bufflen(sc));
1532 else {
1533 scsi_out(sc)->resid = scsi_out(sc)->length;
1534 scsi_in(sc)->resid = scsi_in(sc)->length;
1535 }
Mike Christie052d0142008-05-21 15:54:05 -05001536 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001537 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1541
1542int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1543{
Mike Christie7996a772006-04-06 21:13:41 -05001544 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1545 return sdev->queue_depth;
1546}
1547EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1548
Mike Christie6b5d6c42009-04-21 15:32:32 -05001549int iscsi_target_alloc(struct scsi_target *starget)
1550{
1551 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1552 struct iscsi_session *session = cls_session->dd_data;
1553
1554 starget->can_queue = session->scsi_cmds_max;
1555 return 0;
1556}
1557EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1558
Mike Christie7996a772006-04-06 21:13:41 -05001559void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1560{
Mike Christie75613522008-05-21 15:53:59 -05001561 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001562
1563 spin_lock_bh(&session->lock);
1564 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001565 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001566 if (session->leadconn)
1567 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001568 }
1569 spin_unlock_bh(&session->lock);
1570}
1571EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1572
Mike Christie8e124522008-09-24 11:46:12 -05001573int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001574{
Mike Christie75613522008-05-21 15:53:59 -05001575 struct iscsi_cls_session *cls_session;
1576 struct iscsi_session *session;
1577 struct iscsi_conn *conn;
1578
1579 cls_session = starget_to_session(scsi_target(sc->device));
1580 session = cls_session->dd_data;
1581 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001582
Mike Christiebc436b22007-12-13 12:43:28 -06001583 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001584 spin_lock_bh(&session->lock);
1585 if (session->state == ISCSI_STATE_TERMINATE) {
1586failed:
Erez Zilberbd2199d2009-06-15 22:11:10 -05001587 ISCSI_DBG_EH(session,
1588 "failing target reset: Could not log back into "
1589 "target [age %d]\n",
1590 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001591 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001592 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001593 return FAILED;
1594 }
1595
Mike Christie7996a772006-04-06 21:13:41 -05001596 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001597 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001598 /*
1599 * we drop the lock here but the leadconn cannot be destoyed while
1600 * we are in the scsi eh
1601 */
Mike Christie843c0a82007-12-13 12:43:20 -06001602 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001603
Erez Zilberbd2199d2009-06-15 22:11:10 -05001604 ISCSI_DBG_EH(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001605 wait_event_interruptible(conn->ehwait,
1606 session->state == ISCSI_STATE_TERMINATE ||
1607 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001608 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001609 if (signal_pending(current))
1610 flush_signals(current);
1611
Mike Christiebc436b22007-12-13 12:43:28 -06001612 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001613 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001614 if (session->state == ISCSI_STATE_LOGGED_IN) {
1615 ISCSI_DBG_EH(session,
1616 "target reset succeeded\n");
1617 } else
Mike Christie7996a772006-04-06 21:13:41 -05001618 goto failed;
1619 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001620 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001621 return SUCCESS;
1622}
Mike Christie8e124522008-09-24 11:46:12 -05001623EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001624
Mike Christie843c0a82007-12-13 12:43:20 -06001625static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001626{
Mike Christie843c0a82007-12-13 12:43:20 -06001627 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001628 struct iscsi_session *session = conn->session;
1629
1630 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001631 if (conn->tmf_state == TMF_QUEUED) {
1632 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001633 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001634 /* unblock eh_abort() */
1635 wake_up(&conn->ehwait);
1636 }
1637 spin_unlock(&session->lock);
1638}
1639
Mike Christie9c19a7d2008-05-21 15:54:09 -05001640static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001641 struct iscsi_tm *hdr, int age,
1642 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001643{
Mike Christie7996a772006-04-06 21:13:41 -05001644 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001645 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001646
Mike Christie9c19a7d2008-05-21 15:54:09 -05001647 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001648 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001649 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001650 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001651 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001652 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001653 ISCSI_DBG_EH(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001654 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001655 }
Mike Christie843c0a82007-12-13 12:43:20 -06001656 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001657 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001658 conn->tmf_timer.function = iscsi_tmf_timedout;
1659 conn->tmf_timer.data = (unsigned long)conn;
1660 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001661 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001662
Mike Christie7996a772006-04-06 21:13:41 -05001663 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001664 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001665
1666 /*
1667 * block eh thread until:
1668 *
Mike Christie843c0a82007-12-13 12:43:20 -06001669 * 1) tmf response
1670 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001671 * 3) session is terminated or restarted or userspace has
1672 * given up on recovery
1673 */
Mike Christie843c0a82007-12-13 12:43:20 -06001674 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001675 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001676 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001677 if (signal_pending(current))
1678 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001679 del_timer_sync(&conn->tmf_timer);
1680
Mike Christie6724add2007-08-15 01:38:30 -05001681 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001682 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001683 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001684 if (age != session->age ||
1685 session->state != ISCSI_STATE_LOGGED_IN)
1686 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001687 return 0;
1688}
1689
1690/*
Mike Christie843c0a82007-12-13 12:43:20 -06001691 * Fail commands. session lock held and recv side suspended and xmit
1692 * thread flushed
1693 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001694static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1695 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001696{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001697 struct iscsi_task *task;
1698 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001699
Mike Christie3bbaaad2009-05-13 17:57:46 -05001700 for (i = 0; i < conn->session->cmds_max; i++) {
1701 task = conn->session->cmds[i];
1702 if (!task->sc || task->state == ISCSI_TASK_FREE)
1703 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001704
Mike Christie3bbaaad2009-05-13 17:57:46 -05001705 if (lun != -1 && lun != task->sc->device->lun)
1706 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001707
Mike Christie3bbaaad2009-05-13 17:57:46 -05001708 ISCSI_DBG_SESSION(conn->session,
1709 "failing sc %p itt 0x%x state %d\n",
1710 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001711 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001712 }
1713}
1714
Mike Christieb40977d2008-05-21 15:54:03 -05001715void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001716{
Mike Christie32ae7632009-03-05 14:46:03 -06001717 struct Scsi_Host *shost = conn->session->host;
1718 struct iscsi_host *ihost = shost_priv(shost);
1719
Mike Christie6724add2007-08-15 01:38:30 -05001720 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001721 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001722 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001723}
Mike Christieb40977d2008-05-21 15:54:03 -05001724EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001725
1726static void iscsi_start_tx(struct iscsi_conn *conn)
1727{
1728 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001729 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001730}
1731
Mike Christie4c48a822009-05-13 17:57:45 -05001732/*
1733 * We want to make sure a ping is in flight. It has timed out.
1734 * And we are not busy processing a pdu that is making
1735 * progress but got started before the ping and is taking a while
1736 * to complete so the ping is just stuck behind it in a queue.
1737 */
1738static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1739{
1740 if (conn->ping_task &&
1741 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1742 (conn->ping_timeout * HZ), jiffies))
1743 return 1;
1744 else
1745 return 0;
1746}
1747
Mike Christied355e572009-06-15 22:11:08 -05001748static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001749{
Mike Christied355e572009-06-15 22:11:08 -05001750 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1751 struct iscsi_task *task = NULL;
Mike Christief6d51802007-12-13 12:43:30 -06001752 struct iscsi_cls_session *cls_session;
1753 struct iscsi_session *session;
1754 struct iscsi_conn *conn;
Mike Christief6d51802007-12-13 12:43:30 -06001755
Mike Christied355e572009-06-15 22:11:08 -05001756 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001757 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001758
Erez Zilberbd2199d2009-06-15 22:11:10 -05001759 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001760
1761 spin_lock(&session->lock);
1762 if (session->state != ISCSI_STATE_LOGGED_IN) {
1763 /*
1764 * We are probably in the middle of iscsi recovery so let
1765 * that complete and handle the error.
1766 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001767 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001768 goto done;
1769 }
1770
1771 conn = session->leadconn;
1772 if (!conn) {
1773 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001774 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001775 goto done;
1776 }
1777
Mike Christied355e572009-06-15 22:11:08 -05001778 task = (struct iscsi_task *)sc->SCp.ptr;
1779 if (!task)
1780 goto done;
1781 /*
1782 * If we have sent (at least queued to the network layer) a pdu or
1783 * recvd one for the task since the last timeout ask for
1784 * more time. If on the next timeout we have not made progress
1785 * we can check if it is the task or connection when we send the
1786 * nop as a ping.
1787 */
1788 if (time_after_eq(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001789 ISCSI_DBG_EH(session, "Command making progress. Asking "
1790 "scsi-ml for more time to complete. "
1791 "Last data recv at %lu. Last timeout was at "
1792 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001793 task->have_checked_conn = false;
1794 rc = BLK_EH_RESET_TIMER;
1795 goto done;
1796 }
1797
Mike Christief6d51802007-12-13 12:43:30 -06001798 if (!conn->recv_timeout && !conn->ping_timeout)
1799 goto done;
1800 /*
1801 * if the ping timedout then we are in the middle of cleaning up
1802 * and can let the iscsi eh handle it
1803 */
Mike Christie4c48a822009-05-13 17:57:45 -05001804 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001805 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001806 goto done;
1807 }
Mike Christied355e572009-06-15 22:11:08 -05001808
1809 /* Assumes nop timeout is shorter than scsi cmd timeout */
1810 if (task->have_checked_conn)
1811 goto done;
1812
Mike Christief6d51802007-12-13 12:43:30 -06001813 /*
Mike Christied355e572009-06-15 22:11:08 -05001814 * Checking the transport already or nop from a cmd timeout still
1815 * running
Mike Christief6d51802007-12-13 12:43:30 -06001816 */
Mike Christied355e572009-06-15 22:11:08 -05001817 if (conn->ping_task) {
1818 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001819 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001820 goto done;
1821 }
1822
Mike Christied355e572009-06-15 22:11:08 -05001823 /* Make sure there is a transport check done */
1824 iscsi_send_nopout(conn, NULL);
1825 task->have_checked_conn = true;
1826 rc = BLK_EH_RESET_TIMER;
1827
Mike Christief6d51802007-12-13 12:43:30 -06001828done:
Mike Christied355e572009-06-15 22:11:08 -05001829 if (task)
1830 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06001831 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001832 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1833 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001834 return rc;
1835}
1836
1837static void iscsi_check_transport_timeouts(unsigned long data)
1838{
1839 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1840 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001841 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001842
1843 spin_lock(&session->lock);
1844 if (session->state != ISCSI_STATE_LOGGED_IN)
1845 goto done;
1846
Mike Christie4cf10432008-05-07 20:43:52 -05001847 recv_timeout = conn->recv_timeout;
1848 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001849 goto done;
1850
Mike Christie4cf10432008-05-07 20:43:52 -05001851 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001852 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001853
1854 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001855 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001856 "expired, recv timeout %d, last rx %lu, "
1857 "last ping %lu, now %lu\n",
1858 conn->ping_timeout, conn->recv_timeout,
1859 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001860 spin_unlock(&session->lock);
1861 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1862 return;
1863 }
1864
Mike Christie4cf10432008-05-07 20:43:52 -05001865 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001866 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001867 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001868 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001869 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001870 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001871 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001872
Mike Christie1b2c7af2009-03-05 14:45:58 -06001873 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001874 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001875done:
1876 spin_unlock(&session->lock);
1877}
1878
Mike Christie9c19a7d2008-05-21 15:54:09 -05001879static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001880 struct iscsi_tm *hdr)
1881{
1882 memset(hdr, 0, sizeof(*hdr));
1883 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1884 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1885 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001886 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1887 hdr->rtt = task->hdr_itt;
1888 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001889}
1890
Mike Christie7996a772006-04-06 21:13:41 -05001891int iscsi_eh_abort(struct scsi_cmnd *sc)
1892{
Mike Christie75613522008-05-21 15:53:59 -05001893 struct iscsi_cls_session *cls_session;
1894 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001895 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001896 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001897 struct iscsi_tm *hdr;
1898 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001899
Mike Christie75613522008-05-21 15:53:59 -05001900 cls_session = starget_to_session(scsi_target(sc->device));
1901 session = cls_session->dd_data;
1902
Erez Zilberbd2199d2009-06-15 22:11:10 -05001903 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05001904
Mike Christie6724add2007-08-15 01:38:30 -05001905 mutex_lock(&session->eh_mutex);
1906 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001907 /*
1908 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1909 * got the command.
1910 */
1911 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001912 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
1913 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001914 spin_unlock_bh(&session->lock);
1915 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001916 return SUCCESS;
1917 }
1918
Mike Christie7996a772006-04-06 21:13:41 -05001919 /*
1920 * If we are not logged in or we have started a new session
1921 * then let the host reset code handle this
1922 */
Mike Christie843c0a82007-12-13 12:43:20 -06001923 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1924 sc->SCp.phase != session->age) {
1925 spin_unlock_bh(&session->lock);
1926 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001927 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05001928 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06001929 return FAILED;
1930 }
1931
1932 conn = session->leadconn;
1933 conn->eh_abort_cnt++;
1934 age = session->age;
1935
Mike Christie9c19a7d2008-05-21 15:54:09 -05001936 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001937 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
1938 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001939
Mike Christie9c19a7d2008-05-21 15:54:09 -05001940 /* task completed before time out */
1941 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001942 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001943 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001944 }
Mike Christie7996a772006-04-06 21:13:41 -05001945
Mike Christie9c19a7d2008-05-21 15:54:09 -05001946 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001947 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05001948 goto success;
1949 }
Mike Christie7996a772006-04-06 21:13:41 -05001950
Mike Christie843c0a82007-12-13 12:43:20 -06001951 /* only have one tmf outstanding at a time */
1952 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001953 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001954 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001955
Mike Christie843c0a82007-12-13 12:43:20 -06001956 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001957 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001958
Mike Christie9c19a7d2008-05-21 15:54:09 -05001959 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001960 rc = FAILED;
1961 goto failed;
1962 }
1963
1964 switch (conn->tmf_state) {
1965 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001966 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001967 /*
1968 * stop tx side incase the target had sent a abort rsp but
1969 * the initiator was still writing out data.
1970 */
Mike Christie6724add2007-08-15 01:38:30 -05001971 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001972 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001973 * we do not stop the recv side because targets have been
1974 * good and have never sent us a successful tmf response
1975 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001976 */
Mike Christie77a23c22007-05-30 12:57:18 -05001977 spin_lock(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05001978 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06001979 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001980 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001981 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001982 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001983 case TMF_TIMEDOUT:
1984 spin_unlock_bh(&session->lock);
1985 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1986 goto failed_unlocked;
1987 case TMF_NOT_FOUND:
1988 if (!sc->SCp.ptr) {
1989 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001990 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05001991 ISCSI_DBG_EH(session, "sc completed while abort in "
1992 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001993 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001994 }
1995 /* fall through */
1996 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001997 conn->tmf_state = TMF_INITIAL;
1998 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001999 }
2000
Mike Christie77a23c22007-05-30 12:57:18 -05002001success:
Mike Christie7996a772006-04-06 21:13:41 -05002002 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002003success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002004 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2005 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002006 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002007 return SUCCESS;
2008
2009failed:
2010 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002011failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002012 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2013 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002014 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002015 return FAILED;
2016}
2017EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2018
Mike Christie843c0a82007-12-13 12:43:20 -06002019static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2020{
2021 memset(hdr, 0, sizeof(*hdr));
2022 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2023 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2024 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2025 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002026 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002027}
2028
2029int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2030{
Mike Christie75613522008-05-21 15:53:59 -05002031 struct iscsi_cls_session *cls_session;
2032 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002033 struct iscsi_conn *conn;
2034 struct iscsi_tm *hdr;
2035 int rc = FAILED;
2036
Mike Christie75613522008-05-21 15:53:59 -05002037 cls_session = starget_to_session(scsi_target(sc->device));
2038 session = cls_session->dd_data;
2039
Erez Zilberbd2199d2009-06-15 22:11:10 -05002040 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002041
2042 mutex_lock(&session->eh_mutex);
2043 spin_lock_bh(&session->lock);
2044 /*
2045 * Just check if we are not logged in. We cannot check for
2046 * the phase because the reset could come from a ioctl.
2047 */
2048 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2049 goto unlock;
2050 conn = session->leadconn;
2051
2052 /* only have one tmf outstanding at a time */
2053 if (conn->tmf_state != TMF_INITIAL)
2054 goto unlock;
2055 conn->tmf_state = TMF_QUEUED;
2056
2057 hdr = &conn->tmhdr;
2058 iscsi_prep_lun_reset_pdu(sc, hdr);
2059
Mike Christie9c19a7d2008-05-21 15:54:09 -05002060 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002061 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002062 rc = FAILED;
2063 goto unlock;
2064 }
2065
2066 switch (conn->tmf_state) {
2067 case TMF_SUCCESS:
2068 break;
2069 case TMF_TIMEDOUT:
2070 spin_unlock_bh(&session->lock);
2071 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2072 goto done;
2073 default:
2074 conn->tmf_state = TMF_INITIAL;
2075 goto unlock;
2076 }
2077
2078 rc = SUCCESS;
2079 spin_unlock_bh(&session->lock);
2080
2081 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002082
Mike Christiea3439142008-09-24 11:46:15 -05002083 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002084 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002085 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002086 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002087
2088 iscsi_start_tx(conn);
2089 goto done;
2090
2091unlock:
2092 spin_unlock_bh(&session->lock);
2093done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002094 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2095 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002096 mutex_unlock(&session->eh_mutex);
2097 return rc;
2098}
2099EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2100
Olaf Kirch63203772007-12-13 12:43:25 -06002101/*
2102 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2103 * should be accessed via kfifo_{get,put} on q->queue.
2104 * Optionally, the caller can obtain the array of object pointers
2105 * by passing in a non-NULL @items pointer
2106 */
Mike Christie7996a772006-04-06 21:13:41 -05002107int
Olaf Kirch63203772007-12-13 12:43:25 -06002108iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002109{
Olaf Kirch63203772007-12-13 12:43:25 -06002110 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002111
Olaf Kirch63203772007-12-13 12:43:25 -06002112 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002113
2114 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002115
2116 /* If the user passed an items pointer, he wants a copy of
2117 * the array. */
2118 if (items)
2119 num_arrays++;
2120 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2121 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002122 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002123
2124 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2125 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002126 if (IS_ERR(q->queue)) {
2127 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002128 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002129 }
Mike Christie7996a772006-04-06 21:13:41 -05002130
2131 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002132 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002133 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002134 q->max = i;
2135 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002136 }
Mike Christie7996a772006-04-06 21:13:41 -05002137 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2138 }
Olaf Kirch63203772007-12-13 12:43:25 -06002139
2140 if (items) {
2141 *items = q->pool + max;
2142 memcpy(*items, q->pool, max * sizeof(void *));
2143 }
2144
Mike Christie7996a772006-04-06 21:13:41 -05002145 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002146
2147enomem:
2148 iscsi_pool_free(q);
2149 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002150}
2151EXPORT_SYMBOL_GPL(iscsi_pool_init);
2152
Olaf Kirch63203772007-12-13 12:43:25 -06002153void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002154{
2155 int i;
2156
2157 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002158 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002159 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002160 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002161}
2162EXPORT_SYMBOL_GPL(iscsi_pool_free);
2163
Mike Christiea4804cd2008-05-21 15:54:00 -05002164/**
2165 * iscsi_host_add - add host to system
2166 * @shost: scsi host
2167 * @pdev: parent device
2168 *
2169 * This should be called by partial offload and software iscsi drivers
2170 * to add a host to the system.
2171 */
2172int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002173{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002174 if (!shost->can_queue)
2175 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2176
Mike Christie4d108352009-03-05 14:46:04 -06002177 if (!shost->cmd_per_lun)
2178 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2179
Mike Christie308cec12009-02-06 12:06:20 -06002180 if (!shost->transportt->eh_timed_out)
2181 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002182 return scsi_add_host(shost, pdev);
2183}
2184EXPORT_SYMBOL_GPL(iscsi_host_add);
2185
2186/**
2187 * iscsi_host_alloc - allocate a host and driver data
2188 * @sht: scsi host template
2189 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002190 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002191 *
2192 * This should be called by partial offload and software iscsi drivers.
2193 * To access the driver specific memory use the iscsi_host_priv() macro.
2194 */
2195struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002196 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002197{
2198 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002199 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002200
2201 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2202 if (!shost)
2203 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002204 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002205
2206 if (xmit_can_sleep) {
2207 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2208 "iscsi_q_%d", shost->host_no);
2209 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2210 if (!ihost->workq)
2211 goto free_host;
2212 }
2213
Mike Christiee5bd7b52008-09-24 11:46:10 -05002214 spin_lock_init(&ihost->lock);
2215 ihost->state = ISCSI_HOST_SETUP;
2216 ihost->num_sessions = 0;
2217 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002218 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002219
2220free_host:
2221 scsi_host_put(shost);
2222 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002223}
Mike Christiea4804cd2008-05-21 15:54:00 -05002224EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002225
Mike Christiee5bd7b52008-09-24 11:46:10 -05002226static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2227{
Mike Christie40a06e72009-03-05 14:46:05 -06002228 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002229}
2230
Mike Christiea4804cd2008-05-21 15:54:00 -05002231/**
2232 * iscsi_host_remove - remove host and sessions
2233 * @shost: scsi host
2234 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002235 * If there are any sessions left, this will initiate the removal and wait
2236 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002237 */
2238void iscsi_host_remove(struct Scsi_Host *shost)
2239{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002240 struct iscsi_host *ihost = shost_priv(shost);
2241 unsigned long flags;
2242
2243 spin_lock_irqsave(&ihost->lock, flags);
2244 ihost->state = ISCSI_HOST_REMOVED;
2245 spin_unlock_irqrestore(&ihost->lock, flags);
2246
2247 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2248 wait_event_interruptible(ihost->session_removal_wq,
2249 ihost->num_sessions == 0);
2250 if (signal_pending(current))
2251 flush_signals(current);
2252
Mike Christiea4804cd2008-05-21 15:54:00 -05002253 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002254 if (ihost->workq)
2255 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002256}
2257EXPORT_SYMBOL_GPL(iscsi_host_remove);
2258
2259void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002260{
2261 struct iscsi_host *ihost = shost_priv(shost);
2262
2263 kfree(ihost->netdev);
2264 kfree(ihost->hwaddress);
2265 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002266 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002267}
Mike Christiea4804cd2008-05-21 15:54:00 -05002268EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002269
Mike Christiee5bd7b52008-09-24 11:46:10 -05002270static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2271{
2272 struct iscsi_host *ihost = shost_priv(shost);
2273 unsigned long flags;
2274
2275 shost = scsi_host_get(shost);
2276 if (!shost) {
2277 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2278 "of session teardown event because host already "
2279 "removed.\n");
2280 return;
2281 }
2282
2283 spin_lock_irqsave(&ihost->lock, flags);
2284 ihost->num_sessions--;
2285 if (ihost->num_sessions == 0)
2286 wake_up(&ihost->session_removal_wq);
2287 spin_unlock_irqrestore(&ihost->lock, flags);
2288 scsi_host_put(shost);
2289}
2290
Mike Christie75613522008-05-21 15:53:59 -05002291/**
2292 * iscsi_session_setup - create iscsi cls session and host and session
2293 * @iscsit: iscsi transport template
2294 * @shost: scsi host
2295 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002296 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002297 * @initial_cmdsn: initial CmdSN
2298 *
2299 * This can be used by software iscsi_transports that allocate
2300 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002301 *
2302 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2303 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2304 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002305 */
2306struct iscsi_cls_session *
2307iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002308 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002309 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002310{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002311 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002312 struct iscsi_session *session;
2313 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002314 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002315 unsigned long flags;
2316
2317 spin_lock_irqsave(&ihost->lock, flags);
2318 if (ihost->state == ISCSI_HOST_REMOVED) {
2319 spin_unlock_irqrestore(&ihost->lock, flags);
2320 return NULL;
2321 }
2322 ihost->num_sessions++;
2323 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002324
2325 if (!total_cmds)
2326 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002327 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002328 * The iscsi layer needs some tasks for nop handling and tmfs,
2329 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2330 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002331 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002332 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2333 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2334 "must be a power of two that is at least %d.\n",
2335 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002336 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002337 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002338
2339 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2340 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2341 "must be a power of 2 less than or equal to %d.\n",
2342 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2343 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2344 }
2345
2346 if (!is_power_of_2(total_cmds)) {
2347 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2348 "must be a power of 2.\n", total_cmds);
2349 total_cmds = rounddown_pow_of_two(total_cmds);
2350 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2351 return NULL;
2352 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2353 total_cmds);
2354 }
2355 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002356
Mike Christie5d91e202008-05-21 15:54:01 -05002357 cls_session = iscsi_alloc_session(shost, iscsit,
2358 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002359 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002360 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002361 session = cls_session->dd_data;
2362 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002363 session->host = shost;
2364 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002365 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002366 session->lu_reset_timeout = 15;
2367 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002368 session->scsi_cmds_max = scsi_cmds;
2369 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002370 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002371 session->exp_cmdsn = initial_cmdsn + 1;
2372 session->max_cmdsn = initial_cmdsn + 1;
2373 session->max_r2t = 1;
2374 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002375 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002376 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002377
2378 /* initialize SCSI PDU commands pool */
2379 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2380 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002381 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002382 goto cmdpool_alloc_fail;
2383
2384 /* pre-format cmds pool with ITT */
2385 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002386 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002387
Mike Christie9c19a7d2008-05-21 15:54:09 -05002388 if (cmd_task_size)
2389 task->dd_data = &task[1];
2390 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002391 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002392 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002393 }
2394
Mike Christief53a88d2006-06-28 12:00:27 -05002395 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002396 goto module_get_fail;
2397
Mike Christie79706342008-05-21 15:54:12 -05002398 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002399 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002400
Mike Christie7996a772006-04-06 21:13:41 -05002401 return cls_session;
2402
2403cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002404 module_put(iscsit->owner);
2405module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002406 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002407cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002408 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002409dec_session_count:
2410 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002411 return NULL;
2412}
2413EXPORT_SYMBOL_GPL(iscsi_session_setup);
2414
2415/**
2416 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002417 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002418 *
Mike Christie75613522008-05-21 15:53:59 -05002419 * The driver must have called iscsi_remove_session before
2420 * calling this.
2421 */
Mike Christie7996a772006-04-06 21:13:41 -05002422void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2423{
Mike Christie75613522008-05-21 15:53:59 -05002424 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002425 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002426 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002427
Olaf Kirch63203772007-12-13 12:43:25 -06002428 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002429
Mike Christieb2c64162007-05-30 12:57:16 -05002430 kfree(session->password);
2431 kfree(session->password_in);
2432 kfree(session->username);
2433 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002434 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002435 kfree(session->initiatorname);
2436 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002437
Mike Christie75613522008-05-21 15:53:59 -05002438 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002439 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002440 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002441}
2442EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2443
2444/**
2445 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2446 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002447 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002448 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002449 */
Mike Christie7996a772006-04-06 21:13:41 -05002450struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002451iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2452 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002453{
Mike Christie75613522008-05-21 15:53:59 -05002454 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002455 struct iscsi_conn *conn;
2456 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002457 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002458
Mike Christie5d91e202008-05-21 15:54:01 -05002459 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2460 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002461 if (!cls_conn)
2462 return NULL;
2463 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002464 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002465
Mike Christie5d91e202008-05-21 15:54:01 -05002466 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002467 conn->session = session;
2468 conn->cls_conn = cls_conn;
2469 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2470 conn->id = conn_idx;
2471 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002472 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002473
2474 init_timer(&conn->transport_timer);
2475 conn->transport_timer.data = (unsigned long)conn;
2476 conn->transport_timer.function = iscsi_check_transport_timeouts;
2477
Mike Christie843c0a82007-12-13 12:43:20 -06002478 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002479 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002480 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002481 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002482
Mike Christie9c19a7d2008-05-21 15:54:09 -05002483 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002484 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002485 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002486 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002487 sizeof(void*))) {
2488 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002489 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002490 }
2491 spin_unlock_bh(&session->lock);
2492
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002493 data = (char *) __get_free_pages(GFP_KERNEL,
2494 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002495 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002496 goto login_task_data_alloc_fail;
2497 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002498
Mike Christie843c0a82007-12-13 12:43:20 -06002499 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002500 init_waitqueue_head(&conn->ehwait);
2501
2502 return cls_conn;
2503
Mike Christie9c19a7d2008-05-21 15:54:09 -05002504login_task_data_alloc_fail:
2505 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002506 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002507login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002508 iscsi_destroy_conn(cls_conn);
2509 return NULL;
2510}
2511EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2512
2513/**
2514 * iscsi_conn_teardown - teardown iscsi connection
2515 * cls_conn: iscsi class connection
2516 *
2517 * TODO: we may need to make this into a two step process
2518 * like scsi-mls remove + put host
2519 */
2520void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2521{
2522 struct iscsi_conn *conn = cls_conn->dd_data;
2523 struct iscsi_session *session = conn->session;
2524 unsigned long flags;
2525
Mike Christief6d51802007-12-13 12:43:30 -06002526 del_timer_sync(&conn->transport_timer);
2527
Mike Christie7996a772006-04-06 21:13:41 -05002528 spin_lock_bh(&session->lock);
2529 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2530 if (session->leadconn == conn) {
2531 /*
2532 * leading connection? then give up on recovery.
2533 */
2534 session->state = ISCSI_STATE_TERMINATE;
2535 wake_up(&conn->ehwait);
2536 }
2537 spin_unlock_bh(&session->lock);
2538
Mike Christie7996a772006-04-06 21:13:41 -05002539 /*
2540 * Block until all in-progress commands for this connection
2541 * time out or fail.
2542 */
2543 for (;;) {
2544 spin_lock_irqsave(session->host->host_lock, flags);
2545 if (!session->host->host_busy) { /* OK for ERL == 0 */
2546 spin_unlock_irqrestore(session->host->host_lock, flags);
2547 break;
2548 }
2549 spin_unlock_irqrestore(session->host->host_lock, flags);
2550 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002551 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2552 "host_busy %d host_failed %d\n",
2553 session->host->host_busy,
2554 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002555 /*
2556 * force eh_abort() to unblock
2557 */
2558 wake_up(&conn->ehwait);
2559 }
2560
Mike Christie779ea122007-02-28 17:32:15 -06002561 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002562 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002563
Mike Christie7996a772006-04-06 21:13:41 -05002564 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002565 free_pages((unsigned long) conn->data,
2566 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002567 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002568 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002569 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002570 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002571 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002572 spin_unlock_bh(&session->lock);
2573
Mike Christie7996a772006-04-06 21:13:41 -05002574 iscsi_destroy_conn(cls_conn);
2575}
2576EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2577
2578int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2579{
2580 struct iscsi_conn *conn = cls_conn->dd_data;
2581 struct iscsi_session *session = conn->session;
2582
Mike Christieffd04362006-08-31 18:09:24 -04002583 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002584 iscsi_conn_printk(KERN_ERR, conn,
2585 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002586 return -EPERM;
2587 }
2588
Mike Christiedb98ccd2006-08-31 18:09:31 -04002589 if ((session->imm_data_en || !session->initial_r2t_en) &&
2590 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002591 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2592 "first_burst %d max_burst %d\n",
2593 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002594 return -EINVAL;
2595 }
2596
Mike Christief6d51802007-12-13 12:43:30 -06002597 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002598 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2599 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002600 conn->recv_timeout = 5;
2601 }
2602
2603 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002604 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2605 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002606 conn->ping_timeout = 5;
2607 }
2608
Mike Christie7996a772006-04-06 21:13:41 -05002609 spin_lock_bh(&session->lock);
2610 conn->c_stage = ISCSI_CONN_STARTED;
2611 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002612 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002613
Mike Christief6d51802007-12-13 12:43:30 -06002614 conn->last_recv = jiffies;
2615 conn->last_ping = jiffies;
2616 if (conn->recv_timeout && conn->ping_timeout)
2617 mod_timer(&conn->transport_timer,
2618 jiffies + (conn->recv_timeout * HZ));
2619
Mike Christie7996a772006-04-06 21:13:41 -05002620 switch(conn->stop_stage) {
2621 case STOP_CONN_RECOVER:
2622 /*
2623 * unblock eh_abort() if it is blocked. re-try all
2624 * commands after successful recovery
2625 */
Mike Christie7996a772006-04-06 21:13:41 -05002626 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002627 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002628 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002629 if (session->age == 16)
2630 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002631 break;
Mike Christie7996a772006-04-06 21:13:41 -05002632 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002633 conn->stop_stage = 0;
2634 break;
Mike Christie7996a772006-04-06 21:13:41 -05002635 default:
2636 break;
2637 }
2638 spin_unlock_bh(&session->lock);
2639
Mike Christie75613522008-05-21 15:53:59 -05002640 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002641 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002642 return 0;
2643}
2644EXPORT_SYMBOL_GPL(iscsi_conn_start);
2645
2646static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002647fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002648{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002649 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002650 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002651
Mike Christie3bbaaad2009-05-13 17:57:46 -05002652 for (i = 0; i < conn->session->cmds_max; i++) {
2653 task = conn->session->cmds[i];
2654 if (task->sc)
2655 continue;
2656
2657 if (task->state == ISCSI_TASK_FREE)
2658 continue;
2659
2660 ISCSI_DBG_SESSION(conn->session,
2661 "failing mgmt itt 0x%x state %d\n",
2662 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002663 state = ISCSI_TASK_ABRT_SESS_RECOV;
2664 if (task->state == ISCSI_TASK_PENDING)
2665 state = ISCSI_TASK_COMPLETED;
2666 iscsi_complete_task(task, state);
2667
Mike Christie7996a772006-04-06 21:13:41 -05002668 }
Mike Christie7996a772006-04-06 21:13:41 -05002669}
2670
Mike Christie656cffc2006-05-18 20:31:42 -05002671static void iscsi_start_session_recovery(struct iscsi_session *session,
2672 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002673{
Mike Christieed2abc72006-05-02 19:46:40 -05002674 int old_stop_stage;
2675
Mike Christie6724add2007-08-15 01:38:30 -05002676 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002677 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002678 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002679 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002680 mutex_unlock(&session->eh_mutex);
2681 return;
2682 }
2683
2684 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002685 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002686 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002687 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002688 */
Mike Christie67a61112006-05-30 00:37:20 -05002689 if (flag == STOP_CONN_TERM)
2690 session->state = ISCSI_STATE_TERMINATE;
2691 else if (conn->stop_stage != STOP_CONN_RECOVER)
2692 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002693 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002694
Mike Christie26013ad2009-05-13 17:57:43 -05002695 del_timer_sync(&conn->transport_timer);
2696 iscsi_suspend_tx(conn);
2697
2698 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002699 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002700 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002701 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002702 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002703
Mike Christie7996a772006-04-06 21:13:41 -05002704 /*
2705 * for connection level recovery we should not calculate
2706 * header digest. conn->hdr_size used for optimization
2707 * in hdr_extract() and will be re-negotiated at
2708 * set_param() time.
2709 */
2710 if (flag == STOP_CONN_RECOVER) {
2711 conn->hdrdgst_en = 0;
2712 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002713 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002714 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002715 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002716 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002717 }
Mike Christie7996a772006-04-06 21:13:41 -05002718 }
Mike Christie656cffc2006-05-18 20:31:42 -05002719
Mike Christie656cffc2006-05-18 20:31:42 -05002720 /*
2721 * flush queues.
2722 */
2723 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002724 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002725 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002726 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002727 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002728}
Mike Christie7996a772006-04-06 21:13:41 -05002729
2730void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2731{
2732 struct iscsi_conn *conn = cls_conn->dd_data;
2733 struct iscsi_session *session = conn->session;
2734
2735 switch (flag) {
2736 case STOP_CONN_RECOVER:
2737 case STOP_CONN_TERM:
2738 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002739 break;
Mike Christie7996a772006-04-06 21:13:41 -05002740 default:
Mike Christie322d7392008-01-31 13:36:52 -06002741 iscsi_conn_printk(KERN_ERR, conn,
2742 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002743 }
2744}
2745EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2746
2747int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2748 struct iscsi_cls_conn *cls_conn, int is_leading)
2749{
Mike Christie75613522008-05-21 15:53:59 -05002750 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002751 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002752
Mike Christie7996a772006-04-06 21:13:41 -05002753 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002754 if (is_leading)
2755 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002756 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002757
2758 /*
2759 * Unblock xmitworker(), Login Phase will pass through.
2760 */
2761 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2762 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2763 return 0;
2764}
2765EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2766
Mike Christie5700b1a2009-05-13 17:57:40 -05002767static int iscsi_switch_str_param(char **param, char *new_val_buf)
2768{
2769 char *new_val;
2770
2771 if (*param) {
2772 if (!strcmp(*param, new_val_buf))
2773 return 0;
2774 }
2775
2776 new_val = kstrdup(new_val_buf, GFP_NOIO);
2777 if (!new_val)
2778 return -ENOMEM;
2779
2780 kfree(*param);
2781 *param = new_val;
2782 return 0;
2783}
Mike Christiea54a52c2006-06-28 12:00:23 -05002784
2785int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2786 enum iscsi_param param, char *buf, int buflen)
2787{
2788 struct iscsi_conn *conn = cls_conn->dd_data;
2789 struct iscsi_session *session = conn->session;
2790 uint32_t value;
2791
2792 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002793 case ISCSI_PARAM_FAST_ABORT:
2794 sscanf(buf, "%d", &session->fast_abort);
2795 break;
Mike Christief6d51802007-12-13 12:43:30 -06002796 case ISCSI_PARAM_ABORT_TMO:
2797 sscanf(buf, "%d", &session->abort_timeout);
2798 break;
2799 case ISCSI_PARAM_LU_RESET_TMO:
2800 sscanf(buf, "%d", &session->lu_reset_timeout);
2801 break;
2802 case ISCSI_PARAM_PING_TMO:
2803 sscanf(buf, "%d", &conn->ping_timeout);
2804 break;
2805 case ISCSI_PARAM_RECV_TMO:
2806 sscanf(buf, "%d", &conn->recv_timeout);
2807 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002808 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2809 sscanf(buf, "%d", &conn->max_recv_dlength);
2810 break;
2811 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2812 sscanf(buf, "%d", &conn->max_xmit_dlength);
2813 break;
2814 case ISCSI_PARAM_HDRDGST_EN:
2815 sscanf(buf, "%d", &conn->hdrdgst_en);
2816 break;
2817 case ISCSI_PARAM_DATADGST_EN:
2818 sscanf(buf, "%d", &conn->datadgst_en);
2819 break;
2820 case ISCSI_PARAM_INITIAL_R2T_EN:
2821 sscanf(buf, "%d", &session->initial_r2t_en);
2822 break;
2823 case ISCSI_PARAM_MAX_R2T:
2824 sscanf(buf, "%d", &session->max_r2t);
2825 break;
2826 case ISCSI_PARAM_IMM_DATA_EN:
2827 sscanf(buf, "%d", &session->imm_data_en);
2828 break;
2829 case ISCSI_PARAM_FIRST_BURST:
2830 sscanf(buf, "%d", &session->first_burst);
2831 break;
2832 case ISCSI_PARAM_MAX_BURST:
2833 sscanf(buf, "%d", &session->max_burst);
2834 break;
2835 case ISCSI_PARAM_PDU_INORDER_EN:
2836 sscanf(buf, "%d", &session->pdu_inorder_en);
2837 break;
2838 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2839 sscanf(buf, "%d", &session->dataseq_inorder_en);
2840 break;
2841 case ISCSI_PARAM_ERL:
2842 sscanf(buf, "%d", &session->erl);
2843 break;
2844 case ISCSI_PARAM_IFMARKER_EN:
2845 sscanf(buf, "%d", &value);
2846 BUG_ON(value);
2847 break;
2848 case ISCSI_PARAM_OFMARKER_EN:
2849 sscanf(buf, "%d", &value);
2850 BUG_ON(value);
2851 break;
2852 case ISCSI_PARAM_EXP_STATSN:
2853 sscanf(buf, "%u", &conn->exp_statsn);
2854 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002855 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002856 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002857 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002858 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002859 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002860 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002861 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002862 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002863 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002864 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002865 case ISCSI_PARAM_TPGT:
2866 sscanf(buf, "%d", &session->tpgt);
2867 break;
2868 case ISCSI_PARAM_PERSISTENT_PORT:
2869 sscanf(buf, "%d", &conn->persistent_port);
2870 break;
2871 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002872 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002873 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002874 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002875 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002876 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002877 default:
2878 return -ENOSYS;
2879 }
2880
2881 return 0;
2882}
2883EXPORT_SYMBOL_GPL(iscsi_set_param);
2884
2885int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2886 enum iscsi_param param, char *buf)
2887{
Mike Christie75613522008-05-21 15:53:59 -05002888 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002889 int len;
2890
2891 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002892 case ISCSI_PARAM_FAST_ABORT:
2893 len = sprintf(buf, "%d\n", session->fast_abort);
2894 break;
Mike Christief6d51802007-12-13 12:43:30 -06002895 case ISCSI_PARAM_ABORT_TMO:
2896 len = sprintf(buf, "%d\n", session->abort_timeout);
2897 break;
2898 case ISCSI_PARAM_LU_RESET_TMO:
2899 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2900 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002901 case ISCSI_PARAM_INITIAL_R2T_EN:
2902 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2903 break;
2904 case ISCSI_PARAM_MAX_R2T:
2905 len = sprintf(buf, "%hu\n", session->max_r2t);
2906 break;
2907 case ISCSI_PARAM_IMM_DATA_EN:
2908 len = sprintf(buf, "%d\n", session->imm_data_en);
2909 break;
2910 case ISCSI_PARAM_FIRST_BURST:
2911 len = sprintf(buf, "%u\n", session->first_burst);
2912 break;
2913 case ISCSI_PARAM_MAX_BURST:
2914 len = sprintf(buf, "%u\n", session->max_burst);
2915 break;
2916 case ISCSI_PARAM_PDU_INORDER_EN:
2917 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2918 break;
2919 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2920 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2921 break;
2922 case ISCSI_PARAM_ERL:
2923 len = sprintf(buf, "%d\n", session->erl);
2924 break;
2925 case ISCSI_PARAM_TARGET_NAME:
2926 len = sprintf(buf, "%s\n", session->targetname);
2927 break;
2928 case ISCSI_PARAM_TPGT:
2929 len = sprintf(buf, "%d\n", session->tpgt);
2930 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002931 case ISCSI_PARAM_USERNAME:
2932 len = sprintf(buf, "%s\n", session->username);
2933 break;
2934 case ISCSI_PARAM_USERNAME_IN:
2935 len = sprintf(buf, "%s\n", session->username_in);
2936 break;
2937 case ISCSI_PARAM_PASSWORD:
2938 len = sprintf(buf, "%s\n", session->password);
2939 break;
2940 case ISCSI_PARAM_PASSWORD_IN:
2941 len = sprintf(buf, "%s\n", session->password_in);
2942 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002943 case ISCSI_PARAM_IFACE_NAME:
2944 len = sprintf(buf, "%s\n", session->ifacename);
2945 break;
2946 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002947 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05002948 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002949 default:
2950 return -ENOSYS;
2951 }
2952
2953 return len;
2954}
2955EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2956
2957int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2958 enum iscsi_param param, char *buf)
2959{
2960 struct iscsi_conn *conn = cls_conn->dd_data;
2961 int len;
2962
2963 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002964 case ISCSI_PARAM_PING_TMO:
2965 len = sprintf(buf, "%u\n", conn->ping_timeout);
2966 break;
2967 case ISCSI_PARAM_RECV_TMO:
2968 len = sprintf(buf, "%u\n", conn->recv_timeout);
2969 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002970 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2971 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2972 break;
2973 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2974 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2975 break;
2976 case ISCSI_PARAM_HDRDGST_EN:
2977 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2978 break;
2979 case ISCSI_PARAM_DATADGST_EN:
2980 len = sprintf(buf, "%d\n", conn->datadgst_en);
2981 break;
2982 case ISCSI_PARAM_IFMARKER_EN:
2983 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2984 break;
2985 case ISCSI_PARAM_OFMARKER_EN:
2986 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2987 break;
2988 case ISCSI_PARAM_EXP_STATSN:
2989 len = sprintf(buf, "%u\n", conn->exp_statsn);
2990 break;
2991 case ISCSI_PARAM_PERSISTENT_PORT:
2992 len = sprintf(buf, "%d\n", conn->persistent_port);
2993 break;
2994 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2995 len = sprintf(buf, "%s\n", conn->persistent_address);
2996 break;
2997 default:
2998 return -ENOSYS;
2999 }
3000
3001 return len;
3002}
3003EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3004
Mike Christie0801c242007-05-30 12:57:12 -05003005int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3006 char *buf)
3007{
Mike Christie75613522008-05-21 15:53:59 -05003008 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003009 int len;
3010
3011 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003012 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003013 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003014 break;
Mike Christie0801c242007-05-30 12:57:12 -05003015 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003016 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003017 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003018 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003019 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003020 break;
Mike Christie75613522008-05-21 15:53:59 -05003021 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003022 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003023 break;
Mike Christie0801c242007-05-30 12:57:12 -05003024 default:
3025 return -ENOSYS;
3026 }
3027
3028 return len;
3029}
3030EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3031
3032int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3033 char *buf, int buflen)
3034{
Mike Christie75613522008-05-21 15:53:59 -05003035 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003036
3037 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003038 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003039 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003040 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003041 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003042 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003043 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003044 default:
3045 return -ENOSYS;
3046 }
3047
3048 return 0;
3049}
3050EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3051
Mike Christie7996a772006-04-06 21:13:41 -05003052MODULE_AUTHOR("Mike Christie");
3053MODULE_DESCRIPTION("iSCSI library functions");
3054MODULE_LICENSE("GPL");