blob: 3fdee7370cccd235d9ad37aa8482c29cac715b7a [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 Christie9c19a7d2008-05-21 15:54:09 -050091void iscsi_prep_unsolicit_data_pdu(struct iscsi_task *task,
Mike Christieffd04362006-08-31 18:09:24 -040092 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050093{
Mike Christie9c19a7d2008-05-21 15:54:09 -050094 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -050095
96 memset(hdr, 0, sizeof(struct iscsi_data));
97 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
Mike Christie9c19a7d2008-05-21 15:54:09 -050098 hdr->datasn = cpu_to_be32(task->unsol_datasn);
99 task->unsol_datasn++;
Mike Christie7996a772006-04-06 21:13:41 -0500100 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500101 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
Mike Christie7996a772006-04-06 21:13:41 -0500102
Mike Christie9c19a7d2008-05-21 15:54:09 -0500103 hdr->itt = task->hdr->itt;
Mike Christie7996a772006-04-06 21:13:41 -0500104 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500105 hdr->offset = cpu_to_be32(task->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500106
Mike Christie9c19a7d2008-05-21 15:54:09 -0500107 if (task->unsol_count > conn->max_xmit_dlength) {
Mike Christie7996a772006-04-06 21:13:41 -0500108 hton24(hdr->dlength, conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500109 task->data_count = conn->max_xmit_dlength;
110 task->unsol_offset += task->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500111 hdr->flags = 0;
112 } else {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500113 hton24(hdr->dlength, task->unsol_count);
114 task->data_count = task->unsol_count;
Mike Christie7996a772006-04-06 21:13:41 -0500115 hdr->flags = ISCSI_FLAG_CMD_FINAL;
116 }
117}
118EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
119
Mike Christie9c19a7d2008-05-21 15:54:09 -0500120static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
Boaz Harrosh004d6532007-12-13 12:43:23 -0600121{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500122 unsigned exp_len = task->hdr_len + len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600123
Mike Christie9c19a7d2008-05-21 15:54:09 -0500124 if (exp_len > task->hdr_max) {
Boaz Harrosh004d6532007-12-13 12:43:23 -0600125 WARN_ON(1);
126 return -EINVAL;
127 }
128
129 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500130 task->hdr_len = exp_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600131 return 0;
132}
133
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500134/*
135 * make an extended cdb AHS
136 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500137static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500138{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500139 struct scsi_cmnd *cmd = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500140 unsigned rlen, pad_len;
141 unsigned short ahslength;
142 struct iscsi_ecdb_ahdr *ecdb_ahdr;
143 int rc;
144
Mike Christie9c19a7d2008-05-21 15:54:09 -0500145 ecdb_ahdr = iscsi_next_hdr(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500146 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
147
148 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
149 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
150
151 pad_len = iscsi_padding(rlen);
152
Mike Christie9c19a7d2008-05-21 15:54:09 -0500153 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500154 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
155 if (rc)
156 return rc;
157
158 if (pad_len)
159 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
160
161 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
162 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
163 ecdb_ahdr->reserved = 0;
164 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
165
166 debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
167 "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500168 cmd->cmd_len, rlen, pad_len, ahslength, task->hdr_len);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500169
170 return 0;
171}
172
Mike Christie9c19a7d2008-05-21 15:54:09 -0500173static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
Boaz Harroshc07d4442008-04-18 10:11:52 -0500174{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500175 struct scsi_cmnd *sc = task->sc;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500176 struct iscsi_rlength_ahdr *rlen_ahdr;
177 int rc;
178
Mike Christie9c19a7d2008-05-21 15:54:09 -0500179 rlen_ahdr = iscsi_next_hdr(task);
180 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
Boaz Harroshc07d4442008-04-18 10:11:52 -0500181 if (rc)
182 return rc;
183
184 rlen_ahdr->ahslength =
185 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
186 sizeof(rlen_ahdr->reserved));
187 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
188 rlen_ahdr->reserved = 0;
189 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
190
191 debug_scsi("bidi-in rlen_ahdr->read_length(%d) "
192 "rlen_ahdr->ahslength(%d)\n",
193 be32_to_cpu(rlen_ahdr->read_length),
194 be16_to_cpu(rlen_ahdr->ahslength));
195 return 0;
196}
197
Mike Christie7996a772006-04-06 21:13:41 -0500198/**
199 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
Mike Christie9c19a7d2008-05-21 15:54:09 -0500200 * @task: iscsi task
Mike Christie7996a772006-04-06 21:13:41 -0500201 *
202 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
203 * fields like dlength or final based on how much data it sends
204 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500205static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500206{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500207 struct iscsi_conn *conn = task->conn;
Mike Christie7996a772006-04-06 21:13:41 -0500208 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500209 struct iscsi_cmd *hdr = task->hdr;
210 struct scsi_cmnd *sc = task->sc;
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500211 unsigned hdrlength, cmd_len;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600212 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500213
Mike Christie9c19a7d2008-05-21 15:54:09 -0500214 task->hdr_len = 0;
215 rc = iscsi_add_hdr(task, sizeof(*hdr));
Boaz Harrosh004d6532007-12-13 12:43:23 -0600216 if (rc)
217 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600218 hdr->opcode = ISCSI_OP_SCSI_CMD;
219 hdr->flags = ISCSI_ATTR_SIMPLE;
220 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500221 hdr->itt = build_itt(task->itt, session->age);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600222 hdr->cmdsn = cpu_to_be32(session->cmdsn);
223 session->cmdsn++;
224 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500225 cmd_len = sc->cmd_len;
226 if (cmd_len < ISCSI_CDB_SIZE)
227 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
228 else if (cmd_len > ISCSI_CDB_SIZE) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500229 rc = iscsi_prep_ecdb_ahs(task);
Boaz Harrosh38d1c062008-04-18 10:11:51 -0500230 if (rc)
231 return rc;
232 cmd_len = ISCSI_CDB_SIZE;
233 }
234 memcpy(hdr->cdb, sc->cmnd, cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500235
Mike Christie9c19a7d2008-05-21 15:54:09 -0500236 task->imm_count = 0;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500237 if (scsi_bidi_cmnd(sc)) {
238 hdr->flags |= ISCSI_FLAG_CMD_READ;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500239 rc = iscsi_prep_bidi_ahs(task);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500240 if (rc)
241 return rc;
242 }
Mike Christie7996a772006-04-06 21:13:41 -0500243 if (sc->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500244 unsigned out_len = scsi_out(sc)->length;
245 hdr->data_length = cpu_to_be32(out_len);
Mike Christie7996a772006-04-06 21:13:41 -0500246 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
247 /*
248 * Write counters:
249 *
250 * imm_count bytes to be sent right after
251 * SCSI PDU Header
252 *
253 * unsol_count bytes(as Data-Out) to be sent
254 * without R2T ack right after
255 * immediate data
256 *
257 * r2t_data_count bytes to be sent via R2T ack's
258 *
259 * pad_count bytes to be sent as zero-padding
260 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500261 task->unsol_count = 0;
262 task->unsol_offset = 0;
263 task->unsol_datasn = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500264
265 if (session->imm_data_en) {
Boaz Harroshc07d4442008-04-18 10:11:52 -0500266 if (out_len >= session->first_burst)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500267 task->imm_count = min(session->first_burst,
Mike Christie7996a772006-04-06 21:13:41 -0500268 conn->max_xmit_dlength);
269 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500270 task->imm_count = min(out_len,
Mike Christie7996a772006-04-06 21:13:41 -0500271 conn->max_xmit_dlength);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500272 hton24(hdr->dlength, task->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500273 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600274 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500275
Mike Christieffd04362006-08-31 18:09:24 -0400276 if (!session->initial_r2t_en) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500277 task->unsol_count = min(session->first_burst, out_len)
278 - task->imm_count;
279 task->unsol_offset = task->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400280 }
281
Mike Christie9c19a7d2008-05-21 15:54:09 -0500282 if (!task->unsol_count)
Mike Christie7996a772006-04-06 21:13:41 -0500283 /* No unsolicit Data-Out's */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600284 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500285 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500286 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
287 zero_data(hdr->dlength);
Boaz Harroshc07d4442008-04-18 10:11:52 -0500288 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
Mike Christie7996a772006-04-06 21:13:41 -0500289
290 if (sc->sc_data_direction == DMA_FROM_DEVICE)
291 hdr->flags |= ISCSI_FLAG_CMD_READ;
292 }
293
Boaz Harrosh004d6532007-12-13 12:43:23 -0600294 /* calculate size of additional header segments (AHSs) */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500295 hdrlength = task->hdr_len - sizeof(*hdr);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600296
297 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
298 hdrlength /= ISCSI_PAD_LEN;
299
300 WARN_ON(hdrlength >= 256);
301 hdr->hlength = hdrlength & 0xFF;
302
Mike Christie3e5c28a2008-05-21 15:54:06 -0500303 if (conn->session->tt->init_task &&
Mike Christie9c19a7d2008-05-21 15:54:09 -0500304 conn->session->tt->init_task(task))
Mike Christie052d0142008-05-21 15:54:05 -0500305 return -EIO;
306
Mike Christie9c19a7d2008-05-21 15:54:09 -0500307 task->state = ISCSI_TASK_RUNNING;
308 list_move_tail(&task->running, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500309
Olaf Kircha8ac6312007-12-13 12:43:35 -0600310 conn->scsicmd_pdus_cnt++;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500311 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
312 "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ?
313 "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ?
Mike Christie9c19a7d2008-05-21 15:54:09 -0500314 "write" : "read", conn->id, sc, sc->cmnd[0], task->itt,
Mike Christie3e5c28a2008-05-21 15:54:06 -0500315 scsi_bufflen(sc),
316 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
317 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600318 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500319}
Mike Christie7996a772006-04-06 21:13:41 -0500320
321/**
Mike Christie3e5c28a2008-05-21 15:54:06 -0500322 * iscsi_complete_command - finish a task
Mike Christie9c19a7d2008-05-21 15:54:09 -0500323 * @task: iscsi cmd task
Mike Christie7996a772006-04-06 21:13:41 -0500324 *
325 * Must be called with session lock.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500326 * This function returns the scsi command to scsi-ml or cleans
327 * up mgmt tasks then returns the task to the pool.
Mike Christie7996a772006-04-06 21:13:41 -0500328 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500329static void iscsi_complete_command(struct iscsi_task *task)
Mike Christie7996a772006-04-06 21:13:41 -0500330{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500331 struct iscsi_conn *conn = task->conn;
Mike Christiec1635cb2007-12-13 12:43:33 -0600332 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500333 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500334
Mike Christie9c19a7d2008-05-21 15:54:09 -0500335 list_del_init(&task->running);
336 task->state = ISCSI_TASK_COMPLETED;
337 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500338
Mike Christie9c19a7d2008-05-21 15:54:09 -0500339 if (conn->task == task)
340 conn->task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500341 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -0500342 * login task is preallocated so do not free
Mike Christie3e5c28a2008-05-21 15:54:06 -0500343 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500344 if (conn->login_task == task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500345 return;
346
Mike Christie9c19a7d2008-05-21 15:54:09 -0500347 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
Mike Christie052d0142008-05-21 15:54:05 -0500348
Mike Christie9c19a7d2008-05-21 15:54:09 -0500349 if (conn->ping_task == task)
350 conn->ping_task = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500351
352 if (sc) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500353 task->sc = NULL;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500354 /* SCSI eh reuses commands to verify us */
355 sc->SCp.ptr = NULL;
356 /*
357 * queue command may call this to free the task, but
358 * not have setup the sc callback
359 */
360 if (sc->scsi_done)
361 sc->scsi_done(sc);
362 }
Mike Christie7996a772006-04-06 21:13:41 -0500363}
364
Mike Christie913e5bf2008-05-21 15:54:18 -0500365void __iscsi_get_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400366{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500367 atomic_inc(&task->refcount);
Mike Christie60ecebf2006-08-31 18:09:25 -0400368}
Mike Christie913e5bf2008-05-21 15:54:18 -0500369EXPORT_SYMBOL_GPL(__iscsi_get_task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400370
Mike Christie9c19a7d2008-05-21 15:54:09 -0500371static void __iscsi_put_task(struct iscsi_task *task)
Mike Christie60ecebf2006-08-31 18:09:25 -0400372{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500373 if (atomic_dec_and_test(&task->refcount))
374 iscsi_complete_command(task);
Mike Christie60ecebf2006-08-31 18:09:25 -0400375}
376
Mike Christie9c19a7d2008-05-21 15:54:09 -0500377void iscsi_put_task(struct iscsi_task *task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500378{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500379 struct iscsi_session *session = task->conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500380
381 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500382 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500383 spin_unlock_bh(&session->lock);
384}
Mike Christie9c19a7d2008-05-21 15:54:09 -0500385EXPORT_SYMBOL_GPL(iscsi_put_task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500386
Mike Christieb3a7ea82007-12-13 12:43:26 -0600387/*
388 * session lock must be held
389 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500390static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600391 int err)
392{
393 struct scsi_cmnd *sc;
394
Mike Christie9c19a7d2008-05-21 15:54:09 -0500395 sc = task->sc;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600396 if (!sc)
397 return;
398
Mike Christie9c19a7d2008-05-21 15:54:09 -0500399 if (task->state == ISCSI_TASK_PENDING)
Mike Christieb3a7ea82007-12-13 12:43:26 -0600400 /*
401 * cmd never made it to the xmit thread, so we should not count
402 * the cmd in the sequencing
403 */
404 conn->session->queued_cmdsn--;
405 else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500406 conn->session->tt->cleanup_task(conn, task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600407
408 sc->result = err;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500409 if (!scsi_bidi_cmnd(sc))
410 scsi_set_resid(sc, scsi_bufflen(sc));
411 else {
412 scsi_out(sc)->resid = scsi_out(sc)->length;
413 scsi_in(sc)->resid = scsi_in(sc)->length;
414 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500415
Mike Christie9c19a7d2008-05-21 15:54:09 -0500416 if (conn->task == task)
417 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600418 /* release ref from queuecommand */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500419 __iscsi_put_task(task);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600420}
421
Mike Christie3e5c28a2008-05-21 15:54:06 -0500422static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500423 struct iscsi_task *task)
Mike Christie052d0142008-05-21 15:54:05 -0500424{
425 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500426 struct iscsi_hdr *hdr = (struct iscsi_hdr *)task->hdr;
Mike Christie052d0142008-05-21 15:54:05 -0500427 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
428
429 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
430 return -ENOTCONN;
431
432 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
433 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
434 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
435 /*
436 * pre-format CmdSN for outgoing PDU.
437 */
438 nop->cmdsn = cpu_to_be32(session->cmdsn);
439 if (hdr->itt != RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500440 hdr->itt = build_itt(task->itt, session->age);
Mike Christie052d0142008-05-21 15:54:05 -0500441 /*
442 * TODO: We always use immediate, so we never hit this.
443 * If we start to send tmfs or nops as non-immediate then
444 * we should start checking the cmdsn numbers for mgmt tasks.
445 */
446 if (conn->c_stage == ISCSI_CONN_STARTED &&
447 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
448 session->queued_cmdsn++;
449 session->cmdsn++;
450 }
451 }
452
Mike Christie3e5c28a2008-05-21 15:54:06 -0500453 if (session->tt->init_task)
Mike Christie9c19a7d2008-05-21 15:54:09 -0500454 session->tt->init_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500455
456 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
457 session->state = ISCSI_STATE_LOGGING_OUT;
458
Mike Christie9c19a7d2008-05-21 15:54:09 -0500459 list_move_tail(&task->running, &conn->mgmt_run_list);
Mike Christie052d0142008-05-21 15:54:05 -0500460 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
461 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500462 task->data_count);
Mike Christie052d0142008-05-21 15:54:05 -0500463 return 0;
464}
465
Mike Christie9c19a7d2008-05-21 15:54:09 -0500466static struct iscsi_task *
Mike Christief6d51802007-12-13 12:43:30 -0600467__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
468 char *data, uint32_t data_size)
469{
470 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500471 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600472
473 if (session->state == ISCSI_STATE_TERMINATE)
474 return NULL;
475
476 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
477 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
478 /*
479 * Login and Text are sent serially, in
480 * request-followed-by-response sequence.
Mike Christie3e5c28a2008-05-21 15:54:06 -0500481 * Same task can be used. Same ITT must be used.
482 * Note that login_task is preallocated at conn_create().
Mike Christief6d51802007-12-13 12:43:30 -0600483 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500484 task = conn->login_task;
Mike Christief6d51802007-12-13 12:43:30 -0600485 else {
486 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
487 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
488
Mike Christie3e5c28a2008-05-21 15:54:06 -0500489 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500490 (void*)&task, sizeof(void*)))
Mike Christief6d51802007-12-13 12:43:30 -0600491 return NULL;
492 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500493 /*
494 * released in complete pdu for task we expect a response for, and
495 * released by the lld when it has transmitted the task for
496 * pdus we do not expect a response for.
497 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500498 atomic_set(&task->refcount, 1);
499 task->conn = conn;
500 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600501
502 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500503 memcpy(task->data, data, data_size);
504 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600505 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500506 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600507
Mike Christie9c19a7d2008-05-21 15:54:09 -0500508 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
509 INIT_LIST_HEAD(&task->running);
510 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500511
512 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500513 if (iscsi_prep_mgmt_task(conn, task)) {
514 __iscsi_put_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500515 return NULL;
516 }
517
Mike Christie9c19a7d2008-05-21 15:54:09 -0500518 if (session->tt->xmit_task(task))
519 task = NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500520
521 } else
522 scsi_queue_work(conn->session->host, &conn->xmitwork);
523
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 return task;
Mike Christief6d51802007-12-13 12:43:30 -0600525}
526
527int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
528 char *data, uint32_t data_size)
529{
530 struct iscsi_conn *conn = cls_conn->dd_data;
531 struct iscsi_session *session = conn->session;
532 int err = 0;
533
534 spin_lock_bh(&session->lock);
535 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
536 err = -EPERM;
537 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600538 return err;
539}
540EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
541
Mike Christie7996a772006-04-06 21:13:41 -0500542/**
543 * iscsi_cmd_rsp - SCSI Command Response processing
544 * @conn: iscsi connection
545 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500546 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500547 * @data: cmd data buffer
548 * @datalen: len of buffer
549 *
550 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500551 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500552 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500553static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500554 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500555 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500556{
Mike Christie7996a772006-04-06 21:13:41 -0500557 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
558 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500559 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500560
Mike Christie77a23c22007-05-30 12:57:18 -0500561 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500562 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
563
564 sc->result = (DID_OK << 16) | rhdr->cmd_status;
565
566 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
567 sc->result = DID_ERROR << 16;
568 goto out;
569 }
570
571 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600572 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500573
574 if (datalen < 2) {
575invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600576 iscsi_conn_printk(KERN_ERR, conn,
577 "Got CHECK_CONDITION but invalid data "
578 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500579 sc->result = DID_BAD_TARGET << 16;
580 goto out;
581 }
582
Harvey Harrison8f3339912008-05-21 15:54:20 -0500583 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500584 if (datalen < senselen)
585 goto invalid_datalen;
586
587 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600588 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500589 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600590 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500591 }
592
Boaz Harroshc07d4442008-04-18 10:11:52 -0500593 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
594 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
595 int res_count = be32_to_cpu(rhdr->bi_residual_count);
596
597 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
598 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
599 res_count <= scsi_in(sc)->length))
600 scsi_in(sc)->resid = res_count;
601 else
602 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
603 }
604
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600605 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
606 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500607 int res_count = be32_to_cpu(rhdr->residual_count);
608
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600609 if (res_count > 0 &&
610 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
611 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500612 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900613 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500614 else
615 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500616 }
Mike Christie7996a772006-04-06 21:13:41 -0500617out:
618 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500619 (long)sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500620 conn->scsirsp_pdus_cnt++;
621
Mike Christie9c19a7d2008-05-21 15:54:09 -0500622 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500623}
624
Mike Christie1d9edf02008-09-24 11:46:09 -0500625/**
626 * iscsi_data_in_rsp - SCSI Data-In Response processing
627 * @conn: iscsi connection
628 * @hdr: iscsi pdu
629 * @task: scsi command task
630 **/
631static void
632iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
633 struct iscsi_task *task)
634{
635 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
636 struct scsi_cmnd *sc = task->sc;
637
638 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
639 return;
640
641 sc->result = (DID_OK << 16) | rhdr->cmd_status;
642 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
643 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
644 ISCSI_FLAG_DATA_OVERFLOW)) {
645 int res_count = be32_to_cpu(rhdr->residual_count);
646
647 if (res_count > 0 &&
648 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
649 res_count <= scsi_in(sc)->length))
650 scsi_in(sc)->resid = res_count;
651 else
652 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
653 }
654
655 conn->scsirsp_pdus_cnt++;
656 __iscsi_put_task(task);
657}
658
Mike Christie7ea8b822006-07-24 15:47:22 -0500659static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
660{
661 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
662
663 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
664 conn->tmfrsp_pdus_cnt++;
665
Mike Christie843c0a82007-12-13 12:43:20 -0600666 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500667 return;
668
669 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600670 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500671 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600672 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500673 else
Mike Christie843c0a82007-12-13 12:43:20 -0600674 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500675 wake_up(&conn->ehwait);
676}
677
Mike Christief6d51802007-12-13 12:43:30 -0600678static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
679{
680 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500681 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600682
Mike Christie9c19a7d2008-05-21 15:54:09 -0500683 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600684 return;
685
686 memset(&hdr, 0, sizeof(struct iscsi_nopout));
687 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
688 hdr.flags = ISCSI_FLAG_CMD_FINAL;
689
690 if (rhdr) {
691 memcpy(hdr.lun, rhdr->lun, 8);
692 hdr.ttt = rhdr->ttt;
693 hdr.itt = RESERVED_ITT;
694 } else
695 hdr.ttt = RESERVED_ITT;
696
Mike Christie9c19a7d2008-05-21 15:54:09 -0500697 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
698 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600699 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christied3acf022008-12-01 12:13:00 -0600700 else if (!rhdr) {
701 /* only track our nops */
702 conn->ping_task = task;
703 conn->last_ping = jiffies;
704 }
Mike Christief6d51802007-12-13 12:43:30 -0600705}
706
Mike Christie62f38302006-08-31 18:09:27 -0400707static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
708 char *data, int datalen)
709{
710 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
711 struct iscsi_hdr rejected_pdu;
712 uint32_t itt;
713
714 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
715
716 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
717 if (ntoh24(reject->dlength) > datalen)
718 return ISCSI_ERR_PROTO;
719
720 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
721 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000722 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600723 iscsi_conn_printk(KERN_ERR, conn,
724 "itt 0x%x had pdu (op 0x%x) rejected "
725 "due to DataDigest error.\n", itt,
726 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400727 }
728 }
729 return 0;
730}
731
Mike Christie7996a772006-04-06 21:13:41 -0500732/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500733 * iscsi_itt_to_task - look up task by itt
734 * @conn: iscsi connection
735 * @itt: itt
736 *
737 * This should be used for mgmt tasks like login and nops, or if
738 * the LDD's itt space does not include the session age.
739 *
740 * The session lock must be held.
741 */
742static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
743{
744 struct iscsi_session *session = conn->session;
745 uint32_t i;
746
747 if (itt == RESERVED_ITT)
748 return NULL;
749
750 i = get_itt(itt);
751 if (i >= session->cmds_max)
752 return NULL;
753
754 return session->cmds[i];
755}
756
757/**
Mike Christie7996a772006-04-06 21:13:41 -0500758 * __iscsi_complete_pdu - complete pdu
759 * @conn: iscsi conn
760 * @hdr: iscsi header
761 * @data: data buffer
762 * @datalen: len of data buffer
763 *
764 * Completes pdu processing by freeing any resources allocated at
765 * queuecommand or send generic. session lock must be held and verify
766 * itt must have been called.
767 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500768int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
769 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500770{
771 struct iscsi_session *session = conn->session;
772 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500773 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500774 uint32_t itt;
775
Mike Christief6d51802007-12-13 12:43:30 -0600776 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500777 rc = iscsi_verify_itt(conn, hdr->itt);
778 if (rc)
779 return rc;
780
Al Virob4377352007-02-09 16:39:40 +0000781 if (hdr->itt != RESERVED_ITT)
782 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500783 else
Al Virob4377352007-02-09 16:39:40 +0000784 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500785
Mike Christie3e5c28a2008-05-21 15:54:06 -0500786 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
787 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500788
Mike Christie3e5c28a2008-05-21 15:54:06 -0500789 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500790 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400791
Mike Christie7996a772006-04-06 21:13:41 -0500792 switch(opcode) {
793 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500794 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500795 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500796 break;
797 }
798
Al Virob4377352007-02-09 16:39:40 +0000799 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500800 break;
801
Mike Christief6d51802007-12-13 12:43:30 -0600802 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500803 break;
804 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400805 rc = iscsi_handle_reject(conn, hdr, data, datalen);
806 break;
Mike Christie7996a772006-04-06 21:13:41 -0500807 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500808 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400809 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
810 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500811 break;
812 default:
813 rc = ISCSI_ERR_BAD_OPCODE;
814 break;
815 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500816 goto out;
817 }
Mike Christie7996a772006-04-06 21:13:41 -0500818
Mike Christie3e5c28a2008-05-21 15:54:06 -0500819 switch(opcode) {
820 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500821 case ISCSI_OP_SCSI_DATA_IN:
822 task = iscsi_itt_to_ctask(conn, hdr->itt);
823 if (!task)
824 return ISCSI_ERR_BAD_ITT;
825 break;
826 case ISCSI_OP_R2T:
827 /*
828 * LLD handles R2Ts if they need to.
829 */
830 return 0;
831 case ISCSI_OP_LOGOUT_RSP:
832 case ISCSI_OP_LOGIN_RSP:
833 case ISCSI_OP_TEXT_RSP:
834 case ISCSI_OP_SCSI_TMFUNC_RSP:
835 case ISCSI_OP_NOOP_IN:
836 task = iscsi_itt_to_task(conn, hdr->itt);
837 if (!task)
838 return ISCSI_ERR_BAD_ITT;
839 break;
840 default:
841 return ISCSI_ERR_BAD_OPCODE;
842 }
843
844 switch(opcode) {
845 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500846 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500847 break;
848 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500849 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500850 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500851 case ISCSI_OP_LOGOUT_RSP:
852 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
853 if (datalen) {
854 rc = ISCSI_ERR_PROTO;
855 break;
856 }
857 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
858 goto recv_pdu;
859 case ISCSI_OP_LOGIN_RSP:
860 case ISCSI_OP_TEXT_RSP:
861 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
862 /*
863 * login related PDU's exp_statsn is handled in
864 * userspace
865 */
866 goto recv_pdu;
867 case ISCSI_OP_SCSI_TMFUNC_RSP:
868 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
869 if (datalen) {
870 rc = ISCSI_ERR_PROTO;
871 break;
872 }
873
874 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500875 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500876 break;
877 case ISCSI_OP_NOOP_IN:
878 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
879 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
880 rc = ISCSI_ERR_PROTO;
881 break;
882 }
883 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
884
Mike Christie9c19a7d2008-05-21 15:54:09 -0500885 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500886 /*
887 * If this is not in response to one of our
888 * nops then it must be from userspace.
889 */
890 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500891
892 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
893 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500894 break;
895 default:
896 rc = ISCSI_ERR_BAD_OPCODE;
897 break;
898 }
899
900out:
901 return rc;
902recv_pdu:
903 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
904 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500905 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500906 return rc;
907}
Mike Christie913e5bf2008-05-21 15:54:18 -0500908EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500909
910int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
911 char *data, int datalen)
912{
913 int rc;
914
915 spin_lock(&conn->session->lock);
916 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
917 spin_unlock(&conn->session->lock);
918 return rc;
919}
920EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
921
Mike Christie0af967f2008-05-21 15:54:04 -0500922int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500923{
924 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500925 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500926
Mike Christie0af967f2008-05-21 15:54:04 -0500927 if (itt == RESERVED_ITT)
928 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500929
Mike Christie0af967f2008-05-21 15:54:04 -0500930 if (((__force u32)itt & ISCSI_AGE_MASK) !=
931 (session->age << ISCSI_AGE_SHIFT)) {
932 iscsi_conn_printk(KERN_ERR, conn,
933 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -0500934 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500935 return ISCSI_ERR_BAD_ITT;
936 }
Mike Christie7996a772006-04-06 21:13:41 -0500937
Mike Christie3e5c28a2008-05-21 15:54:06 -0500938 i = get_itt(itt);
939 if (i >= session->cmds_max) {
940 iscsi_conn_printk(KERN_ERR, conn,
941 "received invalid itt index %u (max cmds "
942 "%u.\n", i, session->cmds_max);
943 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500944 }
Mike Christie7996a772006-04-06 21:13:41 -0500945 return 0;
946}
947EXPORT_SYMBOL_GPL(iscsi_verify_itt);
948
Mike Christie913e5bf2008-05-21 15:54:18 -0500949/**
950 * iscsi_itt_to_ctask - look up ctask by itt
951 * @conn: iscsi connection
952 * @itt: itt
953 *
954 * This should be used for cmd tasks.
955 *
956 * The session lock must be held.
957 */
958struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -0500959{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500960 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -0500961
962 if (iscsi_verify_itt(conn, itt))
963 return NULL;
964
Mike Christie913e5bf2008-05-21 15:54:18 -0500965 task = iscsi_itt_to_task(conn, itt);
966 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -0500967 return NULL;
968
Mike Christie913e5bf2008-05-21 15:54:18 -0500969 if (task->sc->SCp.phase != conn->session->age) {
970 iscsi_session_printk(KERN_ERR, conn->session,
971 "task's session age %d, expected %d\n",
972 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500973 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -0500974 }
Mike Christie0af967f2008-05-21 15:54:04 -0500975
Mike Christie9c19a7d2008-05-21 15:54:09 -0500976 return task;
Mike Christie0af967f2008-05-21 15:54:04 -0500977}
978EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
979
Mike Christiee5bd7b52008-09-24 11:46:10 -0500980void iscsi_session_failure(struct iscsi_cls_session *cls_session,
981 enum iscsi_err err)
982{
983 struct iscsi_session *session = cls_session->dd_data;
984 struct iscsi_conn *conn;
985 struct device *dev;
986 unsigned long flags;
987
988 spin_lock_irqsave(&session->lock, flags);
989 conn = session->leadconn;
990 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
991 spin_unlock_irqrestore(&session->lock, flags);
992 return;
993 }
994
995 dev = get_device(&conn->cls_conn->dev);
996 spin_unlock_irqrestore(&session->lock, flags);
997 if (!dev)
998 return;
999 /*
1000 * if the host is being removed bypass the connection
1001 * recovery initialization because we are going to kill
1002 * the session.
1003 */
1004 if (err == ISCSI_ERR_INVALID_HOST)
1005 iscsi_conn_error_event(conn->cls_conn, err);
1006 else
1007 iscsi_conn_failure(conn, err);
1008 put_device(dev);
1009}
1010EXPORT_SYMBOL_GPL(iscsi_session_failure);
1011
Mike Christie7996a772006-04-06 21:13:41 -05001012void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1013{
1014 struct iscsi_session *session = conn->session;
1015 unsigned long flags;
1016
1017 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001018 if (session->state == ISCSI_STATE_FAILED) {
1019 spin_unlock_irqrestore(&session->lock, flags);
1020 return;
1021 }
1022
Mike Christie67a61112006-05-30 00:37:20 -05001023 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001024 session->state = ISCSI_STATE_FAILED;
1025 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001026
Mike Christie7996a772006-04-06 21:13:41 -05001027 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1028 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001029 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001030}
1031EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1032
Mike Christie77a23c22007-05-30 12:57:18 -05001033static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1034{
1035 struct iscsi_session *session = conn->session;
1036
1037 /*
1038 * Check for iSCSI window and take care of CmdSN wrap-around
1039 */
Mike Christiee0726402007-07-26 12:46:48 -05001040 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1041 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
1042 "CmdSN %u/%u\n", session->exp_cmdsn,
1043 session->max_cmdsn, session->cmdsn,
1044 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001045 return -ENOSPC;
1046 }
1047 return 0;
1048}
1049
Mike Christie9c19a7d2008-05-21 15:54:09 -05001050static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001051{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001052 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001053 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001054
Mike Christie9c19a7d2008-05-21 15:54:09 -05001055 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001056 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001057 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001058 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001059 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001060 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001061 /* done with this task */
1062 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001063 return rc;
1064}
1065
Mike Christie7996a772006-04-06 21:13:41 -05001066/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001067 * iscsi_requeue_task - requeue task to run from session workqueue
1068 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001069 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001070 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001071 * this. The session lock must be held. This should only be called
1072 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001073 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001074void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001075{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001076 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001077
Mike Christie9c19a7d2008-05-21 15:54:09 -05001078 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -06001079 scsi_queue_work(conn->session->host, &conn->xmitwork);
1080}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001081EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001082
1083/**
Mike Christie7996a772006-04-06 21:13:41 -05001084 * iscsi_data_xmit - xmit any command into the scheduled connection
1085 * @conn: iscsi connection
1086 *
1087 * Notes:
1088 * The function can return -EAGAIN in which case the caller must
1089 * re-schedule it again later or recover. '0' return code means
1090 * successful xmit.
1091 **/
1092static int iscsi_data_xmit(struct iscsi_conn *conn)
1093{
Mike Christie3219e522006-05-30 00:37:28 -05001094 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001095
Mike Christie77a23c22007-05-30 12:57:18 -05001096 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001097 if (unlikely(conn->suspend_tx)) {
1098 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001099 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001100 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001101 }
Mike Christie7996a772006-04-06 21:13:41 -05001102
Mike Christie9c19a7d2008-05-21 15:54:09 -05001103 if (conn->task) {
1104 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001105 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001106 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001107 }
1108
Mike Christie77a23c22007-05-30 12:57:18 -05001109 /*
1110 * process mgmt pdus like nops before commands since we should
1111 * only have one nop-out as a ping from us and targets should not
1112 * overflow us with nop-ins
1113 */
1114check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001115 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001116 conn->task = list_entry(conn->mgmtqueue.next,
1117 struct iscsi_task, running);
1118 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1119 __iscsi_put_task(conn->task);
1120 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001121 continue;
1122 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001123 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001124 if (rc)
1125 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001126 }
1127
Mike Christie843c0a82007-12-13 12:43:20 -06001128 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001129 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001130 if (conn->tmf_state == TMF_QUEUED)
1131 break;
1132
Mike Christie9c19a7d2008-05-21 15:54:09 -05001133 conn->task = list_entry(conn->xmitqueue.next,
1134 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001135 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001136 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001137 continue;
1138 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001139 if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
1140 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001141 continue;
1142 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001143 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001144 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001145 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001146 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001147 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001148 * we need to check the mgmt queue for nops that need to
1149 * be sent to aviod starvation
1150 */
Mike Christie843c0a82007-12-13 12:43:20 -06001151 if (!list_empty(&conn->mgmtqueue))
1152 goto check_mgmt;
1153 }
1154
1155 while (!list_empty(&conn->requeue)) {
1156 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1157 break;
1158
Mike Christieb3a7ea82007-12-13 12:43:26 -06001159 /*
1160 * we always do fastlogout - conn stop code will clean up.
1161 */
1162 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1163 break;
1164
Mike Christie9c19a7d2008-05-21 15:54:09 -05001165 conn->task = list_entry(conn->requeue.next,
1166 struct iscsi_task, running);
1167 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001168 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001169 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001170 if (rc)
1171 goto again;
1172 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001173 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001174 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001175 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001176 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001177
1178again:
1179 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001180 rc = -ENODATA;
1181 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001182 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001183}
1184
David Howellsc4028952006-11-22 14:57:56 +00001185static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001186{
David Howellsc4028952006-11-22 14:57:56 +00001187 struct iscsi_conn *conn =
1188 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001189 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001190 /*
1191 * serialize Xmit worker on a per-connection basis.
1192 */
Mike Christie3219e522006-05-30 00:37:28 -05001193 do {
1194 rc = iscsi_data_xmit(conn);
1195 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001196}
1197
1198enum {
1199 FAILURE_BAD_HOST = 1,
1200 FAILURE_SESSION_FAILED,
1201 FAILURE_SESSION_FREED,
1202 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001203 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001204 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001205 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001206 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001207 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001208 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001209};
1210
1211int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1212{
Mike Christie75613522008-05-21 15:53:59 -05001213 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001214 struct Scsi_Host *host;
1215 int reason = 0;
1216 struct iscsi_session *session;
1217 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001218 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001219
1220 sc->scsi_done = done;
1221 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001222 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001223
1224 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001225 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001226
Mike Christie75613522008-05-21 15:53:59 -05001227 cls_session = starget_to_session(scsi_target(sc->device));
1228 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001229 spin_lock(&session->lock);
1230
Mike Christie75613522008-05-21 15:53:59 -05001231 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001232 if (reason) {
1233 sc->result = reason;
1234 goto fault;
1235 }
1236
Mike Christie656cffc2006-05-18 20:31:42 -05001237 /*
1238 * ISCSI_STATE_FAILED is a temp. state. The recovery
1239 * code will decide what is best to do with command queued
1240 * during this time
1241 */
1242 if (session->state != ISCSI_STATE_LOGGED_IN &&
1243 session->state != ISCSI_STATE_FAILED) {
1244 /*
1245 * to handle the race between when we set the recovery state
1246 * and block the session we requeue here (commands could
1247 * be entering our queuecommand while a block is starting
1248 * up because the block code is not locked)
1249 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001250 switch (session->state) {
1251 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001252 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christied6d13ee2008-08-17 15:24:43 -05001253 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001254 case ISCSI_STATE_LOGGING_OUT:
1255 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christied6d13ee2008-08-17 15:24:43 -05001256 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001257 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001258 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001259 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001260 break;
1261 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001262 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001263 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001264 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001265 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001266 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001267 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001268 }
Mike Christie7996a772006-04-06 21:13:41 -05001269 goto fault;
1270 }
1271
Mike Christie7996a772006-04-06 21:13:41 -05001272 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001273 if (!conn) {
1274 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001275 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001276 goto fault;
1277 }
Mike Christie7996a772006-04-06 21:13:41 -05001278
Mike Christie77a23c22007-05-30 12:57:18 -05001279 if (iscsi_check_cmdsn_window_closed(conn)) {
1280 reason = FAILURE_WINDOW_CLOSED;
1281 goto reject;
1282 }
1283
Mike Christie9c19a7d2008-05-21 15:54:09 -05001284 if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
Mike Christie60ecebf2006-08-31 18:09:25 -04001285 sizeof(void*))) {
1286 reason = FAILURE_OOM;
1287 goto reject;
1288 }
Mike Christie7996a772006-04-06 21:13:41 -05001289 sc->SCp.phase = session->age;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001290 sc->SCp.ptr = (char *)task;
Mike Christie7996a772006-04-06 21:13:41 -05001291
Mike Christie9c19a7d2008-05-21 15:54:09 -05001292 atomic_set(&task->refcount, 1);
1293 task->state = ISCSI_TASK_PENDING;
1294 task->conn = conn;
1295 task->sc = sc;
1296 INIT_LIST_HEAD(&task->running);
1297 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001298
Mike Christie052d0142008-05-21 15:54:05 -05001299 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001300 if (iscsi_prep_scsi_cmd_pdu(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001301 sc->result = DID_ABORT << 16;
1302 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001303 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001304 goto fault;
1305 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001306 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001307 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001308 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001309 reason = FAILURE_SESSION_NOT_READY;
1310 goto reject;
1311 }
1312 } else
1313 scsi_queue_work(session->host, &conn->xmitwork);
1314
1315 session->queued_cmdsn++;
1316 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001317 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001318 return 0;
1319
1320reject:
1321 spin_unlock(&session->lock);
1322 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001323 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001324 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001325
1326fault:
1327 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001328 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001329 if (!scsi_bidi_cmnd(sc))
1330 scsi_set_resid(sc, scsi_bufflen(sc));
1331 else {
1332 scsi_out(sc)->resid = scsi_out(sc)->length;
1333 scsi_in(sc)->resid = scsi_in(sc)->length;
1334 }
Mike Christie052d0142008-05-21 15:54:05 -05001335 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001336 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001337 return 0;
1338}
1339EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1340
1341int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1342{
1343 if (depth > ISCSI_MAX_CMD_PER_LUN)
1344 depth = ISCSI_MAX_CMD_PER_LUN;
1345 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1346 return sdev->queue_depth;
1347}
1348EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1349
Mike Christie7996a772006-04-06 21:13:41 -05001350void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1351{
Mike Christie75613522008-05-21 15:53:59 -05001352 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001353
1354 spin_lock_bh(&session->lock);
1355 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001356 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001357 if (session->leadconn)
1358 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001359 }
1360 spin_unlock_bh(&session->lock);
1361}
1362EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1363
Mike Christie8e124522008-09-24 11:46:12 -05001364int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001365{
Mike Christie75613522008-05-21 15:53:59 -05001366 struct iscsi_cls_session *cls_session;
1367 struct iscsi_session *session;
1368 struct iscsi_conn *conn;
1369
1370 cls_session = starget_to_session(scsi_target(sc->device));
1371 session = cls_session->dd_data;
1372 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001373
Mike Christiebc436b22007-12-13 12:43:28 -06001374 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001375 spin_lock_bh(&session->lock);
1376 if (session->state == ISCSI_STATE_TERMINATE) {
1377failed:
Mike Christie8e124522008-09-24 11:46:12 -05001378 debug_scsi("failing target reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001379 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001380 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001381 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001382 return FAILED;
1383 }
1384
Mike Christie7996a772006-04-06 21:13:41 -05001385 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001386 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001387 /*
1388 * we drop the lock here but the leadconn cannot be destoyed while
1389 * we are in the scsi eh
1390 */
Mike Christie843c0a82007-12-13 12:43:20 -06001391 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001392
Mike Christie8e124522008-09-24 11:46:12 -05001393 debug_scsi("iscsi_eh_target_reset wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001394 wait_event_interruptible(conn->ehwait,
1395 session->state == ISCSI_STATE_TERMINATE ||
1396 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001397 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001398 if (signal_pending(current))
1399 flush_signals(current);
1400
Mike Christiebc436b22007-12-13 12:43:28 -06001401 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001402 spin_lock_bh(&session->lock);
1403 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001404 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001405 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001406 else
1407 goto failed;
1408 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001409 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001410 return SUCCESS;
1411}
Mike Christie8e124522008-09-24 11:46:12 -05001412EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001413
Mike Christie843c0a82007-12-13 12:43:20 -06001414static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001415{
Mike Christie843c0a82007-12-13 12:43:20 -06001416 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001417 struct iscsi_session *session = conn->session;
1418
1419 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001420 if (conn->tmf_state == TMF_QUEUED) {
1421 conn->tmf_state = TMF_TIMEDOUT;
1422 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001423 /* unblock eh_abort() */
1424 wake_up(&conn->ehwait);
1425 }
1426 spin_unlock(&session->lock);
1427}
1428
Mike Christie9c19a7d2008-05-21 15:54:09 -05001429static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001430 struct iscsi_tm *hdr, int age,
1431 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001432{
Mike Christie7996a772006-04-06 21:13:41 -05001433 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001434 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001435
Mike Christie9c19a7d2008-05-21 15:54:09 -05001436 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001437 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001438 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001439 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001440 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001441 spin_lock_bh(&session->lock);
1442 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001443 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001444 }
Mike Christie843c0a82007-12-13 12:43:20 -06001445 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001446 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001447 conn->tmf_timer.function = iscsi_tmf_timedout;
1448 conn->tmf_timer.data = (unsigned long)conn;
1449 add_timer(&conn->tmf_timer);
1450 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001451
Mike Christie7996a772006-04-06 21:13:41 -05001452 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001453 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001454
1455 /*
1456 * block eh thread until:
1457 *
Mike Christie843c0a82007-12-13 12:43:20 -06001458 * 1) tmf response
1459 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001460 * 3) session is terminated or restarted or userspace has
1461 * given up on recovery
1462 */
Mike Christie843c0a82007-12-13 12:43:20 -06001463 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001464 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001465 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001466 if (signal_pending(current))
1467 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001468 del_timer_sync(&conn->tmf_timer);
1469
Mike Christie6724add2007-08-15 01:38:30 -05001470 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001471 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001472 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001473 if (age != session->age ||
1474 session->state != ISCSI_STATE_LOGGED_IN)
1475 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001476 return 0;
1477}
1478
1479/*
Mike Christie843c0a82007-12-13 12:43:20 -06001480 * Fail commands. session lock held and recv side suspended and xmit
1481 * thread flushed
1482 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001483static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1484 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001485{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001486 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001487
Mike Christie9c19a7d2008-05-21 15:54:09 -05001488 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1489 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001490
1491 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001492 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1493 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001494 debug_scsi("failing pending sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001495 task->sc, task->itt);
1496 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001497 }
1498 }
1499
Mike Christie9c19a7d2008-05-21 15:54:09 -05001500 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1501 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001502 debug_scsi("failing requeued sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001503 task->sc, task->itt);
1504 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001505 }
1506 }
1507
1508 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001509 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1510 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001511 debug_scsi("failing in progress sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001512 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001513 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001514 }
1515 }
1516}
1517
Mike Christieb40977d2008-05-21 15:54:03 -05001518void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001519{
1520 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001521 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1522 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001523}
Mike Christieb40977d2008-05-21 15:54:03 -05001524EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001525
1526static void iscsi_start_tx(struct iscsi_conn *conn)
1527{
1528 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001529 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1530 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001531}
1532
Jens Axboe242f9dc2008-09-14 05:55:09 -07001533static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001534{
1535 struct iscsi_cls_session *cls_session;
1536 struct iscsi_session *session;
1537 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001538 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001539
1540 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001541 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001542
1543 debug_scsi("scsi cmd %p timedout\n", scmd);
1544
1545 spin_lock(&session->lock);
1546 if (session->state != ISCSI_STATE_LOGGED_IN) {
1547 /*
1548 * We are probably in the middle of iscsi recovery so let
1549 * that complete and handle the error.
1550 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001551 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001552 goto done;
1553 }
1554
1555 conn = session->leadconn;
1556 if (!conn) {
1557 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001558 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001559 goto done;
1560 }
1561
1562 if (!conn->recv_timeout && !conn->ping_timeout)
1563 goto done;
1564 /*
1565 * if the ping timedout then we are in the middle of cleaning up
1566 * and can let the iscsi eh handle it
1567 */
1568 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1569 (conn->ping_timeout * HZ), jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001570 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001571 /*
1572 * if we are about to check the transport then give the command
1573 * more time
1574 */
1575 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1576 jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001577 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001578 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001579 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001580 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001581done:
1582 spin_unlock(&session->lock);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001583 debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
1584 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001585 return rc;
1586}
1587
1588static void iscsi_check_transport_timeouts(unsigned long data)
1589{
1590 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1591 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001592 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001593
1594 spin_lock(&session->lock);
1595 if (session->state != ISCSI_STATE_LOGGED_IN)
1596 goto done;
1597
Mike Christie4cf10432008-05-07 20:43:52 -05001598 recv_timeout = conn->recv_timeout;
1599 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001600 goto done;
1601
Mike Christie4cf10432008-05-07 20:43:52 -05001602 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001603 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001604 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001605 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001606 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001607 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1608 "expired, last rx %lu, last ping %lu, "
1609 "now %lu\n", conn->ping_timeout, last_recv,
1610 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001611 spin_unlock(&session->lock);
1612 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1613 return;
1614 }
1615
Mike Christie4cf10432008-05-07 20:43:52 -05001616 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001617 /* send a ping to try to provoke some traffic */
1618 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1619 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001620 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001621 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001622 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001623
Mike Christiead294e92008-01-31 13:36:50 -06001624 debug_scsi("Setting next tmo %lu\n", next_timeout);
1625 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001626done:
1627 spin_unlock(&session->lock);
1628}
1629
Mike Christie9c19a7d2008-05-21 15:54:09 -05001630static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001631 struct iscsi_tm *hdr)
1632{
1633 memset(hdr, 0, sizeof(*hdr));
1634 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1635 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1636 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001637 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1638 hdr->rtt = task->hdr->itt;
1639 hdr->refcmdsn = task->hdr->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001640}
1641
Mike Christie7996a772006-04-06 21:13:41 -05001642int iscsi_eh_abort(struct scsi_cmnd *sc)
1643{
Mike Christie75613522008-05-21 15:53:59 -05001644 struct iscsi_cls_session *cls_session;
1645 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001646 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001647 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001648 struct iscsi_tm *hdr;
1649 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001650
Mike Christie75613522008-05-21 15:53:59 -05001651 cls_session = starget_to_session(scsi_target(sc->device));
1652 session = cls_session->dd_data;
1653
Mike Christie6724add2007-08-15 01:38:30 -05001654 mutex_lock(&session->eh_mutex);
1655 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001656 /*
1657 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1658 * got the command.
1659 */
1660 if (!sc->SCp.ptr) {
1661 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001662 spin_unlock_bh(&session->lock);
1663 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001664 return SUCCESS;
1665 }
1666
Mike Christie7996a772006-04-06 21:13:41 -05001667 /*
1668 * If we are not logged in or we have started a new session
1669 * then let the host reset code handle this
1670 */
Mike Christie843c0a82007-12-13 12:43:20 -06001671 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1672 sc->SCp.phase != session->age) {
1673 spin_unlock_bh(&session->lock);
1674 mutex_unlock(&session->eh_mutex);
1675 return FAILED;
1676 }
1677
1678 conn = session->leadconn;
1679 conn->eh_abort_cnt++;
1680 age = session->age;
1681
Mike Christie9c19a7d2008-05-21 15:54:09 -05001682 task = (struct iscsi_task *)sc->SCp.ptr;
1683 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001684
Mike Christie9c19a7d2008-05-21 15:54:09 -05001685 /* task completed before time out */
1686 if (!task->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001687 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001688 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001689 }
Mike Christie7996a772006-04-06 21:13:41 -05001690
Mike Christie9c19a7d2008-05-21 15:54:09 -05001691 if (task->state == ISCSI_TASK_PENDING) {
1692 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001693 goto success;
1694 }
Mike Christie7996a772006-04-06 21:13:41 -05001695
Mike Christie843c0a82007-12-13 12:43:20 -06001696 /* only have one tmf outstanding at a time */
1697 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001698 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001699 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001700
Mike Christie843c0a82007-12-13 12:43:20 -06001701 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001702 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001703
Mike Christie9c19a7d2008-05-21 15:54:09 -05001704 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001705 rc = FAILED;
1706 goto failed;
1707 }
1708
1709 switch (conn->tmf_state) {
1710 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001711 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001712 /*
1713 * stop tx side incase the target had sent a abort rsp but
1714 * the initiator was still writing out data.
1715 */
Mike Christie6724add2007-08-15 01:38:30 -05001716 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001717 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001718 * we do not stop the recv side because targets have been
1719 * good and have never sent us a successful tmf response
1720 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001721 */
Mike Christie77a23c22007-05-30 12:57:18 -05001722 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001723 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001724 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001725 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001726 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001727 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001728 case TMF_TIMEDOUT:
1729 spin_unlock_bh(&session->lock);
1730 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1731 goto failed_unlocked;
1732 case TMF_NOT_FOUND:
1733 if (!sc->SCp.ptr) {
1734 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001735 /* task completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001736 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001737 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001738 }
1739 /* fall through */
1740 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001741 conn->tmf_state = TMF_INITIAL;
1742 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001743 }
1744
Mike Christie77a23c22007-05-30 12:57:18 -05001745success:
Mike Christie7996a772006-04-06 21:13:41 -05001746 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001747success_unlocked:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001748 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001749 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001750 return SUCCESS;
1751
1752failed:
1753 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001754failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001755 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001756 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001757 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001758 return FAILED;
1759}
1760EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1761
Mike Christie843c0a82007-12-13 12:43:20 -06001762static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1763{
1764 memset(hdr, 0, sizeof(*hdr));
1765 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1766 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1767 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1768 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001769 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001770}
1771
1772int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1773{
Mike Christie75613522008-05-21 15:53:59 -05001774 struct iscsi_cls_session *cls_session;
1775 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001776 struct iscsi_conn *conn;
1777 struct iscsi_tm *hdr;
1778 int rc = FAILED;
1779
Mike Christie75613522008-05-21 15:53:59 -05001780 cls_session = starget_to_session(scsi_target(sc->device));
1781 session = cls_session->dd_data;
1782
Mike Christie843c0a82007-12-13 12:43:20 -06001783 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1784
1785 mutex_lock(&session->eh_mutex);
1786 spin_lock_bh(&session->lock);
1787 /*
1788 * Just check if we are not logged in. We cannot check for
1789 * the phase because the reset could come from a ioctl.
1790 */
1791 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1792 goto unlock;
1793 conn = session->leadconn;
1794
1795 /* only have one tmf outstanding at a time */
1796 if (conn->tmf_state != TMF_INITIAL)
1797 goto unlock;
1798 conn->tmf_state = TMF_QUEUED;
1799
1800 hdr = &conn->tmhdr;
1801 iscsi_prep_lun_reset_pdu(sc, hdr);
1802
Mike Christie9c19a7d2008-05-21 15:54:09 -05001803 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001804 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001805 rc = FAILED;
1806 goto unlock;
1807 }
1808
1809 switch (conn->tmf_state) {
1810 case TMF_SUCCESS:
1811 break;
1812 case TMF_TIMEDOUT:
1813 spin_unlock_bh(&session->lock);
1814 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1815 goto done;
1816 default:
1817 conn->tmf_state = TMF_INITIAL;
1818 goto unlock;
1819 }
1820
1821 rc = SUCCESS;
1822 spin_unlock_bh(&session->lock);
1823
1824 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001825
Mike Christiea3439142008-09-24 11:46:15 -05001826 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001827 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001828 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05001829 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001830
1831 iscsi_start_tx(conn);
1832 goto done;
1833
1834unlock:
1835 spin_unlock_bh(&session->lock);
1836done:
1837 debug_scsi("iscsi_eh_device_reset %s\n",
1838 rc == SUCCESS ? "SUCCESS" : "FAILED");
1839 mutex_unlock(&session->eh_mutex);
1840 return rc;
1841}
1842EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1843
Olaf Kirch63203772007-12-13 12:43:25 -06001844/*
1845 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1846 * should be accessed via kfifo_{get,put} on q->queue.
1847 * Optionally, the caller can obtain the array of object pointers
1848 * by passing in a non-NULL @items pointer
1849 */
Mike Christie7996a772006-04-06 21:13:41 -05001850int
Olaf Kirch63203772007-12-13 12:43:25 -06001851iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001852{
Olaf Kirch63203772007-12-13 12:43:25 -06001853 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001854
Olaf Kirch63203772007-12-13 12:43:25 -06001855 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001856
1857 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001858
1859 /* If the user passed an items pointer, he wants a copy of
1860 * the array. */
1861 if (items)
1862 num_arrays++;
1863 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1864 if (q->pool == NULL)
1865 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001866
1867 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1868 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001869 if (q->queue == ERR_PTR(-ENOMEM))
1870 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001871
1872 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001873 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001874 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001875 q->max = i;
1876 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001877 }
Mike Christie7996a772006-04-06 21:13:41 -05001878 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1879 }
Olaf Kirch63203772007-12-13 12:43:25 -06001880
1881 if (items) {
1882 *items = q->pool + max;
1883 memcpy(*items, q->pool, max * sizeof(void *));
1884 }
1885
Mike Christie7996a772006-04-06 21:13:41 -05001886 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001887
1888enomem:
1889 iscsi_pool_free(q);
1890 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001891}
1892EXPORT_SYMBOL_GPL(iscsi_pool_init);
1893
Olaf Kirch63203772007-12-13 12:43:25 -06001894void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001895{
1896 int i;
1897
1898 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001899 kfree(q->pool[i]);
1900 if (q->pool)
1901 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001902}
1903EXPORT_SYMBOL_GPL(iscsi_pool_free);
1904
Mike Christiea4804cd2008-05-21 15:54:00 -05001905/**
1906 * iscsi_host_add - add host to system
1907 * @shost: scsi host
1908 * @pdev: parent device
1909 *
1910 * This should be called by partial offload and software iscsi drivers
1911 * to add a host to the system.
1912 */
1913int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001914{
Mike Christie8e9a20c2008-06-16 10:11:33 -05001915 if (!shost->can_queue)
1916 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
1917
Mike Christiea4804cd2008-05-21 15:54:00 -05001918 return scsi_add_host(shost, pdev);
1919}
1920EXPORT_SYMBOL_GPL(iscsi_host_add);
1921
1922/**
1923 * iscsi_host_alloc - allocate a host and driver data
1924 * @sht: scsi host template
1925 * @dd_data_size: driver host data size
1926 * @qdepth: default device queue depth
1927 *
1928 * This should be called by partial offload and software iscsi drivers.
1929 * To access the driver specific memory use the iscsi_host_priv() macro.
1930 */
1931struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1932 int dd_data_size, uint16_t qdepth)
1933{
1934 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001935 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05001936
1937 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1938 if (!shost)
1939 return NULL;
1940 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1941
Mike Christie15482712007-05-30 12:57:19 -05001942 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1943 if (qdepth != 0)
1944 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001945 "Queue depth must be between 1 and %d.\n",
1946 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001947 qdepth = ISCSI_DEF_CMD_PER_LUN;
1948 }
Mike Christie75613522008-05-21 15:53:59 -05001949 shost->cmd_per_lun = qdepth;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001950
1951 ihost = shost_priv(shost);
1952 spin_lock_init(&ihost->lock);
1953 ihost->state = ISCSI_HOST_SETUP;
1954 ihost->num_sessions = 0;
1955 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05001956 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001957}
Mike Christiea4804cd2008-05-21 15:54:00 -05001958EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001959
Mike Christiee5bd7b52008-09-24 11:46:10 -05001960static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
1961{
1962 iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
1963}
1964
Mike Christiea4804cd2008-05-21 15:54:00 -05001965/**
1966 * iscsi_host_remove - remove host and sessions
1967 * @shost: scsi host
1968 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05001969 * If there are any sessions left, this will initiate the removal and wait
1970 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05001971 */
1972void iscsi_host_remove(struct Scsi_Host *shost)
1973{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001974 struct iscsi_host *ihost = shost_priv(shost);
1975 unsigned long flags;
1976
1977 spin_lock_irqsave(&ihost->lock, flags);
1978 ihost->state = ISCSI_HOST_REMOVED;
1979 spin_unlock_irqrestore(&ihost->lock, flags);
1980
1981 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
1982 wait_event_interruptible(ihost->session_removal_wq,
1983 ihost->num_sessions == 0);
1984 if (signal_pending(current))
1985 flush_signals(current);
1986
Mike Christiea4804cd2008-05-21 15:54:00 -05001987 scsi_remove_host(shost);
1988}
1989EXPORT_SYMBOL_GPL(iscsi_host_remove);
1990
1991void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001992{
1993 struct iscsi_host *ihost = shost_priv(shost);
1994
1995 kfree(ihost->netdev);
1996 kfree(ihost->hwaddress);
1997 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001998 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05001999}
Mike Christiea4804cd2008-05-21 15:54:00 -05002000EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002001
Mike Christiee5bd7b52008-09-24 11:46:10 -05002002static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2003{
2004 struct iscsi_host *ihost = shost_priv(shost);
2005 unsigned long flags;
2006
2007 shost = scsi_host_get(shost);
2008 if (!shost) {
2009 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2010 "of session teardown event because host already "
2011 "removed.\n");
2012 return;
2013 }
2014
2015 spin_lock_irqsave(&ihost->lock, flags);
2016 ihost->num_sessions--;
2017 if (ihost->num_sessions == 0)
2018 wake_up(&ihost->session_removal_wq);
2019 spin_unlock_irqrestore(&ihost->lock, flags);
2020 scsi_host_put(shost);
2021}
2022
Mike Christie75613522008-05-21 15:53:59 -05002023/**
2024 * iscsi_session_setup - create iscsi cls session and host and session
2025 * @iscsit: iscsi transport template
2026 * @shost: scsi host
2027 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002028 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002029 * @initial_cmdsn: initial CmdSN
2030 *
2031 * This can be used by software iscsi_transports that allocate
2032 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002033 *
2034 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2035 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2036 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002037 */
2038struct iscsi_cls_session *
2039iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002040 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002041 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002042{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002043 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002044 struct iscsi_session *session;
2045 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002046 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002047 unsigned long flags;
2048
2049 spin_lock_irqsave(&ihost->lock, flags);
2050 if (ihost->state == ISCSI_HOST_REMOVED) {
2051 spin_unlock_irqrestore(&ihost->lock, flags);
2052 return NULL;
2053 }
2054 ihost->num_sessions++;
2055 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002056
2057 if (!total_cmds)
2058 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002059 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002060 * The iscsi layer needs some tasks for nop handling and tmfs,
2061 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2062 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002063 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002064 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2065 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2066 "must be a power of two that is at least %d.\n",
2067 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002068 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002069 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002070
2071 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2072 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2073 "must be a power of 2 less than or equal to %d.\n",
2074 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2075 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2076 }
2077
2078 if (!is_power_of_2(total_cmds)) {
2079 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2080 "must be a power of 2.\n", total_cmds);
2081 total_cmds = rounddown_pow_of_two(total_cmds);
2082 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2083 return NULL;
2084 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2085 total_cmds);
2086 }
2087 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002088
Mike Christie5d91e202008-05-21 15:54:01 -05002089 cls_session = iscsi_alloc_session(shost, iscsit,
2090 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002091 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002092 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002093 session = cls_session->dd_data;
2094 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002095 session->host = shost;
2096 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002097 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002098 session->lu_reset_timeout = 15;
2099 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002100 session->scsi_cmds_max = scsi_cmds;
2101 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002102 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002103 session->exp_cmdsn = initial_cmdsn + 1;
2104 session->max_cmdsn = initial_cmdsn + 1;
2105 session->max_r2t = 1;
2106 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002107 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002108 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002109
2110 /* initialize SCSI PDU commands pool */
2111 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2112 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002113 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002114 goto cmdpool_alloc_fail;
2115
2116 /* pre-format cmds pool with ITT */
2117 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002118 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002119
Mike Christie9c19a7d2008-05-21 15:54:09 -05002120 if (cmd_task_size)
2121 task->dd_data = &task[1];
2122 task->itt = cmd_i;
2123 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002124 }
2125
Mike Christief53a88d2006-06-28 12:00:27 -05002126 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002127 goto module_get_fail;
2128
Mike Christie79706342008-05-21 15:54:12 -05002129 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002130 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002131
Mike Christie7996a772006-04-06 21:13:41 -05002132 return cls_session;
2133
2134cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002135 module_put(iscsit->owner);
2136module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002137 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002138cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002139 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002140dec_session_count:
2141 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002142 return NULL;
2143}
2144EXPORT_SYMBOL_GPL(iscsi_session_setup);
2145
2146/**
2147 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002148 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002149 *
Mike Christie75613522008-05-21 15:53:59 -05002150 * The driver must have called iscsi_remove_session before
2151 * calling this.
2152 */
Mike Christie7996a772006-04-06 21:13:41 -05002153void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2154{
Mike Christie75613522008-05-21 15:53:59 -05002155 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002156 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002157 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002158
Olaf Kirch63203772007-12-13 12:43:25 -06002159 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002160
Mike Christieb2c64162007-05-30 12:57:16 -05002161 kfree(session->password);
2162 kfree(session->password_in);
2163 kfree(session->username);
2164 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002165 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002166 kfree(session->initiatorname);
2167 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002168
Mike Christie75613522008-05-21 15:53:59 -05002169 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002170 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002171 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002172}
2173EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2174
2175/**
2176 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2177 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002178 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002179 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002180 */
Mike Christie7996a772006-04-06 21:13:41 -05002181struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002182iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2183 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002184{
Mike Christie75613522008-05-21 15:53:59 -05002185 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002186 struct iscsi_conn *conn;
2187 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002188 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002189
Mike Christie5d91e202008-05-21 15:54:01 -05002190 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2191 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002192 if (!cls_conn)
2193 return NULL;
2194 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002195 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002196
Mike Christie5d91e202008-05-21 15:54:01 -05002197 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002198 conn->session = session;
2199 conn->cls_conn = cls_conn;
2200 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2201 conn->id = conn_idx;
2202 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002203 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002204
2205 init_timer(&conn->transport_timer);
2206 conn->transport_timer.data = (unsigned long)conn;
2207 conn->transport_timer.function = iscsi_check_transport_timeouts;
2208
Mike Christie7996a772006-04-06 21:13:41 -05002209 INIT_LIST_HEAD(&conn->run_list);
2210 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002211 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002212 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002213 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002214 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002215
Mike Christie9c19a7d2008-05-21 15:54:09 -05002216 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002217 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002218 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002219 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002220 sizeof(void*))) {
2221 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002222 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002223 }
2224 spin_unlock_bh(&session->lock);
2225
Mike Christiebf32ed32007-02-28 17:32:17 -06002226 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002227 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002228 goto login_task_data_alloc_fail;
2229 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002230
Mike Christie843c0a82007-12-13 12:43:20 -06002231 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002232 init_waitqueue_head(&conn->ehwait);
2233
2234 return cls_conn;
2235
Mike Christie9c19a7d2008-05-21 15:54:09 -05002236login_task_data_alloc_fail:
2237 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002238 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002239login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002240 iscsi_destroy_conn(cls_conn);
2241 return NULL;
2242}
2243EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2244
2245/**
2246 * iscsi_conn_teardown - teardown iscsi connection
2247 * cls_conn: iscsi class connection
2248 *
2249 * TODO: we may need to make this into a two step process
2250 * like scsi-mls remove + put host
2251 */
2252void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2253{
2254 struct iscsi_conn *conn = cls_conn->dd_data;
2255 struct iscsi_session *session = conn->session;
2256 unsigned long flags;
2257
Mike Christief6d51802007-12-13 12:43:30 -06002258 del_timer_sync(&conn->transport_timer);
2259
Mike Christie7996a772006-04-06 21:13:41 -05002260 spin_lock_bh(&session->lock);
2261 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2262 if (session->leadconn == conn) {
2263 /*
2264 * leading connection? then give up on recovery.
2265 */
2266 session->state = ISCSI_STATE_TERMINATE;
2267 wake_up(&conn->ehwait);
2268 }
2269 spin_unlock_bh(&session->lock);
2270
Mike Christie7996a772006-04-06 21:13:41 -05002271 /*
2272 * Block until all in-progress commands for this connection
2273 * time out or fail.
2274 */
2275 for (;;) {
2276 spin_lock_irqsave(session->host->host_lock, flags);
2277 if (!session->host->host_busy) { /* OK for ERL == 0 */
2278 spin_unlock_irqrestore(session->host->host_lock, flags);
2279 break;
2280 }
2281 spin_unlock_irqrestore(session->host->host_lock, flags);
2282 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002283 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2284 "host_busy %d host_failed %d\n",
2285 session->host->host_busy,
2286 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002287 /*
2288 * force eh_abort() to unblock
2289 */
2290 wake_up(&conn->ehwait);
2291 }
2292
Mike Christie779ea122007-02-28 17:32:15 -06002293 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002294 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002295
Mike Christie7996a772006-04-06 21:13:41 -05002296 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002297 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002298 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002299 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002300 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002301 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002302 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002303 spin_unlock_bh(&session->lock);
2304
Mike Christie7996a772006-04-06 21:13:41 -05002305 iscsi_destroy_conn(cls_conn);
2306}
2307EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2308
2309int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2310{
2311 struct iscsi_conn *conn = cls_conn->dd_data;
2312 struct iscsi_session *session = conn->session;
2313
Mike Christieffd04362006-08-31 18:09:24 -04002314 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002315 iscsi_conn_printk(KERN_ERR, conn,
2316 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002317 return -EPERM;
2318 }
2319
Mike Christiedb98ccd2006-08-31 18:09:31 -04002320 if ((session->imm_data_en || !session->initial_r2t_en) &&
2321 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002322 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2323 "first_burst %d max_burst %d\n",
2324 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002325 return -EINVAL;
2326 }
2327
Mike Christief6d51802007-12-13 12:43:30 -06002328 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002329 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2330 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002331 conn->recv_timeout = 5;
2332 }
2333
2334 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002335 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2336 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002337 conn->ping_timeout = 5;
2338 }
2339
Mike Christie7996a772006-04-06 21:13:41 -05002340 spin_lock_bh(&session->lock);
2341 conn->c_stage = ISCSI_CONN_STARTED;
2342 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002343 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002344
Mike Christief6d51802007-12-13 12:43:30 -06002345 conn->last_recv = jiffies;
2346 conn->last_ping = jiffies;
2347 if (conn->recv_timeout && conn->ping_timeout)
2348 mod_timer(&conn->transport_timer,
2349 jiffies + (conn->recv_timeout * HZ));
2350
Mike Christie7996a772006-04-06 21:13:41 -05002351 switch(conn->stop_stage) {
2352 case STOP_CONN_RECOVER:
2353 /*
2354 * unblock eh_abort() if it is blocked. re-try all
2355 * commands after successful recovery
2356 */
Mike Christie7996a772006-04-06 21:13:41 -05002357 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002358 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002359 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002360 if (session->age == 16)
2361 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002362 break;
Mike Christie7996a772006-04-06 21:13:41 -05002363 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002364 conn->stop_stage = 0;
2365 break;
Mike Christie7996a772006-04-06 21:13:41 -05002366 default:
2367 break;
2368 }
2369 spin_unlock_bh(&session->lock);
2370
Mike Christie75613522008-05-21 15:53:59 -05002371 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002372 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002373 return 0;
2374}
2375EXPORT_SYMBOL_GPL(iscsi_conn_start);
2376
2377static void
2378flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2379{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002380 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002381
2382 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002383 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2384 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2385 /* release ref from prep task */
2386 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002387 }
2388
2389 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002390 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2391 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2392 /* release ref from prep task */
2393 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002394 }
2395
Mike Christie9c19a7d2008-05-21 15:54:09 -05002396 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002397}
2398
Mike Christie656cffc2006-05-18 20:31:42 -05002399static void iscsi_start_session_recovery(struct iscsi_session *session,
2400 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002401{
Mike Christieed2abc72006-05-02 19:46:40 -05002402 int old_stop_stage;
2403
Mike Christief6d51802007-12-13 12:43:30 -06002404 del_timer_sync(&conn->transport_timer);
2405
Mike Christie6724add2007-08-15 01:38:30 -05002406 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002407 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002408 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002409 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002410 mutex_unlock(&session->eh_mutex);
2411 return;
2412 }
2413
2414 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002415 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002416 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002417 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002418 */
Mike Christie67a61112006-05-30 00:37:20 -05002419 if (flag == STOP_CONN_TERM)
2420 session->state = ISCSI_STATE_TERMINATE;
2421 else if (conn->stop_stage != STOP_CONN_RECOVER)
2422 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002423
2424 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002425 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002426 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002427 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002428
2429 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002430 /*
2431 * for connection level recovery we should not calculate
2432 * header digest. conn->hdr_size used for optimization
2433 * in hdr_extract() and will be re-negotiated at
2434 * set_param() time.
2435 */
2436 if (flag == STOP_CONN_RECOVER) {
2437 conn->hdrdgst_en = 0;
2438 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002439 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002440 old_stop_stage != STOP_CONN_RECOVER) {
2441 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002442 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002443 }
Mike Christie7996a772006-04-06 21:13:41 -05002444 }
Mike Christie656cffc2006-05-18 20:31:42 -05002445
Mike Christie656cffc2006-05-18 20:31:42 -05002446 /*
2447 * flush queues.
2448 */
2449 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002450 if (flag == STOP_CONN_RECOVER)
Mike Christie56d7fcf2008-08-19 18:45:26 -05002451 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2452 else
2453 fail_all_commands(conn, -1, DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002454 flush_control_queues(session, conn);
2455 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002456 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002457}
Mike Christie7996a772006-04-06 21:13:41 -05002458
2459void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2460{
2461 struct iscsi_conn *conn = cls_conn->dd_data;
2462 struct iscsi_session *session = conn->session;
2463
2464 switch (flag) {
2465 case STOP_CONN_RECOVER:
2466 case STOP_CONN_TERM:
2467 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002468 break;
Mike Christie7996a772006-04-06 21:13:41 -05002469 default:
Mike Christie322d7392008-01-31 13:36:52 -06002470 iscsi_conn_printk(KERN_ERR, conn,
2471 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002472 }
2473}
2474EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2475
2476int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2477 struct iscsi_cls_conn *cls_conn, int is_leading)
2478{
Mike Christie75613522008-05-21 15:53:59 -05002479 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002480 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002481
Mike Christie7996a772006-04-06 21:13:41 -05002482 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002483 if (is_leading)
2484 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002485 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002486
2487 /*
2488 * Unblock xmitworker(), Login Phase will pass through.
2489 */
2490 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2491 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2492 return 0;
2493}
2494EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2495
Mike Christiea54a52c2006-06-28 12:00:23 -05002496
2497int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2498 enum iscsi_param param, char *buf, int buflen)
2499{
2500 struct iscsi_conn *conn = cls_conn->dd_data;
2501 struct iscsi_session *session = conn->session;
2502 uint32_t value;
2503
2504 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002505 case ISCSI_PARAM_FAST_ABORT:
2506 sscanf(buf, "%d", &session->fast_abort);
2507 break;
Mike Christief6d51802007-12-13 12:43:30 -06002508 case ISCSI_PARAM_ABORT_TMO:
2509 sscanf(buf, "%d", &session->abort_timeout);
2510 break;
2511 case ISCSI_PARAM_LU_RESET_TMO:
2512 sscanf(buf, "%d", &session->lu_reset_timeout);
2513 break;
2514 case ISCSI_PARAM_PING_TMO:
2515 sscanf(buf, "%d", &conn->ping_timeout);
2516 break;
2517 case ISCSI_PARAM_RECV_TMO:
2518 sscanf(buf, "%d", &conn->recv_timeout);
2519 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002520 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2521 sscanf(buf, "%d", &conn->max_recv_dlength);
2522 break;
2523 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2524 sscanf(buf, "%d", &conn->max_xmit_dlength);
2525 break;
2526 case ISCSI_PARAM_HDRDGST_EN:
2527 sscanf(buf, "%d", &conn->hdrdgst_en);
2528 break;
2529 case ISCSI_PARAM_DATADGST_EN:
2530 sscanf(buf, "%d", &conn->datadgst_en);
2531 break;
2532 case ISCSI_PARAM_INITIAL_R2T_EN:
2533 sscanf(buf, "%d", &session->initial_r2t_en);
2534 break;
2535 case ISCSI_PARAM_MAX_R2T:
2536 sscanf(buf, "%d", &session->max_r2t);
2537 break;
2538 case ISCSI_PARAM_IMM_DATA_EN:
2539 sscanf(buf, "%d", &session->imm_data_en);
2540 break;
2541 case ISCSI_PARAM_FIRST_BURST:
2542 sscanf(buf, "%d", &session->first_burst);
2543 break;
2544 case ISCSI_PARAM_MAX_BURST:
2545 sscanf(buf, "%d", &session->max_burst);
2546 break;
2547 case ISCSI_PARAM_PDU_INORDER_EN:
2548 sscanf(buf, "%d", &session->pdu_inorder_en);
2549 break;
2550 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2551 sscanf(buf, "%d", &session->dataseq_inorder_en);
2552 break;
2553 case ISCSI_PARAM_ERL:
2554 sscanf(buf, "%d", &session->erl);
2555 break;
2556 case ISCSI_PARAM_IFMARKER_EN:
2557 sscanf(buf, "%d", &value);
2558 BUG_ON(value);
2559 break;
2560 case ISCSI_PARAM_OFMARKER_EN:
2561 sscanf(buf, "%d", &value);
2562 BUG_ON(value);
2563 break;
2564 case ISCSI_PARAM_EXP_STATSN:
2565 sscanf(buf, "%u", &conn->exp_statsn);
2566 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002567 case ISCSI_PARAM_USERNAME:
2568 kfree(session->username);
2569 session->username = kstrdup(buf, GFP_KERNEL);
2570 if (!session->username)
2571 return -ENOMEM;
2572 break;
2573 case ISCSI_PARAM_USERNAME_IN:
2574 kfree(session->username_in);
2575 session->username_in = kstrdup(buf, GFP_KERNEL);
2576 if (!session->username_in)
2577 return -ENOMEM;
2578 break;
2579 case ISCSI_PARAM_PASSWORD:
2580 kfree(session->password);
2581 session->password = kstrdup(buf, GFP_KERNEL);
2582 if (!session->password)
2583 return -ENOMEM;
2584 break;
2585 case ISCSI_PARAM_PASSWORD_IN:
2586 kfree(session->password_in);
2587 session->password_in = kstrdup(buf, GFP_KERNEL);
2588 if (!session->password_in)
2589 return -ENOMEM;
2590 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002591 case ISCSI_PARAM_TARGET_NAME:
2592 /* this should not change between logins */
2593 if (session->targetname)
2594 break;
2595
2596 session->targetname = kstrdup(buf, GFP_KERNEL);
2597 if (!session->targetname)
2598 return -ENOMEM;
2599 break;
2600 case ISCSI_PARAM_TPGT:
2601 sscanf(buf, "%d", &session->tpgt);
2602 break;
2603 case ISCSI_PARAM_PERSISTENT_PORT:
2604 sscanf(buf, "%d", &conn->persistent_port);
2605 break;
2606 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2607 /*
2608 * this is the address returned in discovery so it should
2609 * not change between logins.
2610 */
2611 if (conn->persistent_address)
2612 break;
2613
2614 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2615 if (!conn->persistent_address)
2616 return -ENOMEM;
2617 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002618 case ISCSI_PARAM_IFACE_NAME:
2619 if (!session->ifacename)
2620 session->ifacename = kstrdup(buf, GFP_KERNEL);
2621 break;
2622 case ISCSI_PARAM_INITIATOR_NAME:
2623 if (!session->initiatorname)
2624 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2625 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002626 default:
2627 return -ENOSYS;
2628 }
2629
2630 return 0;
2631}
2632EXPORT_SYMBOL_GPL(iscsi_set_param);
2633
2634int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2635 enum iscsi_param param, char *buf)
2636{
Mike Christie75613522008-05-21 15:53:59 -05002637 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002638 int len;
2639
2640 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002641 case ISCSI_PARAM_FAST_ABORT:
2642 len = sprintf(buf, "%d\n", session->fast_abort);
2643 break;
Mike Christief6d51802007-12-13 12:43:30 -06002644 case ISCSI_PARAM_ABORT_TMO:
2645 len = sprintf(buf, "%d\n", session->abort_timeout);
2646 break;
2647 case ISCSI_PARAM_LU_RESET_TMO:
2648 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2649 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002650 case ISCSI_PARAM_INITIAL_R2T_EN:
2651 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2652 break;
2653 case ISCSI_PARAM_MAX_R2T:
2654 len = sprintf(buf, "%hu\n", session->max_r2t);
2655 break;
2656 case ISCSI_PARAM_IMM_DATA_EN:
2657 len = sprintf(buf, "%d\n", session->imm_data_en);
2658 break;
2659 case ISCSI_PARAM_FIRST_BURST:
2660 len = sprintf(buf, "%u\n", session->first_burst);
2661 break;
2662 case ISCSI_PARAM_MAX_BURST:
2663 len = sprintf(buf, "%u\n", session->max_burst);
2664 break;
2665 case ISCSI_PARAM_PDU_INORDER_EN:
2666 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2667 break;
2668 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2669 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2670 break;
2671 case ISCSI_PARAM_ERL:
2672 len = sprintf(buf, "%d\n", session->erl);
2673 break;
2674 case ISCSI_PARAM_TARGET_NAME:
2675 len = sprintf(buf, "%s\n", session->targetname);
2676 break;
2677 case ISCSI_PARAM_TPGT:
2678 len = sprintf(buf, "%d\n", session->tpgt);
2679 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002680 case ISCSI_PARAM_USERNAME:
2681 len = sprintf(buf, "%s\n", session->username);
2682 break;
2683 case ISCSI_PARAM_USERNAME_IN:
2684 len = sprintf(buf, "%s\n", session->username_in);
2685 break;
2686 case ISCSI_PARAM_PASSWORD:
2687 len = sprintf(buf, "%s\n", session->password);
2688 break;
2689 case ISCSI_PARAM_PASSWORD_IN:
2690 len = sprintf(buf, "%s\n", session->password_in);
2691 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002692 case ISCSI_PARAM_IFACE_NAME:
2693 len = sprintf(buf, "%s\n", session->ifacename);
2694 break;
2695 case ISCSI_PARAM_INITIATOR_NAME:
2696 if (!session->initiatorname)
2697 len = sprintf(buf, "%s\n", "unknown");
2698 else
2699 len = sprintf(buf, "%s\n", session->initiatorname);
2700 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002701 default:
2702 return -ENOSYS;
2703 }
2704
2705 return len;
2706}
2707EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2708
2709int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2710 enum iscsi_param param, char *buf)
2711{
2712 struct iscsi_conn *conn = cls_conn->dd_data;
2713 int len;
2714
2715 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002716 case ISCSI_PARAM_PING_TMO:
2717 len = sprintf(buf, "%u\n", conn->ping_timeout);
2718 break;
2719 case ISCSI_PARAM_RECV_TMO:
2720 len = sprintf(buf, "%u\n", conn->recv_timeout);
2721 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002722 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2723 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2724 break;
2725 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2726 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2727 break;
2728 case ISCSI_PARAM_HDRDGST_EN:
2729 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2730 break;
2731 case ISCSI_PARAM_DATADGST_EN:
2732 len = sprintf(buf, "%d\n", conn->datadgst_en);
2733 break;
2734 case ISCSI_PARAM_IFMARKER_EN:
2735 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2736 break;
2737 case ISCSI_PARAM_OFMARKER_EN:
2738 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2739 break;
2740 case ISCSI_PARAM_EXP_STATSN:
2741 len = sprintf(buf, "%u\n", conn->exp_statsn);
2742 break;
2743 case ISCSI_PARAM_PERSISTENT_PORT:
2744 len = sprintf(buf, "%d\n", conn->persistent_port);
2745 break;
2746 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2747 len = sprintf(buf, "%s\n", conn->persistent_address);
2748 break;
2749 default:
2750 return -ENOSYS;
2751 }
2752
2753 return len;
2754}
2755EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2756
Mike Christie0801c242007-05-30 12:57:12 -05002757int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2758 char *buf)
2759{
Mike Christie75613522008-05-21 15:53:59 -05002760 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002761 int len;
2762
2763 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002764 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002765 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002766 len = sprintf(buf, "%s\n", "default");
2767 else
Mike Christie75613522008-05-21 15:53:59 -05002768 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002769 break;
Mike Christie0801c242007-05-30 12:57:12 -05002770 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002771 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002772 len = sprintf(buf, "%s\n", "default");
2773 else
Mike Christie75613522008-05-21 15:53:59 -05002774 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002775 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002776 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002777 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002778 len = sprintf(buf, "%s\n", "unknown");
2779 else
Mike Christie75613522008-05-21 15:53:59 -05002780 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002781 break;
Mike Christie75613522008-05-21 15:53:59 -05002782 case ISCSI_HOST_PARAM_IPADDRESS:
2783 if (!strlen(ihost->local_address))
2784 len = sprintf(buf, "%s\n", "unknown");
2785 else
2786 len = sprintf(buf, "%s\n",
2787 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002788 break;
Mike Christie0801c242007-05-30 12:57:12 -05002789 default:
2790 return -ENOSYS;
2791 }
2792
2793 return len;
2794}
2795EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2796
2797int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2798 char *buf, int buflen)
2799{
Mike Christie75613522008-05-21 15:53:59 -05002800 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002801
2802 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002803 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002804 if (!ihost->netdev)
2805 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002806 break;
Mike Christie0801c242007-05-30 12:57:12 -05002807 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002808 if (!ihost->hwaddress)
2809 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002810 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002811 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002812 if (!ihost->initiatorname)
2813 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002814 break;
Mike Christie0801c242007-05-30 12:57:12 -05002815 default:
2816 return -ENOSYS;
2817 }
2818
2819 return 0;
2820}
2821EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2822
Mike Christie7996a772006-04-06 21:13:41 -05002823MODULE_AUTHOR("Mike Christie");
2824MODULE_DESCRIPTION("iSCSI library functions");
2825MODULE_LICENSE("GPL");