blob: 701457cca08afc0dad0245d62079438c56fb30f9 [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
79void
80iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050081{
82 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
83 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
84
Mike Christie77a23c22007-05-30 12:57:18 -050085 /*
86 * standard specifies this check for when to update expected and
87 * max sequence numbers
88 */
89 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
90 return;
91
92 if (exp_cmdsn != session->exp_cmdsn &&
93 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050094 session->exp_cmdsn = exp_cmdsn;
95
Mike Christie77a23c22007-05-30 12:57:18 -050096 if (max_cmdsn != session->max_cmdsn &&
97 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
98 session->max_cmdsn = max_cmdsn;
99 /*
100 * if the window closed with IO queued, then kick the
101 * xmit thread
102 */
103 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie052d0142008-05-21 15:54:05 -0500104 !list_empty(&session->leadconn->mgmtqueue)) {
105 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
106 scsi_queue_work(session->host,
107 &session->leadconn->xmitwork);
108 }
Mike Christie77a23c22007-05-30 12:57:18 -0500109 }
Mike Christie7996a772006-04-06 21:13:41 -0500110}
Mike Christie77a23c22007-05-30 12:57:18 -0500111EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500112
Mike Christie577577d2008-12-02 00:32:05 -0600113/**
114 * iscsi_prep_data_out_pdu - initialize Data-Out
115 * @task: scsi command task
116 * @r2t: R2T info
117 * @hdr: iscsi data in pdu
118 *
119 * Notes:
120 * Initialize Data-Out within this R2T sequence and finds
121 * proper data_offset within this SCSI command.
122 *
123 * This function is called with connection lock taken.
124 **/
125void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
126 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500127{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500128 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600129 unsigned int left = r2t->data_length - r2t->sent;
130
131 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500132
133 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600134 hdr->ttt = r2t->ttt;
135 hdr->datasn = cpu_to_be32(r2t->datasn);
136 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500137 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600138 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
139 hdr->itt = task->hdr_itt;
140 hdr->exp_statsn = r2t->exp_statsn;
141 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
142 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500143 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600144 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500145 hdr->flags = 0;
146 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600147 hton24(hdr->dlength, left);
148 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500149 hdr->flags = ISCSI_FLAG_CMD_FINAL;
150 }
Mike Christie577577d2008-12-02 00:32:05 -0600151 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500152}
Mike Christie577577d2008-12-02 00:32:05 -0600153EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500154
Mike Christie9c19a7d2008-05-21 15:54:09 -0500155static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600156{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500157 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600158
Mike Christie9c19a7d2008-05-21 15:54:09 -0500159 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600160 WARN_ON(1);
161 return -EINVAL;
162 }
163
164 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500165 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600166 return 0;
167}
168
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500169/*
170 * make an extended cdb AHS
171 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500172static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500173{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500174 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500175 unsigned rlen, pad_len;
176 unsigned short ahslength;
177 struct iscsi_ecdb_ahdr *ecdb_ahdr;
178 int rc;
179
Mike Christie9c19a7d2008-05-21 15:54:09 -0500180 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500181 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
182
183 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
184 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
185
186 pad_len = iscsi_padding(rlen);
187
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500189 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
190 if (rc)
191 return rc;
192
193 if (pad_len)
194 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
195
196 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
197 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
198 ecdb_ahdr->reserved = 0;
199 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
200
Mike Christie1b2c7af2009-03-05 14:45:58 -0600201 ISCSI_DBG_SESSION(task->conn->session,
202 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
203 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
204 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
205 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500206 return 0;
207}
208
Mike Christie9c19a7d2008-05-21 15:54:09 -0500209static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500210{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500211 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500212 struct iscsi_rlength_ahdr *rlen_ahdr;
213 int rc;
214
Mike Christie9c19a7d2008-05-21 15:54:09 -0500215 rlen_ahdr = iscsi_next_hdr(task);
216 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500217 if (rc)
218 return rc;
219
220 rlen_ahdr->ahslength =
221 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
222 sizeof(rlen_ahdr->reserved));
223 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
224 rlen_ahdr->reserved = 0;
225 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
226
Mike Christie1b2c7af2009-03-05 14:45:58 -0600227 ISCSI_DBG_SESSION(task->conn->session,
228 "bidi-in rlen_ahdr->read_length(%d) "
229 "rlen_ahdr->ahslength(%d)\n",
230 be32_to_cpu(rlen_ahdr->read_length),
231 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500232 return 0;
233}
234
Mike Christie7996a772006-04-06 21:13:41 -0500235/**
236 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500237 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500238 *
239 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
240 * fields like dlength or final based on how much data it sends
241 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500242static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500243{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500245 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500246 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600247 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500248 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600249 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600250 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500251
Mike Christie2ff79d52008-12-02 00:32:14 -0600252 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
Mike Christie577577d2008-12-02 00:32:05 -0600253 if (rc)
254 return rc;
255 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600256 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600257 memset(hdr, 0, sizeof(*hdr));
258
Mike Christie2ff79d52008-12-02 00:32:14 -0600259 if (session->tt->parse_pdu_itt)
260 hdr->itt = task->hdr_itt = itt;
261 else
262 hdr->itt = task->hdr_itt = build_itt(task->itt,
263 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500264 task->hdr_len = 0;
265 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600266 if (rc)
267 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600268 hdr->opcode = ISCSI_OP_SCSI_CMD;
269 hdr->flags = ISCSI_ATTR_SIMPLE;
270 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600271 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Mike Christie577577d2008-12-02 00:32:05 -0600272 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600273 session->cmdsn++;
274 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500275 cmd_len = sc->cmd_len;
276 if (cmd_len < ISCSI_CDB_SIZE)
277 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
278 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500279 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500280 if (rc)
281 return rc;
282 cmd_len = ISCSI_CDB_SIZE;
283 }
284 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500285
Mike Christie9c19a7d2008-05-21 15:54:09 -0500286 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500287 if (scsi_bidi_cmnd(sc)) {
288 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500289 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500290 if (rc)
291 return rc;
292 }
Mike Christie7996a772006-04-06 21:13:41 -0500293 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500294 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600295 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
296
Boaz Harroshc07d4442008-04-18 10:11:52 -0500297 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500298 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
299 /*
300 * Write counters:
301 *
302 * imm_count bytes to be sent right after
303 * SCSI PDU Header
304 *
305 * unsol_count bytes(as Data-Out) to be sent
306 * without R2T ack right after
307 * immediate data
308 *
Mike Christie577577d2008-12-02 00:32:05 -0600309 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500310 *
311 * pad_count bytes to be sent as zero-padding
312 */
Mike Christie577577d2008-12-02 00:32:05 -0600313 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500314
315 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500316 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500317 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500318 conn->max_xmit_dlength);
319 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500320 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500321 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500322 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500323 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600324 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500325
Mike Christieffd04362006-08-31 18:09:24 -0400326 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600327 r2t->data_length = min(session->first_burst, out_len) -
328 task->imm_count;
329 r2t->data_offset = task->imm_count;
330 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
331 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400332 }
333
Mike Christie577577d2008-12-02 00:32:05 -0600334 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500335 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600336 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500337 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500338 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
339 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500340 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500341
342 if (sc->sc_data_direction == DMA_FROM_DEVICE)
343 hdr->flags |= ISCSI_FLAG_CMD_READ;
344 }
345
Boaz Harrosh004d6532007-12-13 12:43:23 -0600346 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500347 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600348
349 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
350 hdrlength /= ISCSI_PAD_LEN;
351
352 WARN_ON(hdrlength >= 256);
353 hdr->hlength = hdrlength & 0xFF;
354
Mike Christie577577d2008-12-02 00:32:05 -0600355 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500356 return -EIO;
357
Mike Christie9c19a7d2008-05-21 15:54:09 -0500358 task->state = ISCSI_TASK_RUNNING;
359 list_move_tail(&task->running, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500360
Olaf Kircha8ac6312007-12-13 12:43:35 -0600361 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600362 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
363 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
364 scsi_bidi_cmnd(sc) ? "bidirectional" :
365 sc->sc_data_direction == DMA_TO_DEVICE ?
366 "write" : "read", conn->id, sc, sc->cmnd[0],
367 task->itt, scsi_bufflen(sc),
368 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
369 session->cmdsn,
370 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600371 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500372}
Mike Christie7996a772006-04-06 21:13:41 -0500373
374/**
Mike Christie3e5c28a2008-05-21 15:54:06 -0500375 * iscsi_complete_command - finish a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500376 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500377 *
378 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500379 * This function returns the scsi command to scsi-ml or cleans
380 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500381 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500382static void iscsi_complete_command(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500383{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500384 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600385 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500386 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500387
Mike Christie577577d2008-12-02 00:32:05 -0600388 session->tt->cleanup_task(task);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500389 list_del_init(&task->running);
390 task->state = ISCSI_TASK_COMPLETED;
391 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500392
Mike Christie9c19a7d2008-05-21 15:54:09 -0500393 if (conn->task == task)
394 conn->task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500395 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500396 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500397 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500398 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500399 return;
400
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500402
Mike Christie9c19a7d2008-05-21 15:54:09 -0500403 if (conn->ping_task == task)
404 conn->ping_task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500405
406 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500407 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500408 /* SCSI eh reuses commands to verify us */
409 sc->SCp.ptr = NULL;
410 /*
411 * queue command may call this to free the task, but
412 * not have setup the sc callback
413 */
414 if (sc->scsi_done)
415 sc->scsi_done(sc);
416 }
Mike Christie7996a772006-04-06 21:13:41 -0500417}
418
Mike Christie913e5bf2008-05-21 15:54:18 -0500419void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400420{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500421 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400422}
Mike Christie913e5bf2008-05-21 15:54:18 -0500423EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400424
Mike Christie9c19a7d2008-05-21 15:54:09 -0500425static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400426{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500427 if (atomic_dec_and_test(&task->refcount))
428 iscsi_complete_command(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400429}
430
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500432{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500433 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500434
435 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500436 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500437 spin_unlock_bh(&session->lock);
438}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500439EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500440
Mike Christieb3a7ea82007-12-13 12:43:26 -0600441/*
442 * session lock must be held
443 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500444static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600445 int err)
446{
447 struct scsi_cmnd *sc;
448
Mike Christie9c19a7d2008-05-21 15:54:09 -0500449 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600450 if (!sc)
451 return;
452
Mike Christie9c19a7d2008-05-21 15:54:09 -0500453 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600454 /*
455 * cmd never made it to the xmit thread, so we should not count
456 * the cmd in the sequencing
457 */
458 conn->session->queued_cmdsn--;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600459
460 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500461 if (!scsi_bidi_cmnd(sc))
462 scsi_set_resid(sc, scsi_bufflen(sc));
463 else {
464 scsi_out(sc)->resid = scsi_out(sc)->length;
465 scsi_in(sc)->resid = scsi_in(sc)->length;
466 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500467
Mike Christie9c19a7d2008-05-21 15:54:09 -0500468 if (conn->task == task)
469 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600470 /* release ref from queuecommand */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500471 __iscsi_put_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600472}
473
Mike Christie3e5c28a2008-05-21 15:54:06 -0500474static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500475 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500476{
477 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600478 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500479 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
480
481 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
482 return -ENOTCONN;
483
484 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
485 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
486 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
487 /*
488 * pre-format CmdSN for outgoing PDU.
489 */
490 nop->cmdsn = cpu_to_be32(session->cmdsn);
491 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500492 /*
493 * TODO: We always use immediate, so we never hit this.
494 * If we start to send tmfs or nops as non-immediate then
495 * we should start checking the cmdsn numbers for mgmt tasks.
496 */
497 if (conn->c_stage == ISCSI_CONN_STARTED &&
498 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
499 session->queued_cmdsn++;
500 session->cmdsn++;
501 }
502 }
503
Mike Christieae15f802008-12-02 00:32:15 -0600504 if (session->tt->init_task && session->tt->init_task(task))
505 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500506
507 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
508 session->state = ISCSI_STATE_LOGGING_OUT;
509
Mike Christie577577d2008-12-02 00:32:05 -0600510 task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500511 list_move_tail(&task->running, &conn->mgmt_run_list);
Mike Christie1b2c7af2009-03-05 14:45:58 -0600512 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
513 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
514 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500515 return 0;
516}
517
Mike Christie9c19a7d2008-05-21 15:54:09 -0500518static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600519__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
520 char *data, uint32_t data_size)
521{
522 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500523 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600524 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600525
526 if (session->state == ISCSI_STATE_TERMINATE)
527 return NULL;
528
529 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
530 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
531 /*
532 * Login and Text are sent serially, in
533 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500534 * Same task can be used. Same ITT must be used.
535 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600536 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500537 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600538 else {
539 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
540 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
541
Mike Christie3e5c28a2008-05-21 15:54:06 -0500542 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500543 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600544 return NULL;
545 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500546 /*
547 * released in complete pdu for task we expect a response for, and
548 * released by the lld when it has transmitted the task for
549 * pdus we do not expect a response for.
550 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500551 atomic_set(&task->refcount, 1);
552 task->conn = conn;
553 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600554
555 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500556 memcpy(task->data, data, data_size);
557 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600558 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500559 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600560
Mike Christie2ff79d52008-12-02 00:32:14 -0600561 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
Mike Christie577577d2008-12-02 00:32:05 -0600562 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
563 "pdu for mgmt task.\n");
564 goto requeue_task;
565 }
Mike Christie262ef632008-12-02 00:32:13 -0600566 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600567 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500568 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600569
570 if (hdr->itt != RESERVED_ITT) {
571 if (session->tt->parse_pdu_itt)
572 task->hdr->itt = itt;
573 else
574 task->hdr->itt = build_itt(task->itt,
575 task->conn->session->age);
576 }
577
Mike Christie9c19a7d2008-05-21 15:54:09 -0500578 INIT_LIST_HEAD(&task->running);
579 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500580
581 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -0600582 if (iscsi_prep_mgmt_task(conn, task))
583 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500584
Mike Christie9c19a7d2008-05-21 15:54:09 -0500585 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600586 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500587
588 } else
589 scsi_queue_work(conn->session->host, &conn->xmitwork);
590
Mike Christie9c19a7d2008-05-21 15:54:09 -0500591 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600592
593free_task:
594 __iscsi_put_task(task);
595 return NULL;
596
597requeue_task:
598 if (task != conn->login_task)
599 __kfifo_put(session->cmdpool.queue, (void*)&task,
600 sizeof(void*));
601 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600602}
603
604int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
605 char *data, uint32_t data_size)
606{
607 struct iscsi_conn *conn = cls_conn->dd_data;
608 struct iscsi_session *session = conn->session;
609 int err = 0;
610
611 spin_lock_bh(&session->lock);
612 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
613 err = -EPERM;
614 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600615 return err;
616}
617EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
618
Mike Christie7996a772006-04-06 21:13:41 -0500619/**
620 * iscsi_cmd_rsp - SCSI Command Response processing
621 * @conn: iscsi connection
622 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500623 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500624 * @data: cmd data buffer
625 * @datalen: len of buffer
626 *
627 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500628 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500629 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500630static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500631 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500632 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500633{
Mike Christie7996a772006-04-06 21:13:41 -0500634 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
635 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500636 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500637
Mike Christie77a23c22007-05-30 12:57:18 -0500638 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500639 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
640
641 sc->result = (DID_OK << 16) | rhdr->cmd_status;
642
643 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
644 sc->result = DID_ERROR << 16;
645 goto out;
646 }
647
648 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600649 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500650
651 if (datalen < 2) {
652invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600653 iscsi_conn_printk(KERN_ERR, conn,
654 "Got CHECK_CONDITION but invalid data "
655 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500656 sc->result = DID_BAD_TARGET << 16;
657 goto out;
658 }
659
Harvey Harrison8f3339912008-05-21 15:54:20 -0500660 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500661 if (datalen < senselen)
662 goto invalid_datalen;
663
664 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600665 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600666 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
667 min_t(uint16_t, senselen,
668 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500669 }
670
Boaz Harroshc07d4442008-04-18 10:11:52 -0500671 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
672 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
673 int res_count = be32_to_cpu(rhdr->bi_residual_count);
674
675 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
676 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
677 res_count <= scsi_in(sc)->length))
678 scsi_in(sc)->resid = res_count;
679 else
680 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
681 }
682
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600683 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
684 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500685 int res_count = be32_to_cpu(rhdr->residual_count);
686
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600687 if (res_count > 0 &&
688 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
689 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500690 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900691 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500692 else
693 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500694 }
Mike Christie7996a772006-04-06 21:13:41 -0500695out:
Mike Christie1b2c7af2009-03-05 14:45:58 -0600696 ISCSI_DBG_SESSION(session, "done [sc %p res %d itt 0x%x]\n",
697 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500698 conn->scsirsp_pdus_cnt++;
699
Mike Christie9c19a7d2008-05-21 15:54:09 -0500700 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500701}
702
Mike Christie1d9edf02008-09-24 11:46:09 -0500703/**
704 * iscsi_data_in_rsp - SCSI Data-In Response processing
705 * @conn: iscsi connection
706 * @hdr: iscsi pdu
707 * @task: scsi command task
708 **/
709static void
710iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
711 struct iscsi_task *task)
712{
713 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
714 struct scsi_cmnd *sc = task->sc;
715
716 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
717 return;
718
719 sc->result = (DID_OK << 16) | rhdr->cmd_status;
720 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
721 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
722 ISCSI_FLAG_DATA_OVERFLOW)) {
723 int res_count = be32_to_cpu(rhdr->residual_count);
724
725 if (res_count > 0 &&
726 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
727 res_count <= scsi_in(sc)->length))
728 scsi_in(sc)->resid = res_count;
729 else
730 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
731 }
732
733 conn->scsirsp_pdus_cnt++;
734 __iscsi_put_task(task);
735}
736
Mike Christie7ea8b822006-07-24 15:47:22 -0500737static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
738{
739 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
740
741 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
742 conn->tmfrsp_pdus_cnt++;
743
Mike Christie843c0a82007-12-13 12:43:20 -0600744 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500745 return;
746
747 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600748 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500749 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600750 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500751 else
Mike Christie843c0a82007-12-13 12:43:20 -0600752 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500753 wake_up(&conn->ehwait);
754}
755
Mike Christief6d51802007-12-13 12:43:30 -0600756static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
757{
758 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500759 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600760
Mike Christie9c19a7d2008-05-21 15:54:09 -0500761 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600762 return;
763
764 memset(&hdr, 0, sizeof(struct iscsi_nopout));
765 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
766 hdr.flags = ISCSI_FLAG_CMD_FINAL;
767
768 if (rhdr) {
769 memcpy(hdr.lun, rhdr->lun, 8);
770 hdr.ttt = rhdr->ttt;
771 hdr.itt = RESERVED_ITT;
772 } else
773 hdr.ttt = RESERVED_ITT;
774
Mike Christie9c19a7d2008-05-21 15:54:09 -0500775 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
776 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600777 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600778 else if (!rhdr) {
779 /* only track our nops */
780 conn->ping_task = task;
781 conn->last_ping = jiffies;
782 }
Mike Christief6d51802007-12-13 12:43:30 -0600783}
784
Mike Christie62f38302006-08-31 18:09:27 -0400785static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
786 char *data, int datalen)
787{
788 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
789 struct iscsi_hdr rejected_pdu;
Mike Christie62f38302006-08-31 18:09:27 -0400790
791 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
792
793 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
794 if (ntoh24(reject->dlength) > datalen)
795 return ISCSI_ERR_PROTO;
796
797 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
798 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Mike Christie322d7392008-01-31 13:36:52 -0600799 iscsi_conn_printk(KERN_ERR, conn,
Mike Christie262ef632008-12-02 00:32:13 -0600800 "pdu (op 0x%x) rejected "
801 "due to DataDigest error.\n",
Mike Christie322d7392008-01-31 13:36:52 -0600802 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400803 }
804 }
805 return 0;
806}
807
Mike Christie7996a772006-04-06 21:13:41 -0500808/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500809 * iscsi_itt_to_task - look up task by itt
810 * @conn: iscsi connection
811 * @itt: itt
812 *
813 * This should be used for mgmt tasks like login and nops, or if
814 * the LDD's itt space does not include the session age.
815 *
816 * The session lock must be held.
817 */
818static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
819{
820 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600821 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -0500822
823 if (itt == RESERVED_ITT)
824 return NULL;
825
Mike Christie262ef632008-12-02 00:32:13 -0600826 if (session->tt->parse_pdu_itt)
827 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
828 else
829 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -0500830 if (i >= session->cmds_max)
831 return NULL;
832
833 return session->cmds[i];
834}
835
836/**
Mike Christie7996a772006-04-06 21:13:41 -0500837 * __iscsi_complete_pdu - complete pdu
838 * @conn: iscsi conn
839 * @hdr: iscsi header
840 * @data: data buffer
841 * @datalen: len of data buffer
842 *
843 * Completes pdu processing by freeing any resources allocated at
844 * queuecommand or send generic. session lock must be held and verify
845 * itt must have been called.
846 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500847int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
848 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500849{
850 struct iscsi_session *session = conn->session;
851 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500852 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500853 uint32_t itt;
854
Mike Christief6d51802007-12-13 12:43:30 -0600855 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500856 rc = iscsi_verify_itt(conn, hdr->itt);
857 if (rc)
858 return rc;
859
Al Virob4377352007-02-09 16:39:40 +0000860 if (hdr->itt != RESERVED_ITT)
861 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500862 else
Al Virob4377352007-02-09 16:39:40 +0000863 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500864
Mike Christie1b2c7af2009-03-05 14:45:58 -0600865 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
866 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500867
Mike Christie3e5c28a2008-05-21 15:54:06 -0500868 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500869 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400870
Mike Christie7996a772006-04-06 21:13:41 -0500871 switch(opcode) {
872 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500873 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500874 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500875 break;
876 }
877
Al Virob4377352007-02-09 16:39:40 +0000878 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500879 break;
880
Mike Christief6d51802007-12-13 12:43:30 -0600881 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500882 break;
883 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400884 rc = iscsi_handle_reject(conn, hdr, data, datalen);
885 break;
Mike Christie7996a772006-04-06 21:13:41 -0500886 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500887 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400888 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
889 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500890 break;
891 default:
892 rc = ISCSI_ERR_BAD_OPCODE;
893 break;
894 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500895 goto out;
896 }
Mike Christie7996a772006-04-06 21:13:41 -0500897
Mike Christie3e5c28a2008-05-21 15:54:06 -0500898 switch(opcode) {
899 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500900 case ISCSI_OP_SCSI_DATA_IN:
901 task = iscsi_itt_to_ctask(conn, hdr->itt);
902 if (!task)
903 return ISCSI_ERR_BAD_ITT;
904 break;
905 case ISCSI_OP_R2T:
906 /*
907 * LLD handles R2Ts if they need to.
908 */
909 return 0;
910 case ISCSI_OP_LOGOUT_RSP:
911 case ISCSI_OP_LOGIN_RSP:
912 case ISCSI_OP_TEXT_RSP:
913 case ISCSI_OP_SCSI_TMFUNC_RSP:
914 case ISCSI_OP_NOOP_IN:
915 task = iscsi_itt_to_task(conn, hdr->itt);
916 if (!task)
917 return ISCSI_ERR_BAD_ITT;
918 break;
919 default:
920 return ISCSI_ERR_BAD_OPCODE;
921 }
922
923 switch(opcode) {
924 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500925 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500926 break;
927 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500928 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500929 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500930 case ISCSI_OP_LOGOUT_RSP:
931 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
932 if (datalen) {
933 rc = ISCSI_ERR_PROTO;
934 break;
935 }
936 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
937 goto recv_pdu;
938 case ISCSI_OP_LOGIN_RSP:
939 case ISCSI_OP_TEXT_RSP:
940 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
941 /*
942 * login related PDU's exp_statsn is handled in
943 * userspace
944 */
945 goto recv_pdu;
946 case ISCSI_OP_SCSI_TMFUNC_RSP:
947 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
948 if (datalen) {
949 rc = ISCSI_ERR_PROTO;
950 break;
951 }
952
953 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500954 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500955 break;
956 case ISCSI_OP_NOOP_IN:
957 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
958 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
959 rc = ISCSI_ERR_PROTO;
960 break;
961 }
962 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
963
Mike Christie9c19a7d2008-05-21 15:54:09 -0500964 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500965 /*
966 * If this is not in response to one of our
967 * nops then it must be from userspace.
968 */
969 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500970
971 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
972 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500973 break;
974 default:
975 rc = ISCSI_ERR_BAD_OPCODE;
976 break;
977 }
978
979out:
980 return rc;
981recv_pdu:
982 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
983 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500984 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500985 return rc;
986}
Mike Christie913e5bf2008-05-21 15:54:18 -0500987EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500988
989int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
990 char *data, int datalen)
991{
992 int rc;
993
994 spin_lock(&conn->session->lock);
995 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
996 spin_unlock(&conn->session->lock);
997 return rc;
998}
999EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1000
Mike Christie0af967f2008-05-21 15:54:04 -05001001int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001002{
1003 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001004 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001005
Mike Christie0af967f2008-05-21 15:54:04 -05001006 if (itt == RESERVED_ITT)
1007 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001008
Mike Christie262ef632008-12-02 00:32:13 -06001009 if (session->tt->parse_pdu_itt)
1010 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1011 else {
1012 i = get_itt(itt);
1013 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1014 }
1015
1016 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001017 iscsi_conn_printk(KERN_ERR, conn,
1018 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001019 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001020 return ISCSI_ERR_BAD_ITT;
1021 }
Mike Christie7996a772006-04-06 21:13:41 -05001022
Mike Christie3e5c28a2008-05-21 15:54:06 -05001023 if (i >= session->cmds_max) {
1024 iscsi_conn_printk(KERN_ERR, conn,
1025 "received invalid itt index %u (max cmds "
1026 "%u.\n", i, session->cmds_max);
1027 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001028 }
Mike Christie7996a772006-04-06 21:13:41 -05001029 return 0;
1030}
1031EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1032
Mike Christie913e5bf2008-05-21 15:54:18 -05001033/**
1034 * iscsi_itt_to_ctask - look up ctask by itt
1035 * @conn: iscsi connection
1036 * @itt: itt
1037 *
1038 * This should be used for cmd tasks.
1039 *
1040 * The session lock must be held.
1041 */
1042struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001043{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001044 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001045
1046 if (iscsi_verify_itt(conn, itt))
1047 return NULL;
1048
Mike Christie913e5bf2008-05-21 15:54:18 -05001049 task = iscsi_itt_to_task(conn, itt);
1050 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001051 return NULL;
1052
Mike Christie913e5bf2008-05-21 15:54:18 -05001053 if (task->sc->SCp.phase != conn->session->age) {
1054 iscsi_session_printk(KERN_ERR, conn->session,
1055 "task's session age %d, expected %d\n",
1056 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001057 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001058 }
Mike Christie0af967f2008-05-21 15:54:04 -05001059
Mike Christie9c19a7d2008-05-21 15:54:09 -05001060 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001061}
1062EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1063
Mike Christiee5bd7b542008-09-24 11:46:10 -05001064void iscsi_session_failure(struct iscsi_cls_session *cls_session,
1065 enum iscsi_err err)
1066{
1067 struct iscsi_session *session = cls_session->dd_data;
1068 struct iscsi_conn *conn;
1069 struct device *dev;
1070 unsigned long flags;
1071
1072 spin_lock_irqsave(&session->lock, flags);
1073 conn = session->leadconn;
1074 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1075 spin_unlock_irqrestore(&session->lock, flags);
1076 return;
1077 }
1078
1079 dev = get_device(&conn->cls_conn->dev);
1080 spin_unlock_irqrestore(&session->lock, flags);
1081 if (!dev)
1082 return;
1083 /*
1084 * if the host is being removed bypass the connection
1085 * recovery initialization because we are going to kill
1086 * the session.
1087 */
1088 if (err == ISCSI_ERR_INVALID_HOST)
1089 iscsi_conn_error_event(conn->cls_conn, err);
1090 else
1091 iscsi_conn_failure(conn, err);
1092 put_device(dev);
1093}
1094EXPORT_SYMBOL_GPL(iscsi_session_failure);
1095
Mike Christie7996a772006-04-06 21:13:41 -05001096void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1097{
1098 struct iscsi_session *session = conn->session;
1099 unsigned long flags;
1100
1101 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001102 if (session->state == ISCSI_STATE_FAILED) {
1103 spin_unlock_irqrestore(&session->lock, flags);
1104 return;
1105 }
1106
Mike Christie67a61112006-05-30 00:37:20 -05001107 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001108 session->state = ISCSI_STATE_FAILED;
1109 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b542008-09-24 11:46:10 -05001110
Mike Christie7996a772006-04-06 21:13:41 -05001111 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1112 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b542008-09-24 11:46:10 -05001113 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001114}
1115EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1116
Mike Christie77a23c22007-05-30 12:57:18 -05001117static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1118{
1119 struct iscsi_session *session = conn->session;
1120
1121 /*
1122 * Check for iSCSI window and take care of CmdSN wrap-around
1123 */
Mike Christiee0726402007-07-26 12:46:48 -05001124 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001125 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1126 "%u MaxCmdSN %u CmdSN %u/%u\n",
1127 session->exp_cmdsn, session->max_cmdsn,
1128 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001129 return -ENOSPC;
1130 }
1131 return 0;
1132}
1133
Mike Christie9c19a7d2008-05-21 15:54:09 -05001134static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001135{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001136 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001137 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001138
Mike Christie9c19a7d2008-05-21 15:54:09 -05001139 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001140 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001141 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001142 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001143 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001144 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001145 /* done with this task */
1146 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001147 return rc;
1148}
1149
Mike Christie7996a772006-04-06 21:13:41 -05001150/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001151 * iscsi_requeue_task - requeue task to run from session workqueue
1152 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001153 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001154 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001155 * this. The session lock must be held. This should only be called
1156 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001157 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001158void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001159{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001160 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001161
Mike Christie9c19a7d2008-05-21 15:54:09 -05001162 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -06001163 scsi_queue_work(conn->session->host, &conn->xmitwork);
1164}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001165EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001166
1167/**
Mike Christie7996a772006-04-06 21:13:41 -05001168 * iscsi_data_xmit - xmit any command into the scheduled connection
1169 * @conn: iscsi connection
1170 *
1171 * Notes:
1172 * The function can return -EAGAIN in which case the caller must
1173 * re-schedule it again later or recover. '0' return code means
1174 * successful xmit.
1175 **/
1176static int iscsi_data_xmit(struct iscsi_conn *conn)
1177{
Mike Christie3219e522006-05-30 00:37:28 -05001178 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001179
Mike Christie77a23c22007-05-30 12:57:18 -05001180 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001181 if (unlikely(conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001182 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001183 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001184 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001185 }
Mike Christie7996a772006-04-06 21:13:41 -05001186
Mike Christie9c19a7d2008-05-21 15:54:09 -05001187 if (conn->task) {
1188 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001189 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001190 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001191 }
1192
Mike Christie77a23c22007-05-30 12:57:18 -05001193 /*
1194 * process mgmt pdus like nops before commands since we should
1195 * only have one nop-out as a ping from us and targets should not
1196 * overflow us with nop-ins
1197 */
1198check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001199 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001200 conn->task = list_entry(conn->mgmtqueue.next,
1201 struct iscsi_task, running);
1202 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1203 __iscsi_put_task(conn->task);
1204 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001205 continue;
1206 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001207 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001208 if (rc)
1209 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001210 }
1211
Mike Christie843c0a82007-12-13 12:43:20 -06001212 /* process pending command queue */
Mike Christieb6c395ed2006-07-24 15:47:15 -05001213 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001214 if (conn->tmf_state == TMF_QUEUED)
1215 break;
1216
Mike Christie9c19a7d2008-05-21 15:54:09 -05001217 conn->task = list_entry(conn->xmitqueue.next,
1218 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001219 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001220 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001221 continue;
1222 }
Mike Christie577577d2008-12-02 00:32:05 -06001223 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1224 if (rc) {
1225 if (rc == -ENOMEM) {
1226 conn->task = NULL;
1227 goto again;
1228 } else
1229 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001230 continue;
1231 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001232 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001233 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001234 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001235 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001236 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001237 * we need to check the mgmt queue for nops that need to
1238 * be sent to aviod starvation
1239 */
Mike Christie843c0a82007-12-13 12:43:20 -06001240 if (!list_empty(&conn->mgmtqueue))
1241 goto check_mgmt;
1242 }
1243
1244 while (!list_empty(&conn->requeue)) {
1245 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1246 break;
1247
Mike Christieb3a7ea82007-12-13 12:43:26 -06001248 /*
1249 * we always do fastlogout - conn stop code will clean up.
1250 */
1251 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1252 break;
1253
Mike Christie9c19a7d2008-05-21 15:54:09 -05001254 conn->task = list_entry(conn->requeue.next,
1255 struct iscsi_task, running);
1256 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001257 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001258 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001259 if (rc)
1260 goto again;
1261 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001262 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001263 }
Mike Christieb6c395ed2006-07-24 15:47:15 -05001264 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001265 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001266
1267again:
1268 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001269 rc = -ENODATA;
1270 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001271 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001272}
1273
David Howellsc4028952006-11-22 14:57:56 +00001274static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001275{
David Howellsc4028952006-11-22 14:57:56 +00001276 struct iscsi_conn *conn =
1277 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001278 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001279 /*
1280 * serialize Xmit worker on a per-connection basis.
1281 */
Mike Christie3219e522006-05-30 00:37:28 -05001282 do {
1283 rc = iscsi_data_xmit(conn);
1284 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001285}
1286
Mike Christie577577d2008-12-02 00:32:05 -06001287static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1288 struct scsi_cmnd *sc)
1289{
1290 struct iscsi_task *task;
1291
1292 if (!__kfifo_get(conn->session->cmdpool.queue,
1293 (void *) &task, sizeof(void *)))
1294 return NULL;
1295
1296 sc->SCp.phase = conn->session->age;
1297 sc->SCp.ptr = (char *) task;
1298
1299 atomic_set(&task->refcount, 1);
1300 task->state = ISCSI_TASK_PENDING;
1301 task->conn = conn;
1302 task->sc = sc;
1303 INIT_LIST_HEAD(&task->running);
1304 return task;
1305}
1306
Mike Christie7996a772006-04-06 21:13:41 -05001307enum {
1308 FAILURE_BAD_HOST = 1,
1309 FAILURE_SESSION_FAILED,
1310 FAILURE_SESSION_FREED,
1311 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001312 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001313 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001314 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001315 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001316 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001317 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001318};
1319
1320int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1321{
Mike Christie75613522008-05-21 15:53:59 -05001322 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001323 struct Scsi_Host *host;
1324 int reason = 0;
1325 struct iscsi_session *session;
1326 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001327 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001328
1329 sc->scsi_done = done;
1330 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001331 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001332
1333 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001334 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001335
Mike Christie75613522008-05-21 15:53:59 -05001336 cls_session = starget_to_session(scsi_target(sc->device));
1337 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001338 spin_lock(&session->lock);
1339
Mike Christie75613522008-05-21 15:53:59 -05001340 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001341 if (reason) {
1342 sc->result = reason;
1343 goto fault;
1344 }
1345
Mike Christie656cffc2006-05-18 20:31:42 -05001346 /*
1347 * ISCSI_STATE_FAILED is a temp. state. The recovery
1348 * code will decide what is best to do with command queued
1349 * during this time
1350 */
1351 if (session->state != ISCSI_STATE_LOGGED_IN &&
1352 session->state != ISCSI_STATE_FAILED) {
1353 /*
1354 * to handle the race between when we set the recovery state
1355 * and block the session we requeue here (commands could
1356 * be entering our queuecommand while a block is starting
1357 * up because the block code is not locked)
1358 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001359 switch (session->state) {
1360 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001361 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christied6d13ee2008-08-17 15:24:43 -05001362 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001363 case ISCSI_STATE_LOGGING_OUT:
1364 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christied6d13ee2008-08-17 15:24:43 -05001365 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001366 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001367 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001368 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001369 break;
1370 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001371 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001372 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001373 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001374 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001375 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001376 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001377 }
Mike Christie7996a772006-04-06 21:13:41 -05001378 goto fault;
1379 }
1380
Mike Christie7996a772006-04-06 21:13:41 -05001381 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001382 if (!conn) {
1383 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001384 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001385 goto fault;
1386 }
Mike Christie7996a772006-04-06 21:13:41 -05001387
Mike Christie77a23c22007-05-30 12:57:18 -05001388 if (iscsi_check_cmdsn_window_closed(conn)) {
1389 reason = FAILURE_WINDOW_CLOSED;
1390 goto reject;
1391 }
1392
Mike Christie577577d2008-12-02 00:32:05 -06001393 task = iscsi_alloc_task(conn, sc);
1394 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001395 reason = FAILURE_OOM;
1396 goto reject;
1397 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001398 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001399
Mike Christie052d0142008-05-21 15:54:05 -05001400 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -06001401 reason = iscsi_prep_scsi_cmd_pdu(task);
1402 if (reason) {
1403 if (reason == -ENOMEM) {
1404 reason = FAILURE_OOM;
1405 goto prepd_reject;
1406 } else {
1407 sc->result = DID_ABORT << 16;
1408 goto prepd_fault;
1409 }
Mike Christie052d0142008-05-21 15:54:05 -05001410 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001411 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001412 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001413 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001414 }
1415 } else
1416 scsi_queue_work(session->host, &conn->xmitwork);
1417
1418 session->queued_cmdsn++;
1419 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001420 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001421 return 0;
1422
Mike Christie577577d2008-12-02 00:32:05 -06001423prepd_reject:
1424 sc->scsi_done = NULL;
1425 iscsi_complete_command(task);
Mike Christie7996a772006-04-06 21:13:41 -05001426reject:
1427 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001428 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1429 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001430 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001431 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001432
Mike Christie577577d2008-12-02 00:32:05 -06001433prepd_fault:
1434 sc->scsi_done = NULL;
1435 iscsi_complete_command(task);
Mike Christie7996a772006-04-06 21:13:41 -05001436fault:
1437 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001438 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1439 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001440 if (!scsi_bidi_cmnd(sc))
1441 scsi_set_resid(sc, scsi_bufflen(sc));
1442 else {
1443 scsi_out(sc)->resid = scsi_out(sc)->length;
1444 scsi_in(sc)->resid = scsi_in(sc)->length;
1445 }
Mike Christie052d0142008-05-21 15:54:05 -05001446 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001447 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001448 return 0;
1449}
1450EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1451
1452int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1453{
1454 if (depth > ISCSI_MAX_CMD_PER_LUN)
1455 depth = ISCSI_MAX_CMD_PER_LUN;
1456 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1457 return sdev->queue_depth;
1458}
1459EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1460
Mike Christie7996a772006-04-06 21:13:41 -05001461void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1462{
Mike Christie75613522008-05-21 15:53:59 -05001463 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001464
1465 spin_lock_bh(&session->lock);
1466 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001467 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001468 if (session->leadconn)
1469 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001470 }
1471 spin_unlock_bh(&session->lock);
1472}
1473EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1474
Mike Christie8e124522008-09-24 11:46:12 -05001475int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001476{
Mike Christie75613522008-05-21 15:53:59 -05001477 struct iscsi_cls_session *cls_session;
1478 struct iscsi_session *session;
1479 struct iscsi_conn *conn;
1480
1481 cls_session = starget_to_session(scsi_target(sc->device));
1482 session = cls_session->dd_data;
1483 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001484
Mike Christiebc436b22007-12-13 12:43:28 -06001485 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001486 spin_lock_bh(&session->lock);
1487 if (session->state == ISCSI_STATE_TERMINATE) {
1488failed:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001489 iscsi_session_printk(KERN_INFO, session,
1490 "failing target reset: Could not log "
1491 "back into target [age %d]\n",
1492 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001493 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001494 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001495 return FAILED;
1496 }
1497
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 /*
1501 * we drop the lock here but the leadconn cannot be destoyed while
1502 * we are in the scsi eh
1503 */
Mike Christie843c0a82007-12-13 12:43:20 -06001504 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001505
Mike Christie1b2c7af2009-03-05 14:45:58 -06001506 ISCSI_DBG_SESSION(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001507 wait_event_interruptible(conn->ehwait,
1508 session->state == ISCSI_STATE_TERMINATE ||
1509 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001510 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001511 if (signal_pending(current))
1512 flush_signals(current);
1513
Mike Christiebc436b22007-12-13 12:43:28 -06001514 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001515 spin_lock_bh(&session->lock);
1516 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001517 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001518 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001519 else
1520 goto failed;
1521 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001522 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001523 return SUCCESS;
1524}
Mike Christie8e124522008-09-24 11:46:12 -05001525EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001526
Mike Christie843c0a82007-12-13 12:43:20 -06001527static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001528{
Mike Christie843c0a82007-12-13 12:43:20 -06001529 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001530 struct iscsi_session *session = conn->session;
1531
1532 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001533 if (conn->tmf_state == TMF_QUEUED) {
1534 conn->tmf_state = TMF_TIMEDOUT;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001535 ISCSI_DBG_SESSION(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001536 /* unblock eh_abort() */
1537 wake_up(&conn->ehwait);
1538 }
1539 spin_unlock(&session->lock);
1540}
1541
Mike Christie9c19a7d2008-05-21 15:54:09 -05001542static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001543 struct iscsi_tm *hdr, int age,
1544 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001545{
Mike Christie7996a772006-04-06 21:13:41 -05001546 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001547 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001548
Mike Christie9c19a7d2008-05-21 15:54:09 -05001549 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001550 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001551 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001552 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001553 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001554 spin_lock_bh(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001555 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001556 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001557 }
Mike Christie843c0a82007-12-13 12:43:20 -06001558 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001559 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001560 conn->tmf_timer.function = iscsi_tmf_timedout;
1561 conn->tmf_timer.data = (unsigned long)conn;
1562 add_timer(&conn->tmf_timer);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001563 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001564
Mike Christie7996a772006-04-06 21:13:41 -05001565 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001566 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001567
1568 /*
1569 * block eh thread until:
1570 *
Mike Christie843c0a82007-12-13 12:43:20 -06001571 * 1) tmf response
1572 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001573 * 3) session is terminated or restarted or userspace has
1574 * given up on recovery
1575 */
Mike Christie843c0a82007-12-13 12:43:20 -06001576 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001577 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001578 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001579 if (signal_pending(current))
1580 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001581 del_timer_sync(&conn->tmf_timer);
1582
Mike Christie6724add2007-08-15 01:38:30 -05001583 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001584 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001585 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001586 if (age != session->age ||
1587 session->state != ISCSI_STATE_LOGGED_IN)
1588 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001589 return 0;
1590}
1591
1592/*
Mike Christie843c0a82007-12-13 12:43:20 -06001593 * Fail commands. session lock held and recv side suspended and xmit
1594 * thread flushed
1595 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001596static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1597 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001598{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001599 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001600
Mike Christie9c19a7d2008-05-21 15:54:09 -05001601 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1602 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001603
1604 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001605 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1606 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001607 ISCSI_DBG_SESSION(conn->session,
1608 "failing pending sc %p itt 0x%x\n",
1609 task->sc, task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001610 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001611 }
1612 }
1613
Mike Christie9c19a7d2008-05-21 15:54:09 -05001614 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1615 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001616 ISCSI_DBG_SESSION(conn->session,
1617 "failing requeued sc %p itt 0x%x\n",
1618 task->sc, task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001619 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001620 }
1621 }
1622
1623 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001624 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1625 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001626 ISCSI_DBG_SESSION(conn->session,
1627 "failing in progress sc %p itt 0x%x\n",
1628 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001629 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001630 }
1631 }
1632}
1633
Mike Christieb40977d2008-05-21 15:54:03 -05001634void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001635{
1636 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001637 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1638 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001639}
Mike Christieb40977d2008-05-21 15:54:03 -05001640EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001641
1642static void iscsi_start_tx(struct iscsi_conn *conn)
1643{
1644 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001645 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1646 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001647}
1648
Jens Axboe242f9dc2008-09-14 05:55:09 -07001649static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001650{
1651 struct iscsi_cls_session *cls_session;
1652 struct iscsi_session *session;
1653 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001654 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001655
1656 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001657 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001658
Mike Christie1b2c7af2009-03-05 14:45:58 -06001659 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
Mike Christief6d51802007-12-13 12:43:30 -06001660
1661 spin_lock(&session->lock);
1662 if (session->state != ISCSI_STATE_LOGGED_IN) {
1663 /*
1664 * We are probably in the middle of iscsi recovery so let
1665 * that complete and handle the error.
1666 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001667 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001668 goto done;
1669 }
1670
1671 conn = session->leadconn;
1672 if (!conn) {
1673 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001674 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001675 goto done;
1676 }
1677
1678 if (!conn->recv_timeout && !conn->ping_timeout)
1679 goto done;
1680 /*
1681 * if the ping timedout then we are in the middle of cleaning up
1682 * and can let the iscsi eh handle it
1683 */
1684 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1685 (conn->ping_timeout * HZ), jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001686 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001687 /*
1688 * if we are about to check the transport then give the command
1689 * more time
1690 */
1691 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1692 jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001693 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001694 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001695 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001696 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001697done:
1698 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001699 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1700 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001701 return rc;
1702}
1703
1704static void iscsi_check_transport_timeouts(unsigned long data)
1705{
1706 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1707 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001708 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001709
1710 spin_lock(&session->lock);
1711 if (session->state != ISCSI_STATE_LOGGED_IN)
1712 goto done;
1713
Mike Christie4cf10432008-05-07 20:43:52 -05001714 recv_timeout = conn->recv_timeout;
1715 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001716 goto done;
1717
Mike Christie4cf10432008-05-07 20:43:52 -05001718 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001719 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001720 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001721 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001722 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001723 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1724 "expired, last rx %lu, last ping %lu, "
1725 "now %lu\n", conn->ping_timeout, last_recv,
1726 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001727 spin_unlock(&session->lock);
1728 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1729 return;
1730 }
1731
Mike Christie4cf10432008-05-07 20:43:52 -05001732 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001733 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001734 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05001735 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001736 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001737 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001738 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001739
Mike Christie1b2c7af2009-03-05 14:45:58 -06001740 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06001741 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001742done:
1743 spin_unlock(&session->lock);
1744}
1745
Mike Christie9c19a7d2008-05-21 15:54:09 -05001746static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001747 struct iscsi_tm *hdr)
1748{
1749 memset(hdr, 0, sizeof(*hdr));
1750 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1751 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1752 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001753 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1754 hdr->rtt = task->hdr_itt;
1755 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001756}
1757
Mike Christie7996a772006-04-06 21:13:41 -05001758int iscsi_eh_abort(struct scsi_cmnd *sc)
1759{
Mike Christie75613522008-05-21 15:53:59 -05001760 struct iscsi_cls_session *cls_session;
1761 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001762 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001763 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001764 struct iscsi_tm *hdr;
1765 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001766
Mike Christie75613522008-05-21 15:53:59 -05001767 cls_session = starget_to_session(scsi_target(sc->device));
1768 session = cls_session->dd_data;
1769
Mike Christie6724add2007-08-15 01:38:30 -05001770 mutex_lock(&session->eh_mutex);
1771 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001772 /*
1773 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1774 * got the command.
1775 */
1776 if (!sc->SCp.ptr) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001777 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1778 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001779 spin_unlock_bh(&session->lock);
1780 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001781 return SUCCESS;
1782 }
1783
Mike Christie7996a772006-04-06 21:13:41 -05001784 /*
1785 * If we are not logged in or we have started a new session
1786 * then let the host reset code handle this
1787 */
Mike Christie843c0a82007-12-13 12:43:20 -06001788 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1789 sc->SCp.phase != session->age) {
1790 spin_unlock_bh(&session->lock);
1791 mutex_unlock(&session->eh_mutex);
1792 return FAILED;
1793 }
1794
1795 conn = session->leadconn;
1796 conn->eh_abort_cnt++;
1797 age = session->age;
1798
Mike Christie9c19a7d2008-05-21 15:54:09 -05001799 task = (struct iscsi_task *)sc->SCp.ptr;
Mike Christie1b2c7af2009-03-05 14:45:58 -06001800 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1801 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001802
Mike Christie9c19a7d2008-05-21 15:54:09 -05001803 /* task completed before time out */
1804 if (!task->sc) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001805 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1806 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001807 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001808 }
Mike Christie7996a772006-04-06 21:13:41 -05001809
Mike Christie9c19a7d2008-05-21 15:54:09 -05001810 if (task->state == ISCSI_TASK_PENDING) {
1811 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001812 goto success;
1813 }
Mike Christie7996a772006-04-06 21:13:41 -05001814
Mike Christie843c0a82007-12-13 12:43:20 -06001815 /* only have one tmf outstanding at a time */
1816 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001817 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001818 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001819
Mike Christie843c0a82007-12-13 12:43:20 -06001820 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001821 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001822
Mike Christie9c19a7d2008-05-21 15:54:09 -05001823 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001824 rc = FAILED;
1825 goto failed;
1826 }
1827
1828 switch (conn->tmf_state) {
1829 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001830 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001831 /*
1832 * stop tx side incase the target had sent a abort rsp but
1833 * the initiator was still writing out data.
1834 */
Mike Christie6724add2007-08-15 01:38:30 -05001835 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001836 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001837 * we do not stop the recv side because targets have been
1838 * good and have never sent us a successful tmf response
1839 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001840 */
Mike Christie77a23c22007-05-30 12:57:18 -05001841 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001842 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001843 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001844 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001845 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001846 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001847 case TMF_TIMEDOUT:
1848 spin_unlock_bh(&session->lock);
1849 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1850 goto failed_unlocked;
1851 case TMF_NOT_FOUND:
1852 if (!sc->SCp.ptr) {
1853 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001854 /* task completed before tmf abort response */
Mike Christie1b2c7af2009-03-05 14:45:58 -06001855 ISCSI_DBG_SESSION(session, "sc completed while abort "
1856 "in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001857 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001858 }
1859 /* fall through */
1860 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001861 conn->tmf_state = TMF_INITIAL;
1862 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001863 }
1864
Mike Christie77a23c22007-05-30 12:57:18 -05001865success:
Mike Christie7996a772006-04-06 21:13:41 -05001866 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001867success_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001868 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1869 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001870 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001871 return SUCCESS;
1872
1873failed:
1874 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001875failed_unlocked:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001876 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1877 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001878 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001879 return FAILED;
1880}
1881EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1882
Mike Christie843c0a82007-12-13 12:43:20 -06001883static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1884{
1885 memset(hdr, 0, sizeof(*hdr));
1886 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1887 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1888 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1889 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001890 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001891}
1892
1893int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1894{
Mike Christie75613522008-05-21 15:53:59 -05001895 struct iscsi_cls_session *cls_session;
1896 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001897 struct iscsi_conn *conn;
1898 struct iscsi_tm *hdr;
1899 int rc = FAILED;
1900
Mike Christie75613522008-05-21 15:53:59 -05001901 cls_session = starget_to_session(scsi_target(sc->device));
1902 session = cls_session->dd_data;
1903
Mike Christie1b2c7af2009-03-05 14:45:58 -06001904 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1905 sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06001906
1907 mutex_lock(&session->eh_mutex);
1908 spin_lock_bh(&session->lock);
1909 /*
1910 * Just check if we are not logged in. We cannot check for
1911 * the phase because the reset could come from a ioctl.
1912 */
1913 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1914 goto unlock;
1915 conn = session->leadconn;
1916
1917 /* only have one tmf outstanding at a time */
1918 if (conn->tmf_state != TMF_INITIAL)
1919 goto unlock;
1920 conn->tmf_state = TMF_QUEUED;
1921
1922 hdr = &conn->tmhdr;
1923 iscsi_prep_lun_reset_pdu(sc, hdr);
1924
Mike Christie9c19a7d2008-05-21 15:54:09 -05001925 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001926 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001927 rc = FAILED;
1928 goto unlock;
1929 }
1930
1931 switch (conn->tmf_state) {
1932 case TMF_SUCCESS:
1933 break;
1934 case TMF_TIMEDOUT:
1935 spin_unlock_bh(&session->lock);
1936 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1937 goto done;
1938 default:
1939 conn->tmf_state = TMF_INITIAL;
1940 goto unlock;
1941 }
1942
1943 rc = SUCCESS;
1944 spin_unlock_bh(&session->lock);
1945
1946 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001947
Mike Christiea3439142008-09-24 11:46:15 -05001948 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001949 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001950 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05001951 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001952
1953 iscsi_start_tx(conn);
1954 goto done;
1955
1956unlock:
1957 spin_unlock_bh(&session->lock);
1958done:
Mike Christie1b2c7af2009-03-05 14:45:58 -06001959 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
1960 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06001961 mutex_unlock(&session->eh_mutex);
1962 return rc;
1963}
1964EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1965
Olaf Kirch63203772007-12-13 12:43:25 -06001966/*
1967 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1968 * should be accessed via kfifo_{get,put} on q->queue.
1969 * Optionally, the caller can obtain the array of object pointers
1970 * by passing in a non-NULL @items pointer
1971 */
Mike Christie7996a772006-04-06 21:13:41 -05001972int
Olaf Kirch63203772007-12-13 12:43:25 -06001973iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001974{
Olaf Kirch63203772007-12-13 12:43:25 -06001975 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001976
Olaf Kirch63203772007-12-13 12:43:25 -06001977 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001978
1979 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001980
1981 /* If the user passed an items pointer, he wants a copy of
1982 * the array. */
1983 if (items)
1984 num_arrays++;
1985 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1986 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06001987 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001988
1989 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1990 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001991 if (q->queue == ERR_PTR(-ENOMEM))
1992 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001993
1994 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001995 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001996 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001997 q->max = i;
1998 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001999 }
Mike Christie7996a772006-04-06 21:13:41 -05002000 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2001 }
Olaf Kirch63203772007-12-13 12:43:25 -06002002
2003 if (items) {
2004 *items = q->pool + max;
2005 memcpy(*items, q->pool, max * sizeof(void *));
2006 }
2007
Mike Christie7996a772006-04-06 21:13:41 -05002008 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002009
2010enomem:
2011 iscsi_pool_free(q);
2012 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002013}
2014EXPORT_SYMBOL_GPL(iscsi_pool_init);
2015
Olaf Kirch63203772007-12-13 12:43:25 -06002016void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002017{
2018 int i;
2019
2020 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002021 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002022 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002023 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002024}
2025EXPORT_SYMBOL_GPL(iscsi_pool_free);
2026
Mike Christiea4804cd2008-05-21 15:54:00 -05002027/**
2028 * iscsi_host_add - add host to system
2029 * @shost: scsi host
2030 * @pdev: parent device
2031 *
2032 * This should be called by partial offload and software iscsi drivers
2033 * to add a host to the system.
2034 */
2035int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002036{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002037 if (!shost->can_queue)
2038 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2039
Mike Christie308cec12009-02-06 12:06:20 -06002040 if (!shost->transportt->eh_timed_out)
2041 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002042 return scsi_add_host(shost, pdev);
2043}
2044EXPORT_SYMBOL_GPL(iscsi_host_add);
2045
2046/**
2047 * iscsi_host_alloc - allocate a host and driver data
2048 * @sht: scsi host template
2049 * @dd_data_size: driver host data size
2050 * @qdepth: default device queue depth
2051 *
2052 * This should be called by partial offload and software iscsi drivers.
2053 * To access the driver specific memory use the iscsi_host_priv() macro.
2054 */
2055struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
2056 int dd_data_size, uint16_t qdepth)
2057{
2058 struct Scsi_Host *shost;
Mike Christiee5bd7b542008-09-24 11:46:10 -05002059 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002060
2061 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2062 if (!shost)
2063 return NULL;
Mike Christiea4804cd2008-05-21 15:54:00 -05002064
Mike Christie15482712007-05-30 12:57:19 -05002065 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
2066 if (qdepth != 0)
2067 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05002068 "Queue depth must be between 1 and %d.\n",
2069 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05002070 qdepth = ISCSI_DEF_CMD_PER_LUN;
2071 }
Mike Christie75613522008-05-21 15:53:59 -05002072 shost->cmd_per_lun = qdepth;
Mike Christiee5bd7b542008-09-24 11:46:10 -05002073
2074 ihost = shost_priv(shost);
2075 spin_lock_init(&ihost->lock);
2076 ihost->state = ISCSI_HOST_SETUP;
2077 ihost->num_sessions = 0;
2078 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002079 return shost;
Mike Christie75613522008-05-21 15:53:59 -05002080}
Mike Christiea4804cd2008-05-21 15:54:00 -05002081EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002082
Mike Christiee5bd7b542008-09-24 11:46:10 -05002083static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2084{
2085 iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
2086}
2087
Mike Christiea4804cd2008-05-21 15:54:00 -05002088/**
2089 * iscsi_host_remove - remove host and sessions
2090 * @shost: scsi host
2091 *
Mike Christiee5bd7b542008-09-24 11:46:10 -05002092 * If there are any sessions left, this will initiate the removal and wait
2093 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002094 */
2095void iscsi_host_remove(struct Scsi_Host *shost)
2096{
Mike Christiee5bd7b542008-09-24 11:46:10 -05002097 struct iscsi_host *ihost = shost_priv(shost);
2098 unsigned long flags;
2099
2100 spin_lock_irqsave(&ihost->lock, flags);
2101 ihost->state = ISCSI_HOST_REMOVED;
2102 spin_unlock_irqrestore(&ihost->lock, flags);
2103
2104 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2105 wait_event_interruptible(ihost->session_removal_wq,
2106 ihost->num_sessions == 0);
2107 if (signal_pending(current))
2108 flush_signals(current);
2109
Mike Christiea4804cd2008-05-21 15:54:00 -05002110 scsi_remove_host(shost);
2111}
2112EXPORT_SYMBOL_GPL(iscsi_host_remove);
2113
2114void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002115{
2116 struct iscsi_host *ihost = shost_priv(shost);
2117
2118 kfree(ihost->netdev);
2119 kfree(ihost->hwaddress);
2120 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002121 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002122}
Mike Christiea4804cd2008-05-21 15:54:00 -05002123EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002124
Mike Christiee5bd7b542008-09-24 11:46:10 -05002125static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2126{
2127 struct iscsi_host *ihost = shost_priv(shost);
2128 unsigned long flags;
2129
2130 shost = scsi_host_get(shost);
2131 if (!shost) {
2132 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2133 "of session teardown event because host already "
2134 "removed.\n");
2135 return;
2136 }
2137
2138 spin_lock_irqsave(&ihost->lock, flags);
2139 ihost->num_sessions--;
2140 if (ihost->num_sessions == 0)
2141 wake_up(&ihost->session_removal_wq);
2142 spin_unlock_irqrestore(&ihost->lock, flags);
2143 scsi_host_put(shost);
2144}
2145
Mike Christie75613522008-05-21 15:53:59 -05002146/**
2147 * iscsi_session_setup - create iscsi cls session and host and session
2148 * @iscsit: iscsi transport template
2149 * @shost: scsi host
2150 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002151 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002152 * @initial_cmdsn: initial CmdSN
2153 *
2154 * This can be used by software iscsi_transports that allocate
2155 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002156 *
2157 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2158 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2159 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002160 */
2161struct iscsi_cls_session *
2162iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002163 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002164 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002165{
Mike Christiee5bd7b542008-09-24 11:46:10 -05002166 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002167 struct iscsi_session *session;
2168 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002169 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b542008-09-24 11:46:10 -05002170 unsigned long flags;
2171
2172 spin_lock_irqsave(&ihost->lock, flags);
2173 if (ihost->state == ISCSI_HOST_REMOVED) {
2174 spin_unlock_irqrestore(&ihost->lock, flags);
2175 return NULL;
2176 }
2177 ihost->num_sessions++;
2178 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002179
2180 if (!total_cmds)
2181 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002182 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002183 * The iscsi layer needs some tasks for nop handling and tmfs,
2184 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2185 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002186 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002187 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2188 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2189 "must be a power of two that is at least %d.\n",
2190 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b542008-09-24 11:46:10 -05002191 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002192 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002193
2194 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2195 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2196 "must be a power of 2 less than or equal to %d.\n",
2197 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2198 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2199 }
2200
2201 if (!is_power_of_2(total_cmds)) {
2202 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2203 "must be a power of 2.\n", total_cmds);
2204 total_cmds = rounddown_pow_of_two(total_cmds);
2205 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2206 return NULL;
2207 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2208 total_cmds);
2209 }
2210 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002211
Mike Christie5d91e202008-05-21 15:54:01 -05002212 cls_session = iscsi_alloc_session(shost, iscsit,
2213 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002214 if (!cls_session)
Mike Christiee5bd7b542008-09-24 11:46:10 -05002215 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002216 session = cls_session->dd_data;
2217 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002218 session->host = shost;
2219 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002220 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002221 session->lu_reset_timeout = 15;
2222 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002223 session->scsi_cmds_max = scsi_cmds;
2224 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002225 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002226 session->exp_cmdsn = initial_cmdsn + 1;
2227 session->max_cmdsn = initial_cmdsn + 1;
2228 session->max_r2t = 1;
2229 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002230 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002231 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002232
2233 /* initialize SCSI PDU commands pool */
2234 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2235 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002236 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002237 goto cmdpool_alloc_fail;
2238
2239 /* pre-format cmds pool with ITT */
2240 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002241 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002242
Mike Christie9c19a7d2008-05-21 15:54:09 -05002243 if (cmd_task_size)
2244 task->dd_data = &task[1];
2245 task->itt = cmd_i;
2246 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002247 }
2248
Mike Christief53a88d2006-06-28 12:00:27 -05002249 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002250 goto module_get_fail;
2251
Mike Christie79706342008-05-21 15:54:12 -05002252 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002253 goto cls_session_fail;
Mike Christiee5bd7b542008-09-24 11:46:10 -05002254
Mike Christie7996a772006-04-06 21:13:41 -05002255 return cls_session;
2256
2257cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002258 module_put(iscsit->owner);
2259module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002260 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002261cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002262 iscsi_free_session(cls_session);
Mike Christiee5bd7b542008-09-24 11:46:10 -05002263dec_session_count:
2264 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002265 return NULL;
2266}
2267EXPORT_SYMBOL_GPL(iscsi_session_setup);
2268
2269/**
2270 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002271 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002272 *
Mike Christie75613522008-05-21 15:53:59 -05002273 * The driver must have called iscsi_remove_session before
2274 * calling this.
2275 */
Mike Christie7996a772006-04-06 21:13:41 -05002276void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2277{
Mike Christie75613522008-05-21 15:53:59 -05002278 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002279 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b542008-09-24 11:46:10 -05002280 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002281
Olaf Kirch63203772007-12-13 12:43:25 -06002282 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002283
Mike Christieb2c64162007-05-30 12:57:16 -05002284 kfree(session->password);
2285 kfree(session->password_in);
2286 kfree(session->username);
2287 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002288 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002289 kfree(session->initiatorname);
2290 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002291
Mike Christie75613522008-05-21 15:53:59 -05002292 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b542008-09-24 11:46:10 -05002293 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002294 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002295}
2296EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2297
2298/**
2299 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2300 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002301 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002302 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002303 */
Mike Christie7996a772006-04-06 21:13:41 -05002304struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002305iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2306 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002307{
Mike Christie75613522008-05-21 15:53:59 -05002308 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002309 struct iscsi_conn *conn;
2310 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002311 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002312
Mike Christie5d91e202008-05-21 15:54:01 -05002313 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2314 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002315 if (!cls_conn)
2316 return NULL;
2317 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002318 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002319
Mike Christie5d91e202008-05-21 15:54:01 -05002320 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002321 conn->session = session;
2322 conn->cls_conn = cls_conn;
2323 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2324 conn->id = conn_idx;
2325 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002326 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002327
2328 init_timer(&conn->transport_timer);
2329 conn->transport_timer.data = (unsigned long)conn;
2330 conn->transport_timer.function = iscsi_check_transport_timeouts;
2331
Mike Christie7996a772006-04-06 21:13:41 -05002332 INIT_LIST_HEAD(&conn->run_list);
2333 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002334 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395ed2006-07-24 15:47:15 -05002335 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002336 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002337 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002338
Mike Christie9c19a7d2008-05-21 15:54:09 -05002339 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002340 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002341 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002342 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002343 sizeof(void*))) {
2344 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002345 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002346 }
2347 spin_unlock_bh(&session->lock);
2348
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002349 data = (char *) __get_free_pages(GFP_KERNEL,
2350 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002351 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002352 goto login_task_data_alloc_fail;
2353 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002354
Mike Christie843c0a82007-12-13 12:43:20 -06002355 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002356 init_waitqueue_head(&conn->ehwait);
2357
2358 return cls_conn;
2359
Mike Christie9c19a7d2008-05-21 15:54:09 -05002360login_task_data_alloc_fail:
2361 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002362 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002363login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002364 iscsi_destroy_conn(cls_conn);
2365 return NULL;
2366}
2367EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2368
2369/**
2370 * iscsi_conn_teardown - teardown iscsi connection
2371 * cls_conn: iscsi class connection
2372 *
2373 * TODO: we may need to make this into a two step process
2374 * like scsi-mls remove + put host
2375 */
2376void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2377{
2378 struct iscsi_conn *conn = cls_conn->dd_data;
2379 struct iscsi_session *session = conn->session;
2380 unsigned long flags;
2381
Mike Christief6d51802007-12-13 12:43:30 -06002382 del_timer_sync(&conn->transport_timer);
2383
Mike Christie7996a772006-04-06 21:13:41 -05002384 spin_lock_bh(&session->lock);
2385 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2386 if (session->leadconn == conn) {
2387 /*
2388 * leading connection? then give up on recovery.
2389 */
2390 session->state = ISCSI_STATE_TERMINATE;
2391 wake_up(&conn->ehwait);
2392 }
2393 spin_unlock_bh(&session->lock);
2394
Mike Christie7996a772006-04-06 21:13:41 -05002395 /*
2396 * Block until all in-progress commands for this connection
2397 * time out or fail.
2398 */
2399 for (;;) {
2400 spin_lock_irqsave(session->host->host_lock, flags);
2401 if (!session->host->host_busy) { /* OK for ERL == 0 */
2402 spin_unlock_irqrestore(session->host->host_lock, flags);
2403 break;
2404 }
2405 spin_unlock_irqrestore(session->host->host_lock, flags);
2406 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002407 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2408 "host_busy %d host_failed %d\n",
2409 session->host->host_busy,
2410 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002411 /*
2412 * force eh_abort() to unblock
2413 */
2414 wake_up(&conn->ehwait);
2415 }
2416
Mike Christie779ea122007-02-28 17:32:15 -06002417 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002418 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002419
Mike Christie7996a772006-04-06 21:13:41 -05002420 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002421 free_pages((unsigned long) conn->data,
2422 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002423 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002424 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002425 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002426 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002427 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002428 spin_unlock_bh(&session->lock);
2429
Mike Christie7996a772006-04-06 21:13:41 -05002430 iscsi_destroy_conn(cls_conn);
2431}
2432EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2433
2434int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2435{
2436 struct iscsi_conn *conn = cls_conn->dd_data;
2437 struct iscsi_session *session = conn->session;
2438
Mike Christieffd04362006-08-31 18:09:24 -04002439 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002440 iscsi_conn_printk(KERN_ERR, conn,
2441 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002442 return -EPERM;
2443 }
2444
Mike Christiedb98ccd2006-08-31 18:09:31 -04002445 if ((session->imm_data_en || !session->initial_r2t_en) &&
2446 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002447 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2448 "first_burst %d max_burst %d\n",
2449 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002450 return -EINVAL;
2451 }
2452
Mike Christief6d51802007-12-13 12:43:30 -06002453 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002454 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2455 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002456 conn->recv_timeout = 5;
2457 }
2458
2459 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002460 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2461 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002462 conn->ping_timeout = 5;
2463 }
2464
Mike Christie7996a772006-04-06 21:13:41 -05002465 spin_lock_bh(&session->lock);
2466 conn->c_stage = ISCSI_CONN_STARTED;
2467 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002468 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002469
Mike Christief6d51802007-12-13 12:43:30 -06002470 conn->last_recv = jiffies;
2471 conn->last_ping = jiffies;
2472 if (conn->recv_timeout && conn->ping_timeout)
2473 mod_timer(&conn->transport_timer,
2474 jiffies + (conn->recv_timeout * HZ));
2475
Mike Christie7996a772006-04-06 21:13:41 -05002476 switch(conn->stop_stage) {
2477 case STOP_CONN_RECOVER:
2478 /*
2479 * unblock eh_abort() if it is blocked. re-try all
2480 * commands after successful recovery
2481 */
Mike Christie7996a772006-04-06 21:13:41 -05002482 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002483 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002484 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002485 if (session->age == 16)
2486 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002487 break;
Mike Christie7996a772006-04-06 21:13:41 -05002488 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002489 conn->stop_stage = 0;
2490 break;
Mike Christie7996a772006-04-06 21:13:41 -05002491 default:
2492 break;
2493 }
2494 spin_unlock_bh(&session->lock);
2495
Mike Christie75613522008-05-21 15:53:59 -05002496 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002497 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002498 return 0;
2499}
2500EXPORT_SYMBOL_GPL(iscsi_conn_start);
2501
2502static void
2503flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2504{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002505 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002506
2507 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002508 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002509 ISCSI_DBG_SESSION(session, "flushing pending mgmt task "
2510 "itt 0x%x\n", task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002511 /* release ref from prep task */
2512 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002513 }
2514
2515 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002516 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002517 ISCSI_DBG_SESSION(session, "flushing running mgmt task "
2518 "itt 0x%x\n", task->itt);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002519 /* release ref from prep task */
2520 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002521 }
2522
Mike Christie9c19a7d2008-05-21 15:54:09 -05002523 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002524}
2525
Mike Christie656cffc2006-05-18 20:31:42 -05002526static void iscsi_start_session_recovery(struct iscsi_session *session,
2527 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002528{
Mike Christieed2abc72006-05-02 19:46:40 -05002529 int old_stop_stage;
2530
Mike Christief6d51802007-12-13 12:43:30 -06002531 del_timer_sync(&conn->transport_timer);
2532
Mike Christie6724add2007-08-15 01:38:30 -05002533 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002534 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002535 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002536 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002537 mutex_unlock(&session->eh_mutex);
2538 return;
2539 }
2540
2541 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002542 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002543 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002544 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002545 */
Mike Christie67a61112006-05-30 00:37:20 -05002546 if (flag == STOP_CONN_TERM)
2547 session->state = ISCSI_STATE_TERMINATE;
2548 else if (conn->stop_stage != STOP_CONN_RECOVER)
2549 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002550
2551 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002552 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002553 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002554 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002555
2556 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002557 /*
2558 * for connection level recovery we should not calculate
2559 * header digest. conn->hdr_size used for optimization
2560 * in hdr_extract() and will be re-negotiated at
2561 * set_param() time.
2562 */
2563 if (flag == STOP_CONN_RECOVER) {
2564 conn->hdrdgst_en = 0;
2565 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002566 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002567 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002568 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002569 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002570 }
Mike Christie7996a772006-04-06 21:13:41 -05002571 }
Mike Christie656cffc2006-05-18 20:31:42 -05002572
Mike Christie656cffc2006-05-18 20:31:42 -05002573 /*
2574 * flush queues.
2575 */
2576 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002577 if (flag == STOP_CONN_RECOVER)
Mike Christie56d7fcf2008-08-19 18:45:26 -05002578 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2579 else
2580 fail_all_commands(conn, -1, DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002581 flush_control_queues(session, conn);
2582 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002583 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002584}
Mike Christie7996a772006-04-06 21:13:41 -05002585
2586void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2587{
2588 struct iscsi_conn *conn = cls_conn->dd_data;
2589 struct iscsi_session *session = conn->session;
2590
2591 switch (flag) {
2592 case STOP_CONN_RECOVER:
2593 case STOP_CONN_TERM:
2594 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002595 break;
Mike Christie7996a772006-04-06 21:13:41 -05002596 default:
Mike Christie322d7392008-01-31 13:36:52 -06002597 iscsi_conn_printk(KERN_ERR, conn,
2598 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002599 }
2600}
2601EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2602
2603int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2604 struct iscsi_cls_conn *cls_conn, int is_leading)
2605{
Mike Christie75613522008-05-21 15:53:59 -05002606 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002607 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002608
Mike Christie7996a772006-04-06 21:13:41 -05002609 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002610 if (is_leading)
2611 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002612 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002613
2614 /*
2615 * Unblock xmitworker(), Login Phase will pass through.
2616 */
2617 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2618 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2619 return 0;
2620}
2621EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2622
Mike Christiea54a52c2006-06-28 12:00:23 -05002623
2624int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2625 enum iscsi_param param, char *buf, int buflen)
2626{
2627 struct iscsi_conn *conn = cls_conn->dd_data;
2628 struct iscsi_session *session = conn->session;
2629 uint32_t value;
2630
2631 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002632 case ISCSI_PARAM_FAST_ABORT:
2633 sscanf(buf, "%d", &session->fast_abort);
2634 break;
Mike Christief6d51802007-12-13 12:43:30 -06002635 case ISCSI_PARAM_ABORT_TMO:
2636 sscanf(buf, "%d", &session->abort_timeout);
2637 break;
2638 case ISCSI_PARAM_LU_RESET_TMO:
2639 sscanf(buf, "%d", &session->lu_reset_timeout);
2640 break;
2641 case ISCSI_PARAM_PING_TMO:
2642 sscanf(buf, "%d", &conn->ping_timeout);
2643 break;
2644 case ISCSI_PARAM_RECV_TMO:
2645 sscanf(buf, "%d", &conn->recv_timeout);
2646 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002647 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2648 sscanf(buf, "%d", &conn->max_recv_dlength);
2649 break;
2650 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2651 sscanf(buf, "%d", &conn->max_xmit_dlength);
2652 break;
2653 case ISCSI_PARAM_HDRDGST_EN:
2654 sscanf(buf, "%d", &conn->hdrdgst_en);
2655 break;
2656 case ISCSI_PARAM_DATADGST_EN:
2657 sscanf(buf, "%d", &conn->datadgst_en);
2658 break;
2659 case ISCSI_PARAM_INITIAL_R2T_EN:
2660 sscanf(buf, "%d", &session->initial_r2t_en);
2661 break;
2662 case ISCSI_PARAM_MAX_R2T:
2663 sscanf(buf, "%d", &session->max_r2t);
2664 break;
2665 case ISCSI_PARAM_IMM_DATA_EN:
2666 sscanf(buf, "%d", &session->imm_data_en);
2667 break;
2668 case ISCSI_PARAM_FIRST_BURST:
2669 sscanf(buf, "%d", &session->first_burst);
2670 break;
2671 case ISCSI_PARAM_MAX_BURST:
2672 sscanf(buf, "%d", &session->max_burst);
2673 break;
2674 case ISCSI_PARAM_PDU_INORDER_EN:
2675 sscanf(buf, "%d", &session->pdu_inorder_en);
2676 break;
2677 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2678 sscanf(buf, "%d", &session->dataseq_inorder_en);
2679 break;
2680 case ISCSI_PARAM_ERL:
2681 sscanf(buf, "%d", &session->erl);
2682 break;
2683 case ISCSI_PARAM_IFMARKER_EN:
2684 sscanf(buf, "%d", &value);
2685 BUG_ON(value);
2686 break;
2687 case ISCSI_PARAM_OFMARKER_EN:
2688 sscanf(buf, "%d", &value);
2689 BUG_ON(value);
2690 break;
2691 case ISCSI_PARAM_EXP_STATSN:
2692 sscanf(buf, "%u", &conn->exp_statsn);
2693 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002694 case ISCSI_PARAM_USERNAME:
2695 kfree(session->username);
2696 session->username = kstrdup(buf, GFP_KERNEL);
2697 if (!session->username)
2698 return -ENOMEM;
2699 break;
2700 case ISCSI_PARAM_USERNAME_IN:
2701 kfree(session->username_in);
2702 session->username_in = kstrdup(buf, GFP_KERNEL);
2703 if (!session->username_in)
2704 return -ENOMEM;
2705 break;
2706 case ISCSI_PARAM_PASSWORD:
2707 kfree(session->password);
2708 session->password = kstrdup(buf, GFP_KERNEL);
2709 if (!session->password)
2710 return -ENOMEM;
2711 break;
2712 case ISCSI_PARAM_PASSWORD_IN:
2713 kfree(session->password_in);
2714 session->password_in = kstrdup(buf, GFP_KERNEL);
2715 if (!session->password_in)
2716 return -ENOMEM;
2717 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002718 case ISCSI_PARAM_TARGET_NAME:
2719 /* this should not change between logins */
2720 if (session->targetname)
2721 break;
2722
2723 session->targetname = kstrdup(buf, GFP_KERNEL);
2724 if (!session->targetname)
2725 return -ENOMEM;
2726 break;
2727 case ISCSI_PARAM_TPGT:
2728 sscanf(buf, "%d", &session->tpgt);
2729 break;
2730 case ISCSI_PARAM_PERSISTENT_PORT:
2731 sscanf(buf, "%d", &conn->persistent_port);
2732 break;
2733 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2734 /*
2735 * this is the address returned in discovery so it should
2736 * not change between logins.
2737 */
2738 if (conn->persistent_address)
2739 break;
2740
2741 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2742 if (!conn->persistent_address)
2743 return -ENOMEM;
2744 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002745 case ISCSI_PARAM_IFACE_NAME:
2746 if (!session->ifacename)
2747 session->ifacename = kstrdup(buf, GFP_KERNEL);
2748 break;
2749 case ISCSI_PARAM_INITIATOR_NAME:
2750 if (!session->initiatorname)
2751 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2752 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002753 default:
2754 return -ENOSYS;
2755 }
2756
2757 return 0;
2758}
2759EXPORT_SYMBOL_GPL(iscsi_set_param);
2760
2761int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2762 enum iscsi_param param, char *buf)
2763{
Mike Christie75613522008-05-21 15:53:59 -05002764 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002765 int len;
2766
2767 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002768 case ISCSI_PARAM_FAST_ABORT:
2769 len = sprintf(buf, "%d\n", session->fast_abort);
2770 break;
Mike Christief6d51802007-12-13 12:43:30 -06002771 case ISCSI_PARAM_ABORT_TMO:
2772 len = sprintf(buf, "%d\n", session->abort_timeout);
2773 break;
2774 case ISCSI_PARAM_LU_RESET_TMO:
2775 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2776 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002777 case ISCSI_PARAM_INITIAL_R2T_EN:
2778 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2779 break;
2780 case ISCSI_PARAM_MAX_R2T:
2781 len = sprintf(buf, "%hu\n", session->max_r2t);
2782 break;
2783 case ISCSI_PARAM_IMM_DATA_EN:
2784 len = sprintf(buf, "%d\n", session->imm_data_en);
2785 break;
2786 case ISCSI_PARAM_FIRST_BURST:
2787 len = sprintf(buf, "%u\n", session->first_burst);
2788 break;
2789 case ISCSI_PARAM_MAX_BURST:
2790 len = sprintf(buf, "%u\n", session->max_burst);
2791 break;
2792 case ISCSI_PARAM_PDU_INORDER_EN:
2793 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2794 break;
2795 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2796 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2797 break;
2798 case ISCSI_PARAM_ERL:
2799 len = sprintf(buf, "%d\n", session->erl);
2800 break;
2801 case ISCSI_PARAM_TARGET_NAME:
2802 len = sprintf(buf, "%s\n", session->targetname);
2803 break;
2804 case ISCSI_PARAM_TPGT:
2805 len = sprintf(buf, "%d\n", session->tpgt);
2806 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002807 case ISCSI_PARAM_USERNAME:
2808 len = sprintf(buf, "%s\n", session->username);
2809 break;
2810 case ISCSI_PARAM_USERNAME_IN:
2811 len = sprintf(buf, "%s\n", session->username_in);
2812 break;
2813 case ISCSI_PARAM_PASSWORD:
2814 len = sprintf(buf, "%s\n", session->password);
2815 break;
2816 case ISCSI_PARAM_PASSWORD_IN:
2817 len = sprintf(buf, "%s\n", session->password_in);
2818 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002819 case ISCSI_PARAM_IFACE_NAME:
2820 len = sprintf(buf, "%s\n", session->ifacename);
2821 break;
2822 case ISCSI_PARAM_INITIATOR_NAME:
2823 if (!session->initiatorname)
2824 len = sprintf(buf, "%s\n", "unknown");
2825 else
2826 len = sprintf(buf, "%s\n", session->initiatorname);
2827 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002828 default:
2829 return -ENOSYS;
2830 }
2831
2832 return len;
2833}
2834EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2835
2836int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2837 enum iscsi_param param, char *buf)
2838{
2839 struct iscsi_conn *conn = cls_conn->dd_data;
2840 int len;
2841
2842 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002843 case ISCSI_PARAM_PING_TMO:
2844 len = sprintf(buf, "%u\n", conn->ping_timeout);
2845 break;
2846 case ISCSI_PARAM_RECV_TMO:
2847 len = sprintf(buf, "%u\n", conn->recv_timeout);
2848 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002849 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2850 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2851 break;
2852 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2853 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2854 break;
2855 case ISCSI_PARAM_HDRDGST_EN:
2856 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2857 break;
2858 case ISCSI_PARAM_DATADGST_EN:
2859 len = sprintf(buf, "%d\n", conn->datadgst_en);
2860 break;
2861 case ISCSI_PARAM_IFMARKER_EN:
2862 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2863 break;
2864 case ISCSI_PARAM_OFMARKER_EN:
2865 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2866 break;
2867 case ISCSI_PARAM_EXP_STATSN:
2868 len = sprintf(buf, "%u\n", conn->exp_statsn);
2869 break;
2870 case ISCSI_PARAM_PERSISTENT_PORT:
2871 len = sprintf(buf, "%d\n", conn->persistent_port);
2872 break;
2873 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2874 len = sprintf(buf, "%s\n", conn->persistent_address);
2875 break;
2876 default:
2877 return -ENOSYS;
2878 }
2879
2880 return len;
2881}
2882EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2883
Mike Christie0801c242007-05-30 12:57:12 -05002884int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2885 char *buf)
2886{
Mike Christie75613522008-05-21 15:53:59 -05002887 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002888 int len;
2889
2890 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002891 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002892 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002893 len = sprintf(buf, "%s\n", "default");
2894 else
Mike Christie75613522008-05-21 15:53:59 -05002895 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002896 break;
Mike Christie0801c242007-05-30 12:57:12 -05002897 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002898 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002899 len = sprintf(buf, "%s\n", "default");
2900 else
Mike Christie75613522008-05-21 15:53:59 -05002901 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002902 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002903 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002904 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002905 len = sprintf(buf, "%s\n", "unknown");
2906 else
Mike Christie75613522008-05-21 15:53:59 -05002907 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002908 break;
Mike Christie75613522008-05-21 15:53:59 -05002909 case ISCSI_HOST_PARAM_IPADDRESS:
2910 if (!strlen(ihost->local_address))
2911 len = sprintf(buf, "%s\n", "unknown");
2912 else
2913 len = sprintf(buf, "%s\n",
2914 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002915 break;
Mike Christie0801c242007-05-30 12:57:12 -05002916 default:
2917 return -ENOSYS;
2918 }
2919
2920 return len;
2921}
2922EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2923
2924int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2925 char *buf, int buflen)
2926{
Mike Christie75613522008-05-21 15:53:59 -05002927 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002928
2929 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002930 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002931 if (!ihost->netdev)
2932 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002933 break;
Mike Christie0801c242007-05-30 12:57:12 -05002934 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002935 if (!ihost->hwaddress)
2936 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002937 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002938 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002939 if (!ihost->initiatorname)
2940 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002941 break;
Mike Christie0801c242007-05-30 12:57:12 -05002942 default:
2943 return -ENOSYS;
2944 }
2945
2946 return 0;
2947}
2948EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2949
Mike Christie7996a772006-04-06 21:13:41 -05002950MODULE_AUTHOR("Mike Christie");
2951MODULE_DESCRIPTION("iSCSI library functions");
2952MODULE_LICENSE("GPL");