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