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