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