]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libiscsi.c
[SCSI] libiscsi: check if iscsi host has work queue before queueing work
[net-next-2.6.git] / drivers / scsi / libiscsi.c
CommitLineData
7996a778
MC
1/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
7996a778
MC
25#include <linux/kfifo.h>
26#include <linux/delay.h>
11836572 27#include <linux/log2.h>
8eb00539 28#include <asm/unaligned.h>
7996a778
MC
29#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
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
1336aed1
MC
84 if (ihost->workq)
85 queue_work(ihost->workq, &conn->xmitwork);
32ae763e
MC
86}
87EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
88
77a23c21
MC
89void
90iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
7996a778
MC
91{
92 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
93 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
94
77a23c21
MC
95 /*
96 * standard specifies this check for when to update expected and
97 * max sequence numbers
98 */
99 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
100 return;
101
102 if (exp_cmdsn != session->exp_cmdsn &&
103 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
7996a778
MC
104 session->exp_cmdsn = exp_cmdsn;
105
77a23c21
MC
106 if (max_cmdsn != session->max_cmdsn &&
107 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
108 session->max_cmdsn = max_cmdsn;
109 /*
110 * if the window closed with IO queued, then kick the
111 * xmit thread
112 */
3bbaaad9 113 if (!list_empty(&session->leadconn->cmdqueue) ||
1336aed1
MC
114 !list_empty(&session->leadconn->mgmtqueue))
115 iscsi_conn_queue_work(session->leadconn);
77a23c21 116 }
7996a778 117}
77a23c21 118EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
7996a778 119
577577da
MC
120/**
121 * iscsi_prep_data_out_pdu - initialize Data-Out
122 * @task: scsi command task
123 * @r2t: R2T info
124 * @hdr: iscsi data in pdu
125 *
126 * Notes:
127 * Initialize Data-Out within this R2T sequence and finds
128 * proper data_offset within this SCSI command.
129 *
130 * This function is called with connection lock taken.
131 **/
132void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
133 struct iscsi_data *hdr)
7996a778 134{
9c19a7d0 135 struct iscsi_conn *conn = task->conn;
577577da
MC
136 unsigned int left = r2t->data_length - r2t->sent;
137
138 task->hdr_len = sizeof(struct iscsi_data);
7996a778
MC
139
140 memset(hdr, 0, sizeof(struct iscsi_data));
577577da
MC
141 hdr->ttt = r2t->ttt;
142 hdr->datasn = cpu_to_be32(r2t->datasn);
143 r2t->datasn++;
7996a778 144 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
577577da
MC
145 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
146 hdr->itt = task->hdr_itt;
147 hdr->exp_statsn = r2t->exp_statsn;
148 hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
149 if (left > conn->max_xmit_dlength) {
7996a778 150 hton24(hdr->dlength, conn->max_xmit_dlength);
577577da 151 r2t->data_count = conn->max_xmit_dlength;
7996a778
MC
152 hdr->flags = 0;
153 } else {
577577da
MC
154 hton24(hdr->dlength, left);
155 r2t->data_count = left;
7996a778
MC
156 hdr->flags = ISCSI_FLAG_CMD_FINAL;
157 }
577577da 158 conn->dataout_pdus_cnt++;
7996a778 159}
577577da 160EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
7996a778 161
9c19a7d0 162static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
004d6530 163{
9c19a7d0 164 unsigned exp_len = task->hdr_len + len;
004d6530 165
9c19a7d0 166 if (exp_len > task->hdr_max) {
004d6530
BH
167 WARN_ON(1);
168 return -EINVAL;
169 }
170
171 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
9c19a7d0 172 task->hdr_len = exp_len;
004d6530
BH
173 return 0;
174}
175
38d1c069
BH
176/*
177 * make an extended cdb AHS
178 */
9c19a7d0 179static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
38d1c069 180{
9c19a7d0 181 struct scsi_cmnd *cmd = task->sc;
38d1c069
BH
182 unsigned rlen, pad_len;
183 unsigned short ahslength;
184 struct iscsi_ecdb_ahdr *ecdb_ahdr;
185 int rc;
186
9c19a7d0 187 ecdb_ahdr = iscsi_next_hdr(task);
38d1c069
BH
188 rlen = cmd->cmd_len - ISCSI_CDB_SIZE;
189
190 BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb));
191 ahslength = rlen + sizeof(ecdb_ahdr->reserved);
192
193 pad_len = iscsi_padding(rlen);
194
9c19a7d0 195 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
38d1c069
BH
196 sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len);
197 if (rc)
198 return rc;
199
200 if (pad_len)
201 memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len);
202
203 ecdb_ahdr->ahslength = cpu_to_be16(ahslength);
204 ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB;
205 ecdb_ahdr->reserved = 0;
206 memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen);
207
1b2c7af8
MC
208 ISCSI_DBG_SESSION(task->conn->session,
209 "iscsi_prep_ecdb_ahs: varlen_cdb_len %d "
210 "rlen %d pad_len %d ahs_length %d iscsi_headers_size "
211 "%u\n", cmd->cmd_len, rlen, pad_len, ahslength,
212 task->hdr_len);
38d1c069
BH
213 return 0;
214}
215
9c19a7d0 216static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
c07d4444 217{
9c19a7d0 218 struct scsi_cmnd *sc = task->sc;
c07d4444
BH
219 struct iscsi_rlength_ahdr *rlen_ahdr;
220 int rc;
221
9c19a7d0
MC
222 rlen_ahdr = iscsi_next_hdr(task);
223 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
c07d4444
BH
224 if (rc)
225 return rc;
226
227 rlen_ahdr->ahslength =
228 cpu_to_be16(sizeof(rlen_ahdr->read_length) +
229 sizeof(rlen_ahdr->reserved));
230 rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH;
231 rlen_ahdr->reserved = 0;
232 rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length);
233
1b2c7af8
MC
234 ISCSI_DBG_SESSION(task->conn->session,
235 "bidi-in rlen_ahdr->read_length(%d) "
236 "rlen_ahdr->ahslength(%d)\n",
237 be32_to_cpu(rlen_ahdr->read_length),
238 be16_to_cpu(rlen_ahdr->ahslength));
c07d4444
BH
239 return 0;
240}
241
7996a778
MC
242/**
243 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
9c19a7d0 244 * @task: iscsi task
7996a778
MC
245 *
246 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
247 * fields like dlength or final based on how much data it sends
248 */
9c19a7d0 249static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
7996a778 250{
9c19a7d0 251 struct iscsi_conn *conn = task->conn;
7996a778 252 struct iscsi_session *session = conn->session;
9c19a7d0 253 struct scsi_cmnd *sc = task->sc;
577577da 254 struct iscsi_cmd *hdr;
38d1c069 255 unsigned hdrlength, cmd_len;
262ef636 256 itt_t itt;
004d6530 257 int rc;
7996a778 258
184b57c6
MC
259 if (conn->session->tt->alloc_pdu) {
260 rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
261 if (rc)
262 return rc;
263 }
577577da 264 hdr = (struct iscsi_cmd *) task->hdr;
262ef636 265 itt = hdr->itt;
577577da
MC
266 memset(hdr, 0, sizeof(*hdr));
267
2ff79d52
MC
268 if (session->tt->parse_pdu_itt)
269 hdr->itt = task->hdr_itt = itt;
270 else
271 hdr->itt = task->hdr_itt = build_itt(task->itt,
272 task->conn->session->age);
9c19a7d0
MC
273 task->hdr_len = 0;
274 rc = iscsi_add_hdr(task, sizeof(*hdr));
004d6530
BH
275 if (rc)
276 return rc;
a8ac6311
OK
277 hdr->opcode = ISCSI_OP_SCSI_CMD;
278 hdr->flags = ISCSI_ATTR_SIMPLE;
279 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
577577da 280 memcpy(task->lun, hdr->lun, sizeof(task->lun));
577577da 281 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
a8ac6311
OK
282 session->cmdsn++;
283 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
38d1c069
BH
284 cmd_len = sc->cmd_len;
285 if (cmd_len < ISCSI_CDB_SIZE)
286 memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len);
287 else if (cmd_len > ISCSI_CDB_SIZE) {
9c19a7d0 288 rc = iscsi_prep_ecdb_ahs(task);
38d1c069
BH
289 if (rc)
290 return rc;
291 cmd_len = ISCSI_CDB_SIZE;
292 }
293 memcpy(hdr->cdb, sc->cmnd, cmd_len);
7996a778 294
9c19a7d0 295 task->imm_count = 0;
c07d4444
BH
296 if (scsi_bidi_cmnd(sc)) {
297 hdr->flags |= ISCSI_FLAG_CMD_READ;
9c19a7d0 298 rc = iscsi_prep_bidi_ahs(task);
c07d4444
BH
299 if (rc)
300 return rc;
301 }
7996a778 302 if (sc->sc_data_direction == DMA_TO_DEVICE) {
c07d4444 303 unsigned out_len = scsi_out(sc)->length;
577577da
MC
304 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
305
c07d4444 306 hdr->data_length = cpu_to_be32(out_len);
7996a778
MC
307 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
308 /*
309 * Write counters:
310 *
311 * imm_count bytes to be sent right after
312 * SCSI PDU Header
313 *
314 * unsol_count bytes(as Data-Out) to be sent
315 * without R2T ack right after
316 * immediate data
317 *
577577da 318 * r2t data_length bytes to be sent via R2T ack's
7996a778
MC
319 *
320 * pad_count bytes to be sent as zero-padding
321 */
577577da 322 memset(r2t, 0, sizeof(*r2t));
7996a778
MC
323
324 if (session->imm_data_en) {
c07d4444 325 if (out_len >= session->first_burst)
9c19a7d0 326 task->imm_count = min(session->first_burst,
7996a778
MC
327 conn->max_xmit_dlength);
328 else
9c19a7d0 329 task->imm_count = min(out_len,
7996a778 330 conn->max_xmit_dlength);
9c19a7d0 331 hton24(hdr->dlength, task->imm_count);
7996a778 332 } else
a8ac6311 333 zero_data(hdr->dlength);
7996a778 334
ffd0436e 335 if (!session->initial_r2t_en) {
577577da
MC
336 r2t->data_length = min(session->first_burst, out_len) -
337 task->imm_count;
338 r2t->data_offset = task->imm_count;
339 r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
340 r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
ffd0436e
MC
341 }
342
577577da 343 if (!task->unsol_r2t.data_length)
7996a778 344 /* No unsolicit Data-Out's */
a8ac6311 345 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
7996a778 346 } else {
7996a778
MC
347 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
348 zero_data(hdr->dlength);
c07d4444 349 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
7996a778
MC
350
351 if (sc->sc_data_direction == DMA_FROM_DEVICE)
352 hdr->flags |= ISCSI_FLAG_CMD_READ;
353 }
354
004d6530 355 /* calculate size of additional header segments (AHSs) */
9c19a7d0 356 hdrlength = task->hdr_len - sizeof(*hdr);
004d6530
BH
357
358 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
359 hdrlength /= ISCSI_PAD_LEN;
360
361 WARN_ON(hdrlength >= 256);
362 hdr->hlength = hdrlength & 0xFF;
363
577577da 364 if (session->tt->init_task && session->tt->init_task(task))
052d0144
MC
365 return -EIO;
366
9c19a7d0 367 task->state = ISCSI_TASK_RUNNING;
77a23c21 368
a8ac6311 369 conn->scsicmd_pdus_cnt++;
1b2c7af8
MC
370 ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x "
371 "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n",
372 scsi_bidi_cmnd(sc) ? "bidirectional" :
373 sc->sc_data_direction == DMA_TO_DEVICE ?
374 "write" : "read", conn->id, sc, sc->cmnd[0],
375 task->itt, scsi_bufflen(sc),
376 scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0,
377 session->cmdsn,
378 session->max_cmdsn - session->exp_cmdsn + 1);
004d6530 379 return 0;
7996a778 380}
7996a778
MC
381
382/**
3bbaaad9 383 * iscsi_free_task - free a task
9c19a7d0 384 * @task: iscsi cmd task
7996a778
MC
385 *
386 * Must be called with session lock.
3e5c28ad
MC
387 * This function returns the scsi command to scsi-ml or cleans
388 * up mgmt tasks then returns the task to the pool.
7996a778 389 */
3bbaaad9 390static void iscsi_free_task(struct iscsi_task *task)
7996a778 391{
9c19a7d0 392 struct iscsi_conn *conn = task->conn;
c1635cb7 393 struct iscsi_session *session = conn->session;
9c19a7d0 394 struct scsi_cmnd *sc = task->sc;
7996a778 395
577577da 396 session->tt->cleanup_task(task);
3bbaaad9 397 task->state = ISCSI_TASK_FREE;
9c19a7d0 398 task->sc = NULL;
3e5c28ad 399
3e5c28ad 400 /*
9c19a7d0 401 * login task is preallocated so do not free
3e5c28ad 402 */
9c19a7d0 403 if (conn->login_task == task)
3e5c28ad
MC
404 return;
405
9c19a7d0 406 __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*));
052d0144 407
3e5c28ad 408 if (sc) {
9c19a7d0 409 task->sc = NULL;
3e5c28ad
MC
410 /* SCSI eh reuses commands to verify us */
411 sc->SCp.ptr = NULL;
412 /*
413 * queue command may call this to free the task, but
414 * not have setup the sc callback
415 */
416 if (sc->scsi_done)
417 sc->scsi_done(sc);
418 }
7996a778
MC
419}
420
913e5bf4 421void __iscsi_get_task(struct iscsi_task *task)
60ecebf5 422{
9c19a7d0 423 atomic_inc(&task->refcount);
60ecebf5 424}
913e5bf4 425EXPORT_SYMBOL_GPL(__iscsi_get_task);
60ecebf5 426
9c19a7d0 427static void __iscsi_put_task(struct iscsi_task *task)
60ecebf5 428{
9c19a7d0 429 if (atomic_dec_and_test(&task->refcount))
3bbaaad9 430 iscsi_free_task(task);
60ecebf5
MC
431}
432
9c19a7d0 433void iscsi_put_task(struct iscsi_task *task)
3e5c28ad 434{
9c19a7d0 435 struct iscsi_session *session = task->conn->session;
3e5c28ad
MC
436
437 spin_lock_bh(&session->lock);
9c19a7d0 438 __iscsi_put_task(task);
3e5c28ad
MC
439 spin_unlock_bh(&session->lock);
440}
9c19a7d0 441EXPORT_SYMBOL_GPL(iscsi_put_task);
3e5c28ad 442
3bbaaad9
MC
443/**
444 * iscsi_complete_task - finish a task
445 * @task: iscsi cmd task
446 *
447 * Must be called with session lock.
448 */
449static void iscsi_complete_task(struct iscsi_task *task)
450{
451 struct iscsi_conn *conn = task->conn;
452
453 if (task->state == ISCSI_TASK_COMPLETED)
454 return;
455 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
456
457 task->state = ISCSI_TASK_COMPLETED;
458
459 if (!list_empty(&task->running))
460 list_del_init(&task->running);
461
462 if (conn->task == task)
463 conn->task = NULL;
464
465 if (conn->ping_task == task)
466 conn->ping_task = NULL;
467
468 /* release get from queueing */
469 __iscsi_put_task(task);
470}
471
b3a7ea8d 472/*
3bbaaad9
MC
473 * session lock must be held and if not called for a task that is
474 * still pending or from the xmit thread, then xmit thread must
475 * be suspended.
b3a7ea8d 476 */
3bbaaad9 477static void fail_scsi_task(struct iscsi_task *task, int err)
b3a7ea8d 478{
3bbaaad9 479 struct iscsi_conn *conn = task->conn;
b3a7ea8d
MC
480 struct scsi_cmnd *sc;
481
3bbaaad9
MC
482 /*
483 * if a command completes and we get a successful tmf response
484 * we will hit this because the scsi eh abort code does not take
485 * a ref to the task.
486 */
9c19a7d0 487 sc = task->sc;
b3a7ea8d
MC
488 if (!sc)
489 return;
490
9c19a7d0 491 if (task->state == ISCSI_TASK_PENDING)
b3a7ea8d
MC
492 /*
493 * cmd never made it to the xmit thread, so we should not count
494 * the cmd in the sequencing
495 */
496 conn->session->queued_cmdsn--;
b3a7ea8d
MC
497
498 sc->result = err;
c07d4444
BH
499 if (!scsi_bidi_cmnd(sc))
500 scsi_set_resid(sc, scsi_bufflen(sc));
501 else {
502 scsi_out(sc)->resid = scsi_out(sc)->length;
503 scsi_in(sc)->resid = scsi_in(sc)->length;
504 }
3e5c28ad 505
3bbaaad9 506 iscsi_complete_task(task);
b3a7ea8d
MC
507}
508
3e5c28ad 509static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
9c19a7d0 510 struct iscsi_task *task)
052d0144
MC
511{
512 struct iscsi_session *session = conn->session;
577577da 513 struct iscsi_hdr *hdr = task->hdr;
052d0144
MC
514 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
515
516 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
517 return -ENOTCONN;
518
519 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
520 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
521 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
522 /*
523 * pre-format CmdSN for outgoing PDU.
524 */
525 nop->cmdsn = cpu_to_be32(session->cmdsn);
526 if (hdr->itt != RESERVED_ITT) {
052d0144
MC
527 /*
528 * TODO: We always use immediate, so we never hit this.
529 * If we start to send tmfs or nops as non-immediate then
530 * we should start checking the cmdsn numbers for mgmt tasks.
531 */
532 if (conn->c_stage == ISCSI_CONN_STARTED &&
533 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
534 session->queued_cmdsn++;
535 session->cmdsn++;
536 }
537 }
538
ae15f801
MC
539 if (session->tt->init_task && session->tt->init_task(task))
540 return -EIO;
052d0144
MC
541
542 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
543 session->state = ISCSI_STATE_LOGGING_OUT;
544
577577da 545 task->state = ISCSI_TASK_RUNNING;
1b2c7af8
MC
546 ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x "
547 "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK,
548 hdr->itt, task->data_count);
052d0144
MC
549 return 0;
550}
551
9c19a7d0 552static struct iscsi_task *
f6d5180c
MC
553__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
554 char *data, uint32_t data_size)
555{
556 struct iscsi_session *session = conn->session;
1336aed1 557 struct iscsi_host *ihost = shost_priv(session->host);
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
1336aed1 621 if (!ihost->workq) {
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 1370 struct Scsi_Host *host;
1336aed1 1371 struct iscsi_host *ihost;
7996a778
MC
1372 int reason = 0;
1373 struct iscsi_session *session;
1374 struct iscsi_conn *conn;
9c19a7d0 1375 struct iscsi_task *task = NULL;
7996a778
MC
1376
1377 sc->scsi_done = done;
1378 sc->result = 0;
f47f2cf5 1379 sc->SCp.ptr = NULL;
7996a778
MC
1380
1381 host = sc->device->host;
1336aed1 1382 ihost = shost_priv(host);
1040c99d 1383 spin_unlock(host->host_lock);
7996a778 1384
75613521
MC
1385 cls_session = starget_to_session(scsi_target(sc->device));
1386 session = cls_session->dd_data;
7996a778
MC
1387 spin_lock(&session->lock);
1388
75613521 1389 reason = iscsi_session_chkready(cls_session);
6eabafbe
MC
1390 if (reason) {
1391 sc->result = reason;
1392 goto fault;
1393 }
1394
301e0f7e 1395 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9
MC
1396 /*
1397 * to handle the race between when we set the recovery state
1398 * and block the session we requeue here (commands could
1399 * be entering our queuecommand while a block is starting
1400 * up because the block code is not locked)
1401 */
9000bcd6 1402 switch (session->state) {
301e0f7e 1403 case ISCSI_STATE_FAILED:
9000bcd6 1404 case ISCSI_STATE_IN_RECOVERY:
656cffc9 1405 reason = FAILURE_SESSION_IN_RECOVERY;
301e0f7e
MC
1406 sc->result = DID_IMM_RETRY << 16;
1407 break;
9000bcd6
MC
1408 case ISCSI_STATE_LOGGING_OUT:
1409 reason = FAILURE_SESSION_LOGGING_OUT;
301e0f7e
MC
1410 sc->result = DID_IMM_RETRY << 16;
1411 break;
b3a7ea8d 1412 case ISCSI_STATE_RECOVERY_FAILED:
656cffc9 1413 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
56d7fcfa 1414 sc->result = DID_TRANSPORT_FAILFAST << 16;
b3a7ea8d
MC
1415 break;
1416 case ISCSI_STATE_TERMINATE:
656cffc9 1417 reason = FAILURE_SESSION_TERMINATE;
6eabafbe 1418 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d 1419 break;
b3a7ea8d 1420 default:
656cffc9 1421 reason = FAILURE_SESSION_FREED;
6eabafbe 1422 sc->result = DID_NO_CONNECT << 16;
b3a7ea8d 1423 }
7996a778
MC
1424 goto fault;
1425 }
1426
7996a778 1427 conn = session->leadconn;
98644047
MC
1428 if (!conn) {
1429 reason = FAILURE_SESSION_FREED;
6eabafbe 1430 sc->result = DID_NO_CONNECT << 16;
98644047
MC
1431 goto fault;
1432 }
7996a778 1433
77a23c21
MC
1434 if (iscsi_check_cmdsn_window_closed(conn)) {
1435 reason = FAILURE_WINDOW_CLOSED;
1436 goto reject;
1437 }
1438
577577da
MC
1439 task = iscsi_alloc_task(conn, sc);
1440 if (!task) {
60ecebf5
MC
1441 reason = FAILURE_OOM;
1442 goto reject;
1443 }
7996a778 1444
1336aed1 1445 if (!ihost->workq) {
577577da
MC
1446 reason = iscsi_prep_scsi_cmd_pdu(task);
1447 if (reason) {
1448 if (reason == -ENOMEM) {
1449 reason = FAILURE_OOM;
1450 goto prepd_reject;
1451 } else {
1452 sc->result = DID_ABORT << 16;
1453 goto prepd_fault;
1454 }
052d0144 1455 }
9c19a7d0 1456 if (session->tt->xmit_task(task)) {
052d0144 1457 reason = FAILURE_SESSION_NOT_READY;
577577da 1458 goto prepd_reject;
052d0144 1459 }
3bbaaad9
MC
1460 } else {
1461 list_add_tail(&task->running, &conn->cmdqueue);
32ae763e 1462 iscsi_conn_queue_work(conn);
3bbaaad9 1463 }
052d0144
MC
1464
1465 session->queued_cmdsn++;
1466 spin_unlock(&session->lock);
1040c99d 1467 spin_lock(host->host_lock);
7996a778
MC
1468 return 0;
1469
577577da
MC
1470prepd_reject:
1471 sc->scsi_done = NULL;
3bbaaad9 1472 iscsi_complete_task(task);
7996a778
MC
1473reject:
1474 spin_unlock(&session->lock);
1b2c7af8
MC
1475 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1476 sc->cmnd[0], reason);
1040c99d 1477 spin_lock(host->host_lock);
d6d13ee1 1478 return SCSI_MLQUEUE_TARGET_BUSY;
7996a778 1479
577577da
MC
1480prepd_fault:
1481 sc->scsi_done = NULL;
3bbaaad9 1482 iscsi_complete_task(task);
7996a778
MC
1483fault:
1484 spin_unlock(&session->lock);
1b2c7af8
MC
1485 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1486 sc->cmnd[0], reason);
c07d4444
BH
1487 if (!scsi_bidi_cmnd(sc))
1488 scsi_set_resid(sc, scsi_bufflen(sc));
1489 else {
1490 scsi_out(sc)->resid = scsi_out(sc)->length;
1491 scsi_in(sc)->resid = scsi_in(sc)->length;
1492 }
052d0144 1493 done(sc);
1040c99d 1494 spin_lock(host->host_lock);
7996a778
MC
1495 return 0;
1496}
1497EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1498
1499int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1500{
7996a778
MC
1501 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1502 return sdev->queue_depth;
1503}
1504EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1505
6b5d6c44
MC
1506int iscsi_target_alloc(struct scsi_target *starget)
1507{
1508 struct iscsi_cls_session *cls_session = starget_to_session(starget);
1509 struct iscsi_session *session = cls_session->dd_data;
1510
1511 starget->can_queue = session->scsi_cmds_max;
1512 return 0;
1513}
1514EXPORT_SYMBOL_GPL(iscsi_target_alloc);
1515
7996a778
MC
1516void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1517{
75613521 1518 struct iscsi_session *session = cls_session->dd_data;
7996a778
MC
1519
1520 spin_lock_bh(&session->lock);
1521 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9 1522 session->state = ISCSI_STATE_RECOVERY_FAILED;
843c0a8a
MC
1523 if (session->leadconn)
1524 wake_up(&session->leadconn->ehwait);
7996a778
MC
1525 }
1526 spin_unlock_bh(&session->lock);
1527}
1528EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1529
8e124525 1530int iscsi_eh_target_reset(struct scsi_cmnd *sc)
7996a778 1531{
75613521
MC
1532 struct iscsi_cls_session *cls_session;
1533 struct iscsi_session *session;
1534 struct iscsi_conn *conn;
1535
1536 cls_session = starget_to_session(scsi_target(sc->device));
1537 session = cls_session->dd_data;
1538 conn = session->leadconn;
7996a778 1539
bc436b27 1540 mutex_lock(&session->eh_mutex);
7996a778
MC
1541 spin_lock_bh(&session->lock);
1542 if (session->state == ISCSI_STATE_TERMINATE) {
1543failed:
1b2c7af8
MC
1544 iscsi_session_printk(KERN_INFO, session,
1545 "failing target reset: Could not log "
1546 "back into target [age %d]\n",
1547 session->age);
7996a778 1548 spin_unlock_bh(&session->lock);
bc436b27 1549 mutex_unlock(&session->eh_mutex);
7996a778
MC
1550 return FAILED;
1551 }
1552
7996a778 1553 spin_unlock_bh(&session->lock);
bc436b27 1554 mutex_unlock(&session->eh_mutex);
7996a778
MC
1555 /*
1556 * we drop the lock here but the leadconn cannot be destoyed while
1557 * we are in the scsi eh
1558 */
843c0a8a 1559 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778 1560
1b2c7af8 1561 ISCSI_DBG_SESSION(session, "wait for relogin\n");
7996a778
MC
1562 wait_event_interruptible(conn->ehwait,
1563 session->state == ISCSI_STATE_TERMINATE ||
1564 session->state == ISCSI_STATE_LOGGED_IN ||
656cffc9 1565 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
1566 if (signal_pending(current))
1567 flush_signals(current);
1568
bc436b27 1569 mutex_lock(&session->eh_mutex);
7996a778
MC
1570 spin_lock_bh(&session->lock);
1571 if (session->state == ISCSI_STATE_LOGGED_IN)
322d739d 1572 iscsi_session_printk(KERN_INFO, session,
8e124525 1573 "target reset succeeded\n");
7996a778
MC
1574 else
1575 goto failed;
1576 spin_unlock_bh(&session->lock);
bc436b27 1577 mutex_unlock(&session->eh_mutex);
7996a778
MC
1578 return SUCCESS;
1579}
8e124525 1580EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
7996a778 1581
843c0a8a 1582static void iscsi_tmf_timedout(unsigned long data)
7996a778 1583{
843c0a8a 1584 struct iscsi_conn *conn = (struct iscsi_conn *)data;
7996a778
MC
1585 struct iscsi_session *session = conn->session;
1586
1587 spin_lock(&session->lock);
843c0a8a
MC
1588 if (conn->tmf_state == TMF_QUEUED) {
1589 conn->tmf_state = TMF_TIMEDOUT;
1b2c7af8 1590 ISCSI_DBG_SESSION(session, "tmf timedout\n");
7996a778
MC
1591 /* unblock eh_abort() */
1592 wake_up(&conn->ehwait);
1593 }
1594 spin_unlock(&session->lock);
1595}
1596
9c19a7d0 1597static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
f6d5180c
MC
1598 struct iscsi_tm *hdr, int age,
1599 int timeout)
7996a778 1600{
7996a778 1601 struct iscsi_session *session = conn->session;
9c19a7d0 1602 struct iscsi_task *task;
7996a778 1603
9c19a7d0 1604 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
843c0a8a 1605 NULL, 0);
9c19a7d0 1606 if (!task) {
6724add1 1607 spin_unlock_bh(&session->lock);
7996a778 1608 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
843c0a8a 1609 spin_lock_bh(&session->lock);
1b2c7af8 1610 ISCSI_DBG_SESSION(session, "tmf exec failure\n");
77a23c21 1611 return -EPERM;
7996a778 1612 }
843c0a8a 1613 conn->tmfcmd_pdus_cnt++;
f6d5180c 1614 conn->tmf_timer.expires = timeout * HZ + jiffies;
843c0a8a
MC
1615 conn->tmf_timer.function = iscsi_tmf_timedout;
1616 conn->tmf_timer.data = (unsigned long)conn;
1617 add_timer(&conn->tmf_timer);
1b2c7af8 1618 ISCSI_DBG_SESSION(session, "tmf set timeout\n");
7996a778 1619
7996a778 1620 spin_unlock_bh(&session->lock);
6724add1 1621 mutex_unlock(&session->eh_mutex);
7996a778
MC
1622
1623 /*
1624 * block eh thread until:
1625 *
843c0a8a
MC
1626 * 1) tmf response
1627 * 2) tmf timeout
7996a778
MC
1628 * 3) session is terminated or restarted or userspace has
1629 * given up on recovery
1630 */
843c0a8a 1631 wait_event_interruptible(conn->ehwait, age != session->age ||
7996a778 1632 session->state != ISCSI_STATE_LOGGED_IN ||
843c0a8a 1633 conn->tmf_state != TMF_QUEUED);
7996a778
MC
1634 if (signal_pending(current))
1635 flush_signals(current);
843c0a8a
MC
1636 del_timer_sync(&conn->tmf_timer);
1637
6724add1 1638 mutex_lock(&session->eh_mutex);
77a23c21 1639 spin_lock_bh(&session->lock);
9c19a7d0 1640 /* if the session drops it will clean up the task */
843c0a8a
MC
1641 if (age != session->age ||
1642 session->state != ISCSI_STATE_LOGGED_IN)
1643 return -ENOTCONN;
7996a778
MC
1644 return 0;
1645}
1646
843c0a8a
MC
1647/*
1648 * Fail commands. session lock held and recv side suspended and xmit
1649 * thread flushed
1650 */
3bbaaad9
MC
1651static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1652 int error)
843c0a8a 1653{
3bbaaad9
MC
1654 struct iscsi_task *task;
1655 int i;
843c0a8a 1656
3bbaaad9
MC
1657 for (i = 0; i < conn->session->cmds_max; i++) {
1658 task = conn->session->cmds[i];
1659 if (!task->sc || task->state == ISCSI_TASK_FREE)
1660 continue;
843c0a8a 1661
3bbaaad9
MC
1662 if (lun != -1 && lun != task->sc->device->lun)
1663 continue;
843c0a8a 1664
3bbaaad9
MC
1665 ISCSI_DBG_SESSION(conn->session,
1666 "failing sc %p itt 0x%x state %d\n",
1667 task->sc, task->itt, task->state);
1668 fail_scsi_task(task, error << 16);
843c0a8a
MC
1669 }
1670}
1671
b40977d9 1672void iscsi_suspend_tx(struct iscsi_conn *conn)
6724add1 1673{
32ae763e
MC
1674 struct Scsi_Host *shost = conn->session->host;
1675 struct iscsi_host *ihost = shost_priv(shost);
1676
6724add1 1677 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1336aed1 1678 if (ihost->workq)
32ae763e 1679 flush_workqueue(ihost->workq);
6724add1 1680}
b40977d9 1681EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
6724add1
MC
1682
1683static void iscsi_start_tx(struct iscsi_conn *conn)
1684{
1685 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1336aed1 1686 iscsi_conn_queue_work(conn);
6724add1
MC
1687}
1688
4c48a829
MC
1689/*
1690 * We want to make sure a ping is in flight. It has timed out.
1691 * And we are not busy processing a pdu that is making
1692 * progress but got started before the ping and is taking a while
1693 * to complete so the ping is just stuck behind it in a queue.
1694 */
1695static int iscsi_has_ping_timed_out(struct iscsi_conn *conn)
1696{
1697 if (conn->ping_task &&
1698 time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1699 (conn->ping_timeout * HZ), jiffies))
1700 return 1;
1701 else
1702 return 0;
1703}
1704
242f9dcb 1705static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
f6d5180c
MC
1706{
1707 struct iscsi_cls_session *cls_session;
1708 struct iscsi_session *session;
1709 struct iscsi_conn *conn;
242f9dcb 1710 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
f6d5180c
MC
1711
1712 cls_session = starget_to_session(scsi_target(scmd->device));
75613521 1713 session = cls_session->dd_data;
f6d5180c 1714
1b2c7af8 1715 ISCSI_DBG_SESSION(session, "scsi cmd %p timedout\n", scmd);
f6d5180c
MC
1716
1717 spin_lock(&session->lock);
1718 if (session->state != ISCSI_STATE_LOGGED_IN) {
1719 /*
1720 * We are probably in the middle of iscsi recovery so let
1721 * that complete and handle the error.
1722 */
242f9dcb 1723 rc = BLK_EH_RESET_TIMER;
f6d5180c
MC
1724 goto done;
1725 }
1726
1727 conn = session->leadconn;
1728 if (!conn) {
1729 /* In the middle of shuting down */
242f9dcb 1730 rc = BLK_EH_RESET_TIMER;
f6d5180c
MC
1731 goto done;
1732 }
1733
1734 if (!conn->recv_timeout && !conn->ping_timeout)
1735 goto done;
1736 /*
1737 * if the ping timedout then we are in the middle of cleaning up
1738 * and can let the iscsi eh handle it
1739 */
4c48a829 1740 if (iscsi_has_ping_timed_out(conn)) {
242f9dcb 1741 rc = BLK_EH_RESET_TIMER;
4c48a829
MC
1742 goto done;
1743 }
f6d5180c
MC
1744 /*
1745 * if we are about to check the transport then give the command
1746 * more time
1747 */
1748 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
4c48a829 1749 jiffies)) {
242f9dcb 1750 rc = BLK_EH_RESET_TIMER;
4c48a829
MC
1751 goto done;
1752 }
1753
f6d5180c 1754 /* if in the middle of checking the transport then give us more time */
9c19a7d0 1755 if (conn->ping_task)
242f9dcb 1756 rc = BLK_EH_RESET_TIMER;
f6d5180c
MC
1757done:
1758 spin_unlock(&session->lock);
1b2c7af8
MC
1759 ISCSI_DBG_SESSION(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
1760 "timer reset" : "nh");
f6d5180c
MC
1761 return rc;
1762}
1763
1764static void iscsi_check_transport_timeouts(unsigned long data)
1765{
1766 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1767 struct iscsi_session *session = conn->session;
4cf10435 1768 unsigned long recv_timeout, next_timeout = 0, last_recv;
f6d5180c
MC
1769
1770 spin_lock(&session->lock);
1771 if (session->state != ISCSI_STATE_LOGGED_IN)
1772 goto done;
1773
4cf10435
MC
1774 recv_timeout = conn->recv_timeout;
1775 if (!recv_timeout)
f6d5180c
MC
1776 goto done;
1777
4cf10435 1778 recv_timeout *= HZ;
f6d5180c 1779 last_recv = conn->last_recv;
4c48a829
MC
1780
1781 if (iscsi_has_ping_timed_out(conn)) {
322d739d 1782 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
4c48a829
MC
1783 "expired, recv timeout %d, last rx %lu, "
1784 "last ping %lu, now %lu\n",
1785 conn->ping_timeout, conn->recv_timeout,
1786 last_recv, conn->last_ping, jiffies);
f6d5180c
MC
1787 spin_unlock(&session->lock);
1788 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1789 return;
1790 }
1791
4cf10435 1792 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
c8611f97 1793 /* send a ping to try to provoke some traffic */
1b2c7af8 1794 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
c8611f97 1795 iscsi_send_nopout(conn, NULL);
4cf10435 1796 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
ad294e9c 1797 } else
4cf10435 1798 next_timeout = last_recv + recv_timeout;
f6d5180c 1799
1b2c7af8 1800 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
ad294e9c 1801 mod_timer(&conn->transport_timer, next_timeout);
f6d5180c
MC
1802done:
1803 spin_unlock(&session->lock);
1804}
1805
9c19a7d0 1806static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
843c0a8a
MC
1807 struct iscsi_tm *hdr)
1808{
1809 memset(hdr, 0, sizeof(*hdr));
1810 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1811 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1812 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
577577da
MC
1813 memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
1814 hdr->rtt = task->hdr_itt;
1815 hdr->refcmdsn = task->cmdsn;
843c0a8a
MC
1816}
1817
7996a778
MC
1818int iscsi_eh_abort(struct scsi_cmnd *sc)
1819{
75613521
MC
1820 struct iscsi_cls_session *cls_session;
1821 struct iscsi_session *session;
f47f2cf5 1822 struct iscsi_conn *conn;
9c19a7d0 1823 struct iscsi_task *task;
843c0a8a
MC
1824 struct iscsi_tm *hdr;
1825 int rc, age;
7996a778 1826
75613521
MC
1827 cls_session = starget_to_session(scsi_target(sc->device));
1828 session = cls_session->dd_data;
1829
6724add1
MC
1830 mutex_lock(&session->eh_mutex);
1831 spin_lock_bh(&session->lock);
f47f2cf5
MC
1832 /*
1833 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1834 * got the command.
1835 */
1836 if (!sc->SCp.ptr) {
1b2c7af8
MC
1837 ISCSI_DBG_SESSION(session, "sc never reached iscsi layer or "
1838 "it completed.\n");
6724add1
MC
1839 spin_unlock_bh(&session->lock);
1840 mutex_unlock(&session->eh_mutex);
f47f2cf5
MC
1841 return SUCCESS;
1842 }
1843
7996a778
MC
1844 /*
1845 * If we are not logged in or we have started a new session
1846 * then let the host reset code handle this
1847 */
843c0a8a
MC
1848 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1849 sc->SCp.phase != session->age) {
1850 spin_unlock_bh(&session->lock);
1851 mutex_unlock(&session->eh_mutex);
1852 return FAILED;
1853 }
1854
1855 conn = session->leadconn;
1856 conn->eh_abort_cnt++;
1857 age = session->age;
1858
9c19a7d0 1859 task = (struct iscsi_task *)sc->SCp.ptr;
1b2c7af8
MC
1860 ISCSI_DBG_SESSION(session, "aborting [sc %p itt 0x%x]\n",
1861 sc, task->itt);
7996a778 1862
9c19a7d0
MC
1863 /* task completed before time out */
1864 if (!task->sc) {
1b2c7af8
MC
1865 ISCSI_DBG_SESSION(session, "sc completed while abort in "
1866 "progress\n");
77a23c21 1867 goto success;
7ea8b828 1868 }
7996a778 1869
9c19a7d0 1870 if (task->state == ISCSI_TASK_PENDING) {
3bbaaad9 1871 fail_scsi_task(task, DID_ABORT << 16);
77a23c21
MC
1872 goto success;
1873 }
7996a778 1874
843c0a8a
MC
1875 /* only have one tmf outstanding at a time */
1876 if (conn->tmf_state != TMF_INITIAL)
7996a778 1877 goto failed;
843c0a8a 1878 conn->tmf_state = TMF_QUEUED;
7996a778 1879
843c0a8a 1880 hdr = &conn->tmhdr;
9c19a7d0 1881 iscsi_prep_abort_task_pdu(task, hdr);
843c0a8a 1882
9c19a7d0 1883 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
843c0a8a
MC
1884 rc = FAILED;
1885 goto failed;
1886 }
1887
1888 switch (conn->tmf_state) {
1889 case TMF_SUCCESS:
77a23c21 1890 spin_unlock_bh(&session->lock);
913e5bf4
MC
1891 /*
1892 * stop tx side incase the target had sent a abort rsp but
1893 * the initiator was still writing out data.
1894 */
6724add1 1895 iscsi_suspend_tx(conn);
77a23c21 1896 /*
913e5bf4
MC
1897 * we do not stop the recv side because targets have been
1898 * good and have never sent us a successful tmf response
1899 * then sent more data for the cmd.
77a23c21 1900 */
77a23c21 1901 spin_lock(&session->lock);
3bbaaad9 1902 fail_scsi_task(task, DID_ABORT << 16);
843c0a8a 1903 conn->tmf_state = TMF_INITIAL;
77a23c21 1904 spin_unlock(&session->lock);
6724add1 1905 iscsi_start_tx(conn);
77a23c21 1906 goto success_unlocked;
843c0a8a
MC
1907 case TMF_TIMEDOUT:
1908 spin_unlock_bh(&session->lock);
1909 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1910 goto failed_unlocked;
1911 case TMF_NOT_FOUND:
1912 if (!sc->SCp.ptr) {
1913 conn->tmf_state = TMF_INITIAL;
9c19a7d0 1914 /* task completed before tmf abort response */
1b2c7af8
MC
1915 ISCSI_DBG_SESSION(session, "sc completed while abort "
1916 "in progress\n");
77a23c21 1917 goto success;
7ea8b828
MC
1918 }
1919 /* fall through */
1920 default:
843c0a8a
MC
1921 conn->tmf_state = TMF_INITIAL;
1922 goto failed;
7996a778
MC
1923 }
1924
77a23c21 1925success:
7996a778 1926 spin_unlock_bh(&session->lock);
77a23c21 1927success_unlocked:
1b2c7af8
MC
1928 ISCSI_DBG_SESSION(session, "abort success [sc %p itt 0x%x]\n",
1929 sc, task->itt);
6724add1 1930 mutex_unlock(&session->eh_mutex);
7996a778
MC
1931 return SUCCESS;
1932
1933failed:
1934 spin_unlock_bh(&session->lock);
77a23c21 1935failed_unlocked:
1b2c7af8
MC
1936 ISCSI_DBG_SESSION(session, "abort failed [sc %p itt 0x%x]\n", sc,
1937 task ? task->itt : 0);
6724add1 1938 mutex_unlock(&session->eh_mutex);
7996a778
MC
1939 return FAILED;
1940}
1941EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1942
843c0a8a
MC
1943static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1944{
1945 memset(hdr, 0, sizeof(*hdr));
1946 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1947 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1948 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1949 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
f6d5180c 1950 hdr->rtt = RESERVED_ITT;
843c0a8a
MC
1951}
1952
1953int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1954{
75613521
MC
1955 struct iscsi_cls_session *cls_session;
1956 struct iscsi_session *session;
843c0a8a
MC
1957 struct iscsi_conn *conn;
1958 struct iscsi_tm *hdr;
1959 int rc = FAILED;
1960
75613521
MC
1961 cls_session = starget_to_session(scsi_target(sc->device));
1962 session = cls_session->dd_data;
1963
1b2c7af8
MC
1964 ISCSI_DBG_SESSION(session, "LU Reset [sc %p lun %u]\n",
1965 sc, sc->device->lun);
843c0a8a
MC
1966
1967 mutex_lock(&session->eh_mutex);
1968 spin_lock_bh(&session->lock);
1969 /*
1970 * Just check if we are not logged in. We cannot check for
1971 * the phase because the reset could come from a ioctl.
1972 */
1973 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1974 goto unlock;
1975 conn = session->leadconn;
1976
1977 /* only have one tmf outstanding at a time */
1978 if (conn->tmf_state != TMF_INITIAL)
1979 goto unlock;
1980 conn->tmf_state = TMF_QUEUED;
1981
1982 hdr = &conn->tmhdr;
1983 iscsi_prep_lun_reset_pdu(sc, hdr);
1984
9c19a7d0 1985 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
f6d5180c 1986 session->lu_reset_timeout)) {
843c0a8a
MC
1987 rc = FAILED;
1988 goto unlock;
1989 }
1990
1991 switch (conn->tmf_state) {
1992 case TMF_SUCCESS:
1993 break;
1994 case TMF_TIMEDOUT:
1995 spin_unlock_bh(&session->lock);
1996 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1997 goto done;
1998 default:
1999 conn->tmf_state = TMF_INITIAL;
2000 goto unlock;
2001 }
2002
2003 rc = SUCCESS;
2004 spin_unlock_bh(&session->lock);
2005
2006 iscsi_suspend_tx(conn);
913e5bf4 2007
a3439148 2008 spin_lock_bh(&session->lock);
3bbaaad9 2009 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
843c0a8a 2010 conn->tmf_state = TMF_INITIAL;
a3439148 2011 spin_unlock_bh(&session->lock);
843c0a8a
MC
2012
2013 iscsi_start_tx(conn);
2014 goto done;
2015
2016unlock:
2017 spin_unlock_bh(&session->lock);
2018done:
1b2c7af8
MC
2019 ISCSI_DBG_SESSION(session, "dev reset result = %s\n",
2020 rc == SUCCESS ? "SUCCESS" : "FAILED");
843c0a8a
MC
2021 mutex_unlock(&session->eh_mutex);
2022 return rc;
2023}
2024EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2025
6320377f
OK
2026/*
2027 * Pre-allocate a pool of @max items of @item_size. By default, the pool
2028 * should be accessed via kfifo_{get,put} on q->queue.
2029 * Optionally, the caller can obtain the array of object pointers
2030 * by passing in a non-NULL @items pointer
2031 */
7996a778 2032int
6320377f 2033iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
7996a778 2034{
6320377f 2035 int i, num_arrays = 1;
7996a778 2036
6320377f 2037 memset(q, 0, sizeof(*q));
7996a778
MC
2038
2039 q->max = max;
6320377f
OK
2040
2041 /* If the user passed an items pointer, he wants a copy of
2042 * the array. */
2043 if (items)
2044 num_arrays++;
2045 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
2046 if (q->pool == NULL)
f474a37b 2047 return -ENOMEM;
7996a778
MC
2048
2049 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
2050 GFP_KERNEL, NULL);
fd6e1c14
JD
2051 if (IS_ERR(q->queue)) {
2052 q->queue = NULL;
6320377f 2053 goto enomem;
fd6e1c14 2054 }
7996a778
MC
2055
2056 for (i = 0; i < max; i++) {
6320377f 2057 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
7996a778 2058 if (q->pool[i] == NULL) {
6320377f
OK
2059 q->max = i;
2060 goto enomem;
7996a778 2061 }
7996a778
MC
2062 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
2063 }
6320377f
OK
2064
2065 if (items) {
2066 *items = q->pool + max;
2067 memcpy(*items, q->pool, max * sizeof(void *));
2068 }
2069
7996a778 2070 return 0;
6320377f
OK
2071
2072enomem:
2073 iscsi_pool_free(q);
2074 return -ENOMEM;
7996a778
MC
2075}
2076EXPORT_SYMBOL_GPL(iscsi_pool_init);
2077
6320377f 2078void iscsi_pool_free(struct iscsi_pool *q)
7996a778
MC
2079{
2080 int i;
2081
2082 for (i = 0; i < q->max; i++)
6320377f 2083 kfree(q->pool[i]);
f474a37b 2084 kfree(q->pool);
2f5899a3 2085 kfree(q->queue);
7996a778
MC
2086}
2087EXPORT_SYMBOL_GPL(iscsi_pool_free);
2088
a4804cd6
MC
2089/**
2090 * iscsi_host_add - add host to system
2091 * @shost: scsi host
2092 * @pdev: parent device
2093 *
2094 * This should be called by partial offload and software iscsi drivers
2095 * to add a host to the system.
2096 */
2097int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
2098{
8e9a20ce
MC
2099 if (!shost->can_queue)
2100 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2101
4d108350
MC
2102 if (!shost->cmd_per_lun)
2103 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2104
308cec14
MC
2105 if (!shost->transportt->eh_timed_out)
2106 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
a4804cd6
MC
2107 return scsi_add_host(shost, pdev);
2108}
2109EXPORT_SYMBOL_GPL(iscsi_host_add);
2110
2111/**
2112 * iscsi_host_alloc - allocate a host and driver data
2113 * @sht: scsi host template
2114 * @dd_data_size: driver host data size
32ae763e 2115 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
a4804cd6
MC
2116 *
2117 * This should be called by partial offload and software iscsi drivers.
2118 * To access the driver specific memory use the iscsi_host_priv() macro.
2119 */
2120struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
4d108350 2121 int dd_data_size, bool xmit_can_sleep)
75613521 2122{
a4804cd6 2123 struct Scsi_Host *shost;
e5bd7b54 2124 struct iscsi_host *ihost;
a4804cd6
MC
2125
2126 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2127 if (!shost)
2128 return NULL;
e5bd7b54 2129 ihost = shost_priv(shost);
32ae763e
MC
2130
2131 if (xmit_can_sleep) {
2132 snprintf(ihost->workq_name, sizeof(ihost->workq_name),
2133 "iscsi_q_%d", shost->host_no);
2134 ihost->workq = create_singlethread_workqueue(ihost->workq_name);
2135 if (!ihost->workq)
2136 goto free_host;
2137 }
2138
e5bd7b54
MC
2139 spin_lock_init(&ihost->lock);
2140 ihost->state = ISCSI_HOST_SETUP;
2141 ihost->num_sessions = 0;
2142 init_waitqueue_head(&ihost->session_removal_wq);
a4804cd6 2143 return shost;
32ae763e
MC
2144
2145free_host:
2146 scsi_host_put(shost);
2147 return NULL;
a4804cd6
MC
2148}
2149EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2150
e5bd7b54
MC
2151static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2152{
40a06e75 2153 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
e5bd7b54
MC
2154}
2155
a4804cd6
MC
2156/**
2157 * iscsi_host_remove - remove host and sessions
2158 * @shost: scsi host
2159 *
e5bd7b54
MC
2160 * If there are any sessions left, this will initiate the removal and wait
2161 * for the completion.
a4804cd6
MC
2162 */
2163void iscsi_host_remove(struct Scsi_Host *shost)
2164{
e5bd7b54
MC
2165 struct iscsi_host *ihost = shost_priv(shost);
2166 unsigned long flags;
2167
2168 spin_lock_irqsave(&ihost->lock, flags);
2169 ihost->state = ISCSI_HOST_REMOVED;
2170 spin_unlock_irqrestore(&ihost->lock, flags);
2171
2172 iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
2173 wait_event_interruptible(ihost->session_removal_wq,
2174 ihost->num_sessions == 0);
2175 if (signal_pending(current))
2176 flush_signals(current);
2177
a4804cd6 2178 scsi_remove_host(shost);
32ae763e
MC
2179 if (ihost->workq)
2180 destroy_workqueue(ihost->workq);
75613521 2181}
a4804cd6 2182EXPORT_SYMBOL_GPL(iscsi_host_remove);
7996a778 2183
a4804cd6 2184void iscsi_host_free(struct Scsi_Host *shost)
75613521
MC
2185{
2186 struct iscsi_host *ihost = shost_priv(shost);
7996a778 2187
75613521
MC
2188 kfree(ihost->netdev);
2189 kfree(ihost->hwaddress);
2190 kfree(ihost->initiatorname);
a4804cd6 2191 scsi_host_put(shost);
75613521 2192}
a4804cd6 2193EXPORT_SYMBOL_GPL(iscsi_host_free);
7996a778 2194
e5bd7b54
MC
2195static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
2196{
2197 struct iscsi_host *ihost = shost_priv(shost);
2198 unsigned long flags;
2199
2200 shost = scsi_host_get(shost);
2201 if (!shost) {
2202 printk(KERN_ERR "Invalid state. Cannot notify host removal "
2203 "of session teardown event because host already "
2204 "removed.\n");
2205 return;
2206 }
2207
2208 spin_lock_irqsave(&ihost->lock, flags);
2209 ihost->num_sessions--;
2210 if (ihost->num_sessions == 0)
2211 wake_up(&ihost->session_removal_wq);
2212 spin_unlock_irqrestore(&ihost->lock, flags);
2213 scsi_host_put(shost);
2214}
2215
7996a778
MC
2216/**
2217 * iscsi_session_setup - create iscsi cls session and host and session
7996a778 2218 * @iscsit: iscsi transport template
75613521
MC
2219 * @shost: scsi host
2220 * @cmds_max: session can queue
9c19a7d0 2221 * @cmd_task_size: LLD task private data size
7996a778 2222 * @initial_cmdsn: initial CmdSN
7996a778
MC
2223 *
2224 * This can be used by software iscsi_transports that allocate
2225 * a session per scsi host.
3cf7b233
MC
2226 *
2227 * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
2228 * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
2229 * for nop handling and login/logout requests.
75613521 2230 */
7996a778 2231struct iscsi_cls_session *
75613521 2232iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
3cf7b233 2233 uint16_t cmds_max, int cmd_task_size,
7970634b 2234 uint32_t initial_cmdsn, unsigned int id)
7996a778 2235{
e5bd7b54 2236 struct iscsi_host *ihost = shost_priv(shost);
7996a778
MC
2237 struct iscsi_session *session;
2238 struct iscsi_cls_session *cls_session;
3cf7b233 2239 int cmd_i, scsi_cmds, total_cmds = cmds_max;
e5bd7b54
MC
2240 unsigned long flags;
2241
2242 spin_lock_irqsave(&ihost->lock, flags);
2243 if (ihost->state == ISCSI_HOST_REMOVED) {
2244 spin_unlock_irqrestore(&ihost->lock, flags);
2245 return NULL;
2246 }
2247 ihost->num_sessions++;
2248 spin_unlock_irqrestore(&ihost->lock, flags);
8e9a20ce
MC
2249
2250 if (!total_cmds)
2251 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
3e5c28ad 2252 /*
3cf7b233
MC
2253 * The iscsi layer needs some tasks for nop handling and tmfs,
2254 * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
2255 * + 1 command for scsi IO.
3e5c28ad 2256 */
3cf7b233
MC
2257 if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
2258 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2259 "must be a power of two that is at least %d.\n",
2260 total_cmds, ISCSI_TOTAL_CMDS_MIN);
e5bd7b54 2261 goto dec_session_count;
3cf7b233
MC
2262 }
2263
2264 if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
2265 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2266 "must be a power of 2 less than or equal to %d.\n",
2267 cmds_max, ISCSI_TOTAL_CMDS_MAX);
2268 total_cmds = ISCSI_TOTAL_CMDS_MAX;
2269 }
2270
2271 if (!is_power_of_2(total_cmds)) {
2272 printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
2273 "must be a power of 2.\n", total_cmds);
2274 total_cmds = rounddown_pow_of_two(total_cmds);
2275 if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
2276 return NULL;
2277 printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
2278 total_cmds);
1548271e 2279 }
3cf7b233 2280 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
1548271e 2281
5d91e209
MC
2282 cls_session = iscsi_alloc_session(shost, iscsit,
2283 sizeof(struct iscsi_session));
75613521 2284 if (!cls_session)
e5bd7b54 2285 goto dec_session_count;
75613521
MC
2286 session = cls_session->dd_data;
2287 session->cls_session = cls_session;
7996a778
MC
2288 session->host = shost;
2289 session->state = ISCSI_STATE_FREE;
f6d5180c 2290 session->fast_abort = 1;
4cd49ea1
MC
2291 session->lu_reset_timeout = 15;
2292 session->abort_timeout = 10;
3cf7b233
MC
2293 session->scsi_cmds_max = scsi_cmds;
2294 session->cmds_max = total_cmds;
e0726407 2295 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
7996a778
MC
2296 session->exp_cmdsn = initial_cmdsn + 1;
2297 session->max_cmdsn = initial_cmdsn + 1;
2298 session->max_r2t = 1;
2299 session->tt = iscsit;
6724add1 2300 mutex_init(&session->eh_mutex);
75613521 2301 spin_lock_init(&session->lock);
7996a778
MC
2302
2303 /* initialize SCSI PDU commands pool */
2304 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2305 (void***)&session->cmds,
9c19a7d0 2306 cmd_task_size + sizeof(struct iscsi_task)))
7996a778
MC
2307 goto cmdpool_alloc_fail;
2308
2309 /* pre-format cmds pool with ITT */
2310 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
9c19a7d0 2311 struct iscsi_task *task = session->cmds[cmd_i];
7996a778 2312
9c19a7d0
MC
2313 if (cmd_task_size)
2314 task->dd_data = &task[1];
2315 task->itt = cmd_i;
3bbaaad9 2316 task->state = ISCSI_TASK_FREE;
9c19a7d0 2317 INIT_LIST_HEAD(&task->running);
7996a778
MC
2318 }
2319
f53a88da 2320 if (!try_module_get(iscsit->owner))
75613521 2321 goto module_get_fail;
7996a778 2322
7970634b 2323 if (iscsi_add_session(cls_session, id))
75613521 2324 goto cls_session_fail;
e5bd7b54 2325
7996a778
MC
2326 return cls_session;
2327
2328cls_session_fail:
75613521
MC
2329 module_put(iscsit->owner);
2330module_get_fail:
6320377f 2331 iscsi_pool_free(&session->cmdpool);
7996a778 2332cmdpool_alloc_fail:
75613521 2333 iscsi_free_session(cls_session);
e5bd7b54
MC
2334dec_session_count:
2335 iscsi_host_dec_session_cnt(shost);
7996a778
MC
2336 return NULL;
2337}
2338EXPORT_SYMBOL_GPL(iscsi_session_setup);
2339
2340/**
2341 * iscsi_session_teardown - destroy session, host, and cls_session
75613521 2342 * @cls_session: iscsi session
7996a778 2343 *
75613521
MC
2344 * The driver must have called iscsi_remove_session before
2345 * calling this.
2346 */
7996a778
MC
2347void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2348{
75613521 2349 struct iscsi_session *session = cls_session->dd_data;
63f75cc8 2350 struct module *owner = cls_session->transport->owner;
e5bd7b54 2351 struct Scsi_Host *shost = session->host;
7996a778 2352
6320377f 2353 iscsi_pool_free(&session->cmdpool);
7996a778 2354
b2c64167
MC
2355 kfree(session->password);
2356 kfree(session->password_in);
2357 kfree(session->username);
2358 kfree(session->username_in);
f3ff0c36 2359 kfree(session->targetname);
88dfd340
MC
2360 kfree(session->initiatorname);
2361 kfree(session->ifacename);
f3ff0c36 2362
75613521 2363 iscsi_destroy_session(cls_session);
e5bd7b54 2364 iscsi_host_dec_session_cnt(shost);
63f75cc8 2365 module_put(owner);
7996a778
MC
2366}
2367EXPORT_SYMBOL_GPL(iscsi_session_teardown);
2368
2369/**
2370 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
2371 * @cls_session: iscsi_cls_session
5d91e209 2372 * @dd_size: private driver data size
7996a778 2373 * @conn_idx: cid
5d91e209 2374 */
7996a778 2375struct iscsi_cls_conn *
5d91e209
MC
2376iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2377 uint32_t conn_idx)
7996a778 2378{
75613521 2379 struct iscsi_session *session = cls_session->dd_data;
7996a778
MC
2380 struct iscsi_conn *conn;
2381 struct iscsi_cls_conn *cls_conn;
d36ab6f3 2382 char *data;
7996a778 2383
5d91e209
MC
2384 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2385 conn_idx);
7996a778
MC
2386 if (!cls_conn)
2387 return NULL;
2388 conn = cls_conn->dd_data;
5d91e209 2389 memset(conn, 0, sizeof(*conn) + dd_size);
7996a778 2390
5d91e209 2391 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
7996a778
MC
2392 conn->session = session;
2393 conn->cls_conn = cls_conn;
2394 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
2395 conn->id = conn_idx;
2396 conn->exp_statsn = 0;
843c0a8a 2397 conn->tmf_state = TMF_INITIAL;
f6d5180c
MC
2398
2399 init_timer(&conn->transport_timer);
2400 conn->transport_timer.data = (unsigned long)conn;
2401 conn->transport_timer.function = iscsi_check_transport_timeouts;
2402
843c0a8a 2403 INIT_LIST_HEAD(&conn->mgmtqueue);
3bbaaad9 2404 INIT_LIST_HEAD(&conn->cmdqueue);
843c0a8a 2405 INIT_LIST_HEAD(&conn->requeue);
c4028958 2406 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
7996a778 2407
9c19a7d0 2408 /* allocate login_task used for the login/text sequences */
7996a778 2409 spin_lock_bh(&session->lock);
3e5c28ad 2410 if (!__kfifo_get(session->cmdpool.queue,
9c19a7d0 2411 (void*)&conn->login_task,
7996a778
MC
2412 sizeof(void*))) {
2413 spin_unlock_bh(&session->lock);
9c19a7d0 2414 goto login_task_alloc_fail;
7996a778
MC
2415 }
2416 spin_unlock_bh(&session->lock);
2417
cfeb2cf9
MC
2418 data = (char *) __get_free_pages(GFP_KERNEL,
2419 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
d36ab6f3 2420 if (!data)
9c19a7d0
MC
2421 goto login_task_data_alloc_fail;
2422 conn->login_task->data = conn->data = data;
d36ab6f3 2423
843c0a8a 2424 init_timer(&conn->tmf_timer);
7996a778
MC
2425 init_waitqueue_head(&conn->ehwait);
2426
2427 return cls_conn;
2428
9c19a7d0
MC
2429login_task_data_alloc_fail:
2430 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
d36ab6f3 2431 sizeof(void*));
9c19a7d0 2432login_task_alloc_fail:
7996a778
MC
2433 iscsi_destroy_conn(cls_conn);
2434 return NULL;
2435}
2436EXPORT_SYMBOL_GPL(iscsi_conn_setup);
2437
2438/**
2439 * iscsi_conn_teardown - teardown iscsi connection
2440 * cls_conn: iscsi class connection
2441 *
2442 * TODO: we may need to make this into a two step process
2443 * like scsi-mls remove + put host
2444 */
2445void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
2446{
2447 struct iscsi_conn *conn = cls_conn->dd_data;
2448 struct iscsi_session *session = conn->session;
2449 unsigned long flags;
2450
f6d5180c
MC
2451 del_timer_sync(&conn->transport_timer);
2452
7996a778
MC
2453 spin_lock_bh(&session->lock);
2454 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
2455 if (session->leadconn == conn) {
2456 /*
2457 * leading connection? then give up on recovery.
2458 */
2459 session->state = ISCSI_STATE_TERMINATE;
2460 wake_up(&conn->ehwait);
2461 }
2462 spin_unlock_bh(&session->lock);
2463
7996a778
MC
2464 /*
2465 * Block until all in-progress commands for this connection
2466 * time out or fail.
2467 */
2468 for (;;) {
2469 spin_lock_irqsave(session->host->host_lock, flags);
2470 if (!session->host->host_busy) { /* OK for ERL == 0 */
2471 spin_unlock_irqrestore(session->host->host_lock, flags);
2472 break;
2473 }
2474 spin_unlock_irqrestore(session->host->host_lock, flags);
2475 msleep_interruptible(500);
322d739d
MC
2476 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
2477 "host_busy %d host_failed %d\n",
2478 session->host->host_busy,
2479 session->host->host_failed);
7996a778
MC
2480 /*
2481 * force eh_abort() to unblock
2482 */
2483 wake_up(&conn->ehwait);
2484 }
2485
779ea120 2486 /* flush queued up work because we free the connection below */
843c0a8a 2487 iscsi_suspend_tx(conn);
779ea120 2488
7996a778 2489 spin_lock_bh(&session->lock);
cfeb2cf9
MC
2490 free_pages((unsigned long) conn->data,
2491 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
f3ff0c36 2492 kfree(conn->persistent_address);
9c19a7d0 2493 __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
7996a778 2494 sizeof(void*));
e0726407 2495 if (session->leadconn == conn)
7996a778 2496 session->leadconn = NULL;
7996a778
MC
2497 spin_unlock_bh(&session->lock);
2498
7996a778
MC
2499 iscsi_destroy_conn(cls_conn);
2500}
2501EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
2502
2503int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
2504{
2505 struct iscsi_conn *conn = cls_conn->dd_data;
2506 struct iscsi_session *session = conn->session;
2507
ffd0436e 2508 if (!session) {
322d739d
MC
2509 iscsi_conn_printk(KERN_ERR, conn,
2510 "can't start unbound connection\n");
7996a778
MC
2511 return -EPERM;
2512 }
2513
db98ccde
MC
2514 if ((session->imm_data_en || !session->initial_r2t_en) &&
2515 session->first_burst > session->max_burst) {
322d739d
MC
2516 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2517 "first_burst %d max_burst %d\n",
2518 session->first_burst, session->max_burst);
ffd0436e
MC
2519 return -EINVAL;
2520 }
2521
f6d5180c 2522 if (conn->ping_timeout && !conn->recv_timeout) {
322d739d
MC
2523 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2524 "zero. Using 5 seconds\n.");
f6d5180c
MC
2525 conn->recv_timeout = 5;
2526 }
2527
2528 if (conn->recv_timeout && !conn->ping_timeout) {
322d739d
MC
2529 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2530 "zero. Using 5 seconds.\n");
f6d5180c
MC
2531 conn->ping_timeout = 5;
2532 }
2533
7996a778
MC
2534 spin_lock_bh(&session->lock);
2535 conn->c_stage = ISCSI_CONN_STARTED;
2536 session->state = ISCSI_STATE_LOGGED_IN;
e0726407 2537 session->queued_cmdsn = session->cmdsn;
7996a778 2538
f6d5180c
MC
2539 conn->last_recv = jiffies;
2540 conn->last_ping = jiffies;
2541 if (conn->recv_timeout && conn->ping_timeout)
2542 mod_timer(&conn->transport_timer,
2543 jiffies + (conn->recv_timeout * HZ));
2544
7996a778
MC
2545 switch(conn->stop_stage) {
2546 case STOP_CONN_RECOVER:
2547 /*
2548 * unblock eh_abort() if it is blocked. re-try all
2549 * commands after successful recovery
2550 */
7996a778 2551 conn->stop_stage = 0;
843c0a8a 2552 conn->tmf_state = TMF_INITIAL;
7996a778 2553 session->age++;
8b1d0343
MC
2554 if (session->age == 16)
2555 session->age = 0;
6eabafbe 2556 break;
7996a778 2557 case STOP_CONN_TERM:
7996a778 2558 conn->stop_stage = 0;
7996a778
MC
2559 break;
2560 default:
2561 break;
2562 }
2563 spin_unlock_bh(&session->lock);
2564
75613521 2565 iscsi_unblock_session(session->cls_session);
6eabafbe 2566 wake_up(&conn->ehwait);
7996a778
MC
2567 return 0;
2568}
2569EXPORT_SYMBOL_GPL(iscsi_conn_start);
2570
2571static void
3bbaaad9 2572fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
7996a778 2573{
3bbaaad9
MC
2574 struct iscsi_task *task;
2575 int i;
7996a778 2576
3bbaaad9
MC
2577 for (i = 0; i < conn->session->cmds_max; i++) {
2578 task = conn->session->cmds[i];
2579 if (task->sc)
2580 continue;
7996a778 2581
3bbaaad9
MC
2582 if (task->state == ISCSI_TASK_FREE)
2583 continue;
7996a778 2584
3bbaaad9
MC
2585 ISCSI_DBG_SESSION(conn->session,
2586 "failing mgmt itt 0x%x state %d\n",
2587 task->itt, task->state);
2588 iscsi_complete_task(task);
2589 }
7996a778
MC
2590}
2591
656cffc9
MC
2592static void iscsi_start_session_recovery(struct iscsi_session *session,
2593 struct iscsi_conn *conn, int flag)
7996a778 2594{
ed2abc7f
MC
2595 int old_stop_stage;
2596
6724add1 2597 mutex_lock(&session->eh_mutex);
7996a778 2598 spin_lock_bh(&session->lock);
ed2abc7f 2599 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778 2600 spin_unlock_bh(&session->lock);
6724add1
MC
2601 mutex_unlock(&session->eh_mutex);
2602 return;
2603 }
2604
ed2abc7f
MC
2605 /*
2606 * When this is called for the in_login state, we only want to clean
9c19a7d0 2607 * up the login task and connection. We do not need to block and set
67a61114 2608 * the recovery state again
ed2abc7f 2609 */
67a61114
MC
2610 if (flag == STOP_CONN_TERM)
2611 session->state = ISCSI_STATE_TERMINATE;
2612 else if (conn->stop_stage != STOP_CONN_RECOVER)
2613 session->state = ISCSI_STATE_IN_RECOVERY;
26013ad4 2614 spin_unlock_bh(&session->lock);
ed2abc7f 2615
26013ad4
MC
2616 del_timer_sync(&conn->transport_timer);
2617 iscsi_suspend_tx(conn);
2618
2619 spin_lock_bh(&session->lock);
ed2abc7f 2620 old_stop_stage = conn->stop_stage;
7996a778 2621 conn->stop_stage = flag;
67a61114 2622 conn->c_stage = ISCSI_CONN_STOPPED;
7996a778 2623 spin_unlock_bh(&session->lock);
6724add1 2624
7996a778
MC
2625 /*
2626 * for connection level recovery we should not calculate
2627 * header digest. conn->hdr_size used for optimization
2628 * in hdr_extract() and will be re-negotiated at
2629 * set_param() time.
2630 */
2631 if (flag == STOP_CONN_RECOVER) {
2632 conn->hdrdgst_en = 0;
2633 conn->datadgst_en = 0;
656cffc9 2634 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114 2635 old_stop_stage != STOP_CONN_RECOVER) {
1b2c7af8 2636 ISCSI_DBG_SESSION(session, "blocking session\n");
75613521 2637 iscsi_block_session(session->cls_session);
67a61114 2638 }
7996a778 2639 }
656cffc9 2640
656cffc9
MC
2641 /*
2642 * flush queues.
2643 */
2644 spin_lock_bh(&session->lock);
87cd9eab 2645 if (flag == STOP_CONN_RECOVER)
3bbaaad9 2646 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
56d7fcfa 2647 else
3bbaaad9
MC
2648 fail_scsi_tasks(conn, -1, DID_ERROR);
2649 fail_mgmt_tasks(session, conn);
656cffc9 2650 spin_unlock_bh(&session->lock);
6724add1 2651 mutex_unlock(&session->eh_mutex);
7996a778 2652}
7996a778
MC
2653
2654void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2655{
2656 struct iscsi_conn *conn = cls_conn->dd_data;
2657 struct iscsi_session *session = conn->session;
2658
2659 switch (flag) {
2660 case STOP_CONN_RECOVER:
2661 case STOP_CONN_TERM:
2662 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 2663 break;
7996a778 2664 default:
322d739d
MC
2665 iscsi_conn_printk(KERN_ERR, conn,
2666 "invalid stop flag %d\n", flag);
7996a778
MC
2667 }
2668}
2669EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2670
2671int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2672 struct iscsi_cls_conn *cls_conn, int is_leading)
2673{
75613521 2674 struct iscsi_session *session = cls_session->dd_data;
98644047 2675 struct iscsi_conn *conn = cls_conn->dd_data;
7996a778 2676
7996a778 2677 spin_lock_bh(&session->lock);
7996a778
MC
2678 if (is_leading)
2679 session->leadconn = conn;
98644047 2680 spin_unlock_bh(&session->lock);
7996a778
MC
2681
2682 /*
2683 * Unblock xmitworker(), Login Phase will pass through.
2684 */
2685 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2686 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2687 return 0;
2688}
2689EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2690
5700b1af
MC
2691static int iscsi_switch_str_param(char **param, char *new_val_buf)
2692{
2693 char *new_val;
2694
2695 if (*param) {
2696 if (!strcmp(*param, new_val_buf))
2697 return 0;
2698 }
2699
2700 new_val = kstrdup(new_val_buf, GFP_NOIO);
2701 if (!new_val)
2702 return -ENOMEM;
2703
2704 kfree(*param);
2705 *param = new_val;
2706 return 0;
2707}
a54a52ca
MC
2708
2709int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2710 enum iscsi_param param, char *buf, int buflen)
2711{
2712 struct iscsi_conn *conn = cls_conn->dd_data;
2713 struct iscsi_session *session = conn->session;
2714 uint32_t value;
2715
2716 switch(param) {
843c0a8a
MC
2717 case ISCSI_PARAM_FAST_ABORT:
2718 sscanf(buf, "%d", &session->fast_abort);
2719 break;
f6d5180c
MC
2720 case ISCSI_PARAM_ABORT_TMO:
2721 sscanf(buf, "%d", &session->abort_timeout);
2722 break;
2723 case ISCSI_PARAM_LU_RESET_TMO:
2724 sscanf(buf, "%d", &session->lu_reset_timeout);
2725 break;
2726 case ISCSI_PARAM_PING_TMO:
2727 sscanf(buf, "%d", &conn->ping_timeout);
2728 break;
2729 case ISCSI_PARAM_RECV_TMO:
2730 sscanf(buf, "%d", &conn->recv_timeout);
2731 break;
a54a52ca
MC
2732 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2733 sscanf(buf, "%d", &conn->max_recv_dlength);
2734 break;
2735 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2736 sscanf(buf, "%d", &conn->max_xmit_dlength);
2737 break;
2738 case ISCSI_PARAM_HDRDGST_EN:
2739 sscanf(buf, "%d", &conn->hdrdgst_en);
2740 break;
2741 case ISCSI_PARAM_DATADGST_EN:
2742 sscanf(buf, "%d", &conn->datadgst_en);
2743 break;
2744 case ISCSI_PARAM_INITIAL_R2T_EN:
2745 sscanf(buf, "%d", &session->initial_r2t_en);
2746 break;
2747 case ISCSI_PARAM_MAX_R2T:
2748 sscanf(buf, "%d", &session->max_r2t);
2749 break;
2750 case ISCSI_PARAM_IMM_DATA_EN:
2751 sscanf(buf, "%d", &session->imm_data_en);
2752 break;
2753 case ISCSI_PARAM_FIRST_BURST:
2754 sscanf(buf, "%d", &session->first_burst);
2755 break;
2756 case ISCSI_PARAM_MAX_BURST:
2757 sscanf(buf, "%d", &session->max_burst);
2758 break;
2759 case ISCSI_PARAM_PDU_INORDER_EN:
2760 sscanf(buf, "%d", &session->pdu_inorder_en);
2761 break;
2762 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2763 sscanf(buf, "%d", &session->dataseq_inorder_en);
2764 break;
2765 case ISCSI_PARAM_ERL:
2766 sscanf(buf, "%d", &session->erl);
2767 break;
2768 case ISCSI_PARAM_IFMARKER_EN:
2769 sscanf(buf, "%d", &value);
2770 BUG_ON(value);
2771 break;
2772 case ISCSI_PARAM_OFMARKER_EN:
2773 sscanf(buf, "%d", &value);
2774 BUG_ON(value);
2775 break;
2776 case ISCSI_PARAM_EXP_STATSN:
2777 sscanf(buf, "%u", &conn->exp_statsn);
2778 break;
b2c64167 2779 case ISCSI_PARAM_USERNAME:
5700b1af 2780 return iscsi_switch_str_param(&session->username, buf);
b2c64167 2781 case ISCSI_PARAM_USERNAME_IN:
5700b1af 2782 return iscsi_switch_str_param(&session->username_in, buf);
b2c64167 2783 case ISCSI_PARAM_PASSWORD:
5700b1af 2784 return iscsi_switch_str_param(&session->password, buf);
b2c64167 2785 case ISCSI_PARAM_PASSWORD_IN:
5700b1af 2786 return iscsi_switch_str_param(&session->password_in, buf);
a54a52ca 2787 case ISCSI_PARAM_TARGET_NAME:
5700b1af 2788 return iscsi_switch_str_param(&session->targetname, buf);
a54a52ca
MC
2789 case ISCSI_PARAM_TPGT:
2790 sscanf(buf, "%d", &session->tpgt);
2791 break;
2792 case ISCSI_PARAM_PERSISTENT_PORT:
2793 sscanf(buf, "%d", &conn->persistent_port);
2794 break;
2795 case ISCSI_PARAM_PERSISTENT_ADDRESS:
5700b1af 2796 return iscsi_switch_str_param(&conn->persistent_address, buf);
88dfd340 2797 case ISCSI_PARAM_IFACE_NAME:
5700b1af 2798 return iscsi_switch_str_param(&session->ifacename, buf);
88dfd340 2799 case ISCSI_PARAM_INITIATOR_NAME:
5700b1af 2800 return iscsi_switch_str_param(&session->initiatorname, buf);
a54a52ca
MC
2801 default:
2802 return -ENOSYS;
2803 }
2804
2805 return 0;
2806}
2807EXPORT_SYMBOL_GPL(iscsi_set_param);
2808
2809int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2810 enum iscsi_param param, char *buf)
2811{
75613521 2812 struct iscsi_session *session = cls_session->dd_data;
a54a52ca
MC
2813 int len;
2814
2815 switch(param) {
843c0a8a
MC
2816 case ISCSI_PARAM_FAST_ABORT:
2817 len = sprintf(buf, "%d\n", session->fast_abort);
2818 break;
f6d5180c
MC
2819 case ISCSI_PARAM_ABORT_TMO:
2820 len = sprintf(buf, "%d\n", session->abort_timeout);
2821 break;
2822 case ISCSI_PARAM_LU_RESET_TMO:
2823 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2824 break;
a54a52ca
MC
2825 case ISCSI_PARAM_INITIAL_R2T_EN:
2826 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2827 break;
2828 case ISCSI_PARAM_MAX_R2T:
2829 len = sprintf(buf, "%hu\n", session->max_r2t);
2830 break;
2831 case ISCSI_PARAM_IMM_DATA_EN:
2832 len = sprintf(buf, "%d\n", session->imm_data_en);
2833 break;
2834 case ISCSI_PARAM_FIRST_BURST:
2835 len = sprintf(buf, "%u\n", session->first_burst);
2836 break;
2837 case ISCSI_PARAM_MAX_BURST:
2838 len = sprintf(buf, "%u\n", session->max_burst);
2839 break;
2840 case ISCSI_PARAM_PDU_INORDER_EN:
2841 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2842 break;
2843 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2844 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2845 break;
2846 case ISCSI_PARAM_ERL:
2847 len = sprintf(buf, "%d\n", session->erl);
2848 break;
2849 case ISCSI_PARAM_TARGET_NAME:
2850 len = sprintf(buf, "%s\n", session->targetname);
2851 break;
2852 case ISCSI_PARAM_TPGT:
2853 len = sprintf(buf, "%d\n", session->tpgt);
2854 break;
b2c64167
MC
2855 case ISCSI_PARAM_USERNAME:
2856 len = sprintf(buf, "%s\n", session->username);
2857 break;
2858 case ISCSI_PARAM_USERNAME_IN:
2859 len = sprintf(buf, "%s\n", session->username_in);
2860 break;
2861 case ISCSI_PARAM_PASSWORD:
2862 len = sprintf(buf, "%s\n", session->password);
2863 break;
2864 case ISCSI_PARAM_PASSWORD_IN:
2865 len = sprintf(buf, "%s\n", session->password_in);
2866 break;
88dfd340
MC
2867 case ISCSI_PARAM_IFACE_NAME:
2868 len = sprintf(buf, "%s\n", session->ifacename);
2869 break;
2870 case ISCSI_PARAM_INITIATOR_NAME:
5700b1af 2871 len = sprintf(buf, "%s\n", session->initiatorname);
88dfd340 2872 break;
a54a52ca
MC
2873 default:
2874 return -ENOSYS;
2875 }
2876
2877 return len;
2878}
2879EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2880
2881int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2882 enum iscsi_param param, char *buf)
2883{
2884 struct iscsi_conn *conn = cls_conn->dd_data;
2885 int len;
2886
2887 switch(param) {
f6d5180c
MC
2888 case ISCSI_PARAM_PING_TMO:
2889 len = sprintf(buf, "%u\n", conn->ping_timeout);
2890 break;
2891 case ISCSI_PARAM_RECV_TMO:
2892 len = sprintf(buf, "%u\n", conn->recv_timeout);
2893 break;
a54a52ca
MC
2894 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2895 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2896 break;
2897 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2898 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2899 break;
2900 case ISCSI_PARAM_HDRDGST_EN:
2901 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2902 break;
2903 case ISCSI_PARAM_DATADGST_EN:
2904 len = sprintf(buf, "%d\n", conn->datadgst_en);
2905 break;
2906 case ISCSI_PARAM_IFMARKER_EN:
2907 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2908 break;
2909 case ISCSI_PARAM_OFMARKER_EN:
2910 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2911 break;
2912 case ISCSI_PARAM_EXP_STATSN:
2913 len = sprintf(buf, "%u\n", conn->exp_statsn);
2914 break;
2915 case ISCSI_PARAM_PERSISTENT_PORT:
2916 len = sprintf(buf, "%d\n", conn->persistent_port);
2917 break;
2918 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2919 len = sprintf(buf, "%s\n", conn->persistent_address);
2920 break;
2921 default:
2922 return -ENOSYS;
2923 }
2924
2925 return len;
2926}
2927EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2928
0801c242
MC
2929int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2930 char *buf)
2931{
75613521 2932 struct iscsi_host *ihost = shost_priv(shost);
0801c242
MC
2933 int len;
2934
2935 switch (param) {
d8196ed2 2936 case ISCSI_HOST_PARAM_NETDEV_NAME:
5700b1af 2937 len = sprintf(buf, "%s\n", ihost->netdev);
d8196ed2 2938 break;
0801c242 2939 case ISCSI_HOST_PARAM_HWADDRESS:
5700b1af 2940 len = sprintf(buf, "%s\n", ihost->hwaddress);
0801c242 2941 break;
8ad5781a 2942 case ISCSI_HOST_PARAM_INITIATOR_NAME:
5700b1af 2943 len = sprintf(buf, "%s\n", ihost->initiatorname);
8ad5781a 2944 break;
75613521 2945 case ISCSI_HOST_PARAM_IPADDRESS:
5700b1af 2946 len = sprintf(buf, "%s\n", ihost->local_address);
88dfd340 2947 break;
0801c242
MC
2948 default:
2949 return -ENOSYS;
2950 }
2951
2952 return len;
2953}
2954EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2955
2956int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2957 char *buf, int buflen)
2958{
75613521 2959 struct iscsi_host *ihost = shost_priv(shost);
0801c242
MC
2960
2961 switch (param) {
d8196ed2 2962 case ISCSI_HOST_PARAM_NETDEV_NAME:
5700b1af 2963 return iscsi_switch_str_param(&ihost->netdev, buf);
0801c242 2964 case ISCSI_HOST_PARAM_HWADDRESS:
5700b1af 2965 return iscsi_switch_str_param(&ihost->hwaddress, buf);
8ad5781a 2966 case ISCSI_HOST_PARAM_INITIATOR_NAME:
5700b1af 2967 return iscsi_switch_str_param(&ihost->initiatorname, buf);
0801c242
MC
2968 default:
2969 return -ENOSYS;
2970 }
2971
2972 return 0;
2973}
2974EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2975
7996a778
MC
2976MODULE_AUTHOR("Mike Christie");
2977MODULE_DESCRIPTION("iSCSI library functions");
2978MODULE_LICENSE("GPL");