blob: 685eaec532180d107de1dbff954a9e679170b1ea [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Erez Zilberbd2199d2009-06-15 22:11:10 -050041static int iscsi_dbg_lib_conn;
42module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
47
48static int iscsi_dbg_lib_session;
49module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
50 S_IRUGO | S_IWUSR);
51MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
54
55static int iscsi_dbg_lib_eh;
56module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
57 S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
Mike Christie1b2c7af2009-03-05 14:45:58 -060061
62#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
63 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050064 if (iscsi_dbg_lib_conn) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060065 iscsi_conn_printk(KERN_INFO, _conn, \
66 "%s " dbg_fmt, \
67 __func__, ##arg); \
68 } while (0);
69
70#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
71 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050072 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
74 "%s " dbg_fmt, \
75 __func__, ##arg); \
76 } while (0);
77
78#define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
79 do { \
80 if (iscsi_dbg_lib_eh) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060081 iscsi_session_printk(KERN_INFO, _session, \
82 "%s " dbg_fmt, \
83 __func__, ##arg); \
84 } while (0);
85
Mike Christie77a23c22007-05-30 12:57:18 -050086/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050088
Mike Christie77a23c22007-05-30 12:57:18 -050089static int iscsi_sna_lt(u32 n1, u32 n2)
90{
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
93}
94
95/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96static int iscsi_sna_lte(u32 n1, u32 n2)
97{
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
100}
101
Mike Christie32ae7632009-03-05 14:46:03 -0600102inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
103{
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
106
Mike Christie1336aed2009-05-13 17:57:48 -0500107 if (ihost->workq)
108 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -0600109}
110EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
111
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530112static void __iscsi_update_cmdsn(struct iscsi_session *session,
113 uint32_t exp_cmdsn, uint32_t max_cmdsn)
Mike Christie7996a772006-04-06 21:13:41 -0500114{
Mike Christie77a23c22007-05-30 12:57:18 -0500115 /*
116 * standard specifies this check for when to update expected and
117 * max sequence numbers
118 */
119 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
120 return;
121
122 if (exp_cmdsn != session->exp_cmdsn &&
123 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500124 session->exp_cmdsn = exp_cmdsn;
125
Mike Christie77a23c22007-05-30 12:57:18 -0500126 if (max_cmdsn != session->max_cmdsn &&
127 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
128 session->max_cmdsn = max_cmdsn;
129 /*
130 * if the window closed with IO queued, then kick the
131 * xmit thread
132 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500133 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500134 !list_empty(&session->leadconn->mgmtqueue))
135 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500136 }
Mike Christie7996a772006-04-06 21:13:41 -0500137}
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530138
139void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
140{
141 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
142 be32_to_cpu(hdr->max_cmdsn));
143}
Mike Christie77a23c22007-05-30 12:57:18 -0500144EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500145
Mike Christie577577d2008-12-02 00:32:05 -0600146/**
147 * iscsi_prep_data_out_pdu - initialize Data-Out
148 * @task: scsi command task
149 * @r2t: R2T info
150 * @hdr: iscsi data in pdu
151 *
152 * Notes:
153 * Initialize Data-Out within this R2T sequence and finds
154 * proper data_offset within this SCSI command.
155 *
156 * This function is called with connection lock taken.
157 **/
158void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
159 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500160{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500161 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600162 unsigned int left = r2t->data_length - r2t->sent;
163
164 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500165
166 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600167 hdr->ttt = r2t->ttt;
168 hdr->datasn = cpu_to_be32(r2t->datasn);
169 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500170 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600171 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
172 hdr->itt = task->hdr_itt;
173 hdr->exp_statsn = r2t->exp_statsn;
174 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
175 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500176 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600177 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500178 hdr->flags = 0;
179 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600180 hton24(hdr->dlength, left);
181 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500182 hdr->flags = ISCSI_FLAG_CMD_FINAL;
183 }
Mike Christie577577d2008-12-02 00:32:05 -0600184 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500185}
Mike Christie577577d2008-12-02 00:32:05 -0600186EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500187
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600189{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500190 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600191
Mike Christie9c19a7d2008-05-21 15:54:09 -0500192 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600193 WARN_ON(1);
194 return -EINVAL;
195 }
196
197 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500198 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600199 return 0;
200}
201
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500202/*
203 * make an extended cdb AHS
204 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500205static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500206{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500207 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500208 unsigned rlen, pad_len;
209 unsigned short ahslength;
210 struct iscsi_ecdb_ahdr *ecdb_ahdr;
211 int rc;
212
Mike Christie9c19a7d2008-05-21 15:54:09 -0500213 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500214 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
215
216 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
217 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
218
219 pad_len = iscsi_padding(rlen);
220
Mike Christie9c19a7d2008-05-21 15:54:09 -0500221 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500222 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
223 if (rc)
224 return rc;
225
226 if (pad_len)
227 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
228
229 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
230 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
231 ecdb_ahdr->reserved = 0;
232 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
233
Mike Christie1b2c7af2009-03-05 14:45:58 -0600234 ISCSI_DBG_SESSION(task->conn->session,
235 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
236 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
237 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
238 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500239 return 0;
240}
241
Mike Christie9c19a7d2008-05-21 15:54:09 -0500242static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500243{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500245 struct iscsi_rlength_ahdr *rlen_ahdr;
246 int rc;
247
Mike Christie9c19a7d2008-05-21 15:54:09 -0500248 rlen_ahdr = iscsi_next_hdr(task);
249 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500250 if (rc)
251 return rc;
252
253 rlen_ahdr->ahslength =
254 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
255 sizeof(rlen_ahdr->reserved));
256 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
257 rlen_ahdr->reserved = 0;
258 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
259
Mike Christie1b2c7af2009-03-05 14:45:58 -0600260 ISCSI_DBG_SESSION(task->conn->session,
261 "bidi-in rlen_ahdr->read_length(%d) "
262 "rlen_ahdr->ahslength(%d)\n",
263 be32_to_cpu(rlen_ahdr->read_length),
264 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500265 return 0;
266}
267
Mike Christie7996a772006-04-06 21:13:41 -0500268/**
Mike Christie5d12c052009-11-11 16:34:32 -0600269 * iscsi_check_tmf_restrictions - check if a task is affected by TMF
270 * @task: iscsi task
271 * @opcode: opcode to check for
272 *
273 * During TMF a task has to be checked if it's affected.
274 * All unrelated I/O can be passed through, but I/O to the
275 * affected LUN should be restricted.
276 * If 'fast_abort' is set we won't be sending any I/O to the
277 * affected LUN.
278 * Otherwise the target is waiting for all TTTs to be completed,
279 * so we have to send all outstanding Data-Out PDUs to the target.
280 */
281static int iscsi_check_tmf_restrictions(struct iscsi_task *task, int opcode)
282{
283 struct iscsi_conn *conn = task->conn;
284 struct iscsi_tm *tmf = &conn->tmhdr;
285 unsigned int hdr_lun;
286
287 if (conn->tmf_state == TMF_INITIAL)
288 return 0;
289
290 if ((tmf->opcode & ISCSI_OPCODE_MASK) != ISCSI_OP_SCSI_TMFUNC)
291 return 0;
292
293 switch (ISCSI_TM_FUNC_VALUE(tmf)) {
294 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
295 /*
296 * Allow PDUs for unrelated LUNs
297 */
298 hdr_lun = scsilun_to_int((struct scsi_lun *)tmf->lun);
299 if (hdr_lun != task->sc->device->lun)
300 return 0;
Mike Christie3fe5ae82009-11-11 16:34:33 -0600301 /* fall through */
302 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
Mike Christie5d12c052009-11-11 16:34:32 -0600303 /*
304 * Fail all SCSI cmd PDUs
305 */
306 if (opcode != ISCSI_OP_SCSI_DATA_OUT) {
307 iscsi_conn_printk(KERN_INFO, conn,
308 "task [op %x/%x itt "
Mike Christie3fe5ae82009-11-11 16:34:33 -0600309 "0x%x/0x%x] "
Mike Christie5d12c052009-11-11 16:34:32 -0600310 "rejected.\n",
311 task->hdr->opcode, opcode,
Mike Christie3fe5ae82009-11-11 16:34:33 -0600312 task->itt, task->hdr_itt);
Mike Christie5d12c052009-11-11 16:34:32 -0600313 return -EACCES;
314 }
315 /*
316 * And also all data-out PDUs in response to R2T
317 * if fast_abort is set.
318 */
319 if (conn->session->fast_abort) {
320 iscsi_conn_printk(KERN_INFO, conn,
321 "task [op %x/%x itt "
Mike Christie3fe5ae82009-11-11 16:34:33 -0600322 "0x%x/0x%x] fast abort.\n",
Mike Christie5d12c052009-11-11 16:34:32 -0600323 task->hdr->opcode, opcode,
Mike Christie3fe5ae82009-11-11 16:34:33 -0600324 task->itt, task->hdr_itt);
Mike Christie5d12c052009-11-11 16:34:32 -0600325 return -EACCES;
326 }
327 break;
328 case ISCSI_TM_FUNC_ABORT_TASK:
329 /*
330 * the caller has already checked if the task
331 * they want to abort was in the pending queue so if
332 * we are here the cmd pdu has gone out already, and
333 * we will only hit this for data-outs
334 */
335 if (opcode == ISCSI_OP_SCSI_DATA_OUT &&
336 task->hdr_itt == tmf->rtt) {
337 ISCSI_DBG_SESSION(conn->session,
338 "Preventing task %x/%x from sending "
339 "data-out due to abort task in "
340 "progress\n", task->itt,
341 task->hdr_itt);
342 return -EACCES;
343 }
344 break;
345 }
346
347 return 0;
348}
349
350/**
Mike Christie7996a772006-04-06 21:13:41 -0500351 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500352 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500353 *
354 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
355 * fields like dlength or final based on how much data it sends
356 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500357static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500358{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500359 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500360 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500361 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600362 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500363 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600364 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600365 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500366
Mike Christie5d12c052009-11-11 16:34:32 -0600367 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
368 if (rc)
369 return rc;
370
Mike Christie184b57c2009-05-13 17:57:39 -0500371 if (conn->session->tt->alloc_pdu) {
372 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
373 if (rc)
374 return rc;
375 }
Mike Christie577577d2008-12-02 00:32:05 -0600376 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600377 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600378 memset(hdr, 0, sizeof(*hdr));
379
Mike Christie2ff79d52008-12-02 00:32:14 -0600380 if (session->tt->parse_pdu_itt)
381 hdr->itt = task->hdr_itt = itt;
382 else
383 hdr->itt = task->hdr_itt = build_itt(task->itt,
384 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500385 task->hdr_len = 0;
386 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600387 if (rc)
388 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600389 hdr->opcode = ISCSI_OP_SCSI_CMD;
390 hdr->flags = ISCSI_ATTR_SIMPLE;
391 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600392 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600393 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500394 cmd_len = sc->cmd_len;
395 if (cmd_len < ISCSI_CDB_SIZE)
396 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
397 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500398 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500399 if (rc)
400 return rc;
401 cmd_len = ISCSI_CDB_SIZE;
402 }
403 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500404
Mike Christie9c19a7d2008-05-21 15:54:09 -0500405 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500406 if (scsi_bidi_cmnd(sc)) {
407 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500408 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500409 if (rc)
410 return rc;
411 }
Mike Christie7996a772006-04-06 21:13:41 -0500412 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500413 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600414 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
415
Boaz Harroshc07d4442008-04-18 10:11:52 -0500416 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500417 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
418 /*
419 * Write counters:
420 *
421 * imm_count bytes to be sent right after
422 * SCSI PDU Header
423 *
424 * unsol_count bytes(as Data-Out) to be sent
425 * without R2T ack right after
426 * immediate data
427 *
Mike Christie577577d2008-12-02 00:32:05 -0600428 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500429 *
430 * pad_count bytes to be sent as zero-padding
431 */
Mike Christie577577d2008-12-02 00:32:05 -0600432 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500433
434 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500435 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500436 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500437 conn->max_xmit_dlength);
438 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500439 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500440 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500441 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500442 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600443 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500444
Mike Christieffd04362006-08-31 18:09:24 -0400445 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600446 r2t->data_length = min(session->first_burst, out_len) -
447 task->imm_count;
448 r2t->data_offset = task->imm_count;
449 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
450 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400451 }
452
Mike Christie577577d2008-12-02 00:32:05 -0600453 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500454 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600455 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500456 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500457 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
458 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500459 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500460
461 if (sc->sc_data_direction == DMA_FROM_DEVICE)
462 hdr->flags |= ISCSI_FLAG_CMD_READ;
463 }
464
Boaz Harrosh004d6532007-12-13 12:43:23 -0600465 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500466 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600467
468 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
469 hdrlength /= ISCSI_PAD_LEN;
470
471 WARN_ON(hdrlength >= 256);
472 hdr->hlength = hdrlength & 0xFF;
473
Mike Christie577577d2008-12-02 00:32:05 -0600474 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500475 return -EIO;
476
Mike Christie9c19a7d2008-05-21 15:54:09 -0500477 task->state = ISCSI_TASK_RUNNING;
Mike Christied3305f32009-08-20 15:10:58 -0500478 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
479 session->cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500480
Olaf Kircha8ac6312007-12-13 12:43:35 -0600481 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600482 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
483 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
484 scsi_bidi_cmnd(sc) ? "bidirectional" :
485 sc->sc_data_direction == DMA_TO_DEVICE ?
486 "write" : "read", conn->id, sc, sc->cmnd[0],
487 task->itt, scsi_bufflen(sc),
488 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
489 session->cmdsn,
490 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600491 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500492}
Mike Christie7996a772006-04-06 21:13:41 -0500493
494/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500495 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500496 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500497 *
498 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500499 * This function returns the scsi command to scsi-ml or cleans
500 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500501 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500502static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500503{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500504 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600505 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500506 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500507
Mike Christie4421c9e2009-05-13 17:57:50 -0500508 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
509 task->itt, task->state, task->sc);
510
Mike Christie577577d2008-12-02 00:32:05 -0600511 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500512 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500513 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500514 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500515 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500516 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500517 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500518 return;
519
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800520 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500521
Mike Christie3e5c28a2008-05-21 15:54:06 -0500522 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500523 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500524 /* SCSI eh reuses commands to verify us */
525 sc->SCp.ptr = NULL;
526 /*
527 * queue command may call this to free the task, but
528 * not have setup the sc callback
529 */
530 if (sc->scsi_done)
531 sc->scsi_done(sc);
532 }
Mike Christie7996a772006-04-06 21:13:41 -0500533}
534
Mike Christie913e5bf2008-05-21 15:54:18 -0500535void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400536{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500537 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400538}
Mike Christie913e5bf2008-05-21 15:54:18 -0500539EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400540
Mike Christie9c19a7d2008-05-21 15:54:09 -0500541static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400542{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500543 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500544 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400545}
546
Mike Christie9c19a7d2008-05-21 15:54:09 -0500547void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500548{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500549 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500550
551 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500552 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500553 spin_unlock_bh(&session->lock);
554}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500555EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500556
Mike Christie3bbaaad2009-05-13 17:57:46 -0500557/**
558 * iscsi_complete_task - finish a task
559 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500560 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500561 *
562 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600563 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500564static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600565{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500566 struct iscsi_conn *conn = task->conn;
567
Mike Christie4421c9e2009-05-13 17:57:50 -0500568 ISCSI_DBG_SESSION(conn->session,
569 "complete task itt 0x%x state %d sc %p\n",
570 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500571 if (task->state == ISCSI_TASK_COMPLETED ||
572 task->state == ISCSI_TASK_ABRT_TMF ||
573 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500574 return;
575 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500576 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500577
578 if (!list_empty(&task->running))
579 list_del_init(&task->running);
580
581 if (conn->task == task)
582 conn->task = NULL;
583
584 if (conn->ping_task == task)
585 conn->ping_task = NULL;
586
587 /* release get from queueing */
588 __iscsi_put_task(task);
589}
590
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530591/**
592 * iscsi_complete_scsi_task - finish scsi task normally
593 * @task: iscsi task for scsi cmd
594 * @exp_cmdsn: expected cmd sn in cpu format
595 * @max_cmdsn: max cmd sn in cpu format
596 *
597 * This is used when drivers do not need or cannot perform
598 * lower level pdu processing.
599 *
600 * Called with session lock
601 */
602void iscsi_complete_scsi_task(struct iscsi_task *task,
603 uint32_t exp_cmdsn, uint32_t max_cmdsn)
604{
605 struct iscsi_conn *conn = task->conn;
606
607 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
608
609 conn->last_recv = jiffies;
610 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
611 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
612}
613EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
614
615
Mike Christie3bbaaad2009-05-13 17:57:46 -0500616/*
617 * session lock must be held and if not called for a task that is
618 * still pending or from the xmit thread, then xmit thread must
619 * be suspended.
620 */
621static void fail_scsi_task(struct iscsi_task *task, int err)
622{
623 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600624 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500625 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600626
Mike Christie3bbaaad2009-05-13 17:57:46 -0500627 /*
628 * if a command completes and we get a successful tmf response
629 * we will hit this because the scsi eh abort code does not take
630 * a ref to the task.
631 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500632 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600633 if (!sc)
634 return;
635
Mike Christieb3cd5052009-05-13 17:57:49 -0500636 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600637 /*
638 * cmd never made it to the xmit thread, so we should not count
639 * the cmd in the sequencing
640 */
641 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500642 /* it was never sent so just complete like normal */
643 state = ISCSI_TASK_COMPLETED;
644 } else if (err == DID_TRANSPORT_DISRUPTED)
645 state = ISCSI_TASK_ABRT_SESS_RECOV;
646 else
647 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600648
Mike Christieb3cd5052009-05-13 17:57:49 -0500649 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500650 if (!scsi_bidi_cmnd(sc))
651 scsi_set_resid(sc, scsi_bufflen(sc));
652 else {
653 scsi_out(sc)->resid = scsi_out(sc)->length;
654 scsi_in(sc)->resid = scsi_in(sc)->length;
655 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500656
Mike Christieb3cd5052009-05-13 17:57:49 -0500657 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600658}
659
Mike Christie3e5c28a2008-05-21 15:54:06 -0500660static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500661 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500662{
663 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600664 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500665 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
Mike Christie4f704dc2009-11-11 16:34:31 -0600666 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie052d0142008-05-21 15:54:05 -0500667
668 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
669 return -ENOTCONN;
670
Mike Christie4f704dc2009-11-11 16:34:31 -0600671 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
Mike Christie052d0142008-05-21 15:54:05 -0500672 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
673 /*
674 * pre-format CmdSN for outgoing PDU.
675 */
676 nop->cmdsn = cpu_to_be32(session->cmdsn);
677 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500678 /*
Mike Christie4f704dc2009-11-11 16:34:31 -0600679 * TODO: We always use immediate for normal session pdus.
Mike Christie052d0142008-05-21 15:54:05 -0500680 * If we start to send tmfs or nops as non-immediate then
681 * we should start checking the cmdsn numbers for mgmt tasks.
Mike Christie4f704dc2009-11-11 16:34:31 -0600682 *
683 * During discovery sessions iscsid sends TEXT as non immediate,
684 * but we always only send one PDU at a time.
Mike Christie052d0142008-05-21 15:54:05 -0500685 */
686 if (conn->c_stage == ISCSI_CONN_STARTED &&
687 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
688 session->queued_cmdsn++;
689 session->cmdsn++;
690 }
691 }
692
Mike Christieae15f802008-12-02 00:32:15 -0600693 if (session->tt->init_task && session->tt->init_task(task))
694 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500695
696 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
697 session->state = ISCSI_STATE_LOGGING_OUT;
698
Mike Christie577577d2008-12-02 00:32:05 -0600699 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600700 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
701 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
702 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500703 return 0;
704}
705
Mike Christie9c19a7d2008-05-21 15:54:09 -0500706static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600707__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
708 char *data, uint32_t data_size)
709{
710 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500711 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie4f704dc2009-11-11 16:34:31 -0600712 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500713 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600714 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600715
716 if (session->state == ISCSI_STATE_TERMINATE)
717 return NULL;
718
Mike Christie4f704dc2009-11-11 16:34:31 -0600719 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
Mike Christief6d51802007-12-13 12:43:30 -0600720 /*
721 * Login and Text are sent serially, in
722 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500723 * Same task can be used. Same ITT must be used.
724 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600725 */
Mike Christie4f704dc2009-11-11 16:34:31 -0600726 if (conn->login_task->state != ISCSI_TASK_FREE) {
727 iscsi_conn_printk(KERN_ERR, conn, "Login/Text in "
728 "progress. Cannot start new task.\n");
729 return NULL;
730 }
731
Mike Christie9c19a7d2008-05-21 15:54:09 -0500732 task = conn->login_task;
Mike Christie4f704dc2009-11-11 16:34:31 -0600733 } else {
Mike Christie26013ad2009-05-13 17:57:43 -0500734 if (session->state != ISCSI_STATE_LOGGED_IN)
735 return NULL;
736
Mike Christief6d51802007-12-13 12:43:30 -0600737 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
738 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
739
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800740 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500741 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600742 return NULL;
743 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500744 /*
745 * released in complete pdu for task we expect a response for, and
746 * released by the lld when it has transmitted the task for
747 * pdus we do not expect a response for.
748 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500749 atomic_set(&task->refcount, 1);
750 task->conn = conn;
751 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500752 INIT_LIST_HEAD(&task->running);
753 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600754
755 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500756 memcpy(task->data, data, data_size);
757 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600758 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500759 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600760
Mike Christie184b57c2009-05-13 17:57:39 -0500761 if (conn->session->tt->alloc_pdu) {
762 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
763 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
764 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500765 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500766 }
Mike Christie577577d2008-12-02 00:32:05 -0600767 }
Mike Christie184b57c2009-05-13 17:57:39 -0500768
Mike Christie262ef632008-12-02 00:32:13 -0600769 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600770 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500771 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600772
773 if (hdr->itt != RESERVED_ITT) {
774 if (session->tt->parse_pdu_itt)
775 task->hdr->itt = itt;
776 else
777 task->hdr->itt = build_itt(task->itt,
778 task->conn->session->age);
779 }
780
Mike Christie1336aed2009-05-13 17:57:48 -0500781 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600782 if (iscsi_prep_mgmt_task(conn, task))
783 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500784
Mike Christie9c19a7d2008-05-21 15:54:09 -0500785 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600786 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500787 } else {
788 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600789 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500790 }
Mike Christie052d0142008-05-21 15:54:05 -0500791
Mike Christie9c19a7d2008-05-21 15:54:09 -0500792 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600793
794free_task:
795 __iscsi_put_task(task);
796 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600797}
798
799int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
800 char *data, uint32_t data_size)
801{
802 struct iscsi_conn *conn = cls_conn->dd_data;
803 struct iscsi_session *session = conn->session;
804 int err = 0;
805
806 spin_lock_bh(&session->lock);
807 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
808 err = -EPERM;
809 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600810 return err;
811}
812EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
813
Mike Christie7996a772006-04-06 21:13:41 -0500814/**
815 * iscsi_cmd_rsp - SCSI Command Response processing
816 * @conn: iscsi connection
817 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500818 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500819 * @data: cmd data buffer
820 * @datalen: len of buffer
821 *
822 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500823 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500824 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500825static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500826 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500827 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500828{
Mike Christie7996a772006-04-06 21:13:41 -0500829 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
830 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500831 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500832
Mike Christie77a23c22007-05-30 12:57:18 -0500833 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500834 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
835
836 sc->result = (DID_OK << 16) | rhdr->cmd_status;
837
838 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
839 sc->result = DID_ERROR << 16;
840 goto out;
841 }
842
843 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600844 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500845
846 if (datalen < 2) {
847invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600848 iscsi_conn_printk(KERN_ERR, conn,
849 "Got CHECK_CONDITION but invalid data "
850 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500851 sc->result = DID_BAD_TARGET << 16;
852 goto out;
853 }
854
Harvey Harrison8f3339912008-05-21 15:54:20 -0500855 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500856 if (datalen < senselen)
857 goto invalid_datalen;
858
859 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600860 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600861 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
862 min_t(uint16_t, senselen,
863 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500864 }
865
Boaz Harroshc07d4442008-04-18 10:11:52 -0500866 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
867 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
868 int res_count = be32_to_cpu(rhdr->bi_residual_count);
869
870 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
871 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
872 res_count <= scsi_in(sc)->length))
873 scsi_in(sc)->resid = res_count;
874 else
875 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
876 }
877
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600878 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
879 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500880 int res_count = be32_to_cpu(rhdr->residual_count);
881
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600882 if (res_count > 0 &&
883 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
884 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500885 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900886 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500887 else
888 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500889 }
Mike Christie7996a772006-04-06 21:13:41 -0500890out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500891 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600892 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500893 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500894 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500895}
896
Mike Christie1d9edf02008-09-24 11:46:09 -0500897/**
898 * iscsi_data_in_rsp - SCSI Data-In Response processing
899 * @conn: iscsi connection
900 * @hdr: iscsi pdu
901 * @task: scsi command task
902 **/
903static void
904iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
905 struct iscsi_task *task)
906{
907 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
908 struct scsi_cmnd *sc = task->sc;
909
910 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
911 return;
912
Mike Christieedbc9aa052009-05-13 17:57:42 -0500913 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500914 sc->result = (DID_OK << 16) | rhdr->cmd_status;
915 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
916 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
917 ISCSI_FLAG_DATA_OVERFLOW)) {
918 int res_count = be32_to_cpu(rhdr->residual_count);
919
920 if (res_count > 0 &&
921 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
922 res_count <= scsi_in(sc)->length))
923 scsi_in(sc)->resid = res_count;
924 else
925 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
926 }
927
Mike Christie3bbaaad2009-05-13 17:57:46 -0500928 ISCSI_DBG_SESSION(conn->session, "data in with status done "
929 "[sc %p res %d itt 0x%x]\n",
930 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500931 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500932 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500933}
934
Mike Christie7ea8b822006-07-24 15:47:22 -0500935static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
936{
937 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
938
939 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
940 conn->tmfrsp_pdus_cnt++;
941
Mike Christie843c0a82007-12-13 12:43:20 -0600942 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500943 return;
944
945 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600946 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500947 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600948 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500949 else
Mike Christie843c0a82007-12-13 12:43:20 -0600950 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500951 wake_up(&conn->ehwait);
952}
953
Mike Christief6d51802007-12-13 12:43:30 -0600954static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
955{
956 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500957 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600958
Mike Christie9c19a7d2008-05-21 15:54:09 -0500959 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600960 return;
961
962 memset(&hdr, 0, sizeof(struct iscsi_nopout));
963 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
964 hdr.flags = ISCSI_FLAG_CMD_FINAL;
965
966 if (rhdr) {
967 memcpy(hdr.lun, rhdr->lun, 8);
968 hdr.ttt = rhdr->ttt;
969 hdr.itt = RESERVED_ITT;
970 } else
971 hdr.ttt = RESERVED_ITT;
972
Mike Christie9c19a7d2008-05-21 15:54:09 -0500973 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
974 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600975 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600976 else if (!rhdr) {
977 /* only track our nops */
978 conn->ping_task = task;
979 conn->last_ping = jiffies;
980 }
Mike Christief6d51802007-12-13 12:43:30 -0600981}
982
Mike Christie8afa1432009-08-20 15:10:59 -0500983static int iscsi_nop_out_rsp(struct iscsi_task *task,
984 struct iscsi_nopin *nop, char *data, int datalen)
985{
986 struct iscsi_conn *conn = task->conn;
987 int rc = 0;
988
989 if (conn->ping_task != task) {
990 /*
991 * If this is not in response to one of our
992 * nops then it must be from userspace.
993 */
994 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
995 data, datalen))
996 rc = ISCSI_ERR_CONN_FAILED;
997 } else
998 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
999 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1000 return rc;
1001}
1002
Mike Christie62f38302006-08-31 18:09:27 -04001003static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1004 char *data, int datalen)
1005{
1006 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
1007 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -05001008 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -04001009
1010 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1011
Mike Christie8afa1432009-08-20 15:10:59 -05001012 if (ntoh24(reject->dlength) > datalen ||
1013 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
1014 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
1015 "pdu. Invalid data length (pdu dlength "
1016 "%u, datalen %d\n", ntoh24(reject->dlength),
1017 datalen);
1018 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -04001019 }
Mike Christie8afa1432009-08-20 15:10:59 -05001020 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
1021 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
1022
1023 switch (reject->reason) {
1024 case ISCSI_REASON_DATA_DIGEST_ERROR:
1025 iscsi_conn_printk(KERN_ERR, conn,
1026 "pdu (op 0x%x itt 0x%x) rejected "
1027 "due to DataDigest error.\n",
1028 rejected_pdu.itt, opcode);
1029 break;
1030 case ISCSI_REASON_IMM_CMD_REJECT:
1031 iscsi_conn_printk(KERN_ERR, conn,
1032 "pdu (op 0x%x itt 0x%x) rejected. Too many "
1033 "immediate commands.\n",
1034 rejected_pdu.itt, opcode);
1035 /*
1036 * We only send one TMF at a time so if the target could not
1037 * handle it, then it should get fixed (RFC mandates that
1038 * a target can handle one immediate TMF per conn).
1039 *
1040 * For nops-outs, we could have sent more than one if
1041 * the target is sending us lots of nop-ins
1042 */
1043 if (opcode != ISCSI_OP_NOOP_OUT)
1044 return 0;
1045
1046 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
1047 /*
1048 * nop-out in response to target's nop-out rejected.
1049 * Just resend.
1050 */
1051 iscsi_send_nopout(conn,
1052 (struct iscsi_nopin*)&rejected_pdu);
1053 else {
1054 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 "
1074 "code 0x%x\n", rejected_pdu.itt,
1075 rejected_pdu.opcode, reject->reason);
1076 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 *
1089 * The session lock must be held.
1090 */
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
1118 * queuecommand or send generic. session lock must be held and verify
1119 * 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
Mike Christief6d51802007-12-13 12:43:30 -06001155 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001156 break;
1157 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001158 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1159 break;
Mike Christie7996a772006-04-06 21:13:41 -05001160 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001161 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001162 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1163 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001164 break;
1165 default:
1166 rc = ISCSI_ERR_BAD_OPCODE;
1167 break;
1168 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001169 goto out;
1170 }
Mike Christie7996a772006-04-06 21:13:41 -05001171
Mike Christie3e5c28a2008-05-21 15:54:06 -05001172 switch(opcode) {
1173 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001174 case ISCSI_OP_SCSI_DATA_IN:
1175 task = iscsi_itt_to_ctask(conn, hdr->itt);
1176 if (!task)
1177 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001178 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001179 break;
1180 case ISCSI_OP_R2T:
1181 /*
1182 * LLD handles R2Ts if they need to.
1183 */
1184 return 0;
1185 case ISCSI_OP_LOGOUT_RSP:
1186 case ISCSI_OP_LOGIN_RSP:
1187 case ISCSI_OP_TEXT_RSP:
1188 case ISCSI_OP_SCSI_TMFUNC_RSP:
1189 case ISCSI_OP_NOOP_IN:
1190 task = iscsi_itt_to_task(conn, hdr->itt);
1191 if (!task)
1192 return ISCSI_ERR_BAD_ITT;
1193 break;
1194 default:
1195 return ISCSI_ERR_BAD_OPCODE;
1196 }
1197
1198 switch(opcode) {
1199 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001200 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001201 break;
1202 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001203 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001204 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001205 case ISCSI_OP_LOGOUT_RSP:
1206 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1207 if (datalen) {
1208 rc = ISCSI_ERR_PROTO;
1209 break;
1210 }
1211 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1212 goto recv_pdu;
1213 case ISCSI_OP_LOGIN_RSP:
1214 case ISCSI_OP_TEXT_RSP:
1215 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1216 /*
1217 * login related PDU's exp_statsn is handled in
1218 * userspace
1219 */
1220 goto recv_pdu;
1221 case ISCSI_OP_SCSI_TMFUNC_RSP:
1222 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1223 if (datalen) {
1224 rc = ISCSI_ERR_PROTO;
1225 break;
1226 }
1227
1228 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001229 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001230 break;
1231 case ISCSI_OP_NOOP_IN:
1232 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1233 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1234 rc = ISCSI_ERR_PROTO;
1235 break;
1236 }
1237 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1238
Mike Christie8afa1432009-08-20 15:10:59 -05001239 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1240 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001241 break;
1242 default:
1243 rc = ISCSI_ERR_BAD_OPCODE;
1244 break;
1245 }
1246
1247out:
1248 return rc;
1249recv_pdu:
1250 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1251 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001252 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001253 return rc;
1254}
Mike Christie913e5bf2008-05-21 15:54:18 -05001255EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001256
1257int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1258 char *data, int datalen)
1259{
1260 int rc;
1261
1262 spin_lock(&conn->session->lock);
1263 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1264 spin_unlock(&conn->session->lock);
1265 return rc;
1266}
1267EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1268
Mike Christie0af967f2008-05-21 15:54:04 -05001269int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001270{
1271 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001272 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001273
Mike Christie0af967f2008-05-21 15:54:04 -05001274 if (itt == RESERVED_ITT)
1275 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001276
Mike Christie262ef632008-12-02 00:32:13 -06001277 if (session->tt->parse_pdu_itt)
1278 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1279 else {
1280 i = get_itt(itt);
1281 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1282 }
1283
1284 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001285 iscsi_conn_printk(KERN_ERR, conn,
1286 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001287 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001288 return ISCSI_ERR_BAD_ITT;
1289 }
Mike Christie7996a772006-04-06 21:13:41 -05001290
Mike Christie3e5c28a2008-05-21 15:54:06 -05001291 if (i >= session->cmds_max) {
1292 iscsi_conn_printk(KERN_ERR, conn,
1293 "received invalid itt index %u (max cmds "
1294 "%u.\n", i, session->cmds_max);
1295 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001296 }
Mike Christie7996a772006-04-06 21:13:41 -05001297 return 0;
1298}
1299EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1300
Mike Christie913e5bf2008-05-21 15:54:18 -05001301/**
1302 * iscsi_itt_to_ctask - look up ctask by itt
1303 * @conn: iscsi connection
1304 * @itt: itt
1305 *
1306 * This should be used for cmd tasks.
1307 *
1308 * The session lock must be held.
1309 */
1310struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001311{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001312 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001313
1314 if (iscsi_verify_itt(conn, itt))
1315 return NULL;
1316
Mike Christie913e5bf2008-05-21 15:54:18 -05001317 task = iscsi_itt_to_task(conn, itt);
1318 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001319 return NULL;
1320
Mike Christie913e5bf2008-05-21 15:54:18 -05001321 if (task->sc->SCp.phase != conn->session->age) {
1322 iscsi_session_printk(KERN_ERR, conn->session,
1323 "task's session age %d, expected %d\n",
1324 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001325 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001326 }
Mike Christie0af967f2008-05-21 15:54:04 -05001327
Mike Christie9c19a7d2008-05-21 15:54:09 -05001328 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001329}
1330EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1331
Mike Christie40a06e72009-03-05 14:46:05 -06001332void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001333 enum iscsi_err err)
1334{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001335 struct iscsi_conn *conn;
1336 struct device *dev;
1337 unsigned long flags;
1338
1339 spin_lock_irqsave(&session->lock, flags);
1340 conn = session->leadconn;
1341 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1342 spin_unlock_irqrestore(&session->lock, flags);
1343 return;
1344 }
1345
1346 dev = get_device(&conn->cls_conn->dev);
1347 spin_unlock_irqrestore(&session->lock, flags);
1348 if (!dev)
1349 return;
1350 /*
1351 * if the host is being removed bypass the connection
1352 * recovery initialization because we are going to kill
1353 * the session.
1354 */
1355 if (err == ISCSI_ERR_INVALID_HOST)
1356 iscsi_conn_error_event(conn->cls_conn, err);
1357 else
1358 iscsi_conn_failure(conn, err);
1359 put_device(dev);
1360}
1361EXPORT_SYMBOL_GPL(iscsi_session_failure);
1362
Mike Christie7996a772006-04-06 21:13:41 -05001363void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1364{
1365 struct iscsi_session *session = conn->session;
1366 unsigned long flags;
1367
1368 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001369 if (session->state == ISCSI_STATE_FAILED) {
1370 spin_unlock_irqrestore(&session->lock, flags);
1371 return;
1372 }
1373
Mike Christie67a61112006-05-30 00:37:20 -05001374 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001375 session->state = ISCSI_STATE_FAILED;
1376 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001377
Mike Christie7996a772006-04-06 21:13:41 -05001378 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1379 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001380 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001381}
1382EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1383
Mike Christie77a23c22007-05-30 12:57:18 -05001384static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1385{
1386 struct iscsi_session *session = conn->session;
1387
1388 /*
1389 * Check for iSCSI window and take care of CmdSN wrap-around
1390 */
Mike Christiee0726402007-07-26 12:46:48 -05001391 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001392 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1393 "%u MaxCmdSN %u CmdSN %u/%u\n",
1394 session->exp_cmdsn, session->max_cmdsn,
1395 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001396 return -ENOSPC;
1397 }
1398 return 0;
1399}
1400
Mike Christie9c19a7d2008-05-21 15:54:09 -05001401static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001402{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001403 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001404 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001405
Mike Christie70b31c12009-08-20 15:11:03 -05001406 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1407 return -ENODATA;
1408
Mike Christie9c19a7d2008-05-21 15:54:09 -05001409 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001410 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001411 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001412 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001413 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001414 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001415 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001416 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001417 }
1418 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001419 return rc;
1420}
1421
Mike Christie7996a772006-04-06 21:13:41 -05001422/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001423 * iscsi_requeue_task - requeue task to run from session workqueue
1424 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001425 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001426 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001427 * this. The session lock must be held. This should only be called
1428 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001429 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001430void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001431{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001432 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001433
Mike Christie3bbaaad2009-05-13 17:57:46 -05001434 /*
1435 * this may be on the requeue list already if the xmit_task callout
1436 * is handling the r2ts while we are adding new ones
1437 */
1438 if (list_empty(&task->running))
1439 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001440 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001441}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001442EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001443
1444/**
Mike Christie7996a772006-04-06 21:13:41 -05001445 * iscsi_data_xmit - xmit any command into the scheduled connection
1446 * @conn: iscsi connection
1447 *
1448 * Notes:
1449 * The function can return -EAGAIN in which case the caller must
1450 * re-schedule it again later or recover. '0' return code means
1451 * successful xmit.
1452 **/
1453static int iscsi_data_xmit(struct iscsi_conn *conn)
1454{
Mike Christie5d12c052009-11-11 16:34:32 -06001455 struct iscsi_task *task;
Mike Christie3219e522006-05-30 00:37:28 -05001456 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001457
Mike Christie77a23c22007-05-30 12:57:18 -05001458 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001459 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001460 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001461 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001462 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001463 }
Mike Christie7996a772006-04-06 21:13:41 -05001464
Mike Christie9c19a7d2008-05-21 15:54:09 -05001465 if (conn->task) {
1466 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001467 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001468 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001469 }
1470
Mike Christie77a23c22007-05-30 12:57:18 -05001471 /*
1472 * process mgmt pdus like nops before commands since we should
1473 * only have one nop-out as a ping from us and targets should not
1474 * overflow us with nop-ins
1475 */
1476check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001477 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001478 conn->task = list_entry(conn->mgmtqueue.next,
1479 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001480 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001481 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1482 __iscsi_put_task(conn->task);
1483 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001484 continue;
1485 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001486 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001487 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001488 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001489 }
1490
Mike Christie843c0a82007-12-13 12:43:20 -06001491 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001492 while (!list_empty(&conn->cmdqueue)) {
Mike Christie5d12c052009-11-11 16:34:32 -06001493 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1494 running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001495 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001496 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001497 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001498 continue;
1499 }
Mike Christie577577d2008-12-02 00:32:05 -06001500 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1501 if (rc) {
Mike Christie5d12c052009-11-11 16:34:32 -06001502 if (rc == -ENOMEM || rc == -EACCES) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001503 list_add_tail(&conn->task->running,
1504 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001505 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001506 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001507 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001508 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001509 continue;
1510 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001511 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001512 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001513 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001514 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001515 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001516 * we need to check the mgmt queue for nops that need to
1517 * be sent to aviod starvation
1518 */
Mike Christie843c0a82007-12-13 12:43:20 -06001519 if (!list_empty(&conn->mgmtqueue))
1520 goto check_mgmt;
1521 }
1522
1523 while (!list_empty(&conn->requeue)) {
Mike Christieb3a7ea82007-12-13 12:43:26 -06001524 /*
1525 * we always do fastlogout - conn stop code will clean up.
1526 */
1527 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1528 break;
1529
Mike Christie5d12c052009-11-11 16:34:32 -06001530 task = list_entry(conn->requeue.next, struct iscsi_task,
1531 running);
1532 if (iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_DATA_OUT))
1533 break;
1534
1535 conn->task = task;
Mike Christie3bbaaad2009-05-13 17:57:46 -05001536 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001537 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001538 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001539 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001540 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001541 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001542 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001543 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001544 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001545 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001546
Mike Christie70b31c12009-08-20 15:11:03 -05001547done:
Mike Christie77a23c22007-05-30 12:57:18 -05001548 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001549 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001550}
1551
David Howellsc4028952006-11-22 14:57:56 +00001552static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001553{
David Howellsc4028952006-11-22 14:57:56 +00001554 struct iscsi_conn *conn =
1555 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001556 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001557 /*
1558 * serialize Xmit worker on a per-connection basis.
1559 */
Mike Christie3219e522006-05-30 00:37:28 -05001560 do {
1561 rc = iscsi_data_xmit(conn);
1562 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001563}
1564
Mike Christie577577d2008-12-02 00:32:05 -06001565static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1566 struct scsi_cmnd *sc)
1567{
1568 struct iscsi_task *task;
1569
Stefani Seibold7acd72e2009-12-21 14:37:28 -08001570 if (!kfifo_out(&conn->session->cmdpool.queue,
Mike Christie577577d2008-12-02 00:32:05 -06001571 (void *) &task, sizeof(void *)))
1572 return NULL;
1573
1574 sc->SCp.phase = conn->session->age;
1575 sc->SCp.ptr = (char *) task;
1576
1577 atomic_set(&task->refcount, 1);
1578 task->state = ISCSI_TASK_PENDING;
1579 task->conn = conn;
1580 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001581 task->have_checked_conn = false;
1582 task->last_timeout = jiffies;
1583 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001584 INIT_LIST_HEAD(&task->running);
1585 return task;
1586}
1587
Mike Christie7996a772006-04-06 21:13:41 -05001588enum {
1589 FAILURE_BAD_HOST = 1,
1590 FAILURE_SESSION_FAILED,
1591 FAILURE_SESSION_FREED,
1592 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001593 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001594 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001595 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001596 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001597 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001598 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001599};
1600
1601int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1602{
Mike Christie75613522008-05-21 15:53:59 -05001603 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001604 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001605 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001606 int reason = 0;
1607 struct iscsi_session *session;
1608 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001609 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001610
1611 sc->scsi_done = done;
1612 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001613 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001614
1615 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001616 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001617 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001618
Mike Christie75613522008-05-21 15:53:59 -05001619 cls_session = starget_to_session(scsi_target(sc->device));
1620 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001621 spin_lock(&session->lock);
1622
Mike Christie75613522008-05-21 15:53:59 -05001623 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001624 if (reason) {
1625 sc->result = reason;
1626 goto fault;
1627 }
1628
Mike Christie301e0f72009-05-13 17:57:47 -05001629 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001630 /*
1631 * to handle the race between when we set the recovery state
1632 * and block the session we requeue here (commands could
1633 * be entering our queuecommand while a block is starting
1634 * up because the block code is not locked)
1635 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001636 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001637 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001638 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001639 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001640 sc->result = DID_IMM_RETRY << 16;
1641 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001642 case ISCSI_STATE_LOGGING_OUT:
1643 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001644 sc->result = DID_IMM_RETRY << 16;
1645 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001646 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001647 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001648 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001649 break;
1650 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001651 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001652 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001653 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001654 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001655 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001656 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001657 }
Mike Christie7996a772006-04-06 21:13:41 -05001658 goto fault;
1659 }
1660
Mike Christie7996a772006-04-06 21:13:41 -05001661 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001662 if (!conn) {
1663 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001664 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001665 goto fault;
1666 }
Mike Christie7996a772006-04-06 21:13:41 -05001667
Mike Christie661134a2009-09-05 07:35:33 +05301668 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1669 reason = FAILURE_SESSION_IN_RECOVERY;
1670 sc->result = DID_REQUEUE;
1671 goto fault;
1672 }
1673
Mike Christie77a23c22007-05-30 12:57:18 -05001674 if (iscsi_check_cmdsn_window_closed(conn)) {
1675 reason = FAILURE_WINDOW_CLOSED;
1676 goto reject;
1677 }
1678
Mike Christie577577d2008-12-02 00:32:05 -06001679 task = iscsi_alloc_task(conn, sc);
1680 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001681 reason = FAILURE_OOM;
1682 goto reject;
1683 }
Mike Christie7996a772006-04-06 21:13:41 -05001684
Mike Christie1336aed2009-05-13 17:57:48 -05001685 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001686 reason = iscsi_prep_scsi_cmd_pdu(task);
1687 if (reason) {
Mike Christie5d12c052009-11-11 16:34:32 -06001688 if (reason == -ENOMEM || reason == -EACCES) {
Mike Christie577577d2008-12-02 00:32:05 -06001689 reason = FAILURE_OOM;
1690 goto prepd_reject;
1691 } else {
1692 sc->result = DID_ABORT << 16;
1693 goto prepd_fault;
1694 }
Mike Christie052d0142008-05-21 15:54:05 -05001695 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001696 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001697 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001698 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001699 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001700 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001701 } else {
1702 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001703 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001704 }
Mike Christie052d0142008-05-21 15:54:05 -05001705
1706 session->queued_cmdsn++;
1707 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001708 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001709 return 0;
1710
Mike Christie577577d2008-12-02 00:32:05 -06001711prepd_reject:
1712 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001713 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001714reject:
1715 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001716 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1717 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001718 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001719 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001720
Mike Christie577577d2008-12-02 00:32:05 -06001721prepd_fault:
1722 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001723 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001724fault:
1725 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001726 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1727 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001728 if (!scsi_bidi_cmnd(sc))
1729 scsi_set_resid(sc, scsi_bufflen(sc));
1730 else {
1731 scsi_out(sc)->resid = scsi_out(sc)->length;
1732 scsi_in(sc)->resid = scsi_in(sc)->length;
1733 }
Mike Christie052d0142008-05-21 15:54:05 -05001734 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001735 spin_lock(host->host_lock);
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
1774 spin_lock(&session->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 }
1781 spin_unlock(&session->lock);
1782}
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) {
Mike Christie6724add2007-08-15 01:38:30 -05001794 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001795 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001796 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001797 ISCSI_DBG_EH(session, "tmf exec failure\n");
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
Mike Christie7996a772006-04-06 21:13:41 -05001807 spin_unlock_bh(&session->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);
Mike Christie77a23c22007-05-30 12:57:18 -05001826 spin_lock_bh(&session->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 *
1863 * This grabs the session lock to make sure no one is in
1864 * 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{
1872 spin_lock_bh(&conn->session->lock);
1873 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1874 spin_unlock_bh(&conn->session->lock);
1875}
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
1933 spin_lock(&session->lock);
1934 if (session->state != ISCSI_STATE_LOGGED_IN) {
1935 /*
1936 * We are probably in the middle of iscsi recovery so let
1937 * that complete and handle the error.
1938 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001939 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001940 goto done;
1941 }
1942
1943 conn = session->leadconn;
1944 if (!conn) {
1945 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001946 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001947 goto done;
1948 }
1949
Mike Christied355e572009-06-15 22:11:08 -05001950 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie92ed4d62010-02-10 16:51:45 -06001951 if (!task) {
1952 /*
1953 * Raced with completion. Just reset timer, and let it
1954 * complete normally
1955 */
1956 rc = BLK_EH_RESET_TIMER;
Mike Christied355e572009-06-15 22:11:08 -05001957 goto done;
Mike Christie92ed4d62010-02-10 16:51:45 -06001958 }
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;
Mike Christief6d51802007-12-13 12:43:30 -06002047 spin_unlock(&session->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
2059 spin_lock(&session->lock);
2060 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);
Mike Christief6d51802007-12-13 12:43:30 -06002076 spin_unlock(&session->lock);
2077 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:
2092 spin_unlock(&session->lock);
2093}
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;
Mike Christie577577d2008-12-02 00:32:05 -06002102 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2103 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);
2122 spin_lock_bh(&session->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");
Mike Christie6724add2007-08-15 01:38:30 -05002130 spin_unlock_bh(&session->lock);
2131 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) {
2141 spin_unlock_bh(&session->lock);
2142 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:
Mike Christie77a23c22007-05-30 12:57:18 -05002182 spin_unlock_bh(&session->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 */
Mike Christie6187c242009-07-15 15:02:57 -05002193 spin_lock_bh(&session->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));
Mike Christie6187c242009-07-15 15:02:57 -05002197 spin_unlock_bh(&session->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:
2201 spin_unlock_bh(&session->lock);
2202 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2203 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:
Mike Christie7996a772006-04-06 21:13:41 -05002220 spin_unlock_bh(&session->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:
2228 spin_unlock_bh(&session->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;
2243 int_to_scsilun(sc->device->lun, (struct scsi_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);
2261 spin_lock_bh(&session->lock);
2262 /*
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:
2288 spin_unlock_bh(&session->lock);
2289 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2290 goto done;
2291 default:
2292 conn->tmf_state = TMF_INITIAL;
2293 goto unlock;
2294 }
2295
2296 rc = SUCCESS;
2297 spin_unlock_bh(&session->lock);
2298
2299 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002300
Mike Christiea3439142008-09-24 11:46:15 -05002301 spin_lock_bh(&session->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;
Mike Christiea3439142008-09-24 11:46:15 -05002305 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002306
2307 iscsi_start_tx(conn);
2308 goto done;
2309
2310unlock:
2311 spin_unlock_bh(&session->lock);
2312done:
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
2324 spin_lock_bh(&session->lock);
2325 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 }
2330 spin_unlock_bh(&session->lock);
2331}
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);
2352 spin_lock_bh(&session->lock);
2353 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);
2359 spin_unlock_bh(&session->lock);
2360 mutex_unlock(&session->eh_mutex);
2361 return FAILED;
2362 }
2363
2364 spin_unlock_bh(&session->lock);
2365 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 */
2370 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2371
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);
2381 spin_lock_bh(&session->lock);
2382 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;
2388 spin_unlock_bh(&session->lock);
2389 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);
2424 spin_lock_bh(&session->lock);
2425 /*
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:
2451 spin_unlock_bh(&session->lock);
2452 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2453 goto done;
2454 default:
2455 conn->tmf_state = TMF_INITIAL;
2456 goto unlock;
2457 }
2458
2459 rc = SUCCESS;
2460 spin_unlock_bh(&session->lock);
2461
2462 iscsi_suspend_tx(conn);
2463
2464 spin_lock_bh(&session->lock);
2465 memset(hdr, 0, sizeof(*hdr));
2466 fail_scsi_tasks(conn, -1, DID_ERROR);
2467 conn->tmf_state = TMF_INITIAL;
2468 spin_unlock_bh(&session->lock);
2469
2470 iscsi_start_tx(conn);
2471 goto done;
2472
2473unlock:
2474 spin_unlock_bh(&session->lock);
2475done:
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);
Mike Christie6724add2007-08-15 01:38:30 -05002772 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002773 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002774
2775 /* initialize SCSI PDU commands pool */
2776 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2777 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002778 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002779 goto cmdpool_alloc_fail;
2780
2781 /* pre-format cmds pool with ITT */
2782 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002783 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002784
Mike Christie9c19a7d2008-05-21 15:54:09 -05002785 if (cmd_task_size)
2786 task->dd_data = &task[1];
2787 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002788 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002789 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002790 }
2791
Mike Christief53a88d2006-06-28 12:00:27 -05002792 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002793 goto module_get_fail;
2794
Mike Christie79706342008-05-21 15:54:12 -05002795 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002796 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002797
Mike Christie7996a772006-04-06 21:13:41 -05002798 return cls_session;
2799
2800cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002801 module_put(iscsit->owner);
2802module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002803 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002804cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002805 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002806dec_session_count:
2807 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002808 return NULL;
2809}
2810EXPORT_SYMBOL_GPL(iscsi_session_setup);
2811
2812/**
2813 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002814 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002815 *
Mike Christie75613522008-05-21 15:53:59 -05002816 * The driver must have called iscsi_remove_session before
2817 * calling this.
2818 */
Mike Christie7996a772006-04-06 21:13:41 -05002819void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2820{
Mike Christie75613522008-05-21 15:53:59 -05002821 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002822 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002823 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002824
Olaf Kirch63203772007-12-13 12:43:25 -06002825 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002826
Mike Christieb2c64162007-05-30 12:57:16 -05002827 kfree(session->password);
2828 kfree(session->password_in);
2829 kfree(session->username);
2830 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002831 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002832 kfree(session->initiatorname);
2833 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002834
Mike Christie75613522008-05-21 15:53:59 -05002835 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002836 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002837 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002838}
2839EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2840
2841/**
2842 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2843 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002844 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002845 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002846 */
Mike Christie7996a772006-04-06 21:13:41 -05002847struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002848iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2849 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002850{
Mike Christie75613522008-05-21 15:53:59 -05002851 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002852 struct iscsi_conn *conn;
2853 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002854 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002855
Mike Christie5d91e202008-05-21 15:54:01 -05002856 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2857 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002858 if (!cls_conn)
2859 return NULL;
2860 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002861 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002862
Mike Christie5d91e202008-05-21 15:54:01 -05002863 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002864 conn->session = session;
2865 conn->cls_conn = cls_conn;
2866 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2867 conn->id = conn_idx;
2868 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002869 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002870
2871 init_timer(&conn->transport_timer);
2872 conn->transport_timer.data = (unsigned long)conn;
2873 conn->transport_timer.function = iscsi_check_transport_timeouts;
2874
Mike Christie843c0a82007-12-13 12:43:20 -06002875 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002876 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002877 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002878 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002879
Mike Christie9c19a7d2008-05-21 15:54:09 -05002880 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002881 spin_lock_bh(&session->lock);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002882 if (!kfifo_out(&session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002883 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002884 sizeof(void*))) {
2885 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002886 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002887 }
2888 spin_unlock_bh(&session->lock);
2889
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002890 data = (char *) __get_free_pages(GFP_KERNEL,
2891 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002892 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002893 goto login_task_data_alloc_fail;
2894 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002895
Mike Christie843c0a82007-12-13 12:43:20 -06002896 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002897 init_waitqueue_head(&conn->ehwait);
2898
2899 return cls_conn;
2900
Mike Christie9c19a7d2008-05-21 15:54:09 -05002901login_task_data_alloc_fail:
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002902 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002903 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002904login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002905 iscsi_destroy_conn(cls_conn);
2906 return NULL;
2907}
2908EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2909
2910/**
2911 * iscsi_conn_teardown - teardown iscsi connection
2912 * cls_conn: iscsi class connection
2913 *
2914 * TODO: we may need to make this into a two step process
2915 * like scsi-mls remove + put host
2916 */
2917void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2918{
2919 struct iscsi_conn *conn = cls_conn->dd_data;
2920 struct iscsi_session *session = conn->session;
2921 unsigned long flags;
2922
Mike Christief6d51802007-12-13 12:43:30 -06002923 del_timer_sync(&conn->transport_timer);
2924
Mike Christie7996a772006-04-06 21:13:41 -05002925 spin_lock_bh(&session->lock);
2926 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2927 if (session->leadconn == conn) {
2928 /*
2929 * leading connection? then give up on recovery.
2930 */
2931 session->state = ISCSI_STATE_TERMINATE;
2932 wake_up(&conn->ehwait);
2933 }
2934 spin_unlock_bh(&session->lock);
2935
Mike Christie7996a772006-04-06 21:13:41 -05002936 /*
2937 * Block until all in-progress commands for this connection
2938 * time out or fail.
2939 */
2940 for (;;) {
2941 spin_lock_irqsave(session->host->host_lock, flags);
2942 if (!session->host->host_busy) { /* OK for ERL == 0 */
2943 spin_unlock_irqrestore(session->host->host_lock, flags);
2944 break;
2945 }
2946 spin_unlock_irqrestore(session->host->host_lock, flags);
2947 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002948 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2949 "host_busy %d host_failed %d\n",
2950 session->host->host_busy,
2951 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002952 /*
2953 * force eh_abort() to unblock
2954 */
2955 wake_up(&conn->ehwait);
2956 }
2957
Mike Christie779ea122007-02-28 17:32:15 -06002958 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002959 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002960
Mike Christie7996a772006-04-06 21:13:41 -05002961 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002962 free_pages((unsigned long) conn->data,
2963 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002964 kfree(conn->persistent_address);
Stefani Seibold7acd72e2009-12-21 14:37:28 -08002965 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002966 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002967 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002968 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002969 spin_unlock_bh(&session->lock);
2970
Mike Christie7996a772006-04-06 21:13:41 -05002971 iscsi_destroy_conn(cls_conn);
2972}
2973EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2974
2975int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2976{
2977 struct iscsi_conn *conn = cls_conn->dd_data;
2978 struct iscsi_session *session = conn->session;
2979
Mike Christieffd04362006-08-31 18:09:24 -04002980 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002981 iscsi_conn_printk(KERN_ERR, conn,
2982 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002983 return -EPERM;
2984 }
2985
Mike Christiedb98ccd2006-08-31 18:09:31 -04002986 if ((session->imm_data_en || !session->initial_r2t_en) &&
2987 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002988 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2989 "first_burst %d max_burst %d\n",
2990 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002991 return -EINVAL;
2992 }
2993
Mike Christief6d51802007-12-13 12:43:30 -06002994 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002995 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2996 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002997 conn->recv_timeout = 5;
2998 }
2999
3000 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06003001 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3002 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06003003 conn->ping_timeout = 5;
3004 }
3005
Mike Christie7996a772006-04-06 21:13:41 -05003006 spin_lock_bh(&session->lock);
3007 conn->c_stage = ISCSI_CONN_STARTED;
3008 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05003009 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05003010
Mike Christief6d51802007-12-13 12:43:30 -06003011 conn->last_recv = jiffies;
3012 conn->last_ping = jiffies;
3013 if (conn->recv_timeout && conn->ping_timeout)
3014 mod_timer(&conn->transport_timer,
3015 jiffies + (conn->recv_timeout * HZ));
3016
Mike Christie7996a772006-04-06 21:13:41 -05003017 switch(conn->stop_stage) {
3018 case STOP_CONN_RECOVER:
3019 /*
3020 * unblock eh_abort() if it is blocked. re-try all
3021 * commands after successful recovery
3022 */
Mike Christie7996a772006-04-06 21:13:41 -05003023 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06003024 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05003025 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06003026 if (session->age == 16)
3027 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06003028 break;
Mike Christie7996a772006-04-06 21:13:41 -05003029 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05003030 conn->stop_stage = 0;
3031 break;
Mike Christie7996a772006-04-06 21:13:41 -05003032 default:
3033 break;
3034 }
3035 spin_unlock_bh(&session->lock);
3036
Mike Christie75613522008-05-21 15:53:59 -05003037 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06003038 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05003039 return 0;
3040}
3041EXPORT_SYMBOL_GPL(iscsi_conn_start);
3042
3043static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05003044fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05003045{
Mike Christie3bbaaad2009-05-13 17:57:46 -05003046 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05003047 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05003048
Mike Christie3bbaaad2009-05-13 17:57:46 -05003049 for (i = 0; i < conn->session->cmds_max; i++) {
3050 task = conn->session->cmds[i];
3051 if (task->sc)
3052 continue;
3053
3054 if (task->state == ISCSI_TASK_FREE)
3055 continue;
3056
3057 ISCSI_DBG_SESSION(conn->session,
3058 "failing mgmt itt 0x%x state %d\n",
3059 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05003060 state = ISCSI_TASK_ABRT_SESS_RECOV;
3061 if (task->state == ISCSI_TASK_PENDING)
3062 state = ISCSI_TASK_COMPLETED;
3063 iscsi_complete_task(task, state);
3064
Mike Christie7996a772006-04-06 21:13:41 -05003065 }
Mike Christie7996a772006-04-06 21:13:41 -05003066}
3067
Mike Christie656cffc2006-05-18 20:31:42 -05003068static void iscsi_start_session_recovery(struct iscsi_session *session,
3069 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05003070{
Mike Christieed2abc72006-05-02 19:46:40 -05003071 int old_stop_stage;
3072
Mike Christie6724add2007-08-15 01:38:30 -05003073 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003074 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003075 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05003076 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003077 mutex_unlock(&session->eh_mutex);
3078 return;
3079 }
3080
3081 /*
Mike Christieed2abc72006-05-02 19:46:40 -05003082 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05003083 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05003084 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05003085 */
Mike Christie67a61112006-05-30 00:37:20 -05003086 if (flag == STOP_CONN_TERM)
3087 session->state = ISCSI_STATE_TERMINATE;
3088 else if (conn->stop_stage != STOP_CONN_RECOVER)
3089 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05003090 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003091
Mike Christie26013ad2009-05-13 17:57:43 -05003092 del_timer_sync(&conn->transport_timer);
3093 iscsi_suspend_tx(conn);
3094
3095 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05003096 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05003097 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05003098 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05003099 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003100
Mike Christie7996a772006-04-06 21:13:41 -05003101 /*
3102 * for connection level recovery we should not calculate
3103 * header digest. conn->hdr_size used for optimization
3104 * in hdr_extract() and will be re-negotiated at
3105 * set_param() time.
3106 */
3107 if (flag == STOP_CONN_RECOVER) {
3108 conn->hdrdgst_en = 0;
3109 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05003110 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05003111 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06003112 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05003113 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05003114 }
Mike Christie7996a772006-04-06 21:13:41 -05003115 }
Mike Christie656cffc2006-05-18 20:31:42 -05003116
Mike Christie656cffc2006-05-18 20:31:42 -05003117 /*
3118 * flush queues.
3119 */
3120 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05003121 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05003122 fail_mgmt_tasks(session, conn);
Mike Christie5d12c052009-11-11 16:34:32 -06003123 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
Mike Christie656cffc2006-05-18 20:31:42 -05003124 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05003125 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05003126}
Mike Christie7996a772006-04-06 21:13:41 -05003127
3128void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
3129{
3130 struct iscsi_conn *conn = cls_conn->dd_data;
3131 struct iscsi_session *session = conn->session;
3132
3133 switch (flag) {
3134 case STOP_CONN_RECOVER:
3135 case STOP_CONN_TERM:
3136 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05003137 break;
Mike Christie7996a772006-04-06 21:13:41 -05003138 default:
Mike Christie322d7392008-01-31 13:36:52 -06003139 iscsi_conn_printk(KERN_ERR, conn,
3140 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05003141 }
3142}
3143EXPORT_SYMBOL_GPL(iscsi_conn_stop);
3144
3145int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
3146 struct iscsi_cls_conn *cls_conn, int is_leading)
3147{
Mike Christie75613522008-05-21 15:53:59 -05003148 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04003149 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05003150
Mike Christie7996a772006-04-06 21:13:41 -05003151 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003152 if (is_leading)
3153 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04003154 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05003155
3156 /*
3157 * Unblock xmitworker(), Login Phase will pass through.
3158 */
3159 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
3160 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
3161 return 0;
3162}
3163EXPORT_SYMBOL_GPL(iscsi_conn_bind);
3164
Mike Christie5700b1a2009-05-13 17:57:40 -05003165static int iscsi_switch_str_param(char **param, char *new_val_buf)
3166{
3167 char *new_val;
3168
3169 if (*param) {
3170 if (!strcmp(*param, new_val_buf))
3171 return 0;
3172 }
3173
3174 new_val = kstrdup(new_val_buf, GFP_NOIO);
3175 if (!new_val)
3176 return -ENOMEM;
3177
3178 kfree(*param);
3179 *param = new_val;
3180 return 0;
3181}
Mike Christiea54a52c2006-06-28 12:00:23 -05003182
3183int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
3184 enum iscsi_param param, char *buf, int buflen)
3185{
3186 struct iscsi_conn *conn = cls_conn->dd_data;
3187 struct iscsi_session *session = conn->session;
3188 uint32_t value;
3189
3190 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003191 case ISCSI_PARAM_FAST_ABORT:
3192 sscanf(buf, "%d", &session->fast_abort);
3193 break;
Mike Christief6d51802007-12-13 12:43:30 -06003194 case ISCSI_PARAM_ABORT_TMO:
3195 sscanf(buf, "%d", &session->abort_timeout);
3196 break;
3197 case ISCSI_PARAM_LU_RESET_TMO:
3198 sscanf(buf, "%d", &session->lu_reset_timeout);
3199 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003200 case ISCSI_PARAM_TGT_RESET_TMO:
3201 sscanf(buf, "%d", &session->tgt_reset_timeout);
3202 break;
Mike Christief6d51802007-12-13 12:43:30 -06003203 case ISCSI_PARAM_PING_TMO:
3204 sscanf(buf, "%d", &conn->ping_timeout);
3205 break;
3206 case ISCSI_PARAM_RECV_TMO:
3207 sscanf(buf, "%d", &conn->recv_timeout);
3208 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003209 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3210 sscanf(buf, "%d", &conn->max_recv_dlength);
3211 break;
3212 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3213 sscanf(buf, "%d", &conn->max_xmit_dlength);
3214 break;
3215 case ISCSI_PARAM_HDRDGST_EN:
3216 sscanf(buf, "%d", &conn->hdrdgst_en);
3217 break;
3218 case ISCSI_PARAM_DATADGST_EN:
3219 sscanf(buf, "%d", &conn->datadgst_en);
3220 break;
3221 case ISCSI_PARAM_INITIAL_R2T_EN:
3222 sscanf(buf, "%d", &session->initial_r2t_en);
3223 break;
3224 case ISCSI_PARAM_MAX_R2T:
3225 sscanf(buf, "%d", &session->max_r2t);
3226 break;
3227 case ISCSI_PARAM_IMM_DATA_EN:
3228 sscanf(buf, "%d", &session->imm_data_en);
3229 break;
3230 case ISCSI_PARAM_FIRST_BURST:
3231 sscanf(buf, "%d", &session->first_burst);
3232 break;
3233 case ISCSI_PARAM_MAX_BURST:
3234 sscanf(buf, "%d", &session->max_burst);
3235 break;
3236 case ISCSI_PARAM_PDU_INORDER_EN:
3237 sscanf(buf, "%d", &session->pdu_inorder_en);
3238 break;
3239 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3240 sscanf(buf, "%d", &session->dataseq_inorder_en);
3241 break;
3242 case ISCSI_PARAM_ERL:
3243 sscanf(buf, "%d", &session->erl);
3244 break;
3245 case ISCSI_PARAM_IFMARKER_EN:
3246 sscanf(buf, "%d", &value);
3247 BUG_ON(value);
3248 break;
3249 case ISCSI_PARAM_OFMARKER_EN:
3250 sscanf(buf, "%d", &value);
3251 BUG_ON(value);
3252 break;
3253 case ISCSI_PARAM_EXP_STATSN:
3254 sscanf(buf, "%u", &conn->exp_statsn);
3255 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003256 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003257 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003258 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003259 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003260 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05003261 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05003262 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05003263 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003264 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003265 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003266 case ISCSI_PARAM_TPGT:
3267 sscanf(buf, "%d", &session->tpgt);
3268 break;
3269 case ISCSI_PARAM_PERSISTENT_PORT:
3270 sscanf(buf, "%d", &conn->persistent_port);
3271 break;
3272 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003273 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003274 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003275 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003276 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003277 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003278 default:
3279 return -ENOSYS;
3280 }
3281
3282 return 0;
3283}
3284EXPORT_SYMBOL_GPL(iscsi_set_param);
3285
3286int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3287 enum iscsi_param param, char *buf)
3288{
Mike Christie75613522008-05-21 15:53:59 -05003289 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003290 int len;
3291
3292 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003293 case ISCSI_PARAM_FAST_ABORT:
3294 len = sprintf(buf, "%d\n", session->fast_abort);
3295 break;
Mike Christief6d51802007-12-13 12:43:30 -06003296 case ISCSI_PARAM_ABORT_TMO:
3297 len = sprintf(buf, "%d\n", session->abort_timeout);
3298 break;
3299 case ISCSI_PARAM_LU_RESET_TMO:
3300 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3301 break;
Mike Christie3fe5ae82009-11-11 16:34:33 -06003302 case ISCSI_PARAM_TGT_RESET_TMO:
3303 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3304 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003305 case ISCSI_PARAM_INITIAL_R2T_EN:
3306 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3307 break;
3308 case ISCSI_PARAM_MAX_R2T:
3309 len = sprintf(buf, "%hu\n", session->max_r2t);
3310 break;
3311 case ISCSI_PARAM_IMM_DATA_EN:
3312 len = sprintf(buf, "%d\n", session->imm_data_en);
3313 break;
3314 case ISCSI_PARAM_FIRST_BURST:
3315 len = sprintf(buf, "%u\n", session->first_burst);
3316 break;
3317 case ISCSI_PARAM_MAX_BURST:
3318 len = sprintf(buf, "%u\n", session->max_burst);
3319 break;
3320 case ISCSI_PARAM_PDU_INORDER_EN:
3321 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3322 break;
3323 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3324 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3325 break;
3326 case ISCSI_PARAM_ERL:
3327 len = sprintf(buf, "%d\n", session->erl);
3328 break;
3329 case ISCSI_PARAM_TARGET_NAME:
3330 len = sprintf(buf, "%s\n", session->targetname);
3331 break;
3332 case ISCSI_PARAM_TPGT:
3333 len = sprintf(buf, "%d\n", session->tpgt);
3334 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003335 case ISCSI_PARAM_USERNAME:
3336 len = sprintf(buf, "%s\n", session->username);
3337 break;
3338 case ISCSI_PARAM_USERNAME_IN:
3339 len = sprintf(buf, "%s\n", session->username_in);
3340 break;
3341 case ISCSI_PARAM_PASSWORD:
3342 len = sprintf(buf, "%s\n", session->password);
3343 break;
3344 case ISCSI_PARAM_PASSWORD_IN:
3345 len = sprintf(buf, "%s\n", session->password_in);
3346 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003347 case ISCSI_PARAM_IFACE_NAME:
3348 len = sprintf(buf, "%s\n", session->ifacename);
3349 break;
3350 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003351 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003352 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003353 default:
3354 return -ENOSYS;
3355 }
3356
3357 return len;
3358}
3359EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3360
3361int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3362 enum iscsi_param param, char *buf)
3363{
3364 struct iscsi_conn *conn = cls_conn->dd_data;
3365 int len;
3366
3367 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003368 case ISCSI_PARAM_PING_TMO:
3369 len = sprintf(buf, "%u\n", conn->ping_timeout);
3370 break;
3371 case ISCSI_PARAM_RECV_TMO:
3372 len = sprintf(buf, "%u\n", conn->recv_timeout);
3373 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003374 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3375 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3376 break;
3377 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3378 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3379 break;
3380 case ISCSI_PARAM_HDRDGST_EN:
3381 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3382 break;
3383 case ISCSI_PARAM_DATADGST_EN:
3384 len = sprintf(buf, "%d\n", conn->datadgst_en);
3385 break;
3386 case ISCSI_PARAM_IFMARKER_EN:
3387 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3388 break;
3389 case ISCSI_PARAM_OFMARKER_EN:
3390 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3391 break;
3392 case ISCSI_PARAM_EXP_STATSN:
3393 len = sprintf(buf, "%u\n", conn->exp_statsn);
3394 break;
3395 case ISCSI_PARAM_PERSISTENT_PORT:
3396 len = sprintf(buf, "%d\n", conn->persistent_port);
3397 break;
3398 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3399 len = sprintf(buf, "%s\n", conn->persistent_address);
3400 break;
3401 default:
3402 return -ENOSYS;
3403 }
3404
3405 return len;
3406}
3407EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3408
Mike Christie0801c242007-05-30 12:57:12 -05003409int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3410 char *buf)
3411{
Mike Christie75613522008-05-21 15:53:59 -05003412 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003413 int len;
3414
3415 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003416 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003417 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003418 break;
Mike Christie0801c242007-05-30 12:57:12 -05003419 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003420 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003421 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003422 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003423 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003424 break;
Mike Christie75613522008-05-21 15:53:59 -05003425 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003426 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003427 break;
Mike Christie0801c242007-05-30 12:57:12 -05003428 default:
3429 return -ENOSYS;
3430 }
3431
3432 return len;
3433}
3434EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3435
3436int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3437 char *buf, int buflen)
3438{
Mike Christie75613522008-05-21 15:53:59 -05003439 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003440
3441 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003442 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003443 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003444 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003445 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003446 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003447 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003448 default:
3449 return -ENOSYS;
3450 }
3451
3452 return 0;
3453}
3454EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3455
Mike Christie7996a772006-04-06 21:13:41 -05003456MODULE_AUTHOR("Mike Christie");
3457MODULE_DESCRIPTION("iSCSI library functions");
3458MODULE_LICENSE("GPL");