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