blob: 0c550d5b9133473d9308ce502a30df439e7ca077 [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 Christief41d4722010-12-31 02:22:21 -0600508 int oldstate = task->state;
Mike Christie7996a772006-04-06 21:13:41 -0500509
Mike Christie4421c9e2009-05-13 17:57:50 -0500510 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
511 task->itt, task->state, task->sc);
512
Mike Christie577577d2008-12-02 00:32:05 -0600513 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500514 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500515 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500516 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500517 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500518 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500520 return;
521
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800522 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500523
Mike Christie3e5c28a2008-05-21 15:54:06 -0500524 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500525 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500526 /* SCSI eh reuses commands to verify us */
527 sc->SCp.ptr = NULL;
528 /*
Mike Christief41d4722010-12-31 02:22:21 -0600529 * queue command may call this to free the task, so
530 * it will decide how to return sc to scsi-ml.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500531 */
Mike Christief41d4722010-12-31 02:22:21 -0600532 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500533 sc->scsi_done(sc);
534 }
Mike Christie7996a772006-04-06 21:13:41 -0500535}
536
Mike Christie913e5bf2008-05-21 15:54:18 -0500537void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400538{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500539 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400540}
Mike Christie913e5bf2008-05-21 15:54:18 -0500541EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400542
Eddie Wai8eea2f52010-11-23 15:29:21 -0800543void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400544{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500545 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500546 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400547}
Eddie Wai8eea2f52010-11-23 15:29:21 -0800548EXPORT_SYMBOL_GPL(__iscsi_put_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400549
Mike Christie9c19a7d2008-05-21 15:54:09 -0500550void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500551{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500552 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500553
554 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500555 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500556 spin_unlock_bh(&session->lock);
557}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500558EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500559
Mike Christie3bbaaad2009-05-13 17:57:46 -0500560/**
561 * iscsi_complete_task - finish a task
562 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500563 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500564 *
565 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600566 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500567static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600568{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500569 struct iscsi_conn *conn = task->conn;
570
Mike Christie4421c9e2009-05-13 17:57:50 -0500571 ISCSI_DBG_SESSION(conn->session,
572 "complete task itt 0x%x state %d sc %p\n",
573 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500574 if (task->state == ISCSI_TASK_COMPLETED ||
575 task->state == ISCSI_TASK_ABRT_TMF ||
Mike Christief41d4722010-12-31 02:22:21 -0600576 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
577 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500578 return;
579 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500580 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500581
582 if (!list_empty(&task->running))
583 list_del_init(&task->running);
584
585 if (conn->task == task)
586 conn->task = NULL;
587
588 if (conn->ping_task == task)
589 conn->ping_task = NULL;
590
591 /* release get from queueing */
592 __iscsi_put_task(task);
593}
594
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530595/**
596 * iscsi_complete_scsi_task - finish scsi task normally
597 * @task: iscsi task for scsi cmd
598 * @exp_cmdsn: expected cmd sn in cpu format
599 * @max_cmdsn: max cmd sn in cpu format
600 *
601 * This is used when drivers do not need or cannot perform
602 * lower level pdu processing.
603 *
604 * Called with session lock
605 */
606void iscsi_complete_scsi_task(struct iscsi_task *task,
607 uint32_t exp_cmdsn, uint32_t max_cmdsn)
608{
609 struct iscsi_conn *conn = task->conn;
610
611 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
612
613 conn->last_recv = jiffies;
614 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
615 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
616}
617EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
618
619
Mike Christie3bbaaad2009-05-13 17:57:46 -0500620/*
621 * session lock must be held and if not called for a task that is
622 * still pending or from the xmit thread, then xmit thread must
623 * be suspended.
624 */
625static void fail_scsi_task(struct iscsi_task *task, int err)
626{
627 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600628 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500629 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600630
Mike Christie3bbaaad2009-05-13 17:57:46 -0500631 /*
632 * if a command completes and we get a successful tmf response
633 * we will hit this because the scsi eh abort code does not take
634 * a ref to the task.
635 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500636 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600637 if (!sc)
638 return;
639
Mike Christieb3cd5052009-05-13 17:57:49 -0500640 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600641 /*
642 * cmd never made it to the xmit thread, so we should not count
643 * the cmd in the sequencing
644 */
645 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500646 /* it was never sent so just complete like normal */
647 state = ISCSI_TASK_COMPLETED;
648 } else if (err == DID_TRANSPORT_DISRUPTED)
649 state = ISCSI_TASK_ABRT_SESS_RECOV;
650 else
651 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600652
Mike Christieb3cd5052009-05-13 17:57:49 -0500653 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500654 if (!scsi_bidi_cmnd(sc))
655 scsi_set_resid(sc, scsi_bufflen(sc));
656 else {
657 scsi_out(sc)->resid = scsi_out(sc)->length;
658 scsi_in(sc)->resid = scsi_in(sc)->length;
659 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500660
Mike Christieb3cd5052009-05-13 17:57:49 -0500661 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600662}
663
Mike Christie3e5c28a2008-05-21 15:54:06 -0500664static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500665 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500666{
667 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600668 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500669 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
Mike Christie4f704dc2009-11-11 16:34:31 -0600670 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie052d0142008-05-21 15:54:05 -0500671
672 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
673 return -ENOTCONN;
674
Mike Christie4f704dc2009-11-11 16:34:31 -0600675 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
Mike Christie052d0142008-05-21 15:54:05 -0500676 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
677 /*
678 * pre-format CmdSN for outgoing PDU.
679 */
680 nop->cmdsn = cpu_to_be32(session->cmdsn);
681 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500682 /*
Mike Christie4f704dc2009-11-11 16:34:31 -0600683 * TODO: We always use immediate for normal session pdus.
Mike Christie052d0142008-05-21 15:54:05 -0500684 * If we start to send tmfs or nops as non-immediate then
685 * we should start checking the cmdsn numbers for mgmt tasks.
Mike Christie4f704dc2009-11-11 16:34:31 -0600686 *
687 * During discovery sessions iscsid sends TEXT as non immediate,
688 * but we always only send one PDU at a time.
Mike Christie052d0142008-05-21 15:54:05 -0500689 */
690 if (conn->c_stage == ISCSI_CONN_STARTED &&
691 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
692 session->queued_cmdsn++;
693 session->cmdsn++;
694 }
695 }
696
Mike Christieae15f802008-12-02 00:32:15 -0600697 if (session->tt->init_task && session->tt->init_task(task))
698 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500699
700 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
701 session->state = ISCSI_STATE_LOGGING_OUT;
702
Mike Christie577577d2008-12-02 00:32:05 -0600703 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600704 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
705 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
706 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500707 return 0;
708}
709
Mike Christie9c19a7d2008-05-21 15:54:09 -0500710static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600711__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
712 char *data, uint32_t data_size)
713{
714 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500715 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie4f704dc2009-11-11 16:34:31 -0600716 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500717 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600718 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600719
720 if (session->state == ISCSI_STATE_TERMINATE)
721 return NULL;
722
Mike Christie4f704dc2009-11-11 16:34:31 -0600723 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
Mike Christief6d51802007-12-13 12:43:30 -0600724 /*
725 * Login and Text are sent serially, in
726 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500727 * Same task can be used. Same ITT must be used.
728 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600729 */
Mike Christie4f704dc2009-11-11 16:34:31 -0600730 if (conn->login_task->state != ISCSI_TASK_FREE) {
731 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
732 "progress. Cannot start new task.\n");
733 return NULL;
734 }
735
Mike Christie9c19a7d2008-05-21 15:54:09 -0500736 task = conn->login_task;
Mike Christie4f704dc2009-11-11 16:34:31 -0600737 } else {
Mike Christie26013ad2009-05-13 17:57:43 -0500738 if (session->state != ISCSI_STATE_LOGGED_IN)
739 return NULL;
740
Mike Christief6d51802007-12-13 12:43:30 -0600741 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
742 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
743
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800744 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500745 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600746 return NULL;
747 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500748 /*
749 * released in complete pdu for task we expect a response for, and
750 * released by the lld when it has transmitted the task for
751 * pdus we do not expect a response for.
752 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500753 atomic_set(&task->refcount, 1);
754 task->conn = conn;
755 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500756 INIT_LIST_HEAD(&task->running);
757 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600758
759 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500760 memcpy(task->data, data, data_size);
761 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600762 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500763 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600764
Mike Christie184b57c2009-05-13 17:57:39 -0500765 if (conn->session->tt->alloc_pdu) {
766 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
767 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
768 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500769 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500770 }
Mike Christie577577d2008-12-02 00:32:05 -0600771 }
Mike Christie184b57c2009-05-13 17:57:39 -0500772
Mike Christie262ef632008-12-02 00:32:13 -0600773 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600774 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500775 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600776
777 if (hdr->itt != RESERVED_ITT) {
778 if (session->tt->parse_pdu_itt)
779 task->hdr->itt = itt;
780 else
781 task->hdr->itt = build_itt(task->itt,
782 task->conn->session->age);
783 }
784
Mike Christie1336aed2009-05-13 17:57:48 -0500785 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600786 if (iscsi_prep_mgmt_task(conn, task))
787 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500788
Mike Christie9c19a7d2008-05-21 15:54:09 -0500789 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600790 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500791 } else {
792 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600793 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500794 }
Mike Christie052d0142008-05-21 15:54:05 -0500795
Mike Christie9c19a7d2008-05-21 15:54:09 -0500796 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600797
798free_task:
799 __iscsi_put_task(task);
800 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600801}
802
803int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
804 char *data, uint32_t data_size)
805{
806 struct iscsi_conn *conn = cls_conn->dd_data;
807 struct iscsi_session *session = conn->session;
808 int err = 0;
809
810 spin_lock_bh(&session->lock);
811 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
812 err = -EPERM;
813 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600814 return err;
815}
816EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
817
Mike Christie7996a772006-04-06 21:13:41 -0500818/**
819 * iscsi_cmd_rsp - SCSI Command Response processing
820 * @conn: iscsi connection
821 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500822 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500823 * @data: cmd data buffer
824 * @datalen: len of buffer
825 *
826 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500827 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500828 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500829static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500830 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500831 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500832{
Mike Christie7996a772006-04-06 21:13:41 -0500833 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
834 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500835 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500836
Mike Christie77a23c22007-05-30 12:57:18 -0500837 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500838 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
839
840 sc->result = (DID_OK << 16) | rhdr->cmd_status;
841
842 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
843 sc->result = DID_ERROR << 16;
844 goto out;
845 }
846
847 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600848 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500849
850 if (datalen < 2) {
851invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600852 iscsi_conn_printk(KERN_ERR, conn,
853 "Got CHECK_CONDITION but invalid data "
854 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500855 sc->result = DID_BAD_TARGET << 16;
856 goto out;
857 }
858
Harvey Harrison8f3339912008-05-21 15:54:20 -0500859 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500860 if (datalen < senselen)
861 goto invalid_datalen;
862
863 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600864 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600865 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
866 min_t(uint16_t, senselen,
867 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500868 }
869
Boaz Harroshc07d4442008-04-18 10:11:52 -0500870 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
871 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
872 int res_count = be32_to_cpu(rhdr->bi_residual_count);
873
874 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
875 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
876 res_count <= scsi_in(sc)->length))
877 scsi_in(sc)->resid = res_count;
878 else
879 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
880 }
881
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600882 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
883 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500884 int res_count = be32_to_cpu(rhdr->residual_count);
885
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600886 if (res_count > 0 &&
887 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
888 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500889 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900890 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500891 else
892 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500893 }
Mike Christie7996a772006-04-06 21:13:41 -0500894out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500895 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600896 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500897 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500898 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500899}
900
Mike Christie1d9edf02008-09-24 11:46:09 -0500901/**
902 * iscsi_data_in_rsp - SCSI Data-In Response processing
903 * @conn: iscsi connection
904 * @hdr: iscsi pdu
905 * @task: scsi command task
906 **/
907static void
908iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
909 struct iscsi_task *task)
910{
911 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
912 struct scsi_cmnd *sc = task->sc;
913
914 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
915 return;
916
Mike Christieedbc9aa052009-05-13 17:57:42 -0500917 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500918 sc->result = (DID_OK << 16) | rhdr->cmd_status;
919 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
920 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
921 ISCSI_FLAG_DATA_OVERFLOW)) {
922 int res_count = be32_to_cpu(rhdr->residual_count);
923
924 if (res_count > 0 &&
925 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
926 res_count <= scsi_in(sc)->length))
927 scsi_in(sc)->resid = res_count;
928 else
929 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
930 }
931
Mike Christie3bbaaad2009-05-13 17:57:46 -0500932 ISCSI_DBG_SESSION(conn->session, "data in with status done "
933 "[sc %p res %d itt 0x%x]\n",
934 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500935 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500936 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500937}
938
Mike Christie7ea8b822006-07-24 15:47:22 -0500939static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
940{
941 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
942
943 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
944 conn->tmfrsp_pdus_cnt++;
945
Mike Christie843c0a82007-12-13 12:43:20 -0600946 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500947 return;
948
949 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600950 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500951 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600952 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500953 else
Mike Christie843c0a82007-12-13 12:43:20 -0600954 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500955 wake_up(&conn->ehwait);
956}
957
Mike Christief6d51802007-12-13 12:43:30 -0600958static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
959{
960 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500961 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600962
Mike Christie9c19a7d2008-05-21 15:54:09 -0500963 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600964 return;
965
966 memset(&hdr, 0, sizeof(struct iscsi_nopout));
967 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
968 hdr.flags = ISCSI_FLAG_CMD_FINAL;
969
970 if (rhdr) {
971 memcpy(hdr.lun, rhdr->lun, 8);
972 hdr.ttt = rhdr->ttt;
973 hdr.itt = RESERVED_ITT;
974 } else
975 hdr.ttt = RESERVED_ITT;
976
Mike Christie9c19a7d2008-05-21 15:54:09 -0500977 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
978 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600979 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600980 else if (!rhdr) {
981 /* only track our nops */
982 conn->ping_task = task;
983 conn->last_ping = jiffies;
984 }
Mike Christief6d51802007-12-13 12:43:30 -0600985}
986
Mike Christie8afa1432009-08-20 15:10:59 -0500987static int iscsi_nop_out_rsp(struct iscsi_task *task,
988 struct iscsi_nopin *nop, char *data, int datalen)
989{
990 struct iscsi_conn *conn = task->conn;
991 int rc = 0;
992
993 if (conn->ping_task != task) {
994 /*
995 * If this is not in response to one of our
996 * nops then it must be from userspace.
997 */
998 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
999 data, datalen))
1000 rc = ISCSI_ERR_CONN_FAILED;
1001 } else
1002 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
1003 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1004 return rc;
1005}
1006
Mike Christie62f38302006-08-31 18:09:27 -04001007static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1008 char *data, int datalen)
1009{
1010 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1011 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -05001012 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -04001013
1014 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1015
Mike Christie8afa1432009-08-20 15:10:59 -05001016 if (ntoh24(reject->dlength) > datalen ||
1017 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1018 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1019 "pdu. Invalid data length (pdu dlength "
1020 "%u, datalen %d\n", ntoh24(reject->dlength),
1021 datalen);
1022 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -04001023 }
Mike Christie8afa1432009-08-20 15:10:59 -05001024 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1025 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1026
1027 switch (reject->reason) {
1028 case ISCSI_REASON_DATA_DIGEST_ERROR:
1029 iscsi_conn_printk(KERN_ERR, conn,
1030 "pdu (op 0x%x itt 0x%x) rejected "
1031 "due to DataDigest error.\n",
1032 rejected_pdu.itt, opcode);
1033 break;
1034 case ISCSI_REASON_IMM_CMD_REJECT:
1035 iscsi_conn_printk(KERN_ERR, conn,
1036 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1037 "immediate commands.\n",
1038 rejected_pdu.itt, opcode);
1039 /*
1040 * We only send one TMF at a time so if the target could not
1041 * handle it, then it should get fixed (RFC mandates that
1042 * a target can handle one immediate TMF per conn).
1043 *
1044 * For nops-outs, we could have sent more than one if
1045 * the target is sending us lots of nop-ins
1046 */
1047 if (opcode != ISCSI_OP_NOOP_OUT)
1048 return 0;
1049
1050 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
1051 /*
1052 * nop-out in response to target's nop-out rejected.
1053 * Just resend.
1054 */
1055 iscsi_send_nopout(conn,
1056 (struct iscsi_nopin*)&rejected_pdu);
1057 else {
1058 struct iscsi_task *task;
1059 /*
1060 * Our nop as ping got dropped. We know the target
1061 * and transport are ok so just clean up
1062 */
1063 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1064 if (!task) {
1065 iscsi_conn_printk(KERN_ERR, conn,
1066 "Invalid pdu reject. Could "
1067 "not lookup rejected task.\n");
1068 rc = ISCSI_ERR_BAD_ITT;
1069 } else
1070 rc = iscsi_nop_out_rsp(task,
1071 (struct iscsi_nopin*)&rejected_pdu,
1072 NULL, 0);
1073 }
1074 break;
1075 default:
1076 iscsi_conn_printk(KERN_ERR, conn,
1077 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1078 "code 0x%x\n", rejected_pdu.itt,
1079 rejected_pdu.opcode, reject->reason);
1080 break;
1081 }
1082 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001083}
1084
Mike Christie7996a772006-04-06 21:13:41 -05001085/**
Mike Christie913e5bf2008-05-21 15:54:18 -05001086 * iscsi_itt_to_task - look up task by itt
1087 * @conn: iscsi connection
1088 * @itt: itt
1089 *
1090 * This should be used for mgmt tasks like login and nops, or if
1091 * the LDD's itt space does not include the session age.
1092 *
1093 * The session lock must be held.
1094 */
Mike Christie8f9256c2009-05-13 17:57:41 -05001095struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -05001096{
1097 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001098 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -05001099
1100 if (itt == RESERVED_ITT)
1101 return NULL;
1102
Mike Christie262ef632008-12-02 00:32:13 -06001103 if (session->tt->parse_pdu_itt)
1104 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1105 else
1106 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -05001107 if (i >= session->cmds_max)
1108 return NULL;
1109
1110 return session->cmds[i];
1111}
Mike Christie8f9256c2009-05-13 17:57:41 -05001112EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -05001113
1114/**
Mike Christie7996a772006-04-06 21:13:41 -05001115 * __iscsi_complete_pdu - complete pdu
1116 * @conn: iscsi conn
1117 * @hdr: iscsi header
1118 * @data: data buffer
1119 * @datalen: len of data buffer
1120 *
1121 * Completes pdu processing by freeing any resources allocated at
1122 * queuecommand or send generic. session lock must be held and verify
1123 * itt must have been called.
1124 */
Mike Christie913e5bf2008-05-21 15:54:18 -05001125int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1126 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001127{
1128 struct iscsi_session *session = conn->session;
1129 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001130 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001131 uint32_t itt;
1132
Mike Christief6d51802007-12-13 12:43:30 -06001133 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001134 rc = iscsi_verify_itt(conn, hdr->itt);
1135 if (rc)
1136 return rc;
1137
Al Virob4377352007-02-09 16:39:40 +00001138 if (hdr->itt != RESERVED_ITT)
1139 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001140 else
Al Virob4377352007-02-09 16:39:40 +00001141 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001142
Mike Christie1b2c7af2009-03-05 14:45:58 -06001143 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1144 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001145
Mike Christie3e5c28a2008-05-21 15:54:06 -05001146 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001147 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001148
Mike Christie7996a772006-04-06 21:13:41 -05001149 switch(opcode) {
1150 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001151 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001152 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001153 break;
1154 }
1155
Al Virob4377352007-02-09 16:39:40 +00001156 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001157 break;
1158
Mike Christief6d51802007-12-13 12:43:30 -06001159 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001160 break;
1161 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001162 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1163 break;
Mike Christie7996a772006-04-06 21:13:41 -05001164 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001165 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001166 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1167 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001168 break;
1169 default:
1170 rc = ISCSI_ERR_BAD_OPCODE;
1171 break;
1172 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001173 goto out;
1174 }
Mike Christie7996a772006-04-06 21:13:41 -05001175
Mike Christie3e5c28a2008-05-21 15:54:06 -05001176 switch(opcode) {
1177 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001178 case ISCSI_OP_SCSI_DATA_IN:
1179 task = iscsi_itt_to_ctask(conn, hdr->itt);
1180 if (!task)
1181 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001182 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001183 break;
1184 case ISCSI_OP_R2T:
1185 /*
1186 * LLD handles R2Ts if they need to.
1187 */
1188 return 0;
1189 case ISCSI_OP_LOGOUT_RSP:
1190 case ISCSI_OP_LOGIN_RSP:
1191 case ISCSI_OP_TEXT_RSP:
1192 case ISCSI_OP_SCSI_TMFUNC_RSP:
1193 case ISCSI_OP_NOOP_IN:
1194 task = iscsi_itt_to_task(conn, hdr->itt);
1195 if (!task)
1196 return ISCSI_ERR_BAD_ITT;
1197 break;
1198 default:
1199 return ISCSI_ERR_BAD_OPCODE;
1200 }
1201
1202 switch(opcode) {
1203 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001204 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001205 break;
1206 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001207 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001208 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001209 case ISCSI_OP_LOGOUT_RSP:
1210 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1211 if (datalen) {
1212 rc = ISCSI_ERR_PROTO;
1213 break;
1214 }
1215 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1216 goto recv_pdu;
1217 case ISCSI_OP_LOGIN_RSP:
1218 case ISCSI_OP_TEXT_RSP:
1219 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1220 /*
1221 * login related PDU's exp_statsn is handled in
1222 * userspace
1223 */
1224 goto recv_pdu;
1225 case ISCSI_OP_SCSI_TMFUNC_RSP:
1226 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1227 if (datalen) {
1228 rc = ISCSI_ERR_PROTO;
1229 break;
1230 }
1231
1232 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001233 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001234 break;
1235 case ISCSI_OP_NOOP_IN:
1236 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1237 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1238 rc = ISCSI_ERR_PROTO;
1239 break;
1240 }
1241 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1242
Mike Christie8afa1432009-08-20 15:10:59 -05001243 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1244 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001245 break;
1246 default:
1247 rc = ISCSI_ERR_BAD_OPCODE;
1248 break;
1249 }
1250
1251out:
1252 return rc;
1253recv_pdu:
1254 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1255 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001256 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001257 return rc;
1258}
Mike Christie913e5bf2008-05-21 15:54:18 -05001259EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001260
1261int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1262 char *data, int datalen)
1263{
1264 int rc;
1265
1266 spin_lock(&conn->session->lock);
1267 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1268 spin_unlock(&conn->session->lock);
1269 return rc;
1270}
1271EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1272
Mike Christie0af967f2008-05-21 15:54:04 -05001273int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001274{
1275 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001276 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001277
Mike Christie0af967f2008-05-21 15:54:04 -05001278 if (itt == RESERVED_ITT)
1279 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001280
Mike Christie262ef632008-12-02 00:32:13 -06001281 if (session->tt->parse_pdu_itt)
1282 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1283 else {
1284 i = get_itt(itt);
1285 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1286 }
1287
1288 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001289 iscsi_conn_printk(KERN_ERR, conn,
1290 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001291 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001292 return ISCSI_ERR_BAD_ITT;
1293 }
Mike Christie7996a772006-04-06 21:13:41 -05001294
Mike Christie3e5c28a2008-05-21 15:54:06 -05001295 if (i >= session->cmds_max) {
1296 iscsi_conn_printk(KERN_ERR, conn,
1297 "received invalid itt index %u (max cmds "
1298 "%u.\n", i, session->cmds_max);
1299 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001300 }
Mike Christie7996a772006-04-06 21:13:41 -05001301 return 0;
1302}
1303EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1304
Mike Christie913e5bf2008-05-21 15:54:18 -05001305/**
1306 * iscsi_itt_to_ctask - look up ctask by itt
1307 * @conn: iscsi connection
1308 * @itt: itt
1309 *
1310 * This should be used for cmd tasks.
1311 *
1312 * The session lock must be held.
1313 */
1314struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001315{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001316 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001317
1318 if (iscsi_verify_itt(conn, itt))
1319 return NULL;
1320
Mike Christie913e5bf2008-05-21 15:54:18 -05001321 task = iscsi_itt_to_task(conn, itt);
1322 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001323 return NULL;
1324
Mike Christie913e5bf2008-05-21 15:54:18 -05001325 if (task->sc->SCp.phase != conn->session->age) {
1326 iscsi_session_printk(KERN_ERR, conn->session,
1327 "task's session age %d, expected %d\n",
1328 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001329 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001330 }
Mike Christie0af967f2008-05-21 15:54:04 -05001331
Mike Christie9c19a7d2008-05-21 15:54:09 -05001332 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001333}
1334EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1335
Mike Christie40a06e72009-03-05 14:46:05 -06001336void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001337 enum iscsi_err err)
1338{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001339 struct iscsi_conn *conn;
1340 struct device *dev;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001341
Mike Christiebfcf72e2010-12-31 02:22:22 -06001342 spin_lock_bh(&session->lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001343 conn = session->leadconn;
1344 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
Mike Christiebfcf72e2010-12-31 02:22:22 -06001345 spin_unlock_bh(&session->lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001346 return;
1347 }
1348
1349 dev = get_device(&conn->cls_conn->dev);
Mike Christiebfcf72e2010-12-31 02:22:22 -06001350 spin_unlock_bh(&session->lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001351 if (!dev)
1352 return;
1353 /*
1354 * if the host is being removed bypass the connection
1355 * recovery initialization because we are going to kill
1356 * the session.
1357 */
1358 if (err == ISCSI_ERR_INVALID_HOST)
1359 iscsi_conn_error_event(conn->cls_conn, err);
1360 else
1361 iscsi_conn_failure(conn, err);
1362 put_device(dev);
1363}
1364EXPORT_SYMBOL_GPL(iscsi_session_failure);
1365
Mike Christie7996a772006-04-06 21:13:41 -05001366void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1367{
1368 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -05001369
Mike Christiebfcf72e2010-12-31 02:22:22 -06001370 spin_lock_bh(&session->lock);
Mike Christie656cffc2006-05-18 20:31:42 -05001371 if (session->state == ISCSI_STATE_FAILED) {
Mike Christiebfcf72e2010-12-31 02:22:22 -06001372 spin_unlock_bh(&session->lock);
Mike Christie656cffc2006-05-18 20:31:42 -05001373 return;
1374 }
1375
Mike Christie67a61112006-05-30 00:37:20 -05001376 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001377 session->state = ISCSI_STATE_FAILED;
Mike Christiebfcf72e2010-12-31 02:22:22 -06001378 spin_unlock_bh(&session->lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001379
Mike Christie7996a772006-04-06 21:13:41 -05001380 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1381 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001382 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001383}
1384EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1385
Mike Christie77a23c22007-05-30 12:57:18 -05001386static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1387{
1388 struct iscsi_session *session = conn->session;
1389
1390 /*
1391 * Check for iSCSI window and take care of CmdSN wrap-around
1392 */
Mike Christiee0726402007-07-26 12:46:48 -05001393 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001394 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1395 "%u MaxCmdSN %u CmdSN %u/%u\n",
1396 session->exp_cmdsn, session->max_cmdsn,
1397 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001398 return -ENOSPC;
1399 }
1400 return 0;
1401}
1402
Mike Christie9c19a7d2008-05-21 15:54:09 -05001403static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001404{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001405 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001406 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001407
Mike Christie70b31c12009-08-20 15:11:03 -05001408 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1409 return -ENODATA;
1410
Mike Christie9c19a7d2008-05-21 15:54:09 -05001411 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001412 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001413 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001414 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001415 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001416 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001417 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001418 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001419 }
1420 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001421 return rc;
1422}
1423
Mike Christie7996a772006-04-06 21:13:41 -05001424/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001425 * iscsi_requeue_task - requeue task to run from session workqueue
1426 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001427 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001428 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001429 * this. The session lock must be held. This should only be called
1430 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001431 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001432void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001433{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001434 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001435
Mike Christie3bbaaad2009-05-13 17:57:46 -05001436 /*
1437 * this may be on the requeue list already if the xmit_task callout
1438 * is handling the r2ts while we are adding new ones
1439 */
1440 if (list_empty(&task->running))
1441 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001442 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001443}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001444EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001445
1446/**
Mike Christie7996a772006-04-06 21:13:41 -05001447 * iscsi_data_xmit - xmit any command into the scheduled connection
1448 * @conn: iscsi connection
1449 *
1450 * Notes:
1451 * The function can return -EAGAIN in which case the caller must
1452 * re-schedule it again later or recover. '0' return code means
1453 * successful xmit.
1454 **/
1455static int iscsi_data_xmit(struct iscsi_conn *conn)
1456{
Mike Christie5d12c052009-11-11 16:34:32 -06001457 struct iscsi_task *task;
Mike Christie3219e522006-05-30 00:37:28 -05001458 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001459
Mike Christie77a23c22007-05-30 12:57:18 -05001460 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001461 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001462 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001463 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001464 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001465 }
Mike Christie7996a772006-04-06 21:13:41 -05001466
Mike Christie9c19a7d2008-05-21 15:54:09 -05001467 if (conn->task) {
1468 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001469 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001470 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001471 }
1472
Mike Christie77a23c22007-05-30 12:57:18 -05001473 /*
1474 * process mgmt pdus like nops before commands since we should
1475 * only have one nop-out as a ping from us and targets should not
1476 * overflow us with nop-ins
1477 */
1478check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001479 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001480 conn->task = list_entry(conn->mgmtqueue.next,
1481 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001482 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001483 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1484 __iscsi_put_task(conn->task);
1485 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001486 continue;
1487 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001488 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001489 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001490 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001491 }
1492
Mike Christie843c0a82007-12-13 12:43:20 -06001493 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001494 while (!list_empty(&conn->cmdqueue)) {
Mike Christie5d12c052009-11-11 16:34:32 -06001495 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1496 running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001497 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001498 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001499 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001500 continue;
1501 }
Mike Christie577577d2008-12-02 00:32:05 -06001502 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1503 if (rc) {
Mike Christie5d12c052009-11-11 16:34:32 -06001504 if (rc == -ENOMEM || rc == -EACCES) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001505 list_add_tail(&conn->task->running,
1506 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001507 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001508 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001509 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001510 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001511 continue;
1512 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001513 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001514 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001515 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001516 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001517 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001518 * we need to check the mgmt queue for nops that need to
1519 * be sent to aviod starvation
1520 */
Mike Christie843c0a82007-12-13 12:43:20 -06001521 if (!list_empty(&conn->mgmtqueue))
1522 goto check_mgmt;
1523 }
1524
1525 while (!list_empty(&conn->requeue)) {
Mike Christieb3a7ea82007-12-13 12:43:26 -06001526 /*
1527 * we always do fastlogout - conn stop code will clean up.
1528 */
1529 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1530 break;
1531
Mike Christie5d12c052009-11-11 16:34:32 -06001532 task = list_entry(conn->requeue.next, struct iscsi_task,
1533 running);
1534 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1535 break;
1536
1537 conn->task = task;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001538 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001539 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001540 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001541 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001542 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001543 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001544 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001545 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001546 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001547 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001548
Mike Christie70b31c12009-08-20 15:11:03 -05001549done:
Mike Christie77a23c22007-05-30 12:57:18 -05001550 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001551 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001552}
1553
David Howellsc4028952006-11-22 14:57:56 +00001554static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001555{
David Howellsc4028952006-11-22 14:57:56 +00001556 struct iscsi_conn *conn =
1557 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001558 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001559 /*
1560 * serialize Xmit worker on a per-connection basis.
1561 */
Mike Christie3219e522006-05-30 00:37:28 -05001562 do {
1563 rc = iscsi_data_xmit(conn);
1564 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001565}
1566
Mike Christie577577d2008-12-02 00:32:05 -06001567static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1568 struct scsi_cmnd *sc)
1569{
1570 struct iscsi_task *task;
1571
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001572 if (!kfifo_out(&conn->session->cmdpool.queue,
Mike Christie577577d2008-12-02 00:32:05 -06001573 (void *) &task, sizeof(void *)))
1574 return NULL;
1575
1576 sc->SCp.phase = conn->session->age;
1577 sc->SCp.ptr = (char *) task;
1578
1579 atomic_set(&task->refcount, 1);
1580 task->state = ISCSI_TASK_PENDING;
1581 task->conn = conn;
1582 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001583 task->have_checked_conn = false;
1584 task->last_timeout = jiffies;
1585 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001586 INIT_LIST_HEAD(&task->running);
1587 return task;
1588}
1589
Mike Christie7996a772006-04-06 21:13:41 -05001590enum {
1591 FAILURE_BAD_HOST = 1,
1592 FAILURE_SESSION_FAILED,
1593 FAILURE_SESSION_FREED,
1594 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001595 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001596 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001597 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001598 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001599 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001600 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001601};
1602
Mike Christief41d4722010-12-31 02:22:21 -06001603int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001604{
Mike Christie75613522008-05-21 15:53:59 -05001605 struct iscsi_cls_session *cls_session;
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
Mike Christie7996a772006-04-06 21:13:41 -05001612 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001613 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001614
Mike Christie1336aed2009-05-13 17:57:48 -05001615 ihost = shost_priv(host);
Mike Christie7996a772006-04-06 21:13:41 -05001616
Mike Christie75613522008-05-21 15:53:59 -05001617 cls_session = starget_to_session(scsi_target(sc->device));
1618 session = cls_session->dd_data;
Mike Christief41d4722010-12-31 02:22:21 -06001619 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001620
Mike Christie75613522008-05-21 15:53:59 -05001621 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001622 if (reason) {
1623 sc->result = reason;
1624 goto fault;
1625 }
1626
Mike Christie301e0f72009-05-13 17:57:47 -05001627 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001628 /*
1629 * to handle the race between when we set the recovery state
1630 * and block the session we requeue here (commands could
1631 * be entering our queuecommand while a block is starting
1632 * up because the block code is not locked)
1633 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001634 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001635 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001636 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001637 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001638 sc->result = DID_IMM_RETRY << 16;
1639 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001640 case ISCSI_STATE_LOGGING_OUT:
1641 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001642 sc->result = DID_IMM_RETRY << 16;
1643 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001644 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001645 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001646 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001647 break;
1648 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001649 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001650 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001651 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001652 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001653 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001654 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001655 }
Mike Christie7996a772006-04-06 21:13:41 -05001656 goto fault;
1657 }
1658
Mike Christie7996a772006-04-06 21:13:41 -05001659 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001660 if (!conn) {
1661 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001662 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001663 goto fault;
1664 }
Mike Christie7996a772006-04-06 21:13:41 -05001665
Mike Christie661134a2009-09-05 07:35:33 +05301666 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1667 reason = FAILURE_SESSION_IN_RECOVERY;
1668 sc->result = DID_REQUEUE;
1669 goto fault;
1670 }
1671
Mike Christie77a23c22007-05-30 12:57:18 -05001672 if (iscsi_check_cmdsn_window_closed(conn)) {
1673 reason = FAILURE_WINDOW_CLOSED;
1674 goto reject;
1675 }
1676
Mike Christie577577d2008-12-02 00:32:05 -06001677 task = iscsi_alloc_task(conn, sc);
1678 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001679 reason = FAILURE_OOM;
1680 goto reject;
1681 }
Mike Christie7996a772006-04-06 21:13:41 -05001682
Mike Christie1336aed2009-05-13 17:57:48 -05001683 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001684 reason = iscsi_prep_scsi_cmd_pdu(task);
1685 if (reason) {
Mike Christie5d12c052009-11-11 16:34:32 -06001686 if (reason == -ENOMEM || reason == -EACCES) {
Mike Christie577577d2008-12-02 00:32:05 -06001687 reason = FAILURE_OOM;
1688 goto prepd_reject;
1689 } else {
1690 sc->result = DID_ABORT << 16;
1691 goto prepd_fault;
1692 }
Mike Christie052d0142008-05-21 15:54:05 -05001693 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001694 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001695 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001696 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001697 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001698 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001699 } else {
1700 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001701 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001702 }
Mike Christie052d0142008-05-21 15:54:05 -05001703
1704 session->queued_cmdsn++;
Mike Christief41d4722010-12-31 02:22:21 -06001705 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001706 return 0;
1707
Mike Christie577577d2008-12-02 00:32:05 -06001708prepd_reject:
Mike Christief41d4722010-12-31 02:22:21 -06001709 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
Mike Christie7996a772006-04-06 21:13:41 -05001710reject:
Mike Christief41d4722010-12-31 02:22:21 -06001711 spin_unlock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001712 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1713 sc->cmnd[0], reason);
Mike Christied6d13ee2008-08-17 15:24:43 -05001714 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001715
Mike Christie577577d2008-12-02 00:32:05 -06001716prepd_fault:
Mike Christief41d4722010-12-31 02:22:21 -06001717 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
Mike Christie7996a772006-04-06 21:13:41 -05001718fault:
Mike Christief41d4722010-12-31 02:22:21 -06001719 spin_unlock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001720 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1721 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001722 if (!scsi_bidi_cmnd(sc))
1723 scsi_set_resid(sc, scsi_bufflen(sc));
1724 else {
1725 scsi_out(sc)->resid = scsi_out(sc)->length;
1726 scsi_in(sc)->resid = scsi_in(sc)->length;
1727 }
Mike Christief41d4722010-12-31 02:22:21 -06001728 sc->scsi_done(sc);
Mike Christie7996a772006-04-06 21:13:41 -05001729 return 0;
1730}
1731EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1732
Mike Christiee881a172009-10-15 17:46:39 -07001733int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
Mike Christie7996a772006-04-06 21:13:41 -05001734{
Mike Christie1796e722009-11-11 16:34:36 -06001735 switch (reason) {
1736 case SCSI_QDEPTH_DEFAULT:
1737 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1738 break;
1739 case SCSI_QDEPTH_QFULL:
1740 scsi_track_queue_full(sdev, depth);
1741 break;
1742 case SCSI_QDEPTH_RAMP_UP:
1743 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1744 break;
1745 default:
Mike Christiee881a172009-10-15 17:46:39 -07001746 return -EOPNOTSUPP;
Mike Christie1796e722009-11-11 16:34:36 -06001747 }
Mike Christie7996a772006-04-06 21:13:41 -05001748 return sdev->queue_depth;
1749}
1750EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1751
Mike Christie6b5d6c42009-04-21 15:32:32 -05001752int iscsi_target_alloc(struct scsi_target *starget)
1753{
1754 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1755 struct iscsi_session *session = cls_session->dd_data;
1756
1757 starget->can_queue = session->scsi_cmds_max;
1758 return 0;
1759}
1760EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1761
Mike Christie843c0a82007-12-13 12:43:20 -06001762static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001763{
Mike Christie843c0a82007-12-13 12:43:20 -06001764 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001765 struct iscsi_session *session = conn->session;
1766
1767 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001768 if (conn->tmf_state == TMF_QUEUED) {
1769 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001770 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001771 /* unblock eh_abort() */
1772 wake_up(&conn->ehwait);
1773 }
1774 spin_unlock(&session->lock);
1775}
1776
Mike Christie9c19a7d2008-05-21 15:54:09 -05001777static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001778 struct iscsi_tm *hdr, int age,
1779 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001780{
Mike Christie7996a772006-04-06 21:13:41 -05001781 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001782 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001783
Mike Christie9c19a7d2008-05-21 15:54:09 -05001784 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001785 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001786 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001787 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06001788 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
Mike Christie7996a772006-04-06 21:13:41 -05001789 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001790 spin_lock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001791 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001792 }
Mike Christie843c0a82007-12-13 12:43:20 -06001793 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001794 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001795 conn->tmf_timer.function = iscsi_tmf_timedout;
1796 conn->tmf_timer.data = (unsigned long)conn;
1797 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001798 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001799
Mike Christie7996a772006-04-06 21:13:41 -05001800 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001801 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001802
1803 /*
1804 * block eh thread until:
1805 *
Mike Christie843c0a82007-12-13 12:43:20 -06001806 * 1) tmf response
1807 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001808 * 3) session is terminated or restarted or userspace has
1809 * given up on recovery
1810 */
Mike Christie843c0a82007-12-13 12:43:20 -06001811 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001812 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001813 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001814 if (signal_pending(current))
1815 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001816 del_timer_sync(&conn->tmf_timer);
1817
Mike Christie6724add2007-08-15 01:38:30 -05001818 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001819 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001820 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001821 if (age != session->age ||
1822 session->state != ISCSI_STATE_LOGGED_IN)
1823 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001824 return 0;
1825}
1826
1827/*
Mike Christie843c0a82007-12-13 12:43:20 -06001828 * Fail commands. session lock held and recv side suspended and xmit
1829 * thread flushed
1830 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001831static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1832 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001833{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001834 struct iscsi_task *task;
1835 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001836
Mike Christie3bbaaad2009-05-13 17:57:46 -05001837 for (i = 0; i < conn->session->cmds_max; i++) {
1838 task = conn->session->cmds[i];
1839 if (!task->sc || task->state == ISCSI_TASK_FREE)
1840 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001841
Mike Christie3bbaaad2009-05-13 17:57:46 -05001842 if (lun != -1 && lun != task->sc->device->lun)
1843 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001844
Mike Christie3bbaaad2009-05-13 17:57:46 -05001845 ISCSI_DBG_SESSION(conn->session,
1846 "failing sc %p itt 0x%x state %d\n",
1847 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001848 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001849 }
1850}
1851
Mike Christie661134a2009-09-05 07:35:33 +05301852/**
1853 * iscsi_suspend_queue - suspend iscsi_queuecommand
1854 * @conn: iscsi conn to stop queueing IO on
1855 *
1856 * This grabs the session lock to make sure no one is in
1857 * xmit_task/queuecommand, and then sets suspend to prevent
1858 * new commands from being queued. This only needs to be called
1859 * by offload drivers that need to sync a path like ep disconnect
1860 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1861 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1862 */
1863void iscsi_suspend_queue(struct iscsi_conn *conn)
1864{
1865 spin_lock_bh(&conn->session->lock);
1866 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1867 spin_unlock_bh(&conn->session->lock);
1868}
1869EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1870
1871/**
1872 * iscsi_suspend_tx - suspend iscsi_data_xmit
1873 * @conn: iscsi conn tp stop processing IO on.
1874 *
1875 * This function sets the suspend bit to prevent iscsi_data_xmit
1876 * from sending new IO, and if work is queued on the xmit thread
1877 * it will wait for it to be completed.
1878 */
Mike Christieb40977d2008-05-21 15:54:03 -05001879void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001880{
Mike Christie32ae7632009-03-05 14:46:03 -06001881 struct Scsi_Host *shost = conn->session->host;
1882 struct iscsi_host *ihost = shost_priv(shost);
1883
Mike Christie6724add2007-08-15 01:38:30 -05001884 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001885 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001886 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001887}
Mike Christieb40977d2008-05-21 15:54:03 -05001888EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001889
1890static void iscsi_start_tx(struct iscsi_conn *conn)
1891{
1892 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001893 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001894}
1895
Mike Christie4c48a822009-05-13 17:57:45 -05001896/*
1897 * We want to make sure a ping is in flight. It has timed out.
1898 * And we are not busy processing a pdu that is making
1899 * progress but got started before the ping and is taking a while
1900 * to complete so the ping is just stuck behind it in a queue.
1901 */
1902static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1903{
1904 if (conn->ping_task &&
1905 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1906 (conn->ping_timeout * HZ), jiffies))
1907 return 1;
1908 else
1909 return 0;
1910}
1911
Mike Christied355e572009-06-15 22:11:08 -05001912static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001913{
Mike Christied355e572009-06-15 22:11:08 -05001914 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christie92ed4d62010-02-10 16:51:45 -06001915 struct iscsi_task *task = NULL, *running_task;
Mike Christief6d51802007-12-13 12:43:30 -06001916 struct iscsi_cls_session *cls_session;
1917 struct iscsi_session *session;
1918 struct iscsi_conn *conn;
Mike Christie92ed4d62010-02-10 16:51:45 -06001919 int i;
Mike Christief6d51802007-12-13 12:43:30 -06001920
Mike Christied355e572009-06-15 22:11:08 -05001921 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001922 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001923
Erez Zilberbd2199d2009-06-15 22:11:10 -05001924 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001925
1926 spin_lock(&session->lock);
1927 if (session->state != ISCSI_STATE_LOGGED_IN) {
1928 /*
1929 * We are probably in the middle of iscsi recovery so let
1930 * that complete and handle the error.
1931 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001932 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001933 goto done;
1934 }
1935
1936 conn = session->leadconn;
1937 if (!conn) {
1938 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001939 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001940 goto done;
1941 }
1942
Mike Christied355e572009-06-15 22:11:08 -05001943 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie92ed4d62010-02-10 16:51:45 -06001944 if (!task) {
1945 /*
1946 * Raced with completion. Just reset timer, and let it
1947 * complete normally
1948 */
1949 rc = BLK_EH_RESET_TIMER;
Mike Christied355e572009-06-15 22:11:08 -05001950 goto done;
Mike Christie92ed4d62010-02-10 16:51:45 -06001951 }
1952
Mike Christied355e572009-06-15 22:11:08 -05001953 /*
1954 * If we have sent (at least queued to the network layer) a pdu or
1955 * recvd one for the task since the last timeout ask for
1956 * more time. If on the next timeout we have not made progress
1957 * we can check if it is the task or connection when we send the
1958 * nop as a ping.
1959 */
Mike Christie92ed4d62010-02-10 16:51:45 -06001960 if (time_after(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001961 ISCSI_DBG_EH(session, "Command making progress. Asking "
1962 "scsi-ml for more time to complete. "
Mike Christie92ed4d62010-02-10 16:51:45 -06001963 "Last data xfer at %lu. Last timeout was at "
Erez Zilberbd2199d2009-06-15 22:11:10 -05001964 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001965 task->have_checked_conn = false;
1966 rc = BLK_EH_RESET_TIMER;
1967 goto done;
1968 }
1969
Mike Christief6d51802007-12-13 12:43:30 -06001970 if (!conn->recv_timeout && !conn->ping_timeout)
1971 goto done;
1972 /*
1973 * if the ping timedout then we are in the middle of cleaning up
1974 * and can let the iscsi eh handle it
1975 */
Mike Christie4c48a822009-05-13 17:57:45 -05001976 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001977 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001978 goto done;
1979 }
Mike Christied355e572009-06-15 22:11:08 -05001980
Mike Christie92ed4d62010-02-10 16:51:45 -06001981 for (i = 0; i < conn->session->cmds_max; i++) {
1982 running_task = conn->session->cmds[i];
1983 if (!running_task->sc || running_task == task ||
1984 running_task->state != ISCSI_TASK_RUNNING)
1985 continue;
1986
1987 /*
1988 * Only check if cmds started before this one have made
1989 * progress, or this could never fail
1990 */
1991 if (time_after(running_task->sc->jiffies_at_alloc,
1992 task->sc->jiffies_at_alloc))
1993 continue;
1994
1995 if (time_after(running_task->last_xfer, task->last_timeout)) {
1996 /*
1997 * This task has not made progress, but a task
1998 * started before us has transferred data since
1999 * we started/last-checked. We could be queueing
2000 * too many tasks or the LU is bad.
2001 *
2002 * If the device is bad the cmds ahead of us on
2003 * other devs will complete, and this loop will
2004 * eventually fail starting the scsi eh.
2005 */
2006 ISCSI_DBG_EH(session, "Command has not made progress "
2007 "but commands ahead of it have. "
2008 "Asking scsi-ml for more time to "
2009 "complete. Our last xfer vs running task "
2010 "last xfer %lu/%lu. Last check %lu.\n",
2011 task->last_xfer, running_task->last_xfer,
2012 task->last_timeout);
2013 rc = BLK_EH_RESET_TIMER;
2014 goto done;
2015 }
2016 }
2017
Mike Christied355e572009-06-15 22:11:08 -05002018 /* Assumes nop timeout is shorter than scsi cmd timeout */
2019 if (task->have_checked_conn)
2020 goto done;
2021
Mike Christief6d51802007-12-13 12:43:30 -06002022 /*
Mike Christied355e572009-06-15 22:11:08 -05002023 * Checking the transport already or nop from a cmd timeout still
2024 * running
Mike Christief6d51802007-12-13 12:43:30 -06002025 */
Mike Christied355e572009-06-15 22:11:08 -05002026 if (conn->ping_task) {
2027 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07002028 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05002029 goto done;
2030 }
2031
Mike Christied355e572009-06-15 22:11:08 -05002032 /* Make sure there is a transport check done */
2033 iscsi_send_nopout(conn, NULL);
2034 task->have_checked_conn = true;
2035 rc = BLK_EH_RESET_TIMER;
2036
Mike Christief6d51802007-12-13 12:43:30 -06002037done:
Mike Christied355e572009-06-15 22:11:08 -05002038 if (task)
2039 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06002040 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002041 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2042 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06002043 return rc;
2044}
2045
2046static void iscsi_check_transport_timeouts(unsigned long data)
2047{
2048 struct iscsi_conn *conn = (struct iscsi_conn *)data;
2049 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05002050 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06002051
2052 spin_lock(&session->lock);
2053 if (session->state != ISCSI_STATE_LOGGED_IN)
2054 goto done;
2055
Mike Christie4cf10432008-05-07 20:43:52 -05002056 recv_timeout = conn->recv_timeout;
2057 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06002058 goto done;
2059
Mike Christie4cf10432008-05-07 20:43:52 -05002060 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06002061 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05002062
2063 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06002064 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05002065 "expired, recv timeout %d, last rx %lu, "
2066 "last ping %lu, now %lu\n",
2067 conn->ping_timeout, conn->recv_timeout,
2068 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06002069 spin_unlock(&session->lock);
2070 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2071 return;
2072 }
2073
Mike Christie4cf10432008-05-07 20:43:52 -05002074 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05002075 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06002076 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05002077 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05002078 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06002079 } else
Mike Christie4cf10432008-05-07 20:43:52 -05002080 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06002081
Mike Christie1b2c7af2009-03-05 14:45:58 -06002082 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06002083 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06002084done:
2085 spin_unlock(&session->lock);
2086}
2087
Mike Christie9c19a7d2008-05-21 15:54:09 -05002088static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06002089 struct iscsi_tm *hdr)
2090{
2091 memset(hdr, 0, sizeof(*hdr));
2092 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2093 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2094 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06002095 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2096 hdr->rtt = task->hdr_itt;
2097 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06002098}
2099
Mike Christie7996a772006-04-06 21:13:41 -05002100int iscsi_eh_abort(struct scsi_cmnd *sc)
2101{
Mike Christie75613522008-05-21 15:53:59 -05002102 struct iscsi_cls_session *cls_session;
2103 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04002104 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002105 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06002106 struct iscsi_tm *hdr;
2107 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05002108
Mike Christie75613522008-05-21 15:53:59 -05002109 cls_session = starget_to_session(scsi_target(sc->device));
2110 session = cls_session->dd_data;
2111
Erez Zilberbd2199d2009-06-15 22:11:10 -05002112 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05002113
Mike Christie6724add2007-08-15 01:38:30 -05002114 mutex_lock(&session->eh_mutex);
2115 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04002116 /*
2117 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2118 * got the command.
2119 */
2120 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002121 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2122 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05002123 spin_unlock_bh(&session->lock);
2124 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04002125 return SUCCESS;
2126 }
2127
Mike Christie7996a772006-04-06 21:13:41 -05002128 /*
2129 * If we are not logged in or we have started a new session
2130 * then let the host reset code handle this
2131 */
Mike Christie843c0a82007-12-13 12:43:20 -06002132 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2133 sc->SCp.phase != session->age) {
2134 spin_unlock_bh(&session->lock);
2135 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002136 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05002137 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06002138 return FAILED;
2139 }
2140
2141 conn = session->leadconn;
2142 conn->eh_abort_cnt++;
2143 age = session->age;
2144
Mike Christie9c19a7d2008-05-21 15:54:09 -05002145 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002146 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2147 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002148
Mike Christie9c19a7d2008-05-21 15:54:09 -05002149 /* task completed before time out */
2150 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002151 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002152 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002153 }
Mike Christie7996a772006-04-06 21:13:41 -05002154
Mike Christie9c19a7d2008-05-21 15:54:09 -05002155 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002156 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002157 goto success;
2158 }
Mike Christie7996a772006-04-06 21:13:41 -05002159
Mike Christie843c0a82007-12-13 12:43:20 -06002160 /* only have one tmf outstanding at a time */
2161 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002162 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002163 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002164
Mike Christie843c0a82007-12-13 12:43:20 -06002165 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002166 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002167
Mike Christie9c19a7d2008-05-21 15:54:09 -05002168 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002169 rc = FAILED;
2170 goto failed;
2171 }
2172
2173 switch (conn->tmf_state) {
2174 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002175 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002176 /*
2177 * stop tx side incase the target had sent a abort rsp but
2178 * the initiator was still writing out data.
2179 */
Mike Christie6724add2007-08-15 01:38:30 -05002180 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002181 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002182 * we do not stop the recv side because targets have been
2183 * good and have never sent us a successful tmf response
2184 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002185 */
Mike Christie6187c242009-07-15 15:02:57 -05002186 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002187 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002188 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002189 memset(hdr, 0, sizeof(*hdr));
Mike Christie6187c242009-07-15 15:02:57 -05002190 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002191 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002192 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002193 case TMF_TIMEDOUT:
2194 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002195 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie843c0a82007-12-13 12:43:20 -06002196 goto failed_unlocked;
2197 case TMF_NOT_FOUND:
2198 if (!sc->SCp.ptr) {
2199 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002200 memset(hdr, 0, sizeof(*hdr));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002201 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002202 ISCSI_DBG_EH(session, "sc completed while abort in "
2203 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002204 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002205 }
2206 /* fall through */
2207 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002208 conn->tmf_state = TMF_INITIAL;
2209 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002210 }
2211
Mike Christie77a23c22007-05-30 12:57:18 -05002212success:
Mike Christie7996a772006-04-06 21:13:41 -05002213 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002214success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002215 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2216 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002217 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002218 return SUCCESS;
2219
2220failed:
2221 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002222failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002223 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2224 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002225 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002226 return FAILED;
2227}
2228EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2229
Mike Christie843c0a82007-12-13 12:43:20 -06002230static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2231{
2232 memset(hdr, 0, sizeof(*hdr));
2233 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2234 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2235 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2236 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002237 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002238}
2239
2240int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2241{
Mike Christie75613522008-05-21 15:53:59 -05002242 struct iscsi_cls_session *cls_session;
2243 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002244 struct iscsi_conn *conn;
2245 struct iscsi_tm *hdr;
2246 int rc = FAILED;
2247
Mike Christie75613522008-05-21 15:53:59 -05002248 cls_session = starget_to_session(scsi_target(sc->device));
2249 session = cls_session->dd_data;
2250
Erez Zilberbd2199d2009-06-15 22:11:10 -05002251 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002252
2253 mutex_lock(&session->eh_mutex);
2254 spin_lock_bh(&session->lock);
2255 /*
2256 * Just check if we are not logged in. We cannot check for
2257 * the phase because the reset could come from a ioctl.
2258 */
2259 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2260 goto unlock;
2261 conn = session->leadconn;
2262
2263 /* only have one tmf outstanding at a time */
2264 if (conn->tmf_state != TMF_INITIAL)
2265 goto unlock;
2266 conn->tmf_state = TMF_QUEUED;
2267
2268 hdr = &conn->tmhdr;
2269 iscsi_prep_lun_reset_pdu(sc, hdr);
2270
Mike Christie9c19a7d2008-05-21 15:54:09 -05002271 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002272 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002273 rc = FAILED;
2274 goto unlock;
2275 }
2276
2277 switch (conn->tmf_state) {
2278 case TMF_SUCCESS:
2279 break;
2280 case TMF_TIMEDOUT:
2281 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002282 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie843c0a82007-12-13 12:43:20 -06002283 goto done;
2284 default:
2285 conn->tmf_state = TMF_INITIAL;
2286 goto unlock;
2287 }
2288
2289 rc = SUCCESS;
2290 spin_unlock_bh(&session->lock);
2291
2292 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002293
Mike Christiea3439142008-09-24 11:46:15 -05002294 spin_lock_bh(&session->lock);
Mike Christie5d12c052009-11-11 16:34:32 -06002295 memset(hdr, 0, sizeof(*hdr));
Mike Christie3bbaaad2009-05-13 17:57:46 -05002296 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002297 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002298 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002299
2300 iscsi_start_tx(conn);
2301 goto done;
2302
2303unlock:
2304 spin_unlock_bh(&session->lock);
2305done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002306 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2307 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002308 mutex_unlock(&session->eh_mutex);
2309 return rc;
2310}
2311EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2312
Mike Christie3fe5ae82009-11-11 16:34:33 -06002313void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2314{
2315 struct iscsi_session *session = cls_session->dd_data;
2316
2317 spin_lock_bh(&session->lock);
2318 if (session->state != ISCSI_STATE_LOGGED_IN) {
2319 session->state = ISCSI_STATE_RECOVERY_FAILED;
2320 if (session->leadconn)
2321 wake_up(&session->leadconn->ehwait);
2322 }
2323 spin_unlock_bh(&session->lock);
2324}
2325EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2326
2327/**
2328 * iscsi_eh_session_reset - drop session and attempt relogin
2329 * @sc: scsi command
2330 *
2331 * This function will wait for a relogin, session termination from
2332 * userspace, or a recovery/replacement timeout.
2333 */
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302334int iscsi_eh_session_reset(struct scsi_cmnd *sc)
Mike Christie3fe5ae82009-11-11 16:34:33 -06002335{
2336 struct iscsi_cls_session *cls_session;
2337 struct iscsi_session *session;
2338 struct iscsi_conn *conn;
2339
2340 cls_session = starget_to_session(scsi_target(sc->device));
2341 session = cls_session->dd_data;
2342 conn = session->leadconn;
2343
2344 mutex_lock(&session->eh_mutex);
2345 spin_lock_bh(&session->lock);
2346 if (session->state == ISCSI_STATE_TERMINATE) {
2347failed:
2348 ISCSI_DBG_EH(session,
2349 "failing session reset: Could not log back into "
2350 "%s, %s [age %d]\n", session->targetname,
2351 conn->persistent_address, session->age);
2352 spin_unlock_bh(&session->lock);
2353 mutex_unlock(&session->eh_mutex);
2354 return FAILED;
2355 }
2356
2357 spin_unlock_bh(&session->lock);
2358 mutex_unlock(&session->eh_mutex);
2359 /*
2360 * we drop the lock here but the leadconn cannot be destoyed while
2361 * we are in the scsi eh
2362 */
Mike Christiedf4da5c2010-12-31 02:22:18 -06002363 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002364
2365 ISCSI_DBG_EH(session, "wait for relogin\n");
2366 wait_event_interruptible(conn->ehwait,
2367 session->state == ISCSI_STATE_TERMINATE ||
2368 session->state == ISCSI_STATE_LOGGED_IN ||
2369 session->state == ISCSI_STATE_RECOVERY_FAILED);
2370 if (signal_pending(current))
2371 flush_signals(current);
2372
2373 mutex_lock(&session->eh_mutex);
2374 spin_lock_bh(&session->lock);
2375 if (session->state == ISCSI_STATE_LOGGED_IN) {
2376 ISCSI_DBG_EH(session,
2377 "session reset succeeded for %s,%s\n",
2378 session->targetname, conn->persistent_address);
2379 } else
2380 goto failed;
2381 spin_unlock_bh(&session->lock);
2382 mutex_unlock(&session->eh_mutex);
2383 return SUCCESS;
2384}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302385EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002386
2387static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2388{
2389 memset(hdr, 0, sizeof(*hdr));
2390 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2391 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2392 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2393 hdr->rtt = RESERVED_ITT;
2394}
2395
2396/**
2397 * iscsi_eh_target_reset - reset target
2398 * @sc: scsi command
2399 *
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302400 * This will attempt to send a warm target reset.
Mike Christie3fe5ae82009-11-11 16:34:33 -06002401 */
2402int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2403{
2404 struct iscsi_cls_session *cls_session;
2405 struct iscsi_session *session;
2406 struct iscsi_conn *conn;
2407 struct iscsi_tm *hdr;
2408 int rc = FAILED;
2409
2410 cls_session = starget_to_session(scsi_target(sc->device));
2411 session = cls_session->dd_data;
2412
2413 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2414 session->targetname);
2415
2416 mutex_lock(&session->eh_mutex);
2417 spin_lock_bh(&session->lock);
2418 /*
2419 * Just check if we are not logged in. We cannot check for
2420 * the phase because the reset could come from a ioctl.
2421 */
2422 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2423 goto unlock;
2424 conn = session->leadconn;
2425
2426 /* only have one tmf outstanding at a time */
2427 if (conn->tmf_state != TMF_INITIAL)
2428 goto unlock;
2429 conn->tmf_state = TMF_QUEUED;
2430
2431 hdr = &conn->tmhdr;
2432 iscsi_prep_tgt_reset_pdu(sc, hdr);
2433
2434 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2435 session->tgt_reset_timeout)) {
2436 rc = FAILED;
2437 goto unlock;
2438 }
2439
2440 switch (conn->tmf_state) {
2441 case TMF_SUCCESS:
2442 break;
2443 case TMF_TIMEDOUT:
2444 spin_unlock_bh(&session->lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002445 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002446 goto done;
2447 default:
2448 conn->tmf_state = TMF_INITIAL;
2449 goto unlock;
2450 }
2451
2452 rc = SUCCESS;
2453 spin_unlock_bh(&session->lock);
2454
2455 iscsi_suspend_tx(conn);
2456
2457 spin_lock_bh(&session->lock);
2458 memset(hdr, 0, sizeof(*hdr));
2459 fail_scsi_tasks(conn, -1, DID_ERROR);
2460 conn->tmf_state = TMF_INITIAL;
2461 spin_unlock_bh(&session->lock);
2462
2463 iscsi_start_tx(conn);
2464 goto done;
2465
2466unlock:
2467 spin_unlock_bh(&session->lock);
2468done:
2469 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2470 rc == SUCCESS ? "SUCCESS" : "FAILED");
2471 mutex_unlock(&session->eh_mutex);
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302472 return rc;
2473}
2474EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002475
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302476/**
2477 * iscsi_eh_recover_target - reset target and possibly the session
2478 * @sc: scsi command
2479 *
2480 * This will attempt to send a warm target reset. If that fails,
2481 * we will escalate to ERL0 session recovery.
2482 */
2483int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2484{
2485 int rc;
2486
2487 rc = iscsi_eh_target_reset(sc);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002488 if (rc == FAILED)
2489 rc = iscsi_eh_session_reset(sc);
2490 return rc;
2491}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302492EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002493
Olaf Kirch63203772007-12-13 12:43:25 -06002494/*
2495 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2496 * should be accessed via kfifo_{get,put} on q->queue.
2497 * Optionally, the caller can obtain the array of object pointers
2498 * by passing in a non-NULL @items pointer
2499 */
Mike Christie7996a772006-04-06 21:13:41 -05002500int
Olaf Kirch63203772007-12-13 12:43:25 -06002501iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002502{
Olaf Kirch63203772007-12-13 12:43:25 -06002503 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002504
Olaf Kirch63203772007-12-13 12:43:25 -06002505 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002506
2507 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002508
2509 /* If the user passed an items pointer, he wants a copy of
2510 * the array. */
2511 if (items)
2512 num_arrays++;
2513 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2514 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002515 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002516
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002517 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002518
2519 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002520 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002521 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002522 q->max = i;
2523 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002524 }
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002525 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002526 }
Olaf Kirch63203772007-12-13 12:43:25 -06002527
2528 if (items) {
2529 *items = q->pool + max;
2530 memcpy(*items, q->pool, max * sizeof(void *));
2531 }
2532
Mike Christie7996a772006-04-06 21:13:41 -05002533 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002534
2535enomem:
2536 iscsi_pool_free(q);
2537 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002538}
2539EXPORT_SYMBOL_GPL(iscsi_pool_init);
2540
Olaf Kirch63203772007-12-13 12:43:25 -06002541void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002542{
2543 int i;
2544
2545 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002546 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002547 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05002548}
2549EXPORT_SYMBOL_GPL(iscsi_pool_free);
2550
Mike Christiea4804cd2008-05-21 15:54:00 -05002551/**
2552 * iscsi_host_add - add host to system
2553 * @shost: scsi host
2554 * @pdev: parent device
2555 *
2556 * This should be called by partial offload and software iscsi drivers
2557 * to add a host to the system.
2558 */
2559int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002560{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002561 if (!shost->can_queue)
2562 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2563
Mike Christie4d108352009-03-05 14:46:04 -06002564 if (!shost->cmd_per_lun)
2565 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2566
Mike Christie308cec12009-02-06 12:06:20 -06002567 if (!shost->transportt->eh_timed_out)
2568 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002569 return scsi_add_host(shost, pdev);
2570}
2571EXPORT_SYMBOL_GPL(iscsi_host_add);
2572
2573/**
2574 * iscsi_host_alloc - allocate a host and driver data
2575 * @sht: scsi host template
2576 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002577 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002578 *
2579 * This should be called by partial offload and software iscsi drivers.
2580 * To access the driver specific memory use the iscsi_host_priv() macro.
2581 */
2582struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002583 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002584{
2585 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002586 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002587
2588 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2589 if (!shost)
2590 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002591 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002592
2593 if (xmit_can_sleep) {
2594 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2595 "iscsi_q_%d", shost->host_no);
2596 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2597 if (!ihost->workq)
2598 goto free_host;
2599 }
2600
Mike Christiee5bd7b52008-09-24 11:46:10 -05002601 spin_lock_init(&ihost->lock);
2602 ihost->state = ISCSI_HOST_SETUP;
2603 ihost->num_sessions = 0;
2604 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002605 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002606
2607free_host:
2608 scsi_host_put(shost);
2609 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002610}
Mike Christiea4804cd2008-05-21 15:54:00 -05002611EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002612
Mike Christiee5bd7b52008-09-24 11:46:10 -05002613static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2614{
Mike Christie40a06e72009-03-05 14:46:05 -06002615 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002616}
2617
Mike Christiea4804cd2008-05-21 15:54:00 -05002618/**
2619 * iscsi_host_remove - remove host and sessions
2620 * @shost: scsi host
2621 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002622 * If there are any sessions left, this will initiate the removal and wait
2623 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002624 */
2625void iscsi_host_remove(struct Scsi_Host *shost)
2626{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002627 struct iscsi_host *ihost = shost_priv(shost);
2628 unsigned long flags;
2629
2630 spin_lock_irqsave(&ihost->lock, flags);
2631 ihost->state = ISCSI_HOST_REMOVED;
2632 spin_unlock_irqrestore(&ihost->lock, flags);
2633
2634 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2635 wait_event_interruptible(ihost->session_removal_wq,
2636 ihost->num_sessions == 0);
2637 if (signal_pending(current))
2638 flush_signals(current);
2639
Mike Christiea4804cd2008-05-21 15:54:00 -05002640 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002641 if (ihost->workq)
2642 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002643}
2644EXPORT_SYMBOL_GPL(iscsi_host_remove);
2645
2646void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002647{
2648 struct iscsi_host *ihost = shost_priv(shost);
2649
2650 kfree(ihost->netdev);
2651 kfree(ihost->hwaddress);
2652 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002653 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002654}
Mike Christiea4804cd2008-05-21 15:54:00 -05002655EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002656
Mike Christiee5bd7b52008-09-24 11:46:10 -05002657static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2658{
2659 struct iscsi_host *ihost = shost_priv(shost);
2660 unsigned long flags;
2661
2662 shost = scsi_host_get(shost);
2663 if (!shost) {
2664 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2665 "of session teardown event because host already "
2666 "removed.\n");
2667 return;
2668 }
2669
2670 spin_lock_irqsave(&ihost->lock, flags);
2671 ihost->num_sessions--;
2672 if (ihost->num_sessions == 0)
2673 wake_up(&ihost->session_removal_wq);
2674 spin_unlock_irqrestore(&ihost->lock, flags);
2675 scsi_host_put(shost);
2676}
2677
Mike Christie75613522008-05-21 15:53:59 -05002678/**
2679 * iscsi_session_setup - create iscsi cls session and host and session
2680 * @iscsit: iscsi transport template
2681 * @shost: scsi host
2682 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002683 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002684 * @initial_cmdsn: initial CmdSN
2685 *
2686 * This can be used by software iscsi_transports that allocate
2687 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002688 *
2689 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2690 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2691 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002692 */
2693struct iscsi_cls_session *
2694iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302695 uint16_t cmds_max, int dd_size, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002696 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002697{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002698 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002699 struct iscsi_session *session;
2700 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002701 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002702 unsigned long flags;
2703
2704 spin_lock_irqsave(&ihost->lock, flags);
2705 if (ihost->state == ISCSI_HOST_REMOVED) {
2706 spin_unlock_irqrestore(&ihost->lock, flags);
2707 return NULL;
2708 }
2709 ihost->num_sessions++;
2710 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002711
2712 if (!total_cmds)
2713 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002714 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002715 * The iscsi layer needs some tasks for nop handling and tmfs,
2716 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2717 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002718 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002719 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2720 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2721 "must be a power of two that is at least %d.\n",
2722 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002723 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002724 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002725
2726 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2727 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2728 "must be a power of 2 less than or equal to %d.\n",
2729 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2730 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2731 }
2732
2733 if (!is_power_of_2(total_cmds)) {
2734 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2735 "must be a power of 2.\n", total_cmds);
2736 total_cmds = rounddown_pow_of_two(total_cmds);
2737 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2738 return NULL;
2739 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2740 total_cmds);
2741 }
2742 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002743
Mike Christie5d91e202008-05-21 15:54:01 -05002744 cls_session = iscsi_alloc_session(shost, iscsit,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302745 sizeof(struct iscsi_session) +
2746 dd_size);
Mike Christie75613522008-05-21 15:53:59 -05002747 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002748 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002749 session = cls_session->dd_data;
2750 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002751 session->host = shost;
2752 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002753 session->fast_abort = 1;
Mike Christie3fe5ae82009-11-11 16:34:33 -06002754 session->tgt_reset_timeout = 30;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002755 session->lu_reset_timeout = 15;
2756 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002757 session->scsi_cmds_max = scsi_cmds;
2758 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002759 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002760 session->exp_cmdsn = initial_cmdsn + 1;
2761 session->max_cmdsn = initial_cmdsn + 1;
2762 session->max_r2t = 1;
2763 session->tt = iscsit;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302764 session->dd_data = cls_session->dd_data + sizeof(*session);
Mike Christie6724add2007-08-15 01:38:30 -05002765 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002766 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002767
2768 /* initialize SCSI PDU commands pool */
2769 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2770 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002771 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002772 goto cmdpool_alloc_fail;
2773
2774 /* pre-format cmds pool with ITT */
2775 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002776 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002777
Mike Christie9c19a7d2008-05-21 15:54:09 -05002778 if (cmd_task_size)
2779 task->dd_data = &task[1];
2780 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002781 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002782 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002783 }
2784
Mike Christief53a88d2006-06-28 12:00:27 -05002785 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002786 goto module_get_fail;
2787
Mike Christie79706342008-05-21 15:54:12 -05002788 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002789 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002790
Mike Christie7996a772006-04-06 21:13:41 -05002791 return cls_session;
2792
2793cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002794 module_put(iscsit->owner);
2795module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002796 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002797cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002798 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002799dec_session_count:
2800 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002801 return NULL;
2802}
2803EXPORT_SYMBOL_GPL(iscsi_session_setup);
2804
2805/**
2806 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002807 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002808 *
Mike Christie75613522008-05-21 15:53:59 -05002809 * The driver must have called iscsi_remove_session before
2810 * calling this.
2811 */
Mike Christie7996a772006-04-06 21:13:41 -05002812void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2813{
Mike Christie75613522008-05-21 15:53:59 -05002814 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002815 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002816 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002817
Olaf Kirch63203772007-12-13 12:43:25 -06002818 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002819
Mike Christieb2c64162007-05-30 12:57:16 -05002820 kfree(session->password);
2821 kfree(session->password_in);
2822 kfree(session->username);
2823 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002824 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002825 kfree(session->initiatorname);
2826 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002827
Mike Christie75613522008-05-21 15:53:59 -05002828 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002829 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002830 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002831}
2832EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2833
2834/**
2835 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2836 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002837 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002838 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002839 */
Mike Christie7996a772006-04-06 21:13:41 -05002840struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002841iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2842 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002843{
Mike Christie75613522008-05-21 15:53:59 -05002844 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002845 struct iscsi_conn *conn;
2846 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002847 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002848
Mike Christie5d91e202008-05-21 15:54:01 -05002849 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2850 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002851 if (!cls_conn)
2852 return NULL;
2853 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002854 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002855
Mike Christie5d91e202008-05-21 15:54:01 -05002856 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002857 conn->session = session;
2858 conn->cls_conn = cls_conn;
2859 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2860 conn->id = conn_idx;
2861 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002862 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002863
2864 init_timer(&conn->transport_timer);
2865 conn->transport_timer.data = (unsigned long)conn;
2866 conn->transport_timer.function = iscsi_check_transport_timeouts;
2867
Mike Christie843c0a82007-12-13 12:43:20 -06002868 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002869 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002870 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002871 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002872
Mike Christie9c19a7d2008-05-21 15:54:09 -05002873 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002874 spin_lock_bh(&session->lock);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002875 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002876 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002877 sizeof(void*))) {
2878 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002879 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002880 }
2881 spin_unlock_bh(&session->lock);
2882
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002883 data = (char *) __get_free_pages(GFP_KERNEL,
2884 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002885 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002886 goto login_task_data_alloc_fail;
2887 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002888
Mike Christie843c0a82007-12-13 12:43:20 -06002889 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002890 init_waitqueue_head(&conn->ehwait);
2891
2892 return cls_conn;
2893
Mike Christie9c19a7d2008-05-21 15:54:09 -05002894login_task_data_alloc_fail:
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002895 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002896 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002897login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002898 iscsi_destroy_conn(cls_conn);
2899 return NULL;
2900}
2901EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2902
2903/**
2904 * iscsi_conn_teardown - teardown iscsi connection
2905 * cls_conn: iscsi class connection
2906 *
2907 * TODO: we may need to make this into a two step process
2908 * like scsi-mls remove + put host
2909 */
2910void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2911{
2912 struct iscsi_conn *conn = cls_conn->dd_data;
2913 struct iscsi_session *session = conn->session;
2914 unsigned long flags;
2915
Mike Christief6d51802007-12-13 12:43:30 -06002916 del_timer_sync(&conn->transport_timer);
2917
Mike Christie7996a772006-04-06 21:13:41 -05002918 spin_lock_bh(&session->lock);
2919 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2920 if (session->leadconn == conn) {
2921 /*
2922 * leading connection? then give up on recovery.
2923 */
2924 session->state = ISCSI_STATE_TERMINATE;
2925 wake_up(&conn->ehwait);
2926 }
2927 spin_unlock_bh(&session->lock);
2928
Mike Christie7996a772006-04-06 21:13:41 -05002929 /*
2930 * Block until all in-progress commands for this connection
2931 * time out or fail.
2932 */
2933 for (;;) {
2934 spin_lock_irqsave(session->host->host_lock, flags);
2935 if (!session->host->host_busy) { /* OK for ERL == 0 */
2936 spin_unlock_irqrestore(session->host->host_lock, flags);
2937 break;
2938 }
2939 spin_unlock_irqrestore(session->host->host_lock, flags);
2940 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002941 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2942 "host_busy %d host_failed %d\n",
2943 session->host->host_busy,
2944 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002945 /*
2946 * force eh_abort() to unblock
2947 */
2948 wake_up(&conn->ehwait);
2949 }
2950
Mike Christie779ea122007-02-28 17:32:15 -06002951 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002952 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002953
Mike Christie7996a772006-04-06 21:13:41 -05002954 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002955 free_pages((unsigned long) conn->data,
2956 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002957 kfree(conn->persistent_address);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002958 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002959 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002960 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002961 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002962 spin_unlock_bh(&session->lock);
2963
Mike Christie7996a772006-04-06 21:13:41 -05002964 iscsi_destroy_conn(cls_conn);
2965}
2966EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2967
2968int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2969{
2970 struct iscsi_conn *conn = cls_conn->dd_data;
2971 struct iscsi_session *session = conn->session;
2972
Mike Christieffd04362006-08-31 18:09:24 -04002973 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002974 iscsi_conn_printk(KERN_ERR, conn,
2975 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002976 return -EPERM;
2977 }
2978
Mike Christiedb98ccd2006-08-31 18:09:31 -04002979 if ((session->imm_data_en || !session->initial_r2t_en) &&
2980 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002981 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2982 "first_burst %d max_burst %d\n",
2983 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002984 return -EINVAL;
2985 }
2986
Mike Christief6d51802007-12-13 12:43:30 -06002987 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002988 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2989 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002990 conn->recv_timeout = 5;
2991 }
2992
2993 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002994 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2995 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002996 conn->ping_timeout = 5;
2997 }
2998
Mike Christie7996a772006-04-06 21:13:41 -05002999 spin_lock_bh(&session->lock);
3000 conn->c_stage = ISCSI_CONN_STARTED;
3001 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05003002 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05003003
Mike Christief6d51802007-12-13 12:43:30 -06003004 conn->last_recv = jiffies;
3005 conn->last_ping = jiffies;
3006 if (conn->recv_timeout && conn->ping_timeout)
3007 mod_timer(&conn->transport_timer,
3008 jiffies + (conn->recv_timeout * HZ));
3009
Mike Christie7996a772006-04-06 21:13:41 -05003010 switch(conn->stop_stage) {
3011 case STOP_CONN_RECOVER:
3012 /*
3013 * unblock eh_abort() if it is blocked. re-try all
3014 * commands after successful recovery
3015 */
Mike Christie7996a772006-04-06 21:13:41 -05003016 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06003017 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05003018 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06003019 if (session->age == 16)
3020 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06003021 break;
Mike Christie7996a772006-04-06 21:13:41 -05003022 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05003023 conn->stop_stage = 0;
3024 break;
Mike Christie7996a772006-04-06 21:13:41 -05003025 default:
3026 break;
3027 }
3028 spin_unlock_bh(&session->lock);
3029
Mike Christie75613522008-05-21 15:53:59 -05003030 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06003031 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05003032 return 0;
3033}
3034EXPORT_SYMBOL_GPL(iscsi_conn_start);
3035
3036static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05003037fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05003038{
Mike Christie3bbaaad2009-05-13 17:57:46 -05003039 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05003040 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05003041
Mike Christie3bbaaad2009-05-13 17:57:46 -05003042 for (i = 0; i < conn->session->cmds_max; i++) {
3043 task = conn->session->cmds[i];
3044 if (task->sc)
3045 continue;
3046
3047 if (task->state == ISCSI_TASK_FREE)
3048 continue;
3049
3050 ISCSI_DBG_SESSION(conn->session,
3051 "failing mgmt itt 0x%x state %d\n",
3052 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05003053 state = ISCSI_TASK_ABRT_SESS_RECOV;
3054 if (task->state == ISCSI_TASK_PENDING)
3055 state = ISCSI_TASK_COMPLETED;
3056 iscsi_complete_task(task, state);
3057
Mike Christie7996a772006-04-06 21:13:41 -05003058 }
Mike Christie7996a772006-04-06 21:13:41 -05003059}
3060
Mike Christie656cffc2006-05-18 20:31:42 -05003061static void iscsi_start_session_recovery(struct iscsi_session *session,
3062 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05003063{
Mike Christieed2abc72006-05-02 19:46:40 -05003064 int old_stop_stage;
3065
Mike Christie6724add2007-08-15 01:38:30 -05003066 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003067 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003068 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05003069 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003070 mutex_unlock(&session->eh_mutex);
3071 return;
3072 }
3073
3074 /*
Mike Christieed2abc72006-05-02 19:46:40 -05003075 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05003076 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05003077 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05003078 */
Mike Christie67a61112006-05-30 00:37:20 -05003079 if (flag == STOP_CONN_TERM)
3080 session->state = ISCSI_STATE_TERMINATE;
3081 else if (conn->stop_stage != STOP_CONN_RECOVER)
3082 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie4ae0a6c2010-03-09 14:14:51 -06003083
3084 old_stop_stage = conn->stop_stage;
3085 conn->stop_stage = flag;
Mike Christie26013ad2009-05-13 17:57:43 -05003086 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003087
Mike Christie26013ad2009-05-13 17:57:43 -05003088 del_timer_sync(&conn->transport_timer);
3089 iscsi_suspend_tx(conn);
3090
3091 spin_lock_bh(&session->lock);
Mike Christie67a61112006-05-30 00:37:20 -05003092 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05003093 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003094
Mike Christie7996a772006-04-06 21:13:41 -05003095 /*
3096 * for connection level recovery we should not calculate
3097 * header digest. conn->hdr_size used for optimization
3098 * in hdr_extract() and will be re-negotiated at
3099 * set_param() time.
3100 */
3101 if (flag == STOP_CONN_RECOVER) {
3102 conn->hdrdgst_en = 0;
3103 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05003104 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05003105 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06003106 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05003107 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05003108 }
Mike Christie7996a772006-04-06 21:13:41 -05003109 }
Mike Christie656cffc2006-05-18 20:31:42 -05003110
Mike Christie656cffc2006-05-18 20:31:42 -05003111 /*
3112 * flush queues.
3113 */
3114 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05003115 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05003116 fail_mgmt_tasks(session, conn);
Mike Christie5d12c052009-11-11 16:34:32 -06003117 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
Mike Christie656cffc2006-05-18 20:31:42 -05003118 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003119 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003120}
Mike Christie7996a772006-04-06 21:13:41 -05003121
3122void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3123{
3124 struct iscsi_conn *conn = cls_conn->dd_data;
3125 struct iscsi_session *session = conn->session;
3126
3127 switch (flag) {
3128 case STOP_CONN_RECOVER:
3129 case STOP_CONN_TERM:
3130 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05003131 break;
Mike Christie7996a772006-04-06 21:13:41 -05003132 default:
Mike Christie322d7392008-01-31 13:36:52 -06003133 iscsi_conn_printk(KERN_ERR, conn,
3134 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05003135 }
3136}
3137EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3138
3139int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3140 struct iscsi_cls_conn *cls_conn, int is_leading)
3141{
Mike Christie75613522008-05-21 15:53:59 -05003142 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04003143 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05003144
Mike Christie7996a772006-04-06 21:13:41 -05003145 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003146 if (is_leading)
3147 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04003148 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003149
3150 /*
3151 * Unblock xmitworker(), Login Phase will pass through.
3152 */
3153 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3154 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3155 return 0;
3156}
3157EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3158
Mike Christie5700b1a2009-05-13 17:57:40 -05003159static int iscsi_switch_str_param(char **param, char *new_val_buf)
3160{
3161 char *new_val;
3162
3163 if (*param) {
3164 if (!strcmp(*param, new_val_buf))
3165 return 0;
3166 }
3167
3168 new_val = kstrdup(new_val_buf, GFP_NOIO);
3169 if (!new_val)
3170 return -ENOMEM;
3171
3172 kfree(*param);
3173 *param = new_val;
3174 return 0;
3175}
Mike Christiea54a52c2006-06-28 12:00:23 -05003176
3177int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3178 enum iscsi_param param, char *buf, int buflen)
3179{
3180 struct iscsi_conn *conn = cls_conn->dd_data;
3181 struct iscsi_session *session = conn->session;
3182 uint32_t value;
3183
3184 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003185 case ISCSI_PARAM_FAST_ABORT:
3186 sscanf(buf, "%d", &session->fast_abort);
3187 break;
Mike Christief6d51802007-12-13 12:43:30 -06003188 case ISCSI_PARAM_ABORT_TMO:
3189 sscanf(buf, "%d", &session->abort_timeout);
3190 break;
3191 case ISCSI_PARAM_LU_RESET_TMO:
3192 sscanf(buf, "%d", &session->lu_reset_timeout);
3193 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003194 case ISCSI_PARAM_TGT_RESET_TMO:
3195 sscanf(buf, "%d", &session->tgt_reset_timeout);
3196 break;
Mike Christief6d51802007-12-13 12:43:30 -06003197 case ISCSI_PARAM_PING_TMO:
3198 sscanf(buf, "%d", &conn->ping_timeout);
3199 break;
3200 case ISCSI_PARAM_RECV_TMO:
3201 sscanf(buf, "%d", &conn->recv_timeout);
3202 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003203 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3204 sscanf(buf, "%d", &conn->max_recv_dlength);
3205 break;
3206 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3207 sscanf(buf, "%d", &conn->max_xmit_dlength);
3208 break;
3209 case ISCSI_PARAM_HDRDGST_EN:
3210 sscanf(buf, "%d", &conn->hdrdgst_en);
3211 break;
3212 case ISCSI_PARAM_DATADGST_EN:
3213 sscanf(buf, "%d", &conn->datadgst_en);
3214 break;
3215 case ISCSI_PARAM_INITIAL_R2T_EN:
3216 sscanf(buf, "%d", &session->initial_r2t_en);
3217 break;
3218 case ISCSI_PARAM_MAX_R2T:
3219 sscanf(buf, "%d", &session->max_r2t);
3220 break;
3221 case ISCSI_PARAM_IMM_DATA_EN:
3222 sscanf(buf, "%d", &session->imm_data_en);
3223 break;
3224 case ISCSI_PARAM_FIRST_BURST:
3225 sscanf(buf, "%d", &session->first_burst);
3226 break;
3227 case ISCSI_PARAM_MAX_BURST:
3228 sscanf(buf, "%d", &session->max_burst);
3229 break;
3230 case ISCSI_PARAM_PDU_INORDER_EN:
3231 sscanf(buf, "%d", &session->pdu_inorder_en);
3232 break;
3233 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3234 sscanf(buf, "%d", &session->dataseq_inorder_en);
3235 break;
3236 case ISCSI_PARAM_ERL:
3237 sscanf(buf, "%d", &session->erl);
3238 break;
3239 case ISCSI_PARAM_IFMARKER_EN:
3240 sscanf(buf, "%d", &value);
3241 BUG_ON(value);
3242 break;
3243 case ISCSI_PARAM_OFMARKER_EN:
3244 sscanf(buf, "%d", &value);
3245 BUG_ON(value);
3246 break;
3247 case ISCSI_PARAM_EXP_STATSN:
3248 sscanf(buf, "%u", &conn->exp_statsn);
3249 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003250 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003251 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003252 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003253 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003254 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05003255 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003256 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003257 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003258 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003259 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003260 case ISCSI_PARAM_TPGT:
3261 sscanf(buf, "%d", &session->tpgt);
3262 break;
3263 case ISCSI_PARAM_PERSISTENT_PORT:
3264 sscanf(buf, "%d", &conn->persistent_port);
3265 break;
3266 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003267 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003268 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003269 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003270 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003271 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003272 default:
3273 return -ENOSYS;
3274 }
3275
3276 return 0;
3277}
3278EXPORT_SYMBOL_GPL(iscsi_set_param);
3279
3280int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3281 enum iscsi_param param, char *buf)
3282{
Mike Christie75613522008-05-21 15:53:59 -05003283 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003284 int len;
3285
3286 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003287 case ISCSI_PARAM_FAST_ABORT:
3288 len = sprintf(buf, "%d\n", session->fast_abort);
3289 break;
Mike Christief6d51802007-12-13 12:43:30 -06003290 case ISCSI_PARAM_ABORT_TMO:
3291 len = sprintf(buf, "%d\n", session->abort_timeout);
3292 break;
3293 case ISCSI_PARAM_LU_RESET_TMO:
3294 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3295 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003296 case ISCSI_PARAM_TGT_RESET_TMO:
3297 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3298 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003299 case ISCSI_PARAM_INITIAL_R2T_EN:
3300 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3301 break;
3302 case ISCSI_PARAM_MAX_R2T:
3303 len = sprintf(buf, "%hu\n", session->max_r2t);
3304 break;
3305 case ISCSI_PARAM_IMM_DATA_EN:
3306 len = sprintf(buf, "%d\n", session->imm_data_en);
3307 break;
3308 case ISCSI_PARAM_FIRST_BURST:
3309 len = sprintf(buf, "%u\n", session->first_burst);
3310 break;
3311 case ISCSI_PARAM_MAX_BURST:
3312 len = sprintf(buf, "%u\n", session->max_burst);
3313 break;
3314 case ISCSI_PARAM_PDU_INORDER_EN:
3315 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3316 break;
3317 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3318 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3319 break;
3320 case ISCSI_PARAM_ERL:
3321 len = sprintf(buf, "%d\n", session->erl);
3322 break;
3323 case ISCSI_PARAM_TARGET_NAME:
3324 len = sprintf(buf, "%s\n", session->targetname);
3325 break;
3326 case ISCSI_PARAM_TPGT:
3327 len = sprintf(buf, "%d\n", session->tpgt);
3328 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003329 case ISCSI_PARAM_USERNAME:
3330 len = sprintf(buf, "%s\n", session->username);
3331 break;
3332 case ISCSI_PARAM_USERNAME_IN:
3333 len = sprintf(buf, "%s\n", session->username_in);
3334 break;
3335 case ISCSI_PARAM_PASSWORD:
3336 len = sprintf(buf, "%s\n", session->password);
3337 break;
3338 case ISCSI_PARAM_PASSWORD_IN:
3339 len = sprintf(buf, "%s\n", session->password_in);
3340 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003341 case ISCSI_PARAM_IFACE_NAME:
3342 len = sprintf(buf, "%s\n", session->ifacename);
3343 break;
3344 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003345 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003346 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003347 default:
3348 return -ENOSYS;
3349 }
3350
3351 return len;
3352}
3353EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3354
Mike Christie00f37082011-02-16 15:04:35 -06003355int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3356 enum iscsi_param param, char *buf)
3357{
3358 struct sockaddr_in6 *sin6 = NULL;
3359 struct sockaddr_in *sin = NULL;
3360 int len;
3361
3362 switch (addr->ss_family) {
3363 case AF_INET:
3364 sin = (struct sockaddr_in *)addr;
3365 break;
3366 case AF_INET6:
3367 sin6 = (struct sockaddr_in6 *)addr;
3368 break;
3369 default:
3370 return -EINVAL;
3371 }
3372
3373 switch (param) {
3374 case ISCSI_PARAM_CONN_ADDRESS:
3375 case ISCSI_HOST_PARAM_IPADDRESS:
3376 if (sin)
3377 len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
3378 else
3379 len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
3380 break;
3381 case ISCSI_PARAM_CONN_PORT:
3382 if (sin)
3383 len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3384 else
3385 len = sprintf(buf, "%hu\n",
3386 be16_to_cpu(sin6->sin6_port));
3387 break;
3388 default:
3389 return -EINVAL;
3390 }
3391
3392 return len;
3393}
3394EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3395
Mike Christiea54a52c2006-06-28 12:00:23 -05003396int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3397 enum iscsi_param param, char *buf)
3398{
3399 struct iscsi_conn *conn = cls_conn->dd_data;
3400 int len;
3401
3402 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003403 case ISCSI_PARAM_PING_TMO:
3404 len = sprintf(buf, "%u\n", conn->ping_timeout);
3405 break;
3406 case ISCSI_PARAM_RECV_TMO:
3407 len = sprintf(buf, "%u\n", conn->recv_timeout);
3408 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003409 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3410 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3411 break;
3412 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3413 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3414 break;
3415 case ISCSI_PARAM_HDRDGST_EN:
3416 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3417 break;
3418 case ISCSI_PARAM_DATADGST_EN:
3419 len = sprintf(buf, "%d\n", conn->datadgst_en);
3420 break;
3421 case ISCSI_PARAM_IFMARKER_EN:
3422 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3423 break;
3424 case ISCSI_PARAM_OFMARKER_EN:
3425 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3426 break;
3427 case ISCSI_PARAM_EXP_STATSN:
3428 len = sprintf(buf, "%u\n", conn->exp_statsn);
3429 break;
3430 case ISCSI_PARAM_PERSISTENT_PORT:
3431 len = sprintf(buf, "%d\n", conn->persistent_port);
3432 break;
3433 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3434 len = sprintf(buf, "%s\n", conn->persistent_address);
3435 break;
3436 default:
3437 return -ENOSYS;
3438 }
3439
3440 return len;
3441}
3442EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3443
Mike Christie0801c242007-05-30 12:57:12 -05003444int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3445 char *buf)
3446{
Mike Christie75613522008-05-21 15:53:59 -05003447 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003448 int len;
3449
3450 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003451 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003452 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003453 break;
Mike Christie0801c242007-05-30 12:57:12 -05003454 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003455 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003456 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003457 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003458 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003459 break;
Mike Christie0801c242007-05-30 12:57:12 -05003460 default:
3461 return -ENOSYS;
3462 }
3463
3464 return len;
3465}
3466EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3467
3468int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3469 char *buf, int buflen)
3470{
Mike Christie75613522008-05-21 15:53:59 -05003471 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003472
3473 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003474 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003475 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003476 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003477 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003478 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003479 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003480 default:
3481 return -ENOSYS;
3482 }
3483
3484 return 0;
3485}
3486EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3487
Mike Christie7996a772006-04-06 21:13:41 -05003488MODULE_AUTHOR("Mike Christie");
3489MODULE_DESCRIPTION("iSCSI library functions");
3490MODULE_LICENSE("GPL");