blob: b00be6c3efc1b25302fe2643564a0f768f5ffdcf [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 Christie577577d2008-12-02 00:32:05 -0600396 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500397 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500398 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500399
Mike Christie3e5c28a2008-05-21 15:54:06 -0500400 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500402 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500403 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500404 return;
405
Mike Christie9c19a7d2008-05-21 15:54:09 -0500406 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500407
Mike Christie3e5c28a2008-05-21 15:54:06 -0500408 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500409 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500410 /* SCSI eh reuses commands to verify us */
411 sc->SCp.ptr = NULL;
412 /*
413 * queue command may call this to free the task, but
414 * not have setup the sc callback
415 */
416 if (sc->scsi_done)
417 sc->scsi_done(sc);
418 }
Mike Christie7996a772006-04-06 21:13:41 -0500419}
420
Mike Christie913e5bf2008-05-21 15:54:18 -0500421void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400422{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500423 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400424}
Mike Christie913e5bf2008-05-21 15:54:18 -0500425EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400426
Mike Christie9c19a7d2008-05-21 15:54:09 -0500427static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400428{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500430 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400431}
432
Mike Christie9c19a7d2008-05-21 15:54:09 -0500433void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500434{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500435 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500436
437 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500438 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500439 spin_unlock_bh(&session->lock);
440}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500441EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500442
Mike Christie3bbaaad2009-05-13 17:57:46 -0500443/**
444 * iscsi_complete_task - finish a task
445 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500446 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500447 *
448 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600449 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500450static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600451{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500452 struct iscsi_conn *conn = task->conn;
453
Mike Christieb3cd5052009-05-13 17:57:49 -0500454 if (task->state == ISCSI_TASK_COMPLETED ||
455 task->state == ISCSI_TASK_ABRT_TMF ||
456 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500457 return;
458 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500459 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500460
461 if (!list_empty(&task->running))
462 list_del_init(&task->running);
463
464 if (conn->task == task)
465 conn->task = NULL;
466
467 if (conn->ping_task == task)
468 conn->ping_task = NULL;
469
470 /* release get from queueing */
471 __iscsi_put_task(task);
472}
473
474/*
475 * session lock must be held and if not called for a task that is
476 * still pending or from the xmit thread, then xmit thread must
477 * be suspended.
478 */
479static void fail_scsi_task(struct iscsi_task *task, int err)
480{
481 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600482 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500483 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600484
Mike Christie3bbaaad2009-05-13 17:57:46 -0500485 /*
486 * if a command completes and we get a successful tmf response
487 * we will hit this because the scsi eh abort code does not take
488 * a ref to the task.
489 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500490 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600491 if (!sc)
492 return;
493
Mike Christieb3cd5052009-05-13 17:57:49 -0500494 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600495 /*
496 * cmd never made it to the xmit thread, so we should not count
497 * the cmd in the sequencing
498 */
499 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500500 /* it was never sent so just complete like normal */
501 state = ISCSI_TASK_COMPLETED;
502 } else if (err == DID_TRANSPORT_DISRUPTED)
503 state = ISCSI_TASK_ABRT_SESS_RECOV;
504 else
505 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600506
Mike Christieb3cd5052009-05-13 17:57:49 -0500507 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500508 if (!scsi_bidi_cmnd(sc))
509 scsi_set_resid(sc, scsi_bufflen(sc));
510 else {
511 scsi_out(sc)->resid = scsi_out(sc)->length;
512 scsi_in(sc)->resid = scsi_in(sc)->length;
513 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500514
Mike Christieb3cd5052009-05-13 17:57:49 -0500515 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600516}
517
Mike Christie3e5c28a2008-05-21 15:54:06 -0500518static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500520{
521 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600522 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500523 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
524
525 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
526 return -ENOTCONN;
527
528 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
529 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
530 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
531 /*
532 * pre-format CmdSN for outgoing PDU.
533 */
534 nop->cmdsn = cpu_to_be32(session->cmdsn);
535 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500536 /*
537 * TODO: We always use immediate, so we never hit this.
538 * If we start to send tmfs or nops as non-immediate then
539 * we should start checking the cmdsn numbers for mgmt tasks.
540 */
541 if (conn->c_stage == ISCSI_CONN_STARTED &&
542 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
543 session->queued_cmdsn++;
544 session->cmdsn++;
545 }
546 }
547
Mike Christieae15f802008-12-02 00:32:15 -0600548 if (session->tt->init_task && session->tt->init_task(task))
549 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500550
551 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
552 session->state = ISCSI_STATE_LOGGING_OUT;
553
Mike Christie577577d2008-12-02 00:32:05 -0600554 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600555 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
556 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
557 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500558 return 0;
559}
560
Mike Christie9c19a7d2008-05-21 15:54:09 -0500561static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600562__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
563 char *data, uint32_t data_size)
564{
565 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500566 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500567 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600568 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600569
570 if (session->state == ISCSI_STATE_TERMINATE)
571 return NULL;
572
573 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
574 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
575 /*
576 * Login and Text are sent serially, in
577 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500578 * Same task can be used. Same ITT must be used.
579 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600580 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500581 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600582 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500583 if (session->state != ISCSI_STATE_LOGGED_IN)
584 return NULL;
585
Mike Christief6d51802007-12-13 12:43:30 -0600586 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
587 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
588
Mike Christie3e5c28a2008-05-21 15:54:06 -0500589 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500590 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600591 return NULL;
592 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500593 /*
594 * released in complete pdu for task we expect a response for, and
595 * released by the lld when it has transmitted the task for
596 * pdus we do not expect a response for.
597 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500598 atomic_set(&task->refcount, 1);
599 task->conn = conn;
600 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500601 INIT_LIST_HEAD(&task->running);
602 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600603
604 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500605 memcpy(task->data, data, data_size);
606 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600607 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500608 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600609
Mike Christie184b57c2009-05-13 17:57:39 -0500610 if (conn->session->tt->alloc_pdu) {
611 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
612 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
613 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500614 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500615 }
Mike Christie577577d2008-12-02 00:32:05 -0600616 }
Mike Christie184b57c2009-05-13 17:57:39 -0500617
Mike Christie262ef632008-12-02 00:32:13 -0600618 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600619 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500620 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600621
622 if (hdr->itt != RESERVED_ITT) {
623 if (session->tt->parse_pdu_itt)
624 task->hdr->itt = itt;
625 else
626 task->hdr->itt = build_itt(task->itt,
627 task->conn->session->age);
628 }
629
Mike Christie1336aed2009-05-13 17:57:48 -0500630 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600631 if (iscsi_prep_mgmt_task(conn, task))
632 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500633
Mike Christie9c19a7d2008-05-21 15:54:09 -0500634 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600635 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500636 } else {
637 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600638 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500639 }
Mike Christie052d0142008-05-21 15:54:05 -0500640
Mike Christie9c19a7d2008-05-21 15:54:09 -0500641 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600642
643free_task:
644 __iscsi_put_task(task);
645 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600646}
647
648int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
649 char *data, uint32_t data_size)
650{
651 struct iscsi_conn *conn = cls_conn->dd_data;
652 struct iscsi_session *session = conn->session;
653 int err = 0;
654
655 spin_lock_bh(&session->lock);
656 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
657 err = -EPERM;
658 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600659 return err;
660}
661EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
662
Mike Christie7996a772006-04-06 21:13:41 -0500663/**
664 * iscsi_cmd_rsp - SCSI Command Response processing
665 * @conn: iscsi connection
666 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500667 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500668 * @data: cmd data buffer
669 * @datalen: len of buffer
670 *
671 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500672 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500673 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500674static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500675 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500676 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500677{
Mike Christie7996a772006-04-06 21:13:41 -0500678 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
679 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500680 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500681
Mike Christie77a23c22007-05-30 12:57:18 -0500682 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500683 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
684
685 sc->result = (DID_OK << 16) | rhdr->cmd_status;
686
687 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
688 sc->result = DID_ERROR << 16;
689 goto out;
690 }
691
692 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600693 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500694
695 if (datalen < 2) {
696invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600697 iscsi_conn_printk(KERN_ERR, conn,
698 "Got CHECK_CONDITION but invalid data "
699 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500700 sc->result = DID_BAD_TARGET << 16;
701 goto out;
702 }
703
Harvey Harrison8f333992008-05-21 15:54:20 -0500704 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500705 if (datalen < senselen)
706 goto invalid_datalen;
707
708 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600709 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600710 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
711 min_t(uint16_t, senselen,
712 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500713 }
714
Boaz Harroshc07d4442008-04-18 10:11:52 -0500715 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
716 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
717 int res_count = be32_to_cpu(rhdr->bi_residual_count);
718
719 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
720 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
721 res_count <= scsi_in(sc)->length))
722 scsi_in(sc)->resid = res_count;
723 else
724 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
725 }
726
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600727 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
728 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500729 int res_count = be32_to_cpu(rhdr->residual_count);
730
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600731 if (res_count > 0 &&
732 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
733 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500734 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900735 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500736 else
737 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500738 }
Mike Christie7996a772006-04-06 21:13:41 -0500739out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500740 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600741 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500742 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500743 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500744}
745
Mike Christie1d9edf02008-09-24 11:46:09 -0500746/**
747 * iscsi_data_in_rsp - SCSI Data-In Response processing
748 * @conn: iscsi connection
749 * @hdr: iscsi pdu
750 * @task: scsi command task
751 **/
752static void
753iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
754 struct iscsi_task *task)
755{
756 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
757 struct scsi_cmnd *sc = task->sc;
758
759 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
760 return;
761
Mike Christieedbc9aa052009-05-13 17:57:42 -0500762 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500763 sc->result = (DID_OK << 16) | rhdr->cmd_status;
764 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
765 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
766 ISCSI_FLAG_DATA_OVERFLOW)) {
767 int res_count = be32_to_cpu(rhdr->residual_count);
768
769 if (res_count > 0 &&
770 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
771 res_count <= scsi_in(sc)->length))
772 scsi_in(sc)->resid = res_count;
773 else
774 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
775 }
776
Mike Christie3bbaaad2009-05-13 17:57:46 -0500777 ISCSI_DBG_SESSION(conn->session, "data in with status done "
778 "[sc %p res %d itt 0x%x]\n",
779 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500780 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500781 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500782}
783
Mike Christie7ea8b822006-07-24 15:47:22 -0500784static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
785{
786 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
787
788 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
789 conn->tmfrsp_pdus_cnt++;
790
Mike Christie843c0a82007-12-13 12:43:20 -0600791 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500792 return;
793
794 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600795 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500796 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600797 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500798 else
Mike Christie843c0a82007-12-13 12:43:20 -0600799 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500800 wake_up(&conn->ehwait);
801}
802
Mike Christief6d51802007-12-13 12:43:30 -0600803static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
804{
805 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500806 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600807
Mike Christie9c19a7d2008-05-21 15:54:09 -0500808 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600809 return;
810
811 memset(&hdr, 0, sizeof(struct iscsi_nopout));
812 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
813 hdr.flags = ISCSI_FLAG_CMD_FINAL;
814
815 if (rhdr) {
816 memcpy(hdr.lun, rhdr->lun, 8);
817 hdr.ttt = rhdr->ttt;
818 hdr.itt = RESERVED_ITT;
819 } else
820 hdr.ttt = RESERVED_ITT;
821
Mike Christie9c19a7d2008-05-21 15:54:09 -0500822 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
823 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600824 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600825 else if (!rhdr) {
826 /* only track our nops */
827 conn->ping_task = task;
828 conn->last_ping = jiffies;
829 }
Mike Christief6d51802007-12-13 12:43:30 -0600830}
831
Mike Christie62f38302006-08-31 18:09:27 -0400832static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
833 char *data, int datalen)
834{
835 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
836 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400837
838 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
839
840 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
841 if (ntoh24(reject->dlength) > datalen)
842 return ISCSI_ERR_PROTO;
843
844 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
845 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600846 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600847 "pdu (op 0x%x) rejected "
848 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600849 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400850 }
851 }
852 return 0;
853}
854
Mike Christie7996a772006-04-06 21:13:41 -0500855/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500856 * iscsi_itt_to_task - look up task by itt
857 * @conn: iscsi connection
858 * @itt: itt
859 *
860 * This should be used for mgmt tasks like login and nops, or if
861 * the LDD's itt space does not include the session age.
862 *
863 * The session lock must be held.
864 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500865struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500866{
867 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600868 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500869
870 if (itt == RESERVED_ITT)
871 return NULL;
872
Mike Christie262ef632008-12-02 00:32:13 -0600873 if (session->tt->parse_pdu_itt)
874 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
875 else
876 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500877 if (i >= session->cmds_max)
878 return NULL;
879
880 return session->cmds[i];
881}
Mike Christie8f9256c2009-05-13 17:57:41 -0500882EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500883
884/**
Mike Christie7996a772006-04-06 21:13:41 -0500885 * __iscsi_complete_pdu - complete pdu
886 * @conn: iscsi conn
887 * @hdr: iscsi header
888 * @data: data buffer
889 * @datalen: len of data buffer
890 *
891 * Completes pdu processing by freeing any resources allocated at
892 * queuecommand or send generic. session lock must be held and verify
893 * itt must have been called.
894 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500895int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
896 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500897{
898 struct iscsi_session *session = conn->session;
899 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500900 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500901 uint32_t itt;
902
Mike Christief6d51802007-12-13 12:43:30 -0600903 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500904 rc = iscsi_verify_itt(conn, hdr->itt);
905 if (rc)
906 return rc;
907
Al Virob4377352007-02-09 16:39:40 +0000908 if (hdr->itt != RESERVED_ITT)
909 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500910 else
Al Virob4377352007-02-09 16:39:40 +0000911 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500912
Mike Christie1b2c7af2009-03-05 14:45:58 -0600913 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
914 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500915
Mike Christie3e5c28a2008-05-21 15:54:06 -0500916 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500917 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400918
Mike Christie7996a772006-04-06 21:13:41 -0500919 switch(opcode) {
920 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500921 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500922 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500923 break;
924 }
925
Al Virob4377352007-02-09 16:39:40 +0000926 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500927 break;
928
Mike Christief6d51802007-12-13 12:43:30 -0600929 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500930 break;
931 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400932 rc = iscsi_handle_reject(conn, hdr, data, datalen);
933 break;
Mike Christie7996a772006-04-06 21:13:41 -0500934 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500935 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400936 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
937 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500938 break;
939 default:
940 rc = ISCSI_ERR_BAD_OPCODE;
941 break;
942 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500943 goto out;
944 }
Mike Christie7996a772006-04-06 21:13:41 -0500945
Mike Christie3e5c28a2008-05-21 15:54:06 -0500946 switch(opcode) {
947 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500948 case ISCSI_OP_SCSI_DATA_IN:
949 task = iscsi_itt_to_ctask(conn, hdr->itt);
950 if (!task)
951 return ISCSI_ERR_BAD_ITT;
952 break;
953 case ISCSI_OP_R2T:
954 /*
955 * LLD handles R2Ts if they need to.
956 */
957 return 0;
958 case ISCSI_OP_LOGOUT_RSP:
959 case ISCSI_OP_LOGIN_RSP:
960 case ISCSI_OP_TEXT_RSP:
961 case ISCSI_OP_SCSI_TMFUNC_RSP:
962 case ISCSI_OP_NOOP_IN:
963 task = iscsi_itt_to_task(conn, hdr->itt);
964 if (!task)
965 return ISCSI_ERR_BAD_ITT;
966 break;
967 default:
968 return ISCSI_ERR_BAD_OPCODE;
969 }
970
971 switch(opcode) {
972 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500973 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500974 break;
975 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500976 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500977 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500978 case ISCSI_OP_LOGOUT_RSP:
979 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
980 if (datalen) {
981 rc = ISCSI_ERR_PROTO;
982 break;
983 }
984 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
985 goto recv_pdu;
986 case ISCSI_OP_LOGIN_RSP:
987 case ISCSI_OP_TEXT_RSP:
988 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
989 /*
990 * login related PDU's exp_statsn is handled in
991 * userspace
992 */
993 goto recv_pdu;
994 case ISCSI_OP_SCSI_TMFUNC_RSP:
995 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
996 if (datalen) {
997 rc = ISCSI_ERR_PROTO;
998 break;
999 }
1000
1001 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001002 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001003 break;
1004 case ISCSI_OP_NOOP_IN:
1005 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1006 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1007 rc = ISCSI_ERR_PROTO;
1008 break;
1009 }
1010 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1011
Mike Christie9c19a7d2008-05-21 15:54:09 -05001012 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -05001013 /*
1014 * If this is not in response to one of our
1015 * nops then it must be from userspace.
1016 */
1017 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001018
1019 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
Mike Christieb3cd5052009-05-13 17:57:49 -05001020 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001021 break;
1022 default:
1023 rc = ISCSI_ERR_BAD_OPCODE;
1024 break;
1025 }
1026
1027out:
1028 return rc;
1029recv_pdu:
1030 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1031 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001032 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001033 return rc;
1034}
Mike Christie913e5bf2008-05-21 15:54:18 -05001035EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001036
1037int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1038 char *data, int datalen)
1039{
1040 int rc;
1041
1042 spin_lock(&conn->session->lock);
1043 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1044 spin_unlock(&conn->session->lock);
1045 return rc;
1046}
1047EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1048
Mike Christie0af967f2008-05-21 15:54:04 -05001049int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001050{
1051 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001052 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001053
Mike Christie0af967f2008-05-21 15:54:04 -05001054 if (itt == RESERVED_ITT)
1055 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001056
Mike Christie262ef632008-12-02 00:32:13 -06001057 if (session->tt->parse_pdu_itt)
1058 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1059 else {
1060 i = get_itt(itt);
1061 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1062 }
1063
1064 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001065 iscsi_conn_printk(KERN_ERR, conn,
1066 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001067 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001068 return ISCSI_ERR_BAD_ITT;
1069 }
Mike Christie7996a772006-04-06 21:13:41 -05001070
Mike Christie3e5c28a2008-05-21 15:54:06 -05001071 if (i >= session->cmds_max) {
1072 iscsi_conn_printk(KERN_ERR, conn,
1073 "received invalid itt index %u (max cmds "
1074 "%u.\n", i, session->cmds_max);
1075 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001076 }
Mike Christie7996a772006-04-06 21:13:41 -05001077 return 0;
1078}
1079EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1080
Mike Christie913e5bf2008-05-21 15:54:18 -05001081/**
1082 * iscsi_itt_to_ctask - look up ctask by itt
1083 * @conn: iscsi connection
1084 * @itt: itt
1085 *
1086 * This should be used for cmd tasks.
1087 *
1088 * The session lock must be held.
1089 */
1090struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001091{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001092 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001093
1094 if (iscsi_verify_itt(conn, itt))
1095 return NULL;
1096
Mike Christie913e5bf2008-05-21 15:54:18 -05001097 task = iscsi_itt_to_task(conn, itt);
1098 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001099 return NULL;
1100
Mike Christie913e5bf2008-05-21 15:54:18 -05001101 if (task->sc->SCp.phase != conn->session->age) {
1102 iscsi_session_printk(KERN_ERR, conn->session,
1103 "task's session age %d, expected %d\n",
1104 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001105 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001106 }
Mike Christie0af967f2008-05-21 15:54:04 -05001107
Mike Christie9c19a7d2008-05-21 15:54:09 -05001108 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001109}
1110EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1111
Mike Christie40a06e72009-03-05 14:46:05 -06001112void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001113 enum iscsi_err err)
1114{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001115 struct iscsi_conn *conn;
1116 struct device *dev;
1117 unsigned long flags;
1118
1119 spin_lock_irqsave(&session->lock, flags);
1120 conn = session->leadconn;
1121 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1122 spin_unlock_irqrestore(&session->lock, flags);
1123 return;
1124 }
1125
1126 dev = get_device(&conn->cls_conn->dev);
1127 spin_unlock_irqrestore(&session->lock, flags);
1128 if (!dev)
1129 return;
1130 /*
1131 * if the host is being removed bypass the connection
1132 * recovery initialization because we are going to kill
1133 * the session.
1134 */
1135 if (err == ISCSI_ERR_INVALID_HOST)
1136 iscsi_conn_error_event(conn->cls_conn, err);
1137 else
1138 iscsi_conn_failure(conn, err);
1139 put_device(dev);
1140}
1141EXPORT_SYMBOL_GPL(iscsi_session_failure);
1142
Mike Christie7996a772006-04-06 21:13:41 -05001143void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1144{
1145 struct iscsi_session *session = conn->session;
1146 unsigned long flags;
1147
1148 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001149 if (session->state == ISCSI_STATE_FAILED) {
1150 spin_unlock_irqrestore(&session->lock, flags);
1151 return;
1152 }
1153
Mike Christie67a61112006-05-30 00:37:20 -05001154 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001155 session->state = ISCSI_STATE_FAILED;
1156 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001157
Mike Christie7996a772006-04-06 21:13:41 -05001158 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1159 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001160 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001161}
1162EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1163
Mike Christie77a23c22007-05-30 12:57:18 -05001164static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1165{
1166 struct iscsi_session *session = conn->session;
1167
1168 /*
1169 * Check for iSCSI window and take care of CmdSN wrap-around
1170 */
Mike Christiee0726402007-07-26 12:46:48 -05001171 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001172 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1173 "%u MaxCmdSN %u CmdSN %u/%u\n",
1174 session->exp_cmdsn, session->max_cmdsn,
1175 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001176 return -ENOSPC;
1177 }
1178 return 0;
1179}
1180
Mike Christie9c19a7d2008-05-21 15:54:09 -05001181static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001182{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001183 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001184 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001185
Mike Christie9c19a7d2008-05-21 15:54:09 -05001186 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001187 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001188 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001189 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001190 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001191 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001192 /* done with this task */
1193 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001194 return rc;
1195}
1196
Mike Christie7996a772006-04-06 21:13:41 -05001197/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001198 * iscsi_requeue_task - requeue task to run from session workqueue
1199 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001200 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001201 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001202 * this. The session lock must be held. This should only be called
1203 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001204 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001205void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001206{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001207 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001208
Mike Christie3bbaaad2009-05-13 17:57:46 -05001209 /*
1210 * this may be on the requeue list already if the xmit_task callout
1211 * is handling the r2ts while we are adding new ones
1212 */
1213 if (list_empty(&task->running))
1214 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001215 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001216}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001217EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001218
1219/**
Mike Christie7996a772006-04-06 21:13:41 -05001220 * iscsi_data_xmit - xmit any command into the scheduled connection
1221 * @conn: iscsi connection
1222 *
1223 * Notes:
1224 * The function can return -EAGAIN in which case the caller must
1225 * re-schedule it again later or recover. '0' return code means
1226 * successful xmit.
1227 **/
1228static int iscsi_data_xmit(struct iscsi_conn *conn)
1229{
Mike Christie3219e522006-05-30 00:37:28 -05001230 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001231
Mike Christie77a23c22007-05-30 12:57:18 -05001232 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001233 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001234 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001235 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001236 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001237 }
Mike Christie7996a772006-04-06 21:13:41 -05001238
Mike Christie9c19a7d2008-05-21 15:54:09 -05001239 if (conn->task) {
1240 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001241 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001242 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001243 }
1244
Mike Christie77a23c22007-05-30 12:57:18 -05001245 /*
1246 * process mgmt pdus like nops before commands since we should
1247 * only have one nop-out as a ping from us and targets should not
1248 * overflow us with nop-ins
1249 */
1250check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001251 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001252 conn->task = list_entry(conn->mgmtqueue.next,
1253 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001254 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001255 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1256 __iscsi_put_task(conn->task);
1257 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001258 continue;
1259 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001260 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001261 if (rc)
1262 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001263 }
1264
Mike Christie843c0a82007-12-13 12:43:20 -06001265 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001266 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001267 if (conn->tmf_state == TMF_QUEUED)
1268 break;
1269
Mike Christie3bbaaad2009-05-13 17:57:46 -05001270 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001271 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001272 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001273 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001274 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001275 continue;
1276 }
Mike Christie577577d2008-12-02 00:32:05 -06001277 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1278 if (rc) {
1279 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001280 list_add_tail(&conn->task->running,
1281 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001282 conn->task = NULL;
1283 goto again;
1284 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001285 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001286 continue;
1287 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001288 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001289 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001290 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001291 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001292 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001293 * we need to check the mgmt queue for nops that need to
1294 * be sent to aviod starvation
1295 */
Mike Christie843c0a82007-12-13 12:43:20 -06001296 if (!list_empty(&conn->mgmtqueue))
1297 goto check_mgmt;
1298 }
1299
1300 while (!list_empty(&conn->requeue)) {
1301 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1302 break;
1303
Mike Christieb3a7ea82007-12-13 12:43:26 -06001304 /*
1305 * we always do fastlogout - conn stop code will clean up.
1306 */
1307 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1308 break;
1309
Mike Christie9c19a7d2008-05-21 15:54:09 -05001310 conn->task = list_entry(conn->requeue.next,
1311 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001312 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001313 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001314 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001315 if (rc)
1316 goto again;
1317 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001318 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001319 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001320 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001321 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001322
1323again:
1324 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001325 rc = -ENODATA;
1326 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001327 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001328}
1329
David Howellsc4028952006-11-22 14:57:56 +00001330static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001331{
David Howellsc4028952006-11-22 14:57:56 +00001332 struct iscsi_conn *conn =
1333 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001334 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001335 /*
1336 * serialize Xmit worker on a per-connection basis.
1337 */
Mike Christie3219e522006-05-30 00:37:28 -05001338 do {
1339 rc = iscsi_data_xmit(conn);
1340 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001341}
1342
Mike Christie577577d2008-12-02 00:32:05 -06001343static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1344 struct scsi_cmnd *sc)
1345{
1346 struct iscsi_task *task;
1347
1348 if (!__kfifo_get(conn->session->cmdpool.queue,
1349 (void *) &task, sizeof(void *)))
1350 return NULL;
1351
1352 sc->SCp.phase = conn->session->age;
1353 sc->SCp.ptr = (char *) task;
1354
1355 atomic_set(&task->refcount, 1);
1356 task->state = ISCSI_TASK_PENDING;
1357 task->conn = conn;
1358 task->sc = sc;
1359 INIT_LIST_HEAD(&task->running);
1360 return task;
1361}
1362
Mike Christie7996a772006-04-06 21:13:41 -05001363enum {
1364 FAILURE_BAD_HOST = 1,
1365 FAILURE_SESSION_FAILED,
1366 FAILURE_SESSION_FREED,
1367 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001368 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001369 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001370 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001371 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001372 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001373 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001374};
1375
1376int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1377{
Mike Christie75613522008-05-21 15:53:59 -05001378 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001379 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001380 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001381 int reason = 0;
1382 struct iscsi_session *session;
1383 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001384 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001385
1386 sc->scsi_done = done;
1387 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001388 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001389
1390 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001391 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001392 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001393
Mike Christie75613522008-05-21 15:53:59 -05001394 cls_session = starget_to_session(scsi_target(sc->device));
1395 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001396 spin_lock(&session->lock);
1397
Mike Christie75613522008-05-21 15:53:59 -05001398 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001399 if (reason) {
1400 sc->result = reason;
1401 goto fault;
1402 }
1403
Mike Christie301e0f72009-05-13 17:57:47 -05001404 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001405 /*
1406 * to handle the race between when we set the recovery state
1407 * and block the session we requeue here (commands could
1408 * be entering our queuecommand while a block is starting
1409 * up because the block code is not locked)
1410 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001411 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001412 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001413 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001414 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001415 sc->result = DID_IMM_RETRY << 16;
1416 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001417 case ISCSI_STATE_LOGGING_OUT:
1418 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001419 sc->result = DID_IMM_RETRY << 16;
1420 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001421 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001422 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001423 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001424 break;
1425 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001426 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001427 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001428 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001429 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001430 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001431 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001432 }
Mike Christie7996a772006-04-06 21:13:41 -05001433 goto fault;
1434 }
1435
Mike Christie7996a772006-04-06 21:13:41 -05001436 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001437 if (!conn) {
1438 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001439 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001440 goto fault;
1441 }
Mike Christie7996a772006-04-06 21:13:41 -05001442
Mike Christie77a23c22007-05-30 12:57:18 -05001443 if (iscsi_check_cmdsn_window_closed(conn)) {
1444 reason = FAILURE_WINDOW_CLOSED;
1445 goto reject;
1446 }
1447
Mike Christie577577d2008-12-02 00:32:05 -06001448 task = iscsi_alloc_task(conn, sc);
1449 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001450 reason = FAILURE_OOM;
1451 goto reject;
1452 }
Mike Christie7996a772006-04-06 21:13:41 -05001453
Mike Christie1336aed2009-05-13 17:57:48 -05001454 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001455 reason = iscsi_prep_scsi_cmd_pdu(task);
1456 if (reason) {
1457 if (reason == -ENOMEM) {
1458 reason = FAILURE_OOM;
1459 goto prepd_reject;
1460 } else {
1461 sc->result = DID_ABORT << 16;
1462 goto prepd_fault;
1463 }
Mike Christie052d0142008-05-21 15:54:05 -05001464 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001465 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001466 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001467 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001468 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001469 } else {
1470 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001471 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001472 }
Mike Christie052d0142008-05-21 15:54:05 -05001473
1474 session->queued_cmdsn++;
1475 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001476 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001477 return 0;
1478
Mike Christie577577d2008-12-02 00:32:05 -06001479prepd_reject:
1480 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001481 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001482reject:
1483 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001484 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1485 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001486 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001487 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001488
Mike Christie577577d2008-12-02 00:32:05 -06001489prepd_fault:
1490 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001491 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001492fault:
1493 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001494 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1495 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001496 if (!scsi_bidi_cmnd(sc))
1497 scsi_set_resid(sc, scsi_bufflen(sc));
1498 else {
1499 scsi_out(sc)->resid = scsi_out(sc)->length;
1500 scsi_in(sc)->resid = scsi_in(sc)->length;
1501 }
Mike Christie052d0142008-05-21 15:54:05 -05001502 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001503 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001504 return 0;
1505}
1506EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1507
1508int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1509{
Mike Christie7996a772006-04-06 21:13:41 -05001510 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1511 return sdev->queue_depth;
1512}
1513EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1514
Mike Christie6b5d6c42009-04-21 15:32:32 -05001515int iscsi_target_alloc(struct scsi_target *starget)
1516{
1517 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1518 struct iscsi_session *session = cls_session->dd_data;
1519
1520 starget->can_queue = session->scsi_cmds_max;
1521 return 0;
1522}
1523EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1524
Mike Christie7996a772006-04-06 21:13:41 -05001525void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1526{
Mike Christie75613522008-05-21 15:53:59 -05001527 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001528
1529 spin_lock_bh(&session->lock);
1530 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001531 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001532 if (session->leadconn)
1533 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001534 }
1535 spin_unlock_bh(&session->lock);
1536}
1537EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1538
Mike Christie8e124522008-09-24 11:46:12 -05001539int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001540{
Mike Christie75613522008-05-21 15:53:59 -05001541 struct iscsi_cls_session *cls_session;
1542 struct iscsi_session *session;
1543 struct iscsi_conn *conn;
1544
1545 cls_session = starget_to_session(scsi_target(sc->device));
1546 session = cls_session->dd_data;
1547 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001548
Mike Christiebc436b22007-12-13 12:43:28 -06001549 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001550 spin_lock_bh(&session->lock);
1551 if (session->state == ISCSI_STATE_TERMINATE) {
1552failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001553 iscsi_session_printk(KERN_INFO, session,
1554 "failing target reset: Could not log "
1555 "back into target [age %d]\n",
1556 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001557 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001558 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001559 return FAILED;
1560 }
1561
Mike Christie7996a772006-04-06 21:13:41 -05001562 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001563 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001564 /*
1565 * we drop the lock here but the leadconn cannot be destoyed while
1566 * we are in the scsi eh
1567 */
Mike Christie843c0a82007-12-13 12:43:20 -06001568 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001569
Mike Christie1b2c7af2009-03-05 14:45:58 -06001570 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001571 wait_event_interruptible(conn->ehwait,
1572 session->state == ISCSI_STATE_TERMINATE ||
1573 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001574 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001575 if (signal_pending(current))
1576 flush_signals(current);
1577
Mike Christiebc436b22007-12-13 12:43:28 -06001578 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001579 spin_lock_bh(&session->lock);
1580 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001581 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001582 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001583 else
1584 goto failed;
1585 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001586 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001587 return SUCCESS;
1588}
Mike Christie8e124522008-09-24 11:46:12 -05001589EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001590
Mike Christie843c0a82007-12-13 12:43:20 -06001591static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001592{
Mike Christie843c0a82007-12-13 12:43:20 -06001593 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001594 struct iscsi_session *session = conn->session;
1595
1596 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001597 if (conn->tmf_state == TMF_QUEUED) {
1598 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001599 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001600 /* unblock eh_abort() */
1601 wake_up(&conn->ehwait);
1602 }
1603 spin_unlock(&session->lock);
1604}
1605
Mike Christie9c19a7d2008-05-21 15:54:09 -05001606static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001607 struct iscsi_tm *hdr, int age,
1608 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001609{
Mike Christie7996a772006-04-06 21:13:41 -05001610 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001611 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001612
Mike Christie9c19a7d2008-05-21 15:54:09 -05001613 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001614 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001615 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001616 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001617 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001618 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001619 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001620 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001621 }
Mike Christie843c0a82007-12-13 12:43:20 -06001622 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001623 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001624 conn->tmf_timer.function = iscsi_tmf_timedout;
1625 conn->tmf_timer.data = (unsigned long)conn;
1626 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001627 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001628
Mike Christie7996a772006-04-06 21:13:41 -05001629 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001630 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001631
1632 /*
1633 * block eh thread until:
1634 *
Mike Christie843c0a82007-12-13 12:43:20 -06001635 * 1) tmf response
1636 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001637 * 3) session is terminated or restarted or userspace has
1638 * given up on recovery
1639 */
Mike Christie843c0a82007-12-13 12:43:20 -06001640 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001641 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001642 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001643 if (signal_pending(current))
1644 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001645 del_timer_sync(&conn->tmf_timer);
1646
Mike Christie6724add2007-08-15 01:38:30 -05001647 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001648 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001649 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001650 if (age != session->age ||
1651 session->state != ISCSI_STATE_LOGGED_IN)
1652 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001653 return 0;
1654}
1655
1656/*
Mike Christie843c0a82007-12-13 12:43:20 -06001657 * Fail commands. session lock held and recv side suspended and xmit
1658 * thread flushed
1659 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001660static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1661 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001662{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001663 struct iscsi_task *task;
1664 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001665
Mike Christie3bbaaad2009-05-13 17:57:46 -05001666 for (i = 0; i < conn->session->cmds_max; i++) {
1667 task = conn->session->cmds[i];
1668 if (!task->sc || task->state == ISCSI_TASK_FREE)
1669 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001670
Mike Christie3bbaaad2009-05-13 17:57:46 -05001671 if (lun != -1 && lun != task->sc->device->lun)
1672 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001673
Mike Christie3bbaaad2009-05-13 17:57:46 -05001674 ISCSI_DBG_SESSION(conn->session,
1675 "failing sc %p itt 0x%x state %d\n",
1676 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001677 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001678 }
1679}
1680
Mike Christieb40977d2008-05-21 15:54:03 -05001681void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001682{
Mike Christie32ae7632009-03-05 14:46:03 -06001683 struct Scsi_Host *shost = conn->session->host;
1684 struct iscsi_host *ihost = shost_priv(shost);
1685
Mike Christie6724add2007-08-15 01:38:30 -05001686 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001687 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001688 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001689}
Mike Christieb40977d2008-05-21 15:54:03 -05001690EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001691
1692static void iscsi_start_tx(struct iscsi_conn *conn)
1693{
1694 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001695 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001696}
1697
Mike Christie4c48a822009-05-13 17:57:45 -05001698/*
1699 * We want to make sure a ping is in flight. It has timed out.
1700 * And we are not busy processing a pdu that is making
1701 * progress but got started before the ping and is taking a while
1702 * to complete so the ping is just stuck behind it in a queue.
1703 */
1704static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1705{
1706 if (conn->ping_task &&
1707 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1708 (conn->ping_timeout * HZ), jiffies))
1709 return 1;
1710 else
1711 return 0;
1712}
1713
Jens Axboe242f9dc2008-09-14 05:55:09 -07001714static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001715{
1716 struct iscsi_cls_session *cls_session;
1717 struct iscsi_session *session;
1718 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001719 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001720
1721 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001722 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001723
Mike Christie1b2c7af2009-03-05 14:45:58 -06001724 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
Mike Christief6d51802007-12-13 12:43:30 -06001725
1726 spin_lock(&session->lock);
1727 if (session->state != ISCSI_STATE_LOGGED_IN) {
1728 /*
1729 * We are probably in the middle of iscsi recovery so let
1730 * that complete and handle the error.
1731 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001732 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001733 goto done;
1734 }
1735
1736 conn = session->leadconn;
1737 if (!conn) {
1738 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001739 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001740 goto done;
1741 }
1742
1743 if (!conn->recv_timeout && !conn->ping_timeout)
1744 goto done;
1745 /*
1746 * if the ping timedout then we are in the middle of cleaning up
1747 * and can let the iscsi eh handle it
1748 */
Mike Christie4c48a822009-05-13 17:57:45 -05001749 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001750 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001751 goto done;
1752 }
Mike Christief6d51802007-12-13 12:43:30 -06001753 /*
1754 * if we are about to check the transport then give the command
1755 * more time
1756 */
1757 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
Mike Christie4c48a822009-05-13 17:57:45 -05001758 jiffies)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001759 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001760 goto done;
1761 }
1762
Mike Christief6d51802007-12-13 12:43:30 -06001763 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001764 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001765 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001766done:
1767 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001768 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1769 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001770 return rc;
1771}
1772
1773static void iscsi_check_transport_timeouts(unsigned long data)
1774{
1775 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1776 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001777 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001778
1779 spin_lock(&session->lock);
1780 if (session->state != ISCSI_STATE_LOGGED_IN)
1781 goto done;
1782
Mike Christie4cf10432008-05-07 20:43:52 -05001783 recv_timeout = conn->recv_timeout;
1784 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001785 goto done;
1786
Mike Christie4cf10432008-05-07 20:43:52 -05001787 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001788 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001789
1790 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001791 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001792 "expired, recv timeout %d, last rx %lu, "
1793 "last ping %lu, now %lu\n",
1794 conn->ping_timeout, conn->recv_timeout,
1795 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001796 spin_unlock(&session->lock);
1797 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1798 return;
1799 }
1800
Mike Christie4cf10432008-05-07 20:43:52 -05001801 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001802 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001803 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001804 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001805 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001806 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001807 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001808
Mike Christie1b2c7af2009-03-05 14:45:58 -06001809 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001810 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001811done:
1812 spin_unlock(&session->lock);
1813}
1814
Mike Christie9c19a7d2008-05-21 15:54:09 -05001815static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001816 struct iscsi_tm *hdr)
1817{
1818 memset(hdr, 0, sizeof(*hdr));
1819 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1820 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1821 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001822 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1823 hdr->rtt = task->hdr_itt;
1824 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001825}
1826
Mike Christie7996a772006-04-06 21:13:41 -05001827int iscsi_eh_abort(struct scsi_cmnd *sc)
1828{
Mike Christie75613522008-05-21 15:53:59 -05001829 struct iscsi_cls_session *cls_session;
1830 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001831 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001832 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001833 struct iscsi_tm *hdr;
1834 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001835
Mike Christie75613522008-05-21 15:53:59 -05001836 cls_session = starget_to_session(scsi_target(sc->device));
1837 session = cls_session->dd_data;
1838
Mike Christie6724add2007-08-15 01:38:30 -05001839 mutex_lock(&session->eh_mutex);
1840 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001841 /*
1842 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1843 * got the command.
1844 */
1845 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001846 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1847 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001848 spin_unlock_bh(&session->lock);
1849 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001850 return SUCCESS;
1851 }
1852
Mike Christie7996a772006-04-06 21:13:41 -05001853 /*
1854 * If we are not logged in or we have started a new session
1855 * then let the host reset code handle this
1856 */
Mike Christie843c0a82007-12-13 12:43:20 -06001857 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1858 sc->SCp.phase != session->age) {
1859 spin_unlock_bh(&session->lock);
1860 mutex_unlock(&session->eh_mutex);
1861 return FAILED;
1862 }
1863
1864 conn = session->leadconn;
1865 conn->eh_abort_cnt++;
1866 age = session->age;
1867
Mike Christie9c19a7d2008-05-21 15:54:09 -05001868 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001869 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1870 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001871
Mike Christie9c19a7d2008-05-21 15:54:09 -05001872 /* task completed before time out */
1873 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001874 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1875 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001876 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001877 }
Mike Christie7996a772006-04-06 21:13:41 -05001878
Mike Christie9c19a7d2008-05-21 15:54:09 -05001879 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001880 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05001881 goto success;
1882 }
Mike Christie7996a772006-04-06 21:13:41 -05001883
Mike Christie843c0a82007-12-13 12:43:20 -06001884 /* only have one tmf outstanding at a time */
1885 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001886 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001887 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001888
Mike Christie843c0a82007-12-13 12:43:20 -06001889 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001890 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001891
Mike Christie9c19a7d2008-05-21 15:54:09 -05001892 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001893 rc = FAILED;
1894 goto failed;
1895 }
1896
1897 switch (conn->tmf_state) {
1898 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001899 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001900 /*
1901 * stop tx side incase the target had sent a abort rsp but
1902 * the initiator was still writing out data.
1903 */
Mike Christie6724add2007-08-15 01:38:30 -05001904 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001905 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001906 * we do not stop the recv side because targets have been
1907 * good and have never sent us a successful tmf response
1908 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001909 */
Mike Christie77a23c22007-05-30 12:57:18 -05001910 spin_lock(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05001911 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06001912 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001913 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001914 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001915 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001916 case TMF_TIMEDOUT:
1917 spin_unlock_bh(&session->lock);
1918 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1919 goto failed_unlocked;
1920 case TMF_NOT_FOUND:
1921 if (!sc->SCp.ptr) {
1922 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001923 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001924 ISCSI_DBG_SESSION(session, "sc completed while abort "
1925 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001926 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001927 }
1928 /* fall through */
1929 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001930 conn->tmf_state = TMF_INITIAL;
1931 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001932 }
1933
Mike Christie77a23c22007-05-30 12:57:18 -05001934success:
Mike Christie7996a772006-04-06 21:13:41 -05001935 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001936success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001937 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1938 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001939 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001940 return SUCCESS;
1941
1942failed:
1943 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001944failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001945 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1946 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001947 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001948 return FAILED;
1949}
1950EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1951
Mike Christie843c0a82007-12-13 12:43:20 -06001952static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1953{
1954 memset(hdr, 0, sizeof(*hdr));
1955 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1956 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1957 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1958 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001959 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001960}
1961
1962int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1963{
Mike Christie75613522008-05-21 15:53:59 -05001964 struct iscsi_cls_session *cls_session;
1965 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001966 struct iscsi_conn *conn;
1967 struct iscsi_tm *hdr;
1968 int rc = FAILED;
1969
Mike Christie75613522008-05-21 15:53:59 -05001970 cls_session = starget_to_session(scsi_target(sc->device));
1971 session = cls_session->dd_data;
1972
Mike Christie1b2c7af2009-03-05 14:45:58 -06001973 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1974 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06001975
1976 mutex_lock(&session->eh_mutex);
1977 spin_lock_bh(&session->lock);
1978 /*
1979 * Just check if we are not logged in. We cannot check for
1980 * the phase because the reset could come from a ioctl.
1981 */
1982 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1983 goto unlock;
1984 conn = session->leadconn;
1985
1986 /* only have one tmf outstanding at a time */
1987 if (conn->tmf_state != TMF_INITIAL)
1988 goto unlock;
1989 conn->tmf_state = TMF_QUEUED;
1990
1991 hdr = &conn->tmhdr;
1992 iscsi_prep_lun_reset_pdu(sc, hdr);
1993
Mike Christie9c19a7d2008-05-21 15:54:09 -05001994 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001995 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001996 rc = FAILED;
1997 goto unlock;
1998 }
1999
2000 switch (conn->tmf_state) {
2001 case TMF_SUCCESS:
2002 break;
2003 case TMF_TIMEDOUT:
2004 spin_unlock_bh(&session->lock);
2005 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2006 goto done;
2007 default:
2008 conn->tmf_state = TMF_INITIAL;
2009 goto unlock;
2010 }
2011
2012 rc = SUCCESS;
2013 spin_unlock_bh(&session->lock);
2014
2015 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002016
Mike Christiea3439142008-09-24 11:46:15 -05002017 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002018 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002019 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002020 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002021
2022 iscsi_start_tx(conn);
2023 goto done;
2024
2025unlock:
2026 spin_unlock_bh(&session->lock);
2027done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06002028 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
2029 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002030 mutex_unlock(&session->eh_mutex);
2031 return rc;
2032}
2033EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2034
Olaf Kirch63203772007-12-13 12:43:25 -06002035/*
2036 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2037 * should be accessed via kfifo_{get,put} on q->queue.
2038 * Optionally, the caller can obtain the array of object pointers
2039 * by passing in a non-NULL @items pointer
2040 */
Mike Christie7996a772006-04-06 21:13:41 -05002041int
Olaf Kirch63203772007-12-13 12:43:25 -06002042iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002043{
Olaf Kirch63203772007-12-13 12:43:25 -06002044 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002045
Olaf Kirch63203772007-12-13 12:43:25 -06002046 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002047
2048 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002049
2050 /* If the user passed an items pointer, he wants a copy of
2051 * the array. */
2052 if (items)
2053 num_arrays++;
2054 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2055 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002056 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002057
2058 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2059 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002060 if (IS_ERR(q->queue)) {
2061 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002062 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002063 }
Mike Christie7996a772006-04-06 21:13:41 -05002064
2065 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002066 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002067 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002068 q->max = i;
2069 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002070 }
Mike Christie7996a772006-04-06 21:13:41 -05002071 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2072 }
Olaf Kirch63203772007-12-13 12:43:25 -06002073
2074 if (items) {
2075 *items = q->pool + max;
2076 memcpy(*items, q->pool, max * sizeof(void *));
2077 }
2078
Mike Christie7996a772006-04-06 21:13:41 -05002079 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002080
2081enomem:
2082 iscsi_pool_free(q);
2083 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002084}
2085EXPORT_SYMBOL_GPL(iscsi_pool_init);
2086
Olaf Kirch63203772007-12-13 12:43:25 -06002087void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002088{
2089 int i;
2090
2091 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002092 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002093 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002094 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002095}
2096EXPORT_SYMBOL_GPL(iscsi_pool_free);
2097
Mike Christiea4804cd2008-05-21 15:54:00 -05002098/**
2099 * iscsi_host_add - add host to system
2100 * @shost: scsi host
2101 * @pdev: parent device
2102 *
2103 * This should be called by partial offload and software iscsi drivers
2104 * to add a host to the system.
2105 */
2106int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002107{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002108 if (!shost->can_queue)
2109 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2110
Mike Christie4d108352009-03-05 14:46:04 -06002111 if (!shost->cmd_per_lun)
2112 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2113
Mike Christie308cec12009-02-06 12:06:20 -06002114 if (!shost->transportt->eh_timed_out)
2115 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002116 return scsi_add_host(shost, pdev);
2117}
2118EXPORT_SYMBOL_GPL(iscsi_host_add);
2119
2120/**
2121 * iscsi_host_alloc - allocate a host and driver data
2122 * @sht: scsi host template
2123 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002124 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002125 *
2126 * This should be called by partial offload and software iscsi drivers.
2127 * To access the driver specific memory use the iscsi_host_priv() macro.
2128 */
2129struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002130 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002131{
2132 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002133 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002134
2135 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2136 if (!shost)
2137 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002138 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002139
2140 if (xmit_can_sleep) {
2141 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2142 "iscsi_q_%d", shost->host_no);
2143 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2144 if (!ihost->workq)
2145 goto free_host;
2146 }
2147
Mike Christiee5bd7b52008-09-24 11:46:10 -05002148 spin_lock_init(&ihost->lock);
2149 ihost->state = ISCSI_HOST_SETUP;
2150 ihost->num_sessions = 0;
2151 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002152 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002153
2154free_host:
2155 scsi_host_put(shost);
2156 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002157}
Mike Christiea4804cd2008-05-21 15:54:00 -05002158EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002159
Mike Christiee5bd7b52008-09-24 11:46:10 -05002160static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2161{
Mike Christie40a06e72009-03-05 14:46:05 -06002162 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002163}
2164
Mike Christiea4804cd2008-05-21 15:54:00 -05002165/**
2166 * iscsi_host_remove - remove host and sessions
2167 * @shost: scsi host
2168 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002169 * If there are any sessions left, this will initiate the removal and wait
2170 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002171 */
2172void iscsi_host_remove(struct Scsi_Host *shost)
2173{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002174 struct iscsi_host *ihost = shost_priv(shost);
2175 unsigned long flags;
2176
2177 spin_lock_irqsave(&ihost->lock, flags);
2178 ihost->state = ISCSI_HOST_REMOVED;
2179 spin_unlock_irqrestore(&ihost->lock, flags);
2180
2181 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2182 wait_event_interruptible(ihost->session_removal_wq,
2183 ihost->num_sessions == 0);
2184 if (signal_pending(current))
2185 flush_signals(current);
2186
Mike Christiea4804cd2008-05-21 15:54:00 -05002187 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002188 if (ihost->workq)
2189 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002190}
2191EXPORT_SYMBOL_GPL(iscsi_host_remove);
2192
2193void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002194{
2195 struct iscsi_host *ihost = shost_priv(shost);
2196
2197 kfree(ihost->netdev);
2198 kfree(ihost->hwaddress);
2199 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002200 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002201}
Mike Christiea4804cd2008-05-21 15:54:00 -05002202EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002203
Mike Christiee5bd7b52008-09-24 11:46:10 -05002204static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2205{
2206 struct iscsi_host *ihost = shost_priv(shost);
2207 unsigned long flags;
2208
2209 shost = scsi_host_get(shost);
2210 if (!shost) {
2211 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2212 "of session teardown event because host already "
2213 "removed.\n");
2214 return;
2215 }
2216
2217 spin_lock_irqsave(&ihost->lock, flags);
2218 ihost->num_sessions--;
2219 if (ihost->num_sessions == 0)
2220 wake_up(&ihost->session_removal_wq);
2221 spin_unlock_irqrestore(&ihost->lock, flags);
2222 scsi_host_put(shost);
2223}
2224
Mike Christie75613522008-05-21 15:53:59 -05002225/**
2226 * iscsi_session_setup - create iscsi cls session and host and session
2227 * @iscsit: iscsi transport template
2228 * @shost: scsi host
2229 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002230 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002231 * @initial_cmdsn: initial CmdSN
2232 *
2233 * This can be used by software iscsi_transports that allocate
2234 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002235 *
2236 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2237 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2238 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002239 */
2240struct iscsi_cls_session *
2241iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002242 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002243 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002244{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002245 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002246 struct iscsi_session *session;
2247 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002248 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002249 unsigned long flags;
2250
2251 spin_lock_irqsave(&ihost->lock, flags);
2252 if (ihost->state == ISCSI_HOST_REMOVED) {
2253 spin_unlock_irqrestore(&ihost->lock, flags);
2254 return NULL;
2255 }
2256 ihost->num_sessions++;
2257 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002258
2259 if (!total_cmds)
2260 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002261 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002262 * The iscsi layer needs some tasks for nop handling and tmfs,
2263 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2264 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002265 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002266 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2267 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2268 "must be a power of two that is at least %d.\n",
2269 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002270 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002271 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002272
2273 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2274 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2275 "must be a power of 2 less than or equal to %d.\n",
2276 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2277 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2278 }
2279
2280 if (!is_power_of_2(total_cmds)) {
2281 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2282 "must be a power of 2.\n", total_cmds);
2283 total_cmds = rounddown_pow_of_two(total_cmds);
2284 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2285 return NULL;
2286 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2287 total_cmds);
2288 }
2289 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002290
Mike Christie5d91e202008-05-21 15:54:01 -05002291 cls_session = iscsi_alloc_session(shost, iscsit,
2292 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002293 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002294 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002295 session = cls_session->dd_data;
2296 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002297 session->host = shost;
2298 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002299 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002300 session->lu_reset_timeout = 15;
2301 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002302 session->scsi_cmds_max = scsi_cmds;
2303 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002304 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002305 session->exp_cmdsn = initial_cmdsn + 1;
2306 session->max_cmdsn = initial_cmdsn + 1;
2307 session->max_r2t = 1;
2308 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002309 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002310 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002311
2312 /* initialize SCSI PDU commands pool */
2313 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2314 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002315 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002316 goto cmdpool_alloc_fail;
2317
2318 /* pre-format cmds pool with ITT */
2319 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002320 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002321
Mike Christie9c19a7d2008-05-21 15:54:09 -05002322 if (cmd_task_size)
2323 task->dd_data = &task[1];
2324 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002325 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002326 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002327 }
2328
Mike Christief53a88d2006-06-28 12:00:27 -05002329 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002330 goto module_get_fail;
2331
Mike Christie79706342008-05-21 15:54:12 -05002332 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002333 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002334
Mike Christie7996a772006-04-06 21:13:41 -05002335 return cls_session;
2336
2337cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002338 module_put(iscsit->owner);
2339module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002340 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002341cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002342 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002343dec_session_count:
2344 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002345 return NULL;
2346}
2347EXPORT_SYMBOL_GPL(iscsi_session_setup);
2348
2349/**
2350 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002351 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002352 *
Mike Christie75613522008-05-21 15:53:59 -05002353 * The driver must have called iscsi_remove_session before
2354 * calling this.
2355 */
Mike Christie7996a772006-04-06 21:13:41 -05002356void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2357{
Mike Christie75613522008-05-21 15:53:59 -05002358 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002359 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002360 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002361
Olaf Kirch63203772007-12-13 12:43:25 -06002362 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002363
Mike Christieb2c64162007-05-30 12:57:16 -05002364 kfree(session->password);
2365 kfree(session->password_in);
2366 kfree(session->username);
2367 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002368 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002369 kfree(session->initiatorname);
2370 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002371
Mike Christie75613522008-05-21 15:53:59 -05002372 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002373 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002374 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002375}
2376EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2377
2378/**
2379 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2380 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002381 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002382 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002383 */
Mike Christie7996a772006-04-06 21:13:41 -05002384struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002385iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2386 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002387{
Mike Christie75613522008-05-21 15:53:59 -05002388 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002389 struct iscsi_conn *conn;
2390 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002391 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002392
Mike Christie5d91e202008-05-21 15:54:01 -05002393 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2394 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002395 if (!cls_conn)
2396 return NULL;
2397 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002398 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002399
Mike Christie5d91e202008-05-21 15:54:01 -05002400 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002401 conn->session = session;
2402 conn->cls_conn = cls_conn;
2403 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2404 conn->id = conn_idx;
2405 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002406 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002407
2408 init_timer(&conn->transport_timer);
2409 conn->transport_timer.data = (unsigned long)conn;
2410 conn->transport_timer.function = iscsi_check_transport_timeouts;
2411
Mike Christie843c0a82007-12-13 12:43:20 -06002412 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002413 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002414 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002415 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002416
Mike Christie9c19a7d2008-05-21 15:54:09 -05002417 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002418 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002419 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002420 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002421 sizeof(void*))) {
2422 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002423 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002424 }
2425 spin_unlock_bh(&session->lock);
2426
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002427 data = (char *) __get_free_pages(GFP_KERNEL,
2428 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002429 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002430 goto login_task_data_alloc_fail;
2431 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002432
Mike Christie843c0a82007-12-13 12:43:20 -06002433 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002434 init_waitqueue_head(&conn->ehwait);
2435
2436 return cls_conn;
2437
Mike Christie9c19a7d2008-05-21 15:54:09 -05002438login_task_data_alloc_fail:
2439 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002440 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002441login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002442 iscsi_destroy_conn(cls_conn);
2443 return NULL;
2444}
2445EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2446
2447/**
2448 * iscsi_conn_teardown - teardown iscsi connection
2449 * cls_conn: iscsi class connection
2450 *
2451 * TODO: we may need to make this into a two step process
2452 * like scsi-mls remove + put host
2453 */
2454void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2455{
2456 struct iscsi_conn *conn = cls_conn->dd_data;
2457 struct iscsi_session *session = conn->session;
2458 unsigned long flags;
2459
Mike Christief6d51802007-12-13 12:43:30 -06002460 del_timer_sync(&conn->transport_timer);
2461
Mike Christie7996a772006-04-06 21:13:41 -05002462 spin_lock_bh(&session->lock);
2463 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2464 if (session->leadconn == conn) {
2465 /*
2466 * leading connection? then give up on recovery.
2467 */
2468 session->state = ISCSI_STATE_TERMINATE;
2469 wake_up(&conn->ehwait);
2470 }
2471 spin_unlock_bh(&session->lock);
2472
Mike Christie7996a772006-04-06 21:13:41 -05002473 /*
2474 * Block until all in-progress commands for this connection
2475 * time out or fail.
2476 */
2477 for (;;) {
2478 spin_lock_irqsave(session->host->host_lock, flags);
2479 if (!session->host->host_busy) { /* OK for ERL == 0 */
2480 spin_unlock_irqrestore(session->host->host_lock, flags);
2481 break;
2482 }
2483 spin_unlock_irqrestore(session->host->host_lock, flags);
2484 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002485 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2486 "host_busy %d host_failed %d\n",
2487 session->host->host_busy,
2488 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002489 /*
2490 * force eh_abort() to unblock
2491 */
2492 wake_up(&conn->ehwait);
2493 }
2494
Mike Christie779ea122007-02-28 17:32:15 -06002495 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002496 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002497
Mike Christie7996a772006-04-06 21:13:41 -05002498 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002499 free_pages((unsigned long) conn->data,
2500 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002501 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002502 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002503 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002504 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002505 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002506 spin_unlock_bh(&session->lock);
2507
Mike Christie7996a772006-04-06 21:13:41 -05002508 iscsi_destroy_conn(cls_conn);
2509}
2510EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2511
2512int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2513{
2514 struct iscsi_conn *conn = cls_conn->dd_data;
2515 struct iscsi_session *session = conn->session;
2516
Mike Christieffd04362006-08-31 18:09:24 -04002517 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002518 iscsi_conn_printk(KERN_ERR, conn,
2519 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002520 return -EPERM;
2521 }
2522
Mike Christiedb98ccd2006-08-31 18:09:31 -04002523 if ((session->imm_data_en || !session->initial_r2t_en) &&
2524 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002525 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2526 "first_burst %d max_burst %d\n",
2527 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002528 return -EINVAL;
2529 }
2530
Mike Christief6d51802007-12-13 12:43:30 -06002531 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002532 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2533 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002534 conn->recv_timeout = 5;
2535 }
2536
2537 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002538 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2539 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002540 conn->ping_timeout = 5;
2541 }
2542
Mike Christie7996a772006-04-06 21:13:41 -05002543 spin_lock_bh(&session->lock);
2544 conn->c_stage = ISCSI_CONN_STARTED;
2545 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002546 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002547
Mike Christief6d51802007-12-13 12:43:30 -06002548 conn->last_recv = jiffies;
2549 conn->last_ping = jiffies;
2550 if (conn->recv_timeout && conn->ping_timeout)
2551 mod_timer(&conn->transport_timer,
2552 jiffies + (conn->recv_timeout * HZ));
2553
Mike Christie7996a772006-04-06 21:13:41 -05002554 switch(conn->stop_stage) {
2555 case STOP_CONN_RECOVER:
2556 /*
2557 * unblock eh_abort() if it is blocked. re-try all
2558 * commands after successful recovery
2559 */
Mike Christie7996a772006-04-06 21:13:41 -05002560 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002561 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002562 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002563 if (session->age == 16)
2564 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002565 break;
Mike Christie7996a772006-04-06 21:13:41 -05002566 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002567 conn->stop_stage = 0;
2568 break;
Mike Christie7996a772006-04-06 21:13:41 -05002569 default:
2570 break;
2571 }
2572 spin_unlock_bh(&session->lock);
2573
Mike Christie75613522008-05-21 15:53:59 -05002574 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002575 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002576 return 0;
2577}
2578EXPORT_SYMBOL_GPL(iscsi_conn_start);
2579
2580static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002581fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002582{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002583 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002584 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002585
Mike Christie3bbaaad2009-05-13 17:57:46 -05002586 for (i = 0; i < conn->session->cmds_max; i++) {
2587 task = conn->session->cmds[i];
2588 if (task->sc)
2589 continue;
2590
2591 if (task->state == ISCSI_TASK_FREE)
2592 continue;
2593
2594 ISCSI_DBG_SESSION(conn->session,
2595 "failing mgmt itt 0x%x state %d\n",
2596 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002597 state = ISCSI_TASK_ABRT_SESS_RECOV;
2598 if (task->state == ISCSI_TASK_PENDING)
2599 state = ISCSI_TASK_COMPLETED;
2600 iscsi_complete_task(task, state);
2601
Mike Christie7996a772006-04-06 21:13:41 -05002602 }
Mike Christie7996a772006-04-06 21:13:41 -05002603}
2604
Mike Christie656cffc2006-05-18 20:31:42 -05002605static void iscsi_start_session_recovery(struct iscsi_session *session,
2606 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002607{
Mike Christieed2abc72006-05-02 19:46:40 -05002608 int old_stop_stage;
2609
Mike Christie6724add2007-08-15 01:38:30 -05002610 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002611 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002612 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002613 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002614 mutex_unlock(&session->eh_mutex);
2615 return;
2616 }
2617
2618 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002619 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002620 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002621 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002622 */
Mike Christie67a61112006-05-30 00:37:20 -05002623 if (flag == STOP_CONN_TERM)
2624 session->state = ISCSI_STATE_TERMINATE;
2625 else if (conn->stop_stage != STOP_CONN_RECOVER)
2626 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002627 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002628
Mike Christie26013ad2009-05-13 17:57:43 -05002629 del_timer_sync(&conn->transport_timer);
2630 iscsi_suspend_tx(conn);
2631
2632 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002633 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002634 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002635 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002636 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002637
Mike Christie7996a772006-04-06 21:13:41 -05002638 /*
2639 * for connection level recovery we should not calculate
2640 * header digest. conn->hdr_size used for optimization
2641 * in hdr_extract() and will be re-negotiated at
2642 * set_param() time.
2643 */
2644 if (flag == STOP_CONN_RECOVER) {
2645 conn->hdrdgst_en = 0;
2646 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002647 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002648 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002649 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002650 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002651 }
Mike Christie7996a772006-04-06 21:13:41 -05002652 }
Mike Christie656cffc2006-05-18 20:31:42 -05002653
Mike Christie656cffc2006-05-18 20:31:42 -05002654 /*
2655 * flush queues.
2656 */
2657 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002658 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002659 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002660 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002661 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002662}
Mike Christie7996a772006-04-06 21:13:41 -05002663
2664void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2665{
2666 struct iscsi_conn *conn = cls_conn->dd_data;
2667 struct iscsi_session *session = conn->session;
2668
2669 switch (flag) {
2670 case STOP_CONN_RECOVER:
2671 case STOP_CONN_TERM:
2672 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002673 break;
Mike Christie7996a772006-04-06 21:13:41 -05002674 default:
Mike Christie322d7392008-01-31 13:36:52 -06002675 iscsi_conn_printk(KERN_ERR, conn,
2676 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002677 }
2678}
2679EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2680
2681int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2682 struct iscsi_cls_conn *cls_conn, int is_leading)
2683{
Mike Christie75613522008-05-21 15:53:59 -05002684 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002685 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002686
Mike Christie7996a772006-04-06 21:13:41 -05002687 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002688 if (is_leading)
2689 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002690 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002691
2692 /*
2693 * Unblock xmitworker(), Login Phase will pass through.
2694 */
2695 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2696 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2697 return 0;
2698}
2699EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2700
Mike Christie5700b1a2009-05-13 17:57:40 -05002701static int iscsi_switch_str_param(char **param, char *new_val_buf)
2702{
2703 char *new_val;
2704
2705 if (*param) {
2706 if (!strcmp(*param, new_val_buf))
2707 return 0;
2708 }
2709
2710 new_val = kstrdup(new_val_buf, GFP_NOIO);
2711 if (!new_val)
2712 return -ENOMEM;
2713
2714 kfree(*param);
2715 *param = new_val;
2716 return 0;
2717}
Mike Christiea54a52c2006-06-28 12:00:23 -05002718
2719int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2720 enum iscsi_param param, char *buf, int buflen)
2721{
2722 struct iscsi_conn *conn = cls_conn->dd_data;
2723 struct iscsi_session *session = conn->session;
2724 uint32_t value;
2725
2726 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002727 case ISCSI_PARAM_FAST_ABORT:
2728 sscanf(buf, "%d", &session->fast_abort);
2729 break;
Mike Christief6d51802007-12-13 12:43:30 -06002730 case ISCSI_PARAM_ABORT_TMO:
2731 sscanf(buf, "%d", &session->abort_timeout);
2732 break;
2733 case ISCSI_PARAM_LU_RESET_TMO:
2734 sscanf(buf, "%d", &session->lu_reset_timeout);
2735 break;
2736 case ISCSI_PARAM_PING_TMO:
2737 sscanf(buf, "%d", &conn->ping_timeout);
2738 break;
2739 case ISCSI_PARAM_RECV_TMO:
2740 sscanf(buf, "%d", &conn->recv_timeout);
2741 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002742 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2743 sscanf(buf, "%d", &conn->max_recv_dlength);
2744 break;
2745 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2746 sscanf(buf, "%d", &conn->max_xmit_dlength);
2747 break;
2748 case ISCSI_PARAM_HDRDGST_EN:
2749 sscanf(buf, "%d", &conn->hdrdgst_en);
2750 break;
2751 case ISCSI_PARAM_DATADGST_EN:
2752 sscanf(buf, "%d", &conn->datadgst_en);
2753 break;
2754 case ISCSI_PARAM_INITIAL_R2T_EN:
2755 sscanf(buf, "%d", &session->initial_r2t_en);
2756 break;
2757 case ISCSI_PARAM_MAX_R2T:
2758 sscanf(buf, "%d", &session->max_r2t);
2759 break;
2760 case ISCSI_PARAM_IMM_DATA_EN:
2761 sscanf(buf, "%d", &session->imm_data_en);
2762 break;
2763 case ISCSI_PARAM_FIRST_BURST:
2764 sscanf(buf, "%d", &session->first_burst);
2765 break;
2766 case ISCSI_PARAM_MAX_BURST:
2767 sscanf(buf, "%d", &session->max_burst);
2768 break;
2769 case ISCSI_PARAM_PDU_INORDER_EN:
2770 sscanf(buf, "%d", &session->pdu_inorder_en);
2771 break;
2772 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2773 sscanf(buf, "%d", &session->dataseq_inorder_en);
2774 break;
2775 case ISCSI_PARAM_ERL:
2776 sscanf(buf, "%d", &session->erl);
2777 break;
2778 case ISCSI_PARAM_IFMARKER_EN:
2779 sscanf(buf, "%d", &value);
2780 BUG_ON(value);
2781 break;
2782 case ISCSI_PARAM_OFMARKER_EN:
2783 sscanf(buf, "%d", &value);
2784 BUG_ON(value);
2785 break;
2786 case ISCSI_PARAM_EXP_STATSN:
2787 sscanf(buf, "%u", &conn->exp_statsn);
2788 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002789 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002790 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002791 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002792 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002793 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002794 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002795 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002796 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002797 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002798 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002799 case ISCSI_PARAM_TPGT:
2800 sscanf(buf, "%d", &session->tpgt);
2801 break;
2802 case ISCSI_PARAM_PERSISTENT_PORT:
2803 sscanf(buf, "%d", &conn->persistent_port);
2804 break;
2805 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002806 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002807 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002808 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002809 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002810 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002811 default:
2812 return -ENOSYS;
2813 }
2814
2815 return 0;
2816}
2817EXPORT_SYMBOL_GPL(iscsi_set_param);
2818
2819int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2820 enum iscsi_param param, char *buf)
2821{
Mike Christie75613522008-05-21 15:53:59 -05002822 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002823 int len;
2824
2825 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002826 case ISCSI_PARAM_FAST_ABORT:
2827 len = sprintf(buf, "%d\n", session->fast_abort);
2828 break;
Mike Christief6d51802007-12-13 12:43:30 -06002829 case ISCSI_PARAM_ABORT_TMO:
2830 len = sprintf(buf, "%d\n", session->abort_timeout);
2831 break;
2832 case ISCSI_PARAM_LU_RESET_TMO:
2833 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2834 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002835 case ISCSI_PARAM_INITIAL_R2T_EN:
2836 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2837 break;
2838 case ISCSI_PARAM_MAX_R2T:
2839 len = sprintf(buf, "%hu\n", session->max_r2t);
2840 break;
2841 case ISCSI_PARAM_IMM_DATA_EN:
2842 len = sprintf(buf, "%d\n", session->imm_data_en);
2843 break;
2844 case ISCSI_PARAM_FIRST_BURST:
2845 len = sprintf(buf, "%u\n", session->first_burst);
2846 break;
2847 case ISCSI_PARAM_MAX_BURST:
2848 len = sprintf(buf, "%u\n", session->max_burst);
2849 break;
2850 case ISCSI_PARAM_PDU_INORDER_EN:
2851 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2852 break;
2853 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2854 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2855 break;
2856 case ISCSI_PARAM_ERL:
2857 len = sprintf(buf, "%d\n", session->erl);
2858 break;
2859 case ISCSI_PARAM_TARGET_NAME:
2860 len = sprintf(buf, "%s\n", session->targetname);
2861 break;
2862 case ISCSI_PARAM_TPGT:
2863 len = sprintf(buf, "%d\n", session->tpgt);
2864 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002865 case ISCSI_PARAM_USERNAME:
2866 len = sprintf(buf, "%s\n", session->username);
2867 break;
2868 case ISCSI_PARAM_USERNAME_IN:
2869 len = sprintf(buf, "%s\n", session->username_in);
2870 break;
2871 case ISCSI_PARAM_PASSWORD:
2872 len = sprintf(buf, "%s\n", session->password);
2873 break;
2874 case ISCSI_PARAM_PASSWORD_IN:
2875 len = sprintf(buf, "%s\n", session->password_in);
2876 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002877 case ISCSI_PARAM_IFACE_NAME:
2878 len = sprintf(buf, "%s\n", session->ifacename);
2879 break;
2880 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002881 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05002882 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002883 default:
2884 return -ENOSYS;
2885 }
2886
2887 return len;
2888}
2889EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2890
2891int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2892 enum iscsi_param param, char *buf)
2893{
2894 struct iscsi_conn *conn = cls_conn->dd_data;
2895 int len;
2896
2897 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002898 case ISCSI_PARAM_PING_TMO:
2899 len = sprintf(buf, "%u\n", conn->ping_timeout);
2900 break;
2901 case ISCSI_PARAM_RECV_TMO:
2902 len = sprintf(buf, "%u\n", conn->recv_timeout);
2903 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002904 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2905 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2906 break;
2907 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2908 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2909 break;
2910 case ISCSI_PARAM_HDRDGST_EN:
2911 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2912 break;
2913 case ISCSI_PARAM_DATADGST_EN:
2914 len = sprintf(buf, "%d\n", conn->datadgst_en);
2915 break;
2916 case ISCSI_PARAM_IFMARKER_EN:
2917 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2918 break;
2919 case ISCSI_PARAM_OFMARKER_EN:
2920 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2921 break;
2922 case ISCSI_PARAM_EXP_STATSN:
2923 len = sprintf(buf, "%u\n", conn->exp_statsn);
2924 break;
2925 case ISCSI_PARAM_PERSISTENT_PORT:
2926 len = sprintf(buf, "%d\n", conn->persistent_port);
2927 break;
2928 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2929 len = sprintf(buf, "%s\n", conn->persistent_address);
2930 break;
2931 default:
2932 return -ENOSYS;
2933 }
2934
2935 return len;
2936}
2937EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2938
Mike Christie0801c242007-05-30 12:57:12 -05002939int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2940 char *buf)
2941{
Mike Christie75613522008-05-21 15:53:59 -05002942 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002943 int len;
2944
2945 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002946 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002947 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002948 break;
Mike Christie0801c242007-05-30 12:57:12 -05002949 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002950 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002951 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002952 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002953 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002954 break;
Mike Christie75613522008-05-21 15:53:59 -05002955 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002956 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002957 break;
Mike Christie0801c242007-05-30 12:57:12 -05002958 default:
2959 return -ENOSYS;
2960 }
2961
2962 return len;
2963}
2964EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2965
2966int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2967 char *buf, int buflen)
2968{
Mike Christie75613522008-05-21 15:53:59 -05002969 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002970
2971 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002972 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002973 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002974 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002975 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05002976 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002977 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002978 default:
2979 return -ENOSYS;
2980 }
2981
2982 return 0;
2983}
2984EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2985
Mike Christie7996a772006-04-06 21:13:41 -05002986MODULE_AUTHOR("Mike Christie");
2987MODULE_DESCRIPTION("iSCSI library functions");
2988MODULE_LICENSE("GPL");