blob: c15fde808c33e98ea1b48398afaf951e19eedeaa [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
Mike Christie9c19a7d2008-05-21 15:54:09 -0500542static void __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}
547
Mike Christie9c19a7d2008-05-21 15:54:09 -0500548void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500549{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500550 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500551
552 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500553 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500554 spin_unlock_bh(&session->lock);
555}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500556EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500557
Mike Christie3bbaaad2009-05-13 17:57:46 -0500558/**
559 * iscsi_complete_task - finish a task
560 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500561 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500562 *
563 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600564 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500565static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600566{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500567 struct iscsi_conn *conn = task->conn;
568
Mike Christie4421c9e2009-05-13 17:57:50 -0500569 ISCSI_DBG_SESSION(conn->session,
570 "complete task itt 0x%x state %d sc %p\n",
571 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500572 if (task->state == ISCSI_TASK_COMPLETED ||
573 task->state == ISCSI_TASK_ABRT_TMF ||
574 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500575 return;
576 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500577 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500578
579 if (!list_empty(&task->running))
580 list_del_init(&task->running);
581
582 if (conn->task == task)
583 conn->task = NULL;
584
585 if (conn->ping_task == task)
586 conn->ping_task = NULL;
587
588 /* release get from queueing */
589 __iscsi_put_task(task);
590}
591
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530592/**
593 * iscsi_complete_scsi_task - finish scsi task normally
594 * @task: iscsi task for scsi cmd
595 * @exp_cmdsn: expected cmd sn in cpu format
596 * @max_cmdsn: max cmd sn in cpu format
597 *
598 * This is used when drivers do not need or cannot perform
599 * lower level pdu processing.
600 *
601 * Called with session lock
602 */
603void iscsi_complete_scsi_task(struct iscsi_task *task,
604 uint32_t exp_cmdsn, uint32_t max_cmdsn)
605{
606 struct iscsi_conn *conn = task->conn;
607
608 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
609
610 conn->last_recv = jiffies;
611 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
612 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
613}
614EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
615
616
Mike Christie3bbaaad2009-05-13 17:57:46 -0500617/*
618 * session lock must be held and if not called for a task that is
619 * still pending or from the xmit thread, then xmit thread must
620 * be suspended.
621 */
622static void fail_scsi_task(struct iscsi_task *task, int err)
623{
624 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600625 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500626 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600627
Mike Christie3bbaaad2009-05-13 17:57:46 -0500628 /*
629 * if a command completes and we get a successful tmf response
630 * we will hit this because the scsi eh abort code does not take
631 * a ref to the task.
632 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500633 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600634 if (!sc)
635 return;
636
Mike Christieb3cd5052009-05-13 17:57:49 -0500637 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600638 /*
639 * cmd never made it to the xmit thread, so we should not count
640 * the cmd in the sequencing
641 */
642 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500643 /* it was never sent so just complete like normal */
644 state = ISCSI_TASK_COMPLETED;
645 } else if (err == DID_TRANSPORT_DISRUPTED)
646 state = ISCSI_TASK_ABRT_SESS_RECOV;
647 else
648 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600649
Mike Christieb3cd5052009-05-13 17:57:49 -0500650 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500651 if (!scsi_bidi_cmnd(sc))
652 scsi_set_resid(sc, scsi_bufflen(sc));
653 else {
654 scsi_out(sc)->resid = scsi_out(sc)->length;
655 scsi_in(sc)->resid = scsi_in(sc)->length;
656 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500657
Mike Christieb3cd5052009-05-13 17:57:49 -0500658 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600659}
660
Mike Christie3e5c28a2008-05-21 15:54:06 -0500661static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500662 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500663{
664 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600665 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500666 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
Mike Christie4f704dc2009-11-11 16:34:31 -0600667 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie052d0142008-05-21 15:54:05 -0500668
669 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
670 return -ENOTCONN;
671
Mike Christie4f704dc2009-11-11 16:34:31 -0600672 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
Mike Christie052d0142008-05-21 15:54:05 -0500673 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
674 /*
675 * pre-format CmdSN for outgoing PDU.
676 */
677 nop->cmdsn = cpu_to_be32(session->cmdsn);
678 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500679 /*
Mike Christie4f704dc2009-11-11 16:34:31 -0600680 * TODO: We always use immediate for normal session pdus.
Mike Christie052d0142008-05-21 15:54:05 -0500681 * If we start to send tmfs or nops as non-immediate then
682 * we should start checking the cmdsn numbers for mgmt tasks.
Mike Christie4f704dc2009-11-11 16:34:31 -0600683 *
684 * During discovery sessions iscsid sends TEXT as non immediate,
685 * but we always only send one PDU at a time.
Mike Christie052d0142008-05-21 15:54:05 -0500686 */
687 if (conn->c_stage == ISCSI_CONN_STARTED &&
688 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
689 session->queued_cmdsn++;
690 session->cmdsn++;
691 }
692 }
693
Mike Christieae15f802008-12-02 00:32:15 -0600694 if (session->tt->init_task && session->tt->init_task(task))
695 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500696
697 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
698 session->state = ISCSI_STATE_LOGGING_OUT;
699
Mike Christie577577d2008-12-02 00:32:05 -0600700 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600701 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
702 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
703 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500704 return 0;
705}
706
Mike Christie9c19a7d2008-05-21 15:54:09 -0500707static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600708__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
709 char *data, uint32_t data_size)
710{
711 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500712 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie4f704dc2009-11-11 16:34:31 -0600713 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500714 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600715 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600716
717 if (session->state == ISCSI_STATE_TERMINATE)
718 return NULL;
719
Mike Christie4f704dc2009-11-11 16:34:31 -0600720 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
Mike Christief6d51802007-12-13 12:43:30 -0600721 /*
722 * Login and Text are sent serially, in
723 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500724 * Same task can be used. Same ITT must be used.
725 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600726 */
Mike Christie4f704dc2009-11-11 16:34:31 -0600727 if (conn->login_task->state != ISCSI_TASK_FREE) {
728 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
729 "progress. Cannot start new task.\n");
730 return NULL;
731 }
732
Mike Christie9c19a7d2008-05-21 15:54:09 -0500733 task = conn->login_task;
Mike Christie4f704dc2009-11-11 16:34:31 -0600734 } else {
Mike Christie26013ad2009-05-13 17:57:43 -0500735 if (session->state != ISCSI_STATE_LOGGED_IN)
736 return NULL;
737
Mike Christief6d51802007-12-13 12:43:30 -0600738 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
739 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
740
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800741 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500742 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600743 return NULL;
744 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500745 /*
746 * released in complete pdu for task we expect a response for, and
747 * released by the lld when it has transmitted the task for
748 * pdus we do not expect a response for.
749 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500750 atomic_set(&task->refcount, 1);
751 task->conn = conn;
752 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500753 INIT_LIST_HEAD(&task->running);
754 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600755
756 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500757 memcpy(task->data, data, data_size);
758 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600759 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500760 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600761
Mike Christie184b57c2009-05-13 17:57:39 -0500762 if (conn->session->tt->alloc_pdu) {
763 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
764 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
765 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500766 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500767 }
Mike Christie577577d2008-12-02 00:32:05 -0600768 }
Mike Christie184b57c2009-05-13 17:57:39 -0500769
Mike Christie262ef632008-12-02 00:32:13 -0600770 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600771 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500772 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600773
774 if (hdr->itt != RESERVED_ITT) {
775 if (session->tt->parse_pdu_itt)
776 task->hdr->itt = itt;
777 else
778 task->hdr->itt = build_itt(task->itt,
779 task->conn->session->age);
780 }
781
Mike Christie1336aed2009-05-13 17:57:48 -0500782 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600783 if (iscsi_prep_mgmt_task(conn, task))
784 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500785
Mike Christie9c19a7d2008-05-21 15:54:09 -0500786 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600787 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500788 } else {
789 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600790 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500791 }
Mike Christie052d0142008-05-21 15:54:05 -0500792
Mike Christie9c19a7d2008-05-21 15:54:09 -0500793 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600794
795free_task:
796 __iscsi_put_task(task);
797 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600798}
799
800int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
801 char *data, uint32_t data_size)
802{
803 struct iscsi_conn *conn = cls_conn->dd_data;
804 struct iscsi_session *session = conn->session;
805 int err = 0;
806
807 spin_lock_bh(&session->lock);
808 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
809 err = -EPERM;
810 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600811 return err;
812}
813EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
814
Mike Christie7996a772006-04-06 21:13:41 -0500815/**
816 * iscsi_cmd_rsp - SCSI Command Response processing
817 * @conn: iscsi connection
818 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500819 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500820 * @data: cmd data buffer
821 * @datalen: len of buffer
822 *
823 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500824 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500825 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500826static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500827 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500828 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500829{
Mike Christie7996a772006-04-06 21:13:41 -0500830 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
831 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500832 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500833
Mike Christie77a23c22007-05-30 12:57:18 -0500834 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500835 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
836
837 sc->result = (DID_OK << 16) | rhdr->cmd_status;
838
839 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
840 sc->result = DID_ERROR << 16;
841 goto out;
842 }
843
844 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600845 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500846
847 if (datalen < 2) {
848invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600849 iscsi_conn_printk(KERN_ERR, conn,
850 "Got CHECK_CONDITION but invalid data "
851 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500852 sc->result = DID_BAD_TARGET << 16;
853 goto out;
854 }
855
Harvey Harrison8f3339912008-05-21 15:54:20 -0500856 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500857 if (datalen < senselen)
858 goto invalid_datalen;
859
860 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600861 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600862 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
863 min_t(uint16_t, senselen,
864 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500865 }
866
Boaz Harroshc07d4442008-04-18 10:11:52 -0500867 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
868 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
869 int res_count = be32_to_cpu(rhdr->bi_residual_count);
870
871 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
872 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
873 res_count <= scsi_in(sc)->length))
874 scsi_in(sc)->resid = res_count;
875 else
876 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
877 }
878
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600879 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
880 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500881 int res_count = be32_to_cpu(rhdr->residual_count);
882
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600883 if (res_count > 0 &&
884 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
885 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500886 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900887 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500888 else
889 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500890 }
Mike Christie7996a772006-04-06 21:13:41 -0500891out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500892 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600893 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500894 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500895 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500896}
897
Mike Christie1d9edf02008-09-24 11:46:09 -0500898/**
899 * iscsi_data_in_rsp - SCSI Data-In Response processing
900 * @conn: iscsi connection
901 * @hdr: iscsi pdu
902 * @task: scsi command task
903 **/
904static void
905iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
906 struct iscsi_task *task)
907{
908 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
909 struct scsi_cmnd *sc = task->sc;
910
911 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
912 return;
913
Mike Christieedbc9aa052009-05-13 17:57:42 -0500914 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500915 sc->result = (DID_OK << 16) | rhdr->cmd_status;
916 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
917 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
918 ISCSI_FLAG_DATA_OVERFLOW)) {
919 int res_count = be32_to_cpu(rhdr->residual_count);
920
921 if (res_count > 0 &&
922 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
923 res_count <= scsi_in(sc)->length))
924 scsi_in(sc)->resid = res_count;
925 else
926 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
927 }
928
Mike Christie3bbaaad2009-05-13 17:57:46 -0500929 ISCSI_DBG_SESSION(conn->session, "data in with status done "
930 "[sc %p res %d itt 0x%x]\n",
931 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500932 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500933 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500934}
935
Mike Christie7ea8b822006-07-24 15:47:22 -0500936static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
937{
938 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
939
940 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
941 conn->tmfrsp_pdus_cnt++;
942
Mike Christie843c0a82007-12-13 12:43:20 -0600943 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500944 return;
945
946 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600947 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500948 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600949 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500950 else
Mike Christie843c0a82007-12-13 12:43:20 -0600951 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500952 wake_up(&conn->ehwait);
953}
954
Mike Christief6d51802007-12-13 12:43:30 -0600955static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
956{
957 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500958 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600959
Mike Christie9c19a7d2008-05-21 15:54:09 -0500960 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600961 return;
962
963 memset(&hdr, 0, sizeof(struct iscsi_nopout));
964 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
965 hdr.flags = ISCSI_FLAG_CMD_FINAL;
966
967 if (rhdr) {
968 memcpy(hdr.lun, rhdr->lun, 8);
969 hdr.ttt = rhdr->ttt;
970 hdr.itt = RESERVED_ITT;
971 } else
972 hdr.ttt = RESERVED_ITT;
973
Mike Christie9c19a7d2008-05-21 15:54:09 -0500974 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
975 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600976 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600977 else if (!rhdr) {
978 /* only track our nops */
979 conn->ping_task = task;
980 conn->last_ping = jiffies;
981 }
Mike Christief6d51802007-12-13 12:43:30 -0600982}
983
Mike Christie8afa1432009-08-20 15:10:59 -0500984static int iscsi_nop_out_rsp(struct iscsi_task *task,
985 struct iscsi_nopin *nop, char *data, int datalen)
986{
987 struct iscsi_conn *conn = task->conn;
988 int rc = 0;
989
990 if (conn->ping_task != task) {
991 /*
992 * If this is not in response to one of our
993 * nops then it must be from userspace.
994 */
995 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
996 data, datalen))
997 rc = ISCSI_ERR_CONN_FAILED;
998 } else
999 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1000 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1001 return rc;
1002}
1003
Mike Christie62f38302006-08-31 18:09:27 -04001004static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1005 char *data, int datalen)
1006{
1007 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1008 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -05001009 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -04001010
1011 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1012
Mike Christie8afa1432009-08-20 15:10:59 -05001013 if (ntoh24(reject->dlength) > datalen ||
1014 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1015 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1016 "pdu. Invalid data length (pdu dlength "
1017 "%u, datalen %d\n", ntoh24(reject->dlength),
1018 datalen);
1019 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -04001020 }
Mike Christie8afa1432009-08-20 15:10:59 -05001021 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1022 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1023
1024 switch (reject->reason) {
1025 case ISCSI_REASON_DATA_DIGEST_ERROR:
1026 iscsi_conn_printk(KERN_ERR, conn,
1027 "pdu (op 0x%x itt 0x%x) rejected "
1028 "due to DataDigest error.\n",
1029 rejected_pdu.itt, opcode);
1030 break;
1031 case ISCSI_REASON_IMM_CMD_REJECT:
1032 iscsi_conn_printk(KERN_ERR, conn,
1033 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1034 "immediate commands.\n",
1035 rejected_pdu.itt, opcode);
1036 /*
1037 * We only send one TMF at a time so if the target could not
1038 * handle it, then it should get fixed (RFC mandates that
1039 * a target can handle one immediate TMF per conn).
1040 *
1041 * For nops-outs, we could have sent more than one if
1042 * the target is sending us lots of nop-ins
1043 */
1044 if (opcode != ISCSI_OP_NOOP_OUT)
1045 return 0;
1046
1047 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
1048 /*
1049 * nop-out in response to target's nop-out rejected.
1050 * Just resend.
1051 */
1052 iscsi_send_nopout(conn,
1053 (struct iscsi_nopin*)&rejected_pdu);
1054 else {
1055 struct iscsi_task *task;
1056 /*
1057 * Our nop as ping got dropped. We know the target
1058 * and transport are ok so just clean up
1059 */
1060 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1061 if (!task) {
1062 iscsi_conn_printk(KERN_ERR, conn,
1063 "Invalid pdu reject. Could "
1064 "not lookup rejected task.\n");
1065 rc = ISCSI_ERR_BAD_ITT;
1066 } else
1067 rc = iscsi_nop_out_rsp(task,
1068 (struct iscsi_nopin*)&rejected_pdu,
1069 NULL, 0);
1070 }
1071 break;
1072 default:
1073 iscsi_conn_printk(KERN_ERR, conn,
1074 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1075 "code 0x%x\n", rejected_pdu.itt,
1076 rejected_pdu.opcode, reject->reason);
1077 break;
1078 }
1079 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001080}
1081
Mike Christie7996a772006-04-06 21:13:41 -05001082/**
Mike Christie913e5bf2008-05-21 15:54:18 -05001083 * iscsi_itt_to_task - look up task by itt
1084 * @conn: iscsi connection
1085 * @itt: itt
1086 *
1087 * This should be used for mgmt tasks like login and nops, or if
1088 * the LDD's itt space does not include the session age.
1089 *
1090 * The session lock must be held.
1091 */
Mike Christie8f9256c2009-05-13 17:57:41 -05001092struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -05001093{
1094 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001095 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -05001096
1097 if (itt == RESERVED_ITT)
1098 return NULL;
1099
Mike Christie262ef632008-12-02 00:32:13 -06001100 if (session->tt->parse_pdu_itt)
1101 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1102 else
1103 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -05001104 if (i >= session->cmds_max)
1105 return NULL;
1106
1107 return session->cmds[i];
1108}
Mike Christie8f9256c2009-05-13 17:57:41 -05001109EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -05001110
1111/**
Mike Christie7996a772006-04-06 21:13:41 -05001112 * __iscsi_complete_pdu - complete pdu
1113 * @conn: iscsi conn
1114 * @hdr: iscsi header
1115 * @data: data buffer
1116 * @datalen: len of data buffer
1117 *
1118 * Completes pdu processing by freeing any resources allocated at
1119 * queuecommand or send generic. session lock must be held and verify
1120 * itt must have been called.
1121 */
Mike Christie913e5bf2008-05-21 15:54:18 -05001122int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1123 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001124{
1125 struct iscsi_session *session = conn->session;
1126 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001127 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001128 uint32_t itt;
1129
Mike Christief6d51802007-12-13 12:43:30 -06001130 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001131 rc = iscsi_verify_itt(conn, hdr->itt);
1132 if (rc)
1133 return rc;
1134
Al Virob4377352007-02-09 16:39:40 +00001135 if (hdr->itt != RESERVED_ITT)
1136 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001137 else
Al Virob4377352007-02-09 16:39:40 +00001138 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001139
Mike Christie1b2c7af2009-03-05 14:45:58 -06001140 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1141 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001142
Mike Christie3e5c28a2008-05-21 15:54:06 -05001143 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001144 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001145
Mike Christie7996a772006-04-06 21:13:41 -05001146 switch(opcode) {
1147 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001148 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001149 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001150 break;
1151 }
1152
Al Virob4377352007-02-09 16:39:40 +00001153 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001154 break;
1155
Mike Christief6d51802007-12-13 12:43:30 -06001156 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001157 break;
1158 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001159 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1160 break;
Mike Christie7996a772006-04-06 21:13:41 -05001161 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001162 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001163 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1164 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001165 break;
1166 default:
1167 rc = ISCSI_ERR_BAD_OPCODE;
1168 break;
1169 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001170 goto out;
1171 }
Mike Christie7996a772006-04-06 21:13:41 -05001172
Mike Christie3e5c28a2008-05-21 15:54:06 -05001173 switch(opcode) {
1174 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001175 case ISCSI_OP_SCSI_DATA_IN:
1176 task = iscsi_itt_to_ctask(conn, hdr->itt);
1177 if (!task)
1178 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001179 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001180 break;
1181 case ISCSI_OP_R2T:
1182 /*
1183 * LLD handles R2Ts if they need to.
1184 */
1185 return 0;
1186 case ISCSI_OP_LOGOUT_RSP:
1187 case ISCSI_OP_LOGIN_RSP:
1188 case ISCSI_OP_TEXT_RSP:
1189 case ISCSI_OP_SCSI_TMFUNC_RSP:
1190 case ISCSI_OP_NOOP_IN:
1191 task = iscsi_itt_to_task(conn, hdr->itt);
1192 if (!task)
1193 return ISCSI_ERR_BAD_ITT;
1194 break;
1195 default:
1196 return ISCSI_ERR_BAD_OPCODE;
1197 }
1198
1199 switch(opcode) {
1200 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001201 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001202 break;
1203 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001204 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001205 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001206 case ISCSI_OP_LOGOUT_RSP:
1207 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1208 if (datalen) {
1209 rc = ISCSI_ERR_PROTO;
1210 break;
1211 }
1212 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1213 goto recv_pdu;
1214 case ISCSI_OP_LOGIN_RSP:
1215 case ISCSI_OP_TEXT_RSP:
1216 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1217 /*
1218 * login related PDU's exp_statsn is handled in
1219 * userspace
1220 */
1221 goto recv_pdu;
1222 case ISCSI_OP_SCSI_TMFUNC_RSP:
1223 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1224 if (datalen) {
1225 rc = ISCSI_ERR_PROTO;
1226 break;
1227 }
1228
1229 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001230 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001231 break;
1232 case ISCSI_OP_NOOP_IN:
1233 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1234 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1235 rc = ISCSI_ERR_PROTO;
1236 break;
1237 }
1238 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1239
Mike Christie8afa1432009-08-20 15:10:59 -05001240 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1241 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001242 break;
1243 default:
1244 rc = ISCSI_ERR_BAD_OPCODE;
1245 break;
1246 }
1247
1248out:
1249 return rc;
1250recv_pdu:
1251 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1252 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001253 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001254 return rc;
1255}
Mike Christie913e5bf2008-05-21 15:54:18 -05001256EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001257
1258int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1259 char *data, int datalen)
1260{
1261 int rc;
1262
1263 spin_lock(&conn->session->lock);
1264 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1265 spin_unlock(&conn->session->lock);
1266 return rc;
1267}
1268EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1269
Mike Christie0af967f2008-05-21 15:54:04 -05001270int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001271{
1272 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001273 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001274
Mike Christie0af967f2008-05-21 15:54:04 -05001275 if (itt == RESERVED_ITT)
1276 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001277
Mike Christie262ef632008-12-02 00:32:13 -06001278 if (session->tt->parse_pdu_itt)
1279 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1280 else {
1281 i = get_itt(itt);
1282 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1283 }
1284
1285 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001286 iscsi_conn_printk(KERN_ERR, conn,
1287 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001288 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001289 return ISCSI_ERR_BAD_ITT;
1290 }
Mike Christie7996a772006-04-06 21:13:41 -05001291
Mike Christie3e5c28a2008-05-21 15:54:06 -05001292 if (i >= session->cmds_max) {
1293 iscsi_conn_printk(KERN_ERR, conn,
1294 "received invalid itt index %u (max cmds "
1295 "%u.\n", i, session->cmds_max);
1296 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001297 }
Mike Christie7996a772006-04-06 21:13:41 -05001298 return 0;
1299}
1300EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1301
Mike Christie913e5bf2008-05-21 15:54:18 -05001302/**
1303 * iscsi_itt_to_ctask - look up ctask by itt
1304 * @conn: iscsi connection
1305 * @itt: itt
1306 *
1307 * This should be used for cmd tasks.
1308 *
1309 * The session lock must be held.
1310 */
1311struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001312{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001313 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001314
1315 if (iscsi_verify_itt(conn, itt))
1316 return NULL;
1317
Mike Christie913e5bf2008-05-21 15:54:18 -05001318 task = iscsi_itt_to_task(conn, itt);
1319 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001320 return NULL;
1321
Mike Christie913e5bf2008-05-21 15:54:18 -05001322 if (task->sc->SCp.phase != conn->session->age) {
1323 iscsi_session_printk(KERN_ERR, conn->session,
1324 "task's session age %d, expected %d\n",
1325 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001326 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001327 }
Mike Christie0af967f2008-05-21 15:54:04 -05001328
Mike Christie9c19a7d2008-05-21 15:54:09 -05001329 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001330}
1331EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1332
Mike Christie40a06e72009-03-05 14:46:05 -06001333void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001334 enum iscsi_err err)
1335{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001336 struct iscsi_conn *conn;
1337 struct device *dev;
1338 unsigned long flags;
1339
1340 spin_lock_irqsave(&session->lock, flags);
1341 conn = session->leadconn;
1342 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1343 spin_unlock_irqrestore(&session->lock, flags);
1344 return;
1345 }
1346
1347 dev = get_device(&conn->cls_conn->dev);
1348 spin_unlock_irqrestore(&session->lock, flags);
1349 if (!dev)
1350 return;
1351 /*
1352 * if the host is being removed bypass the connection
1353 * recovery initialization because we are going to kill
1354 * the session.
1355 */
1356 if (err == ISCSI_ERR_INVALID_HOST)
1357 iscsi_conn_error_event(conn->cls_conn, err);
1358 else
1359 iscsi_conn_failure(conn, err);
1360 put_device(dev);
1361}
1362EXPORT_SYMBOL_GPL(iscsi_session_failure);
1363
Mike Christie7996a772006-04-06 21:13:41 -05001364void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1365{
1366 struct iscsi_session *session = conn->session;
1367 unsigned long flags;
1368
1369 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001370 if (session->state == ISCSI_STATE_FAILED) {
1371 spin_unlock_irqrestore(&session->lock, flags);
1372 return;
1373 }
1374
Mike Christie67a61112006-05-30 00:37:20 -05001375 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001376 session->state = ISCSI_STATE_FAILED;
1377 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001378
Mike Christie7996a772006-04-06 21:13:41 -05001379 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1380 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001381 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001382}
1383EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1384
Mike Christie77a23c22007-05-30 12:57:18 -05001385static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1386{
1387 struct iscsi_session *session = conn->session;
1388
1389 /*
1390 * Check for iSCSI window and take care of CmdSN wrap-around
1391 */
Mike Christiee0726402007-07-26 12:46:48 -05001392 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001393 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1394 "%u MaxCmdSN %u CmdSN %u/%u\n",
1395 session->exp_cmdsn, session->max_cmdsn,
1396 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001397 return -ENOSPC;
1398 }
1399 return 0;
1400}
1401
Mike Christie9c19a7d2008-05-21 15:54:09 -05001402static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001403{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001404 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001405 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001406
Mike Christie70b31c12009-08-20 15:11:03 -05001407 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1408 return -ENODATA;
1409
Mike Christie9c19a7d2008-05-21 15:54:09 -05001410 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001411 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001412 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001413 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001414 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001415 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001416 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001417 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001418 }
1419 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001420 return rc;
1421}
1422
Mike Christie7996a772006-04-06 21:13:41 -05001423/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001424 * iscsi_requeue_task - requeue task to run from session workqueue
1425 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001426 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001427 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001428 * this. The session lock must be held. This should only be called
1429 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001430 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001431void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001432{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001433 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001434
Mike Christie3bbaaad2009-05-13 17:57:46 -05001435 /*
1436 * this may be on the requeue list already if the xmit_task callout
1437 * is handling the r2ts while we are adding new ones
1438 */
1439 if (list_empty(&task->running))
1440 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001441 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001442}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001443EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001444
1445/**
Mike Christie7996a772006-04-06 21:13:41 -05001446 * iscsi_data_xmit - xmit any command into the scheduled connection
1447 * @conn: iscsi connection
1448 *
1449 * Notes:
1450 * The function can return -EAGAIN in which case the caller must
1451 * re-schedule it again later or recover. '0' return code means
1452 * successful xmit.
1453 **/
1454static int iscsi_data_xmit(struct iscsi_conn *conn)
1455{
Mike Christie5d12c052009-11-11 16:34:32 -06001456 struct iscsi_task *task;
Mike Christie3219e522006-05-30 00:37:28 -05001457 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001458
Mike Christie77a23c22007-05-30 12:57:18 -05001459 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001460 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001461 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001462 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001463 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001464 }
Mike Christie7996a772006-04-06 21:13:41 -05001465
Mike Christie9c19a7d2008-05-21 15:54:09 -05001466 if (conn->task) {
1467 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001468 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001469 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001470 }
1471
Mike Christie77a23c22007-05-30 12:57:18 -05001472 /*
1473 * process mgmt pdus like nops before commands since we should
1474 * only have one nop-out as a ping from us and targets should not
1475 * overflow us with nop-ins
1476 */
1477check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001478 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001479 conn->task = list_entry(conn->mgmtqueue.next,
1480 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001481 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001482 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1483 __iscsi_put_task(conn->task);
1484 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001485 continue;
1486 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001487 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001488 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001489 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001490 }
1491
Mike Christie843c0a82007-12-13 12:43:20 -06001492 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001493 while (!list_empty(&conn->cmdqueue)) {
Mike Christie5d12c052009-11-11 16:34:32 -06001494 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1495 running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001496 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001497 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001498 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001499 continue;
1500 }
Mike Christie577577d2008-12-02 00:32:05 -06001501 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1502 if (rc) {
Mike Christie5d12c052009-11-11 16:34:32 -06001503 if (rc == -ENOMEM || rc == -EACCES) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001504 list_add_tail(&conn->task->running,
1505 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001506 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001507 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001508 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001509 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001510 continue;
1511 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001512 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001513 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001514 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001515 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001516 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001517 * we need to check the mgmt queue for nops that need to
1518 * be sent to aviod starvation
1519 */
Mike Christie843c0a82007-12-13 12:43:20 -06001520 if (!list_empty(&conn->mgmtqueue))
1521 goto check_mgmt;
1522 }
1523
1524 while (!list_empty(&conn->requeue)) {
Mike Christieb3a7ea82007-12-13 12:43:26 -06001525 /*
1526 * we always do fastlogout - conn stop code will clean up.
1527 */
1528 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1529 break;
1530
Mike Christie5d12c052009-11-11 16:34:32 -06001531 task = list_entry(conn->requeue.next, struct iscsi_task,
1532 running);
1533 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1534 break;
1535
1536 conn->task = task;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001537 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001538 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001539 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001540 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001541 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001542 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001543 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001544 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001545 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001546 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001547
Mike Christie70b31c12009-08-20 15:11:03 -05001548done:
Mike Christie77a23c22007-05-30 12:57:18 -05001549 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001550 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001551}
1552
David Howellsc4028952006-11-22 14:57:56 +00001553static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001554{
David Howellsc4028952006-11-22 14:57:56 +00001555 struct iscsi_conn *conn =
1556 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001557 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001558 /*
1559 * serialize Xmit worker on a per-connection basis.
1560 */
Mike Christie3219e522006-05-30 00:37:28 -05001561 do {
1562 rc = iscsi_data_xmit(conn);
1563 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001564}
1565
Mike Christie577577d2008-12-02 00:32:05 -06001566static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1567 struct scsi_cmnd *sc)
1568{
1569 struct iscsi_task *task;
1570
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001571 if (!kfifo_out(&conn->session->cmdpool.queue,
Mike Christie577577d2008-12-02 00:32:05 -06001572 (void *) &task, sizeof(void *)))
1573 return NULL;
1574
1575 sc->SCp.phase = conn->session->age;
1576 sc->SCp.ptr = (char *) task;
1577
1578 atomic_set(&task->refcount, 1);
1579 task->state = ISCSI_TASK_PENDING;
1580 task->conn = conn;
1581 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001582 task->have_checked_conn = false;
1583 task->last_timeout = jiffies;
1584 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001585 INIT_LIST_HEAD(&task->running);
1586 return task;
1587}
1588
Mike Christie7996a772006-04-06 21:13:41 -05001589enum {
1590 FAILURE_BAD_HOST = 1,
1591 FAILURE_SESSION_FAILED,
1592 FAILURE_SESSION_FREED,
1593 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001594 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001595 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001596 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001597 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001598 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001599 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001600};
1601
Jeff Garzikf2812332010-11-16 02:10:29 -05001602static int iscsi_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
Mike Christie7996a772006-04-06 21:13:41 -05001603{
Mike Christie75613522008-05-21 15:53:59 -05001604 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001605 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001606 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001607 int reason = 0;
1608 struct iscsi_session *session;
1609 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001610 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001611
1612 sc->scsi_done = done;
1613 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001614 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001615
1616 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001617 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001618 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001619
Mike Christie75613522008-05-21 15:53:59 -05001620 cls_session = starget_to_session(scsi_target(sc->device));
1621 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001622 spin_lock(&session->lock);
1623
Mike Christie75613522008-05-21 15:53:59 -05001624 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001625 if (reason) {
1626 sc->result = reason;
1627 goto fault;
1628 }
1629
Mike Christie301e0f72009-05-13 17:57:47 -05001630 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001631 /*
1632 * to handle the race between when we set the recovery state
1633 * and block the session we requeue here (commands could
1634 * be entering our queuecommand while a block is starting
1635 * up because the block code is not locked)
1636 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001637 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001638 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001639 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001640 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001641 sc->result = DID_IMM_RETRY << 16;
1642 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001643 case ISCSI_STATE_LOGGING_OUT:
1644 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001645 sc->result = DID_IMM_RETRY << 16;
1646 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001647 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001648 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001649 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001650 break;
1651 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001652 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001653 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001654 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001655 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001656 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001657 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001658 }
Mike Christie7996a772006-04-06 21:13:41 -05001659 goto fault;
1660 }
1661
Mike Christie7996a772006-04-06 21:13:41 -05001662 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001663 if (!conn) {
1664 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001665 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001666 goto fault;
1667 }
Mike Christie7996a772006-04-06 21:13:41 -05001668
Mike Christie661134a2009-09-05 07:35:33 +05301669 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1670 reason = FAILURE_SESSION_IN_RECOVERY;
1671 sc->result = DID_REQUEUE;
1672 goto fault;
1673 }
1674
Mike Christie77a23c22007-05-30 12:57:18 -05001675 if (iscsi_check_cmdsn_window_closed(conn)) {
1676 reason = FAILURE_WINDOW_CLOSED;
1677 goto reject;
1678 }
1679
Mike Christie577577d2008-12-02 00:32:05 -06001680 task = iscsi_alloc_task(conn, sc);
1681 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001682 reason = FAILURE_OOM;
1683 goto reject;
1684 }
Mike Christie7996a772006-04-06 21:13:41 -05001685
Mike Christie1336aed2009-05-13 17:57:48 -05001686 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001687 reason = iscsi_prep_scsi_cmd_pdu(task);
1688 if (reason) {
Mike Christie5d12c052009-11-11 16:34:32 -06001689 if (reason == -ENOMEM || reason == -EACCES) {
Mike Christie577577d2008-12-02 00:32:05 -06001690 reason = FAILURE_OOM;
1691 goto prepd_reject;
1692 } else {
1693 sc->result = DID_ABORT << 16;
1694 goto prepd_fault;
1695 }
Mike Christie052d0142008-05-21 15:54:05 -05001696 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001697 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001698 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001699 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001700 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001701 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001702 } else {
1703 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001704 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001705 }
Mike Christie052d0142008-05-21 15:54:05 -05001706
1707 session->queued_cmdsn++;
1708 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001709 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001710 return 0;
1711
Mike Christie577577d2008-12-02 00:32:05 -06001712prepd_reject:
1713 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001714 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001715reject:
1716 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001717 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1718 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001719 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001720 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001721
Mike Christie577577d2008-12-02 00:32:05 -06001722prepd_fault:
1723 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001724 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001725fault:
1726 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001727 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1728 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001729 if (!scsi_bidi_cmnd(sc))
1730 scsi_set_resid(sc, scsi_bufflen(sc));
1731 else {
1732 scsi_out(sc)->resid = scsi_out(sc)->length;
1733 scsi_in(sc)->resid = scsi_in(sc)->length;
1734 }
Mike Christie052d0142008-05-21 15:54:05 -05001735 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001736 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001737 return 0;
1738}
Jeff Garzikf2812332010-11-16 02:10:29 -05001739
1740DEF_SCSI_QCMD(iscsi_queuecommand)
Mike Christie7996a772006-04-06 21:13:41 -05001741EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1742
Mike Christiee881a172009-10-15 17:46:39 -07001743int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
Mike Christie7996a772006-04-06 21:13:41 -05001744{
Mike Christie1796e722009-11-11 16:34:36 -06001745 switch (reason) {
1746 case SCSI_QDEPTH_DEFAULT:
1747 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1748 break;
1749 case SCSI_QDEPTH_QFULL:
1750 scsi_track_queue_full(sdev, depth);
1751 break;
1752 case SCSI_QDEPTH_RAMP_UP:
1753 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1754 break;
1755 default:
Mike Christiee881a172009-10-15 17:46:39 -07001756 return -EOPNOTSUPP;
Mike Christie1796e722009-11-11 16:34:36 -06001757 }
Mike Christie7996a772006-04-06 21:13:41 -05001758 return sdev->queue_depth;
1759}
1760EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1761
Mike Christie6b5d6c42009-04-21 15:32:32 -05001762int iscsi_target_alloc(struct scsi_target *starget)
1763{
1764 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1765 struct iscsi_session *session = cls_session->dd_data;
1766
1767 starget->can_queue = session->scsi_cmds_max;
1768 return 0;
1769}
1770EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1771
Mike Christie843c0a82007-12-13 12:43:20 -06001772static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001773{
Mike Christie843c0a82007-12-13 12:43:20 -06001774 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001775 struct iscsi_session *session = conn->session;
1776
1777 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001778 if (conn->tmf_state == TMF_QUEUED) {
1779 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001780 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001781 /* unblock eh_abort() */
1782 wake_up(&conn->ehwait);
1783 }
1784 spin_unlock(&session->lock);
1785}
1786
Mike Christie9c19a7d2008-05-21 15:54:09 -05001787static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001788 struct iscsi_tm *hdr, int age,
1789 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001790{
Mike Christie7996a772006-04-06 21:13:41 -05001791 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001792 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001793
Mike Christie9c19a7d2008-05-21 15:54:09 -05001794 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001795 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001796 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001797 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001798 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001799 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001800 ISCSI_DBG_EH(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001801 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001802 }
Mike Christie843c0a82007-12-13 12:43:20 -06001803 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001804 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001805 conn->tmf_timer.function = iscsi_tmf_timedout;
1806 conn->tmf_timer.data = (unsigned long)conn;
1807 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001808 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001809
Mike Christie7996a772006-04-06 21:13:41 -05001810 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001811 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001812
1813 /*
1814 * block eh thread until:
1815 *
Mike Christie843c0a82007-12-13 12:43:20 -06001816 * 1) tmf response
1817 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001818 * 3) session is terminated or restarted or userspace has
1819 * given up on recovery
1820 */
Mike Christie843c0a82007-12-13 12:43:20 -06001821 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001822 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001823 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001824 if (signal_pending(current))
1825 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001826 del_timer_sync(&conn->tmf_timer);
1827
Mike Christie6724add2007-08-15 01:38:30 -05001828 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001829 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001830 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001831 if (age != session->age ||
1832 session->state != ISCSI_STATE_LOGGED_IN)
1833 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001834 return 0;
1835}
1836
1837/*
Mike Christie843c0a82007-12-13 12:43:20 -06001838 * Fail commands. session lock held and recv side suspended and xmit
1839 * thread flushed
1840 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001841static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1842 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001843{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001844 struct iscsi_task *task;
1845 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001846
Mike Christie3bbaaad2009-05-13 17:57:46 -05001847 for (i = 0; i < conn->session->cmds_max; i++) {
1848 task = conn->session->cmds[i];
1849 if (!task->sc || task->state == ISCSI_TASK_FREE)
1850 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001851
Mike Christie3bbaaad2009-05-13 17:57:46 -05001852 if (lun != -1 && lun != task->sc->device->lun)
1853 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001854
Mike Christie3bbaaad2009-05-13 17:57:46 -05001855 ISCSI_DBG_SESSION(conn->session,
1856 "failing sc %p itt 0x%x state %d\n",
1857 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001858 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001859 }
1860}
1861
Mike Christie661134a2009-09-05 07:35:33 +05301862/**
1863 * iscsi_suspend_queue - suspend iscsi_queuecommand
1864 * @conn: iscsi conn to stop queueing IO on
1865 *
1866 * This grabs the session lock to make sure no one is in
1867 * xmit_task/queuecommand, and then sets suspend to prevent
1868 * new commands from being queued. This only needs to be called
1869 * by offload drivers that need to sync a path like ep disconnect
1870 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1871 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1872 */
1873void iscsi_suspend_queue(struct iscsi_conn *conn)
1874{
1875 spin_lock_bh(&conn->session->lock);
1876 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1877 spin_unlock_bh(&conn->session->lock);
1878}
1879EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1880
1881/**
1882 * iscsi_suspend_tx - suspend iscsi_data_xmit
1883 * @conn: iscsi conn tp stop processing IO on.
1884 *
1885 * This function sets the suspend bit to prevent iscsi_data_xmit
1886 * from sending new IO, and if work is queued on the xmit thread
1887 * it will wait for it to be completed.
1888 */
Mike Christieb40977d2008-05-21 15:54:03 -05001889void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001890{
Mike Christie32ae7632009-03-05 14:46:03 -06001891 struct Scsi_Host *shost = conn->session->host;
1892 struct iscsi_host *ihost = shost_priv(shost);
1893
Mike Christie6724add2007-08-15 01:38:30 -05001894 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001895 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001896 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001897}
Mike Christieb40977d2008-05-21 15:54:03 -05001898EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001899
1900static void iscsi_start_tx(struct iscsi_conn *conn)
1901{
1902 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001903 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001904}
1905
Mike Christie4c48a822009-05-13 17:57:45 -05001906/*
1907 * We want to make sure a ping is in flight. It has timed out.
1908 * And we are not busy processing a pdu that is making
1909 * progress but got started before the ping and is taking a while
1910 * to complete so the ping is just stuck behind it in a queue.
1911 */
1912static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1913{
1914 if (conn->ping_task &&
1915 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1916 (conn->ping_timeout * HZ), jiffies))
1917 return 1;
1918 else
1919 return 0;
1920}
1921
Mike Christied355e572009-06-15 22:11:08 -05001922static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001923{
Mike Christied355e572009-06-15 22:11:08 -05001924 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christie92ed4d62010-02-10 16:51:45 -06001925 struct iscsi_task *task = NULL, *running_task;
Mike Christief6d51802007-12-13 12:43:30 -06001926 struct iscsi_cls_session *cls_session;
1927 struct iscsi_session *session;
1928 struct iscsi_conn *conn;
Mike Christie92ed4d62010-02-10 16:51:45 -06001929 int i;
Mike Christief6d51802007-12-13 12:43:30 -06001930
Mike Christied355e572009-06-15 22:11:08 -05001931 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001932 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001933
Erez Zilberbd2199d2009-06-15 22:11:10 -05001934 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001935
1936 spin_lock(&session->lock);
1937 if (session->state != ISCSI_STATE_LOGGED_IN) {
1938 /*
1939 * We are probably in the middle of iscsi recovery so let
1940 * that complete and handle the error.
1941 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001942 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001943 goto done;
1944 }
1945
1946 conn = session->leadconn;
1947 if (!conn) {
1948 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001949 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001950 goto done;
1951 }
1952
Mike Christied355e572009-06-15 22:11:08 -05001953 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie92ed4d62010-02-10 16:51:45 -06001954 if (!task) {
1955 /*
1956 * Raced with completion. Just reset timer, and let it
1957 * complete normally
1958 */
1959 rc = BLK_EH_RESET_TIMER;
Mike Christied355e572009-06-15 22:11:08 -05001960 goto done;
Mike Christie92ed4d62010-02-10 16:51:45 -06001961 }
1962
Mike Christied355e572009-06-15 22:11:08 -05001963 /*
1964 * If we have sent (at least queued to the network layer) a pdu or
1965 * recvd one for the task since the last timeout ask for
1966 * more time. If on the next timeout we have not made progress
1967 * we can check if it is the task or connection when we send the
1968 * nop as a ping.
1969 */
Mike Christie92ed4d62010-02-10 16:51:45 -06001970 if (time_after(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001971 ISCSI_DBG_EH(session, "Command making progress. Asking "
1972 "scsi-ml for more time to complete. "
Mike Christie92ed4d62010-02-10 16:51:45 -06001973 "Last data xfer at %lu. Last timeout was at "
Erez Zilberbd2199d2009-06-15 22:11:10 -05001974 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001975 task->have_checked_conn = false;
1976 rc = BLK_EH_RESET_TIMER;
1977 goto done;
1978 }
1979
Mike Christief6d51802007-12-13 12:43:30 -06001980 if (!conn->recv_timeout && !conn->ping_timeout)
1981 goto done;
1982 /*
1983 * if the ping timedout then we are in the middle of cleaning up
1984 * and can let the iscsi eh handle it
1985 */
Mike Christie4c48a822009-05-13 17:57:45 -05001986 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001987 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001988 goto done;
1989 }
Mike Christied355e572009-06-15 22:11:08 -05001990
Mike Christie92ed4d62010-02-10 16:51:45 -06001991 for (i = 0; i < conn->session->cmds_max; i++) {
1992 running_task = conn->session->cmds[i];
1993 if (!running_task->sc || running_task == task ||
1994 running_task->state != ISCSI_TASK_RUNNING)
1995 continue;
1996
1997 /*
1998 * Only check if cmds started before this one have made
1999 * progress, or this could never fail
2000 */
2001 if (time_after(running_task->sc->jiffies_at_alloc,
2002 task->sc->jiffies_at_alloc))
2003 continue;
2004
2005 if (time_after(running_task->last_xfer, task->last_timeout)) {
2006 /*
2007 * This task has not made progress, but a task
2008 * started before us has transferred data since
2009 * we started/last-checked. We could be queueing
2010 * too many tasks or the LU is bad.
2011 *
2012 * If the device is bad the cmds ahead of us on
2013 * other devs will complete, and this loop will
2014 * eventually fail starting the scsi eh.
2015 */
2016 ISCSI_DBG_EH(session, "Command has not made progress "
2017 "but commands ahead of it have. "
2018 "Asking scsi-ml for more time to "
2019 "complete. Our last xfer vs running task "
2020 "last xfer %lu/%lu. Last check %lu.\n",
2021 task->last_xfer, running_task->last_xfer,
2022 task->last_timeout);
2023 rc = BLK_EH_RESET_TIMER;
2024 goto done;
2025 }
2026 }
2027
Mike Christied355e572009-06-15 22:11:08 -05002028 /* Assumes nop timeout is shorter than scsi cmd timeout */
2029 if (task->have_checked_conn)
2030 goto done;
2031
Mike Christief6d51802007-12-13 12:43:30 -06002032 /*
Mike Christied355e572009-06-15 22:11:08 -05002033 * Checking the transport already or nop from a cmd timeout still
2034 * running
Mike Christief6d51802007-12-13 12:43:30 -06002035 */
Mike Christied355e572009-06-15 22:11:08 -05002036 if (conn->ping_task) {
2037 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07002038 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05002039 goto done;
2040 }
2041
Mike Christied355e572009-06-15 22:11:08 -05002042 /* Make sure there is a transport check done */
2043 iscsi_send_nopout(conn, NULL);
2044 task->have_checked_conn = true;
2045 rc = BLK_EH_RESET_TIMER;
2046
Mike Christief6d51802007-12-13 12:43:30 -06002047done:
Mike Christied355e572009-06-15 22:11:08 -05002048 if (task)
2049 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06002050 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002051 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2052 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06002053 return rc;
2054}
2055
2056static void iscsi_check_transport_timeouts(unsigned long data)
2057{
2058 struct iscsi_conn *conn = (struct iscsi_conn *)data;
2059 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05002060 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06002061
2062 spin_lock(&session->lock);
2063 if (session->state != ISCSI_STATE_LOGGED_IN)
2064 goto done;
2065
Mike Christie4cf10432008-05-07 20:43:52 -05002066 recv_timeout = conn->recv_timeout;
2067 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06002068 goto done;
2069
Mike Christie4cf10432008-05-07 20:43:52 -05002070 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06002071 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05002072
2073 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06002074 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05002075 "expired, recv timeout %d, last rx %lu, "
2076 "last ping %lu, now %lu\n",
2077 conn->ping_timeout, conn->recv_timeout,
2078 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06002079 spin_unlock(&session->lock);
2080 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2081 return;
2082 }
2083
Mike Christie4cf10432008-05-07 20:43:52 -05002084 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05002085 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06002086 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05002087 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05002088 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06002089 } else
Mike Christie4cf10432008-05-07 20:43:52 -05002090 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06002091
Mike Christie1b2c7af2009-03-05 14:45:58 -06002092 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06002093 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06002094done:
2095 spin_unlock(&session->lock);
2096}
2097
Mike Christie9c19a7d2008-05-21 15:54:09 -05002098static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06002099 struct iscsi_tm *hdr)
2100{
2101 memset(hdr, 0, sizeof(*hdr));
2102 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2103 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2104 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06002105 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2106 hdr->rtt = task->hdr_itt;
2107 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06002108}
2109
Mike Christie7996a772006-04-06 21:13:41 -05002110int iscsi_eh_abort(struct scsi_cmnd *sc)
2111{
Mike Christie75613522008-05-21 15:53:59 -05002112 struct iscsi_cls_session *cls_session;
2113 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04002114 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002115 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06002116 struct iscsi_tm *hdr;
2117 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05002118
Mike Christie75613522008-05-21 15:53:59 -05002119 cls_session = starget_to_session(scsi_target(sc->device));
2120 session = cls_session->dd_data;
2121
Erez Zilberbd2199d2009-06-15 22:11:10 -05002122 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05002123
Mike Christie6724add2007-08-15 01:38:30 -05002124 mutex_lock(&session->eh_mutex);
2125 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04002126 /*
2127 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2128 * got the command.
2129 */
2130 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002131 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2132 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05002133 spin_unlock_bh(&session->lock);
2134 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04002135 return SUCCESS;
2136 }
2137
Mike Christie7996a772006-04-06 21:13:41 -05002138 /*
2139 * If we are not logged in or we have started a new session
2140 * then let the host reset code handle this
2141 */
Mike Christie843c0a82007-12-13 12:43:20 -06002142 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2143 sc->SCp.phase != session->age) {
2144 spin_unlock_bh(&session->lock);
2145 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002146 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05002147 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06002148 return FAILED;
2149 }
2150
2151 conn = session->leadconn;
2152 conn->eh_abort_cnt++;
2153 age = session->age;
2154
Mike Christie9c19a7d2008-05-21 15:54:09 -05002155 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002156 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2157 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002158
Mike Christie9c19a7d2008-05-21 15:54:09 -05002159 /* task completed before time out */
2160 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002161 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002162 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002163 }
Mike Christie7996a772006-04-06 21:13:41 -05002164
Mike Christie9c19a7d2008-05-21 15:54:09 -05002165 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002166 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002167 goto success;
2168 }
Mike Christie7996a772006-04-06 21:13:41 -05002169
Mike Christie843c0a82007-12-13 12:43:20 -06002170 /* only have one tmf outstanding at a time */
2171 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002172 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002173 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002174
Mike Christie843c0a82007-12-13 12:43:20 -06002175 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002176 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002177
Mike Christie9c19a7d2008-05-21 15:54:09 -05002178 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002179 rc = FAILED;
2180 goto failed;
2181 }
2182
2183 switch (conn->tmf_state) {
2184 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002185 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002186 /*
2187 * stop tx side incase the target had sent a abort rsp but
2188 * the initiator was still writing out data.
2189 */
Mike Christie6724add2007-08-15 01:38:30 -05002190 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002191 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002192 * we do not stop the recv side because targets have been
2193 * good and have never sent us a successful tmf response
2194 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002195 */
Mike Christie6187c242009-07-15 15:02:57 -05002196 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002197 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002198 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002199 memset(hdr, 0, sizeof(*hdr));
Mike Christie6187c242009-07-15 15:02:57 -05002200 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002201 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002202 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002203 case TMF_TIMEDOUT:
2204 spin_unlock_bh(&session->lock);
2205 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2206 goto failed_unlocked;
2207 case TMF_NOT_FOUND:
2208 if (!sc->SCp.ptr) {
2209 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002210 memset(hdr, 0, sizeof(*hdr));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002211 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002212 ISCSI_DBG_EH(session, "sc completed while abort in "
2213 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002214 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002215 }
2216 /* fall through */
2217 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002218 conn->tmf_state = TMF_INITIAL;
2219 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002220 }
2221
Mike Christie77a23c22007-05-30 12:57:18 -05002222success:
Mike Christie7996a772006-04-06 21:13:41 -05002223 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002224success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002225 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2226 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002227 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002228 return SUCCESS;
2229
2230failed:
2231 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002232failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002233 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2234 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002235 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002236 return FAILED;
2237}
2238EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2239
Mike Christie843c0a82007-12-13 12:43:20 -06002240static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2241{
2242 memset(hdr, 0, sizeof(*hdr));
2243 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2244 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2245 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2246 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002247 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002248}
2249
2250int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2251{
Mike Christie75613522008-05-21 15:53:59 -05002252 struct iscsi_cls_session *cls_session;
2253 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002254 struct iscsi_conn *conn;
2255 struct iscsi_tm *hdr;
2256 int rc = FAILED;
2257
Mike Christie75613522008-05-21 15:53:59 -05002258 cls_session = starget_to_session(scsi_target(sc->device));
2259 session = cls_session->dd_data;
2260
Erez Zilberbd2199d2009-06-15 22:11:10 -05002261 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002262
2263 mutex_lock(&session->eh_mutex);
2264 spin_lock_bh(&session->lock);
2265 /*
2266 * Just check if we are not logged in. We cannot check for
2267 * the phase because the reset could come from a ioctl.
2268 */
2269 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2270 goto unlock;
2271 conn = session->leadconn;
2272
2273 /* only have one tmf outstanding at a time */
2274 if (conn->tmf_state != TMF_INITIAL)
2275 goto unlock;
2276 conn->tmf_state = TMF_QUEUED;
2277
2278 hdr = &conn->tmhdr;
2279 iscsi_prep_lun_reset_pdu(sc, hdr);
2280
Mike Christie9c19a7d2008-05-21 15:54:09 -05002281 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002282 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002283 rc = FAILED;
2284 goto unlock;
2285 }
2286
2287 switch (conn->tmf_state) {
2288 case TMF_SUCCESS:
2289 break;
2290 case TMF_TIMEDOUT:
2291 spin_unlock_bh(&session->lock);
2292 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2293 goto done;
2294 default:
2295 conn->tmf_state = TMF_INITIAL;
2296 goto unlock;
2297 }
2298
2299 rc = SUCCESS;
2300 spin_unlock_bh(&session->lock);
2301
2302 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002303
Mike Christiea3439142008-09-24 11:46:15 -05002304 spin_lock_bh(&session->lock);
Mike Christie5d12c052009-11-11 16:34:32 -06002305 memset(hdr, 0, sizeof(*hdr));
Mike Christie3bbaaad2009-05-13 17:57:46 -05002306 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002307 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002308 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002309
2310 iscsi_start_tx(conn);
2311 goto done;
2312
2313unlock:
2314 spin_unlock_bh(&session->lock);
2315done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002316 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2317 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002318 mutex_unlock(&session->eh_mutex);
2319 return rc;
2320}
2321EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2322
Mike Christie3fe5ae82009-11-11 16:34:33 -06002323void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2324{
2325 struct iscsi_session *session = cls_session->dd_data;
2326
2327 spin_lock_bh(&session->lock);
2328 if (session->state != ISCSI_STATE_LOGGED_IN) {
2329 session->state = ISCSI_STATE_RECOVERY_FAILED;
2330 if (session->leadconn)
2331 wake_up(&session->leadconn->ehwait);
2332 }
2333 spin_unlock_bh(&session->lock);
2334}
2335EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2336
2337/**
2338 * iscsi_eh_session_reset - drop session and attempt relogin
2339 * @sc: scsi command
2340 *
2341 * This function will wait for a relogin, session termination from
2342 * userspace, or a recovery/replacement timeout.
2343 */
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302344int iscsi_eh_session_reset(struct scsi_cmnd *sc)
Mike Christie3fe5ae82009-11-11 16:34:33 -06002345{
2346 struct iscsi_cls_session *cls_session;
2347 struct iscsi_session *session;
2348 struct iscsi_conn *conn;
2349
2350 cls_session = starget_to_session(scsi_target(sc->device));
2351 session = cls_session->dd_data;
2352 conn = session->leadconn;
2353
2354 mutex_lock(&session->eh_mutex);
2355 spin_lock_bh(&session->lock);
2356 if (session->state == ISCSI_STATE_TERMINATE) {
2357failed:
2358 ISCSI_DBG_EH(session,
2359 "failing session reset: Could not log back into "
2360 "%s, %s [age %d]\n", session->targetname,
2361 conn->persistent_address, session->age);
2362 spin_unlock_bh(&session->lock);
2363 mutex_unlock(&session->eh_mutex);
2364 return FAILED;
2365 }
2366
2367 spin_unlock_bh(&session->lock);
2368 mutex_unlock(&session->eh_mutex);
2369 /*
2370 * we drop the lock here but the leadconn cannot be destoyed while
2371 * we are in the scsi eh
2372 */
2373 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2374
2375 ISCSI_DBG_EH(session, "wait for relogin\n");
2376 wait_event_interruptible(conn->ehwait,
2377 session->state == ISCSI_STATE_TERMINATE ||
2378 session->state == ISCSI_STATE_LOGGED_IN ||
2379 session->state == ISCSI_STATE_RECOVERY_FAILED);
2380 if (signal_pending(current))
2381 flush_signals(current);
2382
2383 mutex_lock(&session->eh_mutex);
2384 spin_lock_bh(&session->lock);
2385 if (session->state == ISCSI_STATE_LOGGED_IN) {
2386 ISCSI_DBG_EH(session,
2387 "session reset succeeded for %s,%s\n",
2388 session->targetname, conn->persistent_address);
2389 } else
2390 goto failed;
2391 spin_unlock_bh(&session->lock);
2392 mutex_unlock(&session->eh_mutex);
2393 return SUCCESS;
2394}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302395EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002396
2397static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2398{
2399 memset(hdr, 0, sizeof(*hdr));
2400 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2401 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2402 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2403 hdr->rtt = RESERVED_ITT;
2404}
2405
2406/**
2407 * iscsi_eh_target_reset - reset target
2408 * @sc: scsi command
2409 *
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302410 * This will attempt to send a warm target reset.
Mike Christie3fe5ae82009-11-11 16:34:33 -06002411 */
2412int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2413{
2414 struct iscsi_cls_session *cls_session;
2415 struct iscsi_session *session;
2416 struct iscsi_conn *conn;
2417 struct iscsi_tm *hdr;
2418 int rc = FAILED;
2419
2420 cls_session = starget_to_session(scsi_target(sc->device));
2421 session = cls_session->dd_data;
2422
2423 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2424 session->targetname);
2425
2426 mutex_lock(&session->eh_mutex);
2427 spin_lock_bh(&session->lock);
2428 /*
2429 * Just check if we are not logged in. We cannot check for
2430 * the phase because the reset could come from a ioctl.
2431 */
2432 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2433 goto unlock;
2434 conn = session->leadconn;
2435
2436 /* only have one tmf outstanding at a time */
2437 if (conn->tmf_state != TMF_INITIAL)
2438 goto unlock;
2439 conn->tmf_state = TMF_QUEUED;
2440
2441 hdr = &conn->tmhdr;
2442 iscsi_prep_tgt_reset_pdu(sc, hdr);
2443
2444 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2445 session->tgt_reset_timeout)) {
2446 rc = FAILED;
2447 goto unlock;
2448 }
2449
2450 switch (conn->tmf_state) {
2451 case TMF_SUCCESS:
2452 break;
2453 case TMF_TIMEDOUT:
2454 spin_unlock_bh(&session->lock);
2455 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2456 goto done;
2457 default:
2458 conn->tmf_state = TMF_INITIAL;
2459 goto unlock;
2460 }
2461
2462 rc = SUCCESS;
2463 spin_unlock_bh(&session->lock);
2464
2465 iscsi_suspend_tx(conn);
2466
2467 spin_lock_bh(&session->lock);
2468 memset(hdr, 0, sizeof(*hdr));
2469 fail_scsi_tasks(conn, -1, DID_ERROR);
2470 conn->tmf_state = TMF_INITIAL;
2471 spin_unlock_bh(&session->lock);
2472
2473 iscsi_start_tx(conn);
2474 goto done;
2475
2476unlock:
2477 spin_unlock_bh(&session->lock);
2478done:
2479 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2480 rc == SUCCESS ? "SUCCESS" : "FAILED");
2481 mutex_unlock(&session->eh_mutex);
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302482 return rc;
2483}
2484EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002485
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302486/**
2487 * iscsi_eh_recover_target - reset target and possibly the session
2488 * @sc: scsi command
2489 *
2490 * This will attempt to send a warm target reset. If that fails,
2491 * we will escalate to ERL0 session recovery.
2492 */
2493int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2494{
2495 int rc;
2496
2497 rc = iscsi_eh_target_reset(sc);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002498 if (rc == FAILED)
2499 rc = iscsi_eh_session_reset(sc);
2500 return rc;
2501}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302502EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002503
Olaf Kirch63203772007-12-13 12:43:25 -06002504/*
2505 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2506 * should be accessed via kfifo_{get,put} on q->queue.
2507 * Optionally, the caller can obtain the array of object pointers
2508 * by passing in a non-NULL @items pointer
2509 */
Mike Christie7996a772006-04-06 21:13:41 -05002510int
Olaf Kirch63203772007-12-13 12:43:25 -06002511iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002512{
Olaf Kirch63203772007-12-13 12:43:25 -06002513 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002514
Olaf Kirch63203772007-12-13 12:43:25 -06002515 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002516
2517 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002518
2519 /* If the user passed an items pointer, he wants a copy of
2520 * the array. */
2521 if (items)
2522 num_arrays++;
2523 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2524 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002525 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002526
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002527 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002528
2529 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002530 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002531 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002532 q->max = i;
2533 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002534 }
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002535 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002536 }
Olaf Kirch63203772007-12-13 12:43:25 -06002537
2538 if (items) {
2539 *items = q->pool + max;
2540 memcpy(*items, q->pool, max * sizeof(void *));
2541 }
2542
Mike Christie7996a772006-04-06 21:13:41 -05002543 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002544
2545enomem:
2546 iscsi_pool_free(q);
2547 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002548}
2549EXPORT_SYMBOL_GPL(iscsi_pool_init);
2550
Olaf Kirch63203772007-12-13 12:43:25 -06002551void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002552{
2553 int i;
2554
2555 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002556 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002557 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05002558}
2559EXPORT_SYMBOL_GPL(iscsi_pool_free);
2560
Mike Christiea4804cd2008-05-21 15:54:00 -05002561/**
2562 * iscsi_host_add - add host to system
2563 * @shost: scsi host
2564 * @pdev: parent device
2565 *
2566 * This should be called by partial offload and software iscsi drivers
2567 * to add a host to the system.
2568 */
2569int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002570{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002571 if (!shost->can_queue)
2572 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2573
Mike Christie4d108352009-03-05 14:46:04 -06002574 if (!shost->cmd_per_lun)
2575 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2576
Mike Christie308cec12009-02-06 12:06:20 -06002577 if (!shost->transportt->eh_timed_out)
2578 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002579 return scsi_add_host(shost, pdev);
2580}
2581EXPORT_SYMBOL_GPL(iscsi_host_add);
2582
2583/**
2584 * iscsi_host_alloc - allocate a host and driver data
2585 * @sht: scsi host template
2586 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002587 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002588 *
2589 * This should be called by partial offload and software iscsi drivers.
2590 * To access the driver specific memory use the iscsi_host_priv() macro.
2591 */
2592struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002593 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002594{
2595 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002596 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002597
2598 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2599 if (!shost)
2600 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002601 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002602
2603 if (xmit_can_sleep) {
2604 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2605 "iscsi_q_%d", shost->host_no);
2606 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2607 if (!ihost->workq)
2608 goto free_host;
2609 }
2610
Mike Christiee5bd7b52008-09-24 11:46:10 -05002611 spin_lock_init(&ihost->lock);
2612 ihost->state = ISCSI_HOST_SETUP;
2613 ihost->num_sessions = 0;
2614 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002615 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002616
2617free_host:
2618 scsi_host_put(shost);
2619 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002620}
Mike Christiea4804cd2008-05-21 15:54:00 -05002621EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002622
Mike Christiee5bd7b52008-09-24 11:46:10 -05002623static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2624{
Mike Christie40a06e72009-03-05 14:46:05 -06002625 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002626}
2627
Mike Christiea4804cd2008-05-21 15:54:00 -05002628/**
2629 * iscsi_host_remove - remove host and sessions
2630 * @shost: scsi host
2631 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002632 * If there are any sessions left, this will initiate the removal and wait
2633 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002634 */
2635void iscsi_host_remove(struct Scsi_Host *shost)
2636{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002637 struct iscsi_host *ihost = shost_priv(shost);
2638 unsigned long flags;
2639
2640 spin_lock_irqsave(&ihost->lock, flags);
2641 ihost->state = ISCSI_HOST_REMOVED;
2642 spin_unlock_irqrestore(&ihost->lock, flags);
2643
2644 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2645 wait_event_interruptible(ihost->session_removal_wq,
2646 ihost->num_sessions == 0);
2647 if (signal_pending(current))
2648 flush_signals(current);
2649
Mike Christiea4804cd2008-05-21 15:54:00 -05002650 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002651 if (ihost->workq)
2652 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002653}
2654EXPORT_SYMBOL_GPL(iscsi_host_remove);
2655
2656void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002657{
2658 struct iscsi_host *ihost = shost_priv(shost);
2659
2660 kfree(ihost->netdev);
2661 kfree(ihost->hwaddress);
2662 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002663 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002664}
Mike Christiea4804cd2008-05-21 15:54:00 -05002665EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002666
Mike Christiee5bd7b52008-09-24 11:46:10 -05002667static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2668{
2669 struct iscsi_host *ihost = shost_priv(shost);
2670 unsigned long flags;
2671
2672 shost = scsi_host_get(shost);
2673 if (!shost) {
2674 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2675 "of session teardown event because host already "
2676 "removed.\n");
2677 return;
2678 }
2679
2680 spin_lock_irqsave(&ihost->lock, flags);
2681 ihost->num_sessions--;
2682 if (ihost->num_sessions == 0)
2683 wake_up(&ihost->session_removal_wq);
2684 spin_unlock_irqrestore(&ihost->lock, flags);
2685 scsi_host_put(shost);
2686}
2687
Mike Christie75613522008-05-21 15:53:59 -05002688/**
2689 * iscsi_session_setup - create iscsi cls session and host and session
2690 * @iscsit: iscsi transport template
2691 * @shost: scsi host
2692 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002693 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002694 * @initial_cmdsn: initial CmdSN
2695 *
2696 * This can be used by software iscsi_transports that allocate
2697 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002698 *
2699 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2700 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2701 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002702 */
2703struct iscsi_cls_session *
2704iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302705 uint16_t cmds_max, int dd_size, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002706 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002707{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002708 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002709 struct iscsi_session *session;
2710 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002711 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002712 unsigned long flags;
2713
2714 spin_lock_irqsave(&ihost->lock, flags);
2715 if (ihost->state == ISCSI_HOST_REMOVED) {
2716 spin_unlock_irqrestore(&ihost->lock, flags);
2717 return NULL;
2718 }
2719 ihost->num_sessions++;
2720 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002721
2722 if (!total_cmds)
2723 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002724 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002725 * The iscsi layer needs some tasks for nop handling and tmfs,
2726 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2727 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002728 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002729 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2730 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2731 "must be a power of two that is at least %d.\n",
2732 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002733 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002734 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002735
2736 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2737 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2738 "must be a power of 2 less than or equal to %d.\n",
2739 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2740 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2741 }
2742
2743 if (!is_power_of_2(total_cmds)) {
2744 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2745 "must be a power of 2.\n", total_cmds);
2746 total_cmds = rounddown_pow_of_two(total_cmds);
2747 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2748 return NULL;
2749 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2750 total_cmds);
2751 }
2752 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002753
Mike Christie5d91e202008-05-21 15:54:01 -05002754 cls_session = iscsi_alloc_session(shost, iscsit,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302755 sizeof(struct iscsi_session) +
2756 dd_size);
Mike Christie75613522008-05-21 15:53:59 -05002757 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002758 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002759 session = cls_session->dd_data;
2760 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002761 session->host = shost;
2762 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002763 session->fast_abort = 1;
Mike Christie3fe5ae82009-11-11 16:34:33 -06002764 session->tgt_reset_timeout = 30;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002765 session->lu_reset_timeout = 15;
2766 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002767 session->scsi_cmds_max = scsi_cmds;
2768 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002769 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002770 session->exp_cmdsn = initial_cmdsn + 1;
2771 session->max_cmdsn = initial_cmdsn + 1;
2772 session->max_r2t = 1;
2773 session->tt = iscsit;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302774 session->dd_data = cls_session->dd_data + sizeof(*session);
Mike Christie6724add2007-08-15 01:38:30 -05002775 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002776 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002777
2778 /* initialize SCSI PDU commands pool */
2779 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2780 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002781 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002782 goto cmdpool_alloc_fail;
2783
2784 /* pre-format cmds pool with ITT */
2785 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002786 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002787
Mike Christie9c19a7d2008-05-21 15:54:09 -05002788 if (cmd_task_size)
2789 task->dd_data = &task[1];
2790 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002791 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002792 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002793 }
2794
Mike Christief53a88d2006-06-28 12:00:27 -05002795 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002796 goto module_get_fail;
2797
Mike Christie79706342008-05-21 15:54:12 -05002798 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002799 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002800
Mike Christie7996a772006-04-06 21:13:41 -05002801 return cls_session;
2802
2803cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002804 module_put(iscsit->owner);
2805module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002806 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002807cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002808 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002809dec_session_count:
2810 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002811 return NULL;
2812}
2813EXPORT_SYMBOL_GPL(iscsi_session_setup);
2814
2815/**
2816 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002817 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002818 *
Mike Christie75613522008-05-21 15:53:59 -05002819 * The driver must have called iscsi_remove_session before
2820 * calling this.
2821 */
Mike Christie7996a772006-04-06 21:13:41 -05002822void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2823{
Mike Christie75613522008-05-21 15:53:59 -05002824 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002825 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002826 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002827
Olaf Kirch63203772007-12-13 12:43:25 -06002828 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002829
Mike Christieb2c64162007-05-30 12:57:16 -05002830 kfree(session->password);
2831 kfree(session->password_in);
2832 kfree(session->username);
2833 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002834 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002835 kfree(session->initiatorname);
2836 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002837
Mike Christie75613522008-05-21 15:53:59 -05002838 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002839 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002840 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002841}
2842EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2843
2844/**
2845 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2846 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002847 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002848 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002849 */
Mike Christie7996a772006-04-06 21:13:41 -05002850struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002851iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2852 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002853{
Mike Christie75613522008-05-21 15:53:59 -05002854 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002855 struct iscsi_conn *conn;
2856 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002857 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002858
Mike Christie5d91e202008-05-21 15:54:01 -05002859 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2860 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002861 if (!cls_conn)
2862 return NULL;
2863 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002864 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002865
Mike Christie5d91e202008-05-21 15:54:01 -05002866 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002867 conn->session = session;
2868 conn->cls_conn = cls_conn;
2869 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2870 conn->id = conn_idx;
2871 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002872 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002873
2874 init_timer(&conn->transport_timer);
2875 conn->transport_timer.data = (unsigned long)conn;
2876 conn->transport_timer.function = iscsi_check_transport_timeouts;
2877
Mike Christie843c0a82007-12-13 12:43:20 -06002878 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002879 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002880 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002881 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002882
Mike Christie9c19a7d2008-05-21 15:54:09 -05002883 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002884 spin_lock_bh(&session->lock);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002885 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002886 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002887 sizeof(void*))) {
2888 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002889 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002890 }
2891 spin_unlock_bh(&session->lock);
2892
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002893 data = (char *) __get_free_pages(GFP_KERNEL,
2894 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002895 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002896 goto login_task_data_alloc_fail;
2897 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002898
Mike Christie843c0a82007-12-13 12:43:20 -06002899 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002900 init_waitqueue_head(&conn->ehwait);
2901
2902 return cls_conn;
2903
Mike Christie9c19a7d2008-05-21 15:54:09 -05002904login_task_data_alloc_fail:
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002905 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002906 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002907login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002908 iscsi_destroy_conn(cls_conn);
2909 return NULL;
2910}
2911EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2912
2913/**
2914 * iscsi_conn_teardown - teardown iscsi connection
2915 * cls_conn: iscsi class connection
2916 *
2917 * TODO: we may need to make this into a two step process
2918 * like scsi-mls remove + put host
2919 */
2920void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2921{
2922 struct iscsi_conn *conn = cls_conn->dd_data;
2923 struct iscsi_session *session = conn->session;
2924 unsigned long flags;
2925
Mike Christief6d51802007-12-13 12:43:30 -06002926 del_timer_sync(&conn->transport_timer);
2927
Mike Christie7996a772006-04-06 21:13:41 -05002928 spin_lock_bh(&session->lock);
2929 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2930 if (session->leadconn == conn) {
2931 /*
2932 * leading connection? then give up on recovery.
2933 */
2934 session->state = ISCSI_STATE_TERMINATE;
2935 wake_up(&conn->ehwait);
2936 }
2937 spin_unlock_bh(&session->lock);
2938
Mike Christie7996a772006-04-06 21:13:41 -05002939 /*
2940 * Block until all in-progress commands for this connection
2941 * time out or fail.
2942 */
2943 for (;;) {
2944 spin_lock_irqsave(session->host->host_lock, flags);
2945 if (!session->host->host_busy) { /* OK for ERL == 0 */
2946 spin_unlock_irqrestore(session->host->host_lock, flags);
2947 break;
2948 }
2949 spin_unlock_irqrestore(session->host->host_lock, flags);
2950 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002951 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2952 "host_busy %d host_failed %d\n",
2953 session->host->host_busy,
2954 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002955 /*
2956 * force eh_abort() to unblock
2957 */
2958 wake_up(&conn->ehwait);
2959 }
2960
Mike Christie779ea122007-02-28 17:32:15 -06002961 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002962 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002963
Mike Christie7996a772006-04-06 21:13:41 -05002964 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002965 free_pages((unsigned long) conn->data,
2966 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002967 kfree(conn->persistent_address);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002968 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002969 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002970 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002971 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002972 spin_unlock_bh(&session->lock);
2973
Mike Christie7996a772006-04-06 21:13:41 -05002974 iscsi_destroy_conn(cls_conn);
2975}
2976EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2977
2978int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2979{
2980 struct iscsi_conn *conn = cls_conn->dd_data;
2981 struct iscsi_session *session = conn->session;
2982
Mike Christieffd04362006-08-31 18:09:24 -04002983 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002984 iscsi_conn_printk(KERN_ERR, conn,
2985 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002986 return -EPERM;
2987 }
2988
Mike Christiedb98ccd2006-08-31 18:09:31 -04002989 if ((session->imm_data_en || !session->initial_r2t_en) &&
2990 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002991 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2992 "first_burst %d max_burst %d\n",
2993 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002994 return -EINVAL;
2995 }
2996
Mike Christief6d51802007-12-13 12:43:30 -06002997 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002998 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2999 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06003000 conn->recv_timeout = 5;
3001 }
3002
3003 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06003004 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3005 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06003006 conn->ping_timeout = 5;
3007 }
3008
Mike Christie7996a772006-04-06 21:13:41 -05003009 spin_lock_bh(&session->lock);
3010 conn->c_stage = ISCSI_CONN_STARTED;
3011 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05003012 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05003013
Mike Christief6d51802007-12-13 12:43:30 -06003014 conn->last_recv = jiffies;
3015 conn->last_ping = jiffies;
3016 if (conn->recv_timeout && conn->ping_timeout)
3017 mod_timer(&conn->transport_timer,
3018 jiffies + (conn->recv_timeout * HZ));
3019
Mike Christie7996a772006-04-06 21:13:41 -05003020 switch(conn->stop_stage) {
3021 case STOP_CONN_RECOVER:
3022 /*
3023 * unblock eh_abort() if it is blocked. re-try all
3024 * commands after successful recovery
3025 */
Mike Christie7996a772006-04-06 21:13:41 -05003026 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06003027 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05003028 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06003029 if (session->age == 16)
3030 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06003031 break;
Mike Christie7996a772006-04-06 21:13:41 -05003032 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05003033 conn->stop_stage = 0;
3034 break;
Mike Christie7996a772006-04-06 21:13:41 -05003035 default:
3036 break;
3037 }
3038 spin_unlock_bh(&session->lock);
3039
Mike Christie75613522008-05-21 15:53:59 -05003040 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06003041 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05003042 return 0;
3043}
3044EXPORT_SYMBOL_GPL(iscsi_conn_start);
3045
3046static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05003047fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05003048{
Mike Christie3bbaaad2009-05-13 17:57:46 -05003049 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05003050 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05003051
Mike Christie3bbaaad2009-05-13 17:57:46 -05003052 for (i = 0; i < conn->session->cmds_max; i++) {
3053 task = conn->session->cmds[i];
3054 if (task->sc)
3055 continue;
3056
3057 if (task->state == ISCSI_TASK_FREE)
3058 continue;
3059
3060 ISCSI_DBG_SESSION(conn->session,
3061 "failing mgmt itt 0x%x state %d\n",
3062 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05003063 state = ISCSI_TASK_ABRT_SESS_RECOV;
3064 if (task->state == ISCSI_TASK_PENDING)
3065 state = ISCSI_TASK_COMPLETED;
3066 iscsi_complete_task(task, state);
3067
Mike Christie7996a772006-04-06 21:13:41 -05003068 }
Mike Christie7996a772006-04-06 21:13:41 -05003069}
3070
Mike Christie656cffc2006-05-18 20:31:42 -05003071static void iscsi_start_session_recovery(struct iscsi_session *session,
3072 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05003073{
Mike Christieed2abc72006-05-02 19:46:40 -05003074 int old_stop_stage;
3075
Mike Christie6724add2007-08-15 01:38:30 -05003076 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003077 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003078 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05003079 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003080 mutex_unlock(&session->eh_mutex);
3081 return;
3082 }
3083
3084 /*
Mike Christieed2abc72006-05-02 19:46:40 -05003085 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05003086 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05003087 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05003088 */
Mike Christie67a61112006-05-30 00:37:20 -05003089 if (flag == STOP_CONN_TERM)
3090 session->state = ISCSI_STATE_TERMINATE;
3091 else if (conn->stop_stage != STOP_CONN_RECOVER)
3092 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie4ae0a6c2010-03-09 14:14:51 -06003093
3094 old_stop_stage = conn->stop_stage;
3095 conn->stop_stage = flag;
Mike Christie26013ad2009-05-13 17:57:43 -05003096 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003097
Mike Christie26013ad2009-05-13 17:57:43 -05003098 del_timer_sync(&conn->transport_timer);
3099 iscsi_suspend_tx(conn);
3100
3101 spin_lock_bh(&session->lock);
Mike Christie67a61112006-05-30 00:37:20 -05003102 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05003103 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003104
Mike Christie7996a772006-04-06 21:13:41 -05003105 /*
3106 * for connection level recovery we should not calculate
3107 * header digest. conn->hdr_size used for optimization
3108 * in hdr_extract() and will be re-negotiated at
3109 * set_param() time.
3110 */
3111 if (flag == STOP_CONN_RECOVER) {
3112 conn->hdrdgst_en = 0;
3113 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05003114 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05003115 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06003116 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05003117 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05003118 }
Mike Christie7996a772006-04-06 21:13:41 -05003119 }
Mike Christie656cffc2006-05-18 20:31:42 -05003120
Mike Christie656cffc2006-05-18 20:31:42 -05003121 /*
3122 * flush queues.
3123 */
3124 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05003125 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05003126 fail_mgmt_tasks(session, conn);
Mike Christie5d12c052009-11-11 16:34:32 -06003127 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
Mike Christie656cffc2006-05-18 20:31:42 -05003128 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003129 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003130}
Mike Christie7996a772006-04-06 21:13:41 -05003131
3132void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3133{
3134 struct iscsi_conn *conn = cls_conn->dd_data;
3135 struct iscsi_session *session = conn->session;
3136
3137 switch (flag) {
3138 case STOP_CONN_RECOVER:
3139 case STOP_CONN_TERM:
3140 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05003141 break;
Mike Christie7996a772006-04-06 21:13:41 -05003142 default:
Mike Christie322d7392008-01-31 13:36:52 -06003143 iscsi_conn_printk(KERN_ERR, conn,
3144 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05003145 }
3146}
3147EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3148
3149int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3150 struct iscsi_cls_conn *cls_conn, int is_leading)
3151{
Mike Christie75613522008-05-21 15:53:59 -05003152 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04003153 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05003154
Mike Christie7996a772006-04-06 21:13:41 -05003155 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003156 if (is_leading)
3157 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04003158 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003159
3160 /*
3161 * Unblock xmitworker(), Login Phase will pass through.
3162 */
3163 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3164 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3165 return 0;
3166}
3167EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3168
Mike Christie5700b1a2009-05-13 17:57:40 -05003169static int iscsi_switch_str_param(char **param, char *new_val_buf)
3170{
3171 char *new_val;
3172
3173 if (*param) {
3174 if (!strcmp(*param, new_val_buf))
3175 return 0;
3176 }
3177
3178 new_val = kstrdup(new_val_buf, GFP_NOIO);
3179 if (!new_val)
3180 return -ENOMEM;
3181
3182 kfree(*param);
3183 *param = new_val;
3184 return 0;
3185}
Mike Christiea54a52c2006-06-28 12:00:23 -05003186
3187int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3188 enum iscsi_param param, char *buf, int buflen)
3189{
3190 struct iscsi_conn *conn = cls_conn->dd_data;
3191 struct iscsi_session *session = conn->session;
3192 uint32_t value;
3193
3194 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003195 case ISCSI_PARAM_FAST_ABORT:
3196 sscanf(buf, "%d", &session->fast_abort);
3197 break;
Mike Christief6d51802007-12-13 12:43:30 -06003198 case ISCSI_PARAM_ABORT_TMO:
3199 sscanf(buf, "%d", &session->abort_timeout);
3200 break;
3201 case ISCSI_PARAM_LU_RESET_TMO:
3202 sscanf(buf, "%d", &session->lu_reset_timeout);
3203 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003204 case ISCSI_PARAM_TGT_RESET_TMO:
3205 sscanf(buf, "%d", &session->tgt_reset_timeout);
3206 break;
Mike Christief6d51802007-12-13 12:43:30 -06003207 case ISCSI_PARAM_PING_TMO:
3208 sscanf(buf, "%d", &conn->ping_timeout);
3209 break;
3210 case ISCSI_PARAM_RECV_TMO:
3211 sscanf(buf, "%d", &conn->recv_timeout);
3212 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003213 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3214 sscanf(buf, "%d", &conn->max_recv_dlength);
3215 break;
3216 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3217 sscanf(buf, "%d", &conn->max_xmit_dlength);
3218 break;
3219 case ISCSI_PARAM_HDRDGST_EN:
3220 sscanf(buf, "%d", &conn->hdrdgst_en);
3221 break;
3222 case ISCSI_PARAM_DATADGST_EN:
3223 sscanf(buf, "%d", &conn->datadgst_en);
3224 break;
3225 case ISCSI_PARAM_INITIAL_R2T_EN:
3226 sscanf(buf, "%d", &session->initial_r2t_en);
3227 break;
3228 case ISCSI_PARAM_MAX_R2T:
3229 sscanf(buf, "%d", &session->max_r2t);
3230 break;
3231 case ISCSI_PARAM_IMM_DATA_EN:
3232 sscanf(buf, "%d", &session->imm_data_en);
3233 break;
3234 case ISCSI_PARAM_FIRST_BURST:
3235 sscanf(buf, "%d", &session->first_burst);
3236 break;
3237 case ISCSI_PARAM_MAX_BURST:
3238 sscanf(buf, "%d", &session->max_burst);
3239 break;
3240 case ISCSI_PARAM_PDU_INORDER_EN:
3241 sscanf(buf, "%d", &session->pdu_inorder_en);
3242 break;
3243 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3244 sscanf(buf, "%d", &session->dataseq_inorder_en);
3245 break;
3246 case ISCSI_PARAM_ERL:
3247 sscanf(buf, "%d", &session->erl);
3248 break;
3249 case ISCSI_PARAM_IFMARKER_EN:
3250 sscanf(buf, "%d", &value);
3251 BUG_ON(value);
3252 break;
3253 case ISCSI_PARAM_OFMARKER_EN:
3254 sscanf(buf, "%d", &value);
3255 BUG_ON(value);
3256 break;
3257 case ISCSI_PARAM_EXP_STATSN:
3258 sscanf(buf, "%u", &conn->exp_statsn);
3259 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003260 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003261 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003262 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003263 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003264 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05003265 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003266 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003267 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003268 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003269 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003270 case ISCSI_PARAM_TPGT:
3271 sscanf(buf, "%d", &session->tpgt);
3272 break;
3273 case ISCSI_PARAM_PERSISTENT_PORT:
3274 sscanf(buf, "%d", &conn->persistent_port);
3275 break;
3276 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003277 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003278 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003279 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003280 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003281 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003282 default:
3283 return -ENOSYS;
3284 }
3285
3286 return 0;
3287}
3288EXPORT_SYMBOL_GPL(iscsi_set_param);
3289
3290int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3291 enum iscsi_param param, char *buf)
3292{
Mike Christie75613522008-05-21 15:53:59 -05003293 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003294 int len;
3295
3296 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003297 case ISCSI_PARAM_FAST_ABORT:
3298 len = sprintf(buf, "%d\n", session->fast_abort);
3299 break;
Mike Christief6d51802007-12-13 12:43:30 -06003300 case ISCSI_PARAM_ABORT_TMO:
3301 len = sprintf(buf, "%d\n", session->abort_timeout);
3302 break;
3303 case ISCSI_PARAM_LU_RESET_TMO:
3304 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3305 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003306 case ISCSI_PARAM_TGT_RESET_TMO:
3307 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3308 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003309 case ISCSI_PARAM_INITIAL_R2T_EN:
3310 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3311 break;
3312 case ISCSI_PARAM_MAX_R2T:
3313 len = sprintf(buf, "%hu\n", session->max_r2t);
3314 break;
3315 case ISCSI_PARAM_IMM_DATA_EN:
3316 len = sprintf(buf, "%d\n", session->imm_data_en);
3317 break;
3318 case ISCSI_PARAM_FIRST_BURST:
3319 len = sprintf(buf, "%u\n", session->first_burst);
3320 break;
3321 case ISCSI_PARAM_MAX_BURST:
3322 len = sprintf(buf, "%u\n", session->max_burst);
3323 break;
3324 case ISCSI_PARAM_PDU_INORDER_EN:
3325 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3326 break;
3327 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3328 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3329 break;
3330 case ISCSI_PARAM_ERL:
3331 len = sprintf(buf, "%d\n", session->erl);
3332 break;
3333 case ISCSI_PARAM_TARGET_NAME:
3334 len = sprintf(buf, "%s\n", session->targetname);
3335 break;
3336 case ISCSI_PARAM_TPGT:
3337 len = sprintf(buf, "%d\n", session->tpgt);
3338 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003339 case ISCSI_PARAM_USERNAME:
3340 len = sprintf(buf, "%s\n", session->username);
3341 break;
3342 case ISCSI_PARAM_USERNAME_IN:
3343 len = sprintf(buf, "%s\n", session->username_in);
3344 break;
3345 case ISCSI_PARAM_PASSWORD:
3346 len = sprintf(buf, "%s\n", session->password);
3347 break;
3348 case ISCSI_PARAM_PASSWORD_IN:
3349 len = sprintf(buf, "%s\n", session->password_in);
3350 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003351 case ISCSI_PARAM_IFACE_NAME:
3352 len = sprintf(buf, "%s\n", session->ifacename);
3353 break;
3354 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003355 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003356 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003357 default:
3358 return -ENOSYS;
3359 }
3360
3361 return len;
3362}
3363EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3364
3365int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3366 enum iscsi_param param, char *buf)
3367{
3368 struct iscsi_conn *conn = cls_conn->dd_data;
3369 int len;
3370
3371 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003372 case ISCSI_PARAM_PING_TMO:
3373 len = sprintf(buf, "%u\n", conn->ping_timeout);
3374 break;
3375 case ISCSI_PARAM_RECV_TMO:
3376 len = sprintf(buf, "%u\n", conn->recv_timeout);
3377 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003378 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3379 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3380 break;
3381 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3382 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3383 break;
3384 case ISCSI_PARAM_HDRDGST_EN:
3385 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3386 break;
3387 case ISCSI_PARAM_DATADGST_EN:
3388 len = sprintf(buf, "%d\n", conn->datadgst_en);
3389 break;
3390 case ISCSI_PARAM_IFMARKER_EN:
3391 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3392 break;
3393 case ISCSI_PARAM_OFMARKER_EN:
3394 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3395 break;
3396 case ISCSI_PARAM_EXP_STATSN:
3397 len = sprintf(buf, "%u\n", conn->exp_statsn);
3398 break;
3399 case ISCSI_PARAM_PERSISTENT_PORT:
3400 len = sprintf(buf, "%d\n", conn->persistent_port);
3401 break;
3402 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3403 len = sprintf(buf, "%s\n", conn->persistent_address);
3404 break;
3405 default:
3406 return -ENOSYS;
3407 }
3408
3409 return len;
3410}
3411EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3412
Mike Christie0801c242007-05-30 12:57:12 -05003413int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3414 char *buf)
3415{
Mike Christie75613522008-05-21 15:53:59 -05003416 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003417 int len;
3418
3419 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003420 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003421 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003422 break;
Mike Christie0801c242007-05-30 12:57:12 -05003423 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003424 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003425 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003426 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003427 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003428 break;
Mike Christie75613522008-05-21 15:53:59 -05003429 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003430 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003431 break;
Mike Christie0801c242007-05-30 12:57:12 -05003432 default:
3433 return -ENOSYS;
3434 }
3435
3436 return len;
3437}
3438EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3439
3440int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3441 char *buf, int buflen)
3442{
Mike Christie75613522008-05-21 15:53:59 -05003443 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003444
3445 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003446 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003447 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003448 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003449 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003450 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003451 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003452 default:
3453 return -ENOSYS;
3454 }
3455
3456 return 0;
3457}
3458EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3459
Mike Christie7996a772006-04-06 21:13:41 -05003460MODULE_AUTHOR("Mike Christie");
3461MODULE_DESCRIPTION("iSCSI library functions");
3462MODULE_LICENSE("GPL");