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