blob: 3eddab0774e474f98bf691a8925cce6b8ddc5db6 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Mike Christie8eb00532007-02-28 17:32:19 -060029#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050030#include <net/tcp.h>
31#include <scsi/scsi_cmnd.h>
32#include <scsi/scsi_device.h>
33#include <scsi/scsi_eh.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsi_host.h>
36#include <scsi/scsi.h>
37#include <scsi/iscsi_proto.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_iscsi.h>
40#include <scsi/libiscsi.h>
41
Erez Zilberbd2199d2009-06-15 22:11:10 -050042static int iscsi_dbg_lib_conn;
43module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
44 S_IRUGO | S_IWUSR);
45MODULE_PARM_DESC(debug_libiscsi_conn,
46 "Turn on debugging for connections in libiscsi module. "
47 "Set to 1 to turn on, and zero to turn off. Default is off.");
48
49static int iscsi_dbg_lib_session;
50module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
51 S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug_libiscsi_session,
53 "Turn on debugging for sessions in libiscsi module. "
54 "Set to 1 to turn on, and zero to turn off. Default is off.");
55
56static int iscsi_dbg_lib_eh;
57module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
58 S_IRUGO | S_IWUSR);
59MODULE_PARM_DESC(debug_libiscsi_eh,
60 "Turn on debugging for error handling in libiscsi module. "
61 "Set to 1 to turn on, and zero to turn off. Default is off.");
Mike Christie1b2c7af2009-03-05 14:45:58 -060062
63#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
64 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050065 if (iscsi_dbg_lib_conn) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060066 iscsi_conn_printk(KERN_INFO, _conn, \
67 "%s " dbg_fmt, \
68 __func__, ##arg); \
69 } while (0);
70
71#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
72 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050073 if (iscsi_dbg_lib_session) \
74 iscsi_session_printk(KERN_INFO, _session, \
75 "%s " dbg_fmt, \
76 __func__, ##arg); \
77 } while (0);
78
79#define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
80 do { \
81 if (iscsi_dbg_lib_eh) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060082 iscsi_session_printk(KERN_INFO, _session, \
83 "%s " dbg_fmt, \
84 __func__, ##arg); \
85 } while (0);
86
Mike Christie77a23c22007-05-30 12:57:18 -050087/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
88#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050089
Mike Christie77a23c22007-05-30 12:57:18 -050090static int iscsi_sna_lt(u32 n1, u32 n2)
91{
92 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
93 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
94}
95
96/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
97static int iscsi_sna_lte(u32 n1, u32 n2)
98{
99 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
100 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
101}
102
Mike Christie32ae7632009-03-05 14:46:03 -0600103inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
104{
105 struct Scsi_Host *shost = conn->session->host;
106 struct iscsi_host *ihost = shost_priv(shost);
107
Mike Christie1336aed2009-05-13 17:57:48 -0500108 if (ihost->workq)
109 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -0600110}
111EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
112
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530113static void __iscsi_update_cmdsn(struct iscsi_session *session,
114 uint32_t exp_cmdsn, uint32_t max_cmdsn)
Mike Christie7996a772006-04-06 21:13:41 -0500115{
Mike Christie77a23c22007-05-30 12:57:18 -0500116 /*
117 * standard specifies this check for when to update expected and
118 * max sequence numbers
119 */
120 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
121 return;
122
123 if (exp_cmdsn != session->exp_cmdsn &&
124 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500125 session->exp_cmdsn = exp_cmdsn;
126
Mike Christie77a23c22007-05-30 12:57:18 -0500127 if (max_cmdsn != session->max_cmdsn &&
128 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
129 session->max_cmdsn = max_cmdsn;
130 /*
131 * if the window closed with IO queued, then kick the
132 * xmit thread
133 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500134 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500135 !list_empty(&session->leadconn->mgmtqueue))
136 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500137 }
Mike Christie7996a772006-04-06 21:13:41 -0500138}
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530139
140void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
141{
142 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
143 be32_to_cpu(hdr->max_cmdsn));
144}
Mike Christie77a23c22007-05-30 12:57:18 -0500145EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500146
Mike Christie577577d2008-12-02 00:32:05 -0600147/**
148 * iscsi_prep_data_out_pdu - initialize Data-Out
149 * @task: scsi command task
150 * @r2t: R2T info
151 * @hdr: iscsi data in pdu
152 *
153 * Notes:
154 * Initialize Data-Out within this R2T sequence and finds
155 * proper data_offset within this SCSI command.
156 *
157 * This function is called with connection lock taken.
158 **/
159void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
160 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500161{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500162 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600163 unsigned int left = r2t->data_length - r2t->sent;
164
165 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500166
167 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600168 hdr->ttt = r2t->ttt;
169 hdr->datasn = cpu_to_be32(r2t->datasn);
170 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500171 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600172 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
173 hdr->itt = task->hdr_itt;
174 hdr->exp_statsn = r2t->exp_statsn;
175 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
176 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500177 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600178 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500179 hdr->flags = 0;
180 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600181 hton24(hdr->dlength, left);
182 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500183 hdr->flags = ISCSI_FLAG_CMD_FINAL;
184 }
Mike Christie577577d2008-12-02 00:32:05 -0600185 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500186}
Mike Christie577577d2008-12-02 00:32:05 -0600187EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500188
Mike Christie9c19a7d2008-05-21 15:54:09 -0500189static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600190{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500191 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600192
Mike Christie9c19a7d2008-05-21 15:54:09 -0500193 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600194 WARN_ON(1);
195 return -EINVAL;
196 }
197
198 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500199 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600200 return 0;
201}
202
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500203/*
204 * make an extended cdb AHS
205 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500206static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500207{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500208 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500209 unsigned rlen, pad_len;
210 unsigned short ahslength;
211 struct iscsi_ecdb_ahdr *ecdb_ahdr;
212 int rc;
213
Mike Christie9c19a7d2008-05-21 15:54:09 -0500214 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500215 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
216
217 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
218 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
219
220 pad_len = iscsi_padding(rlen);
221
Mike Christie9c19a7d2008-05-21 15:54:09 -0500222 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500223 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
224 if (rc)
225 return rc;
226
227 if (pad_len)
228 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
229
230 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
231 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
232 ecdb_ahdr->reserved = 0;
233 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
234
Mike Christie1b2c7af2009-03-05 14:45:58 -0600235 ISCSI_DBG_SESSION(task->conn->session,
236 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
237 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
238 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
239 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500240 return 0;
241}
242
Mike Christie9c19a7d2008-05-21 15:54:09 -0500243static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500244{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500245 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500246 struct iscsi_rlength_ahdr *rlen_ahdr;
247 int rc;
248
Mike Christie9c19a7d2008-05-21 15:54:09 -0500249 rlen_ahdr = iscsi_next_hdr(task);
250 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500251 if (rc)
252 return rc;
253
254 rlen_ahdr->ahslength =
255 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
256 sizeof(rlen_ahdr->reserved));
257 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
258 rlen_ahdr->reserved = 0;
259 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
260
Mike Christie1b2c7af2009-03-05 14:45:58 -0600261 ISCSI_DBG_SESSION(task->conn->session,
262 "bidi-in rlen_ahdr->read_length(%d) "
263 "rlen_ahdr->ahslength(%d)\n",
264 be32_to_cpu(rlen_ahdr->read_length),
265 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500266 return 0;
267}
268
Mike Christie7996a772006-04-06 21:13:41 -0500269/**
Mike Christie5d12c052009-11-11 16:34:32 -0600270 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
271 * @task: iscsi task
272 * @opcode: opcode to check for
273 *
274 * During TMF a task has to be checked if it's affected.
275 * All unrelated I/O can be passed through, but I/O to the
276 * affected LUN should be restricted.
277 * If 'fast_abort' is set we won't be sending any I/O to the
278 * affected LUN.
279 * Otherwise the target is waiting for all TTTs to be completed,
280 * so we have to send all outstanding Data-Out PDUs to the target.
281 */
282static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
283{
284 struct iscsi_conn *conn = task->conn;
285 struct iscsi_tm *tmf = &conn->tmhdr;
286 unsigned int hdr_lun;
287
288 if (conn->tmf_state == TMF_INITIAL)
289 return 0;
290
291 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
292 return 0;
293
294 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
295 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
296 /*
297 * Allow PDUs for unrelated LUNs
298 */
299 hdr_lun = scsilun_to_int((struct scsi_lun *)tmf->lun);
300 if (hdr_lun != task->sc->device->lun)
301 return 0;
Mike Christie3fe5ae82009-11-11 16:34:33 -0600302 /* fall through */
303 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
Mike Christie5d12c052009-11-11 16:34:32 -0600304 /*
305 * Fail all SCSI cmd PDUs
306 */
307 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
308 iscsi_conn_printk(KERN_INFO, conn,
309 "task [op %x/%x itt "
Mike Christie3fe5ae82009-11-11 16:34:33 -0600310 "0x%x/0x%x] "
Mike Christie5d12c052009-11-11 16:34:32 -0600311 "rejected.\n",
312 task->hdr->opcode, opcode,
Mike Christie3fe5ae82009-11-11 16:34:33 -0600313 task->itt, task->hdr_itt);
Mike Christie5d12c052009-11-11 16:34:32 -0600314 return -EACCES;
315 }
316 /*
317 * And also all data-out PDUs in response to R2T
318 * if fast_abort is set.
319 */
320 if (conn->session->fast_abort) {
321 iscsi_conn_printk(KERN_INFO, conn,
322 "task [op %x/%x itt "
Mike Christie3fe5ae82009-11-11 16:34:33 -0600323 "0x%x/0x%x] fast abort.\n",
Mike Christie5d12c052009-11-11 16:34:32 -0600324 task->hdr->opcode, opcode,
Mike Christie3fe5ae82009-11-11 16:34:33 -0600325 task->itt, task->hdr_itt);
Mike Christie5d12c052009-11-11 16:34:32 -0600326 return -EACCES;
327 }
328 break;
329 case ISCSI_TM_FUNC_ABORT_TASK:
330 /*
331 * the caller has already checked if the task
332 * they want to abort was in the pending queue so if
333 * we are here the cmd pdu has gone out already, and
334 * we will only hit this for data-outs
335 */
336 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
337 task->hdr_itt == tmf->rtt) {
338 ISCSI_DBG_SESSION(conn->session,
339 "Preventing task %x/%x from sending "
340 "data-out due to abort task in "
341 "progress\n", task->itt,
342 task->hdr_itt);
343 return -EACCES;
344 }
345 break;
346 }
347
348 return 0;
349}
350
351/**
Mike Christie7996a772006-04-06 21:13:41 -0500352 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500353 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500354 *
355 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
356 * fields like dlength or final based on how much data it sends
357 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500358static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500359{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500360 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500361 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500362 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600363 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500364 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600365 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600366 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500367
Mike Christie5d12c052009-11-11 16:34:32 -0600368 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
369 if (rc)
370 return rc;
371
Mike Christie184b57c2009-05-13 17:57:39 -0500372 if (conn->session->tt->alloc_pdu) {
373 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
374 if (rc)
375 return rc;
376 }
Mike Christie577577d2008-12-02 00:32:05 -0600377 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600378 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600379 memset(hdr, 0, sizeof(*hdr));
380
Mike Christie2ff79d52008-12-02 00:32:14 -0600381 if (session->tt->parse_pdu_itt)
382 hdr->itt = task->hdr_itt = itt;
383 else
384 hdr->itt = task->hdr_itt = build_itt(task->itt,
385 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500386 task->hdr_len = 0;
387 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600388 if (rc)
389 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600390 hdr->opcode = ISCSI_OP_SCSI_CMD;
391 hdr->flags = ISCSI_ATTR_SIMPLE;
392 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600393 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600394 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500395 cmd_len = sc->cmd_len;
396 if (cmd_len < ISCSI_CDB_SIZE)
397 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
398 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500399 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500400 if (rc)
401 return rc;
402 cmd_len = ISCSI_CDB_SIZE;
403 }
404 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500405
Mike Christie9c19a7d2008-05-21 15:54:09 -0500406 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500407 if (scsi_bidi_cmnd(sc)) {
408 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500409 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500410 if (rc)
411 return rc;
412 }
Mike Christie7996a772006-04-06 21:13:41 -0500413 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500414 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600415 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
416
Boaz Harroshc07d4442008-04-18 10:11:52 -0500417 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500418 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
419 /*
420 * Write counters:
421 *
422 * imm_count bytes to be sent right after
423 * SCSI PDU Header
424 *
425 * unsol_count bytes(as Data-Out) to be sent
426 * without R2T ack right after
427 * immediate data
428 *
Mike Christie577577d2008-12-02 00:32:05 -0600429 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500430 *
431 * pad_count bytes to be sent as zero-padding
432 */
Mike Christie577577d2008-12-02 00:32:05 -0600433 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500434
435 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500436 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500437 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500438 conn->max_xmit_dlength);
439 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500440 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500441 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500442 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500443 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600444 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500445
Mike Christieffd04362006-08-31 18:09:24 -0400446 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600447 r2t->data_length = min(session->first_burst, out_len) -
448 task->imm_count;
449 r2t->data_offset = task->imm_count;
450 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
451 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400452 }
453
Mike Christie577577d2008-12-02 00:32:05 -0600454 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500455 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600456 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500457 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500458 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
459 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500460 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500461
462 if (sc->sc_data_direction == DMA_FROM_DEVICE)
463 hdr->flags |= ISCSI_FLAG_CMD_READ;
464 }
465
Boaz Harrosh004d6532007-12-13 12:43:23 -0600466 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500467 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600468
469 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
470 hdrlength /= ISCSI_PAD_LEN;
471
472 WARN_ON(hdrlength >= 256);
473 hdr->hlength = hdrlength & 0xFF;
Mike Christie96b1f962010-04-24 16:21:19 -0500474 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600475
Mike Christie577577d2008-12-02 00:32:05 -0600476 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500477 return -EIO;
478
Mike Christie9c19a7d2008-05-21 15:54:09 -0500479 task->state = ISCSI_TASK_RUNNING;
Mike Christied3305f32009-08-20 15:10:58 -0500480 session->cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500481
Olaf Kircha8ac6312007-12-13 12:43:35 -0600482 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600483 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
484 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
485 scsi_bidi_cmnd(sc) ? "bidirectional" :
486 sc->sc_data_direction == DMA_TO_DEVICE ?
487 "write" : "read", conn->id, sc, sc->cmnd[0],
488 task->itt, scsi_bufflen(sc),
489 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
490 session->cmdsn,
491 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600492 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500493}
Mike Christie7996a772006-04-06 21:13:41 -0500494
495/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500496 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500497 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500498 *
499 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500500 * This function returns the scsi command to scsi-ml or cleans
501 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500502 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500503static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500504{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500505 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600506 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500507 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500508
Mike Christie4421c9e2009-05-13 17:57:50 -0500509 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
510 task->itt, task->state, task->sc);
511
Mike Christie577577d2008-12-02 00:32:05 -0600512 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500513 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500514 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500515 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500516 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500517 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500518 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500519 return;
520
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800521 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500522
Mike Christie3e5c28a2008-05-21 15:54:06 -0500523 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500525 /* SCSI eh reuses commands to verify us */
526 sc->SCp.ptr = NULL;
527 /*
528 * queue command may call this to free the task, but
529 * not have setup the sc callback
530 */
531 if (sc->scsi_done)
532 sc->scsi_done(sc);
533 }
Mike Christie7996a772006-04-06 21:13:41 -0500534}
535
Mike Christie913e5bf2008-05-21 15:54:18 -0500536void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400537{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500538 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400539}
Mike Christie913e5bf2008-05-21 15:54:18 -0500540EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400541
Eddie Wai8eea2f52010-11-23 15:29:21 -0800542void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400543{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500544 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500545 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400546}
Eddie Wai8eea2f52010-11-23 15:29:21 -0800547EXPORT_SYMBOL_GPL(__iscsi_put_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400548
Mike Christie9c19a7d2008-05-21 15:54:09 -0500549void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500550{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500551 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500552
553 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500554 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500555 spin_unlock_bh(&session->lock);
556}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500557EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500558
Mike Christie3bbaaad2009-05-13 17:57:46 -0500559/**
560 * iscsi_complete_task - finish a task
561 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500562 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500563 *
564 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600565 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500566static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600567{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500568 struct iscsi_conn *conn = task->conn;
569
Mike Christie4421c9e2009-05-13 17:57:50 -0500570 ISCSI_DBG_SESSION(conn->session,
571 "complete task itt 0x%x state %d sc %p\n",
572 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500573 if (task->state == ISCSI_TASK_COMPLETED ||
574 task->state == ISCSI_TASK_ABRT_TMF ||
575 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500576 return;
577 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500578 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500579
580 if (!list_empty(&task->running))
581 list_del_init(&task->running);
582
583 if (conn->task == task)
584 conn->task = NULL;
585
586 if (conn->ping_task == task)
587 conn->ping_task = NULL;
588
589 /* release get from queueing */
590 __iscsi_put_task(task);
591}
592
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530593/**
594 * iscsi_complete_scsi_task - finish scsi task normally
595 * @task: iscsi task for scsi cmd
596 * @exp_cmdsn: expected cmd sn in cpu format
597 * @max_cmdsn: max cmd sn in cpu format
598 *
599 * This is used when drivers do not need or cannot perform
600 * lower level pdu processing.
601 *
602 * Called with session lock
603 */
604void iscsi_complete_scsi_task(struct iscsi_task *task,
605 uint32_t exp_cmdsn, uint32_t max_cmdsn)
606{
607 struct iscsi_conn *conn = task->conn;
608
609 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
610
611 conn->last_recv = jiffies;
612 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
613 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
614}
615EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
616
617
Mike Christie3bbaaad2009-05-13 17:57:46 -0500618/*
619 * session lock must be held and if not called for a task that is
620 * still pending or from the xmit thread, then xmit thread must
621 * be suspended.
622 */
623static void fail_scsi_task(struct iscsi_task *task, int err)
624{
625 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600626 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500627 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600628
Mike Christie3bbaaad2009-05-13 17:57:46 -0500629 /*
630 * if a command completes and we get a successful tmf response
631 * we will hit this because the scsi eh abort code does not take
632 * a ref to the task.
633 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500634 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600635 if (!sc)
636 return;
637
Mike Christieb3cd5052009-05-13 17:57:49 -0500638 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600639 /*
640 * cmd never made it to the xmit thread, so we should not count
641 * the cmd in the sequencing
642 */
643 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500644 /* it was never sent so just complete like normal */
645 state = ISCSI_TASK_COMPLETED;
646 } else if (err == DID_TRANSPORT_DISRUPTED)
647 state = ISCSI_TASK_ABRT_SESS_RECOV;
648 else
649 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600650
Mike Christieb3cd5052009-05-13 17:57:49 -0500651 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500652 if (!scsi_bidi_cmnd(sc))
653 scsi_set_resid(sc, scsi_bufflen(sc));
654 else {
655 scsi_out(sc)->resid = scsi_out(sc)->length;
656 scsi_in(sc)->resid = scsi_in(sc)->length;
657 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500658
Mike Christieb3cd5052009-05-13 17:57:49 -0500659 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600660}
661
Mike Christie3e5c28a2008-05-21 15:54:06 -0500662static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500663 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500664{
665 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600666 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500667 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
Mike Christie4f704dc2009-11-11 16:34:31 -0600668 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie052d0142008-05-21 15:54:05 -0500669
670 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
671 return -ENOTCONN;
672
Mike Christie4f704dc2009-11-11 16:34:31 -0600673 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
Mike Christie052d0142008-05-21 15:54:05 -0500674 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
675 /*
676 * pre-format CmdSN for outgoing PDU.
677 */
678 nop->cmdsn = cpu_to_be32(session->cmdsn);
679 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500680 /*
Mike Christie4f704dc2009-11-11 16:34:31 -0600681 * TODO: We always use immediate for normal session pdus.
Mike Christie052d0142008-05-21 15:54:05 -0500682 * If we start to send tmfs or nops as non-immediate then
683 * we should start checking the cmdsn numbers for mgmt tasks.
Mike Christie4f704dc2009-11-11 16:34:31 -0600684 *
685 * During discovery sessions iscsid sends TEXT as non immediate,
686 * but we always only send one PDU at a time.
Mike Christie052d0142008-05-21 15:54:05 -0500687 */
688 if (conn->c_stage == ISCSI_CONN_STARTED &&
689 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
690 session->queued_cmdsn++;
691 session->cmdsn++;
692 }
693 }
694
Mike Christieae15f802008-12-02 00:32:15 -0600695 if (session->tt->init_task && session->tt->init_task(task))
696 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500697
698 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
699 session->state = ISCSI_STATE_LOGGING_OUT;
700
Mike Christie577577d2008-12-02 00:32:05 -0600701 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600702 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
703 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
704 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500705 return 0;
706}
707
Mike Christie9c19a7d2008-05-21 15:54:09 -0500708static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600709__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
710 char *data, uint32_t data_size)
711{
712 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500713 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie4f704dc2009-11-11 16:34:31 -0600714 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500715 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600716 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600717
718 if (session->state == ISCSI_STATE_TERMINATE)
719 return NULL;
720
Mike Christie4f704dc2009-11-11 16:34:31 -0600721 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
Mike Christief6d51802007-12-13 12:43:30 -0600722 /*
723 * Login and Text are sent serially, in
724 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500725 * Same task can be used. Same ITT must be used.
726 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600727 */
Mike Christie4f704dc2009-11-11 16:34:31 -0600728 if (conn->login_task->state != ISCSI_TASK_FREE) {
729 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
730 "progress. Cannot start new task.\n");
731 return NULL;
732 }
733
Mike Christie9c19a7d2008-05-21 15:54:09 -0500734 task = conn->login_task;
Mike Christie4f704dc2009-11-11 16:34:31 -0600735 } else {
Mike Christie26013ad2009-05-13 17:57:43 -0500736 if (session->state != ISCSI_STATE_LOGGED_IN)
737 return NULL;
738
Mike Christief6d51802007-12-13 12:43:30 -0600739 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
740 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
741
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800742 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500743 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600744 return NULL;
745 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500746 /*
747 * released in complete pdu for task we expect a response for, and
748 * released by the lld when it has transmitted the task for
749 * pdus we do not expect a response for.
750 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500751 atomic_set(&task->refcount, 1);
752 task->conn = conn;
753 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500754 INIT_LIST_HEAD(&task->running);
755 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600756
757 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500758 memcpy(task->data, data, data_size);
759 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600760 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500761 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600762
Mike Christie184b57c2009-05-13 17:57:39 -0500763 if (conn->session->tt->alloc_pdu) {
764 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
765 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
766 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500767 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500768 }
Mike Christie577577d2008-12-02 00:32:05 -0600769 }
Mike Christie184b57c2009-05-13 17:57:39 -0500770
Mike Christie262ef632008-12-02 00:32:13 -0600771 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600772 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500773 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600774
775 if (hdr->itt != RESERVED_ITT) {
776 if (session->tt->parse_pdu_itt)
777 task->hdr->itt = itt;
778 else
779 task->hdr->itt = build_itt(task->itt,
780 task->conn->session->age);
781 }
782
Mike Christie1336aed2009-05-13 17:57:48 -0500783 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600784 if (iscsi_prep_mgmt_task(conn, task))
785 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500786
Mike Christie9c19a7d2008-05-21 15:54:09 -0500787 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600788 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500789 } else {
790 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600791 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500792 }
Mike Christie052d0142008-05-21 15:54:05 -0500793
Mike Christie9c19a7d2008-05-21 15:54:09 -0500794 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600795
796free_task:
797 __iscsi_put_task(task);
798 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600799}
800
801int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
802 char *data, uint32_t data_size)
803{
804 struct iscsi_conn *conn = cls_conn->dd_data;
805 struct iscsi_session *session = conn->session;
806 int err = 0;
807
808 spin_lock_bh(&session->lock);
809 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
810 err = -EPERM;
811 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600812 return err;
813}
814EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
815
Mike Christie7996a772006-04-06 21:13:41 -0500816/**
817 * iscsi_cmd_rsp - SCSI Command Response processing
818 * @conn: iscsi connection
819 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500820 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500821 * @data: cmd data buffer
822 * @datalen: len of buffer
823 *
824 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500825 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500826 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500827static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500828 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500829 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500830{
Mike Christie7996a772006-04-06 21:13:41 -0500831 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
832 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500833 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500834
Mike Christie77a23c22007-05-30 12:57:18 -0500835 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500836 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
837
838 sc->result = (DID_OK << 16) | rhdr->cmd_status;
839
840 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
841 sc->result = DID_ERROR << 16;
842 goto out;
843 }
844
845 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600846 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500847
848 if (datalen < 2) {
849invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600850 iscsi_conn_printk(KERN_ERR, conn,
851 "Got CHECK_CONDITION but invalid data "
852 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500853 sc->result = DID_BAD_TARGET << 16;
854 goto out;
855 }
856
Harvey Harrison8f3339912008-05-21 15:54:20 -0500857 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500858 if (datalen < senselen)
859 goto invalid_datalen;
860
861 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600862 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600863 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
864 min_t(uint16_t, senselen,
865 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500866 }
867
Boaz Harroshc07d4442008-04-18 10:11:52 -0500868 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
869 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
870 int res_count = be32_to_cpu(rhdr->bi_residual_count);
871
872 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
873 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
874 res_count <= scsi_in(sc)->length))
875 scsi_in(sc)->resid = res_count;
876 else
877 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
878 }
879
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600880 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
881 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500882 int res_count = be32_to_cpu(rhdr->residual_count);
883
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600884 if (res_count > 0 &&
885 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
886 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500887 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900888 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500889 else
890 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500891 }
Mike Christie7996a772006-04-06 21:13:41 -0500892out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500893 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600894 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500895 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500896 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500897}
898
Mike Christie1d9edf02008-09-24 11:46:09 -0500899/**
900 * iscsi_data_in_rsp - SCSI Data-In Response processing
901 * @conn: iscsi connection
902 * @hdr: iscsi pdu
903 * @task: scsi command task
904 **/
905static void
906iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
907 struct iscsi_task *task)
908{
909 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
910 struct scsi_cmnd *sc = task->sc;
911
912 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
913 return;
914
Mike Christieedbc9aa052009-05-13 17:57:42 -0500915 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500916 sc->result = (DID_OK << 16) | rhdr->cmd_status;
917 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
918 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
919 ISCSI_FLAG_DATA_OVERFLOW)) {
920 int res_count = be32_to_cpu(rhdr->residual_count);
921
922 if (res_count > 0 &&
923 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
924 res_count <= scsi_in(sc)->length))
925 scsi_in(sc)->resid = res_count;
926 else
927 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
928 }
929
Mike Christie3bbaaad2009-05-13 17:57:46 -0500930 ISCSI_DBG_SESSION(conn->session, "data in with status done "
931 "[sc %p res %d itt 0x%x]\n",
932 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500933 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500934 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500935}
936
Mike Christie7ea8b822006-07-24 15:47:22 -0500937static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
938{
939 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
940
941 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
942 conn->tmfrsp_pdus_cnt++;
943
Mike Christie843c0a82007-12-13 12:43:20 -0600944 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500945 return;
946
947 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600948 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500949 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600950 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500951 else
Mike Christie843c0a82007-12-13 12:43:20 -0600952 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500953 wake_up(&conn->ehwait);
954}
955
Mike Christief6d51802007-12-13 12:43:30 -0600956static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
957{
958 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500959 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600960
Mike Christie9c19a7d2008-05-21 15:54:09 -0500961 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600962 return;
963
964 memset(&hdr, 0, sizeof(struct iscsi_nopout));
965 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
966 hdr.flags = ISCSI_FLAG_CMD_FINAL;
967
968 if (rhdr) {
969 memcpy(hdr.lun, rhdr->lun, 8);
970 hdr.ttt = rhdr->ttt;
971 hdr.itt = RESERVED_ITT;
972 } else
973 hdr.ttt = RESERVED_ITT;
974
Mike Christie9c19a7d2008-05-21 15:54:09 -0500975 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
976 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600977 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600978 else if (!rhdr) {
979 /* only track our nops */
980 conn->ping_task = task;
981 conn->last_ping = jiffies;
982 }
Mike Christief6d51802007-12-13 12:43:30 -0600983}
984
Mike Christie8afa1432009-08-20 15:10:59 -0500985static int iscsi_nop_out_rsp(struct iscsi_task *task,
986 struct iscsi_nopin *nop, char *data, int datalen)
987{
988 struct iscsi_conn *conn = task->conn;
989 int rc = 0;
990
991 if (conn->ping_task != task) {
992 /*
993 * If this is not in response to one of our
994 * nops then it must be from userspace.
995 */
996 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
997 data, datalen))
998 rc = ISCSI_ERR_CONN_FAILED;
999 } else
1000 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1001 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1002 return rc;
1003}
1004
Mike Christie62f38302006-08-31 18:09:27 -04001005static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1006 char *data, int datalen)
1007{
1008 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1009 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -05001010 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -04001011
1012 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1013
Mike Christie8afa1432009-08-20 15:10:59 -05001014 if (ntoh24(reject->dlength) > datalen ||
1015 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1016 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1017 "pdu. Invalid data length (pdu dlength "
1018 "%u, datalen %d\n", ntoh24(reject->dlength),
1019 datalen);
1020 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -04001021 }
Mike Christie8afa1432009-08-20 15:10:59 -05001022 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1023 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1024
1025 switch (reject->reason) {
1026 case ISCSI_REASON_DATA_DIGEST_ERROR:
1027 iscsi_conn_printk(KERN_ERR, conn,
1028 "pdu (op 0x%x itt 0x%x) rejected "
1029 "due to DataDigest error.\n",
1030 rejected_pdu.itt, opcode);
1031 break;
1032 case ISCSI_REASON_IMM_CMD_REJECT:
1033 iscsi_conn_printk(KERN_ERR, conn,
1034 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1035 "immediate commands.\n",
1036 rejected_pdu.itt, opcode);
1037 /*
1038 * We only send one TMF at a time so if the target could not
1039 * handle it, then it should get fixed (RFC mandates that
1040 * a target can handle one immediate TMF per conn).
1041 *
1042 * For nops-outs, we could have sent more than one if
1043 * the target is sending us lots of nop-ins
1044 */
1045 if (opcode != ISCSI_OP_NOOP_OUT)
1046 return 0;
1047
1048 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
1049 /*
1050 * nop-out in response to target's nop-out rejected.
1051 * Just resend.
1052 */
1053 iscsi_send_nopout(conn,
1054 (struct iscsi_nopin*)&rejected_pdu);
1055 else {
1056 struct iscsi_task *task;
1057 /*
1058 * Our nop as ping got dropped. We know the target
1059 * and transport are ok so just clean up
1060 */
1061 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1062 if (!task) {
1063 iscsi_conn_printk(KERN_ERR, conn,
1064 "Invalid pdu reject. Could "
1065 "not lookup rejected task.\n");
1066 rc = ISCSI_ERR_BAD_ITT;
1067 } else
1068 rc = iscsi_nop_out_rsp(task,
1069 (struct iscsi_nopin*)&rejected_pdu,
1070 NULL, 0);
1071 }
1072 break;
1073 default:
1074 iscsi_conn_printk(KERN_ERR, conn,
1075 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1076 "code 0x%x\n", rejected_pdu.itt,
1077 rejected_pdu.opcode, reject->reason);
1078 break;
1079 }
1080 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001081}
1082
Mike Christie7996a772006-04-06 21:13:41 -05001083/**
Mike Christie913e5bf2008-05-21 15:54:18 -05001084 * iscsi_itt_to_task - look up task by itt
1085 * @conn: iscsi connection
1086 * @itt: itt
1087 *
1088 * This should be used for mgmt tasks like login and nops, or if
1089 * the LDD's itt space does not include the session age.
1090 *
1091 * The session lock must be held.
1092 */
Mike Christie8f9256c2009-05-13 17:57:41 -05001093struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -05001094{
1095 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001096 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -05001097
1098 if (itt == RESERVED_ITT)
1099 return NULL;
1100
Mike Christie262ef632008-12-02 00:32:13 -06001101 if (session->tt->parse_pdu_itt)
1102 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1103 else
1104 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -05001105 if (i >= session->cmds_max)
1106 return NULL;
1107
1108 return session->cmds[i];
1109}
Mike Christie8f9256c2009-05-13 17:57:41 -05001110EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -05001111
1112/**
Mike Christie7996a772006-04-06 21:13:41 -05001113 * __iscsi_complete_pdu - complete pdu
1114 * @conn: iscsi conn
1115 * @hdr: iscsi header
1116 * @data: data buffer
1117 * @datalen: len of data buffer
1118 *
1119 * Completes pdu processing by freeing any resources allocated at
1120 * queuecommand or send generic. session lock must be held and verify
1121 * itt must have been called.
1122 */
Mike Christie913e5bf2008-05-21 15:54:18 -05001123int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1124 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001125{
1126 struct iscsi_session *session = conn->session;
1127 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001128 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001129 uint32_t itt;
1130
Mike Christief6d51802007-12-13 12:43:30 -06001131 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001132 rc = iscsi_verify_itt(conn, hdr->itt);
1133 if (rc)
1134 return rc;
1135
Al Virob4377352007-02-09 16:39:40 +00001136 if (hdr->itt != RESERVED_ITT)
1137 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001138 else
Al Virob4377352007-02-09 16:39:40 +00001139 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001140
Mike Christie1b2c7af2009-03-05 14:45:58 -06001141 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1142 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001143
Mike Christie3e5c28a2008-05-21 15:54:06 -05001144 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001145 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001146
Mike Christie7996a772006-04-06 21:13:41 -05001147 switch(opcode) {
1148 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001149 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001150 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001151 break;
1152 }
1153
Al Virob4377352007-02-09 16:39:40 +00001154 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001155 break;
1156
Mike Christief6d51802007-12-13 12:43:30 -06001157 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001158 break;
1159 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001160 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1161 break;
Mike Christie7996a772006-04-06 21:13:41 -05001162 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001163 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001164 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1165 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001166 break;
1167 default:
1168 rc = ISCSI_ERR_BAD_OPCODE;
1169 break;
1170 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001171 goto out;
1172 }
Mike Christie7996a772006-04-06 21:13:41 -05001173
Mike Christie3e5c28a2008-05-21 15:54:06 -05001174 switch(opcode) {
1175 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001176 case ISCSI_OP_SCSI_DATA_IN:
1177 task = iscsi_itt_to_ctask(conn, hdr->itt);
1178 if (!task)
1179 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001180 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001181 break;
1182 case ISCSI_OP_R2T:
1183 /*
1184 * LLD handles R2Ts if they need to.
1185 */
1186 return 0;
1187 case ISCSI_OP_LOGOUT_RSP:
1188 case ISCSI_OP_LOGIN_RSP:
1189 case ISCSI_OP_TEXT_RSP:
1190 case ISCSI_OP_SCSI_TMFUNC_RSP:
1191 case ISCSI_OP_NOOP_IN:
1192 task = iscsi_itt_to_task(conn, hdr->itt);
1193 if (!task)
1194 return ISCSI_ERR_BAD_ITT;
1195 break;
1196 default:
1197 return ISCSI_ERR_BAD_OPCODE;
1198 }
1199
1200 switch(opcode) {
1201 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001202 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001203 break;
1204 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001205 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001206 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001207 case ISCSI_OP_LOGOUT_RSP:
1208 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1209 if (datalen) {
1210 rc = ISCSI_ERR_PROTO;
1211 break;
1212 }
1213 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1214 goto recv_pdu;
1215 case ISCSI_OP_LOGIN_RSP:
1216 case ISCSI_OP_TEXT_RSP:
1217 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1218 /*
1219 * login related PDU's exp_statsn is handled in
1220 * userspace
1221 */
1222 goto recv_pdu;
1223 case ISCSI_OP_SCSI_TMFUNC_RSP:
1224 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1225 if (datalen) {
1226 rc = ISCSI_ERR_PROTO;
1227 break;
1228 }
1229
1230 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001231 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001232 break;
1233 case ISCSI_OP_NOOP_IN:
1234 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1235 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1236 rc = ISCSI_ERR_PROTO;
1237 break;
1238 }
1239 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1240
Mike Christie8afa1432009-08-20 15:10:59 -05001241 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1242 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001243 break;
1244 default:
1245 rc = ISCSI_ERR_BAD_OPCODE;
1246 break;
1247 }
1248
1249out:
1250 return rc;
1251recv_pdu:
1252 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1253 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001254 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001255 return rc;
1256}
Mike Christie913e5bf2008-05-21 15:54:18 -05001257EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001258
1259int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1260 char *data, int datalen)
1261{
1262 int rc;
1263
1264 spin_lock(&conn->session->lock);
1265 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1266 spin_unlock(&conn->session->lock);
1267 return rc;
1268}
1269EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1270
Mike Christie0af967f2008-05-21 15:54:04 -05001271int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001272{
1273 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001274 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001275
Mike Christie0af967f2008-05-21 15:54:04 -05001276 if (itt == RESERVED_ITT)
1277 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001278
Mike Christie262ef632008-12-02 00:32:13 -06001279 if (session->tt->parse_pdu_itt)
1280 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1281 else {
1282 i = get_itt(itt);
1283 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1284 }
1285
1286 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001287 iscsi_conn_printk(KERN_ERR, conn,
1288 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001289 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001290 return ISCSI_ERR_BAD_ITT;
1291 }
Mike Christie7996a772006-04-06 21:13:41 -05001292
Mike Christie3e5c28a2008-05-21 15:54:06 -05001293 if (i >= session->cmds_max) {
1294 iscsi_conn_printk(KERN_ERR, conn,
1295 "received invalid itt index %u (max cmds "
1296 "%u.\n", i, session->cmds_max);
1297 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001298 }
Mike Christie7996a772006-04-06 21:13:41 -05001299 return 0;
1300}
1301EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1302
Mike Christie913e5bf2008-05-21 15:54:18 -05001303/**
1304 * iscsi_itt_to_ctask - look up ctask by itt
1305 * @conn: iscsi connection
1306 * @itt: itt
1307 *
1308 * This should be used for cmd tasks.
1309 *
1310 * The session lock must be held.
1311 */
1312struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001313{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001314 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001315
1316 if (iscsi_verify_itt(conn, itt))
1317 return NULL;
1318
Mike Christie913e5bf2008-05-21 15:54:18 -05001319 task = iscsi_itt_to_task(conn, itt);
1320 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001321 return NULL;
1322
Mike Christie913e5bf2008-05-21 15:54:18 -05001323 if (task->sc->SCp.phase != conn->session->age) {
1324 iscsi_session_printk(KERN_ERR, conn->session,
1325 "task's session age %d, expected %d\n",
1326 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001327 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001328 }
Mike Christie0af967f2008-05-21 15:54:04 -05001329
Mike Christie9c19a7d2008-05-21 15:54:09 -05001330 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001331}
1332EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1333
Mike Christie40a06e72009-03-05 14:46:05 -06001334void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001335 enum iscsi_err err)
1336{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001337 struct iscsi_conn *conn;
1338 struct device *dev;
1339 unsigned long flags;
1340
1341 spin_lock_irqsave(&session->lock, flags);
1342 conn = session->leadconn;
1343 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1344 spin_unlock_irqrestore(&session->lock, flags);
1345 return;
1346 }
1347
1348 dev = get_device(&conn->cls_conn->dev);
1349 spin_unlock_irqrestore(&session->lock, flags);
1350 if (!dev)
1351 return;
1352 /*
1353 * if the host is being removed bypass the connection
1354 * recovery initialization because we are going to kill
1355 * the session.
1356 */
1357 if (err == ISCSI_ERR_INVALID_HOST)
1358 iscsi_conn_error_event(conn->cls_conn, err);
1359 else
1360 iscsi_conn_failure(conn, err);
1361 put_device(dev);
1362}
1363EXPORT_SYMBOL_GPL(iscsi_session_failure);
1364
Mike Christie7996a772006-04-06 21:13:41 -05001365void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1366{
1367 struct iscsi_session *session = conn->session;
1368 unsigned long flags;
1369
1370 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001371 if (session->state == ISCSI_STATE_FAILED) {
1372 spin_unlock_irqrestore(&session->lock, flags);
1373 return;
1374 }
1375
Mike Christie67a61112006-05-30 00:37:20 -05001376 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001377 session->state = ISCSI_STATE_FAILED;
1378 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001379
Mike Christie7996a772006-04-06 21:13:41 -05001380 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1381 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001382 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001383}
1384EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1385
Mike Christie77a23c22007-05-30 12:57:18 -05001386static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1387{
1388 struct iscsi_session *session = conn->session;
1389
1390 /*
1391 * Check for iSCSI window and take care of CmdSN wrap-around
1392 */
Mike Christiee0726402007-07-26 12:46:48 -05001393 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001394 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1395 "%u MaxCmdSN %u CmdSN %u/%u\n",
1396 session->exp_cmdsn, session->max_cmdsn,
1397 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001398 return -ENOSPC;
1399 }
1400 return 0;
1401}
1402
Mike Christie9c19a7d2008-05-21 15:54:09 -05001403static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001404{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001405 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001406 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001407
Mike Christie70b31c12009-08-20 15:11:03 -05001408 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1409 return -ENODATA;
1410
Mike Christie9c19a7d2008-05-21 15:54:09 -05001411 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001412 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001413 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001414 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001415 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001416 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001417 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001418 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001419 }
1420 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001421 return rc;
1422}
1423
Mike Christie7996a772006-04-06 21:13:41 -05001424/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001425 * iscsi_requeue_task - requeue task to run from session workqueue
1426 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001427 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001428 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001429 * this. The session lock must be held. This should only be called
1430 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001431 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001432void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001433{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001434 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001435
Mike Christie3bbaaad2009-05-13 17:57:46 -05001436 /*
1437 * this may be on the requeue list already if the xmit_task callout
1438 * is handling the r2ts while we are adding new ones
1439 */
1440 if (list_empty(&task->running))
1441 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001442 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001443}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001444EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001445
1446/**
Mike Christie7996a772006-04-06 21:13:41 -05001447 * iscsi_data_xmit - xmit any command into the scheduled connection
1448 * @conn: iscsi connection
1449 *
1450 * Notes:
1451 * The function can return -EAGAIN in which case the caller must
1452 * re-schedule it again later or recover. '0' return code means
1453 * successful xmit.
1454 **/
1455static int iscsi_data_xmit(struct iscsi_conn *conn)
1456{
Mike Christie5d12c052009-11-11 16:34:32 -06001457 struct iscsi_task *task;
Mike Christie3219e522006-05-30 00:37:28 -05001458 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001459
Mike Christie77a23c22007-05-30 12:57:18 -05001460 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001461 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001462 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001463 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001464 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001465 }
Mike Christie7996a772006-04-06 21:13:41 -05001466
Mike Christie9c19a7d2008-05-21 15:54:09 -05001467 if (conn->task) {
1468 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001469 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001470 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001471 }
1472
Mike Christie77a23c22007-05-30 12:57:18 -05001473 /*
1474 * process mgmt pdus like nops before commands since we should
1475 * only have one nop-out as a ping from us and targets should not
1476 * overflow us with nop-ins
1477 */
1478check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001479 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001480 conn->task = list_entry(conn->mgmtqueue.next,
1481 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001482 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001483 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1484 __iscsi_put_task(conn->task);
1485 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001486 continue;
1487 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001488 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001489 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001490 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001491 }
1492
Mike Christie843c0a82007-12-13 12:43:20 -06001493 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001494 while (!list_empty(&conn->cmdqueue)) {
Mike Christie5d12c052009-11-11 16:34:32 -06001495 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1496 running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001497 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001498 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001499 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001500 continue;
1501 }
Mike Christie577577d2008-12-02 00:32:05 -06001502 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1503 if (rc) {
Mike Christie5d12c052009-11-11 16:34:32 -06001504 if (rc == -ENOMEM || rc == -EACCES) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001505 list_add_tail(&conn->task->running,
1506 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001507 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001508 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001509 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001510 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001511 continue;
1512 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001513 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001514 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001515 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001516 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001517 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001518 * we need to check the mgmt queue for nops that need to
1519 * be sent to aviod starvation
1520 */
Mike Christie843c0a82007-12-13 12:43:20 -06001521 if (!list_empty(&conn->mgmtqueue))
1522 goto check_mgmt;
1523 }
1524
1525 while (!list_empty(&conn->requeue)) {
Mike Christieb3a7ea82007-12-13 12:43:26 -06001526 /*
1527 * we always do fastlogout - conn stop code will clean up.
1528 */
1529 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1530 break;
1531
Mike Christie5d12c052009-11-11 16:34:32 -06001532 task = list_entry(conn->requeue.next, struct iscsi_task,
1533 running);
1534 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1535 break;
1536
1537 conn->task = task;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001538 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001539 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001540 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001541 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001542 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001543 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001544 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001545 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001546 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001547 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001548
Mike Christie70b31c12009-08-20 15:11:03 -05001549done:
Mike Christie77a23c22007-05-30 12:57:18 -05001550 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001551 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001552}
1553
David Howellsc4028952006-11-22 14:57:56 +00001554static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001555{
David Howellsc4028952006-11-22 14:57:56 +00001556 struct iscsi_conn *conn =
1557 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001558 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001559 /*
1560 * serialize Xmit worker on a per-connection basis.
1561 */
Mike Christie3219e522006-05-30 00:37:28 -05001562 do {
1563 rc = iscsi_data_xmit(conn);
1564 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001565}
1566
Mike Christie577577d2008-12-02 00:32:05 -06001567static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1568 struct scsi_cmnd *sc)
1569{
1570 struct iscsi_task *task;
1571
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001572 if (!kfifo_out(&conn->session->cmdpool.queue,
Mike Christie577577d2008-12-02 00:32:05 -06001573 (void *) &task, sizeof(void *)))
1574 return NULL;
1575
1576 sc->SCp.phase = conn->session->age;
1577 sc->SCp.ptr = (char *) task;
1578
1579 atomic_set(&task->refcount, 1);
1580 task->state = ISCSI_TASK_PENDING;
1581 task->conn = conn;
1582 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001583 task->have_checked_conn = false;
1584 task->last_timeout = jiffies;
1585 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001586 INIT_LIST_HEAD(&task->running);
1587 return task;
1588}
1589
Mike Christie7996a772006-04-06 21:13:41 -05001590enum {
1591 FAILURE_BAD_HOST = 1,
1592 FAILURE_SESSION_FAILED,
1593 FAILURE_SESSION_FREED,
1594 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001595 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001596 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001597 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001598 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001599 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001600 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001601};
1602
Jeff Garzikf2812332010-11-16 02:10:29 -05001603static int iscsi_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Mike Christie7996a772006-04-06 21:13:41 -05001604{
Mike Christie75613522008-05-21 15:53:59 -05001605 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001606 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001607 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001608 int reason = 0;
1609 struct iscsi_session *session;
1610 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001611 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001612
1613 sc->scsi_done = done;
1614 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001615 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001616
1617 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001618 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001619 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001620
Mike Christie75613522008-05-21 15:53:59 -05001621 cls_session = starget_to_session(scsi_target(sc->device));
1622 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001623 spin_lock(&session->lock);
1624
Mike Christie75613522008-05-21 15:53:59 -05001625 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001626 if (reason) {
1627 sc->result = reason;
1628 goto fault;
1629 }
1630
Mike Christie301e0f72009-05-13 17:57:47 -05001631 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001632 /*
1633 * to handle the race between when we set the recovery state
1634 * and block the session we requeue here (commands could
1635 * be entering our queuecommand while a block is starting
1636 * up because the block code is not locked)
1637 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001638 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001639 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001640 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001641 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001642 sc->result = DID_IMM_RETRY << 16;
1643 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001644 case ISCSI_STATE_LOGGING_OUT:
1645 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001646 sc->result = DID_IMM_RETRY << 16;
1647 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001648 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001649 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001650 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001651 break;
1652 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001653 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001654 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001655 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001656 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001657 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001658 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001659 }
Mike Christie7996a772006-04-06 21:13:41 -05001660 goto fault;
1661 }
1662
Mike Christie7996a772006-04-06 21:13:41 -05001663 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001664 if (!conn) {
1665 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001666 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001667 goto fault;
1668 }
Mike Christie7996a772006-04-06 21:13:41 -05001669
Mike Christie661134a2009-09-05 07:35:33 +05301670 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1671 reason = FAILURE_SESSION_IN_RECOVERY;
1672 sc->result = DID_REQUEUE;
1673 goto fault;
1674 }
1675
Mike Christie77a23c22007-05-30 12:57:18 -05001676 if (iscsi_check_cmdsn_window_closed(conn)) {
1677 reason = FAILURE_WINDOW_CLOSED;
1678 goto reject;
1679 }
1680
Mike Christie577577d2008-12-02 00:32:05 -06001681 task = iscsi_alloc_task(conn, sc);
1682 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001683 reason = FAILURE_OOM;
1684 goto reject;
1685 }
Mike Christie7996a772006-04-06 21:13:41 -05001686
Mike Christie1336aed2009-05-13 17:57:48 -05001687 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001688 reason = iscsi_prep_scsi_cmd_pdu(task);
1689 if (reason) {
Mike Christie5d12c052009-11-11 16:34:32 -06001690 if (reason == -ENOMEM || reason == -EACCES) {
Mike Christie577577d2008-12-02 00:32:05 -06001691 reason = FAILURE_OOM;
1692 goto prepd_reject;
1693 } else {
1694 sc->result = DID_ABORT << 16;
1695 goto prepd_fault;
1696 }
Mike Christie052d0142008-05-21 15:54:05 -05001697 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001698 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001699 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001700 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001701 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001702 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001703 } else {
1704 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001705 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001706 }
Mike Christie052d0142008-05-21 15:54:05 -05001707
1708 session->queued_cmdsn++;
1709 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001710 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001711 return 0;
1712
Mike Christie577577d2008-12-02 00:32:05 -06001713prepd_reject:
1714 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001715 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001716reject:
1717 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001718 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1719 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001720 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001721 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001722
Mike Christie577577d2008-12-02 00:32:05 -06001723prepd_fault:
1724 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001725 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001726fault:
1727 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001728 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1729 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001730 if (!scsi_bidi_cmnd(sc))
1731 scsi_set_resid(sc, scsi_bufflen(sc));
1732 else {
1733 scsi_out(sc)->resid = scsi_out(sc)->length;
1734 scsi_in(sc)->resid = scsi_in(sc)->length;
1735 }
Mike Christie052d0142008-05-21 15:54:05 -05001736 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001737 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001738 return 0;
1739}
Jeff Garzikf2812332010-11-16 02:10:29 -05001740
1741DEF_SCSI_QCMD(iscsi_queuecommand)
Mike Christie7996a772006-04-06 21:13:41 -05001742EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1743
Mike Christiee881a172009-10-15 17:46:39 -07001744int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
Mike Christie7996a772006-04-06 21:13:41 -05001745{
Mike Christie1796e722009-11-11 16:34:36 -06001746 switch (reason) {
1747 case SCSI_QDEPTH_DEFAULT:
1748 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1749 break;
1750 case SCSI_QDEPTH_QFULL:
1751 scsi_track_queue_full(sdev, depth);
1752 break;
1753 case SCSI_QDEPTH_RAMP_UP:
1754 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1755 break;
1756 default:
Mike Christiee881a172009-10-15 17:46:39 -07001757 return -EOPNOTSUPP;
Mike Christie1796e722009-11-11 16:34:36 -06001758 }
Mike Christie7996a772006-04-06 21:13:41 -05001759 return sdev->queue_depth;
1760}
1761EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1762
Mike Christie6b5d6c42009-04-21 15:32:32 -05001763int iscsi_target_alloc(struct scsi_target *starget)
1764{
1765 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1766 struct iscsi_session *session = cls_session->dd_data;
1767
1768 starget->can_queue = session->scsi_cmds_max;
1769 return 0;
1770}
1771EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1772
Mike Christie843c0a82007-12-13 12:43:20 -06001773static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001774{
Mike Christie843c0a82007-12-13 12:43:20 -06001775 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001776 struct iscsi_session *session = conn->session;
1777
1778 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001779 if (conn->tmf_state == TMF_QUEUED) {
1780 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001781 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001782 /* unblock eh_abort() */
1783 wake_up(&conn->ehwait);
1784 }
1785 spin_unlock(&session->lock);
1786}
1787
Mike Christie9c19a7d2008-05-21 15:54:09 -05001788static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001789 struct iscsi_tm *hdr, int age,
1790 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001791{
Mike Christie7996a772006-04-06 21:13:41 -05001792 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001793 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001794
Mike Christie9c19a7d2008-05-21 15:54:09 -05001795 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001796 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001797 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001798 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06001799 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
Mike Christie7996a772006-04-06 21:13:41 -05001800 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001801 spin_lock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001802 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001803 }
Mike Christie843c0a82007-12-13 12:43:20 -06001804 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001805 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001806 conn->tmf_timer.function = iscsi_tmf_timedout;
1807 conn->tmf_timer.data = (unsigned long)conn;
1808 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001809 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001810
Mike Christie7996a772006-04-06 21:13:41 -05001811 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001812 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001813
1814 /*
1815 * block eh thread until:
1816 *
Mike Christie843c0a82007-12-13 12:43:20 -06001817 * 1) tmf response
1818 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001819 * 3) session is terminated or restarted or userspace has
1820 * given up on recovery
1821 */
Mike Christie843c0a82007-12-13 12:43:20 -06001822 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001823 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001824 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001825 if (signal_pending(current))
1826 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001827 del_timer_sync(&conn->tmf_timer);
1828
Mike Christie6724add2007-08-15 01:38:30 -05001829 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001830 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001831 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001832 if (age != session->age ||
1833 session->state != ISCSI_STATE_LOGGED_IN)
1834 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001835 return 0;
1836}
1837
1838/*
Mike Christie843c0a82007-12-13 12:43:20 -06001839 * Fail commands. session lock held and recv side suspended and xmit
1840 * thread flushed
1841 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001842static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1843 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001844{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001845 struct iscsi_task *task;
1846 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001847
Mike Christie3bbaaad2009-05-13 17:57:46 -05001848 for (i = 0; i < conn->session->cmds_max; i++) {
1849 task = conn->session->cmds[i];
1850 if (!task->sc || task->state == ISCSI_TASK_FREE)
1851 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001852
Mike Christie3bbaaad2009-05-13 17:57:46 -05001853 if (lun != -1 && lun != task->sc->device->lun)
1854 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001855
Mike Christie3bbaaad2009-05-13 17:57:46 -05001856 ISCSI_DBG_SESSION(conn->session,
1857 "failing sc %p itt 0x%x state %d\n",
1858 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001859 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001860 }
1861}
1862
Mike Christie661134a2009-09-05 07:35:33 +05301863/**
1864 * iscsi_suspend_queue - suspend iscsi_queuecommand
1865 * @conn: iscsi conn to stop queueing IO on
1866 *
1867 * This grabs the session lock to make sure no one is in
1868 * xmit_task/queuecommand, and then sets suspend to prevent
1869 * new commands from being queued. This only needs to be called
1870 * by offload drivers that need to sync a path like ep disconnect
1871 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1872 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1873 */
1874void iscsi_suspend_queue(struct iscsi_conn *conn)
1875{
1876 spin_lock_bh(&conn->session->lock);
1877 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1878 spin_unlock_bh(&conn->session->lock);
1879}
1880EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1881
1882/**
1883 * iscsi_suspend_tx - suspend iscsi_data_xmit
1884 * @conn: iscsi conn tp stop processing IO on.
1885 *
1886 * This function sets the suspend bit to prevent iscsi_data_xmit
1887 * from sending new IO, and if work is queued on the xmit thread
1888 * it will wait for it to be completed.
1889 */
Mike Christieb40977d2008-05-21 15:54:03 -05001890void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001891{
Mike Christie32ae7632009-03-05 14:46:03 -06001892 struct Scsi_Host *shost = conn->session->host;
1893 struct iscsi_host *ihost = shost_priv(shost);
1894
Mike Christie6724add2007-08-15 01:38:30 -05001895 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001896 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001897 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001898}
Mike Christieb40977d2008-05-21 15:54:03 -05001899EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001900
1901static void iscsi_start_tx(struct iscsi_conn *conn)
1902{
1903 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001904 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001905}
1906
Mike Christie4c48a822009-05-13 17:57:45 -05001907/*
1908 * We want to make sure a ping is in flight. It has timed out.
1909 * And we are not busy processing a pdu that is making
1910 * progress but got started before the ping and is taking a while
1911 * to complete so the ping is just stuck behind it in a queue.
1912 */
1913static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1914{
1915 if (conn->ping_task &&
1916 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1917 (conn->ping_timeout * HZ), jiffies))
1918 return 1;
1919 else
1920 return 0;
1921}
1922
Mike Christied355e572009-06-15 22:11:08 -05001923static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001924{
Mike Christied355e572009-06-15 22:11:08 -05001925 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christie92ed4d62010-02-10 16:51:45 -06001926 struct iscsi_task *task = NULL, *running_task;
Mike Christief6d51802007-12-13 12:43:30 -06001927 struct iscsi_cls_session *cls_session;
1928 struct iscsi_session *session;
1929 struct iscsi_conn *conn;
Mike Christie92ed4d62010-02-10 16:51:45 -06001930 int i;
Mike Christief6d51802007-12-13 12:43:30 -06001931
Mike Christied355e572009-06-15 22:11:08 -05001932 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001933 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001934
Erez Zilberbd2199d2009-06-15 22:11:10 -05001935 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001936
1937 spin_lock(&session->lock);
1938 if (session->state != ISCSI_STATE_LOGGED_IN) {
1939 /*
1940 * We are probably in the middle of iscsi recovery so let
1941 * that complete and handle the error.
1942 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001943 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001944 goto done;
1945 }
1946
1947 conn = session->leadconn;
1948 if (!conn) {
1949 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001950 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001951 goto done;
1952 }
1953
Mike Christied355e572009-06-15 22:11:08 -05001954 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie92ed4d62010-02-10 16:51:45 -06001955 if (!task) {
1956 /*
1957 * Raced with completion. Just reset timer, and let it
1958 * complete normally
1959 */
1960 rc = BLK_EH_RESET_TIMER;
Mike Christied355e572009-06-15 22:11:08 -05001961 goto done;
Mike Christie92ed4d62010-02-10 16:51:45 -06001962 }
1963
Mike Christied355e572009-06-15 22:11:08 -05001964 /*
1965 * If we have sent (at least queued to the network layer) a pdu or
1966 * recvd one for the task since the last timeout ask for
1967 * more time. If on the next timeout we have not made progress
1968 * we can check if it is the task or connection when we send the
1969 * nop as a ping.
1970 */
Mike Christie92ed4d62010-02-10 16:51:45 -06001971 if (time_after(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001972 ISCSI_DBG_EH(session, "Command making progress. Asking "
1973 "scsi-ml for more time to complete. "
Mike Christie92ed4d62010-02-10 16:51:45 -06001974 "Last data xfer at %lu. Last timeout was at "
Erez Zilberbd2199d2009-06-15 22:11:10 -05001975 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001976 task->have_checked_conn = false;
1977 rc = BLK_EH_RESET_TIMER;
1978 goto done;
1979 }
1980
Mike Christief6d51802007-12-13 12:43:30 -06001981 if (!conn->recv_timeout && !conn->ping_timeout)
1982 goto done;
1983 /*
1984 * if the ping timedout then we are in the middle of cleaning up
1985 * and can let the iscsi eh handle it
1986 */
Mike Christie4c48a822009-05-13 17:57:45 -05001987 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001988 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001989 goto done;
1990 }
Mike Christied355e572009-06-15 22:11:08 -05001991
Mike Christie92ed4d62010-02-10 16:51:45 -06001992 for (i = 0; i < conn->session->cmds_max; i++) {
1993 running_task = conn->session->cmds[i];
1994 if (!running_task->sc || running_task == task ||
1995 running_task->state != ISCSI_TASK_RUNNING)
1996 continue;
1997
1998 /*
1999 * Only check if cmds started before this one have made
2000 * progress, or this could never fail
2001 */
2002 if (time_after(running_task->sc->jiffies_at_alloc,
2003 task->sc->jiffies_at_alloc))
2004 continue;
2005
2006 if (time_after(running_task->last_xfer, task->last_timeout)) {
2007 /*
2008 * This task has not made progress, but a task
2009 * started before us has transferred data since
2010 * we started/last-checked. We could be queueing
2011 * too many tasks or the LU is bad.
2012 *
2013 * If the device is bad the cmds ahead of us on
2014 * other devs will complete, and this loop will
2015 * eventually fail starting the scsi eh.
2016 */
2017 ISCSI_DBG_EH(session, "Command has not made progress "
2018 "but commands ahead of it have. "
2019 "Asking scsi-ml for more time to "
2020 "complete. Our last xfer vs running task "
2021 "last xfer %lu/%lu. Last check %lu.\n",
2022 task->last_xfer, running_task->last_xfer,
2023 task->last_timeout);
2024 rc = BLK_EH_RESET_TIMER;
2025 goto done;
2026 }
2027 }
2028
Mike Christied355e572009-06-15 22:11:08 -05002029 /* Assumes nop timeout is shorter than scsi cmd timeout */
2030 if (task->have_checked_conn)
2031 goto done;
2032
Mike Christief6d51802007-12-13 12:43:30 -06002033 /*
Mike Christied355e572009-06-15 22:11:08 -05002034 * Checking the transport already or nop from a cmd timeout still
2035 * running
Mike Christief6d51802007-12-13 12:43:30 -06002036 */
Mike Christied355e572009-06-15 22:11:08 -05002037 if (conn->ping_task) {
2038 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07002039 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05002040 goto done;
2041 }
2042
Mike Christied355e572009-06-15 22:11:08 -05002043 /* Make sure there is a transport check done */
2044 iscsi_send_nopout(conn, NULL);
2045 task->have_checked_conn = true;
2046 rc = BLK_EH_RESET_TIMER;
2047
Mike Christief6d51802007-12-13 12:43:30 -06002048done:
Mike Christied355e572009-06-15 22:11:08 -05002049 if (task)
2050 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06002051 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002052 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2053 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06002054 return rc;
2055}
2056
2057static void iscsi_check_transport_timeouts(unsigned long data)
2058{
2059 struct iscsi_conn *conn = (struct iscsi_conn *)data;
2060 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05002061 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06002062
2063 spin_lock(&session->lock);
2064 if (session->state != ISCSI_STATE_LOGGED_IN)
2065 goto done;
2066
Mike Christie4cf10432008-05-07 20:43:52 -05002067 recv_timeout = conn->recv_timeout;
2068 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06002069 goto done;
2070
Mike Christie4cf10432008-05-07 20:43:52 -05002071 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06002072 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05002073
2074 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06002075 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05002076 "expired, recv timeout %d, last rx %lu, "
2077 "last ping %lu, now %lu\n",
2078 conn->ping_timeout, conn->recv_timeout,
2079 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06002080 spin_unlock(&session->lock);
2081 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2082 return;
2083 }
2084
Mike Christie4cf10432008-05-07 20:43:52 -05002085 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05002086 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06002087 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05002088 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05002089 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06002090 } else
Mike Christie4cf10432008-05-07 20:43:52 -05002091 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06002092
Mike Christie1b2c7af2009-03-05 14:45:58 -06002093 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06002094 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06002095done:
2096 spin_unlock(&session->lock);
2097}
2098
Mike Christie9c19a7d2008-05-21 15:54:09 -05002099static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06002100 struct iscsi_tm *hdr)
2101{
2102 memset(hdr, 0, sizeof(*hdr));
2103 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2104 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2105 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06002106 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2107 hdr->rtt = task->hdr_itt;
2108 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06002109}
2110
Mike Christie7996a772006-04-06 21:13:41 -05002111int iscsi_eh_abort(struct scsi_cmnd *sc)
2112{
Mike Christie75613522008-05-21 15:53:59 -05002113 struct iscsi_cls_session *cls_session;
2114 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04002115 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002116 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06002117 struct iscsi_tm *hdr;
2118 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05002119
Mike Christie75613522008-05-21 15:53:59 -05002120 cls_session = starget_to_session(scsi_target(sc->device));
2121 session = cls_session->dd_data;
2122
Erez Zilberbd2199d2009-06-15 22:11:10 -05002123 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05002124
Mike Christie6724add2007-08-15 01:38:30 -05002125 mutex_lock(&session->eh_mutex);
2126 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04002127 /*
2128 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2129 * got the command.
2130 */
2131 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002132 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2133 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05002134 spin_unlock_bh(&session->lock);
2135 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04002136 return SUCCESS;
2137 }
2138
Mike Christie7996a772006-04-06 21:13:41 -05002139 /*
2140 * If we are not logged in or we have started a new session
2141 * then let the host reset code handle this
2142 */
Mike Christie843c0a82007-12-13 12:43:20 -06002143 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2144 sc->SCp.phase != session->age) {
2145 spin_unlock_bh(&session->lock);
2146 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002147 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05002148 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06002149 return FAILED;
2150 }
2151
2152 conn = session->leadconn;
2153 conn->eh_abort_cnt++;
2154 age = session->age;
2155
Mike Christie9c19a7d2008-05-21 15:54:09 -05002156 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002157 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2158 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002159
Mike Christie9c19a7d2008-05-21 15:54:09 -05002160 /* task completed before time out */
2161 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002162 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002163 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002164 }
Mike Christie7996a772006-04-06 21:13:41 -05002165
Mike Christie9c19a7d2008-05-21 15:54:09 -05002166 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002167 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002168 goto success;
2169 }
Mike Christie7996a772006-04-06 21:13:41 -05002170
Mike Christie843c0a82007-12-13 12:43:20 -06002171 /* only have one tmf outstanding at a time */
2172 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002173 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002174 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002175
Mike Christie843c0a82007-12-13 12:43:20 -06002176 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002177 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002178
Mike Christie9c19a7d2008-05-21 15:54:09 -05002179 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002180 rc = FAILED;
2181 goto failed;
2182 }
2183
2184 switch (conn->tmf_state) {
2185 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002186 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002187 /*
2188 * stop tx side incase the target had sent a abort rsp but
2189 * the initiator was still writing out data.
2190 */
Mike Christie6724add2007-08-15 01:38:30 -05002191 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002192 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002193 * we do not stop the recv side because targets have been
2194 * good and have never sent us a successful tmf response
2195 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002196 */
Mike Christie6187c242009-07-15 15:02:57 -05002197 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002198 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002199 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002200 memset(hdr, 0, sizeof(*hdr));
Mike Christie6187c242009-07-15 15:02:57 -05002201 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002202 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002203 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002204 case TMF_TIMEDOUT:
2205 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002206 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie843c0a82007-12-13 12:43:20 -06002207 goto failed_unlocked;
2208 case TMF_NOT_FOUND:
2209 if (!sc->SCp.ptr) {
2210 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002211 memset(hdr, 0, sizeof(*hdr));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002212 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002213 ISCSI_DBG_EH(session, "sc completed while abort in "
2214 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002215 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002216 }
2217 /* fall through */
2218 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002219 conn->tmf_state = TMF_INITIAL;
2220 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002221 }
2222
Mike Christie77a23c22007-05-30 12:57:18 -05002223success:
Mike Christie7996a772006-04-06 21:13:41 -05002224 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002225success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002226 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2227 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002228 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002229 return SUCCESS;
2230
2231failed:
2232 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002233failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002234 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2235 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002236 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002237 return FAILED;
2238}
2239EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2240
Mike Christie843c0a82007-12-13 12:43:20 -06002241static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2242{
2243 memset(hdr, 0, sizeof(*hdr));
2244 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2245 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2246 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2247 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002248 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002249}
2250
2251int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2252{
Mike Christie75613522008-05-21 15:53:59 -05002253 struct iscsi_cls_session *cls_session;
2254 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002255 struct iscsi_conn *conn;
2256 struct iscsi_tm *hdr;
2257 int rc = FAILED;
2258
Mike Christie75613522008-05-21 15:53:59 -05002259 cls_session = starget_to_session(scsi_target(sc->device));
2260 session = cls_session->dd_data;
2261
Erez Zilberbd2199d2009-06-15 22:11:10 -05002262 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002263
2264 mutex_lock(&session->eh_mutex);
2265 spin_lock_bh(&session->lock);
2266 /*
2267 * Just check if we are not logged in. We cannot check for
2268 * the phase because the reset could come from a ioctl.
2269 */
2270 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2271 goto unlock;
2272 conn = session->leadconn;
2273
2274 /* only have one tmf outstanding at a time */
2275 if (conn->tmf_state != TMF_INITIAL)
2276 goto unlock;
2277 conn->tmf_state = TMF_QUEUED;
2278
2279 hdr = &conn->tmhdr;
2280 iscsi_prep_lun_reset_pdu(sc, hdr);
2281
Mike Christie9c19a7d2008-05-21 15:54:09 -05002282 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002283 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002284 rc = FAILED;
2285 goto unlock;
2286 }
2287
2288 switch (conn->tmf_state) {
2289 case TMF_SUCCESS:
2290 break;
2291 case TMF_TIMEDOUT:
2292 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002293 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie843c0a82007-12-13 12:43:20 -06002294 goto done;
2295 default:
2296 conn->tmf_state = TMF_INITIAL;
2297 goto unlock;
2298 }
2299
2300 rc = SUCCESS;
2301 spin_unlock_bh(&session->lock);
2302
2303 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002304
Mike Christiea3439142008-09-24 11:46:15 -05002305 spin_lock_bh(&session->lock);
Mike Christie5d12c052009-11-11 16:34:32 -06002306 memset(hdr, 0, sizeof(*hdr));
Mike Christie3bbaaad2009-05-13 17:57:46 -05002307 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002308 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002309 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002310
2311 iscsi_start_tx(conn);
2312 goto done;
2313
2314unlock:
2315 spin_unlock_bh(&session->lock);
2316done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002317 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2318 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002319 mutex_unlock(&session->eh_mutex);
2320 return rc;
2321}
2322EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2323
Mike Christie3fe5ae82009-11-11 16:34:33 -06002324void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2325{
2326 struct iscsi_session *session = cls_session->dd_data;
2327
2328 spin_lock_bh(&session->lock);
2329 if (session->state != ISCSI_STATE_LOGGED_IN) {
2330 session->state = ISCSI_STATE_RECOVERY_FAILED;
2331 if (session->leadconn)
2332 wake_up(&session->leadconn->ehwait);
2333 }
2334 spin_unlock_bh(&session->lock);
2335}
2336EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2337
2338/**
2339 * iscsi_eh_session_reset - drop session and attempt relogin
2340 * @sc: scsi command
2341 *
2342 * This function will wait for a relogin, session termination from
2343 * userspace, or a recovery/replacement timeout.
2344 */
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302345int iscsi_eh_session_reset(struct scsi_cmnd *sc)
Mike Christie3fe5ae82009-11-11 16:34:33 -06002346{
2347 struct iscsi_cls_session *cls_session;
2348 struct iscsi_session *session;
2349 struct iscsi_conn *conn;
2350
2351 cls_session = starget_to_session(scsi_target(sc->device));
2352 session = cls_session->dd_data;
2353 conn = session->leadconn;
2354
2355 mutex_lock(&session->eh_mutex);
2356 spin_lock_bh(&session->lock);
2357 if (session->state == ISCSI_STATE_TERMINATE) {
2358failed:
2359 ISCSI_DBG_EH(session,
2360 "failing session reset: Could not log back into "
2361 "%s, %s [age %d]\n", session->targetname,
2362 conn->persistent_address, session->age);
2363 spin_unlock_bh(&session->lock);
2364 mutex_unlock(&session->eh_mutex);
2365 return FAILED;
2366 }
2367
2368 spin_unlock_bh(&session->lock);
2369 mutex_unlock(&session->eh_mutex);
2370 /*
2371 * we drop the lock here but the leadconn cannot be destoyed while
2372 * we are in the scsi eh
2373 */
Mike Christiedf4da5c2010-12-31 02:22:18 -06002374 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002375
2376 ISCSI_DBG_EH(session, "wait for relogin\n");
2377 wait_event_interruptible(conn->ehwait,
2378 session->state == ISCSI_STATE_TERMINATE ||
2379 session->state == ISCSI_STATE_LOGGED_IN ||
2380 session->state == ISCSI_STATE_RECOVERY_FAILED);
2381 if (signal_pending(current))
2382 flush_signals(current);
2383
2384 mutex_lock(&session->eh_mutex);
2385 spin_lock_bh(&session->lock);
2386 if (session->state == ISCSI_STATE_LOGGED_IN) {
2387 ISCSI_DBG_EH(session,
2388 "session reset succeeded for %s,%s\n",
2389 session->targetname, conn->persistent_address);
2390 } else
2391 goto failed;
2392 spin_unlock_bh(&session->lock);
2393 mutex_unlock(&session->eh_mutex);
2394 return SUCCESS;
2395}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302396EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002397
2398static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2399{
2400 memset(hdr, 0, sizeof(*hdr));
2401 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2402 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2403 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2404 hdr->rtt = RESERVED_ITT;
2405}
2406
2407/**
2408 * iscsi_eh_target_reset - reset target
2409 * @sc: scsi command
2410 *
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302411 * This will attempt to send a warm target reset.
Mike Christie3fe5ae82009-11-11 16:34:33 -06002412 */
2413int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2414{
2415 struct iscsi_cls_session *cls_session;
2416 struct iscsi_session *session;
2417 struct iscsi_conn *conn;
2418 struct iscsi_tm *hdr;
2419 int rc = FAILED;
2420
2421 cls_session = starget_to_session(scsi_target(sc->device));
2422 session = cls_session->dd_data;
2423
2424 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2425 session->targetname);
2426
2427 mutex_lock(&session->eh_mutex);
2428 spin_lock_bh(&session->lock);
2429 /*
2430 * Just check if we are not logged in. We cannot check for
2431 * the phase because the reset could come from a ioctl.
2432 */
2433 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2434 goto unlock;
2435 conn = session->leadconn;
2436
2437 /* only have one tmf outstanding at a time */
2438 if (conn->tmf_state != TMF_INITIAL)
2439 goto unlock;
2440 conn->tmf_state = TMF_QUEUED;
2441
2442 hdr = &conn->tmhdr;
2443 iscsi_prep_tgt_reset_pdu(sc, hdr);
2444
2445 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2446 session->tgt_reset_timeout)) {
2447 rc = FAILED;
2448 goto unlock;
2449 }
2450
2451 switch (conn->tmf_state) {
2452 case TMF_SUCCESS:
2453 break;
2454 case TMF_TIMEDOUT:
2455 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002456 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002457 goto done;
2458 default:
2459 conn->tmf_state = TMF_INITIAL;
2460 goto unlock;
2461 }
2462
2463 rc = SUCCESS;
2464 spin_unlock_bh(&session->lock);
2465
2466 iscsi_suspend_tx(conn);
2467
2468 spin_lock_bh(&session->lock);
2469 memset(hdr, 0, sizeof(*hdr));
2470 fail_scsi_tasks(conn, -1, DID_ERROR);
2471 conn->tmf_state = TMF_INITIAL;
2472 spin_unlock_bh(&session->lock);
2473
2474 iscsi_start_tx(conn);
2475 goto done;
2476
2477unlock:
2478 spin_unlock_bh(&session->lock);
2479done:
2480 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2481 rc == SUCCESS ? "SUCCESS" : "FAILED");
2482 mutex_unlock(&session->eh_mutex);
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302483 return rc;
2484}
2485EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002486
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302487/**
2488 * iscsi_eh_recover_target - reset target and possibly the session
2489 * @sc: scsi command
2490 *
2491 * This will attempt to send a warm target reset. If that fails,
2492 * we will escalate to ERL0 session recovery.
2493 */
2494int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2495{
2496 int rc;
2497
2498 rc = iscsi_eh_target_reset(sc);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002499 if (rc == FAILED)
2500 rc = iscsi_eh_session_reset(sc);
2501 return rc;
2502}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302503EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002504
Olaf Kirch63203772007-12-13 12:43:25 -06002505/*
2506 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2507 * should be accessed via kfifo_{get,put} on q->queue.
2508 * Optionally, the caller can obtain the array of object pointers
2509 * by passing in a non-NULL @items pointer
2510 */
Mike Christie7996a772006-04-06 21:13:41 -05002511int
Olaf Kirch63203772007-12-13 12:43:25 -06002512iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002513{
Olaf Kirch63203772007-12-13 12:43:25 -06002514 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002515
Olaf Kirch63203772007-12-13 12:43:25 -06002516 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002517
2518 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002519
2520 /* If the user passed an items pointer, he wants a copy of
2521 * the array. */
2522 if (items)
2523 num_arrays++;
2524 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2525 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002526 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002527
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002528 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002529
2530 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002531 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002532 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002533 q->max = i;
2534 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002535 }
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002536 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002537 }
Olaf Kirch63203772007-12-13 12:43:25 -06002538
2539 if (items) {
2540 *items = q->pool + max;
2541 memcpy(*items, q->pool, max * sizeof(void *));
2542 }
2543
Mike Christie7996a772006-04-06 21:13:41 -05002544 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002545
2546enomem:
2547 iscsi_pool_free(q);
2548 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002549}
2550EXPORT_SYMBOL_GPL(iscsi_pool_init);
2551
Olaf Kirch63203772007-12-13 12:43:25 -06002552void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002553{
2554 int i;
2555
2556 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002557 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002558 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05002559}
2560EXPORT_SYMBOL_GPL(iscsi_pool_free);
2561
Mike Christiea4804cd2008-05-21 15:54:00 -05002562/**
2563 * iscsi_host_add - add host to system
2564 * @shost: scsi host
2565 * @pdev: parent device
2566 *
2567 * This should be called by partial offload and software iscsi drivers
2568 * to add a host to the system.
2569 */
2570int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002571{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002572 if (!shost->can_queue)
2573 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2574
Mike Christie4d108352009-03-05 14:46:04 -06002575 if (!shost->cmd_per_lun)
2576 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2577
Mike Christie308cec12009-02-06 12:06:20 -06002578 if (!shost->transportt->eh_timed_out)
2579 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002580 return scsi_add_host(shost, pdev);
2581}
2582EXPORT_SYMBOL_GPL(iscsi_host_add);
2583
2584/**
2585 * iscsi_host_alloc - allocate a host and driver data
2586 * @sht: scsi host template
2587 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002588 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002589 *
2590 * This should be called by partial offload and software iscsi drivers.
2591 * To access the driver specific memory use the iscsi_host_priv() macro.
2592 */
2593struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002594 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002595{
2596 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002597 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002598
2599 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2600 if (!shost)
2601 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002602 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002603
2604 if (xmit_can_sleep) {
2605 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2606 "iscsi_q_%d", shost->host_no);
2607 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2608 if (!ihost->workq)
2609 goto free_host;
2610 }
2611
Mike Christiee5bd7b52008-09-24 11:46:10 -05002612 spin_lock_init(&ihost->lock);
2613 ihost->state = ISCSI_HOST_SETUP;
2614 ihost->num_sessions = 0;
2615 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002616 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002617
2618free_host:
2619 scsi_host_put(shost);
2620 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002621}
Mike Christiea4804cd2008-05-21 15:54:00 -05002622EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002623
Mike Christiee5bd7b52008-09-24 11:46:10 -05002624static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2625{
Mike Christie40a06e72009-03-05 14:46:05 -06002626 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002627}
2628
Mike Christiea4804cd2008-05-21 15:54:00 -05002629/**
2630 * iscsi_host_remove - remove host and sessions
2631 * @shost: scsi host
2632 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002633 * If there are any sessions left, this will initiate the removal and wait
2634 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002635 */
2636void iscsi_host_remove(struct Scsi_Host *shost)
2637{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002638 struct iscsi_host *ihost = shost_priv(shost);
2639 unsigned long flags;
2640
2641 spin_lock_irqsave(&ihost->lock, flags);
2642 ihost->state = ISCSI_HOST_REMOVED;
2643 spin_unlock_irqrestore(&ihost->lock, flags);
2644
2645 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2646 wait_event_interruptible(ihost->session_removal_wq,
2647 ihost->num_sessions == 0);
2648 if (signal_pending(current))
2649 flush_signals(current);
2650
Mike Christiea4804cd2008-05-21 15:54:00 -05002651 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002652 if (ihost->workq)
2653 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002654}
2655EXPORT_SYMBOL_GPL(iscsi_host_remove);
2656
2657void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002658{
2659 struct iscsi_host *ihost = shost_priv(shost);
2660
2661 kfree(ihost->netdev);
2662 kfree(ihost->hwaddress);
2663 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002664 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002665}
Mike Christiea4804cd2008-05-21 15:54:00 -05002666EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002667
Mike Christiee5bd7b52008-09-24 11:46:10 -05002668static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2669{
2670 struct iscsi_host *ihost = shost_priv(shost);
2671 unsigned long flags;
2672
2673 shost = scsi_host_get(shost);
2674 if (!shost) {
2675 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2676 "of session teardown event because host already "
2677 "removed.\n");
2678 return;
2679 }
2680
2681 spin_lock_irqsave(&ihost->lock, flags);
2682 ihost->num_sessions--;
2683 if (ihost->num_sessions == 0)
2684 wake_up(&ihost->session_removal_wq);
2685 spin_unlock_irqrestore(&ihost->lock, flags);
2686 scsi_host_put(shost);
2687}
2688
Mike Christie75613522008-05-21 15:53:59 -05002689/**
2690 * iscsi_session_setup - create iscsi cls session and host and session
2691 * @iscsit: iscsi transport template
2692 * @shost: scsi host
2693 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002694 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002695 * @initial_cmdsn: initial CmdSN
2696 *
2697 * This can be used by software iscsi_transports that allocate
2698 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002699 *
2700 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2701 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2702 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002703 */
2704struct iscsi_cls_session *
2705iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302706 uint16_t cmds_max, int dd_size, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002707 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002708{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002709 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002710 struct iscsi_session *session;
2711 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002712 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002713 unsigned long flags;
2714
2715 spin_lock_irqsave(&ihost->lock, flags);
2716 if (ihost->state == ISCSI_HOST_REMOVED) {
2717 spin_unlock_irqrestore(&ihost->lock, flags);
2718 return NULL;
2719 }
2720 ihost->num_sessions++;
2721 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002722
2723 if (!total_cmds)
2724 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002725 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002726 * The iscsi layer needs some tasks for nop handling and tmfs,
2727 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2728 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002729 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002730 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2731 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2732 "must be a power of two that is at least %d.\n",
2733 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002734 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002735 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002736
2737 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2738 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2739 "must be a power of 2 less than or equal to %d.\n",
2740 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2741 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2742 }
2743
2744 if (!is_power_of_2(total_cmds)) {
2745 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2746 "must be a power of 2.\n", total_cmds);
2747 total_cmds = rounddown_pow_of_two(total_cmds);
2748 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2749 return NULL;
2750 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2751 total_cmds);
2752 }
2753 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002754
Mike Christie5d91e202008-05-21 15:54:01 -05002755 cls_session = iscsi_alloc_session(shost, iscsit,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302756 sizeof(struct iscsi_session) +
2757 dd_size);
Mike Christie75613522008-05-21 15:53:59 -05002758 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002759 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002760 session = cls_session->dd_data;
2761 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002762 session->host = shost;
2763 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002764 session->fast_abort = 1;
Mike Christie3fe5ae82009-11-11 16:34:33 -06002765 session->tgt_reset_timeout = 30;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002766 session->lu_reset_timeout = 15;
2767 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002768 session->scsi_cmds_max = scsi_cmds;
2769 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002770 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002771 session->exp_cmdsn = initial_cmdsn + 1;
2772 session->max_cmdsn = initial_cmdsn + 1;
2773 session->max_r2t = 1;
2774 session->tt = iscsit;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302775 session->dd_data = cls_session->dd_data + sizeof(*session);
Mike Christie6724add2007-08-15 01:38:30 -05002776 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002777 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002778
2779 /* initialize SCSI PDU commands pool */
2780 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2781 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002782 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002783 goto cmdpool_alloc_fail;
2784
2785 /* pre-format cmds pool with ITT */
2786 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002787 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002788
Mike Christie9c19a7d2008-05-21 15:54:09 -05002789 if (cmd_task_size)
2790 task->dd_data = &task[1];
2791 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002792 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002793 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002794 }
2795
Mike Christief53a88d2006-06-28 12:00:27 -05002796 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002797 goto module_get_fail;
2798
Mike Christie79706342008-05-21 15:54:12 -05002799 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002800 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002801
Mike Christie7996a772006-04-06 21:13:41 -05002802 return cls_session;
2803
2804cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002805 module_put(iscsit->owner);
2806module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002807 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002808cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002809 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002810dec_session_count:
2811 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002812 return NULL;
2813}
2814EXPORT_SYMBOL_GPL(iscsi_session_setup);
2815
2816/**
2817 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002818 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002819 *
Mike Christie75613522008-05-21 15:53:59 -05002820 * The driver must have called iscsi_remove_session before
2821 * calling this.
2822 */
Mike Christie7996a772006-04-06 21:13:41 -05002823void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2824{
Mike Christie75613522008-05-21 15:53:59 -05002825 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002826 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002827 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002828
Olaf Kirch63203772007-12-13 12:43:25 -06002829 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002830
Mike Christieb2c64162007-05-30 12:57:16 -05002831 kfree(session->password);
2832 kfree(session->password_in);
2833 kfree(session->username);
2834 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002835 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002836 kfree(session->initiatorname);
2837 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002838
Mike Christie75613522008-05-21 15:53:59 -05002839 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002840 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002841 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002842}
2843EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2844
2845/**
2846 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2847 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002848 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002849 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002850 */
Mike Christie7996a772006-04-06 21:13:41 -05002851struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002852iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2853 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002854{
Mike Christie75613522008-05-21 15:53:59 -05002855 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002856 struct iscsi_conn *conn;
2857 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002858 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002859
Mike Christie5d91e202008-05-21 15:54:01 -05002860 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2861 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002862 if (!cls_conn)
2863 return NULL;
2864 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002865 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002866
Mike Christie5d91e202008-05-21 15:54:01 -05002867 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002868 conn->session = session;
2869 conn->cls_conn = cls_conn;
2870 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2871 conn->id = conn_idx;
2872 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002873 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002874
2875 init_timer(&conn->transport_timer);
2876 conn->transport_timer.data = (unsigned long)conn;
2877 conn->transport_timer.function = iscsi_check_transport_timeouts;
2878
Mike Christie843c0a82007-12-13 12:43:20 -06002879 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002880 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002881 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002882 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002883
Mike Christie9c19a7d2008-05-21 15:54:09 -05002884 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002885 spin_lock_bh(&session->lock);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002886 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002887 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002888 sizeof(void*))) {
2889 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002890 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002891 }
2892 spin_unlock_bh(&session->lock);
2893
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002894 data = (char *) __get_free_pages(GFP_KERNEL,
2895 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002896 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002897 goto login_task_data_alloc_fail;
2898 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002899
Mike Christie843c0a82007-12-13 12:43:20 -06002900 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002901 init_waitqueue_head(&conn->ehwait);
2902
2903 return cls_conn;
2904
Mike Christie9c19a7d2008-05-21 15:54:09 -05002905login_task_data_alloc_fail:
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002906 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002907 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002908login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002909 iscsi_destroy_conn(cls_conn);
2910 return NULL;
2911}
2912EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2913
2914/**
2915 * iscsi_conn_teardown - teardown iscsi connection
2916 * cls_conn: iscsi class connection
2917 *
2918 * TODO: we may need to make this into a two step process
2919 * like scsi-mls remove + put host
2920 */
2921void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2922{
2923 struct iscsi_conn *conn = cls_conn->dd_data;
2924 struct iscsi_session *session = conn->session;
2925 unsigned long flags;
2926
Mike Christief6d51802007-12-13 12:43:30 -06002927 del_timer_sync(&conn->transport_timer);
2928
Mike Christie7996a772006-04-06 21:13:41 -05002929 spin_lock_bh(&session->lock);
2930 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2931 if (session->leadconn == conn) {
2932 /*
2933 * leading connection? then give up on recovery.
2934 */
2935 session->state = ISCSI_STATE_TERMINATE;
2936 wake_up(&conn->ehwait);
2937 }
2938 spin_unlock_bh(&session->lock);
2939
Mike Christie7996a772006-04-06 21:13:41 -05002940 /*
2941 * Block until all in-progress commands for this connection
2942 * time out or fail.
2943 */
2944 for (;;) {
2945 spin_lock_irqsave(session->host->host_lock, flags);
2946 if (!session->host->host_busy) { /* OK for ERL == 0 */
2947 spin_unlock_irqrestore(session->host->host_lock, flags);
2948 break;
2949 }
2950 spin_unlock_irqrestore(session->host->host_lock, flags);
2951 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002952 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2953 "host_busy %d host_failed %d\n",
2954 session->host->host_busy,
2955 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002956 /*
2957 * force eh_abort() to unblock
2958 */
2959 wake_up(&conn->ehwait);
2960 }
2961
Mike Christie779ea122007-02-28 17:32:15 -06002962 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002963 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002964
Mike Christie7996a772006-04-06 21:13:41 -05002965 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002966 free_pages((unsigned long) conn->data,
2967 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002968 kfree(conn->persistent_address);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002969 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002970 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002971 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002972 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002973 spin_unlock_bh(&session->lock);
2974
Mike Christie7996a772006-04-06 21:13:41 -05002975 iscsi_destroy_conn(cls_conn);
2976}
2977EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2978
2979int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2980{
2981 struct iscsi_conn *conn = cls_conn->dd_data;
2982 struct iscsi_session *session = conn->session;
2983
Mike Christieffd04362006-08-31 18:09:24 -04002984 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002985 iscsi_conn_printk(KERN_ERR, conn,
2986 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002987 return -EPERM;
2988 }
2989
Mike Christiedb98ccd2006-08-31 18:09:31 -04002990 if ((session->imm_data_en || !session->initial_r2t_en) &&
2991 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002992 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2993 "first_burst %d max_burst %d\n",
2994 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002995 return -EINVAL;
2996 }
2997
Mike Christief6d51802007-12-13 12:43:30 -06002998 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002999 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3000 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06003001 conn->recv_timeout = 5;
3002 }
3003
3004 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06003005 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3006 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06003007 conn->ping_timeout = 5;
3008 }
3009
Mike Christie7996a772006-04-06 21:13:41 -05003010 spin_lock_bh(&session->lock);
3011 conn->c_stage = ISCSI_CONN_STARTED;
3012 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05003013 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05003014
Mike Christief6d51802007-12-13 12:43:30 -06003015 conn->last_recv = jiffies;
3016 conn->last_ping = jiffies;
3017 if (conn->recv_timeout && conn->ping_timeout)
3018 mod_timer(&conn->transport_timer,
3019 jiffies + (conn->recv_timeout * HZ));
3020
Mike Christie7996a772006-04-06 21:13:41 -05003021 switch(conn->stop_stage) {
3022 case STOP_CONN_RECOVER:
3023 /*
3024 * unblock eh_abort() if it is blocked. re-try all
3025 * commands after successful recovery
3026 */
Mike Christie7996a772006-04-06 21:13:41 -05003027 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06003028 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05003029 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06003030 if (session->age == 16)
3031 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06003032 break;
Mike Christie7996a772006-04-06 21:13:41 -05003033 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05003034 conn->stop_stage = 0;
3035 break;
Mike Christie7996a772006-04-06 21:13:41 -05003036 default:
3037 break;
3038 }
3039 spin_unlock_bh(&session->lock);
3040
Mike Christie75613522008-05-21 15:53:59 -05003041 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06003042 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05003043 return 0;
3044}
3045EXPORT_SYMBOL_GPL(iscsi_conn_start);
3046
3047static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05003048fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05003049{
Mike Christie3bbaaad2009-05-13 17:57:46 -05003050 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05003051 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05003052
Mike Christie3bbaaad2009-05-13 17:57:46 -05003053 for (i = 0; i < conn->session->cmds_max; i++) {
3054 task = conn->session->cmds[i];
3055 if (task->sc)
3056 continue;
3057
3058 if (task->state == ISCSI_TASK_FREE)
3059 continue;
3060
3061 ISCSI_DBG_SESSION(conn->session,
3062 "failing mgmt itt 0x%x state %d\n",
3063 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05003064 state = ISCSI_TASK_ABRT_SESS_RECOV;
3065 if (task->state == ISCSI_TASK_PENDING)
3066 state = ISCSI_TASK_COMPLETED;
3067 iscsi_complete_task(task, state);
3068
Mike Christie7996a772006-04-06 21:13:41 -05003069 }
Mike Christie7996a772006-04-06 21:13:41 -05003070}
3071
Mike Christie656cffc2006-05-18 20:31:42 -05003072static void iscsi_start_session_recovery(struct iscsi_session *session,
3073 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05003074{
Mike Christieed2abc72006-05-02 19:46:40 -05003075 int old_stop_stage;
3076
Mike Christie6724add2007-08-15 01:38:30 -05003077 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003078 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003079 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05003080 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003081 mutex_unlock(&session->eh_mutex);
3082 return;
3083 }
3084
3085 /*
Mike Christieed2abc72006-05-02 19:46:40 -05003086 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05003087 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05003088 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05003089 */
Mike Christie67a61112006-05-30 00:37:20 -05003090 if (flag == STOP_CONN_TERM)
3091 session->state = ISCSI_STATE_TERMINATE;
3092 else if (conn->stop_stage != STOP_CONN_RECOVER)
3093 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie4ae0a6c2010-03-09 14:14:51 -06003094
3095 old_stop_stage = conn->stop_stage;
3096 conn->stop_stage = flag;
Mike Christie26013ad2009-05-13 17:57:43 -05003097 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003098
Mike Christie26013ad2009-05-13 17:57:43 -05003099 del_timer_sync(&conn->transport_timer);
3100 iscsi_suspend_tx(conn);
3101
3102 spin_lock_bh(&session->lock);
Mike Christie67a61112006-05-30 00:37:20 -05003103 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05003104 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003105
Mike Christie7996a772006-04-06 21:13:41 -05003106 /*
3107 * for connection level recovery we should not calculate
3108 * header digest. conn->hdr_size used for optimization
3109 * in hdr_extract() and will be re-negotiated at
3110 * set_param() time.
3111 */
3112 if (flag == STOP_CONN_RECOVER) {
3113 conn->hdrdgst_en = 0;
3114 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05003115 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05003116 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06003117 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05003118 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05003119 }
Mike Christie7996a772006-04-06 21:13:41 -05003120 }
Mike Christie656cffc2006-05-18 20:31:42 -05003121
Mike Christie656cffc2006-05-18 20:31:42 -05003122 /*
3123 * flush queues.
3124 */
3125 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05003126 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05003127 fail_mgmt_tasks(session, conn);
Mike Christie5d12c052009-11-11 16:34:32 -06003128 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
Mike Christie656cffc2006-05-18 20:31:42 -05003129 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003130 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003131}
Mike Christie7996a772006-04-06 21:13:41 -05003132
3133void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3134{
3135 struct iscsi_conn *conn = cls_conn->dd_data;
3136 struct iscsi_session *session = conn->session;
3137
3138 switch (flag) {
3139 case STOP_CONN_RECOVER:
3140 case STOP_CONN_TERM:
3141 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05003142 break;
Mike Christie7996a772006-04-06 21:13:41 -05003143 default:
Mike Christie322d7392008-01-31 13:36:52 -06003144 iscsi_conn_printk(KERN_ERR, conn,
3145 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05003146 }
3147}
3148EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3149
3150int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3151 struct iscsi_cls_conn *cls_conn, int is_leading)
3152{
Mike Christie75613522008-05-21 15:53:59 -05003153 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04003154 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05003155
Mike Christie7996a772006-04-06 21:13:41 -05003156 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003157 if (is_leading)
3158 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04003159 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003160
3161 /*
3162 * Unblock xmitworker(), Login Phase will pass through.
3163 */
3164 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3165 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3166 return 0;
3167}
3168EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3169
Mike Christie5700b1a2009-05-13 17:57:40 -05003170static int iscsi_switch_str_param(char **param, char *new_val_buf)
3171{
3172 char *new_val;
3173
3174 if (*param) {
3175 if (!strcmp(*param, new_val_buf))
3176 return 0;
3177 }
3178
3179 new_val = kstrdup(new_val_buf, GFP_NOIO);
3180 if (!new_val)
3181 return -ENOMEM;
3182
3183 kfree(*param);
3184 *param = new_val;
3185 return 0;
3186}
Mike Christiea54a52c2006-06-28 12:00:23 -05003187
3188int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3189 enum iscsi_param param, char *buf, int buflen)
3190{
3191 struct iscsi_conn *conn = cls_conn->dd_data;
3192 struct iscsi_session *session = conn->session;
3193 uint32_t value;
3194
3195 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003196 case ISCSI_PARAM_FAST_ABORT:
3197 sscanf(buf, "%d", &session->fast_abort);
3198 break;
Mike Christief6d51802007-12-13 12:43:30 -06003199 case ISCSI_PARAM_ABORT_TMO:
3200 sscanf(buf, "%d", &session->abort_timeout);
3201 break;
3202 case ISCSI_PARAM_LU_RESET_TMO:
3203 sscanf(buf, "%d", &session->lu_reset_timeout);
3204 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003205 case ISCSI_PARAM_TGT_RESET_TMO:
3206 sscanf(buf, "%d", &session->tgt_reset_timeout);
3207 break;
Mike Christief6d51802007-12-13 12:43:30 -06003208 case ISCSI_PARAM_PING_TMO:
3209 sscanf(buf, "%d", &conn->ping_timeout);
3210 break;
3211 case ISCSI_PARAM_RECV_TMO:
3212 sscanf(buf, "%d", &conn->recv_timeout);
3213 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003214 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3215 sscanf(buf, "%d", &conn->max_recv_dlength);
3216 break;
3217 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3218 sscanf(buf, "%d", &conn->max_xmit_dlength);
3219 break;
3220 case ISCSI_PARAM_HDRDGST_EN:
3221 sscanf(buf, "%d", &conn->hdrdgst_en);
3222 break;
3223 case ISCSI_PARAM_DATADGST_EN:
3224 sscanf(buf, "%d", &conn->datadgst_en);
3225 break;
3226 case ISCSI_PARAM_INITIAL_R2T_EN:
3227 sscanf(buf, "%d", &session->initial_r2t_en);
3228 break;
3229 case ISCSI_PARAM_MAX_R2T:
3230 sscanf(buf, "%d", &session->max_r2t);
3231 break;
3232 case ISCSI_PARAM_IMM_DATA_EN:
3233 sscanf(buf, "%d", &session->imm_data_en);
3234 break;
3235 case ISCSI_PARAM_FIRST_BURST:
3236 sscanf(buf, "%d", &session->first_burst);
3237 break;
3238 case ISCSI_PARAM_MAX_BURST:
3239 sscanf(buf, "%d", &session->max_burst);
3240 break;
3241 case ISCSI_PARAM_PDU_INORDER_EN:
3242 sscanf(buf, "%d", &session->pdu_inorder_en);
3243 break;
3244 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3245 sscanf(buf, "%d", &session->dataseq_inorder_en);
3246 break;
3247 case ISCSI_PARAM_ERL:
3248 sscanf(buf, "%d", &session->erl);
3249 break;
3250 case ISCSI_PARAM_IFMARKER_EN:
3251 sscanf(buf, "%d", &value);
3252 BUG_ON(value);
3253 break;
3254 case ISCSI_PARAM_OFMARKER_EN:
3255 sscanf(buf, "%d", &value);
3256 BUG_ON(value);
3257 break;
3258 case ISCSI_PARAM_EXP_STATSN:
3259 sscanf(buf, "%u", &conn->exp_statsn);
3260 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003261 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003262 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003263 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003264 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003265 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05003266 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003267 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003268 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003269 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003270 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003271 case ISCSI_PARAM_TPGT:
3272 sscanf(buf, "%d", &session->tpgt);
3273 break;
3274 case ISCSI_PARAM_PERSISTENT_PORT:
3275 sscanf(buf, "%d", &conn->persistent_port);
3276 break;
3277 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003278 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003279 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003280 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003281 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003282 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003283 default:
3284 return -ENOSYS;
3285 }
3286
3287 return 0;
3288}
3289EXPORT_SYMBOL_GPL(iscsi_set_param);
3290
3291int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3292 enum iscsi_param param, char *buf)
3293{
Mike Christie75613522008-05-21 15:53:59 -05003294 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003295 int len;
3296
3297 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003298 case ISCSI_PARAM_FAST_ABORT:
3299 len = sprintf(buf, "%d\n", session->fast_abort);
3300 break;
Mike Christief6d51802007-12-13 12:43:30 -06003301 case ISCSI_PARAM_ABORT_TMO:
3302 len = sprintf(buf, "%d\n", session->abort_timeout);
3303 break;
3304 case ISCSI_PARAM_LU_RESET_TMO:
3305 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3306 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003307 case ISCSI_PARAM_TGT_RESET_TMO:
3308 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3309 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003310 case ISCSI_PARAM_INITIAL_R2T_EN:
3311 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3312 break;
3313 case ISCSI_PARAM_MAX_R2T:
3314 len = sprintf(buf, "%hu\n", session->max_r2t);
3315 break;
3316 case ISCSI_PARAM_IMM_DATA_EN:
3317 len = sprintf(buf, "%d\n", session->imm_data_en);
3318 break;
3319 case ISCSI_PARAM_FIRST_BURST:
3320 len = sprintf(buf, "%u\n", session->first_burst);
3321 break;
3322 case ISCSI_PARAM_MAX_BURST:
3323 len = sprintf(buf, "%u\n", session->max_burst);
3324 break;
3325 case ISCSI_PARAM_PDU_INORDER_EN:
3326 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3327 break;
3328 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3329 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3330 break;
3331 case ISCSI_PARAM_ERL:
3332 len = sprintf(buf, "%d\n", session->erl);
3333 break;
3334 case ISCSI_PARAM_TARGET_NAME:
3335 len = sprintf(buf, "%s\n", session->targetname);
3336 break;
3337 case ISCSI_PARAM_TPGT:
3338 len = sprintf(buf, "%d\n", session->tpgt);
3339 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003340 case ISCSI_PARAM_USERNAME:
3341 len = sprintf(buf, "%s\n", session->username);
3342 break;
3343 case ISCSI_PARAM_USERNAME_IN:
3344 len = sprintf(buf, "%s\n", session->username_in);
3345 break;
3346 case ISCSI_PARAM_PASSWORD:
3347 len = sprintf(buf, "%s\n", session->password);
3348 break;
3349 case ISCSI_PARAM_PASSWORD_IN:
3350 len = sprintf(buf, "%s\n", session->password_in);
3351 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003352 case ISCSI_PARAM_IFACE_NAME:
3353 len = sprintf(buf, "%s\n", session->ifacename);
3354 break;
3355 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003356 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003357 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003358 default:
3359 return -ENOSYS;
3360 }
3361
3362 return len;
3363}
3364EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3365
3366int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3367 enum iscsi_param param, char *buf)
3368{
3369 struct iscsi_conn *conn = cls_conn->dd_data;
3370 int len;
3371
3372 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003373 case ISCSI_PARAM_PING_TMO:
3374 len = sprintf(buf, "%u\n", conn->ping_timeout);
3375 break;
3376 case ISCSI_PARAM_RECV_TMO:
3377 len = sprintf(buf, "%u\n", conn->recv_timeout);
3378 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003379 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3380 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3381 break;
3382 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3383 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3384 break;
3385 case ISCSI_PARAM_HDRDGST_EN:
3386 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3387 break;
3388 case ISCSI_PARAM_DATADGST_EN:
3389 len = sprintf(buf, "%d\n", conn->datadgst_en);
3390 break;
3391 case ISCSI_PARAM_IFMARKER_EN:
3392 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3393 break;
3394 case ISCSI_PARAM_OFMARKER_EN:
3395 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3396 break;
3397 case ISCSI_PARAM_EXP_STATSN:
3398 len = sprintf(buf, "%u\n", conn->exp_statsn);
3399 break;
3400 case ISCSI_PARAM_PERSISTENT_PORT:
3401 len = sprintf(buf, "%d\n", conn->persistent_port);
3402 break;
3403 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3404 len = sprintf(buf, "%s\n", conn->persistent_address);
3405 break;
3406 default:
3407 return -ENOSYS;
3408 }
3409
3410 return len;
3411}
3412EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3413
Mike Christie0801c242007-05-30 12:57:12 -05003414int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3415 char *buf)
3416{
Mike Christie75613522008-05-21 15:53:59 -05003417 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003418 int len;
3419
3420 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003421 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003422 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003423 break;
Mike Christie0801c242007-05-30 12:57:12 -05003424 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003425 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003426 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003427 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003428 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003429 break;
Mike Christie75613522008-05-21 15:53:59 -05003430 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003431 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003432 break;
Mike Christie0801c242007-05-30 12:57:12 -05003433 default:
3434 return -ENOSYS;
3435 }
3436
3437 return len;
3438}
3439EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3440
3441int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3442 char *buf, int buflen)
3443{
Mike Christie75613522008-05-21 15:53:59 -05003444 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003445
3446 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003447 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003448 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003449 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003450 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003451 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003452 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003453 default:
3454 return -ENOSYS;
3455 }
3456
3457 return 0;
3458}
3459EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3460
Mike Christie7996a772006-04-06 21:13:41 -05003461MODULE_AUTHOR("Mike Christie");
3462MODULE_DESCRIPTION("iSCSI library functions");
3463MODULE_LICENSE("GPL");