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