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