blob: 801c7cf54d2ebb29b209fbd692eb7c807514fdc6 [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;
Mike Christie052d0142008-05-21 15:54:05 -0500492
493 if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
494 hdr->ttt == RESERVED_ITT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500495 conn->ping_task = task;
Mike Christie052d0142008-05-21 15:54:05 -0500496 conn->last_ping = jiffies;
497 }
Mike Christief6d51802007-12-13 12:43:30 -0600498 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500499 /*
500 * released in complete pdu for task we expect a response for, and
501 * released by the lld when it has transmitted the task for
502 * pdus we do not expect a response for.
503 */
Mike Christie9c19a7d2008-05-21 15:54:09 -0500504 atomic_set(&task->refcount, 1);
505 task->conn = conn;
506 task->sc = NULL;
Mike Christief6d51802007-12-13 12:43:30 -0600507
508 if (data_size) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500509 memcpy(task->data, data, data_size);
510 task->data_count = data_size;
Mike Christief6d51802007-12-13 12:43:30 -0600511 } else
Mike Christie9c19a7d2008-05-21 15:54:09 -0500512 task->data_count = 0;
Mike Christief6d51802007-12-13 12:43:30 -0600513
Mike Christie9c19a7d2008-05-21 15:54:09 -0500514 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
515 INIT_LIST_HEAD(&task->running);
516 list_add_tail(&task->running, &conn->mgmtqueue);
Mike Christie052d0142008-05-21 15:54:05 -0500517
518 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -0500519 if (iscsi_prep_mgmt_task(conn, task)) {
520 __iscsi_put_task(task);
Mike Christie052d0142008-05-21 15:54:05 -0500521 return NULL;
522 }
523
Mike Christie9c19a7d2008-05-21 15:54:09 -0500524 if (session->tt->xmit_task(task))
525 task = NULL;
Mike Christie052d0142008-05-21 15:54:05 -0500526
527 } else
528 scsi_queue_work(conn->session->host, &conn->xmitwork);
529
Mike Christie9c19a7d2008-05-21 15:54:09 -0500530 return task;
Mike Christief6d51802007-12-13 12:43:30 -0600531}
532
533int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
534 char *data, uint32_t data_size)
535{
536 struct iscsi_conn *conn = cls_conn->dd_data;
537 struct iscsi_session *session = conn->session;
538 int err = 0;
539
540 spin_lock_bh(&session->lock);
541 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
542 err = -EPERM;
543 spin_unlock_bh(&session->lock);
Mike Christief6d51802007-12-13 12:43:30 -0600544 return err;
545}
546EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
547
Mike Christie7996a772006-04-06 21:13:41 -0500548/**
549 * iscsi_cmd_rsp - SCSI Command Response processing
550 * @conn: iscsi connection
551 * @hdr: iscsi header
Mike Christie9c19a7d2008-05-21 15:54:09 -0500552 * @task: scsi command task
Mike Christie7996a772006-04-06 21:13:41 -0500553 * @data: cmd data buffer
554 * @datalen: len of buffer
555 *
556 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
Mike Christie9c19a7d2008-05-21 15:54:09 -0500557 * then completes the command and task.
Mike Christie7996a772006-04-06 21:13:41 -0500558 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500559static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
Mike Christie9c19a7d2008-05-21 15:54:09 -0500560 struct iscsi_task *task, char *data,
Mike Christie77a23c22007-05-30 12:57:18 -0500561 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500562{
Mike Christie7996a772006-04-06 21:13:41 -0500563 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
564 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500565 struct scsi_cmnd *sc = task->sc;
Mike Christie7996a772006-04-06 21:13:41 -0500566
Mike Christie77a23c22007-05-30 12:57:18 -0500567 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500568 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
569
570 sc->result = (DID_OK << 16) | rhdr->cmd_status;
571
572 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
573 sc->result = DID_ERROR << 16;
574 goto out;
575 }
576
577 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600578 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500579
580 if (datalen < 2) {
581invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600582 iscsi_conn_printk(KERN_ERR, conn,
583 "Got CHECK_CONDITION but invalid data "
584 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500585 sc->result = DID_BAD_TARGET << 16;
586 goto out;
587 }
588
Harvey Harrison8f3339912008-05-21 15:54:20 -0500589 senselen = get_unaligned_be16(data);
Mike Christie7996a772006-04-06 21:13:41 -0500590 if (datalen < senselen)
591 goto invalid_datalen;
592
593 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600594 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500595 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600596 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500597 }
598
Boaz Harroshc07d4442008-04-18 10:11:52 -0500599 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
600 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
601 int res_count = be32_to_cpu(rhdr->bi_residual_count);
602
603 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
604 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
605 res_count <= scsi_in(sc)->length))
606 scsi_in(sc)->resid = res_count;
607 else
608 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
609 }
610
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600611 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
612 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500613 int res_count = be32_to_cpu(rhdr->residual_count);
614
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600615 if (res_count > 0 &&
616 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
617 res_count <= scsi_bufflen(sc)))
Boaz Harroshc07d4442008-04-18 10:11:52 -0500618 /* write side for bidi or uni-io set_resid */
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900619 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500620 else
621 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harroshc07d4442008-04-18 10:11:52 -0500622 }
Mike Christie7996a772006-04-06 21:13:41 -0500623out:
624 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -0500625 (long)sc, sc->result, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500626 conn->scsirsp_pdus_cnt++;
627
Mike Christie9c19a7d2008-05-21 15:54:09 -0500628 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500629}
630
Mike Christie1d9edf02008-09-24 11:46:09 -0500631/**
632 * iscsi_data_in_rsp - SCSI Data-In Response processing
633 * @conn: iscsi connection
634 * @hdr: iscsi pdu
635 * @task: scsi command task
636 **/
637static void
638iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
639 struct iscsi_task *task)
640{
641 struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
642 struct scsi_cmnd *sc = task->sc;
643
644 if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
645 return;
646
647 sc->result = (DID_OK << 16) | rhdr->cmd_status;
648 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
649 if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
650 ISCSI_FLAG_DATA_OVERFLOW)) {
651 int res_count = be32_to_cpu(rhdr->residual_count);
652
653 if (res_count > 0 &&
654 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
655 res_count <= scsi_in(sc)->length))
656 scsi_in(sc)->resid = res_count;
657 else
658 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
659 }
660
661 conn->scsirsp_pdus_cnt++;
662 __iscsi_put_task(task);
663}
664
Mike Christie7ea8b822006-07-24 15:47:22 -0500665static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
666{
667 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
668
669 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
670 conn->tmfrsp_pdus_cnt++;
671
Mike Christie843c0a82007-12-13 12:43:20 -0600672 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500673 return;
674
675 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600676 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500677 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600678 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500679 else
Mike Christie843c0a82007-12-13 12:43:20 -0600680 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500681 wake_up(&conn->ehwait);
682}
683
Mike Christief6d51802007-12-13 12:43:30 -0600684static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
685{
686 struct iscsi_nopout hdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500687 struct iscsi_task *task;
Mike Christief6d51802007-12-13 12:43:30 -0600688
Mike Christie9c19a7d2008-05-21 15:54:09 -0500689 if (!rhdr && conn->ping_task)
Mike Christief6d51802007-12-13 12:43:30 -0600690 return;
691
692 memset(&hdr, 0, sizeof(struct iscsi_nopout));
693 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
694 hdr.flags = ISCSI_FLAG_CMD_FINAL;
695
696 if (rhdr) {
697 memcpy(hdr.lun, rhdr->lun, 8);
698 hdr.ttt = rhdr->ttt;
699 hdr.itt = RESERVED_ITT;
700 } else
701 hdr.ttt = RESERVED_ITT;
702
Mike Christie9c19a7d2008-05-21 15:54:09 -0500703 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
704 if (!task)
Mike Christie322d7392008-01-31 13:36:52 -0600705 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600706}
707
Mike Christie62f38302006-08-31 18:09:27 -0400708static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
709 char *data, int datalen)
710{
711 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
712 struct iscsi_hdr rejected_pdu;
713 uint32_t itt;
714
715 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
716
717 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
718 if (ntoh24(reject->dlength) > datalen)
719 return ISCSI_ERR_PROTO;
720
721 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
722 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000723 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600724 iscsi_conn_printk(KERN_ERR, conn,
725 "itt 0x%x had pdu (op 0x%x) rejected "
726 "due to DataDigest error.\n", itt,
727 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400728 }
729 }
730 return 0;
731}
732
Mike Christie7996a772006-04-06 21:13:41 -0500733/**
Mike Christie913e5bf2008-05-21 15:54:18 -0500734 * iscsi_itt_to_task - look up task by itt
735 * @conn: iscsi connection
736 * @itt: itt
737 *
738 * This should be used for mgmt tasks like login and nops, or if
739 * the LDD's itt space does not include the session age.
740 *
741 * The session lock must be held.
742 */
743static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
744{
745 struct iscsi_session *session = conn->session;
746 uint32_t i;
747
748 if (itt == RESERVED_ITT)
749 return NULL;
750
751 i = get_itt(itt);
752 if (i >= session->cmds_max)
753 return NULL;
754
755 return session->cmds[i];
756}
757
758/**
Mike Christie7996a772006-04-06 21:13:41 -0500759 * __iscsi_complete_pdu - complete pdu
760 * @conn: iscsi conn
761 * @hdr: iscsi header
762 * @data: data buffer
763 * @datalen: len of data buffer
764 *
765 * Completes pdu processing by freeing any resources allocated at
766 * queuecommand or send generic. session lock must be held and verify
767 * itt must have been called.
768 */
Mike Christie913e5bf2008-05-21 15:54:18 -0500769int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
770 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500771{
772 struct iscsi_session *session = conn->session;
773 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500774 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -0500775 uint32_t itt;
776
Mike Christief6d51802007-12-13 12:43:30 -0600777 conn->last_recv = jiffies;
Mike Christie0af967f2008-05-21 15:54:04 -0500778 rc = iscsi_verify_itt(conn, hdr->itt);
779 if (rc)
780 return rc;
781
Al Virob4377352007-02-09 16:39:40 +0000782 if (hdr->itt != RESERVED_ITT)
783 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500784 else
Al Virob4377352007-02-09 16:39:40 +0000785 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500786
Mike Christie3e5c28a2008-05-21 15:54:06 -0500787 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
788 opcode, conn->id, itt, datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500789
Mike Christie3e5c28a2008-05-21 15:54:06 -0500790 if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500791 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400792
Mike Christie7996a772006-04-06 21:13:41 -0500793 switch(opcode) {
794 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500795 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500796 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500797 break;
798 }
799
Al Virob4377352007-02-09 16:39:40 +0000800 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500801 break;
802
Mike Christief6d51802007-12-13 12:43:30 -0600803 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500804 break;
805 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400806 rc = iscsi_handle_reject(conn, hdr, data, datalen);
807 break;
Mike Christie7996a772006-04-06 21:13:41 -0500808 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500809 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400810 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
811 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500812 break;
813 default:
814 rc = ISCSI_ERR_BAD_OPCODE;
815 break;
816 }
Mike Christie3e5c28a2008-05-21 15:54:06 -0500817 goto out;
818 }
Mike Christie7996a772006-04-06 21:13:41 -0500819
Mike Christie3e5c28a2008-05-21 15:54:06 -0500820 switch(opcode) {
821 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie913e5bf2008-05-21 15:54:18 -0500822 case ISCSI_OP_SCSI_DATA_IN:
823 task = iscsi_itt_to_ctask(conn, hdr->itt);
824 if (!task)
825 return ISCSI_ERR_BAD_ITT;
826 break;
827 case ISCSI_OP_R2T:
828 /*
829 * LLD handles R2Ts if they need to.
830 */
831 return 0;
832 case ISCSI_OP_LOGOUT_RSP:
833 case ISCSI_OP_LOGIN_RSP:
834 case ISCSI_OP_TEXT_RSP:
835 case ISCSI_OP_SCSI_TMFUNC_RSP:
836 case ISCSI_OP_NOOP_IN:
837 task = iscsi_itt_to_task(conn, hdr->itt);
838 if (!task)
839 return ISCSI_ERR_BAD_ITT;
840 break;
841 default:
842 return ISCSI_ERR_BAD_OPCODE;
843 }
844
845 switch(opcode) {
846 case ISCSI_OP_SCSI_CMD_RSP:
Mike Christie9c19a7d2008-05-21 15:54:09 -0500847 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500848 break;
849 case ISCSI_OP_SCSI_DATA_IN:
Mike Christie1d9edf02008-09-24 11:46:09 -0500850 iscsi_data_in_rsp(conn, hdr, task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500851 break;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500852 case ISCSI_OP_LOGOUT_RSP:
853 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
854 if (datalen) {
855 rc = ISCSI_ERR_PROTO;
856 break;
857 }
858 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
859 goto recv_pdu;
860 case ISCSI_OP_LOGIN_RSP:
861 case ISCSI_OP_TEXT_RSP:
862 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
863 /*
864 * login related PDU's exp_statsn is handled in
865 * userspace
866 */
867 goto recv_pdu;
868 case ISCSI_OP_SCSI_TMFUNC_RSP:
869 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
870 if (datalen) {
871 rc = ISCSI_ERR_PROTO;
872 break;
873 }
874
875 iscsi_tmf_rsp(conn, hdr);
Mike Christie9c19a7d2008-05-21 15:54:09 -0500876 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500877 break;
878 case ISCSI_OP_NOOP_IN:
879 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
880 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
881 rc = ISCSI_ERR_PROTO;
882 break;
883 }
884 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
885
Mike Christie9c19a7d2008-05-21 15:54:09 -0500886 if (conn->ping_task != task)
Mike Christie3e5c28a2008-05-21 15:54:06 -0500887 /*
888 * If this is not in response to one of our
889 * nops then it must be from userspace.
890 */
891 goto recv_pdu;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500892
893 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
894 __iscsi_put_task(task);
Mike Christie3e5c28a2008-05-21 15:54:06 -0500895 break;
896 default:
897 rc = ISCSI_ERR_BAD_OPCODE;
898 break;
899 }
900
901out:
902 return rc;
903recv_pdu:
904 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
905 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie9c19a7d2008-05-21 15:54:09 -0500906 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -0500907 return rc;
908}
Mike Christie913e5bf2008-05-21 15:54:18 -0500909EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
Mike Christie7996a772006-04-06 21:13:41 -0500910
911int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
912 char *data, int datalen)
913{
914 int rc;
915
916 spin_lock(&conn->session->lock);
917 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
918 spin_unlock(&conn->session->lock);
919 return rc;
920}
921EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
922
Mike Christie0af967f2008-05-21 15:54:04 -0500923int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
Mike Christie7996a772006-04-06 21:13:41 -0500924{
925 struct iscsi_session *session = conn->session;
Mike Christie3e5c28a2008-05-21 15:54:06 -0500926 uint32_t i;
Mike Christie7996a772006-04-06 21:13:41 -0500927
Mike Christie0af967f2008-05-21 15:54:04 -0500928 if (itt == RESERVED_ITT)
929 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500930
Mike Christie0af967f2008-05-21 15:54:04 -0500931 if (((__force u32)itt & ISCSI_AGE_MASK) !=
932 (session->age << ISCSI_AGE_SHIFT)) {
933 iscsi_conn_printk(KERN_ERR, conn,
934 "received itt %x expected session age (%x)\n",
Mike Christie913e5bf2008-05-21 15:54:18 -0500935 (__force u32)itt, session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500936 return ISCSI_ERR_BAD_ITT;
937 }
Mike Christie7996a772006-04-06 21:13:41 -0500938
Mike Christie3e5c28a2008-05-21 15:54:06 -0500939 i = get_itt(itt);
940 if (i >= session->cmds_max) {
941 iscsi_conn_printk(KERN_ERR, conn,
942 "received invalid itt index %u (max cmds "
943 "%u.\n", i, session->cmds_max);
944 return ISCSI_ERR_BAD_ITT;
Mike Christie7996a772006-04-06 21:13:41 -0500945 }
Mike Christie7996a772006-04-06 21:13:41 -0500946 return 0;
947}
948EXPORT_SYMBOL_GPL(iscsi_verify_itt);
949
Mike Christie913e5bf2008-05-21 15:54:18 -0500950/**
951 * iscsi_itt_to_ctask - look up ctask by itt
952 * @conn: iscsi connection
953 * @itt: itt
954 *
955 * This should be used for cmd tasks.
956 *
957 * The session lock must be held.
958 */
959struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
Mike Christie0af967f2008-05-21 15:54:04 -0500960{
Mike Christie9c19a7d2008-05-21 15:54:09 -0500961 struct iscsi_task *task;
Mike Christie0af967f2008-05-21 15:54:04 -0500962
963 if (iscsi_verify_itt(conn, itt))
964 return NULL;
965
Mike Christie913e5bf2008-05-21 15:54:18 -0500966 task = iscsi_itt_to_task(conn, itt);
967 if (!task || !task->sc)
Mike Christie0af967f2008-05-21 15:54:04 -0500968 return NULL;
969
Mike Christie913e5bf2008-05-21 15:54:18 -0500970 if (task->sc->SCp.phase != conn->session->age) {
971 iscsi_session_printk(KERN_ERR, conn->session,
972 "task's session age %d, expected %d\n",
973 task->sc->SCp.phase, conn->session->age);
Mike Christie0af967f2008-05-21 15:54:04 -0500974 return NULL;
Mike Christie913e5bf2008-05-21 15:54:18 -0500975 }
Mike Christie0af967f2008-05-21 15:54:04 -0500976
Mike Christie9c19a7d2008-05-21 15:54:09 -0500977 return task;
Mike Christie0af967f2008-05-21 15:54:04 -0500978}
979EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
980
Mike Christiee5bd7b52008-09-24 11:46:10 -0500981void iscsi_session_failure(struct iscsi_cls_session *cls_session,
982 enum iscsi_err err)
983{
984 struct iscsi_session *session = cls_session->dd_data;
985 struct iscsi_conn *conn;
986 struct device *dev;
987 unsigned long flags;
988
989 spin_lock_irqsave(&session->lock, flags);
990 conn = session->leadconn;
991 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
992 spin_unlock_irqrestore(&session->lock, flags);
993 return;
994 }
995
996 dev = get_device(&conn->cls_conn->dev);
997 spin_unlock_irqrestore(&session->lock, flags);
998 if (!dev)
999 return;
1000 /*
1001 * if the host is being removed bypass the connection
1002 * recovery initialization because we are going to kill
1003 * the session.
1004 */
1005 if (err == ISCSI_ERR_INVALID_HOST)
1006 iscsi_conn_error_event(conn->cls_conn, err);
1007 else
1008 iscsi_conn_failure(conn, err);
1009 put_device(dev);
1010}
1011EXPORT_SYMBOL_GPL(iscsi_session_failure);
1012
Mike Christie7996a772006-04-06 21:13:41 -05001013void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1014{
1015 struct iscsi_session *session = conn->session;
1016 unsigned long flags;
1017
1018 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -05001019 if (session->state == ISCSI_STATE_FAILED) {
1020 spin_unlock_irqrestore(&session->lock, flags);
1021 return;
1022 }
1023
Mike Christie67a61112006-05-30 00:37:20 -05001024 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -05001025 session->state = ISCSI_STATE_FAILED;
1026 spin_unlock_irqrestore(&session->lock, flags);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001027
Mike Christie7996a772006-04-06 21:13:41 -05001028 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1029 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
Mike Christiee5bd7b52008-09-24 11:46:10 -05001030 iscsi_conn_error_event(conn->cls_conn, err);
Mike Christie7996a772006-04-06 21:13:41 -05001031}
1032EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1033
Mike Christie77a23c22007-05-30 12:57:18 -05001034static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
1035{
1036 struct iscsi_session *session = conn->session;
1037
1038 /*
1039 * Check for iSCSI window and take care of CmdSN wrap-around
1040 */
Mike Christiee0726402007-07-26 12:46:48 -05001041 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
1042 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
1043 "CmdSN %u/%u\n", session->exp_cmdsn,
1044 session->max_cmdsn, session->cmdsn,
1045 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -05001046 return -ENOSPC;
1047 }
1048 return 0;
1049}
1050
Mike Christie9c19a7d2008-05-21 15:54:09 -05001051static int iscsi_xmit_task(struct iscsi_conn *conn)
Mike Christie77a23c22007-05-30 12:57:18 -05001052{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001053 struct iscsi_task *task = conn->task;
Mike Christie843c0a82007-12-13 12:43:20 -06001054 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -05001055
Mike Christie9c19a7d2008-05-21 15:54:09 -05001056 __iscsi_get_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001057 spin_unlock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001058 rc = conn->session->tt->xmit_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001059 spin_lock_bh(&conn->session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001060 __iscsi_put_task(task);
Mike Christie77a23c22007-05-30 12:57:18 -05001061 if (!rc)
Mike Christie9c19a7d2008-05-21 15:54:09 -05001062 /* done with this task */
1063 conn->task = NULL;
Mike Christie77a23c22007-05-30 12:57:18 -05001064 return rc;
1065}
1066
Mike Christie7996a772006-04-06 21:13:41 -05001067/**
Mike Christie9c19a7d2008-05-21 15:54:09 -05001068 * iscsi_requeue_task - requeue task to run from session workqueue
1069 * @task: task to requeue
Mike Christie843c0a82007-12-13 12:43:20 -06001070 *
Mike Christie9c19a7d2008-05-21 15:54:09 -05001071 * LLDs that need to run a task from the session workqueue should call
Mike Christie052d0142008-05-21 15:54:05 -05001072 * this. The session lock must be held. This should only be called
1073 * by software drivers.
Mike Christie843c0a82007-12-13 12:43:20 -06001074 */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001075void iscsi_requeue_task(struct iscsi_task *task)
Mike Christie843c0a82007-12-13 12:43:20 -06001076{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001077 struct iscsi_conn *conn = task->conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001078
Mike Christie9c19a7d2008-05-21 15:54:09 -05001079 list_move_tail(&task->running, &conn->requeue);
Mike Christie843c0a82007-12-13 12:43:20 -06001080 scsi_queue_work(conn->session->host, &conn->xmitwork);
1081}
Mike Christie9c19a7d2008-05-21 15:54:09 -05001082EXPORT_SYMBOL_GPL(iscsi_requeue_task);
Mike Christie843c0a82007-12-13 12:43:20 -06001083
1084/**
Mike Christie7996a772006-04-06 21:13:41 -05001085 * iscsi_data_xmit - xmit any command into the scheduled connection
1086 * @conn: iscsi connection
1087 *
1088 * Notes:
1089 * The function can return -EAGAIN in which case the caller must
1090 * re-schedule it again later or recover. '0' return code means
1091 * successful xmit.
1092 **/
1093static int iscsi_data_xmit(struct iscsi_conn *conn)
1094{
Mike Christie3219e522006-05-30 00:37:28 -05001095 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -05001096
Mike Christie77a23c22007-05-30 12:57:18 -05001097 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001098 if (unlikely(conn->suspend_tx)) {
1099 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -05001100 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001101 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001102 }
Mike Christie7996a772006-04-06 21:13:41 -05001103
Mike Christie9c19a7d2008-05-21 15:54:09 -05001104 if (conn->task) {
1105 rc = iscsi_xmit_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -05001106 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -05001107 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001108 }
1109
Mike Christie77a23c22007-05-30 12:57:18 -05001110 /*
1111 * process mgmt pdus like nops before commands since we should
1112 * only have one nop-out as a ping from us and targets should not
1113 * overflow us with nop-ins
1114 */
1115check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -06001116 while (!list_empty(&conn->mgmtqueue)) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001117 conn->task = list_entry(conn->mgmtqueue.next,
1118 struct iscsi_task, running);
1119 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1120 __iscsi_put_task(conn->task);
1121 conn->task = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001122 continue;
1123 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001124 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001125 if (rc)
1126 goto again;
Mike Christie7996a772006-04-06 21:13:41 -05001127 }
1128
Mike Christie843c0a82007-12-13 12:43:20 -06001129 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -05001130 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001131 if (conn->tmf_state == TMF_QUEUED)
1132 break;
1133
Mike Christie9c19a7d2008-05-21 15:54:09 -05001134 conn->task = list_entry(conn->xmitqueue.next,
1135 struct iscsi_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001136 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001137 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -06001138 continue;
1139 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001140 if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
1141 fail_command(conn, conn->task, DID_ABORT << 16);
Boaz Harrosh004d6532007-12-13 12:43:23 -06001142 continue;
1143 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001144 rc = iscsi_xmit_task(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001145 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -04001146 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -05001147 /*
Mike Christie9c19a7d2008-05-21 15:54:09 -05001148 * we could continuously get new task requests so
Mike Christie77a23c22007-05-30 12:57:18 -05001149 * we need to check the mgmt queue for nops that need to
1150 * be sent to aviod starvation
1151 */
Mike Christie843c0a82007-12-13 12:43:20 -06001152 if (!list_empty(&conn->mgmtqueue))
1153 goto check_mgmt;
1154 }
1155
1156 while (!list_empty(&conn->requeue)) {
1157 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1158 break;
1159
Mike Christieb3a7ea82007-12-13 12:43:26 -06001160 /*
1161 * we always do fastlogout - conn stop code will clean up.
1162 */
1163 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1164 break;
1165
Mike Christie9c19a7d2008-05-21 15:54:09 -05001166 conn->task = list_entry(conn->requeue.next,
1167 struct iscsi_task, running);
1168 conn->task->state = ISCSI_TASK_RUNNING;
Mike Christie843c0a82007-12-13 12:43:20 -06001169 list_move_tail(conn->requeue.next, &conn->run_list);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001170 rc = iscsi_xmit_task(conn);
Mike Christie843c0a82007-12-13 12:43:20 -06001171 if (rc)
1172 goto again;
1173 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -05001174 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -05001175 }
Mike Christieb6c395e2006-07-24 15:47:15 -05001176 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001177 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -05001178
1179again:
1180 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -05001181 rc = -ENODATA;
1182 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -05001183 return rc;
Mike Christie7996a772006-04-06 21:13:41 -05001184}
1185
David Howellsc4028952006-11-22 14:57:56 +00001186static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -05001187{
David Howellsc4028952006-11-22 14:57:56 +00001188 struct iscsi_conn *conn =
1189 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -05001190 int rc;
Mike Christie7996a772006-04-06 21:13:41 -05001191 /*
1192 * serialize Xmit worker on a per-connection basis.
1193 */
Mike Christie3219e522006-05-30 00:37:28 -05001194 do {
1195 rc = iscsi_data_xmit(conn);
1196 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -05001197}
1198
1199enum {
1200 FAILURE_BAD_HOST = 1,
1201 FAILURE_SESSION_FAILED,
1202 FAILURE_SESSION_FREED,
1203 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001204 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001205 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001206 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001207 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001208 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001209 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001210};
1211
1212int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1213{
Mike Christie75613522008-05-21 15:53:59 -05001214 struct iscsi_cls_session *cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05001215 struct Scsi_Host *host;
1216 int reason = 0;
1217 struct iscsi_session *session;
1218 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001219 struct iscsi_task *task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001220
1221 sc->scsi_done = done;
1222 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001223 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001224
1225 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001226 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001227
Mike Christie75613522008-05-21 15:53:59 -05001228 cls_session = starget_to_session(scsi_target(sc->device));
1229 session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001230 spin_lock(&session->lock);
1231
Mike Christie75613522008-05-21 15:53:59 -05001232 reason = iscsi_session_chkready(cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06001233 if (reason) {
1234 sc->result = reason;
1235 goto fault;
1236 }
1237
Mike Christie656cffc2006-05-18 20:31:42 -05001238 /*
1239 * ISCSI_STATE_FAILED is a temp. state. The recovery
1240 * code will decide what is best to do with command queued
1241 * during this time
1242 */
1243 if (session->state != ISCSI_STATE_LOGGED_IN &&
1244 session->state != ISCSI_STATE_FAILED) {
1245 /*
1246 * to handle the race between when we set the recovery state
1247 * and block the session we requeue here (commands could
1248 * be entering our queuecommand while a block is starting
1249 * up because the block code is not locked)
1250 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001251 switch (session->state) {
1252 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001253 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christied6d13ee2008-08-17 15:24:43 -05001254 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001255 case ISCSI_STATE_LOGGING_OUT:
1256 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christied6d13ee2008-08-17 15:24:43 -05001257 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001258 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001259 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie56d7fcf2008-08-19 18:45:26 -05001260 sc->result = DID_TRANSPORT_FAILFAST << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001261 break;
1262 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001263 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001264 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001265 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001266 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001267 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001268 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001269 }
Mike Christie7996a772006-04-06 21:13:41 -05001270 goto fault;
1271 }
1272
Mike Christie7996a772006-04-06 21:13:41 -05001273 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001274 if (!conn) {
1275 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001276 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001277 goto fault;
1278 }
Mike Christie7996a772006-04-06 21:13:41 -05001279
Mike Christie77a23c22007-05-30 12:57:18 -05001280 if (iscsi_check_cmdsn_window_closed(conn)) {
1281 reason = FAILURE_WINDOW_CLOSED;
1282 goto reject;
1283 }
1284
Mike Christie9c19a7d2008-05-21 15:54:09 -05001285 if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
Mike Christie60ecebf2006-08-31 18:09:25 -04001286 sizeof(void*))) {
1287 reason = FAILURE_OOM;
1288 goto reject;
1289 }
Mike Christie7996a772006-04-06 21:13:41 -05001290 sc->SCp.phase = session->age;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001291 sc->SCp.ptr = (char *)task;
Mike Christie7996a772006-04-06 21:13:41 -05001292
Mike Christie9c19a7d2008-05-21 15:54:09 -05001293 atomic_set(&task->refcount, 1);
1294 task->state = ISCSI_TASK_PENDING;
1295 task->conn = conn;
1296 task->sc = sc;
1297 INIT_LIST_HEAD(&task->running);
1298 list_add_tail(&task->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001299
Mike Christie052d0142008-05-21 15:54:05 -05001300 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05001301 if (iscsi_prep_scsi_cmd_pdu(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001302 sc->result = DID_ABORT << 16;
1303 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001304 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001305 goto fault;
1306 }
Mike Christie9c19a7d2008-05-21 15:54:09 -05001307 if (session->tt->xmit_task(task)) {
Mike Christie052d0142008-05-21 15:54:05 -05001308 sc->scsi_done = NULL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001309 iscsi_complete_command(task);
Mike Christie052d0142008-05-21 15:54:05 -05001310 reason = FAILURE_SESSION_NOT_READY;
1311 goto reject;
1312 }
1313 } else
1314 scsi_queue_work(session->host, &conn->xmitwork);
1315
1316 session->queued_cmdsn++;
1317 spin_unlock(&session->lock);
Mike Christie1040c992007-12-13 12:43:34 -06001318 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001319 return 0;
1320
1321reject:
1322 spin_unlock(&session->lock);
1323 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001324 spin_lock(host->host_lock);
Mike Christied6d13ee2008-08-17 15:24:43 -05001325 return SCSI_MLQUEUE_TARGET_BUSY;
Mike Christie7996a772006-04-06 21:13:41 -05001326
1327fault:
1328 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001329 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
Boaz Harroshc07d4442008-04-18 10:11:52 -05001330 if (!scsi_bidi_cmnd(sc))
1331 scsi_set_resid(sc, scsi_bufflen(sc));
1332 else {
1333 scsi_out(sc)->resid = scsi_out(sc)->length;
1334 scsi_in(sc)->resid = scsi_in(sc)->length;
1335 }
Mike Christie052d0142008-05-21 15:54:05 -05001336 done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001337 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001338 return 0;
1339}
1340EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1341
1342int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1343{
1344 if (depth > ISCSI_MAX_CMD_PER_LUN)
1345 depth = ISCSI_MAX_CMD_PER_LUN;
1346 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1347 return sdev->queue_depth;
1348}
1349EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1350
Mike Christie7996a772006-04-06 21:13:41 -05001351void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1352{
Mike Christie75613522008-05-21 15:53:59 -05001353 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001354
1355 spin_lock_bh(&session->lock);
1356 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001357 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001358 if (session->leadconn)
1359 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001360 }
1361 spin_unlock_bh(&session->lock);
1362}
1363EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1364
Mike Christie8e124522008-09-24 11:46:12 -05001365int iscsi_eh_target_reset(struct scsi_cmnd *sc)
Mike Christie7996a772006-04-06 21:13:41 -05001366{
Mike Christie75613522008-05-21 15:53:59 -05001367 struct iscsi_cls_session *cls_session;
1368 struct iscsi_session *session;
1369 struct iscsi_conn *conn;
1370
1371 cls_session = starget_to_session(scsi_target(sc->device));
1372 session = cls_session->dd_data;
1373 conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001374
Mike Christiebc436b22007-12-13 12:43:28 -06001375 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001376 spin_lock_bh(&session->lock);
1377 if (session->state == ISCSI_STATE_TERMINATE) {
1378failed:
Mike Christie8e124522008-09-24 11:46:12 -05001379 debug_scsi("failing target reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001380 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001381 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001382 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001383 return FAILED;
1384 }
1385
Mike Christie7996a772006-04-06 21:13:41 -05001386 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001387 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001388 /*
1389 * we drop the lock here but the leadconn cannot be destoyed while
1390 * we are in the scsi eh
1391 */
Mike Christie843c0a82007-12-13 12:43:20 -06001392 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001393
Mike Christie8e124522008-09-24 11:46:12 -05001394 debug_scsi("iscsi_eh_target_reset wait for relogin\n");
Mike Christie7996a772006-04-06 21:13:41 -05001395 wait_event_interruptible(conn->ehwait,
1396 session->state == ISCSI_STATE_TERMINATE ||
1397 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001398 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001399 if (signal_pending(current))
1400 flush_signals(current);
1401
Mike Christiebc436b22007-12-13 12:43:28 -06001402 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001403 spin_lock_bh(&session->lock);
1404 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001405 iscsi_session_printk(KERN_INFO, session,
Mike Christie8e124522008-09-24 11:46:12 -05001406 "target reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001407 else
1408 goto failed;
1409 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001410 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001411 return SUCCESS;
1412}
Mike Christie8e124522008-09-24 11:46:12 -05001413EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
Mike Christie7996a772006-04-06 21:13:41 -05001414
Mike Christie843c0a82007-12-13 12:43:20 -06001415static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001416{
Mike Christie843c0a82007-12-13 12:43:20 -06001417 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001418 struct iscsi_session *session = conn->session;
1419
1420 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001421 if (conn->tmf_state == TMF_QUEUED) {
1422 conn->tmf_state = TMF_TIMEDOUT;
1423 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001424 /* unblock eh_abort() */
1425 wake_up(&conn->ehwait);
1426 }
1427 spin_unlock(&session->lock);
1428}
1429
Mike Christie9c19a7d2008-05-21 15:54:09 -05001430static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001431 struct iscsi_tm *hdr, int age,
1432 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001433{
Mike Christie7996a772006-04-06 21:13:41 -05001434 struct iscsi_session *session = conn->session;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001435 struct iscsi_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001436
Mike Christie9c19a7d2008-05-21 15:54:09 -05001437 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
Mike Christie843c0a82007-12-13 12:43:20 -06001438 NULL, 0);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001439 if (!task) {
Mike Christie6724add2007-08-15 01:38:30 -05001440 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001441 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001442 spin_lock_bh(&session->lock);
1443 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001444 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001445 }
Mike Christie843c0a82007-12-13 12:43:20 -06001446 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001447 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001448 conn->tmf_timer.function = iscsi_tmf_timedout;
1449 conn->tmf_timer.data = (unsigned long)conn;
1450 add_timer(&conn->tmf_timer);
1451 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001452
Mike Christie7996a772006-04-06 21:13:41 -05001453 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001454 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001455
1456 /*
1457 * block eh thread until:
1458 *
Mike Christie843c0a82007-12-13 12:43:20 -06001459 * 1) tmf response
1460 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001461 * 3) session is terminated or restarted or userspace has
1462 * given up on recovery
1463 */
Mike Christie843c0a82007-12-13 12:43:20 -06001464 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001465 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001466 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001467 if (signal_pending(current))
1468 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001469 del_timer_sync(&conn->tmf_timer);
1470
Mike Christie6724add2007-08-15 01:38:30 -05001471 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001472 spin_lock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001473 /* if the session drops it will clean up the task */
Mike Christie843c0a82007-12-13 12:43:20 -06001474 if (age != session->age ||
1475 session->state != ISCSI_STATE_LOGGED_IN)
1476 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001477 return 0;
1478}
1479
1480/*
Mike Christie843c0a82007-12-13 12:43:20 -06001481 * Fail commands. session lock held and recv side suspended and xmit
1482 * thread flushed
1483 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001484static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1485 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001486{
Mike Christie9c19a7d2008-05-21 15:54:09 -05001487 struct iscsi_task *task, *tmp;
Mike Christie843c0a82007-12-13 12:43:20 -06001488
Mike Christie9c19a7d2008-05-21 15:54:09 -05001489 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1490 conn->task = NULL;
Mike Christie843c0a82007-12-13 12:43:20 -06001491
1492 /* flush pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001493 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1494 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001495 debug_scsi("failing pending sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001496 task->sc, task->itt);
1497 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001498 }
1499 }
1500
Mike Christie9c19a7d2008-05-21 15:54:09 -05001501 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1502 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001503 debug_scsi("failing requeued sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001504 task->sc, task->itt);
1505 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001506 }
1507 }
1508
1509 /* fail all other running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001510 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1511 if (lun == task->sc->device->lun || lun == -1) {
Mike Christie843c0a82007-12-13 12:43:20 -06001512 debug_scsi("failing in progress sc %p itt 0x%x\n",
Mike Christie9c19a7d2008-05-21 15:54:09 -05001513 task->sc, task->itt);
Mike Christieac26d412008-09-06 08:39:15 -05001514 fail_command(conn, task, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001515 }
1516 }
1517}
1518
Mike Christieb40977d2008-05-21 15:54:03 -05001519void iscsi_suspend_tx(struct iscsi_conn *conn)
Mike Christie6724add2007-08-15 01:38:30 -05001520{
1521 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001522 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1523 scsi_flush_work(conn->session->host);
Mike Christie6724add2007-08-15 01:38:30 -05001524}
Mike Christieb40977d2008-05-21 15:54:03 -05001525EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
Mike Christie6724add2007-08-15 01:38:30 -05001526
1527static void iscsi_start_tx(struct iscsi_conn *conn)
1528{
1529 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie052d0142008-05-21 15:54:05 -05001530 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1531 scsi_queue_work(conn->session->host, &conn->xmitwork);
Mike Christie6724add2007-08-15 01:38:30 -05001532}
1533
Jens Axboe242f9dc2008-09-14 05:55:09 -07001534static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
Mike Christief6d51802007-12-13 12:43:30 -06001535{
1536 struct iscsi_cls_session *cls_session;
1537 struct iscsi_session *session;
1538 struct iscsi_conn *conn;
Jens Axboe242f9dc2008-09-14 05:55:09 -07001539 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
Mike Christief6d51802007-12-13 12:43:30 -06001540
1541 cls_session = starget_to_session(scsi_target(scmd->device));
Mike Christie75613522008-05-21 15:53:59 -05001542 session = cls_session->dd_data;
Mike Christief6d51802007-12-13 12:43:30 -06001543
1544 debug_scsi("scsi cmd %p timedout\n", scmd);
1545
1546 spin_lock(&session->lock);
1547 if (session->state != ISCSI_STATE_LOGGED_IN) {
1548 /*
1549 * We are probably in the middle of iscsi recovery so let
1550 * that complete and handle the error.
1551 */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001552 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001553 goto done;
1554 }
1555
1556 conn = session->leadconn;
1557 if (!conn) {
1558 /* In the middle of shuting down */
Jens Axboe242f9dc2008-09-14 05:55:09 -07001559 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001560 goto done;
1561 }
1562
1563 if (!conn->recv_timeout && !conn->ping_timeout)
1564 goto done;
1565 /*
1566 * if the ping timedout then we are in the middle of cleaning up
1567 * and can let the iscsi eh handle it
1568 */
1569 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1570 (conn->ping_timeout * HZ), jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001571 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001572 /*
1573 * if we are about to check the transport then give the command
1574 * more time
1575 */
1576 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1577 jiffies))
Jens Axboe242f9dc2008-09-14 05:55:09 -07001578 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001579 /* if in the middle of checking the transport then give us more time */
Mike Christie9c19a7d2008-05-21 15:54:09 -05001580 if (conn->ping_task)
Jens Axboe242f9dc2008-09-14 05:55:09 -07001581 rc = BLK_EH_RESET_TIMER;
Mike Christief6d51802007-12-13 12:43:30 -06001582done:
1583 spin_unlock(&session->lock);
Jens Axboe242f9dc2008-09-14 05:55:09 -07001584 debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
1585 "timer reset" : "nh");
Mike Christief6d51802007-12-13 12:43:30 -06001586 return rc;
1587}
1588
1589static void iscsi_check_transport_timeouts(unsigned long data)
1590{
1591 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1592 struct iscsi_session *session = conn->session;
Mike Christie4cf10432008-05-07 20:43:52 -05001593 unsigned long recv_timeout, next_timeout = 0, last_recv;
Mike Christief6d51802007-12-13 12:43:30 -06001594
1595 spin_lock(&session->lock);
1596 if (session->state != ISCSI_STATE_LOGGED_IN)
1597 goto done;
1598
Mike Christie4cf10432008-05-07 20:43:52 -05001599 recv_timeout = conn->recv_timeout;
1600 if (!recv_timeout)
Mike Christief6d51802007-12-13 12:43:30 -06001601 goto done;
1602
Mike Christie4cf10432008-05-07 20:43:52 -05001603 recv_timeout *= HZ;
Mike Christief6d51802007-12-13 12:43:30 -06001604 last_recv = conn->last_recv;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001605 if (conn->ping_task &&
Mike Christie4cf10432008-05-07 20:43:52 -05001606 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
Mike Christief6d51802007-12-13 12:43:30 -06001607 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001608 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1609 "expired, last rx %lu, last ping %lu, "
1610 "now %lu\n", conn->ping_timeout, last_recv,
1611 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001612 spin_unlock(&session->lock);
1613 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1614 return;
1615 }
1616
Mike Christie4cf10432008-05-07 20:43:52 -05001617 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
Mike Christiec8611f92008-05-08 20:15:34 -05001618 /* send a ping to try to provoke some traffic */
1619 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1620 iscsi_send_nopout(conn, NULL);
Mike Christie4cf10432008-05-07 20:43:52 -05001621 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001622 } else
Mike Christie4cf10432008-05-07 20:43:52 -05001623 next_timeout = last_recv + recv_timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001624
Mike Christiead294e92008-01-31 13:36:50 -06001625 debug_scsi("Setting next tmo %lu\n", next_timeout);
1626 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001627done:
1628 spin_unlock(&session->lock);
1629}
1630
Mike Christie9c19a7d2008-05-21 15:54:09 -05001631static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
Mike Christie843c0a82007-12-13 12:43:20 -06001632 struct iscsi_tm *hdr)
1633{
1634 memset(hdr, 0, sizeof(*hdr));
1635 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1636 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1637 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001638 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1639 hdr->rtt = task->hdr->itt;
1640 hdr->refcmdsn = task->hdr->cmdsn;
Mike Christie843c0a82007-12-13 12:43:20 -06001641}
1642
Mike Christie7996a772006-04-06 21:13:41 -05001643int iscsi_eh_abort(struct scsi_cmnd *sc)
1644{
Mike Christie75613522008-05-21 15:53:59 -05001645 struct iscsi_cls_session *cls_session;
1646 struct iscsi_session *session;
Mike Christief47f2cf2006-08-31 18:09:33 -04001647 struct iscsi_conn *conn;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001648 struct iscsi_task *task;
Mike Christie843c0a82007-12-13 12:43:20 -06001649 struct iscsi_tm *hdr;
1650 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001651
Mike Christie75613522008-05-21 15:53:59 -05001652 cls_session = starget_to_session(scsi_target(sc->device));
1653 session = cls_session->dd_data;
1654
Mike Christie6724add2007-08-15 01:38:30 -05001655 mutex_lock(&session->eh_mutex);
1656 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001657 /*
1658 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1659 * got the command.
1660 */
1661 if (!sc->SCp.ptr) {
1662 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001663 spin_unlock_bh(&session->lock);
1664 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001665 return SUCCESS;
1666 }
1667
Mike Christie7996a772006-04-06 21:13:41 -05001668 /*
1669 * If we are not logged in or we have started a new session
1670 * then let the host reset code handle this
1671 */
Mike Christie843c0a82007-12-13 12:43:20 -06001672 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1673 sc->SCp.phase != session->age) {
1674 spin_unlock_bh(&session->lock);
1675 mutex_unlock(&session->eh_mutex);
1676 return FAILED;
1677 }
1678
1679 conn = session->leadconn;
1680 conn->eh_abort_cnt++;
1681 age = session->age;
1682
Mike Christie9c19a7d2008-05-21 15:54:09 -05001683 task = (struct iscsi_task *)sc->SCp.ptr;
1684 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001685
Mike Christie9c19a7d2008-05-21 15:54:09 -05001686 /* task completed before time out */
1687 if (!task->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001688 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001689 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001690 }
Mike Christie7996a772006-04-06 21:13:41 -05001691
Mike Christie9c19a7d2008-05-21 15:54:09 -05001692 if (task->state == ISCSI_TASK_PENDING) {
1693 fail_command(conn, task, DID_ABORT << 16);
Mike Christie77a23c22007-05-30 12:57:18 -05001694 goto success;
1695 }
Mike Christie7996a772006-04-06 21:13:41 -05001696
Mike Christie843c0a82007-12-13 12:43:20 -06001697 /* only have one tmf outstanding at a time */
1698 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001699 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001700 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001701
Mike Christie843c0a82007-12-13 12:43:20 -06001702 hdr = &conn->tmhdr;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001703 iscsi_prep_abort_task_pdu(task, hdr);
Mike Christie843c0a82007-12-13 12:43:20 -06001704
Mike Christie9c19a7d2008-05-21 15:54:09 -05001705 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001706 rc = FAILED;
1707 goto failed;
1708 }
1709
1710 switch (conn->tmf_state) {
1711 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001712 spin_unlock_bh(&session->lock);
Mike Christie913e5bf2008-05-21 15:54:18 -05001713 /*
1714 * stop tx side incase the target had sent a abort rsp but
1715 * the initiator was still writing out data.
1716 */
Mike Christie6724add2007-08-15 01:38:30 -05001717 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001718 /*
Mike Christie913e5bf2008-05-21 15:54:18 -05001719 * we do not stop the recv side because targets have been
1720 * good and have never sent us a successful tmf response
1721 * then sent more data for the cmd.
Mike Christie77a23c22007-05-30 12:57:18 -05001722 */
Mike Christie77a23c22007-05-30 12:57:18 -05001723 spin_lock(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05001724 fail_command(conn, task, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001725 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001726 spin_unlock(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001727 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001728 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001729 case TMF_TIMEDOUT:
1730 spin_unlock_bh(&session->lock);
1731 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1732 goto failed_unlocked;
1733 case TMF_NOT_FOUND:
1734 if (!sc->SCp.ptr) {
1735 conn->tmf_state = TMF_INITIAL;
Mike Christie9c19a7d2008-05-21 15:54:09 -05001736 /* task completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001737 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001738 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001739 }
1740 /* fall through */
1741 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001742 conn->tmf_state = TMF_INITIAL;
1743 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001744 }
1745
Mike Christie77a23c22007-05-30 12:57:18 -05001746success:
Mike Christie7996a772006-04-06 21:13:41 -05001747 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001748success_unlocked:
Mike Christie9c19a7d2008-05-21 15:54:09 -05001749 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001750 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001751 return SUCCESS;
1752
1753failed:
1754 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001755failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001756 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
Mike Christie9c19a7d2008-05-21 15:54:09 -05001757 task ? task->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001758 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001759 return FAILED;
1760}
1761EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1762
Mike Christie843c0a82007-12-13 12:43:20 -06001763static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1764{
1765 memset(hdr, 0, sizeof(*hdr));
1766 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1767 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1768 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1769 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001770 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001771}
1772
1773int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1774{
Mike Christie75613522008-05-21 15:53:59 -05001775 struct iscsi_cls_session *cls_session;
1776 struct iscsi_session *session;
Mike Christie843c0a82007-12-13 12:43:20 -06001777 struct iscsi_conn *conn;
1778 struct iscsi_tm *hdr;
1779 int rc = FAILED;
1780
Mike Christie75613522008-05-21 15:53:59 -05001781 cls_session = starget_to_session(scsi_target(sc->device));
1782 session = cls_session->dd_data;
1783
Mike Christie843c0a82007-12-13 12:43:20 -06001784 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1785
1786 mutex_lock(&session->eh_mutex);
1787 spin_lock_bh(&session->lock);
1788 /*
1789 * Just check if we are not logged in. We cannot check for
1790 * the phase because the reset could come from a ioctl.
1791 */
1792 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1793 goto unlock;
1794 conn = session->leadconn;
1795
1796 /* only have one tmf outstanding at a time */
1797 if (conn->tmf_state != TMF_INITIAL)
1798 goto unlock;
1799 conn->tmf_state = TMF_QUEUED;
1800
1801 hdr = &conn->tmhdr;
1802 iscsi_prep_lun_reset_pdu(sc, hdr);
1803
Mike Christie9c19a7d2008-05-21 15:54:09 -05001804 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
Mike Christief6d51802007-12-13 12:43:30 -06001805 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001806 rc = FAILED;
1807 goto unlock;
1808 }
1809
1810 switch (conn->tmf_state) {
1811 case TMF_SUCCESS:
1812 break;
1813 case TMF_TIMEDOUT:
1814 spin_unlock_bh(&session->lock);
1815 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1816 goto done;
1817 default:
1818 conn->tmf_state = TMF_INITIAL;
1819 goto unlock;
1820 }
1821
1822 rc = SUCCESS;
1823 spin_unlock_bh(&session->lock);
1824
1825 iscsi_suspend_tx(conn);
Mike Christie913e5bf2008-05-21 15:54:18 -05001826
Mike Christiea3439142008-09-24 11:46:15 -05001827 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001828 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001829 conn->tmf_state = TMF_INITIAL;
Mike Christiea3439142008-09-24 11:46:15 -05001830 spin_unlock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001831
1832 iscsi_start_tx(conn);
1833 goto done;
1834
1835unlock:
1836 spin_unlock_bh(&session->lock);
1837done:
1838 debug_scsi("iscsi_eh_device_reset %s\n",
1839 rc == SUCCESS ? "SUCCESS" : "FAILED");
1840 mutex_unlock(&session->eh_mutex);
1841 return rc;
1842}
1843EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1844
Olaf Kirch63203772007-12-13 12:43:25 -06001845/*
1846 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1847 * should be accessed via kfifo_{get,put} on q->queue.
1848 * Optionally, the caller can obtain the array of object pointers
1849 * by passing in a non-NULL @items pointer
1850 */
Mike Christie7996a772006-04-06 21:13:41 -05001851int
Olaf Kirch63203772007-12-13 12:43:25 -06001852iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001853{
Olaf Kirch63203772007-12-13 12:43:25 -06001854 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001855
Olaf Kirch63203772007-12-13 12:43:25 -06001856 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001857
1858 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001859
1860 /* If the user passed an items pointer, he wants a copy of
1861 * the array. */
1862 if (items)
1863 num_arrays++;
1864 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1865 if (q->pool == NULL)
1866 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001867
1868 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1869 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001870 if (q->queue == ERR_PTR(-ENOMEM))
1871 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001872
1873 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001874 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001875 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001876 q->max = i;
1877 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001878 }
Mike Christie7996a772006-04-06 21:13:41 -05001879 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1880 }
Olaf Kirch63203772007-12-13 12:43:25 -06001881
1882 if (items) {
1883 *items = q->pool + max;
1884 memcpy(*items, q->pool, max * sizeof(void *));
1885 }
1886
Mike Christie7996a772006-04-06 21:13:41 -05001887 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001888
1889enomem:
1890 iscsi_pool_free(q);
1891 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001892}
1893EXPORT_SYMBOL_GPL(iscsi_pool_init);
1894
Olaf Kirch63203772007-12-13 12:43:25 -06001895void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001896{
1897 int i;
1898
1899 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001900 kfree(q->pool[i]);
1901 if (q->pool)
1902 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001903}
1904EXPORT_SYMBOL_GPL(iscsi_pool_free);
1905
Mike Christiea4804cd2008-05-21 15:54:00 -05001906/**
1907 * iscsi_host_add - add host to system
1908 * @shost: scsi host
1909 * @pdev: parent device
1910 *
1911 * This should be called by partial offload and software iscsi drivers
1912 * to add a host to the system.
1913 */
1914int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
Mike Christie7996a772006-04-06 21:13:41 -05001915{
Mike Christie8e9a20c2008-06-16 10:11:33 -05001916 if (!shost->can_queue)
1917 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
1918
Mike Christiea4804cd2008-05-21 15:54:00 -05001919 return scsi_add_host(shost, pdev);
1920}
1921EXPORT_SYMBOL_GPL(iscsi_host_add);
1922
1923/**
1924 * iscsi_host_alloc - allocate a host and driver data
1925 * @sht: scsi host template
1926 * @dd_data_size: driver host data size
1927 * @qdepth: default device queue depth
1928 *
1929 * This should be called by partial offload and software iscsi drivers.
1930 * To access the driver specific memory use the iscsi_host_priv() macro.
1931 */
1932struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1933 int dd_data_size, uint16_t qdepth)
1934{
1935 struct Scsi_Host *shost;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001936 struct iscsi_host *ihost;
Mike Christiea4804cd2008-05-21 15:54:00 -05001937
1938 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1939 if (!shost)
1940 return NULL;
1941 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1942
Mike Christie15482712007-05-30 12:57:19 -05001943 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1944 if (qdepth != 0)
1945 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
Mike Christie75613522008-05-21 15:53:59 -05001946 "Queue depth must be between 1 and %d.\n",
1947 qdepth, ISCSI_MAX_CMD_PER_LUN);
Mike Christie15482712007-05-30 12:57:19 -05001948 qdepth = ISCSI_DEF_CMD_PER_LUN;
1949 }
Mike Christie75613522008-05-21 15:53:59 -05001950 shost->cmd_per_lun = qdepth;
Mike Christiee5bd7b52008-09-24 11:46:10 -05001951
1952 ihost = shost_priv(shost);
1953 spin_lock_init(&ihost->lock);
1954 ihost->state = ISCSI_HOST_SETUP;
1955 ihost->num_sessions = 0;
1956 init_waitqueue_head(&ihost->session_removal_wq);
Mike Christiea4804cd2008-05-21 15:54:00 -05001957 return shost;
Mike Christie75613522008-05-21 15:53:59 -05001958}
Mike Christiea4804cd2008-05-21 15:54:00 -05001959EXPORT_SYMBOL_GPL(iscsi_host_alloc);
Mike Christie75613522008-05-21 15:53:59 -05001960
Mike Christiee5bd7b52008-09-24 11:46:10 -05001961static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
1962{
1963 iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
1964}
1965
Mike Christiea4804cd2008-05-21 15:54:00 -05001966/**
1967 * iscsi_host_remove - remove host and sessions
1968 * @shost: scsi host
1969 *
Mike Christiee5bd7b52008-09-24 11:46:10 -05001970 * If there are any sessions left, this will initiate the removal and wait
1971 * for the completion.
Mike Christiea4804cd2008-05-21 15:54:00 -05001972 */
1973void iscsi_host_remove(struct Scsi_Host *shost)
1974{
Mike Christiee5bd7b52008-09-24 11:46:10 -05001975 struct iscsi_host *ihost = shost_priv(shost);
1976 unsigned long flags;
1977
1978 spin_lock_irqsave(&ihost->lock, flags);
1979 ihost->state = ISCSI_HOST_REMOVED;
1980 spin_unlock_irqrestore(&ihost->lock, flags);
1981
1982 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
1983 wait_event_interruptible(ihost->session_removal_wq,
1984 ihost->num_sessions == 0);
1985 if (signal_pending(current))
1986 flush_signals(current);
1987
Mike Christiea4804cd2008-05-21 15:54:00 -05001988 scsi_remove_host(shost);
1989}
1990EXPORT_SYMBOL_GPL(iscsi_host_remove);
1991
1992void iscsi_host_free(struct Scsi_Host *shost)
Mike Christie75613522008-05-21 15:53:59 -05001993{
1994 struct iscsi_host *ihost = shost_priv(shost);
1995
1996 kfree(ihost->netdev);
1997 kfree(ihost->hwaddress);
1998 kfree(ihost->initiatorname);
Mike Christiea4804cd2008-05-21 15:54:00 -05001999 scsi_host_put(shost);
Mike Christie75613522008-05-21 15:53:59 -05002000}
Mike Christiea4804cd2008-05-21 15:54:00 -05002001EXPORT_SYMBOL_GPL(iscsi_host_free);
Mike Christie75613522008-05-21 15:53:59 -05002002
Mike Christiee5bd7b52008-09-24 11:46:10 -05002003static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2004{
2005 struct iscsi_host *ihost = shost_priv(shost);
2006 unsigned long flags;
2007
2008 shost = scsi_host_get(shost);
2009 if (!shost) {
2010 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2011 "of session teardown event because host already "
2012 "removed.\n");
2013 return;
2014 }
2015
2016 spin_lock_irqsave(&ihost->lock, flags);
2017 ihost->num_sessions--;
2018 if (ihost->num_sessions == 0)
2019 wake_up(&ihost->session_removal_wq);
2020 spin_unlock_irqrestore(&ihost->lock, flags);
2021 scsi_host_put(shost);
2022}
2023
Mike Christie75613522008-05-21 15:53:59 -05002024/**
2025 * iscsi_session_setup - create iscsi cls session and host and session
2026 * @iscsit: iscsi transport template
2027 * @shost: scsi host
2028 * @cmds_max: session can queue
Mike Christie9c19a7d2008-05-21 15:54:09 -05002029 * @cmd_task_size: LLD task private data size
Mike Christie75613522008-05-21 15:53:59 -05002030 * @initial_cmdsn: initial CmdSN
2031 *
2032 * This can be used by software iscsi_transports that allocate
2033 * a session per scsi host.
Mike Christie3cf7b232008-05-21 15:54:17 -05002034 *
2035 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2036 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2037 * for nop handling and login/logout requests.
Mike Christie75613522008-05-21 15:53:59 -05002038 */
2039struct iscsi_cls_session *
2040iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
Mike Christie3cf7b232008-05-21 15:54:17 -05002041 uint16_t cmds_max, int cmd_task_size,
Mike Christie79706342008-05-21 15:54:12 -05002042 uint32_t initial_cmdsn, unsigned int id)
Mike Christie75613522008-05-21 15:53:59 -05002043{
Mike Christiee5bd7b52008-09-24 11:46:10 -05002044 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie75613522008-05-21 15:53:59 -05002045 struct iscsi_session *session;
2046 struct iscsi_cls_session *cls_session;
Mike Christie3cf7b232008-05-21 15:54:17 -05002047 int cmd_i, scsi_cmds, total_cmds = cmds_max;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002048 unsigned long flags;
2049
2050 spin_lock_irqsave(&ihost->lock, flags);
2051 if (ihost->state == ISCSI_HOST_REMOVED) {
2052 spin_unlock_irqrestore(&ihost->lock, flags);
2053 return NULL;
2054 }
2055 ihost->num_sessions++;
2056 spin_unlock_irqrestore(&ihost->lock, flags);
Mike Christie8e9a20c2008-06-16 10:11:33 -05002057
2058 if (!total_cmds)
2059 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
Mike Christie3e5c28a2008-05-21 15:54:06 -05002060 /*
Mike Christie3cf7b232008-05-21 15:54:17 -05002061 * The iscsi layer needs some tasks for nop handling and tmfs,
2062 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2063 * + 1 command for scsi IO.
Mike Christie3e5c28a2008-05-21 15:54:06 -05002064 */
Mike Christie3cf7b232008-05-21 15:54:17 -05002065 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2066 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2067 "must be a power of two that is at least %d.\n",
2068 total_cmds, ISCSI_TOTAL_CMDS_MIN);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002069 goto dec_session_count;
Mike Christie15482712007-05-30 12:57:19 -05002070 }
Mike Christie3cf7b232008-05-21 15:54:17 -05002071
2072 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2073 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2074 "must be a power of 2 less than or equal to %d.\n",
2075 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2076 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2077 }
2078
2079 if (!is_power_of_2(total_cmds)) {
2080 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2081 "must be a power of 2.\n", total_cmds);
2082 total_cmds = rounddown_pow_of_two(total_cmds);
2083 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2084 return NULL;
2085 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2086 total_cmds);
2087 }
2088 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05002089
Mike Christie5d91e202008-05-21 15:54:01 -05002090 cls_session = iscsi_alloc_session(shost, iscsit,
2091 sizeof(struct iscsi_session));
Mike Christie75613522008-05-21 15:53:59 -05002092 if (!cls_session)
Mike Christiee5bd7b52008-09-24 11:46:10 -05002093 goto dec_session_count;
Mike Christie75613522008-05-21 15:53:59 -05002094 session = cls_session->dd_data;
2095 session->cls_session = cls_session;
Mike Christie7996a772006-04-06 21:13:41 -05002096 session->host = shost;
2097 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06002098 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06002099 session->lu_reset_timeout = 15;
2100 session->abort_timeout = 10;
Mike Christie3cf7b232008-05-21 15:54:17 -05002101 session->scsi_cmds_max = scsi_cmds;
2102 session->cmds_max = total_cmds;
Mike Christiee0726402007-07-26 12:46:48 -05002103 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002104 session->exp_cmdsn = initial_cmdsn + 1;
2105 session->max_cmdsn = initial_cmdsn + 1;
2106 session->max_r2t = 1;
2107 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05002108 mutex_init(&session->eh_mutex);
Mike Christie75613522008-05-21 15:53:59 -05002109 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002110
2111 /* initialize SCSI PDU commands pool */
2112 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2113 (void***)&session->cmds,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002114 cmd_task_size + sizeof(struct iscsi_task)))
Mike Christie7996a772006-04-06 21:13:41 -05002115 goto cmdpool_alloc_fail;
2116
2117 /* pre-format cmds pool with ITT */
2118 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
Mike Christie9c19a7d2008-05-21 15:54:09 -05002119 struct iscsi_task *task = session->cmds[cmd_i];
Mike Christie7996a772006-04-06 21:13:41 -05002120
Mike Christie9c19a7d2008-05-21 15:54:09 -05002121 if (cmd_task_size)
2122 task->dd_data = &task[1];
2123 task->itt = cmd_i;
2124 INIT_LIST_HEAD(&task->running);
Mike Christie7996a772006-04-06 21:13:41 -05002125 }
2126
Mike Christief53a88d2006-06-28 12:00:27 -05002127 if (!try_module_get(iscsit->owner))
Mike Christie75613522008-05-21 15:53:59 -05002128 goto module_get_fail;
2129
Mike Christie79706342008-05-21 15:54:12 -05002130 if (iscsi_add_session(cls_session, id))
Mike Christief53a88d2006-06-28 12:00:27 -05002131 goto cls_session_fail;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002132
Mike Christie7996a772006-04-06 21:13:41 -05002133 return cls_session;
2134
2135cls_session_fail:
Mike Christie75613522008-05-21 15:53:59 -05002136 module_put(iscsit->owner);
2137module_get_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06002138 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002139cmdpool_alloc_fail:
Mike Christie75613522008-05-21 15:53:59 -05002140 iscsi_free_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002141dec_session_count:
2142 iscsi_host_dec_session_cnt(shost);
Mike Christie7996a772006-04-06 21:13:41 -05002143 return NULL;
2144}
2145EXPORT_SYMBOL_GPL(iscsi_session_setup);
2146
2147/**
2148 * iscsi_session_teardown - destroy session, host, and cls_session
Mike Christie75613522008-05-21 15:53:59 -05002149 * @cls_session: iscsi session
Mike Christie7996a772006-04-06 21:13:41 -05002150 *
Mike Christie75613522008-05-21 15:53:59 -05002151 * The driver must have called iscsi_remove_session before
2152 * calling this.
2153 */
Mike Christie7996a772006-04-06 21:13:41 -05002154void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2155{
Mike Christie75613522008-05-21 15:53:59 -05002156 struct iscsi_session *session = cls_session->dd_data;
Mike Christie63f75cc2006-07-24 15:47:29 -05002157 struct module *owner = cls_session->transport->owner;
Mike Christiee5bd7b52008-09-24 11:46:10 -05002158 struct Scsi_Host *shost = session->host;
Mike Christie7996a772006-04-06 21:13:41 -05002159
Olaf Kirch63203772007-12-13 12:43:25 -06002160 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05002161
Mike Christieb2c64162007-05-30 12:57:16 -05002162 kfree(session->password);
2163 kfree(session->password_in);
2164 kfree(session->username);
2165 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05002166 kfree(session->targetname);
Mike Christie88dfd342008-05-21 15:54:16 -05002167 kfree(session->initiatorname);
2168 kfree(session->ifacename);
Mike Christief3ff0c32006-07-24 15:47:50 -05002169
Mike Christie75613522008-05-21 15:53:59 -05002170 iscsi_destroy_session(cls_session);
Mike Christiee5bd7b52008-09-24 11:46:10 -05002171 iscsi_host_dec_session_cnt(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05002172 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05002173}
2174EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2175
2176/**
2177 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2178 * @cls_session: iscsi_cls_session
Mike Christie5d91e202008-05-21 15:54:01 -05002179 * @dd_size: private driver data size
Mike Christie7996a772006-04-06 21:13:41 -05002180 * @conn_idx: cid
Mike Christie5d91e202008-05-21 15:54:01 -05002181 */
Mike Christie7996a772006-04-06 21:13:41 -05002182struct iscsi_cls_conn *
Mike Christie5d91e202008-05-21 15:54:01 -05002183iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2184 uint32_t conn_idx)
Mike Christie7996a772006-04-06 21:13:41 -05002185{
Mike Christie75613522008-05-21 15:53:59 -05002186 struct iscsi_session *session = cls_session->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002187 struct iscsi_conn *conn;
2188 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05002189 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05002190
Mike Christie5d91e202008-05-21 15:54:01 -05002191 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2192 conn_idx);
Mike Christie7996a772006-04-06 21:13:41 -05002193 if (!cls_conn)
2194 return NULL;
2195 conn = cls_conn->dd_data;
Mike Christie5d91e202008-05-21 15:54:01 -05002196 memset(conn, 0, sizeof(*conn) + dd_size);
Mike Christie7996a772006-04-06 21:13:41 -05002197
Mike Christie5d91e202008-05-21 15:54:01 -05002198 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
Mike Christie7996a772006-04-06 21:13:41 -05002199 conn->session = session;
2200 conn->cls_conn = cls_conn;
2201 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2202 conn->id = conn_idx;
2203 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002204 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06002205
2206 init_timer(&conn->transport_timer);
2207 conn->transport_timer.data = (unsigned long)conn;
2208 conn->transport_timer.function = iscsi_check_transport_timeouts;
2209
Mike Christie7996a772006-04-06 21:13:41 -05002210 INIT_LIST_HEAD(&conn->run_list);
2211 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06002212 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05002213 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06002214 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00002215 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05002216
Mike Christie9c19a7d2008-05-21 15:54:09 -05002217 /* allocate login_task used for the login/text sequences */
Mike Christie7996a772006-04-06 21:13:41 -05002218 spin_lock_bh(&session->lock);
Mike Christie3e5c28a2008-05-21 15:54:06 -05002219 if (!__kfifo_get(session->cmdpool.queue,
Mike Christie9c19a7d2008-05-21 15:54:09 -05002220 (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002221 sizeof(void*))) {
2222 spin_unlock_bh(&session->lock);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002223 goto login_task_alloc_fail;
Mike Christie7996a772006-04-06 21:13:41 -05002224 }
2225 spin_unlock_bh(&session->lock);
2226
Mike Christiebf32ed32007-02-28 17:32:17 -06002227 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05002228 if (!data)
Mike Christie9c19a7d2008-05-21 15:54:09 -05002229 goto login_task_data_alloc_fail;
2230 conn->login_task->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05002231
Mike Christie843c0a82007-12-13 12:43:20 -06002232 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05002233 init_waitqueue_head(&conn->ehwait);
2234
2235 return cls_conn;
2236
Mike Christie9c19a7d2008-05-21 15:54:09 -05002237login_task_data_alloc_fail:
2238 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christied36ab6f2006-05-18 20:31:34 -05002239 sizeof(void*));
Mike Christie9c19a7d2008-05-21 15:54:09 -05002240login_task_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05002241 iscsi_destroy_conn(cls_conn);
2242 return NULL;
2243}
2244EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2245
2246/**
2247 * iscsi_conn_teardown - teardown iscsi connection
2248 * cls_conn: iscsi class connection
2249 *
2250 * TODO: we may need to make this into a two step process
2251 * like scsi-mls remove + put host
2252 */
2253void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2254{
2255 struct iscsi_conn *conn = cls_conn->dd_data;
2256 struct iscsi_session *session = conn->session;
2257 unsigned long flags;
2258
Mike Christief6d51802007-12-13 12:43:30 -06002259 del_timer_sync(&conn->transport_timer);
2260
Mike Christie7996a772006-04-06 21:13:41 -05002261 spin_lock_bh(&session->lock);
2262 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2263 if (session->leadconn == conn) {
2264 /*
2265 * leading connection? then give up on recovery.
2266 */
2267 session->state = ISCSI_STATE_TERMINATE;
2268 wake_up(&conn->ehwait);
2269 }
2270 spin_unlock_bh(&session->lock);
2271
Mike Christie7996a772006-04-06 21:13:41 -05002272 /*
2273 * Block until all in-progress commands for this connection
2274 * time out or fail.
2275 */
2276 for (;;) {
2277 spin_lock_irqsave(session->host->host_lock, flags);
2278 if (!session->host->host_busy) { /* OK for ERL == 0 */
2279 spin_unlock_irqrestore(session->host->host_lock, flags);
2280 break;
2281 }
2282 spin_unlock_irqrestore(session->host->host_lock, flags);
2283 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06002284 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2285 "host_busy %d host_failed %d\n",
2286 session->host->host_busy,
2287 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05002288 /*
2289 * force eh_abort() to unblock
2290 */
2291 wake_up(&conn->ehwait);
2292 }
2293
Mike Christie779ea122007-02-28 17:32:15 -06002294 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06002295 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06002296
Mike Christie7996a772006-04-06 21:13:41 -05002297 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05002298 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05002299 kfree(conn->persistent_address);
Mike Christie9c19a7d2008-05-21 15:54:09 -05002300 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
Mike Christie7996a772006-04-06 21:13:41 -05002301 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05002302 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05002303 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002304 spin_unlock_bh(&session->lock);
2305
Mike Christie7996a772006-04-06 21:13:41 -05002306 iscsi_destroy_conn(cls_conn);
2307}
2308EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2309
2310int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2311{
2312 struct iscsi_conn *conn = cls_conn->dd_data;
2313 struct iscsi_session *session = conn->session;
2314
Mike Christieffd04362006-08-31 18:09:24 -04002315 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06002316 iscsi_conn_printk(KERN_ERR, conn,
2317 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05002318 return -EPERM;
2319 }
2320
Mike Christiedb98ccd2006-08-31 18:09:31 -04002321 if ((session->imm_data_en || !session->initial_r2t_en) &&
2322 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002323 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2324 "first_burst %d max_burst %d\n",
2325 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002326 return -EINVAL;
2327 }
2328
Mike Christief6d51802007-12-13 12:43:30 -06002329 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002330 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2331 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002332 conn->recv_timeout = 5;
2333 }
2334
2335 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002336 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2337 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002338 conn->ping_timeout = 5;
2339 }
2340
Mike Christie7996a772006-04-06 21:13:41 -05002341 spin_lock_bh(&session->lock);
2342 conn->c_stage = ISCSI_CONN_STARTED;
2343 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002344 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002345
Mike Christief6d51802007-12-13 12:43:30 -06002346 conn->last_recv = jiffies;
2347 conn->last_ping = jiffies;
2348 if (conn->recv_timeout && conn->ping_timeout)
2349 mod_timer(&conn->transport_timer,
2350 jiffies + (conn->recv_timeout * HZ));
2351
Mike Christie7996a772006-04-06 21:13:41 -05002352 switch(conn->stop_stage) {
2353 case STOP_CONN_RECOVER:
2354 /*
2355 * unblock eh_abort() if it is blocked. re-try all
2356 * commands after successful recovery
2357 */
Mike Christie7996a772006-04-06 21:13:41 -05002358 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002359 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002360 session->age++;
Mike Christie8b1d0342008-01-31 13:36:53 -06002361 if (session->age == 16)
2362 session->age = 0;
Mike Christie6eabafb2008-01-31 13:36:43 -06002363 break;
Mike Christie7996a772006-04-06 21:13:41 -05002364 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002365 conn->stop_stage = 0;
2366 break;
Mike Christie7996a772006-04-06 21:13:41 -05002367 default:
2368 break;
2369 }
2370 spin_unlock_bh(&session->lock);
2371
Mike Christie75613522008-05-21 15:53:59 -05002372 iscsi_unblock_session(session->cls_session);
Mike Christie6eabafb2008-01-31 13:36:43 -06002373 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002374 return 0;
2375}
2376EXPORT_SYMBOL_GPL(iscsi_conn_start);
2377
2378static void
2379flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2380{
Mike Christie9c19a7d2008-05-21 15:54:09 -05002381 struct iscsi_task *task, *tmp;
Mike Christie7996a772006-04-06 21:13:41 -05002382
2383 /* handle pending */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002384 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2385 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2386 /* release ref from prep task */
2387 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002388 }
2389
2390 /* handle running */
Mike Christie9c19a7d2008-05-21 15:54:09 -05002391 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2392 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2393 /* release ref from prep task */
2394 __iscsi_put_task(task);
Mike Christie7996a772006-04-06 21:13:41 -05002395 }
2396
Mike Christie9c19a7d2008-05-21 15:54:09 -05002397 conn->task = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05002398}
2399
Mike Christie656cffc2006-05-18 20:31:42 -05002400static void iscsi_start_session_recovery(struct iscsi_session *session,
2401 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002402{
Mike Christieed2abc72006-05-02 19:46:40 -05002403 int old_stop_stage;
2404
Mike Christief6d51802007-12-13 12:43:30 -06002405 del_timer_sync(&conn->transport_timer);
2406
Mike Christie6724add2007-08-15 01:38:30 -05002407 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002408 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002409 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002410 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002411 mutex_unlock(&session->eh_mutex);
2412 return;
2413 }
2414
2415 /*
Mike Christieed2abc72006-05-02 19:46:40 -05002416 * When this is called for the in_login state, we only want to clean
Mike Christie9c19a7d2008-05-21 15:54:09 -05002417 * up the login task and connection. We do not need to block and set
Mike Christie67a61112006-05-30 00:37:20 -05002418 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002419 */
Mike Christie67a61112006-05-30 00:37:20 -05002420 if (flag == STOP_CONN_TERM)
2421 session->state = ISCSI_STATE_TERMINATE;
2422 else if (conn->stop_stage != STOP_CONN_RECOVER)
2423 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002424
2425 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002426 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002427 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002428 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002429
2430 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002431 /*
2432 * for connection level recovery we should not calculate
2433 * header digest. conn->hdr_size used for optimization
2434 * in hdr_extract() and will be re-negotiated at
2435 * set_param() time.
2436 */
2437 if (flag == STOP_CONN_RECOVER) {
2438 conn->hdrdgst_en = 0;
2439 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002440 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002441 old_stop_stage != STOP_CONN_RECOVER) {
2442 debug_scsi("blocking session\n");
Mike Christie75613522008-05-21 15:53:59 -05002443 iscsi_block_session(session->cls_session);
Mike Christie67a61112006-05-30 00:37:20 -05002444 }
Mike Christie7996a772006-04-06 21:13:41 -05002445 }
Mike Christie656cffc2006-05-18 20:31:42 -05002446
Mike Christie656cffc2006-05-18 20:31:42 -05002447 /*
2448 * flush queues.
2449 */
2450 spin_lock_bh(&session->lock);
Mike Christie87cd9ea2008-09-24 11:46:14 -05002451 if (flag == STOP_CONN_RECOVER)
Mike Christie56d7fcf2008-08-19 18:45:26 -05002452 fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
2453 else
2454 fail_all_commands(conn, -1, DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002455 flush_control_queues(session, conn);
2456 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002457 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002458}
Mike Christie7996a772006-04-06 21:13:41 -05002459
2460void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2461{
2462 struct iscsi_conn *conn = cls_conn->dd_data;
2463 struct iscsi_session *session = conn->session;
2464
2465 switch (flag) {
2466 case STOP_CONN_RECOVER:
2467 case STOP_CONN_TERM:
2468 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002469 break;
Mike Christie7996a772006-04-06 21:13:41 -05002470 default:
Mike Christie322d7392008-01-31 13:36:52 -06002471 iscsi_conn_printk(KERN_ERR, conn,
2472 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002473 }
2474}
2475EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2476
2477int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2478 struct iscsi_cls_conn *cls_conn, int is_leading)
2479{
Mike Christie75613522008-05-21 15:53:59 -05002480 struct iscsi_session *session = cls_session->dd_data;
Mike Christie98644042006-10-16 18:09:39 -04002481 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002482
Mike Christie7996a772006-04-06 21:13:41 -05002483 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002484 if (is_leading)
2485 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002486 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002487
2488 /*
2489 * Unblock xmitworker(), Login Phase will pass through.
2490 */
2491 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2492 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2493 return 0;
2494}
2495EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2496
Mike Christiea54a52c2006-06-28 12:00:23 -05002497
2498int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2499 enum iscsi_param param, char *buf, int buflen)
2500{
2501 struct iscsi_conn *conn = cls_conn->dd_data;
2502 struct iscsi_session *session = conn->session;
2503 uint32_t value;
2504
2505 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002506 case ISCSI_PARAM_FAST_ABORT:
2507 sscanf(buf, "%d", &session->fast_abort);
2508 break;
Mike Christief6d51802007-12-13 12:43:30 -06002509 case ISCSI_PARAM_ABORT_TMO:
2510 sscanf(buf, "%d", &session->abort_timeout);
2511 break;
2512 case ISCSI_PARAM_LU_RESET_TMO:
2513 sscanf(buf, "%d", &session->lu_reset_timeout);
2514 break;
2515 case ISCSI_PARAM_PING_TMO:
2516 sscanf(buf, "%d", &conn->ping_timeout);
2517 break;
2518 case ISCSI_PARAM_RECV_TMO:
2519 sscanf(buf, "%d", &conn->recv_timeout);
2520 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002521 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2522 sscanf(buf, "%d", &conn->max_recv_dlength);
2523 break;
2524 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2525 sscanf(buf, "%d", &conn->max_xmit_dlength);
2526 break;
2527 case ISCSI_PARAM_HDRDGST_EN:
2528 sscanf(buf, "%d", &conn->hdrdgst_en);
2529 break;
2530 case ISCSI_PARAM_DATADGST_EN:
2531 sscanf(buf, "%d", &conn->datadgst_en);
2532 break;
2533 case ISCSI_PARAM_INITIAL_R2T_EN:
2534 sscanf(buf, "%d", &session->initial_r2t_en);
2535 break;
2536 case ISCSI_PARAM_MAX_R2T:
2537 sscanf(buf, "%d", &session->max_r2t);
2538 break;
2539 case ISCSI_PARAM_IMM_DATA_EN:
2540 sscanf(buf, "%d", &session->imm_data_en);
2541 break;
2542 case ISCSI_PARAM_FIRST_BURST:
2543 sscanf(buf, "%d", &session->first_burst);
2544 break;
2545 case ISCSI_PARAM_MAX_BURST:
2546 sscanf(buf, "%d", &session->max_burst);
2547 break;
2548 case ISCSI_PARAM_PDU_INORDER_EN:
2549 sscanf(buf, "%d", &session->pdu_inorder_en);
2550 break;
2551 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2552 sscanf(buf, "%d", &session->dataseq_inorder_en);
2553 break;
2554 case ISCSI_PARAM_ERL:
2555 sscanf(buf, "%d", &session->erl);
2556 break;
2557 case ISCSI_PARAM_IFMARKER_EN:
2558 sscanf(buf, "%d", &value);
2559 BUG_ON(value);
2560 break;
2561 case ISCSI_PARAM_OFMARKER_EN:
2562 sscanf(buf, "%d", &value);
2563 BUG_ON(value);
2564 break;
2565 case ISCSI_PARAM_EXP_STATSN:
2566 sscanf(buf, "%u", &conn->exp_statsn);
2567 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002568 case ISCSI_PARAM_USERNAME:
2569 kfree(session->username);
2570 session->username = kstrdup(buf, GFP_KERNEL);
2571 if (!session->username)
2572 return -ENOMEM;
2573 break;
2574 case ISCSI_PARAM_USERNAME_IN:
2575 kfree(session->username_in);
2576 session->username_in = kstrdup(buf, GFP_KERNEL);
2577 if (!session->username_in)
2578 return -ENOMEM;
2579 break;
2580 case ISCSI_PARAM_PASSWORD:
2581 kfree(session->password);
2582 session->password = kstrdup(buf, GFP_KERNEL);
2583 if (!session->password)
2584 return -ENOMEM;
2585 break;
2586 case ISCSI_PARAM_PASSWORD_IN:
2587 kfree(session->password_in);
2588 session->password_in = kstrdup(buf, GFP_KERNEL);
2589 if (!session->password_in)
2590 return -ENOMEM;
2591 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002592 case ISCSI_PARAM_TARGET_NAME:
2593 /* this should not change between logins */
2594 if (session->targetname)
2595 break;
2596
2597 session->targetname = kstrdup(buf, GFP_KERNEL);
2598 if (!session->targetname)
2599 return -ENOMEM;
2600 break;
2601 case ISCSI_PARAM_TPGT:
2602 sscanf(buf, "%d", &session->tpgt);
2603 break;
2604 case ISCSI_PARAM_PERSISTENT_PORT:
2605 sscanf(buf, "%d", &conn->persistent_port);
2606 break;
2607 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2608 /*
2609 * this is the address returned in discovery so it should
2610 * not change between logins.
2611 */
2612 if (conn->persistent_address)
2613 break;
2614
2615 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2616 if (!conn->persistent_address)
2617 return -ENOMEM;
2618 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002619 case ISCSI_PARAM_IFACE_NAME:
2620 if (!session->ifacename)
2621 session->ifacename = kstrdup(buf, GFP_KERNEL);
2622 break;
2623 case ISCSI_PARAM_INITIATOR_NAME:
2624 if (!session->initiatorname)
2625 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2626 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002627 default:
2628 return -ENOSYS;
2629 }
2630
2631 return 0;
2632}
2633EXPORT_SYMBOL_GPL(iscsi_set_param);
2634
2635int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2636 enum iscsi_param param, char *buf)
2637{
Mike Christie75613522008-05-21 15:53:59 -05002638 struct iscsi_session *session = cls_session->dd_data;
Mike Christiea54a52c2006-06-28 12:00:23 -05002639 int len;
2640
2641 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002642 case ISCSI_PARAM_FAST_ABORT:
2643 len = sprintf(buf, "%d\n", session->fast_abort);
2644 break;
Mike Christief6d51802007-12-13 12:43:30 -06002645 case ISCSI_PARAM_ABORT_TMO:
2646 len = sprintf(buf, "%d\n", session->abort_timeout);
2647 break;
2648 case ISCSI_PARAM_LU_RESET_TMO:
2649 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2650 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002651 case ISCSI_PARAM_INITIAL_R2T_EN:
2652 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2653 break;
2654 case ISCSI_PARAM_MAX_R2T:
2655 len = sprintf(buf, "%hu\n", session->max_r2t);
2656 break;
2657 case ISCSI_PARAM_IMM_DATA_EN:
2658 len = sprintf(buf, "%d\n", session->imm_data_en);
2659 break;
2660 case ISCSI_PARAM_FIRST_BURST:
2661 len = sprintf(buf, "%u\n", session->first_burst);
2662 break;
2663 case ISCSI_PARAM_MAX_BURST:
2664 len = sprintf(buf, "%u\n", session->max_burst);
2665 break;
2666 case ISCSI_PARAM_PDU_INORDER_EN:
2667 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2668 break;
2669 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2670 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2671 break;
2672 case ISCSI_PARAM_ERL:
2673 len = sprintf(buf, "%d\n", session->erl);
2674 break;
2675 case ISCSI_PARAM_TARGET_NAME:
2676 len = sprintf(buf, "%s\n", session->targetname);
2677 break;
2678 case ISCSI_PARAM_TPGT:
2679 len = sprintf(buf, "%d\n", session->tpgt);
2680 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002681 case ISCSI_PARAM_USERNAME:
2682 len = sprintf(buf, "%s\n", session->username);
2683 break;
2684 case ISCSI_PARAM_USERNAME_IN:
2685 len = sprintf(buf, "%s\n", session->username_in);
2686 break;
2687 case ISCSI_PARAM_PASSWORD:
2688 len = sprintf(buf, "%s\n", session->password);
2689 break;
2690 case ISCSI_PARAM_PASSWORD_IN:
2691 len = sprintf(buf, "%s\n", session->password_in);
2692 break;
Mike Christie88dfd342008-05-21 15:54:16 -05002693 case ISCSI_PARAM_IFACE_NAME:
2694 len = sprintf(buf, "%s\n", session->ifacename);
2695 break;
2696 case ISCSI_PARAM_INITIATOR_NAME:
2697 if (!session->initiatorname)
2698 len = sprintf(buf, "%s\n", "unknown");
2699 else
2700 len = sprintf(buf, "%s\n", session->initiatorname);
2701 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002702 default:
2703 return -ENOSYS;
2704 }
2705
2706 return len;
2707}
2708EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2709
2710int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2711 enum iscsi_param param, char *buf)
2712{
2713 struct iscsi_conn *conn = cls_conn->dd_data;
2714 int len;
2715
2716 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002717 case ISCSI_PARAM_PING_TMO:
2718 len = sprintf(buf, "%u\n", conn->ping_timeout);
2719 break;
2720 case ISCSI_PARAM_RECV_TMO:
2721 len = sprintf(buf, "%u\n", conn->recv_timeout);
2722 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002723 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2724 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2725 break;
2726 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2727 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2728 break;
2729 case ISCSI_PARAM_HDRDGST_EN:
2730 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2731 break;
2732 case ISCSI_PARAM_DATADGST_EN:
2733 len = sprintf(buf, "%d\n", conn->datadgst_en);
2734 break;
2735 case ISCSI_PARAM_IFMARKER_EN:
2736 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2737 break;
2738 case ISCSI_PARAM_OFMARKER_EN:
2739 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2740 break;
2741 case ISCSI_PARAM_EXP_STATSN:
2742 len = sprintf(buf, "%u\n", conn->exp_statsn);
2743 break;
2744 case ISCSI_PARAM_PERSISTENT_PORT:
2745 len = sprintf(buf, "%d\n", conn->persistent_port);
2746 break;
2747 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2748 len = sprintf(buf, "%s\n", conn->persistent_address);
2749 break;
2750 default:
2751 return -ENOSYS;
2752 }
2753
2754 return len;
2755}
2756EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2757
Mike Christie0801c242007-05-30 12:57:12 -05002758int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2759 char *buf)
2760{
Mike Christie75613522008-05-21 15:53:59 -05002761 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002762 int len;
2763
2764 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002765 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002766 if (!ihost->netdev)
Mike Christied8196ed2007-05-30 12:57:25 -05002767 len = sprintf(buf, "%s\n", "default");
2768 else
Mike Christie75613522008-05-21 15:53:59 -05002769 len = sprintf(buf, "%s\n", ihost->netdev);
Mike Christied8196ed2007-05-30 12:57:25 -05002770 break;
Mike Christie0801c242007-05-30 12:57:12 -05002771 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002772 if (!ihost->hwaddress)
Mike Christie0801c242007-05-30 12:57:12 -05002773 len = sprintf(buf, "%s\n", "default");
2774 else
Mike Christie75613522008-05-21 15:53:59 -05002775 len = sprintf(buf, "%s\n", ihost->hwaddress);
Mike Christie0801c242007-05-30 12:57:12 -05002776 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002777 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002778 if (!ihost->initiatorname)
Mike Christie8ad57812007-05-30 12:57:13 -05002779 len = sprintf(buf, "%s\n", "unknown");
2780 else
Mike Christie75613522008-05-21 15:53:59 -05002781 len = sprintf(buf, "%s\n", ihost->initiatorname);
Mike Christie8ad57812007-05-30 12:57:13 -05002782 break;
Mike Christie75613522008-05-21 15:53:59 -05002783 case ISCSI_HOST_PARAM_IPADDRESS:
2784 if (!strlen(ihost->local_address))
2785 len = sprintf(buf, "%s\n", "unknown");
2786 else
2787 len = sprintf(buf, "%s\n",
2788 ihost->local_address);
Mike Christie88dfd342008-05-21 15:54:16 -05002789 break;
Mike Christie0801c242007-05-30 12:57:12 -05002790 default:
2791 return -ENOSYS;
2792 }
2793
2794 return len;
2795}
2796EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2797
2798int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2799 char *buf, int buflen)
2800{
Mike Christie75613522008-05-21 15:53:59 -05002801 struct iscsi_host *ihost = shost_priv(shost);
Mike Christie0801c242007-05-30 12:57:12 -05002802
2803 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002804 case ISCSI_HOST_PARAM_NETDEV_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002805 if (!ihost->netdev)
2806 ihost->netdev = kstrdup(buf, GFP_KERNEL);
Mike Christied8196ed2007-05-30 12:57:25 -05002807 break;
Mike Christie0801c242007-05-30 12:57:12 -05002808 case ISCSI_HOST_PARAM_HWADDRESS:
Mike Christie75613522008-05-21 15:53:59 -05002809 if (!ihost->hwaddress)
2810 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
Mike Christie0801c242007-05-30 12:57:12 -05002811 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002812 case ISCSI_HOST_PARAM_INITIATOR_NAME:
Mike Christie75613522008-05-21 15:53:59 -05002813 if (!ihost->initiatorname)
2814 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
Mike Christie8ad57812007-05-30 12:57:13 -05002815 break;
Mike Christie0801c242007-05-30 12:57:12 -05002816 default:
2817 return -ENOSYS;
2818 }
2819
2820 return 0;
2821}
2822EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2823
Mike Christie7996a772006-04-06 21:13:41 -05002824MODULE_AUTHOR("Mike Christie");
2825MODULE_DESCRIPTION("iSCSI library functions");
2826MODULE_LICENSE("GPL");