blob: 59908aead531e6257ad63f1767cf736c82585956 [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Mike Christie1b2c7af2009-03-05 14:45:58 -060041static int iscsi_dbg_lib;
42module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
48 do { \
49 if (iscsi_dbg_lib) \
50 iscsi_conn_printk(KERN_INFO, _conn, \
51 "%s " dbg_fmt, \
52 __func__, ##arg); \
53 } while (0);
54
55#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (iscsi_dbg_lib) \
58 iscsi_session_printk(KERN_INFO, _session, \
59 "%s " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
64#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050065
Mike Christie77a23c22007-05-30 12:57:18 -050066static int iscsi_sna_lt(u32 n1, u32 n2)
67{
68 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
69 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
70}
71
72/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
73static int iscsi_sna_lte(u32 n1, u32 n2)
74{
75 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
76 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
77}
78
Mike Christie32ae7632009-03-05 14:46:03 -060079inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
80{
81 struct Scsi_Host *shost = conn->session->host;
82 struct iscsi_host *ihost = shost_priv(shost);
83
Mike Christie1336aed2009-05-13 17:57:48 -050084 if (ihost->workq)
85 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -060086}
87EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
88
Mike Christie77a23c22007-05-30 12:57:18 -050089void
90iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050091{
92 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
93 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
94
Mike Christie77a23c22007-05-30 12:57:18 -050095 /*
96 * standard specifies this check for when to update expected and
97 * max sequence numbers
98 */
99 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
100 return;
101
102 if (exp_cmdsn != session->exp_cmdsn &&
103 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500104 session->exp_cmdsn = exp_cmdsn;
105
Mike Christie77a23c22007-05-30 12:57:18 -0500106 if (max_cmdsn != session->max_cmdsn &&
107 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
108 session->max_cmdsn = max_cmdsn;
109 /*
110 * if the window closed with IO queued, then kick the
111 * xmit thread
112 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500113 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500114 !list_empty(&session->leadconn->mgmtqueue))
115 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500116 }
Mike Christie7996a772006-04-06 21:13:41 -0500117}
Mike Christie77a23c22007-05-30 12:57:18 -0500118EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500119
Mike Christie577577d2008-12-02 00:32:05 -0600120/**
121 * iscsi_prep_data_out_pdu - initialize Data-Out
122 * @task: scsi command task
123 * @r2t: R2T info
124 * @hdr: iscsi data in pdu
125 *
126 * Notes:
127 * Initialize Data-Out within this R2T sequence and finds
128 * proper data_offset within this SCSI command.
129 *
130 * This function is called with connection lock taken.
131 **/
132void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
133 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500134{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500135 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600136 unsigned int left = r2t->data_length - r2t->sent;
137
138 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500139
140 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600141 hdr->ttt = r2t->ttt;
142 hdr->datasn = cpu_to_be32(r2t->datasn);
143 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500144 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600145 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
146 hdr->itt = task->hdr_itt;
147 hdr->exp_statsn = r2t->exp_statsn;
148 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
149 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500150 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600151 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500152 hdr->flags = 0;
153 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600154 hton24(hdr->dlength, left);
155 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500156 hdr->flags = ISCSI_FLAG_CMD_FINAL;
157 }
Mike Christie577577d2008-12-02 00:32:05 -0600158 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500159}
Mike Christie577577d2008-12-02 00:32:05 -0600160EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500161
Mike Christie9c19a7d2008-05-21 15:54:09 -0500162static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600163{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500164 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600165
Mike Christie9c19a7d2008-05-21 15:54:09 -0500166 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600167 WARN_ON(1);
168 return -EINVAL;
169 }
170
171 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500172 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600173 return 0;
174}
175
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500176/*
177 * make an extended cdb AHS
178 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500179static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500180{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500181 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500182 unsigned rlen, pad_len;
183 unsigned short ahslength;
184 struct iscsi_ecdb_ahdr *ecdb_ahdr;
185 int rc;
186
Mike Christie9c19a7d2008-05-21 15:54:09 -0500187 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500188 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
189
190 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
191 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
192
193 pad_len = iscsi_padding(rlen);
194
Mike Christie9c19a7d2008-05-21 15:54:09 -0500195 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500196 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
197 if (rc)
198 return rc;
199
200 if (pad_len)
201 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
202
203 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
204 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
205 ecdb_ahdr->reserved = 0;
206 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
207
Mike Christie1b2c7af2009-03-05 14:45:58 -0600208 ISCSI_DBG_SESSION(task->conn->session,
209 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
210 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
211 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
212 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500213 return 0;
214}
215
Mike Christie9c19a7d2008-05-21 15:54:09 -0500216static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500217{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500218 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500219 struct iscsi_rlength_ahdr *rlen_ahdr;
220 int rc;
221
Mike Christie9c19a7d2008-05-21 15:54:09 -0500222 rlen_ahdr = iscsi_next_hdr(task);
223 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500224 if (rc)
225 return rc;
226
227 rlen_ahdr->ahslength =
228 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
229 sizeof(rlen_ahdr->reserved));
230 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
231 rlen_ahdr->reserved = 0;
232 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
233
Mike Christie1b2c7af2009-03-05 14:45:58 -0600234 ISCSI_DBG_SESSION(task->conn->session,
235 "bidi-in rlen_ahdr->read_length(%d) "
236 "rlen_ahdr->ahslength(%d)\n",
237 be32_to_cpu(rlen_ahdr->read_length),
238 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500239 return 0;
240}
241
Mike Christie7996a772006-04-06 21:13:41 -0500242/**
243 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500245 *
246 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
247 * fields like dlength or final based on how much data it sends
248 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500249static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500250{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500251 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500252 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500253 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600254 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500255 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600256 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600257 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500258
Mike Christie184b57c2009-05-13 17:57:39 -0500259 if (conn->session->tt->alloc_pdu) {
260 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
261 if (rc)
262 return rc;
263 }
Mike Christie577577d2008-12-02 00:32:05 -0600264 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600265 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600266 memset(hdr, 0, sizeof(*hdr));
267
Mike Christie2ff79d52008-12-02 00:32:14 -0600268 if (session->tt->parse_pdu_itt)
269 hdr->itt = task->hdr_itt = itt;
270 else
271 hdr->itt = task->hdr_itt = build_itt(task->itt,
272 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500273 task->hdr_len = 0;
274 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600275 if (rc)
276 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600277 hdr->opcode = ISCSI_OP_SCSI_CMD;
278 hdr->flags = ISCSI_ATTR_SIMPLE;
279 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600280 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600281 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600282 session->cmdsn++;
283 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500284 cmd_len = sc->cmd_len;
285 if (cmd_len < ISCSI_CDB_SIZE)
286 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
287 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500288 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500289 if (rc)
290 return rc;
291 cmd_len = ISCSI_CDB_SIZE;
292 }
293 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500294
Mike Christie9c19a7d2008-05-21 15:54:09 -0500295 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500296 if (scsi_bidi_cmnd(sc)) {
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500298 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500299 if (rc)
300 return rc;
301 }
Mike Christie7996a772006-04-06 21:13:41 -0500302 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500303 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600304 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
305
Boaz Harroshc07d4442008-04-18 10:11:52 -0500306 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500307 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
308 /*
309 * Write counters:
310 *
311 * imm_count bytes to be sent right after
312 * SCSI PDU Header
313 *
314 * unsol_count bytes(as Data-Out) to be sent
315 * without R2T ack right after
316 * immediate data
317 *
Mike Christie577577d2008-12-02 00:32:05 -0600318 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500319 *
320 * pad_count bytes to be sent as zero-padding
321 */
Mike Christie577577d2008-12-02 00:32:05 -0600322 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500323
324 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500325 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500326 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500327 conn->max_xmit_dlength);
328 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500329 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500330 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500331 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500332 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600333 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500334
Mike Christieffd04362006-08-31 18:09:24 -0400335 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600336 r2t->data_length = min(session->first_burst, out_len) -
337 task->imm_count;
338 r2t->data_offset = task->imm_count;
339 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
340 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400341 }
342
Mike Christie577577d2008-12-02 00:32:05 -0600343 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500344 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600345 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500346 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500347 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
348 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500349 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500350
351 if (sc->sc_data_direction == DMA_FROM_DEVICE)
352 hdr->flags |= ISCSI_FLAG_CMD_READ;
353 }
354
Boaz Harrosh004d6532007-12-13 12:43:23 -0600355 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500356 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600357
358 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
359 hdrlength /= ISCSI_PAD_LEN;
360
361 WARN_ON(hdrlength >= 256);
362 hdr->hlength = hdrlength & 0xFF;
363
Mike Christie577577d2008-12-02 00:32:05 -0600364 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500365 return -EIO;
366
Mike Christie9c19a7d2008-05-21 15:54:09 -0500367 task->state = ISCSI_TASK_RUNNING;
Mike Christie77a23c22007-05-30 12:57:18 -0500368
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600370 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
371 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
372 scsi_bidi_cmnd(sc) ? "bidirectional" :
373 sc->sc_data_direction == DMA_TO_DEVICE ?
374 "write" : "read", conn->id, sc, sc->cmnd[0],
375 task->itt, scsi_bufflen(sc),
376 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
377 session->cmdsn,
378 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600379 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500380}
Mike Christie7996a772006-04-06 21:13:41 -0500381
382/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500383 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500385 *
386 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500387 * This function returns the scsi command to scsi-ml or cleans
388 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500389 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500390static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500391{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500392 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600393 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500394 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500395
Mike Christie4421c9e2009-05-13 17:57:50 -0500396 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
397 task->itt, task->state, task->sc);
398
Mike Christie577577d2008-12-02 00:32:05 -0600399 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500400 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500402 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500403 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500404 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500405 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500406 return;
407
Mike Christie9c19a7d2008-05-21 15:54:09 -0500408 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500409
Mike Christie3e5c28a2008-05-21 15:54:06 -0500410 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500411 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500412 /* SCSI eh reuses commands to verify us */
413 sc->SCp.ptr = NULL;
414 /*
415 * queue command may call this to free the task, but
416 * not have setup the sc callback
417 */
418 if (sc->scsi_done)
419 sc->scsi_done(sc);
420 }
Mike Christie7996a772006-04-06 21:13:41 -0500421}
422
Mike Christie913e5bf2008-05-21 15:54:18 -0500423void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400424{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500425 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400426}
Mike Christie913e5bf2008-05-21 15:54:18 -0500427EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400428
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400430{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500432 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400433}
434
Mike Christie9c19a7d2008-05-21 15:54:09 -0500435void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500436{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500437 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500438
439 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500440 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500441 spin_unlock_bh(&session->lock);
442}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500443EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500444
Mike Christie3bbaaad2009-05-13 17:57:46 -0500445/**
446 * iscsi_complete_task - finish a task
447 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500448 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500449 *
450 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600451 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500452static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600453{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500454 struct iscsi_conn *conn = task->conn;
455
Mike Christie4421c9e2009-05-13 17:57:50 -0500456 ISCSI_DBG_SESSION(conn->session,
457 "complete task itt 0x%x state %d sc %p\n",
458 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500459 if (task->state == ISCSI_TASK_COMPLETED ||
460 task->state == ISCSI_TASK_ABRT_TMF ||
461 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500462 return;
463 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500464 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500465
466 if (!list_empty(&task->running))
467 list_del_init(&task->running);
468
469 if (conn->task == task)
470 conn->task = NULL;
471
472 if (conn->ping_task == task)
473 conn->ping_task = NULL;
474
475 /* release get from queueing */
476 __iscsi_put_task(task);
477}
478
479/*
480 * session lock must be held and if not called for a task that is
481 * still pending or from the xmit thread, then xmit thread must
482 * be suspended.
483 */
484static void fail_scsi_task(struct iscsi_task *task, int err)
485{
486 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600487 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500488 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600489
Mike Christie3bbaaad2009-05-13 17:57:46 -0500490 /*
491 * if a command completes and we get a successful tmf response
492 * we will hit this because the scsi eh abort code does not take
493 * a ref to the task.
494 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500495 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600496 if (!sc)
497 return;
498
Mike Christieb3cd5052009-05-13 17:57:49 -0500499 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600500 /*
501 * cmd never made it to the xmit thread, so we should not count
502 * the cmd in the sequencing
503 */
504 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500505 /* it was never sent so just complete like normal */
506 state = ISCSI_TASK_COMPLETED;
507 } else if (err == DID_TRANSPORT_DISRUPTED)
508 state = ISCSI_TASK_ABRT_SESS_RECOV;
509 else
510 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600511
Mike Christieb3cd5052009-05-13 17:57:49 -0500512 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500513 if (!scsi_bidi_cmnd(sc))
514 scsi_set_resid(sc, scsi_bufflen(sc));
515 else {
516 scsi_out(sc)->resid = scsi_out(sc)->length;
517 scsi_in(sc)->resid = scsi_in(sc)->length;
518 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500519
Mike Christieb3cd5052009-05-13 17:57:49 -0500520 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600521}
522
Mike Christie3e5c28a2008-05-21 15:54:06 -0500523static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500525{
526 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600527 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500528 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
529
530 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
531 return -ENOTCONN;
532
533 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
534 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
535 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
536 /*
537 * pre-format CmdSN for outgoing PDU.
538 */
539 nop->cmdsn = cpu_to_be32(session->cmdsn);
540 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500541 /*
542 * TODO: We always use immediate, so we never hit this.
543 * If we start to send tmfs or nops as non-immediate then
544 * we should start checking the cmdsn numbers for mgmt tasks.
545 */
546 if (conn->c_stage == ISCSI_CONN_STARTED &&
547 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
548 session->queued_cmdsn++;
549 session->cmdsn++;
550 }
551 }
552
Mike Christieae15f802008-12-02 00:32:15 -0600553 if (session->tt->init_task && session->tt->init_task(task))
554 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500555
556 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
557 session->state = ISCSI_STATE_LOGGING_OUT;
558
Mike Christie577577d2008-12-02 00:32:05 -0600559 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600560 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
561 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
562 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500563 return 0;
564}
565
Mike Christie9c19a7d2008-05-21 15:54:09 -0500566static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600567__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
568 char *data, uint32_t data_size)
569{
570 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500571 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500572 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600573 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600574
575 if (session->state == ISCSI_STATE_TERMINATE)
576 return NULL;
577
578 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
579 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
580 /*
581 * Login and Text are sent serially, in
582 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500583 * Same task can be used. Same ITT must be used.
584 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600585 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500586 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600587 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500588 if (session->state != ISCSI_STATE_LOGGED_IN)
589 return NULL;
590
Mike Christief6d51802007-12-13 12:43:30 -0600591 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
592 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
593
Mike Christie3e5c28a2008-05-21 15:54:06 -0500594 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500595 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600596 return NULL;
597 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500598 /*
599 * released in complete pdu for task we expect a response for, and
600 * released by the lld when it has transmitted the task for
601 * pdus we do not expect a response for.
602 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500603 atomic_set(&task->refcount, 1);
604 task->conn = conn;
605 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500606 INIT_LIST_HEAD(&task->running);
607 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600608
609 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500610 memcpy(task->data, data, data_size);
611 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600612 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500613 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600614
Mike Christie184b57c2009-05-13 17:57:39 -0500615 if (conn->session->tt->alloc_pdu) {
616 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
617 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
618 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500619 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500620 }
Mike Christie577577d2008-12-02 00:32:05 -0600621 }
Mike Christie184b57c2009-05-13 17:57:39 -0500622
Mike Christie262ef632008-12-02 00:32:13 -0600623 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600624 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500625 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600626
627 if (hdr->itt != RESERVED_ITT) {
628 if (session->tt->parse_pdu_itt)
629 task->hdr->itt = itt;
630 else
631 task->hdr->itt = build_itt(task->itt,
632 task->conn->session->age);
633 }
634
Mike Christie1336aed2009-05-13 17:57:48 -0500635 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600636 if (iscsi_prep_mgmt_task(conn, task))
637 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500638
Mike Christie9c19a7d2008-05-21 15:54:09 -0500639 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600640 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500641 } else {
642 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600643 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500644 }
Mike Christie052d0142008-05-21 15:54:05 -0500645
Mike Christie9c19a7d2008-05-21 15:54:09 -0500646 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600647
648free_task:
649 __iscsi_put_task(task);
650 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600651}
652
653int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
654 char *data, uint32_t data_size)
655{
656 struct iscsi_conn *conn = cls_conn->dd_data;
657 struct iscsi_session *session = conn->session;
658 int err = 0;
659
660 spin_lock_bh(&session->lock);
661 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
662 err = -EPERM;
663 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600664 return err;
665}
666EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
667
Mike Christie7996a772006-04-06 21:13:41 -0500668/**
669 * iscsi_cmd_rsp - SCSI Command Response processing
670 * @conn: iscsi connection
671 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500672 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500673 * @data: cmd data buffer
674 * @datalen: len of buffer
675 *
676 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500677 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500678 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500679static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500680 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500681 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500682{
Mike Christie7996a772006-04-06 21:13:41 -0500683 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
684 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500685 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500686
Mike Christie77a23c22007-05-30 12:57:18 -0500687 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500688 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
689
690 sc->result = (DID_OK << 16) | rhdr->cmd_status;
691
692 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
693 sc->result = DID_ERROR << 16;
694 goto out;
695 }
696
697 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600698 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500699
700 if (datalen < 2) {
701invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600702 iscsi_conn_printk(KERN_ERR, conn,
703 "Got CHECK_CONDITION but invalid data "
704 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500705 sc->result = DID_BAD_TARGET << 16;
706 goto out;
707 }
708
Harvey Harrison8f3339912008-05-21 15:54:20 -0500709 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500710 if (datalen < senselen)
711 goto invalid_datalen;
712
713 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600714 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600715 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
716 min_t(uint16_t, senselen,
717 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500718 }
719
Boaz Harroshc07d4442008-04-18 10:11:52 -0500720 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
721 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
722 int res_count = be32_to_cpu(rhdr->bi_residual_count);
723
724 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
725 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
726 res_count <= scsi_in(sc)->length))
727 scsi_in(sc)->resid = res_count;
728 else
729 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
730 }
731
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600732 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
733 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500734 int res_count = be32_to_cpu(rhdr->residual_count);
735
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600736 if (res_count > 0 &&
737 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
738 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500739 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900740 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500741 else
742 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500743 }
Mike Christie7996a772006-04-06 21:13:41 -0500744out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500745 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600746 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500747 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500748 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500749}
750
Mike Christie1d9edf02008-09-24 11:46:09 -0500751/**
752 * iscsi_data_in_rsp - SCSI Data-In Response processing
753 * @conn: iscsi connection
754 * @hdr: iscsi pdu
755 * @task: scsi command task
756 **/
757static void
758iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
759 struct iscsi_task *task)
760{
761 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
762 struct scsi_cmnd *sc = task->sc;
763
764 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
765 return;
766
Mike Christieedbc9aa052009-05-13 17:57:42 -0500767 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500768 sc->result = (DID_OK << 16) | rhdr->cmd_status;
769 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
770 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
771 ISCSI_FLAG_DATA_OVERFLOW)) {
772 int res_count = be32_to_cpu(rhdr->residual_count);
773
774 if (res_count > 0 &&
775 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
776 res_count <= scsi_in(sc)->length))
777 scsi_in(sc)->resid = res_count;
778 else
779 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
780 }
781
Mike Christie3bbaaad2009-05-13 17:57:46 -0500782 ISCSI_DBG_SESSION(conn->session, "data in with status done "
783 "[sc %p res %d itt 0x%x]\n",
784 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500785 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500786 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500787}
788
Mike Christie7ea8b822006-07-24 15:47:22 -0500789static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
790{
791 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
792
793 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
794 conn->tmfrsp_pdus_cnt++;
795
Mike Christie843c0a82007-12-13 12:43:20 -0600796 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500797 return;
798
799 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600800 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500801 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600802 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500803 else
Mike Christie843c0a82007-12-13 12:43:20 -0600804 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500805 wake_up(&conn->ehwait);
806}
807
Mike Christief6d51802007-12-13 12:43:30 -0600808static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
809{
810 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500811 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600812
Mike Christie9c19a7d2008-05-21 15:54:09 -0500813 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600814 return;
815
816 memset(&hdr, 0, sizeof(struct iscsi_nopout));
817 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
818 hdr.flags = ISCSI_FLAG_CMD_FINAL;
819
820 if (rhdr) {
821 memcpy(hdr.lun, rhdr->lun, 8);
822 hdr.ttt = rhdr->ttt;
823 hdr.itt = RESERVED_ITT;
824 } else
825 hdr.ttt = RESERVED_ITT;
826
Mike Christie9c19a7d2008-05-21 15:54:09 -0500827 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
828 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600829 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600830 else if (!rhdr) {
831 /* only track our nops */
832 conn->ping_task = task;
833 conn->last_ping = jiffies;
834 }
Mike Christief6d51802007-12-13 12:43:30 -0600835}
836
Mike Christie62f38302006-08-31 18:09:27 -0400837static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
838 char *data, int datalen)
839{
840 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
841 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400842
843 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
844
845 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
846 if (ntoh24(reject->dlength) > datalen)
847 return ISCSI_ERR_PROTO;
848
849 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
850 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600851 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600852 "pdu (op 0x%x) rejected "
853 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600854 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400855 }
856 }
857 return 0;
858}
859
Mike Christie7996a772006-04-06 21:13:41 -0500860/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500861 * iscsi_itt_to_task - look up task by itt
862 * @conn: iscsi connection
863 * @itt: itt
864 *
865 * This should be used for mgmt tasks like login and nops, or if
866 * the LDD's itt space does not include the session age.
867 *
868 * The session lock must be held.
869 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500870struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500871{
872 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600873 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500874
875 if (itt == RESERVED_ITT)
876 return NULL;
877
Mike Christie262ef632008-12-02 00:32:13 -0600878 if (session->tt->parse_pdu_itt)
879 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
880 else
881 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500882 if (i >= session->cmds_max)
883 return NULL;
884
885 return session->cmds[i];
886}
Mike Christie8f9256c2009-05-13 17:57:41 -0500887EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500888
889/**
Mike Christie7996a772006-04-06 21:13:41 -0500890 * __iscsi_complete_pdu - complete pdu
891 * @conn: iscsi conn
892 * @hdr: iscsi header
893 * @data: data buffer
894 * @datalen: len of data buffer
895 *
896 * Completes pdu processing by freeing any resources allocated at
897 * queuecommand or send generic. session lock must be held and verify
898 * itt must have been called.
899 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500900int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
901 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500902{
903 struct iscsi_session *session = conn->session;
904 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500905 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500906 uint32_t itt;
907
Mike Christief6d51802007-12-13 12:43:30 -0600908 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500909 rc = iscsi_verify_itt(conn, hdr->itt);
910 if (rc)
911 return rc;
912
Al Virob4377352007-02-09 16:39:40 +0000913 if (hdr->itt != RESERVED_ITT)
914 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500915 else
Al Virob4377352007-02-09 16:39:40 +0000916 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500917
Mike Christie1b2c7af2009-03-05 14:45:58 -0600918 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
919 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500920
Mike Christie3e5c28a2008-05-21 15:54:06 -0500921 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500922 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400923
Mike Christie7996a772006-04-06 21:13:41 -0500924 switch(opcode) {
925 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500926 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500927 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500928 break;
929 }
930
Al Virob4377352007-02-09 16:39:40 +0000931 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500932 break;
933
Mike Christief6d51802007-12-13 12:43:30 -0600934 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500935 break;
936 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400937 rc = iscsi_handle_reject(conn, hdr, data, datalen);
938 break;
Mike Christie7996a772006-04-06 21:13:41 -0500939 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500940 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400941 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
942 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500943 break;
944 default:
945 rc = ISCSI_ERR_BAD_OPCODE;
946 break;
947 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500948 goto out;
949 }
Mike Christie7996a772006-04-06 21:13:41 -0500950
Mike Christie3e5c28a2008-05-21 15:54:06 -0500951 switch(opcode) {
952 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500953 case ISCSI_OP_SCSI_DATA_IN:
954 task = iscsi_itt_to_ctask(conn, hdr->itt);
955 if (!task)
956 return ISCSI_ERR_BAD_ITT;
957 break;
958 case ISCSI_OP_R2T:
959 /*
960 * LLD handles R2Ts if they need to.
961 */
962 return 0;
963 case ISCSI_OP_LOGOUT_RSP:
964 case ISCSI_OP_LOGIN_RSP:
965 case ISCSI_OP_TEXT_RSP:
966 case ISCSI_OP_SCSI_TMFUNC_RSP:
967 case ISCSI_OP_NOOP_IN:
968 task = iscsi_itt_to_task(conn, hdr->itt);
969 if (!task)
970 return ISCSI_ERR_BAD_ITT;
971 break;
972 default:
973 return ISCSI_ERR_BAD_OPCODE;
974 }
975
976 switch(opcode) {
977 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500978 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500979 break;
980 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500981 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500982 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500983 case ISCSI_OP_LOGOUT_RSP:
984 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
985 if (datalen) {
986 rc = ISCSI_ERR_PROTO;
987 break;
988 }
989 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
990 goto recv_pdu;
991 case ISCSI_OP_LOGIN_RSP:
992 case ISCSI_OP_TEXT_RSP:
993 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
994 /*
995 * login related PDU's exp_statsn is handled in
996 * userspace
997 */
998 goto recv_pdu;
999 case ISCSI_OP_SCSI_TMFUNC_RSP:
1000 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1001 if (datalen) {
1002 rc = ISCSI_ERR_PROTO;
1003 break;
1004 }
1005
1006 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001007 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001008 break;
1009 case ISCSI_OP_NOOP_IN:
1010 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1011 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1012 rc = ISCSI_ERR_PROTO;
1013 break;
1014 }
1015 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1016
Mike Christie9c19a7d2008-05-21 15:54:09 -05001017 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -05001018 /*
1019 * If this is not in response to one of our
1020 * nops then it must be from userspace.
1021 */
1022 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001023
1024 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
Mike Christieb3cd5052009-05-13 17:57:49 -05001025 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001026 break;
1027 default:
1028 rc = ISCSI_ERR_BAD_OPCODE;
1029 break;
1030 }
1031
1032out:
1033 return rc;
1034recv_pdu:
1035 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1036 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001037 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001038 return rc;
1039}
Mike Christie913e5bf2008-05-21 15:54:18 -05001040EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001041
1042int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1043 char *data, int datalen)
1044{
1045 int rc;
1046
1047 spin_lock(&conn->session->lock);
1048 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1049 spin_unlock(&conn->session->lock);
1050 return rc;
1051}
1052EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1053
Mike Christie0af967f2008-05-21 15:54:04 -05001054int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001055{
1056 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001057 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001058
Mike Christie0af967f2008-05-21 15:54:04 -05001059 if (itt == RESERVED_ITT)
1060 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001061
Mike Christie262ef632008-12-02 00:32:13 -06001062 if (session->tt->parse_pdu_itt)
1063 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1064 else {
1065 i = get_itt(itt);
1066 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1067 }
1068
1069 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001070 iscsi_conn_printk(KERN_ERR, conn,
1071 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001072 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001073 return ISCSI_ERR_BAD_ITT;
1074 }
Mike Christie7996a772006-04-06 21:13:41 -05001075
Mike Christie3e5c28a2008-05-21 15:54:06 -05001076 if (i >= session->cmds_max) {
1077 iscsi_conn_printk(KERN_ERR, conn,
1078 "received invalid itt index %u (max cmds "
1079 "%u.\n", i, session->cmds_max);
1080 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001081 }
Mike Christie7996a772006-04-06 21:13:41 -05001082 return 0;
1083}
1084EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1085
Mike Christie913e5bf2008-05-21 15:54:18 -05001086/**
1087 * iscsi_itt_to_ctask - look up ctask by itt
1088 * @conn: iscsi connection
1089 * @itt: itt
1090 *
1091 * This should be used for cmd tasks.
1092 *
1093 * The session lock must be held.
1094 */
1095struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001096{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001097 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001098
1099 if (iscsi_verify_itt(conn, itt))
1100 return NULL;
1101
Mike Christie913e5bf2008-05-21 15:54:18 -05001102 task = iscsi_itt_to_task(conn, itt);
1103 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001104 return NULL;
1105
Mike Christie913e5bf2008-05-21 15:54:18 -05001106 if (task->sc->SCp.phase != conn->session->age) {
1107 iscsi_session_printk(KERN_ERR, conn->session,
1108 "task's session age %d, expected %d\n",
1109 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001110 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001111 }
Mike Christie0af967f2008-05-21 15:54:04 -05001112
Mike Christie9c19a7d2008-05-21 15:54:09 -05001113 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001114}
1115EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1116
Mike Christie40a06e72009-03-05 14:46:05 -06001117void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001118 enum iscsi_err err)
1119{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001120 struct iscsi_conn *conn;
1121 struct device *dev;
1122 unsigned long flags;
1123
1124 spin_lock_irqsave(&session->lock, flags);
1125 conn = session->leadconn;
1126 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1127 spin_unlock_irqrestore(&session->lock, flags);
1128 return;
1129 }
1130
1131 dev = get_device(&conn->cls_conn->dev);
1132 spin_unlock_irqrestore(&session->lock, flags);
1133 if (!dev)
1134 return;
1135 /*
1136 * if the host is being removed bypass the connection
1137 * recovery initialization because we are going to kill
1138 * the session.
1139 */
1140 if (err == ISCSI_ERR_INVALID_HOST)
1141 iscsi_conn_error_event(conn->cls_conn, err);
1142 else
1143 iscsi_conn_failure(conn, err);
1144 put_device(dev);
1145}
1146EXPORT_SYMBOL_GPL(iscsi_session_failure);
1147
Mike Christie7996a772006-04-06 21:13:41 -05001148void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1149{
1150 struct iscsi_session *session = conn->session;
1151 unsigned long flags;
1152
1153 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001154 if (session->state == ISCSI_STATE_FAILED) {
1155 spin_unlock_irqrestore(&session->lock, flags);
1156 return;
1157 }
1158
Mike Christie67a61112006-05-30 00:37:20 -05001159 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001160 session->state = ISCSI_STATE_FAILED;
1161 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001162
Mike Christie7996a772006-04-06 21:13:41 -05001163 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1164 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001165 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001166}
1167EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1168
Mike Christie77a23c22007-05-30 12:57:18 -05001169static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1170{
1171 struct iscsi_session *session = conn->session;
1172
1173 /*
1174 * Check for iSCSI window and take care of CmdSN wrap-around
1175 */
Mike Christiee0726402007-07-26 12:46:48 -05001176 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001177 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1178 "%u MaxCmdSN %u CmdSN %u/%u\n",
1179 session->exp_cmdsn, session->max_cmdsn,
1180 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001181 return -ENOSPC;
1182 }
1183 return 0;
1184}
1185
Mike Christie9c19a7d2008-05-21 15:54:09 -05001186static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001187{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001188 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001189 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001190
Mike Christie9c19a7d2008-05-21 15:54:09 -05001191 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001192 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001193 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001194 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001195 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001196 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001197 /* done with this task */
1198 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001199 return rc;
1200}
1201
Mike Christie7996a772006-04-06 21:13:41 -05001202/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001203 * iscsi_requeue_task - requeue task to run from session workqueue
1204 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001205 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001206 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001207 * this. The session lock must be held. This should only be called
1208 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001209 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001210void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001211{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001212 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001213
Mike Christie3bbaaad2009-05-13 17:57:46 -05001214 /*
1215 * this may be on the requeue list already if the xmit_task callout
1216 * is handling the r2ts while we are adding new ones
1217 */
1218 if (list_empty(&task->running))
1219 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001220 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001221}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001222EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001223
1224/**
Mike Christie7996a772006-04-06 21:13:41 -05001225 * iscsi_data_xmit - xmit any command into the scheduled connection
1226 * @conn: iscsi connection
1227 *
1228 * Notes:
1229 * The function can return -EAGAIN in which case the caller must
1230 * re-schedule it again later or recover. '0' return code means
1231 * successful xmit.
1232 **/
1233static int iscsi_data_xmit(struct iscsi_conn *conn)
1234{
Mike Christie3219e522006-05-30 00:37:28 -05001235 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001236
Mike Christie77a23c22007-05-30 12:57:18 -05001237 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001238 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001239 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001240 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001241 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001242 }
Mike Christie7996a772006-04-06 21:13:41 -05001243
Mike Christie9c19a7d2008-05-21 15:54:09 -05001244 if (conn->task) {
1245 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001246 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001247 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001248 }
1249
Mike Christie77a23c22007-05-30 12:57:18 -05001250 /*
1251 * process mgmt pdus like nops before commands since we should
1252 * only have one nop-out as a ping from us and targets should not
1253 * overflow us with nop-ins
1254 */
1255check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001256 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001257 conn->task = list_entry(conn->mgmtqueue.next,
1258 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001259 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001260 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1261 __iscsi_put_task(conn->task);
1262 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001263 continue;
1264 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001265 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001266 if (rc)
1267 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001268 }
1269
Mike Christie843c0a82007-12-13 12:43:20 -06001270 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001271 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001272 if (conn->tmf_state == TMF_QUEUED)
1273 break;
1274
Mike Christie3bbaaad2009-05-13 17:57:46 -05001275 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001276 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001277 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001278 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001279 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001280 continue;
1281 }
Mike Christie577577d2008-12-02 00:32:05 -06001282 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1283 if (rc) {
1284 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001285 list_add_tail(&conn->task->running,
1286 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001287 conn->task = NULL;
1288 goto again;
1289 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001290 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001291 continue;
1292 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001293 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001294 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001295 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001296 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001297 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001298 * we need to check the mgmt queue for nops that need to
1299 * be sent to aviod starvation
1300 */
Mike Christie843c0a82007-12-13 12:43:20 -06001301 if (!list_empty(&conn->mgmtqueue))
1302 goto check_mgmt;
1303 }
1304
1305 while (!list_empty(&conn->requeue)) {
1306 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1307 break;
1308
Mike Christieb3a7ea82007-12-13 12:43:26 -06001309 /*
1310 * we always do fastlogout - conn stop code will clean up.
1311 */
1312 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1313 break;
1314
Mike Christie9c19a7d2008-05-21 15:54:09 -05001315 conn->task = list_entry(conn->requeue.next,
1316 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001317 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001318 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001319 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001320 if (rc)
1321 goto again;
1322 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001323 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001324 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001325 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001326 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001327
1328again:
1329 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001330 rc = -ENODATA;
1331 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001332 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001333}
1334
David Howellsc4028952006-11-22 14:57:56 +00001335static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001336{
David Howellsc4028952006-11-22 14:57:56 +00001337 struct iscsi_conn *conn =
1338 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001339 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001340 /*
1341 * serialize Xmit worker on a per-connection basis.
1342 */
Mike Christie3219e522006-05-30 00:37:28 -05001343 do {
1344 rc = iscsi_data_xmit(conn);
1345 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001346}
1347
Mike Christie577577d2008-12-02 00:32:05 -06001348static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1349 struct scsi_cmnd *sc)
1350{
1351 struct iscsi_task *task;
1352
1353 if (!__kfifo_get(conn->session->cmdpool.queue,
1354 (void *) &task, sizeof(void *)))
1355 return NULL;
1356
1357 sc->SCp.phase = conn->session->age;
1358 sc->SCp.ptr = (char *) task;
1359
1360 atomic_set(&task->refcount, 1);
1361 task->state = ISCSI_TASK_PENDING;
1362 task->conn = conn;
1363 task->sc = sc;
1364 INIT_LIST_HEAD(&task->running);
1365 return task;
1366}
1367
Mike Christie7996a772006-04-06 21:13:41 -05001368enum {
1369 FAILURE_BAD_HOST = 1,
1370 FAILURE_SESSION_FAILED,
1371 FAILURE_SESSION_FREED,
1372 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001373 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001374 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001375 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001376 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001377 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001378 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001379};
1380
1381int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1382{
Mike Christie75613522008-05-21 15:53:59 -05001383 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001384 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001385 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001386 int reason = 0;
1387 struct iscsi_session *session;
1388 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001389 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001390
1391 sc->scsi_done = done;
1392 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001393 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001394
1395 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001396 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001397 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001398
Mike Christie75613522008-05-21 15:53:59 -05001399 cls_session = starget_to_session(scsi_target(sc->device));
1400 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001401 spin_lock(&session->lock);
1402
Mike Christie75613522008-05-21 15:53:59 -05001403 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001404 if (reason) {
1405 sc->result = reason;
1406 goto fault;
1407 }
1408
Mike Christie301e0f72009-05-13 17:57:47 -05001409 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001410 /*
1411 * to handle the race between when we set the recovery state
1412 * and block the session we requeue here (commands could
1413 * be entering our queuecommand while a block is starting
1414 * up because the block code is not locked)
1415 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001416 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001417 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001418 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001419 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001420 sc->result = DID_IMM_RETRY << 16;
1421 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001422 case ISCSI_STATE_LOGGING_OUT:
1423 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001424 sc->result = DID_IMM_RETRY << 16;
1425 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001426 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001427 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001428 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001429 break;
1430 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001431 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001432 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001433 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001434 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001435 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001436 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001437 }
Mike Christie7996a772006-04-06 21:13:41 -05001438 goto fault;
1439 }
1440
Mike Christie7996a772006-04-06 21:13:41 -05001441 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001442 if (!conn) {
1443 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001444 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001445 goto fault;
1446 }
Mike Christie7996a772006-04-06 21:13:41 -05001447
Mike Christie77a23c22007-05-30 12:57:18 -05001448 if (iscsi_check_cmdsn_window_closed(conn)) {
1449 reason = FAILURE_WINDOW_CLOSED;
1450 goto reject;
1451 }
1452
Mike Christie577577d2008-12-02 00:32:05 -06001453 task = iscsi_alloc_task(conn, sc);
1454 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001455 reason = FAILURE_OOM;
1456 goto reject;
1457 }
Mike Christie7996a772006-04-06 21:13:41 -05001458
Mike Christie1336aed2009-05-13 17:57:48 -05001459 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001460 reason = iscsi_prep_scsi_cmd_pdu(task);
1461 if (reason) {
1462 if (reason == -ENOMEM) {
1463 reason = FAILURE_OOM;
1464 goto prepd_reject;
1465 } else {
1466 sc->result = DID_ABORT << 16;
1467 goto prepd_fault;
1468 }
Mike Christie052d0142008-05-21 15:54:05 -05001469 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001470 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001471 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001472 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001473 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001474 } else {
1475 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001476 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001477 }
Mike Christie052d0142008-05-21 15:54:05 -05001478
1479 session->queued_cmdsn++;
1480 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001481 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001482 return 0;
1483
Mike Christie577577d2008-12-02 00:32:05 -06001484prepd_reject:
1485 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001486 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001487reject:
1488 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001489 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1490 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001491 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001492 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001493
Mike Christie577577d2008-12-02 00:32:05 -06001494prepd_fault:
1495 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001496 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001497fault:
1498 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001499 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1500 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001501 if (!scsi_bidi_cmnd(sc))
1502 scsi_set_resid(sc, scsi_bufflen(sc));
1503 else {
1504 scsi_out(sc)->resid = scsi_out(sc)->length;
1505 scsi_in(sc)->resid = scsi_in(sc)->length;
1506 }
Mike Christie052d0142008-05-21 15:54:05 -05001507 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001508 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001509 return 0;
1510}
1511EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1512
1513int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1514{
Mike Christie7996a772006-04-06 21:13:41 -05001515 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1516 return sdev->queue_depth;
1517}
1518EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1519
Mike Christie6b5d6c42009-04-21 15:32:32 -05001520int iscsi_target_alloc(struct scsi_target *starget)
1521{
1522 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1523 struct iscsi_session *session = cls_session->dd_data;
1524
1525 starget->can_queue = session->scsi_cmds_max;
1526 return 0;
1527}
1528EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1529
Mike Christie7996a772006-04-06 21:13:41 -05001530void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1531{
Mike Christie75613522008-05-21 15:53:59 -05001532 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001533
1534 spin_lock_bh(&session->lock);
1535 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001536 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001537 if (session->leadconn)
1538 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001539 }
1540 spin_unlock_bh(&session->lock);
1541}
1542EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1543
Mike Christie8e124522008-09-24 11:46:12 -05001544int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001545{
Mike Christie75613522008-05-21 15:53:59 -05001546 struct iscsi_cls_session *cls_session;
1547 struct iscsi_session *session;
1548 struct iscsi_conn *conn;
1549
1550 cls_session = starget_to_session(scsi_target(sc->device));
1551 session = cls_session->dd_data;
1552 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001553
Mike Christiebc436b22007-12-13 12:43:28 -06001554 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001555 spin_lock_bh(&session->lock);
1556 if (session->state == ISCSI_STATE_TERMINATE) {
1557failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001558 iscsi_session_printk(KERN_INFO, session,
1559 "failing target reset: Could not log "
1560 "back into target [age %d]\n",
1561 session->age);
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 return FAILED;
1565 }
1566
Mike Christie7996a772006-04-06 21:13:41 -05001567 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001568 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001569 /*
1570 * we drop the lock here but the leadconn cannot be destoyed while
1571 * we are in the scsi eh
1572 */
Mike Christie843c0a82007-12-13 12:43:20 -06001573 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001574
Mike Christie1b2c7af2009-03-05 14:45:58 -06001575 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001576 wait_event_interruptible(conn->ehwait,
1577 session->state == ISCSI_STATE_TERMINATE ||
1578 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001579 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001580 if (signal_pending(current))
1581 flush_signals(current);
1582
Mike Christiebc436b22007-12-13 12:43:28 -06001583 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001584 spin_lock_bh(&session->lock);
1585 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001586 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001587 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001588 else
1589 goto failed;
1590 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001591 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001592 return SUCCESS;
1593}
Mike Christie8e124522008-09-24 11:46:12 -05001594EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001595
Mike Christie843c0a82007-12-13 12:43:20 -06001596static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001597{
Mike Christie843c0a82007-12-13 12:43:20 -06001598 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001599 struct iscsi_session *session = conn->session;
1600
1601 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001602 if (conn->tmf_state == TMF_QUEUED) {
1603 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001604 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001605 /* unblock eh_abort() */
1606 wake_up(&conn->ehwait);
1607 }
1608 spin_unlock(&session->lock);
1609}
1610
Mike Christie9c19a7d2008-05-21 15:54:09 -05001611static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001612 struct iscsi_tm *hdr, int age,
1613 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001614{
Mike Christie7996a772006-04-06 21:13:41 -05001615 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001616 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001617
Mike Christie9c19a7d2008-05-21 15:54:09 -05001618 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001619 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001620 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001621 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001622 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001623 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001624 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001625 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001626 }
Mike Christie843c0a82007-12-13 12:43:20 -06001627 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001628 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001629 conn->tmf_timer.function = iscsi_tmf_timedout;
1630 conn->tmf_timer.data = (unsigned long)conn;
1631 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001632 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001633
Mike Christie7996a772006-04-06 21:13:41 -05001634 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001635 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001636
1637 /*
1638 * block eh thread until:
1639 *
Mike Christie843c0a82007-12-13 12:43:20 -06001640 * 1) tmf response
1641 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001642 * 3) session is terminated or restarted or userspace has
1643 * given up on recovery
1644 */
Mike Christie843c0a82007-12-13 12:43:20 -06001645 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001646 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001647 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001648 if (signal_pending(current))
1649 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001650 del_timer_sync(&conn->tmf_timer);
1651
Mike Christie6724add2007-08-15 01:38:30 -05001652 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001653 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001654 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001655 if (age != session->age ||
1656 session->state != ISCSI_STATE_LOGGED_IN)
1657 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001658 return 0;
1659}
1660
1661/*
Mike Christie843c0a82007-12-13 12:43:20 -06001662 * Fail commands. session lock held and recv side suspended and xmit
1663 * thread flushed
1664 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001665static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1666 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001667{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001668 struct iscsi_task *task;
1669 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001670
Mike Christie3bbaaad2009-05-13 17:57:46 -05001671 for (i = 0; i < conn->session->cmds_max; i++) {
1672 task = conn->session->cmds[i];
1673 if (!task->sc || task->state == ISCSI_TASK_FREE)
1674 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001675
Mike Christie3bbaaad2009-05-13 17:57:46 -05001676 if (lun != -1 && lun != task->sc->device->lun)
1677 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001678
Mike Christie3bbaaad2009-05-13 17:57:46 -05001679 ISCSI_DBG_SESSION(conn->session,
1680 "failing sc %p itt 0x%x state %d\n",
1681 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001682 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001683 }
1684}
1685
Mike Christieb40977d2008-05-21 15:54:03 -05001686void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001687{
Mike Christie32ae7632009-03-05 14:46:03 -06001688 struct Scsi_Host *shost = conn->session->host;
1689 struct iscsi_host *ihost = shost_priv(shost);
1690
Mike Christie6724add2007-08-15 01:38:30 -05001691 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001692 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001693 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001694}
Mike Christieb40977d2008-05-21 15:54:03 -05001695EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001696
1697static void iscsi_start_tx(struct iscsi_conn *conn)
1698{
1699 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001700 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001701}
1702
Mike Christie4c48a822009-05-13 17:57:45 -05001703/*
1704 * We want to make sure a ping is in flight. It has timed out.
1705 * And we are not busy processing a pdu that is making
1706 * progress but got started before the ping and is taking a while
1707 * to complete so the ping is just stuck behind it in a queue.
1708 */
1709static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1710{
1711 if (conn->ping_task &&
1712 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1713 (conn->ping_timeout * HZ), jiffies))
1714 return 1;
1715 else
1716 return 0;
1717}
1718
Jens Axboe242f9dc2008-09-14 05:55:09 -07001719static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001720{
1721 struct iscsi_cls_session *cls_session;
1722 struct iscsi_session *session;
1723 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001724 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001725
1726 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001727 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001728
Mike Christie1b2c7af2009-03-05 14:45:58 -06001729 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
Mike Christief6d51802007-12-13 12:43:30 -06001730
1731 spin_lock(&session->lock);
1732 if (session->state != ISCSI_STATE_LOGGED_IN) {
1733 /*
1734 * We are probably in the middle of iscsi recovery so let
1735 * that complete and handle the error.
1736 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001737 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001738 goto done;
1739 }
1740
1741 conn = session->leadconn;
1742 if (!conn) {
1743 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001744 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001745 goto done;
1746 }
1747
1748 if (!conn->recv_timeout && !conn->ping_timeout)
1749 goto done;
1750 /*
1751 * if the ping timedout then we are in the middle of cleaning up
1752 * and can let the iscsi eh handle it
1753 */
Mike Christie4c48a822009-05-13 17:57:45 -05001754 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001755 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001756 goto done;
1757 }
Mike Christief6d51802007-12-13 12:43:30 -06001758 /*
1759 * if we are about to check the transport then give the command
1760 * more time
1761 */
1762 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
Mike Christie4c48a822009-05-13 17:57:45 -05001763 jiffies)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001764 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001765 goto done;
1766 }
1767
Mike Christief6d51802007-12-13 12:43:30 -06001768 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001769 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001770 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001771done:
1772 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001773 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1774 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001775 return rc;
1776}
1777
1778static void iscsi_check_transport_timeouts(unsigned long data)
1779{
1780 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1781 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001782 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001783
1784 spin_lock(&session->lock);
1785 if (session->state != ISCSI_STATE_LOGGED_IN)
1786 goto done;
1787
Mike Christie4cf10432008-05-07 20:43:52 -05001788 recv_timeout = conn->recv_timeout;
1789 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001790 goto done;
1791
Mike Christie4cf10432008-05-07 20:43:52 -05001792 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001793 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001794
1795 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001796 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001797 "expired, recv timeout %d, last rx %lu, "
1798 "last ping %lu, now %lu\n",
1799 conn->ping_timeout, conn->recv_timeout,
1800 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001801 spin_unlock(&session->lock);
1802 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1803 return;
1804 }
1805
Mike Christie4cf10432008-05-07 20:43:52 -05001806 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001807 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001808 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001809 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001810 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001811 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001812 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001813
Mike Christie1b2c7af2009-03-05 14:45:58 -06001814 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001815 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001816done:
1817 spin_unlock(&session->lock);
1818}
1819
Mike Christie9c19a7d2008-05-21 15:54:09 -05001820static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001821 struct iscsi_tm *hdr)
1822{
1823 memset(hdr, 0, sizeof(*hdr));
1824 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1825 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1826 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001827 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1828 hdr->rtt = task->hdr_itt;
1829 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001830}
1831
Mike Christie7996a772006-04-06 21:13:41 -05001832int iscsi_eh_abort(struct scsi_cmnd *sc)
1833{
Mike Christie75613522008-05-21 15:53:59 -05001834 struct iscsi_cls_session *cls_session;
1835 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001836 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001837 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001838 struct iscsi_tm *hdr;
1839 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001840
Mike Christie75613522008-05-21 15:53:59 -05001841 cls_session = starget_to_session(scsi_target(sc->device));
1842 session = cls_session->dd_data;
1843
Mike Christie4421c9e2009-05-13 17:57:50 -05001844 ISCSI_DBG_SESSION(session, "aborting sc %p\n", sc);
1845
Mike Christie6724add2007-08-15 01:38:30 -05001846 mutex_lock(&session->eh_mutex);
1847 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001848 /*
1849 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1850 * got the command.
1851 */
1852 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001853 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1854 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001855 spin_unlock_bh(&session->lock);
1856 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001857 return SUCCESS;
1858 }
1859
Mike Christie7996a772006-04-06 21:13:41 -05001860 /*
1861 * If we are not logged in or we have started a new session
1862 * then let the host reset code handle this
1863 */
Mike Christie843c0a82007-12-13 12:43:20 -06001864 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1865 sc->SCp.phase != session->age) {
1866 spin_unlock_bh(&session->lock);
1867 mutex_unlock(&session->eh_mutex);
Mike Christie4421c9e2009-05-13 17:57:50 -05001868 ISCSI_DBG_SESSION(session, "failing abort due to dropped "
1869 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06001870 return FAILED;
1871 }
1872
1873 conn = session->leadconn;
1874 conn->eh_abort_cnt++;
1875 age = session->age;
1876
Mike Christie9c19a7d2008-05-21 15:54:09 -05001877 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001878 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1879 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001880
Mike Christie9c19a7d2008-05-21 15:54:09 -05001881 /* task completed before time out */
1882 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001883 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1884 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001885 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001886 }
Mike Christie7996a772006-04-06 21:13:41 -05001887
Mike Christie9c19a7d2008-05-21 15:54:09 -05001888 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001889 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05001890 goto success;
1891 }
Mike Christie7996a772006-04-06 21:13:41 -05001892
Mike Christie843c0a82007-12-13 12:43:20 -06001893 /* only have one tmf outstanding at a time */
1894 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001895 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001896 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001897
Mike Christie843c0a82007-12-13 12:43:20 -06001898 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001899 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001900
Mike Christie9c19a7d2008-05-21 15:54:09 -05001901 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001902 rc = FAILED;
1903 goto failed;
1904 }
1905
1906 switch (conn->tmf_state) {
1907 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001908 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001909 /*
1910 * stop tx side incase the target had sent a abort rsp but
1911 * the initiator was still writing out data.
1912 */
Mike Christie6724add2007-08-15 01:38:30 -05001913 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001914 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001915 * we do not stop the recv side because targets have been
1916 * good and have never sent us a successful tmf response
1917 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001918 */
Mike Christie77a23c22007-05-30 12:57:18 -05001919 spin_lock(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05001920 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06001921 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001922 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001923 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001924 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001925 case TMF_TIMEDOUT:
1926 spin_unlock_bh(&session->lock);
1927 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1928 goto failed_unlocked;
1929 case TMF_NOT_FOUND:
1930 if (!sc->SCp.ptr) {
1931 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001932 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001933 ISCSI_DBG_SESSION(session, "sc completed while abort "
1934 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001935 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001936 }
1937 /* fall through */
1938 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001939 conn->tmf_state = TMF_INITIAL;
1940 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001941 }
1942
Mike Christie77a23c22007-05-30 12:57:18 -05001943success:
Mike Christie7996a772006-04-06 21:13:41 -05001944 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001945success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001946 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1947 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001948 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001949 return SUCCESS;
1950
1951failed:
1952 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001953failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001954 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1955 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001956 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001957 return FAILED;
1958}
1959EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1960
Mike Christie843c0a82007-12-13 12:43:20 -06001961static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1962{
1963 memset(hdr, 0, sizeof(*hdr));
1964 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1965 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1966 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1967 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001968 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001969}
1970
1971int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1972{
Mike Christie75613522008-05-21 15:53:59 -05001973 struct iscsi_cls_session *cls_session;
1974 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001975 struct iscsi_conn *conn;
1976 struct iscsi_tm *hdr;
1977 int rc = FAILED;
1978
Mike Christie75613522008-05-21 15:53:59 -05001979 cls_session = starget_to_session(scsi_target(sc->device));
1980 session = cls_session->dd_data;
1981
Mike Christie1b2c7af2009-03-05 14:45:58 -06001982 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1983 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06001984
1985 mutex_lock(&session->eh_mutex);
1986 spin_lock_bh(&session->lock);
1987 /*
1988 * Just check if we are not logged in. We cannot check for
1989 * the phase because the reset could come from a ioctl.
1990 */
1991 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1992 goto unlock;
1993 conn = session->leadconn;
1994
1995 /* only have one tmf outstanding at a time */
1996 if (conn->tmf_state != TMF_INITIAL)
1997 goto unlock;
1998 conn->tmf_state = TMF_QUEUED;
1999
2000 hdr = &conn->tmhdr;
2001 iscsi_prep_lun_reset_pdu(sc, hdr);
2002
Mike Christie9c19a7d2008-05-21 15:54:09 -05002003 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002004 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002005 rc = FAILED;
2006 goto unlock;
2007 }
2008
2009 switch (conn->tmf_state) {
2010 case TMF_SUCCESS:
2011 break;
2012 case TMF_TIMEDOUT:
2013 spin_unlock_bh(&session->lock);
2014 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2015 goto done;
2016 default:
2017 conn->tmf_state = TMF_INITIAL;
2018 goto unlock;
2019 }
2020
2021 rc = SUCCESS;
2022 spin_unlock_bh(&session->lock);
2023
2024 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002025
Mike Christiea3439142008-09-24 11:46:15 -05002026 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002027 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002028 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002029 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002030
2031 iscsi_start_tx(conn);
2032 goto done;
2033
2034unlock:
2035 spin_unlock_bh(&session->lock);
2036done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06002037 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
2038 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002039 mutex_unlock(&session->eh_mutex);
2040 return rc;
2041}
2042EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2043
Olaf Kirch63203772007-12-13 12:43:25 -06002044/*
2045 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2046 * should be accessed via kfifo_{get,put} on q->queue.
2047 * Optionally, the caller can obtain the array of object pointers
2048 * by passing in a non-NULL @items pointer
2049 */
Mike Christie7996a772006-04-06 21:13:41 -05002050int
Olaf Kirch63203772007-12-13 12:43:25 -06002051iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002052{
Olaf Kirch63203772007-12-13 12:43:25 -06002053 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002054
Olaf Kirch63203772007-12-13 12:43:25 -06002055 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002056
2057 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002058
2059 /* If the user passed an items pointer, he wants a copy of
2060 * the array. */
2061 if (items)
2062 num_arrays++;
2063 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2064 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002065 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002066
2067 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2068 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002069 if (IS_ERR(q->queue)) {
2070 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002071 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002072 }
Mike Christie7996a772006-04-06 21:13:41 -05002073
2074 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002075 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002076 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002077 q->max = i;
2078 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002079 }
Mike Christie7996a772006-04-06 21:13:41 -05002080 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2081 }
Olaf Kirch63203772007-12-13 12:43:25 -06002082
2083 if (items) {
2084 *items = q->pool + max;
2085 memcpy(*items, q->pool, max * sizeof(void *));
2086 }
2087
Mike Christie7996a772006-04-06 21:13:41 -05002088 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002089
2090enomem:
2091 iscsi_pool_free(q);
2092 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002093}
2094EXPORT_SYMBOL_GPL(iscsi_pool_init);
2095
Olaf Kirch63203772007-12-13 12:43:25 -06002096void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002097{
2098 int i;
2099
2100 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002101 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002102 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002103 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002104}
2105EXPORT_SYMBOL_GPL(iscsi_pool_free);
2106
Mike Christiea4804cd2008-05-21 15:54:00 -05002107/**
2108 * iscsi_host_add - add host to system
2109 * @shost: scsi host
2110 * @pdev: parent device
2111 *
2112 * This should be called by partial offload and software iscsi drivers
2113 * to add a host to the system.
2114 */
2115int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002116{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002117 if (!shost->can_queue)
2118 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2119
Mike Christie4d108352009-03-05 14:46:04 -06002120 if (!shost->cmd_per_lun)
2121 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2122
Mike Christie308cec12009-02-06 12:06:20 -06002123 if (!shost->transportt->eh_timed_out)
2124 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002125 return scsi_add_host(shost, pdev);
2126}
2127EXPORT_SYMBOL_GPL(iscsi_host_add);
2128
2129/**
2130 * iscsi_host_alloc - allocate a host and driver data
2131 * @sht: scsi host template
2132 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002133 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002134 *
2135 * This should be called by partial offload and software iscsi drivers.
2136 * To access the driver specific memory use the iscsi_host_priv() macro.
2137 */
2138struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002139 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002140{
2141 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002142 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002143
2144 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2145 if (!shost)
2146 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002147 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002148
2149 if (xmit_can_sleep) {
2150 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2151 "iscsi_q_%d", shost->host_no);
2152 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2153 if (!ihost->workq)
2154 goto free_host;
2155 }
2156
Mike Christiee5bd7b52008-09-24 11:46:10 -05002157 spin_lock_init(&ihost->lock);
2158 ihost->state = ISCSI_HOST_SETUP;
2159 ihost->num_sessions = 0;
2160 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002161 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002162
2163free_host:
2164 scsi_host_put(shost);
2165 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002166}
Mike Christiea4804cd2008-05-21 15:54:00 -05002167EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002168
Mike Christiee5bd7b52008-09-24 11:46:10 -05002169static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2170{
Mike Christie40a06e72009-03-05 14:46:05 -06002171 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002172}
2173
Mike Christiea4804cd2008-05-21 15:54:00 -05002174/**
2175 * iscsi_host_remove - remove host and sessions
2176 * @shost: scsi host
2177 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002178 * If there are any sessions left, this will initiate the removal and wait
2179 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002180 */
2181void iscsi_host_remove(struct Scsi_Host *shost)
2182{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002183 struct iscsi_host *ihost = shost_priv(shost);
2184 unsigned long flags;
2185
2186 spin_lock_irqsave(&ihost->lock, flags);
2187 ihost->state = ISCSI_HOST_REMOVED;
2188 spin_unlock_irqrestore(&ihost->lock, flags);
2189
2190 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2191 wait_event_interruptible(ihost->session_removal_wq,
2192 ihost->num_sessions == 0);
2193 if (signal_pending(current))
2194 flush_signals(current);
2195
Mike Christiea4804cd2008-05-21 15:54:00 -05002196 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002197 if (ihost->workq)
2198 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002199}
2200EXPORT_SYMBOL_GPL(iscsi_host_remove);
2201
2202void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002203{
2204 struct iscsi_host *ihost = shost_priv(shost);
2205
2206 kfree(ihost->netdev);
2207 kfree(ihost->hwaddress);
2208 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002209 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002210}
Mike Christiea4804cd2008-05-21 15:54:00 -05002211EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002212
Mike Christiee5bd7b52008-09-24 11:46:10 -05002213static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2214{
2215 struct iscsi_host *ihost = shost_priv(shost);
2216 unsigned long flags;
2217
2218 shost = scsi_host_get(shost);
2219 if (!shost) {
2220 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2221 "of session teardown event because host already "
2222 "removed.\n");
2223 return;
2224 }
2225
2226 spin_lock_irqsave(&ihost->lock, flags);
2227 ihost->num_sessions--;
2228 if (ihost->num_sessions == 0)
2229 wake_up(&ihost->session_removal_wq);
2230 spin_unlock_irqrestore(&ihost->lock, flags);
2231 scsi_host_put(shost);
2232}
2233
Mike Christie75613522008-05-21 15:53:59 -05002234/**
2235 * iscsi_session_setup - create iscsi cls session and host and session
2236 * @iscsit: iscsi transport template
2237 * @shost: scsi host
2238 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002239 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002240 * @initial_cmdsn: initial CmdSN
2241 *
2242 * This can be used by software iscsi_transports that allocate
2243 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002244 *
2245 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2246 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2247 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002248 */
2249struct iscsi_cls_session *
2250iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002251 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002252 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002253{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002254 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002255 struct iscsi_session *session;
2256 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002257 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002258 unsigned long flags;
2259
2260 spin_lock_irqsave(&ihost->lock, flags);
2261 if (ihost->state == ISCSI_HOST_REMOVED) {
2262 spin_unlock_irqrestore(&ihost->lock, flags);
2263 return NULL;
2264 }
2265 ihost->num_sessions++;
2266 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002267
2268 if (!total_cmds)
2269 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002270 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002271 * The iscsi layer needs some tasks for nop handling and tmfs,
2272 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2273 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002274 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002275 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2276 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2277 "must be a power of two that is at least %d.\n",
2278 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002279 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002280 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002281
2282 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2283 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2284 "must be a power of 2 less than or equal to %d.\n",
2285 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2286 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2287 }
2288
2289 if (!is_power_of_2(total_cmds)) {
2290 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2291 "must be a power of 2.\n", total_cmds);
2292 total_cmds = rounddown_pow_of_two(total_cmds);
2293 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2294 return NULL;
2295 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2296 total_cmds);
2297 }
2298 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002299
Mike Christie5d91e202008-05-21 15:54:01 -05002300 cls_session = iscsi_alloc_session(shost, iscsit,
2301 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002302 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002303 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002304 session = cls_session->dd_data;
2305 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002306 session->host = shost;
2307 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002308 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002309 session->lu_reset_timeout = 15;
2310 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002311 session->scsi_cmds_max = scsi_cmds;
2312 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002313 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002314 session->exp_cmdsn = initial_cmdsn + 1;
2315 session->max_cmdsn = initial_cmdsn + 1;
2316 session->max_r2t = 1;
2317 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002318 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002319 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002320
2321 /* initialize SCSI PDU commands pool */
2322 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2323 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002324 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002325 goto cmdpool_alloc_fail;
2326
2327 /* pre-format cmds pool with ITT */
2328 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002329 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002330
Mike Christie9c19a7d2008-05-21 15:54:09 -05002331 if (cmd_task_size)
2332 task->dd_data = &task[1];
2333 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002334 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002335 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002336 }
2337
Mike Christief53a88d2006-06-28 12:00:27 -05002338 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002339 goto module_get_fail;
2340
Mike Christie79706342008-05-21 15:54:12 -05002341 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002342 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002343
Mike Christie7996a772006-04-06 21:13:41 -05002344 return cls_session;
2345
2346cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002347 module_put(iscsit->owner);
2348module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002349 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002350cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002351 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002352dec_session_count:
2353 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002354 return NULL;
2355}
2356EXPORT_SYMBOL_GPL(iscsi_session_setup);
2357
2358/**
2359 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002360 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002361 *
Mike Christie75613522008-05-21 15:53:59 -05002362 * The driver must have called iscsi_remove_session before
2363 * calling this.
2364 */
Mike Christie7996a772006-04-06 21:13:41 -05002365void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2366{
Mike Christie75613522008-05-21 15:53:59 -05002367 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002368 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002369 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002370
Olaf Kirch63203772007-12-13 12:43:25 -06002371 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002372
Mike Christieb2c64162007-05-30 12:57:16 -05002373 kfree(session->password);
2374 kfree(session->password_in);
2375 kfree(session->username);
2376 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002377 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002378 kfree(session->initiatorname);
2379 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002380
Mike Christie75613522008-05-21 15:53:59 -05002381 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002382 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002383 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002384}
2385EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2386
2387/**
2388 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2389 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002390 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002391 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002392 */
Mike Christie7996a772006-04-06 21:13:41 -05002393struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002394iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2395 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002396{
Mike Christie75613522008-05-21 15:53:59 -05002397 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002398 struct iscsi_conn *conn;
2399 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002400 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002401
Mike Christie5d91e202008-05-21 15:54:01 -05002402 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2403 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002404 if (!cls_conn)
2405 return NULL;
2406 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002407 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002408
Mike Christie5d91e202008-05-21 15:54:01 -05002409 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002410 conn->session = session;
2411 conn->cls_conn = cls_conn;
2412 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2413 conn->id = conn_idx;
2414 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002415 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002416
2417 init_timer(&conn->transport_timer);
2418 conn->transport_timer.data = (unsigned long)conn;
2419 conn->transport_timer.function = iscsi_check_transport_timeouts;
2420
Mike Christie843c0a82007-12-13 12:43:20 -06002421 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002422 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002423 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002424 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002425
Mike Christie9c19a7d2008-05-21 15:54:09 -05002426 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002427 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002428 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002429 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002430 sizeof(void*))) {
2431 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002432 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002433 }
2434 spin_unlock_bh(&session->lock);
2435
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002436 data = (char *) __get_free_pages(GFP_KERNEL,
2437 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002438 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002439 goto login_task_data_alloc_fail;
2440 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002441
Mike Christie843c0a82007-12-13 12:43:20 -06002442 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002443 init_waitqueue_head(&conn->ehwait);
2444
2445 return cls_conn;
2446
Mike Christie9c19a7d2008-05-21 15:54:09 -05002447login_task_data_alloc_fail:
2448 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002449 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002450login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002451 iscsi_destroy_conn(cls_conn);
2452 return NULL;
2453}
2454EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2455
2456/**
2457 * iscsi_conn_teardown - teardown iscsi connection
2458 * cls_conn: iscsi class connection
2459 *
2460 * TODO: we may need to make this into a two step process
2461 * like scsi-mls remove + put host
2462 */
2463void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2464{
2465 struct iscsi_conn *conn = cls_conn->dd_data;
2466 struct iscsi_session *session = conn->session;
2467 unsigned long flags;
2468
Mike Christief6d51802007-12-13 12:43:30 -06002469 del_timer_sync(&conn->transport_timer);
2470
Mike Christie7996a772006-04-06 21:13:41 -05002471 spin_lock_bh(&session->lock);
2472 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2473 if (session->leadconn == conn) {
2474 /*
2475 * leading connection? then give up on recovery.
2476 */
2477 session->state = ISCSI_STATE_TERMINATE;
2478 wake_up(&conn->ehwait);
2479 }
2480 spin_unlock_bh(&session->lock);
2481
Mike Christie7996a772006-04-06 21:13:41 -05002482 /*
2483 * Block until all in-progress commands for this connection
2484 * time out or fail.
2485 */
2486 for (;;) {
2487 spin_lock_irqsave(session->host->host_lock, flags);
2488 if (!session->host->host_busy) { /* OK for ERL == 0 */
2489 spin_unlock_irqrestore(session->host->host_lock, flags);
2490 break;
2491 }
2492 spin_unlock_irqrestore(session->host->host_lock, flags);
2493 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002494 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2495 "host_busy %d host_failed %d\n",
2496 session->host->host_busy,
2497 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002498 /*
2499 * force eh_abort() to unblock
2500 */
2501 wake_up(&conn->ehwait);
2502 }
2503
Mike Christie779ea122007-02-28 17:32:15 -06002504 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002505 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002506
Mike Christie7996a772006-04-06 21:13:41 -05002507 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002508 free_pages((unsigned long) conn->data,
2509 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002510 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002511 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002512 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002513 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002514 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002515 spin_unlock_bh(&session->lock);
2516
Mike Christie7996a772006-04-06 21:13:41 -05002517 iscsi_destroy_conn(cls_conn);
2518}
2519EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2520
2521int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2522{
2523 struct iscsi_conn *conn = cls_conn->dd_data;
2524 struct iscsi_session *session = conn->session;
2525
Mike Christieffd04362006-08-31 18:09:24 -04002526 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002527 iscsi_conn_printk(KERN_ERR, conn,
2528 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002529 return -EPERM;
2530 }
2531
Mike Christiedb98ccd2006-08-31 18:09:31 -04002532 if ((session->imm_data_en || !session->initial_r2t_en) &&
2533 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002534 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2535 "first_burst %d max_burst %d\n",
2536 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002537 return -EINVAL;
2538 }
2539
Mike Christief6d51802007-12-13 12:43:30 -06002540 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002541 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2542 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002543 conn->recv_timeout = 5;
2544 }
2545
2546 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002547 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2548 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002549 conn->ping_timeout = 5;
2550 }
2551
Mike Christie7996a772006-04-06 21:13:41 -05002552 spin_lock_bh(&session->lock);
2553 conn->c_stage = ISCSI_CONN_STARTED;
2554 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002555 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002556
Mike Christief6d51802007-12-13 12:43:30 -06002557 conn->last_recv = jiffies;
2558 conn->last_ping = jiffies;
2559 if (conn->recv_timeout && conn->ping_timeout)
2560 mod_timer(&conn->transport_timer,
2561 jiffies + (conn->recv_timeout * HZ));
2562
Mike Christie7996a772006-04-06 21:13:41 -05002563 switch(conn->stop_stage) {
2564 case STOP_CONN_RECOVER:
2565 /*
2566 * unblock eh_abort() if it is blocked. re-try all
2567 * commands after successful recovery
2568 */
Mike Christie7996a772006-04-06 21:13:41 -05002569 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002570 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002571 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002572 if (session->age == 16)
2573 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002574 break;
Mike Christie7996a772006-04-06 21:13:41 -05002575 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002576 conn->stop_stage = 0;
2577 break;
Mike Christie7996a772006-04-06 21:13:41 -05002578 default:
2579 break;
2580 }
2581 spin_unlock_bh(&session->lock);
2582
Mike Christie75613522008-05-21 15:53:59 -05002583 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002584 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002585 return 0;
2586}
2587EXPORT_SYMBOL_GPL(iscsi_conn_start);
2588
2589static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002590fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002591{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002592 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002593 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002594
Mike Christie3bbaaad2009-05-13 17:57:46 -05002595 for (i = 0; i < conn->session->cmds_max; i++) {
2596 task = conn->session->cmds[i];
2597 if (task->sc)
2598 continue;
2599
2600 if (task->state == ISCSI_TASK_FREE)
2601 continue;
2602
2603 ISCSI_DBG_SESSION(conn->session,
2604 "failing mgmt itt 0x%x state %d\n",
2605 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002606 state = ISCSI_TASK_ABRT_SESS_RECOV;
2607 if (task->state == ISCSI_TASK_PENDING)
2608 state = ISCSI_TASK_COMPLETED;
2609 iscsi_complete_task(task, state);
2610
Mike Christie7996a772006-04-06 21:13:41 -05002611 }
Mike Christie7996a772006-04-06 21:13:41 -05002612}
2613
Mike Christie656cffc2006-05-18 20:31:42 -05002614static void iscsi_start_session_recovery(struct iscsi_session *session,
2615 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002616{
Mike Christieed2abc72006-05-02 19:46:40 -05002617 int old_stop_stage;
2618
Mike Christie6724add2007-08-15 01:38:30 -05002619 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002620 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002621 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002622 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002623 mutex_unlock(&session->eh_mutex);
2624 return;
2625 }
2626
2627 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002628 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002629 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002630 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002631 */
Mike Christie67a61112006-05-30 00:37:20 -05002632 if (flag == STOP_CONN_TERM)
2633 session->state = ISCSI_STATE_TERMINATE;
2634 else if (conn->stop_stage != STOP_CONN_RECOVER)
2635 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002636 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002637
Mike Christie26013ad2009-05-13 17:57:43 -05002638 del_timer_sync(&conn->transport_timer);
2639 iscsi_suspend_tx(conn);
2640
2641 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002642 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002643 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002644 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002645 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002646
Mike Christie7996a772006-04-06 21:13:41 -05002647 /*
2648 * for connection level recovery we should not calculate
2649 * header digest. conn->hdr_size used for optimization
2650 * in hdr_extract() and will be re-negotiated at
2651 * set_param() time.
2652 */
2653 if (flag == STOP_CONN_RECOVER) {
2654 conn->hdrdgst_en = 0;
2655 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002656 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002657 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002658 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002659 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002660 }
Mike Christie7996a772006-04-06 21:13:41 -05002661 }
Mike Christie656cffc2006-05-18 20:31:42 -05002662
Mike Christie656cffc2006-05-18 20:31:42 -05002663 /*
2664 * flush queues.
2665 */
2666 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002667 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002668 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002669 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002670 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002671}
Mike Christie7996a772006-04-06 21:13:41 -05002672
2673void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2674{
2675 struct iscsi_conn *conn = cls_conn->dd_data;
2676 struct iscsi_session *session = conn->session;
2677
2678 switch (flag) {
2679 case STOP_CONN_RECOVER:
2680 case STOP_CONN_TERM:
2681 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002682 break;
Mike Christie7996a772006-04-06 21:13:41 -05002683 default:
Mike Christie322d7392008-01-31 13:36:52 -06002684 iscsi_conn_printk(KERN_ERR, conn,
2685 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002686 }
2687}
2688EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2689
2690int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2691 struct iscsi_cls_conn *cls_conn, int is_leading)
2692{
Mike Christie75613522008-05-21 15:53:59 -05002693 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002694 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002695
Mike Christie7996a772006-04-06 21:13:41 -05002696 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002697 if (is_leading)
2698 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002699 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002700
2701 /*
2702 * Unblock xmitworker(), Login Phase will pass through.
2703 */
2704 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2705 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2706 return 0;
2707}
2708EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2709
Mike Christie5700b1a2009-05-13 17:57:40 -05002710static int iscsi_switch_str_param(char **param, char *new_val_buf)
2711{
2712 char *new_val;
2713
2714 if (*param) {
2715 if (!strcmp(*param, new_val_buf))
2716 return 0;
2717 }
2718
2719 new_val = kstrdup(new_val_buf, GFP_NOIO);
2720 if (!new_val)
2721 return -ENOMEM;
2722
2723 kfree(*param);
2724 *param = new_val;
2725 return 0;
2726}
Mike Christiea54a52c2006-06-28 12:00:23 -05002727
2728int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2729 enum iscsi_param param, char *buf, int buflen)
2730{
2731 struct iscsi_conn *conn = cls_conn->dd_data;
2732 struct iscsi_session *session = conn->session;
2733 uint32_t value;
2734
2735 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002736 case ISCSI_PARAM_FAST_ABORT:
2737 sscanf(buf, "%d", &session->fast_abort);
2738 break;
Mike Christief6d51802007-12-13 12:43:30 -06002739 case ISCSI_PARAM_ABORT_TMO:
2740 sscanf(buf, "%d", &session->abort_timeout);
2741 break;
2742 case ISCSI_PARAM_LU_RESET_TMO:
2743 sscanf(buf, "%d", &session->lu_reset_timeout);
2744 break;
2745 case ISCSI_PARAM_PING_TMO:
2746 sscanf(buf, "%d", &conn->ping_timeout);
2747 break;
2748 case ISCSI_PARAM_RECV_TMO:
2749 sscanf(buf, "%d", &conn->recv_timeout);
2750 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002751 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2752 sscanf(buf, "%d", &conn->max_recv_dlength);
2753 break;
2754 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2755 sscanf(buf, "%d", &conn->max_xmit_dlength);
2756 break;
2757 case ISCSI_PARAM_HDRDGST_EN:
2758 sscanf(buf, "%d", &conn->hdrdgst_en);
2759 break;
2760 case ISCSI_PARAM_DATADGST_EN:
2761 sscanf(buf, "%d", &conn->datadgst_en);
2762 break;
2763 case ISCSI_PARAM_INITIAL_R2T_EN:
2764 sscanf(buf, "%d", &session->initial_r2t_en);
2765 break;
2766 case ISCSI_PARAM_MAX_R2T:
2767 sscanf(buf, "%d", &session->max_r2t);
2768 break;
2769 case ISCSI_PARAM_IMM_DATA_EN:
2770 sscanf(buf, "%d", &session->imm_data_en);
2771 break;
2772 case ISCSI_PARAM_FIRST_BURST:
2773 sscanf(buf, "%d", &session->first_burst);
2774 break;
2775 case ISCSI_PARAM_MAX_BURST:
2776 sscanf(buf, "%d", &session->max_burst);
2777 break;
2778 case ISCSI_PARAM_PDU_INORDER_EN:
2779 sscanf(buf, "%d", &session->pdu_inorder_en);
2780 break;
2781 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2782 sscanf(buf, "%d", &session->dataseq_inorder_en);
2783 break;
2784 case ISCSI_PARAM_ERL:
2785 sscanf(buf, "%d", &session->erl);
2786 break;
2787 case ISCSI_PARAM_IFMARKER_EN:
2788 sscanf(buf, "%d", &value);
2789 BUG_ON(value);
2790 break;
2791 case ISCSI_PARAM_OFMARKER_EN:
2792 sscanf(buf, "%d", &value);
2793 BUG_ON(value);
2794 break;
2795 case ISCSI_PARAM_EXP_STATSN:
2796 sscanf(buf, "%u", &conn->exp_statsn);
2797 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002798 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002799 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002800 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002801 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002802 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002803 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002804 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002805 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002806 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002807 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002808 case ISCSI_PARAM_TPGT:
2809 sscanf(buf, "%d", &session->tpgt);
2810 break;
2811 case ISCSI_PARAM_PERSISTENT_PORT:
2812 sscanf(buf, "%d", &conn->persistent_port);
2813 break;
2814 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002815 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002816 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002817 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002818 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002819 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002820 default:
2821 return -ENOSYS;
2822 }
2823
2824 return 0;
2825}
2826EXPORT_SYMBOL_GPL(iscsi_set_param);
2827
2828int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2829 enum iscsi_param param, char *buf)
2830{
Mike Christie75613522008-05-21 15:53:59 -05002831 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002832 int len;
2833
2834 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002835 case ISCSI_PARAM_FAST_ABORT:
2836 len = sprintf(buf, "%d\n", session->fast_abort);
2837 break;
Mike Christief6d51802007-12-13 12:43:30 -06002838 case ISCSI_PARAM_ABORT_TMO:
2839 len = sprintf(buf, "%d\n", session->abort_timeout);
2840 break;
2841 case ISCSI_PARAM_LU_RESET_TMO:
2842 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2843 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002844 case ISCSI_PARAM_INITIAL_R2T_EN:
2845 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2846 break;
2847 case ISCSI_PARAM_MAX_R2T:
2848 len = sprintf(buf, "%hu\n", session->max_r2t);
2849 break;
2850 case ISCSI_PARAM_IMM_DATA_EN:
2851 len = sprintf(buf, "%d\n", session->imm_data_en);
2852 break;
2853 case ISCSI_PARAM_FIRST_BURST:
2854 len = sprintf(buf, "%u\n", session->first_burst);
2855 break;
2856 case ISCSI_PARAM_MAX_BURST:
2857 len = sprintf(buf, "%u\n", session->max_burst);
2858 break;
2859 case ISCSI_PARAM_PDU_INORDER_EN:
2860 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2861 break;
2862 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2863 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2864 break;
2865 case ISCSI_PARAM_ERL:
2866 len = sprintf(buf, "%d\n", session->erl);
2867 break;
2868 case ISCSI_PARAM_TARGET_NAME:
2869 len = sprintf(buf, "%s\n", session->targetname);
2870 break;
2871 case ISCSI_PARAM_TPGT:
2872 len = sprintf(buf, "%d\n", session->tpgt);
2873 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002874 case ISCSI_PARAM_USERNAME:
2875 len = sprintf(buf, "%s\n", session->username);
2876 break;
2877 case ISCSI_PARAM_USERNAME_IN:
2878 len = sprintf(buf, "%s\n", session->username_in);
2879 break;
2880 case ISCSI_PARAM_PASSWORD:
2881 len = sprintf(buf, "%s\n", session->password);
2882 break;
2883 case ISCSI_PARAM_PASSWORD_IN:
2884 len = sprintf(buf, "%s\n", session->password_in);
2885 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002886 case ISCSI_PARAM_IFACE_NAME:
2887 len = sprintf(buf, "%s\n", session->ifacename);
2888 break;
2889 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002890 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05002891 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002892 default:
2893 return -ENOSYS;
2894 }
2895
2896 return len;
2897}
2898EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2899
2900int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2901 enum iscsi_param param, char *buf)
2902{
2903 struct iscsi_conn *conn = cls_conn->dd_data;
2904 int len;
2905
2906 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002907 case ISCSI_PARAM_PING_TMO:
2908 len = sprintf(buf, "%u\n", conn->ping_timeout);
2909 break;
2910 case ISCSI_PARAM_RECV_TMO:
2911 len = sprintf(buf, "%u\n", conn->recv_timeout);
2912 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002913 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2914 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2915 break;
2916 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2917 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2918 break;
2919 case ISCSI_PARAM_HDRDGST_EN:
2920 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2921 break;
2922 case ISCSI_PARAM_DATADGST_EN:
2923 len = sprintf(buf, "%d\n", conn->datadgst_en);
2924 break;
2925 case ISCSI_PARAM_IFMARKER_EN:
2926 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2927 break;
2928 case ISCSI_PARAM_OFMARKER_EN:
2929 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2930 break;
2931 case ISCSI_PARAM_EXP_STATSN:
2932 len = sprintf(buf, "%u\n", conn->exp_statsn);
2933 break;
2934 case ISCSI_PARAM_PERSISTENT_PORT:
2935 len = sprintf(buf, "%d\n", conn->persistent_port);
2936 break;
2937 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2938 len = sprintf(buf, "%s\n", conn->persistent_address);
2939 break;
2940 default:
2941 return -ENOSYS;
2942 }
2943
2944 return len;
2945}
2946EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2947
Mike Christie0801c242007-05-30 12:57:12 -05002948int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2949 char *buf)
2950{
Mike Christie75613522008-05-21 15:53:59 -05002951 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002952 int len;
2953
2954 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002955 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002956 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002957 break;
Mike Christie0801c242007-05-30 12:57:12 -05002958 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002959 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002960 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002961 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002962 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002963 break;
Mike Christie75613522008-05-21 15:53:59 -05002964 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002965 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002966 break;
Mike Christie0801c242007-05-30 12:57:12 -05002967 default:
2968 return -ENOSYS;
2969 }
2970
2971 return len;
2972}
2973EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2974
2975int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2976 char *buf, int buflen)
2977{
Mike Christie75613522008-05-21 15:53:59 -05002978 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002979
2980 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002981 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002982 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002983 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002984 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05002985 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002986 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002987 default:
2988 return -ENOSYS;
2989 }
2990
2991 return 0;
2992}
2993EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2994
Mike Christie7996a772006-04-06 21:13:41 -05002995MODULE_AUTHOR("Mike Christie");
2996MODULE_DESCRIPTION("iSCSI library functions");
2997MODULE_LICENSE("GPL");