blob: 80cbcdce7fa3b64c4d8304b89915c7d1c5283faa [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
Mike Christie77a23c22007-05-30 12:57:18 -050041/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050043
Mike Christie77a23c22007-05-30 12:57:18 -050044static int iscsi_sna_lt(u32 n1, u32 n2)
45{
46 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
47 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
48}
49
50/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
51static int iscsi_sna_lte(u32 n1, u32 n2)
52{
53 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55}
56
57void
58iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050059{
60 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
61 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
62
Mike Christie77a23c22007-05-30 12:57:18 -050063 /*
64 * standard specifies this check for when to update expected and
65 * max sequence numbers
66 */
67 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
68 return;
69
70 if (exp_cmdsn != session->exp_cmdsn &&
71 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050072 session->exp_cmdsn = exp_cmdsn;
73
Mike Christie77a23c22007-05-30 12:57:18 -050074 if (max_cmdsn != session->max_cmdsn &&
75 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
76 session->max_cmdsn = max_cmdsn;
77 /*
78 * if the window closed with IO queued, then kick the
79 * xmit thread
80 */
81 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie052d0142008-05-21 15:54:05 -050082 !list_empty(&session->leadconn->mgmtqueue)) {
83 if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD))
84 scsi_queue_work(session->host,
85 &session->leadconn->xmitwork);
86 }
Mike Christie77a23c22007-05-30 12:57:18 -050087 }
Mike Christie7996a772006-04-06 21:13:41 -050088}
Mike Christie77a23c22007-05-30 12:57:18 -050089EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050090
Mike Christie577577d2008-12-02 00:32:05 -060091/**
92 * iscsi_prep_data_out_pdu - initialize Data-Out
93 * @task: scsi command task
94 * @r2t: R2T info
95 * @hdr: iscsi data in pdu
96 *
97 * Notes:
98 * Initialize Data-Out within this R2T sequence and finds
99 * proper data_offset within this SCSI command.
100 *
101 * This function is called with connection lock taken.
102 **/
103void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
104 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -0500105{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500106 struct iscsi_conn *conn = task->conn;
Mike Christie577577d2008-12-02 00:32:05 -0600107 unsigned int left = r2t->data_length - r2t->sent;
108
109 task->hdr_len = sizeof(struct iscsi_data);
Mike Christie7996a772006-04-06 21:13:41 -0500110
111 memset(hdr, 0, sizeof(struct iscsi_data));
Mike Christie577577d2008-12-02 00:32:05 -0600112 hdr->ttt = r2t->ttt;
113 hdr->datasn = cpu_to_be32(r2t->datasn);
114 r2t->datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500115 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie577577d2008-12-02 00:32:05 -0600116 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
117 hdr->itt = task->hdr_itt;
118 hdr->exp_statsn = r2t->exp_statsn;
119 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
120 if (left > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500121 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie577577d2008-12-02 00:32:05 -0600122 r2t->data_count = conn->max_xmit_dlength;
Mike Christie7996a772006-04-06 21:13:41 -0500123 hdr->flags = 0;
124 } else {
Mike Christie577577d2008-12-02 00:32:05 -0600125 hton24(hdr->dlength, left);
126 r2t->data_count = left;
Mike Christie7996a772006-04-06 21:13:41 -0500127 hdr->flags = ISCSI_FLAG_CMD_FINAL;
128 }
Mike Christie577577d2008-12-02 00:32:05 -0600129 conn->dataout_pdus_cnt++;
Mike Christie7996a772006-04-06 21:13:41 -0500130}
Mike Christie577577d2008-12-02 00:32:05 -0600131EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500132
Mike Christie9c19a7d2008-05-21 15:54:09 -0500133static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600134{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500135 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600136
Mike Christie9c19a7d2008-05-21 15:54:09 -0500137 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600138 WARN_ON(1);
139 return -EINVAL;
140 }
141
142 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500143 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600144 return 0;
145}
146
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500147/*
148 * make an extended cdb AHS
149 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500150static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500151{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500152 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500153 unsigned rlen, pad_len;
154 unsigned short ahslength;
155 struct iscsi_ecdb_ahdr *ecdb_ahdr;
156 int rc;
157
Mike Christie9c19a7d2008-05-21 15:54:09 -0500158 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500159 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
160
161 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
162 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
163
164 pad_len = iscsi_padding(rlen);
165
Mike Christie9c19a7d2008-05-21 15:54:09 -0500166 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500167 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
168 if (rc)
169 return rc;
170
171 if (pad_len)
172 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
173
174 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
175 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
176 ecdb_ahdr->reserved = 0;
177 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
178
179 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
180 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500181 cmd->cmd_len, rlen, pad_len, ahslength, task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500182
183 return 0;
184}
185
Mike Christie9c19a7d2008-05-21 15:54:09 -0500186static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500187{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500188 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500189 struct iscsi_rlength_ahdr *rlen_ahdr;
190 int rc;
191
Mike Christie9c19a7d2008-05-21 15:54:09 -0500192 rlen_ahdr = iscsi_next_hdr(task);
193 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500194 if (rc)
195 return rc;
196
197 rlen_ahdr->ahslength =
198 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
199 sizeof(rlen_ahdr->reserved));
200 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
201 rlen_ahdr->reserved = 0;
202 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
203
204 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
205 "rlen_ahdr->ahslength(%d)\n",
206 be32_to_cpu(rlen_ahdr->read_length),
207 be16_to_cpu(rlen_ahdr->ahslength));
208 return 0;
209}
210
Mike Christie7996a772006-04-06 21:13:41 -0500211/**
212 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500213 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500214 *
215 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
216 * fields like dlength or final based on how much data it sends
217 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500218static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500219{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500220 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500221 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500222 struct scsi_cmnd *sc = task->sc;
Mike Christie577577d2008-12-02 00:32:05 -0600223 struct iscsi_cmd *hdr;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500224 unsigned hdrlength, cmd_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600225 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500226
Mike Christie577577d2008-12-02 00:32:05 -0600227 rc = conn->session->tt->alloc_pdu(task);
228 if (rc)
229 return rc;
230 hdr = (struct iscsi_cmd *) task->hdr;
231 memset(hdr, 0, sizeof(*hdr));
232
Mike Christie9c19a7d2008-05-21 15:54:09 -0500233 task->hdr_len = 0;
234 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600235 if (rc)
236 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600237 hdr->opcode = ISCSI_OP_SCSI_CMD;
238 hdr->flags = ISCSI_ATTR_SIMPLE;
239 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie577577d2008-12-02 00:32:05 -0600240 memcpy(task->lun, hdr->lun, sizeof(task->lun));
241 hdr->itt = task->hdr_itt = build_itt(task->itt, session->age);
242 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600243 session->cmdsn++;
244 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500245 cmd_len = sc->cmd_len;
246 if (cmd_len < ISCSI_CDB_SIZE)
247 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
248 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500249 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500250 if (rc)
251 return rc;
252 cmd_len = ISCSI_CDB_SIZE;
253 }
254 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500255
Mike Christie9c19a7d2008-05-21 15:54:09 -0500256 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500257 if (scsi_bidi_cmnd(sc)) {
258 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500259 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500260 if (rc)
261 return rc;
262 }
Mike Christie7996a772006-04-06 21:13:41 -0500263 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500264 unsigned out_len = scsi_out(sc)->length;
Mike Christie577577d2008-12-02 00:32:05 -0600265 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
266
Boaz Harroshc07d4442008-04-18 10:11:52 -0500267 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500268 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
269 /*
270 * Write counters:
271 *
272 * imm_count bytes to be sent right after
273 * SCSI PDU Header
274 *
275 * unsol_count bytes(as Data-Out) to be sent
276 * without R2T ack right after
277 * immediate data
278 *
Mike Christie577577d2008-12-02 00:32:05 -0600279 * r2t data_length bytes to be sent via R2T ack's
Mike Christie7996a772006-04-06 21:13:41 -0500280 *
281 * pad_count bytes to be sent as zero-padding
282 */
Mike Christie577577d2008-12-02 00:32:05 -0600283 memset(r2t, 0, sizeof(*r2t));
Mike Christie7996a772006-04-06 21:13:41 -0500284
285 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500286 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500287 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500288 conn->max_xmit_dlength);
289 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500290 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500291 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500292 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500293 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600294 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500295
Mike Christieffd04362006-08-31 18:09:24 -0400296 if (!session->initial_r2t_en) {
Mike Christie577577d2008-12-02 00:32:05 -0600297 r2t->data_length = min(session->first_burst, out_len) -
298 task->imm_count;
299 r2t->data_offset = task->imm_count;
300 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
301 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400302 }
303
Mike Christie577577d2008-12-02 00:32:05 -0600304 if (!task->unsol_r2t.data_length)
Mike Christie7996a772006-04-06 21:13:41 -0500305 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600306 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500307 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500308 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
309 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500310 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500311
312 if (sc->sc_data_direction == DMA_FROM_DEVICE)
313 hdr->flags |= ISCSI_FLAG_CMD_READ;
314 }
315
Boaz Harrosh004d6532007-12-13 12:43:23 -0600316 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500317 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600318
319 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
320 hdrlength /= ISCSI_PAD_LEN;
321
322 WARN_ON(hdrlength >= 256);
323 hdr->hlength = hdrlength & 0xFF;
324
Mike Christie577577d2008-12-02 00:32:05 -0600325 if (session->tt->init_task && session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500326 return -EIO;
327
Mike Christie9c19a7d2008-05-21 15:54:09 -0500328 task->state = ISCSI_TASK_RUNNING;
329 list_move_tail(&task->running, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500330
Olaf Kircha8ac6312007-12-13 12:43:35 -0600331 conn->scsicmd_pdus_cnt++;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500332 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
333 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ?
334 "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ?
Mike Christie9c19a7d2008-05-21 15:54:09 -0500335 "write" : "read", conn->id, sc, sc->cmnd[0], task->itt,
Mike Christie3e5c28a2008-05-21 15:54:06 -0500336 scsi_bufflen(sc),
337 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
338 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600339 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500340}
Mike Christie7996a772006-04-06 21:13:41 -0500341
342/**
Mike Christie3e5c28a2008-05-21 15:54:06 -0500343 * iscsi_complete_command - finish a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500344 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500345 *
346 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500347 * This function returns the scsi command to scsi-ml or cleans
348 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500349 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500350static void iscsi_complete_command(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500351{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500352 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600353 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500354 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500355
Mike Christie577577d2008-12-02 00:32:05 -0600356 session->tt->cleanup_task(task);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500357 list_del_init(&task->running);
358 task->state = ISCSI_TASK_COMPLETED;
359 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500360
Mike Christie9c19a7d2008-05-21 15:54:09 -0500361 if (conn->task == task)
362 conn->task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500363 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500364 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500365 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500366 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500367 return;
368
Mike Christie9c19a7d2008-05-21 15:54:09 -0500369 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500370
Mike Christie9c19a7d2008-05-21 15:54:09 -0500371 if (conn->ping_task == task)
372 conn->ping_task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500373
374 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500375 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500376 /* SCSI eh reuses commands to verify us */
377 sc->SCp.ptr = NULL;
378 /*
379 * queue command may call this to free the task, but
380 * not have setup the sc callback
381 */
382 if (sc->scsi_done)
383 sc->scsi_done(sc);
384 }
Mike Christie7996a772006-04-06 21:13:41 -0500385}
386
Mike Christie913e5bf2008-05-21 15:54:18 -0500387void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400388{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500389 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400390}
Mike Christie913e5bf2008-05-21 15:54:18 -0500391EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400392
Mike Christie9c19a7d2008-05-21 15:54:09 -0500393static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400394{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500395 if (atomic_dec_and_test(&task->refcount))
396 iscsi_complete_command(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400397}
398
Mike Christie9c19a7d2008-05-21 15:54:09 -0500399void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500400{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500401 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500402
403 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500404 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500405 spin_unlock_bh(&session->lock);
406}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500407EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500408
Mike Christieb3a7ea82007-12-13 12:43:26 -0600409/*
410 * session lock must be held
411 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500412static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600413 int err)
414{
415 struct scsi_cmnd *sc;
416
Mike Christie9c19a7d2008-05-21 15:54:09 -0500417 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600418 if (!sc)
419 return;
420
Mike Christie9c19a7d2008-05-21 15:54:09 -0500421 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600422 /*
423 * cmd never made it to the xmit thread, so we should not count
424 * the cmd in the sequencing
425 */
426 conn->session->queued_cmdsn--;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600427
428 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500429 if (!scsi_bidi_cmnd(sc))
430 scsi_set_resid(sc, scsi_bufflen(sc));
431 else {
432 scsi_out(sc)->resid = scsi_out(sc)->length;
433 scsi_in(sc)->resid = scsi_in(sc)->length;
434 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500435
Mike Christie9c19a7d2008-05-21 15:54:09 -0500436 if (conn->task == task)
437 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600438 /* release ref from queuecommand */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500439 __iscsi_put_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600440}
441
Mike Christie3e5c28a2008-05-21 15:54:06 -0500442static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500443 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500444{
445 struct iscsi_session *session = conn->session;
Mike Christie577577d2008-12-02 00:32:05 -0600446 struct iscsi_hdr *hdr = task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500447 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
448
449 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
450 return -ENOTCONN;
451
452 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
453 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
454 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
455 /*
456 * pre-format CmdSN for outgoing PDU.
457 */
458 nop->cmdsn = cpu_to_be32(session->cmdsn);
459 if (hdr->itt != RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500460 hdr->itt = build_itt(task->itt, session->age);
Mike Christie052d0142008-05-21 15:54:05 -0500461 /*
462 * TODO: We always use immediate, so we never hit this.
463 * If we start to send tmfs or nops as non-immediate then
464 * we should start checking the cmdsn numbers for mgmt tasks.
465 */
466 if (conn->c_stage == ISCSI_CONN_STARTED &&
467 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
468 session->queued_cmdsn++;
469 session->cmdsn++;
470 }
471 }
472
Mike Christie3e5c28a2008-05-21 15:54:06 -0500473 if (session->tt->init_task)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500474 session->tt->init_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500475
476 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
477 session->state = ISCSI_STATE_LOGGING_OUT;
478
Mike Christie577577d2008-12-02 00:32:05 -0600479 task->state = ISCSI_TASK_RUNNING;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500480 list_move_tail(&task->running, &conn->mgmt_run_list);
Mike Christie052d0142008-05-21 15:54:05 -0500481 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
482 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500483 task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500484 return 0;
485}
486
Mike Christie9c19a7d2008-05-21 15:54:09 -0500487static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600488__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
489 char *data, uint32_t data_size)
490{
491 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500492 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600493
494 if (session->state == ISCSI_STATE_TERMINATE)
495 return NULL;
496
497 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
498 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
499 /*
500 * Login and Text are sent serially, in
501 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500502 * Same task can be used. Same ITT must be used.
503 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600504 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500505 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600506 else {
507 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
508 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
509
Mike Christie3e5c28a2008-05-21 15:54:06 -0500510 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500511 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600512 return NULL;
513 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500514 /*
515 * released in complete pdu for task we expect a response for, and
516 * released by the lld when it has transmitted the task for
517 * pdus we do not expect a response for.
518 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 atomic_set(&task->refcount, 1);
520 task->conn = conn;
521 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600522
523 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 memcpy(task->data, data, data_size);
525 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600526 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500527 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600528
Mike Christie577577d2008-12-02 00:32:05 -0600529 if (conn->session->tt->alloc_pdu(task)) {
530 iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
531 "pdu for mgmt task.\n");
532 goto requeue_task;
533 }
534 task->hdr_len = sizeof(struct iscsi_hdr);
535
Mike Christie9c19a7d2008-05-21 15:54:09 -0500536 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
537 INIT_LIST_HEAD(&task->running);
538 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500539
540 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -0600541 if (iscsi_prep_mgmt_task(conn, task))
542 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500543
Mike Christie9c19a7d2008-05-21 15:54:09 -0500544 if (session->tt->xmit_task(task))
Mike Christie577577d2008-12-02 00:32:05 -0600545 goto free_task;
Mike Christie052d0142008-05-21 15:54:05 -0500546
547 } else
548 scsi_queue_work(conn->session->host, &conn->xmitwork);
549
Mike Christie9c19a7d2008-05-21 15:54:09 -0500550 return task;
Mike Christie577577d2008-12-02 00:32:05 -0600551
552free_task:
553 __iscsi_put_task(task);
554 return NULL;
555
556requeue_task:
557 if (task != conn->login_task)
558 __kfifo_put(session->cmdpool.queue, (void*)&task,
559 sizeof(void*));
560 return NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600561}
562
563int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
564 char *data, uint32_t data_size)
565{
566 struct iscsi_conn *conn = cls_conn->dd_data;
567 struct iscsi_session *session = conn->session;
568 int err = 0;
569
570 spin_lock_bh(&session->lock);
571 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
572 err = -EPERM;
573 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600574 return err;
575}
576EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
577
Mike Christie7996a772006-04-06 21:13:41 -0500578/**
579 * iscsi_cmd_rsp - SCSI Command Response processing
580 * @conn: iscsi connection
581 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500582 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500583 * @data: cmd data buffer
584 * @datalen: len of buffer
585 *
586 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500587 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500588 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500589static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500590 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500591 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500592{
Mike Christie7996a772006-04-06 21:13:41 -0500593 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
594 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500595 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500596
Mike Christie77a23c22007-05-30 12:57:18 -0500597 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500598 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
599
600 sc->result = (DID_OK << 16) | rhdr->cmd_status;
601
602 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
603 sc->result = DID_ERROR << 16;
604 goto out;
605 }
606
607 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600608 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500609
610 if (datalen < 2) {
611invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600612 iscsi_conn_printk(KERN_ERR, conn,
613 "Got CHECK_CONDITION but invalid data "
614 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500615 sc->result = DID_BAD_TARGET << 16;
616 goto out;
617 }
618
Harvey Harrison8f333992008-05-21 15:54:20 -0500619 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500620 if (datalen < senselen)
621 goto invalid_datalen;
622
623 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600624 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500625 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600626 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500627 }
628
Boaz Harroshc07d4442008-04-18 10:11:52 -0500629 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
630 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
631 int res_count = be32_to_cpu(rhdr->bi_residual_count);
632
633 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
634 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
635 res_count <= scsi_in(sc)->length))
636 scsi_in(sc)->resid = res_count;
637 else
638 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
639 }
640
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600641 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
642 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500643 int res_count = be32_to_cpu(rhdr->residual_count);
644
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600645 if (res_count > 0 &&
646 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
647 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500648 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900649 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500650 else
651 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500652 }
Mike Christie7996a772006-04-06 21:13:41 -0500653out:
654 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500655 (long)sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500656 conn->scsirsp_pdus_cnt++;
657
Mike Christie9c19a7d2008-05-21 15:54:09 -0500658 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500659}
660
Mike Christie1d9edf02008-09-24 11:46:09 -0500661/**
662 * iscsi_data_in_rsp - SCSI Data-In Response processing
663 * @conn: iscsi connection
664 * @hdr: iscsi pdu
665 * @task: scsi command task
666 **/
667static void
668iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
669 struct iscsi_task *task)
670{
671 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
672 struct scsi_cmnd *sc = task->sc;
673
674 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
675 return;
676
677 sc->result = (DID_OK << 16) | rhdr->cmd_status;
678 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
679 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
680 ISCSI_FLAG_DATA_OVERFLOW)) {
681 int res_count = be32_to_cpu(rhdr->residual_count);
682
683 if (res_count > 0 &&
684 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
685 res_count <= scsi_in(sc)->length))
686 scsi_in(sc)->resid = res_count;
687 else
688 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
689 }
690
691 conn->scsirsp_pdus_cnt++;
692 __iscsi_put_task(task);
693}
694
Mike Christie7ea8b822006-07-24 15:47:22 -0500695static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
696{
697 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
698
699 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
700 conn->tmfrsp_pdus_cnt++;
701
Mike Christie843c0a82007-12-13 12:43:20 -0600702 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500703 return;
704
705 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600706 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500707 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600708 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500709 else
Mike Christie843c0a82007-12-13 12:43:20 -0600710 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500711 wake_up(&conn->ehwait);
712}
713
Mike Christief6d51802007-12-13 12:43:30 -0600714static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
715{
716 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500717 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600718
Mike Christie9c19a7d2008-05-21 15:54:09 -0500719 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600720 return;
721
722 memset(&hdr, 0, sizeof(struct iscsi_nopout));
723 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
724 hdr.flags = ISCSI_FLAG_CMD_FINAL;
725
726 if (rhdr) {
727 memcpy(hdr.lun, rhdr->lun, 8);
728 hdr.ttt = rhdr->ttt;
729 hdr.itt = RESERVED_ITT;
730 } else
731 hdr.ttt = RESERVED_ITT;
732
Mike Christie9c19a7d2008-05-21 15:54:09 -0500733 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
734 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600735 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600736 else if (!rhdr) {
737 /* only track our nops */
738 conn->ping_task = task;
739 conn->last_ping = jiffies;
740 }
Mike Christief6d51802007-12-13 12:43:30 -0600741}
742
Mike Christie62f38302006-08-31 18:09:27 -0400743static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
744 char *data, int datalen)
745{
746 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
747 struct iscsi_hdr rejected_pdu;
748 uint32_t itt;
749
750 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
751
752 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
753 if (ntoh24(reject->dlength) > datalen)
754 return ISCSI_ERR_PROTO;
755
756 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
757 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000758 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600759 iscsi_conn_printk(KERN_ERR, conn,
760 "itt 0x%x had pdu (op 0x%x) rejected "
761 "due to DataDigest error.\n", itt,
762 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400763 }
764 }
765 return 0;
766}
767
Mike Christie7996a772006-04-06 21:13:41 -0500768/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500769 * iscsi_itt_to_task - look up task by itt
770 * @conn: iscsi connection
771 * @itt: itt
772 *
773 * This should be used for mgmt tasks like login and nops, or if
774 * the LDD's itt space does not include the session age.
775 *
776 * The session lock must be held.
777 */
778static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
779{
780 struct iscsi_session *session = conn->session;
781 uint32_t i;
782
783 if (itt == RESERVED_ITT)
784 return NULL;
785
786 i = get_itt(itt);
787 if (i >= session->cmds_max)
788 return NULL;
789
790 return session->cmds[i];
791}
792
793/**
Mike Christie7996a772006-04-06 21:13:41 -0500794 * __iscsi_complete_pdu - complete pdu
795 * @conn: iscsi conn
796 * @hdr: iscsi header
797 * @data: data buffer
798 * @datalen: len of data buffer
799 *
800 * Completes pdu processing by freeing any resources allocated at
801 * queuecommand or send generic. session lock must be held and verify
802 * itt must have been called.
803 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500804int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
805 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500806{
807 struct iscsi_session *session = conn->session;
808 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500809 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500810 uint32_t itt;
811
Mike Christief6d51802007-12-13 12:43:30 -0600812 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500813 rc = iscsi_verify_itt(conn, hdr->itt);
814 if (rc)
815 return rc;
816
Al Virob4377352007-02-09 16:39:40 +0000817 if (hdr->itt != RESERVED_ITT)
818 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500819 else
Al Virob4377352007-02-09 16:39:40 +0000820 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500821
Mike Christie3e5c28a2008-05-21 15:54:06 -0500822 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
823 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500824
Mike Christie3e5c28a2008-05-21 15:54:06 -0500825 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500826 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400827
Mike Christie7996a772006-04-06 21:13:41 -0500828 switch(opcode) {
829 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500830 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500831 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500832 break;
833 }
834
Al Virob4377352007-02-09 16:39:40 +0000835 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500836 break;
837
Mike Christief6d51802007-12-13 12:43:30 -0600838 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500839 break;
840 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400841 rc = iscsi_handle_reject(conn, hdr, data, datalen);
842 break;
Mike Christie7996a772006-04-06 21:13:41 -0500843 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500844 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400845 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
846 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500847 break;
848 default:
849 rc = ISCSI_ERR_BAD_OPCODE;
850 break;
851 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500852 goto out;
853 }
Mike Christie7996a772006-04-06 21:13:41 -0500854
Mike Christie3e5c28a2008-05-21 15:54:06 -0500855 switch(opcode) {
856 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500857 case ISCSI_OP_SCSI_DATA_IN:
858 task = iscsi_itt_to_ctask(conn, hdr->itt);
859 if (!task)
860 return ISCSI_ERR_BAD_ITT;
861 break;
862 case ISCSI_OP_R2T:
863 /*
864 * LLD handles R2Ts if they need to.
865 */
866 return 0;
867 case ISCSI_OP_LOGOUT_RSP:
868 case ISCSI_OP_LOGIN_RSP:
869 case ISCSI_OP_TEXT_RSP:
870 case ISCSI_OP_SCSI_TMFUNC_RSP:
871 case ISCSI_OP_NOOP_IN:
872 task = iscsi_itt_to_task(conn, hdr->itt);
873 if (!task)
874 return ISCSI_ERR_BAD_ITT;
875 break;
876 default:
877 return ISCSI_ERR_BAD_OPCODE;
878 }
879
880 switch(opcode) {
881 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500882 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500883 break;
884 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500885 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500886 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500887 case ISCSI_OP_LOGOUT_RSP:
888 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
889 if (datalen) {
890 rc = ISCSI_ERR_PROTO;
891 break;
892 }
893 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
894 goto recv_pdu;
895 case ISCSI_OP_LOGIN_RSP:
896 case ISCSI_OP_TEXT_RSP:
897 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
898 /*
899 * login related PDU's exp_statsn is handled in
900 * userspace
901 */
902 goto recv_pdu;
903 case ISCSI_OP_SCSI_TMFUNC_RSP:
904 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
905 if (datalen) {
906 rc = ISCSI_ERR_PROTO;
907 break;
908 }
909
910 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500911 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500912 break;
913 case ISCSI_OP_NOOP_IN:
914 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
915 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
916 rc = ISCSI_ERR_PROTO;
917 break;
918 }
919 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
920
Mike Christie9c19a7d2008-05-21 15:54:09 -0500921 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500922 /*
923 * If this is not in response to one of our
924 * nops then it must be from userspace.
925 */
926 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500927
928 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
929 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500930 break;
931 default:
932 rc = ISCSI_ERR_BAD_OPCODE;
933 break;
934 }
935
936out:
937 return rc;
938recv_pdu:
939 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
940 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500941 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500942 return rc;
943}
Mike Christie913e5bf2008-05-21 15:54:18 -0500944EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500945
946int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
947 char *data, int datalen)
948{
949 int rc;
950
951 spin_lock(&conn->session->lock);
952 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
953 spin_unlock(&conn->session->lock);
954 return rc;
955}
956EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
957
Mike Christie0af967f2008-05-21 15:54:04 -0500958int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500959{
960 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500961 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500962
Mike Christie0af967f2008-05-21 15:54:04 -0500963 if (itt == RESERVED_ITT)
964 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500965
Mike Christie0af967f2008-05-21 15:54:04 -0500966 if (((__force u32)itt & ISCSI_AGE_MASK) !=
967 (session->age << ISCSI_AGE_SHIFT)) {
968 iscsi_conn_printk(KERN_ERR, conn,
969 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -0500970 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500971 return ISCSI_ERR_BAD_ITT;
972 }
Mike Christie7996a772006-04-06 21:13:41 -0500973
Mike Christie3e5c28a2008-05-21 15:54:06 -0500974 i = get_itt(itt);
975 if (i >= session->cmds_max) {
976 iscsi_conn_printk(KERN_ERR, conn,
977 "received invalid itt index %u (max cmds "
978 "%u.\n", i, session->cmds_max);
979 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500980 }
Mike Christie7996a772006-04-06 21:13:41 -0500981 return 0;
982}
983EXPORT_SYMBOL_GPL(iscsi_verify_itt);
984
Mike Christie913e5bf2008-05-21 15:54:18 -0500985/**
986 * iscsi_itt_to_ctask - look up ctask by itt
987 * @conn: iscsi connection
988 * @itt: itt
989 *
990 * This should be used for cmd tasks.
991 *
992 * The session lock must be held.
993 */
994struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -0500995{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500996 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -0500997
998 if (iscsi_verify_itt(conn, itt))
999 return NULL;
1000
Mike Christie913e5bf2008-05-21 15:54:18 -05001001 task = iscsi_itt_to_task(conn, itt);
1002 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -05001003 return NULL;
1004
Mike Christie913e5bf2008-05-21 15:54:18 -05001005 if (task->sc->SCp.phase != conn->session->age) {
1006 iscsi_session_printk(KERN_ERR, conn->session,
1007 "task's session age %d, expected %d\n",
1008 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -05001009 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -05001010 }
Mike Christie0af967f2008-05-21 15:54:04 -05001011
Mike Christie9c19a7d2008-05-21 15:54:09 -05001012 return task;
Mike Christie0af967f2008-05-21 15:54:04 -05001013}
1014EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1015
Mike Christiee5bd7b52008-09-24 11:46:10 -05001016void iscsi_session_failure(struct iscsi_cls_session *cls_session,
1017 enum iscsi_err err)
1018{
1019 struct iscsi_session *session = cls_session->dd_data;
1020 struct iscsi_conn *conn;
1021 struct device *dev;
1022 unsigned long flags;
1023
1024 spin_lock_irqsave(&session->lock, flags);
1025 conn = session->leadconn;
1026 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1027 spin_unlock_irqrestore(&session->lock, flags);
1028 return;
1029 }
1030
1031 dev = get_device(&conn->cls_conn->dev);
1032 spin_unlock_irqrestore(&session->lock, flags);
1033 if (!dev)
1034 return;
1035 /*
1036 * if the host is being removed bypass the connection
1037 * recovery initialization because we are going to kill
1038 * the session.
1039 */
1040 if (err == ISCSI_ERR_INVALID_HOST)
1041 iscsi_conn_error_event(conn->cls_conn, err);
1042 else
1043 iscsi_conn_failure(conn, err);
1044 put_device(dev);
1045}
1046EXPORT_SYMBOL_GPL(iscsi_session_failure);
1047
Mike Christie7996a772006-04-06 21:13:41 -05001048void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1049{
1050 struct iscsi_session *session = conn->session;
1051 unsigned long flags;
1052
1053 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001054 if (session->state == ISCSI_STATE_FAILED) {
1055 spin_unlock_irqrestore(&session->lock, flags);
1056 return;
1057 }
1058
Mike Christie67a61112006-05-30 00:37:20 -05001059 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001060 session->state = ISCSI_STATE_FAILED;
1061 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001062
Mike Christie7996a772006-04-06 21:13:41 -05001063 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1064 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001065 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001066}
1067EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1068
Mike Christie77a23c22007-05-30 12:57:18 -05001069static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1070{
1071 struct iscsi_session *session = conn->session;
1072
1073 /*
1074 * Check for iSCSI window and take care of CmdSN wrap-around
1075 */
Mike Christiee0726402007-07-26 12:46:48 -05001076 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1077 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
1078 "CmdSN %u/%u\n", session->exp_cmdsn,
1079 session->max_cmdsn, session->cmdsn,
1080 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001081 return -ENOSPC;
1082 }
1083 return 0;
1084}
1085
Mike Christie9c19a7d2008-05-21 15:54:09 -05001086static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001087{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001088 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001089 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001090
Mike Christie9c19a7d2008-05-21 15:54:09 -05001091 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001092 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001093 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001094 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001095 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001096 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001097 /* done with this task */
1098 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001099 return rc;
1100}
1101
Mike Christie7996a772006-04-06 21:13:41 -05001102/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001103 * iscsi_requeue_task - requeue task to run from session workqueue
1104 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001105 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001106 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001107 * this. The session lock must be held. This should only be called
1108 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001109 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001110void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001111{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001112 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001113
Mike Christie9c19a7d2008-05-21 15:54:09 -05001114 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -06001115 scsi_queue_work(conn->session->host, &conn->xmitwork);
1116}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001117EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001118
1119/**
Mike Christie7996a772006-04-06 21:13:41 -05001120 * iscsi_data_xmit - xmit any command into the scheduled connection
1121 * @conn: iscsi connection
1122 *
1123 * Notes:
1124 * The function can return -EAGAIN in which case the caller must
1125 * re-schedule it again later or recover. '0' return code means
1126 * successful xmit.
1127 **/
1128static int iscsi_data_xmit(struct iscsi_conn *conn)
1129{
Mike Christie3219e522006-05-30 00:37:28 -05001130 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001131
Mike Christie77a23c22007-05-30 12:57:18 -05001132 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001133 if (unlikely(conn->suspend_tx)) {
1134 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001135 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001136 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001137 }
Mike Christie7996a772006-04-06 21:13:41 -05001138
Mike Christie9c19a7d2008-05-21 15:54:09 -05001139 if (conn->task) {
1140 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001141 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001142 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001143 }
1144
Mike Christie77a23c22007-05-30 12:57:18 -05001145 /*
1146 * process mgmt pdus like nops before commands since we should
1147 * only have one nop-out as a ping from us and targets should not
1148 * overflow us with nop-ins
1149 */
1150check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001151 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001152 conn->task = list_entry(conn->mgmtqueue.next,
1153 struct iscsi_task, running);
1154 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1155 __iscsi_put_task(conn->task);
1156 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001157 continue;
1158 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001159 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001160 if (rc)
1161 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001162 }
1163
Mike Christie843c0a82007-12-13 12:43:20 -06001164 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001165 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001166 if (conn->tmf_state == TMF_QUEUED)
1167 break;
1168
Mike Christie9c19a7d2008-05-21 15:54:09 -05001169 conn->task = list_entry(conn->xmitqueue.next,
1170 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001171 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001172 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001173 continue;
1174 }
Mike Christie577577d2008-12-02 00:32:05 -06001175 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1176 if (rc) {
1177 if (rc == -ENOMEM) {
1178 conn->task = NULL;
1179 goto again;
1180 } else
1181 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001182 continue;
1183 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001184 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001185 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001186 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001187 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001188 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001189 * we need to check the mgmt queue for nops that need to
1190 * be sent to aviod starvation
1191 */
Mike Christie843c0a82007-12-13 12:43:20 -06001192 if (!list_empty(&conn->mgmtqueue))
1193 goto check_mgmt;
1194 }
1195
1196 while (!list_empty(&conn->requeue)) {
1197 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1198 break;
1199
Mike Christieb3a7ea82007-12-13 12:43:26 -06001200 /*
1201 * we always do fastlogout - conn stop code will clean up.
1202 */
1203 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1204 break;
1205
Mike Christie9c19a7d2008-05-21 15:54:09 -05001206 conn->task = list_entry(conn->requeue.next,
1207 struct iscsi_task, running);
1208 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001209 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001210 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001211 if (rc)
1212 goto again;
1213 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001214 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001215 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001216 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001217 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001218
1219again:
1220 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001221 rc = -ENODATA;
1222 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001223 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001224}
1225
David Howellsc4028952006-11-22 14:57:56 +00001226static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001227{
David Howellsc4028952006-11-22 14:57:56 +00001228 struct iscsi_conn *conn =
1229 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001230 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001231 /*
1232 * serialize Xmit worker on a per-connection basis.
1233 */
Mike Christie3219e522006-05-30 00:37:28 -05001234 do {
1235 rc = iscsi_data_xmit(conn);
1236 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001237}
1238
Mike Christie577577d2008-12-02 00:32:05 -06001239static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1240 struct scsi_cmnd *sc)
1241{
1242 struct iscsi_task *task;
1243
1244 if (!__kfifo_get(conn->session->cmdpool.queue,
1245 (void *) &task, sizeof(void *)))
1246 return NULL;
1247
1248 sc->SCp.phase = conn->session->age;
1249 sc->SCp.ptr = (char *) task;
1250
1251 atomic_set(&task->refcount, 1);
1252 task->state = ISCSI_TASK_PENDING;
1253 task->conn = conn;
1254 task->sc = sc;
1255 INIT_LIST_HEAD(&task->running);
1256 return task;
1257}
1258
Mike Christie7996a772006-04-06 21:13:41 -05001259enum {
1260 FAILURE_BAD_HOST = 1,
1261 FAILURE_SESSION_FAILED,
1262 FAILURE_SESSION_FREED,
1263 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001264 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001265 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001266 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001267 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001268 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001269 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001270};
1271
1272int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1273{
Mike Christie75613522008-05-21 15:53:59 -05001274 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001275 struct Scsi_Host *host;
1276 int reason = 0;
1277 struct iscsi_session *session;
1278 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001279 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001280
1281 sc->scsi_done = done;
1282 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001283 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001284
1285 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001286 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001287
Mike Christie75613522008-05-21 15:53:59 -05001288 cls_session = starget_to_session(scsi_target(sc->device));
1289 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001290 spin_lock(&session->lock);
1291
Mike Christie75613522008-05-21 15:53:59 -05001292 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001293 if (reason) {
1294 sc->result = reason;
1295 goto fault;
1296 }
1297
Mike Christie656cffc2006-05-18 20:31:42 -05001298 /*
1299 * ISCSI_STATE_FAILED is a temp. state. The recovery
1300 * code will decide what is best to do with command queued
1301 * during this time
1302 */
1303 if (session->state != ISCSI_STATE_LOGGED_IN &&
1304 session->state != ISCSI_STATE_FAILED) {
1305 /*
1306 * to handle the race between when we set the recovery state
1307 * and block the session we requeue here (commands could
1308 * be entering our queuecommand while a block is starting
1309 * up because the block code is not locked)
1310 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001311 switch (session->state) {
1312 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001313 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christied6d13ee2008-08-17 15:24:43 -05001314 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001315 case ISCSI_STATE_LOGGING_OUT:
1316 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christied6d13ee2008-08-17 15:24:43 -05001317 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001318 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001319 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001320 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001321 break;
1322 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001323 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001324 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001325 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001326 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001327 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001328 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001329 }
Mike Christie7996a772006-04-06 21:13:41 -05001330 goto fault;
1331 }
1332
Mike Christie7996a772006-04-06 21:13:41 -05001333 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001334 if (!conn) {
1335 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001336 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001337 goto fault;
1338 }
Mike Christie7996a772006-04-06 21:13:41 -05001339
Mike Christie77a23c22007-05-30 12:57:18 -05001340 if (iscsi_check_cmdsn_window_closed(conn)) {
1341 reason = FAILURE_WINDOW_CLOSED;
1342 goto reject;
1343 }
1344
Mike Christie577577d2008-12-02 00:32:05 -06001345 task = iscsi_alloc_task(conn, sc);
1346 if (!task) {
Mike Christie60ecebf2006-08-31 18:09:25 -04001347 reason = FAILURE_OOM;
1348 goto reject;
1349 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001350 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001351
Mike Christie052d0142008-05-21 15:54:05 -05001352 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie577577d2008-12-02 00:32:05 -06001353 reason = iscsi_prep_scsi_cmd_pdu(task);
1354 if (reason) {
1355 if (reason == -ENOMEM) {
1356 reason = FAILURE_OOM;
1357 goto prepd_reject;
1358 } else {
1359 sc->result = DID_ABORT << 16;
1360 goto prepd_fault;
1361 }
Mike Christie052d0142008-05-21 15:54:05 -05001362 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001363 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001364 reason = FAILURE_SESSION_NOT_READY;
Mike Christie577577d2008-12-02 00:32:05 -06001365 goto prepd_reject;
Mike Christie052d0142008-05-21 15:54:05 -05001366 }
1367 } else
1368 scsi_queue_work(session->host, &conn->xmitwork);
1369
1370 session->queued_cmdsn++;
1371 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001372 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001373 return 0;
1374
Mike Christie577577d2008-12-02 00:32:05 -06001375prepd_reject:
1376 sc->scsi_done = NULL;
1377 iscsi_complete_command(task);
Mike Christie7996a772006-04-06 21:13:41 -05001378reject:
1379 spin_unlock(&session->lock);
1380 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001381 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001382 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001383
Mike Christie577577d2008-12-02 00:32:05 -06001384prepd_fault:
1385 sc->scsi_done = NULL;
1386 iscsi_complete_command(task);
Mike Christie7996a772006-04-06 21:13:41 -05001387fault:
1388 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001389 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001390 if (!scsi_bidi_cmnd(sc))
1391 scsi_set_resid(sc, scsi_bufflen(sc));
1392 else {
1393 scsi_out(sc)->resid = scsi_out(sc)->length;
1394 scsi_in(sc)->resid = scsi_in(sc)->length;
1395 }
Mike Christie052d0142008-05-21 15:54:05 -05001396 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001397 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001398 return 0;
1399}
1400EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1401
1402int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1403{
1404 if (depth > ISCSI_MAX_CMD_PER_LUN)
1405 depth = ISCSI_MAX_CMD_PER_LUN;
1406 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1407 return sdev->queue_depth;
1408}
1409EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1410
Mike Christie7996a772006-04-06 21:13:41 -05001411void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1412{
Mike Christie75613522008-05-21 15:53:59 -05001413 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001414
1415 spin_lock_bh(&session->lock);
1416 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001417 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001418 if (session->leadconn)
1419 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001420 }
1421 spin_unlock_bh(&session->lock);
1422}
1423EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1424
Mike Christie8e124522008-09-24 11:46:12 -05001425int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001426{
Mike Christie75613522008-05-21 15:53:59 -05001427 struct iscsi_cls_session *cls_session;
1428 struct iscsi_session *session;
1429 struct iscsi_conn *conn;
1430
1431 cls_session = starget_to_session(scsi_target(sc->device));
1432 session = cls_session->dd_data;
1433 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001434
Mike Christiebc436b22007-12-13 12:43:28 -06001435 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001436 spin_lock_bh(&session->lock);
1437 if (session->state == ISCSI_STATE_TERMINATE) {
1438failed:
Mike Christie8e124522008-09-24 11:46:12 -05001439 debug_scsi("failing target reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001440 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001441 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001442 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001443 return FAILED;
1444 }
1445
Mike Christie7996a772006-04-06 21:13:41 -05001446 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001447 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001448 /*
1449 * we drop the lock here but the leadconn cannot be destoyed while
1450 * we are in the scsi eh
1451 */
Mike Christie843c0a82007-12-13 12:43:20 -06001452 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001453
Mike Christie8e124522008-09-24 11:46:12 -05001454 debug_scsi("iscsi_eh_target_reset wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001455 wait_event_interruptible(conn->ehwait,
1456 session->state == ISCSI_STATE_TERMINATE ||
1457 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001458 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001459 if (signal_pending(current))
1460 flush_signals(current);
1461
Mike Christiebc436b22007-12-13 12:43:28 -06001462 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001463 spin_lock_bh(&session->lock);
1464 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001465 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001466 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001467 else
1468 goto failed;
1469 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001470 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001471 return SUCCESS;
1472}
Mike Christie8e124522008-09-24 11:46:12 -05001473EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001474
Mike Christie843c0a82007-12-13 12:43:20 -06001475static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001476{
Mike Christie843c0a82007-12-13 12:43:20 -06001477 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001478 struct iscsi_session *session = conn->session;
1479
1480 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001481 if (conn->tmf_state == TMF_QUEUED) {
1482 conn->tmf_state = TMF_TIMEDOUT;
1483 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001484 /* unblock eh_abort() */
1485 wake_up(&conn->ehwait);
1486 }
1487 spin_unlock(&session->lock);
1488}
1489
Mike Christie9c19a7d2008-05-21 15:54:09 -05001490static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001491 struct iscsi_tm *hdr, int age,
1492 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001493{
Mike Christie7996a772006-04-06 21:13:41 -05001494 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001495 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001496
Mike Christie9c19a7d2008-05-21 15:54:09 -05001497 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001498 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001499 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001500 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001501 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001502 spin_lock_bh(&session->lock);
1503 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001504 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001505 }
Mike Christie843c0a82007-12-13 12:43:20 -06001506 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001507 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001508 conn->tmf_timer.function = iscsi_tmf_timedout;
1509 conn->tmf_timer.data = (unsigned long)conn;
1510 add_timer(&conn->tmf_timer);
1511 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001512
Mike Christie7996a772006-04-06 21:13:41 -05001513 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001514 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001515
1516 /*
1517 * block eh thread until:
1518 *
Mike Christie843c0a82007-12-13 12:43:20 -06001519 * 1) tmf response
1520 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001521 * 3) session is terminated or restarted or userspace has
1522 * given up on recovery
1523 */
Mike Christie843c0a82007-12-13 12:43:20 -06001524 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001525 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001526 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001527 if (signal_pending(current))
1528 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001529 del_timer_sync(&conn->tmf_timer);
1530
Mike Christie6724add2007-08-15 01:38:30 -05001531 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001532 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001533 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001534 if (age != session->age ||
1535 session->state != ISCSI_STATE_LOGGED_IN)
1536 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001537 return 0;
1538}
1539
1540/*
Mike Christie843c0a82007-12-13 12:43:20 -06001541 * Fail commands. session lock held and recv side suspended and xmit
1542 * thread flushed
1543 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001544static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1545 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001546{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001547 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001548
Mike Christie9c19a7d2008-05-21 15:54:09 -05001549 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1550 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001551
1552 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001553 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1554 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001555 debug_scsi("failing pending sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001556 task->sc, task->itt);
1557 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001558 }
1559 }
1560
Mike Christie9c19a7d2008-05-21 15:54:09 -05001561 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1562 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001563 debug_scsi("failing requeued sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001564 task->sc, task->itt);
1565 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001566 }
1567 }
1568
1569 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001570 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1571 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001572 debug_scsi("failing in progress sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001573 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001574 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001575 }
1576 }
1577}
1578
Mike Christieb40977d2008-05-21 15:54:03 -05001579void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001580{
1581 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001582 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1583 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001584}
Mike Christieb40977d2008-05-21 15:54:03 -05001585EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001586
1587static void iscsi_start_tx(struct iscsi_conn *conn)
1588{
1589 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001590 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1591 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001592}
1593
Jens Axboe242f9dc2008-09-14 05:55:09 -07001594static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001595{
1596 struct iscsi_cls_session *cls_session;
1597 struct iscsi_session *session;
1598 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001599 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001600
1601 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001602 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001603
1604 debug_scsi("scsi cmd %p timedout\n", scmd);
1605
1606 spin_lock(&session->lock);
1607 if (session->state != ISCSI_STATE_LOGGED_IN) {
1608 /*
1609 * We are probably in the middle of iscsi recovery so let
1610 * that complete and handle the error.
1611 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001612 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001613 goto done;
1614 }
1615
1616 conn = session->leadconn;
1617 if (!conn) {
1618 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001619 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001620 goto done;
1621 }
1622
1623 if (!conn->recv_timeout && !conn->ping_timeout)
1624 goto done;
1625 /*
1626 * if the ping timedout then we are in the middle of cleaning up
1627 * and can let the iscsi eh handle it
1628 */
1629 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1630 (conn->ping_timeout * HZ), jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001631 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001632 /*
1633 * if we are about to check the transport then give the command
1634 * more time
1635 */
1636 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1637 jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001638 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001639 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001640 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001641 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001642done:
1643 spin_unlock(&session->lock);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001644 debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
1645 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001646 return rc;
1647}
1648
1649static void iscsi_check_transport_timeouts(unsigned long data)
1650{
1651 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1652 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001653 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001654
1655 spin_lock(&session->lock);
1656 if (session->state != ISCSI_STATE_LOGGED_IN)
1657 goto done;
1658
Mike Christie4cf10432008-05-07 20:43:52 -05001659 recv_timeout = conn->recv_timeout;
1660 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001661 goto done;
1662
Mike Christie4cf10432008-05-07 20:43:52 -05001663 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001664 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001665 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001666 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001667 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001668 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1669 "expired, last rx %lu, last ping %lu, "
1670 "now %lu\n", conn->ping_timeout, last_recv,
1671 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001672 spin_unlock(&session->lock);
1673 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1674 return;
1675 }
1676
Mike Christie4cf10432008-05-07 20:43:52 -05001677 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001678 /* send a ping to try to provoke some traffic */
1679 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1680 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001681 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001682 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001683 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001684
Mike Christiead294e92008-01-31 13:36:50 -06001685 debug_scsi("Setting next tmo %lu\n", next_timeout);
1686 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001687done:
1688 spin_unlock(&session->lock);
1689}
1690
Mike Christie9c19a7d2008-05-21 15:54:09 -05001691static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001692 struct iscsi_tm *hdr)
1693{
1694 memset(hdr, 0, sizeof(*hdr));
1695 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1696 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1697 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie577577d2008-12-02 00:32:05 -06001698 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1699 hdr->rtt = task->hdr_itt;
1700 hdr->refcmdsn = task->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001701}
1702
Mike Christie7996a772006-04-06 21:13:41 -05001703int iscsi_eh_abort(struct scsi_cmnd *sc)
1704{
Mike Christie75613522008-05-21 15:53:59 -05001705 struct iscsi_cls_session *cls_session;
1706 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001707 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001708 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001709 struct iscsi_tm *hdr;
1710 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001711
Mike Christie75613522008-05-21 15:53:59 -05001712 cls_session = starget_to_session(scsi_target(sc->device));
1713 session = cls_session->dd_data;
1714
Mike Christie6724add2007-08-15 01:38:30 -05001715 mutex_lock(&session->eh_mutex);
1716 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001717 /*
1718 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1719 * got the command.
1720 */
1721 if (!sc->SCp.ptr) {
1722 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001723 spin_unlock_bh(&session->lock);
1724 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001725 return SUCCESS;
1726 }
1727
Mike Christie7996a772006-04-06 21:13:41 -05001728 /*
1729 * If we are not logged in or we have started a new session
1730 * then let the host reset code handle this
1731 */
Mike Christie843c0a82007-12-13 12:43:20 -06001732 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1733 sc->SCp.phase != session->age) {
1734 spin_unlock_bh(&session->lock);
1735 mutex_unlock(&session->eh_mutex);
1736 return FAILED;
1737 }
1738
1739 conn = session->leadconn;
1740 conn->eh_abort_cnt++;
1741 age = session->age;
1742
Mike Christie9c19a7d2008-05-21 15:54:09 -05001743 task = (struct iscsi_task *)sc->SCp.ptr;
1744 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001745
Mike Christie9c19a7d2008-05-21 15:54:09 -05001746 /* task completed before time out */
1747 if (!task->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001748 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001749 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001750 }
Mike Christie7996a772006-04-06 21:13:41 -05001751
Mike Christie9c19a7d2008-05-21 15:54:09 -05001752 if (task->state == ISCSI_TASK_PENDING) {
1753 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001754 goto success;
1755 }
Mike Christie7996a772006-04-06 21:13:41 -05001756
Mike Christie843c0a82007-12-13 12:43:20 -06001757 /* only have one tmf outstanding at a time */
1758 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001759 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001760 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001761
Mike Christie843c0a82007-12-13 12:43:20 -06001762 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001763 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001764
Mike Christie9c19a7d2008-05-21 15:54:09 -05001765 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001766 rc = FAILED;
1767 goto failed;
1768 }
1769
1770 switch (conn->tmf_state) {
1771 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001772 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001773 /*
1774 * stop tx side incase the target had sent a abort rsp but
1775 * the initiator was still writing out data.
1776 */
Mike Christie6724add2007-08-15 01:38:30 -05001777 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001778 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001779 * we do not stop the recv side because targets have been
1780 * good and have never sent us a successful tmf response
1781 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001782 */
Mike Christie77a23c22007-05-30 12:57:18 -05001783 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001784 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001785 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001786 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001787 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001788 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001789 case TMF_TIMEDOUT:
1790 spin_unlock_bh(&session->lock);
1791 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1792 goto failed_unlocked;
1793 case TMF_NOT_FOUND:
1794 if (!sc->SCp.ptr) {
1795 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001796 /* task completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001797 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001798 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001799 }
1800 /* fall through */
1801 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001802 conn->tmf_state = TMF_INITIAL;
1803 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001804 }
1805
Mike Christie77a23c22007-05-30 12:57:18 -05001806success:
Mike Christie7996a772006-04-06 21:13:41 -05001807 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001808success_unlocked:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001809 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001810 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001811 return SUCCESS;
1812
1813failed:
1814 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001815failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001816 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001817 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001818 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001819 return FAILED;
1820}
1821EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1822
Mike Christie843c0a82007-12-13 12:43:20 -06001823static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1824{
1825 memset(hdr, 0, sizeof(*hdr));
1826 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1827 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1828 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1829 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001830 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001831}
1832
1833int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1834{
Mike Christie75613522008-05-21 15:53:59 -05001835 struct iscsi_cls_session *cls_session;
1836 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001837 struct iscsi_conn *conn;
1838 struct iscsi_tm *hdr;
1839 int rc = FAILED;
1840
Mike Christie75613522008-05-21 15:53:59 -05001841 cls_session = starget_to_session(scsi_target(sc->device));
1842 session = cls_session->dd_data;
1843
Mike Christie843c0a82007-12-13 12:43:20 -06001844 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1845
1846 mutex_lock(&session->eh_mutex);
1847 spin_lock_bh(&session->lock);
1848 /*
1849 * Just check if we are not logged in. We cannot check for
1850 * the phase because the reset could come from a ioctl.
1851 */
1852 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1853 goto unlock;
1854 conn = session->leadconn;
1855
1856 /* only have one tmf outstanding at a time */
1857 if (conn->tmf_state != TMF_INITIAL)
1858 goto unlock;
1859 conn->tmf_state = TMF_QUEUED;
1860
1861 hdr = &conn->tmhdr;
1862 iscsi_prep_lun_reset_pdu(sc, hdr);
1863
Mike Christie9c19a7d2008-05-21 15:54:09 -05001864 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001865 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001866 rc = FAILED;
1867 goto unlock;
1868 }
1869
1870 switch (conn->tmf_state) {
1871 case TMF_SUCCESS:
1872 break;
1873 case TMF_TIMEDOUT:
1874 spin_unlock_bh(&session->lock);
1875 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1876 goto done;
1877 default:
1878 conn->tmf_state = TMF_INITIAL;
1879 goto unlock;
1880 }
1881
1882 rc = SUCCESS;
1883 spin_unlock_bh(&session->lock);
1884
1885 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001886
Mike Christiea3439142008-09-24 11:46:15 -05001887 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001888 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001889 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05001890 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001891
1892 iscsi_start_tx(conn);
1893 goto done;
1894
1895unlock:
1896 spin_unlock_bh(&session->lock);
1897done:
1898 debug_scsi("iscsi_eh_device_reset %s\n",
1899 rc == SUCCESS ? "SUCCESS" : "FAILED");
1900 mutex_unlock(&session->eh_mutex);
1901 return rc;
1902}
1903EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1904
Olaf Kirch63203772007-12-13 12:43:25 -06001905/*
1906 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1907 * should be accessed via kfifo_{get,put} on q->queue.
1908 * Optionally, the caller can obtain the array of object pointers
1909 * by passing in a non-NULL @items pointer
1910 */
Mike Christie7996a772006-04-06 21:13:41 -05001911int
Olaf Kirch63203772007-12-13 12:43:25 -06001912iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001913{
Olaf Kirch63203772007-12-13 12:43:25 -06001914 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001915
Olaf Kirch63203772007-12-13 12:43:25 -06001916 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001917
1918 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001919
1920 /* If the user passed an items pointer, he wants a copy of
1921 * the array. */
1922 if (items)
1923 num_arrays++;
1924 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1925 if (q->pool == NULL)
1926 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001927
1928 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1929 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001930 if (q->queue == ERR_PTR(-ENOMEM))
1931 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001932
1933 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001934 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001935 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001936 q->max = i;
1937 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001938 }
Mike Christie7996a772006-04-06 21:13:41 -05001939 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1940 }
Olaf Kirch63203772007-12-13 12:43:25 -06001941
1942 if (items) {
1943 *items = q->pool + max;
1944 memcpy(*items, q->pool, max * sizeof(void *));
1945 }
1946
Mike Christie7996a772006-04-06 21:13:41 -05001947 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001948
1949enomem:
1950 iscsi_pool_free(q);
1951 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001952}
1953EXPORT_SYMBOL_GPL(iscsi_pool_init);
1954
Olaf Kirch63203772007-12-13 12:43:25 -06001955void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001956{
1957 int i;
1958
1959 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001960 kfree(q->pool[i]);
1961 if (q->pool)
1962 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001963}
1964EXPORT_SYMBOL_GPL(iscsi_pool_free);
1965
Mike Christiea4804cd2008-05-21 15:54:00 -05001966/**
1967 * iscsi_host_add - add host to system
1968 * @shost: scsi host
1969 * @pdev: parent device
1970 *
1971 * This should be called by partial offload and software iscsi drivers
1972 * to add a host to the system.
1973 */
1974int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001975{
Mike Christie8e9a20c2008-06-16 10:11:33 -05001976 if (!shost->can_queue)
1977 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
1978
Mike Christiea4804cd2008-05-21 15:54:00 -05001979 return scsi_add_host(shost, pdev);
1980}
1981EXPORT_SYMBOL_GPL(iscsi_host_add);
1982
1983/**
1984 * iscsi_host_alloc - allocate a host and driver data
1985 * @sht: scsi host template
1986 * @dd_data_size: driver host data size
1987 * @qdepth: default device queue depth
1988 *
1989 * This should be called by partial offload and software iscsi drivers.
1990 * To access the driver specific memory use the iscsi_host_priv() macro.
1991 */
1992struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1993 int dd_data_size, uint16_t qdepth)
1994{
1995 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001996 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05001997
1998 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1999 if (!shost)
2000 return NULL;
2001 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
2002
Mike Christie15482712007-05-30 12:57:19 -05002003 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
2004 if (qdepth != 0)
2005 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05002006 "Queue depth must be between 1 and %d.\n",
2007 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05002008 qdepth = ISCSI_DEF_CMD_PER_LUN;
2009 }
Mike Christie75613522008-05-21 15:53:59 -05002010 shost->cmd_per_lun = qdepth;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002011
2012 ihost = shost_priv(shost);
2013 spin_lock_init(&ihost->lock);
2014 ihost->state = ISCSI_HOST_SETUP;
2015 ihost->num_sessions = 0;
2016 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05002017 return shost;
Mike Christie75613522008-05-21 15:53:59 -05002018}
Mike Christiea4804cd2008-05-21 15:54:00 -05002019EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05002020
Mike Christiee5bd7b52008-09-24 11:46:10 -05002021static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2022{
2023 iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
2024}
2025
Mike Christiea4804cd2008-05-21 15:54:00 -05002026/**
2027 * iscsi_host_remove - remove host and sessions
2028 * @shost: scsi host
2029 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05002030 * If there are any sessions left, this will initiate the removal and wait
2031 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05002032 */
2033void iscsi_host_remove(struct Scsi_Host *shost)
2034{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002035 struct iscsi_host *ihost = shost_priv(shost);
2036 unsigned long flags;
2037
2038 spin_lock_irqsave(&ihost->lock, flags);
2039 ihost->state = ISCSI_HOST_REMOVED;
2040 spin_unlock_irqrestore(&ihost->lock, flags);
2041
2042 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2043 wait_event_interruptible(ihost->session_removal_wq,
2044 ihost->num_sessions == 0);
2045 if (signal_pending(current))
2046 flush_signals(current);
2047
Mike Christiea4804cd2008-05-21 15:54:00 -05002048 scsi_remove_host(shost);
2049}
2050EXPORT_SYMBOL_GPL(iscsi_host_remove);
2051
2052void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05002053{
2054 struct iscsi_host *ihost = shost_priv(shost);
2055
2056 kfree(ihost->netdev);
2057 kfree(ihost->hwaddress);
2058 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05002059 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002060}
Mike Christiea4804cd2008-05-21 15:54:00 -05002061EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002062
Mike Christiee5bd7b52008-09-24 11:46:10 -05002063static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2064{
2065 struct iscsi_host *ihost = shost_priv(shost);
2066 unsigned long flags;
2067
2068 shost = scsi_host_get(shost);
2069 if (!shost) {
2070 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2071 "of session teardown event because host already "
2072 "removed.\n");
2073 return;
2074 }
2075
2076 spin_lock_irqsave(&ihost->lock, flags);
2077 ihost->num_sessions--;
2078 if (ihost->num_sessions == 0)
2079 wake_up(&ihost->session_removal_wq);
2080 spin_unlock_irqrestore(&ihost->lock, flags);
2081 scsi_host_put(shost);
2082}
2083
Mike Christie75613522008-05-21 15:53:59 -05002084/**
2085 * iscsi_session_setup - create iscsi cls session and host and session
2086 * @iscsit: iscsi transport template
2087 * @shost: scsi host
2088 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002089 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002090 * @initial_cmdsn: initial CmdSN
2091 *
2092 * This can be used by software iscsi_transports that allocate
2093 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002094 *
2095 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2096 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2097 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002098 */
2099struct iscsi_cls_session *
2100iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002101 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002102 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002103{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002104 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002105 struct iscsi_session *session;
2106 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002107 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002108 unsigned long flags;
2109
2110 spin_lock_irqsave(&ihost->lock, flags);
2111 if (ihost->state == ISCSI_HOST_REMOVED) {
2112 spin_unlock_irqrestore(&ihost->lock, flags);
2113 return NULL;
2114 }
2115 ihost->num_sessions++;
2116 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002117
2118 if (!total_cmds)
2119 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002120 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002121 * The iscsi layer needs some tasks for nop handling and tmfs,
2122 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2123 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002124 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002125 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2126 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2127 "must be a power of two that is at least %d.\n",
2128 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002129 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002130 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002131
2132 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2133 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2134 "must be a power of 2 less than or equal to %d.\n",
2135 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2136 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2137 }
2138
2139 if (!is_power_of_2(total_cmds)) {
2140 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2141 "must be a power of 2.\n", total_cmds);
2142 total_cmds = rounddown_pow_of_two(total_cmds);
2143 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2144 return NULL;
2145 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2146 total_cmds);
2147 }
2148 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002149
Mike Christie5d91e202008-05-21 15:54:01 -05002150 cls_session = iscsi_alloc_session(shost, iscsit,
2151 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002152 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002153 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002154 session = cls_session->dd_data;
2155 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002156 session->host = shost;
2157 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002158 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002159 session->lu_reset_timeout = 15;
2160 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002161 session->scsi_cmds_max = scsi_cmds;
2162 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002163 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002164 session->exp_cmdsn = initial_cmdsn + 1;
2165 session->max_cmdsn = initial_cmdsn + 1;
2166 session->max_r2t = 1;
2167 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002168 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002169 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002170
2171 /* initialize SCSI PDU commands pool */
2172 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2173 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002174 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002175 goto cmdpool_alloc_fail;
2176
2177 /* pre-format cmds pool with ITT */
2178 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002179 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002180
Mike Christie9c19a7d2008-05-21 15:54:09 -05002181 if (cmd_task_size)
2182 task->dd_data = &task[1];
2183 task->itt = cmd_i;
2184 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002185 }
2186
Mike Christief53a88d2006-06-28 12:00:27 -05002187 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002188 goto module_get_fail;
2189
Mike Christie79706342008-05-21 15:54:12 -05002190 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002191 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002192
Mike Christie7996a772006-04-06 21:13:41 -05002193 return cls_session;
2194
2195cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002196 module_put(iscsit->owner);
2197module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002198 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002199cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002200 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002201dec_session_count:
2202 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002203 return NULL;
2204}
2205EXPORT_SYMBOL_GPL(iscsi_session_setup);
2206
2207/**
2208 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002209 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002210 *
Mike Christie75613522008-05-21 15:53:59 -05002211 * The driver must have called iscsi_remove_session before
2212 * calling this.
2213 */
Mike Christie7996a772006-04-06 21:13:41 -05002214void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2215{
Mike Christie75613522008-05-21 15:53:59 -05002216 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002217 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002218 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002219
Olaf Kirch63203772007-12-13 12:43:25 -06002220 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002221
Mike Christieb2c64162007-05-30 12:57:16 -05002222 kfree(session->password);
2223 kfree(session->password_in);
2224 kfree(session->username);
2225 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002226 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002227 kfree(session->initiatorname);
2228 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002229
Mike Christie75613522008-05-21 15:53:59 -05002230 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002231 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002232 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002233}
2234EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2235
2236/**
2237 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2238 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002239 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002240 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002241 */
Mike Christie7996a772006-04-06 21:13:41 -05002242struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002243iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2244 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002245{
Mike Christie75613522008-05-21 15:53:59 -05002246 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002247 struct iscsi_conn *conn;
2248 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002249 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002250
Mike Christie5d91e202008-05-21 15:54:01 -05002251 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2252 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002253 if (!cls_conn)
2254 return NULL;
2255 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002256 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002257
Mike Christie5d91e202008-05-21 15:54:01 -05002258 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002259 conn->session = session;
2260 conn->cls_conn = cls_conn;
2261 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2262 conn->id = conn_idx;
2263 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002264 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002265
2266 init_timer(&conn->transport_timer);
2267 conn->transport_timer.data = (unsigned long)conn;
2268 conn->transport_timer.function = iscsi_check_transport_timeouts;
2269
Mike Christie7996a772006-04-06 21:13:41 -05002270 INIT_LIST_HEAD(&conn->run_list);
2271 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002272 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002273 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002274 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002275 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002276
Mike Christie9c19a7d2008-05-21 15:54:09 -05002277 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002278 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002279 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002280 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002281 sizeof(void*))) {
2282 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002283 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002284 }
2285 spin_unlock_bh(&session->lock);
2286
Mike Christiebf32ed32007-02-28 17:32:17 -06002287 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002288 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002289 goto login_task_data_alloc_fail;
2290 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002291
Mike Christie843c0a82007-12-13 12:43:20 -06002292 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002293 init_waitqueue_head(&conn->ehwait);
2294
2295 return cls_conn;
2296
Mike Christie9c19a7d2008-05-21 15:54:09 -05002297login_task_data_alloc_fail:
2298 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002299 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002300login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002301 iscsi_destroy_conn(cls_conn);
2302 return NULL;
2303}
2304EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2305
2306/**
2307 * iscsi_conn_teardown - teardown iscsi connection
2308 * cls_conn: iscsi class connection
2309 *
2310 * TODO: we may need to make this into a two step process
2311 * like scsi-mls remove + put host
2312 */
2313void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2314{
2315 struct iscsi_conn *conn = cls_conn->dd_data;
2316 struct iscsi_session *session = conn->session;
2317 unsigned long flags;
2318
Mike Christief6d51802007-12-13 12:43:30 -06002319 del_timer_sync(&conn->transport_timer);
2320
Mike Christie7996a772006-04-06 21:13:41 -05002321 spin_lock_bh(&session->lock);
2322 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2323 if (session->leadconn == conn) {
2324 /*
2325 * leading connection? then give up on recovery.
2326 */
2327 session->state = ISCSI_STATE_TERMINATE;
2328 wake_up(&conn->ehwait);
2329 }
2330 spin_unlock_bh(&session->lock);
2331
Mike Christie7996a772006-04-06 21:13:41 -05002332 /*
2333 * Block until all in-progress commands for this connection
2334 * time out or fail.
2335 */
2336 for (;;) {
2337 spin_lock_irqsave(session->host->host_lock, flags);
2338 if (!session->host->host_busy) { /* OK for ERL == 0 */
2339 spin_unlock_irqrestore(session->host->host_lock, flags);
2340 break;
2341 }
2342 spin_unlock_irqrestore(session->host->host_lock, flags);
2343 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002344 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2345 "host_busy %d host_failed %d\n",
2346 session->host->host_busy,
2347 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002348 /*
2349 * force eh_abort() to unblock
2350 */
2351 wake_up(&conn->ehwait);
2352 }
2353
Mike Christie779ea122007-02-28 17:32:15 -06002354 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002355 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002356
Mike Christie7996a772006-04-06 21:13:41 -05002357 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002358 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002359 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002360 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002361 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002362 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002363 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002364 spin_unlock_bh(&session->lock);
2365
Mike Christie7996a772006-04-06 21:13:41 -05002366 iscsi_destroy_conn(cls_conn);
2367}
2368EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2369
2370int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2371{
2372 struct iscsi_conn *conn = cls_conn->dd_data;
2373 struct iscsi_session *session = conn->session;
2374
Mike Christieffd04362006-08-31 18:09:24 -04002375 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002376 iscsi_conn_printk(KERN_ERR, conn,
2377 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002378 return -EPERM;
2379 }
2380
Mike Christiedb98ccd2006-08-31 18:09:31 -04002381 if ((session->imm_data_en || !session->initial_r2t_en) &&
2382 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002383 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2384 "first_burst %d max_burst %d\n",
2385 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002386 return -EINVAL;
2387 }
2388
Mike Christief6d51802007-12-13 12:43:30 -06002389 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002390 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2391 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002392 conn->recv_timeout = 5;
2393 }
2394
2395 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002396 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2397 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002398 conn->ping_timeout = 5;
2399 }
2400
Mike Christie7996a772006-04-06 21:13:41 -05002401 spin_lock_bh(&session->lock);
2402 conn->c_stage = ISCSI_CONN_STARTED;
2403 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002404 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002405
Mike Christief6d51802007-12-13 12:43:30 -06002406 conn->last_recv = jiffies;
2407 conn->last_ping = jiffies;
2408 if (conn->recv_timeout && conn->ping_timeout)
2409 mod_timer(&conn->transport_timer,
2410 jiffies + (conn->recv_timeout * HZ));
2411
Mike Christie7996a772006-04-06 21:13:41 -05002412 switch(conn->stop_stage) {
2413 case STOP_CONN_RECOVER:
2414 /*
2415 * unblock eh_abort() if it is blocked. re-try all
2416 * commands after successful recovery
2417 */
Mike Christie7996a772006-04-06 21:13:41 -05002418 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002419 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002420 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002421 if (session->age == 16)
2422 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002423 break;
Mike Christie7996a772006-04-06 21:13:41 -05002424 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002425 conn->stop_stage = 0;
2426 break;
Mike Christie7996a772006-04-06 21:13:41 -05002427 default:
2428 break;
2429 }
2430 spin_unlock_bh(&session->lock);
2431
Mike Christie75613522008-05-21 15:53:59 -05002432 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002433 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002434 return 0;
2435}
2436EXPORT_SYMBOL_GPL(iscsi_conn_start);
2437
2438static void
2439flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2440{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002441 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002442
2443 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002444 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2445 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2446 /* release ref from prep task */
2447 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002448 }
2449
2450 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002451 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2452 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2453 /* release ref from prep task */
2454 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002455 }
2456
Mike Christie9c19a7d2008-05-21 15:54:09 -05002457 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002458}
2459
Mike Christie656cffc2006-05-18 20:31:42 -05002460static void iscsi_start_session_recovery(struct iscsi_session *session,
2461 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002462{
Mike Christieed2abc72006-05-02 19:46:40 -05002463 int old_stop_stage;
2464
Mike Christief6d51802007-12-13 12:43:30 -06002465 del_timer_sync(&conn->transport_timer);
2466
Mike Christie6724add2007-08-15 01:38:30 -05002467 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002468 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002469 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002470 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002471 mutex_unlock(&session->eh_mutex);
2472 return;
2473 }
2474
2475 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002476 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002477 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002478 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002479 */
Mike Christie67a61112006-05-30 00:37:20 -05002480 if (flag == STOP_CONN_TERM)
2481 session->state = ISCSI_STATE_TERMINATE;
2482 else if (conn->stop_stage != STOP_CONN_RECOVER)
2483 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002484
2485 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002486 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002487 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002488 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002489
2490 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002491 /*
2492 * for connection level recovery we should not calculate
2493 * header digest. conn->hdr_size used for optimization
2494 * in hdr_extract() and will be re-negotiated at
2495 * set_param() time.
2496 */
2497 if (flag == STOP_CONN_RECOVER) {
2498 conn->hdrdgst_en = 0;
2499 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002500 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002501 old_stop_stage != STOP_CONN_RECOVER) {
2502 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002503 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002504 }
Mike Christie7996a772006-04-06 21:13:41 -05002505 }
Mike Christie656cffc2006-05-18 20:31:42 -05002506
Mike Christie656cffc2006-05-18 20:31:42 -05002507 /*
2508 * flush queues.
2509 */
2510 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002511 if (flag == STOP_CONN_RECOVER)
Mike Christie56d7fcf2008-08-19 18:45:26 -05002512 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2513 else
2514 fail_all_commands(conn, -1, DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002515 flush_control_queues(session, conn);
2516 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002517 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002518}
Mike Christie7996a772006-04-06 21:13:41 -05002519
2520void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2521{
2522 struct iscsi_conn *conn = cls_conn->dd_data;
2523 struct iscsi_session *session = conn->session;
2524
2525 switch (flag) {
2526 case STOP_CONN_RECOVER:
2527 case STOP_CONN_TERM:
2528 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002529 break;
Mike Christie7996a772006-04-06 21:13:41 -05002530 default:
Mike Christie322d7392008-01-31 13:36:52 -06002531 iscsi_conn_printk(KERN_ERR, conn,
2532 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002533 }
2534}
2535EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2536
2537int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2538 struct iscsi_cls_conn *cls_conn, int is_leading)
2539{
Mike Christie75613522008-05-21 15:53:59 -05002540 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002541 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002542
Mike Christie7996a772006-04-06 21:13:41 -05002543 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002544 if (is_leading)
2545 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002546 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002547
2548 /*
2549 * Unblock xmitworker(), Login Phase will pass through.
2550 */
2551 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2552 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2553 return 0;
2554}
2555EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2556
Mike Christiea54a52c2006-06-28 12:00:23 -05002557
2558int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2559 enum iscsi_param param, char *buf, int buflen)
2560{
2561 struct iscsi_conn *conn = cls_conn->dd_data;
2562 struct iscsi_session *session = conn->session;
2563 uint32_t value;
2564
2565 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002566 case ISCSI_PARAM_FAST_ABORT:
2567 sscanf(buf, "%d", &session->fast_abort);
2568 break;
Mike Christief6d51802007-12-13 12:43:30 -06002569 case ISCSI_PARAM_ABORT_TMO:
2570 sscanf(buf, "%d", &session->abort_timeout);
2571 break;
2572 case ISCSI_PARAM_LU_RESET_TMO:
2573 sscanf(buf, "%d", &session->lu_reset_timeout);
2574 break;
2575 case ISCSI_PARAM_PING_TMO:
2576 sscanf(buf, "%d", &conn->ping_timeout);
2577 break;
2578 case ISCSI_PARAM_RECV_TMO:
2579 sscanf(buf, "%d", &conn->recv_timeout);
2580 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002581 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2582 sscanf(buf, "%d", &conn->max_recv_dlength);
2583 break;
2584 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2585 sscanf(buf, "%d", &conn->max_xmit_dlength);
2586 break;
2587 case ISCSI_PARAM_HDRDGST_EN:
2588 sscanf(buf, "%d", &conn->hdrdgst_en);
2589 break;
2590 case ISCSI_PARAM_DATADGST_EN:
2591 sscanf(buf, "%d", &conn->datadgst_en);
2592 break;
2593 case ISCSI_PARAM_INITIAL_R2T_EN:
2594 sscanf(buf, "%d", &session->initial_r2t_en);
2595 break;
2596 case ISCSI_PARAM_MAX_R2T:
2597 sscanf(buf, "%d", &session->max_r2t);
2598 break;
2599 case ISCSI_PARAM_IMM_DATA_EN:
2600 sscanf(buf, "%d", &session->imm_data_en);
2601 break;
2602 case ISCSI_PARAM_FIRST_BURST:
2603 sscanf(buf, "%d", &session->first_burst);
2604 break;
2605 case ISCSI_PARAM_MAX_BURST:
2606 sscanf(buf, "%d", &session->max_burst);
2607 break;
2608 case ISCSI_PARAM_PDU_INORDER_EN:
2609 sscanf(buf, "%d", &session->pdu_inorder_en);
2610 break;
2611 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2612 sscanf(buf, "%d", &session->dataseq_inorder_en);
2613 break;
2614 case ISCSI_PARAM_ERL:
2615 sscanf(buf, "%d", &session->erl);
2616 break;
2617 case ISCSI_PARAM_IFMARKER_EN:
2618 sscanf(buf, "%d", &value);
2619 BUG_ON(value);
2620 break;
2621 case ISCSI_PARAM_OFMARKER_EN:
2622 sscanf(buf, "%d", &value);
2623 BUG_ON(value);
2624 break;
2625 case ISCSI_PARAM_EXP_STATSN:
2626 sscanf(buf, "%u", &conn->exp_statsn);
2627 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002628 case ISCSI_PARAM_USERNAME:
2629 kfree(session->username);
2630 session->username = kstrdup(buf, GFP_KERNEL);
2631 if (!session->username)
2632 return -ENOMEM;
2633 break;
2634 case ISCSI_PARAM_USERNAME_IN:
2635 kfree(session->username_in);
2636 session->username_in = kstrdup(buf, GFP_KERNEL);
2637 if (!session->username_in)
2638 return -ENOMEM;
2639 break;
2640 case ISCSI_PARAM_PASSWORD:
2641 kfree(session->password);
2642 session->password = kstrdup(buf, GFP_KERNEL);
2643 if (!session->password)
2644 return -ENOMEM;
2645 break;
2646 case ISCSI_PARAM_PASSWORD_IN:
2647 kfree(session->password_in);
2648 session->password_in = kstrdup(buf, GFP_KERNEL);
2649 if (!session->password_in)
2650 return -ENOMEM;
2651 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002652 case ISCSI_PARAM_TARGET_NAME:
2653 /* this should not change between logins */
2654 if (session->targetname)
2655 break;
2656
2657 session->targetname = kstrdup(buf, GFP_KERNEL);
2658 if (!session->targetname)
2659 return -ENOMEM;
2660 break;
2661 case ISCSI_PARAM_TPGT:
2662 sscanf(buf, "%d", &session->tpgt);
2663 break;
2664 case ISCSI_PARAM_PERSISTENT_PORT:
2665 sscanf(buf, "%d", &conn->persistent_port);
2666 break;
2667 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2668 /*
2669 * this is the address returned in discovery so it should
2670 * not change between logins.
2671 */
2672 if (conn->persistent_address)
2673 break;
2674
2675 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2676 if (!conn->persistent_address)
2677 return -ENOMEM;
2678 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002679 case ISCSI_PARAM_IFACE_NAME:
2680 if (!session->ifacename)
2681 session->ifacename = kstrdup(buf, GFP_KERNEL);
2682 break;
2683 case ISCSI_PARAM_INITIATOR_NAME:
2684 if (!session->initiatorname)
2685 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2686 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002687 default:
2688 return -ENOSYS;
2689 }
2690
2691 return 0;
2692}
2693EXPORT_SYMBOL_GPL(iscsi_set_param);
2694
2695int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2696 enum iscsi_param param, char *buf)
2697{
Mike Christie75613522008-05-21 15:53:59 -05002698 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002699 int len;
2700
2701 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002702 case ISCSI_PARAM_FAST_ABORT:
2703 len = sprintf(buf, "%d\n", session->fast_abort);
2704 break;
Mike Christief6d51802007-12-13 12:43:30 -06002705 case ISCSI_PARAM_ABORT_TMO:
2706 len = sprintf(buf, "%d\n", session->abort_timeout);
2707 break;
2708 case ISCSI_PARAM_LU_RESET_TMO:
2709 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2710 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002711 case ISCSI_PARAM_INITIAL_R2T_EN:
2712 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2713 break;
2714 case ISCSI_PARAM_MAX_R2T:
2715 len = sprintf(buf, "%hu\n", session->max_r2t);
2716 break;
2717 case ISCSI_PARAM_IMM_DATA_EN:
2718 len = sprintf(buf, "%d\n", session->imm_data_en);
2719 break;
2720 case ISCSI_PARAM_FIRST_BURST:
2721 len = sprintf(buf, "%u\n", session->first_burst);
2722 break;
2723 case ISCSI_PARAM_MAX_BURST:
2724 len = sprintf(buf, "%u\n", session->max_burst);
2725 break;
2726 case ISCSI_PARAM_PDU_INORDER_EN:
2727 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2728 break;
2729 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2730 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2731 break;
2732 case ISCSI_PARAM_ERL:
2733 len = sprintf(buf, "%d\n", session->erl);
2734 break;
2735 case ISCSI_PARAM_TARGET_NAME:
2736 len = sprintf(buf, "%s\n", session->targetname);
2737 break;
2738 case ISCSI_PARAM_TPGT:
2739 len = sprintf(buf, "%d\n", session->tpgt);
2740 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002741 case ISCSI_PARAM_USERNAME:
2742 len = sprintf(buf, "%s\n", session->username);
2743 break;
2744 case ISCSI_PARAM_USERNAME_IN:
2745 len = sprintf(buf, "%s\n", session->username_in);
2746 break;
2747 case ISCSI_PARAM_PASSWORD:
2748 len = sprintf(buf, "%s\n", session->password);
2749 break;
2750 case ISCSI_PARAM_PASSWORD_IN:
2751 len = sprintf(buf, "%s\n", session->password_in);
2752 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002753 case ISCSI_PARAM_IFACE_NAME:
2754 len = sprintf(buf, "%s\n", session->ifacename);
2755 break;
2756 case ISCSI_PARAM_INITIATOR_NAME:
2757 if (!session->initiatorname)
2758 len = sprintf(buf, "%s\n", "unknown");
2759 else
2760 len = sprintf(buf, "%s\n", session->initiatorname);
2761 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002762 default:
2763 return -ENOSYS;
2764 }
2765
2766 return len;
2767}
2768EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2769
2770int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2771 enum iscsi_param param, char *buf)
2772{
2773 struct iscsi_conn *conn = cls_conn->dd_data;
2774 int len;
2775
2776 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002777 case ISCSI_PARAM_PING_TMO:
2778 len = sprintf(buf, "%u\n", conn->ping_timeout);
2779 break;
2780 case ISCSI_PARAM_RECV_TMO:
2781 len = sprintf(buf, "%u\n", conn->recv_timeout);
2782 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002783 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2784 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2785 break;
2786 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2787 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2788 break;
2789 case ISCSI_PARAM_HDRDGST_EN:
2790 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2791 break;
2792 case ISCSI_PARAM_DATADGST_EN:
2793 len = sprintf(buf, "%d\n", conn->datadgst_en);
2794 break;
2795 case ISCSI_PARAM_IFMARKER_EN:
2796 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2797 break;
2798 case ISCSI_PARAM_OFMARKER_EN:
2799 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2800 break;
2801 case ISCSI_PARAM_EXP_STATSN:
2802 len = sprintf(buf, "%u\n", conn->exp_statsn);
2803 break;
2804 case ISCSI_PARAM_PERSISTENT_PORT:
2805 len = sprintf(buf, "%d\n", conn->persistent_port);
2806 break;
2807 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2808 len = sprintf(buf, "%s\n", conn->persistent_address);
2809 break;
2810 default:
2811 return -ENOSYS;
2812 }
2813
2814 return len;
2815}
2816EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2817
Mike Christie0801c242007-05-30 12:57:12 -05002818int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2819 char *buf)
2820{
Mike Christie75613522008-05-21 15:53:59 -05002821 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002822 int len;
2823
2824 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002825 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002826 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002827 len = sprintf(buf, "%s\n", "default");
2828 else
Mike Christie75613522008-05-21 15:53:59 -05002829 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002830 break;
Mike Christie0801c242007-05-30 12:57:12 -05002831 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002832 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002833 len = sprintf(buf, "%s\n", "default");
2834 else
Mike Christie75613522008-05-21 15:53:59 -05002835 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002836 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002837 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002838 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002839 len = sprintf(buf, "%s\n", "unknown");
2840 else
Mike Christie75613522008-05-21 15:53:59 -05002841 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002842 break;
Mike Christie75613522008-05-21 15:53:59 -05002843 case ISCSI_HOST_PARAM_IPADDRESS:
2844 if (!strlen(ihost->local_address))
2845 len = sprintf(buf, "%s\n", "unknown");
2846 else
2847 len = sprintf(buf, "%s\n",
2848 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002849 break;
Mike Christie0801c242007-05-30 12:57:12 -05002850 default:
2851 return -ENOSYS;
2852 }
2853
2854 return len;
2855}
2856EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2857
2858int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2859 char *buf, int buflen)
2860{
Mike Christie75613522008-05-21 15:53:59 -05002861 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002862
2863 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002864 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002865 if (!ihost->netdev)
2866 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002867 break;
Mike Christie0801c242007-05-30 12:57:12 -05002868 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002869 if (!ihost->hwaddress)
2870 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002871 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002872 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002873 if (!ihost->initiatorname)
2874 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002875 break;
Mike Christie0801c242007-05-30 12:57:12 -05002876 default:
2877 return -ENOSYS;
2878 }
2879
2880 return 0;
2881}
2882EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2883
Mike Christie7996a772006-04-06 21:13:41 -05002884MODULE_AUTHOR("Mike Christie");
2885MODULE_DESCRIPTION("iSCSI library functions");
2886MODULE_LICENSE("GPL");