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