blob: d07017911139804019b155d95da6234d1ad2ab21 [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
Mike Christie1b2c7af2009-03-05 14:45:58 -060041static int iscsi_dbg_lib;
42module_param_named(debug_libiscsi, iscsi_dbg_lib, int, S_IRUGO | S_IWUSR);
43MODULE_PARM_DESC(debug_libiscsi, "Turn on debugging for libiscsi module. "
44 "Set to 1 to turn on, and zero to turn off. Default "
45 "is off.");
46
47#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
48 do { \
49 if (iscsi_dbg_lib) \
50 iscsi_conn_printk(KERN_INFO, _conn, \
51 "%s " dbg_fmt, \
52 __func__, ##arg); \
53 } while (0);
54
55#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
56 do { \
57 if (iscsi_dbg_lib) \
58 iscsi_session_printk(KERN_INFO, _session, \
59 "%s " dbg_fmt, \
60 __func__, ##arg); \
61 } while (0);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
64#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050065
Mike Christie77a23c22007-05-30 12:57:18 -050066static int iscsi_sna_lt(u32 n1, u32 n2)
67{
68 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
69 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
70}
71
72/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
73static int iscsi_sna_lte(u32 n1, u32 n2)
74{
75 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
76 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
77}
78
Mike Christie32ae7632009-03-05 14:46:03 -060079inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
80{
81 struct Scsi_Host *shost = conn->session->host;
82 struct iscsi_host *ihost = shost_priv(shost);
83
84 queue_work(ihost->workq, &conn->xmitwork);
85}
86EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
87
Mike Christie77a23c22007-05-30 12:57:18 -050088void
89iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050090{
91 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
92 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
93
Mike Christie77a23c22007-05-30 12:57:18 -050094 /*
95 * standard specifies this check for when to update expected and
96 * max sequence numbers
97 */
98 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
99 return;
100
101 if (exp_cmdsn != session->exp_cmdsn &&
102 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500103 session->exp_cmdsn = exp_cmdsn;
104
Mike Christie77a23c22007-05-30 12:57:18 -0500105 if (max_cmdsn != session->max_cmdsn &&
106 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
107 session->max_cmdsn = max_cmdsn;
108 /*
109 * if the window closed with IO queued, then kick the
110 * xmit thread
111 */
112 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie052d0142008-05-21 15:54:05 -0500113 !list_empty(&session->leadconn->mgmtqueue)) {
114 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
Mike Christie32ae7632009-03-05 14:46:03 -0600115 iscsi_conn_queue_work(session->leadconn);
Mike Christie052d0142008-05-21 15:54:05 -0500116 }
Mike Christie77a23c22007-05-30 12:57:18 -0500117 }
Mike Christie7996a772006-04-06 21:13:41 -0500118}
Mike Christie77a23c22007-05-30 12:57:18 -0500119EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500120
Mike Christie577577d2008-12-02 00:32:05 -0600121/**
122 * iscsi_prep_data_out_pdu - initialize Data-Out
123 * @task: scsi command task
124 * @r2t: R2T info
125 * @hdr: iscsi data in pdu
126 *
127 * Notes:
128 * Initialize Data-Out within this R2T sequence and finds
129 * proper data_offset within this SCSI command.
130 *
131 * This function is called with connection lock taken.
132 **/
133void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
134 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500135{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500136 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600137 unsigned int left = r2t->data_length - r2t->sent;
138
139 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500140
141 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600142 hdr->ttt = r2t->ttt;
143 hdr->datasn = cpu_to_be32(r2t->datasn);
144 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500145 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600146 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
147 hdr->itt = task->hdr_itt;
148 hdr->exp_statsn = r2t->exp_statsn;
149 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
150 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500151 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600152 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500153 hdr->flags = 0;
154 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600155 hton24(hdr->dlength, left);
156 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500157 hdr->flags = ISCSI_FLAG_CMD_FINAL;
158 }
Mike Christie577577d2008-12-02 00:32:05 -0600159 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500160}
Mike Christie577577d2008-12-02 00:32:05 -0600161EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500162
Mike Christie9c19a7d2008-05-21 15:54:09 -0500163static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600164{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500165 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600166
Mike Christie9c19a7d2008-05-21 15:54:09 -0500167 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600168 WARN_ON(1);
169 return -EINVAL;
170 }
171
172 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500173 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600174 return 0;
175}
176
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500177/*
178 * make an extended cdb AHS
179 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500180static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500181{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500182 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500183 unsigned rlen, pad_len;
184 unsigned short ahslength;
185 struct iscsi_ecdb_ahdr *ecdb_ahdr;
186 int rc;
187
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500189 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
190
191 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
192 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
193
194 pad_len = iscsi_padding(rlen);
195
Mike Christie9c19a7d2008-05-21 15:54:09 -0500196 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500197 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
198 if (rc)
199 return rc;
200
201 if (pad_len)
202 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
203
204 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
205 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
206 ecdb_ahdr->reserved = 0;
207 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
208
Mike Christie1b2c7af2009-03-05 14:45:58 -0600209 ISCSI_DBG_SESSION(task->conn->session,
210 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
211 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
212 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
213 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500214 return 0;
215}
216
Mike Christie9c19a7d2008-05-21 15:54:09 -0500217static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500218{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500219 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500220 struct iscsi_rlength_ahdr *rlen_ahdr;
221 int rc;
222
Mike Christie9c19a7d2008-05-21 15:54:09 -0500223 rlen_ahdr = iscsi_next_hdr(task);
224 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500225 if (rc)
226 return rc;
227
228 rlen_ahdr->ahslength =
229 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
230 sizeof(rlen_ahdr->reserved));
231 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
232 rlen_ahdr->reserved = 0;
233 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
234
Mike Christie1b2c7af2009-03-05 14:45:58 -0600235 ISCSI_DBG_SESSION(task->conn->session,
236 "bidi-in rlen_ahdr->read_length(%d) "
237 "rlen_ahdr->ahslength(%d)\n",
238 be32_to_cpu(rlen_ahdr->read_length),
239 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500240 return 0;
241}
242
Mike Christie7996a772006-04-06 21:13:41 -0500243/**
244 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500245 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500246 *
247 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
248 * fields like dlength or final based on how much data it sends
249 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500250static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500251{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500252 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500253 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500254 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600255 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500256 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600257 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600258 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500259
Mike Christie2ff79d52008-12-02 00:32:14 -0600260 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
Mike Christie577577d2008-12-02 00:32:05 -0600261 if (rc)
262 return rc;
263 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600264 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600265 memset(hdr, 0, sizeof(*hdr));
266
Mike Christie2ff79d52008-12-02 00:32:14 -0600267 if (session->tt->parse_pdu_itt)
268 hdr->itt = task->hdr_itt = itt;
269 else
270 hdr->itt = task->hdr_itt = build_itt(task->itt,
271 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500272 task->hdr_len = 0;
273 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600274 if (rc)
275 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600276 hdr->opcode = ISCSI_OP_SCSI_CMD;
277 hdr->flags = ISCSI_ATTR_SIMPLE;
278 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600279 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600280 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600281 session->cmdsn++;
282 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500283 cmd_len = sc->cmd_len;
284 if (cmd_len < ISCSI_CDB_SIZE)
285 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
286 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500287 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500288 if (rc)
289 return rc;
290 cmd_len = ISCSI_CDB_SIZE;
291 }
292 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500293
Mike Christie9c19a7d2008-05-21 15:54:09 -0500294 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500295 if (scsi_bidi_cmnd(sc)) {
296 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500297 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500298 if (rc)
299 return rc;
300 }
Mike Christie7996a772006-04-06 21:13:41 -0500301 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500302 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600303 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
304
Boaz Harroshc07d4442008-04-18 10:11:52 -0500305 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500306 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
307 /*
308 * Write counters:
309 *
310 * imm_count bytes to be sent right after
311 * SCSI PDU Header
312 *
313 * unsol_count bytes(as Data-Out) to be sent
314 * without R2T ack right after
315 * immediate data
316 *
Mike Christie577577d2008-12-02 00:32:05 -0600317 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500318 *
319 * pad_count bytes to be sent as zero-padding
320 */
Mike Christie577577d2008-12-02 00:32:05 -0600321 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500322
323 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500324 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500325 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500326 conn->max_xmit_dlength);
327 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500328 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500329 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500330 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500331 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600332 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500333
Mike Christieffd04362006-08-31 18:09:24 -0400334 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600335 r2t->data_length = min(session->first_burst, out_len) -
336 task->imm_count;
337 r2t->data_offset = task->imm_count;
338 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
339 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400340 }
341
Mike Christie577577d2008-12-02 00:32:05 -0600342 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500343 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600344 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500345 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500346 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
347 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500348 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500349
350 if (sc->sc_data_direction == DMA_FROM_DEVICE)
351 hdr->flags |= ISCSI_FLAG_CMD_READ;
352 }
353
Boaz Harrosh004d6532007-12-13 12:43:23 -0600354 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500355 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600356
357 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
358 hdrlength /= ISCSI_PAD_LEN;
359
360 WARN_ON(hdrlength >= 256);
361 hdr->hlength = hdrlength & 0xFF;
362
Mike Christie577577d2008-12-02 00:32:05 -0600363 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500364 return -EIO;
365
Mike Christie9c19a7d2008-05-21 15:54:09 -0500366 task->state = ISCSI_TASK_RUNNING;
367 list_move_tail(&task->running, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500368
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600370 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
371 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
372 scsi_bidi_cmnd(sc) ? "bidirectional" :
373 sc->sc_data_direction == DMA_TO_DEVICE ?
374 "write" : "read", conn->id, sc, sc->cmnd[0],
375 task->itt, scsi_bufflen(sc),
376 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
377 session->cmdsn,
378 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600379 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500380}
Mike Christie7996a772006-04-06 21:13:41 -0500381
382/**
Mike Christie3e5c28a2008-05-21 15:54:06 -0500383 * iscsi_complete_command - finish a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500385 *
386 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500387 * This function returns the scsi command to scsi-ml or cleans
388 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500389 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500390static void iscsi_complete_command(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500391{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500392 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600393 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500394 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500395
Mike Christie577577d2008-12-02 00:32:05 -0600396 session->tt->cleanup_task(task);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500397 list_del_init(&task->running);
398 task->state = ISCSI_TASK_COMPLETED;
399 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500400
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 if (conn->task == task)
402 conn->task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500403 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500404 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500405 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500406 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500407 return;
408
Mike Christie9c19a7d2008-05-21 15:54:09 -0500409 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500410
Mike Christie9c19a7d2008-05-21 15:54:09 -0500411 if (conn->ping_task == task)
412 conn->ping_task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500413
414 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500415 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500416 /* SCSI eh reuses commands to verify us */
417 sc->SCp.ptr = NULL;
418 /*
419 * queue command may call this to free the task, but
420 * not have setup the sc callback
421 */
422 if (sc->scsi_done)
423 sc->scsi_done(sc);
424 }
Mike Christie7996a772006-04-06 21:13:41 -0500425}
426
Mike Christie913e5bf2008-05-21 15:54:18 -0500427void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400428{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400430}
Mike Christie913e5bf2008-05-21 15:54:18 -0500431EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400432
Mike Christie9c19a7d2008-05-21 15:54:09 -0500433static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400434{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500435 if (atomic_dec_and_test(&task->refcount))
436 iscsi_complete_command(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400437}
438
Mike Christie9c19a7d2008-05-21 15:54:09 -0500439void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500440{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500441 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500442
443 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500444 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500445 spin_unlock_bh(&session->lock);
446}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500447EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500448
Mike Christieb3a7ea82007-12-13 12:43:26 -0600449/*
450 * session lock must be held
451 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500452static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600453 int err)
454{
455 struct scsi_cmnd *sc;
456
Mike Christie9c19a7d2008-05-21 15:54:09 -0500457 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600458 if (!sc)
459 return;
460
Mike Christie9c19a7d2008-05-21 15:54:09 -0500461 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600462 /*
463 * cmd never made it to the xmit thread, so we should not count
464 * the cmd in the sequencing
465 */
466 conn->session->queued_cmdsn--;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600467
468 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500469 if (!scsi_bidi_cmnd(sc))
470 scsi_set_resid(sc, scsi_bufflen(sc));
471 else {
472 scsi_out(sc)->resid = scsi_out(sc)->length;
473 scsi_in(sc)->resid = scsi_in(sc)->length;
474 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500475
Mike Christie9c19a7d2008-05-21 15:54:09 -0500476 if (conn->task == task)
477 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600478 /* release ref from queuecommand */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500479 __iscsi_put_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600480}
481
Mike Christie3e5c28a2008-05-21 15:54:06 -0500482static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500483 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500484{
485 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600486 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500487 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
488
489 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
490 return -ENOTCONN;
491
492 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
493 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
494 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
495 /*
496 * pre-format CmdSN for outgoing PDU.
497 */
498 nop->cmdsn = cpu_to_be32(session->cmdsn);
499 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500500 /*
501 * TODO: We always use immediate, so we never hit this.
502 * If we start to send tmfs or nops as non-immediate then
503 * we should start checking the cmdsn numbers for mgmt tasks.
504 */
505 if (conn->c_stage == ISCSI_CONN_STARTED &&
506 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
507 session->queued_cmdsn++;
508 session->cmdsn++;
509 }
510 }
511
Mike Christieae15f802008-12-02 00:32:15 -0600512 if (session->tt->init_task && session->tt->init_task(task))
513 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500514
515 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
516 session->state = ISCSI_STATE_LOGGING_OUT;
517
Mike Christie577577d2008-12-02 00:32:05 -0600518 task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 list_move_tail(&task->running, &conn->mgmt_run_list);
Mike Christie1b2c7af2009-03-05 14:45:58 -0600520 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
521 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
522 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500523 return 0;
524}
525
Mike Christie9c19a7d2008-05-21 15:54:09 -0500526static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600527__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
528 char *data, uint32_t data_size)
529{
530 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500531 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600532 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600533
534 if (session->state == ISCSI_STATE_TERMINATE)
535 return NULL;
536
537 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
538 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
539 /*
540 * Login and Text are sent serially, in
541 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500542 * Same task can be used. Same ITT must be used.
543 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600544 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500545 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600546 else {
547 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
548 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
549
Mike Christie3e5c28a2008-05-21 15:54:06 -0500550 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500551 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600552 return NULL;
553 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500554 /*
555 * released in complete pdu for task we expect a response for, and
556 * released by the lld when it has transmitted the task for
557 * pdus we do not expect a response for.
558 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500559 atomic_set(&task->refcount, 1);
560 task->conn = conn;
561 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600562
563 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500564 memcpy(task->data, data, data_size);
565 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600566 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500567 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600568
Mike Christie2ff79d52008-12-02 00:32:14 -0600569 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
Mike Christie577577d2008-12-02 00:32:05 -0600570 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
571 "pdu for mgmt task.\n");
572 goto requeue_task;
573 }
Mike Christie262ef632008-12-02 00:32:13 -0600574 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600575 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500576 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600577
578 if (hdr->itt != RESERVED_ITT) {
579 if (session->tt->parse_pdu_itt)
580 task->hdr->itt = itt;
581 else
582 task->hdr->itt = build_itt(task->itt,
583 task->conn->session->age);
584 }
585
Mike Christie9c19a7d2008-05-21 15:54:09 -0500586 INIT_LIST_HEAD(&task->running);
587 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500588
589 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -0600590 if (iscsi_prep_mgmt_task(conn, task))
591 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500592
Mike Christie9c19a7d2008-05-21 15:54:09 -0500593 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600594 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500595
596 } else
Mike Christie32ae7632009-03-05 14:46:03 -0600597 iscsi_conn_queue_work(conn);
Mike Christie052d0142008-05-21 15:54:05 -0500598
Mike Christie9c19a7d2008-05-21 15:54:09 -0500599 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600600
601free_task:
602 __iscsi_put_task(task);
603 return NULL;
604
605requeue_task:
606 if (task != conn->login_task)
607 __kfifo_put(session->cmdpool.queue, (void*)&task,
608 sizeof(void*));
609 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600610}
611
612int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
613 char *data, uint32_t data_size)
614{
615 struct iscsi_conn *conn = cls_conn->dd_data;
616 struct iscsi_session *session = conn->session;
617 int err = 0;
618
619 spin_lock_bh(&session->lock);
620 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
621 err = -EPERM;
622 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600623 return err;
624}
625EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
626
Mike Christie7996a772006-04-06 21:13:41 -0500627/**
628 * iscsi_cmd_rsp - SCSI Command Response processing
629 * @conn: iscsi connection
630 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500631 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500632 * @data: cmd data buffer
633 * @datalen: len of buffer
634 *
635 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500636 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500637 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500638static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500639 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500640 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500641{
Mike Christie7996a772006-04-06 21:13:41 -0500642 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
643 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500644 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500645
Mike Christie77a23c22007-05-30 12:57:18 -0500646 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500647 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
648
649 sc->result = (DID_OK << 16) | rhdr->cmd_status;
650
651 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
652 sc->result = DID_ERROR << 16;
653 goto out;
654 }
655
656 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600657 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500658
659 if (datalen < 2) {
660invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600661 iscsi_conn_printk(KERN_ERR, conn,
662 "Got CHECK_CONDITION but invalid data "
663 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500664 sc->result = DID_BAD_TARGET << 16;
665 goto out;
666 }
667
Harvey Harrison8f3339912008-05-21 15:54:20 -0500668 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500669 if (datalen < senselen)
670 goto invalid_datalen;
671
672 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600673 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600674 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
675 min_t(uint16_t, senselen,
676 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500677 }
678
Boaz Harroshc07d4442008-04-18 10:11:52 -0500679 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
680 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
681 int res_count = be32_to_cpu(rhdr->bi_residual_count);
682
683 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
684 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
685 res_count <= scsi_in(sc)->length))
686 scsi_in(sc)->resid = res_count;
687 else
688 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
689 }
690
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600691 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
692 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500693 int res_count = be32_to_cpu(rhdr->residual_count);
694
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600695 if (res_count > 0 &&
696 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
697 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500698 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900699 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500700 else
701 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500702 }
Mike Christie7996a772006-04-06 21:13:41 -0500703out:
Mike Christie1b2c7af2009-03-05 14:45:58 -0600704 ISCSI_DBG_SESSION(session, "done [sc %p res %d itt 0x%x]\n",
705 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500706 conn->scsirsp_pdus_cnt++;
707
Mike Christie9c19a7d2008-05-21 15:54:09 -0500708 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500709}
710
Mike Christie1d9edf02008-09-24 11:46:09 -0500711/**
712 * iscsi_data_in_rsp - SCSI Data-In Response processing
713 * @conn: iscsi connection
714 * @hdr: iscsi pdu
715 * @task: scsi command task
716 **/
717static void
718iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
719 struct iscsi_task *task)
720{
721 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
722 struct scsi_cmnd *sc = task->sc;
723
724 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
725 return;
726
727 sc->result = (DID_OK << 16) | rhdr->cmd_status;
728 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
729 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
730 ISCSI_FLAG_DATA_OVERFLOW)) {
731 int res_count = be32_to_cpu(rhdr->residual_count);
732
733 if (res_count > 0 &&
734 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
735 res_count <= scsi_in(sc)->length))
736 scsi_in(sc)->resid = res_count;
737 else
738 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
739 }
740
741 conn->scsirsp_pdus_cnt++;
742 __iscsi_put_task(task);
743}
744
Mike Christie7ea8b822006-07-24 15:47:22 -0500745static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
746{
747 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
748
749 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
750 conn->tmfrsp_pdus_cnt++;
751
Mike Christie843c0a82007-12-13 12:43:20 -0600752 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500753 return;
754
755 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600756 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500757 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600758 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500759 else
Mike Christie843c0a82007-12-13 12:43:20 -0600760 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500761 wake_up(&conn->ehwait);
762}
763
Mike Christief6d51802007-12-13 12:43:30 -0600764static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
765{
766 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500767 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600768
Mike Christie9c19a7d2008-05-21 15:54:09 -0500769 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600770 return;
771
772 memset(&hdr, 0, sizeof(struct iscsi_nopout));
773 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
774 hdr.flags = ISCSI_FLAG_CMD_FINAL;
775
776 if (rhdr) {
777 memcpy(hdr.lun, rhdr->lun, 8);
778 hdr.ttt = rhdr->ttt;
779 hdr.itt = RESERVED_ITT;
780 } else
781 hdr.ttt = RESERVED_ITT;
782
Mike Christie9c19a7d2008-05-21 15:54:09 -0500783 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
784 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600785 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600786 else if (!rhdr) {
787 /* only track our nops */
788 conn->ping_task = task;
789 conn->last_ping = jiffies;
790 }
Mike Christief6d51802007-12-13 12:43:30 -0600791}
792
Mike Christie62f38302006-08-31 18:09:27 -0400793static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
794 char *data, int datalen)
795{
796 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
797 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400798
799 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
800
801 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
802 if (ntoh24(reject->dlength) > datalen)
803 return ISCSI_ERR_PROTO;
804
805 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
806 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600807 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600808 "pdu (op 0x%x) rejected "
809 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600810 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400811 }
812 }
813 return 0;
814}
815
Mike Christie7996a772006-04-06 21:13:41 -0500816/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500817 * iscsi_itt_to_task - look up task by itt
818 * @conn: iscsi connection
819 * @itt: itt
820 *
821 * This should be used for mgmt tasks like login and nops, or if
822 * the LDD's itt space does not include the session age.
823 *
824 * The session lock must be held.
825 */
826static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
827{
828 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600829 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500830
831 if (itt == RESERVED_ITT)
832 return NULL;
833
Mike Christie262ef632008-12-02 00:32:13 -0600834 if (session->tt->parse_pdu_itt)
835 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
836 else
837 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500838 if (i >= session->cmds_max)
839 return NULL;
840
841 return session->cmds[i];
842}
843
844/**
Mike Christie7996a772006-04-06 21:13:41 -0500845 * __iscsi_complete_pdu - complete pdu
846 * @conn: iscsi conn
847 * @hdr: iscsi header
848 * @data: data buffer
849 * @datalen: len of data buffer
850 *
851 * Completes pdu processing by freeing any resources allocated at
852 * queuecommand or send generic. session lock must be held and verify
853 * itt must have been called.
854 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500855int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
856 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500857{
858 struct iscsi_session *session = conn->session;
859 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500860 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500861 uint32_t itt;
862
Mike Christief6d51802007-12-13 12:43:30 -0600863 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500864 rc = iscsi_verify_itt(conn, hdr->itt);
865 if (rc)
866 return rc;
867
Al Virob4377352007-02-09 16:39:40 +0000868 if (hdr->itt != RESERVED_ITT)
869 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500870 else
Al Virob4377352007-02-09 16:39:40 +0000871 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500872
Mike Christie1b2c7af2009-03-05 14:45:58 -0600873 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
874 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500875
Mike Christie3e5c28a2008-05-21 15:54:06 -0500876 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500877 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400878
Mike Christie7996a772006-04-06 21:13:41 -0500879 switch(opcode) {
880 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500881 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500882 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500883 break;
884 }
885
Al Virob4377352007-02-09 16:39:40 +0000886 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500887 break;
888
Mike Christief6d51802007-12-13 12:43:30 -0600889 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500890 break;
891 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400892 rc = iscsi_handle_reject(conn, hdr, data, datalen);
893 break;
Mike Christie7996a772006-04-06 21:13:41 -0500894 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500895 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400896 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
897 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500898 break;
899 default:
900 rc = ISCSI_ERR_BAD_OPCODE;
901 break;
902 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500903 goto out;
904 }
Mike Christie7996a772006-04-06 21:13:41 -0500905
Mike Christie3e5c28a2008-05-21 15:54:06 -0500906 switch(opcode) {
907 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500908 case ISCSI_OP_SCSI_DATA_IN:
909 task = iscsi_itt_to_ctask(conn, hdr->itt);
910 if (!task)
911 return ISCSI_ERR_BAD_ITT;
912 break;
913 case ISCSI_OP_R2T:
914 /*
915 * LLD handles R2Ts if they need to.
916 */
917 return 0;
918 case ISCSI_OP_LOGOUT_RSP:
919 case ISCSI_OP_LOGIN_RSP:
920 case ISCSI_OP_TEXT_RSP:
921 case ISCSI_OP_SCSI_TMFUNC_RSP:
922 case ISCSI_OP_NOOP_IN:
923 task = iscsi_itt_to_task(conn, hdr->itt);
924 if (!task)
925 return ISCSI_ERR_BAD_ITT;
926 break;
927 default:
928 return ISCSI_ERR_BAD_OPCODE;
929 }
930
931 switch(opcode) {
932 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500933 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500934 break;
935 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500936 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500937 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500938 case ISCSI_OP_LOGOUT_RSP:
939 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
940 if (datalen) {
941 rc = ISCSI_ERR_PROTO;
942 break;
943 }
944 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
945 goto recv_pdu;
946 case ISCSI_OP_LOGIN_RSP:
947 case ISCSI_OP_TEXT_RSP:
948 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
949 /*
950 * login related PDU's exp_statsn is handled in
951 * userspace
952 */
953 goto recv_pdu;
954 case ISCSI_OP_SCSI_TMFUNC_RSP:
955 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
956 if (datalen) {
957 rc = ISCSI_ERR_PROTO;
958 break;
959 }
960
961 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500962 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500963 break;
964 case ISCSI_OP_NOOP_IN:
965 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
966 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
967 rc = ISCSI_ERR_PROTO;
968 break;
969 }
970 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
971
Mike Christie9c19a7d2008-05-21 15:54:09 -0500972 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500973 /*
974 * If this is not in response to one of our
975 * nops then it must be from userspace.
976 */
977 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500978
979 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
980 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500981 break;
982 default:
983 rc = ISCSI_ERR_BAD_OPCODE;
984 break;
985 }
986
987out:
988 return rc;
989recv_pdu:
990 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
991 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500992 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500993 return rc;
994}
Mike Christie913e5bf2008-05-21 15:54:18 -0500995EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500996
997int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
998 char *data, int datalen)
999{
1000 int rc;
1001
1002 spin_lock(&conn->session->lock);
1003 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1004 spin_unlock(&conn->session->lock);
1005 return rc;
1006}
1007EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1008
Mike Christie0af967f2008-05-21 15:54:04 -05001009int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001010{
1011 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001012 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001013
Mike Christie0af967f2008-05-21 15:54:04 -05001014 if (itt == RESERVED_ITT)
1015 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001016
Mike Christie262ef632008-12-02 00:32:13 -06001017 if (session->tt->parse_pdu_itt)
1018 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1019 else {
1020 i = get_itt(itt);
1021 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1022 }
1023
1024 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001025 iscsi_conn_printk(KERN_ERR, conn,
1026 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001027 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001028 return ISCSI_ERR_BAD_ITT;
1029 }
Mike Christie7996a772006-04-06 21:13:41 -05001030
Mike Christie3e5c28a2008-05-21 15:54:06 -05001031 if (i >= session->cmds_max) {
1032 iscsi_conn_printk(KERN_ERR, conn,
1033 "received invalid itt index %u (max cmds "
1034 "%u.\n", i, session->cmds_max);
1035 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001036 }
Mike Christie7996a772006-04-06 21:13:41 -05001037 return 0;
1038}
1039EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1040
Mike Christie913e5bf2008-05-21 15:54:18 -05001041/**
1042 * iscsi_itt_to_ctask - look up ctask by itt
1043 * @conn: iscsi connection
1044 * @itt: itt
1045 *
1046 * This should be used for cmd tasks.
1047 *
1048 * The session lock must be held.
1049 */
1050struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001051{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001052 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001053
1054 if (iscsi_verify_itt(conn, itt))
1055 return NULL;
1056
Mike Christie913e5bf2008-05-21 15:54:18 -05001057 task = iscsi_itt_to_task(conn, itt);
1058 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001059 return NULL;
1060
Mike Christie913e5bf2008-05-21 15:54:18 -05001061 if (task->sc->SCp.phase != conn->session->age) {
1062 iscsi_session_printk(KERN_ERR, conn->session,
1063 "task's session age %d, expected %d\n",
1064 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001065 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001066 }
Mike Christie0af967f2008-05-21 15:54:04 -05001067
Mike Christie9c19a7d2008-05-21 15:54:09 -05001068 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001069}
1070EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1071
Mike Christie40a06e72009-03-05 14:46:05 -06001072void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001073 enum iscsi_err err)
1074{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001075 struct iscsi_conn *conn;
1076 struct device *dev;
1077 unsigned long flags;
1078
1079 spin_lock_irqsave(&session->lock, flags);
1080 conn = session->leadconn;
1081 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1082 spin_unlock_irqrestore(&session->lock, flags);
1083 return;
1084 }
1085
1086 dev = get_device(&conn->cls_conn->dev);
1087 spin_unlock_irqrestore(&session->lock, flags);
1088 if (!dev)
1089 return;
1090 /*
1091 * if the host is being removed bypass the connection
1092 * recovery initialization because we are going to kill
1093 * the session.
1094 */
1095 if (err == ISCSI_ERR_INVALID_HOST)
1096 iscsi_conn_error_event(conn->cls_conn, err);
1097 else
1098 iscsi_conn_failure(conn, err);
1099 put_device(dev);
1100}
1101EXPORT_SYMBOL_GPL(iscsi_session_failure);
1102
Mike Christie7996a772006-04-06 21:13:41 -05001103void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1104{
1105 struct iscsi_session *session = conn->session;
1106 unsigned long flags;
1107
1108 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001109 if (session->state == ISCSI_STATE_FAILED) {
1110 spin_unlock_irqrestore(&session->lock, flags);
1111 return;
1112 }
1113
Mike Christie67a61112006-05-30 00:37:20 -05001114 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001115 session->state = ISCSI_STATE_FAILED;
1116 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001117
Mike Christie7996a772006-04-06 21:13:41 -05001118 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1119 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001120 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001121}
1122EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1123
Mike Christie77a23c22007-05-30 12:57:18 -05001124static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1125{
1126 struct iscsi_session *session = conn->session;
1127
1128 /*
1129 * Check for iSCSI window and take care of CmdSN wrap-around
1130 */
Mike Christiee0726402007-07-26 12:46:48 -05001131 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001132 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1133 "%u MaxCmdSN %u CmdSN %u/%u\n",
1134 session->exp_cmdsn, session->max_cmdsn,
1135 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001136 return -ENOSPC;
1137 }
1138 return 0;
1139}
1140
Mike Christie9c19a7d2008-05-21 15:54:09 -05001141static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001142{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001143 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001144 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001145
Mike Christie9c19a7d2008-05-21 15:54:09 -05001146 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001147 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001148 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001149 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001150 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001151 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001152 /* done with this task */
1153 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001154 return rc;
1155}
1156
Mike Christie7996a772006-04-06 21:13:41 -05001157/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001158 * iscsi_requeue_task - requeue task to run from session workqueue
1159 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001160 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001161 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001162 * this. The session lock must be held. This should only be called
1163 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001164 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001165void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001166{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001167 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001168
Mike Christie9c19a7d2008-05-21 15:54:09 -05001169 list_move_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001170 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001171}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001172EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001173
1174/**
Mike Christie7996a772006-04-06 21:13:41 -05001175 * iscsi_data_xmit - xmit any command into the scheduled connection
1176 * @conn: iscsi connection
1177 *
1178 * Notes:
1179 * The function can return -EAGAIN in which case the caller must
1180 * re-schedule it again later or recover. '0' return code means
1181 * successful xmit.
1182 **/
1183static int iscsi_data_xmit(struct iscsi_conn *conn)
1184{
Mike Christie3219e522006-05-30 00:37:28 -05001185 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001186
Mike Christie77a23c22007-05-30 12:57:18 -05001187 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001188 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001189 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001190 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001191 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001192 }
Mike Christie7996a772006-04-06 21:13:41 -05001193
Mike Christie9c19a7d2008-05-21 15:54:09 -05001194 if (conn->task) {
1195 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001196 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001197 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001198 }
1199
Mike Christie77a23c22007-05-30 12:57:18 -05001200 /*
1201 * process mgmt pdus like nops before commands since we should
1202 * only have one nop-out as a ping from us and targets should not
1203 * overflow us with nop-ins
1204 */
1205check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001206 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001207 conn->task = list_entry(conn->mgmtqueue.next,
1208 struct iscsi_task, running);
1209 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1210 __iscsi_put_task(conn->task);
1211 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001212 continue;
1213 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001214 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001215 if (rc)
1216 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001217 }
1218
Mike Christie843c0a82007-12-13 12:43:20 -06001219 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001220 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001221 if (conn->tmf_state == TMF_QUEUED)
1222 break;
1223
Mike Christie9c19a7d2008-05-21 15:54:09 -05001224 conn->task = list_entry(conn->xmitqueue.next,
1225 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001226 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001227 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001228 continue;
1229 }
Mike Christie577577d2008-12-02 00:32:05 -06001230 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1231 if (rc) {
1232 if (rc == -ENOMEM) {
1233 conn->task = NULL;
1234 goto again;
1235 } else
1236 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001237 continue;
1238 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001239 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001240 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001241 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001242 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001243 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001244 * we need to check the mgmt queue for nops that need to
1245 * be sent to aviod starvation
1246 */
Mike Christie843c0a82007-12-13 12:43:20 -06001247 if (!list_empty(&conn->mgmtqueue))
1248 goto check_mgmt;
1249 }
1250
1251 while (!list_empty(&conn->requeue)) {
1252 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1253 break;
1254
Mike Christieb3a7ea82007-12-13 12:43:26 -06001255 /*
1256 * we always do fastlogout - conn stop code will clean up.
1257 */
1258 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1259 break;
1260
Mike Christie9c19a7d2008-05-21 15:54:09 -05001261 conn->task = list_entry(conn->requeue.next,
1262 struct iscsi_task, running);
1263 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001264 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001265 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001266 if (rc)
1267 goto again;
1268 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001269 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001270 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001271 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001272 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001273
1274again:
1275 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001276 rc = -ENODATA;
1277 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001278 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001279}
1280
David Howellsc4028952006-11-22 14:57:56 +00001281static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001282{
David Howellsc4028952006-11-22 14:57:56 +00001283 struct iscsi_conn *conn =
1284 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001285 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001286 /*
1287 * serialize Xmit worker on a per-connection basis.
1288 */
Mike Christie3219e522006-05-30 00:37:28 -05001289 do {
1290 rc = iscsi_data_xmit(conn);
1291 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001292}
1293
Mike Christie577577d2008-12-02 00:32:05 -06001294static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1295 struct scsi_cmnd *sc)
1296{
1297 struct iscsi_task *task;
1298
1299 if (!__kfifo_get(conn->session->cmdpool.queue,
1300 (void *) &task, sizeof(void *)))
1301 return NULL;
1302
1303 sc->SCp.phase = conn->session->age;
1304 sc->SCp.ptr = (char *) task;
1305
1306 atomic_set(&task->refcount, 1);
1307 task->state = ISCSI_TASK_PENDING;
1308 task->conn = conn;
1309 task->sc = sc;
1310 INIT_LIST_HEAD(&task->running);
1311 return task;
1312}
1313
Mike Christie7996a772006-04-06 21:13:41 -05001314enum {
1315 FAILURE_BAD_HOST = 1,
1316 FAILURE_SESSION_FAILED,
1317 FAILURE_SESSION_FREED,
1318 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001319 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001320 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001321 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001322 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001323 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001324 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001325};
1326
1327int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1328{
Mike Christie75613522008-05-21 15:53:59 -05001329 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001330 struct Scsi_Host *host;
1331 int reason = 0;
1332 struct iscsi_session *session;
1333 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001334 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001335
1336 sc->scsi_done = done;
1337 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001338 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001339
1340 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001341 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001342
Mike Christie75613522008-05-21 15:53:59 -05001343 cls_session = starget_to_session(scsi_target(sc->device));
1344 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001345 spin_lock(&session->lock);
1346
Mike Christie75613522008-05-21 15:53:59 -05001347 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001348 if (reason) {
1349 sc->result = reason;
1350 goto fault;
1351 }
1352
Mike Christie656cffc2006-05-18 20:31:42 -05001353 /*
1354 * ISCSI_STATE_FAILED is a temp. state. The recovery
1355 * code will decide what is best to do with command queued
1356 * during this time
1357 */
1358 if (session->state != ISCSI_STATE_LOGGED_IN &&
1359 session->state != ISCSI_STATE_FAILED) {
1360 /*
1361 * to handle the race between when we set the recovery state
1362 * and block the session we requeue here (commands could
1363 * be entering our queuecommand while a block is starting
1364 * up because the block code is not locked)
1365 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001366 switch (session->state) {
1367 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001368 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christied6d13ee2008-08-17 15:24:43 -05001369 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001370 case ISCSI_STATE_LOGGING_OUT:
1371 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christied6d13ee2008-08-17 15:24:43 -05001372 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001373 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001374 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001375 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001376 break;
1377 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001378 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001379 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001380 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001381 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001382 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001383 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001384 }
Mike Christie7996a772006-04-06 21:13:41 -05001385 goto fault;
1386 }
1387
Mike Christie7996a772006-04-06 21:13:41 -05001388 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001389 if (!conn) {
1390 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001391 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001392 goto fault;
1393 }
Mike Christie7996a772006-04-06 21:13:41 -05001394
Mike Christie77a23c22007-05-30 12:57:18 -05001395 if (iscsi_check_cmdsn_window_closed(conn)) {
1396 reason = FAILURE_WINDOW_CLOSED;
1397 goto reject;
1398 }
1399
Mike Christie577577d2008-12-02 00:32:05 -06001400 task = iscsi_alloc_task(conn, sc);
1401 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001402 reason = FAILURE_OOM;
1403 goto reject;
1404 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001405 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001406
Mike Christie052d0142008-05-21 15:54:05 -05001407 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -06001408 reason = iscsi_prep_scsi_cmd_pdu(task);
1409 if (reason) {
1410 if (reason == -ENOMEM) {
1411 reason = FAILURE_OOM;
1412 goto prepd_reject;
1413 } else {
1414 sc->result = DID_ABORT << 16;
1415 goto prepd_fault;
1416 }
Mike Christie052d0142008-05-21 15:54:05 -05001417 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001418 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001419 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001420 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001421 }
1422 } else
Mike Christie32ae7632009-03-05 14:46:03 -06001423 iscsi_conn_queue_work(conn);
Mike Christie052d0142008-05-21 15:54:05 -05001424
1425 session->queued_cmdsn++;
1426 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001427 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001428 return 0;
1429
Mike Christie577577d2008-12-02 00:32:05 -06001430prepd_reject:
1431 sc->scsi_done = NULL;
1432 iscsi_complete_command(task);
Mike Christie7996a772006-04-06 21:13:41 -05001433reject:
1434 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001435 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1436 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001437 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001438 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001439
Mike Christie577577d2008-12-02 00:32:05 -06001440prepd_fault:
1441 sc->scsi_done = NULL;
1442 iscsi_complete_command(task);
Mike Christie7996a772006-04-06 21:13:41 -05001443fault:
1444 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001445 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1446 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001447 if (!scsi_bidi_cmnd(sc))
1448 scsi_set_resid(sc, scsi_bufflen(sc));
1449 else {
1450 scsi_out(sc)->resid = scsi_out(sc)->length;
1451 scsi_in(sc)->resid = scsi_in(sc)->length;
1452 }
Mike Christie052d0142008-05-21 15:54:05 -05001453 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001454 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001455 return 0;
1456}
1457EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1458
1459int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1460{
Mike Christie7996a772006-04-06 21:13:41 -05001461 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1462 return sdev->queue_depth;
1463}
1464EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1465
Mike Christie7996a772006-04-06 21:13:41 -05001466void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1467{
Mike Christie75613522008-05-21 15:53:59 -05001468 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001469
1470 spin_lock_bh(&session->lock);
1471 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001472 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001473 if (session->leadconn)
1474 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001475 }
1476 spin_unlock_bh(&session->lock);
1477}
1478EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1479
Mike Christie8e124522008-09-24 11:46:12 -05001480int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001481{
Mike Christie75613522008-05-21 15:53:59 -05001482 struct iscsi_cls_session *cls_session;
1483 struct iscsi_session *session;
1484 struct iscsi_conn *conn;
1485
1486 cls_session = starget_to_session(scsi_target(sc->device));
1487 session = cls_session->dd_data;
1488 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001489
Mike Christiebc436b22007-12-13 12:43:28 -06001490 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001491 spin_lock_bh(&session->lock);
1492 if (session->state == ISCSI_STATE_TERMINATE) {
1493failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001494 iscsi_session_printk(KERN_INFO, session,
1495 "failing target reset: Could not log "
1496 "back into target [age %d]\n",
1497 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001498 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001499 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001500 return FAILED;
1501 }
1502
Mike Christie7996a772006-04-06 21:13:41 -05001503 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001504 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001505 /*
1506 * we drop the lock here but the leadconn cannot be destoyed while
1507 * we are in the scsi eh
1508 */
Mike Christie843c0a82007-12-13 12:43:20 -06001509 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001510
Mike Christie1b2c7af2009-03-05 14:45:58 -06001511 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001512 wait_event_interruptible(conn->ehwait,
1513 session->state == ISCSI_STATE_TERMINATE ||
1514 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001515 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001516 if (signal_pending(current))
1517 flush_signals(current);
1518
Mike Christiebc436b22007-12-13 12:43:28 -06001519 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001520 spin_lock_bh(&session->lock);
1521 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001522 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001523 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001524 else
1525 goto failed;
1526 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001527 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001528 return SUCCESS;
1529}
Mike Christie8e124522008-09-24 11:46:12 -05001530EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001531
Mike Christie843c0a82007-12-13 12:43:20 -06001532static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001533{
Mike Christie843c0a82007-12-13 12:43:20 -06001534 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001535 struct iscsi_session *session = conn->session;
1536
1537 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001538 if (conn->tmf_state == TMF_QUEUED) {
1539 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001540 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001541 /* unblock eh_abort() */
1542 wake_up(&conn->ehwait);
1543 }
1544 spin_unlock(&session->lock);
1545}
1546
Mike Christie9c19a7d2008-05-21 15:54:09 -05001547static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001548 struct iscsi_tm *hdr, int age,
1549 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001550{
Mike Christie7996a772006-04-06 21:13:41 -05001551 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001552 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001553
Mike Christie9c19a7d2008-05-21 15:54:09 -05001554 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001555 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001556 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001557 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001558 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001559 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001560 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001561 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001562 }
Mike Christie843c0a82007-12-13 12:43:20 -06001563 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001564 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001565 conn->tmf_timer.function = iscsi_tmf_timedout;
1566 conn->tmf_timer.data = (unsigned long)conn;
1567 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001568 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001569
Mike Christie7996a772006-04-06 21:13:41 -05001570 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001571 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001572
1573 /*
1574 * block eh thread until:
1575 *
Mike Christie843c0a82007-12-13 12:43:20 -06001576 * 1) tmf response
1577 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001578 * 3) session is terminated or restarted or userspace has
1579 * given up on recovery
1580 */
Mike Christie843c0a82007-12-13 12:43:20 -06001581 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001582 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001583 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001584 if (signal_pending(current))
1585 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001586 del_timer_sync(&conn->tmf_timer);
1587
Mike Christie6724add2007-08-15 01:38:30 -05001588 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001589 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001590 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001591 if (age != session->age ||
1592 session->state != ISCSI_STATE_LOGGED_IN)
1593 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001594 return 0;
1595}
1596
1597/*
Mike Christie843c0a82007-12-13 12:43:20 -06001598 * Fail commands. session lock held and recv side suspended and xmit
1599 * thread flushed
1600 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001601static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1602 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001603{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001604 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001605
Mike Christie9c19a7d2008-05-21 15:54:09 -05001606 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1607 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001608
1609 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001610 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1611 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001612 ISCSI_DBG_SESSION(conn->session,
1613 "failing pending sc %p itt 0x%x\n",
1614 task->sc, task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001615 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001616 }
1617 }
1618
Mike Christie9c19a7d2008-05-21 15:54:09 -05001619 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1620 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001621 ISCSI_DBG_SESSION(conn->session,
1622 "failing requeued sc %p itt 0x%x\n",
1623 task->sc, task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001624 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001625 }
1626 }
1627
1628 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001629 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1630 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001631 ISCSI_DBG_SESSION(conn->session,
1632 "failing in progress sc %p itt 0x%x\n",
1633 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001634 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001635 }
1636 }
1637}
1638
Mike Christieb40977d2008-05-21 15:54:03 -05001639void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001640{
Mike Christie32ae7632009-03-05 14:46:03 -06001641 struct Scsi_Host *shost = conn->session->host;
1642 struct iscsi_host *ihost = shost_priv(shost);
1643
Mike Christie6724add2007-08-15 01:38:30 -05001644 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001645 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
Mike Christie32ae7632009-03-05 14:46:03 -06001646 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001647}
Mike Christieb40977d2008-05-21 15:54:03 -05001648EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001649
1650static void iscsi_start_tx(struct iscsi_conn *conn)
1651{
1652 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001653 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
Mike Christie32ae7632009-03-05 14:46:03 -06001654 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001655}
1656
Jens Axboe242f9dc2008-09-14 05:55:09 -07001657static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001658{
1659 struct iscsi_cls_session *cls_session;
1660 struct iscsi_session *session;
1661 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001662 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001663
1664 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001665 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001666
Mike Christie1b2c7af2009-03-05 14:45:58 -06001667 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
Mike Christief6d51802007-12-13 12:43:30 -06001668
1669 spin_lock(&session->lock);
1670 if (session->state != ISCSI_STATE_LOGGED_IN) {
1671 /*
1672 * We are probably in the middle of iscsi recovery so let
1673 * that complete and handle the error.
1674 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001675 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001676 goto done;
1677 }
1678
1679 conn = session->leadconn;
1680 if (!conn) {
1681 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001682 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001683 goto done;
1684 }
1685
1686 if (!conn->recv_timeout && !conn->ping_timeout)
1687 goto done;
1688 /*
1689 * if the ping timedout then we are in the middle of cleaning up
1690 * and can let the iscsi eh handle it
1691 */
1692 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1693 (conn->ping_timeout * HZ), jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001694 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001695 /*
1696 * if we are about to check the transport then give the command
1697 * more time
1698 */
1699 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1700 jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001701 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001702 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001703 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001704 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001705done:
1706 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001707 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1708 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001709 return rc;
1710}
1711
1712static void iscsi_check_transport_timeouts(unsigned long data)
1713{
1714 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1715 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001716 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001717
1718 spin_lock(&session->lock);
1719 if (session->state != ISCSI_STATE_LOGGED_IN)
1720 goto done;
1721
Mike Christie4cf10432008-05-07 20:43:52 -05001722 recv_timeout = conn->recv_timeout;
1723 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001724 goto done;
1725
Mike Christie4cf10432008-05-07 20:43:52 -05001726 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001727 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001728 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001729 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001730 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001731 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1732 "expired, last rx %lu, last ping %lu, "
1733 "now %lu\n", conn->ping_timeout, last_recv,
1734 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001735 spin_unlock(&session->lock);
1736 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1737 return;
1738 }
1739
Mike Christie4cf10432008-05-07 20:43:52 -05001740 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001741 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001742 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001743 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001744 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001745 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001746 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001747
Mike Christie1b2c7af2009-03-05 14:45:58 -06001748 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001749 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001750done:
1751 spin_unlock(&session->lock);
1752}
1753
Mike Christie9c19a7d2008-05-21 15:54:09 -05001754static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001755 struct iscsi_tm *hdr)
1756{
1757 memset(hdr, 0, sizeof(*hdr));
1758 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1759 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1760 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001761 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1762 hdr->rtt = task->hdr_itt;
1763 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001764}
1765
Mike Christie7996a772006-04-06 21:13:41 -05001766int iscsi_eh_abort(struct scsi_cmnd *sc)
1767{
Mike Christie75613522008-05-21 15:53:59 -05001768 struct iscsi_cls_session *cls_session;
1769 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001770 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001771 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001772 struct iscsi_tm *hdr;
1773 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001774
Mike Christie75613522008-05-21 15:53:59 -05001775 cls_session = starget_to_session(scsi_target(sc->device));
1776 session = cls_session->dd_data;
1777
Mike Christie6724add2007-08-15 01:38:30 -05001778 mutex_lock(&session->eh_mutex);
1779 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001780 /*
1781 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1782 * got the command.
1783 */
1784 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001785 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1786 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001787 spin_unlock_bh(&session->lock);
1788 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001789 return SUCCESS;
1790 }
1791
Mike Christie7996a772006-04-06 21:13:41 -05001792 /*
1793 * If we are not logged in or we have started a new session
1794 * then let the host reset code handle this
1795 */
Mike Christie843c0a82007-12-13 12:43:20 -06001796 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1797 sc->SCp.phase != session->age) {
1798 spin_unlock_bh(&session->lock);
1799 mutex_unlock(&session->eh_mutex);
1800 return FAILED;
1801 }
1802
1803 conn = session->leadconn;
1804 conn->eh_abort_cnt++;
1805 age = session->age;
1806
Mike Christie9c19a7d2008-05-21 15:54:09 -05001807 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001808 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1809 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001810
Mike Christie9c19a7d2008-05-21 15:54:09 -05001811 /* task completed before time out */
1812 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001813 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1814 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001815 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001816 }
Mike Christie7996a772006-04-06 21:13:41 -05001817
Mike Christie9c19a7d2008-05-21 15:54:09 -05001818 if (task->state == ISCSI_TASK_PENDING) {
1819 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001820 goto success;
1821 }
Mike Christie7996a772006-04-06 21:13:41 -05001822
Mike Christie843c0a82007-12-13 12:43:20 -06001823 /* only have one tmf outstanding at a time */
1824 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001825 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001826 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001827
Mike Christie843c0a82007-12-13 12:43:20 -06001828 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001829 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001830
Mike Christie9c19a7d2008-05-21 15:54:09 -05001831 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001832 rc = FAILED;
1833 goto failed;
1834 }
1835
1836 switch (conn->tmf_state) {
1837 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001838 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001839 /*
1840 * stop tx side incase the target had sent a abort rsp but
1841 * the initiator was still writing out data.
1842 */
Mike Christie6724add2007-08-15 01:38:30 -05001843 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001844 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001845 * we do not stop the recv side because targets have been
1846 * good and have never sent us a successful tmf response
1847 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001848 */
Mike Christie77a23c22007-05-30 12:57:18 -05001849 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001850 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001851 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001852 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001853 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001854 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001855 case TMF_TIMEDOUT:
1856 spin_unlock_bh(&session->lock);
1857 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1858 goto failed_unlocked;
1859 case TMF_NOT_FOUND:
1860 if (!sc->SCp.ptr) {
1861 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001862 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001863 ISCSI_DBG_SESSION(session, "sc completed while abort "
1864 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001865 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001866 }
1867 /* fall through */
1868 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001869 conn->tmf_state = TMF_INITIAL;
1870 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001871 }
1872
Mike Christie77a23c22007-05-30 12:57:18 -05001873success:
Mike Christie7996a772006-04-06 21:13:41 -05001874 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001875success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001876 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1877 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001878 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001879 return SUCCESS;
1880
1881failed:
1882 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001883failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001884 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1885 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001886 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001887 return FAILED;
1888}
1889EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1890
Mike Christie843c0a82007-12-13 12:43:20 -06001891static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1892{
1893 memset(hdr, 0, sizeof(*hdr));
1894 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1895 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1896 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1897 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001898 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001899}
1900
1901int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1902{
Mike Christie75613522008-05-21 15:53:59 -05001903 struct iscsi_cls_session *cls_session;
1904 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001905 struct iscsi_conn *conn;
1906 struct iscsi_tm *hdr;
1907 int rc = FAILED;
1908
Mike Christie75613522008-05-21 15:53:59 -05001909 cls_session = starget_to_session(scsi_target(sc->device));
1910 session = cls_session->dd_data;
1911
Mike Christie1b2c7af2009-03-05 14:45:58 -06001912 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1913 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06001914
1915 mutex_lock(&session->eh_mutex);
1916 spin_lock_bh(&session->lock);
1917 /*
1918 * Just check if we are not logged in. We cannot check for
1919 * the phase because the reset could come from a ioctl.
1920 */
1921 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1922 goto unlock;
1923 conn = session->leadconn;
1924
1925 /* only have one tmf outstanding at a time */
1926 if (conn->tmf_state != TMF_INITIAL)
1927 goto unlock;
1928 conn->tmf_state = TMF_QUEUED;
1929
1930 hdr = &conn->tmhdr;
1931 iscsi_prep_lun_reset_pdu(sc, hdr);
1932
Mike Christie9c19a7d2008-05-21 15:54:09 -05001933 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001934 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001935 rc = FAILED;
1936 goto unlock;
1937 }
1938
1939 switch (conn->tmf_state) {
1940 case TMF_SUCCESS:
1941 break;
1942 case TMF_TIMEDOUT:
1943 spin_unlock_bh(&session->lock);
1944 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1945 goto done;
1946 default:
1947 conn->tmf_state = TMF_INITIAL;
1948 goto unlock;
1949 }
1950
1951 rc = SUCCESS;
1952 spin_unlock_bh(&session->lock);
1953
1954 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001955
Mike Christiea3439142008-09-24 11:46:15 -05001956 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001957 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001958 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05001959 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001960
1961 iscsi_start_tx(conn);
1962 goto done;
1963
1964unlock:
1965 spin_unlock_bh(&session->lock);
1966done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001967 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
1968 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06001969 mutex_unlock(&session->eh_mutex);
1970 return rc;
1971}
1972EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1973
Olaf Kirch63203772007-12-13 12:43:25 -06001974/*
1975 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1976 * should be accessed via kfifo_{get,put} on q->queue.
1977 * Optionally, the caller can obtain the array of object pointers
1978 * by passing in a non-NULL @items pointer
1979 */
Mike Christie7996a772006-04-06 21:13:41 -05001980int
Olaf Kirch63203772007-12-13 12:43:25 -06001981iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001982{
Olaf Kirch63203772007-12-13 12:43:25 -06001983 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001984
Olaf Kirch63203772007-12-13 12:43:25 -06001985 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001986
1987 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001988
1989 /* If the user passed an items pointer, he wants a copy of
1990 * the array. */
1991 if (items)
1992 num_arrays++;
1993 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1994 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06001995 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001996
1997 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1998 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001999 if (q->queue == ERR_PTR(-ENOMEM))
2000 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002001
2002 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002003 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002004 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002005 q->max = i;
2006 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002007 }
Mike Christie7996a772006-04-06 21:13:41 -05002008 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2009 }
Olaf Kirch63203772007-12-13 12:43:25 -06002010
2011 if (items) {
2012 *items = q->pool + max;
2013 memcpy(*items, q->pool, max * sizeof(void *));
2014 }
2015
Mike Christie7996a772006-04-06 21:13:41 -05002016 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002017
2018enomem:
2019 iscsi_pool_free(q);
2020 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002021}
2022EXPORT_SYMBOL_GPL(iscsi_pool_init);
2023
Olaf Kirch63203772007-12-13 12:43:25 -06002024void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002025{
2026 int i;
2027
2028 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002029 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002030 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002031 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002032}
2033EXPORT_SYMBOL_GPL(iscsi_pool_free);
2034
Mike Christiea4804cd2008-05-21 15:54:00 -05002035/**
2036 * iscsi_host_add - add host to system
2037 * @shost: scsi host
2038 * @pdev: parent device
2039 *
2040 * This should be called by partial offload and software iscsi drivers
2041 * to add a host to the system.
2042 */
2043int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002044{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002045 if (!shost->can_queue)
2046 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2047
Mike Christie4d108352009-03-05 14:46:04 -06002048 if (!shost->cmd_per_lun)
2049 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2050
Mike Christie308cec12009-02-06 12:06:20 -06002051 if (!shost->transportt->eh_timed_out)
2052 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002053 return scsi_add_host(shost, pdev);
2054}
2055EXPORT_SYMBOL_GPL(iscsi_host_add);
2056
2057/**
2058 * iscsi_host_alloc - allocate a host and driver data
2059 * @sht: scsi host template
2060 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002061 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002062 *
2063 * This should be called by partial offload and software iscsi drivers.
2064 * To access the driver specific memory use the iscsi_host_priv() macro.
2065 */
2066struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002067 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002068{
2069 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002070 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002071
2072 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2073 if (!shost)
2074 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002075 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002076
2077 if (xmit_can_sleep) {
2078 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2079 "iscsi_q_%d", shost->host_no);
2080 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2081 if (!ihost->workq)
2082 goto free_host;
2083 }
2084
Mike Christiee5bd7b52008-09-24 11:46:10 -05002085 spin_lock_init(&ihost->lock);
2086 ihost->state = ISCSI_HOST_SETUP;
2087 ihost->num_sessions = 0;
2088 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002089 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002090
2091free_host:
2092 scsi_host_put(shost);
2093 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002094}
Mike Christiea4804cd2008-05-21 15:54:00 -05002095EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002096
Mike Christiee5bd7b52008-09-24 11:46:10 -05002097static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2098{
Mike Christie40a06e72009-03-05 14:46:05 -06002099 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002100}
2101
Mike Christiea4804cd2008-05-21 15:54:00 -05002102/**
2103 * iscsi_host_remove - remove host and sessions
2104 * @shost: scsi host
2105 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002106 * If there are any sessions left, this will initiate the removal and wait
2107 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002108 */
2109void iscsi_host_remove(struct Scsi_Host *shost)
2110{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002111 struct iscsi_host *ihost = shost_priv(shost);
2112 unsigned long flags;
2113
2114 spin_lock_irqsave(&ihost->lock, flags);
2115 ihost->state = ISCSI_HOST_REMOVED;
2116 spin_unlock_irqrestore(&ihost->lock, flags);
2117
2118 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2119 wait_event_interruptible(ihost->session_removal_wq,
2120 ihost->num_sessions == 0);
2121 if (signal_pending(current))
2122 flush_signals(current);
2123
Mike Christiea4804cd2008-05-21 15:54:00 -05002124 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002125 if (ihost->workq)
2126 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002127}
2128EXPORT_SYMBOL_GPL(iscsi_host_remove);
2129
2130void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002131{
2132 struct iscsi_host *ihost = shost_priv(shost);
2133
2134 kfree(ihost->netdev);
2135 kfree(ihost->hwaddress);
2136 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002137 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002138}
Mike Christiea4804cd2008-05-21 15:54:00 -05002139EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002140
Mike Christiee5bd7b52008-09-24 11:46:10 -05002141static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2142{
2143 struct iscsi_host *ihost = shost_priv(shost);
2144 unsigned long flags;
2145
2146 shost = scsi_host_get(shost);
2147 if (!shost) {
2148 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2149 "of session teardown event because host already "
2150 "removed.\n");
2151 return;
2152 }
2153
2154 spin_lock_irqsave(&ihost->lock, flags);
2155 ihost->num_sessions--;
2156 if (ihost->num_sessions == 0)
2157 wake_up(&ihost->session_removal_wq);
2158 spin_unlock_irqrestore(&ihost->lock, flags);
2159 scsi_host_put(shost);
2160}
2161
Mike Christie75613522008-05-21 15:53:59 -05002162/**
2163 * iscsi_session_setup - create iscsi cls session and host and session
2164 * @iscsit: iscsi transport template
2165 * @shost: scsi host
2166 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002167 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002168 * @initial_cmdsn: initial CmdSN
2169 *
2170 * This can be used by software iscsi_transports that allocate
2171 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002172 *
2173 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2174 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2175 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002176 */
2177struct iscsi_cls_session *
2178iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002179 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002180 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002181{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002182 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002183 struct iscsi_session *session;
2184 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002185 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002186 unsigned long flags;
2187
2188 spin_lock_irqsave(&ihost->lock, flags);
2189 if (ihost->state == ISCSI_HOST_REMOVED) {
2190 spin_unlock_irqrestore(&ihost->lock, flags);
2191 return NULL;
2192 }
2193 ihost->num_sessions++;
2194 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002195
2196 if (!total_cmds)
2197 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002198 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002199 * The iscsi layer needs some tasks for nop handling and tmfs,
2200 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2201 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002202 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002203 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2204 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2205 "must be a power of two that is at least %d.\n",
2206 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002207 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002208 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002209
2210 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2211 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2212 "must be a power of 2 less than or equal to %d.\n",
2213 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2214 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2215 }
2216
2217 if (!is_power_of_2(total_cmds)) {
2218 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2219 "must be a power of 2.\n", total_cmds);
2220 total_cmds = rounddown_pow_of_two(total_cmds);
2221 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2222 return NULL;
2223 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2224 total_cmds);
2225 }
2226 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002227
Mike Christie5d91e202008-05-21 15:54:01 -05002228 cls_session = iscsi_alloc_session(shost, iscsit,
2229 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002230 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002231 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002232 session = cls_session->dd_data;
2233 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002234 session->host = shost;
2235 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002236 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002237 session->lu_reset_timeout = 15;
2238 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002239 session->scsi_cmds_max = scsi_cmds;
2240 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002241 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002242 session->exp_cmdsn = initial_cmdsn + 1;
2243 session->max_cmdsn = initial_cmdsn + 1;
2244 session->max_r2t = 1;
2245 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002246 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002247 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002248
2249 /* initialize SCSI PDU commands pool */
2250 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2251 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002252 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002253 goto cmdpool_alloc_fail;
2254
2255 /* pre-format cmds pool with ITT */
2256 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002257 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002258
Mike Christie9c19a7d2008-05-21 15:54:09 -05002259 if (cmd_task_size)
2260 task->dd_data = &task[1];
2261 task->itt = cmd_i;
2262 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002263 }
2264
Mike Christief53a88d2006-06-28 12:00:27 -05002265 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002266 goto module_get_fail;
2267
Mike Christie79706342008-05-21 15:54:12 -05002268 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002269 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002270
Mike Christie7996a772006-04-06 21:13:41 -05002271 return cls_session;
2272
2273cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002274 module_put(iscsit->owner);
2275module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002276 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002277cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002278 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002279dec_session_count:
2280 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002281 return NULL;
2282}
2283EXPORT_SYMBOL_GPL(iscsi_session_setup);
2284
2285/**
2286 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002287 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002288 *
Mike Christie75613522008-05-21 15:53:59 -05002289 * The driver must have called iscsi_remove_session before
2290 * calling this.
2291 */
Mike Christie7996a772006-04-06 21:13:41 -05002292void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2293{
Mike Christie75613522008-05-21 15:53:59 -05002294 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002295 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002296 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002297
Olaf Kirch63203772007-12-13 12:43:25 -06002298 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002299
Mike Christieb2c64162007-05-30 12:57:16 -05002300 kfree(session->password);
2301 kfree(session->password_in);
2302 kfree(session->username);
2303 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002304 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002305 kfree(session->initiatorname);
2306 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002307
Mike Christie75613522008-05-21 15:53:59 -05002308 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002309 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002310 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002311}
2312EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2313
2314/**
2315 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2316 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002317 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002318 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002319 */
Mike Christie7996a772006-04-06 21:13:41 -05002320struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002321iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2322 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002323{
Mike Christie75613522008-05-21 15:53:59 -05002324 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002325 struct iscsi_conn *conn;
2326 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002327 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002328
Mike Christie5d91e202008-05-21 15:54:01 -05002329 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2330 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002331 if (!cls_conn)
2332 return NULL;
2333 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002334 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002335
Mike Christie5d91e202008-05-21 15:54:01 -05002336 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002337 conn->session = session;
2338 conn->cls_conn = cls_conn;
2339 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2340 conn->id = conn_idx;
2341 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002342 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002343
2344 init_timer(&conn->transport_timer);
2345 conn->transport_timer.data = (unsigned long)conn;
2346 conn->transport_timer.function = iscsi_check_transport_timeouts;
2347
Mike Christie7996a772006-04-06 21:13:41 -05002348 INIT_LIST_HEAD(&conn->run_list);
2349 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002350 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002351 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002352 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002353 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002354
Mike Christie9c19a7d2008-05-21 15:54:09 -05002355 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002356 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002357 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002358 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002359 sizeof(void*))) {
2360 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002361 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002362 }
2363 spin_unlock_bh(&session->lock);
2364
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002365 data = (char *) __get_free_pages(GFP_KERNEL,
2366 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002367 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002368 goto login_task_data_alloc_fail;
2369 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002370
Mike Christie843c0a82007-12-13 12:43:20 -06002371 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002372 init_waitqueue_head(&conn->ehwait);
2373
2374 return cls_conn;
2375
Mike Christie9c19a7d2008-05-21 15:54:09 -05002376login_task_data_alloc_fail:
2377 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002378 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002379login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002380 iscsi_destroy_conn(cls_conn);
2381 return NULL;
2382}
2383EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2384
2385/**
2386 * iscsi_conn_teardown - teardown iscsi connection
2387 * cls_conn: iscsi class connection
2388 *
2389 * TODO: we may need to make this into a two step process
2390 * like scsi-mls remove + put host
2391 */
2392void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2393{
2394 struct iscsi_conn *conn = cls_conn->dd_data;
2395 struct iscsi_session *session = conn->session;
2396 unsigned long flags;
2397
Mike Christief6d51802007-12-13 12:43:30 -06002398 del_timer_sync(&conn->transport_timer);
2399
Mike Christie7996a772006-04-06 21:13:41 -05002400 spin_lock_bh(&session->lock);
2401 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2402 if (session->leadconn == conn) {
2403 /*
2404 * leading connection? then give up on recovery.
2405 */
2406 session->state = ISCSI_STATE_TERMINATE;
2407 wake_up(&conn->ehwait);
2408 }
2409 spin_unlock_bh(&session->lock);
2410
Mike Christie7996a772006-04-06 21:13:41 -05002411 /*
2412 * Block until all in-progress commands for this connection
2413 * time out or fail.
2414 */
2415 for (;;) {
2416 spin_lock_irqsave(session->host->host_lock, flags);
2417 if (!session->host->host_busy) { /* OK for ERL == 0 */
2418 spin_unlock_irqrestore(session->host->host_lock, flags);
2419 break;
2420 }
2421 spin_unlock_irqrestore(session->host->host_lock, flags);
2422 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002423 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2424 "host_busy %d host_failed %d\n",
2425 session->host->host_busy,
2426 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002427 /*
2428 * force eh_abort() to unblock
2429 */
2430 wake_up(&conn->ehwait);
2431 }
2432
Mike Christie779ea122007-02-28 17:32:15 -06002433 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002434 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002435
Mike Christie7996a772006-04-06 21:13:41 -05002436 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002437 free_pages((unsigned long) conn->data,
2438 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002439 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002440 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002441 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002442 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002443 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002444 spin_unlock_bh(&session->lock);
2445
Mike Christie7996a772006-04-06 21:13:41 -05002446 iscsi_destroy_conn(cls_conn);
2447}
2448EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2449
2450int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2451{
2452 struct iscsi_conn *conn = cls_conn->dd_data;
2453 struct iscsi_session *session = conn->session;
2454
Mike Christieffd04362006-08-31 18:09:24 -04002455 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002456 iscsi_conn_printk(KERN_ERR, conn,
2457 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002458 return -EPERM;
2459 }
2460
Mike Christiedb98ccd2006-08-31 18:09:31 -04002461 if ((session->imm_data_en || !session->initial_r2t_en) &&
2462 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002463 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2464 "first_burst %d max_burst %d\n",
2465 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002466 return -EINVAL;
2467 }
2468
Mike Christief6d51802007-12-13 12:43:30 -06002469 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002470 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2471 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002472 conn->recv_timeout = 5;
2473 }
2474
2475 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002476 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2477 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002478 conn->ping_timeout = 5;
2479 }
2480
Mike Christie7996a772006-04-06 21:13:41 -05002481 spin_lock_bh(&session->lock);
2482 conn->c_stage = ISCSI_CONN_STARTED;
2483 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002484 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002485
Mike Christief6d51802007-12-13 12:43:30 -06002486 conn->last_recv = jiffies;
2487 conn->last_ping = jiffies;
2488 if (conn->recv_timeout && conn->ping_timeout)
2489 mod_timer(&conn->transport_timer,
2490 jiffies + (conn->recv_timeout * HZ));
2491
Mike Christie7996a772006-04-06 21:13:41 -05002492 switch(conn->stop_stage) {
2493 case STOP_CONN_RECOVER:
2494 /*
2495 * unblock eh_abort() if it is blocked. re-try all
2496 * commands after successful recovery
2497 */
Mike Christie7996a772006-04-06 21:13:41 -05002498 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002499 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002500 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002501 if (session->age == 16)
2502 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002503 break;
Mike Christie7996a772006-04-06 21:13:41 -05002504 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002505 conn->stop_stage = 0;
2506 break;
Mike Christie7996a772006-04-06 21:13:41 -05002507 default:
2508 break;
2509 }
2510 spin_unlock_bh(&session->lock);
2511
Mike Christie75613522008-05-21 15:53:59 -05002512 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002513 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002514 return 0;
2515}
2516EXPORT_SYMBOL_GPL(iscsi_conn_start);
2517
2518static void
2519flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2520{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002521 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002522
2523 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002524 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002525 ISCSI_DBG_SESSION(session, "flushing pending mgmt task "
2526 "itt 0x%x\n", task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002527 /* release ref from prep task */
2528 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002529 }
2530
2531 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002532 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002533 ISCSI_DBG_SESSION(session, "flushing running mgmt task "
2534 "itt 0x%x\n", task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002535 /* release ref from prep task */
2536 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002537 }
2538
Mike Christie9c19a7d2008-05-21 15:54:09 -05002539 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002540}
2541
Mike Christie656cffc2006-05-18 20:31:42 -05002542static void iscsi_start_session_recovery(struct iscsi_session *session,
2543 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002544{
Mike Christieed2abc72006-05-02 19:46:40 -05002545 int old_stop_stage;
2546
Mike Christief6d51802007-12-13 12:43:30 -06002547 del_timer_sync(&conn->transport_timer);
2548
Mike Christie6724add2007-08-15 01:38:30 -05002549 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002550 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002551 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002552 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002553 mutex_unlock(&session->eh_mutex);
2554 return;
2555 }
2556
2557 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002558 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002559 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002560 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002561 */
Mike Christie67a61112006-05-30 00:37:20 -05002562 if (flag == STOP_CONN_TERM)
2563 session->state = ISCSI_STATE_TERMINATE;
2564 else if (conn->stop_stage != STOP_CONN_RECOVER)
2565 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002566
2567 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002568 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002569 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002570 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002571
2572 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002573 /*
2574 * for connection level recovery we should not calculate
2575 * header digest. conn->hdr_size used for optimization
2576 * in hdr_extract() and will be re-negotiated at
2577 * set_param() time.
2578 */
2579 if (flag == STOP_CONN_RECOVER) {
2580 conn->hdrdgst_en = 0;
2581 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002582 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002583 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002584 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002585 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002586 }
Mike Christie7996a772006-04-06 21:13:41 -05002587 }
Mike Christie656cffc2006-05-18 20:31:42 -05002588
Mike Christie656cffc2006-05-18 20:31:42 -05002589 /*
2590 * flush queues.
2591 */
2592 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002593 if (flag == STOP_CONN_RECOVER)
Mike Christie56d7fcf2008-08-19 18:45:26 -05002594 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2595 else
2596 fail_all_commands(conn, -1, DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002597 flush_control_queues(session, conn);
2598 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002599 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002600}
Mike Christie7996a772006-04-06 21:13:41 -05002601
2602void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2603{
2604 struct iscsi_conn *conn = cls_conn->dd_data;
2605 struct iscsi_session *session = conn->session;
2606
2607 switch (flag) {
2608 case STOP_CONN_RECOVER:
2609 case STOP_CONN_TERM:
2610 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002611 break;
Mike Christie7996a772006-04-06 21:13:41 -05002612 default:
Mike Christie322d7392008-01-31 13:36:52 -06002613 iscsi_conn_printk(KERN_ERR, conn,
2614 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002615 }
2616}
2617EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2618
2619int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2620 struct iscsi_cls_conn *cls_conn, int is_leading)
2621{
Mike Christie75613522008-05-21 15:53:59 -05002622 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002623 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002624
Mike Christie7996a772006-04-06 21:13:41 -05002625 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002626 if (is_leading)
2627 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002628 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002629
2630 /*
2631 * Unblock xmitworker(), Login Phase will pass through.
2632 */
2633 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2634 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2635 return 0;
2636}
2637EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2638
Mike Christiea54a52c2006-06-28 12:00:23 -05002639
2640int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2641 enum iscsi_param param, char *buf, int buflen)
2642{
2643 struct iscsi_conn *conn = cls_conn->dd_data;
2644 struct iscsi_session *session = conn->session;
2645 uint32_t value;
2646
2647 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002648 case ISCSI_PARAM_FAST_ABORT:
2649 sscanf(buf, "%d", &session->fast_abort);
2650 break;
Mike Christief6d51802007-12-13 12:43:30 -06002651 case ISCSI_PARAM_ABORT_TMO:
2652 sscanf(buf, "%d", &session->abort_timeout);
2653 break;
2654 case ISCSI_PARAM_LU_RESET_TMO:
2655 sscanf(buf, "%d", &session->lu_reset_timeout);
2656 break;
2657 case ISCSI_PARAM_PING_TMO:
2658 sscanf(buf, "%d", &conn->ping_timeout);
2659 break;
2660 case ISCSI_PARAM_RECV_TMO:
2661 sscanf(buf, "%d", &conn->recv_timeout);
2662 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002663 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2664 sscanf(buf, "%d", &conn->max_recv_dlength);
2665 break;
2666 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2667 sscanf(buf, "%d", &conn->max_xmit_dlength);
2668 break;
2669 case ISCSI_PARAM_HDRDGST_EN:
2670 sscanf(buf, "%d", &conn->hdrdgst_en);
2671 break;
2672 case ISCSI_PARAM_DATADGST_EN:
2673 sscanf(buf, "%d", &conn->datadgst_en);
2674 break;
2675 case ISCSI_PARAM_INITIAL_R2T_EN:
2676 sscanf(buf, "%d", &session->initial_r2t_en);
2677 break;
2678 case ISCSI_PARAM_MAX_R2T:
2679 sscanf(buf, "%d", &session->max_r2t);
2680 break;
2681 case ISCSI_PARAM_IMM_DATA_EN:
2682 sscanf(buf, "%d", &session->imm_data_en);
2683 break;
2684 case ISCSI_PARAM_FIRST_BURST:
2685 sscanf(buf, "%d", &session->first_burst);
2686 break;
2687 case ISCSI_PARAM_MAX_BURST:
2688 sscanf(buf, "%d", &session->max_burst);
2689 break;
2690 case ISCSI_PARAM_PDU_INORDER_EN:
2691 sscanf(buf, "%d", &session->pdu_inorder_en);
2692 break;
2693 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2694 sscanf(buf, "%d", &session->dataseq_inorder_en);
2695 break;
2696 case ISCSI_PARAM_ERL:
2697 sscanf(buf, "%d", &session->erl);
2698 break;
2699 case ISCSI_PARAM_IFMARKER_EN:
2700 sscanf(buf, "%d", &value);
2701 BUG_ON(value);
2702 break;
2703 case ISCSI_PARAM_OFMARKER_EN:
2704 sscanf(buf, "%d", &value);
2705 BUG_ON(value);
2706 break;
2707 case ISCSI_PARAM_EXP_STATSN:
2708 sscanf(buf, "%u", &conn->exp_statsn);
2709 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002710 case ISCSI_PARAM_USERNAME:
2711 kfree(session->username);
2712 session->username = kstrdup(buf, GFP_KERNEL);
2713 if (!session->username)
2714 return -ENOMEM;
2715 break;
2716 case ISCSI_PARAM_USERNAME_IN:
2717 kfree(session->username_in);
2718 session->username_in = kstrdup(buf, GFP_KERNEL);
2719 if (!session->username_in)
2720 return -ENOMEM;
2721 break;
2722 case ISCSI_PARAM_PASSWORD:
2723 kfree(session->password);
2724 session->password = kstrdup(buf, GFP_KERNEL);
2725 if (!session->password)
2726 return -ENOMEM;
2727 break;
2728 case ISCSI_PARAM_PASSWORD_IN:
2729 kfree(session->password_in);
2730 session->password_in = kstrdup(buf, GFP_KERNEL);
2731 if (!session->password_in)
2732 return -ENOMEM;
2733 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002734 case ISCSI_PARAM_TARGET_NAME:
2735 /* this should not change between logins */
2736 if (session->targetname)
2737 break;
2738
2739 session->targetname = kstrdup(buf, GFP_KERNEL);
2740 if (!session->targetname)
2741 return -ENOMEM;
2742 break;
2743 case ISCSI_PARAM_TPGT:
2744 sscanf(buf, "%d", &session->tpgt);
2745 break;
2746 case ISCSI_PARAM_PERSISTENT_PORT:
2747 sscanf(buf, "%d", &conn->persistent_port);
2748 break;
2749 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2750 /*
2751 * this is the address returned in discovery so it should
2752 * not change between logins.
2753 */
2754 if (conn->persistent_address)
2755 break;
2756
2757 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2758 if (!conn->persistent_address)
2759 return -ENOMEM;
2760 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002761 case ISCSI_PARAM_IFACE_NAME:
2762 if (!session->ifacename)
2763 session->ifacename = kstrdup(buf, GFP_KERNEL);
2764 break;
2765 case ISCSI_PARAM_INITIATOR_NAME:
2766 if (!session->initiatorname)
2767 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2768 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002769 default:
2770 return -ENOSYS;
2771 }
2772
2773 return 0;
2774}
2775EXPORT_SYMBOL_GPL(iscsi_set_param);
2776
2777int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2778 enum iscsi_param param, char *buf)
2779{
Mike Christie75613522008-05-21 15:53:59 -05002780 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002781 int len;
2782
2783 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002784 case ISCSI_PARAM_FAST_ABORT:
2785 len = sprintf(buf, "%d\n", session->fast_abort);
2786 break;
Mike Christief6d51802007-12-13 12:43:30 -06002787 case ISCSI_PARAM_ABORT_TMO:
2788 len = sprintf(buf, "%d\n", session->abort_timeout);
2789 break;
2790 case ISCSI_PARAM_LU_RESET_TMO:
2791 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2792 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002793 case ISCSI_PARAM_INITIAL_R2T_EN:
2794 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2795 break;
2796 case ISCSI_PARAM_MAX_R2T:
2797 len = sprintf(buf, "%hu\n", session->max_r2t);
2798 break;
2799 case ISCSI_PARAM_IMM_DATA_EN:
2800 len = sprintf(buf, "%d\n", session->imm_data_en);
2801 break;
2802 case ISCSI_PARAM_FIRST_BURST:
2803 len = sprintf(buf, "%u\n", session->first_burst);
2804 break;
2805 case ISCSI_PARAM_MAX_BURST:
2806 len = sprintf(buf, "%u\n", session->max_burst);
2807 break;
2808 case ISCSI_PARAM_PDU_INORDER_EN:
2809 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2810 break;
2811 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2812 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2813 break;
2814 case ISCSI_PARAM_ERL:
2815 len = sprintf(buf, "%d\n", session->erl);
2816 break;
2817 case ISCSI_PARAM_TARGET_NAME:
2818 len = sprintf(buf, "%s\n", session->targetname);
2819 break;
2820 case ISCSI_PARAM_TPGT:
2821 len = sprintf(buf, "%d\n", session->tpgt);
2822 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002823 case ISCSI_PARAM_USERNAME:
2824 len = sprintf(buf, "%s\n", session->username);
2825 break;
2826 case ISCSI_PARAM_USERNAME_IN:
2827 len = sprintf(buf, "%s\n", session->username_in);
2828 break;
2829 case ISCSI_PARAM_PASSWORD:
2830 len = sprintf(buf, "%s\n", session->password);
2831 break;
2832 case ISCSI_PARAM_PASSWORD_IN:
2833 len = sprintf(buf, "%s\n", session->password_in);
2834 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002835 case ISCSI_PARAM_IFACE_NAME:
2836 len = sprintf(buf, "%s\n", session->ifacename);
2837 break;
2838 case ISCSI_PARAM_INITIATOR_NAME:
2839 if (!session->initiatorname)
2840 len = sprintf(buf, "%s\n", "unknown");
2841 else
2842 len = sprintf(buf, "%s\n", session->initiatorname);
2843 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002844 default:
2845 return -ENOSYS;
2846 }
2847
2848 return len;
2849}
2850EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2851
2852int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2853 enum iscsi_param param, char *buf)
2854{
2855 struct iscsi_conn *conn = cls_conn->dd_data;
2856 int len;
2857
2858 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002859 case ISCSI_PARAM_PING_TMO:
2860 len = sprintf(buf, "%u\n", conn->ping_timeout);
2861 break;
2862 case ISCSI_PARAM_RECV_TMO:
2863 len = sprintf(buf, "%u\n", conn->recv_timeout);
2864 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002865 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2866 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2867 break;
2868 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2869 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2870 break;
2871 case ISCSI_PARAM_HDRDGST_EN:
2872 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2873 break;
2874 case ISCSI_PARAM_DATADGST_EN:
2875 len = sprintf(buf, "%d\n", conn->datadgst_en);
2876 break;
2877 case ISCSI_PARAM_IFMARKER_EN:
2878 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2879 break;
2880 case ISCSI_PARAM_OFMARKER_EN:
2881 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2882 break;
2883 case ISCSI_PARAM_EXP_STATSN:
2884 len = sprintf(buf, "%u\n", conn->exp_statsn);
2885 break;
2886 case ISCSI_PARAM_PERSISTENT_PORT:
2887 len = sprintf(buf, "%d\n", conn->persistent_port);
2888 break;
2889 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2890 len = sprintf(buf, "%s\n", conn->persistent_address);
2891 break;
2892 default:
2893 return -ENOSYS;
2894 }
2895
2896 return len;
2897}
2898EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2899
Mike Christie0801c242007-05-30 12:57:12 -05002900int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2901 char *buf)
2902{
Mike Christie75613522008-05-21 15:53:59 -05002903 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002904 int len;
2905
2906 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002907 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002908 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002909 len = sprintf(buf, "%s\n", "default");
2910 else
Mike Christie75613522008-05-21 15:53:59 -05002911 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002912 break;
Mike Christie0801c242007-05-30 12:57:12 -05002913 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002914 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002915 len = sprintf(buf, "%s\n", "default");
2916 else
Mike Christie75613522008-05-21 15:53:59 -05002917 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002918 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002919 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002920 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002921 len = sprintf(buf, "%s\n", "unknown");
2922 else
Mike Christie75613522008-05-21 15:53:59 -05002923 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002924 break;
Mike Christie75613522008-05-21 15:53:59 -05002925 case ISCSI_HOST_PARAM_IPADDRESS:
2926 if (!strlen(ihost->local_address))
2927 len = sprintf(buf, "%s\n", "unknown");
2928 else
2929 len = sprintf(buf, "%s\n",
2930 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002931 break;
Mike Christie0801c242007-05-30 12:57:12 -05002932 default:
2933 return -ENOSYS;
2934 }
2935
2936 return len;
2937}
2938EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2939
2940int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2941 char *buf, int buflen)
2942{
Mike Christie75613522008-05-21 15:53:59 -05002943 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002944
2945 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002946 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002947 if (!ihost->netdev)
2948 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002949 break;
Mike Christie0801c242007-05-30 12:57:12 -05002950 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002951 if (!ihost->hwaddress)
2952 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002953 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002954 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002955 if (!ihost->initiatorname)
2956 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002957 break;
Mike Christie0801c242007-05-30 12:57:12 -05002958 default:
2959 return -ENOSYS;
2960 }
2961
2962 return 0;
2963}
2964EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2965
Mike Christie7996a772006-04-06 21:13:41 -05002966MODULE_AUTHOR("Mike Christie");
2967MODULE_DESCRIPTION("iSCSI library functions");
2968MODULE_LICENSE("GPL");