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