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