blob: dafa054537f6bcfb9e44af3f0517c8051ef0b724 [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Mike Christie1b2c7af2009-03-05 14:45:58 -060041static int iscsi_dbg_lib;
42module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
48 do { \
49 if (iscsi_dbg_lib) \
50 iscsi_conn_printk(KERN_INFO, _conn, \
51 "%s " dbg_fmt, \
52 __func__, ##arg); \
53 } while (0);
54
55#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (iscsi_dbg_lib) \
58 iscsi_session_printk(KERN_INFO, _session, \
59 "%s " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
64#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050065
Mike Christie77a23c22007-05-30 12:57:18 -050066static int iscsi_sna_lt(u32 n1, u32 n2)
67{
68 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
69 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
70}
71
72/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
73static int iscsi_sna_lte(u32 n1, u32 n2)
74{
75 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
76 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
77}
78
Mike Christie32ae7632009-03-05 14:46:03 -060079inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
80{
81 struct Scsi_Host *shost = conn->session->host;
82 struct iscsi_host *ihost = shost_priv(shost);
83
Mike Christie1336aed2009-05-13 17:57:48 -050084 if (ihost->workq)
85 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -060086}
87EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
88
Mike Christie77a23c22007-05-30 12:57:18 -050089void
90iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050091{
92 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
93 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
94
Mike Christie77a23c22007-05-30 12:57:18 -050095 /*
96 * standard specifies this check for when to update expected and
97 * max sequence numbers
98 */
99 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
100 return;
101
102 if (exp_cmdsn != session->exp_cmdsn &&
103 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500104 session->exp_cmdsn = exp_cmdsn;
105
Mike Christie77a23c22007-05-30 12:57:18 -0500106 if (max_cmdsn != session->max_cmdsn &&
107 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
108 session->max_cmdsn = max_cmdsn;
109 /*
110 * if the window closed with IO queued, then kick the
111 * xmit thread
112 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500113 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500114 !list_empty(&session->leadconn->mgmtqueue))
115 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500116 }
Mike Christie7996a772006-04-06 21:13:41 -0500117}
Mike Christie77a23c22007-05-30 12:57:18 -0500118EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500119
Mike Christie577577d2008-12-02 00:32:05 -0600120/**
121 * iscsi_prep_data_out_pdu - initialize Data-Out
122 * @task: scsi command task
123 * @r2t: R2T info
124 * @hdr: iscsi data in pdu
125 *
126 * Notes:
127 * Initialize Data-Out within this R2T sequence and finds
128 * proper data_offset within this SCSI command.
129 *
130 * This function is called with connection lock taken.
131 **/
132void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
133 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500134{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500135 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600136 unsigned int left = r2t->data_length - r2t->sent;
137
138 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500139
140 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600141 hdr->ttt = r2t->ttt;
142 hdr->datasn = cpu_to_be32(r2t->datasn);
143 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500144 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600145 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
146 hdr->itt = task->hdr_itt;
147 hdr->exp_statsn = r2t->exp_statsn;
148 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
149 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500150 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600151 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500152 hdr->flags = 0;
153 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600154 hton24(hdr->dlength, left);
155 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500156 hdr->flags = ISCSI_FLAG_CMD_FINAL;
157 }
Mike Christie577577d2008-12-02 00:32:05 -0600158 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500159}
Mike Christie577577d2008-12-02 00:32:05 -0600160EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500161
Mike Christie9c19a7d2008-05-21 15:54:09 -0500162static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600163{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500164 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600165
Mike Christie9c19a7d2008-05-21 15:54:09 -0500166 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600167 WARN_ON(1);
168 return -EINVAL;
169 }
170
171 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500172 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600173 return 0;
174}
175
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500176/*
177 * make an extended cdb AHS
178 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500179static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500180{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500181 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500182 unsigned rlen, pad_len;
183 unsigned short ahslength;
184 struct iscsi_ecdb_ahdr *ecdb_ahdr;
185 int rc;
186
Mike Christie9c19a7d2008-05-21 15:54:09 -0500187 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500188 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
189
190 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
191 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
192
193 pad_len = iscsi_padding(rlen);
194
Mike Christie9c19a7d2008-05-21 15:54:09 -0500195 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500196 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
197 if (rc)
198 return rc;
199
200 if (pad_len)
201 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
202
203 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
204 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
205 ecdb_ahdr->reserved = 0;
206 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
207
Mike Christie1b2c7af2009-03-05 14:45:58 -0600208 ISCSI_DBG_SESSION(task->conn->session,
209 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
210 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
211 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
212 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500213 return 0;
214}
215
Mike Christie9c19a7d2008-05-21 15:54:09 -0500216static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500217{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500218 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500219 struct iscsi_rlength_ahdr *rlen_ahdr;
220 int rc;
221
Mike Christie9c19a7d2008-05-21 15:54:09 -0500222 rlen_ahdr = iscsi_next_hdr(task);
223 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500224 if (rc)
225 return rc;
226
227 rlen_ahdr->ahslength =
228 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
229 sizeof(rlen_ahdr->reserved));
230 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
231 rlen_ahdr->reserved = 0;
232 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
233
Mike Christie1b2c7af2009-03-05 14:45:58 -0600234 ISCSI_DBG_SESSION(task->conn->session,
235 "bidi-in rlen_ahdr->read_length(%d) "
236 "rlen_ahdr->ahslength(%d)\n",
237 be32_to_cpu(rlen_ahdr->read_length),
238 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500239 return 0;
240}
241
Mike Christie7996a772006-04-06 21:13:41 -0500242/**
243 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500245 *
246 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
247 * fields like dlength or final based on how much data it sends
248 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500249static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500250{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500251 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500252 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500253 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600254 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500255 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600256 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600257 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500258
Mike Christie184b57c2009-05-13 17:57:39 -0500259 if (conn->session->tt->alloc_pdu) {
260 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
261 if (rc)
262 return rc;
263 }
Mike Christie577577d2008-12-02 00:32:05 -0600264 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600265 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600266 memset(hdr, 0, sizeof(*hdr));
267
Mike Christie2ff79d52008-12-02 00:32:14 -0600268 if (session->tt->parse_pdu_itt)
269 hdr->itt = task->hdr_itt = itt;
270 else
271 hdr->itt = task->hdr_itt = build_itt(task->itt,
272 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500273 task->hdr_len = 0;
274 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600275 if (rc)
276 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600277 hdr->opcode = ISCSI_OP_SCSI_CMD;
278 hdr->flags = ISCSI_ATTR_SIMPLE;
279 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600280 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600281 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600282 session->cmdsn++;
283 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500284 cmd_len = sc->cmd_len;
285 if (cmd_len < ISCSI_CDB_SIZE)
286 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
287 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500288 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500289 if (rc)
290 return rc;
291 cmd_len = ISCSI_CDB_SIZE;
292 }
293 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500294
Mike Christie9c19a7d2008-05-21 15:54:09 -0500295 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500296 if (scsi_bidi_cmnd(sc)) {
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500298 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500299 if (rc)
300 return rc;
301 }
Mike Christie7996a772006-04-06 21:13:41 -0500302 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500303 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600304 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
305
Boaz Harroshc07d4442008-04-18 10:11:52 -0500306 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500307 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
308 /*
309 * Write counters:
310 *
311 * imm_count bytes to be sent right after
312 * SCSI PDU Header
313 *
314 * unsol_count bytes(as Data-Out) to be sent
315 * without R2T ack right after
316 * immediate data
317 *
Mike Christie577577d2008-12-02 00:32:05 -0600318 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500319 *
320 * pad_count bytes to be sent as zero-padding
321 */
Mike Christie577577d2008-12-02 00:32:05 -0600322 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500323
324 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500325 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500326 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500327 conn->max_xmit_dlength);
328 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500329 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500330 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500331 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500332 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600333 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500334
Mike Christieffd04362006-08-31 18:09:24 -0400335 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600336 r2t->data_length = min(session->first_burst, out_len) -
337 task->imm_count;
338 r2t->data_offset = task->imm_count;
339 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
340 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400341 }
342
Mike Christie577577d2008-12-02 00:32:05 -0600343 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500344 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600345 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500346 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500347 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
348 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500349 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500350
351 if (sc->sc_data_direction == DMA_FROM_DEVICE)
352 hdr->flags |= ISCSI_FLAG_CMD_READ;
353 }
354
Boaz Harrosh004d6532007-12-13 12:43:23 -0600355 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500356 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600357
358 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
359 hdrlength /= ISCSI_PAD_LEN;
360
361 WARN_ON(hdrlength >= 256);
362 hdr->hlength = hdrlength & 0xFF;
363
Mike Christie577577d2008-12-02 00:32:05 -0600364 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500365 return -EIO;
366
Mike Christie9c19a7d2008-05-21 15:54:09 -0500367 task->state = ISCSI_TASK_RUNNING;
Mike Christie77a23c22007-05-30 12:57:18 -0500368
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600370 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
371 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
372 scsi_bidi_cmnd(sc) ? "bidirectional" :
373 sc->sc_data_direction == DMA_TO_DEVICE ?
374 "write" : "read", conn->id, sc, sc->cmnd[0],
375 task->itt, scsi_bufflen(sc),
376 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
377 session->cmdsn,
378 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600379 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500380}
Mike Christie7996a772006-04-06 21:13:41 -0500381
382/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500383 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500385 *
386 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500387 * This function returns the scsi command to scsi-ml or cleans
388 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500389 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500390static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500391{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500392 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600393 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500394 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500395
Mike Christie577577d2008-12-02 00:32:05 -0600396 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500397 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500398 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500399
Mike Christie3e5c28a2008-05-21 15:54:06 -0500400 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500402 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500403 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500404 return;
405
Mike Christie9c19a7d2008-05-21 15:54:09 -0500406 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500407
Mike Christie3e5c28a2008-05-21 15:54:06 -0500408 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500409 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500410 /* SCSI eh reuses commands to verify us */
411 sc->SCp.ptr = NULL;
412 /*
413 * queue command may call this to free the task, but
414 * not have setup the sc callback
415 */
416 if (sc->scsi_done)
417 sc->scsi_done(sc);
418 }
Mike Christie7996a772006-04-06 21:13:41 -0500419}
420
Mike Christie913e5bf2008-05-21 15:54:18 -0500421void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400422{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500423 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400424}
Mike Christie913e5bf2008-05-21 15:54:18 -0500425EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400426
Mike Christie9c19a7d2008-05-21 15:54:09 -0500427static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400428{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500430 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400431}
432
Mike Christie9c19a7d2008-05-21 15:54:09 -0500433void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500434{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500435 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500436
437 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500438 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500439 spin_unlock_bh(&session->lock);
440}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500441EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500442
Mike Christie3bbaaad2009-05-13 17:57:46 -0500443/**
444 * iscsi_complete_task - finish a task
445 * @task: iscsi cmd task
446 *
447 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600448 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500449static void iscsi_complete_task(struct iscsi_task *task)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600450{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500451 struct iscsi_conn *conn = task->conn;
452
453 if (task->state == ISCSI_TASK_COMPLETED)
454 return;
455 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
456
457 task->state = ISCSI_TASK_COMPLETED;
458
459 if (!list_empty(&task->running))
460 list_del_init(&task->running);
461
462 if (conn->task == task)
463 conn->task = NULL;
464
465 if (conn->ping_task == task)
466 conn->ping_task = NULL;
467
468 /* release get from queueing */
469 __iscsi_put_task(task);
470}
471
472/*
473 * session lock must be held and if not called for a task that is
474 * still pending or from the xmit thread, then xmit thread must
475 * be suspended.
476 */
477static void fail_scsi_task(struct iscsi_task *task, int err)
478{
479 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600480 struct scsi_cmnd *sc;
481
Mike Christie3bbaaad2009-05-13 17:57:46 -0500482 /*
483 * if a command completes and we get a successful tmf response
484 * we will hit this because the scsi eh abort code does not take
485 * a ref to the task.
486 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500487 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600488 if (!sc)
489 return;
490
Mike Christie9c19a7d2008-05-21 15:54:09 -0500491 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600492 /*
493 * cmd never made it to the xmit thread, so we should not count
494 * the cmd in the sequencing
495 */
496 conn->session->queued_cmdsn--;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600497
498 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500499 if (!scsi_bidi_cmnd(sc))
500 scsi_set_resid(sc, scsi_bufflen(sc));
501 else {
502 scsi_out(sc)->resid = scsi_out(sc)->length;
503 scsi_in(sc)->resid = scsi_in(sc)->length;
504 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500505
Mike Christie3bbaaad2009-05-13 17:57:46 -0500506 iscsi_complete_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600507}
508
Mike Christie3e5c28a2008-05-21 15:54:06 -0500509static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500510 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500511{
512 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600513 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500514 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
515
516 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
517 return -ENOTCONN;
518
519 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
520 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
521 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
522 /*
523 * pre-format CmdSN for outgoing PDU.
524 */
525 nop->cmdsn = cpu_to_be32(session->cmdsn);
526 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500527 /*
528 * TODO: We always use immediate, so we never hit this.
529 * If we start to send tmfs or nops as non-immediate then
530 * we should start checking the cmdsn numbers for mgmt tasks.
531 */
532 if (conn->c_stage == ISCSI_CONN_STARTED &&
533 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
534 session->queued_cmdsn++;
535 session->cmdsn++;
536 }
537 }
538
Mike Christieae15f802008-12-02 00:32:15 -0600539 if (session->tt->init_task && session->tt->init_task(task))
540 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500541
542 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
543 session->state = ISCSI_STATE_LOGGING_OUT;
544
Mike Christie577577d2008-12-02 00:32:05 -0600545 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600546 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
547 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
548 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500549 return 0;
550}
551
Mike Christie9c19a7d2008-05-21 15:54:09 -0500552static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600553__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
554 char *data, uint32_t data_size)
555{
556 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500557 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500558 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600559 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600560
561 if (session->state == ISCSI_STATE_TERMINATE)
562 return NULL;
563
564 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
565 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
566 /*
567 * Login and Text are sent serially, in
568 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500569 * Same task can be used. Same ITT must be used.
570 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600571 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500572 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600573 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500574 if (session->state != ISCSI_STATE_LOGGED_IN)
575 return NULL;
576
Mike Christief6d51802007-12-13 12:43:30 -0600577 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
578 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
579
Mike Christie3e5c28a2008-05-21 15:54:06 -0500580 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500581 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600582 return NULL;
583 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500584 /*
585 * released in complete pdu for task we expect a response for, and
586 * released by the lld when it has transmitted the task for
587 * pdus we do not expect a response for.
588 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500589 atomic_set(&task->refcount, 1);
590 task->conn = conn;
591 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500592 INIT_LIST_HEAD(&task->running);
593 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600594
595 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500596 memcpy(task->data, data, data_size);
597 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600598 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500599 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600600
Mike Christie184b57c2009-05-13 17:57:39 -0500601 if (conn->session->tt->alloc_pdu) {
602 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
603 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
604 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500605 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500606 }
Mike Christie577577d2008-12-02 00:32:05 -0600607 }
Mike Christie184b57c2009-05-13 17:57:39 -0500608
Mike Christie262ef632008-12-02 00:32:13 -0600609 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600610 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500611 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600612
613 if (hdr->itt != RESERVED_ITT) {
614 if (session->tt->parse_pdu_itt)
615 task->hdr->itt = itt;
616 else
617 task->hdr->itt = build_itt(task->itt,
618 task->conn->session->age);
619 }
620
Mike Christie1336aed2009-05-13 17:57:48 -0500621 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600622 if (iscsi_prep_mgmt_task(conn, task))
623 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500624
Mike Christie9c19a7d2008-05-21 15:54:09 -0500625 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600626 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500627 } else {
628 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600629 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500630 }
Mike Christie052d0142008-05-21 15:54:05 -0500631
Mike Christie9c19a7d2008-05-21 15:54:09 -0500632 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600633
634free_task:
635 __iscsi_put_task(task);
636 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600637}
638
639int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
640 char *data, uint32_t data_size)
641{
642 struct iscsi_conn *conn = cls_conn->dd_data;
643 struct iscsi_session *session = conn->session;
644 int err = 0;
645
646 spin_lock_bh(&session->lock);
647 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
648 err = -EPERM;
649 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600650 return err;
651}
652EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
653
Mike Christie7996a772006-04-06 21:13:41 -0500654/**
655 * iscsi_cmd_rsp - SCSI Command Response processing
656 * @conn: iscsi connection
657 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500658 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500659 * @data: cmd data buffer
660 * @datalen: len of buffer
661 *
662 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500663 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500664 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500665static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500666 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500667 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500668{
Mike Christie7996a772006-04-06 21:13:41 -0500669 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
670 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500671 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500672
Mike Christie77a23c22007-05-30 12:57:18 -0500673 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500674 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
675
676 sc->result = (DID_OK << 16) | rhdr->cmd_status;
677
678 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
679 sc->result = DID_ERROR << 16;
680 goto out;
681 }
682
683 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600684 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500685
686 if (datalen < 2) {
687invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600688 iscsi_conn_printk(KERN_ERR, conn,
689 "Got CHECK_CONDITION but invalid data "
690 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500691 sc->result = DID_BAD_TARGET << 16;
692 goto out;
693 }
694
Harvey Harrison8f3339912008-05-21 15:54:20 -0500695 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500696 if (datalen < senselen)
697 goto invalid_datalen;
698
699 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600700 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600701 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
702 min_t(uint16_t, senselen,
703 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500704 }
705
Boaz Harroshc07d4442008-04-18 10:11:52 -0500706 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
707 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
708 int res_count = be32_to_cpu(rhdr->bi_residual_count);
709
710 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
711 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
712 res_count <= scsi_in(sc)->length))
713 scsi_in(sc)->resid = res_count;
714 else
715 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
716 }
717
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600718 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
719 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500720 int res_count = be32_to_cpu(rhdr->residual_count);
721
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600722 if (res_count > 0 &&
723 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
724 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500725 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900726 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500727 else
728 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500729 }
Mike Christie7996a772006-04-06 21:13:41 -0500730out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500731 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600732 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500733 conn->scsirsp_pdus_cnt++;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500734 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500735}
736
Mike Christie1d9edf02008-09-24 11:46:09 -0500737/**
738 * iscsi_data_in_rsp - SCSI Data-In Response processing
739 * @conn: iscsi connection
740 * @hdr: iscsi pdu
741 * @task: scsi command task
742 **/
743static void
744iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
745 struct iscsi_task *task)
746{
747 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
748 struct scsi_cmnd *sc = task->sc;
749
750 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
751 return;
752
Mike Christieedbc9aa052009-05-13 17:57:42 -0500753 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500754 sc->result = (DID_OK << 16) | rhdr->cmd_status;
755 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
756 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
757 ISCSI_FLAG_DATA_OVERFLOW)) {
758 int res_count = be32_to_cpu(rhdr->residual_count);
759
760 if (res_count > 0 &&
761 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
762 res_count <= scsi_in(sc)->length))
763 scsi_in(sc)->resid = res_count;
764 else
765 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
766 }
767
Mike Christie3bbaaad2009-05-13 17:57:46 -0500768 ISCSI_DBG_SESSION(conn->session, "data in with status done "
769 "[sc %p res %d itt 0x%x]\n",
770 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500771 conn->scsirsp_pdus_cnt++;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500772 iscsi_complete_task(task);
Mike Christie1d9edf02008-09-24 11:46:09 -0500773}
774
Mike Christie7ea8b822006-07-24 15:47:22 -0500775static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
776{
777 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
778
779 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
780 conn->tmfrsp_pdus_cnt++;
781
Mike Christie843c0a82007-12-13 12:43:20 -0600782 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500783 return;
784
785 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600786 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500787 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600788 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500789 else
Mike Christie843c0a82007-12-13 12:43:20 -0600790 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500791 wake_up(&conn->ehwait);
792}
793
Mike Christief6d51802007-12-13 12:43:30 -0600794static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
795{
796 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500797 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600798
Mike Christie9c19a7d2008-05-21 15:54:09 -0500799 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600800 return;
801
802 memset(&hdr, 0, sizeof(struct iscsi_nopout));
803 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
804 hdr.flags = ISCSI_FLAG_CMD_FINAL;
805
806 if (rhdr) {
807 memcpy(hdr.lun, rhdr->lun, 8);
808 hdr.ttt = rhdr->ttt;
809 hdr.itt = RESERVED_ITT;
810 } else
811 hdr.ttt = RESERVED_ITT;
812
Mike Christie9c19a7d2008-05-21 15:54:09 -0500813 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
814 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600815 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600816 else if (!rhdr) {
817 /* only track our nops */
818 conn->ping_task = task;
819 conn->last_ping = jiffies;
820 }
Mike Christief6d51802007-12-13 12:43:30 -0600821}
822
Mike Christie62f38302006-08-31 18:09:27 -0400823static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
824 char *data, int datalen)
825{
826 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
827 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400828
829 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
830
831 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
832 if (ntoh24(reject->dlength) > datalen)
833 return ISCSI_ERR_PROTO;
834
835 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
836 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600837 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600838 "pdu (op 0x%x) rejected "
839 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600840 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400841 }
842 }
843 return 0;
844}
845
Mike Christie7996a772006-04-06 21:13:41 -0500846/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500847 * iscsi_itt_to_task - look up task by itt
848 * @conn: iscsi connection
849 * @itt: itt
850 *
851 * This should be used for mgmt tasks like login and nops, or if
852 * the LDD's itt space does not include the session age.
853 *
854 * The session lock must be held.
855 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500856struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500857{
858 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600859 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500860
861 if (itt == RESERVED_ITT)
862 return NULL;
863
Mike Christie262ef632008-12-02 00:32:13 -0600864 if (session->tt->parse_pdu_itt)
865 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
866 else
867 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500868 if (i >= session->cmds_max)
869 return NULL;
870
871 return session->cmds[i];
872}
Mike Christie8f9256c2009-05-13 17:57:41 -0500873EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -0500874
875/**
Mike Christie7996a772006-04-06 21:13:41 -0500876 * __iscsi_complete_pdu - complete pdu
877 * @conn: iscsi conn
878 * @hdr: iscsi header
879 * @data: data buffer
880 * @datalen: len of data buffer
881 *
882 * Completes pdu processing by freeing any resources allocated at
883 * queuecommand or send generic. session lock must be held and verify
884 * itt must have been called.
885 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500886int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
887 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500888{
889 struct iscsi_session *session = conn->session;
890 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500891 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500892 uint32_t itt;
893
Mike Christief6d51802007-12-13 12:43:30 -0600894 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500895 rc = iscsi_verify_itt(conn, hdr->itt);
896 if (rc)
897 return rc;
898
Al Virob4377352007-02-09 16:39:40 +0000899 if (hdr->itt != RESERVED_ITT)
900 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500901 else
Al Virob4377352007-02-09 16:39:40 +0000902 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500903
Mike Christie1b2c7af2009-03-05 14:45:58 -0600904 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
905 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500906
Mike Christie3e5c28a2008-05-21 15:54:06 -0500907 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500908 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400909
Mike Christie7996a772006-04-06 21:13:41 -0500910 switch(opcode) {
911 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500912 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500913 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500914 break;
915 }
916
Al Virob4377352007-02-09 16:39:40 +0000917 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500918 break;
919
Mike Christief6d51802007-12-13 12:43:30 -0600920 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500921 break;
922 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400923 rc = iscsi_handle_reject(conn, hdr, data, datalen);
924 break;
Mike Christie7996a772006-04-06 21:13:41 -0500925 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500926 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400927 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
928 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500929 break;
930 default:
931 rc = ISCSI_ERR_BAD_OPCODE;
932 break;
933 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500934 goto out;
935 }
Mike Christie7996a772006-04-06 21:13:41 -0500936
Mike Christie3e5c28a2008-05-21 15:54:06 -0500937 switch(opcode) {
938 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500939 case ISCSI_OP_SCSI_DATA_IN:
940 task = iscsi_itt_to_ctask(conn, hdr->itt);
941 if (!task)
942 return ISCSI_ERR_BAD_ITT;
943 break;
944 case ISCSI_OP_R2T:
945 /*
946 * LLD handles R2Ts if they need to.
947 */
948 return 0;
949 case ISCSI_OP_LOGOUT_RSP:
950 case ISCSI_OP_LOGIN_RSP:
951 case ISCSI_OP_TEXT_RSP:
952 case ISCSI_OP_SCSI_TMFUNC_RSP:
953 case ISCSI_OP_NOOP_IN:
954 task = iscsi_itt_to_task(conn, hdr->itt);
955 if (!task)
956 return ISCSI_ERR_BAD_ITT;
957 break;
958 default:
959 return ISCSI_ERR_BAD_OPCODE;
960 }
961
962 switch(opcode) {
963 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500964 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500965 break;
966 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500967 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500968 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500969 case ISCSI_OP_LOGOUT_RSP:
970 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
971 if (datalen) {
972 rc = ISCSI_ERR_PROTO;
973 break;
974 }
975 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
976 goto recv_pdu;
977 case ISCSI_OP_LOGIN_RSP:
978 case ISCSI_OP_TEXT_RSP:
979 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
980 /*
981 * login related PDU's exp_statsn is handled in
982 * userspace
983 */
984 goto recv_pdu;
985 case ISCSI_OP_SCSI_TMFUNC_RSP:
986 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
987 if (datalen) {
988 rc = ISCSI_ERR_PROTO;
989 break;
990 }
991
992 iscsi_tmf_rsp(conn, hdr);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500993 iscsi_complete_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500994 break;
995 case ISCSI_OP_NOOP_IN:
996 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
997 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
998 rc = ISCSI_ERR_PROTO;
999 break;
1000 }
1001 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1002
Mike Christie9c19a7d2008-05-21 15:54:09 -05001003 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -05001004 /*
1005 * If this is not in response to one of our
1006 * nops then it must be from userspace.
1007 */
1008 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001009
1010 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001011 iscsi_complete_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001012 break;
1013 default:
1014 rc = ISCSI_ERR_BAD_OPCODE;
1015 break;
1016 }
1017
1018out:
1019 return rc;
1020recv_pdu:
1021 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1022 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001023 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05001024 return rc;
1025}
Mike Christie913e5bf2008-05-21 15:54:18 -05001026EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001027
1028int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1029 char *data, int datalen)
1030{
1031 int rc;
1032
1033 spin_lock(&conn->session->lock);
1034 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1035 spin_unlock(&conn->session->lock);
1036 return rc;
1037}
1038EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1039
Mike Christie0af967f2008-05-21 15:54:04 -05001040int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001041{
1042 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001043 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001044
Mike Christie0af967f2008-05-21 15:54:04 -05001045 if (itt == RESERVED_ITT)
1046 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001047
Mike Christie262ef632008-12-02 00:32:13 -06001048 if (session->tt->parse_pdu_itt)
1049 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1050 else {
1051 i = get_itt(itt);
1052 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1053 }
1054
1055 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001056 iscsi_conn_printk(KERN_ERR, conn,
1057 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001058 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001059 return ISCSI_ERR_BAD_ITT;
1060 }
Mike Christie7996a772006-04-06 21:13:41 -05001061
Mike Christie3e5c28a2008-05-21 15:54:06 -05001062 if (i >= session->cmds_max) {
1063 iscsi_conn_printk(KERN_ERR, conn,
1064 "received invalid itt index %u (max cmds "
1065 "%u.\n", i, session->cmds_max);
1066 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001067 }
Mike Christie7996a772006-04-06 21:13:41 -05001068 return 0;
1069}
1070EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1071
Mike Christie913e5bf2008-05-21 15:54:18 -05001072/**
1073 * iscsi_itt_to_ctask - look up ctask by itt
1074 * @conn: iscsi connection
1075 * @itt: itt
1076 *
1077 * This should be used for cmd tasks.
1078 *
1079 * The session lock must be held.
1080 */
1081struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001082{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001083 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001084
1085 if (iscsi_verify_itt(conn, itt))
1086 return NULL;
1087
Mike Christie913e5bf2008-05-21 15:54:18 -05001088 task = iscsi_itt_to_task(conn, itt);
1089 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001090 return NULL;
1091
Mike Christie913e5bf2008-05-21 15:54:18 -05001092 if (task->sc->SCp.phase != conn->session->age) {
1093 iscsi_session_printk(KERN_ERR, conn->session,
1094 "task's session age %d, expected %d\n",
1095 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001096 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001097 }
Mike Christie0af967f2008-05-21 15:54:04 -05001098
Mike Christie9c19a7d2008-05-21 15:54:09 -05001099 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001100}
1101EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1102
Mike Christie40a06e72009-03-05 14:46:05 -06001103void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001104 enum iscsi_err err)
1105{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001106 struct iscsi_conn *conn;
1107 struct device *dev;
1108 unsigned long flags;
1109
1110 spin_lock_irqsave(&session->lock, flags);
1111 conn = session->leadconn;
1112 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1113 spin_unlock_irqrestore(&session->lock, flags);
1114 return;
1115 }
1116
1117 dev = get_device(&conn->cls_conn->dev);
1118 spin_unlock_irqrestore(&session->lock, flags);
1119 if (!dev)
1120 return;
1121 /*
1122 * if the host is being removed bypass the connection
1123 * recovery initialization because we are going to kill
1124 * the session.
1125 */
1126 if (err == ISCSI_ERR_INVALID_HOST)
1127 iscsi_conn_error_event(conn->cls_conn, err);
1128 else
1129 iscsi_conn_failure(conn, err);
1130 put_device(dev);
1131}
1132EXPORT_SYMBOL_GPL(iscsi_session_failure);
1133
Mike Christie7996a772006-04-06 21:13:41 -05001134void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1135{
1136 struct iscsi_session *session = conn->session;
1137 unsigned long flags;
1138
1139 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001140 if (session->state == ISCSI_STATE_FAILED) {
1141 spin_unlock_irqrestore(&session->lock, flags);
1142 return;
1143 }
1144
Mike Christie67a61112006-05-30 00:37:20 -05001145 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001146 session->state = ISCSI_STATE_FAILED;
1147 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001148
Mike Christie7996a772006-04-06 21:13:41 -05001149 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1150 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001151 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001152}
1153EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1154
Mike Christie77a23c22007-05-30 12:57:18 -05001155static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1156{
1157 struct iscsi_session *session = conn->session;
1158
1159 /*
1160 * Check for iSCSI window and take care of CmdSN wrap-around
1161 */
Mike Christiee0726402007-07-26 12:46:48 -05001162 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001163 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1164 "%u MaxCmdSN %u CmdSN %u/%u\n",
1165 session->exp_cmdsn, session->max_cmdsn,
1166 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001167 return -ENOSPC;
1168 }
1169 return 0;
1170}
1171
Mike Christie9c19a7d2008-05-21 15:54:09 -05001172static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001173{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001174 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001175 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001176
Mike Christie9c19a7d2008-05-21 15:54:09 -05001177 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001178 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001179 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001180 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001181 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001182 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001183 /* done with this task */
1184 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001185 return rc;
1186}
1187
Mike Christie7996a772006-04-06 21:13:41 -05001188/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001189 * iscsi_requeue_task - requeue task to run from session workqueue
1190 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001191 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001192 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001193 * this. The session lock must be held. This should only be called
1194 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001195 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001196void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001197{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001198 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001199
Mike Christie3bbaaad2009-05-13 17:57:46 -05001200 /*
1201 * this may be on the requeue list already if the xmit_task callout
1202 * is handling the r2ts while we are adding new ones
1203 */
1204 if (list_empty(&task->running))
1205 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001206 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001207}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001208EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001209
1210/**
Mike Christie7996a772006-04-06 21:13:41 -05001211 * iscsi_data_xmit - xmit any command into the scheduled connection
1212 * @conn: iscsi connection
1213 *
1214 * Notes:
1215 * The function can return -EAGAIN in which case the caller must
1216 * re-schedule it again later or recover. '0' return code means
1217 * successful xmit.
1218 **/
1219static int iscsi_data_xmit(struct iscsi_conn *conn)
1220{
Mike Christie3219e522006-05-30 00:37:28 -05001221 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001222
Mike Christie77a23c22007-05-30 12:57:18 -05001223 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001224 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001225 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001226 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001227 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001228 }
Mike Christie7996a772006-04-06 21:13:41 -05001229
Mike Christie9c19a7d2008-05-21 15:54:09 -05001230 if (conn->task) {
1231 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001232 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001233 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001234 }
1235
Mike Christie77a23c22007-05-30 12:57:18 -05001236 /*
1237 * process mgmt pdus like nops before commands since we should
1238 * only have one nop-out as a ping from us and targets should not
1239 * overflow us with nop-ins
1240 */
1241check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001242 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001243 conn->task = list_entry(conn->mgmtqueue.next,
1244 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001245 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001246 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1247 __iscsi_put_task(conn->task);
1248 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001249 continue;
1250 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001251 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001252 if (rc)
1253 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001254 }
1255
Mike Christie843c0a82007-12-13 12:43:20 -06001256 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001257 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001258 if (conn->tmf_state == TMF_QUEUED)
1259 break;
1260
Mike Christie3bbaaad2009-05-13 17:57:46 -05001261 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001262 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001263 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001264 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001265 fail_scsi_task(conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001266 continue;
1267 }
Mike Christie577577d2008-12-02 00:32:05 -06001268 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1269 if (rc) {
1270 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001271 list_add_tail(&conn->task->running,
1272 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001273 conn->task = NULL;
1274 goto again;
1275 } else
Mike Christie3bbaaad2009-05-13 17:57:46 -05001276 fail_scsi_task(conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001277 continue;
1278 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001279 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001280 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001281 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001282 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001283 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001284 * we need to check the mgmt queue for nops that need to
1285 * be sent to aviod starvation
1286 */
Mike Christie843c0a82007-12-13 12:43:20 -06001287 if (!list_empty(&conn->mgmtqueue))
1288 goto check_mgmt;
1289 }
1290
1291 while (!list_empty(&conn->requeue)) {
1292 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1293 break;
1294
Mike Christieb3a7ea82007-12-13 12:43:26 -06001295 /*
1296 * we always do fastlogout - conn stop code will clean up.
1297 */
1298 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1299 break;
1300
Mike Christie9c19a7d2008-05-21 15:54:09 -05001301 conn->task = list_entry(conn->requeue.next,
1302 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001303 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001304 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001305 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001306 if (rc)
1307 goto again;
1308 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001309 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001310 }
Mike Christieb6c395ed2006-07-24 15:47:15 -05001311 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001312 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001313
1314again:
1315 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001316 rc = -ENODATA;
1317 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001318 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001319}
1320
David Howellsc4028952006-11-22 14:57:56 +00001321static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001322{
David Howellsc4028952006-11-22 14:57:56 +00001323 struct iscsi_conn *conn =
1324 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001325 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001326 /*
1327 * serialize Xmit worker on a per-connection basis.
1328 */
Mike Christie3219e522006-05-30 00:37:28 -05001329 do {
1330 rc = iscsi_data_xmit(conn);
1331 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001332}
1333
Mike Christie577577d2008-12-02 00:32:05 -06001334static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1335 struct scsi_cmnd *sc)
1336{
1337 struct iscsi_task *task;
1338
1339 if (!__kfifo_get(conn->session->cmdpool.queue,
1340 (void *) &task, sizeof(void *)))
1341 return NULL;
1342
1343 sc->SCp.phase = conn->session->age;
1344 sc->SCp.ptr = (char *) task;
1345
1346 atomic_set(&task->refcount, 1);
1347 task->state = ISCSI_TASK_PENDING;
1348 task->conn = conn;
1349 task->sc = sc;
1350 INIT_LIST_HEAD(&task->running);
1351 return task;
1352}
1353
Mike Christie7996a772006-04-06 21:13:41 -05001354enum {
1355 FAILURE_BAD_HOST = 1,
1356 FAILURE_SESSION_FAILED,
1357 FAILURE_SESSION_FREED,
1358 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001359 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001360 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001361 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001362 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001363 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001364 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001365};
1366
1367int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1368{
Mike Christie75613522008-05-21 15:53:59 -05001369 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001370 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001371 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001372 int reason = 0;
1373 struct iscsi_session *session;
1374 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001375 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001376
1377 sc->scsi_done = done;
1378 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001379 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001380
1381 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001382 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001383 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001384
Mike Christie75613522008-05-21 15:53:59 -05001385 cls_session = starget_to_session(scsi_target(sc->device));
1386 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001387 spin_lock(&session->lock);
1388
Mike Christie75613522008-05-21 15:53:59 -05001389 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001390 if (reason) {
1391 sc->result = reason;
1392 goto fault;
1393 }
1394
Mike Christie301e0f7e2009-05-13 17:57:47 -05001395 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001396 /*
1397 * to handle the race between when we set the recovery state
1398 * and block the session we requeue here (commands could
1399 * be entering our queuecommand while a block is starting
1400 * up because the block code is not locked)
1401 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001402 switch (session->state) {
Mike Christie301e0f7e2009-05-13 17:57:47 -05001403 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001404 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001405 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f7e2009-05-13 17:57:47 -05001406 sc->result = DID_IMM_RETRY << 16;
1407 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001408 case ISCSI_STATE_LOGGING_OUT:
1409 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f7e2009-05-13 17:57:47 -05001410 sc->result = DID_IMM_RETRY << 16;
1411 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001412 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001413 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001414 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001415 break;
1416 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001417 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001418 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001419 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001420 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001421 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001422 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001423 }
Mike Christie7996a772006-04-06 21:13:41 -05001424 goto fault;
1425 }
1426
Mike Christie7996a772006-04-06 21:13:41 -05001427 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001428 if (!conn) {
1429 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001430 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001431 goto fault;
1432 }
Mike Christie7996a772006-04-06 21:13:41 -05001433
Mike Christie77a23c22007-05-30 12:57:18 -05001434 if (iscsi_check_cmdsn_window_closed(conn)) {
1435 reason = FAILURE_WINDOW_CLOSED;
1436 goto reject;
1437 }
1438
Mike Christie577577d2008-12-02 00:32:05 -06001439 task = iscsi_alloc_task(conn, sc);
1440 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001441 reason = FAILURE_OOM;
1442 goto reject;
1443 }
Mike Christie7996a772006-04-06 21:13:41 -05001444
Mike Christie1336aed2009-05-13 17:57:48 -05001445 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001446 reason = iscsi_prep_scsi_cmd_pdu(task);
1447 if (reason) {
1448 if (reason == -ENOMEM) {
1449 reason = FAILURE_OOM;
1450 goto prepd_reject;
1451 } else {
1452 sc->result = DID_ABORT << 16;
1453 goto prepd_fault;
1454 }
Mike Christie052d0142008-05-21 15:54:05 -05001455 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001456 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001457 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001458 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001459 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001460 } else {
1461 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001462 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001463 }
Mike Christie052d0142008-05-21 15:54:05 -05001464
1465 session->queued_cmdsn++;
1466 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001467 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001468 return 0;
1469
Mike Christie577577d2008-12-02 00:32:05 -06001470prepd_reject:
1471 sc->scsi_done = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001472 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05001473reject:
1474 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001475 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1476 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001477 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001478 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001479
Mike Christie577577d2008-12-02 00:32:05 -06001480prepd_fault:
1481 sc->scsi_done = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001482 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05001483fault:
1484 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001485 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1486 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001487 if (!scsi_bidi_cmnd(sc))
1488 scsi_set_resid(sc, scsi_bufflen(sc));
1489 else {
1490 scsi_out(sc)->resid = scsi_out(sc)->length;
1491 scsi_in(sc)->resid = scsi_in(sc)->length;
1492 }
Mike Christie052d0142008-05-21 15:54:05 -05001493 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001494 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001495 return 0;
1496}
1497EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1498
1499int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1500{
Mike Christie7996a772006-04-06 21:13:41 -05001501 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1502 return sdev->queue_depth;
1503}
1504EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1505
Mike Christie6b5d6c42009-04-21 15:32:32 -05001506int iscsi_target_alloc(struct scsi_target *starget)
1507{
1508 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1509 struct iscsi_session *session = cls_session->dd_data;
1510
1511 starget->can_queue = session->scsi_cmds_max;
1512 return 0;
1513}
1514EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1515
Mike Christie7996a772006-04-06 21:13:41 -05001516void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1517{
Mike Christie75613522008-05-21 15:53:59 -05001518 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001519
1520 spin_lock_bh(&session->lock);
1521 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001522 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001523 if (session->leadconn)
1524 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001525 }
1526 spin_unlock_bh(&session->lock);
1527}
1528EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1529
Mike Christie8e124522008-09-24 11:46:12 -05001530int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001531{
Mike Christie75613522008-05-21 15:53:59 -05001532 struct iscsi_cls_session *cls_session;
1533 struct iscsi_session *session;
1534 struct iscsi_conn *conn;
1535
1536 cls_session = starget_to_session(scsi_target(sc->device));
1537 session = cls_session->dd_data;
1538 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001539
Mike Christiebc436b22007-12-13 12:43:28 -06001540 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001541 spin_lock_bh(&session->lock);
1542 if (session->state == ISCSI_STATE_TERMINATE) {
1543failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001544 iscsi_session_printk(KERN_INFO, session,
1545 "failing target reset: Could not log "
1546 "back into target [age %d]\n",
1547 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001548 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001549 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001550 return FAILED;
1551 }
1552
Mike Christie7996a772006-04-06 21:13:41 -05001553 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001554 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001555 /*
1556 * we drop the lock here but the leadconn cannot be destoyed while
1557 * we are in the scsi eh
1558 */
Mike Christie843c0a82007-12-13 12:43:20 -06001559 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001560
Mike Christie1b2c7af2009-03-05 14:45:58 -06001561 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001562 wait_event_interruptible(conn->ehwait,
1563 session->state == ISCSI_STATE_TERMINATE ||
1564 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001565 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001566 if (signal_pending(current))
1567 flush_signals(current);
1568
Mike Christiebc436b22007-12-13 12:43:28 -06001569 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001570 spin_lock_bh(&session->lock);
1571 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001572 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001573 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001574 else
1575 goto failed;
1576 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001577 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001578 return SUCCESS;
1579}
Mike Christie8e124522008-09-24 11:46:12 -05001580EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001581
Mike Christie843c0a82007-12-13 12:43:20 -06001582static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001583{
Mike Christie843c0a82007-12-13 12:43:20 -06001584 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001585 struct iscsi_session *session = conn->session;
1586
1587 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001588 if (conn->tmf_state == TMF_QUEUED) {
1589 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001590 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001591 /* unblock eh_abort() */
1592 wake_up(&conn->ehwait);
1593 }
1594 spin_unlock(&session->lock);
1595}
1596
Mike Christie9c19a7d2008-05-21 15:54:09 -05001597static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001598 struct iscsi_tm *hdr, int age,
1599 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001600{
Mike Christie7996a772006-04-06 21:13:41 -05001601 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001602 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001603
Mike Christie9c19a7d2008-05-21 15:54:09 -05001604 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001605 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001606 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001607 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001608 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001609 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001610 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001611 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001612 }
Mike Christie843c0a82007-12-13 12:43:20 -06001613 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001614 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001615 conn->tmf_timer.function = iscsi_tmf_timedout;
1616 conn->tmf_timer.data = (unsigned long)conn;
1617 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001618 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001619
Mike Christie7996a772006-04-06 21:13:41 -05001620 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001621 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001622
1623 /*
1624 * block eh thread until:
1625 *
Mike Christie843c0a82007-12-13 12:43:20 -06001626 * 1) tmf response
1627 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001628 * 3) session is terminated or restarted or userspace has
1629 * given up on recovery
1630 */
Mike Christie843c0a82007-12-13 12:43:20 -06001631 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001632 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001633 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001634 if (signal_pending(current))
1635 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001636 del_timer_sync(&conn->tmf_timer);
1637
Mike Christie6724add2007-08-15 01:38:30 -05001638 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001639 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001640 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001641 if (age != session->age ||
1642 session->state != ISCSI_STATE_LOGGED_IN)
1643 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001644 return 0;
1645}
1646
1647/*
Mike Christie843c0a82007-12-13 12:43:20 -06001648 * Fail commands. session lock held and recv side suspended and xmit
1649 * thread flushed
1650 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001651static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1652 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001653{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001654 struct iscsi_task *task;
1655 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001656
Mike Christie3bbaaad2009-05-13 17:57:46 -05001657 for (i = 0; i < conn->session->cmds_max; i++) {
1658 task = conn->session->cmds[i];
1659 if (!task->sc || task->state == ISCSI_TASK_FREE)
1660 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001661
Mike Christie3bbaaad2009-05-13 17:57:46 -05001662 if (lun != -1 && lun != task->sc->device->lun)
1663 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001664
Mike Christie3bbaaad2009-05-13 17:57:46 -05001665 ISCSI_DBG_SESSION(conn->session,
1666 "failing sc %p itt 0x%x state %d\n",
1667 task->sc, task->itt, task->state);
1668 fail_scsi_task(task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001669 }
1670}
1671
Mike Christieb40977d2008-05-21 15:54:03 -05001672void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001673{
Mike Christie32ae7632009-03-05 14:46:03 -06001674 struct Scsi_Host *shost = conn->session->host;
1675 struct iscsi_host *ihost = shost_priv(shost);
1676
Mike Christie6724add2007-08-15 01:38:30 -05001677 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001678 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001679 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001680}
Mike Christieb40977d2008-05-21 15:54:03 -05001681EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001682
1683static void iscsi_start_tx(struct iscsi_conn *conn)
1684{
1685 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001686 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001687}
1688
Mike Christie4c48a822009-05-13 17:57:45 -05001689/*
1690 * We want to make sure a ping is in flight. It has timed out.
1691 * And we are not busy processing a pdu that is making
1692 * progress but got started before the ping and is taking a while
1693 * to complete so the ping is just stuck behind it in a queue.
1694 */
1695static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1696{
1697 if (conn->ping_task &&
1698 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1699 (conn->ping_timeout * HZ), jiffies))
1700 return 1;
1701 else
1702 return 0;
1703}
1704
Jens Axboe242f9dc2008-09-14 05:55:09 -07001705static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001706{
1707 struct iscsi_cls_session *cls_session;
1708 struct iscsi_session *session;
1709 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001710 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001711
1712 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001713 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001714
Mike Christie1b2c7af2009-03-05 14:45:58 -06001715 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
Mike Christief6d51802007-12-13 12:43:30 -06001716
1717 spin_lock(&session->lock);
1718 if (session->state != ISCSI_STATE_LOGGED_IN) {
1719 /*
1720 * We are probably in the middle of iscsi recovery so let
1721 * that complete and handle the error.
1722 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001723 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001724 goto done;
1725 }
1726
1727 conn = session->leadconn;
1728 if (!conn) {
1729 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001730 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001731 goto done;
1732 }
1733
1734 if (!conn->recv_timeout && !conn->ping_timeout)
1735 goto done;
1736 /*
1737 * if the ping timedout then we are in the middle of cleaning up
1738 * and can let the iscsi eh handle it
1739 */
Mike Christie4c48a822009-05-13 17:57:45 -05001740 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001741 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001742 goto done;
1743 }
Mike Christief6d51802007-12-13 12:43:30 -06001744 /*
1745 * if we are about to check the transport then give the command
1746 * more time
1747 */
1748 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
Mike Christie4c48a822009-05-13 17:57:45 -05001749 jiffies)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001750 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001751 goto done;
1752 }
1753
Mike Christief6d51802007-12-13 12:43:30 -06001754 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001755 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001756 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001757done:
1758 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001759 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1760 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001761 return rc;
1762}
1763
1764static void iscsi_check_transport_timeouts(unsigned long data)
1765{
1766 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1767 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001768 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001769
1770 spin_lock(&session->lock);
1771 if (session->state != ISCSI_STATE_LOGGED_IN)
1772 goto done;
1773
Mike Christie4cf10432008-05-07 20:43:52 -05001774 recv_timeout = conn->recv_timeout;
1775 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001776 goto done;
1777
Mike Christie4cf10432008-05-07 20:43:52 -05001778 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001779 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001780
1781 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001782 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001783 "expired, recv timeout %d, last rx %lu, "
1784 "last ping %lu, now %lu\n",
1785 conn->ping_timeout, conn->recv_timeout,
1786 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001787 spin_unlock(&session->lock);
1788 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1789 return;
1790 }
1791
Mike Christie4cf10432008-05-07 20:43:52 -05001792 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001793 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001794 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001795 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001796 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001797 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001798 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001799
Mike Christie1b2c7af2009-03-05 14:45:58 -06001800 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001801 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001802done:
1803 spin_unlock(&session->lock);
1804}
1805
Mike Christie9c19a7d2008-05-21 15:54:09 -05001806static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001807 struct iscsi_tm *hdr)
1808{
1809 memset(hdr, 0, sizeof(*hdr));
1810 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1811 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1812 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001813 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1814 hdr->rtt = task->hdr_itt;
1815 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001816}
1817
Mike Christie7996a772006-04-06 21:13:41 -05001818int iscsi_eh_abort(struct scsi_cmnd *sc)
1819{
Mike Christie75613522008-05-21 15:53:59 -05001820 struct iscsi_cls_session *cls_session;
1821 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001822 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001823 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001824 struct iscsi_tm *hdr;
1825 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001826
Mike Christie75613522008-05-21 15:53:59 -05001827 cls_session = starget_to_session(scsi_target(sc->device));
1828 session = cls_session->dd_data;
1829
Mike Christie6724add2007-08-15 01:38:30 -05001830 mutex_lock(&session->eh_mutex);
1831 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001832 /*
1833 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1834 * got the command.
1835 */
1836 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001837 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1838 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001839 spin_unlock_bh(&session->lock);
1840 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001841 return SUCCESS;
1842 }
1843
Mike Christie7996a772006-04-06 21:13:41 -05001844 /*
1845 * If we are not logged in or we have started a new session
1846 * then let the host reset code handle this
1847 */
Mike Christie843c0a82007-12-13 12:43:20 -06001848 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1849 sc->SCp.phase != session->age) {
1850 spin_unlock_bh(&session->lock);
1851 mutex_unlock(&session->eh_mutex);
1852 return FAILED;
1853 }
1854
1855 conn = session->leadconn;
1856 conn->eh_abort_cnt++;
1857 age = session->age;
1858
Mike Christie9c19a7d2008-05-21 15:54:09 -05001859 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001860 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1861 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001862
Mike Christie9c19a7d2008-05-21 15:54:09 -05001863 /* task completed before time out */
1864 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001865 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1866 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001867 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001868 }
Mike Christie7996a772006-04-06 21:13:41 -05001869
Mike Christie9c19a7d2008-05-21 15:54:09 -05001870 if (task->state == ISCSI_TASK_PENDING) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001871 fail_scsi_task(task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001872 goto success;
1873 }
Mike Christie7996a772006-04-06 21:13:41 -05001874
Mike Christie843c0a82007-12-13 12:43:20 -06001875 /* only have one tmf outstanding at a time */
1876 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001877 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001878 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001879
Mike Christie843c0a82007-12-13 12:43:20 -06001880 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001881 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001882
Mike Christie9c19a7d2008-05-21 15:54:09 -05001883 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001884 rc = FAILED;
1885 goto failed;
1886 }
1887
1888 switch (conn->tmf_state) {
1889 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001890 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001891 /*
1892 * stop tx side incase the target had sent a abort rsp but
1893 * the initiator was still writing out data.
1894 */
Mike Christie6724add2007-08-15 01:38:30 -05001895 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001896 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001897 * we do not stop the recv side because targets have been
1898 * good and have never sent us a successful tmf response
1899 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001900 */
Mike Christie77a23c22007-05-30 12:57:18 -05001901 spin_lock(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001902 fail_scsi_task(task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001903 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001904 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001905 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001906 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001907 case TMF_TIMEDOUT:
1908 spin_unlock_bh(&session->lock);
1909 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1910 goto failed_unlocked;
1911 case TMF_NOT_FOUND:
1912 if (!sc->SCp.ptr) {
1913 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001914 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001915 ISCSI_DBG_SESSION(session, "sc completed while abort "
1916 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001917 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001918 }
1919 /* fall through */
1920 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001921 conn->tmf_state = TMF_INITIAL;
1922 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001923 }
1924
Mike Christie77a23c22007-05-30 12:57:18 -05001925success:
Mike Christie7996a772006-04-06 21:13:41 -05001926 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001927success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001928 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1929 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001930 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001931 return SUCCESS;
1932
1933failed:
1934 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001935failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001936 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1937 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001938 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001939 return FAILED;
1940}
1941EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1942
Mike Christie843c0a82007-12-13 12:43:20 -06001943static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1944{
1945 memset(hdr, 0, sizeof(*hdr));
1946 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1947 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1948 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1949 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001950 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001951}
1952
1953int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1954{
Mike Christie75613522008-05-21 15:53:59 -05001955 struct iscsi_cls_session *cls_session;
1956 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001957 struct iscsi_conn *conn;
1958 struct iscsi_tm *hdr;
1959 int rc = FAILED;
1960
Mike Christie75613522008-05-21 15:53:59 -05001961 cls_session = starget_to_session(scsi_target(sc->device));
1962 session = cls_session->dd_data;
1963
Mike Christie1b2c7af2009-03-05 14:45:58 -06001964 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1965 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06001966
1967 mutex_lock(&session->eh_mutex);
1968 spin_lock_bh(&session->lock);
1969 /*
1970 * Just check if we are not logged in. We cannot check for
1971 * the phase because the reset could come from a ioctl.
1972 */
1973 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1974 goto unlock;
1975 conn = session->leadconn;
1976
1977 /* only have one tmf outstanding at a time */
1978 if (conn->tmf_state != TMF_INITIAL)
1979 goto unlock;
1980 conn->tmf_state = TMF_QUEUED;
1981
1982 hdr = &conn->tmhdr;
1983 iscsi_prep_lun_reset_pdu(sc, hdr);
1984
Mike Christie9c19a7d2008-05-21 15:54:09 -05001985 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001986 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001987 rc = FAILED;
1988 goto unlock;
1989 }
1990
1991 switch (conn->tmf_state) {
1992 case TMF_SUCCESS:
1993 break;
1994 case TMF_TIMEDOUT:
1995 spin_unlock_bh(&session->lock);
1996 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1997 goto done;
1998 default:
1999 conn->tmf_state = TMF_INITIAL;
2000 goto unlock;
2001 }
2002
2003 rc = SUCCESS;
2004 spin_unlock_bh(&session->lock);
2005
2006 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002007
Mike Christiea3439142008-09-24 11:46:15 -05002008 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002009 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002010 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002011 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002012
2013 iscsi_start_tx(conn);
2014 goto done;
2015
2016unlock:
2017 spin_unlock_bh(&session->lock);
2018done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06002019 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
2020 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002021 mutex_unlock(&session->eh_mutex);
2022 return rc;
2023}
2024EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2025
Olaf Kirch63203772007-12-13 12:43:25 -06002026/*
2027 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2028 * should be accessed via kfifo_{get,put} on q->queue.
2029 * Optionally, the caller can obtain the array of object pointers
2030 * by passing in a non-NULL @items pointer
2031 */
Mike Christie7996a772006-04-06 21:13:41 -05002032int
Olaf Kirch63203772007-12-13 12:43:25 -06002033iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002034{
Olaf Kirch63203772007-12-13 12:43:25 -06002035 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002036
Olaf Kirch63203772007-12-13 12:43:25 -06002037 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002038
2039 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002040
2041 /* If the user passed an items pointer, he wants a copy of
2042 * the array. */
2043 if (items)
2044 num_arrays++;
2045 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2046 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002047 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002048
2049 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2050 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002051 if (IS_ERR(q->queue)) {
2052 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002053 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002054 }
Mike Christie7996a772006-04-06 21:13:41 -05002055
2056 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002057 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002058 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002059 q->max = i;
2060 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002061 }
Mike Christie7996a772006-04-06 21:13:41 -05002062 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2063 }
Olaf Kirch63203772007-12-13 12:43:25 -06002064
2065 if (items) {
2066 *items = q->pool + max;
2067 memcpy(*items, q->pool, max * sizeof(void *));
2068 }
2069
Mike Christie7996a772006-04-06 21:13:41 -05002070 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002071
2072enomem:
2073 iscsi_pool_free(q);
2074 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002075}
2076EXPORT_SYMBOL_GPL(iscsi_pool_init);
2077
Olaf Kirch63203772007-12-13 12:43:25 -06002078void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002079{
2080 int i;
2081
2082 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002083 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002084 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002085 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002086}
2087EXPORT_SYMBOL_GPL(iscsi_pool_free);
2088
Mike Christiea4804cd2008-05-21 15:54:00 -05002089/**
2090 * iscsi_host_add - add host to system
2091 * @shost: scsi host
2092 * @pdev: parent device
2093 *
2094 * This should be called by partial offload and software iscsi drivers
2095 * to add a host to the system.
2096 */
2097int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002098{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002099 if (!shost->can_queue)
2100 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2101
Mike Christie4d108352009-03-05 14:46:04 -06002102 if (!shost->cmd_per_lun)
2103 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2104
Mike Christie308cec12009-02-06 12:06:20 -06002105 if (!shost->transportt->eh_timed_out)
2106 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002107 return scsi_add_host(shost, pdev);
2108}
2109EXPORT_SYMBOL_GPL(iscsi_host_add);
2110
2111/**
2112 * iscsi_host_alloc - allocate a host and driver data
2113 * @sht: scsi host template
2114 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002115 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002116 *
2117 * This should be called by partial offload and software iscsi drivers.
2118 * To access the driver specific memory use the iscsi_host_priv() macro.
2119 */
2120struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002121 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002122{
2123 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002124 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002125
2126 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2127 if (!shost)
2128 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002129 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002130
2131 if (xmit_can_sleep) {
2132 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2133 "iscsi_q_%d", shost->host_no);
2134 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2135 if (!ihost->workq)
2136 goto free_host;
2137 }
2138
Mike Christiee5bd7b52008-09-24 11:46:10 -05002139 spin_lock_init(&ihost->lock);
2140 ihost->state = ISCSI_HOST_SETUP;
2141 ihost->num_sessions = 0;
2142 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002143 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002144
2145free_host:
2146 scsi_host_put(shost);
2147 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002148}
Mike Christiea4804cd2008-05-21 15:54:00 -05002149EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002150
Mike Christiee5bd7b52008-09-24 11:46:10 -05002151static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2152{
Mike Christie40a06e72009-03-05 14:46:05 -06002153 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002154}
2155
Mike Christiea4804cd2008-05-21 15:54:00 -05002156/**
2157 * iscsi_host_remove - remove host and sessions
2158 * @shost: scsi host
2159 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002160 * If there are any sessions left, this will initiate the removal and wait
2161 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002162 */
2163void iscsi_host_remove(struct Scsi_Host *shost)
2164{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002165 struct iscsi_host *ihost = shost_priv(shost);
2166 unsigned long flags;
2167
2168 spin_lock_irqsave(&ihost->lock, flags);
2169 ihost->state = ISCSI_HOST_REMOVED;
2170 spin_unlock_irqrestore(&ihost->lock, flags);
2171
2172 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2173 wait_event_interruptible(ihost->session_removal_wq,
2174 ihost->num_sessions == 0);
2175 if (signal_pending(current))
2176 flush_signals(current);
2177
Mike Christiea4804cd2008-05-21 15:54:00 -05002178 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002179 if (ihost->workq)
2180 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002181}
2182EXPORT_SYMBOL_GPL(iscsi_host_remove);
2183
2184void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002185{
2186 struct iscsi_host *ihost = shost_priv(shost);
2187
2188 kfree(ihost->netdev);
2189 kfree(ihost->hwaddress);
2190 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002191 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002192}
Mike Christiea4804cd2008-05-21 15:54:00 -05002193EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002194
Mike Christiee5bd7b52008-09-24 11:46:10 -05002195static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2196{
2197 struct iscsi_host *ihost = shost_priv(shost);
2198 unsigned long flags;
2199
2200 shost = scsi_host_get(shost);
2201 if (!shost) {
2202 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2203 "of session teardown event because host already "
2204 "removed.\n");
2205 return;
2206 }
2207
2208 spin_lock_irqsave(&ihost->lock, flags);
2209 ihost->num_sessions--;
2210 if (ihost->num_sessions == 0)
2211 wake_up(&ihost->session_removal_wq);
2212 spin_unlock_irqrestore(&ihost->lock, flags);
2213 scsi_host_put(shost);
2214}
2215
Mike Christie75613522008-05-21 15:53:59 -05002216/**
2217 * iscsi_session_setup - create iscsi cls session and host and session
2218 * @iscsit: iscsi transport template
2219 * @shost: scsi host
2220 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002221 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002222 * @initial_cmdsn: initial CmdSN
2223 *
2224 * This can be used by software iscsi_transports that allocate
2225 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002226 *
2227 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2228 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2229 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002230 */
2231struct iscsi_cls_session *
2232iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002233 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002234 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002235{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002236 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002237 struct iscsi_session *session;
2238 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002239 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002240 unsigned long flags;
2241
2242 spin_lock_irqsave(&ihost->lock, flags);
2243 if (ihost->state == ISCSI_HOST_REMOVED) {
2244 spin_unlock_irqrestore(&ihost->lock, flags);
2245 return NULL;
2246 }
2247 ihost->num_sessions++;
2248 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002249
2250 if (!total_cmds)
2251 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002252 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002253 * The iscsi layer needs some tasks for nop handling and tmfs,
2254 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2255 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002256 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002257 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2258 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2259 "must be a power of two that is at least %d.\n",
2260 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002261 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002262 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002263
2264 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2265 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2266 "must be a power of 2 less than or equal to %d.\n",
2267 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2268 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2269 }
2270
2271 if (!is_power_of_2(total_cmds)) {
2272 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2273 "must be a power of 2.\n", total_cmds);
2274 total_cmds = rounddown_pow_of_two(total_cmds);
2275 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2276 return NULL;
2277 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2278 total_cmds);
2279 }
2280 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002281
Mike Christie5d91e202008-05-21 15:54:01 -05002282 cls_session = iscsi_alloc_session(shost, iscsit,
2283 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002284 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002285 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002286 session = cls_session->dd_data;
2287 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002288 session->host = shost;
2289 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002290 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002291 session->lu_reset_timeout = 15;
2292 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002293 session->scsi_cmds_max = scsi_cmds;
2294 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002295 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002296 session->exp_cmdsn = initial_cmdsn + 1;
2297 session->max_cmdsn = initial_cmdsn + 1;
2298 session->max_r2t = 1;
2299 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002300 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002301 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002302
2303 /* initialize SCSI PDU commands pool */
2304 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2305 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002306 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002307 goto cmdpool_alloc_fail;
2308
2309 /* pre-format cmds pool with ITT */
2310 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002311 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002312
Mike Christie9c19a7d2008-05-21 15:54:09 -05002313 if (cmd_task_size)
2314 task->dd_data = &task[1];
2315 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002316 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002317 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002318 }
2319
Mike Christief53a88d2006-06-28 12:00:27 -05002320 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002321 goto module_get_fail;
2322
Mike Christie79706342008-05-21 15:54:12 -05002323 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002324 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002325
Mike Christie7996a772006-04-06 21:13:41 -05002326 return cls_session;
2327
2328cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002329 module_put(iscsit->owner);
2330module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002331 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002332cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002333 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002334dec_session_count:
2335 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002336 return NULL;
2337}
2338EXPORT_SYMBOL_GPL(iscsi_session_setup);
2339
2340/**
2341 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002342 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002343 *
Mike Christie75613522008-05-21 15:53:59 -05002344 * The driver must have called iscsi_remove_session before
2345 * calling this.
2346 */
Mike Christie7996a772006-04-06 21:13:41 -05002347void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2348{
Mike Christie75613522008-05-21 15:53:59 -05002349 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002350 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002351 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002352
Olaf Kirch63203772007-12-13 12:43:25 -06002353 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002354
Mike Christieb2c64162007-05-30 12:57:16 -05002355 kfree(session->password);
2356 kfree(session->password_in);
2357 kfree(session->username);
2358 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002359 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002360 kfree(session->initiatorname);
2361 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002362
Mike Christie75613522008-05-21 15:53:59 -05002363 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002364 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002365 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002366}
2367EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2368
2369/**
2370 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2371 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002372 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002373 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002374 */
Mike Christie7996a772006-04-06 21:13:41 -05002375struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002376iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2377 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002378{
Mike Christie75613522008-05-21 15:53:59 -05002379 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002380 struct iscsi_conn *conn;
2381 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002382 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002383
Mike Christie5d91e202008-05-21 15:54:01 -05002384 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2385 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002386 if (!cls_conn)
2387 return NULL;
2388 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002389 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002390
Mike Christie5d91e202008-05-21 15:54:01 -05002391 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002392 conn->session = session;
2393 conn->cls_conn = cls_conn;
2394 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2395 conn->id = conn_idx;
2396 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002397 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002398
2399 init_timer(&conn->transport_timer);
2400 conn->transport_timer.data = (unsigned long)conn;
2401 conn->transport_timer.function = iscsi_check_transport_timeouts;
2402
Mike Christie843c0a82007-12-13 12:43:20 -06002403 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002404 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002405 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002406 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002407
Mike Christie9c19a7d2008-05-21 15:54:09 -05002408 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002409 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002410 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002411 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002412 sizeof(void*))) {
2413 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002414 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002415 }
2416 spin_unlock_bh(&session->lock);
2417
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002418 data = (char *) __get_free_pages(GFP_KERNEL,
2419 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002420 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002421 goto login_task_data_alloc_fail;
2422 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002423
Mike Christie843c0a82007-12-13 12:43:20 -06002424 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002425 init_waitqueue_head(&conn->ehwait);
2426
2427 return cls_conn;
2428
Mike Christie9c19a7d2008-05-21 15:54:09 -05002429login_task_data_alloc_fail:
2430 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002431 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002432login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002433 iscsi_destroy_conn(cls_conn);
2434 return NULL;
2435}
2436EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2437
2438/**
2439 * iscsi_conn_teardown - teardown iscsi connection
2440 * cls_conn: iscsi class connection
2441 *
2442 * TODO: we may need to make this into a two step process
2443 * like scsi-mls remove + put host
2444 */
2445void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2446{
2447 struct iscsi_conn *conn = cls_conn->dd_data;
2448 struct iscsi_session *session = conn->session;
2449 unsigned long flags;
2450
Mike Christief6d51802007-12-13 12:43:30 -06002451 del_timer_sync(&conn->transport_timer);
2452
Mike Christie7996a772006-04-06 21:13:41 -05002453 spin_lock_bh(&session->lock);
2454 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2455 if (session->leadconn == conn) {
2456 /*
2457 * leading connection? then give up on recovery.
2458 */
2459 session->state = ISCSI_STATE_TERMINATE;
2460 wake_up(&conn->ehwait);
2461 }
2462 spin_unlock_bh(&session->lock);
2463
Mike Christie7996a772006-04-06 21:13:41 -05002464 /*
2465 * Block until all in-progress commands for this connection
2466 * time out or fail.
2467 */
2468 for (;;) {
2469 spin_lock_irqsave(session->host->host_lock, flags);
2470 if (!session->host->host_busy) { /* OK for ERL == 0 */
2471 spin_unlock_irqrestore(session->host->host_lock, flags);
2472 break;
2473 }
2474 spin_unlock_irqrestore(session->host->host_lock, flags);
2475 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002476 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2477 "host_busy %d host_failed %d\n",
2478 session->host->host_busy,
2479 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002480 /*
2481 * force eh_abort() to unblock
2482 */
2483 wake_up(&conn->ehwait);
2484 }
2485
Mike Christie779ea122007-02-28 17:32:15 -06002486 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002487 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002488
Mike Christie7996a772006-04-06 21:13:41 -05002489 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002490 free_pages((unsigned long) conn->data,
2491 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002492 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002493 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002494 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002495 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002496 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002497 spin_unlock_bh(&session->lock);
2498
Mike Christie7996a772006-04-06 21:13:41 -05002499 iscsi_destroy_conn(cls_conn);
2500}
2501EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2502
2503int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2504{
2505 struct iscsi_conn *conn = cls_conn->dd_data;
2506 struct iscsi_session *session = conn->session;
2507
Mike Christieffd04362006-08-31 18:09:24 -04002508 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002509 iscsi_conn_printk(KERN_ERR, conn,
2510 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002511 return -EPERM;
2512 }
2513
Mike Christiedb98ccd2006-08-31 18:09:31 -04002514 if ((session->imm_data_en || !session->initial_r2t_en) &&
2515 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002516 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2517 "first_burst %d max_burst %d\n",
2518 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002519 return -EINVAL;
2520 }
2521
Mike Christief6d51802007-12-13 12:43:30 -06002522 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002523 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2524 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002525 conn->recv_timeout = 5;
2526 }
2527
2528 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002529 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2530 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002531 conn->ping_timeout = 5;
2532 }
2533
Mike Christie7996a772006-04-06 21:13:41 -05002534 spin_lock_bh(&session->lock);
2535 conn->c_stage = ISCSI_CONN_STARTED;
2536 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002537 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002538
Mike Christief6d51802007-12-13 12:43:30 -06002539 conn->last_recv = jiffies;
2540 conn->last_ping = jiffies;
2541 if (conn->recv_timeout && conn->ping_timeout)
2542 mod_timer(&conn->transport_timer,
2543 jiffies + (conn->recv_timeout * HZ));
2544
Mike Christie7996a772006-04-06 21:13:41 -05002545 switch(conn->stop_stage) {
2546 case STOP_CONN_RECOVER:
2547 /*
2548 * unblock eh_abort() if it is blocked. re-try all
2549 * commands after successful recovery
2550 */
Mike Christie7996a772006-04-06 21:13:41 -05002551 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002552 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002553 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002554 if (session->age == 16)
2555 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002556 break;
Mike Christie7996a772006-04-06 21:13:41 -05002557 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002558 conn->stop_stage = 0;
2559 break;
Mike Christie7996a772006-04-06 21:13:41 -05002560 default:
2561 break;
2562 }
2563 spin_unlock_bh(&session->lock);
2564
Mike Christie75613522008-05-21 15:53:59 -05002565 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002566 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002567 return 0;
2568}
2569EXPORT_SYMBOL_GPL(iscsi_conn_start);
2570
2571static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002572fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002573{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002574 struct iscsi_task *task;
2575 int i;
Mike Christie7996a772006-04-06 21:13:41 -05002576
Mike Christie3bbaaad2009-05-13 17:57:46 -05002577 for (i = 0; i < conn->session->cmds_max; i++) {
2578 task = conn->session->cmds[i];
2579 if (task->sc)
2580 continue;
2581
2582 if (task->state == ISCSI_TASK_FREE)
2583 continue;
2584
2585 ISCSI_DBG_SESSION(conn->session,
2586 "failing mgmt itt 0x%x state %d\n",
2587 task->itt, task->state);
2588 iscsi_complete_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002589 }
Mike Christie7996a772006-04-06 21:13:41 -05002590}
2591
Mike Christie656cffc2006-05-18 20:31:42 -05002592static void iscsi_start_session_recovery(struct iscsi_session *session,
2593 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002594{
Mike Christieed2abc72006-05-02 19:46:40 -05002595 int old_stop_stage;
2596
Mike Christie6724add2007-08-15 01:38:30 -05002597 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002598 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002599 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002600 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002601 mutex_unlock(&session->eh_mutex);
2602 return;
2603 }
2604
2605 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002606 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002607 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002608 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002609 */
Mike Christie67a61112006-05-30 00:37:20 -05002610 if (flag == STOP_CONN_TERM)
2611 session->state = ISCSI_STATE_TERMINATE;
2612 else if (conn->stop_stage != STOP_CONN_RECOVER)
2613 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002614 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002615
Mike Christie26013ad2009-05-13 17:57:43 -05002616 del_timer_sync(&conn->transport_timer);
2617 iscsi_suspend_tx(conn);
2618
2619 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002620 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002621 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002622 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002623 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002624
Mike Christie7996a772006-04-06 21:13:41 -05002625 /*
2626 * for connection level recovery we should not calculate
2627 * header digest. conn->hdr_size used for optimization
2628 * in hdr_extract() and will be re-negotiated at
2629 * set_param() time.
2630 */
2631 if (flag == STOP_CONN_RECOVER) {
2632 conn->hdrdgst_en = 0;
2633 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002634 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002635 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002636 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002637 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002638 }
Mike Christie7996a772006-04-06 21:13:41 -05002639 }
Mike Christie656cffc2006-05-18 20:31:42 -05002640
Mike Christie656cffc2006-05-18 20:31:42 -05002641 /*
2642 * flush queues.
2643 */
2644 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002645 if (flag == STOP_CONN_RECOVER)
Mike Christie3bbaaad2009-05-13 17:57:46 -05002646 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie56d7fcf2008-08-19 18:45:26 -05002647 else
Mike Christie3bbaaad2009-05-13 17:57:46 -05002648 fail_scsi_tasks(conn, -1, DID_ERROR);
2649 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002650 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002651 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002652}
Mike Christie7996a772006-04-06 21:13:41 -05002653
2654void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2655{
2656 struct iscsi_conn *conn = cls_conn->dd_data;
2657 struct iscsi_session *session = conn->session;
2658
2659 switch (flag) {
2660 case STOP_CONN_RECOVER:
2661 case STOP_CONN_TERM:
2662 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002663 break;
Mike Christie7996a772006-04-06 21:13:41 -05002664 default:
Mike Christie322d7392008-01-31 13:36:52 -06002665 iscsi_conn_printk(KERN_ERR, conn,
2666 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002667 }
2668}
2669EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2670
2671int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2672 struct iscsi_cls_conn *cls_conn, int is_leading)
2673{
Mike Christie75613522008-05-21 15:53:59 -05002674 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002675 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002676
Mike Christie7996a772006-04-06 21:13:41 -05002677 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002678 if (is_leading)
2679 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002680 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002681
2682 /*
2683 * Unblock xmitworker(), Login Phase will pass through.
2684 */
2685 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2686 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2687 return 0;
2688}
2689EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2690
Mike Christie5700b1a2009-05-13 17:57:40 -05002691static int iscsi_switch_str_param(char **param, char *new_val_buf)
2692{
2693 char *new_val;
2694
2695 if (*param) {
2696 if (!strcmp(*param, new_val_buf))
2697 return 0;
2698 }
2699
2700 new_val = kstrdup(new_val_buf, GFP_NOIO);
2701 if (!new_val)
2702 return -ENOMEM;
2703
2704 kfree(*param);
2705 *param = new_val;
2706 return 0;
2707}
Mike Christiea54a52c2006-06-28 12:00:23 -05002708
2709int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2710 enum iscsi_param param, char *buf, int buflen)
2711{
2712 struct iscsi_conn *conn = cls_conn->dd_data;
2713 struct iscsi_session *session = conn->session;
2714 uint32_t value;
2715
2716 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002717 case ISCSI_PARAM_FAST_ABORT:
2718 sscanf(buf, "%d", &session->fast_abort);
2719 break;
Mike Christief6d51802007-12-13 12:43:30 -06002720 case ISCSI_PARAM_ABORT_TMO:
2721 sscanf(buf, "%d", &session->abort_timeout);
2722 break;
2723 case ISCSI_PARAM_LU_RESET_TMO:
2724 sscanf(buf, "%d", &session->lu_reset_timeout);
2725 break;
2726 case ISCSI_PARAM_PING_TMO:
2727 sscanf(buf, "%d", &conn->ping_timeout);
2728 break;
2729 case ISCSI_PARAM_RECV_TMO:
2730 sscanf(buf, "%d", &conn->recv_timeout);
2731 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002732 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2733 sscanf(buf, "%d", &conn->max_recv_dlength);
2734 break;
2735 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2736 sscanf(buf, "%d", &conn->max_xmit_dlength);
2737 break;
2738 case ISCSI_PARAM_HDRDGST_EN:
2739 sscanf(buf, "%d", &conn->hdrdgst_en);
2740 break;
2741 case ISCSI_PARAM_DATADGST_EN:
2742 sscanf(buf, "%d", &conn->datadgst_en);
2743 break;
2744 case ISCSI_PARAM_INITIAL_R2T_EN:
2745 sscanf(buf, "%d", &session->initial_r2t_en);
2746 break;
2747 case ISCSI_PARAM_MAX_R2T:
2748 sscanf(buf, "%d", &session->max_r2t);
2749 break;
2750 case ISCSI_PARAM_IMM_DATA_EN:
2751 sscanf(buf, "%d", &session->imm_data_en);
2752 break;
2753 case ISCSI_PARAM_FIRST_BURST:
2754 sscanf(buf, "%d", &session->first_burst);
2755 break;
2756 case ISCSI_PARAM_MAX_BURST:
2757 sscanf(buf, "%d", &session->max_burst);
2758 break;
2759 case ISCSI_PARAM_PDU_INORDER_EN:
2760 sscanf(buf, "%d", &session->pdu_inorder_en);
2761 break;
2762 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2763 sscanf(buf, "%d", &session->dataseq_inorder_en);
2764 break;
2765 case ISCSI_PARAM_ERL:
2766 sscanf(buf, "%d", &session->erl);
2767 break;
2768 case ISCSI_PARAM_IFMARKER_EN:
2769 sscanf(buf, "%d", &value);
2770 BUG_ON(value);
2771 break;
2772 case ISCSI_PARAM_OFMARKER_EN:
2773 sscanf(buf, "%d", &value);
2774 BUG_ON(value);
2775 break;
2776 case ISCSI_PARAM_EXP_STATSN:
2777 sscanf(buf, "%u", &conn->exp_statsn);
2778 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002779 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002780 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002781 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002782 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002783 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002784 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002785 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002786 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002787 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002788 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002789 case ISCSI_PARAM_TPGT:
2790 sscanf(buf, "%d", &session->tpgt);
2791 break;
2792 case ISCSI_PARAM_PERSISTENT_PORT:
2793 sscanf(buf, "%d", &conn->persistent_port);
2794 break;
2795 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002796 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002797 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002798 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05002799 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002800 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002801 default:
2802 return -ENOSYS;
2803 }
2804
2805 return 0;
2806}
2807EXPORT_SYMBOL_GPL(iscsi_set_param);
2808
2809int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2810 enum iscsi_param param, char *buf)
2811{
Mike Christie75613522008-05-21 15:53:59 -05002812 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002813 int len;
2814
2815 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002816 case ISCSI_PARAM_FAST_ABORT:
2817 len = sprintf(buf, "%d\n", session->fast_abort);
2818 break;
Mike Christief6d51802007-12-13 12:43:30 -06002819 case ISCSI_PARAM_ABORT_TMO:
2820 len = sprintf(buf, "%d\n", session->abort_timeout);
2821 break;
2822 case ISCSI_PARAM_LU_RESET_TMO:
2823 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2824 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002825 case ISCSI_PARAM_INITIAL_R2T_EN:
2826 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2827 break;
2828 case ISCSI_PARAM_MAX_R2T:
2829 len = sprintf(buf, "%hu\n", session->max_r2t);
2830 break;
2831 case ISCSI_PARAM_IMM_DATA_EN:
2832 len = sprintf(buf, "%d\n", session->imm_data_en);
2833 break;
2834 case ISCSI_PARAM_FIRST_BURST:
2835 len = sprintf(buf, "%u\n", session->first_burst);
2836 break;
2837 case ISCSI_PARAM_MAX_BURST:
2838 len = sprintf(buf, "%u\n", session->max_burst);
2839 break;
2840 case ISCSI_PARAM_PDU_INORDER_EN:
2841 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2842 break;
2843 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2844 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2845 break;
2846 case ISCSI_PARAM_ERL:
2847 len = sprintf(buf, "%d\n", session->erl);
2848 break;
2849 case ISCSI_PARAM_TARGET_NAME:
2850 len = sprintf(buf, "%s\n", session->targetname);
2851 break;
2852 case ISCSI_PARAM_TPGT:
2853 len = sprintf(buf, "%d\n", session->tpgt);
2854 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002855 case ISCSI_PARAM_USERNAME:
2856 len = sprintf(buf, "%s\n", session->username);
2857 break;
2858 case ISCSI_PARAM_USERNAME_IN:
2859 len = sprintf(buf, "%s\n", session->username_in);
2860 break;
2861 case ISCSI_PARAM_PASSWORD:
2862 len = sprintf(buf, "%s\n", session->password);
2863 break;
2864 case ISCSI_PARAM_PASSWORD_IN:
2865 len = sprintf(buf, "%s\n", session->password_in);
2866 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002867 case ISCSI_PARAM_IFACE_NAME:
2868 len = sprintf(buf, "%s\n", session->ifacename);
2869 break;
2870 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002871 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05002872 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002873 default:
2874 return -ENOSYS;
2875 }
2876
2877 return len;
2878}
2879EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2880
2881int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2882 enum iscsi_param param, char *buf)
2883{
2884 struct iscsi_conn *conn = cls_conn->dd_data;
2885 int len;
2886
2887 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002888 case ISCSI_PARAM_PING_TMO:
2889 len = sprintf(buf, "%u\n", conn->ping_timeout);
2890 break;
2891 case ISCSI_PARAM_RECV_TMO:
2892 len = sprintf(buf, "%u\n", conn->recv_timeout);
2893 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002894 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2895 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2896 break;
2897 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2898 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2899 break;
2900 case ISCSI_PARAM_HDRDGST_EN:
2901 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2902 break;
2903 case ISCSI_PARAM_DATADGST_EN:
2904 len = sprintf(buf, "%d\n", conn->datadgst_en);
2905 break;
2906 case ISCSI_PARAM_IFMARKER_EN:
2907 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2908 break;
2909 case ISCSI_PARAM_OFMARKER_EN:
2910 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2911 break;
2912 case ISCSI_PARAM_EXP_STATSN:
2913 len = sprintf(buf, "%u\n", conn->exp_statsn);
2914 break;
2915 case ISCSI_PARAM_PERSISTENT_PORT:
2916 len = sprintf(buf, "%d\n", conn->persistent_port);
2917 break;
2918 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2919 len = sprintf(buf, "%s\n", conn->persistent_address);
2920 break;
2921 default:
2922 return -ENOSYS;
2923 }
2924
2925 return len;
2926}
2927EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2928
Mike Christie0801c242007-05-30 12:57:12 -05002929int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2930 char *buf)
2931{
Mike Christie75613522008-05-21 15:53:59 -05002932 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002933 int len;
2934
2935 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002936 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002937 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002938 break;
Mike Christie0801c242007-05-30 12:57:12 -05002939 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002940 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002941 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002942 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002943 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002944 break;
Mike Christie75613522008-05-21 15:53:59 -05002945 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002946 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002947 break;
Mike Christie0801c242007-05-30 12:57:12 -05002948 default:
2949 return -ENOSYS;
2950 }
2951
2952 return len;
2953}
2954EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2955
2956int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2957 char *buf, int buflen)
2958{
Mike Christie75613522008-05-21 15:53:59 -05002959 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002960
2961 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002962 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002963 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002964 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05002965 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05002966 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002967 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05002968 default:
2969 return -ENOSYS;
2970 }
2971
2972 return 0;
2973}
2974EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2975
Mike Christie7996a772006-04-06 21:13:41 -05002976MODULE_AUTHOR("Mike Christie");
2977MODULE_DESCRIPTION("iSCSI library functions");
2978MODULE_LICENSE("GPL");