]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libiscsi.c
[SCSI] Replace __FUNCTION__ with __func__ in iscsi_tcp.
[net-next-2.6.git] / drivers / scsi / libiscsi.c
CommitLineData
7996a778
MC
1/*
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>
7996a778
MC
25#include <linux/kfifo.h>
26#include <linux/delay.h>
11836572 27#include <linux/log2.h>
8eb00539 28#include <asm/unaligned.h>
7996a778
MC
29#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
77a23c21
MC
41/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
42#define SNA32_CHECK 2147483648UL
7996a778 43
77a23c21
MC
44static 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)
7996a778
MC
59{
60 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
61 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
62
77a23c21
MC
63 /*
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))
7996a778
MC
72 session->exp_cmdsn = exp_cmdsn;
73
77a23c21
MC
74 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) ||
052d0144
MC
82 !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 }
77a23c21 87 }
7996a778 88}
77a23c21 89EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
7996a778 90
9c19a7d0 91void iscsi_prep_unsolicit_data_pdu(struct iscsi_task *task,
ffd0436e 92 struct iscsi_data *hdr)
7996a778 93{
9c19a7d0 94 struct iscsi_conn *conn = task->conn;
7996a778
MC
95
96 memset(hdr, 0, sizeof(struct iscsi_data));
97 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
9c19a7d0
MC
98 hdr->datasn = cpu_to_be32(task->unsol_datasn);
99 task->unsol_datasn++;
7996a778 100 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
9c19a7d0 101 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
7996a778 102
9c19a7d0 103 hdr->itt = task->hdr->itt;
7996a778 104 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
9c19a7d0 105 hdr->offset = cpu_to_be32(task->unsol_offset);
7996a778 106
9c19a7d0 107 if (task->unsol_count > conn->max_xmit_dlength) {
7996a778 108 hton24(hdr->dlength, conn->max_xmit_dlength);
9c19a7d0
MC
109 task->data_count = conn->max_xmit_dlength;
110 task->unsol_offset += task->data_count;
7996a778
MC
111 hdr->flags = 0;
112 } else {
9c19a7d0
MC
113 hton24(hdr->dlength, task->unsol_count);
114 task->data_count = task->unsol_count;
7996a778
MC
115 hdr->flags = ISCSI_FLAG_CMD_FINAL;
116 }
117}
118EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
119
9c19a7d0 120static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
004d6530 121{
9c19a7d0 122 unsigned exp_len = task->hdr_len + len;
004d6530 123
9c19a7d0 124 if (exp_len > task->hdr_max) {
004d6530
BH
125 WARN_ON(1);
126 return -EINVAL;
127 }
128
129 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
9c19a7d0 130 task->hdr_len = exp_len;
004d6530
BH
131 return 0;
132}
133
38d1c069
BH
134/*
135 * make an extended cdb AHS
136 */
9c19a7d0 137static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
38d1c069 138{
9c19a7d0 139 struct scsi_cmnd *cmd = task->sc;
38d1c069
BH
140 unsigned rlen, pad_len;
141 unsigned short ahslength;
142 struct iscsi_ecdb_ahdr *ecdb_ahdr;
143 int rc;
144
9c19a7d0 145 ecdb_ahdr = iscsi_next_hdr(task);
38d1c069
BH
146 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
9c19a7d0 153 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
38d1c069
BH
154 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",
9c19a7d0 168 cmd->cmd_len, rlen, pad_len, ahslength, task->hdr_len);
38d1c069
BH
169
170 return 0;
171}
172
9c19a7d0 173static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
c07d4444 174{
9c19a7d0 175 struct scsi_cmnd *sc = task->sc;
c07d4444
BH
176 struct iscsi_rlength_ahdr *rlen_ahdr;
177 int rc;
178
9c19a7d0
MC
179 rlen_ahdr = iscsi_next_hdr(task);
180 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
c07d4444
BH
181 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
7996a778
MC
198/**
199 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
9c19a7d0 200 * @task: iscsi task
7996a778
MC
201 *
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 */
9c19a7d0 205static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
7996a778 206{
9c19a7d0 207 struct iscsi_conn *conn = task->conn;
7996a778 208 struct iscsi_session *session = conn->session;
9c19a7d0
MC
209 struct iscsi_cmd *hdr = task->hdr;
210 struct scsi_cmnd *sc = task->sc;
38d1c069 211 unsigned hdrlength, cmd_len;
004d6530 212 int rc;
7996a778 213
9c19a7d0
MC
214 task->hdr_len = 0;
215 rc = iscsi_add_hdr(task, sizeof(*hdr));
004d6530
BH
216 if (rc)
217 return rc;
a8ac6311
OK
218 hdr->opcode = ISCSI_OP_SCSI_CMD;
219 hdr->flags = ISCSI_ATTR_SIMPLE;
220 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
9c19a7d0 221 hdr->itt = build_itt(task->itt, session->age);
a8ac6311
OK
222 hdr->cmdsn = cpu_to_be32(session->cmdsn);
223 session->cmdsn++;
224 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
38d1c069
BH
225 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) {
9c19a7d0 229 rc = iscsi_prep_ecdb_ahs(task);
38d1c069
BH
230 if (rc)
231 return rc;
232 cmd_len = ISCSI_CDB_SIZE;
233 }
234 memcpy(hdr->cdb, sc->cmnd, cmd_len);
7996a778 235
9c19a7d0 236 task->imm_count = 0;
c07d4444
BH
237 if (scsi_bidi_cmnd(sc)) {
238 hdr->flags |= ISCSI_FLAG_CMD_READ;
9c19a7d0 239 rc = iscsi_prep_bidi_ahs(task);
c07d4444
BH
240 if (rc)
241 return rc;
242 }
7996a778 243 if (sc->sc_data_direction == DMA_TO_DEVICE) {
c07d4444
BH
244 unsigned out_len = scsi_out(sc)->length;
245 hdr->data_length = cpu_to_be32(out_len);
7996a778
MC
246 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 */
9c19a7d0
MC
261 task->unsol_count = 0;
262 task->unsol_offset = 0;
263 task->unsol_datasn = 0;
7996a778
MC
264
265 if (session->imm_data_en) {
c07d4444 266 if (out_len >= session->first_burst)
9c19a7d0 267 task->imm_count = min(session->first_burst,
7996a778
MC
268 conn->max_xmit_dlength);
269 else
9c19a7d0 270 task->imm_count = min(out_len,
7996a778 271 conn->max_xmit_dlength);
9c19a7d0 272 hton24(hdr->dlength, task->imm_count);
7996a778 273 } else
a8ac6311 274 zero_data(hdr->dlength);
7996a778 275
ffd0436e 276 if (!session->initial_r2t_en) {
9c19a7d0
MC
277 task->unsol_count = min(session->first_burst, out_len)
278 - task->imm_count;
279 task->unsol_offset = task->imm_count;
ffd0436e
MC
280 }
281
9c19a7d0 282 if (!task->unsol_count)
7996a778 283 /* No unsolicit Data-Out's */
a8ac6311 284 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
7996a778 285 } else {
7996a778
MC
286 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
287 zero_data(hdr->dlength);
c07d4444 288 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
7996a778
MC
289
290 if (sc->sc_data_direction == DMA_FROM_DEVICE)
291 hdr->flags |= ISCSI_FLAG_CMD_READ;
292 }
293
004d6530 294 /* calculate size of additional header segments (AHSs) */
9c19a7d0 295 hdrlength = task->hdr_len - sizeof(*hdr);
004d6530
BH
296
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
3e5c28ad 303 if (conn->session->tt->init_task &&
9c19a7d0 304 conn->session->tt->init_task(task))
052d0144
MC
305 return -EIO;
306
9c19a7d0
MC
307 task->state = ISCSI_TASK_RUNNING;
308 list_move_tail(&task->running, &conn->run_list);
77a23c21 309
a8ac6311 310 conn->scsicmd_pdus_cnt++;
3e5c28ad
MC
311 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 ?
9c19a7d0 314 "write" : "read", conn->id, sc, sc->cmnd[0], task->itt,
3e5c28ad
MC
315 scsi_bufflen(sc),
316 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
317 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
004d6530 318 return 0;
7996a778 319}
7996a778
MC
320
321/**
3e5c28ad 322 * iscsi_complete_command - finish a task
9c19a7d0 323 * @task: iscsi cmd task
7996a778
MC
324 *
325 * Must be called with session lock.
3e5c28ad
MC
326 * This function returns the scsi command to scsi-ml or cleans
327 * up mgmt tasks then returns the task to the pool.
7996a778 328 */
9c19a7d0 329static void iscsi_complete_command(struct iscsi_task *task)
7996a778 330{
9c19a7d0 331 struct iscsi_conn *conn = task->conn;
c1635cb7 332 struct iscsi_session *session = conn->session;
9c19a7d0 333 struct scsi_cmnd *sc = task->sc;
7996a778 334
9c19a7d0
MC
335 list_del_init(&task->running);
336 task->state = ISCSI_TASK_COMPLETED;
337 task->sc = NULL;
3e5c28ad 338
9c19a7d0
MC
339 if (conn->task == task)
340 conn->task = NULL;
3e5c28ad 341 /*
9c19a7d0 342 * login task is preallocated so do not free
3e5c28ad 343 */
9c19a7d0 344 if (conn->login_task == task)
3e5c28ad
MC
345 return;
346
9c19a7d0 347 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
052d0144 348
9c19a7d0
MC
349 if (conn->ping_task == task)
350 conn->ping_task = NULL;
3e5c28ad
MC
351
352 if (sc) {
9c19a7d0 353 task->sc = NULL;
3e5c28ad
MC
354 /* 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 }
7996a778
MC
363}
364
913e5bf4 365void __iscsi_get_task(struct iscsi_task *task)
60ecebf5 366{
9c19a7d0 367 atomic_inc(&task->refcount);
60ecebf5 368}
913e5bf4 369EXPORT_SYMBOL_GPL(__iscsi_get_task);
60ecebf5 370
9c19a7d0 371static void __iscsi_put_task(struct iscsi_task *task)
60ecebf5 372{
9c19a7d0
MC
373 if (atomic_dec_and_test(&task->refcount))
374 iscsi_complete_command(task);
60ecebf5
MC
375}
376
9c19a7d0 377void iscsi_put_task(struct iscsi_task *task)
3e5c28ad 378{
9c19a7d0 379 struct iscsi_session *session = task->conn->session;
3e5c28ad
MC
380
381 spin_lock_bh(&session->lock);
9c19a7d0 382 __iscsi_put_task(task);
3e5c28ad
MC
383 spin_unlock_bh(&session->lock);
384}
9c19a7d0 385EXPORT_SYMBOL_GPL(iscsi_put_task);
3e5c28ad 386
b3a7ea8d
MC
387/*
388 * session lock must be held
389 */
9c19a7d0 390static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
b3a7ea8d
MC
391 int err)
392{
393 struct scsi_cmnd *sc;
394
9c19a7d0 395 sc = task->sc;
b3a7ea8d
MC
396 if (!sc)
397 return;
398
9c19a7d0 399 if (task->state == ISCSI_TASK_PENDING)
b3a7ea8d
MC
400 /*
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
9c19a7d0 406 conn->session->tt->cleanup_task(conn, task);
913e5bf4
MC
407 /*
408 * Check if cleanup_task dropped the lock and the command completed,
409 */
410 if (!task->sc)
411 return;
b3a7ea8d
MC
412
413 sc->result = err;
c07d4444
BH
414 if (!scsi_bidi_cmnd(sc))
415 scsi_set_resid(sc, scsi_bufflen(sc));
416 else {
417 scsi_out(sc)->resid = scsi_out(sc)->length;
418 scsi_in(sc)->resid = scsi_in(sc)->length;
419 }
3e5c28ad 420
9c19a7d0
MC
421 if (conn->task == task)
422 conn->task = NULL;
b3a7ea8d 423 /* release ref from queuecommand */
9c19a7d0 424 __iscsi_put_task(task);
b3a7ea8d
MC
425}
426
3e5c28ad 427static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
9c19a7d0 428 struct iscsi_task *task)
052d0144
MC
429{
430 struct iscsi_session *session = conn->session;
9c19a7d0 431 struct iscsi_hdr *hdr = (struct iscsi_hdr *)task->hdr;
052d0144
MC
432 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
433
434 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
435 return -ENOTCONN;
436
437 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
438 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
439 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
440 /*
441 * pre-format CmdSN for outgoing PDU.
442 */
443 nop->cmdsn = cpu_to_be32(session->cmdsn);
444 if (hdr->itt != RESERVED_ITT) {
9c19a7d0 445 hdr->itt = build_itt(task->itt, session->age);
052d0144
MC
446 /*
447 * TODO: We always use immediate, so we never hit this.
448 * If we start to send tmfs or nops as non-immediate then
449 * we should start checking the cmdsn numbers for mgmt tasks.
450 */
451 if (conn->c_stage == ISCSI_CONN_STARTED &&
452 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
453 session->queued_cmdsn++;
454 session->cmdsn++;
455 }
456 }
457
3e5c28ad 458 if (session->tt->init_task)
9c19a7d0 459 session->tt->init_task(task);
052d0144
MC
460
461 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
462 session->state = ISCSI_STATE_LOGGING_OUT;
463
9c19a7d0 464 list_move_tail(&task->running, &conn->mgmt_run_list);
052d0144
MC
465 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
466 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
9c19a7d0 467 task->data_count);
052d0144
MC
468 return 0;
469}
470
9c19a7d0 471static struct iscsi_task *
f6d5180c
MC
472__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
473 char *data, uint32_t data_size)
474{
475 struct iscsi_session *session = conn->session;
9c19a7d0 476 struct iscsi_task *task;
f6d5180c
MC
477
478 if (session->state == ISCSI_STATE_TERMINATE)
479 return NULL;
480
481 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
482 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
483 /*
484 * Login and Text are sent serially, in
485 * request-followed-by-response sequence.
3e5c28ad
MC
486 * Same task can be used. Same ITT must be used.
487 * Note that login_task is preallocated at conn_create().
f6d5180c 488 */
9c19a7d0 489 task = conn->login_task;
f6d5180c
MC
490 else {
491 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
492 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
493
3e5c28ad 494 if (!__kfifo_get(session->cmdpool.queue,
9c19a7d0 495 (void*)&task, sizeof(void*)))
f6d5180c 496 return NULL;
052d0144
MC
497
498 if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
499 hdr->ttt == RESERVED_ITT) {
9c19a7d0 500 conn->ping_task = task;
052d0144
MC
501 conn->last_ping = jiffies;
502 }
f6d5180c 503 }
3e5c28ad
MC
504 /*
505 * released in complete pdu for task we expect a response for, and
506 * released by the lld when it has transmitted the task for
507 * pdus we do not expect a response for.
508 */
9c19a7d0
MC
509 atomic_set(&task->refcount, 1);
510 task->conn = conn;
511 task->sc = NULL;
f6d5180c
MC
512
513 if (data_size) {
9c19a7d0
MC
514 memcpy(task->data, data, data_size);
515 task->data_count = data_size;
f6d5180c 516 } else
9c19a7d0 517 task->data_count = 0;
f6d5180c 518
9c19a7d0
MC
519 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
520 INIT_LIST_HEAD(&task->running);
521 list_add_tail(&task->running, &conn->mgmtqueue);
052d0144
MC
522
523 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
9c19a7d0
MC
524 if (iscsi_prep_mgmt_task(conn, task)) {
525 __iscsi_put_task(task);
052d0144
MC
526 return NULL;
527 }
528
9c19a7d0
MC
529 if (session->tt->xmit_task(task))
530 task = NULL;
052d0144
MC
531
532 } else
533 scsi_queue_work(conn->session->host, &conn->xmitwork);
534
9c19a7d0 535 return task;
f6d5180c
MC
536}
537
538int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
539 char *data, uint32_t data_size)
540{
541 struct iscsi_conn *conn = cls_conn->dd_data;
542 struct iscsi_session *session = conn->session;
543 int err = 0;
544
545 spin_lock_bh(&session->lock);
546 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
547 err = -EPERM;
548 spin_unlock_bh(&session->lock);
f6d5180c
MC
549 return err;
550}
551EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
552
7996a778
MC
553/**
554 * iscsi_cmd_rsp - SCSI Command Response processing
555 * @conn: iscsi connection
556 * @hdr: iscsi header
9c19a7d0 557 * @task: scsi command task
7996a778
MC
558 * @data: cmd data buffer
559 * @datalen: len of buffer
560 *
561 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
9c19a7d0 562 * then completes the command and task.
7996a778 563 **/
77a23c21 564static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
9c19a7d0 565 struct iscsi_task *task, char *data,
77a23c21 566 int datalen)
7996a778 567{
7996a778
MC
568 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
569 struct iscsi_session *session = conn->session;
9c19a7d0 570 struct scsi_cmnd *sc = task->sc;
7996a778 571
77a23c21 572 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
7996a778
MC
573 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
574
575 sc->result = (DID_OK << 16) | rhdr->cmd_status;
576
577 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
578 sc->result = DID_ERROR << 16;
579 goto out;
580 }
581
582 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
9b80cb4b 583 uint16_t senselen;
7996a778
MC
584
585 if (datalen < 2) {
586invalid_datalen:
322d739d
MC
587 iscsi_conn_printk(KERN_ERR, conn,
588 "Got CHECK_CONDITION but invalid data "
589 "buffer size of %d\n", datalen);
7996a778
MC
590 sc->result = DID_BAD_TARGET << 16;
591 goto out;
592 }
593
8eb00539 594 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
7996a778
MC
595 if (datalen < senselen)
596 goto invalid_datalen;
597
598 memcpy(sc->sense_buffer, data + 2,
9b80cb4b 599 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778 600 debug_scsi("copied %d bytes of sense\n",
8eb00539 601 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778
MC
602 }
603
c07d4444
BH
604 if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
605 ISCSI_FLAG_CMD_BIDI_OVERFLOW)) {
606 int res_count = be32_to_cpu(rhdr->bi_residual_count);
607
608 if (scsi_bidi_cmnd(sc) && res_count > 0 &&
609 (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW ||
610 res_count <= scsi_in(sc)->length))
611 scsi_in(sc)->resid = res_count;
612 else
613 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
614 }
615
7207fea4
BH
616 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
617 ISCSI_FLAG_CMD_OVERFLOW)) {
7996a778
MC
618 int res_count = be32_to_cpu(rhdr->residual_count);
619
7207fea4
BH
620 if (res_count > 0 &&
621 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
622 res_count <= scsi_bufflen(sc)))
c07d4444 623 /* write side for bidi or uni-io set_resid */
1c138991 624 scsi_set_resid(sc, res_count);
7996a778
MC
625 else
626 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
c07d4444 627 }
7996a778
MC
628out:
629 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
9c19a7d0 630 (long)sc, sc->result, task->itt);
7996a778
MC
631 conn->scsirsp_pdus_cnt++;
632
9c19a7d0 633 __iscsi_put_task(task);
7996a778
MC
634}
635
7ea8b828
MC
636static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
637{
638 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
639
640 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
641 conn->tmfrsp_pdus_cnt++;
642
843c0a8a 643 if (conn->tmf_state != TMF_QUEUED)
7ea8b828
MC
644 return;
645
646 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
843c0a8a 647 conn->tmf_state = TMF_SUCCESS;
7ea8b828 648 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
843c0a8a 649 conn->tmf_state = TMF_NOT_FOUND;
7ea8b828 650 else
843c0a8a 651 conn->tmf_state = TMF_FAILED;
7ea8b828
MC
652 wake_up(&conn->ehwait);
653}
654
f6d5180c
MC
655static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
656{
657 struct iscsi_nopout hdr;
9c19a7d0 658 struct iscsi_task *task;
f6d5180c 659
9c19a7d0 660 if (!rhdr && conn->ping_task)
f6d5180c
MC
661 return;
662
663 memset(&hdr, 0, sizeof(struct iscsi_nopout));
664 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
665 hdr.flags = ISCSI_FLAG_CMD_FINAL;
666
667 if (rhdr) {
668 memcpy(hdr.lun, rhdr->lun, 8);
669 hdr.ttt = rhdr->ttt;
670 hdr.itt = RESERVED_ITT;
671 } else
672 hdr.ttt = RESERVED_ITT;
673
9c19a7d0
MC
674 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
675 if (!task)
322d739d 676 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
f6d5180c
MC
677}
678
62f38300
MC
679static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
680 char *data, int datalen)
681{
682 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
683 struct iscsi_hdr rejected_pdu;
684 uint32_t itt;
685
686 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
687
688 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
689 if (ntoh24(reject->dlength) > datalen)
690 return ISCSI_ERR_PROTO;
691
692 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
693 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
b4377356 694 itt = get_itt(rejected_pdu.itt);
322d739d
MC
695 iscsi_conn_printk(KERN_ERR, conn,
696 "itt 0x%x had pdu (op 0x%x) rejected "
697 "due to DataDigest error.\n", itt,
698 rejected_pdu.opcode);
62f38300
MC
699 }
700 }
701 return 0;
702}
703
913e5bf4
MC
704/**
705 * iscsi_itt_to_task - look up task by itt
706 * @conn: iscsi connection
707 * @itt: itt
708 *
709 * This should be used for mgmt tasks like login and nops, or if
710 * the LDD's itt space does not include the session age.
711 *
712 * The session lock must be held.
713 */
714static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
715{
716 struct iscsi_session *session = conn->session;
717 uint32_t i;
718
719 if (itt == RESERVED_ITT)
720 return NULL;
721
722 i = get_itt(itt);
723 if (i >= session->cmds_max)
724 return NULL;
725
726 return session->cmds[i];
727}
728
7996a778
MC
729/**
730 * __iscsi_complete_pdu - complete pdu
731 * @conn: iscsi conn
732 * @hdr: iscsi header
733 * @data: data buffer
734 * @datalen: len of data buffer
735 *
736 * Completes pdu processing by freeing any resources allocated at
737 * queuecommand or send generic. session lock must be held and verify
738 * itt must have been called.
739 */
913e5bf4
MC
740int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
741 char *data, int datalen)
7996a778
MC
742{
743 struct iscsi_session *session = conn->session;
744 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
9c19a7d0 745 struct iscsi_task *task;
7996a778
MC
746 uint32_t itt;
747
f6d5180c 748 conn->last_recv = jiffies;
0af967f5
MC
749 rc = iscsi_verify_itt(conn, hdr->itt);
750 if (rc)
751 return rc;
752
b4377356
AV
753 if (hdr->itt != RESERVED_ITT)
754 itt = get_itt(hdr->itt);
7996a778 755 else
b4377356 756 itt = ~0U;
7996a778 757
3e5c28ad
MC
758 debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n",
759 opcode, conn->id, itt, datalen);
7996a778 760
3e5c28ad 761 if (itt == ~0U) {
77a23c21 762 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
62f38300 763
7996a778
MC
764 switch(opcode) {
765 case ISCSI_OP_NOOP_IN:
40527afe 766 if (datalen) {
7996a778 767 rc = ISCSI_ERR_PROTO;
40527afe
MC
768 break;
769 }
770
b4377356 771 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
40527afe
MC
772 break;
773
f6d5180c 774 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
7996a778
MC
775 break;
776 case ISCSI_OP_REJECT:
62f38300
MC
777 rc = iscsi_handle_reject(conn, hdr, data, datalen);
778 break;
7996a778 779 case ISCSI_OP_ASYNC_EVENT:
8d2860b3 780 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
5831c737
MC
781 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
782 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
783 break;
784 default:
785 rc = ISCSI_ERR_BAD_OPCODE;
786 break;
787 }
3e5c28ad
MC
788 goto out;
789 }
790
3e5c28ad
MC
791 switch(opcode) {
792 case ISCSI_OP_SCSI_CMD_RSP:
913e5bf4
MC
793 case ISCSI_OP_SCSI_DATA_IN:
794 task = iscsi_itt_to_ctask(conn, hdr->itt);
795 if (!task)
796 return ISCSI_ERR_BAD_ITT;
797 break;
798 case ISCSI_OP_R2T:
799 /*
800 * LLD handles R2Ts if they need to.
801 */
802 return 0;
803 case ISCSI_OP_LOGOUT_RSP:
804 case ISCSI_OP_LOGIN_RSP:
805 case ISCSI_OP_TEXT_RSP:
806 case ISCSI_OP_SCSI_TMFUNC_RSP:
807 case ISCSI_OP_NOOP_IN:
808 task = iscsi_itt_to_task(conn, hdr->itt);
809 if (!task)
810 return ISCSI_ERR_BAD_ITT;
811 break;
812 default:
813 return ISCSI_ERR_BAD_OPCODE;
814 }
815
816 switch(opcode) {
817 case ISCSI_OP_SCSI_CMD_RSP:
9c19a7d0 818 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
3e5c28ad
MC
819 break;
820 case ISCSI_OP_SCSI_DATA_IN:
3e5c28ad
MC
821 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
822 conn->scsirsp_pdus_cnt++;
823 iscsi_update_cmdsn(session,
824 (struct iscsi_nopin*) hdr);
9c19a7d0 825 __iscsi_put_task(task);
3e5c28ad
MC
826 }
827 break;
3e5c28ad
MC
828 case ISCSI_OP_LOGOUT_RSP:
829 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
830 if (datalen) {
831 rc = ISCSI_ERR_PROTO;
832 break;
833 }
834 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
835 goto recv_pdu;
836 case ISCSI_OP_LOGIN_RSP:
837 case ISCSI_OP_TEXT_RSP:
838 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
839 /*
840 * login related PDU's exp_statsn is handled in
841 * userspace
842 */
843 goto recv_pdu;
844 case ISCSI_OP_SCSI_TMFUNC_RSP:
845 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
846 if (datalen) {
847 rc = ISCSI_ERR_PROTO;
848 break;
849 }
850
851 iscsi_tmf_rsp(conn, hdr);
9c19a7d0 852 __iscsi_put_task(task);
3e5c28ad
MC
853 break;
854 case ISCSI_OP_NOOP_IN:
855 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
856 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
857 rc = ISCSI_ERR_PROTO;
858 break;
859 }
860 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
861
9c19a7d0 862 if (conn->ping_task != task)
3e5c28ad
MC
863 /*
864 * If this is not in response to one of our
865 * nops then it must be from userspace.
866 */
867 goto recv_pdu;
9c19a7d0
MC
868
869 mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout);
870 __iscsi_put_task(task);
3e5c28ad
MC
871 break;
872 default:
873 rc = ISCSI_ERR_BAD_OPCODE;
874 break;
875 }
7996a778 876
3e5c28ad
MC
877out:
878 return rc;
879recv_pdu:
880 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
881 rc = ISCSI_ERR_CONN_FAILED;
9c19a7d0 882 __iscsi_put_task(task);
7996a778
MC
883 return rc;
884}
913e5bf4 885EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
7996a778
MC
886
887int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
888 char *data, int datalen)
889{
890 int rc;
891
892 spin_lock(&conn->session->lock);
893 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
894 spin_unlock(&conn->session->lock);
895 return rc;
896}
897EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
898
0af967f5 899int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
7996a778
MC
900{
901 struct iscsi_session *session = conn->session;
3e5c28ad 902 uint32_t i;
7996a778 903
0af967f5
MC
904 if (itt == RESERVED_ITT)
905 return 0;
7996a778 906
0af967f5
MC
907 if (((__force u32)itt & ISCSI_AGE_MASK) !=
908 (session->age << ISCSI_AGE_SHIFT)) {
909 iscsi_conn_printk(KERN_ERR, conn,
910 "received itt %x expected session age (%x)\n",
913e5bf4 911 (__force u32)itt, session->age);
0af967f5
MC
912 return ISCSI_ERR_BAD_ITT;
913 }
7996a778 914
3e5c28ad
MC
915 i = get_itt(itt);
916 if (i >= session->cmds_max) {
917 iscsi_conn_printk(KERN_ERR, conn,
918 "received invalid itt index %u (max cmds "
919 "%u.\n", i, session->cmds_max);
920 return ISCSI_ERR_BAD_ITT;
7996a778 921 }
7996a778
MC
922 return 0;
923}
924EXPORT_SYMBOL_GPL(iscsi_verify_itt);
925
913e5bf4
MC
926/**
927 * iscsi_itt_to_ctask - look up ctask by itt
928 * @conn: iscsi connection
929 * @itt: itt
930 *
931 * This should be used for cmd tasks.
932 *
933 * The session lock must be held.
934 */
935struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
0af967f5 936{
9c19a7d0 937 struct iscsi_task *task;
0af967f5
MC
938
939 if (iscsi_verify_itt(conn, itt))
940 return NULL;
941
913e5bf4
MC
942 task = iscsi_itt_to_task(conn, itt);
943 if (!task || !task->sc)
0af967f5
MC
944 return NULL;
945
913e5bf4
MC
946 if (task->sc->SCp.phase != conn->session->age) {
947 iscsi_session_printk(KERN_ERR, conn->session,
948 "task's session age %d, expected %d\n",
949 task->sc->SCp.phase, conn->session->age);
0af967f5 950 return NULL;
913e5bf4 951 }
0af967f5 952
9c19a7d0 953 return task;
0af967f5
MC
954}
955EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
956
7996a778
MC
957void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
958{
959 struct iscsi_session *session = conn->session;
960 unsigned long flags;
961
962 spin_lock_irqsave(&session->lock, flags);
656cffc9
MC
963 if (session->state == ISCSI_STATE_FAILED) {
964 spin_unlock_irqrestore(&session->lock, flags);
965 return;
966 }
967
67a61114 968 if (conn->stop_stage == 0)
7996a778
MC
969 session->state = ISCSI_STATE_FAILED;
970 spin_unlock_irqrestore(&session->lock, flags);
971 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
972 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
973 iscsi_conn_error(conn->cls_conn, err);
974}
975EXPORT_SYMBOL_GPL(iscsi_conn_failure);
976
77a23c21
MC
977static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
978{
979 struct iscsi_session *session = conn->session;
980
981 /*
982 * Check for iSCSI window and take care of CmdSN wrap-around
983 */
e0726407
MC
984 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
985 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
986 "CmdSN %u/%u\n", session->exp_cmdsn,
987 session->max_cmdsn, session->cmdsn,
988 session->queued_cmdsn);
77a23c21
MC
989 return -ENOSPC;
990 }
991 return 0;
992}
993
9c19a7d0 994static int iscsi_xmit_task(struct iscsi_conn *conn)
77a23c21 995{
9c19a7d0 996 struct iscsi_task *task = conn->task;
843c0a8a 997 int rc;
77a23c21 998
9c19a7d0 999 __iscsi_get_task(task);
77a23c21 1000 spin_unlock_bh(&conn->session->lock);
9c19a7d0 1001 rc = conn->session->tt->xmit_task(task);
77a23c21 1002 spin_lock_bh(&conn->session->lock);
9c19a7d0 1003 __iscsi_put_task(task);
77a23c21 1004 if (!rc)
9c19a7d0
MC
1005 /* done with this task */
1006 conn->task = NULL;
77a23c21
MC
1007 return rc;
1008}
1009
843c0a8a 1010/**
9c19a7d0
MC
1011 * iscsi_requeue_task - requeue task to run from session workqueue
1012 * @task: task to requeue
843c0a8a 1013 *
9c19a7d0 1014 * LLDs that need to run a task from the session workqueue should call
052d0144
MC
1015 * this. The session lock must be held. This should only be called
1016 * by software drivers.
843c0a8a 1017 */
9c19a7d0 1018void iscsi_requeue_task(struct iscsi_task *task)
843c0a8a 1019{
9c19a7d0 1020 struct iscsi_conn *conn = task->conn;
843c0a8a 1021
9c19a7d0 1022 list_move_tail(&task->running, &conn->requeue);
843c0a8a
MC
1023 scsi_queue_work(conn->session->host, &conn->xmitwork);
1024}
9c19a7d0 1025EXPORT_SYMBOL_GPL(iscsi_requeue_task);
843c0a8a 1026
7996a778
MC
1027/**
1028 * iscsi_data_xmit - xmit any command into the scheduled connection
1029 * @conn: iscsi connection
1030 *
1031 * Notes:
1032 * The function can return -EAGAIN in which case the caller must
1033 * re-schedule it again later or recover. '0' return code means
1034 * successful xmit.
1035 **/
1036static int iscsi_data_xmit(struct iscsi_conn *conn)
1037{
3219e529 1038 int rc = 0;
7996a778 1039
77a23c21 1040 spin_lock_bh(&conn->session->lock);
7996a778
MC
1041 if (unlikely(conn->suspend_tx)) {
1042 debug_scsi("conn %d Tx suspended!\n", conn->id);
77a23c21 1043 spin_unlock_bh(&conn->session->lock);
3219e529 1044 return -ENODATA;
7996a778 1045 }
7996a778 1046
9c19a7d0
MC
1047 if (conn->task) {
1048 rc = iscsi_xmit_task(conn);
3219e529 1049 if (rc)
7996a778 1050 goto again;
7996a778
MC
1051 }
1052
77a23c21
MC
1053 /*
1054 * process mgmt pdus like nops before commands since we should
1055 * only have one nop-out as a ping from us and targets should not
1056 * overflow us with nop-ins
1057 */
1058check_mgmt:
843c0a8a 1059 while (!list_empty(&conn->mgmtqueue)) {
9c19a7d0
MC
1060 conn->task = list_entry(conn->mgmtqueue.next,
1061 struct iscsi_task, running);
1062 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1063 __iscsi_put_task(conn->task);
1064 conn->task = NULL;
b3a7ea8d
MC
1065 continue;
1066 }
9c19a7d0 1067 rc = iscsi_xmit_task(conn);
77a23c21
MC
1068 if (rc)
1069 goto again;
7996a778
MC
1070 }
1071
843c0a8a 1072 /* process pending command queue */
b6c395ed 1073 while (!list_empty(&conn->xmitqueue)) {
843c0a8a
MC
1074 if (conn->tmf_state == TMF_QUEUED)
1075 break;
1076
9c19a7d0
MC
1077 conn->task = list_entry(conn->xmitqueue.next,
1078 struct iscsi_task, running);
b3a7ea8d 1079 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
9c19a7d0 1080 fail_command(conn, conn->task, DID_IMM_RETRY << 16);
b3a7ea8d
MC
1081 continue;
1082 }
9c19a7d0
MC
1083 if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
1084 fail_command(conn, conn->task, DID_ABORT << 16);
004d6530
BH
1085 continue;
1086 }
9c19a7d0 1087 rc = iscsi_xmit_task(conn);
77a23c21 1088 if (rc)
60ecebf5 1089 goto again;
77a23c21 1090 /*
9c19a7d0 1091 * we could continuously get new task requests so
77a23c21
MC
1092 * we need to check the mgmt queue for nops that need to
1093 * be sent to aviod starvation
1094 */
843c0a8a
MC
1095 if (!list_empty(&conn->mgmtqueue))
1096 goto check_mgmt;
1097 }
1098
1099 while (!list_empty(&conn->requeue)) {
1100 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
1101 break;
1102
b3a7ea8d
MC
1103 /*
1104 * we always do fastlogout - conn stop code will clean up.
1105 */
1106 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
1107 break;
1108
9c19a7d0
MC
1109 conn->task = list_entry(conn->requeue.next,
1110 struct iscsi_task, running);
1111 conn->task->state = ISCSI_TASK_RUNNING;
843c0a8a 1112 list_move_tail(conn->requeue.next, &conn->run_list);
9c19a7d0 1113 rc = iscsi_xmit_task(conn);
843c0a8a
MC
1114 if (rc)
1115 goto again;
1116 if (!list_empty(&conn->mgmtqueue))
77a23c21 1117 goto check_mgmt;
7996a778 1118 }
b6c395ed 1119 spin_unlock_bh(&conn->session->lock);
3219e529 1120 return -ENODATA;
7996a778
MC
1121
1122again:
1123 if (unlikely(conn->suspend_tx))
77a23c21
MC
1124 rc = -ENODATA;
1125 spin_unlock_bh(&conn->session->lock);
3219e529 1126 return rc;
7996a778
MC
1127}
1128
c4028958 1129static void iscsi_xmitworker(struct work_struct *work)
7996a778 1130{
c4028958
DH
1131 struct iscsi_conn *conn =
1132 container_of(work, struct iscsi_conn, xmitwork);
3219e529 1133 int rc;
7996a778
MC
1134 /*
1135 * serialize Xmit worker on a per-connection basis.
1136 */
3219e529
MC
1137 do {
1138 rc = iscsi_data_xmit(conn);
1139 } while (rc >= 0 || rc == -EAGAIN);
7996a778
MC
1140}
1141
1142enum {
1143 FAILURE_BAD_HOST = 1,
1144 FAILURE_SESSION_FAILED,
1145 FAILURE_SESSION_FREED,
1146 FAILURE_WINDOW_CLOSED,
60ecebf5 1147 FAILURE_OOM,
7996a778 1148 FAILURE_SESSION_TERMINATE,
656cffc9 1149 FAILURE_SESSION_IN_RECOVERY,
7996a778 1150 FAILURE_SESSION_RECOVERY_TIMEOUT,
b3a7ea8d 1151 FAILURE_SESSION_LOGGING_OUT,
6eabafbe 1152 FAILURE_SESSION_NOT_READY,
7996a778
MC
1153};
1154
1155int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1156{
75613521 1157 struct iscsi_cls_session *cls_session;
7996a778
MC
1158 struct Scsi_Host *host;
1159 int reason = 0;
1160 struct iscsi_session *session;
1161 struct iscsi_conn *conn;
9c19a7d0 1162 struct iscsi_task *task = NULL;
7996a778
MC
1163
1164 sc->scsi_done = done;
1165 sc->result = 0;
f47f2cf5 1166 sc->SCp.ptr = NULL;
7996a778
MC
1167
1168 host = sc->device->host;
1040c99d 1169 spin_unlock(host->host_lock);
7996a778 1170
75613521
MC
1171 cls_session = starget_to_session(scsi_target(sc->device));
1172 session = cls_session->dd_data;
7996a778
MC
1173 spin_lock(&session->lock);
1174
75613521 1175 reason = iscsi_session_chkready(cls_session);
6eabafbe
MC
1176 if (reason) {
1177 sc->result = reason;
1178 goto fault;
1179 }
1180
656cffc9
MC
1181 /*
1182 * ISCSI_STATE_FAILED is a temp. state. The recovery
1183 * code will decide what is best to do with command queued
1184 * during this time
1185 */
1186 if (session->state != ISCSI_STATE_LOGGED_IN &&
1187 session->state != ISCSI_STATE_FAILED) {
1188 /*
1189 * to handle the race between when we set the recovery state
1190 * and block the session we requeue here (commands could
1191 * be entering our queuecommand while a block is starting
1192 * up because the block code is not locked)
1193 */
9000bcd6
MC
1194 switch (session->state) {
1195 case ISCSI_STATE_IN_RECOVERY:
656cffc9 1196 reason = FAILURE_SESSION_IN_RECOVERY;
6eabafbe
MC
1197 sc->result = DID_IMM_RETRY << 16;
1198 break;
9000bcd6
MC
1199 case ISCSI_STATE_LOGGING_OUT:
1200 reason = FAILURE_SESSION_LOGGING_OUT;
6eabafbe
MC
1201 sc->result = DID_IMM_RETRY << 16;
1202 break;
b3a7ea8d 1203 case ISCSI_STATE_RECOVERY_FAILED:
656cffc9 1204 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
6eabafbe 1205 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d
MC
1206 break;
1207 case ISCSI_STATE_TERMINATE:
656cffc9 1208 reason = FAILURE_SESSION_TERMINATE;
6eabafbe 1209 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d 1210 break;
b3a7ea8d 1211 default:
656cffc9 1212 reason = FAILURE_SESSION_FREED;
6eabafbe 1213 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d 1214 }
7996a778
MC
1215 goto fault;
1216 }
1217
7996a778 1218 conn = session->leadconn;
98644047
MC
1219 if (!conn) {
1220 reason = FAILURE_SESSION_FREED;
6eabafbe 1221 sc->result = DID_NO_CONNECT << 16;
98644047
MC
1222 goto fault;
1223 }
7996a778 1224
77a23c21
MC
1225 if (iscsi_check_cmdsn_window_closed(conn)) {
1226 reason = FAILURE_WINDOW_CLOSED;
1227 goto reject;
1228 }
1229
9c19a7d0 1230 if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
60ecebf5
MC
1231 sizeof(void*))) {
1232 reason = FAILURE_OOM;
1233 goto reject;
1234 }
7996a778 1235 sc->SCp.phase = session->age;
9c19a7d0 1236 sc->SCp.ptr = (char *)task;
7996a778 1237
9c19a7d0
MC
1238 atomic_set(&task->refcount, 1);
1239 task->state = ISCSI_TASK_PENDING;
1240 task->conn = conn;
1241 task->sc = sc;
1242 INIT_LIST_HEAD(&task->running);
1243 list_add_tail(&task->running, &conn->xmitqueue);
7996a778 1244
052d0144 1245 if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
9c19a7d0 1246 if (iscsi_prep_scsi_cmd_pdu(task)) {
052d0144
MC
1247 sc->result = DID_ABORT << 16;
1248 sc->scsi_done = NULL;
9c19a7d0 1249 iscsi_complete_command(task);
052d0144
MC
1250 goto fault;
1251 }
9c19a7d0 1252 if (session->tt->xmit_task(task)) {
052d0144 1253 sc->scsi_done = NULL;
9c19a7d0 1254 iscsi_complete_command(task);
052d0144
MC
1255 reason = FAILURE_SESSION_NOT_READY;
1256 goto reject;
1257 }
1258 } else
1259 scsi_queue_work(session->host, &conn->xmitwork);
1260
1261 session->queued_cmdsn++;
1262 spin_unlock(&session->lock);
1040c99d 1263 spin_lock(host->host_lock);
7996a778
MC
1264 return 0;
1265
1266reject:
1267 spin_unlock(&session->lock);
1268 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1040c99d 1269 spin_lock(host->host_lock);
7996a778
MC
1270 return SCSI_MLQUEUE_HOST_BUSY;
1271
1272fault:
1273 spin_unlock(&session->lock);
6eabafbe 1274 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
c07d4444
BH
1275 if (!scsi_bidi_cmnd(sc))
1276 scsi_set_resid(sc, scsi_bufflen(sc));
1277 else {
1278 scsi_out(sc)->resid = scsi_out(sc)->length;
1279 scsi_in(sc)->resid = scsi_in(sc)->length;
1280 }
052d0144 1281 done(sc);
1040c99d 1282 spin_lock(host->host_lock);
7996a778
MC
1283 return 0;
1284}
1285EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1286
1287int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1288{
1289 if (depth > ISCSI_MAX_CMD_PER_LUN)
1290 depth = ISCSI_MAX_CMD_PER_LUN;
1291 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1292 return sdev->queue_depth;
1293}
1294EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1295
7996a778
MC
1296void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1297{
75613521 1298 struct iscsi_session *session = cls_session->dd_data;
7996a778
MC
1299
1300 spin_lock_bh(&session->lock);
1301 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9 1302 session->state = ISCSI_STATE_RECOVERY_FAILED;
843c0a8a
MC
1303 if (session->leadconn)
1304 wake_up(&session->leadconn->ehwait);
7996a778
MC
1305 }
1306 spin_unlock_bh(&session->lock);
1307}
1308EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1309
1310int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1311{
75613521
MC
1312 struct iscsi_cls_session *cls_session;
1313 struct iscsi_session *session;
1314 struct iscsi_conn *conn;
1315
1316 cls_session = starget_to_session(scsi_target(sc->device));
1317 session = cls_session->dd_data;
1318 conn = session->leadconn;
7996a778 1319
bc436b27 1320 mutex_lock(&session->eh_mutex);
7996a778
MC
1321 spin_lock_bh(&session->lock);
1322 if (session->state == ISCSI_STATE_TERMINATE) {
1323failed:
1324 debug_scsi("failing host reset: session terminated "
d6e24d1c 1325 "[CID %d age %d]\n", conn->id, session->age);
7996a778 1326 spin_unlock_bh(&session->lock);
bc436b27 1327 mutex_unlock(&session->eh_mutex);
7996a778
MC
1328 return FAILED;
1329 }
1330
7996a778 1331 spin_unlock_bh(&session->lock);
bc436b27 1332 mutex_unlock(&session->eh_mutex);
7996a778
MC
1333 /*
1334 * we drop the lock here but the leadconn cannot be destoyed while
1335 * we are in the scsi eh
1336 */
843c0a8a 1337 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778
MC
1338
1339 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1340 wait_event_interruptible(conn->ehwait,
1341 session->state == ISCSI_STATE_TERMINATE ||
1342 session->state == ISCSI_STATE_LOGGED_IN ||
656cffc9 1343 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
1344 if (signal_pending(current))
1345 flush_signals(current);
1346
bc436b27 1347 mutex_lock(&session->eh_mutex);
7996a778
MC
1348 spin_lock_bh(&session->lock);
1349 if (session->state == ISCSI_STATE_LOGGED_IN)
322d739d
MC
1350 iscsi_session_printk(KERN_INFO, session,
1351 "host reset succeeded\n");
7996a778
MC
1352 else
1353 goto failed;
1354 spin_unlock_bh(&session->lock);
bc436b27 1355 mutex_unlock(&session->eh_mutex);
7996a778
MC
1356 return SUCCESS;
1357}
1358EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1359
843c0a8a 1360static void iscsi_tmf_timedout(unsigned long data)
7996a778 1361{
843c0a8a 1362 struct iscsi_conn *conn = (struct iscsi_conn *)data;
7996a778
MC
1363 struct iscsi_session *session = conn->session;
1364
1365 spin_lock(&session->lock);
843c0a8a
MC
1366 if (conn->tmf_state == TMF_QUEUED) {
1367 conn->tmf_state = TMF_TIMEDOUT;
1368 debug_scsi("tmf timedout\n");
7996a778
MC
1369 /* unblock eh_abort() */
1370 wake_up(&conn->ehwait);
1371 }
1372 spin_unlock(&session->lock);
1373}
1374
9c19a7d0 1375static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
f6d5180c
MC
1376 struct iscsi_tm *hdr, int age,
1377 int timeout)
7996a778 1378{
7996a778 1379 struct iscsi_session *session = conn->session;
9c19a7d0 1380 struct iscsi_task *task;
7996a778 1381
9c19a7d0 1382 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
843c0a8a 1383 NULL, 0);
9c19a7d0 1384 if (!task) {
6724add1 1385 spin_unlock_bh(&session->lock);
7996a778 1386 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
843c0a8a
MC
1387 spin_lock_bh(&session->lock);
1388 debug_scsi("tmf exec failure\n");
77a23c21 1389 return -EPERM;
7996a778 1390 }
843c0a8a 1391 conn->tmfcmd_pdus_cnt++;
f6d5180c 1392 conn->tmf_timer.expires = timeout * HZ + jiffies;
843c0a8a
MC
1393 conn->tmf_timer.function = iscsi_tmf_timedout;
1394 conn->tmf_timer.data = (unsigned long)conn;
1395 add_timer(&conn->tmf_timer);
1396 debug_scsi("tmf set timeout\n");
7996a778 1397
7996a778 1398 spin_unlock_bh(&session->lock);
6724add1 1399 mutex_unlock(&session->eh_mutex);
7996a778
MC
1400
1401 /*
1402 * block eh thread until:
1403 *
843c0a8a
MC
1404 * 1) tmf response
1405 * 2) tmf timeout
7996a778
MC
1406 * 3) session is terminated or restarted or userspace has
1407 * given up on recovery
1408 */
843c0a8a 1409 wait_event_interruptible(conn->ehwait, age != session->age ||
7996a778 1410 session->state != ISCSI_STATE_LOGGED_IN ||
843c0a8a 1411 conn->tmf_state != TMF_QUEUED);
7996a778
MC
1412 if (signal_pending(current))
1413 flush_signals(current);
843c0a8a
MC
1414 del_timer_sync(&conn->tmf_timer);
1415
6724add1 1416 mutex_lock(&session->eh_mutex);
77a23c21 1417 spin_lock_bh(&session->lock);
9c19a7d0 1418 /* if the session drops it will clean up the task */
843c0a8a
MC
1419 if (age != session->age ||
1420 session->state != ISCSI_STATE_LOGGED_IN)
1421 return -ENOTCONN;
7996a778
MC
1422 return 0;
1423}
1424
843c0a8a
MC
1425/*
1426 * Fail commands. session lock held and recv side suspended and xmit
1427 * thread flushed
1428 */
6eabafbe
MC
1429static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1430 int error)
843c0a8a 1431{
9c19a7d0 1432 struct iscsi_task *task, *tmp;
843c0a8a 1433
9c19a7d0
MC
1434 if (conn->task && (conn->task->sc->device->lun == lun || lun == -1))
1435 conn->task = NULL;
843c0a8a
MC
1436
1437 /* flush pending */
9c19a7d0
MC
1438 list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) {
1439 if (lun == task->sc->device->lun || lun == -1) {
843c0a8a 1440 debug_scsi("failing pending sc %p itt 0x%x\n",
9c19a7d0
MC
1441 task->sc, task->itt);
1442 fail_command(conn, task, error << 16);
843c0a8a
MC
1443 }
1444 }
1445
9c19a7d0
MC
1446 list_for_each_entry_safe(task, tmp, &conn->requeue, running) {
1447 if (lun == task->sc->device->lun || lun == -1) {
843c0a8a 1448 debug_scsi("failing requeued sc %p itt 0x%x\n",
9c19a7d0
MC
1449 task->sc, task->itt);
1450 fail_command(conn, task, error << 16);
843c0a8a
MC
1451 }
1452 }
1453
1454 /* fail all other running */
9c19a7d0
MC
1455 list_for_each_entry_safe(task, tmp, &conn->run_list, running) {
1456 if (lun == task->sc->device->lun || lun == -1) {
843c0a8a 1457 debug_scsi("failing in progress sc %p itt 0x%x\n",
9c19a7d0
MC
1458 task->sc, task->itt);
1459 fail_command(conn, task, DID_BUS_BUSY << 16);
843c0a8a
MC
1460 }
1461 }
1462}
1463
b40977d9 1464void iscsi_suspend_tx(struct iscsi_conn *conn)
6724add1
MC
1465{
1466 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
052d0144
MC
1467 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1468 scsi_flush_work(conn->session->host);
6724add1 1469}
b40977d9 1470EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
6724add1
MC
1471
1472static void iscsi_start_tx(struct iscsi_conn *conn)
1473{
1474 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
052d0144
MC
1475 if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD))
1476 scsi_queue_work(conn->session->host, &conn->xmitwork);
6724add1
MC
1477}
1478
f6d5180c
MC
1479static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1480{
1481 struct iscsi_cls_session *cls_session;
1482 struct iscsi_session *session;
1483 struct iscsi_conn *conn;
1484 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1485
1486 cls_session = starget_to_session(scsi_target(scmd->device));
75613521 1487 session = cls_session->dd_data;
f6d5180c
MC
1488
1489 debug_scsi("scsi cmd %p timedout\n", scmd);
1490
1491 spin_lock(&session->lock);
1492 if (session->state != ISCSI_STATE_LOGGED_IN) {
1493 /*
1494 * We are probably in the middle of iscsi recovery so let
1495 * that complete and handle the error.
1496 */
1497 rc = EH_RESET_TIMER;
1498 goto done;
1499 }
1500
1501 conn = session->leadconn;
1502 if (!conn) {
1503 /* In the middle of shuting down */
1504 rc = EH_RESET_TIMER;
1505 goto done;
1506 }
1507
1508 if (!conn->recv_timeout && !conn->ping_timeout)
1509 goto done;
1510 /*
1511 * if the ping timedout then we are in the middle of cleaning up
1512 * and can let the iscsi eh handle it
1513 */
1514 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1515 (conn->ping_timeout * HZ), jiffies))
1516 rc = EH_RESET_TIMER;
1517 /*
1518 * if we are about to check the transport then give the command
1519 * more time
1520 */
1521 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1522 jiffies))
1523 rc = EH_RESET_TIMER;
1524 /* if in the middle of checking the transport then give us more time */
9c19a7d0 1525 if (conn->ping_task)
f6d5180c
MC
1526 rc = EH_RESET_TIMER;
1527done:
1528 spin_unlock(&session->lock);
1529 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1530 return rc;
1531}
1532
1533static void iscsi_check_transport_timeouts(unsigned long data)
1534{
1535 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1536 struct iscsi_session *session = conn->session;
4cf10435 1537 unsigned long recv_timeout, next_timeout = 0, last_recv;
f6d5180c
MC
1538
1539 spin_lock(&session->lock);
1540 if (session->state != ISCSI_STATE_LOGGED_IN)
1541 goto done;
1542
4cf10435
MC
1543 recv_timeout = conn->recv_timeout;
1544 if (!recv_timeout)
f6d5180c
MC
1545 goto done;
1546
4cf10435 1547 recv_timeout *= HZ;
f6d5180c 1548 last_recv = conn->last_recv;
9c19a7d0 1549 if (conn->ping_task &&
4cf10435 1550 time_before_eq(conn->last_ping + (conn->ping_timeout * HZ),
f6d5180c 1551 jiffies)) {
322d739d
MC
1552 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1553 "expired, last rx %lu, last ping %lu, "
1554 "now %lu\n", conn->ping_timeout, last_recv,
1555 conn->last_ping, jiffies);
f6d5180c
MC
1556 spin_unlock(&session->lock);
1557 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1558 return;
1559 }
1560
4cf10435 1561 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
c8611f97
MC
1562 /* send a ping to try to provoke some traffic */
1563 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1564 iscsi_send_nopout(conn, NULL);
4cf10435 1565 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
ad294e9c 1566 } else
4cf10435 1567 next_timeout = last_recv + recv_timeout;
f6d5180c 1568
ad294e9c
MC
1569 debug_scsi("Setting next tmo %lu\n", next_timeout);
1570 mod_timer(&conn->transport_timer, next_timeout);
f6d5180c
MC
1571done:
1572 spin_unlock(&session->lock);
1573}
1574
9c19a7d0 1575static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
843c0a8a
MC
1576 struct iscsi_tm *hdr)
1577{
1578 memset(hdr, 0, sizeof(*hdr));
1579 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1580 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1581 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
9c19a7d0
MC
1582 memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
1583 hdr->rtt = task->hdr->itt;
1584 hdr->refcmdsn = task->hdr->cmdsn;
843c0a8a
MC
1585}
1586
7996a778
MC
1587int iscsi_eh_abort(struct scsi_cmnd *sc)
1588{
75613521
MC
1589 struct iscsi_cls_session *cls_session;
1590 struct iscsi_session *session;
f47f2cf5 1591 struct iscsi_conn *conn;
9c19a7d0 1592 struct iscsi_task *task;
843c0a8a
MC
1593 struct iscsi_tm *hdr;
1594 int rc, age;
7996a778 1595
75613521
MC
1596 cls_session = starget_to_session(scsi_target(sc->device));
1597 session = cls_session->dd_data;
1598
6724add1
MC
1599 mutex_lock(&session->eh_mutex);
1600 spin_lock_bh(&session->lock);
f47f2cf5
MC
1601 /*
1602 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1603 * got the command.
1604 */
1605 if (!sc->SCp.ptr) {
1606 debug_scsi("sc never reached iscsi layer or it completed.\n");
6724add1
MC
1607 spin_unlock_bh(&session->lock);
1608 mutex_unlock(&session->eh_mutex);
f47f2cf5
MC
1609 return SUCCESS;
1610 }
1611
7996a778
MC
1612 /*
1613 * If we are not logged in or we have started a new session
1614 * then let the host reset code handle this
1615 */
843c0a8a
MC
1616 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1617 sc->SCp.phase != session->age) {
1618 spin_unlock_bh(&session->lock);
1619 mutex_unlock(&session->eh_mutex);
1620 return FAILED;
1621 }
1622
1623 conn = session->leadconn;
1624 conn->eh_abort_cnt++;
1625 age = session->age;
1626
9c19a7d0
MC
1627 task = (struct iscsi_task *)sc->SCp.ptr;
1628 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt);
7996a778 1629
9c19a7d0
MC
1630 /* task completed before time out */
1631 if (!task->sc) {
7ea8b828 1632 debug_scsi("sc completed while abort in progress\n");
77a23c21 1633 goto success;
7ea8b828 1634 }
7996a778 1635
9c19a7d0
MC
1636 if (task->state == ISCSI_TASK_PENDING) {
1637 fail_command(conn, task, DID_ABORT << 16);
77a23c21
MC
1638 goto success;
1639 }
7996a778 1640
843c0a8a
MC
1641 /* only have one tmf outstanding at a time */
1642 if (conn->tmf_state != TMF_INITIAL)
7996a778 1643 goto failed;
843c0a8a 1644 conn->tmf_state = TMF_QUEUED;
7996a778 1645
843c0a8a 1646 hdr = &conn->tmhdr;
9c19a7d0 1647 iscsi_prep_abort_task_pdu(task, hdr);
843c0a8a 1648
9c19a7d0 1649 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
843c0a8a
MC
1650 rc = FAILED;
1651 goto failed;
1652 }
1653
1654 switch (conn->tmf_state) {
1655 case TMF_SUCCESS:
77a23c21 1656 spin_unlock_bh(&session->lock);
913e5bf4
MC
1657 /*
1658 * stop tx side incase the target had sent a abort rsp but
1659 * the initiator was still writing out data.
1660 */
6724add1 1661 iscsi_suspend_tx(conn);
77a23c21 1662 /*
913e5bf4
MC
1663 * we do not stop the recv side because targets have been
1664 * good and have never sent us a successful tmf response
1665 * then sent more data for the cmd.
77a23c21 1666 */
77a23c21 1667 spin_lock(&session->lock);
9c19a7d0 1668 fail_command(conn, task, DID_ABORT << 16);
843c0a8a 1669 conn->tmf_state = TMF_INITIAL;
77a23c21 1670 spin_unlock(&session->lock);
6724add1 1671 iscsi_start_tx(conn);
77a23c21 1672 goto success_unlocked;
843c0a8a
MC
1673 case TMF_TIMEDOUT:
1674 spin_unlock_bh(&session->lock);
1675 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1676 goto failed_unlocked;
1677 case TMF_NOT_FOUND:
1678 if (!sc->SCp.ptr) {
1679 conn->tmf_state = TMF_INITIAL;
9c19a7d0 1680 /* task completed before tmf abort response */
7ea8b828 1681 debug_scsi("sc completed while abort in progress\n");
77a23c21 1682 goto success;
7ea8b828
MC
1683 }
1684 /* fall through */
1685 default:
843c0a8a
MC
1686 conn->tmf_state = TMF_INITIAL;
1687 goto failed;
7996a778
MC
1688 }
1689
77a23c21 1690success:
7996a778 1691 spin_unlock_bh(&session->lock);
77a23c21 1692success_unlocked:
9c19a7d0 1693 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt);
6724add1 1694 mutex_unlock(&session->eh_mutex);
7996a778
MC
1695 return SUCCESS;
1696
1697failed:
1698 spin_unlock_bh(&session->lock);
77a23c21 1699failed_unlocked:
843c0a8a 1700 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
9c19a7d0 1701 task ? task->itt : 0);
6724add1 1702 mutex_unlock(&session->eh_mutex);
7996a778
MC
1703 return FAILED;
1704}
1705EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1706
843c0a8a
MC
1707static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1708{
1709 memset(hdr, 0, sizeof(*hdr));
1710 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1711 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1712 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1713 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
f6d5180c 1714 hdr->rtt = RESERVED_ITT;
843c0a8a
MC
1715}
1716
1717int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1718{
75613521
MC
1719 struct iscsi_cls_session *cls_session;
1720 struct iscsi_session *session;
843c0a8a
MC
1721 struct iscsi_conn *conn;
1722 struct iscsi_tm *hdr;
1723 int rc = FAILED;
1724
75613521
MC
1725 cls_session = starget_to_session(scsi_target(sc->device));
1726 session = cls_session->dd_data;
1727
843c0a8a
MC
1728 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1729
1730 mutex_lock(&session->eh_mutex);
1731 spin_lock_bh(&session->lock);
1732 /*
1733 * Just check if we are not logged in. We cannot check for
1734 * the phase because the reset could come from a ioctl.
1735 */
1736 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1737 goto unlock;
1738 conn = session->leadconn;
1739
1740 /* only have one tmf outstanding at a time */
1741 if (conn->tmf_state != TMF_INITIAL)
1742 goto unlock;
1743 conn->tmf_state = TMF_QUEUED;
1744
1745 hdr = &conn->tmhdr;
1746 iscsi_prep_lun_reset_pdu(sc, hdr);
1747
9c19a7d0 1748 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
f6d5180c 1749 session->lu_reset_timeout)) {
843c0a8a
MC
1750 rc = FAILED;
1751 goto unlock;
1752 }
1753
1754 switch (conn->tmf_state) {
1755 case TMF_SUCCESS:
1756 break;
1757 case TMF_TIMEDOUT:
1758 spin_unlock_bh(&session->lock);
1759 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1760 goto done;
1761 default:
1762 conn->tmf_state = TMF_INITIAL;
1763 goto unlock;
1764 }
1765
1766 rc = SUCCESS;
1767 spin_unlock_bh(&session->lock);
1768
1769 iscsi_suspend_tx(conn);
913e5bf4 1770
843c0a8a 1771 spin_lock(&session->lock);
6eabafbe 1772 fail_all_commands(conn, sc->device->lun, DID_ERROR);
843c0a8a
MC
1773 conn->tmf_state = TMF_INITIAL;
1774 spin_unlock(&session->lock);
843c0a8a
MC
1775
1776 iscsi_start_tx(conn);
1777 goto done;
1778
1779unlock:
1780 spin_unlock_bh(&session->lock);
1781done:
1782 debug_scsi("iscsi_eh_device_reset %s\n",
1783 rc == SUCCESS ? "SUCCESS" : "FAILED");
1784 mutex_unlock(&session->eh_mutex);
1785 return rc;
1786}
1787EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1788
6320377f
OK
1789/*
1790 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1791 * should be accessed via kfifo_{get,put} on q->queue.
1792 * Optionally, the caller can obtain the array of object pointers
1793 * by passing in a non-NULL @items pointer
1794 */
7996a778 1795int
6320377f 1796iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
7996a778 1797{
6320377f 1798 int i, num_arrays = 1;
7996a778 1799
6320377f 1800 memset(q, 0, sizeof(*q));
7996a778
MC
1801
1802 q->max = max;
6320377f
OK
1803
1804 /* If the user passed an items pointer, he wants a copy of
1805 * the array. */
1806 if (items)
1807 num_arrays++;
1808 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1809 if (q->pool == NULL)
1810 goto enomem;
7996a778
MC
1811
1812 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1813 GFP_KERNEL, NULL);
6320377f
OK
1814 if (q->queue == ERR_PTR(-ENOMEM))
1815 goto enomem;
7996a778
MC
1816
1817 for (i = 0; i < max; i++) {
6320377f 1818 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
7996a778 1819 if (q->pool[i] == NULL) {
6320377f
OK
1820 q->max = i;
1821 goto enomem;
7996a778 1822 }
7996a778
MC
1823 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1824 }
6320377f
OK
1825
1826 if (items) {
1827 *items = q->pool + max;
1828 memcpy(*items, q->pool, max * sizeof(void *));
1829 }
1830
7996a778 1831 return 0;
6320377f
OK
1832
1833enomem:
1834 iscsi_pool_free(q);
1835 return -ENOMEM;
7996a778
MC
1836}
1837EXPORT_SYMBOL_GPL(iscsi_pool_init);
1838
6320377f 1839void iscsi_pool_free(struct iscsi_pool *q)
7996a778
MC
1840{
1841 int i;
1842
1843 for (i = 0; i < q->max; i++)
6320377f
OK
1844 kfree(q->pool[i]);
1845 if (q->pool)
1846 kfree(q->pool);
7996a778
MC
1847}
1848EXPORT_SYMBOL_GPL(iscsi_pool_free);
1849
a4804cd6
MC
1850/**
1851 * iscsi_host_add - add host to system
1852 * @shost: scsi host
1853 * @pdev: parent device
1854 *
1855 * This should be called by partial offload and software iscsi drivers
1856 * to add a host to the system.
1857 */
1858int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
1859{
1860 return scsi_add_host(shost, pdev);
1861}
1862EXPORT_SYMBOL_GPL(iscsi_host_add);
1863
1864/**
1865 * iscsi_host_alloc - allocate a host and driver data
1866 * @sht: scsi host template
1867 * @dd_data_size: driver host data size
1868 * @qdepth: default device queue depth
1869 *
1870 * This should be called by partial offload and software iscsi drivers.
1871 * To access the driver specific memory use the iscsi_host_priv() macro.
1872 */
1873struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
1874 int dd_data_size, uint16_t qdepth)
75613521 1875{
a4804cd6
MC
1876 struct Scsi_Host *shost;
1877
1878 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
1879 if (!shost)
1880 return NULL;
1881 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
1882
75613521
MC
1883 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1884 if (qdepth != 0)
1885 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1886 "Queue depth must be between 1 and %d.\n",
1887 qdepth, ISCSI_MAX_CMD_PER_LUN);
1888 qdepth = ISCSI_DEF_CMD_PER_LUN;
1889 }
75613521 1890 shost->cmd_per_lun = qdepth;
a4804cd6
MC
1891 return shost;
1892}
1893EXPORT_SYMBOL_GPL(iscsi_host_alloc);
1894
1895/**
1896 * iscsi_host_remove - remove host and sessions
1897 * @shost: scsi host
1898 *
1899 * This will also remove any sessions attached to the host, but if userspace
1900 * is managing the session at the same time this will break. TODO: add
1901 * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
1902 * does not remove the memory from under us.
1903 */
1904void iscsi_host_remove(struct Scsi_Host *shost)
1905{
1906 iscsi_host_for_each_session(shost, iscsi_session_teardown);
1907 scsi_remove_host(shost);
75613521 1908}
a4804cd6 1909EXPORT_SYMBOL_GPL(iscsi_host_remove);
7996a778 1910
a4804cd6 1911void iscsi_host_free(struct Scsi_Host *shost)
75613521
MC
1912{
1913 struct iscsi_host *ihost = shost_priv(shost);
7996a778 1914
75613521
MC
1915 kfree(ihost->netdev);
1916 kfree(ihost->hwaddress);
1917 kfree(ihost->initiatorname);
a4804cd6 1918 scsi_host_put(shost);
75613521 1919}
a4804cd6 1920EXPORT_SYMBOL_GPL(iscsi_host_free);
7996a778
MC
1921
1922/**
1923 * iscsi_session_setup - create iscsi cls session and host and session
7996a778 1924 * @iscsit: iscsi transport template
75613521
MC
1925 * @shost: scsi host
1926 * @cmds_max: session can queue
9c19a7d0 1927 * @cmd_task_size: LLD task private data size
7996a778 1928 * @initial_cmdsn: initial CmdSN
7996a778
MC
1929 *
1930 * This can be used by software iscsi_transports that allocate
1931 * a session per scsi host.
3cf7b233
MC
1932 *
1933 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
1934 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
1935 * for nop handling and login/logout requests.
75613521 1936 */
7996a778 1937struct iscsi_cls_session *
75613521 1938iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
3cf7b233 1939 uint16_t cmds_max, int cmd_task_size,
7970634b 1940 uint32_t initial_cmdsn, unsigned int id)
7996a778 1941{
7996a778
MC
1942 struct iscsi_session *session;
1943 struct iscsi_cls_session *cls_session;
3cf7b233 1944 int cmd_i, scsi_cmds, total_cmds = cmds_max;
3e5c28ad 1945 /*
3cf7b233
MC
1946 * The iscsi layer needs some tasks for nop handling and tmfs,
1947 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
1948 * + 1 command for scsi IO.
3e5c28ad 1949 */
3cf7b233
MC
1950 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
1951 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1952 "must be a power of two that is at least %d.\n",
1953 total_cmds, ISCSI_TOTAL_CMDS_MIN);
1954 return NULL;
1955 }
1956
1957 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
1958 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1959 "must be a power of 2 less than or equal to %d.\n",
1960 cmds_max, ISCSI_TOTAL_CMDS_MAX);
1961 total_cmds = ISCSI_TOTAL_CMDS_MAX;
1962 }
1963
1964 if (!is_power_of_2(total_cmds)) {
1965 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
1966 "must be a power of 2.\n", total_cmds);
1967 total_cmds = rounddown_pow_of_two(total_cmds);
1968 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
1969 return NULL;
1970 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
1971 total_cmds);
1548271e 1972 }
3cf7b233 1973 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
1548271e 1974
5d91e209
MC
1975 cls_session = iscsi_alloc_session(shost, iscsit,
1976 sizeof(struct iscsi_session));
75613521 1977 if (!cls_session)
7996a778 1978 return NULL;
75613521
MC
1979 session = cls_session->dd_data;
1980 session->cls_session = cls_session;
7996a778
MC
1981 session->host = shost;
1982 session->state = ISCSI_STATE_FREE;
f6d5180c 1983 session->fast_abort = 1;
4cd49ea1
MC
1984 session->lu_reset_timeout = 15;
1985 session->abort_timeout = 10;
3cf7b233
MC
1986 session->scsi_cmds_max = scsi_cmds;
1987 session->cmds_max = total_cmds;
e0726407 1988 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
7996a778
MC
1989 session->exp_cmdsn = initial_cmdsn + 1;
1990 session->max_cmdsn = initial_cmdsn + 1;
1991 session->max_r2t = 1;
1992 session->tt = iscsit;
6724add1 1993 mutex_init(&session->eh_mutex);
75613521 1994 spin_lock_init(&session->lock);
7996a778
MC
1995
1996 /* initialize SCSI PDU commands pool */
1997 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1998 (void***)&session->cmds,
9c19a7d0 1999 cmd_task_size + sizeof(struct iscsi_task)))
7996a778
MC
2000 goto cmdpool_alloc_fail;
2001
2002 /* pre-format cmds pool with ITT */
2003 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
9c19a7d0 2004 struct iscsi_task *task = session->cmds[cmd_i];
7996a778 2005
9c19a7d0
MC
2006 if (cmd_task_size)
2007 task->dd_data = &task[1];
2008 task->itt = cmd_i;
2009 INIT_LIST_HEAD(&task->running);
7996a778
MC
2010 }
2011
f53a88da 2012 if (!try_module_get(iscsit->owner))
75613521 2013 goto module_get_fail;
7996a778 2014
7970634b 2015 if (iscsi_add_session(cls_session, id))
75613521 2016 goto cls_session_fail;
7996a778
MC
2017 return cls_session;
2018
2019cls_session_fail:
75613521
MC
2020 module_put(iscsit->owner);
2021module_get_fail:
6320377f 2022 iscsi_pool_free(&session->cmdpool);
7996a778 2023cmdpool_alloc_fail:
75613521 2024 iscsi_free_session(cls_session);
7996a778
MC
2025 return NULL;
2026}
2027EXPORT_SYMBOL_GPL(iscsi_session_setup);
2028
2029/**
2030 * iscsi_session_teardown - destroy session, host, and cls_session
75613521 2031 * @cls_session: iscsi session
7996a778 2032 *
75613521
MC
2033 * The driver must have called iscsi_remove_session before
2034 * calling this.
2035 */
7996a778
MC
2036void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2037{
75613521 2038 struct iscsi_session *session = cls_session->dd_data;
63f75cc8 2039 struct module *owner = cls_session->transport->owner;
7996a778 2040
6320377f 2041 iscsi_pool_free(&session->cmdpool);
7996a778 2042
b2c64167
MC
2043 kfree(session->password);
2044 kfree(session->password_in);
2045 kfree(session->username);
2046 kfree(session->username_in);
f3ff0c36 2047 kfree(session->targetname);
88dfd340
MC
2048 kfree(session->initiatorname);
2049 kfree(session->ifacename);
f3ff0c36 2050
75613521 2051 iscsi_destroy_session(cls_session);
63f75cc8 2052 module_put(owner);
7996a778
MC
2053}
2054EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2055
2056/**
2057 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2058 * @cls_session: iscsi_cls_session
5d91e209 2059 * @dd_size: private driver data size
7996a778 2060 * @conn_idx: cid
5d91e209 2061 */
7996a778 2062struct iscsi_cls_conn *
5d91e209
MC
2063iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2064 uint32_t conn_idx)
7996a778 2065{
75613521 2066 struct iscsi_session *session = cls_session->dd_data;
7996a778
MC
2067 struct iscsi_conn *conn;
2068 struct iscsi_cls_conn *cls_conn;
d36ab6f3 2069 char *data;
7996a778 2070
5d91e209
MC
2071 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2072 conn_idx);
7996a778
MC
2073 if (!cls_conn)
2074 return NULL;
2075 conn = cls_conn->dd_data;
5d91e209 2076 memset(conn, 0, sizeof(*conn) + dd_size);
7996a778 2077
5d91e209 2078 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
7996a778
MC
2079 conn->session = session;
2080 conn->cls_conn = cls_conn;
2081 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2082 conn->id = conn_idx;
2083 conn->exp_statsn = 0;
843c0a8a 2084 conn->tmf_state = TMF_INITIAL;
f6d5180c
MC
2085
2086 init_timer(&conn->transport_timer);
2087 conn->transport_timer.data = (unsigned long)conn;
2088 conn->transport_timer.function = iscsi_check_transport_timeouts;
2089
7996a778
MC
2090 INIT_LIST_HEAD(&conn->run_list);
2091 INIT_LIST_HEAD(&conn->mgmt_run_list);
843c0a8a 2092 INIT_LIST_HEAD(&conn->mgmtqueue);
b6c395ed 2093 INIT_LIST_HEAD(&conn->xmitqueue);
843c0a8a 2094 INIT_LIST_HEAD(&conn->requeue);
c4028958 2095 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
7996a778 2096
9c19a7d0 2097 /* allocate login_task used for the login/text sequences */
7996a778 2098 spin_lock_bh(&session->lock);
3e5c28ad 2099 if (!__kfifo_get(session->cmdpool.queue,
9c19a7d0 2100 (void*)&conn->login_task,
7996a778
MC
2101 sizeof(void*))) {
2102 spin_unlock_bh(&session->lock);
9c19a7d0 2103 goto login_task_alloc_fail;
7996a778
MC
2104 }
2105 spin_unlock_bh(&session->lock);
2106
bf32ed33 2107 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
d36ab6f3 2108 if (!data)
9c19a7d0
MC
2109 goto login_task_data_alloc_fail;
2110 conn->login_task->data = conn->data = data;
d36ab6f3 2111
843c0a8a 2112 init_timer(&conn->tmf_timer);
7996a778
MC
2113 init_waitqueue_head(&conn->ehwait);
2114
2115 return cls_conn;
2116
9c19a7d0
MC
2117login_task_data_alloc_fail:
2118 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
d36ab6f3 2119 sizeof(void*));
9c19a7d0 2120login_task_alloc_fail:
7996a778
MC
2121 iscsi_destroy_conn(cls_conn);
2122 return NULL;
2123}
2124EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2125
2126/**
2127 * iscsi_conn_teardown - teardown iscsi connection
2128 * cls_conn: iscsi class connection
2129 *
2130 * TODO: we may need to make this into a two step process
2131 * like scsi-mls remove + put host
2132 */
2133void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2134{
2135 struct iscsi_conn *conn = cls_conn->dd_data;
2136 struct iscsi_session *session = conn->session;
2137 unsigned long flags;
2138
f6d5180c
MC
2139 del_timer_sync(&conn->transport_timer);
2140
7996a778
MC
2141 spin_lock_bh(&session->lock);
2142 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2143 if (session->leadconn == conn) {
2144 /*
2145 * leading connection? then give up on recovery.
2146 */
2147 session->state = ISCSI_STATE_TERMINATE;
2148 wake_up(&conn->ehwait);
2149 }
2150 spin_unlock_bh(&session->lock);
2151
7996a778
MC
2152 /*
2153 * Block until all in-progress commands for this connection
2154 * time out or fail.
2155 */
2156 for (;;) {
2157 spin_lock_irqsave(session->host->host_lock, flags);
2158 if (!session->host->host_busy) { /* OK for ERL == 0 */
2159 spin_unlock_irqrestore(session->host->host_lock, flags);
2160 break;
2161 }
2162 spin_unlock_irqrestore(session->host->host_lock, flags);
2163 msleep_interruptible(500);
322d739d
MC
2164 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2165 "host_busy %d host_failed %d\n",
2166 session->host->host_busy,
2167 session->host->host_failed);
7996a778
MC
2168 /*
2169 * force eh_abort() to unblock
2170 */
2171 wake_up(&conn->ehwait);
2172 }
2173
779ea120 2174 /* flush queued up work because we free the connection below */
843c0a8a 2175 iscsi_suspend_tx(conn);
779ea120 2176
7996a778 2177 spin_lock_bh(&session->lock);
c8dc1e52 2178 kfree(conn->data);
f3ff0c36 2179 kfree(conn->persistent_address);
9c19a7d0 2180 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
7996a778 2181 sizeof(void*));
e0726407 2182 if (session->leadconn == conn)
7996a778 2183 session->leadconn = NULL;
7996a778
MC
2184 spin_unlock_bh(&session->lock);
2185
7996a778
MC
2186 iscsi_destroy_conn(cls_conn);
2187}
2188EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2189
2190int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2191{
2192 struct iscsi_conn *conn = cls_conn->dd_data;
2193 struct iscsi_session *session = conn->session;
2194
ffd0436e 2195 if (!session) {
322d739d
MC
2196 iscsi_conn_printk(KERN_ERR, conn,
2197 "can't start unbound connection\n");
7996a778
MC
2198 return -EPERM;
2199 }
2200
db98ccde
MC
2201 if ((session->imm_data_en || !session->initial_r2t_en) &&
2202 session->first_burst > session->max_burst) {
322d739d
MC
2203 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2204 "first_burst %d max_burst %d\n",
2205 session->first_burst, session->max_burst);
ffd0436e
MC
2206 return -EINVAL;
2207 }
2208
f6d5180c 2209 if (conn->ping_timeout && !conn->recv_timeout) {
322d739d
MC
2210 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2211 "zero. Using 5 seconds\n.");
f6d5180c
MC
2212 conn->recv_timeout = 5;
2213 }
2214
2215 if (conn->recv_timeout && !conn->ping_timeout) {
322d739d
MC
2216 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2217 "zero. Using 5 seconds.\n");
f6d5180c
MC
2218 conn->ping_timeout = 5;
2219 }
2220
7996a778
MC
2221 spin_lock_bh(&session->lock);
2222 conn->c_stage = ISCSI_CONN_STARTED;
2223 session->state = ISCSI_STATE_LOGGED_IN;
e0726407 2224 session->queued_cmdsn = session->cmdsn;
7996a778 2225
f6d5180c
MC
2226 conn->last_recv = jiffies;
2227 conn->last_ping = jiffies;
2228 if (conn->recv_timeout && conn->ping_timeout)
2229 mod_timer(&conn->transport_timer,
2230 jiffies + (conn->recv_timeout * HZ));
2231
7996a778
MC
2232 switch(conn->stop_stage) {
2233 case STOP_CONN_RECOVER:
2234 /*
2235 * unblock eh_abort() if it is blocked. re-try all
2236 * commands after successful recovery
2237 */
7996a778 2238 conn->stop_stage = 0;
843c0a8a 2239 conn->tmf_state = TMF_INITIAL;
7996a778 2240 session->age++;
8b1d0343
MC
2241 if (session->age == 16)
2242 session->age = 0;
6eabafbe 2243 break;
7996a778 2244 case STOP_CONN_TERM:
7996a778 2245 conn->stop_stage = 0;
7996a778
MC
2246 break;
2247 default:
2248 break;
2249 }
2250 spin_unlock_bh(&session->lock);
2251
75613521 2252 iscsi_unblock_session(session->cls_session);
6eabafbe 2253 wake_up(&conn->ehwait);
7996a778
MC
2254 return 0;
2255}
2256EXPORT_SYMBOL_GPL(iscsi_conn_start);
2257
2258static void
2259flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2260{
9c19a7d0 2261 struct iscsi_task *task, *tmp;
7996a778
MC
2262
2263 /* handle pending */
9c19a7d0
MC
2264 list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) {
2265 debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt);
2266 /* release ref from prep task */
2267 __iscsi_put_task(task);
7996a778
MC
2268 }
2269
2270 /* handle running */
9c19a7d0
MC
2271 list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) {
2272 debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt);
2273 /* release ref from prep task */
2274 __iscsi_put_task(task);
7996a778
MC
2275 }
2276
9c19a7d0 2277 conn->task = NULL;
7996a778
MC
2278}
2279
656cffc9
MC
2280static void iscsi_start_session_recovery(struct iscsi_session *session,
2281 struct iscsi_conn *conn, int flag)
7996a778 2282{
ed2abc7f
MC
2283 int old_stop_stage;
2284
f6d5180c
MC
2285 del_timer_sync(&conn->transport_timer);
2286
6724add1 2287 mutex_lock(&session->eh_mutex);
7996a778 2288 spin_lock_bh(&session->lock);
ed2abc7f 2289 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778 2290 spin_unlock_bh(&session->lock);
6724add1
MC
2291 mutex_unlock(&session->eh_mutex);
2292 return;
2293 }
2294
ed2abc7f
MC
2295 /*
2296 * When this is called for the in_login state, we only want to clean
9c19a7d0 2297 * up the login task and connection. We do not need to block and set
67a61114 2298 * the recovery state again
ed2abc7f 2299 */
67a61114
MC
2300 if (flag == STOP_CONN_TERM)
2301 session->state = ISCSI_STATE_TERMINATE;
2302 else if (conn->stop_stage != STOP_CONN_RECOVER)
2303 session->state = ISCSI_STATE_IN_RECOVERY;
ed2abc7f
MC
2304
2305 old_stop_stage = conn->stop_stage;
7996a778 2306 conn->stop_stage = flag;
67a61114 2307 conn->c_stage = ISCSI_CONN_STOPPED;
7996a778 2308 spin_unlock_bh(&session->lock);
6724add1
MC
2309
2310 iscsi_suspend_tx(conn);
7996a778
MC
2311 /*
2312 * for connection level recovery we should not calculate
2313 * header digest. conn->hdr_size used for optimization
2314 * in hdr_extract() and will be re-negotiated at
2315 * set_param() time.
2316 */
2317 if (flag == STOP_CONN_RECOVER) {
2318 conn->hdrdgst_en = 0;
2319 conn->datadgst_en = 0;
656cffc9 2320 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114
MC
2321 old_stop_stage != STOP_CONN_RECOVER) {
2322 debug_scsi("blocking session\n");
75613521 2323 iscsi_block_session(session->cls_session);
67a61114 2324 }
7996a778 2325 }
656cffc9 2326
656cffc9
MC
2327 /*
2328 * flush queues.
2329 */
2330 spin_lock_bh(&session->lock);
6eabafbe
MC
2331 fail_all_commands(conn, -1,
2332 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
656cffc9
MC
2333 flush_control_queues(session, conn);
2334 spin_unlock_bh(&session->lock);
6724add1 2335 mutex_unlock(&session->eh_mutex);
7996a778 2336}
7996a778
MC
2337
2338void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2339{
2340 struct iscsi_conn *conn = cls_conn->dd_data;
2341 struct iscsi_session *session = conn->session;
2342
2343 switch (flag) {
2344 case STOP_CONN_RECOVER:
2345 case STOP_CONN_TERM:
2346 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 2347 break;
7996a778 2348 default:
322d739d
MC
2349 iscsi_conn_printk(KERN_ERR, conn,
2350 "invalid stop flag %d\n", flag);
7996a778
MC
2351 }
2352}
2353EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2354
2355int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2356 struct iscsi_cls_conn *cls_conn, int is_leading)
2357{
75613521 2358 struct iscsi_session *session = cls_session->dd_data;
98644047 2359 struct iscsi_conn *conn = cls_conn->dd_data;
7996a778 2360
7996a778 2361 spin_lock_bh(&session->lock);
7996a778
MC
2362 if (is_leading)
2363 session->leadconn = conn;
98644047 2364 spin_unlock_bh(&session->lock);
7996a778
MC
2365
2366 /*
2367 * Unblock xmitworker(), Login Phase will pass through.
2368 */
2369 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2370 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2371 return 0;
2372}
2373EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2374
a54a52ca
MC
2375
2376int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2377 enum iscsi_param param, char *buf, int buflen)
2378{
2379 struct iscsi_conn *conn = cls_conn->dd_data;
2380 struct iscsi_session *session = conn->session;
2381 uint32_t value;
2382
2383 switch(param) {
843c0a8a
MC
2384 case ISCSI_PARAM_FAST_ABORT:
2385 sscanf(buf, "%d", &session->fast_abort);
2386 break;
f6d5180c
MC
2387 case ISCSI_PARAM_ABORT_TMO:
2388 sscanf(buf, "%d", &session->abort_timeout);
2389 break;
2390 case ISCSI_PARAM_LU_RESET_TMO:
2391 sscanf(buf, "%d", &session->lu_reset_timeout);
2392 break;
2393 case ISCSI_PARAM_PING_TMO:
2394 sscanf(buf, "%d", &conn->ping_timeout);
2395 break;
2396 case ISCSI_PARAM_RECV_TMO:
2397 sscanf(buf, "%d", &conn->recv_timeout);
2398 break;
a54a52ca
MC
2399 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2400 sscanf(buf, "%d", &conn->max_recv_dlength);
2401 break;
2402 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2403 sscanf(buf, "%d", &conn->max_xmit_dlength);
2404 break;
2405 case ISCSI_PARAM_HDRDGST_EN:
2406 sscanf(buf, "%d", &conn->hdrdgst_en);
2407 break;
2408 case ISCSI_PARAM_DATADGST_EN:
2409 sscanf(buf, "%d", &conn->datadgst_en);
2410 break;
2411 case ISCSI_PARAM_INITIAL_R2T_EN:
2412 sscanf(buf, "%d", &session->initial_r2t_en);
2413 break;
2414 case ISCSI_PARAM_MAX_R2T:
2415 sscanf(buf, "%d", &session->max_r2t);
2416 break;
2417 case ISCSI_PARAM_IMM_DATA_EN:
2418 sscanf(buf, "%d", &session->imm_data_en);
2419 break;
2420 case ISCSI_PARAM_FIRST_BURST:
2421 sscanf(buf, "%d", &session->first_burst);
2422 break;
2423 case ISCSI_PARAM_MAX_BURST:
2424 sscanf(buf, "%d", &session->max_burst);
2425 break;
2426 case ISCSI_PARAM_PDU_INORDER_EN:
2427 sscanf(buf, "%d", &session->pdu_inorder_en);
2428 break;
2429 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2430 sscanf(buf, "%d", &session->dataseq_inorder_en);
2431 break;
2432 case ISCSI_PARAM_ERL:
2433 sscanf(buf, "%d", &session->erl);
2434 break;
2435 case ISCSI_PARAM_IFMARKER_EN:
2436 sscanf(buf, "%d", &value);
2437 BUG_ON(value);
2438 break;
2439 case ISCSI_PARAM_OFMARKER_EN:
2440 sscanf(buf, "%d", &value);
2441 BUG_ON(value);
2442 break;
2443 case ISCSI_PARAM_EXP_STATSN:
2444 sscanf(buf, "%u", &conn->exp_statsn);
2445 break;
b2c64167
MC
2446 case ISCSI_PARAM_USERNAME:
2447 kfree(session->username);
2448 session->username = kstrdup(buf, GFP_KERNEL);
2449 if (!session->username)
2450 return -ENOMEM;
2451 break;
2452 case ISCSI_PARAM_USERNAME_IN:
2453 kfree(session->username_in);
2454 session->username_in = kstrdup(buf, GFP_KERNEL);
2455 if (!session->username_in)
2456 return -ENOMEM;
2457 break;
2458 case ISCSI_PARAM_PASSWORD:
2459 kfree(session->password);
2460 session->password = kstrdup(buf, GFP_KERNEL);
2461 if (!session->password)
2462 return -ENOMEM;
2463 break;
2464 case ISCSI_PARAM_PASSWORD_IN:
2465 kfree(session->password_in);
2466 session->password_in = kstrdup(buf, GFP_KERNEL);
2467 if (!session->password_in)
2468 return -ENOMEM;
2469 break;
a54a52ca
MC
2470 case ISCSI_PARAM_TARGET_NAME:
2471 /* this should not change between logins */
2472 if (session->targetname)
2473 break;
2474
2475 session->targetname = kstrdup(buf, GFP_KERNEL);
2476 if (!session->targetname)
2477 return -ENOMEM;
2478 break;
2479 case ISCSI_PARAM_TPGT:
2480 sscanf(buf, "%d", &session->tpgt);
2481 break;
2482 case ISCSI_PARAM_PERSISTENT_PORT:
2483 sscanf(buf, "%d", &conn->persistent_port);
2484 break;
2485 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2486 /*
2487 * this is the address returned in discovery so it should
2488 * not change between logins.
2489 */
2490 if (conn->persistent_address)
2491 break;
2492
2493 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2494 if (!conn->persistent_address)
2495 return -ENOMEM;
2496 break;
88dfd340
MC
2497 case ISCSI_PARAM_IFACE_NAME:
2498 if (!session->ifacename)
2499 session->ifacename = kstrdup(buf, GFP_KERNEL);
2500 break;
2501 case ISCSI_PARAM_INITIATOR_NAME:
2502 if (!session->initiatorname)
2503 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2504 break;
a54a52ca
MC
2505 default:
2506 return -ENOSYS;
2507 }
2508
2509 return 0;
2510}
2511EXPORT_SYMBOL_GPL(iscsi_set_param);
2512
2513int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2514 enum iscsi_param param, char *buf)
2515{
75613521 2516 struct iscsi_session *session = cls_session->dd_data;
a54a52ca
MC
2517 int len;
2518
2519 switch(param) {
843c0a8a
MC
2520 case ISCSI_PARAM_FAST_ABORT:
2521 len = sprintf(buf, "%d\n", session->fast_abort);
2522 break;
f6d5180c
MC
2523 case ISCSI_PARAM_ABORT_TMO:
2524 len = sprintf(buf, "%d\n", session->abort_timeout);
2525 break;
2526 case ISCSI_PARAM_LU_RESET_TMO:
2527 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2528 break;
a54a52ca
MC
2529 case ISCSI_PARAM_INITIAL_R2T_EN:
2530 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2531 break;
2532 case ISCSI_PARAM_MAX_R2T:
2533 len = sprintf(buf, "%hu\n", session->max_r2t);
2534 break;
2535 case ISCSI_PARAM_IMM_DATA_EN:
2536 len = sprintf(buf, "%d\n", session->imm_data_en);
2537 break;
2538 case ISCSI_PARAM_FIRST_BURST:
2539 len = sprintf(buf, "%u\n", session->first_burst);
2540 break;
2541 case ISCSI_PARAM_MAX_BURST:
2542 len = sprintf(buf, "%u\n", session->max_burst);
2543 break;
2544 case ISCSI_PARAM_PDU_INORDER_EN:
2545 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2546 break;
2547 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2548 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2549 break;
2550 case ISCSI_PARAM_ERL:
2551 len = sprintf(buf, "%d\n", session->erl);
2552 break;
2553 case ISCSI_PARAM_TARGET_NAME:
2554 len = sprintf(buf, "%s\n", session->targetname);
2555 break;
2556 case ISCSI_PARAM_TPGT:
2557 len = sprintf(buf, "%d\n", session->tpgt);
2558 break;
b2c64167
MC
2559 case ISCSI_PARAM_USERNAME:
2560 len = sprintf(buf, "%s\n", session->username);
2561 break;
2562 case ISCSI_PARAM_USERNAME_IN:
2563 len = sprintf(buf, "%s\n", session->username_in);
2564 break;
2565 case ISCSI_PARAM_PASSWORD:
2566 len = sprintf(buf, "%s\n", session->password);
2567 break;
2568 case ISCSI_PARAM_PASSWORD_IN:
2569 len = sprintf(buf, "%s\n", session->password_in);
2570 break;
88dfd340
MC
2571 case ISCSI_PARAM_IFACE_NAME:
2572 len = sprintf(buf, "%s\n", session->ifacename);
2573 break;
2574 case ISCSI_PARAM_INITIATOR_NAME:
2575 if (!session->initiatorname)
2576 len = sprintf(buf, "%s\n", "unknown");
2577 else
2578 len = sprintf(buf, "%s\n", session->initiatorname);
2579 break;
a54a52ca
MC
2580 default:
2581 return -ENOSYS;
2582 }
2583
2584 return len;
2585}
2586EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2587
2588int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2589 enum iscsi_param param, char *buf)
2590{
2591 struct iscsi_conn *conn = cls_conn->dd_data;
2592 int len;
2593
2594 switch(param) {
f6d5180c
MC
2595 case ISCSI_PARAM_PING_TMO:
2596 len = sprintf(buf, "%u\n", conn->ping_timeout);
2597 break;
2598 case ISCSI_PARAM_RECV_TMO:
2599 len = sprintf(buf, "%u\n", conn->recv_timeout);
2600 break;
a54a52ca
MC
2601 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2602 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2603 break;
2604 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2605 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2606 break;
2607 case ISCSI_PARAM_HDRDGST_EN:
2608 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2609 break;
2610 case ISCSI_PARAM_DATADGST_EN:
2611 len = sprintf(buf, "%d\n", conn->datadgst_en);
2612 break;
2613 case ISCSI_PARAM_IFMARKER_EN:
2614 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2615 break;
2616 case ISCSI_PARAM_OFMARKER_EN:
2617 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2618 break;
2619 case ISCSI_PARAM_EXP_STATSN:
2620 len = sprintf(buf, "%u\n", conn->exp_statsn);
2621 break;
2622 case ISCSI_PARAM_PERSISTENT_PORT:
2623 len = sprintf(buf, "%d\n", conn->persistent_port);
2624 break;
2625 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2626 len = sprintf(buf, "%s\n", conn->persistent_address);
2627 break;
2628 default:
2629 return -ENOSYS;
2630 }
2631
2632 return len;
2633}
2634EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2635
0801c242
MC
2636int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2637 char *buf)
2638{
75613521 2639 struct iscsi_host *ihost = shost_priv(shost);
0801c242
MC
2640 int len;
2641
2642 switch (param) {
d8196ed2 2643 case ISCSI_HOST_PARAM_NETDEV_NAME:
75613521 2644 if (!ihost->netdev)
d8196ed2
MC
2645 len = sprintf(buf, "%s\n", "default");
2646 else
75613521 2647 len = sprintf(buf, "%s\n", ihost->netdev);
d8196ed2 2648 break;
0801c242 2649 case ISCSI_HOST_PARAM_HWADDRESS:
75613521 2650 if (!ihost->hwaddress)
0801c242
MC
2651 len = sprintf(buf, "%s\n", "default");
2652 else
75613521 2653 len = sprintf(buf, "%s\n", ihost->hwaddress);
0801c242 2654 break;
8ad5781a 2655 case ISCSI_HOST_PARAM_INITIATOR_NAME:
75613521 2656 if (!ihost->initiatorname)
8ad5781a
MC
2657 len = sprintf(buf, "%s\n", "unknown");
2658 else
75613521 2659 len = sprintf(buf, "%s\n", ihost->initiatorname);
8ad5781a 2660 break;
75613521
MC
2661 case ISCSI_HOST_PARAM_IPADDRESS:
2662 if (!strlen(ihost->local_address))
2663 len = sprintf(buf, "%s\n", "unknown");
2664 else
2665 len = sprintf(buf, "%s\n",
2666 ihost->local_address);
88dfd340 2667 break;
0801c242
MC
2668 default:
2669 return -ENOSYS;
2670 }
2671
2672 return len;
2673}
2674EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2675
2676int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2677 char *buf, int buflen)
2678{
75613521 2679 struct iscsi_host *ihost = shost_priv(shost);
0801c242
MC
2680
2681 switch (param) {
d8196ed2 2682 case ISCSI_HOST_PARAM_NETDEV_NAME:
75613521
MC
2683 if (!ihost->netdev)
2684 ihost->netdev = kstrdup(buf, GFP_KERNEL);
d8196ed2 2685 break;
0801c242 2686 case ISCSI_HOST_PARAM_HWADDRESS:
75613521
MC
2687 if (!ihost->hwaddress)
2688 ihost->hwaddress = kstrdup(buf, GFP_KERNEL);
0801c242 2689 break;
8ad5781a 2690 case ISCSI_HOST_PARAM_INITIATOR_NAME:
75613521
MC
2691 if (!ihost->initiatorname)
2692 ihost->initiatorname = kstrdup(buf, GFP_KERNEL);
8ad5781a 2693 break;
0801c242
MC
2694 default:
2695 return -ENOSYS;
2696 }
2697
2698 return 0;
2699}
2700EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2701
7996a778
MC
2702MODULE_AUTHOR("Mike Christie");
2703MODULE_DESCRIPTION("iSCSI library functions");
2704MODULE_LICENSE("GPL");