blob: b55b7991d5fa65c2ac4367ff8fa519e4df2f6492 [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Mike Christie1b2c7af2009-03-05 14:45:58 -060041static int iscsi_dbg_lib;
42module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
48 do { \
49 if (iscsi_dbg_lib) \
50 iscsi_conn_printk(KERN_INFO, _conn, \
51 "%s " dbg_fmt, \
52 __func__, ##arg); \
53 } while (0);
54
55#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (iscsi_dbg_lib) \
58 iscsi_session_printk(KERN_INFO, _session, \
59 "%s " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
64#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050065
Mike Christie77a23c22007-05-30 12:57:18 -050066static int iscsi_sna_lt(u32 n1, u32 n2)
67{
68 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
69 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
70}
71
72/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
73static int iscsi_sna_lte(u32 n1, u32 n2)
74{
75 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
76 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
77}
78
Mike Christie32ae7632009-03-05 14:46:03 -060079inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
80{
81 struct Scsi_Host *shost = conn->session->host;
82 struct iscsi_host *ihost = shost_priv(shost);
83
Mike Christie1336aed2009-05-13 17:57:48 -050084 if (ihost->workq)
85 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -060086}
87EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
88
Mike Christie77a23c22007-05-30 12:57:18 -050089void
90iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050091{
92 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
93 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
94
Mike Christie77a23c22007-05-30 12:57:18 -050095 /*
96 * standard specifies this check for when to update expected and
97 * max sequence numbers
98 */
99 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
100 return;
101
102 if (exp_cmdsn != session->exp_cmdsn &&
103 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500104 session->exp_cmdsn = exp_cmdsn;
105
Mike Christie77a23c22007-05-30 12:57:18 -0500106 if (max_cmdsn != session->max_cmdsn &&
107 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
108 session->max_cmdsn = max_cmdsn;
109 /*
110 * if the window closed with IO queued, then kick the
111 * xmit thread
112 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500113 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500114 !list_empty(&session->leadconn->mgmtqueue))
115 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500116 }
Mike Christie7996a772006-04-06 21:13:41 -0500117}
Mike Christie77a23c22007-05-30 12:57:18 -0500118EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500119
Mike Christie577577d2008-12-02 00:32:05 -0600120/**
121 * iscsi_prep_data_out_pdu - initialize Data-Out
122 * @task: scsi command task
123 * @r2t: R2T info
124 * @hdr: iscsi data in pdu
125 *
126 * Notes:
127 * Initialize Data-Out within this R2T sequence and finds
128 * proper data_offset within this SCSI command.
129 *
130 * This function is called with connection lock taken.
131 **/
132void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
133 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500134{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500135 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600136 unsigned int left = r2t->data_length - r2t->sent;
137
138 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500139
140 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600141 hdr->ttt = r2t->ttt;
142 hdr->datasn = cpu_to_be32(r2t->datasn);
143 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500144 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600145 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
146 hdr->itt = task->hdr_itt;
147 hdr->exp_statsn = r2t->exp_statsn;
148 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
149 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500150 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600151 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500152 hdr->flags = 0;
153 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600154 hton24(hdr->dlength, left);
155 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500156 hdr->flags = ISCSI_FLAG_CMD_FINAL;
157 }
Mike Christie577577d2008-12-02 00:32:05 -0600158 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500159}
Mike Christie577577d2008-12-02 00:32:05 -0600160EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500161
Mike Christie9c19a7d2008-05-21 15:54:09 -0500162static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600163{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500164 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600165
Mike Christie9c19a7d2008-05-21 15:54:09 -0500166 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600167 WARN_ON(1);
168 return -EINVAL;
169 }
170
171 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500172 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600173 return 0;
174}
175
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500176/*
177 * make an extended cdb AHS
178 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500179static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500180{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500181 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500182 unsigned rlen, pad_len;
183 unsigned short ahslength;
184 struct iscsi_ecdb_ahdr *ecdb_ahdr;
185 int rc;
186
Mike Christie9c19a7d2008-05-21 15:54:09 -0500187 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500188 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
189
190 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
191 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
192
193 pad_len = iscsi_padding(rlen);
194
Mike Christie9c19a7d2008-05-21 15:54:09 -0500195 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500196 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
197 if (rc)
198 return rc;
199
200 if (pad_len)
201 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
202
203 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
204 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
205 ecdb_ahdr->reserved = 0;
206 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
207
Mike Christie1b2c7af2009-03-05 14:45:58 -0600208 ISCSI_DBG_SESSION(task->conn->session,
209 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
210 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
211 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
212 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500213 return 0;
214}
215
Mike Christie9c19a7d2008-05-21 15:54:09 -0500216static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500217{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500218 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500219 struct iscsi_rlength_ahdr *rlen_ahdr;
220 int rc;
221
Mike Christie9c19a7d2008-05-21 15:54:09 -0500222 rlen_ahdr = iscsi_next_hdr(task);
223 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500224 if (rc)
225 return rc;
226
227 rlen_ahdr->ahslength =
228 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
229 sizeof(rlen_ahdr->reserved));
230 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
231 rlen_ahdr->reserved = 0;
232 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
233
Mike Christie1b2c7af2009-03-05 14:45:58 -0600234 ISCSI_DBG_SESSION(task->conn->session,
235 "bidi-in rlen_ahdr->read_length(%d) "
236 "rlen_ahdr->ahslength(%d)\n",
237 be32_to_cpu(rlen_ahdr->read_length),
238 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500239 return 0;
240}
241
Mike Christie7996a772006-04-06 21:13:41 -0500242/**
243 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500245 *
246 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
247 * fields like dlength or final based on how much data it sends
248 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500249static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500250{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500251 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500252 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500253 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600254 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500255 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600256 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600257 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500258
Mike Christie184b57c2009-05-13 17:57:39 -0500259 if (conn->session->tt->alloc_pdu) {
260 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
261 if (rc)
262 return rc;
263 }
Mike Christie577577d2008-12-02 00:32:05 -0600264 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600265 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600266 memset(hdr, 0, sizeof(*hdr));
267
Mike Christie2ff79d52008-12-02 00:32:14 -0600268 if (session->tt->parse_pdu_itt)
269 hdr->itt = task->hdr_itt = itt;
270 else
271 hdr->itt = task->hdr_itt = build_itt(task->itt,
272 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500273 task->hdr_len = 0;
274 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600275 if (rc)
276 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600277 hdr->opcode = ISCSI_OP_SCSI_CMD;
278 hdr->flags = ISCSI_ATTR_SIMPLE;
279 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600280 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600281 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600282 session->cmdsn++;
283 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500284 cmd_len = sc->cmd_len;
285 if (cmd_len < ISCSI_CDB_SIZE)
286 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
287 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500288 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500289 if (rc)
290 return rc;
291 cmd_len = ISCSI_CDB_SIZE;
292 }
293 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500294
Mike Christie9c19a7d2008-05-21 15:54:09 -0500295 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500296 if (scsi_bidi_cmnd(sc)) {
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500298 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500299 if (rc)
300 return rc;
301 }
Mike Christie7996a772006-04-06 21:13:41 -0500302 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500303 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600304 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
305
Boaz Harroshc07d4442008-04-18 10:11:52 -0500306 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500307 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
308 /*
309 * Write counters:
310 *
311 * imm_count bytes to be sent right after
312 * SCSI PDU Header
313 *
314 * unsol_count bytes(as Data-Out) to be sent
315 * without R2T ack right after
316 * immediate data
317 *
Mike Christie577577d2008-12-02 00:32:05 -0600318 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500319 *
320 * pad_count bytes to be sent as zero-padding
321 */
Mike Christie577577d2008-12-02 00:32:05 -0600322 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500323
324 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500325 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500326 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500327 conn->max_xmit_dlength);
328 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500329 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500330 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500331 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500332 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600333 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500334
Mike Christieffd04362006-08-31 18:09:24 -0400335 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600336 r2t->data_length = min(session->first_burst, out_len) -
337 task->imm_count;
338 r2t->data_offset = task->imm_count;
339 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
340 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400341 }
342
Mike Christie577577d2008-12-02 00:32:05 -0600343 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500344 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600345 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500346 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500347 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
348 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500349 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500350
351 if (sc->sc_data_direction == DMA_FROM_DEVICE)
352 hdr->flags |= ISCSI_FLAG_CMD_READ;
353 }
354
Boaz Harrosh004d6532007-12-13 12:43:23 -0600355 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500356 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600357
358 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
359 hdrlength /= ISCSI_PAD_LEN;
360
361 WARN_ON(hdrlength >= 256);
362 hdr->hlength = hdrlength & 0xFF;
363
Mike Christie577577d2008-12-02 00:32:05 -0600364 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500365 return -EIO;
366
Mike Christie9c19a7d2008-05-21 15:54:09 -0500367 task->state = ISCSI_TASK_RUNNING;
Mike Christie77a23c22007-05-30 12:57:18 -0500368
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600370 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
371 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
372 scsi_bidi_cmnd(sc) ? "bidirectional" :
373 sc->sc_data_direction == DMA_TO_DEVICE ?
374 "write" : "read", conn->id, sc, sc->cmnd[0],
375 task->itt, scsi_bufflen(sc),
376 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
377 session->cmdsn,
378 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600379 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500380}
Mike Christie7996a772006-04-06 21:13:41 -0500381
382/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500383 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500385 *
386 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500387 * This function returns the scsi command to scsi-ml or cleans
388 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500389 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500390static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500391{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500392 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600393 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500394 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500395
Mike Christie4421c9e2009-05-13 17:57:50 -0500396 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
397 task->itt, task->state, task->sc);
398
Mike Christie577577d2008-12-02 00:32:05 -0600399 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500400 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500402 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500403 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500404 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500405 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500406 return;
407
Mike Christie9c19a7d2008-05-21 15:54:09 -0500408 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500409
Mike Christie3e5c28a2008-05-21 15:54:06 -0500410 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500411 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500412 /* SCSI eh reuses commands to verify us */
413 sc->SCp.ptr = NULL;
414 /*
415 * queue command may call this to free the task, but
416 * not have setup the sc callback
417 */
418 if (sc->scsi_done)
419 sc->scsi_done(sc);
420 }
Mike Christie7996a772006-04-06 21:13:41 -0500421}
422
Mike Christie913e5bf2008-05-21 15:54:18 -0500423void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400424{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500425 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400426}
Mike Christie913e5bf2008-05-21 15:54:18 -0500427EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400428
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400430{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500432 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400433}
434
Mike Christie9c19a7d2008-05-21 15:54:09 -0500435void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500436{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500437 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500438
439 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500440 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500441 spin_unlock_bh(&session->lock);
442}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500443EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500444
Mike Christie3bbaaad2009-05-13 17:57:46 -0500445/**
446 * iscsi_complete_task - finish a task
447 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500448 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500449 *
450 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600451 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500452static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600453{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500454 struct iscsi_conn *conn = task->conn;
455
Mike Christie4421c9e2009-05-13 17:57:50 -0500456 ISCSI_DBG_SESSION(conn->session,
457 "complete task itt 0x%x state %d sc %p\n",
458 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500459 if (task->state == ISCSI_TASK_COMPLETED ||
460 task->state == ISCSI_TASK_ABRT_TMF ||
461 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500462 return;
463 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500464 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500465
466 if (!list_empty(&task->running))
467 list_del_init(&task->running);
468
469 if (conn->task == task)
470 conn->task = NULL;
471
472 if (conn->ping_task == task)
473 conn->ping_task = NULL;
474
475 /* release get from queueing */
476 __iscsi_put_task(task);
477}
478
479/*
480 * session lock must be held and if not called for a task that is
481 * still pending or from the xmit thread, then xmit thread must
482 * be suspended.
483 */
484static void fail_scsi_task(struct iscsi_task *task, int err)
485{
486 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600487 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500488 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600489
Mike Christie3bbaaad2009-05-13 17:57:46 -0500490 /*
491 * if a command completes and we get a successful tmf response
492 * we will hit this because the scsi eh abort code does not take
493 * a ref to the task.
494 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500495 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600496 if (!sc)
497 return;
498
Mike Christieb3cd5052009-05-13 17:57:49 -0500499 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600500 /*
501 * cmd never made it to the xmit thread, so we should not count
502 * the cmd in the sequencing
503 */
504 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500505 /* it was never sent so just complete like normal */
506 state = ISCSI_TASK_COMPLETED;
507 } else if (err == DID_TRANSPORT_DISRUPTED)
508 state = ISCSI_TASK_ABRT_SESS_RECOV;
509 else
510 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600511
Mike Christieb3cd5052009-05-13 17:57:49 -0500512 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500513 if (!scsi_bidi_cmnd(sc))
514 scsi_set_resid(sc, scsi_bufflen(sc));
515 else {
516 scsi_out(sc)->resid = scsi_out(sc)->length;
517 scsi_in(sc)->resid = scsi_in(sc)->length;
518 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500519
Mike Christieb3cd5052009-05-13 17:57:49 -0500520 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600521}
522
Mike Christie3e5c28a2008-05-21 15:54:06 -0500523static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500525{
526 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600527 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500528 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
529
530 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
531 return -ENOTCONN;
532
533 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
534 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
535 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
536 /*
537 * pre-format CmdSN for outgoing PDU.
538 */
539 nop->cmdsn = cpu_to_be32(session->cmdsn);
540 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500541 /*
542 * TODO: We always use immediate, so we never hit this.
543 * If we start to send tmfs or nops as non-immediate then
544 * we should start checking the cmdsn numbers for mgmt tasks.
545 */
546 if (conn->c_stage == ISCSI_CONN_STARTED &&
547 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
548 session->queued_cmdsn++;
549 session->cmdsn++;
550 }
551 }
552
Mike Christieae15f802008-12-02 00:32:15 -0600553 if (session->tt->init_task && session->tt->init_task(task))
554 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500555
556 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
557 session->state = ISCSI_STATE_LOGGING_OUT;
558
Mike Christie577577d2008-12-02 00:32:05 -0600559 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600560 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
561 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
562 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500563 return 0;
564}
565
Mike Christie9c19a7d2008-05-21 15:54:09 -0500566static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600567__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
568 char *data, uint32_t data_size)
569{
570 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500571 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500572 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600573 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600574
575 if (session->state == ISCSI_STATE_TERMINATE)
576 return NULL;
577
578 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
579 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
580 /*
581 * Login and Text are sent serially, in
582 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500583 * Same task can be used. Same ITT must be used.
584 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600585 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500586 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600587 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500588 if (session->state != ISCSI_STATE_LOGGED_IN)
589 return NULL;
590
Mike Christief6d51802007-12-13 12:43:30 -0600591 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
592 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
593
Mike Christie3e5c28a2008-05-21 15:54:06 -0500594 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500595 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600596 return NULL;
597 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500598 /*
599 * released in complete pdu for task we expect a response for, and
600 * released by the lld when it has transmitted the task for
601 * pdus we do not expect a response for.
602 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500603 atomic_set(&task->refcount, 1);
604 task->conn = conn;
605 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500606 INIT_LIST_HEAD(&task->running);
607 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600608
609 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500610 memcpy(task->data, data, data_size);
611 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600612 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500613 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600614
Mike Christie184b57c2009-05-13 17:57:39 -0500615 if (conn->session->tt->alloc_pdu) {
616 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
617 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
618 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500619 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500620 }
Mike Christie577577d2008-12-02 00:32:05 -0600621 }
Mike Christie184b57c2009-05-13 17:57:39 -0500622
Mike Christie262ef632008-12-02 00:32:13 -0600623 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600624 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500625 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600626
627 if (hdr->itt != RESERVED_ITT) {
628 if (session->tt->parse_pdu_itt)
629 task->hdr->itt = itt;
630 else
631 task->hdr->itt = build_itt(task->itt,
632 task->conn->session->age);
633 }
634
Mike Christie1336aed2009-05-13 17:57:48 -0500635 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600636 if (iscsi_prep_mgmt_task(conn, task))
637 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500638
Mike Christie9c19a7d2008-05-21 15:54:09 -0500639 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600640 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500641 } else {
642 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600643 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500644 }
Mike Christie052d0142008-05-21 15:54:05 -0500645
Mike Christie9c19a7d2008-05-21 15:54:09 -0500646 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600647
648free_task:
649 __iscsi_put_task(task);
650 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600651}
652
653int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
654 char *data, uint32_t data_size)
655{
656 struct iscsi_conn *conn = cls_conn->dd_data;
657 struct iscsi_session *session = conn->session;
658 int err = 0;
659
660 spin_lock_bh(&session->lock);
661 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
662 err = -EPERM;
663 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600664 return err;
665}
666EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
667
Mike Christie7996a772006-04-06 21:13:41 -0500668/**
669 * iscsi_cmd_rsp - SCSI Command Response processing
670 * @conn: iscsi connection
671 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500672 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500673 * @data: cmd data buffer
674 * @datalen: len of buffer
675 *
676 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500677 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500678 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500679static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500680 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500681 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500682{
Mike Christie7996a772006-04-06 21:13:41 -0500683 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
684 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500685 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500686
Mike Christie77a23c22007-05-30 12:57:18 -0500687 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500688 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
689
690 sc->result = (DID_OK << 16) | rhdr->cmd_status;
691
692 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
693 sc->result = DID_ERROR << 16;
694 goto out;
695 }
696
697 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600698 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500699
700 if (datalen < 2) {
701invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600702 iscsi_conn_printk(KERN_ERR, conn,
703 "Got CHECK_CONDITION but invalid data "
704 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500705 sc->result = DID_BAD_TARGET << 16;
706 goto out;
707 }
708
Harvey Harrison8f3339912008-05-21 15:54:20 -0500709 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500710 if (datalen < senselen)
711 goto invalid_datalen;
712
713 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600714 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600715 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
716 min_t(uint16_t, senselen,
717 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500718 }
719
Boaz Harroshc07d4442008-04-18 10:11:52 -0500720 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
721 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
722 int res_count = be32_to_cpu(rhdr->bi_residual_count);
723
724 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
725 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
726 res_count <= scsi_in(sc)->length))
727 scsi_in(sc)->resid = res_count;
728 else
729 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
730 }
731
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600732 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
733 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500734 int res_count = be32_to_cpu(rhdr->residual_count);
735
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600736 if (res_count > 0 &&
737 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
738 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500739 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900740 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500741 else
742 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500743 }
Mike Christie7996a772006-04-06 21:13:41 -0500744out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500745 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600746 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500747 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500748 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500749}
750
Mike Christie1d9edf02008-09-24 11:46:09 -0500751/**
752 * iscsi_data_in_rsp - SCSI Data-In Response processing
753 * @conn: iscsi connection
754 * @hdr: iscsi pdu
755 * @task: scsi command task
756 **/
757static void
758iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
759 struct iscsi_task *task)
760{
761 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
762 struct scsi_cmnd *sc = task->sc;
763
764 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
765 return;
766
Mike Christieedbc9aa052009-05-13 17:57:42 -0500767 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500768 sc->result = (DID_OK << 16) | rhdr->cmd_status;
769 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
770 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
771 ISCSI_FLAG_DATA_OVERFLOW)) {
772 int res_count = be32_to_cpu(rhdr->residual_count);
773
774 if (res_count > 0 &&
775 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
776 res_count <= scsi_in(sc)->length))
777 scsi_in(sc)->resid = res_count;
778 else
779 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
780 }
781
Mike Christie3bbaaad2009-05-13 17:57:46 -0500782 ISCSI_DBG_SESSION(conn->session, "data in with status done "
783 "[sc %p res %d itt 0x%x]\n",
784 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500785 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500786 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500787}
788
Mike Christie7ea8b822006-07-24 15:47:22 -0500789static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
790{
791 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
792
793 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
794 conn->tmfrsp_pdus_cnt++;
795
Mike Christie843c0a82007-12-13 12:43:20 -0600796 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500797 return;
798
799 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600800 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500801 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600802 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500803 else
Mike Christie843c0a82007-12-13 12:43:20 -0600804 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500805 wake_up(&conn->ehwait);
806}
807
Mike Christief6d51802007-12-13 12:43:30 -0600808static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
809{
810 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500811 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600812
Mike Christie9c19a7d2008-05-21 15:54:09 -0500813 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600814 return;
815
816 memset(&hdr, 0, sizeof(struct iscsi_nopout));
817 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
818 hdr.flags = ISCSI_FLAG_CMD_FINAL;
819
820 if (rhdr) {
821 memcpy(hdr.lun, rhdr->lun, 8);
822 hdr.ttt = rhdr->ttt;
823 hdr.itt = RESERVED_ITT;
824 } else
825 hdr.ttt = RESERVED_ITT;
826
Mike Christie9c19a7d2008-05-21 15:54:09 -0500827 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
828 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600829 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600830 else if (!rhdr) {
831 /* only track our nops */
832 conn->ping_task = task;
833 conn->last_ping = jiffies;
834 }
Mike Christief6d51802007-12-13 12:43:30 -0600835}
836
Mike Christie62f38302006-08-31 18:09:27 -0400837static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
838 char *data, int datalen)
839{
840 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
841 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400842
843 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
844
845 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
846 if (ntoh24(reject->dlength) > datalen)
847 return ISCSI_ERR_PROTO;
848
849 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
850 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600851 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600852 "pdu (op 0x%x) rejected "
853 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600854 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400855 }
856 }
857 return 0;
858}
859
Mike Christie7996a772006-04-06 21:13:41 -0500860/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500861 * iscsi_itt_to_task - look up task by itt
862 * @conn: iscsi connection
863 * @itt: itt
864 *
865 * This should be used for mgmt tasks like login and nops, or if
866 * the LDD's itt space does not include the session age.
867 *
868 * The session lock must be held.
869 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500870struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500871{
872 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600873 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500874
875 if (itt == RESERVED_ITT)
876 return NULL;
877
Mike Christie262ef632008-12-02 00:32:13 -0600878 if (session->tt->parse_pdu_itt)
879 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
880 else
881 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500882 if (i >= session->cmds_max)
883 return NULL;
884
885 return session->cmds[i];
886}
Mike Christie8f9256c2009-05-13 17:57:41 -0500887EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500888
889/**
Mike Christie7996a772006-04-06 21:13:41 -0500890 * __iscsi_complete_pdu - complete pdu
891 * @conn: iscsi conn
892 * @hdr: iscsi header
893 * @data: data buffer
894 * @datalen: len of data buffer
895 *
896 * Completes pdu processing by freeing any resources allocated at
897 * queuecommand or send generic. session lock must be held and verify
898 * itt must have been called.
899 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500900int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
901 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500902{
903 struct iscsi_session *session = conn->session;
904 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500905 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500906 uint32_t itt;
907
Mike Christief6d51802007-12-13 12:43:30 -0600908 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500909 rc = iscsi_verify_itt(conn, hdr->itt);
910 if (rc)
911 return rc;
912
Al Virob4377352007-02-09 16:39:40 +0000913 if (hdr->itt != RESERVED_ITT)
914 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500915 else
Al Virob4377352007-02-09 16:39:40 +0000916 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500917
Mike Christie1b2c7af2009-03-05 14:45:58 -0600918 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
919 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500920
Mike Christie3e5c28a2008-05-21 15:54:06 -0500921 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500922 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400923
Mike Christie7996a772006-04-06 21:13:41 -0500924 switch(opcode) {
925 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500926 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500927 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500928 break;
929 }
930
Al Virob4377352007-02-09 16:39:40 +0000931 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500932 break;
933
Mike Christief6d51802007-12-13 12:43:30 -0600934 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500935 break;
936 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400937 rc = iscsi_handle_reject(conn, hdr, data, datalen);
938 break;
Mike Christie7996a772006-04-06 21:13:41 -0500939 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500940 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400941 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
942 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500943 break;
944 default:
945 rc = ISCSI_ERR_BAD_OPCODE;
946 break;
947 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500948 goto out;
949 }
Mike Christie7996a772006-04-06 21:13:41 -0500950
Mike Christie3e5c28a2008-05-21 15:54:06 -0500951 switch(opcode) {
952 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500953 case ISCSI_OP_SCSI_DATA_IN:
954 task = iscsi_itt_to_ctask(conn, hdr->itt);
955 if (!task)
956 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -0500957 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -0500958 break;
959 case ISCSI_OP_R2T:
960 /*
961 * LLD handles R2Ts if they need to.
962 */
963 return 0;
964 case ISCSI_OP_LOGOUT_RSP:
965 case ISCSI_OP_LOGIN_RSP:
966 case ISCSI_OP_TEXT_RSP:
967 case ISCSI_OP_SCSI_TMFUNC_RSP:
968 case ISCSI_OP_NOOP_IN:
969 task = iscsi_itt_to_task(conn, hdr->itt);
970 if (!task)
971 return ISCSI_ERR_BAD_ITT;
972 break;
973 default:
974 return ISCSI_ERR_BAD_OPCODE;
975 }
976
977 switch(opcode) {
978 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500979 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500980 break;
981 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500982 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500983 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500984 case ISCSI_OP_LOGOUT_RSP:
985 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
986 if (datalen) {
987 rc = ISCSI_ERR_PROTO;
988 break;
989 }
990 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
991 goto recv_pdu;
992 case ISCSI_OP_LOGIN_RSP:
993 case ISCSI_OP_TEXT_RSP:
994 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
995 /*
996 * login related PDU's exp_statsn is handled in
997 * userspace
998 */
999 goto recv_pdu;
1000 case ISCSI_OP_SCSI_TMFUNC_RSP:
1001 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1002 if (datalen) {
1003 rc = ISCSI_ERR_PROTO;
1004 break;
1005 }
1006
1007 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001008 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001009 break;
1010 case ISCSI_OP_NOOP_IN:
1011 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1012 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1013 rc = ISCSI_ERR_PROTO;
1014 break;
1015 }
1016 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1017
Mike Christie9c19a7d2008-05-21 15:54:09 -05001018 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -05001019 /*
1020 * If this is not in response to one of our
1021 * nops then it must be from userspace.
1022 */
1023 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001024
1025 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
Mike Christieb3cd5052009-05-13 17:57:49 -05001026 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001027 break;
1028 default:
1029 rc = ISCSI_ERR_BAD_OPCODE;
1030 break;
1031 }
1032
1033out:
1034 return rc;
1035recv_pdu:
1036 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1037 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001038 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001039 return rc;
1040}
Mike Christie913e5bf2008-05-21 15:54:18 -05001041EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001042
1043int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1044 char *data, int datalen)
1045{
1046 int rc;
1047
1048 spin_lock(&conn->session->lock);
1049 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1050 spin_unlock(&conn->session->lock);
1051 return rc;
1052}
1053EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1054
Mike Christie0af967f2008-05-21 15:54:04 -05001055int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001056{
1057 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001058 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001059
Mike Christie0af967f2008-05-21 15:54:04 -05001060 if (itt == RESERVED_ITT)
1061 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001062
Mike Christie262ef632008-12-02 00:32:13 -06001063 if (session->tt->parse_pdu_itt)
1064 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1065 else {
1066 i = get_itt(itt);
1067 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1068 }
1069
1070 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001071 iscsi_conn_printk(KERN_ERR, conn,
1072 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001073 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001074 return ISCSI_ERR_BAD_ITT;
1075 }
Mike Christie7996a772006-04-06 21:13:41 -05001076
Mike Christie3e5c28a2008-05-21 15:54:06 -05001077 if (i >= session->cmds_max) {
1078 iscsi_conn_printk(KERN_ERR, conn,
1079 "received invalid itt index %u (max cmds "
1080 "%u.\n", i, session->cmds_max);
1081 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001082 }
Mike Christie7996a772006-04-06 21:13:41 -05001083 return 0;
1084}
1085EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1086
Mike Christie913e5bf2008-05-21 15:54:18 -05001087/**
1088 * iscsi_itt_to_ctask - look up ctask by itt
1089 * @conn: iscsi connection
1090 * @itt: itt
1091 *
1092 * This should be used for cmd tasks.
1093 *
1094 * The session lock must be held.
1095 */
1096struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001097{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001098 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001099
1100 if (iscsi_verify_itt(conn, itt))
1101 return NULL;
1102
Mike Christie913e5bf2008-05-21 15:54:18 -05001103 task = iscsi_itt_to_task(conn, itt);
1104 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001105 return NULL;
1106
Mike Christie913e5bf2008-05-21 15:54:18 -05001107 if (task->sc->SCp.phase != conn->session->age) {
1108 iscsi_session_printk(KERN_ERR, conn->session,
1109 "task's session age %d, expected %d\n",
1110 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001111 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001112 }
Mike Christie0af967f2008-05-21 15:54:04 -05001113
Mike Christie9c19a7d2008-05-21 15:54:09 -05001114 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001115}
1116EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1117
Mike Christie40a06e72009-03-05 14:46:05 -06001118void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001119 enum iscsi_err err)
1120{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001121 struct iscsi_conn *conn;
1122 struct device *dev;
1123 unsigned long flags;
1124
1125 spin_lock_irqsave(&session->lock, flags);
1126 conn = session->leadconn;
1127 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1128 spin_unlock_irqrestore(&session->lock, flags);
1129 return;
1130 }
1131
1132 dev = get_device(&conn->cls_conn->dev);
1133 spin_unlock_irqrestore(&session->lock, flags);
1134 if (!dev)
1135 return;
1136 /*
1137 * if the host is being removed bypass the connection
1138 * recovery initialization because we are going to kill
1139 * the session.
1140 */
1141 if (err == ISCSI_ERR_INVALID_HOST)
1142 iscsi_conn_error_event(conn->cls_conn, err);
1143 else
1144 iscsi_conn_failure(conn, err);
1145 put_device(dev);
1146}
1147EXPORT_SYMBOL_GPL(iscsi_session_failure);
1148
Mike Christie7996a772006-04-06 21:13:41 -05001149void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1150{
1151 struct iscsi_session *session = conn->session;
1152 unsigned long flags;
1153
1154 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001155 if (session->state == ISCSI_STATE_FAILED) {
1156 spin_unlock_irqrestore(&session->lock, flags);
1157 return;
1158 }
1159
Mike Christie67a61112006-05-30 00:37:20 -05001160 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001161 session->state = ISCSI_STATE_FAILED;
1162 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001163
Mike Christie7996a772006-04-06 21:13:41 -05001164 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1165 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001166 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001167}
1168EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1169
Mike Christie77a23c22007-05-30 12:57:18 -05001170static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1171{
1172 struct iscsi_session *session = conn->session;
1173
1174 /*
1175 * Check for iSCSI window and take care of CmdSN wrap-around
1176 */
Mike Christiee0726402007-07-26 12:46:48 -05001177 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001178 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1179 "%u MaxCmdSN %u CmdSN %u/%u\n",
1180 session->exp_cmdsn, session->max_cmdsn,
1181 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001182 return -ENOSPC;
1183 }
1184 return 0;
1185}
1186
Mike Christie9c19a7d2008-05-21 15:54:09 -05001187static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001188{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001189 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001190 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001191
Mike Christie9c19a7d2008-05-21 15:54:09 -05001192 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001193 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001194 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001195 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001196 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001197 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001198 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001199 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001200 }
1201 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001202 return rc;
1203}
1204
Mike Christie7996a772006-04-06 21:13:41 -05001205/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001206 * iscsi_requeue_task - requeue task to run from session workqueue
1207 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001208 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001209 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001210 * this. The session lock must be held. This should only be called
1211 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001212 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001213void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001214{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001215 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001216
Mike Christie3bbaaad2009-05-13 17:57:46 -05001217 /*
1218 * this may be on the requeue list already if the xmit_task callout
1219 * is handling the r2ts while we are adding new ones
1220 */
1221 if (list_empty(&task->running))
1222 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001223 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001224}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001225EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001226
1227/**
Mike Christie7996a772006-04-06 21:13:41 -05001228 * iscsi_data_xmit - xmit any command into the scheduled connection
1229 * @conn: iscsi connection
1230 *
1231 * Notes:
1232 * The function can return -EAGAIN in which case the caller must
1233 * re-schedule it again later or recover. '0' return code means
1234 * successful xmit.
1235 **/
1236static int iscsi_data_xmit(struct iscsi_conn *conn)
1237{
Mike Christie3219e522006-05-30 00:37:28 -05001238 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001239
Mike Christie77a23c22007-05-30 12:57:18 -05001240 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001241 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001242 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001243 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001244 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001245 }
Mike Christie7996a772006-04-06 21:13:41 -05001246
Mike Christie9c19a7d2008-05-21 15:54:09 -05001247 if (conn->task) {
1248 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001249 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001250 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001251 }
1252
Mike Christie77a23c22007-05-30 12:57:18 -05001253 /*
1254 * process mgmt pdus like nops before commands since we should
1255 * only have one nop-out as a ping from us and targets should not
1256 * overflow us with nop-ins
1257 */
1258check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001259 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001260 conn->task = list_entry(conn->mgmtqueue.next,
1261 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001262 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001263 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1264 __iscsi_put_task(conn->task);
1265 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001266 continue;
1267 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001268 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001269 if (rc)
1270 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001271 }
1272
Mike Christie843c0a82007-12-13 12:43:20 -06001273 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001274 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001275 if (conn->tmf_state == TMF_QUEUED)
1276 break;
1277
Mike Christie3bbaaad2009-05-13 17:57:46 -05001278 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001279 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001280 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001281 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001282 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001283 continue;
1284 }
Mike Christie577577d2008-12-02 00:32:05 -06001285 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1286 if (rc) {
1287 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001288 list_add_tail(&conn->task->running,
1289 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001290 conn->task = NULL;
1291 goto again;
1292 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001293 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001294 continue;
1295 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001296 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001297 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001298 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001299 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001300 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001301 * we need to check the mgmt queue for nops that need to
1302 * be sent to aviod starvation
1303 */
Mike Christie843c0a82007-12-13 12:43:20 -06001304 if (!list_empty(&conn->mgmtqueue))
1305 goto check_mgmt;
1306 }
1307
1308 while (!list_empty(&conn->requeue)) {
1309 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1310 break;
1311
Mike Christieb3a7ea82007-12-13 12:43:26 -06001312 /*
1313 * we always do fastlogout - conn stop code will clean up.
1314 */
1315 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1316 break;
1317
Mike Christie9c19a7d2008-05-21 15:54:09 -05001318 conn->task = list_entry(conn->requeue.next,
1319 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001320 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001321 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001322 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001323 if (rc)
1324 goto again;
1325 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001326 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001327 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001328 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001329 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001330
1331again:
1332 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001333 rc = -ENODATA;
1334 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001335 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001336}
1337
David Howellsc4028952006-11-22 14:57:56 +00001338static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001339{
David Howellsc4028952006-11-22 14:57:56 +00001340 struct iscsi_conn *conn =
1341 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001342 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001343 /*
1344 * serialize Xmit worker on a per-connection basis.
1345 */
Mike Christie3219e522006-05-30 00:37:28 -05001346 do {
1347 rc = iscsi_data_xmit(conn);
1348 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001349}
1350
Mike Christie577577d2008-12-02 00:32:05 -06001351static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1352 struct scsi_cmnd *sc)
1353{
1354 struct iscsi_task *task;
1355
1356 if (!__kfifo_get(conn->session->cmdpool.queue,
1357 (void *) &task, sizeof(void *)))
1358 return NULL;
1359
1360 sc->SCp.phase = conn->session->age;
1361 sc->SCp.ptr = (char *) task;
1362
1363 atomic_set(&task->refcount, 1);
1364 task->state = ISCSI_TASK_PENDING;
1365 task->conn = conn;
1366 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001367 task->have_checked_conn = false;
1368 task->last_timeout = jiffies;
1369 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001370 INIT_LIST_HEAD(&task->running);
1371 return task;
1372}
1373
Mike Christie7996a772006-04-06 21:13:41 -05001374enum {
1375 FAILURE_BAD_HOST = 1,
1376 FAILURE_SESSION_FAILED,
1377 FAILURE_SESSION_FREED,
1378 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001379 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001380 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001381 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001382 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001383 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001384 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001385};
1386
1387int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1388{
Mike Christie75613522008-05-21 15:53:59 -05001389 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001390 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001391 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001392 int reason = 0;
1393 struct iscsi_session *session;
1394 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001395 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001396
1397 sc->scsi_done = done;
1398 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001399 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001400
1401 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001402 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001403 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001404
Mike Christie75613522008-05-21 15:53:59 -05001405 cls_session = starget_to_session(scsi_target(sc->device));
1406 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001407 spin_lock(&session->lock);
1408
Mike Christie75613522008-05-21 15:53:59 -05001409 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001410 if (reason) {
1411 sc->result = reason;
1412 goto fault;
1413 }
1414
Mike Christie301e0f72009-05-13 17:57:47 -05001415 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001416 /*
1417 * to handle the race between when we set the recovery state
1418 * and block the session we requeue here (commands could
1419 * be entering our queuecommand while a block is starting
1420 * up because the block code is not locked)
1421 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001422 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001423 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001424 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001425 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001426 sc->result = DID_IMM_RETRY << 16;
1427 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001428 case ISCSI_STATE_LOGGING_OUT:
1429 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001430 sc->result = DID_IMM_RETRY << 16;
1431 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001432 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001433 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001434 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001435 break;
1436 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001437 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001438 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001439 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001440 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001441 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001442 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001443 }
Mike Christie7996a772006-04-06 21:13:41 -05001444 goto fault;
1445 }
1446
Mike Christie7996a772006-04-06 21:13:41 -05001447 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001448 if (!conn) {
1449 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001450 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001451 goto fault;
1452 }
Mike Christie7996a772006-04-06 21:13:41 -05001453
Mike Christie77a23c22007-05-30 12:57:18 -05001454 if (iscsi_check_cmdsn_window_closed(conn)) {
1455 reason = FAILURE_WINDOW_CLOSED;
1456 goto reject;
1457 }
1458
Mike Christie577577d2008-12-02 00:32:05 -06001459 task = iscsi_alloc_task(conn, sc);
1460 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001461 reason = FAILURE_OOM;
1462 goto reject;
1463 }
Mike Christie7996a772006-04-06 21:13:41 -05001464
Mike Christie1336aed2009-05-13 17:57:48 -05001465 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001466 reason = iscsi_prep_scsi_cmd_pdu(task);
1467 if (reason) {
1468 if (reason == -ENOMEM) {
1469 reason = FAILURE_OOM;
1470 goto prepd_reject;
1471 } else {
1472 sc->result = DID_ABORT << 16;
1473 goto prepd_fault;
1474 }
Mike Christie052d0142008-05-21 15:54:05 -05001475 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001476 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001477 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001478 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001479 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001480 } else {
1481 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001482 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001483 }
Mike Christie052d0142008-05-21 15:54:05 -05001484
1485 session->queued_cmdsn++;
1486 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001487 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001488 return 0;
1489
Mike Christie577577d2008-12-02 00:32:05 -06001490prepd_reject:
1491 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001492 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001493reject:
1494 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001495 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1496 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001497 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001498 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001499
Mike Christie577577d2008-12-02 00:32:05 -06001500prepd_fault:
1501 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001502 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001503fault:
1504 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001505 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1506 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001507 if (!scsi_bidi_cmnd(sc))
1508 scsi_set_resid(sc, scsi_bufflen(sc));
1509 else {
1510 scsi_out(sc)->resid = scsi_out(sc)->length;
1511 scsi_in(sc)->resid = scsi_in(sc)->length;
1512 }
Mike Christie052d0142008-05-21 15:54:05 -05001513 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001514 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001515 return 0;
1516}
1517EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1518
1519int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1520{
Mike Christie7996a772006-04-06 21:13:41 -05001521 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1522 return sdev->queue_depth;
1523}
1524EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1525
Mike Christie6b5d6c42009-04-21 15:32:32 -05001526int iscsi_target_alloc(struct scsi_target *starget)
1527{
1528 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1529 struct iscsi_session *session = cls_session->dd_data;
1530
1531 starget->can_queue = session->scsi_cmds_max;
1532 return 0;
1533}
1534EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1535
Mike Christie7996a772006-04-06 21:13:41 -05001536void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1537{
Mike Christie75613522008-05-21 15:53:59 -05001538 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001539
1540 spin_lock_bh(&session->lock);
1541 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001542 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001543 if (session->leadconn)
1544 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001545 }
1546 spin_unlock_bh(&session->lock);
1547}
1548EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1549
Mike Christie8e124522008-09-24 11:46:12 -05001550int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001551{
Mike Christie75613522008-05-21 15:53:59 -05001552 struct iscsi_cls_session *cls_session;
1553 struct iscsi_session *session;
1554 struct iscsi_conn *conn;
1555
1556 cls_session = starget_to_session(scsi_target(sc->device));
1557 session = cls_session->dd_data;
1558 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001559
Mike Christiebc436b22007-12-13 12:43:28 -06001560 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001561 spin_lock_bh(&session->lock);
1562 if (session->state == ISCSI_STATE_TERMINATE) {
1563failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001564 iscsi_session_printk(KERN_INFO, session,
1565 "failing target reset: Could not log "
1566 "back into target [age %d]\n",
1567 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001568 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001569 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001570 return FAILED;
1571 }
1572
Mike Christie7996a772006-04-06 21:13:41 -05001573 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001574 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001575 /*
1576 * we drop the lock here but the leadconn cannot be destoyed while
1577 * we are in the scsi eh
1578 */
Mike Christie843c0a82007-12-13 12:43:20 -06001579 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001580
Mike Christie1b2c7af2009-03-05 14:45:58 -06001581 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001582 wait_event_interruptible(conn->ehwait,
1583 session->state == ISCSI_STATE_TERMINATE ||
1584 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001585 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001586 if (signal_pending(current))
1587 flush_signals(current);
1588
Mike Christiebc436b22007-12-13 12:43:28 -06001589 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001590 spin_lock_bh(&session->lock);
1591 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001592 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001593 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001594 else
1595 goto failed;
1596 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001597 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001598 return SUCCESS;
1599}
Mike Christie8e124522008-09-24 11:46:12 -05001600EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001601
Mike Christie843c0a82007-12-13 12:43:20 -06001602static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001603{
Mike Christie843c0a82007-12-13 12:43:20 -06001604 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001605 struct iscsi_session *session = conn->session;
1606
1607 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001608 if (conn->tmf_state == TMF_QUEUED) {
1609 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001610 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001611 /* unblock eh_abort() */
1612 wake_up(&conn->ehwait);
1613 }
1614 spin_unlock(&session->lock);
1615}
1616
Mike Christie9c19a7d2008-05-21 15:54:09 -05001617static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001618 struct iscsi_tm *hdr, int age,
1619 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001620{
Mike Christie7996a772006-04-06 21:13:41 -05001621 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001622 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001623
Mike Christie9c19a7d2008-05-21 15:54:09 -05001624 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001625 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001626 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001627 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001628 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001629 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001630 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001631 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001632 }
Mike Christie843c0a82007-12-13 12:43:20 -06001633 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001634 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001635 conn->tmf_timer.function = iscsi_tmf_timedout;
1636 conn->tmf_timer.data = (unsigned long)conn;
1637 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001638 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001639
Mike Christie7996a772006-04-06 21:13:41 -05001640 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001641 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001642
1643 /*
1644 * block eh thread until:
1645 *
Mike Christie843c0a82007-12-13 12:43:20 -06001646 * 1) tmf response
1647 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001648 * 3) session is terminated or restarted or userspace has
1649 * given up on recovery
1650 */
Mike Christie843c0a82007-12-13 12:43:20 -06001651 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001652 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001653 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001654 if (signal_pending(current))
1655 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001656 del_timer_sync(&conn->tmf_timer);
1657
Mike Christie6724add2007-08-15 01:38:30 -05001658 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001659 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001660 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001661 if (age != session->age ||
1662 session->state != ISCSI_STATE_LOGGED_IN)
1663 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001664 return 0;
1665}
1666
1667/*
Mike Christie843c0a82007-12-13 12:43:20 -06001668 * Fail commands. session lock held and recv side suspended and xmit
1669 * thread flushed
1670 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001671static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1672 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001673{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001674 struct iscsi_task *task;
1675 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001676
Mike Christie3bbaaad2009-05-13 17:57:46 -05001677 for (i = 0; i < conn->session->cmds_max; i++) {
1678 task = conn->session->cmds[i];
1679 if (!task->sc || task->state == ISCSI_TASK_FREE)
1680 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001681
Mike Christie3bbaaad2009-05-13 17:57:46 -05001682 if (lun != -1 && lun != task->sc->device->lun)
1683 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001684
Mike Christie3bbaaad2009-05-13 17:57:46 -05001685 ISCSI_DBG_SESSION(conn->session,
1686 "failing sc %p itt 0x%x state %d\n",
1687 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001688 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001689 }
1690}
1691
Mike Christieb40977d2008-05-21 15:54:03 -05001692void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001693{
Mike Christie32ae7632009-03-05 14:46:03 -06001694 struct Scsi_Host *shost = conn->session->host;
1695 struct iscsi_host *ihost = shost_priv(shost);
1696
Mike Christie6724add2007-08-15 01:38:30 -05001697 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001698 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001699 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001700}
Mike Christieb40977d2008-05-21 15:54:03 -05001701EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001702
1703static void iscsi_start_tx(struct iscsi_conn *conn)
1704{
1705 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001706 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001707}
1708
Mike Christie4c48a822009-05-13 17:57:45 -05001709/*
1710 * We want to make sure a ping is in flight. It has timed out.
1711 * And we are not busy processing a pdu that is making
1712 * progress but got started before the ping and is taking a while
1713 * to complete so the ping is just stuck behind it in a queue.
1714 */
1715static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1716{
1717 if (conn->ping_task &&
1718 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1719 (conn->ping_timeout * HZ), jiffies))
1720 return 1;
1721 else
1722 return 0;
1723}
1724
Mike Christied355e572009-06-15 22:11:08 -05001725static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001726{
Mike Christied355e572009-06-15 22:11:08 -05001727 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1728 struct iscsi_task *task = NULL;
Mike Christief6d51802007-12-13 12:43:30 -06001729 struct iscsi_cls_session *cls_session;
1730 struct iscsi_session *session;
1731 struct iscsi_conn *conn;
Mike Christief6d51802007-12-13 12:43:30 -06001732
Mike Christied355e572009-06-15 22:11:08 -05001733 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001734 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001735
Mike Christied355e572009-06-15 22:11:08 -05001736 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001737
1738 spin_lock(&session->lock);
1739 if (session->state != ISCSI_STATE_LOGGED_IN) {
1740 /*
1741 * We are probably in the middle of iscsi recovery so let
1742 * that complete and handle the error.
1743 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001744 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001745 goto done;
1746 }
1747
1748 conn = session->leadconn;
1749 if (!conn) {
1750 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001751 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001752 goto done;
1753 }
1754
Mike Christied355e572009-06-15 22:11:08 -05001755 task = (struct iscsi_task *)sc->SCp.ptr;
1756 if (!task)
1757 goto done;
1758 /*
1759 * If we have sent (at least queued to the network layer) a pdu or
1760 * recvd one for the task since the last timeout ask for
1761 * more time. If on the next timeout we have not made progress
1762 * we can check if it is the task or connection when we send the
1763 * nop as a ping.
1764 */
1765 if (time_after_eq(task->last_xfer, task->last_timeout)) {
1766 ISCSI_DBG_CONN(conn, "Command making progress. Asking "
1767 "scsi-ml for more time to complete. "
1768 "Last data recv at %lu. Last timeout was at "
1769 "%lu\n.", task->last_xfer, task->last_timeout);
1770 task->have_checked_conn = false;
1771 rc = BLK_EH_RESET_TIMER;
1772 goto done;
1773 }
1774
Mike Christief6d51802007-12-13 12:43:30 -06001775 if (!conn->recv_timeout && !conn->ping_timeout)
1776 goto done;
1777 /*
1778 * if the ping timedout then we are in the middle of cleaning up
1779 * and can let the iscsi eh handle it
1780 */
Mike Christie4c48a822009-05-13 17:57:45 -05001781 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001782 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001783 goto done;
1784 }
Mike Christied355e572009-06-15 22:11:08 -05001785
1786 /* Assumes nop timeout is shorter than scsi cmd timeout */
1787 if (task->have_checked_conn)
1788 goto done;
1789
Mike Christief6d51802007-12-13 12:43:30 -06001790 /*
Mike Christied355e572009-06-15 22:11:08 -05001791 * Checking the transport already or nop from a cmd timeout still
1792 * running
Mike Christief6d51802007-12-13 12:43:30 -06001793 */
Mike Christied355e572009-06-15 22:11:08 -05001794 if (conn->ping_task) {
1795 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001796 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001797 goto done;
1798 }
1799
Mike Christied355e572009-06-15 22:11:08 -05001800 /* Make sure there is a transport check done */
1801 iscsi_send_nopout(conn, NULL);
1802 task->have_checked_conn = true;
1803 rc = BLK_EH_RESET_TIMER;
1804
Mike Christief6d51802007-12-13 12:43:30 -06001805done:
Mike Christied355e572009-06-15 22:11:08 -05001806 if (task)
1807 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06001808 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001809 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1810 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001811 return rc;
1812}
1813
1814static void iscsi_check_transport_timeouts(unsigned long data)
1815{
1816 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1817 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001818 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001819
1820 spin_lock(&session->lock);
1821 if (session->state != ISCSI_STATE_LOGGED_IN)
1822 goto done;
1823
Mike Christie4cf10432008-05-07 20:43:52 -05001824 recv_timeout = conn->recv_timeout;
1825 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001826 goto done;
1827
Mike Christie4cf10432008-05-07 20:43:52 -05001828 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001829 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001830
1831 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001832 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001833 "expired, recv timeout %d, last rx %lu, "
1834 "last ping %lu, now %lu\n",
1835 conn->ping_timeout, conn->recv_timeout,
1836 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001837 spin_unlock(&session->lock);
1838 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1839 return;
1840 }
1841
Mike Christie4cf10432008-05-07 20:43:52 -05001842 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001843 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001844 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001845 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001846 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001847 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001848 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001849
Mike Christie1b2c7af2009-03-05 14:45:58 -06001850 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001851 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001852done:
1853 spin_unlock(&session->lock);
1854}
1855
Mike Christie9c19a7d2008-05-21 15:54:09 -05001856static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001857 struct iscsi_tm *hdr)
1858{
1859 memset(hdr, 0, sizeof(*hdr));
1860 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1861 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1862 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001863 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1864 hdr->rtt = task->hdr_itt;
1865 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001866}
1867
Mike Christie7996a772006-04-06 21:13:41 -05001868int iscsi_eh_abort(struct scsi_cmnd *sc)
1869{
Mike Christie75613522008-05-21 15:53:59 -05001870 struct iscsi_cls_session *cls_session;
1871 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001872 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001873 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001874 struct iscsi_tm *hdr;
1875 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001876
Mike Christie75613522008-05-21 15:53:59 -05001877 cls_session = starget_to_session(scsi_target(sc->device));
1878 session = cls_session->dd_data;
1879
Mike Christie4421c9e2009-05-13 17:57:50 -05001880 ISCSI_DBG_SESSION(session, "aborting sc %p\n", sc);
1881
Mike Christie6724add2007-08-15 01:38:30 -05001882 mutex_lock(&session->eh_mutex);
1883 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001884 /*
1885 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1886 * got the command.
1887 */
1888 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001889 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1890 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001891 spin_unlock_bh(&session->lock);
1892 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001893 return SUCCESS;
1894 }
1895
Mike Christie7996a772006-04-06 21:13:41 -05001896 /*
1897 * If we are not logged in or we have started a new session
1898 * then let the host reset code handle this
1899 */
Mike Christie843c0a82007-12-13 12:43:20 -06001900 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1901 sc->SCp.phase != session->age) {
1902 spin_unlock_bh(&session->lock);
1903 mutex_unlock(&session->eh_mutex);
Mike Christie4421c9e2009-05-13 17:57:50 -05001904 ISCSI_DBG_SESSION(session, "failing abort due to dropped "
1905 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06001906 return FAILED;
1907 }
1908
1909 conn = session->leadconn;
1910 conn->eh_abort_cnt++;
1911 age = session->age;
1912
Mike Christie9c19a7d2008-05-21 15:54:09 -05001913 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001914 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1915 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001916
Mike Christie9c19a7d2008-05-21 15:54:09 -05001917 /* task completed before time out */
1918 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001919 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1920 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001921 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001922 }
Mike Christie7996a772006-04-06 21:13:41 -05001923
Mike Christie9c19a7d2008-05-21 15:54:09 -05001924 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001925 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05001926 goto success;
1927 }
Mike Christie7996a772006-04-06 21:13:41 -05001928
Mike Christie843c0a82007-12-13 12:43:20 -06001929 /* only have one tmf outstanding at a time */
1930 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001931 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001932 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001933
Mike Christie843c0a82007-12-13 12:43:20 -06001934 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001935 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001936
Mike Christie9c19a7d2008-05-21 15:54:09 -05001937 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001938 rc = FAILED;
1939 goto failed;
1940 }
1941
1942 switch (conn->tmf_state) {
1943 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001944 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001945 /*
1946 * stop tx side incase the target had sent a abort rsp but
1947 * the initiator was still writing out data.
1948 */
Mike Christie6724add2007-08-15 01:38:30 -05001949 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001950 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001951 * we do not stop the recv side because targets have been
1952 * good and have never sent us a successful tmf response
1953 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001954 */
Mike Christie77a23c22007-05-30 12:57:18 -05001955 spin_lock(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05001956 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06001957 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001958 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001959 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001960 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001961 case TMF_TIMEDOUT:
1962 spin_unlock_bh(&session->lock);
1963 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1964 goto failed_unlocked;
1965 case TMF_NOT_FOUND:
1966 if (!sc->SCp.ptr) {
1967 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001968 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001969 ISCSI_DBG_SESSION(session, "sc completed while abort "
1970 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001971 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001972 }
1973 /* fall through */
1974 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001975 conn->tmf_state = TMF_INITIAL;
1976 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001977 }
1978
Mike Christie77a23c22007-05-30 12:57:18 -05001979success:
Mike Christie7996a772006-04-06 21:13:41 -05001980 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001981success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001982 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1983 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001984 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001985 return SUCCESS;
1986
1987failed:
1988 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001989failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001990 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1991 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001992 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001993 return FAILED;
1994}
1995EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1996
Mike Christie843c0a82007-12-13 12:43:20 -06001997static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1998{
1999 memset(hdr, 0, sizeof(*hdr));
2000 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2001 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2002 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2003 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002004 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002005}
2006
2007int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2008{
Mike Christie75613522008-05-21 15:53:59 -05002009 struct iscsi_cls_session *cls_session;
2010 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002011 struct iscsi_conn *conn;
2012 struct iscsi_tm *hdr;
2013 int rc = FAILED;
2014
Mike Christie75613522008-05-21 15:53:59 -05002015 cls_session = starget_to_session(scsi_target(sc->device));
2016 session = cls_session->dd_data;
2017
Mike Christie1b2c7af2009-03-05 14:45:58 -06002018 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
2019 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002020
2021 mutex_lock(&session->eh_mutex);
2022 spin_lock_bh(&session->lock);
2023 /*
2024 * Just check if we are not logged in. We cannot check for
2025 * the phase because the reset could come from a ioctl.
2026 */
2027 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2028 goto unlock;
2029 conn = session->leadconn;
2030
2031 /* only have one tmf outstanding at a time */
2032 if (conn->tmf_state != TMF_INITIAL)
2033 goto unlock;
2034 conn->tmf_state = TMF_QUEUED;
2035
2036 hdr = &conn->tmhdr;
2037 iscsi_prep_lun_reset_pdu(sc, hdr);
2038
Mike Christie9c19a7d2008-05-21 15:54:09 -05002039 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002040 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002041 rc = FAILED;
2042 goto unlock;
2043 }
2044
2045 switch (conn->tmf_state) {
2046 case TMF_SUCCESS:
2047 break;
2048 case TMF_TIMEDOUT:
2049 spin_unlock_bh(&session->lock);
2050 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2051 goto done;
2052 default:
2053 conn->tmf_state = TMF_INITIAL;
2054 goto unlock;
2055 }
2056
2057 rc = SUCCESS;
2058 spin_unlock_bh(&session->lock);
2059
2060 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002061
Mike Christiea3439142008-09-24 11:46:15 -05002062 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002063 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002064 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002065 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002066
2067 iscsi_start_tx(conn);
2068 goto done;
2069
2070unlock:
2071 spin_unlock_bh(&session->lock);
2072done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06002073 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
2074 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002075 mutex_unlock(&session->eh_mutex);
2076 return rc;
2077}
2078EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2079
Olaf Kirch63203772007-12-13 12:43:25 -06002080/*
2081 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2082 * should be accessed via kfifo_{get,put} on q->queue.
2083 * Optionally, the caller can obtain the array of object pointers
2084 * by passing in a non-NULL @items pointer
2085 */
Mike Christie7996a772006-04-06 21:13:41 -05002086int
Olaf Kirch63203772007-12-13 12:43:25 -06002087iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002088{
Olaf Kirch63203772007-12-13 12:43:25 -06002089 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002090
Olaf Kirch63203772007-12-13 12:43:25 -06002091 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002092
2093 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002094
2095 /* If the user passed an items pointer, he wants a copy of
2096 * the array. */
2097 if (items)
2098 num_arrays++;
2099 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2100 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002101 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002102
2103 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2104 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002105 if (IS_ERR(q->queue)) {
2106 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002107 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002108 }
Mike Christie7996a772006-04-06 21:13:41 -05002109
2110 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002111 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002112 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002113 q->max = i;
2114 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002115 }
Mike Christie7996a772006-04-06 21:13:41 -05002116 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2117 }
Olaf Kirch63203772007-12-13 12:43:25 -06002118
2119 if (items) {
2120 *items = q->pool + max;
2121 memcpy(*items, q->pool, max * sizeof(void *));
2122 }
2123
Mike Christie7996a772006-04-06 21:13:41 -05002124 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002125
2126enomem:
2127 iscsi_pool_free(q);
2128 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002129}
2130EXPORT_SYMBOL_GPL(iscsi_pool_init);
2131
Olaf Kirch63203772007-12-13 12:43:25 -06002132void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002133{
2134 int i;
2135
2136 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002137 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002138 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002139 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002140}
2141EXPORT_SYMBOL_GPL(iscsi_pool_free);
2142
Mike Christiea4804cd2008-05-21 15:54:00 -05002143/**
2144 * iscsi_host_add - add host to system
2145 * @shost: scsi host
2146 * @pdev: parent device
2147 *
2148 * This should be called by partial offload and software iscsi drivers
2149 * to add a host to the system.
2150 */
2151int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002152{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002153 if (!shost->can_queue)
2154 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2155
Mike Christie4d108352009-03-05 14:46:04 -06002156 if (!shost->cmd_per_lun)
2157 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2158
Mike Christie308cec12009-02-06 12:06:20 -06002159 if (!shost->transportt->eh_timed_out)
2160 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002161 return scsi_add_host(shost, pdev);
2162}
2163EXPORT_SYMBOL_GPL(iscsi_host_add);
2164
2165/**
2166 * iscsi_host_alloc - allocate a host and driver data
2167 * @sht: scsi host template
2168 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002169 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002170 *
2171 * This should be called by partial offload and software iscsi drivers.
2172 * To access the driver specific memory use the iscsi_host_priv() macro.
2173 */
2174struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002175 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002176{
2177 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002178 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002179
2180 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2181 if (!shost)
2182 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002183 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002184
2185 if (xmit_can_sleep) {
2186 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2187 "iscsi_q_%d", shost->host_no);
2188 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2189 if (!ihost->workq)
2190 goto free_host;
2191 }
2192
Mike Christiee5bd7b52008-09-24 11:46:10 -05002193 spin_lock_init(&ihost->lock);
2194 ihost->state = ISCSI_HOST_SETUP;
2195 ihost->num_sessions = 0;
2196 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002197 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002198
2199free_host:
2200 scsi_host_put(shost);
2201 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002202}
Mike Christiea4804cd2008-05-21 15:54:00 -05002203EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002204
Mike Christiee5bd7b52008-09-24 11:46:10 -05002205static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2206{
Mike Christie40a06e72009-03-05 14:46:05 -06002207 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002208}
2209
Mike Christiea4804cd2008-05-21 15:54:00 -05002210/**
2211 * iscsi_host_remove - remove host and sessions
2212 * @shost: scsi host
2213 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002214 * If there are any sessions left, this will initiate the removal and wait
2215 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002216 */
2217void iscsi_host_remove(struct Scsi_Host *shost)
2218{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002219 struct iscsi_host *ihost = shost_priv(shost);
2220 unsigned long flags;
2221
2222 spin_lock_irqsave(&ihost->lock, flags);
2223 ihost->state = ISCSI_HOST_REMOVED;
2224 spin_unlock_irqrestore(&ihost->lock, flags);
2225
2226 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2227 wait_event_interruptible(ihost->session_removal_wq,
2228 ihost->num_sessions == 0);
2229 if (signal_pending(current))
2230 flush_signals(current);
2231
Mike Christiea4804cd2008-05-21 15:54:00 -05002232 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002233 if (ihost->workq)
2234 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002235}
2236EXPORT_SYMBOL_GPL(iscsi_host_remove);
2237
2238void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002239{
2240 struct iscsi_host *ihost = shost_priv(shost);
2241
2242 kfree(ihost->netdev);
2243 kfree(ihost->hwaddress);
2244 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002245 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002246}
Mike Christiea4804cd2008-05-21 15:54:00 -05002247EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002248
Mike Christiee5bd7b52008-09-24 11:46:10 -05002249static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2250{
2251 struct iscsi_host *ihost = shost_priv(shost);
2252 unsigned long flags;
2253
2254 shost = scsi_host_get(shost);
2255 if (!shost) {
2256 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2257 "of session teardown event because host already "
2258 "removed.\n");
2259 return;
2260 }
2261
2262 spin_lock_irqsave(&ihost->lock, flags);
2263 ihost->num_sessions--;
2264 if (ihost->num_sessions == 0)
2265 wake_up(&ihost->session_removal_wq);
2266 spin_unlock_irqrestore(&ihost->lock, flags);
2267 scsi_host_put(shost);
2268}
2269
Mike Christie75613522008-05-21 15:53:59 -05002270/**
2271 * iscsi_session_setup - create iscsi cls session and host and session
2272 * @iscsit: iscsi transport template
2273 * @shost: scsi host
2274 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002275 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002276 * @initial_cmdsn: initial CmdSN
2277 *
2278 * This can be used by software iscsi_transports that allocate
2279 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002280 *
2281 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2282 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2283 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002284 */
2285struct iscsi_cls_session *
2286iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002287 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002288 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002289{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002290 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002291 struct iscsi_session *session;
2292 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002293 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002294 unsigned long flags;
2295
2296 spin_lock_irqsave(&ihost->lock, flags);
2297 if (ihost->state == ISCSI_HOST_REMOVED) {
2298 spin_unlock_irqrestore(&ihost->lock, flags);
2299 return NULL;
2300 }
2301 ihost->num_sessions++;
2302 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002303
2304 if (!total_cmds)
2305 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002306 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002307 * The iscsi layer needs some tasks for nop handling and tmfs,
2308 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2309 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002310 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002311 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2312 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2313 "must be a power of two that is at least %d.\n",
2314 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002315 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002316 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002317
2318 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2319 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2320 "must be a power of 2 less than or equal to %d.\n",
2321 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2322 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2323 }
2324
2325 if (!is_power_of_2(total_cmds)) {
2326 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2327 "must be a power of 2.\n", total_cmds);
2328 total_cmds = rounddown_pow_of_two(total_cmds);
2329 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2330 return NULL;
2331 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2332 total_cmds);
2333 }
2334 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002335
Mike Christie5d91e202008-05-21 15:54:01 -05002336 cls_session = iscsi_alloc_session(shost, iscsit,
2337 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002338 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002339 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002340 session = cls_session->dd_data;
2341 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002342 session->host = shost;
2343 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002344 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002345 session->lu_reset_timeout = 15;
2346 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002347 session->scsi_cmds_max = scsi_cmds;
2348 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002349 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002350 session->exp_cmdsn = initial_cmdsn + 1;
2351 session->max_cmdsn = initial_cmdsn + 1;
2352 session->max_r2t = 1;
2353 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002354 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002355 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002356
2357 /* initialize SCSI PDU commands pool */
2358 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2359 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002360 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002361 goto cmdpool_alloc_fail;
2362
2363 /* pre-format cmds pool with ITT */
2364 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002365 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002366
Mike Christie9c19a7d2008-05-21 15:54:09 -05002367 if (cmd_task_size)
2368 task->dd_data = &task[1];
2369 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002370 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002371 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002372 }
2373
Mike Christief53a88d2006-06-28 12:00:27 -05002374 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002375 goto module_get_fail;
2376
Mike Christie79706342008-05-21 15:54:12 -05002377 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002378 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002379
Mike Christie7996a772006-04-06 21:13:41 -05002380 return cls_session;
2381
2382cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002383 module_put(iscsit->owner);
2384module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002385 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002386cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002387 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002388dec_session_count:
2389 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002390 return NULL;
2391}
2392EXPORT_SYMBOL_GPL(iscsi_session_setup);
2393
2394/**
2395 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002396 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002397 *
Mike Christie75613522008-05-21 15:53:59 -05002398 * The driver must have called iscsi_remove_session before
2399 * calling this.
2400 */
Mike Christie7996a772006-04-06 21:13:41 -05002401void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2402{
Mike Christie75613522008-05-21 15:53:59 -05002403 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002404 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002405 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002406
Olaf Kirch63203772007-12-13 12:43:25 -06002407 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002408
Mike Christieb2c64162007-05-30 12:57:16 -05002409 kfree(session->password);
2410 kfree(session->password_in);
2411 kfree(session->username);
2412 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002413 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002414 kfree(session->initiatorname);
2415 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002416
Mike Christie75613522008-05-21 15:53:59 -05002417 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002418 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002419 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002420}
2421EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2422
2423/**
2424 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2425 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002426 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002427 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002428 */
Mike Christie7996a772006-04-06 21:13:41 -05002429struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002430iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2431 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002432{
Mike Christie75613522008-05-21 15:53:59 -05002433 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002434 struct iscsi_conn *conn;
2435 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002436 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002437
Mike Christie5d91e202008-05-21 15:54:01 -05002438 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2439 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002440 if (!cls_conn)
2441 return NULL;
2442 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002443 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002444
Mike Christie5d91e202008-05-21 15:54:01 -05002445 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002446 conn->session = session;
2447 conn->cls_conn = cls_conn;
2448 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2449 conn->id = conn_idx;
2450 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002451 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002452
2453 init_timer(&conn->transport_timer);
2454 conn->transport_timer.data = (unsigned long)conn;
2455 conn->transport_timer.function = iscsi_check_transport_timeouts;
2456
Mike Christie843c0a82007-12-13 12:43:20 -06002457 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002458 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002459 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002460 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002461
Mike Christie9c19a7d2008-05-21 15:54:09 -05002462 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002463 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002464 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002465 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002466 sizeof(void*))) {
2467 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002468 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002469 }
2470 spin_unlock_bh(&session->lock);
2471
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002472 data = (char *) __get_free_pages(GFP_KERNEL,
2473 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002474 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002475 goto login_task_data_alloc_fail;
2476 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002477
Mike Christie843c0a82007-12-13 12:43:20 -06002478 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002479 init_waitqueue_head(&conn->ehwait);
2480
2481 return cls_conn;
2482
Mike Christie9c19a7d2008-05-21 15:54:09 -05002483login_task_data_alloc_fail:
2484 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002485 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002486login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002487 iscsi_destroy_conn(cls_conn);
2488 return NULL;
2489}
2490EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2491
2492/**
2493 * iscsi_conn_teardown - teardown iscsi connection
2494 * cls_conn: iscsi class connection
2495 *
2496 * TODO: we may need to make this into a two step process
2497 * like scsi-mls remove + put host
2498 */
2499void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2500{
2501 struct iscsi_conn *conn = cls_conn->dd_data;
2502 struct iscsi_session *session = conn->session;
2503 unsigned long flags;
2504
Mike Christief6d51802007-12-13 12:43:30 -06002505 del_timer_sync(&conn->transport_timer);
2506
Mike Christie7996a772006-04-06 21:13:41 -05002507 spin_lock_bh(&session->lock);
2508 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2509 if (session->leadconn == conn) {
2510 /*
2511 * leading connection? then give up on recovery.
2512 */
2513 session->state = ISCSI_STATE_TERMINATE;
2514 wake_up(&conn->ehwait);
2515 }
2516 spin_unlock_bh(&session->lock);
2517
Mike Christie7996a772006-04-06 21:13:41 -05002518 /*
2519 * Block until all in-progress commands for this connection
2520 * time out or fail.
2521 */
2522 for (;;) {
2523 spin_lock_irqsave(session->host->host_lock, flags);
2524 if (!session->host->host_busy) { /* OK for ERL == 0 */
2525 spin_unlock_irqrestore(session->host->host_lock, flags);
2526 break;
2527 }
2528 spin_unlock_irqrestore(session->host->host_lock, flags);
2529 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002530 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2531 "host_busy %d host_failed %d\n",
2532 session->host->host_busy,
2533 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002534 /*
2535 * force eh_abort() to unblock
2536 */
2537 wake_up(&conn->ehwait);
2538 }
2539
Mike Christie779ea122007-02-28 17:32:15 -06002540 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002541 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002542
Mike Christie7996a772006-04-06 21:13:41 -05002543 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002544 free_pages((unsigned long) conn->data,
2545 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002546 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002547 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002548 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002549 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002550 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002551 spin_unlock_bh(&session->lock);
2552
Mike Christie7996a772006-04-06 21:13:41 -05002553 iscsi_destroy_conn(cls_conn);
2554}
2555EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2556
2557int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2558{
2559 struct iscsi_conn *conn = cls_conn->dd_data;
2560 struct iscsi_session *session = conn->session;
2561
Mike Christieffd04362006-08-31 18:09:24 -04002562 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002563 iscsi_conn_printk(KERN_ERR, conn,
2564 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002565 return -EPERM;
2566 }
2567
Mike Christiedb98ccd2006-08-31 18:09:31 -04002568 if ((session->imm_data_en || !session->initial_r2t_en) &&
2569 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002570 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2571 "first_burst %d max_burst %d\n",
2572 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002573 return -EINVAL;
2574 }
2575
Mike Christief6d51802007-12-13 12:43:30 -06002576 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002577 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2578 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002579 conn->recv_timeout = 5;
2580 }
2581
2582 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002583 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2584 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002585 conn->ping_timeout = 5;
2586 }
2587
Mike Christie7996a772006-04-06 21:13:41 -05002588 spin_lock_bh(&session->lock);
2589 conn->c_stage = ISCSI_CONN_STARTED;
2590 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002591 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002592
Mike Christief6d51802007-12-13 12:43:30 -06002593 conn->last_recv = jiffies;
2594 conn->last_ping = jiffies;
2595 if (conn->recv_timeout && conn->ping_timeout)
2596 mod_timer(&conn->transport_timer,
2597 jiffies + (conn->recv_timeout * HZ));
2598
Mike Christie7996a772006-04-06 21:13:41 -05002599 switch(conn->stop_stage) {
2600 case STOP_CONN_RECOVER:
2601 /*
2602 * unblock eh_abort() if it is blocked. re-try all
2603 * commands after successful recovery
2604 */
Mike Christie7996a772006-04-06 21:13:41 -05002605 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002606 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002607 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002608 if (session->age == 16)
2609 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002610 break;
Mike Christie7996a772006-04-06 21:13:41 -05002611 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002612 conn->stop_stage = 0;
2613 break;
Mike Christie7996a772006-04-06 21:13:41 -05002614 default:
2615 break;
2616 }
2617 spin_unlock_bh(&session->lock);
2618
Mike Christie75613522008-05-21 15:53:59 -05002619 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002620 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002621 return 0;
2622}
2623EXPORT_SYMBOL_GPL(iscsi_conn_start);
2624
2625static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002626fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002627{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002628 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002629 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002630
Mike Christie3bbaaad2009-05-13 17:57:46 -05002631 for (i = 0; i < conn->session->cmds_max; i++) {
2632 task = conn->session->cmds[i];
2633 if (task->sc)
2634 continue;
2635
2636 if (task->state == ISCSI_TASK_FREE)
2637 continue;
2638
2639 ISCSI_DBG_SESSION(conn->session,
2640 "failing mgmt itt 0x%x state %d\n",
2641 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002642 state = ISCSI_TASK_ABRT_SESS_RECOV;
2643 if (task->state == ISCSI_TASK_PENDING)
2644 state = ISCSI_TASK_COMPLETED;
2645 iscsi_complete_task(task, state);
2646
Mike Christie7996a772006-04-06 21:13:41 -05002647 }
Mike Christie7996a772006-04-06 21:13:41 -05002648}
2649
Mike Christie656cffc2006-05-18 20:31:42 -05002650static void iscsi_start_session_recovery(struct iscsi_session *session,
2651 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002652{
Mike Christieed2abc72006-05-02 19:46:40 -05002653 int old_stop_stage;
2654
Mike Christie6724add2007-08-15 01:38:30 -05002655 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002656 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002657 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002658 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002659 mutex_unlock(&session->eh_mutex);
2660 return;
2661 }
2662
2663 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002664 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002665 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002666 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002667 */
Mike Christie67a61112006-05-30 00:37:20 -05002668 if (flag == STOP_CONN_TERM)
2669 session->state = ISCSI_STATE_TERMINATE;
2670 else if (conn->stop_stage != STOP_CONN_RECOVER)
2671 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002672 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002673
Mike Christie26013ad2009-05-13 17:57:43 -05002674 del_timer_sync(&conn->transport_timer);
2675 iscsi_suspend_tx(conn);
2676
2677 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002678 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002679 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002680 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002681 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002682
Mike Christie7996a772006-04-06 21:13:41 -05002683 /*
2684 * for connection level recovery we should not calculate
2685 * header digest. conn->hdr_size used for optimization
2686 * in hdr_extract() and will be re-negotiated at
2687 * set_param() time.
2688 */
2689 if (flag == STOP_CONN_RECOVER) {
2690 conn->hdrdgst_en = 0;
2691 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002692 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002693 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002694 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002695 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002696 }
Mike Christie7996a772006-04-06 21:13:41 -05002697 }
Mike Christie656cffc2006-05-18 20:31:42 -05002698
Mike Christie656cffc2006-05-18 20:31:42 -05002699 /*
2700 * flush queues.
2701 */
2702 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002703 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002704 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002705 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002706 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002707}
Mike Christie7996a772006-04-06 21:13:41 -05002708
2709void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2710{
2711 struct iscsi_conn *conn = cls_conn->dd_data;
2712 struct iscsi_session *session = conn->session;
2713
2714 switch (flag) {
2715 case STOP_CONN_RECOVER:
2716 case STOP_CONN_TERM:
2717 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002718 break;
Mike Christie7996a772006-04-06 21:13:41 -05002719 default:
Mike Christie322d7392008-01-31 13:36:52 -06002720 iscsi_conn_printk(KERN_ERR, conn,
2721 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002722 }
2723}
2724EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2725
2726int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2727 struct iscsi_cls_conn *cls_conn, int is_leading)
2728{
Mike Christie75613522008-05-21 15:53:59 -05002729 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002730 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002731
Mike Christie7996a772006-04-06 21:13:41 -05002732 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002733 if (is_leading)
2734 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002735 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002736
2737 /*
2738 * Unblock xmitworker(), Login Phase will pass through.
2739 */
2740 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2741 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2742 return 0;
2743}
2744EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2745
Mike Christie5700b1a2009-05-13 17:57:40 -05002746static int iscsi_switch_str_param(char **param, char *new_val_buf)
2747{
2748 char *new_val;
2749
2750 if (*param) {
2751 if (!strcmp(*param, new_val_buf))
2752 return 0;
2753 }
2754
2755 new_val = kstrdup(new_val_buf, GFP_NOIO);
2756 if (!new_val)
2757 return -ENOMEM;
2758
2759 kfree(*param);
2760 *param = new_val;
2761 return 0;
2762}
Mike Christiea54a52c2006-06-28 12:00:23 -05002763
2764int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2765 enum iscsi_param param, char *buf, int buflen)
2766{
2767 struct iscsi_conn *conn = cls_conn->dd_data;
2768 struct iscsi_session *session = conn->session;
2769 uint32_t value;
2770
2771 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002772 case ISCSI_PARAM_FAST_ABORT:
2773 sscanf(buf, "%d", &session->fast_abort);
2774 break;
Mike Christief6d51802007-12-13 12:43:30 -06002775 case ISCSI_PARAM_ABORT_TMO:
2776 sscanf(buf, "%d", &session->abort_timeout);
2777 break;
2778 case ISCSI_PARAM_LU_RESET_TMO:
2779 sscanf(buf, "%d", &session->lu_reset_timeout);
2780 break;
2781 case ISCSI_PARAM_PING_TMO:
2782 sscanf(buf, "%d", &conn->ping_timeout);
2783 break;
2784 case ISCSI_PARAM_RECV_TMO:
2785 sscanf(buf, "%d", &conn->recv_timeout);
2786 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002787 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2788 sscanf(buf, "%d", &conn->max_recv_dlength);
2789 break;
2790 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2791 sscanf(buf, "%d", &conn->max_xmit_dlength);
2792 break;
2793 case ISCSI_PARAM_HDRDGST_EN:
2794 sscanf(buf, "%d", &conn->hdrdgst_en);
2795 break;
2796 case ISCSI_PARAM_DATADGST_EN:
2797 sscanf(buf, "%d", &conn->datadgst_en);
2798 break;
2799 case ISCSI_PARAM_INITIAL_R2T_EN:
2800 sscanf(buf, "%d", &session->initial_r2t_en);
2801 break;
2802 case ISCSI_PARAM_MAX_R2T:
2803 sscanf(buf, "%d", &session->max_r2t);
2804 break;
2805 case ISCSI_PARAM_IMM_DATA_EN:
2806 sscanf(buf, "%d", &session->imm_data_en);
2807 break;
2808 case ISCSI_PARAM_FIRST_BURST:
2809 sscanf(buf, "%d", &session->first_burst);
2810 break;
2811 case ISCSI_PARAM_MAX_BURST:
2812 sscanf(buf, "%d", &session->max_burst);
2813 break;
2814 case ISCSI_PARAM_PDU_INORDER_EN:
2815 sscanf(buf, "%d", &session->pdu_inorder_en);
2816 break;
2817 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2818 sscanf(buf, "%d", &session->dataseq_inorder_en);
2819 break;
2820 case ISCSI_PARAM_ERL:
2821 sscanf(buf, "%d", &session->erl);
2822 break;
2823 case ISCSI_PARAM_IFMARKER_EN:
2824 sscanf(buf, "%d", &value);
2825 BUG_ON(value);
2826 break;
2827 case ISCSI_PARAM_OFMARKER_EN:
2828 sscanf(buf, "%d", &value);
2829 BUG_ON(value);
2830 break;
2831 case ISCSI_PARAM_EXP_STATSN:
2832 sscanf(buf, "%u", &conn->exp_statsn);
2833 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002834 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002835 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002836 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002837 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002838 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002839 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002840 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002841 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002842 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002843 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002844 case ISCSI_PARAM_TPGT:
2845 sscanf(buf, "%d", &session->tpgt);
2846 break;
2847 case ISCSI_PARAM_PERSISTENT_PORT:
2848 sscanf(buf, "%d", &conn->persistent_port);
2849 break;
2850 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002851 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002852 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002853 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002854 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002855 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002856 default:
2857 return -ENOSYS;
2858 }
2859
2860 return 0;
2861}
2862EXPORT_SYMBOL_GPL(iscsi_set_param);
2863
2864int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2865 enum iscsi_param param, char *buf)
2866{
Mike Christie75613522008-05-21 15:53:59 -05002867 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002868 int len;
2869
2870 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002871 case ISCSI_PARAM_FAST_ABORT:
2872 len = sprintf(buf, "%d\n", session->fast_abort);
2873 break;
Mike Christief6d51802007-12-13 12:43:30 -06002874 case ISCSI_PARAM_ABORT_TMO:
2875 len = sprintf(buf, "%d\n", session->abort_timeout);
2876 break;
2877 case ISCSI_PARAM_LU_RESET_TMO:
2878 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2879 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002880 case ISCSI_PARAM_INITIAL_R2T_EN:
2881 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2882 break;
2883 case ISCSI_PARAM_MAX_R2T:
2884 len = sprintf(buf, "%hu\n", session->max_r2t);
2885 break;
2886 case ISCSI_PARAM_IMM_DATA_EN:
2887 len = sprintf(buf, "%d\n", session->imm_data_en);
2888 break;
2889 case ISCSI_PARAM_FIRST_BURST:
2890 len = sprintf(buf, "%u\n", session->first_burst);
2891 break;
2892 case ISCSI_PARAM_MAX_BURST:
2893 len = sprintf(buf, "%u\n", session->max_burst);
2894 break;
2895 case ISCSI_PARAM_PDU_INORDER_EN:
2896 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2897 break;
2898 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2899 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2900 break;
2901 case ISCSI_PARAM_ERL:
2902 len = sprintf(buf, "%d\n", session->erl);
2903 break;
2904 case ISCSI_PARAM_TARGET_NAME:
2905 len = sprintf(buf, "%s\n", session->targetname);
2906 break;
2907 case ISCSI_PARAM_TPGT:
2908 len = sprintf(buf, "%d\n", session->tpgt);
2909 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002910 case ISCSI_PARAM_USERNAME:
2911 len = sprintf(buf, "%s\n", session->username);
2912 break;
2913 case ISCSI_PARAM_USERNAME_IN:
2914 len = sprintf(buf, "%s\n", session->username_in);
2915 break;
2916 case ISCSI_PARAM_PASSWORD:
2917 len = sprintf(buf, "%s\n", session->password);
2918 break;
2919 case ISCSI_PARAM_PASSWORD_IN:
2920 len = sprintf(buf, "%s\n", session->password_in);
2921 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002922 case ISCSI_PARAM_IFACE_NAME:
2923 len = sprintf(buf, "%s\n", session->ifacename);
2924 break;
2925 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002926 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05002927 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002928 default:
2929 return -ENOSYS;
2930 }
2931
2932 return len;
2933}
2934EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2935
2936int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2937 enum iscsi_param param, char *buf)
2938{
2939 struct iscsi_conn *conn = cls_conn->dd_data;
2940 int len;
2941
2942 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002943 case ISCSI_PARAM_PING_TMO:
2944 len = sprintf(buf, "%u\n", conn->ping_timeout);
2945 break;
2946 case ISCSI_PARAM_RECV_TMO:
2947 len = sprintf(buf, "%u\n", conn->recv_timeout);
2948 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002949 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2950 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2951 break;
2952 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2953 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2954 break;
2955 case ISCSI_PARAM_HDRDGST_EN:
2956 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2957 break;
2958 case ISCSI_PARAM_DATADGST_EN:
2959 len = sprintf(buf, "%d\n", conn->datadgst_en);
2960 break;
2961 case ISCSI_PARAM_IFMARKER_EN:
2962 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2963 break;
2964 case ISCSI_PARAM_OFMARKER_EN:
2965 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2966 break;
2967 case ISCSI_PARAM_EXP_STATSN:
2968 len = sprintf(buf, "%u\n", conn->exp_statsn);
2969 break;
2970 case ISCSI_PARAM_PERSISTENT_PORT:
2971 len = sprintf(buf, "%d\n", conn->persistent_port);
2972 break;
2973 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2974 len = sprintf(buf, "%s\n", conn->persistent_address);
2975 break;
2976 default:
2977 return -ENOSYS;
2978 }
2979
2980 return len;
2981}
2982EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2983
Mike Christie0801c242007-05-30 12:57:12 -05002984int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2985 char *buf)
2986{
Mike Christie75613522008-05-21 15:53:59 -05002987 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002988 int len;
2989
2990 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002991 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002992 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002993 break;
Mike Christie0801c242007-05-30 12:57:12 -05002994 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002995 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002996 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002997 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002998 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002999 break;
Mike Christie75613522008-05-21 15:53:59 -05003000 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003001 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003002 break;
Mike Christie0801c242007-05-30 12:57:12 -05003003 default:
3004 return -ENOSYS;
3005 }
3006
3007 return len;
3008}
3009EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3010
3011int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3012 char *buf, int buflen)
3013{
Mike Christie75613522008-05-21 15:53:59 -05003014 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003015
3016 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003017 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003018 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003019 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003020 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003021 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003022 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003023 default:
3024 return -ENOSYS;
3025 }
3026
3027 return 0;
3028}
3029EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3030
Mike Christie7996a772006-04-06 21:13:41 -05003031MODULE_AUTHOR("Mike Christie");
3032MODULE_DESCRIPTION("iSCSI library functions");
3033MODULE_LICENSE("GPL");