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