blob: 67d0f3fc8ac001b9ac97a7a7bf566124cdf788c6 [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Erez Zilberbd2199d2009-06-15 22:11:10 -050041static int iscsi_dbg_lib_conn;
42module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int,
43 S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(debug_libiscsi_conn,
45 "Turn on debugging for connections in libiscsi module. "
46 "Set to 1 to turn on, and zero to turn off. Default is off.");
47
48static int iscsi_dbg_lib_session;
49module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int,
50 S_IRUGO | S_IWUSR);
51MODULE_PARM_DESC(debug_libiscsi_session,
52 "Turn on debugging for sessions in libiscsi module. "
53 "Set to 1 to turn on, and zero to turn off. Default is off.");
54
55static int iscsi_dbg_lib_eh;
56module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int,
57 S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(debug_libiscsi_eh,
59 "Turn on debugging for error handling in libiscsi module. "
60 "Set to 1 to turn on, and zero to turn off. Default is off.");
Mike Christie1b2c7af2009-03-05 14:45:58 -060061
62#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
63 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050064 if (iscsi_dbg_lib_conn) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060065 iscsi_conn_printk(KERN_INFO, _conn, \
66 "%s " dbg_fmt, \
67 __func__, ##arg); \
68 } while (0);
69
70#define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \
71 do { \
Erez Zilberbd2199d2009-06-15 22:11:10 -050072 if (iscsi_dbg_lib_session) \
73 iscsi_session_printk(KERN_INFO, _session, \
74 "%s " dbg_fmt, \
75 __func__, ##arg); \
76 } while (0);
77
78#define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \
79 do { \
80 if (iscsi_dbg_lib_eh) \
Mike Christie1b2c7af2009-03-05 14:45:58 -060081 iscsi_session_printk(KERN_INFO, _session, \
82 "%s " dbg_fmt, \
83 __func__, ##arg); \
84 } while (0);
85
Mike Christie77a23c22007-05-30 12:57:18 -050086/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
87#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050088
Mike Christie77a23c22007-05-30 12:57:18 -050089static int iscsi_sna_lt(u32 n1, u32 n2)
90{
91 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
92 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
93}
94
95/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
96static int iscsi_sna_lte(u32 n1, u32 n2)
97{
98 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
99 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
100}
101
Mike Christie32ae7632009-03-05 14:46:03 -0600102inline void iscsi_conn_queue_work(struct iscsi_conn *conn)
103{
104 struct Scsi_Host *shost = conn->session->host;
105 struct iscsi_host *ihost = shost_priv(shost);
106
Mike Christie1336aed2009-05-13 17:57:48 -0500107 if (ihost->workq)
108 queue_work(ihost->workq, &conn->xmitwork);
Mike Christie32ae7632009-03-05 14:46:03 -0600109}
110EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
111
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530112static void __iscsi_update_cmdsn(struct iscsi_session *session,
113 uint32_t exp_cmdsn, uint32_t max_cmdsn)
Mike Christie7996a772006-04-06 21:13:41 -0500114{
Mike Christie77a23c22007-05-30 12:57:18 -0500115 /*
116 * standard specifies this check for when to update expected and
117 * max sequence numbers
118 */
119 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
120 return;
121
122 if (exp_cmdsn != session->exp_cmdsn &&
123 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -0500124 session->exp_cmdsn = exp_cmdsn;
125
Mike Christie77a23c22007-05-30 12:57:18 -0500126 if (max_cmdsn != session->max_cmdsn &&
127 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
128 session->max_cmdsn = max_cmdsn;
129 /*
130 * if the window closed with IO queued, then kick the
131 * xmit thread
132 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500133 if (!list_empty(&session->leadconn->cmdqueue) ||
Mike Christie1336aed2009-05-13 17:57:48 -0500134 !list_empty(&session->leadconn->mgmtqueue))
135 iscsi_conn_queue_work(session->leadconn);
Mike Christie77a23c22007-05-30 12:57:18 -0500136 }
Mike Christie7996a772006-04-06 21:13:41 -0500137}
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530138
139void iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
140{
141 __iscsi_update_cmdsn(session, be32_to_cpu(hdr->exp_cmdsn),
142 be32_to_cpu(hdr->max_cmdsn));
143}
Mike Christie77a23c22007-05-30 12:57:18 -0500144EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -0500145
Mike Christie577577d2008-12-02 00:32:05 -0600146/**
147 * iscsi_prep_data_out_pdu - initialize Data-Out
148 * @task: scsi command task
149 * @r2t: R2T info
150 * @hdr: iscsi data in pdu
151 *
152 * Notes:
153 * Initialize Data-Out within this R2T sequence and finds
154 * proper data_offset within this SCSI command.
155 *
156 * This function is called with connection lock taken.
157 **/
158void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
159 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500160{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500161 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600162 unsigned int left = r2t->data_length - r2t->sent;
163
164 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500165
166 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600167 hdr->ttt = r2t->ttt;
168 hdr->datasn = cpu_to_be32(r2t->datasn);
169 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500170 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600171 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
172 hdr->itt = task->hdr_itt;
173 hdr->exp_statsn = r2t->exp_statsn;
174 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
175 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500176 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600177 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500178 hdr->flags = 0;
179 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600180 hton24(hdr->dlength, left);
181 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500182 hdr->flags = ISCSI_FLAG_CMD_FINAL;
183 }
Mike Christie577577d2008-12-02 00:32:05 -0600184 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500185}
Mike Christie577577d2008-12-02 00:32:05 -0600186EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500187
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600189{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500190 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600191
Mike Christie9c19a7d2008-05-21 15:54:09 -0500192 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600193 WARN_ON(1);
194 return -EINVAL;
195 }
196
197 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500198 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600199 return 0;
200}
201
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500202/*
203 * make an extended cdb AHS
204 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500205static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500206{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500207 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500208 unsigned rlen, pad_len;
209 unsigned short ahslength;
210 struct iscsi_ecdb_ahdr *ecdb_ahdr;
211 int rc;
212
Mike Christie9c19a7d2008-05-21 15:54:09 -0500213 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500214 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
215
216 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
217 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
218
219 pad_len = iscsi_padding(rlen);
220
Mike Christie9c19a7d2008-05-21 15:54:09 -0500221 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500222 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
223 if (rc)
224 return rc;
225
226 if (pad_len)
227 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
228
229 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
230 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
231 ecdb_ahdr->reserved = 0;
232 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
233
Mike Christie1b2c7af2009-03-05 14:45:58 -0600234 ISCSI_DBG_SESSION(task->conn->session,
235 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
236 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
237 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
238 task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500239 return 0;
240}
241
Mike Christie9c19a7d2008-05-21 15:54:09 -0500242static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500243{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500244 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500245 struct iscsi_rlength_ahdr *rlen_ahdr;
246 int rc;
247
Mike Christie9c19a7d2008-05-21 15:54:09 -0500248 rlen_ahdr = iscsi_next_hdr(task);
249 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500250 if (rc)
251 return rc;
252
253 rlen_ahdr->ahslength =
254 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
255 sizeof(rlen_ahdr->reserved));
256 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
257 rlen_ahdr->reserved = 0;
258 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
259
Mike Christie1b2c7af2009-03-05 14:45:58 -0600260 ISCSI_DBG_SESSION(task->conn->session,
261 "bidi-in rlen_ahdr->read_length(%d) "
262 "rlen_ahdr->ahslength(%d)\n",
263 be32_to_cpu(rlen_ahdr->read_length),
264 be16_to_cpu(rlen_ahdr->ahslength));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500265 return 0;
266}
267
Mike Christie7996a772006-04-06 21:13:41 -0500268/**
269 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500270 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500271 *
272 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
273 * fields like dlength or final based on how much data it sends
274 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500275static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500276{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500277 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500278 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500279 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600280 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500281 unsigned hdrlength, cmd_len;
Mike Christie262ef632008-12-02 00:32:13 -0600282 itt_t itt;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600283 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500284
Mike Christie184b57c2009-05-13 17:57:39 -0500285 if (conn->session->tt->alloc_pdu) {
286 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
287 if (rc)
288 return rc;
289 }
Mike Christie577577d2008-12-02 00:32:05 -0600290 hdr = (struct iscsi_cmd *) task->hdr;
Mike Christie262ef632008-12-02 00:32:13 -0600291 itt = hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600292 memset(hdr, 0, sizeof(*hdr));
293
Mike Christie2ff79d52008-12-02 00:32:14 -0600294 if (session->tt->parse_pdu_itt)
295 hdr->itt = task->hdr_itt = itt;
296 else
297 hdr->itt = task->hdr_itt = build_itt(task->itt,
298 task->conn->session->age);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500299 task->hdr_len = 0;
300 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600301 if (rc)
302 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600303 hdr->opcode = ISCSI_OP_SCSI_CMD;
304 hdr->flags = ISCSI_ATTR_SIMPLE;
305 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600306 memcpy(task->lun, hdr->lun, sizeof(task->lun));
Olaf Kircha8ac6312007-12-13 12:43:35 -0600307 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500308 cmd_len = sc->cmd_len;
309 if (cmd_len < ISCSI_CDB_SIZE)
310 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
311 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500312 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500313 if (rc)
314 return rc;
315 cmd_len = ISCSI_CDB_SIZE;
316 }
317 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500318
Mike Christie9c19a7d2008-05-21 15:54:09 -0500319 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500320 if (scsi_bidi_cmnd(sc)) {
321 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500322 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500323 if (rc)
324 return rc;
325 }
Mike Christie7996a772006-04-06 21:13:41 -0500326 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500327 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600328 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
329
Boaz Harroshc07d4442008-04-18 10:11:52 -0500330 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500331 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
332 /*
333 * Write counters:
334 *
335 * imm_count bytes to be sent right after
336 * SCSI PDU Header
337 *
338 * unsol_count bytes(as Data-Out) to be sent
339 * without R2T ack right after
340 * immediate data
341 *
Mike Christie577577d2008-12-02 00:32:05 -0600342 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500343 *
344 * pad_count bytes to be sent as zero-padding
345 */
Mike Christie577577d2008-12-02 00:32:05 -0600346 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500347
348 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500349 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500350 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500351 conn->max_xmit_dlength);
352 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500353 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500354 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500355 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500356 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600357 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500358
Mike Christieffd04362006-08-31 18:09:24 -0400359 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600360 r2t->data_length = min(session->first_burst, out_len) -
361 task->imm_count;
362 r2t->data_offset = task->imm_count;
363 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
364 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400365 }
366
Mike Christie577577d2008-12-02 00:32:05 -0600367 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500368 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600369 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500370 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500371 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
372 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500373 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500374
375 if (sc->sc_data_direction == DMA_FROM_DEVICE)
376 hdr->flags |= ISCSI_FLAG_CMD_READ;
377 }
378
Boaz Harrosh004d6532007-12-13 12:43:23 -0600379 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500380 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600381
382 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
383 hdrlength /= ISCSI_PAD_LEN;
384
385 WARN_ON(hdrlength >= 256);
386 hdr->hlength = hdrlength & 0xFF;
387
Mike Christie577577d2008-12-02 00:32:05 -0600388 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500389 return -EIO;
390
Mike Christie9c19a7d2008-05-21 15:54:09 -0500391 task->state = ISCSI_TASK_RUNNING;
Mike Christied3305f32009-08-20 15:10:58 -0500392 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
393 session->cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500394
Olaf Kircha8ac6312007-12-13 12:43:35 -0600395 conn->scsicmd_pdus_cnt++;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600396 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
397 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
398 scsi_bidi_cmnd(sc) ? "bidirectional" :
399 sc->sc_data_direction == DMA_TO_DEVICE ?
400 "write" : "read", conn->id, sc, sc->cmnd[0],
401 task->itt, scsi_bufflen(sc),
402 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
403 session->cmdsn,
404 session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600405 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500406}
Mike Christie7996a772006-04-06 21:13:41 -0500407
408/**
Mike Christie3bbaaad2009-05-13 17:57:46 -0500409 * iscsi_free_task - free a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500410 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500411 *
412 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500413 * This function returns the scsi command to scsi-ml or cleans
414 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500415 */
Mike Christie3bbaaad2009-05-13 17:57:46 -0500416static void iscsi_free_task(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500417{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500418 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600419 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500420 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500421
Mike Christie4421c9e2009-05-13 17:57:50 -0500422 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
423 task->itt, task->state, task->sc);
424
Mike Christie577577d2008-12-02 00:32:05 -0600425 session->tt->cleanup_task(task);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500426 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500427 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500428 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500429 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500430 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500431 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500432 return;
433
Mike Christie9c19a7d2008-05-21 15:54:09 -0500434 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500435
Mike Christie3e5c28a2008-05-21 15:54:06 -0500436 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500437 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500438 /* SCSI eh reuses commands to verify us */
439 sc->SCp.ptr = NULL;
440 /*
441 * queue command may call this to free the task, but
442 * not have setup the sc callback
443 */
444 if (sc->scsi_done)
445 sc->scsi_done(sc);
446 }
Mike Christie7996a772006-04-06 21:13:41 -0500447}
448
Mike Christie913e5bf2008-05-21 15:54:18 -0500449void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400450{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500451 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400452}
Mike Christie913e5bf2008-05-21 15:54:18 -0500453EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400454
Mike Christie9c19a7d2008-05-21 15:54:09 -0500455static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400456{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500457 if (atomic_dec_and_test(&task->refcount))
Mike Christie3bbaaad2009-05-13 17:57:46 -0500458 iscsi_free_task(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400459}
460
Mike Christie9c19a7d2008-05-21 15:54:09 -0500461void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500462{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500463 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500464
465 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500466 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500467 spin_unlock_bh(&session->lock);
468}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500469EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500470
Mike Christie3bbaaad2009-05-13 17:57:46 -0500471/**
472 * iscsi_complete_task - finish a task
473 * @task: iscsi cmd task
Mike Christieb3cd5052009-05-13 17:57:49 -0500474 * @state: state to complete task with
Mike Christie3bbaaad2009-05-13 17:57:46 -0500475 *
476 * Must be called with session lock.
Mike Christieb3a7ea82007-12-13 12:43:26 -0600477 */
Mike Christieb3cd5052009-05-13 17:57:49 -0500478static void iscsi_complete_task(struct iscsi_task *task, int state)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600479{
Mike Christie3bbaaad2009-05-13 17:57:46 -0500480 struct iscsi_conn *conn = task->conn;
481
Mike Christie4421c9e2009-05-13 17:57:50 -0500482 ISCSI_DBG_SESSION(conn->session,
483 "complete task itt 0x%x state %d sc %p\n",
484 task->itt, task->state, task->sc);
Mike Christieb3cd5052009-05-13 17:57:49 -0500485 if (task->state == ISCSI_TASK_COMPLETED ||
486 task->state == ISCSI_TASK_ABRT_TMF ||
487 task->state == ISCSI_TASK_ABRT_SESS_RECOV)
Mike Christie3bbaaad2009-05-13 17:57:46 -0500488 return;
489 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
Mike Christieb3cd5052009-05-13 17:57:49 -0500490 task->state = state;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500491
492 if (!list_empty(&task->running))
493 list_del_init(&task->running);
494
495 if (conn->task == task)
496 conn->task = NULL;
497
498 if (conn->ping_task == task)
499 conn->ping_task = NULL;
500
501 /* release get from queueing */
502 __iscsi_put_task(task);
503}
504
Mike Christie4c0ba5d2009-09-05 07:34:23 +0530505/**
506 * iscsi_complete_scsi_task - finish scsi task normally
507 * @task: iscsi task for scsi cmd
508 * @exp_cmdsn: expected cmd sn in cpu format
509 * @max_cmdsn: max cmd sn in cpu format
510 *
511 * This is used when drivers do not need or cannot perform
512 * lower level pdu processing.
513 *
514 * Called with session lock
515 */
516void iscsi_complete_scsi_task(struct iscsi_task *task,
517 uint32_t exp_cmdsn, uint32_t max_cmdsn)
518{
519 struct iscsi_conn *conn = task->conn;
520
521 ISCSI_DBG_SESSION(conn->session, "[itt 0x%x]\n", task->itt);
522
523 conn->last_recv = jiffies;
524 __iscsi_update_cmdsn(conn->session, exp_cmdsn, max_cmdsn);
525 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
526}
527EXPORT_SYMBOL_GPL(iscsi_complete_scsi_task);
528
529
Mike Christie3bbaaad2009-05-13 17:57:46 -0500530/*
531 * session lock must be held and if not called for a task that is
532 * still pending or from the xmit thread, then xmit thread must
533 * be suspended.
534 */
535static void fail_scsi_task(struct iscsi_task *task, int err)
536{
537 struct iscsi_conn *conn = task->conn;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600538 struct scsi_cmnd *sc;
Mike Christieb3cd5052009-05-13 17:57:49 -0500539 int state;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600540
Mike Christie3bbaaad2009-05-13 17:57:46 -0500541 /*
542 * if a command completes and we get a successful tmf response
543 * we will hit this because the scsi eh abort code does not take
544 * a ref to the task.
545 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500546 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600547 if (!sc)
548 return;
549
Mike Christieb3cd5052009-05-13 17:57:49 -0500550 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3a7ea82007-12-13 12:43:26 -0600551 /*
552 * cmd never made it to the xmit thread, so we should not count
553 * the cmd in the sequencing
554 */
555 conn->session->queued_cmdsn--;
Mike Christieb3cd5052009-05-13 17:57:49 -0500556 /* it was never sent so just complete like normal */
557 state = ISCSI_TASK_COMPLETED;
558 } else if (err == DID_TRANSPORT_DISRUPTED)
559 state = ISCSI_TASK_ABRT_SESS_RECOV;
560 else
561 state = ISCSI_TASK_ABRT_TMF;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600562
Mike Christieb3cd5052009-05-13 17:57:49 -0500563 sc->result = err << 16;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500564 if (!scsi_bidi_cmnd(sc))
565 scsi_set_resid(sc, scsi_bufflen(sc));
566 else {
567 scsi_out(sc)->resid = scsi_out(sc)->length;
568 scsi_in(sc)->resid = scsi_in(sc)->length;
569 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500570
Mike Christieb3cd5052009-05-13 17:57:49 -0500571 iscsi_complete_task(task, state);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600572}
573
Mike Christie3e5c28a2008-05-21 15:54:06 -0500574static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500575 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500576{
577 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600578 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500579 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
580
581 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
582 return -ENOTCONN;
583
584 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
585 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
586 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
587 /*
588 * pre-format CmdSN for outgoing PDU.
589 */
590 nop->cmdsn = cpu_to_be32(session->cmdsn);
591 if (hdr->itt != RESERVED_ITT) {
Mike Christie052d0142008-05-21 15:54:05 -0500592 /*
593 * TODO: We always use immediate, so we never hit this.
594 * If we start to send tmfs or nops as non-immediate then
595 * we should start checking the cmdsn numbers for mgmt tasks.
596 */
597 if (conn->c_stage == ISCSI_CONN_STARTED &&
598 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
599 session->queued_cmdsn++;
600 session->cmdsn++;
601 }
602 }
603
Mike Christieae15f802008-12-02 00:32:15 -0600604 if (session->tt->init_task && session->tt->init_task(task))
605 return -EIO;
Mike Christie052d0142008-05-21 15:54:05 -0500606
607 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
608 session->state = ISCSI_STATE_LOGGING_OUT;
609
Mike Christie577577d2008-12-02 00:32:05 -0600610 task->state = ISCSI_TASK_RUNNING;
Mike Christie1b2c7af2009-03-05 14:45:58 -0600611 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
612 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
613 hdr->itt, task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500614 return 0;
615}
616
Mike Christie9c19a7d2008-05-21 15:54:09 -0500617static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600618__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
619 char *data, uint32_t data_size)
620{
621 struct iscsi_session *session = conn->session;
Mike Christie1336aed2009-05-13 17:57:48 -0500622 struct iscsi_host *ihost = shost_priv(session->host);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500623 struct iscsi_task *task;
Mike Christie262ef632008-12-02 00:32:13 -0600624 itt_t itt;
Mike Christief6d51802007-12-13 12:43:30 -0600625
626 if (session->state == ISCSI_STATE_TERMINATE)
627 return NULL;
628
629 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
630 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
631 /*
632 * Login and Text are sent serially, in
633 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500634 * Same task can be used. Same ITT must be used.
635 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600636 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500637 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600638 else {
Mike Christie26013ad2009-05-13 17:57:43 -0500639 if (session->state != ISCSI_STATE_LOGGED_IN)
640 return NULL;
641
Mike Christief6d51802007-12-13 12:43:30 -0600642 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
643 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
644
Mike Christie3e5c28a2008-05-21 15:54:06 -0500645 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500646 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600647 return NULL;
648 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500649 /*
650 * released in complete pdu for task we expect a response for, and
651 * released by the lld when it has transmitted the task for
652 * pdus we do not expect a response for.
653 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500654 atomic_set(&task->refcount, 1);
655 task->conn = conn;
656 task->sc = NULL;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500657 INIT_LIST_HEAD(&task->running);
658 task->state = ISCSI_TASK_PENDING;
Mike Christief6d51802007-12-13 12:43:30 -0600659
660 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500661 memcpy(task->data, data, data_size);
662 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600663 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500664 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600665
Mike Christie184b57c2009-05-13 17:57:39 -0500666 if (conn->session->tt->alloc_pdu) {
667 if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
668 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
669 "pdu for mgmt task.\n");
Mike Christie3bbaaad2009-05-13 17:57:46 -0500670 goto free_task;
Mike Christie184b57c2009-05-13 17:57:39 -0500671 }
Mike Christie577577d2008-12-02 00:32:05 -0600672 }
Mike Christie184b57c2009-05-13 17:57:39 -0500673
Mike Christie262ef632008-12-02 00:32:13 -0600674 itt = task->hdr->itt;
Mike Christie577577d2008-12-02 00:32:05 -0600675 task->hdr_len = sizeof(struct iscsi_hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500676 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie262ef632008-12-02 00:32:13 -0600677
678 if (hdr->itt != RESERVED_ITT) {
679 if (session->tt->parse_pdu_itt)
680 task->hdr->itt = itt;
681 else
682 task->hdr->itt = build_itt(task->itt,
683 task->conn->session->age);
684 }
685
Mike Christie1336aed2009-05-13 17:57:48 -0500686 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -0600687 if (iscsi_prep_mgmt_task(conn, task))
688 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500689
Mike Christie9c19a7d2008-05-21 15:54:09 -0500690 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600691 goto free_task;
Mike Christie3bbaaad2009-05-13 17:57:46 -0500692 } else {
693 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie32ae7632009-03-05 14:46:03 -0600694 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -0500695 }
Mike Christie052d0142008-05-21 15:54:05 -0500696
Mike Christie9c19a7d2008-05-21 15:54:09 -0500697 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600698
699free_task:
700 __iscsi_put_task(task);
701 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600702}
703
704int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
705 char *data, uint32_t data_size)
706{
707 struct iscsi_conn *conn = cls_conn->dd_data;
708 struct iscsi_session *session = conn->session;
709 int err = 0;
710
711 spin_lock_bh(&session->lock);
712 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
713 err = -EPERM;
714 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600715 return err;
716}
717EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
718
Mike Christie7996a772006-04-06 21:13:41 -0500719/**
720 * iscsi_cmd_rsp - SCSI Command Response processing
721 * @conn: iscsi connection
722 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500723 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500724 * @data: cmd data buffer
725 * @datalen: len of buffer
726 *
727 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500728 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500729 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500730static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500731 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500732 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500733{
Mike Christie7996a772006-04-06 21:13:41 -0500734 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
735 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500736 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500737
Mike Christie77a23c22007-05-30 12:57:18 -0500738 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500739 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
740
741 sc->result = (DID_OK << 16) | rhdr->cmd_status;
742
743 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
744 sc->result = DID_ERROR << 16;
745 goto out;
746 }
747
748 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600749 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500750
751 if (datalen < 2) {
752invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600753 iscsi_conn_printk(KERN_ERR, conn,
754 "Got CHECK_CONDITION but invalid data "
755 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500756 sc->result = DID_BAD_TARGET << 16;
757 goto out;
758 }
759
Harvey Harrison8f3339912008-05-21 15:54:20 -0500760 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500761 if (datalen < senselen)
762 goto invalid_datalen;
763
764 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600765 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie1b2c7af2009-03-05 14:45:58 -0600766 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
767 min_t(uint16_t, senselen,
768 SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500769 }
770
Boaz Harroshc07d4442008-04-18 10:11:52 -0500771 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
772 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
773 int res_count = be32_to_cpu(rhdr->bi_residual_count);
774
775 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
776 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
777 res_count <= scsi_in(sc)->length))
778 scsi_in(sc)->resid = res_count;
779 else
780 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
781 }
782
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600783 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
784 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500785 int res_count = be32_to_cpu(rhdr->residual_count);
786
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600787 if (res_count > 0 &&
788 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
789 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500790 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900791 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500792 else
793 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500794 }
Mike Christie7996a772006-04-06 21:13:41 -0500795out:
Mike Christie3bbaaad2009-05-13 17:57:46 -0500796 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
Mike Christie1b2c7af2009-03-05 14:45:58 -0600797 sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500798 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500799 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -0500800}
801
Mike Christie1d9edf02008-09-24 11:46:09 -0500802/**
803 * iscsi_data_in_rsp - SCSI Data-In Response processing
804 * @conn: iscsi connection
805 * @hdr: iscsi pdu
806 * @task: scsi command task
807 **/
808static void
809iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
810 struct iscsi_task *task)
811{
812 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
813 struct scsi_cmnd *sc = task->sc;
814
815 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
816 return;
817
Mike Christieedbc9aa052009-05-13 17:57:42 -0500818 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
Mike Christie1d9edf02008-09-24 11:46:09 -0500819 sc->result = (DID_OK << 16) | rhdr->cmd_status;
820 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
821 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
822 ISCSI_FLAG_DATA_OVERFLOW)) {
823 int res_count = be32_to_cpu(rhdr->residual_count);
824
825 if (res_count > 0 &&
826 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
827 res_count <= scsi_in(sc)->length))
828 scsi_in(sc)->resid = res_count;
829 else
830 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
831 }
832
Mike Christie3bbaaad2009-05-13 17:57:46 -0500833 ISCSI_DBG_SESSION(conn->session, "data in with status done "
834 "[sc %p res %d itt 0x%x]\n",
835 sc, sc->result, task->itt);
Mike Christie1d9edf02008-09-24 11:46:09 -0500836 conn->scsirsp_pdus_cnt++;
Mike Christieb3cd5052009-05-13 17:57:49 -0500837 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie1d9edf02008-09-24 11:46:09 -0500838}
839
Mike Christie7ea8b822006-07-24 15:47:22 -0500840static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
841{
842 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
843
844 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
845 conn->tmfrsp_pdus_cnt++;
846
Mike Christie843c0a82007-12-13 12:43:20 -0600847 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500848 return;
849
850 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600851 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500852 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600853 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500854 else
Mike Christie843c0a82007-12-13 12:43:20 -0600855 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500856 wake_up(&conn->ehwait);
857}
858
Mike Christief6d51802007-12-13 12:43:30 -0600859static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
860{
861 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500862 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600863
Mike Christie9c19a7d2008-05-21 15:54:09 -0500864 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600865 return;
866
867 memset(&hdr, 0, sizeof(struct iscsi_nopout));
868 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
869 hdr.flags = ISCSI_FLAG_CMD_FINAL;
870
871 if (rhdr) {
872 memcpy(hdr.lun, rhdr->lun, 8);
873 hdr.ttt = rhdr->ttt;
874 hdr.itt = RESERVED_ITT;
875 } else
876 hdr.ttt = RESERVED_ITT;
877
Mike Christie9c19a7d2008-05-21 15:54:09 -0500878 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
879 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600880 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600881 else if (!rhdr) {
882 /* only track our nops */
883 conn->ping_task = task;
884 conn->last_ping = jiffies;
885 }
Mike Christief6d51802007-12-13 12:43:30 -0600886}
887
Mike Christie8afa1432009-08-20 15:10:59 -0500888static int iscsi_nop_out_rsp(struct iscsi_task *task,
889 struct iscsi_nopin *nop, char *data, int datalen)
890{
891 struct iscsi_conn *conn = task->conn;
892 int rc = 0;
893
894 if (conn->ping_task != task) {
895 /*
896 * If this is not in response to one of our
897 * nops then it must be from userspace.
898 */
899 if (iscsi_recv_pdu(conn->cls_conn, (struct iscsi_hdr *)nop,
900 data, datalen))
901 rc = ISCSI_ERR_CONN_FAILED;
902 } else
903 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
904 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
905 return rc;
906}
907
Mike Christie62f38302006-08-31 18:09:27 -0400908static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
909 char *data, int datalen)
910{
911 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
912 struct iscsi_hdr rejected_pdu;
Mike Christie8afa1432009-08-20 15:10:59 -0500913 int opcode, rc = 0;
Mike Christie62f38302006-08-31 18:09:27 -0400914
915 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
916
Mike Christie8afa1432009-08-20 15:10:59 -0500917 if (ntoh24(reject->dlength) > datalen ||
918 ntoh24(reject->dlength) < sizeof(struct iscsi_hdr)) {
919 iscsi_conn_printk(KERN_ERR, conn, "Cannot handle rejected "
920 "pdu. Invalid data length (pdu dlength "
921 "%u, datalen %d\n", ntoh24(reject->dlength),
922 datalen);
923 return ISCSI_ERR_PROTO;
Mike Christie62f38302006-08-31 18:09:27 -0400924 }
Mike Christie8afa1432009-08-20 15:10:59 -0500925 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
926 opcode = rejected_pdu.opcode & ISCSI_OPCODE_MASK;
927
928 switch (reject->reason) {
929 case ISCSI_REASON_DATA_DIGEST_ERROR:
930 iscsi_conn_printk(KERN_ERR, conn,
931 "pdu (op 0x%x itt 0x%x) rejected "
932 "due to DataDigest error.\n",
933 rejected_pdu.itt, opcode);
934 break;
935 case ISCSI_REASON_IMM_CMD_REJECT:
936 iscsi_conn_printk(KERN_ERR, conn,
937 "pdu (op 0x%x itt 0x%x) rejected. Too many "
938 "immediate commands.\n",
939 rejected_pdu.itt, opcode);
940 /*
941 * We only send one TMF at a time so if the target could not
942 * handle it, then it should get fixed (RFC mandates that
943 * a target can handle one immediate TMF per conn).
944 *
945 * For nops-outs, we could have sent more than one if
946 * the target is sending us lots of nop-ins
947 */
948 if (opcode != ISCSI_OP_NOOP_OUT)
949 return 0;
950
951 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG))
952 /*
953 * nop-out in response to target's nop-out rejected.
954 * Just resend.
955 */
956 iscsi_send_nopout(conn,
957 (struct iscsi_nopin*)&rejected_pdu);
958 else {
959 struct iscsi_task *task;
960 /*
961 * Our nop as ping got dropped. We know the target
962 * and transport are ok so just clean up
963 */
964 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
965 if (!task) {
966 iscsi_conn_printk(KERN_ERR, conn,
967 "Invalid pdu reject. Could "
968 "not lookup rejected task.\n");
969 rc = ISCSI_ERR_BAD_ITT;
970 } else
971 rc = iscsi_nop_out_rsp(task,
972 (struct iscsi_nopin*)&rejected_pdu,
973 NULL, 0);
974 }
975 break;
976 default:
977 iscsi_conn_printk(KERN_ERR, conn,
978 "pdu (op 0x%x itt 0x%x) rejected. Reason "
979 "code 0x%x\n", rejected_pdu.itt,
980 rejected_pdu.opcode, reject->reason);
981 break;
982 }
983 return rc;
Mike Christie62f38302006-08-31 18:09:27 -0400984}
985
Mike Christie7996a772006-04-06 21:13:41 -0500986/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500987 * iscsi_itt_to_task - look up task by itt
988 * @conn: iscsi connection
989 * @itt: itt
990 *
991 * This should be used for mgmt tasks like login and nops, or if
992 * the LDD's itt space does not include the session age.
993 *
994 * The session lock must be held.
995 */
Mike Christie8f9256c2009-05-13 17:57:41 -0500996struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
Mike Christie913e5bf2008-05-21 15:54:18 -0500997{
998 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -0600999 int i;
Mike Christie913e5bf2008-05-21 15:54:18 -05001000
1001 if (itt == RESERVED_ITT)
1002 return NULL;
1003
Mike Christie262ef632008-12-02 00:32:13 -06001004 if (session->tt->parse_pdu_itt)
1005 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1006 else
1007 i = get_itt(itt);
Mike Christie913e5bf2008-05-21 15:54:18 -05001008 if (i >= session->cmds_max)
1009 return NULL;
1010
1011 return session->cmds[i];
1012}
Mike Christie8f9256c2009-05-13 17:57:41 -05001013EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
Mike Christie913e5bf2008-05-21 15:54:18 -05001014
1015/**
Mike Christie7996a772006-04-06 21:13:41 -05001016 * __iscsi_complete_pdu - complete pdu
1017 * @conn: iscsi conn
1018 * @hdr: iscsi header
1019 * @data: data buffer
1020 * @datalen: len of data buffer
1021 *
1022 * Completes pdu processing by freeing any resources allocated at
1023 * queuecommand or send generic. session lock must be held and verify
1024 * itt must have been called.
1025 */
Mike Christie913e5bf2008-05-21 15:54:18 -05001026int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1027 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -05001028{
1029 struct iscsi_session *session = conn->session;
1030 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001031 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001032 uint32_t itt;
1033
Mike Christief6d51802007-12-13 12:43:30 -06001034 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -05001035 rc = iscsi_verify_itt(conn, hdr->itt);
1036 if (rc)
1037 return rc;
1038
Al Virob4377352007-02-09 16:39:40 +00001039 if (hdr->itt != RESERVED_ITT)
1040 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001041 else
Al Virob4377352007-02-09 16:39:40 +00001042 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -05001043
Mike Christie1b2c7af2009-03-05 14:45:58 -06001044 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1045 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -05001046
Mike Christie3e5c28a2008-05-21 15:54:06 -05001047 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -05001048 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -04001049
Mike Christie7996a772006-04-06 21:13:41 -05001050 switch(opcode) {
1051 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -05001052 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -05001053 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -05001054 break;
1055 }
1056
Al Virob4377352007-02-09 16:39:40 +00001057 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -05001058 break;
1059
Mike Christief6d51802007-12-13 12:43:30 -06001060 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -05001061 break;
1062 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -04001063 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1064 break;
Mike Christie7996a772006-04-06 21:13:41 -05001065 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -05001066 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -04001067 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1068 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -05001069 break;
1070 default:
1071 rc = ISCSI_ERR_BAD_OPCODE;
1072 break;
1073 }
Mike Christie3e5c28a2008-05-21 15:54:06 -05001074 goto out;
1075 }
Mike Christie7996a772006-04-06 21:13:41 -05001076
Mike Christie3e5c28a2008-05-21 15:54:06 -05001077 switch(opcode) {
1078 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -05001079 case ISCSI_OP_SCSI_DATA_IN:
1080 task = iscsi_itt_to_ctask(conn, hdr->itt);
1081 if (!task)
1082 return ISCSI_ERR_BAD_ITT;
Mike Christied355e572009-06-15 22:11:08 -05001083 task->last_xfer = jiffies;
Mike Christie913e5bf2008-05-21 15:54:18 -05001084 break;
1085 case ISCSI_OP_R2T:
1086 /*
1087 * LLD handles R2Ts if they need to.
1088 */
1089 return 0;
1090 case ISCSI_OP_LOGOUT_RSP:
1091 case ISCSI_OP_LOGIN_RSP:
1092 case ISCSI_OP_TEXT_RSP:
1093 case ISCSI_OP_SCSI_TMFUNC_RSP:
1094 case ISCSI_OP_NOOP_IN:
1095 task = iscsi_itt_to_task(conn, hdr->itt);
1096 if (!task)
1097 return ISCSI_ERR_BAD_ITT;
1098 break;
1099 default:
1100 return ISCSI_ERR_BAD_OPCODE;
1101 }
1102
1103 switch(opcode) {
1104 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001105 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001106 break;
1107 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -05001108 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001109 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -05001110 case ISCSI_OP_LOGOUT_RSP:
1111 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1112 if (datalen) {
1113 rc = ISCSI_ERR_PROTO;
1114 break;
1115 }
1116 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1117 goto recv_pdu;
1118 case ISCSI_OP_LOGIN_RSP:
1119 case ISCSI_OP_TEXT_RSP:
1120 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1121 /*
1122 * login related PDU's exp_statsn is handled in
1123 * userspace
1124 */
1125 goto recv_pdu;
1126 case ISCSI_OP_SCSI_TMFUNC_RSP:
1127 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1128 if (datalen) {
1129 rc = ISCSI_ERR_PROTO;
1130 break;
1131 }
1132
1133 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3cd5052009-05-13 17:57:49 -05001134 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001135 break;
1136 case ISCSI_OP_NOOP_IN:
1137 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1138 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
1139 rc = ISCSI_ERR_PROTO;
1140 break;
1141 }
1142 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1143
Mike Christie8afa1432009-08-20 15:10:59 -05001144 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1145 data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -05001146 break;
1147 default:
1148 rc = ISCSI_ERR_BAD_OPCODE;
1149 break;
1150 }
1151
1152out:
1153 return rc;
1154recv_pdu:
1155 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1156 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3cd5052009-05-13 17:57:49 -05001157 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001158 return rc;
1159}
Mike Christie913e5bf2008-05-21 15:54:18 -05001160EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -05001161
1162int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1163 char *data, int datalen)
1164{
1165 int rc;
1166
1167 spin_lock(&conn->session->lock);
1168 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1169 spin_unlock(&conn->session->lock);
1170 return rc;
1171}
1172EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1173
Mike Christie0af967f2008-05-21 15:54:04 -05001174int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -05001175{
1176 struct iscsi_session *session = conn->session;
Mike Christie262ef632008-12-02 00:32:13 -06001177 int age = 0, i = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001178
Mike Christie0af967f2008-05-21 15:54:04 -05001179 if (itt == RESERVED_ITT)
1180 return 0;
Mike Christie7996a772006-04-06 21:13:41 -05001181
Mike Christie262ef632008-12-02 00:32:13 -06001182 if (session->tt->parse_pdu_itt)
1183 session->tt->parse_pdu_itt(conn, itt, &i, &age);
1184 else {
1185 i = get_itt(itt);
1186 age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
1187 }
1188
1189 if (age != session->age) {
Mike Christie0af967f2008-05-21 15:54:04 -05001190 iscsi_conn_printk(KERN_ERR, conn,
1191 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -05001192 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001193 return ISCSI_ERR_BAD_ITT;
1194 }
Mike Christie7996a772006-04-06 21:13:41 -05001195
Mike Christie3e5c28a2008-05-21 15:54:06 -05001196 if (i >= session->cmds_max) {
1197 iscsi_conn_printk(KERN_ERR, conn,
1198 "received invalid itt index %u (max cmds "
1199 "%u.\n", i, session->cmds_max);
1200 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -05001201 }
Mike Christie7996a772006-04-06 21:13:41 -05001202 return 0;
1203}
1204EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1205
Mike Christie913e5bf2008-05-21 15:54:18 -05001206/**
1207 * iscsi_itt_to_ctask - look up ctask by itt
1208 * @conn: iscsi connection
1209 * @itt: itt
1210 *
1211 * This should be used for cmd tasks.
1212 *
1213 * The session lock must be held.
1214 */
1215struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -05001216{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001217 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -05001218
1219 if (iscsi_verify_itt(conn, itt))
1220 return NULL;
1221
Mike Christie913e5bf2008-05-21 15:54:18 -05001222 task = iscsi_itt_to_task(conn, itt);
1223 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001224 return NULL;
1225
Mike Christie913e5bf2008-05-21 15:54:18 -05001226 if (task->sc->SCp.phase != conn->session->age) {
1227 iscsi_session_printk(KERN_ERR, conn->session,
1228 "task's session age %d, expected %d\n",
1229 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001230 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001231 }
Mike Christie0af967f2008-05-21 15:54:04 -05001232
Mike Christie9c19a7d2008-05-21 15:54:09 -05001233 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001234}
1235EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1236
Mike Christie40a06e72009-03-05 14:46:05 -06001237void iscsi_session_failure(struct iscsi_session *session,
Mike Christiee5bd7b52008-09-24 11:46:10 -05001238 enum iscsi_err err)
1239{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001240 struct iscsi_conn *conn;
1241 struct device *dev;
1242 unsigned long flags;
1243
1244 spin_lock_irqsave(&session->lock, flags);
1245 conn = session->leadconn;
1246 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1247 spin_unlock_irqrestore(&session->lock, flags);
1248 return;
1249 }
1250
1251 dev = get_device(&conn->cls_conn->dev);
1252 spin_unlock_irqrestore(&session->lock, flags);
1253 if (!dev)
1254 return;
1255 /*
1256 * if the host is being removed bypass the connection
1257 * recovery initialization because we are going to kill
1258 * the session.
1259 */
1260 if (err == ISCSI_ERR_INVALID_HOST)
1261 iscsi_conn_error_event(conn->cls_conn, err);
1262 else
1263 iscsi_conn_failure(conn, err);
1264 put_device(dev);
1265}
1266EXPORT_SYMBOL_GPL(iscsi_session_failure);
1267
Mike Christie7996a772006-04-06 21:13:41 -05001268void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1269{
1270 struct iscsi_session *session = conn->session;
1271 unsigned long flags;
1272
1273 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001274 if (session->state == ISCSI_STATE_FAILED) {
1275 spin_unlock_irqrestore(&session->lock, flags);
1276 return;
1277 }
1278
Mike Christie67a61112006-05-30 00:37:20 -05001279 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001280 session->state = ISCSI_STATE_FAILED;
1281 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001282
Mike Christie7996a772006-04-06 21:13:41 -05001283 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1284 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001285 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001286}
1287EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1288
Mike Christie77a23c22007-05-30 12:57:18 -05001289static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1290{
1291 struct iscsi_session *session = conn->session;
1292
1293 /*
1294 * Check for iSCSI window and take care of CmdSN wrap-around
1295 */
Mike Christiee0726402007-07-26 12:46:48 -05001296 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001297 ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn "
1298 "%u MaxCmdSN %u CmdSN %u/%u\n",
1299 session->exp_cmdsn, session->max_cmdsn,
1300 session->cmdsn, session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001301 return -ENOSPC;
1302 }
1303 return 0;
1304}
1305
Mike Christie9c19a7d2008-05-21 15:54:09 -05001306static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001307{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001308 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001309 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001310
Mike Christie70b31c12009-08-20 15:11:03 -05001311 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1312 return -ENODATA;
1313
Mike Christie9c19a7d2008-05-21 15:54:09 -05001314 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001315 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001316 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001317 spin_lock_bh(&conn->session->lock);
Mike Christied355e572009-06-15 22:11:08 -05001318 if (!rc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001319 /* done with this task */
Mike Christied355e572009-06-15 22:11:08 -05001320 task->last_xfer = jiffies;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001321 conn->task = NULL;
Mike Christied355e572009-06-15 22:11:08 -05001322 }
1323 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001324 return rc;
1325}
1326
Mike Christie7996a772006-04-06 21:13:41 -05001327/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001328 * iscsi_requeue_task - requeue task to run from session workqueue
1329 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001330 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001331 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001332 * this. The session lock must be held. This should only be called
1333 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001334 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001335void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001336{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001337 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001338
Mike Christie3bbaaad2009-05-13 17:57:46 -05001339 /*
1340 * this may be on the requeue list already if the xmit_task callout
1341 * is handling the r2ts while we are adding new ones
1342 */
1343 if (list_empty(&task->running))
1344 list_add_tail(&task->running, &conn->requeue);
Mike Christie32ae7632009-03-05 14:46:03 -06001345 iscsi_conn_queue_work(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001346}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001347EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001348
1349/**
Mike Christie7996a772006-04-06 21:13:41 -05001350 * iscsi_data_xmit - xmit any command into the scheduled connection
1351 * @conn: iscsi connection
1352 *
1353 * Notes:
1354 * The function can return -EAGAIN in which case the caller must
1355 * re-schedule it again later or recover. '0' return code means
1356 * successful xmit.
1357 **/
1358static int iscsi_data_xmit(struct iscsi_conn *conn)
1359{
Mike Christie3219e522006-05-30 00:37:28 -05001360 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001361
Mike Christie77a23c22007-05-30 12:57:18 -05001362 spin_lock_bh(&conn->session->lock);
Mike Christie70b31c12009-08-20 15:11:03 -05001363 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06001364 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001365 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001366 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001367 }
Mike Christie7996a772006-04-06 21:13:41 -05001368
Mike Christie9c19a7d2008-05-21 15:54:09 -05001369 if (conn->task) {
1370 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001371 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001372 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001373 }
1374
Mike Christie77a23c22007-05-30 12:57:18 -05001375 /*
1376 * process mgmt pdus like nops before commands since we should
1377 * only have one nop-out as a ping from us and targets should not
1378 * overflow us with nop-ins
1379 */
1380check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001381 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001382 conn->task = list_entry(conn->mgmtqueue.next,
1383 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001384 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001385 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1386 __iscsi_put_task(conn->task);
1387 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001388 continue;
1389 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001390 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001391 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001392 goto done;
Mike Christie7996a772006-04-06 21:13:41 -05001393 }
1394
Mike Christie843c0a82007-12-13 12:43:20 -06001395 /* process pending command queue */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001396 while (!list_empty(&conn->cmdqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001397 if (conn->tmf_state == TMF_QUEUED)
1398 break;
1399
Mike Christie3bbaaad2009-05-13 17:57:46 -05001400 conn->task = list_entry(conn->cmdqueue.next,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001401 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001402 list_del_init(&conn->task->running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001403 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christieb3cd5052009-05-13 17:57:49 -05001404 fail_scsi_task(conn->task, DID_IMM_RETRY);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001405 continue;
1406 }
Mike Christie577577d2008-12-02 00:32:05 -06001407 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1408 if (rc) {
1409 if (rc == -ENOMEM) {
Mike Christie3bbaaad2009-05-13 17:57:46 -05001410 list_add_tail(&conn->task->running,
1411 &conn->cmdqueue);
Mike Christie577577d2008-12-02 00:32:05 -06001412 conn->task = NULL;
Mike Christie70b31c12009-08-20 15:11:03 -05001413 goto done;
Mike Christie577577d2008-12-02 00:32:05 -06001414 } else
Mike Christieb3cd5052009-05-13 17:57:49 -05001415 fail_scsi_task(conn->task, DID_ABORT);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001416 continue;
1417 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001418 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001419 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001420 goto done;
Mike Christie77a23c22007-05-30 12:57:18 -05001421 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001422 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001423 * we need to check the mgmt queue for nops that need to
1424 * be sent to aviod starvation
1425 */
Mike Christie843c0a82007-12-13 12:43:20 -06001426 if (!list_empty(&conn->mgmtqueue))
1427 goto check_mgmt;
1428 }
1429
1430 while (!list_empty(&conn->requeue)) {
1431 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1432 break;
1433
Mike Christieb3a7ea82007-12-13 12:43:26 -06001434 /*
1435 * we always do fastlogout - conn stop code will clean up.
1436 */
1437 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1438 break;
1439
Mike Christie9c19a7d2008-05-21 15:54:09 -05001440 conn->task = list_entry(conn->requeue.next,
1441 struct iscsi_task, running);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001442 list_del_init(&conn->task->running);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001443 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001444 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001445 if (rc)
Mike Christie70b31c12009-08-20 15:11:03 -05001446 goto done;
Mike Christie843c0a82007-12-13 12:43:20 -06001447 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001448 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001449 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001450 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001451 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001452
Mike Christie70b31c12009-08-20 15:11:03 -05001453done:
Mike Christie77a23c22007-05-30 12:57:18 -05001454 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001455 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001456}
1457
David Howellsc4028952006-11-22 14:57:56 +00001458static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001459{
David Howellsc4028952006-11-22 14:57:56 +00001460 struct iscsi_conn *conn =
1461 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001462 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001463 /*
1464 * serialize Xmit worker on a per-connection basis.
1465 */
Mike Christie3219e522006-05-30 00:37:28 -05001466 do {
1467 rc = iscsi_data_xmit(conn);
1468 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001469}
1470
Mike Christie577577d2008-12-02 00:32:05 -06001471static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1472 struct scsi_cmnd *sc)
1473{
1474 struct iscsi_task *task;
1475
1476 if (!__kfifo_get(conn->session->cmdpool.queue,
1477 (void *) &task, sizeof(void *)))
1478 return NULL;
1479
1480 sc->SCp.phase = conn->session->age;
1481 sc->SCp.ptr = (char *) task;
1482
1483 atomic_set(&task->refcount, 1);
1484 task->state = ISCSI_TASK_PENDING;
1485 task->conn = conn;
1486 task->sc = sc;
Mike Christied355e572009-06-15 22:11:08 -05001487 task->have_checked_conn = false;
1488 task->last_timeout = jiffies;
1489 task->last_xfer = jiffies;
Mike Christie577577d2008-12-02 00:32:05 -06001490 INIT_LIST_HEAD(&task->running);
1491 return task;
1492}
1493
Mike Christie7996a772006-04-06 21:13:41 -05001494enum {
1495 FAILURE_BAD_HOST = 1,
1496 FAILURE_SESSION_FAILED,
1497 FAILURE_SESSION_FREED,
1498 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001499 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001500 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001501 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001502 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001503 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001504 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001505};
1506
1507int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1508{
Mike Christie75613522008-05-21 15:53:59 -05001509 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001510 struct Scsi_Host *host;
Mike Christie1336aed2009-05-13 17:57:48 -05001511 struct iscsi_host *ihost;
Mike Christie7996a772006-04-06 21:13:41 -05001512 int reason = 0;
1513 struct iscsi_session *session;
1514 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001515 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001516
1517 sc->scsi_done = done;
1518 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001519 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001520
1521 host = sc->device->host;
Mike Christie1336aed2009-05-13 17:57:48 -05001522 ihost = shost_priv(host);
Mike Christie1040c992007-12-13 12:43:34 -06001523 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001524
Mike Christie75613522008-05-21 15:53:59 -05001525 cls_session = starget_to_session(scsi_target(sc->device));
1526 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001527 spin_lock(&session->lock);
1528
Mike Christie75613522008-05-21 15:53:59 -05001529 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001530 if (reason) {
1531 sc->result = reason;
1532 goto fault;
1533 }
1534
Mike Christie301e0f72009-05-13 17:57:47 -05001535 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001536 /*
1537 * to handle the race between when we set the recovery state
1538 * and block the session we requeue here (commands could
1539 * be entering our queuecommand while a block is starting
1540 * up because the block code is not locked)
1541 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001542 switch (session->state) {
Mike Christie301e0f72009-05-13 17:57:47 -05001543 case ISCSI_STATE_FAILED:
Mike Christie9000bcd2007-12-13 12:43:32 -06001544 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001545 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie301e0f72009-05-13 17:57:47 -05001546 sc->result = DID_IMM_RETRY << 16;
1547 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001548 case ISCSI_STATE_LOGGING_OUT:
1549 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie301e0f72009-05-13 17:57:47 -05001550 sc->result = DID_IMM_RETRY << 16;
1551 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001552 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001553 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001554 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001555 break;
1556 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001557 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001558 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001559 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001560 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001561 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001562 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001563 }
Mike Christie7996a772006-04-06 21:13:41 -05001564 goto fault;
1565 }
1566
Mike Christie7996a772006-04-06 21:13:41 -05001567 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001568 if (!conn) {
1569 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001570 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001571 goto fault;
1572 }
Mike Christie7996a772006-04-06 21:13:41 -05001573
Mike Christie661134a2009-09-05 07:35:33 +05301574 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1575 reason = FAILURE_SESSION_IN_RECOVERY;
1576 sc->result = DID_REQUEUE;
1577 goto fault;
1578 }
1579
Mike Christie77a23c22007-05-30 12:57:18 -05001580 if (iscsi_check_cmdsn_window_closed(conn)) {
1581 reason = FAILURE_WINDOW_CLOSED;
1582 goto reject;
1583 }
1584
Mike Christie577577d2008-12-02 00:32:05 -06001585 task = iscsi_alloc_task(conn, sc);
1586 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001587 reason = FAILURE_OOM;
1588 goto reject;
1589 }
Mike Christie7996a772006-04-06 21:13:41 -05001590
Mike Christie1336aed2009-05-13 17:57:48 -05001591 if (!ihost->workq) {
Mike Christie577577d2008-12-02 00:32:05 -06001592 reason = iscsi_prep_scsi_cmd_pdu(task);
1593 if (reason) {
1594 if (reason == -ENOMEM) {
1595 reason = FAILURE_OOM;
1596 goto prepd_reject;
1597 } else {
1598 sc->result = DID_ABORT << 16;
1599 goto prepd_fault;
1600 }
Mike Christie052d0142008-05-21 15:54:05 -05001601 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001602 if (session->tt->xmit_task(task)) {
Mike Christied3305f32009-08-20 15:10:58 -05001603 session->cmdsn--;
Mike Christie052d0142008-05-21 15:54:05 -05001604 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001605 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001606 }
Mike Christie3bbaaad2009-05-13 17:57:46 -05001607 } else {
1608 list_add_tail(&task->running, &conn->cmdqueue);
Mike Christie32ae7632009-03-05 14:46:03 -06001609 iscsi_conn_queue_work(conn);
Mike Christie3bbaaad2009-05-13 17:57:46 -05001610 }
Mike Christie052d0142008-05-21 15:54:05 -05001611
1612 session->queued_cmdsn++;
1613 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001614 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001615 return 0;
1616
Mike Christie577577d2008-12-02 00:32:05 -06001617prepd_reject:
1618 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001619 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001620reject:
1621 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001622 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1623 sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001624 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001625 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001626
Mike Christie577577d2008-12-02 00:32:05 -06001627prepd_fault:
1628 sc->scsi_done = NULL;
Mike Christieb3cd5052009-05-13 17:57:49 -05001629 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
Mike Christie7996a772006-04-06 21:13:41 -05001630fault:
1631 spin_unlock(&session->lock);
Mike Christie1b2c7af2009-03-05 14:45:58 -06001632 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1633 sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001634 if (!scsi_bidi_cmnd(sc))
1635 scsi_set_resid(sc, scsi_bufflen(sc));
1636 else {
1637 scsi_out(sc)->resid = scsi_out(sc)->length;
1638 scsi_in(sc)->resid = scsi_in(sc)->length;
1639 }
Mike Christie052d0142008-05-21 15:54:05 -05001640 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001641 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001642 return 0;
1643}
1644EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1645
Mike Christiee881a172009-10-15 17:46:39 -07001646int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
Mike Christie7996a772006-04-06 21:13:41 -05001647{
Mike Christiee881a172009-10-15 17:46:39 -07001648 if (reason != SCSI_QDEPTH_DEFAULT)
1649 return -EOPNOTSUPP;
1650
Mike Christie7996a772006-04-06 21:13:41 -05001651 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1652 return sdev->queue_depth;
1653}
1654EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1655
Mike Christie6b5d6c42009-04-21 15:32:32 -05001656int iscsi_target_alloc(struct scsi_target *starget)
1657{
1658 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1659 struct iscsi_session *session = cls_session->dd_data;
1660
1661 starget->can_queue = session->scsi_cmds_max;
1662 return 0;
1663}
1664EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1665
Mike Christie7996a772006-04-06 21:13:41 -05001666void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1667{
Mike Christie75613522008-05-21 15:53:59 -05001668 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001669
1670 spin_lock_bh(&session->lock);
1671 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001672 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001673 if (session->leadconn)
1674 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001675 }
1676 spin_unlock_bh(&session->lock);
1677}
1678EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1679
Mike Christie8e124522008-09-24 11:46:12 -05001680int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001681{
Mike Christie75613522008-05-21 15:53:59 -05001682 struct iscsi_cls_session *cls_session;
1683 struct iscsi_session *session;
1684 struct iscsi_conn *conn;
1685
1686 cls_session = starget_to_session(scsi_target(sc->device));
1687 session = cls_session->dd_data;
1688 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001689
Mike Christiebc436b22007-12-13 12:43:28 -06001690 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001691 spin_lock_bh(&session->lock);
1692 if (session->state == ISCSI_STATE_TERMINATE) {
1693failed:
Erez Zilberbd2199d2009-06-15 22:11:10 -05001694 ISCSI_DBG_EH(session,
1695 "failing target reset: Could not log back into "
1696 "target [age %d]\n",
1697 session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001698 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001699 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001700 return FAILED;
1701 }
1702
Mike Christie7996a772006-04-06 21:13:41 -05001703 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001704 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001705 /*
1706 * we drop the lock here but the leadconn cannot be destoyed while
1707 * we are in the scsi eh
1708 */
Mike Christie843c0a82007-12-13 12:43:20 -06001709 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001710
Erez Zilberbd2199d2009-06-15 22:11:10 -05001711 ISCSI_DBG_EH(session, "wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001712 wait_event_interruptible(conn->ehwait,
1713 session->state == ISCSI_STATE_TERMINATE ||
1714 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001715 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001716 if (signal_pending(current))
1717 flush_signals(current);
1718
Mike Christiebc436b22007-12-13 12:43:28 -06001719 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001720 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001721 if (session->state == ISCSI_STATE_LOGGED_IN) {
1722 ISCSI_DBG_EH(session,
1723 "target reset succeeded\n");
1724 } else
Mike Christie7996a772006-04-06 21:13:41 -05001725 goto failed;
1726 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001727 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001728 return SUCCESS;
1729}
Mike Christie8e124522008-09-24 11:46:12 -05001730EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001731
Mike Christie843c0a82007-12-13 12:43:20 -06001732static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001733{
Mike Christie843c0a82007-12-13 12:43:20 -06001734 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001735 struct iscsi_session *session = conn->session;
1736
1737 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001738 if (conn->tmf_state == TMF_QUEUED) {
1739 conn->tmf_state = TMF_TIMEDOUT;
Erez Zilberbd2199d2009-06-15 22:11:10 -05001740 ISCSI_DBG_EH(session, "tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001741 /* unblock eh_abort() */
1742 wake_up(&conn->ehwait);
1743 }
1744 spin_unlock(&session->lock);
1745}
1746
Mike Christie9c19a7d2008-05-21 15:54:09 -05001747static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001748 struct iscsi_tm *hdr, int age,
1749 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001750{
Mike Christie7996a772006-04-06 21:13:41 -05001751 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001752 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001753
Mike Christie9c19a7d2008-05-21 15:54:09 -05001754 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001755 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001756 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001757 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001758 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001759 spin_lock_bh(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001760 ISCSI_DBG_EH(session, "tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001761 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001762 }
Mike Christie843c0a82007-12-13 12:43:20 -06001763 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001764 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001765 conn->tmf_timer.function = iscsi_tmf_timedout;
1766 conn->tmf_timer.data = (unsigned long)conn;
1767 add_timer(&conn->tmf_timer);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001768 ISCSI_DBG_EH(session, "tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001769
Mike Christie7996a772006-04-06 21:13:41 -05001770 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001771 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001772
1773 /*
1774 * block eh thread until:
1775 *
Mike Christie843c0a82007-12-13 12:43:20 -06001776 * 1) tmf response
1777 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001778 * 3) session is terminated or restarted or userspace has
1779 * given up on recovery
1780 */
Mike Christie843c0a82007-12-13 12:43:20 -06001781 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001782 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001783 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001784 if (signal_pending(current))
1785 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001786 del_timer_sync(&conn->tmf_timer);
1787
Mike Christie6724add2007-08-15 01:38:30 -05001788 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001789 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001790 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001791 if (age != session->age ||
1792 session->state != ISCSI_STATE_LOGGED_IN)
1793 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001794 return 0;
1795}
1796
1797/*
Mike Christie843c0a82007-12-13 12:43:20 -06001798 * Fail commands. session lock held and recv side suspended and xmit
1799 * thread flushed
1800 */
Mike Christie3bbaaad2009-05-13 17:57:46 -05001801static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1802 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001803{
Mike Christie3bbaaad2009-05-13 17:57:46 -05001804 struct iscsi_task *task;
1805 int i;
Mike Christie843c0a82007-12-13 12:43:20 -06001806
Mike Christie3bbaaad2009-05-13 17:57:46 -05001807 for (i = 0; i < conn->session->cmds_max; i++) {
1808 task = conn->session->cmds[i];
1809 if (!task->sc || task->state == ISCSI_TASK_FREE)
1810 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001811
Mike Christie3bbaaad2009-05-13 17:57:46 -05001812 if (lun != -1 && lun != task->sc->device->lun)
1813 continue;
Mike Christie843c0a82007-12-13 12:43:20 -06001814
Mike Christie3bbaaad2009-05-13 17:57:46 -05001815 ISCSI_DBG_SESSION(conn->session,
1816 "failing sc %p itt 0x%x state %d\n",
1817 task->sc, task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05001818 fail_scsi_task(task, error);
Mike Christie843c0a82007-12-13 12:43:20 -06001819 }
1820}
1821
Mike Christie661134a2009-09-05 07:35:33 +05301822/**
1823 * iscsi_suspend_queue - suspend iscsi_queuecommand
1824 * @conn: iscsi conn to stop queueing IO on
1825 *
1826 * This grabs the session lock to make sure no one is in
1827 * xmit_task/queuecommand, and then sets suspend to prevent
1828 * new commands from being queued. This only needs to be called
1829 * by offload drivers that need to sync a path like ep disconnect
1830 * with the iscsi_queuecommand/xmit_task. To start IO again libiscsi
1831 * will call iscsi_start_tx and iscsi_unblock_session when in FFP.
1832 */
1833void iscsi_suspend_queue(struct iscsi_conn *conn)
1834{
1835 spin_lock_bh(&conn->session->lock);
1836 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1837 spin_unlock_bh(&conn->session->lock);
1838}
1839EXPORT_SYMBOL_GPL(iscsi_suspend_queue);
1840
1841/**
1842 * iscsi_suspend_tx - suspend iscsi_data_xmit
1843 * @conn: iscsi conn tp stop processing IO on.
1844 *
1845 * This function sets the suspend bit to prevent iscsi_data_xmit
1846 * from sending new IO, and if work is queued on the xmit thread
1847 * it will wait for it to be completed.
1848 */
Mike Christieb40977d2008-05-21 15:54:03 -05001849void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001850{
Mike Christie32ae7632009-03-05 14:46:03 -06001851 struct Scsi_Host *shost = conn->session->host;
1852 struct iscsi_host *ihost = shost_priv(shost);
1853
Mike Christie6724add2007-08-15 01:38:30 -05001854 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001855 if (ihost->workq)
Mike Christie32ae7632009-03-05 14:46:03 -06001856 flush_workqueue(ihost->workq);
Mike Christie6724add2007-08-15 01:38:30 -05001857}
Mike Christieb40977d2008-05-21 15:54:03 -05001858EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001859
1860static void iscsi_start_tx(struct iscsi_conn *conn)
1861{
1862 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie1336aed2009-05-13 17:57:48 -05001863 iscsi_conn_queue_work(conn);
Mike Christie6724add2007-08-15 01:38:30 -05001864}
1865
Mike Christie4c48a822009-05-13 17:57:45 -05001866/*
1867 * We want to make sure a ping is in flight. It has timed out.
1868 * And we are not busy processing a pdu that is making
1869 * progress but got started before the ping and is taking a while
1870 * to complete so the ping is just stuck behind it in a queue.
1871 */
1872static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1873{
1874 if (conn->ping_task &&
1875 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1876 (conn->ping_timeout * HZ), jiffies))
1877 return 1;
1878 else
1879 return 0;
1880}
1881
Mike Christied355e572009-06-15 22:11:08 -05001882static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
Mike Christief6d51802007-12-13 12:43:30 -06001883{
Mike Christied355e572009-06-15 22:11:08 -05001884 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1885 struct iscsi_task *task = NULL;
Mike Christief6d51802007-12-13 12:43:30 -06001886 struct iscsi_cls_session *cls_session;
1887 struct iscsi_session *session;
1888 struct iscsi_conn *conn;
Mike Christief6d51802007-12-13 12:43:30 -06001889
Mike Christied355e572009-06-15 22:11:08 -05001890 cls_session = starget_to_session(scsi_target(sc->device));
Mike Christie75613522008-05-21 15:53:59 -05001891 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001892
Erez Zilberbd2199d2009-06-15 22:11:10 -05001893 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
Mike Christief6d51802007-12-13 12:43:30 -06001894
1895 spin_lock(&session->lock);
1896 if (session->state != ISCSI_STATE_LOGGED_IN) {
1897 /*
1898 * We are probably in the middle of iscsi recovery so let
1899 * that complete and handle the error.
1900 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001901 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001902 goto done;
1903 }
1904
1905 conn = session->leadconn;
1906 if (!conn) {
1907 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001908 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001909 goto done;
1910 }
1911
Mike Christied355e572009-06-15 22:11:08 -05001912 task = (struct iscsi_task *)sc->SCp.ptr;
1913 if (!task)
1914 goto done;
1915 /*
1916 * If we have sent (at least queued to the network layer) a pdu or
1917 * recvd one for the task since the last timeout ask for
1918 * more time. If on the next timeout we have not made progress
1919 * we can check if it is the task or connection when we send the
1920 * nop as a ping.
1921 */
1922 if (time_after_eq(task->last_xfer, task->last_timeout)) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05001923 ISCSI_DBG_EH(session, "Command making progress. Asking "
1924 "scsi-ml for more time to complete. "
1925 "Last data recv at %lu. Last timeout was at "
1926 "%lu\n.", task->last_xfer, task->last_timeout);
Mike Christied355e572009-06-15 22:11:08 -05001927 task->have_checked_conn = false;
1928 rc = BLK_EH_RESET_TIMER;
1929 goto done;
1930 }
1931
Mike Christief6d51802007-12-13 12:43:30 -06001932 if (!conn->recv_timeout && !conn->ping_timeout)
1933 goto done;
1934 /*
1935 * if the ping timedout then we are in the middle of cleaning up
1936 * and can let the iscsi eh handle it
1937 */
Mike Christie4c48a822009-05-13 17:57:45 -05001938 if (iscsi_has_ping_timed_out(conn)) {
Jens Axboe242f9dc2008-09-14 05:55:09 -07001939 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001940 goto done;
1941 }
Mike Christied355e572009-06-15 22:11:08 -05001942
1943 /* Assumes nop timeout is shorter than scsi cmd timeout */
1944 if (task->have_checked_conn)
1945 goto done;
1946
Mike Christief6d51802007-12-13 12:43:30 -06001947 /*
Mike Christied355e572009-06-15 22:11:08 -05001948 * Checking the transport already or nop from a cmd timeout still
1949 * running
Mike Christief6d51802007-12-13 12:43:30 -06001950 */
Mike Christied355e572009-06-15 22:11:08 -05001951 if (conn->ping_task) {
1952 task->have_checked_conn = true;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001953 rc = BLK_EH_RESET_TIMER;
Mike Christie4c48a822009-05-13 17:57:45 -05001954 goto done;
1955 }
1956
Mike Christied355e572009-06-15 22:11:08 -05001957 /* Make sure there is a transport check done */
1958 iscsi_send_nopout(conn, NULL);
1959 task->have_checked_conn = true;
1960 rc = BLK_EH_RESET_TIMER;
1961
Mike Christief6d51802007-12-13 12:43:30 -06001962done:
Mike Christied355e572009-06-15 22:11:08 -05001963 if (task)
1964 task->last_timeout = jiffies;
Mike Christief6d51802007-12-13 12:43:30 -06001965 spin_unlock(&session->lock);
Erez Zilberbd2199d2009-06-15 22:11:10 -05001966 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1967 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001968 return rc;
1969}
1970
1971static void iscsi_check_transport_timeouts(unsigned long data)
1972{
1973 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1974 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001975 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001976
1977 spin_lock(&session->lock);
1978 if (session->state != ISCSI_STATE_LOGGED_IN)
1979 goto done;
1980
Mike Christie4cf10432008-05-07 20:43:52 -05001981 recv_timeout = conn->recv_timeout;
1982 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001983 goto done;
1984
Mike Christie4cf10432008-05-07 20:43:52 -05001985 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001986 last_recv = conn->last_recv;
Mike Christie4c48a822009-05-13 17:57:45 -05001987
1988 if (iscsi_has_ping_timed_out(conn)) {
Mike Christie322d7392008-01-31 13:36:52 -06001989 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
Mike Christie4c48a822009-05-13 17:57:45 -05001990 "expired, recv timeout %d, last rx %lu, "
1991 "last ping %lu, now %lu\n",
1992 conn->ping_timeout, conn->recv_timeout,
1993 last_recv, conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001994 spin_unlock(&session->lock);
1995 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1996 return;
1997 }
1998
Mike Christie4cf10432008-05-07 20:43:52 -05001999 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05002000 /* send a ping to try to provoke some traffic */
Mike Christie1b2c7af2009-03-05 14:45:58 -06002001 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
Mike Christiec8611f92008-05-08 20:15:34 -05002002 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05002003 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06002004 } else
Mike Christie4cf10432008-05-07 20:43:52 -05002005 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06002006
Mike Christie1b2c7af2009-03-05 14:45:58 -06002007 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
Mike Christiead294e92008-01-31 13:36:50 -06002008 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06002009done:
2010 spin_unlock(&session->lock);
2011}
2012
Mike Christie9c19a7d2008-05-21 15:54:09 -05002013static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06002014 struct iscsi_tm *hdr)
2015{
2016 memset(hdr, 0, sizeof(*hdr));
2017 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2018 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
2019 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06002020 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
2021 hdr->rtt = task->hdr_itt;
2022 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06002023}
2024
Mike Christie7996a772006-04-06 21:13:41 -05002025int iscsi_eh_abort(struct scsi_cmnd *sc)
2026{
Mike Christie75613522008-05-21 15:53:59 -05002027 struct iscsi_cls_session *cls_session;
2028 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04002029 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002030 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06002031 struct iscsi_tm *hdr;
2032 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05002033
Mike Christie75613522008-05-21 15:53:59 -05002034 cls_session = starget_to_session(scsi_target(sc->device));
2035 session = cls_session->dd_data;
2036
Erez Zilberbd2199d2009-06-15 22:11:10 -05002037 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
Mike Christie4421c9e2009-05-13 17:57:50 -05002038
Mike Christie6724add2007-08-15 01:38:30 -05002039 mutex_lock(&session->eh_mutex);
2040 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04002041 /*
2042 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2043 * got the command.
2044 */
2045 if (!sc->SCp.ptr) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002046 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2047 "it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05002048 spin_unlock_bh(&session->lock);
2049 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04002050 return SUCCESS;
2051 }
2052
Mike Christie7996a772006-04-06 21:13:41 -05002053 /*
2054 * If we are not logged in or we have started a new session
2055 * then let the host reset code handle this
2056 */
Mike Christie843c0a82007-12-13 12:43:20 -06002057 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2058 sc->SCp.phase != session->age) {
2059 spin_unlock_bh(&session->lock);
2060 mutex_unlock(&session->eh_mutex);
Erez Zilberbd2199d2009-06-15 22:11:10 -05002061 ISCSI_DBG_EH(session, "failing abort due to dropped "
Mike Christie4421c9e2009-05-13 17:57:50 -05002062 "session.\n");
Mike Christie843c0a82007-12-13 12:43:20 -06002063 return FAILED;
2064 }
2065
2066 conn = session->leadconn;
2067 conn->eh_abort_cnt++;
2068 age = session->age;
2069
Mike Christie9c19a7d2008-05-21 15:54:09 -05002070 task = (struct iscsi_task *)sc->SCp.ptr;
Erez Zilberbd2199d2009-06-15 22:11:10 -05002071 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2072 sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05002073
Mike Christie9c19a7d2008-05-21 15:54:09 -05002074 /* task completed before time out */
2075 if (!task->sc) {
Erez Zilberbd2199d2009-06-15 22:11:10 -05002076 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002077 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002078 }
Mike Christie7996a772006-04-06 21:13:41 -05002079
Mike Christie9c19a7d2008-05-21 15:54:09 -05002080 if (task->state == ISCSI_TASK_PENDING) {
Mike Christieb3cd5052009-05-13 17:57:49 -05002081 fail_scsi_task(task, DID_ABORT);
Mike Christie77a23c22007-05-30 12:57:18 -05002082 goto success;
2083 }
Mike Christie7996a772006-04-06 21:13:41 -05002084
Mike Christie843c0a82007-12-13 12:43:20 -06002085 /* only have one tmf outstanding at a time */
2086 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05002087 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06002088 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05002089
Mike Christie843c0a82007-12-13 12:43:20 -06002090 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002091 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06002092
Mike Christie9c19a7d2008-05-21 15:54:09 -05002093 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002094 rc = FAILED;
2095 goto failed;
2096 }
2097
2098 switch (conn->tmf_state) {
2099 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05002100 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05002101 /*
2102 * stop tx side incase the target had sent a abort rsp but
2103 * the initiator was still writing out data.
2104 */
Mike Christie6724add2007-08-15 01:38:30 -05002105 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002106 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05002107 * we do not stop the recv side because targets have been
2108 * good and have never sent us a successful tmf response
2109 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05002110 */
Mike Christie6187c242009-07-15 15:02:57 -05002111 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002112 fail_scsi_task(task, DID_ABORT);
Mike Christie843c0a82007-12-13 12:43:20 -06002113 conn->tmf_state = TMF_INITIAL;
Mike Christie6187c242009-07-15 15:02:57 -05002114 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002115 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05002116 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06002117 case TMF_TIMEDOUT:
2118 spin_unlock_bh(&session->lock);
2119 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2120 goto failed_unlocked;
2121 case TMF_NOT_FOUND:
2122 if (!sc->SCp.ptr) {
2123 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002124 /* task completed before tmf abort response */
Erez Zilberbd2199d2009-06-15 22:11:10 -05002125 ISCSI_DBG_EH(session, "sc completed while abort in "
2126 "progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05002127 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05002128 }
2129 /* fall through */
2130 default:
Mike Christie843c0a82007-12-13 12:43:20 -06002131 conn->tmf_state = TMF_INITIAL;
2132 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05002133 }
2134
Mike Christie77a23c22007-05-30 12:57:18 -05002135success:
Mike Christie7996a772006-04-06 21:13:41 -05002136 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002137success_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002138 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2139 sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05002140 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002141 return SUCCESS;
2142
2143failed:
2144 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05002145failed_unlocked:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002146 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2147 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05002148 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002149 return FAILED;
2150}
2151EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2152
Mike Christie843c0a82007-12-13 12:43:20 -06002153static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
2154{
2155 memset(hdr, 0, sizeof(*hdr));
2156 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
2157 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
2158 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2159 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06002160 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06002161}
2162
2163int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2164{
Mike Christie75613522008-05-21 15:53:59 -05002165 struct iscsi_cls_session *cls_session;
2166 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06002167 struct iscsi_conn *conn;
2168 struct iscsi_tm *hdr;
2169 int rc = FAILED;
2170
Mike Christie75613522008-05-21 15:53:59 -05002171 cls_session = starget_to_session(scsi_target(sc->device));
2172 session = cls_session->dd_data;
2173
Erez Zilberbd2199d2009-06-15 22:11:10 -05002174 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
Mike Christie843c0a82007-12-13 12:43:20 -06002175
2176 mutex_lock(&session->eh_mutex);
2177 spin_lock_bh(&session->lock);
2178 /*
2179 * Just check if we are not logged in. We cannot check for
2180 * the phase because the reset could come from a ioctl.
2181 */
2182 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
2183 goto unlock;
2184 conn = session->leadconn;
2185
2186 /* only have one tmf outstanding at a time */
2187 if (conn->tmf_state != TMF_INITIAL)
2188 goto unlock;
2189 conn->tmf_state = TMF_QUEUED;
2190
2191 hdr = &conn->tmhdr;
2192 iscsi_prep_lun_reset_pdu(sc, hdr);
2193
Mike Christie9c19a7d2008-05-21 15:54:09 -05002194 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06002195 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06002196 rc = FAILED;
2197 goto unlock;
2198 }
2199
2200 switch (conn->tmf_state) {
2201 case TMF_SUCCESS:
2202 break;
2203 case TMF_TIMEDOUT:
2204 spin_unlock_bh(&session->lock);
2205 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2206 goto done;
2207 default:
2208 conn->tmf_state = TMF_INITIAL;
2209 goto unlock;
2210 }
2211
2212 rc = SUCCESS;
2213 spin_unlock_bh(&session->lock);
2214
2215 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05002216
Mike Christiea3439142008-09-24 11:46:15 -05002217 spin_lock_bh(&session->lock);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002218 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06002219 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05002220 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002221
2222 iscsi_start_tx(conn);
2223 goto done;
2224
2225unlock:
2226 spin_unlock_bh(&session->lock);
2227done:
Erez Zilberbd2199d2009-06-15 22:11:10 -05002228 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2229 rc == SUCCESS ? "SUCCESS" : "FAILED");
Mike Christie843c0a82007-12-13 12:43:20 -06002230 mutex_unlock(&session->eh_mutex);
2231 return rc;
2232}
2233EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2234
Olaf Kirch63203772007-12-13 12:43:25 -06002235/*
2236 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2237 * should be accessed via kfifo_{get,put} on q->queue.
2238 * Optionally, the caller can obtain the array of object pointers
2239 * by passing in a non-NULL @items pointer
2240 */
Mike Christie7996a772006-04-06 21:13:41 -05002241int
Olaf Kirch63203772007-12-13 12:43:25 -06002242iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05002243{
Olaf Kirch63203772007-12-13 12:43:25 -06002244 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05002245
Olaf Kirch63203772007-12-13 12:43:25 -06002246 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05002247
2248 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06002249
2250 /* If the user passed an items pointer, he wants a copy of
2251 * the array. */
2252 if (items)
2253 num_arrays++;
2254 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2255 if (q->pool == NULL)
Jean Delvaref474a372009-03-05 14:45:55 -06002256 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002257
2258 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2259 GFP_KERNEL, NULL);
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002260 if (IS_ERR(q->queue)) {
2261 q->queue = NULL;
Olaf Kirch63203772007-12-13 12:43:25 -06002262 goto enomem;
Jean Delvarefd6e1c12009-04-01 13:11:29 -05002263 }
Mike Christie7996a772006-04-06 21:13:41 -05002264
2265 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06002266 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05002267 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06002268 q->max = i;
2269 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05002270 }
Mike Christie7996a772006-04-06 21:13:41 -05002271 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2272 }
Olaf Kirch63203772007-12-13 12:43:25 -06002273
2274 if (items) {
2275 *items = q->pool + max;
2276 memcpy(*items, q->pool, max * sizeof(void *));
2277 }
2278
Mike Christie7996a772006-04-06 21:13:41 -05002279 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06002280
2281enomem:
2282 iscsi_pool_free(q);
2283 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05002284}
2285EXPORT_SYMBOL_GPL(iscsi_pool_init);
2286
Olaf Kirch63203772007-12-13 12:43:25 -06002287void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05002288{
2289 int i;
2290
2291 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06002292 kfree(q->pool[i]);
Jean Delvaref474a372009-03-05 14:45:55 -06002293 kfree(q->pool);
Mike Christie2f5899a2009-01-16 12:36:51 -06002294 kfree(q->queue);
Mike Christie7996a772006-04-06 21:13:41 -05002295}
2296EXPORT_SYMBOL_GPL(iscsi_pool_free);
2297
Mike Christiea4804cd2008-05-21 15:54:00 -05002298/**
2299 * iscsi_host_add - add host to system
2300 * @shost: scsi host
2301 * @pdev: parent device
2302 *
2303 * This should be called by partial offload and software iscsi drivers
2304 * to add a host to the system.
2305 */
2306int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05002307{
Mike Christie8e9a20c2008-06-16 10:11:33 -05002308 if (!shost->can_queue)
2309 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2310
Mike Christie4d108352009-03-05 14:46:04 -06002311 if (!shost->cmd_per_lun)
2312 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2313
Mike Christie308cec12009-02-06 12:06:20 -06002314 if (!shost->transportt->eh_timed_out)
2315 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christiea4804cd2008-05-21 15:54:00 -05002316 return scsi_add_host(shost, pdev);
2317}
2318EXPORT_SYMBOL_GPL(iscsi_host_add);
2319
2320/**
2321 * iscsi_host_alloc - allocate a host and driver data
2322 * @sht: scsi host template
2323 * @dd_data_size: driver host data size
Mike Christie32ae7632009-03-05 14:46:03 -06002324 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
Mike Christiea4804cd2008-05-21 15:54:00 -05002325 *
2326 * This should be called by partial offload and software iscsi drivers.
2327 * To access the driver specific memory use the iscsi_host_priv() macro.
2328 */
2329struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
Mike Christie4d108352009-03-05 14:46:04 -06002330 int dd_data_size, bool xmit_can_sleep)
Mike Christiea4804cd2008-05-21 15:54:00 -05002331{
2332 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002333 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05002334
2335 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2336 if (!shost)
2337 return NULL;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002338 ihost = shost_priv(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002339
2340 if (xmit_can_sleep) {
2341 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2342 "iscsi_q_%d", shost->host_no);
2343 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2344 if (!ihost->workq)
2345 goto free_host;
2346 }
2347
Mike Christiee5bd7b52008-09-24 11:46:10 -05002348 spin_lock_init(&ihost->lock);
2349 ihost->state = ISCSI_HOST_SETUP;
2350 ihost->num_sessions = 0;
2351 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002352 return shost;
Mike Christie32ae7632009-03-05 14:46:03 -06002353
2354free_host:
2355 scsi_host_put(shost);
2356 return NULL;
Mike Christie75613522008-05-21 15:53:59 -05002357}
Mike Christiea4804cd2008-05-21 15:54:00 -05002358EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002359
Mike Christiee5bd7b52008-09-24 11:46:10 -05002360static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2361{
Mike Christie40a06e72009-03-05 14:46:05 -06002362 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002363}
2364
Mike Christiea4804cd2008-05-21 15:54:00 -05002365/**
2366 * iscsi_host_remove - remove host and sessions
2367 * @shost: scsi host
2368 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002369 * If there are any sessions left, this will initiate the removal and wait
2370 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002371 */
2372void iscsi_host_remove(struct Scsi_Host *shost)
2373{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002374 struct iscsi_host *ihost = shost_priv(shost);
2375 unsigned long flags;
2376
2377 spin_lock_irqsave(&ihost->lock, flags);
2378 ihost->state = ISCSI_HOST_REMOVED;
2379 spin_unlock_irqrestore(&ihost->lock, flags);
2380
2381 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2382 wait_event_interruptible(ihost->session_removal_wq,
2383 ihost->num_sessions == 0);
2384 if (signal_pending(current))
2385 flush_signals(current);
2386
Mike Christiea4804cd2008-05-21 15:54:00 -05002387 scsi_remove_host(shost);
Mike Christie32ae7632009-03-05 14:46:03 -06002388 if (ihost->workq)
2389 destroy_workqueue(ihost->workq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002390}
2391EXPORT_SYMBOL_GPL(iscsi_host_remove);
2392
2393void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002394{
2395 struct iscsi_host *ihost = shost_priv(shost);
2396
2397 kfree(ihost->netdev);
2398 kfree(ihost->hwaddress);
2399 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002400 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002401}
Mike Christiea4804cd2008-05-21 15:54:00 -05002402EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002403
Mike Christiee5bd7b52008-09-24 11:46:10 -05002404static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2405{
2406 struct iscsi_host *ihost = shost_priv(shost);
2407 unsigned long flags;
2408
2409 shost = scsi_host_get(shost);
2410 if (!shost) {
2411 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2412 "of session teardown event because host already "
2413 "removed.\n");
2414 return;
2415 }
2416
2417 spin_lock_irqsave(&ihost->lock, flags);
2418 ihost->num_sessions--;
2419 if (ihost->num_sessions == 0)
2420 wake_up(&ihost->session_removal_wq);
2421 spin_unlock_irqrestore(&ihost->lock, flags);
2422 scsi_host_put(shost);
2423}
2424
Mike Christie75613522008-05-21 15:53:59 -05002425/**
2426 * iscsi_session_setup - create iscsi cls session and host and session
2427 * @iscsit: iscsi transport template
2428 * @shost: scsi host
2429 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002430 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002431 * @initial_cmdsn: initial CmdSN
2432 *
2433 * This can be used by software iscsi_transports that allocate
2434 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002435 *
2436 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2437 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2438 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002439 */
2440struct iscsi_cls_session *
2441iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302442 uint16_t cmds_max, int dd_size, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002443 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002444{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002445 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002446 struct iscsi_session *session;
2447 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002448 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002449 unsigned long flags;
2450
2451 spin_lock_irqsave(&ihost->lock, flags);
2452 if (ihost->state == ISCSI_HOST_REMOVED) {
2453 spin_unlock_irqrestore(&ihost->lock, flags);
2454 return NULL;
2455 }
2456 ihost->num_sessions++;
2457 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002458
2459 if (!total_cmds)
2460 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002461 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002462 * The iscsi layer needs some tasks for nop handling and tmfs,
2463 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2464 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002465 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002466 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2467 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2468 "must be a power of two that is at least %d.\n",
2469 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002470 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002471 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002472
2473 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2474 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2475 "must be a power of 2 less than or equal to %d.\n",
2476 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2477 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2478 }
2479
2480 if (!is_power_of_2(total_cmds)) {
2481 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2482 "must be a power of 2.\n", total_cmds);
2483 total_cmds = rounddown_pow_of_two(total_cmds);
2484 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2485 return NULL;
2486 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2487 total_cmds);
2488 }
2489 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002490
Mike Christie5d91e202008-05-21 15:54:01 -05002491 cls_session = iscsi_alloc_session(shost, iscsit,
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302492 sizeof(struct iscsi_session) +
2493 dd_size);
Mike Christie75613522008-05-21 15:53:59 -05002494 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002495 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002496 session = cls_session->dd_data;
2497 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002498 session->host = shost;
2499 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002500 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002501 session->lu_reset_timeout = 15;
2502 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002503 session->scsi_cmds_max = scsi_cmds;
2504 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002505 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002506 session->exp_cmdsn = initial_cmdsn + 1;
2507 session->max_cmdsn = initial_cmdsn + 1;
2508 session->max_r2t = 1;
2509 session->tt = iscsit;
Jayamohan Kallickalb8b9e1b82009-09-22 08:21:22 +05302510 session->dd_data = cls_session->dd_data + sizeof(*session);
Mike Christie6724add2007-08-15 01:38:30 -05002511 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002512 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002513
2514 /* initialize SCSI PDU commands pool */
2515 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2516 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002517 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002518 goto cmdpool_alloc_fail;
2519
2520 /* pre-format cmds pool with ITT */
2521 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002522 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002523
Mike Christie9c19a7d2008-05-21 15:54:09 -05002524 if (cmd_task_size)
2525 task->dd_data = &task[1];
2526 task->itt = cmd_i;
Mike Christie3bbaaad2009-05-13 17:57:46 -05002527 task->state = ISCSI_TASK_FREE;
Mike Christie9c19a7d2008-05-21 15:54:09 -05002528 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002529 }
2530
Mike Christief53a88d2006-06-28 12:00:27 -05002531 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002532 goto module_get_fail;
2533
Mike Christie79706342008-05-21 15:54:12 -05002534 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002535 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002536
Mike Christie7996a772006-04-06 21:13:41 -05002537 return cls_session;
2538
2539cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002540 module_put(iscsit->owner);
2541module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002542 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002543cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002544 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002545dec_session_count:
2546 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002547 return NULL;
2548}
2549EXPORT_SYMBOL_GPL(iscsi_session_setup);
2550
2551/**
2552 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002553 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002554 *
Mike Christie75613522008-05-21 15:53:59 -05002555 * The driver must have called iscsi_remove_session before
2556 * calling this.
2557 */
Mike Christie7996a772006-04-06 21:13:41 -05002558void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2559{
Mike Christie75613522008-05-21 15:53:59 -05002560 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002561 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002562 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002563
Olaf Kirch63203772007-12-13 12:43:25 -06002564 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002565
Mike Christieb2c64162007-05-30 12:57:16 -05002566 kfree(session->password);
2567 kfree(session->password_in);
2568 kfree(session->username);
2569 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002570 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002571 kfree(session->initiatorname);
2572 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002573
Mike Christie75613522008-05-21 15:53:59 -05002574 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002575 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002576 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002577}
2578EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2579
2580/**
2581 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2582 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002583 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002584 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002585 */
Mike Christie7996a772006-04-06 21:13:41 -05002586struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002587iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2588 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002589{
Mike Christie75613522008-05-21 15:53:59 -05002590 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002591 struct iscsi_conn *conn;
2592 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002593 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002594
Mike Christie5d91e202008-05-21 15:54:01 -05002595 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2596 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002597 if (!cls_conn)
2598 return NULL;
2599 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002600 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002601
Mike Christie5d91e202008-05-21 15:54:01 -05002602 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002603 conn->session = session;
2604 conn->cls_conn = cls_conn;
2605 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2606 conn->id = conn_idx;
2607 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002608 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002609
2610 init_timer(&conn->transport_timer);
2611 conn->transport_timer.data = (unsigned long)conn;
2612 conn->transport_timer.function = iscsi_check_transport_timeouts;
2613
Mike Christie843c0a82007-12-13 12:43:20 -06002614 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002615 INIT_LIST_HEAD(&conn->cmdqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002616 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002617 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002618
Mike Christie9c19a7d2008-05-21 15:54:09 -05002619 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002620 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002621 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002622 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002623 sizeof(void*))) {
2624 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002625 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002626 }
2627 spin_unlock_bh(&session->lock);
2628
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002629 data = (char *) __get_free_pages(GFP_KERNEL,
2630 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christied36ab6f2006-05-18 20:31:34 -05002631 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002632 goto login_task_data_alloc_fail;
2633 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002634
Mike Christie843c0a82007-12-13 12:43:20 -06002635 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002636 init_waitqueue_head(&conn->ehwait);
2637
2638 return cls_conn;
2639
Mike Christie9c19a7d2008-05-21 15:54:09 -05002640login_task_data_alloc_fail:
2641 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002642 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002643login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002644 iscsi_destroy_conn(cls_conn);
2645 return NULL;
2646}
2647EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2648
2649/**
2650 * iscsi_conn_teardown - teardown iscsi connection
2651 * cls_conn: iscsi class connection
2652 *
2653 * TODO: we may need to make this into a two step process
2654 * like scsi-mls remove + put host
2655 */
2656void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2657{
2658 struct iscsi_conn *conn = cls_conn->dd_data;
2659 struct iscsi_session *session = conn->session;
2660 unsigned long flags;
2661
Mike Christief6d51802007-12-13 12:43:30 -06002662 del_timer_sync(&conn->transport_timer);
2663
Mike Christie7996a772006-04-06 21:13:41 -05002664 spin_lock_bh(&session->lock);
2665 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2666 if (session->leadconn == conn) {
2667 /*
2668 * leading connection? then give up on recovery.
2669 */
2670 session->state = ISCSI_STATE_TERMINATE;
2671 wake_up(&conn->ehwait);
2672 }
2673 spin_unlock_bh(&session->lock);
2674
Mike Christie7996a772006-04-06 21:13:41 -05002675 /*
2676 * Block until all in-progress commands for this connection
2677 * time out or fail.
2678 */
2679 for (;;) {
2680 spin_lock_irqsave(session->host->host_lock, flags);
2681 if (!session->host->host_busy) { /* OK for ERL == 0 */
2682 spin_unlock_irqrestore(session->host->host_lock, flags);
2683 break;
2684 }
2685 spin_unlock_irqrestore(session->host->host_lock, flags);
2686 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002687 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2688 "host_busy %d host_failed %d\n",
2689 session->host->host_busy,
2690 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002691 /*
2692 * force eh_abort() to unblock
2693 */
2694 wake_up(&conn->ehwait);
2695 }
2696
Mike Christie779ea122007-02-28 17:32:15 -06002697 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002698 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002699
Mike Christie7996a772006-04-06 21:13:41 -05002700 spin_lock_bh(&session->lock);
Mike Christiecfeb2cf2008-12-02 00:32:09 -06002701 free_pages((unsigned long) conn->data,
2702 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
Mike Christief3ff0c32006-07-24 15:47:50 -05002703 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002704 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002705 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002706 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002707 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002708 spin_unlock_bh(&session->lock);
2709
Mike Christie7996a772006-04-06 21:13:41 -05002710 iscsi_destroy_conn(cls_conn);
2711}
2712EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2713
2714int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2715{
2716 struct iscsi_conn *conn = cls_conn->dd_data;
2717 struct iscsi_session *session = conn->session;
2718
Mike Christieffd04362006-08-31 18:09:24 -04002719 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002720 iscsi_conn_printk(KERN_ERR, conn,
2721 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002722 return -EPERM;
2723 }
2724
Mike Christiedb98ccd2006-08-31 18:09:31 -04002725 if ((session->imm_data_en || !session->initial_r2t_en) &&
2726 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002727 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2728 "first_burst %d max_burst %d\n",
2729 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002730 return -EINVAL;
2731 }
2732
Mike Christief6d51802007-12-13 12:43:30 -06002733 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002734 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2735 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002736 conn->recv_timeout = 5;
2737 }
2738
2739 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002740 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2741 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002742 conn->ping_timeout = 5;
2743 }
2744
Mike Christie7996a772006-04-06 21:13:41 -05002745 spin_lock_bh(&session->lock);
2746 conn->c_stage = ISCSI_CONN_STARTED;
2747 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002748 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002749
Mike Christief6d51802007-12-13 12:43:30 -06002750 conn->last_recv = jiffies;
2751 conn->last_ping = jiffies;
2752 if (conn->recv_timeout && conn->ping_timeout)
2753 mod_timer(&conn->transport_timer,
2754 jiffies + (conn->recv_timeout * HZ));
2755
Mike Christie7996a772006-04-06 21:13:41 -05002756 switch(conn->stop_stage) {
2757 case STOP_CONN_RECOVER:
2758 /*
2759 * unblock eh_abort() if it is blocked. re-try all
2760 * commands after successful recovery
2761 */
Mike Christie7996a772006-04-06 21:13:41 -05002762 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002763 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002764 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002765 if (session->age == 16)
2766 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002767 break;
Mike Christie7996a772006-04-06 21:13:41 -05002768 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002769 conn->stop_stage = 0;
2770 break;
Mike Christie7996a772006-04-06 21:13:41 -05002771 default:
2772 break;
2773 }
2774 spin_unlock_bh(&session->lock);
2775
Mike Christie75613522008-05-21 15:53:59 -05002776 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002777 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002778 return 0;
2779}
2780EXPORT_SYMBOL_GPL(iscsi_conn_start);
2781
2782static void
Mike Christie3bbaaad2009-05-13 17:57:46 -05002783fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
Mike Christie7996a772006-04-06 21:13:41 -05002784{
Mike Christie3bbaaad2009-05-13 17:57:46 -05002785 struct iscsi_task *task;
Mike Christieb3cd5052009-05-13 17:57:49 -05002786 int i, state;
Mike Christie7996a772006-04-06 21:13:41 -05002787
Mike Christie3bbaaad2009-05-13 17:57:46 -05002788 for (i = 0; i < conn->session->cmds_max; i++) {
2789 task = conn->session->cmds[i];
2790 if (task->sc)
2791 continue;
2792
2793 if (task->state == ISCSI_TASK_FREE)
2794 continue;
2795
2796 ISCSI_DBG_SESSION(conn->session,
2797 "failing mgmt itt 0x%x state %d\n",
2798 task->itt, task->state);
Mike Christieb3cd5052009-05-13 17:57:49 -05002799 state = ISCSI_TASK_ABRT_SESS_RECOV;
2800 if (task->state == ISCSI_TASK_PENDING)
2801 state = ISCSI_TASK_COMPLETED;
2802 iscsi_complete_task(task, state);
2803
Mike Christie7996a772006-04-06 21:13:41 -05002804 }
Mike Christie7996a772006-04-06 21:13:41 -05002805}
2806
Mike Christie656cffc2006-05-18 20:31:42 -05002807static void iscsi_start_session_recovery(struct iscsi_session *session,
2808 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002809{
Mike Christieed2abc72006-05-02 19:46:40 -05002810 int old_stop_stage;
2811
Mike Christie6724add2007-08-15 01:38:30 -05002812 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002813 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002814 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002815 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002816 mutex_unlock(&session->eh_mutex);
2817 return;
2818 }
2819
2820 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002821 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002822 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002823 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002824 */
Mike Christie67a61112006-05-30 00:37:20 -05002825 if (flag == STOP_CONN_TERM)
2826 session->state = ISCSI_STATE_TERMINATE;
2827 else if (conn->stop_stage != STOP_CONN_RECOVER)
2828 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christie26013ad2009-05-13 17:57:43 -05002829 spin_unlock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002830
Mike Christie26013ad2009-05-13 17:57:43 -05002831 del_timer_sync(&conn->transport_timer);
2832 iscsi_suspend_tx(conn);
2833
2834 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002835 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002836 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002837 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002838 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002839
Mike Christie7996a772006-04-06 21:13:41 -05002840 /*
2841 * for connection level recovery we should not calculate
2842 * header digest. conn->hdr_size used for optimization
2843 * in hdr_extract() and will be re-negotiated at
2844 * set_param() time.
2845 */
2846 if (flag == STOP_CONN_RECOVER) {
2847 conn->hdrdgst_en = 0;
2848 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002849 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002850 old_stop_stage != STOP_CONN_RECOVER) {
Mike Christie1b2c7af2009-03-05 14:45:58 -06002851 ISCSI_DBG_SESSION(session, "blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002852 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002853 }
Mike Christie7996a772006-04-06 21:13:41 -05002854 }
Mike Christie656cffc2006-05-18 20:31:42 -05002855
Mike Christie656cffc2006-05-18 20:31:42 -05002856 /*
2857 * flush queues.
2858 */
2859 spin_lock_bh(&session->lock);
Mike Christieb3cd5052009-05-13 17:57:49 -05002860 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
Mike Christie3bbaaad2009-05-13 17:57:46 -05002861 fail_mgmt_tasks(session, conn);
Mike Christie656cffc2006-05-18 20:31:42 -05002862 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002863 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002864}
Mike Christie7996a772006-04-06 21:13:41 -05002865
2866void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2867{
2868 struct iscsi_conn *conn = cls_conn->dd_data;
2869 struct iscsi_session *session = conn->session;
2870
2871 switch (flag) {
2872 case STOP_CONN_RECOVER:
2873 case STOP_CONN_TERM:
2874 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002875 break;
Mike Christie7996a772006-04-06 21:13:41 -05002876 default:
Mike Christie322d7392008-01-31 13:36:52 -06002877 iscsi_conn_printk(KERN_ERR, conn,
2878 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002879 }
2880}
2881EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2882
2883int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2884 struct iscsi_cls_conn *cls_conn, int is_leading)
2885{
Mike Christie75613522008-05-21 15:53:59 -05002886 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002887 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002888
Mike Christie7996a772006-04-06 21:13:41 -05002889 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002890 if (is_leading)
2891 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002892 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002893
2894 /*
2895 * Unblock xmitworker(), Login Phase will pass through.
2896 */
2897 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2898 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2899 return 0;
2900}
2901EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2902
Mike Christie5700b1a2009-05-13 17:57:40 -05002903static int iscsi_switch_str_param(char **param, char *new_val_buf)
2904{
2905 char *new_val;
2906
2907 if (*param) {
2908 if (!strcmp(*param, new_val_buf))
2909 return 0;
2910 }
2911
2912 new_val = kstrdup(new_val_buf, GFP_NOIO);
2913 if (!new_val)
2914 return -ENOMEM;
2915
2916 kfree(*param);
2917 *param = new_val;
2918 return 0;
2919}
Mike Christiea54a52c2006-06-28 12:00:23 -05002920
2921int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2922 enum iscsi_param param, char *buf, int buflen)
2923{
2924 struct iscsi_conn *conn = cls_conn->dd_data;
2925 struct iscsi_session *session = conn->session;
2926 uint32_t value;
2927
2928 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002929 case ISCSI_PARAM_FAST_ABORT:
2930 sscanf(buf, "%d", &session->fast_abort);
2931 break;
Mike Christief6d51802007-12-13 12:43:30 -06002932 case ISCSI_PARAM_ABORT_TMO:
2933 sscanf(buf, "%d", &session->abort_timeout);
2934 break;
2935 case ISCSI_PARAM_LU_RESET_TMO:
2936 sscanf(buf, "%d", &session->lu_reset_timeout);
2937 break;
2938 case ISCSI_PARAM_PING_TMO:
2939 sscanf(buf, "%d", &conn->ping_timeout);
2940 break;
2941 case ISCSI_PARAM_RECV_TMO:
2942 sscanf(buf, "%d", &conn->recv_timeout);
2943 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002944 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2945 sscanf(buf, "%d", &conn->max_recv_dlength);
2946 break;
2947 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2948 sscanf(buf, "%d", &conn->max_xmit_dlength);
2949 break;
2950 case ISCSI_PARAM_HDRDGST_EN:
2951 sscanf(buf, "%d", &conn->hdrdgst_en);
2952 break;
2953 case ISCSI_PARAM_DATADGST_EN:
2954 sscanf(buf, "%d", &conn->datadgst_en);
2955 break;
2956 case ISCSI_PARAM_INITIAL_R2T_EN:
2957 sscanf(buf, "%d", &session->initial_r2t_en);
2958 break;
2959 case ISCSI_PARAM_MAX_R2T:
2960 sscanf(buf, "%d", &session->max_r2t);
2961 break;
2962 case ISCSI_PARAM_IMM_DATA_EN:
2963 sscanf(buf, "%d", &session->imm_data_en);
2964 break;
2965 case ISCSI_PARAM_FIRST_BURST:
2966 sscanf(buf, "%d", &session->first_burst);
2967 break;
2968 case ISCSI_PARAM_MAX_BURST:
2969 sscanf(buf, "%d", &session->max_burst);
2970 break;
2971 case ISCSI_PARAM_PDU_INORDER_EN:
2972 sscanf(buf, "%d", &session->pdu_inorder_en);
2973 break;
2974 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2975 sscanf(buf, "%d", &session->dataseq_inorder_en);
2976 break;
2977 case ISCSI_PARAM_ERL:
2978 sscanf(buf, "%d", &session->erl);
2979 break;
2980 case ISCSI_PARAM_IFMARKER_EN:
2981 sscanf(buf, "%d", &value);
2982 BUG_ON(value);
2983 break;
2984 case ISCSI_PARAM_OFMARKER_EN:
2985 sscanf(buf, "%d", &value);
2986 BUG_ON(value);
2987 break;
2988 case ISCSI_PARAM_EXP_STATSN:
2989 sscanf(buf, "%u", &conn->exp_statsn);
2990 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002991 case ISCSI_PARAM_USERNAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05002992 return iscsi_switch_str_param(&session->username, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002993 case ISCSI_PARAM_USERNAME_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002994 return iscsi_switch_str_param(&session->username_in, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002995 case ISCSI_PARAM_PASSWORD:
Mike Christie5700b1a2009-05-13 17:57:40 -05002996 return iscsi_switch_str_param(&session->password, buf);
Mike Christieb2c64162007-05-30 12:57:16 -05002997 case ISCSI_PARAM_PASSWORD_IN:
Mike Christie5700b1a2009-05-13 17:57:40 -05002998 return iscsi_switch_str_param(&session->password_in, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05002999 case ISCSI_PARAM_TARGET_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003000 return iscsi_switch_str_param(&session->targetname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003001 case ISCSI_PARAM_TPGT:
3002 sscanf(buf, "%d", &session->tpgt);
3003 break;
3004 case ISCSI_PARAM_PERSISTENT_PORT:
3005 sscanf(buf, "%d", &conn->persistent_port);
3006 break;
3007 case ISCSI_PARAM_PERSISTENT_ADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003008 return iscsi_switch_str_param(&conn->persistent_address, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003009 case ISCSI_PARAM_IFACE_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003010 return iscsi_switch_str_param(&session->ifacename, buf);
Mike Christie88dfd342008-05-21 15:54:16 -05003011 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003012 return iscsi_switch_str_param(&session->initiatorname, buf);
Mike Christiea54a52c2006-06-28 12:00:23 -05003013 default:
3014 return -ENOSYS;
3015 }
3016
3017 return 0;
3018}
3019EXPORT_SYMBOL_GPL(iscsi_set_param);
3020
3021int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
3022 enum iscsi_param param, char *buf)
3023{
Mike Christie75613522008-05-21 15:53:59 -05003024 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05003025 int len;
3026
3027 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06003028 case ISCSI_PARAM_FAST_ABORT:
3029 len = sprintf(buf, "%d\n", session->fast_abort);
3030 break;
Mike Christief6d51802007-12-13 12:43:30 -06003031 case ISCSI_PARAM_ABORT_TMO:
3032 len = sprintf(buf, "%d\n", session->abort_timeout);
3033 break;
3034 case ISCSI_PARAM_LU_RESET_TMO:
3035 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
3036 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003037 case ISCSI_PARAM_INITIAL_R2T_EN:
3038 len = sprintf(buf, "%d\n", session->initial_r2t_en);
3039 break;
3040 case ISCSI_PARAM_MAX_R2T:
3041 len = sprintf(buf, "%hu\n", session->max_r2t);
3042 break;
3043 case ISCSI_PARAM_IMM_DATA_EN:
3044 len = sprintf(buf, "%d\n", session->imm_data_en);
3045 break;
3046 case ISCSI_PARAM_FIRST_BURST:
3047 len = sprintf(buf, "%u\n", session->first_burst);
3048 break;
3049 case ISCSI_PARAM_MAX_BURST:
3050 len = sprintf(buf, "%u\n", session->max_burst);
3051 break;
3052 case ISCSI_PARAM_PDU_INORDER_EN:
3053 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
3054 break;
3055 case ISCSI_PARAM_DATASEQ_INORDER_EN:
3056 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
3057 break;
3058 case ISCSI_PARAM_ERL:
3059 len = sprintf(buf, "%d\n", session->erl);
3060 break;
3061 case ISCSI_PARAM_TARGET_NAME:
3062 len = sprintf(buf, "%s\n", session->targetname);
3063 break;
3064 case ISCSI_PARAM_TPGT:
3065 len = sprintf(buf, "%d\n", session->tpgt);
3066 break;
Mike Christieb2c64162007-05-30 12:57:16 -05003067 case ISCSI_PARAM_USERNAME:
3068 len = sprintf(buf, "%s\n", session->username);
3069 break;
3070 case ISCSI_PARAM_USERNAME_IN:
3071 len = sprintf(buf, "%s\n", session->username_in);
3072 break;
3073 case ISCSI_PARAM_PASSWORD:
3074 len = sprintf(buf, "%s\n", session->password);
3075 break;
3076 case ISCSI_PARAM_PASSWORD_IN:
3077 len = sprintf(buf, "%s\n", session->password_in);
3078 break;
Mike Christie88dfd342008-05-21 15:54:16 -05003079 case ISCSI_PARAM_IFACE_NAME:
3080 len = sprintf(buf, "%s\n", session->ifacename);
3081 break;
3082 case ISCSI_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003083 len = sprintf(buf, "%s\n", session->initiatorname);
Mike Christie88dfd342008-05-21 15:54:16 -05003084 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003085 default:
3086 return -ENOSYS;
3087 }
3088
3089 return len;
3090}
3091EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3092
3093int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
3094 enum iscsi_param param, char *buf)
3095{
3096 struct iscsi_conn *conn = cls_conn->dd_data;
3097 int len;
3098
3099 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06003100 case ISCSI_PARAM_PING_TMO:
3101 len = sprintf(buf, "%u\n", conn->ping_timeout);
3102 break;
3103 case ISCSI_PARAM_RECV_TMO:
3104 len = sprintf(buf, "%u\n", conn->recv_timeout);
3105 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05003106 case ISCSI_PARAM_MAX_RECV_DLENGTH:
3107 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
3108 break;
3109 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
3110 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
3111 break;
3112 case ISCSI_PARAM_HDRDGST_EN:
3113 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
3114 break;
3115 case ISCSI_PARAM_DATADGST_EN:
3116 len = sprintf(buf, "%d\n", conn->datadgst_en);
3117 break;
3118 case ISCSI_PARAM_IFMARKER_EN:
3119 len = sprintf(buf, "%d\n", conn->ifmarker_en);
3120 break;
3121 case ISCSI_PARAM_OFMARKER_EN:
3122 len = sprintf(buf, "%d\n", conn->ofmarker_en);
3123 break;
3124 case ISCSI_PARAM_EXP_STATSN:
3125 len = sprintf(buf, "%u\n", conn->exp_statsn);
3126 break;
3127 case ISCSI_PARAM_PERSISTENT_PORT:
3128 len = sprintf(buf, "%d\n", conn->persistent_port);
3129 break;
3130 case ISCSI_PARAM_PERSISTENT_ADDRESS:
3131 len = sprintf(buf, "%s\n", conn->persistent_address);
3132 break;
3133 default:
3134 return -ENOSYS;
3135 }
3136
3137 return len;
3138}
3139EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3140
Mike Christie0801c242007-05-30 12:57:12 -05003141int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3142 char *buf)
3143{
Mike Christie75613522008-05-21 15:53:59 -05003144 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003145 int len;
3146
3147 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003148 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003149 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05003150 break;
Mike Christie0801c242007-05-30 12:57:12 -05003151 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003152 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05003153 break;
Mike Christie8ad57812007-05-30 12:57:13 -05003154 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003155 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05003156 break;
Mike Christie75613522008-05-21 15:53:59 -05003157 case ISCSI_HOST_PARAM_IPADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003158 len = sprintf(buf, "%s\n", ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05003159 break;
Mike Christie0801c242007-05-30 12:57:12 -05003160 default:
3161 return -ENOSYS;
3162 }
3163
3164 return len;
3165}
3166EXPORT_SYMBOL_GPL(iscsi_host_get_param);
3167
3168int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3169 char *buf, int buflen)
3170{
Mike Christie75613522008-05-21 15:53:59 -05003171 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05003172
3173 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05003174 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003175 return iscsi_switch_str_param(&ihost->netdev, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003176 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie5700b1a2009-05-13 17:57:40 -05003177 return iscsi_switch_str_param(&ihost->hwaddress, buf);
Mike Christie8ad57812007-05-30 12:57:13 -05003178 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie5700b1a2009-05-13 17:57:40 -05003179 return iscsi_switch_str_param(&ihost->initiatorname, buf);
Mike Christie0801c242007-05-30 12:57:12 -05003180 default:
3181 return -ENOSYS;
3182 }
3183
3184 return 0;
3185}
3186EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3187
Mike Christie7996a772006-04-06 21:13:41 -05003188MODULE_AUTHOR("Mike Christie");
3189MODULE_DESCRIPTION("iSCSI library functions");
3190MODULE_LICENSE("GPL");