blob: 8c29480fc02be49970de458585aacd57e17bb122 [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Erez Zilberbd2199d2009-06-15 22:11:10 -050041static int iscsi_dbg_lib_conn;
42module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
47
48static int iscsi_dbg_lib_session;
49module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
50 S_IRUGO | S_IWUSR);
51MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
54
55static int iscsi_dbg_lib_eh;
56module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
57 S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
Mike Christie1b2c7af2009-03-05 14:45:58 -060061
62#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
63 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050064 if (iscsi_dbg_lib_conn) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060065 iscsi_conn_printk(KERN_INFO, _conn, \
66 "%s " dbg_fmt, \
67 __func__, ##arg); \
68 } while (0);
69
70#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
71 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050072 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
74 "%s " dbg_fmt, \
75 __func__, ##arg); \
76 } while (0);
77
78#define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
79 do { \
80 if (iscsi_dbg_lib_eh) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060081 iscsi_session_printk(KERN_INFO, _session, \
82 "%s " dbg_fmt, \
83 __func__, ##arg); \
84 } while (0);
85
Mike Christie77a23c22007-05-30 12:57:18 -050086/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050088
Mike Christie77a23c22007-05-30 12:57:18 -050089static int iscsi_sna_lt(u32 n1, u32 n2)
90{
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
93}
94
95/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96static int iscsi_sna_lte(u32 n1, u32 n2)
97{
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
100}
101
Mike Christie32ae7632009-03-05 14:46:03 -0600102inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
103{
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
106
Mike Christie1336aed2009-05-13 17:57:48 -0500107 if (ihost->workq)
108 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -0600109}
110EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
111
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530112static void __iscsi_update_cmdsn(struct iscsi_session *session,
113 uint32_t exp_cmdsn, uint32_t max_cmdsn)
Mike Christie7996a772006-04-06 21:13:41 -0500114{
Mike Christie77a23c22007-05-30 12:57:18 -0500115 /*
116 * standard specifies this check for when to update expected and
117 * max sequence numbers
118 */
119 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
120 return;
121
122 if (exp_cmdsn != session->exp_cmdsn &&
123 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500124 session->exp_cmdsn = exp_cmdsn;
125
Mike Christie77a23c22007-05-30 12:57:18 -0500126 if (max_cmdsn != session->max_cmdsn &&
127 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
128 session->max_cmdsn = max_cmdsn;
129 /*
130 * if the window closed with IO queued, then kick the
131 * xmit thread
132 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500133 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500134 !list_empty(&session->leadconn->mgmtqueue))
135 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500136 }
Mike Christie7996a772006-04-06 21:13:41 -0500137}
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530138
139void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
140{
141 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
142 be32_to_cpu(hdr->max_cmdsn));
143}
Mike Christie77a23c22007-05-30 12:57:18 -0500144EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500145
Mike Christie577577d2008-12-02 00:32:05 -0600146/**
147 * iscsi_prep_data_out_pdu - initialize Data-Out
148 * @task: scsi command task
149 * @r2t: R2T info
150 * @hdr: iscsi data in pdu
151 *
152 * Notes:
153 * Initialize Data-Out within this R2T sequence and finds
154 * proper data_offset within this SCSI command.
155 *
156 * This function is called with connection lock taken.
157 **/
158void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
159 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500160{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500161 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600162 unsigned int left = r2t->data_length - r2t->sent;
163
164 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500165
166 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600167 hdr->ttt = r2t->ttt;
168 hdr->datasn = cpu_to_be32(r2t->datasn);
169 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500170 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600171 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
172 hdr->itt = task->hdr_itt;
173 hdr->exp_statsn = r2t->exp_statsn;
174 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
175 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500176 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600177 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500178 hdr->flags = 0;
179 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600180 hton24(hdr->dlength, left);
181 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500182 hdr->flags = ISCSI_FLAG_CMD_FINAL;
183 }
Mike Christie577577d2008-12-02 00:32:05 -0600184 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500185}
Mike Christie577577d2008-12-02 00:32:05 -0600186EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500187
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600189{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500190 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600191
Mike Christie9c19a7d2008-05-21 15:54:09 -0500192 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600193 WARN_ON(1);
194 return -EINVAL;
195 }
196
197 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500198 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600199 return 0;
200}
201
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500202/*
203 * make an extended cdb AHS
204 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500205static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500206{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500207 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500208 unsigned rlen, pad_len;
209 unsigned short ahslength;
210 struct iscsi_ecdb_ahdr *ecdb_ahdr;
211 int rc;
212
Mike Christie9c19a7d2008-05-21 15:54:09 -0500213 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500214 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
215
216 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
217 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
218
219 pad_len = iscsi_padding(rlen);
220
Mike Christie9c19a7d2008-05-21 15:54:09 -0500221 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500222 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
223 if (rc)
224 return rc;
225
226 if (pad_len)
227 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
228
229 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
230 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
231 ecdb_ahdr->reserved = 0;
232 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
233
Mike Christie1b2c7af2009-03-05 14:45:58 -0600234 ISCSI_DBG_SESSION(task->conn->session,
235 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
236 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
237 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
238 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500239 return 0;
240}
241
Mike Christie9c19a7d2008-05-21 15:54:09 -0500242static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500243{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500245 struct iscsi_rlength_ahdr *rlen_ahdr;
246 int rc;
247
Mike Christie9c19a7d2008-05-21 15:54:09 -0500248 rlen_ahdr = iscsi_next_hdr(task);
249 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500250 if (rc)
251 return rc;
252
253 rlen_ahdr->ahslength =
254 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
255 sizeof(rlen_ahdr->reserved));
256 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
257 rlen_ahdr->reserved = 0;
258 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
259
Mike Christie1b2c7af2009-03-05 14:45:58 -0600260 ISCSI_DBG_SESSION(task->conn->session,
261 "bidi-in rlen_ahdr->read_length(%d) "
262 "rlen_ahdr->ahslength(%d)\n",
263 be32_to_cpu(rlen_ahdr->read_length),
264 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500265 return 0;
266}
267
Mike Christie7996a772006-04-06 21:13:41 -0500268/**
269 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500270 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500271 *
272 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
273 * fields like dlength or final based on how much data it sends
274 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500275static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500276{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500277 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500278 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500279 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600280 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500281 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600282 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600283 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500284
Mike Christie184b57c2009-05-13 17:57:39 -0500285 if (conn->session->tt->alloc_pdu) {
286 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
287 if (rc)
288 return rc;
289 }
Mike Christie577577d2008-12-02 00:32:05 -0600290 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600291 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600292 memset(hdr, 0, sizeof(*hdr));
293
Mike Christie2ff79d52008-12-02 00:32:14 -0600294 if (session->tt->parse_pdu_itt)
295 hdr->itt = task->hdr_itt = itt;
296 else
297 hdr->itt = task->hdr_itt = build_itt(task->itt,
298 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500299 task->hdr_len = 0;
300 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600301 if (rc)
302 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600303 hdr->opcode = ISCSI_OP_SCSI_CMD;
304 hdr->flags = ISCSI_ATTR_SIMPLE;
305 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600306 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600307 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500308 cmd_len = sc->cmd_len;
309 if (cmd_len < ISCSI_CDB_SIZE)
310 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
311 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500312 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500313 if (rc)
314 return rc;
315 cmd_len = ISCSI_CDB_SIZE;
316 }
317 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500318
Mike Christie9c19a7d2008-05-21 15:54:09 -0500319 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500320 if (scsi_bidi_cmnd(sc)) {
321 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500322 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500323 if (rc)
324 return rc;
325 }
Mike Christie7996a772006-04-06 21:13:41 -0500326 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500327 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600328 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
329
Boaz Harroshc07d4442008-04-18 10:11:52 -0500330 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500331 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
332 /*
333 * Write counters:
334 *
335 * imm_count bytes to be sent right after
336 * SCSI PDU Header
337 *
338 * unsol_count bytes(as Data-Out) to be sent
339 * without R2T ack right after
340 * immediate data
341 *
Mike Christie577577d2008-12-02 00:32:05 -0600342 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500343 *
344 * pad_count bytes to be sent as zero-padding
345 */
Mike Christie577577d2008-12-02 00:32:05 -0600346 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500347
348 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500349 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500350 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500351 conn->max_xmit_dlength);
352 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500353 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500354 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500355 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500356 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600357 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500358
Mike Christieffd04362006-08-31 18:09:24 -0400359 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600360 r2t->data_length = min(session->first_burst, out_len) -
361 task->imm_count;
362 r2t->data_offset = task->imm_count;
363 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
364 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400365 }
366
Mike Christie577577d2008-12-02 00:32:05 -0600367 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500368 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500370 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500371 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
372 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500373 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500374
375 if (sc->sc_data_direction == DMA_FROM_DEVICE)
376 hdr->flags |= ISCSI_FLAG_CMD_READ;
377 }
378
Boaz Harrosh004d6532007-12-13 12:43:23 -0600379 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500380 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600381
382 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
383 hdrlength /= ISCSI_PAD_LEN;
384
385 WARN_ON(hdrlength >= 256);
386 hdr->hlength = hdrlength & 0xFF;
387
Mike Christie577577d2008-12-02 00:32:05 -0600388 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500389 return -EIO;
390
Mike Christie9c19a7d2008-05-21 15:54:09 -0500391 task->state = ISCSI_TASK_RUNNING;
Mike Christied3305f32009-08-20 15:10:58 -0500392 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
393 session->cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500394
Olaf Kircha8ac6312007-12-13 12:43:35 -0600395 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600396 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
397 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
398 scsi_bidi_cmnd(sc) ? "bidirectional" :
399 sc->sc_data_direction == DMA_TO_DEVICE ?
400 "write" : "read", conn->id, sc, sc->cmnd[0],
401 task->itt, scsi_bufflen(sc),
402 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
403 session->cmdsn,
404 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600405 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500406}
Mike Christie7996a772006-04-06 21:13:41 -0500407
408/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500409 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500410 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500411 *
412 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500413 * This function returns the scsi command to scsi-ml or cleans
414 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500415 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500416static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500417{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500418 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600419 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500420 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500421
Mike Christie4421c9e2009-05-13 17:57:50 -0500422 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
423 task->itt, task->state, task->sc);
424
Mike Christie577577d2008-12-02 00:32:05 -0600425 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500426 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500427 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500428 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500430 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500432 return;
433
Mike Christie9c19a7d2008-05-21 15:54:09 -0500434 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500435
Mike Christie3e5c28a2008-05-21 15:54:06 -0500436 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500437 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500438 /* SCSI eh reuses commands to verify us */
439 sc->SCp.ptr = NULL;
440 /*
441 * queue command may call this to free the task, but
442 * not have setup the sc callback
443 */
444 if (sc->scsi_done)
445 sc->scsi_done(sc);
446 }
Mike Christie7996a772006-04-06 21:13:41 -0500447}
448
Mike Christie913e5bf2008-05-21 15:54:18 -0500449void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400450{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500451 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400452}
Mike Christie913e5bf2008-05-21 15:54:18 -0500453EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400454
Mike Christie9c19a7d2008-05-21 15:54:09 -0500455static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400456{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500457 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500458 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400459}
460
Mike Christie9c19a7d2008-05-21 15:54:09 -0500461void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500462{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500463 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500464
465 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500466 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500467 spin_unlock_bh(&session->lock);
468}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500469EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500470
Mike Christie3bbaaad2009-05-13 17:57:46 -0500471/**
472 * iscsi_complete_task - finish a task
473 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500474 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500475 *
476 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600477 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500478static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600479{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500480 struct iscsi_conn *conn = task->conn;
481
Mike Christie4421c9e2009-05-13 17:57:50 -0500482 ISCSI_DBG_SESSION(conn->session,
483 "complete task itt 0x%x state %d sc %p\n",
484 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500485 if (task->state == ISCSI_TASK_COMPLETED ||
486 task->state == ISCSI_TASK_ABRT_TMF ||
487 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500488 return;
489 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500490 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500491
492 if (!list_empty(&task->running))
493 list_del_init(&task->running);
494
495 if (conn->task == task)
496 conn->task = NULL;
497
498 if (conn->ping_task == task)
499 conn->ping_task = NULL;
500
501 /* release get from queueing */
502 __iscsi_put_task(task);
503}
504
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530505/**
506 * iscsi_complete_scsi_task - finish scsi task normally
507 * @task: iscsi task for scsi cmd
508 * @exp_cmdsn: expected cmd sn in cpu format
509 * @max_cmdsn: max cmd sn in cpu format
510 *
511 * This is used when drivers do not need or cannot perform
512 * lower level pdu processing.
513 *
514 * Called with session lock
515 */
516void iscsi_complete_scsi_task(struct iscsi_task *task,
517 uint32_t exp_cmdsn, uint32_t max_cmdsn)
518{
519 struct iscsi_conn *conn = task->conn;
520
521 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
522
523 conn->last_recv = jiffies;
524 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
525 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
526}
527EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
528
529
Mike Christie3bbaaad2009-05-13 17:57:46 -0500530/*
531 * session lock must be held and if not called for a task that is
532 * still pending or from the xmit thread, then xmit thread must
533 * be suspended.
534 */
535static void fail_scsi_task(struct iscsi_task *task, int err)
536{
537 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600538 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500539 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600540
Mike Christie3bbaaad2009-05-13 17:57:46 -0500541 /*
542 * if a command completes and we get a successful tmf response
543 * we will hit this because the scsi eh abort code does not take
544 * a ref to the task.
545 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500546 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600547 if (!sc)
548 return;
549
Mike Christieb3cd5052009-05-13 17:57:49 -0500550 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600551 /*
552 * cmd never made it to the xmit thread, so we should not count
553 * the cmd in the sequencing
554 */
555 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500556 /* it was never sent so just complete like normal */
557 state = ISCSI_TASK_COMPLETED;
558 } else if (err == DID_TRANSPORT_DISRUPTED)
559 state = ISCSI_TASK_ABRT_SESS_RECOV;
560 else
561 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600562
Mike Christieb3cd5052009-05-13 17:57:49 -0500563 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500564 if (!scsi_bidi_cmnd(sc))
565 scsi_set_resid(sc, scsi_bufflen(sc));
566 else {
567 scsi_out(sc)->resid = scsi_out(sc)->length;
568 scsi_in(sc)->resid = scsi_in(sc)->length;
569 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500570
Mike Christieb3cd5052009-05-13 17:57:49 -0500571 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600572}
573
Mike Christie3e5c28a2008-05-21 15:54:06 -0500574static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500575 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500576{
577 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600578 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500579 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
Mike Christie4f704dc2009-11-11 16:34:31 -0600580 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie052d0142008-05-21 15:54:05 -0500581
582 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
583 return -ENOTCONN;
584
Mike Christie4f704dc2009-11-11 16:34:31 -0600585 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
Mike Christie052d0142008-05-21 15:54:05 -0500586 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
587 /*
588 * pre-format CmdSN for outgoing PDU.
589 */
590 nop->cmdsn = cpu_to_be32(session->cmdsn);
591 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500592 /*
Mike Christie4f704dc2009-11-11 16:34:31 -0600593 * TODO: We always use immediate for normal session pdus.
Mike Christie052d0142008-05-21 15:54:05 -0500594 * If we start to send tmfs or nops as non-immediate then
595 * we should start checking the cmdsn numbers for mgmt tasks.
Mike Christie4f704dc2009-11-11 16:34:31 -0600596 *
597 * During discovery sessions iscsid sends TEXT as non immediate,
598 * but we always only send one PDU at a time.
Mike Christie052d0142008-05-21 15:54:05 -0500599 */
600 if (conn->c_stage == ISCSI_CONN_STARTED &&
601 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
602 session->queued_cmdsn++;
603 session->cmdsn++;
604 }
605 }
606
Mike Christieae15f802008-12-02 00:32:15 -0600607 if (session->tt->init_task && session->tt->init_task(task))
608 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500609
610 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
611 session->state = ISCSI_STATE_LOGGING_OUT;
612
Mike Christie577577d2008-12-02 00:32:05 -0600613 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600614 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
615 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
616 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500617 return 0;
618}
619
Mike Christie9c19a7d2008-05-21 15:54:09 -0500620static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600621__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
622 char *data, uint32_t data_size)
623{
624 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500625 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie4f704dc2009-11-11 16:34:31 -0600626 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500627 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600628 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600629
630 if (session->state == ISCSI_STATE_TERMINATE)
631 return NULL;
632
Mike Christie4f704dc2009-11-11 16:34:31 -0600633 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
Mike Christief6d51802007-12-13 12:43:30 -0600634 /*
635 * Login and Text are sent serially, in
636 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500637 * Same task can be used. Same ITT must be used.
638 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600639 */
Mike Christie4f704dc2009-11-11 16:34:31 -0600640 if (conn->login_task->state != ISCSI_TASK_FREE) {
641 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
642 "progress. Cannot start new task.\n");
643 return NULL;
644 }
645
Mike Christie9c19a7d2008-05-21 15:54:09 -0500646 task = conn->login_task;
Mike Christie4f704dc2009-11-11 16:34:31 -0600647 } else {
Mike Christie26013ad2009-05-13 17:57:43 -0500648 if (session->state != ISCSI_STATE_LOGGED_IN)
649 return NULL;
650
Mike Christief6d51802007-12-13 12:43:30 -0600651 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
652 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
653
Mike Christie3e5c28a2008-05-21 15:54:06 -0500654 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500655 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600656 return NULL;
657 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500658 /*
659 * released in complete pdu for task we expect a response for, and
660 * released by the lld when it has transmitted the task for
661 * pdus we do not expect a response for.
662 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500663 atomic_set(&task->refcount, 1);
664 task->conn = conn;
665 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500666 INIT_LIST_HEAD(&task->running);
667 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600668
669 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500670 memcpy(task->data, data, data_size);
671 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600672 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500673 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600674
Mike Christie184b57c2009-05-13 17:57:39 -0500675 if (conn->session->tt->alloc_pdu) {
676 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
677 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
678 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500679 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500680 }
Mike Christie577577d2008-12-02 00:32:05 -0600681 }
Mike Christie184b57c2009-05-13 17:57:39 -0500682
Mike Christie262ef632008-12-02 00:32:13 -0600683 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600684 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500685 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600686
687 if (hdr->itt != RESERVED_ITT) {
688 if (session->tt->parse_pdu_itt)
689 task->hdr->itt = itt;
690 else
691 task->hdr->itt = build_itt(task->itt,
692 task->conn->session->age);
693 }
694
Mike Christie1336aed2009-05-13 17:57:48 -0500695 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600696 if (iscsi_prep_mgmt_task(conn, task))
697 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500698
Mike Christie9c19a7d2008-05-21 15:54:09 -0500699 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600700 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500701 } else {
702 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600703 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500704 }
Mike Christie052d0142008-05-21 15:54:05 -0500705
Mike Christie9c19a7d2008-05-21 15:54:09 -0500706 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600707
708free_task:
709 __iscsi_put_task(task);
710 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600711}
712
713int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
714 char *data, uint32_t data_size)
715{
716 struct iscsi_conn *conn = cls_conn->dd_data;
717 struct iscsi_session *session = conn->session;
718 int err = 0;
719
720 spin_lock_bh(&session->lock);
721 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
722 err = -EPERM;
723 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600724 return err;
725}
726EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
727
Mike Christie7996a772006-04-06 21:13:41 -0500728/**
729 * iscsi_cmd_rsp - SCSI Command Response processing
730 * @conn: iscsi connection
731 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500732 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500733 * @data: cmd data buffer
734 * @datalen: len of buffer
735 *
736 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500737 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500738 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500739static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500740 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500741 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500742{
Mike Christie7996a772006-04-06 21:13:41 -0500743 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
744 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500745 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500746
Mike Christie77a23c22007-05-30 12:57:18 -0500747 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500748 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
749
750 sc->result = (DID_OK << 16) | rhdr->cmd_status;
751
752 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
753 sc->result = DID_ERROR << 16;
754 goto out;
755 }
756
757 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600758 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500759
760 if (datalen < 2) {
761invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600762 iscsi_conn_printk(KERN_ERR, conn,
763 "Got CHECK_CONDITION but invalid data "
764 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500765 sc->result = DID_BAD_TARGET << 16;
766 goto out;
767 }
768
Harvey Harrison8f3339912008-05-21 15:54:20 -0500769 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500770 if (datalen < senselen)
771 goto invalid_datalen;
772
773 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600774 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600775 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
776 min_t(uint16_t, senselen,
777 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500778 }
779
Boaz Harroshc07d4442008-04-18 10:11:52 -0500780 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
781 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
782 int res_count = be32_to_cpu(rhdr->bi_residual_count);
783
784 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
785 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
786 res_count <= scsi_in(sc)->length))
787 scsi_in(sc)->resid = res_count;
788 else
789 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
790 }
791
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600792 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
793 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500794 int res_count = be32_to_cpu(rhdr->residual_count);
795
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600796 if (res_count > 0 &&
797 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
798 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500799 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900800 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500801 else
802 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500803 }
Mike Christie7996a772006-04-06 21:13:41 -0500804out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500805 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600806 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500807 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500808 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500809}
810
Mike Christie1d9edf02008-09-24 11:46:09 -0500811/**
812 * iscsi_data_in_rsp - SCSI Data-In Response processing
813 * @conn: iscsi connection
814 * @hdr: iscsi pdu
815 * @task: scsi command task
816 **/
817static void
818iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
819 struct iscsi_task *task)
820{
821 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
822 struct scsi_cmnd *sc = task->sc;
823
824 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
825 return;
826
Mike Christieedbc9aa052009-05-13 17:57:42 -0500827 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500828 sc->result = (DID_OK << 16) | rhdr->cmd_status;
829 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
830 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
831 ISCSI_FLAG_DATA_OVERFLOW)) {
832 int res_count = be32_to_cpu(rhdr->residual_count);
833
834 if (res_count > 0 &&
835 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
836 res_count <= scsi_in(sc)->length))
837 scsi_in(sc)->resid = res_count;
838 else
839 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
840 }
841
Mike Christie3bbaaad2009-05-13 17:57:46 -0500842 ISCSI_DBG_SESSION(conn->session, "data in with status done "
843 "[sc %p res %d itt 0x%x]\n",
844 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500845 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500846 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500847}
848
Mike Christie7ea8b822006-07-24 15:47:22 -0500849static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
850{
851 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
852
853 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
854 conn->tmfrsp_pdus_cnt++;
855
Mike Christie843c0a82007-12-13 12:43:20 -0600856 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500857 return;
858
859 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600860 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500861 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600862 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500863 else
Mike Christie843c0a82007-12-13 12:43:20 -0600864 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500865 wake_up(&conn->ehwait);
866}
867
Mike Christief6d51802007-12-13 12:43:30 -0600868static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
869{
870 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500871 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600872
Mike Christie9c19a7d2008-05-21 15:54:09 -0500873 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600874 return;
875
876 memset(&hdr, 0, sizeof(struct iscsi_nopout));
877 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
878 hdr.flags = ISCSI_FLAG_CMD_FINAL;
879
880 if (rhdr) {
881 memcpy(hdr.lun, rhdr->lun, 8);
882 hdr.ttt = rhdr->ttt;
883 hdr.itt = RESERVED_ITT;
884 } else
885 hdr.ttt = RESERVED_ITT;
886
Mike Christie9c19a7d2008-05-21 15:54:09 -0500887 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
888 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600889 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600890 else if (!rhdr) {
891 /* only track our nops */
892 conn->ping_task = task;
893 conn->last_ping = jiffies;
894 }
Mike Christief6d51802007-12-13 12:43:30 -0600895}
896
Mike Christie8afa1432009-08-20 15:10:59 -0500897static int iscsi_nop_out_rsp(struct iscsi_task *task,
898 struct iscsi_nopin *nop, char *data, int datalen)
899{
900 struct iscsi_conn *conn = task->conn;
901 int rc = 0;
902
903 if (conn->ping_task != task) {
904 /*
905 * If this is not in response to one of our
906 * nops then it must be from userspace.
907 */
908 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
909 data, datalen))
910 rc = ISCSI_ERR_CONN_FAILED;
911 } else
912 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
913 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
914 return rc;
915}
916
Mike Christie62f38302006-08-31 18:09:27 -0400917static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
918 char *data, int datalen)
919{
920 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
921 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -0500922 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -0400923
924 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
925
Mike Christie8afa1432009-08-20 15:10:59 -0500926 if (ntoh24(reject->dlength) > datalen ||
927 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
928 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
929 "pdu. Invalid data length (pdu dlength "
930 "%u, datalen %d\n", ntoh24(reject->dlength),
931 datalen);
932 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -0400933 }
Mike Christie8afa1432009-08-20 15:10:59 -0500934 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
935 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
936
937 switch (reject->reason) {
938 case ISCSI_REASON_DATA_DIGEST_ERROR:
939 iscsi_conn_printk(KERN_ERR, conn,
940 "pdu (op 0x%x itt 0x%x) rejected "
941 "due to DataDigest error.\n",
942 rejected_pdu.itt, opcode);
943 break;
944 case ISCSI_REASON_IMM_CMD_REJECT:
945 iscsi_conn_printk(KERN_ERR, conn,
946 "pdu (op 0x%x itt 0x%x) rejected. Too many "
947 "immediate commands.\n",
948 rejected_pdu.itt, opcode);
949 /*
950 * We only send one TMF at a time so if the target could not
951 * handle it, then it should get fixed (RFC mandates that
952 * a target can handle one immediate TMF per conn).
953 *
954 * For nops-outs, we could have sent more than one if
955 * the target is sending us lots of nop-ins
956 */
957 if (opcode != ISCSI_OP_NOOP_OUT)
958 return 0;
959
960 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
961 /*
962 * nop-out in response to target's nop-out rejected.
963 * Just resend.
964 */
965 iscsi_send_nopout(conn,
966 (struct iscsi_nopin*)&rejected_pdu);
967 else {
968 struct iscsi_task *task;
969 /*
970 * Our nop as ping got dropped. We know the target
971 * and transport are ok so just clean up
972 */
973 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
974 if (!task) {
975 iscsi_conn_printk(KERN_ERR, conn,
976 "Invalid pdu reject. Could "
977 "not lookup rejected task.\n");
978 rc = ISCSI_ERR_BAD_ITT;
979 } else
980 rc = iscsi_nop_out_rsp(task,
981 (struct iscsi_nopin*)&rejected_pdu,
982 NULL, 0);
983 }
984 break;
985 default:
986 iscsi_conn_printk(KERN_ERR, conn,
987 "pdu (op 0x%x itt 0x%x) rejected. Reason "
988 "code 0x%x\n", rejected_pdu.itt,
989 rejected_pdu.opcode, reject->reason);
990 break;
991 }
992 return rc;
Mike Christie62f38302006-08-31 18:09:27 -0400993}
994
Mike Christie7996a772006-04-06 21:13:41 -0500995/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500996 * iscsi_itt_to_task - look up task by itt
997 * @conn: iscsi connection
998 * @itt: itt
999 *
1000 * This should be used for mgmt tasks like login and nops, or if
1001 * the LDD's itt space does not include the session age.
1002 *
1003 * The session lock must be held.
1004 */
Mike Christie8f9256c2009-05-13 17:57:41 -05001005struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -05001006{
1007 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001008 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -05001009
1010 if (itt == RESERVED_ITT)
1011 return NULL;
1012
Mike Christie262ef632008-12-02 00:32:13 -06001013 if (session->tt->parse_pdu_itt)
1014 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1015 else
1016 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -05001017 if (i >= session->cmds_max)
1018 return NULL;
1019
1020 return session->cmds[i];
1021}
Mike Christie8f9256c2009-05-13 17:57:41 -05001022EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -05001023
1024/**
Mike Christie7996a772006-04-06 21:13:41 -05001025 * __iscsi_complete_pdu - complete pdu
1026 * @conn: iscsi conn
1027 * @hdr: iscsi header
1028 * @data: data buffer
1029 * @datalen: len of data buffer
1030 *
1031 * Completes pdu processing by freeing any resources allocated at
1032 * queuecommand or send generic. session lock must be held and verify
1033 * itt must have been called.
1034 */
Mike Christie913e5bf2008-05-21 15:54:18 -05001035int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1036 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001037{
1038 struct iscsi_session *session = conn->session;
1039 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001040 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001041 uint32_t itt;
1042
Mike Christief6d51802007-12-13 12:43:30 -06001043 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001044 rc = iscsi_verify_itt(conn, hdr->itt);
1045 if (rc)
1046 return rc;
1047
Al Virob4377352007-02-09 16:39:40 +00001048 if (hdr->itt != RESERVED_ITT)
1049 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001050 else
Al Virob4377352007-02-09 16:39:40 +00001051 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001052
Mike Christie1b2c7af2009-03-05 14:45:58 -06001053 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1054 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001055
Mike Christie3e5c28a2008-05-21 15:54:06 -05001056 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001057 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001058
Mike Christie7996a772006-04-06 21:13:41 -05001059 switch(opcode) {
1060 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001061 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001062 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001063 break;
1064 }
1065
Al Virob4377352007-02-09 16:39:40 +00001066 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001067 break;
1068
Mike Christief6d51802007-12-13 12:43:30 -06001069 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001070 break;
1071 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001072 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1073 break;
Mike Christie7996a772006-04-06 21:13:41 -05001074 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001075 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001076 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1077 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001078 break;
1079 default:
1080 rc = ISCSI_ERR_BAD_OPCODE;
1081 break;
1082 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001083 goto out;
1084 }
Mike Christie7996a772006-04-06 21:13:41 -05001085
Mike Christie3e5c28a2008-05-21 15:54:06 -05001086 switch(opcode) {
1087 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001088 case ISCSI_OP_SCSI_DATA_IN:
1089 task = iscsi_itt_to_ctask(conn, hdr->itt);
1090 if (!task)
1091 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001092 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001093 break;
1094 case ISCSI_OP_R2T:
1095 /*
1096 * LLD handles R2Ts if they need to.
1097 */
1098 return 0;
1099 case ISCSI_OP_LOGOUT_RSP:
1100 case ISCSI_OP_LOGIN_RSP:
1101 case ISCSI_OP_TEXT_RSP:
1102 case ISCSI_OP_SCSI_TMFUNC_RSP:
1103 case ISCSI_OP_NOOP_IN:
1104 task = iscsi_itt_to_task(conn, hdr->itt);
1105 if (!task)
1106 return ISCSI_ERR_BAD_ITT;
1107 break;
1108 default:
1109 return ISCSI_ERR_BAD_OPCODE;
1110 }
1111
1112 switch(opcode) {
1113 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001114 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001115 break;
1116 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001117 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001118 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001119 case ISCSI_OP_LOGOUT_RSP:
1120 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1121 if (datalen) {
1122 rc = ISCSI_ERR_PROTO;
1123 break;
1124 }
1125 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1126 goto recv_pdu;
1127 case ISCSI_OP_LOGIN_RSP:
1128 case ISCSI_OP_TEXT_RSP:
1129 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1130 /*
1131 * login related PDU's exp_statsn is handled in
1132 * userspace
1133 */
1134 goto recv_pdu;
1135 case ISCSI_OP_SCSI_TMFUNC_RSP:
1136 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1137 if (datalen) {
1138 rc = ISCSI_ERR_PROTO;
1139 break;
1140 }
1141
1142 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001143 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001144 break;
1145 case ISCSI_OP_NOOP_IN:
1146 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1147 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1148 rc = ISCSI_ERR_PROTO;
1149 break;
1150 }
1151 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1152
Mike Christie8afa1432009-08-20 15:10:59 -05001153 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1154 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001155 break;
1156 default:
1157 rc = ISCSI_ERR_BAD_OPCODE;
1158 break;
1159 }
1160
1161out:
1162 return rc;
1163recv_pdu:
1164 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1165 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001166 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001167 return rc;
1168}
Mike Christie913e5bf2008-05-21 15:54:18 -05001169EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001170
1171int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1172 char *data, int datalen)
1173{
1174 int rc;
1175
1176 spin_lock(&conn->session->lock);
1177 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1178 spin_unlock(&conn->session->lock);
1179 return rc;
1180}
1181EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1182
Mike Christie0af967f2008-05-21 15:54:04 -05001183int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001184{
1185 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001186 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001187
Mike Christie0af967f2008-05-21 15:54:04 -05001188 if (itt == RESERVED_ITT)
1189 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001190
Mike Christie262ef632008-12-02 00:32:13 -06001191 if (session->tt->parse_pdu_itt)
1192 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1193 else {
1194 i = get_itt(itt);
1195 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1196 }
1197
1198 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001199 iscsi_conn_printk(KERN_ERR, conn,
1200 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001201 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001202 return ISCSI_ERR_BAD_ITT;
1203 }
Mike Christie7996a772006-04-06 21:13:41 -05001204
Mike Christie3e5c28a2008-05-21 15:54:06 -05001205 if (i >= session->cmds_max) {
1206 iscsi_conn_printk(KERN_ERR, conn,
1207 "received invalid itt index %u (max cmds "
1208 "%u.\n", i, session->cmds_max);
1209 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001210 }
Mike Christie7996a772006-04-06 21:13:41 -05001211 return 0;
1212}
1213EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1214
Mike Christie913e5bf2008-05-21 15:54:18 -05001215/**
1216 * iscsi_itt_to_ctask - look up ctask by itt
1217 * @conn: iscsi connection
1218 * @itt: itt
1219 *
1220 * This should be used for cmd tasks.
1221 *
1222 * The session lock must be held.
1223 */
1224struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001225{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001226 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001227
1228 if (iscsi_verify_itt(conn, itt))
1229 return NULL;
1230
Mike Christie913e5bf2008-05-21 15:54:18 -05001231 task = iscsi_itt_to_task(conn, itt);
1232 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001233 return NULL;
1234
Mike Christie913e5bf2008-05-21 15:54:18 -05001235 if (task->sc->SCp.phase != conn->session->age) {
1236 iscsi_session_printk(KERN_ERR, conn->session,
1237 "task's session age %d, expected %d\n",
1238 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001239 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001240 }
Mike Christie0af967f2008-05-21 15:54:04 -05001241
Mike Christie9c19a7d2008-05-21 15:54:09 -05001242 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001243}
1244EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1245
Mike Christie40a06e72009-03-05 14:46:05 -06001246void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001247 enum iscsi_err err)
1248{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001249 struct iscsi_conn *conn;
1250 struct device *dev;
1251 unsigned long flags;
1252
1253 spin_lock_irqsave(&session->lock, flags);
1254 conn = session->leadconn;
1255 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1256 spin_unlock_irqrestore(&session->lock, flags);
1257 return;
1258 }
1259
1260 dev = get_device(&conn->cls_conn->dev);
1261 spin_unlock_irqrestore(&session->lock, flags);
1262 if (!dev)
1263 return;
1264 /*
1265 * if the host is being removed bypass the connection
1266 * recovery initialization because we are going to kill
1267 * the session.
1268 */
1269 if (err == ISCSI_ERR_INVALID_HOST)
1270 iscsi_conn_error_event(conn->cls_conn, err);
1271 else
1272 iscsi_conn_failure(conn, err);
1273 put_device(dev);
1274}
1275EXPORT_SYMBOL_GPL(iscsi_session_failure);
1276
Mike Christie7996a772006-04-06 21:13:41 -05001277void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1278{
1279 struct iscsi_session *session = conn->session;
1280 unsigned long flags;
1281
1282 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001283 if (session->state == ISCSI_STATE_FAILED) {
1284 spin_unlock_irqrestore(&session->lock, flags);
1285 return;
1286 }
1287
Mike Christie67a61112006-05-30 00:37:20 -05001288 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001289 session->state = ISCSI_STATE_FAILED;
1290 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001291
Mike Christie7996a772006-04-06 21:13:41 -05001292 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1293 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001294 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001295}
1296EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1297
Mike Christie77a23c22007-05-30 12:57:18 -05001298static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1299{
1300 struct iscsi_session *session = conn->session;
1301
1302 /*
1303 * Check for iSCSI window and take care of CmdSN wrap-around
1304 */
Mike Christiee0726402007-07-26 12:46:48 -05001305 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001306 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1307 "%u MaxCmdSN %u CmdSN %u/%u\n",
1308 session->exp_cmdsn, session->max_cmdsn,
1309 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001310 return -ENOSPC;
1311 }
1312 return 0;
1313}
1314
Mike Christie9c19a7d2008-05-21 15:54:09 -05001315static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001316{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001317 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001318 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001319
Mike Christie70b31c12009-08-20 15:11:03 -05001320 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1321 return -ENODATA;
1322
Mike Christie9c19a7d2008-05-21 15:54:09 -05001323 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001324 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001325 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001326 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001327 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001328 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001329 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001330 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001331 }
1332 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001333 return rc;
1334}
1335
Mike Christie7996a772006-04-06 21:13:41 -05001336/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001337 * iscsi_requeue_task - requeue task to run from session workqueue
1338 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001339 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001340 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001341 * this. The session lock must be held. This should only be called
1342 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001343 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001344void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001345{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001346 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001347
Mike Christie3bbaaad2009-05-13 17:57:46 -05001348 /*
1349 * this may be on the requeue list already if the xmit_task callout
1350 * is handling the r2ts while we are adding new ones
1351 */
1352 if (list_empty(&task->running))
1353 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001354 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001355}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001356EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001357
1358/**
Mike Christie7996a772006-04-06 21:13:41 -05001359 * iscsi_data_xmit - xmit any command into the scheduled connection
1360 * @conn: iscsi connection
1361 *
1362 * Notes:
1363 * The function can return -EAGAIN in which case the caller must
1364 * re-schedule it again later or recover. '0' return code means
1365 * successful xmit.
1366 **/
1367static int iscsi_data_xmit(struct iscsi_conn *conn)
1368{
Mike Christie3219e522006-05-30 00:37:28 -05001369 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001370
Mike Christie77a23c22007-05-30 12:57:18 -05001371 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001372 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001373 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001374 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001375 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001376 }
Mike Christie7996a772006-04-06 21:13:41 -05001377
Mike Christie9c19a7d2008-05-21 15:54:09 -05001378 if (conn->task) {
1379 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001380 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001381 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001382 }
1383
Mike Christie77a23c22007-05-30 12:57:18 -05001384 /*
1385 * process mgmt pdus like nops before commands since we should
1386 * only have one nop-out as a ping from us and targets should not
1387 * overflow us with nop-ins
1388 */
1389check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001390 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001391 conn->task = list_entry(conn->mgmtqueue.next,
1392 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001393 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001394 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1395 __iscsi_put_task(conn->task);
1396 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001397 continue;
1398 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001399 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001400 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001401 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001402 }
1403
Mike Christie843c0a82007-12-13 12:43:20 -06001404 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001405 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001406 if (conn->tmf_state == TMF_QUEUED)
1407 break;
1408
Mike Christie3bbaaad2009-05-13 17:57:46 -05001409 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001410 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001411 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001412 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001413 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001414 continue;
1415 }
Mike Christie577577d2008-12-02 00:32:05 -06001416 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1417 if (rc) {
1418 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001419 list_add_tail(&conn->task->running,
1420 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001421 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001422 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001423 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001424 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001425 continue;
1426 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001427 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001428 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001429 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001430 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001431 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001432 * we need to check the mgmt queue for nops that need to
1433 * be sent to aviod starvation
1434 */
Mike Christie843c0a82007-12-13 12:43:20 -06001435 if (!list_empty(&conn->mgmtqueue))
1436 goto check_mgmt;
1437 }
1438
1439 while (!list_empty(&conn->requeue)) {
1440 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1441 break;
1442
Mike Christieb3a7ea82007-12-13 12:43:26 -06001443 /*
1444 * we always do fastlogout - conn stop code will clean up.
1445 */
1446 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1447 break;
1448
Mike Christie9c19a7d2008-05-21 15:54:09 -05001449 conn->task = list_entry(conn->requeue.next,
1450 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001451 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001452 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001453 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001454 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001455 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001456 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001457 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001458 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001459 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001460 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001461
Mike Christie70b31c12009-08-20 15:11:03 -05001462done:
Mike Christie77a23c22007-05-30 12:57:18 -05001463 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001464 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001465}
1466
David Howellsc4028952006-11-22 14:57:56 +00001467static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001468{
David Howellsc4028952006-11-22 14:57:56 +00001469 struct iscsi_conn *conn =
1470 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001471 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001472 /*
1473 * serialize Xmit worker on a per-connection basis.
1474 */
Mike Christie3219e522006-05-30 00:37:28 -05001475 do {
1476 rc = iscsi_data_xmit(conn);
1477 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001478}
1479
Mike Christie577577d2008-12-02 00:32:05 -06001480static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1481 struct scsi_cmnd *sc)
1482{
1483 struct iscsi_task *task;
1484
1485 if (!__kfifo_get(conn->session->cmdpool.queue,
1486 (void *) &task, sizeof(void *)))
1487 return NULL;
1488
1489 sc->SCp.phase = conn->session->age;
1490 sc->SCp.ptr = (char *) task;
1491
1492 atomic_set(&task->refcount, 1);
1493 task->state = ISCSI_TASK_PENDING;
1494 task->conn = conn;
1495 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001496 task->have_checked_conn = false;
1497 task->last_timeout = jiffies;
1498 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001499 INIT_LIST_HEAD(&task->running);
1500 return task;
1501}
1502
Mike Christie7996a772006-04-06 21:13:41 -05001503enum {
1504 FAILURE_BAD_HOST = 1,
1505 FAILURE_SESSION_FAILED,
1506 FAILURE_SESSION_FREED,
1507 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001508 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001509 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001510 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001511 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001512 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001513 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001514};
1515
1516int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1517{
Mike Christie75613522008-05-21 15:53:59 -05001518 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001519 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001520 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001521 int reason = 0;
1522 struct iscsi_session *session;
1523 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001524 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001525
1526 sc->scsi_done = done;
1527 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001528 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001529
1530 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001531 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001532 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001533
Mike Christie75613522008-05-21 15:53:59 -05001534 cls_session = starget_to_session(scsi_target(sc->device));
1535 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001536 spin_lock(&session->lock);
1537
Mike Christie75613522008-05-21 15:53:59 -05001538 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001539 if (reason) {
1540 sc->result = reason;
1541 goto fault;
1542 }
1543
Mike Christie301e0f72009-05-13 17:57:47 -05001544 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001545 /*
1546 * to handle the race between when we set the recovery state
1547 * and block the session we requeue here (commands could
1548 * be entering our queuecommand while a block is starting
1549 * up because the block code is not locked)
1550 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001551 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001552 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001553 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001554 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001555 sc->result = DID_IMM_RETRY << 16;
1556 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001557 case ISCSI_STATE_LOGGING_OUT:
1558 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001559 sc->result = DID_IMM_RETRY << 16;
1560 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001561 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001562 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001563 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001564 break;
1565 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001566 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001567 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001568 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001569 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001570 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001571 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001572 }
Mike Christie7996a772006-04-06 21:13:41 -05001573 goto fault;
1574 }
1575
Mike Christie7996a772006-04-06 21:13:41 -05001576 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001577 if (!conn) {
1578 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001579 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001580 goto fault;
1581 }
Mike Christie7996a772006-04-06 21:13:41 -05001582
Mike Christie661134a2009-09-05 07:35:33 +05301583 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1584 reason = FAILURE_SESSION_IN_RECOVERY;
1585 sc->result = DID_REQUEUE;
1586 goto fault;
1587 }
1588
Mike Christie77a23c22007-05-30 12:57:18 -05001589 if (iscsi_check_cmdsn_window_closed(conn)) {
1590 reason = FAILURE_WINDOW_CLOSED;
1591 goto reject;
1592 }
1593
Mike Christie577577d2008-12-02 00:32:05 -06001594 task = iscsi_alloc_task(conn, sc);
1595 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001596 reason = FAILURE_OOM;
1597 goto reject;
1598 }
Mike Christie7996a772006-04-06 21:13:41 -05001599
Mike Christie1336aed2009-05-13 17:57:48 -05001600 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001601 reason = iscsi_prep_scsi_cmd_pdu(task);
1602 if (reason) {
1603 if (reason == -ENOMEM) {
1604 reason = FAILURE_OOM;
1605 goto prepd_reject;
1606 } else {
1607 sc->result = DID_ABORT << 16;
1608 goto prepd_fault;
1609 }
Mike Christie052d0142008-05-21 15:54:05 -05001610 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001611 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001612 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001613 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001614 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001615 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001616 } else {
1617 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001618 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001619 }
Mike Christie052d0142008-05-21 15:54:05 -05001620
1621 session->queued_cmdsn++;
1622 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001623 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001624 return 0;
1625
Mike Christie577577d2008-12-02 00:32:05 -06001626prepd_reject:
1627 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001628 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001629reject:
1630 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001631 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1632 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001633 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001634 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001635
Mike Christie577577d2008-12-02 00:32:05 -06001636prepd_fault:
1637 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001638 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001639fault:
1640 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001641 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1642 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001643 if (!scsi_bidi_cmnd(sc))
1644 scsi_set_resid(sc, scsi_bufflen(sc));
1645 else {
1646 scsi_out(sc)->resid = scsi_out(sc)->length;
1647 scsi_in(sc)->resid = scsi_in(sc)->length;
1648 }
Mike Christie052d0142008-05-21 15:54:05 -05001649 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001650 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001651 return 0;
1652}
1653EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1654
Mike Christiee881a172009-10-15 17:46:39 -07001655int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
Mike Christie7996a772006-04-06 21:13:41 -05001656{
Mike Christiee881a172009-10-15 17:46:39 -07001657 if (reason != SCSI_QDEPTH_DEFAULT)
1658 return -EOPNOTSUPP;
1659
Mike Christie7996a772006-04-06 21:13:41 -05001660 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1661 return sdev->queue_depth;
1662}
1663EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1664
Mike Christie6b5d6c42009-04-21 15:32:32 -05001665int iscsi_target_alloc(struct scsi_target *starget)
1666{
1667 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1668 struct iscsi_session *session = cls_session->dd_data;
1669
1670 starget->can_queue = session->scsi_cmds_max;
1671 return 0;
1672}
1673EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1674
Mike Christie7996a772006-04-06 21:13:41 -05001675void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1676{
Mike Christie75613522008-05-21 15:53:59 -05001677 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001678
1679 spin_lock_bh(&session->lock);
1680 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001681 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001682 if (session->leadconn)
1683 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001684 }
1685 spin_unlock_bh(&session->lock);
1686}
1687EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1688
Mike Christie8e124522008-09-24 11:46:12 -05001689int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001690{
Mike Christie75613522008-05-21 15:53:59 -05001691 struct iscsi_cls_session *cls_session;
1692 struct iscsi_session *session;
1693 struct iscsi_conn *conn;
1694
1695 cls_session = starget_to_session(scsi_target(sc->device));
1696 session = cls_session->dd_data;
1697 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001698
Mike Christiebc436b22007-12-13 12:43:28 -06001699 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001700 spin_lock_bh(&session->lock);
1701 if (session->state == ISCSI_STATE_TERMINATE) {
1702failed:
Erez Zilberbd2199d2009-06-15 22:11:10 -05001703 ISCSI_DBG_EH(session,
1704 "failing target reset: Could not log back into "
1705 "target [age %d]\n",
1706 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001707 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001708 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001709 return FAILED;
1710 }
1711
Mike Christie7996a772006-04-06 21:13:41 -05001712 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001713 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001714 /*
1715 * we drop the lock here but the leadconn cannot be destoyed while
1716 * we are in the scsi eh
1717 */
Mike Christie843c0a82007-12-13 12:43:20 -06001718 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001719
Erez Zilberbd2199d2009-06-15 22:11:10 -05001720 ISCSI_DBG_EH(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001721 wait_event_interruptible(conn->ehwait,
1722 session->state == ISCSI_STATE_TERMINATE ||
1723 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001724 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001725 if (signal_pending(current))
1726 flush_signals(current);
1727
Mike Christiebc436b22007-12-13 12:43:28 -06001728 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001729 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001730 if (session->state == ISCSI_STATE_LOGGED_IN) {
1731 ISCSI_DBG_EH(session,
1732 "target reset succeeded\n");
1733 } else
Mike Christie7996a772006-04-06 21:13:41 -05001734 goto failed;
1735 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001736 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001737 return SUCCESS;
1738}
Mike Christie8e124522008-09-24 11:46:12 -05001739EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001740
Mike Christie843c0a82007-12-13 12:43:20 -06001741static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001742{
Mike Christie843c0a82007-12-13 12:43:20 -06001743 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001744 struct iscsi_session *session = conn->session;
1745
1746 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001747 if (conn->tmf_state == TMF_QUEUED) {
1748 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001749 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001750 /* unblock eh_abort() */
1751 wake_up(&conn->ehwait);
1752 }
1753 spin_unlock(&session->lock);
1754}
1755
Mike Christie9c19a7d2008-05-21 15:54:09 -05001756static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001757 struct iscsi_tm *hdr, int age,
1758 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001759{
Mike Christie7996a772006-04-06 21:13:41 -05001760 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001761 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001762
Mike Christie9c19a7d2008-05-21 15:54:09 -05001763 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001764 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001765 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001766 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001767 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001768 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001769 ISCSI_DBG_EH(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001770 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001771 }
Mike Christie843c0a82007-12-13 12:43:20 -06001772 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001773 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001774 conn->tmf_timer.function = iscsi_tmf_timedout;
1775 conn->tmf_timer.data = (unsigned long)conn;
1776 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001777 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001778
Mike Christie7996a772006-04-06 21:13:41 -05001779 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001780 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001781
1782 /*
1783 * block eh thread until:
1784 *
Mike Christie843c0a82007-12-13 12:43:20 -06001785 * 1) tmf response
1786 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001787 * 3) session is terminated or restarted or userspace has
1788 * given up on recovery
1789 */
Mike Christie843c0a82007-12-13 12:43:20 -06001790 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001791 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001792 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001793 if (signal_pending(current))
1794 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001795 del_timer_sync(&conn->tmf_timer);
1796
Mike Christie6724add2007-08-15 01:38:30 -05001797 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001798 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001799 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001800 if (age != session->age ||
1801 session->state != ISCSI_STATE_LOGGED_IN)
1802 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001803 return 0;
1804}
1805
1806/*
Mike Christie843c0a82007-12-13 12:43:20 -06001807 * Fail commands. session lock held and recv side suspended and xmit
1808 * thread flushed
1809 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001810static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1811 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001812{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001813 struct iscsi_task *task;
1814 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001815
Mike Christie3bbaaad2009-05-13 17:57:46 -05001816 for (i = 0; i < conn->session->cmds_max; i++) {
1817 task = conn->session->cmds[i];
1818 if (!task->sc || task->state == ISCSI_TASK_FREE)
1819 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001820
Mike Christie3bbaaad2009-05-13 17:57:46 -05001821 if (lun != -1 && lun != task->sc->device->lun)
1822 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001823
Mike Christie3bbaaad2009-05-13 17:57:46 -05001824 ISCSI_DBG_SESSION(conn->session,
1825 "failing sc %p itt 0x%x state %d\n",
1826 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001827 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001828 }
1829}
1830
Mike Christie661134a2009-09-05 07:35:33 +05301831/**
1832 * iscsi_suspend_queue - suspend iscsi_queuecommand
1833 * @conn: iscsi conn to stop queueing IO on
1834 *
1835 * This grabs the session lock to make sure no one is in
1836 * xmit_task/queuecommand, and then sets suspend to prevent
1837 * new commands from being queued. This only needs to be called
1838 * by offload drivers that need to sync a path like ep disconnect
1839 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1840 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1841 */
1842void iscsi_suspend_queue(struct iscsi_conn *conn)
1843{
1844 spin_lock_bh(&conn->session->lock);
1845 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1846 spin_unlock_bh(&conn->session->lock);
1847}
1848EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1849
1850/**
1851 * iscsi_suspend_tx - suspend iscsi_data_xmit
1852 * @conn: iscsi conn tp stop processing IO on.
1853 *
1854 * This function sets the suspend bit to prevent iscsi_data_xmit
1855 * from sending new IO, and if work is queued on the xmit thread
1856 * it will wait for it to be completed.
1857 */
Mike Christieb40977d2008-05-21 15:54:03 -05001858void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001859{
Mike Christie32ae7632009-03-05 14:46:03 -06001860 struct Scsi_Host *shost = conn->session->host;
1861 struct iscsi_host *ihost = shost_priv(shost);
1862
Mike Christie6724add2007-08-15 01:38:30 -05001863 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001864 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001865 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001866}
Mike Christieb40977d2008-05-21 15:54:03 -05001867EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001868
1869static void iscsi_start_tx(struct iscsi_conn *conn)
1870{
1871 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001872 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001873}
1874
Mike Christie4c48a822009-05-13 17:57:45 -05001875/*
1876 * We want to make sure a ping is in flight. It has timed out.
1877 * And we are not busy processing a pdu that is making
1878 * progress but got started before the ping and is taking a while
1879 * to complete so the ping is just stuck behind it in a queue.
1880 */
1881static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1882{
1883 if (conn->ping_task &&
1884 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1885 (conn->ping_timeout * HZ), jiffies))
1886 return 1;
1887 else
1888 return 0;
1889}
1890
Mike Christied355e572009-06-15 22:11:08 -05001891static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001892{
Mike Christied355e572009-06-15 22:11:08 -05001893 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1894 struct iscsi_task *task = NULL;
Mike Christief6d51802007-12-13 12:43:30 -06001895 struct iscsi_cls_session *cls_session;
1896 struct iscsi_session *session;
1897 struct iscsi_conn *conn;
Mike Christief6d51802007-12-13 12:43:30 -06001898
Mike Christied355e572009-06-15 22:11:08 -05001899 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001900 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001901
Erez Zilberbd2199d2009-06-15 22:11:10 -05001902 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001903
1904 spin_lock(&session->lock);
1905 if (session->state != ISCSI_STATE_LOGGED_IN) {
1906 /*
1907 * We are probably in the middle of iscsi recovery so let
1908 * that complete and handle the error.
1909 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001910 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001911 goto done;
1912 }
1913
1914 conn = session->leadconn;
1915 if (!conn) {
1916 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001917 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001918 goto done;
1919 }
1920
Mike Christied355e572009-06-15 22:11:08 -05001921 task = (struct iscsi_task *)sc->SCp.ptr;
1922 if (!task)
1923 goto done;
1924 /*
1925 * If we have sent (at least queued to the network layer) a pdu or
1926 * recvd one for the task since the last timeout ask for
1927 * more time. If on the next timeout we have not made progress
1928 * we can check if it is the task or connection when we send the
1929 * nop as a ping.
1930 */
1931 if (time_after_eq(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001932 ISCSI_DBG_EH(session, "Command making progress. Asking "
1933 "scsi-ml for more time to complete. "
1934 "Last data recv at %lu. Last timeout was at "
1935 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001936 task->have_checked_conn = false;
1937 rc = BLK_EH_RESET_TIMER;
1938 goto done;
1939 }
1940
Mike Christief6d51802007-12-13 12:43:30 -06001941 if (!conn->recv_timeout && !conn->ping_timeout)
1942 goto done;
1943 /*
1944 * if the ping timedout then we are in the middle of cleaning up
1945 * and can let the iscsi eh handle it
1946 */
Mike Christie4c48a822009-05-13 17:57:45 -05001947 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001948 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001949 goto done;
1950 }
Mike Christied355e572009-06-15 22:11:08 -05001951
1952 /* Assumes nop timeout is shorter than scsi cmd timeout */
1953 if (task->have_checked_conn)
1954 goto done;
1955
Mike Christief6d51802007-12-13 12:43:30 -06001956 /*
Mike Christied355e572009-06-15 22:11:08 -05001957 * Checking the transport already or nop from a cmd timeout still
1958 * running
Mike Christief6d51802007-12-13 12:43:30 -06001959 */
Mike Christied355e572009-06-15 22:11:08 -05001960 if (conn->ping_task) {
1961 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001962 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001963 goto done;
1964 }
1965
Mike Christied355e572009-06-15 22:11:08 -05001966 /* Make sure there is a transport check done */
1967 iscsi_send_nopout(conn, NULL);
1968 task->have_checked_conn = true;
1969 rc = BLK_EH_RESET_TIMER;
1970
Mike Christief6d51802007-12-13 12:43:30 -06001971done:
Mike Christied355e572009-06-15 22:11:08 -05001972 if (task)
1973 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06001974 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001975 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1976 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001977 return rc;
1978}
1979
1980static void iscsi_check_transport_timeouts(unsigned long data)
1981{
1982 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1983 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001984 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001985
1986 spin_lock(&session->lock);
1987 if (session->state != ISCSI_STATE_LOGGED_IN)
1988 goto done;
1989
Mike Christie4cf10432008-05-07 20:43:52 -05001990 recv_timeout = conn->recv_timeout;
1991 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001992 goto done;
1993
Mike Christie4cf10432008-05-07 20:43:52 -05001994 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001995 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001996
1997 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001998 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001999 "expired, recv timeout %d, last rx %lu, "
2000 "last ping %lu, now %lu\n",
2001 conn->ping_timeout, conn->recv_timeout,
2002 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06002003 spin_unlock(&session->lock);
2004 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2005 return;
2006 }
2007
Mike Christie4cf10432008-05-07 20:43:52 -05002008 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05002009 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06002010 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05002011 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05002012 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06002013 } else
Mike Christie4cf10432008-05-07 20:43:52 -05002014 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06002015
Mike Christie1b2c7af2009-03-05 14:45:58 -06002016 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06002017 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06002018done:
2019 spin_unlock(&session->lock);
2020}
2021
Mike Christie9c19a7d2008-05-21 15:54:09 -05002022static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06002023 struct iscsi_tm *hdr)
2024{
2025 memset(hdr, 0, sizeof(*hdr));
2026 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2027 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2028 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06002029 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2030 hdr->rtt = task->hdr_itt;
2031 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06002032}
2033
Mike Christie7996a772006-04-06 21:13:41 -05002034int iscsi_eh_abort(struct scsi_cmnd *sc)
2035{
Mike Christie75613522008-05-21 15:53:59 -05002036 struct iscsi_cls_session *cls_session;
2037 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04002038 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002039 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06002040 struct iscsi_tm *hdr;
2041 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05002042
Mike Christie75613522008-05-21 15:53:59 -05002043 cls_session = starget_to_session(scsi_target(sc->device));
2044 session = cls_session->dd_data;
2045
Erez Zilberbd2199d2009-06-15 22:11:10 -05002046 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05002047
Mike Christie6724add2007-08-15 01:38:30 -05002048 mutex_lock(&session->eh_mutex);
2049 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04002050 /*
2051 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2052 * got the command.
2053 */
2054 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002055 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2056 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05002057 spin_unlock_bh(&session->lock);
2058 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04002059 return SUCCESS;
2060 }
2061
Mike Christie7996a772006-04-06 21:13:41 -05002062 /*
2063 * If we are not logged in or we have started a new session
2064 * then let the host reset code handle this
2065 */
Mike Christie843c0a82007-12-13 12:43:20 -06002066 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2067 sc->SCp.phase != session->age) {
2068 spin_unlock_bh(&session->lock);
2069 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002070 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05002071 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06002072 return FAILED;
2073 }
2074
2075 conn = session->leadconn;
2076 conn->eh_abort_cnt++;
2077 age = session->age;
2078
Mike Christie9c19a7d2008-05-21 15:54:09 -05002079 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002080 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2081 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002082
Mike Christie9c19a7d2008-05-21 15:54:09 -05002083 /* task completed before time out */
2084 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002085 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002086 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002087 }
Mike Christie7996a772006-04-06 21:13:41 -05002088
Mike Christie9c19a7d2008-05-21 15:54:09 -05002089 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002090 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002091 goto success;
2092 }
Mike Christie7996a772006-04-06 21:13:41 -05002093
Mike Christie843c0a82007-12-13 12:43:20 -06002094 /* only have one tmf outstanding at a time */
2095 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002096 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002097 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002098
Mike Christie843c0a82007-12-13 12:43:20 -06002099 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002100 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002101
Mike Christie9c19a7d2008-05-21 15:54:09 -05002102 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002103 rc = FAILED;
2104 goto failed;
2105 }
2106
2107 switch (conn->tmf_state) {
2108 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002109 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002110 /*
2111 * stop tx side incase the target had sent a abort rsp but
2112 * the initiator was still writing out data.
2113 */
Mike Christie6724add2007-08-15 01:38:30 -05002114 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002115 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002116 * we do not stop the recv side because targets have been
2117 * good and have never sent us a successful tmf response
2118 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002119 */
Mike Christie6187c242009-07-15 15:02:57 -05002120 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002121 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002122 conn->tmf_state = TMF_INITIAL;
Mike Christie6187c242009-07-15 15:02:57 -05002123 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002124 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002125 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002126 case TMF_TIMEDOUT:
2127 spin_unlock_bh(&session->lock);
2128 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2129 goto failed_unlocked;
2130 case TMF_NOT_FOUND:
2131 if (!sc->SCp.ptr) {
2132 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002133 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002134 ISCSI_DBG_EH(session, "sc completed while abort in "
2135 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002136 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002137 }
2138 /* fall through */
2139 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002140 conn->tmf_state = TMF_INITIAL;
2141 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002142 }
2143
Mike Christie77a23c22007-05-30 12:57:18 -05002144success:
Mike Christie7996a772006-04-06 21:13:41 -05002145 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002146success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002147 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2148 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002149 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002150 return SUCCESS;
2151
2152failed:
2153 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002154failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002155 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2156 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002157 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002158 return FAILED;
2159}
2160EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2161
Mike Christie843c0a82007-12-13 12:43:20 -06002162static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2163{
2164 memset(hdr, 0, sizeof(*hdr));
2165 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2166 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2167 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2168 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002169 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002170}
2171
2172int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2173{
Mike Christie75613522008-05-21 15:53:59 -05002174 struct iscsi_cls_session *cls_session;
2175 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002176 struct iscsi_conn *conn;
2177 struct iscsi_tm *hdr;
2178 int rc = FAILED;
2179
Mike Christie75613522008-05-21 15:53:59 -05002180 cls_session = starget_to_session(scsi_target(sc->device));
2181 session = cls_session->dd_data;
2182
Erez Zilberbd2199d2009-06-15 22:11:10 -05002183 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002184
2185 mutex_lock(&session->eh_mutex);
2186 spin_lock_bh(&session->lock);
2187 /*
2188 * Just check if we are not logged in. We cannot check for
2189 * the phase because the reset could come from a ioctl.
2190 */
2191 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2192 goto unlock;
2193 conn = session->leadconn;
2194
2195 /* only have one tmf outstanding at a time */
2196 if (conn->tmf_state != TMF_INITIAL)
2197 goto unlock;
2198 conn->tmf_state = TMF_QUEUED;
2199
2200 hdr = &conn->tmhdr;
2201 iscsi_prep_lun_reset_pdu(sc, hdr);
2202
Mike Christie9c19a7d2008-05-21 15:54:09 -05002203 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002204 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002205 rc = FAILED;
2206 goto unlock;
2207 }
2208
2209 switch (conn->tmf_state) {
2210 case TMF_SUCCESS:
2211 break;
2212 case TMF_TIMEDOUT:
2213 spin_unlock_bh(&session->lock);
2214 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2215 goto done;
2216 default:
2217 conn->tmf_state = TMF_INITIAL;
2218 goto unlock;
2219 }
2220
2221 rc = SUCCESS;
2222 spin_unlock_bh(&session->lock);
2223
2224 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002225
Mike Christiea3439142008-09-24 11:46:15 -05002226 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002227 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002228 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002229 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002230
2231 iscsi_start_tx(conn);
2232 goto done;
2233
2234unlock:
2235 spin_unlock_bh(&session->lock);
2236done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002237 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2238 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002239 mutex_unlock(&session->eh_mutex);
2240 return rc;
2241}
2242EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2243
Olaf Kirch63203772007-12-13 12:43:25 -06002244/*
2245 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2246 * should be accessed via kfifo_{get,put} on q->queue.
2247 * Optionally, the caller can obtain the array of object pointers
2248 * by passing in a non-NULL @items pointer
2249 */
Mike Christie7996a772006-04-06 21:13:41 -05002250int
Olaf Kirch63203772007-12-13 12:43:25 -06002251iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002252{
Olaf Kirch63203772007-12-13 12:43:25 -06002253 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002254
Olaf Kirch63203772007-12-13 12:43:25 -06002255 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002256
2257 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002258
2259 /* If the user passed an items pointer, he wants a copy of
2260 * the array. */
2261 if (items)
2262 num_arrays++;
2263 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2264 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002265 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002266
2267 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2268 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002269 if (IS_ERR(q->queue)) {
2270 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002271 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002272 }
Mike Christie7996a772006-04-06 21:13:41 -05002273
2274 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002275 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002276 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002277 q->max = i;
2278 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002279 }
Mike Christie7996a772006-04-06 21:13:41 -05002280 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2281 }
Olaf Kirch63203772007-12-13 12:43:25 -06002282
2283 if (items) {
2284 *items = q->pool + max;
2285 memcpy(*items, q->pool, max * sizeof(void *));
2286 }
2287
Mike Christie7996a772006-04-06 21:13:41 -05002288 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002289
2290enomem:
2291 iscsi_pool_free(q);
2292 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002293}
2294EXPORT_SYMBOL_GPL(iscsi_pool_init);
2295
Olaf Kirch63203772007-12-13 12:43:25 -06002296void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002297{
2298 int i;
2299
2300 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002301 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002302 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002303 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002304}
2305EXPORT_SYMBOL_GPL(iscsi_pool_free);
2306
Mike Christiea4804cd2008-05-21 15:54:00 -05002307/**
2308 * iscsi_host_add - add host to system
2309 * @shost: scsi host
2310 * @pdev: parent device
2311 *
2312 * This should be called by partial offload and software iscsi drivers
2313 * to add a host to the system.
2314 */
2315int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002316{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002317 if (!shost->can_queue)
2318 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2319
Mike Christie4d108352009-03-05 14:46:04 -06002320 if (!shost->cmd_per_lun)
2321 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2322
Mike Christie308cec12009-02-06 12:06:20 -06002323 if (!shost->transportt->eh_timed_out)
2324 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002325 return scsi_add_host(shost, pdev);
2326}
2327EXPORT_SYMBOL_GPL(iscsi_host_add);
2328
2329/**
2330 * iscsi_host_alloc - allocate a host and driver data
2331 * @sht: scsi host template
2332 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002333 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002334 *
2335 * This should be called by partial offload and software iscsi drivers.
2336 * To access the driver specific memory use the iscsi_host_priv() macro.
2337 */
2338struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002339 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002340{
2341 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002342 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002343
2344 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2345 if (!shost)
2346 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002347 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002348
2349 if (xmit_can_sleep) {
2350 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2351 "iscsi_q_%d", shost->host_no);
2352 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2353 if (!ihost->workq)
2354 goto free_host;
2355 }
2356
Mike Christiee5bd7b52008-09-24 11:46:10 -05002357 spin_lock_init(&ihost->lock);
2358 ihost->state = ISCSI_HOST_SETUP;
2359 ihost->num_sessions = 0;
2360 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002361 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002362
2363free_host:
2364 scsi_host_put(shost);
2365 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002366}
Mike Christiea4804cd2008-05-21 15:54:00 -05002367EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002368
Mike Christiee5bd7b52008-09-24 11:46:10 -05002369static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2370{
Mike Christie40a06e72009-03-05 14:46:05 -06002371 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002372}
2373
Mike Christiea4804cd2008-05-21 15:54:00 -05002374/**
2375 * iscsi_host_remove - remove host and sessions
2376 * @shost: scsi host
2377 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002378 * If there are any sessions left, this will initiate the removal and wait
2379 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002380 */
2381void iscsi_host_remove(struct Scsi_Host *shost)
2382{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002383 struct iscsi_host *ihost = shost_priv(shost);
2384 unsigned long flags;
2385
2386 spin_lock_irqsave(&ihost->lock, flags);
2387 ihost->state = ISCSI_HOST_REMOVED;
2388 spin_unlock_irqrestore(&ihost->lock, flags);
2389
2390 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2391 wait_event_interruptible(ihost->session_removal_wq,
2392 ihost->num_sessions == 0);
2393 if (signal_pending(current))
2394 flush_signals(current);
2395
Mike Christiea4804cd2008-05-21 15:54:00 -05002396 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002397 if (ihost->workq)
2398 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002399}
2400EXPORT_SYMBOL_GPL(iscsi_host_remove);
2401
2402void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002403{
2404 struct iscsi_host *ihost = shost_priv(shost);
2405
2406 kfree(ihost->netdev);
2407 kfree(ihost->hwaddress);
2408 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002409 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002410}
Mike Christiea4804cd2008-05-21 15:54:00 -05002411EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002412
Mike Christiee5bd7b52008-09-24 11:46:10 -05002413static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2414{
2415 struct iscsi_host *ihost = shost_priv(shost);
2416 unsigned long flags;
2417
2418 shost = scsi_host_get(shost);
2419 if (!shost) {
2420 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2421 "of session teardown event because host already "
2422 "removed.\n");
2423 return;
2424 }
2425
2426 spin_lock_irqsave(&ihost->lock, flags);
2427 ihost->num_sessions--;
2428 if (ihost->num_sessions == 0)
2429 wake_up(&ihost->session_removal_wq);
2430 spin_unlock_irqrestore(&ihost->lock, flags);
2431 scsi_host_put(shost);
2432}
2433
Mike Christie75613522008-05-21 15:53:59 -05002434/**
2435 * iscsi_session_setup - create iscsi cls session and host and session
2436 * @iscsit: iscsi transport template
2437 * @shost: scsi host
2438 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002439 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002440 * @initial_cmdsn: initial CmdSN
2441 *
2442 * This can be used by software iscsi_transports that allocate
2443 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002444 *
2445 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2446 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2447 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002448 */
2449struct iscsi_cls_session *
2450iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302451 uint16_t cmds_max, int dd_size, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002452 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002453{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002454 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002455 struct iscsi_session *session;
2456 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002457 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002458 unsigned long flags;
2459
2460 spin_lock_irqsave(&ihost->lock, flags);
2461 if (ihost->state == ISCSI_HOST_REMOVED) {
2462 spin_unlock_irqrestore(&ihost->lock, flags);
2463 return NULL;
2464 }
2465 ihost->num_sessions++;
2466 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002467
2468 if (!total_cmds)
2469 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002470 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002471 * The iscsi layer needs some tasks for nop handling and tmfs,
2472 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2473 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002474 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002475 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2476 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2477 "must be a power of two that is at least %d.\n",
2478 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002479 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002480 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002481
2482 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2483 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2484 "must be a power of 2 less than or equal to %d.\n",
2485 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2486 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2487 }
2488
2489 if (!is_power_of_2(total_cmds)) {
2490 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2491 "must be a power of 2.\n", total_cmds);
2492 total_cmds = rounddown_pow_of_two(total_cmds);
2493 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2494 return NULL;
2495 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2496 total_cmds);
2497 }
2498 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002499
Mike Christie5d91e202008-05-21 15:54:01 -05002500 cls_session = iscsi_alloc_session(shost, iscsit,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302501 sizeof(struct iscsi_session) +
2502 dd_size);
Mike Christie75613522008-05-21 15:53:59 -05002503 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002504 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002505 session = cls_session->dd_data;
2506 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002507 session->host = shost;
2508 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002509 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002510 session->lu_reset_timeout = 15;
2511 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002512 session->scsi_cmds_max = scsi_cmds;
2513 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002514 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002515 session->exp_cmdsn = initial_cmdsn + 1;
2516 session->max_cmdsn = initial_cmdsn + 1;
2517 session->max_r2t = 1;
2518 session->tt = iscsit;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302519 session->dd_data = cls_session->dd_data + sizeof(*session);
Mike Christie6724add2007-08-15 01:38:30 -05002520 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002521 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002522
2523 /* initialize SCSI PDU commands pool */
2524 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2525 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002526 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002527 goto cmdpool_alloc_fail;
2528
2529 /* pre-format cmds pool with ITT */
2530 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002531 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002532
Mike Christie9c19a7d2008-05-21 15:54:09 -05002533 if (cmd_task_size)
2534 task->dd_data = &task[1];
2535 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002536 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002537 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002538 }
2539
Mike Christief53a88d2006-06-28 12:00:27 -05002540 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002541 goto module_get_fail;
2542
Mike Christie79706342008-05-21 15:54:12 -05002543 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002544 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002545
Mike Christie7996a772006-04-06 21:13:41 -05002546 return cls_session;
2547
2548cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002549 module_put(iscsit->owner);
2550module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002551 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002552cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002553 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002554dec_session_count:
2555 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002556 return NULL;
2557}
2558EXPORT_SYMBOL_GPL(iscsi_session_setup);
2559
2560/**
2561 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002562 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002563 *
Mike Christie75613522008-05-21 15:53:59 -05002564 * The driver must have called iscsi_remove_session before
2565 * calling this.
2566 */
Mike Christie7996a772006-04-06 21:13:41 -05002567void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2568{
Mike Christie75613522008-05-21 15:53:59 -05002569 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002570 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002571 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002572
Olaf Kirch63203772007-12-13 12:43:25 -06002573 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002574
Mike Christieb2c64162007-05-30 12:57:16 -05002575 kfree(session->password);
2576 kfree(session->password_in);
2577 kfree(session->username);
2578 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002579 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002580 kfree(session->initiatorname);
2581 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002582
Mike Christie75613522008-05-21 15:53:59 -05002583 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002584 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002585 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002586}
2587EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2588
2589/**
2590 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2591 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002592 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002593 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002594 */
Mike Christie7996a772006-04-06 21:13:41 -05002595struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002596iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2597 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002598{
Mike Christie75613522008-05-21 15:53:59 -05002599 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002600 struct iscsi_conn *conn;
2601 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002602 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002603
Mike Christie5d91e202008-05-21 15:54:01 -05002604 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2605 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002606 if (!cls_conn)
2607 return NULL;
2608 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002609 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002610
Mike Christie5d91e202008-05-21 15:54:01 -05002611 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002612 conn->session = session;
2613 conn->cls_conn = cls_conn;
2614 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2615 conn->id = conn_idx;
2616 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002617 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002618
2619 init_timer(&conn->transport_timer);
2620 conn->transport_timer.data = (unsigned long)conn;
2621 conn->transport_timer.function = iscsi_check_transport_timeouts;
2622
Mike Christie843c0a82007-12-13 12:43:20 -06002623 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002624 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002625 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002626 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002627
Mike Christie9c19a7d2008-05-21 15:54:09 -05002628 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002629 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002630 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002631 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002632 sizeof(void*))) {
2633 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002634 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002635 }
2636 spin_unlock_bh(&session->lock);
2637
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002638 data = (char *) __get_free_pages(GFP_KERNEL,
2639 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002640 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002641 goto login_task_data_alloc_fail;
2642 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002643
Mike Christie843c0a82007-12-13 12:43:20 -06002644 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002645 init_waitqueue_head(&conn->ehwait);
2646
2647 return cls_conn;
2648
Mike Christie9c19a7d2008-05-21 15:54:09 -05002649login_task_data_alloc_fail:
2650 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002651 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002652login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002653 iscsi_destroy_conn(cls_conn);
2654 return NULL;
2655}
2656EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2657
2658/**
2659 * iscsi_conn_teardown - teardown iscsi connection
2660 * cls_conn: iscsi class connection
2661 *
2662 * TODO: we may need to make this into a two step process
2663 * like scsi-mls remove + put host
2664 */
2665void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2666{
2667 struct iscsi_conn *conn = cls_conn->dd_data;
2668 struct iscsi_session *session = conn->session;
2669 unsigned long flags;
2670
Mike Christief6d51802007-12-13 12:43:30 -06002671 del_timer_sync(&conn->transport_timer);
2672
Mike Christie7996a772006-04-06 21:13:41 -05002673 spin_lock_bh(&session->lock);
2674 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2675 if (session->leadconn == conn) {
2676 /*
2677 * leading connection? then give up on recovery.
2678 */
2679 session->state = ISCSI_STATE_TERMINATE;
2680 wake_up(&conn->ehwait);
2681 }
2682 spin_unlock_bh(&session->lock);
2683
Mike Christie7996a772006-04-06 21:13:41 -05002684 /*
2685 * Block until all in-progress commands for this connection
2686 * time out or fail.
2687 */
2688 for (;;) {
2689 spin_lock_irqsave(session->host->host_lock, flags);
2690 if (!session->host->host_busy) { /* OK for ERL == 0 */
2691 spin_unlock_irqrestore(session->host->host_lock, flags);
2692 break;
2693 }
2694 spin_unlock_irqrestore(session->host->host_lock, flags);
2695 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002696 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2697 "host_busy %d host_failed %d\n",
2698 session->host->host_busy,
2699 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002700 /*
2701 * force eh_abort() to unblock
2702 */
2703 wake_up(&conn->ehwait);
2704 }
2705
Mike Christie779ea122007-02-28 17:32:15 -06002706 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002707 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002708
Mike Christie7996a772006-04-06 21:13:41 -05002709 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002710 free_pages((unsigned long) conn->data,
2711 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002712 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002713 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002714 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002715 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002716 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002717 spin_unlock_bh(&session->lock);
2718
Mike Christie7996a772006-04-06 21:13:41 -05002719 iscsi_destroy_conn(cls_conn);
2720}
2721EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2722
2723int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2724{
2725 struct iscsi_conn *conn = cls_conn->dd_data;
2726 struct iscsi_session *session = conn->session;
2727
Mike Christieffd04362006-08-31 18:09:24 -04002728 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002729 iscsi_conn_printk(KERN_ERR, conn,
2730 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002731 return -EPERM;
2732 }
2733
Mike Christiedb98ccd2006-08-31 18:09:31 -04002734 if ((session->imm_data_en || !session->initial_r2t_en) &&
2735 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002736 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2737 "first_burst %d max_burst %d\n",
2738 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002739 return -EINVAL;
2740 }
2741
Mike Christief6d51802007-12-13 12:43:30 -06002742 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002743 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2744 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002745 conn->recv_timeout = 5;
2746 }
2747
2748 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002749 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2750 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002751 conn->ping_timeout = 5;
2752 }
2753
Mike Christie7996a772006-04-06 21:13:41 -05002754 spin_lock_bh(&session->lock);
2755 conn->c_stage = ISCSI_CONN_STARTED;
2756 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002757 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002758
Mike Christief6d51802007-12-13 12:43:30 -06002759 conn->last_recv = jiffies;
2760 conn->last_ping = jiffies;
2761 if (conn->recv_timeout && conn->ping_timeout)
2762 mod_timer(&conn->transport_timer,
2763 jiffies + (conn->recv_timeout * HZ));
2764
Mike Christie7996a772006-04-06 21:13:41 -05002765 switch(conn->stop_stage) {
2766 case STOP_CONN_RECOVER:
2767 /*
2768 * unblock eh_abort() if it is blocked. re-try all
2769 * commands after successful recovery
2770 */
Mike Christie7996a772006-04-06 21:13:41 -05002771 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002772 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002773 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002774 if (session->age == 16)
2775 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002776 break;
Mike Christie7996a772006-04-06 21:13:41 -05002777 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002778 conn->stop_stage = 0;
2779 break;
Mike Christie7996a772006-04-06 21:13:41 -05002780 default:
2781 break;
2782 }
2783 spin_unlock_bh(&session->lock);
2784
Mike Christie75613522008-05-21 15:53:59 -05002785 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002786 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002787 return 0;
2788}
2789EXPORT_SYMBOL_GPL(iscsi_conn_start);
2790
2791static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002792fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002793{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002794 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002795 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002796
Mike Christie3bbaaad2009-05-13 17:57:46 -05002797 for (i = 0; i < conn->session->cmds_max; i++) {
2798 task = conn->session->cmds[i];
2799 if (task->sc)
2800 continue;
2801
2802 if (task->state == ISCSI_TASK_FREE)
2803 continue;
2804
2805 ISCSI_DBG_SESSION(conn->session,
2806 "failing mgmt itt 0x%x state %d\n",
2807 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002808 state = ISCSI_TASK_ABRT_SESS_RECOV;
2809 if (task->state == ISCSI_TASK_PENDING)
2810 state = ISCSI_TASK_COMPLETED;
2811 iscsi_complete_task(task, state);
2812
Mike Christie7996a772006-04-06 21:13:41 -05002813 }
Mike Christie7996a772006-04-06 21:13:41 -05002814}
2815
Mike Christie656cffc2006-05-18 20:31:42 -05002816static void iscsi_start_session_recovery(struct iscsi_session *session,
2817 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002818{
Mike Christieed2abc72006-05-02 19:46:40 -05002819 int old_stop_stage;
2820
Mike Christie6724add2007-08-15 01:38:30 -05002821 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002822 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002823 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002824 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002825 mutex_unlock(&session->eh_mutex);
2826 return;
2827 }
2828
2829 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002830 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002831 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002832 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002833 */
Mike Christie67a61112006-05-30 00:37:20 -05002834 if (flag == STOP_CONN_TERM)
2835 session->state = ISCSI_STATE_TERMINATE;
2836 else if (conn->stop_stage != STOP_CONN_RECOVER)
2837 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002838 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002839
Mike Christie26013ad2009-05-13 17:57:43 -05002840 del_timer_sync(&conn->transport_timer);
2841 iscsi_suspend_tx(conn);
2842
2843 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002844 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002845 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002846 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002847 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002848
Mike Christie7996a772006-04-06 21:13:41 -05002849 /*
2850 * for connection level recovery we should not calculate
2851 * header digest. conn->hdr_size used for optimization
2852 * in hdr_extract() and will be re-negotiated at
2853 * set_param() time.
2854 */
2855 if (flag == STOP_CONN_RECOVER) {
2856 conn->hdrdgst_en = 0;
2857 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002858 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002859 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002860 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002861 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002862 }
Mike Christie7996a772006-04-06 21:13:41 -05002863 }
Mike Christie656cffc2006-05-18 20:31:42 -05002864
Mike Christie656cffc2006-05-18 20:31:42 -05002865 /*
2866 * flush queues.
2867 */
2868 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002869 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002870 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002871 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002872 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002873}
Mike Christie7996a772006-04-06 21:13:41 -05002874
2875void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2876{
2877 struct iscsi_conn *conn = cls_conn->dd_data;
2878 struct iscsi_session *session = conn->session;
2879
2880 switch (flag) {
2881 case STOP_CONN_RECOVER:
2882 case STOP_CONN_TERM:
2883 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002884 break;
Mike Christie7996a772006-04-06 21:13:41 -05002885 default:
Mike Christie322d7392008-01-31 13:36:52 -06002886 iscsi_conn_printk(KERN_ERR, conn,
2887 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002888 }
2889}
2890EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2891
2892int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2893 struct iscsi_cls_conn *cls_conn, int is_leading)
2894{
Mike Christie75613522008-05-21 15:53:59 -05002895 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002896 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002897
Mike Christie7996a772006-04-06 21:13:41 -05002898 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002899 if (is_leading)
2900 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002901 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002902
2903 /*
2904 * Unblock xmitworker(), Login Phase will pass through.
2905 */
2906 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2907 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2908 return 0;
2909}
2910EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2911
Mike Christie5700b1a2009-05-13 17:57:40 -05002912static int iscsi_switch_str_param(char **param, char *new_val_buf)
2913{
2914 char *new_val;
2915
2916 if (*param) {
2917 if (!strcmp(*param, new_val_buf))
2918 return 0;
2919 }
2920
2921 new_val = kstrdup(new_val_buf, GFP_NOIO);
2922 if (!new_val)
2923 return -ENOMEM;
2924
2925 kfree(*param);
2926 *param = new_val;
2927 return 0;
2928}
Mike Christiea54a52c2006-06-28 12:00:23 -05002929
2930int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2931 enum iscsi_param param, char *buf, int buflen)
2932{
2933 struct iscsi_conn *conn = cls_conn->dd_data;
2934 struct iscsi_session *session = conn->session;
2935 uint32_t value;
2936
2937 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002938 case ISCSI_PARAM_FAST_ABORT:
2939 sscanf(buf, "%d", &session->fast_abort);
2940 break;
Mike Christief6d51802007-12-13 12:43:30 -06002941 case ISCSI_PARAM_ABORT_TMO:
2942 sscanf(buf, "%d", &session->abort_timeout);
2943 break;
2944 case ISCSI_PARAM_LU_RESET_TMO:
2945 sscanf(buf, "%d", &session->lu_reset_timeout);
2946 break;
2947 case ISCSI_PARAM_PING_TMO:
2948 sscanf(buf, "%d", &conn->ping_timeout);
2949 break;
2950 case ISCSI_PARAM_RECV_TMO:
2951 sscanf(buf, "%d", &conn->recv_timeout);
2952 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002953 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2954 sscanf(buf, "%d", &conn->max_recv_dlength);
2955 break;
2956 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2957 sscanf(buf, "%d", &conn->max_xmit_dlength);
2958 break;
2959 case ISCSI_PARAM_HDRDGST_EN:
2960 sscanf(buf, "%d", &conn->hdrdgst_en);
2961 break;
2962 case ISCSI_PARAM_DATADGST_EN:
2963 sscanf(buf, "%d", &conn->datadgst_en);
2964 break;
2965 case ISCSI_PARAM_INITIAL_R2T_EN:
2966 sscanf(buf, "%d", &session->initial_r2t_en);
2967 break;
2968 case ISCSI_PARAM_MAX_R2T:
2969 sscanf(buf, "%d", &session->max_r2t);
2970 break;
2971 case ISCSI_PARAM_IMM_DATA_EN:
2972 sscanf(buf, "%d", &session->imm_data_en);
2973 break;
2974 case ISCSI_PARAM_FIRST_BURST:
2975 sscanf(buf, "%d", &session->first_burst);
2976 break;
2977 case ISCSI_PARAM_MAX_BURST:
2978 sscanf(buf, "%d", &session->max_burst);
2979 break;
2980 case ISCSI_PARAM_PDU_INORDER_EN:
2981 sscanf(buf, "%d", &session->pdu_inorder_en);
2982 break;
2983 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2984 sscanf(buf, "%d", &session->dataseq_inorder_en);
2985 break;
2986 case ISCSI_PARAM_ERL:
2987 sscanf(buf, "%d", &session->erl);
2988 break;
2989 case ISCSI_PARAM_IFMARKER_EN:
2990 sscanf(buf, "%d", &value);
2991 BUG_ON(value);
2992 break;
2993 case ISCSI_PARAM_OFMARKER_EN:
2994 sscanf(buf, "%d", &value);
2995 BUG_ON(value);
2996 break;
2997 case ISCSI_PARAM_EXP_STATSN:
2998 sscanf(buf, "%u", &conn->exp_statsn);
2999 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003000 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003001 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003002 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003003 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003004 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05003005 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003006 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003007 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003008 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003009 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003010 case ISCSI_PARAM_TPGT:
3011 sscanf(buf, "%d", &session->tpgt);
3012 break;
3013 case ISCSI_PARAM_PERSISTENT_PORT:
3014 sscanf(buf, "%d", &conn->persistent_port);
3015 break;
3016 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003017 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003018 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003019 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003020 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003021 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003022 default:
3023 return -ENOSYS;
3024 }
3025
3026 return 0;
3027}
3028EXPORT_SYMBOL_GPL(iscsi_set_param);
3029
3030int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3031 enum iscsi_param param, char *buf)
3032{
Mike Christie75613522008-05-21 15:53:59 -05003033 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003034 int len;
3035
3036 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003037 case ISCSI_PARAM_FAST_ABORT:
3038 len = sprintf(buf, "%d\n", session->fast_abort);
3039 break;
Mike Christief6d51802007-12-13 12:43:30 -06003040 case ISCSI_PARAM_ABORT_TMO:
3041 len = sprintf(buf, "%d\n", session->abort_timeout);
3042 break;
3043 case ISCSI_PARAM_LU_RESET_TMO:
3044 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3045 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003046 case ISCSI_PARAM_INITIAL_R2T_EN:
3047 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3048 break;
3049 case ISCSI_PARAM_MAX_R2T:
3050 len = sprintf(buf, "%hu\n", session->max_r2t);
3051 break;
3052 case ISCSI_PARAM_IMM_DATA_EN:
3053 len = sprintf(buf, "%d\n", session->imm_data_en);
3054 break;
3055 case ISCSI_PARAM_FIRST_BURST:
3056 len = sprintf(buf, "%u\n", session->first_burst);
3057 break;
3058 case ISCSI_PARAM_MAX_BURST:
3059 len = sprintf(buf, "%u\n", session->max_burst);
3060 break;
3061 case ISCSI_PARAM_PDU_INORDER_EN:
3062 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3063 break;
3064 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3065 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3066 break;
3067 case ISCSI_PARAM_ERL:
3068 len = sprintf(buf, "%d\n", session->erl);
3069 break;
3070 case ISCSI_PARAM_TARGET_NAME:
3071 len = sprintf(buf, "%s\n", session->targetname);
3072 break;
3073 case ISCSI_PARAM_TPGT:
3074 len = sprintf(buf, "%d\n", session->tpgt);
3075 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003076 case ISCSI_PARAM_USERNAME:
3077 len = sprintf(buf, "%s\n", session->username);
3078 break;
3079 case ISCSI_PARAM_USERNAME_IN:
3080 len = sprintf(buf, "%s\n", session->username_in);
3081 break;
3082 case ISCSI_PARAM_PASSWORD:
3083 len = sprintf(buf, "%s\n", session->password);
3084 break;
3085 case ISCSI_PARAM_PASSWORD_IN:
3086 len = sprintf(buf, "%s\n", session->password_in);
3087 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003088 case ISCSI_PARAM_IFACE_NAME:
3089 len = sprintf(buf, "%s\n", session->ifacename);
3090 break;
3091 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003092 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003093 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003094 default:
3095 return -ENOSYS;
3096 }
3097
3098 return len;
3099}
3100EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3101
3102int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3103 enum iscsi_param param, char *buf)
3104{
3105 struct iscsi_conn *conn = cls_conn->dd_data;
3106 int len;
3107
3108 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003109 case ISCSI_PARAM_PING_TMO:
3110 len = sprintf(buf, "%u\n", conn->ping_timeout);
3111 break;
3112 case ISCSI_PARAM_RECV_TMO:
3113 len = sprintf(buf, "%u\n", conn->recv_timeout);
3114 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003115 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3116 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3117 break;
3118 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3119 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3120 break;
3121 case ISCSI_PARAM_HDRDGST_EN:
3122 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3123 break;
3124 case ISCSI_PARAM_DATADGST_EN:
3125 len = sprintf(buf, "%d\n", conn->datadgst_en);
3126 break;
3127 case ISCSI_PARAM_IFMARKER_EN:
3128 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3129 break;
3130 case ISCSI_PARAM_OFMARKER_EN:
3131 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3132 break;
3133 case ISCSI_PARAM_EXP_STATSN:
3134 len = sprintf(buf, "%u\n", conn->exp_statsn);
3135 break;
3136 case ISCSI_PARAM_PERSISTENT_PORT:
3137 len = sprintf(buf, "%d\n", conn->persistent_port);
3138 break;
3139 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3140 len = sprintf(buf, "%s\n", conn->persistent_address);
3141 break;
3142 default:
3143 return -ENOSYS;
3144 }
3145
3146 return len;
3147}
3148EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3149
Mike Christie0801c242007-05-30 12:57:12 -05003150int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3151 char *buf)
3152{
Mike Christie75613522008-05-21 15:53:59 -05003153 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003154 int len;
3155
3156 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003157 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003158 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003159 break;
Mike Christie0801c242007-05-30 12:57:12 -05003160 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003161 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003162 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003163 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003164 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003165 break;
Mike Christie75613522008-05-21 15:53:59 -05003166 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003167 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003168 break;
Mike Christie0801c242007-05-30 12:57:12 -05003169 default:
3170 return -ENOSYS;
3171 }
3172
3173 return len;
3174}
3175EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3176
3177int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3178 char *buf, int buflen)
3179{
Mike Christie75613522008-05-21 15:53:59 -05003180 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003181
3182 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003183 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003184 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003185 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003186 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003187 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003188 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003189 default:
3190 return -ENOSYS;
3191 }
3192
3193 return 0;
3194}
3195EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3196
Mike Christie7996a772006-04-06 21:13:41 -05003197MODULE_AUTHOR("Mike Christie");
3198MODULE_DESCRIPTION("iSCSI library functions");
3199MODULE_LICENSE("GPL");