blob: 8738b989a801d6365c9a7e87de8fbe8f3df927bf [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>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040029#include <linux/module.h>
Mike Christie8eb00532007-02-28 17:32:19 -060030#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050031#include <net/tcp.h>
32#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_eh.h>
35#include <scsi/scsi_tcq.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi.h>
38#include <scsi/iscsi_proto.h>
39#include <scsi/scsi_transport.h>
40#include <scsi/scsi_transport_iscsi.h>
41#include <scsi/libiscsi.h>
42
Erez Zilberbd2199d2009-06-15 22:11:10 -050043static int iscsi_dbg_lib_conn;
44module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
45 S_IRUGO | S_IWUSR);
46MODULE_PARM_DESC(debug_libiscsi_conn,
47 "Turn on debugging for connections in libiscsi module. "
48 "Set to 1 to turn on, and zero to turn off. Default is off.");
49
50static int iscsi_dbg_lib_session;
51module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
52 S_IRUGO | S_IWUSR);
53MODULE_PARM_DESC(debug_libiscsi_session,
54 "Turn on debugging for sessions in libiscsi module. "
55 "Set to 1 to turn on, and zero to turn off. Default is off.");
56
57static int iscsi_dbg_lib_eh;
58module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
59 S_IRUGO | S_IWUSR);
60MODULE_PARM_DESC(debug_libiscsi_eh,
61 "Turn on debugging for error handling in libiscsi module. "
62 "Set to 1 to turn on, and zero to turn off. Default is off.");
Mike Christie1b2c7af2009-03-05 14:45:58 -060063
64#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
65 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050066 if (iscsi_dbg_lib_conn) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060067 iscsi_conn_printk(KERN_INFO, _conn, \
68 "%s " dbg_fmt, \
69 __func__, ##arg); \
70 } while (0);
71
72#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
73 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050074 if (iscsi_dbg_lib_session) \
75 iscsi_session_printk(KERN_INFO, _session, \
76 "%s " dbg_fmt, \
77 __func__, ##arg); \
78 } while (0);
79
80#define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
81 do { \
82 if (iscsi_dbg_lib_eh) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060083 iscsi_session_printk(KERN_INFO, _session, \
84 "%s " dbg_fmt, \
85 __func__, ##arg); \
86 } while (0);
87
Mike Christie32ae7632009-03-05 14:46:03 -060088inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
89{
90 struct Scsi_Host *shost = conn->session->host;
91 struct iscsi_host *ihost = shost_priv(shost);
92
Mike Christie1336aed2009-05-13 17:57:48 -050093 if (ihost->workq)
94 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -060095}
96EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
97
Mike Christie4c0ba5d2009-09-05 07:34:23 +053098static void __iscsi_update_cmdsn(struct iscsi_session *session,
99 uint32_t exp_cmdsn, uint32_t max_cmdsn)
Mike Christie7996a772006-04-06 21:13:41 -0500100{
Mike Christie77a23c22007-05-30 12:57:18 -0500101 /*
102 * standard specifies this check for when to update expected and
103 * max sequence numbers
104 */
105 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
106 return;
107
108 if (exp_cmdsn != session->exp_cmdsn &&
109 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500110 session->exp_cmdsn = exp_cmdsn;
111
Mike Christie77a23c22007-05-30 12:57:18 -0500112 if (max_cmdsn != session->max_cmdsn &&
113 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
114 session->max_cmdsn = max_cmdsn;
115 /*
116 * if the window closed with IO queued, then kick the
117 * xmit thread
118 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500119 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500120 !list_empty(&session->leadconn->mgmtqueue))
121 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500122 }
Mike Christie7996a772006-04-06 21:13:41 -0500123}
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530124
125void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
126{
127 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
128 be32_to_cpu(hdr->max_cmdsn));
129}
Mike Christie77a23c22007-05-30 12:57:18 -0500130EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500131
Mike Christie577577d2008-12-02 00:32:05 -0600132/**
133 * iscsi_prep_data_out_pdu - initialize Data-Out
134 * @task: scsi command task
135 * @r2t: R2T info
136 * @hdr: iscsi data in pdu
137 *
138 * Notes:
139 * Initialize Data-Out within this R2T sequence and finds
140 * proper data_offset within this SCSI command.
141 *
142 * This function is called with connection lock taken.
143 **/
144void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
145 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500146{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500147 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600148 unsigned int left = r2t->data_length - r2t->sent;
149
150 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500151
152 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600153 hdr->ttt = r2t->ttt;
154 hdr->datasn = cpu_to_be32(r2t->datasn);
155 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500156 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Andy Grover55bdabd2011-06-16 15:57:09 -0700157 hdr->lun = task->lun;
Mike Christie577577d2008-12-02 00:32:05 -0600158 hdr->itt = task->hdr_itt;
159 hdr->exp_statsn = r2t->exp_statsn;
160 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
161 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500162 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600163 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500164 hdr->flags = 0;
165 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600166 hton24(hdr->dlength, left);
167 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500168 hdr->flags = ISCSI_FLAG_CMD_FINAL;
169 }
Mike Christie577577d2008-12-02 00:32:05 -0600170 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500171}
Mike Christie577577d2008-12-02 00:32:05 -0600172EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500173
Mike Christie9c19a7d2008-05-21 15:54:09 -0500174static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600175{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500176 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600177
Mike Christie9c19a7d2008-05-21 15:54:09 -0500178 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600179 WARN_ON(1);
180 return -EINVAL;
181 }
182
183 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500184 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600185 return 0;
186}
187
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500188/*
189 * make an extended cdb AHS
190 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500191static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500192{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500193 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500194 unsigned rlen, pad_len;
195 unsigned short ahslength;
196 struct iscsi_ecdb_ahdr *ecdb_ahdr;
197 int rc;
198
Mike Christie9c19a7d2008-05-21 15:54:09 -0500199 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500200 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
201
202 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
203 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
204
205 pad_len = iscsi_padding(rlen);
206
Mike Christie9c19a7d2008-05-21 15:54:09 -0500207 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500208 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
209 if (rc)
210 return rc;
211
212 if (pad_len)
213 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
214
215 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
216 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
217 ecdb_ahdr->reserved = 0;
218 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
219
Mike Christie1b2c7af2009-03-05 14:45:58 -0600220 ISCSI_DBG_SESSION(task->conn->session,
221 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
222 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
223 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
224 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500225 return 0;
226}
227
Mike Christie9c19a7d2008-05-21 15:54:09 -0500228static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500229{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500230 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500231 struct iscsi_rlength_ahdr *rlen_ahdr;
232 int rc;
233
Mike Christie9c19a7d2008-05-21 15:54:09 -0500234 rlen_ahdr = iscsi_next_hdr(task);
235 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500236 if (rc)
237 return rc;
238
239 rlen_ahdr->ahslength =
240 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
241 sizeof(rlen_ahdr->reserved));
242 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
243 rlen_ahdr->reserved = 0;
244 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
245
Mike Christie1b2c7af2009-03-05 14:45:58 -0600246 ISCSI_DBG_SESSION(task->conn->session,
247 "bidi-in rlen_ahdr->read_length(%d) "
248 "rlen_ahdr->ahslength(%d)\n",
249 be32_to_cpu(rlen_ahdr->read_length),
250 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500251 return 0;
252}
253
Mike Christie7996a772006-04-06 21:13:41 -0500254/**
Mike Christie5d12c052009-11-11 16:34:32 -0600255 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
256 * @task: iscsi task
257 * @opcode: opcode to check for
258 *
259 * During TMF a task has to be checked if it's affected.
260 * All unrelated I/O can be passed through, but I/O to the
261 * affected LUN should be restricted.
262 * If 'fast_abort' is set we won't be sending any I/O to the
263 * affected LUN.
264 * Otherwise the target is waiting for all TTTs to be completed,
265 * so we have to send all outstanding Data-Out PDUs to the target.
266 */
267static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
268{
269 struct iscsi_conn *conn = task->conn;
270 struct iscsi_tm *tmf = &conn->tmhdr;
271 unsigned int hdr_lun;
272
273 if (conn->tmf_state == TMF_INITIAL)
274 return 0;
275
276 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
277 return 0;
278
279 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
280 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
281 /*
282 * Allow PDUs for unrelated LUNs
283 */
Andy Grover55bdabd2011-06-16 15:57:09 -0700284 hdr_lun = scsilun_to_int(&tmf->lun);
Mike Christie5d12c052009-11-11 16:34:32 -0600285 if (hdr_lun != task->sc->device->lun)
286 return 0;
Mike Christie3fe5ae82009-11-11 16:34:33 -0600287 /* fall through */
288 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
Mike Christie5d12c052009-11-11 16:34:32 -0600289 /*
290 * Fail all SCSI cmd PDUs
291 */
292 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
293 iscsi_conn_printk(KERN_INFO, conn,
294 "task [op %x/%x itt "
Mike Christie3fe5ae82009-11-11 16:34:33 -0600295 "0x%x/0x%x] "
Mike Christie5d12c052009-11-11 16:34:32 -0600296 "rejected.\n",
297 task->hdr->opcode, opcode,
Mike Christie3fe5ae82009-11-11 16:34:33 -0600298 task->itt, task->hdr_itt);
Mike Christie5d12c052009-11-11 16:34:32 -0600299 return -EACCES;
300 }
301 /*
302 * And also all data-out PDUs in response to R2T
303 * if fast_abort is set.
304 */
305 if (conn->session->fast_abort) {
306 iscsi_conn_printk(KERN_INFO, conn,
307 "task [op %x/%x itt "
Mike Christie3fe5ae82009-11-11 16:34:33 -0600308 "0x%x/0x%x] fast abort.\n",
Mike Christie5d12c052009-11-11 16:34:32 -0600309 task->hdr->opcode, opcode,
Mike Christie3fe5ae82009-11-11 16:34:33 -0600310 task->itt, task->hdr_itt);
Mike Christie5d12c052009-11-11 16:34:32 -0600311 return -EACCES;
312 }
313 break;
314 case ISCSI_TM_FUNC_ABORT_TASK:
315 /*
316 * the caller has already checked if the task
317 * they want to abort was in the pending queue so if
318 * we are here the cmd pdu has gone out already, and
319 * we will only hit this for data-outs
320 */
321 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
322 task->hdr_itt == tmf->rtt) {
323 ISCSI_DBG_SESSION(conn->session,
324 "Preventing task %x/%x from sending "
325 "data-out due to abort task in "
326 "progress\n", task->itt,
327 task->hdr_itt);
328 return -EACCES;
329 }
330 break;
331 }
332
333 return 0;
334}
335
336/**
Mike Christie7996a772006-04-06 21:13:41 -0500337 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500338 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500339 *
340 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
341 * fields like dlength or final based on how much data it sends
342 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500343static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500344{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500345 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500346 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500347 struct scsi_cmnd *sc = task->sc;
Nicholas Bellinger12352182011-05-27 11:16:33 +0000348 struct iscsi_scsi_req *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500349 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600350 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600351 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500352
Mike Christie5d12c052009-11-11 16:34:32 -0600353 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
354 if (rc)
355 return rc;
356
Mike Christie184b57c2009-05-13 17:57:39 -0500357 if (conn->session->tt->alloc_pdu) {
358 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
359 if (rc)
360 return rc;
361 }
Nicholas Bellinger12352182011-05-27 11:16:33 +0000362 hdr = (struct iscsi_scsi_req *)task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600363 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600364 memset(hdr, 0, sizeof(*hdr));
365
Mike Christie2ff79d52008-12-02 00:32:14 -0600366 if (session->tt->parse_pdu_itt)
367 hdr->itt = task->hdr_itt = itt;
368 else
369 hdr->itt = task->hdr_itt = build_itt(task->itt,
370 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500371 task->hdr_len = 0;
372 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600373 if (rc)
374 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600375 hdr->opcode = ISCSI_OP_SCSI_CMD;
376 hdr->flags = ISCSI_ATTR_SIMPLE;
Andy Grover55bdabd2011-06-16 15:57:09 -0700377 int_to_scsilun(sc->device->lun, &hdr->lun);
378 task->lun = hdr->lun;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600379 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500380 cmd_len = sc->cmd_len;
381 if (cmd_len < ISCSI_CDB_SIZE)
382 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
383 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500385 if (rc)
386 return rc;
387 cmd_len = ISCSI_CDB_SIZE;
388 }
389 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500390
Mike Christie9c19a7d2008-05-21 15:54:09 -0500391 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500392 if (scsi_bidi_cmnd(sc)) {
393 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500394 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500395 if (rc)
396 return rc;
397 }
Mike Christie7996a772006-04-06 21:13:41 -0500398 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500399 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600400 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
401
Boaz Harroshc07d4442008-04-18 10:11:52 -0500402 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500403 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
404 /*
405 * Write counters:
406 *
407 * imm_count bytes to be sent right after
408 * SCSI PDU Header
409 *
410 * unsol_count bytes(as Data-Out) to be sent
411 * without R2T ack right after
412 * immediate data
413 *
Mike Christie577577d2008-12-02 00:32:05 -0600414 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500415 *
416 * pad_count bytes to be sent as zero-padding
417 */
Mike Christie577577d2008-12-02 00:32:05 -0600418 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500419
420 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500421 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500422 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500423 conn->max_xmit_dlength);
424 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500425 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500426 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500427 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500428 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600429 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500430
Mike Christieffd04362006-08-31 18:09:24 -0400431 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600432 r2t->data_length = min(session->first_burst, out_len) -
433 task->imm_count;
434 r2t->data_offset = task->imm_count;
435 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
436 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400437 }
438
Mike Christie577577d2008-12-02 00:32:05 -0600439 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500440 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600441 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500442 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500443 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
444 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500445 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500446
447 if (sc->sc_data_direction == DMA_FROM_DEVICE)
448 hdr->flags |= ISCSI_FLAG_CMD_READ;
449 }
450
Boaz Harrosh004d6532007-12-13 12:43:23 -0600451 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500452 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600453
454 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
455 hdrlength /= ISCSI_PAD_LEN;
456
457 WARN_ON(hdrlength >= 256);
458 hdr->hlength = hdrlength & 0xFF;
Mike Christie96b1f962010-04-24 16:21:19 -0500459 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600460
Mike Christie577577d2008-12-02 00:32:05 -0600461 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500462 return -EIO;
463
Mike Christie9c19a7d2008-05-21 15:54:09 -0500464 task->state = ISCSI_TASK_RUNNING;
Mike Christied3305f32009-08-20 15:10:58 -0500465 session->cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500466
Olaf Kircha8ac6312007-12-13 12:43:35 -0600467 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600468 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
469 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
470 scsi_bidi_cmnd(sc) ? "bidirectional" :
471 sc->sc_data_direction == DMA_TO_DEVICE ?
472 "write" : "read", conn->id, sc, sc->cmnd[0],
473 task->itt, scsi_bufflen(sc),
474 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
475 session->cmdsn,
476 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600477 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500478}
Mike Christie7996a772006-04-06 21:13:41 -0500479
480/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500481 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500482 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500483 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600484 * Must be called with session back_lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500485 * This function returns the scsi command to scsi-ml or cleans
486 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500487 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500488static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500489{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500490 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600491 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500492 struct scsi_cmnd *sc = task->sc;
Mike Christief41d4722010-12-31 02:22:21 -0600493 int oldstate = task->state;
Mike Christie7996a772006-04-06 21:13:41 -0500494
Mike Christie4421c9e2009-05-13 17:57:50 -0500495 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
496 task->itt, task->state, task->sc);
497
Mike Christie577577d2008-12-02 00:32:05 -0600498 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500499 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500500 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500501 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500502 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500503 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500504 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500505 return;
506
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800507 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500508
Mike Christie3e5c28a2008-05-21 15:54:06 -0500509 if (sc) {
Mike Christie3e5c28a2008-05-21 15:54:06 -0500510 /* SCSI eh reuses commands to verify us */
511 sc->SCp.ptr = NULL;
512 /*
Mike Christief41d4722010-12-31 02:22:21 -0600513 * queue command may call this to free the task, so
514 * it will decide how to return sc to scsi-ml.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500515 */
Mike Christief41d4722010-12-31 02:22:21 -0600516 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500517 sc->scsi_done(sc);
518 }
Mike Christie7996a772006-04-06 21:13:41 -0500519}
520
Mike Christie913e5bf2008-05-21 15:54:18 -0500521void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400522{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500523 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400524}
Mike Christie913e5bf2008-05-21 15:54:18 -0500525EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400526
Eddie Wai8eea2f52010-11-23 15:29:21 -0800527void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400528{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500529 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500530 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400531}
Eddie Wai8eea2f52010-11-23 15:29:21 -0800532EXPORT_SYMBOL_GPL(__iscsi_put_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400533
Mike Christie9c19a7d2008-05-21 15:54:09 -0500534void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500535{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500536 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500537
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600538 /* regular RX path uses back_lock */
539 spin_lock_bh(&session->back_lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500540 __iscsi_put_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600541 spin_unlock_bh(&session->back_lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500542}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500543EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500544
Mike Christie3bbaaad2009-05-13 17:57:46 -0500545/**
546 * iscsi_complete_task - finish a task
547 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500548 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500549 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600550 * Must be called with session back_lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600551 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500552static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600553{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500554 struct iscsi_conn *conn = task->conn;
555
Mike Christie4421c9e2009-05-13 17:57:50 -0500556 ISCSI_DBG_SESSION(conn->session,
557 "complete task itt 0x%x state %d sc %p\n",
558 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500559 if (task->state == ISCSI_TASK_COMPLETED ||
560 task->state == ISCSI_TASK_ABRT_TMF ||
Mike Christief41d4722010-12-31 02:22:21 -0600561 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
562 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500563 return;
564 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500565 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500566
567 if (!list_empty(&task->running))
568 list_del_init(&task->running);
569
570 if (conn->task == task)
571 conn->task = NULL;
572
573 if (conn->ping_task == task)
574 conn->ping_task = NULL;
575
576 /* release get from queueing */
577 __iscsi_put_task(task);
578}
579
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530580/**
581 * iscsi_complete_scsi_task - finish scsi task normally
582 * @task: iscsi task for scsi cmd
583 * @exp_cmdsn: expected cmd sn in cpu format
584 * @max_cmdsn: max cmd sn in cpu format
585 *
586 * This is used when drivers do not need or cannot perform
587 * lower level pdu processing.
588 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600589 * Called with session back_lock
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530590 */
591void iscsi_complete_scsi_task(struct iscsi_task *task,
592 uint32_t exp_cmdsn, uint32_t max_cmdsn)
593{
594 struct iscsi_conn *conn = task->conn;
595
596 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
597
598 conn->last_recv = jiffies;
599 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
600 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
601}
602EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
603
604
Mike Christie3bbaaad2009-05-13 17:57:46 -0500605/*
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600606 * session back_lock must be held and if not called for a task that is
Mike Christie3bbaaad2009-05-13 17:57:46 -0500607 * still pending or from the xmit thread, then xmit thread must
608 * be suspended.
609 */
610static void fail_scsi_task(struct iscsi_task *task, int err)
611{
612 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600613 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500614 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600615
Mike Christie3bbaaad2009-05-13 17:57:46 -0500616 /*
617 * if a command completes and we get a successful tmf response
618 * we will hit this because the scsi eh abort code does not take
619 * a ref to the task.
620 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500621 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600622 if (!sc)
623 return;
624
Mike Christieb3cd5052009-05-13 17:57:49 -0500625 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600626 /*
627 * cmd never made it to the xmit thread, so we should not count
628 * the cmd in the sequencing
629 */
630 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500631 /* it was never sent so just complete like normal */
632 state = ISCSI_TASK_COMPLETED;
633 } else if (err == DID_TRANSPORT_DISRUPTED)
634 state = ISCSI_TASK_ABRT_SESS_RECOV;
635 else
636 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600637
Mike Christieb3cd5052009-05-13 17:57:49 -0500638 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500639 if (!scsi_bidi_cmnd(sc))
640 scsi_set_resid(sc, scsi_bufflen(sc));
641 else {
642 scsi_out(sc)->resid = scsi_out(sc)->length;
643 scsi_in(sc)->resid = scsi_in(sc)->length;
644 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500645
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600646 /* regular RX path uses back_lock */
647 spin_lock_bh(&conn->session->back_lock);
Mike Christieb3cd5052009-05-13 17:57:49 -0500648 iscsi_complete_task(task, state);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600649 spin_unlock_bh(&conn->session->back_lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600650}
651
Mike Christie3e5c28a2008-05-21 15:54:06 -0500652static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500653 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500654{
655 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600656 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500657 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
Mike Christie4f704dc2009-11-11 16:34:31 -0600658 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie052d0142008-05-21 15:54:05 -0500659
660 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
661 return -ENOTCONN;
662
Mike Christie4f704dc2009-11-11 16:34:31 -0600663 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
Mike Christie052d0142008-05-21 15:54:05 -0500664 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
665 /*
666 * pre-format CmdSN for outgoing PDU.
667 */
668 nop->cmdsn = cpu_to_be32(session->cmdsn);
669 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500670 /*
Mike Christie4f704dc2009-11-11 16:34:31 -0600671 * TODO: We always use immediate for normal session pdus.
Mike Christie052d0142008-05-21 15:54:05 -0500672 * If we start to send tmfs or nops as non-immediate then
673 * we should start checking the cmdsn numbers for mgmt tasks.
Mike Christie4f704dc2009-11-11 16:34:31 -0600674 *
675 * During discovery sessions iscsid sends TEXT as non immediate,
676 * but we always only send one PDU at a time.
Mike Christie052d0142008-05-21 15:54:05 -0500677 */
678 if (conn->c_stage == ISCSI_CONN_STARTED &&
679 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
680 session->queued_cmdsn++;
681 session->cmdsn++;
682 }
683 }
684
Mike Christieae15f802008-12-02 00:32:15 -0600685 if (session->tt->init_task && session->tt->init_task(task))
686 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500687
688 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
689 session->state = ISCSI_STATE_LOGGING_OUT;
690
Mike Christie577577d2008-12-02 00:32:05 -0600691 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600692 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
693 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
694 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500695 return 0;
696}
697
Mike Christie9c19a7d2008-05-21 15:54:09 -0500698static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600699__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
700 char *data, uint32_t data_size)
701{
702 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500703 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie4f704dc2009-11-11 16:34:31 -0600704 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500705 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600706 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600707
708 if (session->state == ISCSI_STATE_TERMINATE)
709 return NULL;
710
Mike Christie4f704dc2009-11-11 16:34:31 -0600711 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
Mike Christief6d51802007-12-13 12:43:30 -0600712 /*
713 * Login and Text are sent serially, in
714 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500715 * Same task can be used. Same ITT must be used.
716 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600717 */
Mike Christie4f704dc2009-11-11 16:34:31 -0600718 if (conn->login_task->state != ISCSI_TASK_FREE) {
719 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
720 "progress. Cannot start new task.\n");
721 return NULL;
722 }
723
Mike Christie9c19a7d2008-05-21 15:54:09 -0500724 task = conn->login_task;
Mike Christie4f704dc2009-11-11 16:34:31 -0600725 } else {
Mike Christie26013ad2009-05-13 17:57:43 -0500726 if (session->state != ISCSI_STATE_LOGGED_IN)
727 return NULL;
728
Mike Christief6d51802007-12-13 12:43:30 -0600729 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
730 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
731
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800732 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500733 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600734 return NULL;
735 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500736 /*
737 * released in complete pdu for task we expect a response for, and
738 * released by the lld when it has transmitted the task for
739 * pdus we do not expect a response for.
740 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500741 atomic_set(&task->refcount, 1);
742 task->conn = conn;
743 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500744 INIT_LIST_HEAD(&task->running);
745 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600746
747 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500748 memcpy(task->data, data, data_size);
749 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600750 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500751 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600752
Mike Christie184b57c2009-05-13 17:57:39 -0500753 if (conn->session->tt->alloc_pdu) {
754 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
755 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
756 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500757 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500758 }
Mike Christie577577d2008-12-02 00:32:05 -0600759 }
Mike Christie184b57c2009-05-13 17:57:39 -0500760
Mike Christie262ef632008-12-02 00:32:13 -0600761 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600762 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500763 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600764
765 if (hdr->itt != RESERVED_ITT) {
766 if (session->tt->parse_pdu_itt)
767 task->hdr->itt = itt;
768 else
769 task->hdr->itt = build_itt(task->itt,
770 task->conn->session->age);
771 }
772
Mike Christie1336aed2009-05-13 17:57:48 -0500773 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600774 if (iscsi_prep_mgmt_task(conn, task))
775 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500776
Mike Christie9c19a7d2008-05-21 15:54:09 -0500777 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600778 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500779 } else {
780 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600781 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500782 }
Mike Christie052d0142008-05-21 15:54:05 -0500783
Mike Christie9c19a7d2008-05-21 15:54:09 -0500784 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600785
786free_task:
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600787 /* regular RX path uses back_lock */
788 spin_lock_bh(&session->back_lock);
Mike Christie577577d2008-12-02 00:32:05 -0600789 __iscsi_put_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600790 spin_unlock_bh(&session->back_lock);
Mike Christie577577d2008-12-02 00:32:05 -0600791 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600792}
793
794int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
795 char *data, uint32_t data_size)
796{
797 struct iscsi_conn *conn = cls_conn->dd_data;
798 struct iscsi_session *session = conn->session;
799 int err = 0;
800
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600801 spin_lock_bh(&session->frwd_lock);
Mike Christief6d51802007-12-13 12:43:30 -0600802 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
803 err = -EPERM;
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600804 spin_unlock_bh(&session->frwd_lock);
Mike Christief6d51802007-12-13 12:43:30 -0600805 return err;
806}
807EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
808
Mike Christie7996a772006-04-06 21:13:41 -0500809/**
810 * iscsi_cmd_rsp - SCSI Command Response processing
811 * @conn: iscsi connection
812 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500813 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500814 * @data: cmd data buffer
815 * @datalen: len of buffer
816 *
817 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500818 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500819 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500820static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500821 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500822 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500823{
Nicholas Bellinger12352182011-05-27 11:16:33 +0000824 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
Mike Christie7996a772006-04-06 21:13:41 -0500825 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500826 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500827
Mike Christie77a23c22007-05-30 12:57:18 -0500828 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500829 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
830
831 sc->result = (DID_OK << 16) | rhdr->cmd_status;
832
833 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
834 sc->result = DID_ERROR << 16;
835 goto out;
836 }
837
838 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600839 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500840
841 if (datalen < 2) {
842invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600843 iscsi_conn_printk(KERN_ERR, conn,
844 "Got CHECK_CONDITION but invalid data "
845 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500846 sc->result = DID_BAD_TARGET << 16;
847 goto out;
848 }
849
Harvey Harrison8f3339912008-05-21 15:54:20 -0500850 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500851 if (datalen < senselen)
852 goto invalid_datalen;
853
854 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600855 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600856 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
857 min_t(uint16_t, senselen,
858 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500859 }
860
Boaz Harroshc07d4442008-04-18 10:11:52 -0500861 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
862 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
863 int res_count = be32_to_cpu(rhdr->bi_residual_count);
864
865 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
866 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
867 res_count <= scsi_in(sc)->length))
868 scsi_in(sc)->resid = res_count;
869 else
870 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
871 }
872
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600873 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
874 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500875 int res_count = be32_to_cpu(rhdr->residual_count);
876
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600877 if (res_count > 0 &&
878 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
879 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500880 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900881 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500882 else
883 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500884 }
Mike Christie7996a772006-04-06 21:13:41 -0500885out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500886 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600887 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500888 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500889 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500890}
891
Mike Christie1d9edf02008-09-24 11:46:09 -0500892/**
893 * iscsi_data_in_rsp - SCSI Data-In Response processing
894 * @conn: iscsi connection
895 * @hdr: iscsi pdu
896 * @task: scsi command task
897 **/
898static void
899iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
900 struct iscsi_task *task)
901{
902 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
903 struct scsi_cmnd *sc = task->sc;
904
905 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
906 return;
907
Mike Christieedbc9aa052009-05-13 17:57:42 -0500908 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500909 sc->result = (DID_OK << 16) | rhdr->cmd_status;
910 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
911 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
912 ISCSI_FLAG_DATA_OVERFLOW)) {
913 int res_count = be32_to_cpu(rhdr->residual_count);
914
915 if (res_count > 0 &&
916 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
917 res_count <= scsi_in(sc)->length))
918 scsi_in(sc)->resid = res_count;
919 else
920 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
921 }
922
Mike Christie3bbaaad2009-05-13 17:57:46 -0500923 ISCSI_DBG_SESSION(conn->session, "data in with status done "
924 "[sc %p res %d itt 0x%x]\n",
925 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500926 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500927 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500928}
929
Mike Christie7ea8b822006-07-24 15:47:22 -0500930static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
931{
932 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
933
934 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
935 conn->tmfrsp_pdus_cnt++;
936
Mike Christie843c0a82007-12-13 12:43:20 -0600937 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500938 return;
939
940 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600941 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500942 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600943 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500944 else
Mike Christie843c0a82007-12-13 12:43:20 -0600945 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500946 wake_up(&conn->ehwait);
947}
948
Mike Christief6d51802007-12-13 12:43:30 -0600949static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
950{
951 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500952 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600953
Mike Christie9c19a7d2008-05-21 15:54:09 -0500954 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600955 return;
956
957 memset(&hdr, 0, sizeof(struct iscsi_nopout));
958 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
959 hdr.flags = ISCSI_FLAG_CMD_FINAL;
960
961 if (rhdr) {
Andy Grover55bdabd2011-06-16 15:57:09 -0700962 hdr.lun = rhdr->lun;
Mike Christief6d51802007-12-13 12:43:30 -0600963 hdr.ttt = rhdr->ttt;
964 hdr.itt = RESERVED_ITT;
965 } else
966 hdr.ttt = RESERVED_ITT;
967
Mike Christie9c19a7d2008-05-21 15:54:09 -0500968 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
969 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600970 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600971 else if (!rhdr) {
972 /* only track our nops */
973 conn->ping_task = task;
974 conn->last_ping = jiffies;
975 }
Mike Christief6d51802007-12-13 12:43:30 -0600976}
977
Mike Christie8afa1432009-08-20 15:10:59 -0500978static int iscsi_nop_out_rsp(struct iscsi_task *task,
979 struct iscsi_nopin *nop, char *data, int datalen)
980{
981 struct iscsi_conn *conn = task->conn;
982 int rc = 0;
983
984 if (conn->ping_task != task) {
985 /*
986 * If this is not in response to one of our
987 * nops then it must be from userspace.
988 */
989 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
990 data, datalen))
991 rc = ISCSI_ERR_CONN_FAILED;
992 } else
993 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
994 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
995 return rc;
996}
997
Mike Christie62f38302006-08-31 18:09:27 -0400998static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
999 char *data, int datalen)
1000{
1001 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1002 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -05001003 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -04001004
1005 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1006
Mike Christie8afa1432009-08-20 15:10:59 -05001007 if (ntoh24(reject->dlength) > datalen ||
1008 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1009 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1010 "pdu. Invalid data length (pdu dlength "
1011 "%u, datalen %d\n", ntoh24(reject->dlength),
1012 datalen);
1013 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -04001014 }
Mike Christie8afa1432009-08-20 15:10:59 -05001015 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1016 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1017
1018 switch (reject->reason) {
1019 case ISCSI_REASON_DATA_DIGEST_ERROR:
1020 iscsi_conn_printk(KERN_ERR, conn,
1021 "pdu (op 0x%x itt 0x%x) rejected "
1022 "due to DataDigest error.\n",
Vaughan Caoe5dbbe22014-01-09 10:21:34 +08001023 opcode, rejected_pdu.itt);
Mike Christie8afa1432009-08-20 15:10:59 -05001024 break;
1025 case ISCSI_REASON_IMM_CMD_REJECT:
1026 iscsi_conn_printk(KERN_ERR, conn,
1027 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1028 "immediate commands.\n",
Vaughan Caoe5dbbe22014-01-09 10:21:34 +08001029 opcode, rejected_pdu.itt);
Mike Christie8afa1432009-08-20 15:10:59 -05001030 /*
1031 * We only send one TMF at a time so if the target could not
1032 * handle it, then it should get fixed (RFC mandates that
1033 * a target can handle one immediate TMF per conn).
1034 *
1035 * For nops-outs, we could have sent more than one if
1036 * the target is sending us lots of nop-ins
1037 */
1038 if (opcode != ISCSI_OP_NOOP_OUT)
1039 return 0;
1040
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001041 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
Mike Christie8afa1432009-08-20 15:10:59 -05001042 /*
1043 * nop-out in response to target's nop-out rejected.
1044 * Just resend.
1045 */
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001046 /* In RX path we are under back lock */
1047 spin_unlock(&conn->session->back_lock);
1048 spin_lock(&conn->session->frwd_lock);
Mike Christie8afa1432009-08-20 15:10:59 -05001049 iscsi_send_nopout(conn,
1050 (struct iscsi_nopin*)&rejected_pdu);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001051 spin_unlock(&conn->session->frwd_lock);
1052 spin_lock(&conn->session->back_lock);
1053 } else {
Mike Christie8afa1432009-08-20 15:10:59 -05001054 struct iscsi_task *task;
1055 /*
1056 * Our nop as ping got dropped. We know the target
1057 * and transport are ok so just clean up
1058 */
1059 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1060 if (!task) {
1061 iscsi_conn_printk(KERN_ERR, conn,
1062 "Invalid pdu reject. Could "
1063 "not lookup rejected task.\n");
1064 rc = ISCSI_ERR_BAD_ITT;
1065 } else
1066 rc = iscsi_nop_out_rsp(task,
1067 (struct iscsi_nopin*)&rejected_pdu,
1068 NULL, 0);
1069 }
1070 break;
1071 default:
1072 iscsi_conn_printk(KERN_ERR, conn,
1073 "pdu (op 0x%x itt 0x%x) rejected. Reason "
Vaughan Caoe5dbbe22014-01-09 10:21:34 +08001074 "code 0x%x\n", rejected_pdu.opcode,
1075 rejected_pdu.itt, reject->reason);
Mike Christie8afa1432009-08-20 15:10:59 -05001076 break;
1077 }
1078 return rc;
Mike Christie62f38302006-08-31 18:09:27 -04001079}
1080
Mike Christie7996a772006-04-06 21:13:41 -05001081/**
Mike Christie913e5bf2008-05-21 15:54:18 -05001082 * iscsi_itt_to_task - look up task by itt
1083 * @conn: iscsi connection
1084 * @itt: itt
1085 *
1086 * This should be used for mgmt tasks like login and nops, or if
1087 * the LDD's itt space does not include the session age.
1088 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001089 * The session back_lock must be held.
Mike Christie913e5bf2008-05-21 15:54:18 -05001090 */
Mike Christie8f9256c2009-05-13 17:57:41 -05001091struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -05001092{
1093 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001094 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -05001095
1096 if (itt == RESERVED_ITT)
1097 return NULL;
1098
Mike Christie262ef632008-12-02 00:32:13 -06001099 if (session->tt->parse_pdu_itt)
1100 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1101 else
1102 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -05001103 if (i >= session->cmds_max)
1104 return NULL;
1105
1106 return session->cmds[i];
1107}
Mike Christie8f9256c2009-05-13 17:57:41 -05001108EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -05001109
1110/**
Mike Christie7996a772006-04-06 21:13:41 -05001111 * __iscsi_complete_pdu - complete pdu
1112 * @conn: iscsi conn
1113 * @hdr: iscsi header
1114 * @data: data buffer
1115 * @datalen: len of data buffer
1116 *
1117 * Completes pdu processing by freeing any resources allocated at
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001118 * queuecommand or send generic. session back_lock must be held and verify
Mike Christie7996a772006-04-06 21:13:41 -05001119 * itt must have been called.
1120 */
Mike Christie913e5bf2008-05-21 15:54:18 -05001121int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1122 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001123{
1124 struct iscsi_session *session = conn->session;
1125 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001126 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001127 uint32_t itt;
1128
Mike Christief6d51802007-12-13 12:43:30 -06001129 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001130 rc = iscsi_verify_itt(conn, hdr->itt);
1131 if (rc)
1132 return rc;
1133
Al Virob4377352007-02-09 16:39:40 +00001134 if (hdr->itt != RESERVED_ITT)
1135 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001136 else
Al Virob4377352007-02-09 16:39:40 +00001137 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001138
Mike Christie1b2c7af2009-03-05 14:45:58 -06001139 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1140 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001141
Mike Christie3e5c28a2008-05-21 15:54:06 -05001142 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001143 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001144
Mike Christie7996a772006-04-06 21:13:41 -05001145 switch(opcode) {
1146 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001147 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001148 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001149 break;
1150 }
1151
Al Virob4377352007-02-09 16:39:40 +00001152 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001153 break;
1154
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001155 /* In RX path we are under back lock */
1156 spin_unlock(&session->back_lock);
1157 spin_lock(&session->frwd_lock);
Mike Christief6d51802007-12-13 12:43:30 -06001158 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001159 spin_unlock(&session->frwd_lock);
1160 spin_lock(&session->back_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001161 break;
1162 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001163 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1164 break;
Mike Christie7996a772006-04-06 21:13:41 -05001165 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001166 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001167 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1168 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001169 break;
1170 default:
1171 rc = ISCSI_ERR_BAD_OPCODE;
1172 break;
1173 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001174 goto out;
1175 }
Mike Christie7996a772006-04-06 21:13:41 -05001176
Mike Christie3e5c28a2008-05-21 15:54:06 -05001177 switch(opcode) {
1178 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001179 case ISCSI_OP_SCSI_DATA_IN:
1180 task = iscsi_itt_to_ctask(conn, hdr->itt);
1181 if (!task)
1182 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001183 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001184 break;
1185 case ISCSI_OP_R2T:
1186 /*
1187 * LLD handles R2Ts if they need to.
1188 */
1189 return 0;
1190 case ISCSI_OP_LOGOUT_RSP:
1191 case ISCSI_OP_LOGIN_RSP:
1192 case ISCSI_OP_TEXT_RSP:
1193 case ISCSI_OP_SCSI_TMFUNC_RSP:
1194 case ISCSI_OP_NOOP_IN:
1195 task = iscsi_itt_to_task(conn, hdr->itt);
1196 if (!task)
1197 return ISCSI_ERR_BAD_ITT;
1198 break;
1199 default:
1200 return ISCSI_ERR_BAD_OPCODE;
1201 }
1202
1203 switch(opcode) {
1204 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001205 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001206 break;
1207 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001208 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001209 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001210 case ISCSI_OP_LOGOUT_RSP:
1211 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1212 if (datalen) {
1213 rc = ISCSI_ERR_PROTO;
1214 break;
1215 }
1216 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1217 goto recv_pdu;
1218 case ISCSI_OP_LOGIN_RSP:
1219 case ISCSI_OP_TEXT_RSP:
1220 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1221 /*
1222 * login related PDU's exp_statsn is handled in
1223 * userspace
1224 */
1225 goto recv_pdu;
1226 case ISCSI_OP_SCSI_TMFUNC_RSP:
1227 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1228 if (datalen) {
1229 rc = ISCSI_ERR_PROTO;
1230 break;
1231 }
1232
1233 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001234 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001235 break;
1236 case ISCSI_OP_NOOP_IN:
1237 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1238 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1239 rc = ISCSI_ERR_PROTO;
1240 break;
1241 }
1242 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1243
Mike Christie8afa1432009-08-20 15:10:59 -05001244 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1245 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001246 break;
1247 default:
1248 rc = ISCSI_ERR_BAD_OPCODE;
1249 break;
1250 }
1251
1252out:
1253 return rc;
1254recv_pdu:
1255 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1256 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001257 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001258 return rc;
1259}
Mike Christie913e5bf2008-05-21 15:54:18 -05001260EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001261
1262int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1263 char *data, int datalen)
1264{
1265 int rc;
1266
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001267 spin_lock(&conn->session->back_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001268 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001269 spin_unlock(&conn->session->back_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001270 return rc;
1271}
1272EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1273
Mike Christie0af967f2008-05-21 15:54:04 -05001274int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001275{
1276 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001277 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001278
Mike Christie0af967f2008-05-21 15:54:04 -05001279 if (itt == RESERVED_ITT)
1280 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001281
Mike Christie262ef632008-12-02 00:32:13 -06001282 if (session->tt->parse_pdu_itt)
1283 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1284 else {
1285 i = get_itt(itt);
1286 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1287 }
1288
1289 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001290 iscsi_conn_printk(KERN_ERR, conn,
1291 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001292 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001293 return ISCSI_ERR_BAD_ITT;
1294 }
Mike Christie7996a772006-04-06 21:13:41 -05001295
Mike Christie3e5c28a2008-05-21 15:54:06 -05001296 if (i >= session->cmds_max) {
1297 iscsi_conn_printk(KERN_ERR, conn,
1298 "received invalid itt index %u (max cmds "
1299 "%u.\n", i, session->cmds_max);
1300 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001301 }
Mike Christie7996a772006-04-06 21:13:41 -05001302 return 0;
1303}
1304EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1305
Mike Christie913e5bf2008-05-21 15:54:18 -05001306/**
1307 * iscsi_itt_to_ctask - look up ctask by itt
1308 * @conn: iscsi connection
1309 * @itt: itt
1310 *
1311 * This should be used for cmd tasks.
1312 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001313 * The session back_lock must be held.
Mike Christie913e5bf2008-05-21 15:54:18 -05001314 */
1315struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001316{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001317 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001318
1319 if (iscsi_verify_itt(conn, itt))
1320 return NULL;
1321
Mike Christie913e5bf2008-05-21 15:54:18 -05001322 task = iscsi_itt_to_task(conn, itt);
1323 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001324 return NULL;
1325
Mike Christie913e5bf2008-05-21 15:54:18 -05001326 if (task->sc->SCp.phase != conn->session->age) {
1327 iscsi_session_printk(KERN_ERR, conn->session,
1328 "task's session age %d, expected %d\n",
1329 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001330 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001331 }
Mike Christie0af967f2008-05-21 15:54:04 -05001332
Mike Christie9c19a7d2008-05-21 15:54:09 -05001333 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001334}
1335EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1336
Mike Christie40a06e72009-03-05 14:46:05 -06001337void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001338 enum iscsi_err err)
1339{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001340 struct iscsi_conn *conn;
1341 struct device *dev;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001342
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001343 spin_lock_bh(&session->frwd_lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001344 conn = session->leadconn;
1345 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001346 spin_unlock_bh(&session->frwd_lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001347 return;
1348 }
1349
1350 dev = get_device(&conn->cls_conn->dev);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001351 spin_unlock_bh(&session->frwd_lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001352 if (!dev)
1353 return;
1354 /*
1355 * if the host is being removed bypass the connection
1356 * recovery initialization because we are going to kill
1357 * the session.
1358 */
1359 if (err == ISCSI_ERR_INVALID_HOST)
1360 iscsi_conn_error_event(conn->cls_conn, err);
1361 else
1362 iscsi_conn_failure(conn, err);
1363 put_device(dev);
1364}
1365EXPORT_SYMBOL_GPL(iscsi_session_failure);
1366
Mike Christie7996a772006-04-06 21:13:41 -05001367void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1368{
1369 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -05001370
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001371 spin_lock_bh(&session->frwd_lock);
Mike Christie656cffc2006-05-18 20:31:42 -05001372 if (session->state == ISCSI_STATE_FAILED) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001373 spin_unlock_bh(&session->frwd_lock);
Mike Christie656cffc2006-05-18 20:31:42 -05001374 return;
1375 }
1376
Mike Christie67a61112006-05-30 00:37:20 -05001377 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001378 session->state = ISCSI_STATE_FAILED;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001379 spin_unlock_bh(&session->frwd_lock);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001380
Mike Christie7996a772006-04-06 21:13:41 -05001381 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1382 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001383 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001384}
1385EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1386
Mike Christie77a23c22007-05-30 12:57:18 -05001387static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1388{
1389 struct iscsi_session *session = conn->session;
1390
1391 /*
1392 * Check for iSCSI window and take care of CmdSN wrap-around
1393 */
Mike Christiee0726402007-07-26 12:46:48 -05001394 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001395 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1396 "%u MaxCmdSN %u CmdSN %u/%u\n",
1397 session->exp_cmdsn, session->max_cmdsn,
1398 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001399 return -ENOSPC;
1400 }
1401 return 0;
1402}
1403
Mike Christie9c19a7d2008-05-21 15:54:09 -05001404static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001405{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001406 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001407 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001408
Mike Christie70b31c12009-08-20 15:11:03 -05001409 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1410 return -ENODATA;
1411
Mike Christie9c19a7d2008-05-21 15:54:09 -05001412 __iscsi_get_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001413 spin_unlock_bh(&conn->session->frwd_lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001414 rc = conn->session->tt->xmit_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001415 spin_lock_bh(&conn->session->frwd_lock);
Mike Christied355e572009-06-15 22:11:08 -05001416 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001417 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001418 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001419 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001420 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001421 /* regular RX path uses back_lock */
1422 spin_lock_bh(&conn->session->back_lock);
Mike Christied355e572009-06-15 22:11:08 -05001423 __iscsi_put_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001424 spin_unlock_bh(&conn->session->back_lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001425 return rc;
1426}
1427
Mike Christie7996a772006-04-06 21:13:41 -05001428/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001429 * iscsi_requeue_task - requeue task to run from session workqueue
1430 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001431 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001432 * LLDs that need to run a task from the session workqueue should call
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001433 * this. The session frwd_lock must be held. This should only be called
Mike Christie052d0142008-05-21 15:54:05 -05001434 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001435 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001436void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001437{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001438 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001439
Mike Christie3bbaaad2009-05-13 17:57:46 -05001440 /*
1441 * this may be on the requeue list already if the xmit_task callout
1442 * is handling the r2ts while we are adding new ones
1443 */
1444 if (list_empty(&task->running))
1445 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001446 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001447}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001448EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001449
1450/**
Mike Christie7996a772006-04-06 21:13:41 -05001451 * iscsi_data_xmit - xmit any command into the scheduled connection
1452 * @conn: iscsi connection
1453 *
1454 * Notes:
1455 * The function can return -EAGAIN in which case the caller must
1456 * re-schedule it again later or recover. '0' return code means
1457 * successful xmit.
1458 **/
1459static int iscsi_data_xmit(struct iscsi_conn *conn)
1460{
Mike Christie5d12c052009-11-11 16:34:32 -06001461 struct iscsi_task *task;
Mike Christie3219e522006-05-30 00:37:28 -05001462 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001463
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001464 spin_lock_bh(&conn->session->frwd_lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001465 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001466 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001467 spin_unlock_bh(&conn->session->frwd_lock);
Mike Christie3219e522006-05-30 00:37:28 -05001468 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001469 }
Mike Christie7996a772006-04-06 21:13:41 -05001470
Mike Christie9c19a7d2008-05-21 15:54:09 -05001471 if (conn->task) {
1472 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001473 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001474 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001475 }
1476
Mike Christie77a23c22007-05-30 12:57:18 -05001477 /*
1478 * process mgmt pdus like nops before commands since we should
1479 * only have one nop-out as a ping from us and targets should not
1480 * overflow us with nop-ins
1481 */
1482check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001483 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001484 conn->task = list_entry(conn->mgmtqueue.next,
1485 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001486 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001487 if (iscsi_prep_mgmt_task(conn, conn->task)) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001488 /* regular RX path uses back_lock */
1489 spin_lock_bh(&conn->session->back_lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001490 __iscsi_put_task(conn->task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001491 spin_unlock_bh(&conn->session->back_lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001492 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001493 continue;
1494 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001495 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001496 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001497 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001498 }
1499
Mike Christie843c0a82007-12-13 12:43:20 -06001500 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001501 while (!list_empty(&conn->cmdqueue)) {
Mike Christie5d12c052009-11-11 16:34:32 -06001502 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1503 running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001504 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001505 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001506 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001507 continue;
1508 }
Mike Christie577577d2008-12-02 00:32:05 -06001509 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1510 if (rc) {
Mike Christie5d12c052009-11-11 16:34:32 -06001511 if (rc == -ENOMEM || rc == -EACCES) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001512 list_add_tail(&conn->task->running,
1513 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001514 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001515 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001516 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001517 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001518 continue;
1519 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001520 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001521 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001522 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001523 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001524 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001525 * we need to check the mgmt queue for nops that need to
1526 * be sent to aviod starvation
1527 */
Mike Christie843c0a82007-12-13 12:43:20 -06001528 if (!list_empty(&conn->mgmtqueue))
1529 goto check_mgmt;
1530 }
1531
1532 while (!list_empty(&conn->requeue)) {
Mike Christieb3a7ea82007-12-13 12:43:26 -06001533 /*
1534 * we always do fastlogout - conn stop code will clean up.
1535 */
1536 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1537 break;
1538
Mike Christie5d12c052009-11-11 16:34:32 -06001539 task = list_entry(conn->requeue.next, struct iscsi_task,
1540 running);
1541 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1542 break;
1543
1544 conn->task = task;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001545 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001546 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001547 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001548 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001549 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001550 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001551 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001552 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001553 spin_unlock_bh(&conn->session->frwd_lock);
Mike Christie3219e522006-05-30 00:37:28 -05001554 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001555
Mike Christie70b31c12009-08-20 15:11:03 -05001556done:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001557 spin_unlock_bh(&conn->session->frwd_lock);
Mike Christie3219e522006-05-30 00:37:28 -05001558 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001559}
1560
David Howellsc4028952006-11-22 14:57:56 +00001561static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001562{
David Howellsc4028952006-11-22 14:57:56 +00001563 struct iscsi_conn *conn =
1564 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001565 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001566 /*
1567 * serialize Xmit worker on a per-connection basis.
1568 */
Mike Christie3219e522006-05-30 00:37:28 -05001569 do {
1570 rc = iscsi_data_xmit(conn);
1571 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001572}
1573
Mike Christie577577d2008-12-02 00:32:05 -06001574static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1575 struct scsi_cmnd *sc)
1576{
1577 struct iscsi_task *task;
1578
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001579 if (!kfifo_out(&conn->session->cmdpool.queue,
Mike Christie577577d2008-12-02 00:32:05 -06001580 (void *) &task, sizeof(void *)))
1581 return NULL;
1582
1583 sc->SCp.phase = conn->session->age;
1584 sc->SCp.ptr = (char *) task;
1585
1586 atomic_set(&task->refcount, 1);
1587 task->state = ISCSI_TASK_PENDING;
1588 task->conn = conn;
1589 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001590 task->have_checked_conn = false;
1591 task->last_timeout = jiffies;
1592 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001593 INIT_LIST_HEAD(&task->running);
1594 return task;
1595}
1596
Mike Christie7996a772006-04-06 21:13:41 -05001597enum {
1598 FAILURE_BAD_HOST = 1,
1599 FAILURE_SESSION_FAILED,
1600 FAILURE_SESSION_FREED,
1601 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001602 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001603 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001604 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001605 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001606 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001607 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001608};
1609
Mike Christief41d4722010-12-31 02:22:21 -06001610int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001611{
Mike Christie75613522008-05-21 15:53:59 -05001612 struct iscsi_cls_session *cls_session;
Mike Christie1336aed2009-05-13 17:57:48 -05001613 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001614 int reason = 0;
1615 struct iscsi_session *session;
1616 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001617 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001618
Mike Christie7996a772006-04-06 21:13:41 -05001619 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001620 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001621
Mike Christie1336aed2009-05-13 17:57:48 -05001622 ihost = shost_priv(host);
Mike Christie7996a772006-04-06 21:13:41 -05001623
Mike Christie75613522008-05-21 15:53:59 -05001624 cls_session = starget_to_session(scsi_target(sc->device));
1625 session = cls_session->dd_data;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001626 spin_lock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001627
Mike Christie75613522008-05-21 15:53:59 -05001628 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001629 if (reason) {
1630 sc->result = reason;
1631 goto fault;
1632 }
1633
Mike Christie301e0f72009-05-13 17:57:47 -05001634 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001635 /*
1636 * to handle the race between when we set the recovery state
1637 * and block the session we requeue here (commands could
1638 * be entering our queuecommand while a block is starting
1639 * up because the block code is not locked)
1640 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001641 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001642 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001643 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001644 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001645 sc->result = DID_IMM_RETRY << 16;
1646 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001647 case ISCSI_STATE_LOGGING_OUT:
1648 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001649 sc->result = DID_IMM_RETRY << 16;
1650 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001651 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001652 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001653 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001654 break;
1655 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001656 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001657 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001658 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001659 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001660 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001661 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001662 }
Mike Christie7996a772006-04-06 21:13:41 -05001663 goto fault;
1664 }
1665
Mike Christie7996a772006-04-06 21:13:41 -05001666 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001667 if (!conn) {
1668 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001669 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001670 goto fault;
1671 }
Mike Christie7996a772006-04-06 21:13:41 -05001672
Mike Christie661134a2009-09-05 07:35:33 +05301673 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1674 reason = FAILURE_SESSION_IN_RECOVERY;
1675 sc->result = DID_REQUEUE;
1676 goto fault;
1677 }
1678
Mike Christie77a23c22007-05-30 12:57:18 -05001679 if (iscsi_check_cmdsn_window_closed(conn)) {
1680 reason = FAILURE_WINDOW_CLOSED;
1681 goto reject;
1682 }
1683
Mike Christie577577d2008-12-02 00:32:05 -06001684 task = iscsi_alloc_task(conn, sc);
1685 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001686 reason = FAILURE_OOM;
1687 goto reject;
1688 }
Mike Christie7996a772006-04-06 21:13:41 -05001689
Mike Christie1336aed2009-05-13 17:57:48 -05001690 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001691 reason = iscsi_prep_scsi_cmd_pdu(task);
1692 if (reason) {
Mike Christie5d12c052009-11-11 16:34:32 -06001693 if (reason == -ENOMEM || reason == -EACCES) {
Mike Christie577577d2008-12-02 00:32:05 -06001694 reason = FAILURE_OOM;
1695 goto prepd_reject;
1696 } else {
1697 sc->result = DID_ABORT << 16;
1698 goto prepd_fault;
1699 }
Mike Christie052d0142008-05-21 15:54:05 -05001700 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001701 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001702 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001703 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001704 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001705 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001706 } else {
1707 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001708 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001709 }
Mike Christie052d0142008-05-21 15:54:05 -05001710
1711 session->queued_cmdsn++;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001712 spin_unlock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001713 return 0;
1714
Mike Christie577577d2008-12-02 00:32:05 -06001715prepd_reject:
Mike Christief41d4722010-12-31 02:22:21 -06001716 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
Mike Christie7996a772006-04-06 21:13:41 -05001717reject:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001718 spin_unlock_bh(&session->frwd_lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001719 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1720 sc->cmnd[0], reason);
Mike Christied6d13ee2008-08-17 15:24:43 -05001721 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001722
Mike Christie577577d2008-12-02 00:32:05 -06001723prepd_fault:
Mike Christief41d4722010-12-31 02:22:21 -06001724 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
Mike Christie7996a772006-04-06 21:13:41 -05001725fault:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001726 spin_unlock_bh(&session->frwd_lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001727 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1728 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001729 if (!scsi_bidi_cmnd(sc))
1730 scsi_set_resid(sc, scsi_bufflen(sc));
1731 else {
1732 scsi_out(sc)->resid = scsi_out(sc)->length;
1733 scsi_in(sc)->resid = scsi_in(sc)->length;
1734 }
Mike Christief41d4722010-12-31 02:22:21 -06001735 sc->scsi_done(sc);
Mike Christie7996a772006-04-06 21:13:41 -05001736 return 0;
1737}
1738EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1739
Mike Christiee881a172009-10-15 17:46:39 -07001740int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
Mike Christie7996a772006-04-06 21:13:41 -05001741{
Mike Christie1796e722009-11-11 16:34:36 -06001742 switch (reason) {
1743 case SCSI_QDEPTH_DEFAULT:
1744 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1745 break;
1746 case SCSI_QDEPTH_QFULL:
1747 scsi_track_queue_full(sdev, depth);
1748 break;
1749 case SCSI_QDEPTH_RAMP_UP:
1750 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1751 break;
1752 default:
Mike Christiee881a172009-10-15 17:46:39 -07001753 return -EOPNOTSUPP;
Mike Christie1796e722009-11-11 16:34:36 -06001754 }
Mike Christie7996a772006-04-06 21:13:41 -05001755 return sdev->queue_depth;
1756}
1757EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1758
Mike Christie6b5d6c42009-04-21 15:32:32 -05001759int iscsi_target_alloc(struct scsi_target *starget)
1760{
1761 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1762 struct iscsi_session *session = cls_session->dd_data;
1763
1764 starget->can_queue = session->scsi_cmds_max;
1765 return 0;
1766}
1767EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1768
Mike Christie843c0a82007-12-13 12:43:20 -06001769static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001770{
Mike Christie843c0a82007-12-13 12:43:20 -06001771 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001772 struct iscsi_session *session = conn->session;
1773
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001774 spin_lock(&session->frwd_lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001775 if (conn->tmf_state == TMF_QUEUED) {
1776 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001777 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001778 /* unblock eh_abort() */
1779 wake_up(&conn->ehwait);
1780 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001781 spin_unlock(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001782}
1783
Mike Christie9c19a7d2008-05-21 15:54:09 -05001784static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001785 struct iscsi_tm *hdr, int age,
1786 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001787{
Mike Christie7996a772006-04-06 21:13:41 -05001788 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001789 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001790
Mike Christie9c19a7d2008-05-21 15:54:09 -05001791 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001792 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001793 if (!task) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001794 spin_unlock_bh(&session->frwd_lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06001795 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
Mike Christie7996a772006-04-06 21:13:41 -05001796 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001797 spin_lock_bh(&session->frwd_lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001798 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001799 }
Mike Christie843c0a82007-12-13 12:43:20 -06001800 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001801 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001802 conn->tmf_timer.function = iscsi_tmf_timedout;
1803 conn->tmf_timer.data = (unsigned long)conn;
1804 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001805 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001806
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001807 spin_unlock_bh(&session->frwd_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001808 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001809
1810 /*
1811 * block eh thread until:
1812 *
Mike Christie843c0a82007-12-13 12:43:20 -06001813 * 1) tmf response
1814 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001815 * 3) session is terminated or restarted or userspace has
1816 * given up on recovery
1817 */
Mike Christie843c0a82007-12-13 12:43:20 -06001818 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001819 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001820 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001821 if (signal_pending(current))
1822 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001823 del_timer_sync(&conn->tmf_timer);
1824
Mike Christie6724add2007-08-15 01:38:30 -05001825 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001826 spin_lock_bh(&session->frwd_lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001827 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001828 if (age != session->age ||
1829 session->state != ISCSI_STATE_LOGGED_IN)
1830 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001831 return 0;
1832}
1833
1834/*
Mike Christie843c0a82007-12-13 12:43:20 -06001835 * Fail commands. session lock held and recv side suspended and xmit
1836 * thread flushed
1837 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001838static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1839 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001840{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001841 struct iscsi_task *task;
1842 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001843
Mike Christie3bbaaad2009-05-13 17:57:46 -05001844 for (i = 0; i < conn->session->cmds_max; i++) {
1845 task = conn->session->cmds[i];
1846 if (!task->sc || task->state == ISCSI_TASK_FREE)
1847 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001848
Mike Christie3bbaaad2009-05-13 17:57:46 -05001849 if (lun != -1 && lun != task->sc->device->lun)
1850 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001851
Mike Christie3bbaaad2009-05-13 17:57:46 -05001852 ISCSI_DBG_SESSION(conn->session,
1853 "failing sc %p itt 0x%x state %d\n",
1854 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001855 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001856 }
1857}
1858
Mike Christie661134a2009-09-05 07:35:33 +05301859/**
1860 * iscsi_suspend_queue - suspend iscsi_queuecommand
1861 * @conn: iscsi conn to stop queueing IO on
1862 *
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001863 * This grabs the session frwd_lock to make sure no one is in
Mike Christie661134a2009-09-05 07:35:33 +05301864 * xmit_task/queuecommand, and then sets suspend to prevent
1865 * new commands from being queued. This only needs to be called
1866 * by offload drivers that need to sync a path like ep disconnect
1867 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1868 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1869 */
1870void iscsi_suspend_queue(struct iscsi_conn *conn)
1871{
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001872 spin_lock_bh(&conn->session->frwd_lock);
Mike Christie661134a2009-09-05 07:35:33 +05301873 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001874 spin_unlock_bh(&conn->session->frwd_lock);
Mike Christie661134a2009-09-05 07:35:33 +05301875}
1876EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1877
1878/**
1879 * iscsi_suspend_tx - suspend iscsi_data_xmit
1880 * @conn: iscsi conn tp stop processing IO on.
1881 *
1882 * This function sets the suspend bit to prevent iscsi_data_xmit
1883 * from sending new IO, and if work is queued on the xmit thread
1884 * it will wait for it to be completed.
1885 */
Mike Christieb40977d2008-05-21 15:54:03 -05001886void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001887{
Mike Christie32ae7632009-03-05 14:46:03 -06001888 struct Scsi_Host *shost = conn->session->host;
1889 struct iscsi_host *ihost = shost_priv(shost);
1890
Mike Christie6724add2007-08-15 01:38:30 -05001891 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001892 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001893 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001894}
Mike Christieb40977d2008-05-21 15:54:03 -05001895EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001896
1897static void iscsi_start_tx(struct iscsi_conn *conn)
1898{
1899 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001900 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001901}
1902
Mike Christie4c48a822009-05-13 17:57:45 -05001903/*
1904 * We want to make sure a ping is in flight. It has timed out.
1905 * And we are not busy processing a pdu that is making
1906 * progress but got started before the ping and is taking a while
1907 * to complete so the ping is just stuck behind it in a queue.
1908 */
1909static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1910{
1911 if (conn->ping_task &&
1912 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1913 (conn->ping_timeout * HZ), jiffies))
1914 return 1;
1915 else
1916 return 0;
1917}
1918
Mike Christied355e572009-06-15 22:11:08 -05001919static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001920{
Mike Christied355e572009-06-15 22:11:08 -05001921 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christie92ed4d62010-02-10 16:51:45 -06001922 struct iscsi_task *task = NULL, *running_task;
Mike Christief6d51802007-12-13 12:43:30 -06001923 struct iscsi_cls_session *cls_session;
1924 struct iscsi_session *session;
1925 struct iscsi_conn *conn;
Mike Christie92ed4d62010-02-10 16:51:45 -06001926 int i;
Mike Christief6d51802007-12-13 12:43:30 -06001927
Mike Christied355e572009-06-15 22:11:08 -05001928 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001929 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001930
Erez Zilberbd2199d2009-06-15 22:11:10 -05001931 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001932
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001933 spin_lock(&session->frwd_lock);
Mike Christiee3d338a2012-01-26 21:13:11 -06001934 task = (struct iscsi_task *)sc->SCp.ptr;
1935 if (!task) {
1936 /*
1937 * Raced with completion. Blk layer has taken ownership
1938 * so let timeout code complete it now.
1939 */
1940 rc = BLK_EH_HANDLED;
1941 goto done;
1942 }
1943
Mike Christief6d51802007-12-13 12:43:30 -06001944 if (session->state != ISCSI_STATE_LOGGED_IN) {
1945 /*
1946 * We are probably in the middle of iscsi recovery so let
1947 * that complete and handle the error.
1948 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001949 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001950 goto done;
1951 }
1952
1953 conn = session->leadconn;
1954 if (!conn) {
1955 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001956 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001957 goto done;
1958 }
1959
Mike Christied355e572009-06-15 22:11:08 -05001960 /*
1961 * If we have sent (at least queued to the network layer) a pdu or
1962 * recvd one for the task since the last timeout ask for
1963 * more time. If on the next timeout we have not made progress
1964 * we can check if it is the task or connection when we send the
1965 * nop as a ping.
1966 */
Mike Christie92ed4d62010-02-10 16:51:45 -06001967 if (time_after(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001968 ISCSI_DBG_EH(session, "Command making progress. Asking "
1969 "scsi-ml for more time to complete. "
Mike Christie92ed4d62010-02-10 16:51:45 -06001970 "Last data xfer at %lu. Last timeout was at "
Erez Zilberbd2199d2009-06-15 22:11:10 -05001971 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001972 task->have_checked_conn = false;
1973 rc = BLK_EH_RESET_TIMER;
1974 goto done;
1975 }
1976
Mike Christief6d51802007-12-13 12:43:30 -06001977 if (!conn->recv_timeout && !conn->ping_timeout)
1978 goto done;
1979 /*
1980 * if the ping timedout then we are in the middle of cleaning up
1981 * and can let the iscsi eh handle it
1982 */
Mike Christie4c48a822009-05-13 17:57:45 -05001983 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001984 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001985 goto done;
1986 }
Mike Christied355e572009-06-15 22:11:08 -05001987
Mike Christie92ed4d62010-02-10 16:51:45 -06001988 for (i = 0; i < conn->session->cmds_max; i++) {
1989 running_task = conn->session->cmds[i];
1990 if (!running_task->sc || running_task == task ||
1991 running_task->state != ISCSI_TASK_RUNNING)
1992 continue;
1993
1994 /*
1995 * Only check if cmds started before this one have made
1996 * progress, or this could never fail
1997 */
1998 if (time_after(running_task->sc->jiffies_at_alloc,
1999 task->sc->jiffies_at_alloc))
2000 continue;
2001
2002 if (time_after(running_task->last_xfer, task->last_timeout)) {
2003 /*
2004 * This task has not made progress, but a task
2005 * started before us has transferred data since
2006 * we started/last-checked. We could be queueing
2007 * too many tasks or the LU is bad.
2008 *
2009 * If the device is bad the cmds ahead of us on
2010 * other devs will complete, and this loop will
2011 * eventually fail starting the scsi eh.
2012 */
2013 ISCSI_DBG_EH(session, "Command has not made progress "
2014 "but commands ahead of it have. "
2015 "Asking scsi-ml for more time to "
2016 "complete. Our last xfer vs running task "
2017 "last xfer %lu/%lu. Last check %lu.\n",
2018 task->last_xfer, running_task->last_xfer,
2019 task->last_timeout);
2020 rc = BLK_EH_RESET_TIMER;
2021 goto done;
2022 }
2023 }
2024
Mike Christied355e572009-06-15 22:11:08 -05002025 /* Assumes nop timeout is shorter than scsi cmd timeout */
2026 if (task->have_checked_conn)
2027 goto done;
2028
Mike Christief6d51802007-12-13 12:43:30 -06002029 /*
Mike Christied355e572009-06-15 22:11:08 -05002030 * Checking the transport already or nop from a cmd timeout still
2031 * running
Mike Christief6d51802007-12-13 12:43:30 -06002032 */
Mike Christied355e572009-06-15 22:11:08 -05002033 if (conn->ping_task) {
2034 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07002035 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05002036 goto done;
2037 }
2038
Mike Christied355e572009-06-15 22:11:08 -05002039 /* Make sure there is a transport check done */
2040 iscsi_send_nopout(conn, NULL);
2041 task->have_checked_conn = true;
2042 rc = BLK_EH_RESET_TIMER;
2043
Mike Christief6d51802007-12-13 12:43:30 -06002044done:
Mike Christied355e572009-06-15 22:11:08 -05002045 if (task)
2046 task->last_timeout = jiffies;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002047 spin_unlock(&session->frwd_lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002048 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2049 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06002050 return rc;
2051}
2052
2053static void iscsi_check_transport_timeouts(unsigned long data)
2054{
2055 struct iscsi_conn *conn = (struct iscsi_conn *)data;
2056 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05002057 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06002058
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002059 spin_lock(&session->frwd_lock);
Mike Christief6d51802007-12-13 12:43:30 -06002060 if (session->state != ISCSI_STATE_LOGGED_IN)
2061 goto done;
2062
Mike Christie4cf10432008-05-07 20:43:52 -05002063 recv_timeout = conn->recv_timeout;
2064 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06002065 goto done;
2066
Mike Christie4cf10432008-05-07 20:43:52 -05002067 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06002068 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05002069
2070 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06002071 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05002072 "expired, recv timeout %d, last rx %lu, "
2073 "last ping %lu, now %lu\n",
2074 conn->ping_timeout, conn->recv_timeout,
2075 last_recv, conn->last_ping, jiffies);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002076 spin_unlock(&session->frwd_lock);
Mike Christief6d51802007-12-13 12:43:30 -06002077 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2078 return;
2079 }
2080
Mike Christie4cf10432008-05-07 20:43:52 -05002081 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05002082 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06002083 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05002084 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05002085 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06002086 } else
Mike Christie4cf10432008-05-07 20:43:52 -05002087 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06002088
Mike Christie1b2c7af2009-03-05 14:45:58 -06002089 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06002090 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06002091done:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002092 spin_unlock(&session->frwd_lock);
Mike Christief6d51802007-12-13 12:43:30 -06002093}
2094
Mike Christie9c19a7d2008-05-21 15:54:09 -05002095static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06002096 struct iscsi_tm *hdr)
2097{
2098 memset(hdr, 0, sizeof(*hdr));
2099 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2100 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2101 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Andy Grover55bdabd2011-06-16 15:57:09 -07002102 hdr->lun = task->lun;
Mike Christie577577d2008-12-02 00:32:05 -06002103 hdr->rtt = task->hdr_itt;
2104 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06002105}
2106
Mike Christie7996a772006-04-06 21:13:41 -05002107int iscsi_eh_abort(struct scsi_cmnd *sc)
2108{
Mike Christie75613522008-05-21 15:53:59 -05002109 struct iscsi_cls_session *cls_session;
2110 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04002111 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002112 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06002113 struct iscsi_tm *hdr;
2114 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05002115
Mike Christie75613522008-05-21 15:53:59 -05002116 cls_session = starget_to_session(scsi_target(sc->device));
2117 session = cls_session->dd_data;
2118
Erez Zilberbd2199d2009-06-15 22:11:10 -05002119 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05002120
Mike Christie6724add2007-08-15 01:38:30 -05002121 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002122 spin_lock_bh(&session->frwd_lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04002123 /*
2124 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2125 * got the command.
2126 */
2127 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002128 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2129 "it completed.\n");
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002130 spin_unlock_bh(&session->frwd_lock);
Mike Christie6724add2007-08-15 01:38:30 -05002131 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04002132 return SUCCESS;
2133 }
2134
Mike Christie7996a772006-04-06 21:13:41 -05002135 /*
2136 * If we are not logged in or we have started a new session
2137 * then let the host reset code handle this
2138 */
Mike Christie843c0a82007-12-13 12:43:20 -06002139 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2140 sc->SCp.phase != session->age) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002141 spin_unlock_bh(&session->frwd_lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002142 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002143 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05002144 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06002145 return FAILED;
2146 }
2147
2148 conn = session->leadconn;
2149 conn->eh_abort_cnt++;
2150 age = session->age;
2151
Mike Christie9c19a7d2008-05-21 15:54:09 -05002152 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002153 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2154 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002155
Mike Christie9c19a7d2008-05-21 15:54:09 -05002156 /* task completed before time out */
2157 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002158 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002159 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002160 }
Mike Christie7996a772006-04-06 21:13:41 -05002161
Mike Christie9c19a7d2008-05-21 15:54:09 -05002162 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002163 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002164 goto success;
2165 }
Mike Christie7996a772006-04-06 21:13:41 -05002166
Mike Christie843c0a82007-12-13 12:43:20 -06002167 /* only have one tmf outstanding at a time */
2168 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002169 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002170 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002171
Mike Christie843c0a82007-12-13 12:43:20 -06002172 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002173 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002174
Mike Christie9c19a7d2008-05-21 15:54:09 -05002175 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002176 rc = FAILED;
2177 goto failed;
2178 }
2179
2180 switch (conn->tmf_state) {
2181 case TMF_SUCCESS:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002182 spin_unlock_bh(&session->frwd_lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002183 /*
2184 * stop tx side incase the target had sent a abort rsp but
2185 * the initiator was still writing out data.
2186 */
Mike Christie6724add2007-08-15 01:38:30 -05002187 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002188 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002189 * we do not stop the recv side because targets have been
2190 * good and have never sent us a successful tmf response
2191 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002192 */
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002193 spin_lock_bh(&session->frwd_lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002194 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002195 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002196 memset(hdr, 0, sizeof(*hdr));
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002197 spin_unlock_bh(&session->frwd_lock);
Mike Christie6724add2007-08-15 01:38:30 -05002198 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002199 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002200 case TMF_TIMEDOUT:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002201 spin_unlock_bh(&session->frwd_lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002202 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie843c0a82007-12-13 12:43:20 -06002203 goto failed_unlocked;
2204 case TMF_NOT_FOUND:
2205 if (!sc->SCp.ptr) {
2206 conn->tmf_state = TMF_INITIAL;
Mike Christie5d12c052009-11-11 16:34:32 -06002207 memset(hdr, 0, sizeof(*hdr));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002208 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002209 ISCSI_DBG_EH(session, "sc completed while abort in "
2210 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002211 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002212 }
2213 /* fall through */
2214 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002215 conn->tmf_state = TMF_INITIAL;
2216 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002217 }
2218
Mike Christie77a23c22007-05-30 12:57:18 -05002219success:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002220 spin_unlock_bh(&session->frwd_lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002221success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002222 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2223 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002224 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002225 return SUCCESS;
2226
2227failed:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002228 spin_unlock_bh(&session->frwd_lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002229failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002230 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2231 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002232 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002233 return FAILED;
2234}
2235EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2236
Mike Christie843c0a82007-12-13 12:43:20 -06002237static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2238{
2239 memset(hdr, 0, sizeof(*hdr));
2240 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2241 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2242 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Andy Grover55bdabd2011-06-16 15:57:09 -07002243 int_to_scsilun(sc->device->lun, &hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002244 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002245}
2246
2247int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2248{
Mike Christie75613522008-05-21 15:53:59 -05002249 struct iscsi_cls_session *cls_session;
2250 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002251 struct iscsi_conn *conn;
2252 struct iscsi_tm *hdr;
2253 int rc = FAILED;
2254
Mike Christie75613522008-05-21 15:53:59 -05002255 cls_session = starget_to_session(scsi_target(sc->device));
2256 session = cls_session->dd_data;
2257
Erez Zilberbd2199d2009-06-15 22:11:10 -05002258 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002259
2260 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002261 spin_lock_bh(&session->frwd_lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002262 /*
2263 * Just check if we are not logged in. We cannot check for
2264 * the phase because the reset could come from a ioctl.
2265 */
2266 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2267 goto unlock;
2268 conn = session->leadconn;
2269
2270 /* only have one tmf outstanding at a time */
2271 if (conn->tmf_state != TMF_INITIAL)
2272 goto unlock;
2273 conn->tmf_state = TMF_QUEUED;
2274
2275 hdr = &conn->tmhdr;
2276 iscsi_prep_lun_reset_pdu(sc, hdr);
2277
Mike Christie9c19a7d2008-05-21 15:54:09 -05002278 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002279 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002280 rc = FAILED;
2281 goto unlock;
2282 }
2283
2284 switch (conn->tmf_state) {
2285 case TMF_SUCCESS:
2286 break;
2287 case TMF_TIMEDOUT:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002288 spin_unlock_bh(&session->frwd_lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002289 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie843c0a82007-12-13 12:43:20 -06002290 goto done;
2291 default:
2292 conn->tmf_state = TMF_INITIAL;
2293 goto unlock;
2294 }
2295
2296 rc = SUCCESS;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002297 spin_unlock_bh(&session->frwd_lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002298
2299 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002300
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002301 spin_lock_bh(&session->frwd_lock);
Mike Christie5d12c052009-11-11 16:34:32 -06002302 memset(hdr, 0, sizeof(*hdr));
Mike Christie3bbaaad2009-05-13 17:57:46 -05002303 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002304 conn->tmf_state = TMF_INITIAL;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002305 spin_unlock_bh(&session->frwd_lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002306
2307 iscsi_start_tx(conn);
2308 goto done;
2309
2310unlock:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002311 spin_unlock_bh(&session->frwd_lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002312done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002313 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2314 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002315 mutex_unlock(&session->eh_mutex);
2316 return rc;
2317}
2318EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2319
Mike Christie3fe5ae82009-11-11 16:34:33 -06002320void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2321{
2322 struct iscsi_session *session = cls_session->dd_data;
2323
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002324 spin_lock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002325 if (session->state != ISCSI_STATE_LOGGED_IN) {
2326 session->state = ISCSI_STATE_RECOVERY_FAILED;
2327 if (session->leadconn)
2328 wake_up(&session->leadconn->ehwait);
2329 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002330 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002331}
2332EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
2333
2334/**
2335 * iscsi_eh_session_reset - drop session and attempt relogin
2336 * @sc: scsi command
2337 *
2338 * This function will wait for a relogin, session termination from
2339 * userspace, or a recovery/replacement timeout.
2340 */
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302341int iscsi_eh_session_reset(struct scsi_cmnd *sc)
Mike Christie3fe5ae82009-11-11 16:34:33 -06002342{
2343 struct iscsi_cls_session *cls_session;
2344 struct iscsi_session *session;
2345 struct iscsi_conn *conn;
2346
2347 cls_session = starget_to_session(scsi_target(sc->device));
2348 session = cls_session->dd_data;
2349 conn = session->leadconn;
2350
2351 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002352 spin_lock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002353 if (session->state == ISCSI_STATE_TERMINATE) {
2354failed:
2355 ISCSI_DBG_EH(session,
2356 "failing session reset: Could not log back into "
2357 "%s, %s [age %d]\n", session->targetname,
2358 conn->persistent_address, session->age);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002359 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002360 mutex_unlock(&session->eh_mutex);
2361 return FAILED;
2362 }
2363
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002364 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002365 mutex_unlock(&session->eh_mutex);
2366 /*
2367 * we drop the lock here but the leadconn cannot be destoyed while
2368 * we are in the scsi eh
2369 */
Mike Christiedf4da5c2010-12-31 02:22:18 -06002370 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002371
2372 ISCSI_DBG_EH(session, "wait for relogin\n");
2373 wait_event_interruptible(conn->ehwait,
2374 session->state == ISCSI_STATE_TERMINATE ||
2375 session->state == ISCSI_STATE_LOGGED_IN ||
2376 session->state == ISCSI_STATE_RECOVERY_FAILED);
2377 if (signal_pending(current))
2378 flush_signals(current);
2379
2380 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002381 spin_lock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002382 if (session->state == ISCSI_STATE_LOGGED_IN) {
2383 ISCSI_DBG_EH(session,
2384 "session reset succeeded for %s,%s\n",
2385 session->targetname, conn->persistent_address);
2386 } else
2387 goto failed;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002388 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002389 mutex_unlock(&session->eh_mutex);
2390 return SUCCESS;
2391}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302392EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002393
2394static void iscsi_prep_tgt_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2395{
2396 memset(hdr, 0, sizeof(*hdr));
2397 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2398 hdr->flags = ISCSI_TM_FUNC_TARGET_WARM_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2399 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2400 hdr->rtt = RESERVED_ITT;
2401}
2402
2403/**
2404 * iscsi_eh_target_reset - reset target
2405 * @sc: scsi command
2406 *
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302407 * This will attempt to send a warm target reset.
Mike Christie3fe5ae82009-11-11 16:34:33 -06002408 */
2409int iscsi_eh_target_reset(struct scsi_cmnd *sc)
2410{
2411 struct iscsi_cls_session *cls_session;
2412 struct iscsi_session *session;
2413 struct iscsi_conn *conn;
2414 struct iscsi_tm *hdr;
2415 int rc = FAILED;
2416
2417 cls_session = starget_to_session(scsi_target(sc->device));
2418 session = cls_session->dd_data;
2419
2420 ISCSI_DBG_EH(session, "tgt Reset [sc %p tgt %s]\n", sc,
2421 session->targetname);
2422
2423 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002424 spin_lock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002425 /*
2426 * Just check if we are not logged in. We cannot check for
2427 * the phase because the reset could come from a ioctl.
2428 */
2429 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2430 goto unlock;
2431 conn = session->leadconn;
2432
2433 /* only have one tmf outstanding at a time */
2434 if (conn->tmf_state != TMF_INITIAL)
2435 goto unlock;
2436 conn->tmf_state = TMF_QUEUED;
2437
2438 hdr = &conn->tmhdr;
2439 iscsi_prep_tgt_reset_pdu(sc, hdr);
2440
2441 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2442 session->tgt_reset_timeout)) {
2443 rc = FAILED;
2444 goto unlock;
2445 }
2446
2447 switch (conn->tmf_state) {
2448 case TMF_SUCCESS:
2449 break;
2450 case TMF_TIMEDOUT:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002451 spin_unlock_bh(&session->frwd_lock);
Mike Christiedf4da5c2010-12-31 02:22:18 -06002452 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002453 goto done;
2454 default:
2455 conn->tmf_state = TMF_INITIAL;
2456 goto unlock;
2457 }
2458
2459 rc = SUCCESS;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002460 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002461
2462 iscsi_suspend_tx(conn);
2463
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002464 spin_lock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002465 memset(hdr, 0, sizeof(*hdr));
2466 fail_scsi_tasks(conn, -1, DID_ERROR);
2467 conn->tmf_state = TMF_INITIAL;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002468 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002469
2470 iscsi_start_tx(conn);
2471 goto done;
2472
2473unlock:
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002474 spin_unlock_bh(&session->frwd_lock);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002475done:
2476 ISCSI_DBG_EH(session, "tgt %s reset result = %s\n", session->targetname,
2477 rc == SUCCESS ? "SUCCESS" : "FAILED");
2478 mutex_unlock(&session->eh_mutex);
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302479 return rc;
2480}
2481EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002482
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302483/**
2484 * iscsi_eh_recover_target - reset target and possibly the session
2485 * @sc: scsi command
2486 *
2487 * This will attempt to send a warm target reset. If that fails,
2488 * we will escalate to ERL0 session recovery.
2489 */
2490int iscsi_eh_recover_target(struct scsi_cmnd *sc)
2491{
2492 int rc;
2493
2494 rc = iscsi_eh_target_reset(sc);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002495 if (rc == FAILED)
2496 rc = iscsi_eh_session_reset(sc);
2497 return rc;
2498}
Jayamohan Kallickal309ce152010-02-20 08:02:10 +05302499EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
Mike Christie3fe5ae82009-11-11 16:34:33 -06002500
Olaf Kirch63203772007-12-13 12:43:25 -06002501/*
2502 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2503 * should be accessed via kfifo_{get,put} on q->queue.
2504 * Optionally, the caller can obtain the array of object pointers
2505 * by passing in a non-NULL @items pointer
2506 */
Mike Christie7996a772006-04-06 21:13:41 -05002507int
Olaf Kirch63203772007-12-13 12:43:25 -06002508iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002509{
Olaf Kirch63203772007-12-13 12:43:25 -06002510 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002511
Olaf Kirch63203772007-12-13 12:43:25 -06002512 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002513
2514 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002515
2516 /* If the user passed an items pointer, he wants a copy of
2517 * the array. */
2518 if (items)
2519 num_arrays++;
2520 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2521 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002522 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002523
Stefani Seiboldc1e13f22009-12-21 14:37:27 -08002524 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002525
2526 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002527 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002528 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002529 q->max = i;
2530 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002531 }
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002532 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05002533 }
Olaf Kirch63203772007-12-13 12:43:25 -06002534
2535 if (items) {
2536 *items = q->pool + max;
2537 memcpy(*items, q->pool, max * sizeof(void *));
2538 }
2539
Mike Christie7996a772006-04-06 21:13:41 -05002540 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002541
2542enomem:
2543 iscsi_pool_free(q);
2544 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002545}
2546EXPORT_SYMBOL_GPL(iscsi_pool_init);
2547
Olaf Kirch63203772007-12-13 12:43:25 -06002548void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002549{
2550 int i;
2551
2552 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002553 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002554 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05002555}
2556EXPORT_SYMBOL_GPL(iscsi_pool_free);
2557
Mike Christiea4804cd2008-05-21 15:54:00 -05002558/**
2559 * iscsi_host_add - add host to system
2560 * @shost: scsi host
2561 * @pdev: parent device
2562 *
2563 * This should be called by partial offload and software iscsi drivers
2564 * to add a host to the system.
2565 */
2566int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002567{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002568 if (!shost->can_queue)
2569 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2570
Mike Christie4d108352009-03-05 14:46:04 -06002571 if (!shost->cmd_per_lun)
2572 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2573
Mike Christie308cec12009-02-06 12:06:20 -06002574 if (!shost->transportt->eh_timed_out)
2575 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002576 return scsi_add_host(shost, pdev);
2577}
2578EXPORT_SYMBOL_GPL(iscsi_host_add);
2579
2580/**
2581 * iscsi_host_alloc - allocate a host and driver data
2582 * @sht: scsi host template
2583 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002584 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002585 *
2586 * This should be called by partial offload and software iscsi drivers.
2587 * To access the driver specific memory use the iscsi_host_priv() macro.
2588 */
2589struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002590 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002591{
2592 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002593 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002594
2595 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2596 if (!shost)
2597 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002598 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002599
2600 if (xmit_can_sleep) {
2601 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2602 "iscsi_q_%d", shost->host_no);
2603 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2604 if (!ihost->workq)
2605 goto free_host;
2606 }
2607
Mike Christiee5bd7b52008-09-24 11:46:10 -05002608 spin_lock_init(&ihost->lock);
2609 ihost->state = ISCSI_HOST_SETUP;
2610 ihost->num_sessions = 0;
2611 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002612 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002613
2614free_host:
2615 scsi_host_put(shost);
2616 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002617}
Mike Christiea4804cd2008-05-21 15:54:00 -05002618EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002619
Mike Christiee5bd7b52008-09-24 11:46:10 -05002620static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2621{
Mike Christie40a06e72009-03-05 14:46:05 -06002622 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002623}
2624
Mike Christiea4804cd2008-05-21 15:54:00 -05002625/**
2626 * iscsi_host_remove - remove host and sessions
2627 * @shost: scsi host
2628 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002629 * If there are any sessions left, this will initiate the removal and wait
2630 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002631 */
2632void iscsi_host_remove(struct Scsi_Host *shost)
2633{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002634 struct iscsi_host *ihost = shost_priv(shost);
2635 unsigned long flags;
2636
2637 spin_lock_irqsave(&ihost->lock, flags);
2638 ihost->state = ISCSI_HOST_REMOVED;
2639 spin_unlock_irqrestore(&ihost->lock, flags);
2640
2641 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2642 wait_event_interruptible(ihost->session_removal_wq,
2643 ihost->num_sessions == 0);
2644 if (signal_pending(current))
2645 flush_signals(current);
2646
Mike Christiea4804cd2008-05-21 15:54:00 -05002647 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002648 if (ihost->workq)
2649 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002650}
2651EXPORT_SYMBOL_GPL(iscsi_host_remove);
2652
2653void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002654{
2655 struct iscsi_host *ihost = shost_priv(shost);
2656
2657 kfree(ihost->netdev);
2658 kfree(ihost->hwaddress);
2659 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002660 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002661}
Mike Christiea4804cd2008-05-21 15:54:00 -05002662EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002663
Mike Christiee5bd7b52008-09-24 11:46:10 -05002664static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2665{
2666 struct iscsi_host *ihost = shost_priv(shost);
2667 unsigned long flags;
2668
2669 shost = scsi_host_get(shost);
2670 if (!shost) {
2671 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2672 "of session teardown event because host already "
2673 "removed.\n");
2674 return;
2675 }
2676
2677 spin_lock_irqsave(&ihost->lock, flags);
2678 ihost->num_sessions--;
2679 if (ihost->num_sessions == 0)
2680 wake_up(&ihost->session_removal_wq);
2681 spin_unlock_irqrestore(&ihost->lock, flags);
2682 scsi_host_put(shost);
2683}
2684
Mike Christie75613522008-05-21 15:53:59 -05002685/**
2686 * iscsi_session_setup - create iscsi cls session and host and session
2687 * @iscsit: iscsi transport template
2688 * @shost: scsi host
2689 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002690 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002691 * @initial_cmdsn: initial CmdSN
2692 *
2693 * This can be used by software iscsi_transports that allocate
2694 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002695 *
2696 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2697 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2698 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002699 */
2700struct iscsi_cls_session *
2701iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302702 uint16_t cmds_max, int dd_size, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002703 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002704{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002705 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002706 struct iscsi_session *session;
2707 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002708 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002709 unsigned long flags;
2710
2711 spin_lock_irqsave(&ihost->lock, flags);
2712 if (ihost->state == ISCSI_HOST_REMOVED) {
2713 spin_unlock_irqrestore(&ihost->lock, flags);
2714 return NULL;
2715 }
2716 ihost->num_sessions++;
2717 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002718
2719 if (!total_cmds)
2720 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002721 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002722 * The iscsi layer needs some tasks for nop handling and tmfs,
2723 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2724 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002725 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002726 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2727 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2728 "must be a power of two that is at least %d.\n",
2729 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002730 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002731 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002732
2733 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2734 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2735 "must be a power of 2 less than or equal to %d.\n",
2736 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2737 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2738 }
2739
2740 if (!is_power_of_2(total_cmds)) {
2741 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2742 "must be a power of 2.\n", total_cmds);
2743 total_cmds = rounddown_pow_of_two(total_cmds);
2744 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2745 return NULL;
2746 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2747 total_cmds);
2748 }
2749 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002750
Mike Christie5d91e202008-05-21 15:54:01 -05002751 cls_session = iscsi_alloc_session(shost, iscsit,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302752 sizeof(struct iscsi_session) +
2753 dd_size);
Mike Christie75613522008-05-21 15:53:59 -05002754 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002755 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002756 session = cls_session->dd_data;
2757 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002758 session->host = shost;
2759 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002760 session->fast_abort = 1;
Mike Christie3fe5ae82009-11-11 16:34:33 -06002761 session->tgt_reset_timeout = 30;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002762 session->lu_reset_timeout = 15;
2763 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002764 session->scsi_cmds_max = scsi_cmds;
2765 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002766 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002767 session->exp_cmdsn = initial_cmdsn + 1;
2768 session->max_cmdsn = initial_cmdsn + 1;
2769 session->max_r2t = 1;
2770 session->tt = iscsit;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302771 session->dd_data = cls_session->dd_data + sizeof(*session);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002772
Mike Christie6724add2007-08-15 01:38:30 -05002773 mutex_init(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002774 spin_lock_init(&session->frwd_lock);
2775 spin_lock_init(&session->back_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002776
2777 /* initialize SCSI PDU commands pool */
2778 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2779 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002780 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002781 goto cmdpool_alloc_fail;
2782
2783 /* pre-format cmds pool with ITT */
2784 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002785 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002786
Mike Christie9c19a7d2008-05-21 15:54:09 -05002787 if (cmd_task_size)
2788 task->dd_data = &task[1];
2789 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002790 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002791 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002792 }
2793
Mike Christief53a88d2006-06-28 12:00:27 -05002794 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002795 goto module_get_fail;
2796
Mike Christie79706342008-05-21 15:54:12 -05002797 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002798 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002799
Mike Christie7996a772006-04-06 21:13:41 -05002800 return cls_session;
2801
2802cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002803 module_put(iscsit->owner);
2804module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002805 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002806cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002807 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002808dec_session_count:
2809 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002810 return NULL;
2811}
2812EXPORT_SYMBOL_GPL(iscsi_session_setup);
2813
2814/**
2815 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002816 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002817 *
Mike Christie75613522008-05-21 15:53:59 -05002818 * The driver must have called iscsi_remove_session before
2819 * calling this.
2820 */
Mike Christie7996a772006-04-06 21:13:41 -05002821void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2822{
Mike Christie75613522008-05-21 15:53:59 -05002823 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002824 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002825 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002826
Olaf Kirch63203772007-12-13 12:43:25 -06002827 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002828
Mike Christieb2c64162007-05-30 12:57:16 -05002829 kfree(session->password);
2830 kfree(session->password_in);
2831 kfree(session->username);
2832 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002833 kfree(session->targetname);
Vikas Chaudhary3c5c4802012-01-19 03:06:53 -08002834 kfree(session->targetalias);
Mike Christie88dfd342008-05-21 15:54:16 -05002835 kfree(session->initiatorname);
Eddie Wai3b9373e2013-06-20 10:21:26 -07002836 kfree(session->boot_root);
2837 kfree(session->boot_nic);
2838 kfree(session->boot_target);
Mike Christie88dfd342008-05-21 15:54:16 -05002839 kfree(session->ifacename);
Adheer Chandravanshif8525eb2013-07-01 05:54:12 -04002840 kfree(session->portal_type);
2841 kfree(session->discovery_parent_type);
Mike Christief3ff0c32006-07-24 15:47:50 -05002842
Mike Christie75613522008-05-21 15:53:59 -05002843 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002844 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002845 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002846}
2847EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2848
2849/**
2850 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2851 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002852 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002853 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002854 */
Mike Christie7996a772006-04-06 21:13:41 -05002855struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002856iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2857 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002858{
Mike Christie75613522008-05-21 15:53:59 -05002859 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002860 struct iscsi_conn *conn;
2861 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002862 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002863
Mike Christie5d91e202008-05-21 15:54:01 -05002864 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2865 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002866 if (!cls_conn)
2867 return NULL;
2868 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002869 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002870
Mike Christie5d91e202008-05-21 15:54:01 -05002871 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002872 conn->session = session;
2873 conn->cls_conn = cls_conn;
2874 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2875 conn->id = conn_idx;
2876 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002877 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002878
2879 init_timer(&conn->transport_timer);
2880 conn->transport_timer.data = (unsigned long)conn;
2881 conn->transport_timer.function = iscsi_check_transport_timeouts;
2882
Mike Christie843c0a82007-12-13 12:43:20 -06002883 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002884 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002885 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002886 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002887
Mike Christie9c19a7d2008-05-21 15:54:09 -05002888 /* allocate login_task used for the login/text sequences */
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002889 spin_lock_bh(&session->frwd_lock);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002890 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002891 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002892 sizeof(void*))) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002893 spin_unlock_bh(&session->frwd_lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002894 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002895 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002896 spin_unlock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002897
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002898 data = (char *) __get_free_pages(GFP_KERNEL,
2899 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002900 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002901 goto login_task_data_alloc_fail;
2902 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002903
Mike Christie843c0a82007-12-13 12:43:20 -06002904 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002905 init_waitqueue_head(&conn->ehwait);
2906
2907 return cls_conn;
2908
Mike Christie9c19a7d2008-05-21 15:54:09 -05002909login_task_data_alloc_fail:
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002910 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002911 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002912login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002913 iscsi_destroy_conn(cls_conn);
2914 return NULL;
2915}
2916EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2917
2918/**
2919 * iscsi_conn_teardown - teardown iscsi connection
2920 * cls_conn: iscsi class connection
2921 *
2922 * TODO: we may need to make this into a two step process
2923 * like scsi-mls remove + put host
2924 */
2925void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2926{
2927 struct iscsi_conn *conn = cls_conn->dd_data;
2928 struct iscsi_session *session = conn->session;
2929 unsigned long flags;
2930
Mike Christief6d51802007-12-13 12:43:30 -06002931 del_timer_sync(&conn->transport_timer);
2932
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002933 spin_lock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002934 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2935 if (session->leadconn == conn) {
2936 /*
2937 * leading connection? then give up on recovery.
2938 */
2939 session->state = ISCSI_STATE_TERMINATE;
2940 wake_up(&conn->ehwait);
2941 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002942 spin_unlock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002943
Mike Christie7996a772006-04-06 21:13:41 -05002944 /*
2945 * Block until all in-progress commands for this connection
2946 * time out or fail.
2947 */
2948 for (;;) {
2949 spin_lock_irqsave(session->host->host_lock, flags);
2950 if (!session->host->host_busy) { /* OK for ERL == 0 */
2951 spin_unlock_irqrestore(session->host->host_lock, flags);
2952 break;
2953 }
2954 spin_unlock_irqrestore(session->host->host_lock, flags);
2955 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002956 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2957 "host_busy %d host_failed %d\n",
2958 session->host->host_busy,
2959 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002960 /*
2961 * force eh_abort() to unblock
2962 */
2963 wake_up(&conn->ehwait);
2964 }
2965
Mike Christie779ea122007-02-28 17:32:15 -06002966 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002967 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002968
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002969 spin_lock_bh(&session->frwd_lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002970 free_pages((unsigned long) conn->data,
2971 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002972 kfree(conn->persistent_address);
Adheer Chandravanshiae56ff42013-11-22 05:28:21 -05002973 kfree(conn->local_ipaddr);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002974 /* regular RX path uses back_lock */
2975 spin_lock_bh(&session->back_lock);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002976 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002977 sizeof(void*));
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002978 spin_unlock_bh(&session->back_lock);
Mike Christiee0726402007-07-26 12:46:48 -05002979 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002980 session->leadconn = NULL;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06002981 spin_unlock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002982
Mike Christie7996a772006-04-06 21:13:41 -05002983 iscsi_destroy_conn(cls_conn);
2984}
2985EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2986
2987int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2988{
2989 struct iscsi_conn *conn = cls_conn->dd_data;
2990 struct iscsi_session *session = conn->session;
2991
Mike Christieffd04362006-08-31 18:09:24 -04002992 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002993 iscsi_conn_printk(KERN_ERR, conn,
2994 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002995 return -EPERM;
2996 }
2997
Mike Christiedb98ccd2006-08-31 18:09:31 -04002998 if ((session->imm_data_en || !session->initial_r2t_en) &&
2999 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06003000 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
3001 "first_burst %d max_burst %d\n",
3002 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04003003 return -EINVAL;
3004 }
3005
Mike Christief6d51802007-12-13 12:43:30 -06003006 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06003007 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3008 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06003009 conn->recv_timeout = 5;
3010 }
3011
3012 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06003013 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3014 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06003015 conn->ping_timeout = 5;
3016 }
3017
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003018 spin_lock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05003019 conn->c_stage = ISCSI_CONN_STARTED;
3020 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05003021 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05003022
Mike Christief6d51802007-12-13 12:43:30 -06003023 conn->last_recv = jiffies;
3024 conn->last_ping = jiffies;
3025 if (conn->recv_timeout && conn->ping_timeout)
3026 mod_timer(&conn->transport_timer,
3027 jiffies + (conn->recv_timeout * HZ));
3028
Mike Christie7996a772006-04-06 21:13:41 -05003029 switch(conn->stop_stage) {
3030 case STOP_CONN_RECOVER:
3031 /*
3032 * unblock eh_abort() if it is blocked. re-try all
3033 * commands after successful recovery
3034 */
Mike Christie7996a772006-04-06 21:13:41 -05003035 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06003036 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05003037 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06003038 if (session->age == 16)
3039 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06003040 break;
Mike Christie7996a772006-04-06 21:13:41 -05003041 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05003042 conn->stop_stage = 0;
3043 break;
Mike Christie7996a772006-04-06 21:13:41 -05003044 default:
3045 break;
3046 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003047 spin_unlock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05003048
Mike Christie75613522008-05-21 15:53:59 -05003049 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06003050 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05003051 return 0;
3052}
3053EXPORT_SYMBOL_GPL(iscsi_conn_start);
3054
3055static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05003056fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05003057{
Mike Christie3bbaaad2009-05-13 17:57:46 -05003058 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05003059 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05003060
Mike Christie3bbaaad2009-05-13 17:57:46 -05003061 for (i = 0; i < conn->session->cmds_max; i++) {
3062 task = conn->session->cmds[i];
3063 if (task->sc)
3064 continue;
3065
3066 if (task->state == ISCSI_TASK_FREE)
3067 continue;
3068
3069 ISCSI_DBG_SESSION(conn->session,
3070 "failing mgmt itt 0x%x state %d\n",
3071 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05003072 state = ISCSI_TASK_ABRT_SESS_RECOV;
3073 if (task->state == ISCSI_TASK_PENDING)
3074 state = ISCSI_TASK_COMPLETED;
3075 iscsi_complete_task(task, state);
3076
Mike Christie7996a772006-04-06 21:13:41 -05003077 }
Mike Christie7996a772006-04-06 21:13:41 -05003078}
3079
Mike Christie656cffc2006-05-18 20:31:42 -05003080static void iscsi_start_session_recovery(struct iscsi_session *session,
3081 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05003082{
Mike Christieed2abc72006-05-02 19:46:40 -05003083 int old_stop_stage;
3084
Mike Christie6724add2007-08-15 01:38:30 -05003085 mutex_lock(&session->eh_mutex);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003086 spin_lock_bh(&session->frwd_lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003087 if (conn->stop_stage == STOP_CONN_TERM) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003088 spin_unlock_bh(&session->frwd_lock);
Mike Christie6724add2007-08-15 01:38:30 -05003089 mutex_unlock(&session->eh_mutex);
3090 return;
3091 }
3092
3093 /*
Mike Christieed2abc72006-05-02 19:46:40 -05003094 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05003095 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05003096 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05003097 */
Mike Christie67a61112006-05-30 00:37:20 -05003098 if (flag == STOP_CONN_TERM)
3099 session->state = ISCSI_STATE_TERMINATE;
3100 else if (conn->stop_stage != STOP_CONN_RECOVER)
3101 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie4ae0a6c2010-03-09 14:14:51 -06003102
3103 old_stop_stage = conn->stop_stage;
3104 conn->stop_stage = flag;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003105 spin_unlock_bh(&session->frwd_lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003106
Mike Christie26013ad2009-05-13 17:57:43 -05003107 del_timer_sync(&conn->transport_timer);
3108 iscsi_suspend_tx(conn);
3109
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003110 spin_lock_bh(&session->frwd_lock);
Mike Christie67a61112006-05-30 00:37:20 -05003111 conn->c_stage = ISCSI_CONN_STOPPED;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003112 spin_unlock_bh(&session->frwd_lock);
Mike Christie6724add2007-08-15 01:38:30 -05003113
Mike Christie7996a772006-04-06 21:13:41 -05003114 /*
3115 * for connection level recovery we should not calculate
3116 * header digest. conn->hdr_size used for optimization
3117 * in hdr_extract() and will be re-negotiated at
3118 * set_param() time.
3119 */
3120 if (flag == STOP_CONN_RECOVER) {
3121 conn->hdrdgst_en = 0;
3122 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05003123 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05003124 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06003125 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05003126 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05003127 }
Mike Christie7996a772006-04-06 21:13:41 -05003128 }
Mike Christie656cffc2006-05-18 20:31:42 -05003129
Mike Christie656cffc2006-05-18 20:31:42 -05003130 /*
3131 * flush queues.
3132 */
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003133 spin_lock_bh(&session->frwd_lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05003134 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05003135 fail_mgmt_tasks(session, conn);
Mike Christie5d12c052009-11-11 16:34:32 -06003136 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003137 spin_unlock_bh(&session->frwd_lock);
Mike Christie6724add2007-08-15 01:38:30 -05003138 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003139}
Mike Christie7996a772006-04-06 21:13:41 -05003140
3141void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3142{
3143 struct iscsi_conn *conn = cls_conn->dd_data;
3144 struct iscsi_session *session = conn->session;
3145
3146 switch (flag) {
3147 case STOP_CONN_RECOVER:
3148 case STOP_CONN_TERM:
3149 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05003150 break;
Mike Christie7996a772006-04-06 21:13:41 -05003151 default:
Mike Christie322d7392008-01-31 13:36:52 -06003152 iscsi_conn_printk(KERN_ERR, conn,
3153 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05003154 }
3155}
3156EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3157
3158int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3159 struct iscsi_cls_conn *cls_conn, int is_leading)
3160{
Mike Christie75613522008-05-21 15:53:59 -05003161 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04003162 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05003163
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003164 spin_lock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05003165 if (is_leading)
3166 session->leadconn = conn;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06003167 spin_unlock_bh(&session->frwd_lock);
Mike Christie7996a772006-04-06 21:13:41 -05003168
3169 /*
3170 * Unblock xmitworker(), Login Phase will pass through.
3171 */
3172 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3173 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3174 return 0;
3175}
3176EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3177
Adheer Chandravanshiadaf6992013-03-22 07:41:30 -04003178int iscsi_switch_str_param(char **param, char *new_val_buf)
Mike Christie5700b1a2009-05-13 17:57:40 -05003179{
3180 char *new_val;
3181
3182 if (*param) {
3183 if (!strcmp(*param, new_val_buf))
3184 return 0;
3185 }
3186
3187 new_val = kstrdup(new_val_buf, GFP_NOIO);
3188 if (!new_val)
3189 return -ENOMEM;
3190
3191 kfree(*param);
3192 *param = new_val;
3193 return 0;
3194}
Adheer Chandravanshiadaf6992013-03-22 07:41:30 -04003195EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
Mike Christiea54a52c2006-06-28 12:00:23 -05003196
3197int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3198 enum iscsi_param param, char *buf, int buflen)
3199{
3200 struct iscsi_conn *conn = cls_conn->dd_data;
3201 struct iscsi_session *session = conn->session;
Or Gerlitz6a06a4b2013-08-08 13:44:29 +03003202 int val;
Mike Christiea54a52c2006-06-28 12:00:23 -05003203
3204 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003205 case ISCSI_PARAM_FAST_ABORT:
3206 sscanf(buf, "%d", &session->fast_abort);
3207 break;
Mike Christief6d51802007-12-13 12:43:30 -06003208 case ISCSI_PARAM_ABORT_TMO:
3209 sscanf(buf, "%d", &session->abort_timeout);
3210 break;
3211 case ISCSI_PARAM_LU_RESET_TMO:
3212 sscanf(buf, "%d", &session->lu_reset_timeout);
3213 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003214 case ISCSI_PARAM_TGT_RESET_TMO:
3215 sscanf(buf, "%d", &session->tgt_reset_timeout);
3216 break;
Mike Christief6d51802007-12-13 12:43:30 -06003217 case ISCSI_PARAM_PING_TMO:
3218 sscanf(buf, "%d", &conn->ping_timeout);
3219 break;
3220 case ISCSI_PARAM_RECV_TMO:
3221 sscanf(buf, "%d", &conn->recv_timeout);
3222 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003223 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3224 sscanf(buf, "%d", &conn->max_recv_dlength);
3225 break;
3226 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3227 sscanf(buf, "%d", &conn->max_xmit_dlength);
3228 break;
3229 case ISCSI_PARAM_HDRDGST_EN:
3230 sscanf(buf, "%d", &conn->hdrdgst_en);
3231 break;
3232 case ISCSI_PARAM_DATADGST_EN:
3233 sscanf(buf, "%d", &conn->datadgst_en);
3234 break;
3235 case ISCSI_PARAM_INITIAL_R2T_EN:
3236 sscanf(buf, "%d", &session->initial_r2t_en);
3237 break;
3238 case ISCSI_PARAM_MAX_R2T:
Mike Christie1304be52012-01-26 21:13:10 -06003239 sscanf(buf, "%hu", &session->max_r2t);
Mike Christiea54a52c2006-06-28 12:00:23 -05003240 break;
3241 case ISCSI_PARAM_IMM_DATA_EN:
3242 sscanf(buf, "%d", &session->imm_data_en);
3243 break;
3244 case ISCSI_PARAM_FIRST_BURST:
3245 sscanf(buf, "%d", &session->first_burst);
3246 break;
3247 case ISCSI_PARAM_MAX_BURST:
3248 sscanf(buf, "%d", &session->max_burst);
3249 break;
3250 case ISCSI_PARAM_PDU_INORDER_EN:
3251 sscanf(buf, "%d", &session->pdu_inorder_en);
3252 break;
3253 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3254 sscanf(buf, "%d", &session->dataseq_inorder_en);
3255 break;
3256 case ISCSI_PARAM_ERL:
3257 sscanf(buf, "%d", &session->erl);
3258 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003259 case ISCSI_PARAM_EXP_STATSN:
3260 sscanf(buf, "%u", &conn->exp_statsn);
3261 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003262 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003263 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003264 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003265 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003266 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05003267 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003268 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003269 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003270 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003271 return iscsi_switch_str_param(&session->targetname, buf);
Vikas Chaudhary3c5c4802012-01-19 03:06:53 -08003272 case ISCSI_PARAM_TARGET_ALIAS:
3273 return iscsi_switch_str_param(&session->targetalias, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003274 case ISCSI_PARAM_TPGT:
3275 sscanf(buf, "%d", &session->tpgt);
3276 break;
3277 case ISCSI_PARAM_PERSISTENT_PORT:
3278 sscanf(buf, "%d", &conn->persistent_port);
3279 break;
3280 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003281 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003282 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003283 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003284 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003285 return iscsi_switch_str_param(&session->initiatorname, buf);
Eddie Wai3b9373e2013-06-20 10:21:26 -07003286 case ISCSI_PARAM_BOOT_ROOT:
3287 return iscsi_switch_str_param(&session->boot_root, buf);
3288 case ISCSI_PARAM_BOOT_NIC:
3289 return iscsi_switch_str_param(&session->boot_nic, buf);
3290 case ISCSI_PARAM_BOOT_TARGET:
3291 return iscsi_switch_str_param(&session->boot_target, buf);
Adheer Chandravanshif8525eb2013-07-01 05:54:12 -04003292 case ISCSI_PARAM_PORTAL_TYPE:
3293 return iscsi_switch_str_param(&session->portal_type, buf);
3294 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3295 return iscsi_switch_str_param(&session->discovery_parent_type,
3296 buf);
Or Gerlitz6a06a4b2013-08-08 13:44:29 +03003297 case ISCSI_PARAM_DISCOVERY_SESS:
3298 sscanf(buf, "%d", &val);
3299 session->discovery_sess = !!val;
3300 break;
Adheer Chandravanshiae56ff42013-11-22 05:28:21 -05003301 case ISCSI_PARAM_LOCAL_IPADDR:
3302 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003303 default:
3304 return -ENOSYS;
3305 }
3306
3307 return 0;
3308}
3309EXPORT_SYMBOL_GPL(iscsi_set_param);
3310
3311int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3312 enum iscsi_param param, char *buf)
3313{
Mike Christie75613522008-05-21 15:53:59 -05003314 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003315 int len;
3316
3317 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003318 case ISCSI_PARAM_FAST_ABORT:
3319 len = sprintf(buf, "%d\n", session->fast_abort);
3320 break;
Mike Christief6d51802007-12-13 12:43:30 -06003321 case ISCSI_PARAM_ABORT_TMO:
3322 len = sprintf(buf, "%d\n", session->abort_timeout);
3323 break;
3324 case ISCSI_PARAM_LU_RESET_TMO:
3325 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3326 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003327 case ISCSI_PARAM_TGT_RESET_TMO:
3328 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3329 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003330 case ISCSI_PARAM_INITIAL_R2T_EN:
3331 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3332 break;
3333 case ISCSI_PARAM_MAX_R2T:
3334 len = sprintf(buf, "%hu\n", session->max_r2t);
3335 break;
3336 case ISCSI_PARAM_IMM_DATA_EN:
3337 len = sprintf(buf, "%d\n", session->imm_data_en);
3338 break;
3339 case ISCSI_PARAM_FIRST_BURST:
3340 len = sprintf(buf, "%u\n", session->first_burst);
3341 break;
3342 case ISCSI_PARAM_MAX_BURST:
3343 len = sprintf(buf, "%u\n", session->max_burst);
3344 break;
3345 case ISCSI_PARAM_PDU_INORDER_EN:
3346 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3347 break;
3348 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3349 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3350 break;
Adheer Chandravanshi5f09e1f2013-07-22 07:46:10 -04003351 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3352 len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
3353 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003354 case ISCSI_PARAM_ERL:
3355 len = sprintf(buf, "%d\n", session->erl);
3356 break;
3357 case ISCSI_PARAM_TARGET_NAME:
3358 len = sprintf(buf, "%s\n", session->targetname);
3359 break;
Vikas Chaudhary3c5c4802012-01-19 03:06:53 -08003360 case ISCSI_PARAM_TARGET_ALIAS:
3361 len = sprintf(buf, "%s\n", session->targetalias);
3362 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003363 case ISCSI_PARAM_TPGT:
3364 len = sprintf(buf, "%d\n", session->tpgt);
3365 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003366 case ISCSI_PARAM_USERNAME:
3367 len = sprintf(buf, "%s\n", session->username);
3368 break;
3369 case ISCSI_PARAM_USERNAME_IN:
3370 len = sprintf(buf, "%s\n", session->username_in);
3371 break;
3372 case ISCSI_PARAM_PASSWORD:
3373 len = sprintf(buf, "%s\n", session->password);
3374 break;
3375 case ISCSI_PARAM_PASSWORD_IN:
3376 len = sprintf(buf, "%s\n", session->password_in);
3377 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003378 case ISCSI_PARAM_IFACE_NAME:
3379 len = sprintf(buf, "%s\n", session->ifacename);
3380 break;
3381 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003382 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003383 break;
Eddie Wai3b9373e2013-06-20 10:21:26 -07003384 case ISCSI_PARAM_BOOT_ROOT:
3385 len = sprintf(buf, "%s\n", session->boot_root);
3386 break;
3387 case ISCSI_PARAM_BOOT_NIC:
3388 len = sprintf(buf, "%s\n", session->boot_nic);
3389 break;
3390 case ISCSI_PARAM_BOOT_TARGET:
3391 len = sprintf(buf, "%s\n", session->boot_target);
Adheer Chandravanshi9a2307b2013-07-22 07:46:09 -04003392 break;
Adheer Chandravanshif8525eb2013-07-01 05:54:12 -04003393 case ISCSI_PARAM_AUTO_SND_TGT_DISABLE:
3394 len = sprintf(buf, "%u\n", session->auto_snd_tgt_disable);
3395 break;
3396 case ISCSI_PARAM_DISCOVERY_SESS:
3397 len = sprintf(buf, "%u\n", session->discovery_sess);
3398 break;
3399 case ISCSI_PARAM_PORTAL_TYPE:
3400 len = sprintf(buf, "%s\n", session->portal_type);
3401 break;
3402 case ISCSI_PARAM_CHAP_AUTH_EN:
3403 len = sprintf(buf, "%u\n", session->chap_auth_en);
3404 break;
3405 case ISCSI_PARAM_DISCOVERY_LOGOUT_EN:
3406 len = sprintf(buf, "%u\n", session->discovery_logout_en);
3407 break;
3408 case ISCSI_PARAM_BIDI_CHAP_EN:
3409 len = sprintf(buf, "%u\n", session->bidi_chap_en);
3410 break;
3411 case ISCSI_PARAM_DISCOVERY_AUTH_OPTIONAL:
3412 len = sprintf(buf, "%u\n", session->discovery_auth_optional);
3413 break;
3414 case ISCSI_PARAM_DEF_TIME2WAIT:
3415 len = sprintf(buf, "%d\n", session->time2wait);
3416 break;
3417 case ISCSI_PARAM_DEF_TIME2RETAIN:
3418 len = sprintf(buf, "%d\n", session->time2retain);
3419 break;
3420 case ISCSI_PARAM_TSID:
3421 len = sprintf(buf, "%u\n", session->tsid);
3422 break;
3423 case ISCSI_PARAM_ISID:
3424 len = sprintf(buf, "%02x%02x%02x%02x%02x%02x\n",
3425 session->isid[0], session->isid[1],
3426 session->isid[2], session->isid[3],
3427 session->isid[4], session->isid[5]);
3428 break;
3429 case ISCSI_PARAM_DISCOVERY_PARENT_IDX:
3430 len = sprintf(buf, "%u\n", session->discovery_parent_idx);
3431 break;
3432 case ISCSI_PARAM_DISCOVERY_PARENT_TYPE:
3433 if (session->discovery_parent_type)
3434 len = sprintf(buf, "%s\n",
3435 session->discovery_parent_type);
3436 else
3437 len = sprintf(buf, "\n");
Eddie Wai3b9373e2013-06-20 10:21:26 -07003438 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003439 default:
3440 return -ENOSYS;
3441 }
3442
3443 return len;
3444}
3445EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3446
Mike Christie00f37082011-02-16 15:04:35 -06003447int iscsi_conn_get_addr_param(struct sockaddr_storage *addr,
3448 enum iscsi_param param, char *buf)
3449{
3450 struct sockaddr_in6 *sin6 = NULL;
3451 struct sockaddr_in *sin = NULL;
3452 int len;
3453
3454 switch (addr->ss_family) {
3455 case AF_INET:
3456 sin = (struct sockaddr_in *)addr;
3457 break;
3458 case AF_INET6:
3459 sin6 = (struct sockaddr_in6 *)addr;
3460 break;
3461 default:
3462 return -EINVAL;
3463 }
3464
3465 switch (param) {
3466 case ISCSI_PARAM_CONN_ADDRESS:
3467 case ISCSI_HOST_PARAM_IPADDRESS:
3468 if (sin)
3469 len = sprintf(buf, "%pI4\n", &sin->sin_addr.s_addr);
3470 else
3471 len = sprintf(buf, "%pI6\n", &sin6->sin6_addr);
3472 break;
3473 case ISCSI_PARAM_CONN_PORT:
3474 if (sin)
3475 len = sprintf(buf, "%hu\n", be16_to_cpu(sin->sin_port));
3476 else
3477 len = sprintf(buf, "%hu\n",
3478 be16_to_cpu(sin6->sin6_port));
3479 break;
3480 default:
3481 return -EINVAL;
3482 }
3483
3484 return len;
3485}
3486EXPORT_SYMBOL_GPL(iscsi_conn_get_addr_param);
3487
Mike Christiea54a52c2006-06-28 12:00:23 -05003488int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3489 enum iscsi_param param, char *buf)
3490{
3491 struct iscsi_conn *conn = cls_conn->dd_data;
3492 int len;
3493
3494 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003495 case ISCSI_PARAM_PING_TMO:
3496 len = sprintf(buf, "%u\n", conn->ping_timeout);
3497 break;
3498 case ISCSI_PARAM_RECV_TMO:
3499 len = sprintf(buf, "%u\n", conn->recv_timeout);
3500 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003501 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3502 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3503 break;
3504 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3505 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3506 break;
3507 case ISCSI_PARAM_HDRDGST_EN:
3508 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3509 break;
3510 case ISCSI_PARAM_DATADGST_EN:
3511 len = sprintf(buf, "%d\n", conn->datadgst_en);
3512 break;
3513 case ISCSI_PARAM_IFMARKER_EN:
3514 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3515 break;
3516 case ISCSI_PARAM_OFMARKER_EN:
3517 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3518 break;
3519 case ISCSI_PARAM_EXP_STATSN:
3520 len = sprintf(buf, "%u\n", conn->exp_statsn);
3521 break;
3522 case ISCSI_PARAM_PERSISTENT_PORT:
3523 len = sprintf(buf, "%d\n", conn->persistent_port);
3524 break;
3525 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3526 len = sprintf(buf, "%s\n", conn->persistent_address);
3527 break;
Adheer Chandravanshif8525eb2013-07-01 05:54:12 -04003528 case ISCSI_PARAM_STATSN:
3529 len = sprintf(buf, "%u\n", conn->statsn);
3530 break;
3531 case ISCSI_PARAM_MAX_SEGMENT_SIZE:
3532 len = sprintf(buf, "%u\n", conn->max_segment_size);
3533 break;
3534 case ISCSI_PARAM_KEEPALIVE_TMO:
3535 len = sprintf(buf, "%u\n", conn->keepalive_tmo);
3536 break;
3537 case ISCSI_PARAM_LOCAL_PORT:
3538 len = sprintf(buf, "%u\n", conn->local_port);
3539 break;
3540 case ISCSI_PARAM_TCP_TIMESTAMP_STAT:
3541 len = sprintf(buf, "%u\n", conn->tcp_timestamp_stat);
3542 break;
3543 case ISCSI_PARAM_TCP_NAGLE_DISABLE:
3544 len = sprintf(buf, "%u\n", conn->tcp_nagle_disable);
3545 break;
3546 case ISCSI_PARAM_TCP_WSF_DISABLE:
3547 len = sprintf(buf, "%u\n", conn->tcp_wsf_disable);
3548 break;
3549 case ISCSI_PARAM_TCP_TIMER_SCALE:
3550 len = sprintf(buf, "%u\n", conn->tcp_timer_scale);
3551 break;
3552 case ISCSI_PARAM_TCP_TIMESTAMP_EN:
3553 len = sprintf(buf, "%u\n", conn->tcp_timestamp_en);
3554 break;
3555 case ISCSI_PARAM_IP_FRAGMENT_DISABLE:
3556 len = sprintf(buf, "%u\n", conn->fragment_disable);
3557 break;
3558 case ISCSI_PARAM_IPV4_TOS:
3559 len = sprintf(buf, "%u\n", conn->ipv4_tos);
3560 break;
3561 case ISCSI_PARAM_IPV6_TC:
3562 len = sprintf(buf, "%u\n", conn->ipv6_traffic_class);
3563 break;
Adheer Chandravanshi5f09e1f2013-07-22 07:46:10 -04003564 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3565 len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
3566 break;
Adheer Chandravanshif8525eb2013-07-01 05:54:12 -04003567 case ISCSI_PARAM_IS_FW_ASSIGNED_IPV6:
3568 len = sprintf(buf, "%u\n", conn->is_fw_assigned_ipv6);
3569 break;
3570 case ISCSI_PARAM_TCP_XMIT_WSF:
3571 len = sprintf(buf, "%u\n", conn->tcp_xmit_wsf);
3572 break;
3573 case ISCSI_PARAM_TCP_RECV_WSF:
3574 len = sprintf(buf, "%u\n", conn->tcp_recv_wsf);
3575 break;
Adheer Chandravanshiae56ff42013-11-22 05:28:21 -05003576 case ISCSI_PARAM_LOCAL_IPADDR:
3577 len = sprintf(buf, "%s\n", conn->local_ipaddr);
3578 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003579 default:
3580 return -ENOSYS;
3581 }
3582
3583 return len;
3584}
3585EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3586
Mike Christie0801c242007-05-30 12:57:12 -05003587int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3588 char *buf)
3589{
Mike Christie75613522008-05-21 15:53:59 -05003590 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003591 int len;
3592
3593 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003594 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003595 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003596 break;
Mike Christie0801c242007-05-30 12:57:12 -05003597 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003598 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003599 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003600 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003601 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003602 break;
Mike Christie0801c242007-05-30 12:57:12 -05003603 default:
3604 return -ENOSYS;
3605 }
3606
3607 return len;
3608}
3609EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3610
3611int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3612 char *buf, int buflen)
3613{
Mike Christie75613522008-05-21 15:53:59 -05003614 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003615
3616 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003617 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003618 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003619 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003620 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003621 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003622 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003623 default:
3624 return -ENOSYS;
3625 }
3626
3627 return 0;
3628}
3629EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3630
Mike Christie7996a772006-04-06 21:13:41 -05003631MODULE_AUTHOR("Mike Christie");
3632MODULE_DESCRIPTION("iSCSI library functions");
3633MODULE_LICENSE("GPL");