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