blob: a7ee4bb40708bc9f7f673b84b8e015989dd2988f [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));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600304 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500305 cmd_len = sc->cmd_len;
306 if (cmd_len < ISCSI_CDB_SIZE)
307 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
308 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500309 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500310 if (rc)
311 return rc;
312 cmd_len = ISCSI_CDB_SIZE;
313 }
314 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500315
Mike Christie9c19a7d2008-05-21 15:54:09 -0500316 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500317 if (scsi_bidi_cmnd(sc)) {
318 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500319 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500320 if (rc)
321 return rc;
322 }
Mike Christie7996a772006-04-06 21:13:41 -0500323 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500324 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600325 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
326
Boaz Harroshc07d4442008-04-18 10:11:52 -0500327 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500328 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
329 /*
330 * Write counters:
331 *
332 * imm_count bytes to be sent right after
333 * SCSI PDU Header
334 *
335 * unsol_count bytes(as Data-Out) to be sent
336 * without R2T ack right after
337 * immediate data
338 *
Mike Christie577577d2008-12-02 00:32:05 -0600339 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500340 *
341 * pad_count bytes to be sent as zero-padding
342 */
Mike Christie577577d2008-12-02 00:32:05 -0600343 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500344
345 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500346 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500347 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500348 conn->max_xmit_dlength);
349 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500350 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500351 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500352 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500353 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600354 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500355
Mike Christieffd04362006-08-31 18:09:24 -0400356 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600357 r2t->data_length = min(session->first_burst, out_len) -
358 task->imm_count;
359 r2t->data_offset = task->imm_count;
360 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
361 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400362 }
363
Mike Christie577577d2008-12-02 00:32:05 -0600364 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500365 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600366 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500367 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500368 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
369 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500370 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500371
372 if (sc->sc_data_direction == DMA_FROM_DEVICE)
373 hdr->flags |= ISCSI_FLAG_CMD_READ;
374 }
375
Boaz Harrosh004d6532007-12-13 12:43:23 -0600376 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500377 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600378
379 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
380 hdrlength /= ISCSI_PAD_LEN;
381
382 WARN_ON(hdrlength >= 256);
383 hdr->hlength = hdrlength & 0xFF;
384
Mike Christie577577d2008-12-02 00:32:05 -0600385 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500386 return -EIO;
387
Mike Christie9c19a7d2008-05-21 15:54:09 -0500388 task->state = ISCSI_TASK_RUNNING;
Mike Christied3305f32009-08-20 15:10:58 -0500389 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
390 session->cmdsn++;
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 Christie8afa1432009-08-20 15:10:59 -0500860static int iscsi_nop_out_rsp(struct iscsi_task *task,
861 struct iscsi_nopin *nop, char *data, int datalen)
862{
863 struct iscsi_conn *conn = task->conn;
864 int rc = 0;
865
866 if (conn->ping_task != task) {
867 /*
868 * If this is not in response to one of our
869 * nops then it must be from userspace.
870 */
871 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
872 data, datalen))
873 rc = ISCSI_ERR_CONN_FAILED;
874 } else
875 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
876 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
877 return rc;
878}
879
Mike Christie62f38302006-08-31 18:09:27 -0400880static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
881 char *data, int datalen)
882{
883 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
884 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -0500885 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -0400886
887 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
888
Mike Christie8afa1432009-08-20 15:10:59 -0500889 if (ntoh24(reject->dlength) > datalen ||
890 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
891 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
892 "pdu. Invalid data length (pdu dlength "
893 "%u, datalen %d\n", ntoh24(reject->dlength),
894 datalen);
895 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -0400896 }
Mike Christie8afa1432009-08-20 15:10:59 -0500897 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
898 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
899
900 switch (reject->reason) {
901 case ISCSI_REASON_DATA_DIGEST_ERROR:
902 iscsi_conn_printk(KERN_ERR, conn,
903 "pdu (op 0x%x itt 0x%x) rejected "
904 "due to DataDigest error.\n",
905 rejected_pdu.itt, opcode);
906 break;
907 case ISCSI_REASON_IMM_CMD_REJECT:
908 iscsi_conn_printk(KERN_ERR, conn,
909 "pdu (op 0x%x itt 0x%x) rejected. Too many "
910 "immediate commands.\n",
911 rejected_pdu.itt, opcode);
912 /*
913 * We only send one TMF at a time so if the target could not
914 * handle it, then it should get fixed (RFC mandates that
915 * a target can handle one immediate TMF per conn).
916 *
917 * For nops-outs, we could have sent more than one if
918 * the target is sending us lots of nop-ins
919 */
920 if (opcode != ISCSI_OP_NOOP_OUT)
921 return 0;
922
923 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
924 /*
925 * nop-out in response to target's nop-out rejected.
926 * Just resend.
927 */
928 iscsi_send_nopout(conn,
929 (struct iscsi_nopin*)&rejected_pdu);
930 else {
931 struct iscsi_task *task;
932 /*
933 * Our nop as ping got dropped. We know the target
934 * and transport are ok so just clean up
935 */
936 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
937 if (!task) {
938 iscsi_conn_printk(KERN_ERR, conn,
939 "Invalid pdu reject. Could "
940 "not lookup rejected task.\n");
941 rc = ISCSI_ERR_BAD_ITT;
942 } else
943 rc = iscsi_nop_out_rsp(task,
944 (struct iscsi_nopin*)&rejected_pdu,
945 NULL, 0);
946 }
947 break;
948 default:
949 iscsi_conn_printk(KERN_ERR, conn,
950 "pdu (op 0x%x itt 0x%x) rejected. Reason "
951 "code 0x%x\n", rejected_pdu.itt,
952 rejected_pdu.opcode, reject->reason);
953 break;
954 }
955 return rc;
Mike Christie62f38302006-08-31 18:09:27 -0400956}
957
Mike Christie7996a772006-04-06 21:13:41 -0500958/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500959 * iscsi_itt_to_task - look up task by itt
960 * @conn: iscsi connection
961 * @itt: itt
962 *
963 * This should be used for mgmt tasks like login and nops, or if
964 * the LDD's itt space does not include the session age.
965 *
966 * The session lock must be held.
967 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500968struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500969{
970 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600971 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500972
973 if (itt == RESERVED_ITT)
974 return NULL;
975
Mike Christie262ef632008-12-02 00:32:13 -0600976 if (session->tt->parse_pdu_itt)
977 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
978 else
979 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500980 if (i >= session->cmds_max)
981 return NULL;
982
983 return session->cmds[i];
984}
Mike Christie8f9256c2009-05-13 17:57:41 -0500985EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500986
987/**
Mike Christie7996a772006-04-06 21:13:41 -0500988 * __iscsi_complete_pdu - complete pdu
989 * @conn: iscsi conn
990 * @hdr: iscsi header
991 * @data: data buffer
992 * @datalen: len of data buffer
993 *
994 * Completes pdu processing by freeing any resources allocated at
995 * queuecommand or send generic. session lock must be held and verify
996 * itt must have been called.
997 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500998int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
999 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001000{
1001 struct iscsi_session *session = conn->session;
1002 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001003 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001004 uint32_t itt;
1005
Mike Christief6d51802007-12-13 12:43:30 -06001006 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001007 rc = iscsi_verify_itt(conn, hdr->itt);
1008 if (rc)
1009 return rc;
1010
Al Virob4377352007-02-09 16:39:40 +00001011 if (hdr->itt != RESERVED_ITT)
1012 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001013 else
Al Virob4377352007-02-09 16:39:40 +00001014 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001015
Mike Christie1b2c7af2009-03-05 14:45:58 -06001016 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1017 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001018
Mike Christie3e5c28a2008-05-21 15:54:06 -05001019 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001020 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001021
Mike Christie7996a772006-04-06 21:13:41 -05001022 switch(opcode) {
1023 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001024 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001025 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001026 break;
1027 }
1028
Al Virob4377352007-02-09 16:39:40 +00001029 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001030 break;
1031
Mike Christief6d51802007-12-13 12:43:30 -06001032 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001033 break;
1034 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001035 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1036 break;
Mike Christie7996a772006-04-06 21:13:41 -05001037 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001038 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001039 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1040 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001041 break;
1042 default:
1043 rc = ISCSI_ERR_BAD_OPCODE;
1044 break;
1045 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001046 goto out;
1047 }
Mike Christie7996a772006-04-06 21:13:41 -05001048
Mike Christie3e5c28a2008-05-21 15:54:06 -05001049 switch(opcode) {
1050 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001051 case ISCSI_OP_SCSI_DATA_IN:
1052 task = iscsi_itt_to_ctask(conn, hdr->itt);
1053 if (!task)
1054 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001055 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001056 break;
1057 case ISCSI_OP_R2T:
1058 /*
1059 * LLD handles R2Ts if they need to.
1060 */
1061 return 0;
1062 case ISCSI_OP_LOGOUT_RSP:
1063 case ISCSI_OP_LOGIN_RSP:
1064 case ISCSI_OP_TEXT_RSP:
1065 case ISCSI_OP_SCSI_TMFUNC_RSP:
1066 case ISCSI_OP_NOOP_IN:
1067 task = iscsi_itt_to_task(conn, hdr->itt);
1068 if (!task)
1069 return ISCSI_ERR_BAD_ITT;
1070 break;
1071 default:
1072 return ISCSI_ERR_BAD_OPCODE;
1073 }
1074
1075 switch(opcode) {
1076 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001077 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001078 break;
1079 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001080 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001081 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001082 case ISCSI_OP_LOGOUT_RSP:
1083 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1084 if (datalen) {
1085 rc = ISCSI_ERR_PROTO;
1086 break;
1087 }
1088 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1089 goto recv_pdu;
1090 case ISCSI_OP_LOGIN_RSP:
1091 case ISCSI_OP_TEXT_RSP:
1092 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1093 /*
1094 * login related PDU's exp_statsn is handled in
1095 * userspace
1096 */
1097 goto recv_pdu;
1098 case ISCSI_OP_SCSI_TMFUNC_RSP:
1099 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1100 if (datalen) {
1101 rc = ISCSI_ERR_PROTO;
1102 break;
1103 }
1104
1105 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001106 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001107 break;
1108 case ISCSI_OP_NOOP_IN:
1109 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1110 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1111 rc = ISCSI_ERR_PROTO;
1112 break;
1113 }
1114 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1115
Mike Christie8afa1432009-08-20 15:10:59 -05001116 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1117 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001118 break;
1119 default:
1120 rc = ISCSI_ERR_BAD_OPCODE;
1121 break;
1122 }
1123
1124out:
1125 return rc;
1126recv_pdu:
1127 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1128 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001129 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001130 return rc;
1131}
Mike Christie913e5bf2008-05-21 15:54:18 -05001132EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001133
1134int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1135 char *data, int datalen)
1136{
1137 int rc;
1138
1139 spin_lock(&conn->session->lock);
1140 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1141 spin_unlock(&conn->session->lock);
1142 return rc;
1143}
1144EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1145
Mike Christie0af967f2008-05-21 15:54:04 -05001146int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001147{
1148 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001149 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001150
Mike Christie0af967f2008-05-21 15:54:04 -05001151 if (itt == RESERVED_ITT)
1152 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001153
Mike Christie262ef632008-12-02 00:32:13 -06001154 if (session->tt->parse_pdu_itt)
1155 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1156 else {
1157 i = get_itt(itt);
1158 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1159 }
1160
1161 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001162 iscsi_conn_printk(KERN_ERR, conn,
1163 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001164 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001165 return ISCSI_ERR_BAD_ITT;
1166 }
Mike Christie7996a772006-04-06 21:13:41 -05001167
Mike Christie3e5c28a2008-05-21 15:54:06 -05001168 if (i >= session->cmds_max) {
1169 iscsi_conn_printk(KERN_ERR, conn,
1170 "received invalid itt index %u (max cmds "
1171 "%u.\n", i, session->cmds_max);
1172 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001173 }
Mike Christie7996a772006-04-06 21:13:41 -05001174 return 0;
1175}
1176EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1177
Mike Christie913e5bf2008-05-21 15:54:18 -05001178/**
1179 * iscsi_itt_to_ctask - look up ctask by itt
1180 * @conn: iscsi connection
1181 * @itt: itt
1182 *
1183 * This should be used for cmd tasks.
1184 *
1185 * The session lock must be held.
1186 */
1187struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001188{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001189 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001190
1191 if (iscsi_verify_itt(conn, itt))
1192 return NULL;
1193
Mike Christie913e5bf2008-05-21 15:54:18 -05001194 task = iscsi_itt_to_task(conn, itt);
1195 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001196 return NULL;
1197
Mike Christie913e5bf2008-05-21 15:54:18 -05001198 if (task->sc->SCp.phase != conn->session->age) {
1199 iscsi_session_printk(KERN_ERR, conn->session,
1200 "task's session age %d, expected %d\n",
1201 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001202 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001203 }
Mike Christie0af967f2008-05-21 15:54:04 -05001204
Mike Christie9c19a7d2008-05-21 15:54:09 -05001205 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001206}
1207EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1208
Mike Christie40a06e72009-03-05 14:46:05 -06001209void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001210 enum iscsi_err err)
1211{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001212 struct iscsi_conn *conn;
1213 struct device *dev;
1214 unsigned long flags;
1215
1216 spin_lock_irqsave(&session->lock, flags);
1217 conn = session->leadconn;
1218 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1219 spin_unlock_irqrestore(&session->lock, flags);
1220 return;
1221 }
1222
1223 dev = get_device(&conn->cls_conn->dev);
1224 spin_unlock_irqrestore(&session->lock, flags);
1225 if (!dev)
1226 return;
1227 /*
1228 * if the host is being removed bypass the connection
1229 * recovery initialization because we are going to kill
1230 * the session.
1231 */
1232 if (err == ISCSI_ERR_INVALID_HOST)
1233 iscsi_conn_error_event(conn->cls_conn, err);
1234 else
1235 iscsi_conn_failure(conn, err);
1236 put_device(dev);
1237}
1238EXPORT_SYMBOL_GPL(iscsi_session_failure);
1239
Mike Christie7996a772006-04-06 21:13:41 -05001240void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1241{
1242 struct iscsi_session *session = conn->session;
1243 unsigned long flags;
1244
1245 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001246 if (session->state == ISCSI_STATE_FAILED) {
1247 spin_unlock_irqrestore(&session->lock, flags);
1248 return;
1249 }
1250
Mike Christie67a61112006-05-30 00:37:20 -05001251 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001252 session->state = ISCSI_STATE_FAILED;
1253 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001254
Mike Christie7996a772006-04-06 21:13:41 -05001255 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1256 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001257 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001258}
1259EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1260
Mike Christie77a23c22007-05-30 12:57:18 -05001261static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1262{
1263 struct iscsi_session *session = conn->session;
1264
1265 /*
1266 * Check for iSCSI window and take care of CmdSN wrap-around
1267 */
Mike Christiee0726402007-07-26 12:46:48 -05001268 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001269 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1270 "%u MaxCmdSN %u CmdSN %u/%u\n",
1271 session->exp_cmdsn, session->max_cmdsn,
1272 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001273 return -ENOSPC;
1274 }
1275 return 0;
1276}
1277
Mike Christie9c19a7d2008-05-21 15:54:09 -05001278static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001279{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001280 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001281 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001282
Mike Christie70b31c12009-08-20 15:11:03 -05001283 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1284 return -ENODATA;
1285
Mike Christie9c19a7d2008-05-21 15:54:09 -05001286 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001287 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001288 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001289 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001290 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001291 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001292 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001293 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001294 }
1295 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001296 return rc;
1297}
1298
Mike Christie7996a772006-04-06 21:13:41 -05001299/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001300 * iscsi_requeue_task - requeue task to run from session workqueue
1301 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001302 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001303 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001304 * this. The session lock must be held. This should only be called
1305 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001306 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001307void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001308{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001309 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001310
Mike Christie3bbaaad2009-05-13 17:57:46 -05001311 /*
1312 * this may be on the requeue list already if the xmit_task callout
1313 * is handling the r2ts while we are adding new ones
1314 */
1315 if (list_empty(&task->running))
1316 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001317 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001318}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001319EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001320
1321/**
Mike Christie7996a772006-04-06 21:13:41 -05001322 * iscsi_data_xmit - xmit any command into the scheduled connection
1323 * @conn: iscsi connection
1324 *
1325 * Notes:
1326 * The function can return -EAGAIN in which case the caller must
1327 * re-schedule it again later or recover. '0' return code means
1328 * successful xmit.
1329 **/
1330static int iscsi_data_xmit(struct iscsi_conn *conn)
1331{
Mike Christie3219e522006-05-30 00:37:28 -05001332 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001333
Mike Christie77a23c22007-05-30 12:57:18 -05001334 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001335 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001336 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001337 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001338 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001339 }
Mike Christie7996a772006-04-06 21:13:41 -05001340
Mike Christie9c19a7d2008-05-21 15:54:09 -05001341 if (conn->task) {
1342 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001343 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001344 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001345 }
1346
Mike Christie77a23c22007-05-30 12:57:18 -05001347 /*
1348 * process mgmt pdus like nops before commands since we should
1349 * only have one nop-out as a ping from us and targets should not
1350 * overflow us with nop-ins
1351 */
1352check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001353 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001354 conn->task = list_entry(conn->mgmtqueue.next,
1355 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001356 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001357 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1358 __iscsi_put_task(conn->task);
1359 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001360 continue;
1361 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001362 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001363 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001364 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001365 }
1366
Mike Christie843c0a82007-12-13 12:43:20 -06001367 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001368 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001369 if (conn->tmf_state == TMF_QUEUED)
1370 break;
1371
Mike Christie3bbaaad2009-05-13 17:57:46 -05001372 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001373 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001374 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001375 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001376 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001377 continue;
1378 }
Mike Christie577577d2008-12-02 00:32:05 -06001379 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1380 if (rc) {
1381 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001382 list_add_tail(&conn->task->running,
1383 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001384 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001385 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001386 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001387 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001388 continue;
1389 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001390 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001391 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001392 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001393 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001394 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001395 * we need to check the mgmt queue for nops that need to
1396 * be sent to aviod starvation
1397 */
Mike Christie843c0a82007-12-13 12:43:20 -06001398 if (!list_empty(&conn->mgmtqueue))
1399 goto check_mgmt;
1400 }
1401
1402 while (!list_empty(&conn->requeue)) {
1403 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1404 break;
1405
Mike Christieb3a7ea82007-12-13 12:43:26 -06001406 /*
1407 * we always do fastlogout - conn stop code will clean up.
1408 */
1409 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1410 break;
1411
Mike Christie9c19a7d2008-05-21 15:54:09 -05001412 conn->task = list_entry(conn->requeue.next,
1413 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001414 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001415 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001416 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001417 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001418 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001419 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001420 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001421 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001422 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001423 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001424
Mike Christie70b31c12009-08-20 15:11:03 -05001425done:
Mike Christie77a23c22007-05-30 12:57:18 -05001426 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001427 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001428}
1429
David Howellsc4028952006-11-22 14:57:56 +00001430static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001431{
David Howellsc4028952006-11-22 14:57:56 +00001432 struct iscsi_conn *conn =
1433 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001434 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001435 /*
1436 * serialize Xmit worker on a per-connection basis.
1437 */
Mike Christie3219e522006-05-30 00:37:28 -05001438 do {
1439 rc = iscsi_data_xmit(conn);
1440 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001441}
1442
Mike Christie577577d2008-12-02 00:32:05 -06001443static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1444 struct scsi_cmnd *sc)
1445{
1446 struct iscsi_task *task;
1447
1448 if (!__kfifo_get(conn->session->cmdpool.queue,
1449 (void *) &task, sizeof(void *)))
1450 return NULL;
1451
1452 sc->SCp.phase = conn->session->age;
1453 sc->SCp.ptr = (char *) task;
1454
1455 atomic_set(&task->refcount, 1);
1456 task->state = ISCSI_TASK_PENDING;
1457 task->conn = conn;
1458 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001459 task->have_checked_conn = false;
1460 task->last_timeout = jiffies;
1461 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001462 INIT_LIST_HEAD(&task->running);
1463 return task;
1464}
1465
Mike Christie7996a772006-04-06 21:13:41 -05001466enum {
1467 FAILURE_BAD_HOST = 1,
1468 FAILURE_SESSION_FAILED,
1469 FAILURE_SESSION_FREED,
1470 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001471 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001472 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001473 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001474 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001475 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001476 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001477};
1478
1479int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1480{
Mike Christie75613522008-05-21 15:53:59 -05001481 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001482 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001483 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001484 int reason = 0;
1485 struct iscsi_session *session;
1486 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001487 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001488
1489 sc->scsi_done = done;
1490 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001491 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001492
1493 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001494 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001495 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001496
Mike Christie75613522008-05-21 15:53:59 -05001497 cls_session = starget_to_session(scsi_target(sc->device));
1498 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001499 spin_lock(&session->lock);
1500
Mike Christie75613522008-05-21 15:53:59 -05001501 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001502 if (reason) {
1503 sc->result = reason;
1504 goto fault;
1505 }
1506
Mike Christie301e0f72009-05-13 17:57:47 -05001507 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001508 /*
1509 * to handle the race between when we set the recovery state
1510 * and block the session we requeue here (commands could
1511 * be entering our queuecommand while a block is starting
1512 * up because the block code is not locked)
1513 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001514 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001515 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001516 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001517 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001518 sc->result = DID_IMM_RETRY << 16;
1519 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001520 case ISCSI_STATE_LOGGING_OUT:
1521 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001522 sc->result = DID_IMM_RETRY << 16;
1523 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001524 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001525 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001526 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001527 break;
1528 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001529 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001530 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001531 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001532 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001533 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001534 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001535 }
Mike Christie7996a772006-04-06 21:13:41 -05001536 goto fault;
1537 }
1538
Mike Christie7996a772006-04-06 21:13:41 -05001539 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001540 if (!conn) {
1541 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001542 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001543 goto fault;
1544 }
Mike Christie7996a772006-04-06 21:13:41 -05001545
Mike Christie77a23c22007-05-30 12:57:18 -05001546 if (iscsi_check_cmdsn_window_closed(conn)) {
1547 reason = FAILURE_WINDOW_CLOSED;
1548 goto reject;
1549 }
1550
Mike Christie577577d2008-12-02 00:32:05 -06001551 task = iscsi_alloc_task(conn, sc);
1552 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001553 reason = FAILURE_OOM;
1554 goto reject;
1555 }
Mike Christie7996a772006-04-06 21:13:41 -05001556
Mike Christie1336aed2009-05-13 17:57:48 -05001557 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001558 reason = iscsi_prep_scsi_cmd_pdu(task);
1559 if (reason) {
1560 if (reason == -ENOMEM) {
1561 reason = FAILURE_OOM;
1562 goto prepd_reject;
1563 } else {
1564 sc->result = DID_ABORT << 16;
1565 goto prepd_fault;
1566 }
Mike Christie052d0142008-05-21 15:54:05 -05001567 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001568 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001569 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001570 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001571 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001572 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001573 } else {
1574 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001575 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001576 }
Mike Christie052d0142008-05-21 15:54:05 -05001577
1578 session->queued_cmdsn++;
1579 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001580 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001581 return 0;
1582
Mike Christie577577d2008-12-02 00:32:05 -06001583prepd_reject:
1584 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001585 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001586reject:
1587 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001588 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1589 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001590 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001591 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001592
Mike Christie577577d2008-12-02 00:32:05 -06001593prepd_fault:
1594 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001595 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001596fault:
1597 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001598 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1599 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001600 if (!scsi_bidi_cmnd(sc))
1601 scsi_set_resid(sc, scsi_bufflen(sc));
1602 else {
1603 scsi_out(sc)->resid = scsi_out(sc)->length;
1604 scsi_in(sc)->resid = scsi_in(sc)->length;
1605 }
Mike Christie052d0142008-05-21 15:54:05 -05001606 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001607 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001608 return 0;
1609}
1610EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1611
1612int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1613{
Mike Christie7996a772006-04-06 21:13:41 -05001614 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1615 return sdev->queue_depth;
1616}
1617EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1618
Mike Christie6b5d6c42009-04-21 15:32:32 -05001619int iscsi_target_alloc(struct scsi_target *starget)
1620{
1621 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1622 struct iscsi_session *session = cls_session->dd_data;
1623
1624 starget->can_queue = session->scsi_cmds_max;
1625 return 0;
1626}
1627EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1628
Mike Christie7996a772006-04-06 21:13:41 -05001629void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1630{
Mike Christie75613522008-05-21 15:53:59 -05001631 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001632
1633 spin_lock_bh(&session->lock);
1634 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001635 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001636 if (session->leadconn)
1637 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001638 }
1639 spin_unlock_bh(&session->lock);
1640}
1641EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1642
Mike Christie8e124522008-09-24 11:46:12 -05001643int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001644{
Mike Christie75613522008-05-21 15:53:59 -05001645 struct iscsi_cls_session *cls_session;
1646 struct iscsi_session *session;
1647 struct iscsi_conn *conn;
1648
1649 cls_session = starget_to_session(scsi_target(sc->device));
1650 session = cls_session->dd_data;
1651 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001652
Mike Christiebc436b22007-12-13 12:43:28 -06001653 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001654 spin_lock_bh(&session->lock);
1655 if (session->state == ISCSI_STATE_TERMINATE) {
1656failed:
Erez Zilberbd2199d2009-06-15 22:11:10 -05001657 ISCSI_DBG_EH(session,
1658 "failing target reset: Could not log back into "
1659 "target [age %d]\n",
1660 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001661 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001662 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001663 return FAILED;
1664 }
1665
Mike Christie7996a772006-04-06 21:13:41 -05001666 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001667 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001668 /*
1669 * we drop the lock here but the leadconn cannot be destoyed while
1670 * we are in the scsi eh
1671 */
Mike Christie843c0a82007-12-13 12:43:20 -06001672 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001673
Erez Zilberbd2199d2009-06-15 22:11:10 -05001674 ISCSI_DBG_EH(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001675 wait_event_interruptible(conn->ehwait,
1676 session->state == ISCSI_STATE_TERMINATE ||
1677 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001678 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001679 if (signal_pending(current))
1680 flush_signals(current);
1681
Mike Christiebc436b22007-12-13 12:43:28 -06001682 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001683 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001684 if (session->state == ISCSI_STATE_LOGGED_IN) {
1685 ISCSI_DBG_EH(session,
1686 "target reset succeeded\n");
1687 } else
Mike Christie7996a772006-04-06 21:13:41 -05001688 goto failed;
1689 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001690 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001691 return SUCCESS;
1692}
Mike Christie8e124522008-09-24 11:46:12 -05001693EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001694
Mike Christie843c0a82007-12-13 12:43:20 -06001695static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001696{
Mike Christie843c0a82007-12-13 12:43:20 -06001697 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001698 struct iscsi_session *session = conn->session;
1699
1700 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001701 if (conn->tmf_state == TMF_QUEUED) {
1702 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001703 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001704 /* unblock eh_abort() */
1705 wake_up(&conn->ehwait);
1706 }
1707 spin_unlock(&session->lock);
1708}
1709
Mike Christie9c19a7d2008-05-21 15:54:09 -05001710static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001711 struct iscsi_tm *hdr, int age,
1712 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001713{
Mike Christie7996a772006-04-06 21:13:41 -05001714 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001715 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001716
Mike Christie9c19a7d2008-05-21 15:54:09 -05001717 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001718 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001719 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001720 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001721 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001722 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001723 ISCSI_DBG_EH(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001724 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001725 }
Mike Christie843c0a82007-12-13 12:43:20 -06001726 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001727 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001728 conn->tmf_timer.function = iscsi_tmf_timedout;
1729 conn->tmf_timer.data = (unsigned long)conn;
1730 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001731 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001732
Mike Christie7996a772006-04-06 21:13:41 -05001733 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001734 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001735
1736 /*
1737 * block eh thread until:
1738 *
Mike Christie843c0a82007-12-13 12:43:20 -06001739 * 1) tmf response
1740 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001741 * 3) session is terminated or restarted or userspace has
1742 * given up on recovery
1743 */
Mike Christie843c0a82007-12-13 12:43:20 -06001744 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001745 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001746 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001747 if (signal_pending(current))
1748 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001749 del_timer_sync(&conn->tmf_timer);
1750
Mike Christie6724add2007-08-15 01:38:30 -05001751 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001752 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001753 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001754 if (age != session->age ||
1755 session->state != ISCSI_STATE_LOGGED_IN)
1756 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001757 return 0;
1758}
1759
1760/*
Mike Christie843c0a82007-12-13 12:43:20 -06001761 * Fail commands. session lock held and recv side suspended and xmit
1762 * thread flushed
1763 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001764static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1765 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001766{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001767 struct iscsi_task *task;
1768 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001769
Mike Christie3bbaaad2009-05-13 17:57:46 -05001770 for (i = 0; i < conn->session->cmds_max; i++) {
1771 task = conn->session->cmds[i];
1772 if (!task->sc || task->state == ISCSI_TASK_FREE)
1773 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001774
Mike Christie3bbaaad2009-05-13 17:57:46 -05001775 if (lun != -1 && lun != task->sc->device->lun)
1776 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001777
Mike Christie3bbaaad2009-05-13 17:57:46 -05001778 ISCSI_DBG_SESSION(conn->session,
1779 "failing sc %p itt 0x%x state %d\n",
1780 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001781 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001782 }
1783}
1784
Mike Christieb40977d2008-05-21 15:54:03 -05001785void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001786{
Mike Christie32ae7632009-03-05 14:46:03 -06001787 struct Scsi_Host *shost = conn->session->host;
1788 struct iscsi_host *ihost = shost_priv(shost);
1789
Mike Christie6724add2007-08-15 01:38:30 -05001790 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001791 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001792 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001793}
Mike Christieb40977d2008-05-21 15:54:03 -05001794EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001795
1796static void iscsi_start_tx(struct iscsi_conn *conn)
1797{
1798 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001799 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001800}
1801
Mike Christie4c48a822009-05-13 17:57:45 -05001802/*
1803 * We want to make sure a ping is in flight. It has timed out.
1804 * And we are not busy processing a pdu that is making
1805 * progress but got started before the ping and is taking a while
1806 * to complete so the ping is just stuck behind it in a queue.
1807 */
1808static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1809{
1810 if (conn->ping_task &&
1811 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1812 (conn->ping_timeout * HZ), jiffies))
1813 return 1;
1814 else
1815 return 0;
1816}
1817
Mike Christied355e572009-06-15 22:11:08 -05001818static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001819{
Mike Christied355e572009-06-15 22:11:08 -05001820 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1821 struct iscsi_task *task = NULL;
Mike Christief6d51802007-12-13 12:43:30 -06001822 struct iscsi_cls_session *cls_session;
1823 struct iscsi_session *session;
1824 struct iscsi_conn *conn;
Mike Christief6d51802007-12-13 12:43:30 -06001825
Mike Christied355e572009-06-15 22:11:08 -05001826 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001827 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001828
Erez Zilberbd2199d2009-06-15 22:11:10 -05001829 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001830
1831 spin_lock(&session->lock);
1832 if (session->state != ISCSI_STATE_LOGGED_IN) {
1833 /*
1834 * We are probably in the middle of iscsi recovery so let
1835 * that complete and handle the error.
1836 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001837 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001838 goto done;
1839 }
1840
1841 conn = session->leadconn;
1842 if (!conn) {
1843 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001844 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001845 goto done;
1846 }
1847
Mike Christied355e572009-06-15 22:11:08 -05001848 task = (struct iscsi_task *)sc->SCp.ptr;
1849 if (!task)
1850 goto done;
1851 /*
1852 * If we have sent (at least queued to the network layer) a pdu or
1853 * recvd one for the task since the last timeout ask for
1854 * more time. If on the next timeout we have not made progress
1855 * we can check if it is the task or connection when we send the
1856 * nop as a ping.
1857 */
1858 if (time_after_eq(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001859 ISCSI_DBG_EH(session, "Command making progress. Asking "
1860 "scsi-ml for more time to complete. "
1861 "Last data recv at %lu. Last timeout was at "
1862 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001863 task->have_checked_conn = false;
1864 rc = BLK_EH_RESET_TIMER;
1865 goto done;
1866 }
1867
Mike Christief6d51802007-12-13 12:43:30 -06001868 if (!conn->recv_timeout && !conn->ping_timeout)
1869 goto done;
1870 /*
1871 * if the ping timedout then we are in the middle of cleaning up
1872 * and can let the iscsi eh handle it
1873 */
Mike Christie4c48a822009-05-13 17:57:45 -05001874 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001875 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001876 goto done;
1877 }
Mike Christied355e572009-06-15 22:11:08 -05001878
1879 /* Assumes nop timeout is shorter than scsi cmd timeout */
1880 if (task->have_checked_conn)
1881 goto done;
1882
Mike Christief6d51802007-12-13 12:43:30 -06001883 /*
Mike Christied355e572009-06-15 22:11:08 -05001884 * Checking the transport already or nop from a cmd timeout still
1885 * running
Mike Christief6d51802007-12-13 12:43:30 -06001886 */
Mike Christied355e572009-06-15 22:11:08 -05001887 if (conn->ping_task) {
1888 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001889 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001890 goto done;
1891 }
1892
Mike Christied355e572009-06-15 22:11:08 -05001893 /* Make sure there is a transport check done */
1894 iscsi_send_nopout(conn, NULL);
1895 task->have_checked_conn = true;
1896 rc = BLK_EH_RESET_TIMER;
1897
Mike Christief6d51802007-12-13 12:43:30 -06001898done:
Mike Christied355e572009-06-15 22:11:08 -05001899 if (task)
1900 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06001901 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001902 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1903 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001904 return rc;
1905}
1906
1907static void iscsi_check_transport_timeouts(unsigned long data)
1908{
1909 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1910 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001911 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001912
1913 spin_lock(&session->lock);
1914 if (session->state != ISCSI_STATE_LOGGED_IN)
1915 goto done;
1916
Mike Christie4cf10432008-05-07 20:43:52 -05001917 recv_timeout = conn->recv_timeout;
1918 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001919 goto done;
1920
Mike Christie4cf10432008-05-07 20:43:52 -05001921 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001922 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001923
1924 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001925 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001926 "expired, recv timeout %d, last rx %lu, "
1927 "last ping %lu, now %lu\n",
1928 conn->ping_timeout, conn->recv_timeout,
1929 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001930 spin_unlock(&session->lock);
1931 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1932 return;
1933 }
1934
Mike Christie4cf10432008-05-07 20:43:52 -05001935 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001936 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001937 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001938 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001939 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001940 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001941 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001942
Mike Christie1b2c7af2009-03-05 14:45:58 -06001943 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001944 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001945done:
1946 spin_unlock(&session->lock);
1947}
1948
Mike Christie9c19a7d2008-05-21 15:54:09 -05001949static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001950 struct iscsi_tm *hdr)
1951{
1952 memset(hdr, 0, sizeof(*hdr));
1953 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1954 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1955 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001956 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1957 hdr->rtt = task->hdr_itt;
1958 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001959}
1960
Mike Christie7996a772006-04-06 21:13:41 -05001961int iscsi_eh_abort(struct scsi_cmnd *sc)
1962{
Mike Christie75613522008-05-21 15:53:59 -05001963 struct iscsi_cls_session *cls_session;
1964 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001965 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001966 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001967 struct iscsi_tm *hdr;
1968 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001969
Mike Christie75613522008-05-21 15:53:59 -05001970 cls_session = starget_to_session(scsi_target(sc->device));
1971 session = cls_session->dd_data;
1972
Erez Zilberbd2199d2009-06-15 22:11:10 -05001973 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05001974
Mike Christie6724add2007-08-15 01:38:30 -05001975 mutex_lock(&session->eh_mutex);
1976 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001977 /*
1978 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1979 * got the command.
1980 */
1981 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001982 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
1983 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001984 spin_unlock_bh(&session->lock);
1985 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001986 return SUCCESS;
1987 }
1988
Mike Christie7996a772006-04-06 21:13:41 -05001989 /*
1990 * If we are not logged in or we have started a new session
1991 * then let the host reset code handle this
1992 */
Mike Christie843c0a82007-12-13 12:43:20 -06001993 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1994 sc->SCp.phase != session->age) {
1995 spin_unlock_bh(&session->lock);
1996 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001997 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05001998 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06001999 return FAILED;
2000 }
2001
2002 conn = session->leadconn;
2003 conn->eh_abort_cnt++;
2004 age = session->age;
2005
Mike Christie9c19a7d2008-05-21 15:54:09 -05002006 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002007 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2008 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002009
Mike Christie9c19a7d2008-05-21 15:54:09 -05002010 /* task completed before time out */
2011 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002012 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002013 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002014 }
Mike Christie7996a772006-04-06 21:13:41 -05002015
Mike Christie9c19a7d2008-05-21 15:54:09 -05002016 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002017 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002018 goto success;
2019 }
Mike Christie7996a772006-04-06 21:13:41 -05002020
Mike Christie843c0a82007-12-13 12:43:20 -06002021 /* only have one tmf outstanding at a time */
2022 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002023 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002024 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002025
Mike Christie843c0a82007-12-13 12:43:20 -06002026 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002027 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002028
Mike Christie9c19a7d2008-05-21 15:54:09 -05002029 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002030 rc = FAILED;
2031 goto failed;
2032 }
2033
2034 switch (conn->tmf_state) {
2035 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002036 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002037 /*
2038 * stop tx side incase the target had sent a abort rsp but
2039 * the initiator was still writing out data.
2040 */
Mike Christie6724add2007-08-15 01:38:30 -05002041 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002042 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002043 * we do not stop the recv side because targets have been
2044 * good and have never sent us a successful tmf response
2045 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002046 */
Mike Christie6187c242009-07-15 15:02:57 -05002047 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002048 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002049 conn->tmf_state = TMF_INITIAL;
Mike Christie6187c242009-07-15 15:02:57 -05002050 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002051 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002052 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002053 case TMF_TIMEDOUT:
2054 spin_unlock_bh(&session->lock);
2055 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2056 goto failed_unlocked;
2057 case TMF_NOT_FOUND:
2058 if (!sc->SCp.ptr) {
2059 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002060 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002061 ISCSI_DBG_EH(session, "sc completed while abort in "
2062 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002063 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002064 }
2065 /* fall through */
2066 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002067 conn->tmf_state = TMF_INITIAL;
2068 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002069 }
2070
Mike Christie77a23c22007-05-30 12:57:18 -05002071success:
Mike Christie7996a772006-04-06 21:13:41 -05002072 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002073success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002074 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2075 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002076 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002077 return SUCCESS;
2078
2079failed:
2080 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002081failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002082 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2083 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002084 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002085 return FAILED;
2086}
2087EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2088
Mike Christie843c0a82007-12-13 12:43:20 -06002089static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2090{
2091 memset(hdr, 0, sizeof(*hdr));
2092 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2093 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2094 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2095 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002096 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002097}
2098
2099int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2100{
Mike Christie75613522008-05-21 15:53:59 -05002101 struct iscsi_cls_session *cls_session;
2102 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002103 struct iscsi_conn *conn;
2104 struct iscsi_tm *hdr;
2105 int rc = FAILED;
2106
Mike Christie75613522008-05-21 15:53:59 -05002107 cls_session = starget_to_session(scsi_target(sc->device));
2108 session = cls_session->dd_data;
2109
Erez Zilberbd2199d2009-06-15 22:11:10 -05002110 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002111
2112 mutex_lock(&session->eh_mutex);
2113 spin_lock_bh(&session->lock);
2114 /*
2115 * Just check if we are not logged in. We cannot check for
2116 * the phase because the reset could come from a ioctl.
2117 */
2118 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2119 goto unlock;
2120 conn = session->leadconn;
2121
2122 /* only have one tmf outstanding at a time */
2123 if (conn->tmf_state != TMF_INITIAL)
2124 goto unlock;
2125 conn->tmf_state = TMF_QUEUED;
2126
2127 hdr = &conn->tmhdr;
2128 iscsi_prep_lun_reset_pdu(sc, hdr);
2129
Mike Christie9c19a7d2008-05-21 15:54:09 -05002130 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002131 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002132 rc = FAILED;
2133 goto unlock;
2134 }
2135
2136 switch (conn->tmf_state) {
2137 case TMF_SUCCESS:
2138 break;
2139 case TMF_TIMEDOUT:
2140 spin_unlock_bh(&session->lock);
2141 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2142 goto done;
2143 default:
2144 conn->tmf_state = TMF_INITIAL;
2145 goto unlock;
2146 }
2147
2148 rc = SUCCESS;
2149 spin_unlock_bh(&session->lock);
2150
2151 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002152
Mike Christiea3439142008-09-24 11:46:15 -05002153 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002154 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002155 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002156 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002157
2158 iscsi_start_tx(conn);
2159 goto done;
2160
2161unlock:
2162 spin_unlock_bh(&session->lock);
2163done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002164 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2165 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002166 mutex_unlock(&session->eh_mutex);
2167 return rc;
2168}
2169EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2170
Olaf Kirch63203772007-12-13 12:43:25 -06002171/*
2172 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2173 * should be accessed via kfifo_{get,put} on q->queue.
2174 * Optionally, the caller can obtain the array of object pointers
2175 * by passing in a non-NULL @items pointer
2176 */
Mike Christie7996a772006-04-06 21:13:41 -05002177int
Olaf Kirch63203772007-12-13 12:43:25 -06002178iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002179{
Olaf Kirch63203772007-12-13 12:43:25 -06002180 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002181
Olaf Kirch63203772007-12-13 12:43:25 -06002182 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002183
2184 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002185
2186 /* If the user passed an items pointer, he wants a copy of
2187 * the array. */
2188 if (items)
2189 num_arrays++;
2190 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2191 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002192 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002193
2194 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2195 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002196 if (IS_ERR(q->queue)) {
2197 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002198 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002199 }
Mike Christie7996a772006-04-06 21:13:41 -05002200
2201 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002202 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002203 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002204 q->max = i;
2205 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002206 }
Mike Christie7996a772006-04-06 21:13:41 -05002207 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2208 }
Olaf Kirch63203772007-12-13 12:43:25 -06002209
2210 if (items) {
2211 *items = q->pool + max;
2212 memcpy(*items, q->pool, max * sizeof(void *));
2213 }
2214
Mike Christie7996a772006-04-06 21:13:41 -05002215 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002216
2217enomem:
2218 iscsi_pool_free(q);
2219 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002220}
2221EXPORT_SYMBOL_GPL(iscsi_pool_init);
2222
Olaf Kirch63203772007-12-13 12:43:25 -06002223void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002224{
2225 int i;
2226
2227 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002228 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002229 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002230 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002231}
2232EXPORT_SYMBOL_GPL(iscsi_pool_free);
2233
Mike Christiea4804cd2008-05-21 15:54:00 -05002234/**
2235 * iscsi_host_add - add host to system
2236 * @shost: scsi host
2237 * @pdev: parent device
2238 *
2239 * This should be called by partial offload and software iscsi drivers
2240 * to add a host to the system.
2241 */
2242int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002243{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002244 if (!shost->can_queue)
2245 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2246
Mike Christie4d108352009-03-05 14:46:04 -06002247 if (!shost->cmd_per_lun)
2248 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2249
Mike Christie308cec12009-02-06 12:06:20 -06002250 if (!shost->transportt->eh_timed_out)
2251 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002252 return scsi_add_host(shost, pdev);
2253}
2254EXPORT_SYMBOL_GPL(iscsi_host_add);
2255
2256/**
2257 * iscsi_host_alloc - allocate a host and driver data
2258 * @sht: scsi host template
2259 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002260 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002261 *
2262 * This should be called by partial offload and software iscsi drivers.
2263 * To access the driver specific memory use the iscsi_host_priv() macro.
2264 */
2265struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002266 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002267{
2268 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002269 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002270
2271 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2272 if (!shost)
2273 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002274 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002275
2276 if (xmit_can_sleep) {
2277 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2278 "iscsi_q_%d", shost->host_no);
2279 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2280 if (!ihost->workq)
2281 goto free_host;
2282 }
2283
Mike Christiee5bd7b52008-09-24 11:46:10 -05002284 spin_lock_init(&ihost->lock);
2285 ihost->state = ISCSI_HOST_SETUP;
2286 ihost->num_sessions = 0;
2287 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002288 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002289
2290free_host:
2291 scsi_host_put(shost);
2292 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002293}
Mike Christiea4804cd2008-05-21 15:54:00 -05002294EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002295
Mike Christiee5bd7b52008-09-24 11:46:10 -05002296static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2297{
Mike Christie40a06e72009-03-05 14:46:05 -06002298 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002299}
2300
Mike Christiea4804cd2008-05-21 15:54:00 -05002301/**
2302 * iscsi_host_remove - remove host and sessions
2303 * @shost: scsi host
2304 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002305 * If there are any sessions left, this will initiate the removal and wait
2306 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002307 */
2308void iscsi_host_remove(struct Scsi_Host *shost)
2309{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002310 struct iscsi_host *ihost = shost_priv(shost);
2311 unsigned long flags;
2312
2313 spin_lock_irqsave(&ihost->lock, flags);
2314 ihost->state = ISCSI_HOST_REMOVED;
2315 spin_unlock_irqrestore(&ihost->lock, flags);
2316
2317 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2318 wait_event_interruptible(ihost->session_removal_wq,
2319 ihost->num_sessions == 0);
2320 if (signal_pending(current))
2321 flush_signals(current);
2322
Mike Christiea4804cd2008-05-21 15:54:00 -05002323 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002324 if (ihost->workq)
2325 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002326}
2327EXPORT_SYMBOL_GPL(iscsi_host_remove);
2328
2329void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002330{
2331 struct iscsi_host *ihost = shost_priv(shost);
2332
2333 kfree(ihost->netdev);
2334 kfree(ihost->hwaddress);
2335 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002336 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002337}
Mike Christiea4804cd2008-05-21 15:54:00 -05002338EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002339
Mike Christiee5bd7b52008-09-24 11:46:10 -05002340static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2341{
2342 struct iscsi_host *ihost = shost_priv(shost);
2343 unsigned long flags;
2344
2345 shost = scsi_host_get(shost);
2346 if (!shost) {
2347 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2348 "of session teardown event because host already "
2349 "removed.\n");
2350 return;
2351 }
2352
2353 spin_lock_irqsave(&ihost->lock, flags);
2354 ihost->num_sessions--;
2355 if (ihost->num_sessions == 0)
2356 wake_up(&ihost->session_removal_wq);
2357 spin_unlock_irqrestore(&ihost->lock, flags);
2358 scsi_host_put(shost);
2359}
2360
Mike Christie75613522008-05-21 15:53:59 -05002361/**
2362 * iscsi_session_setup - create iscsi cls session and host and session
2363 * @iscsit: iscsi transport template
2364 * @shost: scsi host
2365 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002366 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002367 * @initial_cmdsn: initial CmdSN
2368 *
2369 * This can be used by software iscsi_transports that allocate
2370 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002371 *
2372 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2373 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2374 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002375 */
2376struct iscsi_cls_session *
2377iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002378 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002379 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002380{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002381 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002382 struct iscsi_session *session;
2383 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002384 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002385 unsigned long flags;
2386
2387 spin_lock_irqsave(&ihost->lock, flags);
2388 if (ihost->state == ISCSI_HOST_REMOVED) {
2389 spin_unlock_irqrestore(&ihost->lock, flags);
2390 return NULL;
2391 }
2392 ihost->num_sessions++;
2393 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002394
2395 if (!total_cmds)
2396 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002397 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002398 * The iscsi layer needs some tasks for nop handling and tmfs,
2399 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2400 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002401 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002402 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2403 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2404 "must be a power of two that is at least %d.\n",
2405 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002406 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002407 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002408
2409 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2410 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2411 "must be a power of 2 less than or equal to %d.\n",
2412 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2413 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2414 }
2415
2416 if (!is_power_of_2(total_cmds)) {
2417 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2418 "must be a power of 2.\n", total_cmds);
2419 total_cmds = rounddown_pow_of_two(total_cmds);
2420 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2421 return NULL;
2422 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2423 total_cmds);
2424 }
2425 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002426
Mike Christie5d91e202008-05-21 15:54:01 -05002427 cls_session = iscsi_alloc_session(shost, iscsit,
2428 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002429 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002430 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002431 session = cls_session->dd_data;
2432 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002433 session->host = shost;
2434 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002435 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002436 session->lu_reset_timeout = 15;
2437 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002438 session->scsi_cmds_max = scsi_cmds;
2439 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002440 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002441 session->exp_cmdsn = initial_cmdsn + 1;
2442 session->max_cmdsn = initial_cmdsn + 1;
2443 session->max_r2t = 1;
2444 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002445 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002446 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002447
2448 /* initialize SCSI PDU commands pool */
2449 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2450 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002451 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002452 goto cmdpool_alloc_fail;
2453
2454 /* pre-format cmds pool with ITT */
2455 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002456 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002457
Mike Christie9c19a7d2008-05-21 15:54:09 -05002458 if (cmd_task_size)
2459 task->dd_data = &task[1];
2460 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002461 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002462 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002463 }
2464
Mike Christief53a88d2006-06-28 12:00:27 -05002465 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002466 goto module_get_fail;
2467
Mike Christie79706342008-05-21 15:54:12 -05002468 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002469 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002470
Mike Christie7996a772006-04-06 21:13:41 -05002471 return cls_session;
2472
2473cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002474 module_put(iscsit->owner);
2475module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002476 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002477cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002478 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002479dec_session_count:
2480 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002481 return NULL;
2482}
2483EXPORT_SYMBOL_GPL(iscsi_session_setup);
2484
2485/**
2486 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002487 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002488 *
Mike Christie75613522008-05-21 15:53:59 -05002489 * The driver must have called iscsi_remove_session before
2490 * calling this.
2491 */
Mike Christie7996a772006-04-06 21:13:41 -05002492void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2493{
Mike Christie75613522008-05-21 15:53:59 -05002494 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002495 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002496 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002497
Olaf Kirch63203772007-12-13 12:43:25 -06002498 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002499
Mike Christieb2c64162007-05-30 12:57:16 -05002500 kfree(session->password);
2501 kfree(session->password_in);
2502 kfree(session->username);
2503 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002504 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002505 kfree(session->initiatorname);
2506 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002507
Mike Christie75613522008-05-21 15:53:59 -05002508 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002509 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002510 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002511}
2512EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2513
2514/**
2515 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2516 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002517 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002518 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002519 */
Mike Christie7996a772006-04-06 21:13:41 -05002520struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002521iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2522 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002523{
Mike Christie75613522008-05-21 15:53:59 -05002524 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002525 struct iscsi_conn *conn;
2526 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002527 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002528
Mike Christie5d91e202008-05-21 15:54:01 -05002529 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2530 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002531 if (!cls_conn)
2532 return NULL;
2533 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002534 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002535
Mike Christie5d91e202008-05-21 15:54:01 -05002536 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002537 conn->session = session;
2538 conn->cls_conn = cls_conn;
2539 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2540 conn->id = conn_idx;
2541 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002542 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002543
2544 init_timer(&conn->transport_timer);
2545 conn->transport_timer.data = (unsigned long)conn;
2546 conn->transport_timer.function = iscsi_check_transport_timeouts;
2547
Mike Christie843c0a82007-12-13 12:43:20 -06002548 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002549 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002550 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002551 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002552
Mike Christie9c19a7d2008-05-21 15:54:09 -05002553 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002554 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002555 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002556 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002557 sizeof(void*))) {
2558 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002559 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002560 }
2561 spin_unlock_bh(&session->lock);
2562
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002563 data = (char *) __get_free_pages(GFP_KERNEL,
2564 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002565 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002566 goto login_task_data_alloc_fail;
2567 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002568
Mike Christie843c0a82007-12-13 12:43:20 -06002569 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002570 init_waitqueue_head(&conn->ehwait);
2571
2572 return cls_conn;
2573
Mike Christie9c19a7d2008-05-21 15:54:09 -05002574login_task_data_alloc_fail:
2575 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002576 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002577login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002578 iscsi_destroy_conn(cls_conn);
2579 return NULL;
2580}
2581EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2582
2583/**
2584 * iscsi_conn_teardown - teardown iscsi connection
2585 * cls_conn: iscsi class connection
2586 *
2587 * TODO: we may need to make this into a two step process
2588 * like scsi-mls remove + put host
2589 */
2590void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2591{
2592 struct iscsi_conn *conn = cls_conn->dd_data;
2593 struct iscsi_session *session = conn->session;
2594 unsigned long flags;
2595
Mike Christief6d51802007-12-13 12:43:30 -06002596 del_timer_sync(&conn->transport_timer);
2597
Mike Christie7996a772006-04-06 21:13:41 -05002598 spin_lock_bh(&session->lock);
2599 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2600 if (session->leadconn == conn) {
2601 /*
2602 * leading connection? then give up on recovery.
2603 */
2604 session->state = ISCSI_STATE_TERMINATE;
2605 wake_up(&conn->ehwait);
2606 }
2607 spin_unlock_bh(&session->lock);
2608
Mike Christie7996a772006-04-06 21:13:41 -05002609 /*
2610 * Block until all in-progress commands for this connection
2611 * time out or fail.
2612 */
2613 for (;;) {
2614 spin_lock_irqsave(session->host->host_lock, flags);
2615 if (!session->host->host_busy) { /* OK for ERL == 0 */
2616 spin_unlock_irqrestore(session->host->host_lock, flags);
2617 break;
2618 }
2619 spin_unlock_irqrestore(session->host->host_lock, flags);
2620 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002621 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2622 "host_busy %d host_failed %d\n",
2623 session->host->host_busy,
2624 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002625 /*
2626 * force eh_abort() to unblock
2627 */
2628 wake_up(&conn->ehwait);
2629 }
2630
Mike Christie779ea122007-02-28 17:32:15 -06002631 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002632 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002633
Mike Christie7996a772006-04-06 21:13:41 -05002634 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002635 free_pages((unsigned long) conn->data,
2636 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002637 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002638 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002639 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002640 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002641 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002642 spin_unlock_bh(&session->lock);
2643
Mike Christie7996a772006-04-06 21:13:41 -05002644 iscsi_destroy_conn(cls_conn);
2645}
2646EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2647
2648int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2649{
2650 struct iscsi_conn *conn = cls_conn->dd_data;
2651 struct iscsi_session *session = conn->session;
2652
Mike Christieffd04362006-08-31 18:09:24 -04002653 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002654 iscsi_conn_printk(KERN_ERR, conn,
2655 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002656 return -EPERM;
2657 }
2658
Mike Christiedb98ccd2006-08-31 18:09:31 -04002659 if ((session->imm_data_en || !session->initial_r2t_en) &&
2660 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002661 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2662 "first_burst %d max_burst %d\n",
2663 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002664 return -EINVAL;
2665 }
2666
Mike Christief6d51802007-12-13 12:43:30 -06002667 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002668 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2669 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002670 conn->recv_timeout = 5;
2671 }
2672
2673 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002674 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2675 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002676 conn->ping_timeout = 5;
2677 }
2678
Mike Christie7996a772006-04-06 21:13:41 -05002679 spin_lock_bh(&session->lock);
2680 conn->c_stage = ISCSI_CONN_STARTED;
2681 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002682 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002683
Mike Christief6d51802007-12-13 12:43:30 -06002684 conn->last_recv = jiffies;
2685 conn->last_ping = jiffies;
2686 if (conn->recv_timeout && conn->ping_timeout)
2687 mod_timer(&conn->transport_timer,
2688 jiffies + (conn->recv_timeout * HZ));
2689
Mike Christie7996a772006-04-06 21:13:41 -05002690 switch(conn->stop_stage) {
2691 case STOP_CONN_RECOVER:
2692 /*
2693 * unblock eh_abort() if it is blocked. re-try all
2694 * commands after successful recovery
2695 */
Mike Christie7996a772006-04-06 21:13:41 -05002696 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002697 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002698 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002699 if (session->age == 16)
2700 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002701 break;
Mike Christie7996a772006-04-06 21:13:41 -05002702 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002703 conn->stop_stage = 0;
2704 break;
Mike Christie7996a772006-04-06 21:13:41 -05002705 default:
2706 break;
2707 }
2708 spin_unlock_bh(&session->lock);
2709
Mike Christie75613522008-05-21 15:53:59 -05002710 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002711 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002712 return 0;
2713}
2714EXPORT_SYMBOL_GPL(iscsi_conn_start);
2715
2716static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002717fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002718{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002719 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002720 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002721
Mike Christie3bbaaad2009-05-13 17:57:46 -05002722 for (i = 0; i < conn->session->cmds_max; i++) {
2723 task = conn->session->cmds[i];
2724 if (task->sc)
2725 continue;
2726
2727 if (task->state == ISCSI_TASK_FREE)
2728 continue;
2729
2730 ISCSI_DBG_SESSION(conn->session,
2731 "failing mgmt itt 0x%x state %d\n",
2732 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002733 state = ISCSI_TASK_ABRT_SESS_RECOV;
2734 if (task->state == ISCSI_TASK_PENDING)
2735 state = ISCSI_TASK_COMPLETED;
2736 iscsi_complete_task(task, state);
2737
Mike Christie7996a772006-04-06 21:13:41 -05002738 }
Mike Christie7996a772006-04-06 21:13:41 -05002739}
2740
Mike Christie656cffc2006-05-18 20:31:42 -05002741static void iscsi_start_session_recovery(struct iscsi_session *session,
2742 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002743{
Mike Christieed2abc72006-05-02 19:46:40 -05002744 int old_stop_stage;
2745
Mike Christie6724add2007-08-15 01:38:30 -05002746 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002747 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002748 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002749 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002750 mutex_unlock(&session->eh_mutex);
2751 return;
2752 }
2753
2754 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002755 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002756 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002757 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002758 */
Mike Christie67a61112006-05-30 00:37:20 -05002759 if (flag == STOP_CONN_TERM)
2760 session->state = ISCSI_STATE_TERMINATE;
2761 else if (conn->stop_stage != STOP_CONN_RECOVER)
2762 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002763 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002764
Mike Christie26013ad2009-05-13 17:57:43 -05002765 del_timer_sync(&conn->transport_timer);
2766 iscsi_suspend_tx(conn);
2767
2768 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002769 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002770 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002771 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002772 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002773
Mike Christie7996a772006-04-06 21:13:41 -05002774 /*
2775 * for connection level recovery we should not calculate
2776 * header digest. conn->hdr_size used for optimization
2777 * in hdr_extract() and will be re-negotiated at
2778 * set_param() time.
2779 */
2780 if (flag == STOP_CONN_RECOVER) {
2781 conn->hdrdgst_en = 0;
2782 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002783 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002784 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002785 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002786 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002787 }
Mike Christie7996a772006-04-06 21:13:41 -05002788 }
Mike Christie656cffc2006-05-18 20:31:42 -05002789
Mike Christie656cffc2006-05-18 20:31:42 -05002790 /*
2791 * flush queues.
2792 */
2793 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002794 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002795 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002796 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002797 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002798}
Mike Christie7996a772006-04-06 21:13:41 -05002799
2800void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2801{
2802 struct iscsi_conn *conn = cls_conn->dd_data;
2803 struct iscsi_session *session = conn->session;
2804
2805 switch (flag) {
2806 case STOP_CONN_RECOVER:
2807 case STOP_CONN_TERM:
2808 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002809 break;
Mike Christie7996a772006-04-06 21:13:41 -05002810 default:
Mike Christie322d7392008-01-31 13:36:52 -06002811 iscsi_conn_printk(KERN_ERR, conn,
2812 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002813 }
2814}
2815EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2816
2817int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2818 struct iscsi_cls_conn *cls_conn, int is_leading)
2819{
Mike Christie75613522008-05-21 15:53:59 -05002820 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002821 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002822
Mike Christie7996a772006-04-06 21:13:41 -05002823 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002824 if (is_leading)
2825 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002826 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002827
2828 /*
2829 * Unblock xmitworker(), Login Phase will pass through.
2830 */
2831 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2832 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2833 return 0;
2834}
2835EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2836
Mike Christie5700b1a2009-05-13 17:57:40 -05002837static int iscsi_switch_str_param(char **param, char *new_val_buf)
2838{
2839 char *new_val;
2840
2841 if (*param) {
2842 if (!strcmp(*param, new_val_buf))
2843 return 0;
2844 }
2845
2846 new_val = kstrdup(new_val_buf, GFP_NOIO);
2847 if (!new_val)
2848 return -ENOMEM;
2849
2850 kfree(*param);
2851 *param = new_val;
2852 return 0;
2853}
Mike Christiea54a52c2006-06-28 12:00:23 -05002854
2855int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2856 enum iscsi_param param, char *buf, int buflen)
2857{
2858 struct iscsi_conn *conn = cls_conn->dd_data;
2859 struct iscsi_session *session = conn->session;
2860 uint32_t value;
2861
2862 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002863 case ISCSI_PARAM_FAST_ABORT:
2864 sscanf(buf, "%d", &session->fast_abort);
2865 break;
Mike Christief6d51802007-12-13 12:43:30 -06002866 case ISCSI_PARAM_ABORT_TMO:
2867 sscanf(buf, "%d", &session->abort_timeout);
2868 break;
2869 case ISCSI_PARAM_LU_RESET_TMO:
2870 sscanf(buf, "%d", &session->lu_reset_timeout);
2871 break;
2872 case ISCSI_PARAM_PING_TMO:
2873 sscanf(buf, "%d", &conn->ping_timeout);
2874 break;
2875 case ISCSI_PARAM_RECV_TMO:
2876 sscanf(buf, "%d", &conn->recv_timeout);
2877 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002878 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2879 sscanf(buf, "%d", &conn->max_recv_dlength);
2880 break;
2881 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2882 sscanf(buf, "%d", &conn->max_xmit_dlength);
2883 break;
2884 case ISCSI_PARAM_HDRDGST_EN:
2885 sscanf(buf, "%d", &conn->hdrdgst_en);
2886 break;
2887 case ISCSI_PARAM_DATADGST_EN:
2888 sscanf(buf, "%d", &conn->datadgst_en);
2889 break;
2890 case ISCSI_PARAM_INITIAL_R2T_EN:
2891 sscanf(buf, "%d", &session->initial_r2t_en);
2892 break;
2893 case ISCSI_PARAM_MAX_R2T:
2894 sscanf(buf, "%d", &session->max_r2t);
2895 break;
2896 case ISCSI_PARAM_IMM_DATA_EN:
2897 sscanf(buf, "%d", &session->imm_data_en);
2898 break;
2899 case ISCSI_PARAM_FIRST_BURST:
2900 sscanf(buf, "%d", &session->first_burst);
2901 break;
2902 case ISCSI_PARAM_MAX_BURST:
2903 sscanf(buf, "%d", &session->max_burst);
2904 break;
2905 case ISCSI_PARAM_PDU_INORDER_EN:
2906 sscanf(buf, "%d", &session->pdu_inorder_en);
2907 break;
2908 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2909 sscanf(buf, "%d", &session->dataseq_inorder_en);
2910 break;
2911 case ISCSI_PARAM_ERL:
2912 sscanf(buf, "%d", &session->erl);
2913 break;
2914 case ISCSI_PARAM_IFMARKER_EN:
2915 sscanf(buf, "%d", &value);
2916 BUG_ON(value);
2917 break;
2918 case ISCSI_PARAM_OFMARKER_EN:
2919 sscanf(buf, "%d", &value);
2920 BUG_ON(value);
2921 break;
2922 case ISCSI_PARAM_EXP_STATSN:
2923 sscanf(buf, "%u", &conn->exp_statsn);
2924 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002925 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002926 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002927 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002928 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002929 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002930 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002931 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002932 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002933 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002934 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002935 case ISCSI_PARAM_TPGT:
2936 sscanf(buf, "%d", &session->tpgt);
2937 break;
2938 case ISCSI_PARAM_PERSISTENT_PORT:
2939 sscanf(buf, "%d", &conn->persistent_port);
2940 break;
2941 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002942 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002943 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002944 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002945 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002946 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002947 default:
2948 return -ENOSYS;
2949 }
2950
2951 return 0;
2952}
2953EXPORT_SYMBOL_GPL(iscsi_set_param);
2954
2955int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2956 enum iscsi_param param, char *buf)
2957{
Mike Christie75613522008-05-21 15:53:59 -05002958 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002959 int len;
2960
2961 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002962 case ISCSI_PARAM_FAST_ABORT:
2963 len = sprintf(buf, "%d\n", session->fast_abort);
2964 break;
Mike Christief6d51802007-12-13 12:43:30 -06002965 case ISCSI_PARAM_ABORT_TMO:
2966 len = sprintf(buf, "%d\n", session->abort_timeout);
2967 break;
2968 case ISCSI_PARAM_LU_RESET_TMO:
2969 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2970 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002971 case ISCSI_PARAM_INITIAL_R2T_EN:
2972 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2973 break;
2974 case ISCSI_PARAM_MAX_R2T:
2975 len = sprintf(buf, "%hu\n", session->max_r2t);
2976 break;
2977 case ISCSI_PARAM_IMM_DATA_EN:
2978 len = sprintf(buf, "%d\n", session->imm_data_en);
2979 break;
2980 case ISCSI_PARAM_FIRST_BURST:
2981 len = sprintf(buf, "%u\n", session->first_burst);
2982 break;
2983 case ISCSI_PARAM_MAX_BURST:
2984 len = sprintf(buf, "%u\n", session->max_burst);
2985 break;
2986 case ISCSI_PARAM_PDU_INORDER_EN:
2987 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2988 break;
2989 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2990 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2991 break;
2992 case ISCSI_PARAM_ERL:
2993 len = sprintf(buf, "%d\n", session->erl);
2994 break;
2995 case ISCSI_PARAM_TARGET_NAME:
2996 len = sprintf(buf, "%s\n", session->targetname);
2997 break;
2998 case ISCSI_PARAM_TPGT:
2999 len = sprintf(buf, "%d\n", session->tpgt);
3000 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003001 case ISCSI_PARAM_USERNAME:
3002 len = sprintf(buf, "%s\n", session->username);
3003 break;
3004 case ISCSI_PARAM_USERNAME_IN:
3005 len = sprintf(buf, "%s\n", session->username_in);
3006 break;
3007 case ISCSI_PARAM_PASSWORD:
3008 len = sprintf(buf, "%s\n", session->password);
3009 break;
3010 case ISCSI_PARAM_PASSWORD_IN:
3011 len = sprintf(buf, "%s\n", session->password_in);
3012 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003013 case ISCSI_PARAM_IFACE_NAME:
3014 len = sprintf(buf, "%s\n", session->ifacename);
3015 break;
3016 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003017 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003018 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003019 default:
3020 return -ENOSYS;
3021 }
3022
3023 return len;
3024}
3025EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3026
3027int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3028 enum iscsi_param param, char *buf)
3029{
3030 struct iscsi_conn *conn = cls_conn->dd_data;
3031 int len;
3032
3033 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003034 case ISCSI_PARAM_PING_TMO:
3035 len = sprintf(buf, "%u\n", conn->ping_timeout);
3036 break;
3037 case ISCSI_PARAM_RECV_TMO:
3038 len = sprintf(buf, "%u\n", conn->recv_timeout);
3039 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003040 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3041 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3042 break;
3043 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3044 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3045 break;
3046 case ISCSI_PARAM_HDRDGST_EN:
3047 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3048 break;
3049 case ISCSI_PARAM_DATADGST_EN:
3050 len = sprintf(buf, "%d\n", conn->datadgst_en);
3051 break;
3052 case ISCSI_PARAM_IFMARKER_EN:
3053 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3054 break;
3055 case ISCSI_PARAM_OFMARKER_EN:
3056 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3057 break;
3058 case ISCSI_PARAM_EXP_STATSN:
3059 len = sprintf(buf, "%u\n", conn->exp_statsn);
3060 break;
3061 case ISCSI_PARAM_PERSISTENT_PORT:
3062 len = sprintf(buf, "%d\n", conn->persistent_port);
3063 break;
3064 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3065 len = sprintf(buf, "%s\n", conn->persistent_address);
3066 break;
3067 default:
3068 return -ENOSYS;
3069 }
3070
3071 return len;
3072}
3073EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3074
Mike Christie0801c242007-05-30 12:57:12 -05003075int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3076 char *buf)
3077{
Mike Christie75613522008-05-21 15:53:59 -05003078 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003079 int len;
3080
3081 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003082 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003083 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003084 break;
Mike Christie0801c242007-05-30 12:57:12 -05003085 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003086 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003087 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003088 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003089 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003090 break;
Mike Christie75613522008-05-21 15:53:59 -05003091 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003092 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003093 break;
Mike Christie0801c242007-05-30 12:57:12 -05003094 default:
3095 return -ENOSYS;
3096 }
3097
3098 return len;
3099}
3100EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3101
3102int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3103 char *buf, int buflen)
3104{
Mike Christie75613522008-05-21 15:53:59 -05003105 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003106
3107 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003108 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003109 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003110 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003111 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003112 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003113 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003114 default:
3115 return -ENOSYS;
3116 }
3117
3118 return 0;
3119}
3120EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3121
Mike Christie7996a772006-04-06 21:13:41 -05003122MODULE_AUTHOR("Mike Christie");
3123MODULE_DESCRIPTION("iSCSI library functions");
3124MODULE_LICENSE("GPL");