[SCSI] libiscsi: Reduce locking contention in fast path
[linux-block.git] / drivers / scsi / libiscsi.c
... / ...
CommitLineData
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>
25#include <linux/kfifo.h>
26#include <linux/delay.h>
27#include <linux/log2.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <asm/unaligned.h>
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
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.");
63
64#define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \
65 do { \
66 if (iscsi_dbg_lib_conn) \
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 { \
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) \
83 iscsi_session_printk(KERN_INFO, _session, \
84 "%s " dbg_fmt, \
85 __func__, ##arg); \
86 } while (0);
87
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
93 if (ihost->workq)
94 queue_work(ihost->workq, &conn->xmitwork);
95}
96EXPORT_SYMBOL_GPL(iscsi_conn_queue_work);
97
98static void __iscsi_update_cmdsn(struct iscsi_session *session,
99 uint32_t exp_cmdsn, uint32_t max_cmdsn)
100{
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))
110 session->exp_cmdsn = exp_cmdsn;
111
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 */
119 if (!list_empty(&session->leadconn->cmdqueue) ||
120 !list_empty(&session->leadconn->mgmtqueue))
121 iscsi_conn_queue_work(session->leadconn);
122 }
123}
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}
130EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
131
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)
146{
147 struct iscsi_conn *conn = task->conn;
148 unsigned int left = r2t->data_length - r2t->sent;
149
150 task->hdr_len = sizeof(struct iscsi_data);
151
152 memset(hdr, 0, sizeof(struct iscsi_data));
153 hdr->ttt = r2t->ttt;
154 hdr->datasn = cpu_to_be32(r2t->datasn);
155 r2t->datasn++;
156 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
157 hdr->lun = task->lun;
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) {
162 hton24(hdr->dlength, conn->max_xmit_dlength);
163 r2t->data_count = conn->max_xmit_dlength;
164 hdr->flags = 0;
165 } else {
166 hton24(hdr->dlength, left);
167 r2t->data_count = left;
168 hdr->flags = ISCSI_FLAG_CMD_FINAL;
169 }
170 conn->dataout_pdus_cnt++;
171}
172EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
173
174static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
175{
176 unsigned exp_len = task->hdr_len + len;
177
178 if (exp_len > task->hdr_max) {
179 WARN_ON(1);
180 return -EINVAL;
181 }
182
183 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
184 task->hdr_len = exp_len;
185 return 0;
186}
187
188/*
189 * make an extended cdb AHS
190 */
191static int iscsi_prep_ecdb_ahs(struct iscsi_task *task)
192{
193 struct scsi_cmnd *cmd = task->sc;
194 unsigned rlen, pad_len;
195 unsigned short ahslength;
196 struct iscsi_ecdb_ahdr *ecdb_ahdr;
197 int rc;
198
199 ecdb_ahdr = iscsi_next_hdr(task);
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
207 rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) +
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
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);
225 return 0;
226}
227
228static int iscsi_prep_bidi_ahs(struct iscsi_task *task)
229{
230 struct scsi_cmnd *sc = task->sc;
231 struct iscsi_rlength_ahdr *rlen_ahdr;
232 int rc;
233
234 rlen_ahdr = iscsi_next_hdr(task);
235 rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr));
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
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));
251 return 0;
252}
253
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 */
284 hdr_lun = scsilun_to_int(&tmf->lun);
285 if (hdr_lun != task->sc->device->lun)
286 return 0;
287 /* fall through */
288 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
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 "
295 "0x%x/0x%x] "
296 "rejected.\n",
297 task->hdr->opcode, opcode,
298 task->itt, task->hdr_itt);
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 "
308 "0x%x/0x%x] fast abort.\n",
309 task->hdr->opcode, opcode,
310 task->itt, task->hdr_itt);
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
336/**
337 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
338 * @task: iscsi task
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 */
343static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
344{
345 struct iscsi_conn *conn = task->conn;
346 struct iscsi_session *session = conn->session;
347 struct scsi_cmnd *sc = task->sc;
348 struct iscsi_scsi_req *hdr;
349 unsigned hdrlength, cmd_len;
350 itt_t itt;
351 int rc;
352
353 rc = iscsi_check_tmf_restrictions(task, ISCSI_OP_SCSI_CMD);
354 if (rc)
355 return rc;
356
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 }
362 hdr = (struct iscsi_scsi_req *)task->hdr;
363 itt = hdr->itt;
364 memset(hdr, 0, sizeof(*hdr));
365
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);
371 task->hdr_len = 0;
372 rc = iscsi_add_hdr(task, sizeof(*hdr));
373 if (rc)
374 return rc;
375 hdr->opcode = ISCSI_OP_SCSI_CMD;
376 hdr->flags = ISCSI_ATTR_SIMPLE;
377 int_to_scsilun(sc->device->lun, &hdr->lun);
378 task->lun = hdr->lun;
379 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
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) {
384 rc = iscsi_prep_ecdb_ahs(task);
385 if (rc)
386 return rc;
387 cmd_len = ISCSI_CDB_SIZE;
388 }
389 memcpy(hdr->cdb, sc->cmnd, cmd_len);
390
391 task->imm_count = 0;
392 if (scsi_bidi_cmnd(sc)) {
393 hdr->flags |= ISCSI_FLAG_CMD_READ;
394 rc = iscsi_prep_bidi_ahs(task);
395 if (rc)
396 return rc;
397 }
398 if (sc->sc_data_direction == DMA_TO_DEVICE) {
399 unsigned out_len = scsi_out(sc)->length;
400 struct iscsi_r2t_info *r2t = &task->unsol_r2t;
401
402 hdr->data_length = cpu_to_be32(out_len);
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 *
414 * r2t data_length bytes to be sent via R2T ack's
415 *
416 * pad_count bytes to be sent as zero-padding
417 */
418 memset(r2t, 0, sizeof(*r2t));
419
420 if (session->imm_data_en) {
421 if (out_len >= session->first_burst)
422 task->imm_count = min(session->first_burst,
423 conn->max_xmit_dlength);
424 else
425 task->imm_count = min(out_len,
426 conn->max_xmit_dlength);
427 hton24(hdr->dlength, task->imm_count);
428 } else
429 zero_data(hdr->dlength);
430
431 if (!session->initial_r2t_en) {
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);
437 }
438
439 if (!task->unsol_r2t.data_length)
440 /* No unsolicit Data-Out's */
441 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
442 } else {
443 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
444 zero_data(hdr->dlength);
445 hdr->data_length = cpu_to_be32(scsi_in(sc)->length);
446
447 if (sc->sc_data_direction == DMA_FROM_DEVICE)
448 hdr->flags |= ISCSI_FLAG_CMD_READ;
449 }
450
451 /* calculate size of additional header segments (AHSs) */
452 hdrlength = task->hdr_len - sizeof(*hdr);
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;
459 hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
460
461 if (session->tt->init_task && session->tt->init_task(task))
462 return -EIO;
463
464 task->state = ISCSI_TASK_RUNNING;
465 session->cmdsn++;
466
467 conn->scsicmd_pdus_cnt++;
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);
477 return 0;
478}
479
480/**
481 * iscsi_free_task - free a task
482 * @task: iscsi cmd task
483 *
484 * Must be called with session back_lock.
485 * This function returns the scsi command to scsi-ml or cleans
486 * up mgmt tasks then returns the task to the pool.
487 */
488static void iscsi_free_task(struct iscsi_task *task)
489{
490 struct iscsi_conn *conn = task->conn;
491 struct iscsi_session *session = conn->session;
492 struct scsi_cmnd *sc = task->sc;
493 int oldstate = task->state;
494
495 ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n",
496 task->itt, task->state, task->sc);
497
498 session->tt->cleanup_task(task);
499 task->state = ISCSI_TASK_FREE;
500 task->sc = NULL;
501 /*
502 * login task is preallocated so do not free
503 */
504 if (conn->login_task == task)
505 return;
506
507 kfifo_in(&session->cmdpool.queue, (void*)&task, sizeof(void*));
508
509 if (sc) {
510 /* SCSI eh reuses commands to verify us */
511 sc->SCp.ptr = NULL;
512 /*
513 * queue command may call this to free the task, so
514 * it will decide how to return sc to scsi-ml.
515 */
516 if (oldstate != ISCSI_TASK_REQUEUE_SCSIQ)
517 sc->scsi_done(sc);
518 }
519}
520
521void __iscsi_get_task(struct iscsi_task *task)
522{
523 atomic_inc(&task->refcount);
524}
525EXPORT_SYMBOL_GPL(__iscsi_get_task);
526
527void __iscsi_put_task(struct iscsi_task *task)
528{
529 if (atomic_dec_and_test(&task->refcount))
530 iscsi_free_task(task);
531}
532EXPORT_SYMBOL_GPL(__iscsi_put_task);
533
534void iscsi_put_task(struct iscsi_task *task)
535{
536 struct iscsi_session *session = task->conn->session;
537
538 /* regular RX path uses back_lock */
539 spin_lock_bh(&session->back_lock);
540 __iscsi_put_task(task);
541 spin_unlock_bh(&session->back_lock);
542}
543EXPORT_SYMBOL_GPL(iscsi_put_task);
544
545/**
546 * iscsi_complete_task - finish a task
547 * @task: iscsi cmd task
548 * @state: state to complete task with
549 *
550 * Must be called with session back_lock.
551 */
552static void iscsi_complete_task(struct iscsi_task *task, int state)
553{
554 struct iscsi_conn *conn = task->conn;
555
556 ISCSI_DBG_SESSION(conn->session,
557 "complete task itt 0x%x state %d sc %p\n",
558 task->itt, task->state, task->sc);
559 if (task->state == ISCSI_TASK_COMPLETED ||
560 task->state == ISCSI_TASK_ABRT_TMF ||
561 task->state == ISCSI_TASK_ABRT_SESS_RECOV ||
562 task->state == ISCSI_TASK_REQUEUE_SCSIQ)
563 return;
564 WARN_ON_ONCE(task->state == ISCSI_TASK_FREE);
565 task->state = state;
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
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 *
589 * Called with session back_lock
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
605/*
606 * session back_lock must be held and if not called for a task that is
607 * still pending or from the xmit thread, then xmit thread must
608 * be suspended.
609 */
610static void fail_scsi_task(struct iscsi_task *task, int err)
611{
612 struct iscsi_conn *conn = task->conn;
613 struct scsi_cmnd *sc;
614 int state;
615
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 */
621 sc = task->sc;
622 if (!sc)
623 return;
624
625 if (task->state == ISCSI_TASK_PENDING) {
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--;
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;
637
638 sc->result = err << 16;
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 }
645
646 /* regular RX path uses back_lock */
647 spin_lock_bh(&conn->session->back_lock);
648 iscsi_complete_task(task, state);
649 spin_unlock_bh(&conn->session->back_lock);
650}
651
652static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
653 struct iscsi_task *task)
654{
655 struct iscsi_session *session = conn->session;
656 struct iscsi_hdr *hdr = task->hdr;
657 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
658 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
659
660 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
661 return -ENOTCONN;
662
663 if (opcode != ISCSI_OP_LOGIN && opcode != ISCSI_OP_TEXT)
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) {
670 /*
671 * TODO: We always use immediate for normal session pdus.
672 * If we start to send tmfs or nops as non-immediate then
673 * we should start checking the cmdsn numbers for mgmt tasks.
674 *
675 * During discovery sessions iscsid sends TEXT as non immediate,
676 * but we always only send one PDU at a time.
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
685 if (session->tt->init_task && session->tt->init_task(task))
686 return -EIO;
687
688 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
689 session->state = ISCSI_STATE_LOGGING_OUT;
690
691 task->state = ISCSI_TASK_RUNNING;
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);
695 return 0;
696}
697
698static struct iscsi_task *
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;
703 struct iscsi_host *ihost = shost_priv(session->host);
704 uint8_t opcode = hdr->opcode & ISCSI_OPCODE_MASK;
705 struct iscsi_task *task;
706 itt_t itt;
707
708 if (session->state == ISCSI_STATE_TERMINATE)
709 return NULL;
710
711 if (opcode == ISCSI_OP_LOGIN || opcode == ISCSI_OP_TEXT) {
712 /*
713 * Login and Text are sent serially, in
714 * request-followed-by-response sequence.
715 * Same task can be used. Same ITT must be used.
716 * Note that login_task is preallocated at conn_create().
717 */
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
724 task = conn->login_task;
725 } else {
726 if (session->state != ISCSI_STATE_LOGGED_IN)
727 return NULL;
728
729 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
730 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
731
732 if (!kfifo_out(&session->cmdpool.queue,
733 (void*)&task, sizeof(void*)))
734 return NULL;
735 }
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 */
741 atomic_set(&task->refcount, 1);
742 task->conn = conn;
743 task->sc = NULL;
744 INIT_LIST_HEAD(&task->running);
745 task->state = ISCSI_TASK_PENDING;
746
747 if (data_size) {
748 memcpy(task->data, data, data_size);
749 task->data_count = data_size;
750 } else
751 task->data_count = 0;
752
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");
757 goto free_task;
758 }
759 }
760
761 itt = task->hdr->itt;
762 task->hdr_len = sizeof(struct iscsi_hdr);
763 memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
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
773 if (!ihost->workq) {
774 if (iscsi_prep_mgmt_task(conn, task))
775 goto free_task;
776
777 if (session->tt->xmit_task(task))
778 goto free_task;
779 } else {
780 list_add_tail(&task->running, &conn->mgmtqueue);
781 iscsi_conn_queue_work(conn);
782 }
783
784 return task;
785
786free_task:
787 /* regular RX path uses back_lock */
788 spin_lock_bh(&session->back_lock);
789 __iscsi_put_task(task);
790 spin_unlock_bh(&session->back_lock);
791 return NULL;
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
801 spin_lock_bh(&session->frwd_lock);
802 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
803 err = -EPERM;
804 spin_unlock_bh(&session->frwd_lock);
805 return err;
806}
807EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
808
809/**
810 * iscsi_cmd_rsp - SCSI Command Response processing
811 * @conn: iscsi connection
812 * @hdr: iscsi header
813 * @task: scsi command task
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
818 * then completes the command and task.
819 **/
820static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
821 struct iscsi_task *task, char *data,
822 int datalen)
823{
824 struct iscsi_scsi_rsp *rhdr = (struct iscsi_scsi_rsp *)hdr;
825 struct iscsi_session *session = conn->session;
826 struct scsi_cmnd *sc = task->sc;
827
828 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
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) {
839 uint16_t senselen;
840
841 if (datalen < 2) {
842invalid_datalen:
843 iscsi_conn_printk(KERN_ERR, conn,
844 "Got CHECK_CONDITION but invalid data "
845 "buffer size of %d\n", datalen);
846 sc->result = DID_BAD_TARGET << 16;
847 goto out;
848 }
849
850 senselen = get_unaligned_be16(data);
851 if (datalen < senselen)
852 goto invalid_datalen;
853
854 memcpy(sc->sense_buffer, data + 2,
855 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
856 ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n",
857 min_t(uint16_t, senselen,
858 SCSI_SENSE_BUFFERSIZE));
859 }
860
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
873 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
874 ISCSI_FLAG_CMD_OVERFLOW)) {
875 int res_count = be32_to_cpu(rhdr->residual_count);
876
877 if (res_count > 0 &&
878 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
879 res_count <= scsi_bufflen(sc)))
880 /* write side for bidi or uni-io set_resid */
881 scsi_set_resid(sc, res_count);
882 else
883 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
884 }
885out:
886 ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n",
887 sc, sc->result, task->itt);
888 conn->scsirsp_pdus_cnt++;
889 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
890}
891
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
908 iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr);
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
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);
926 conn->scsirsp_pdus_cnt++;
927 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
928}
929
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
937 if (conn->tmf_state != TMF_QUEUED)
938 return;
939
940 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
941 conn->tmf_state = TMF_SUCCESS;
942 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
943 conn->tmf_state = TMF_NOT_FOUND;
944 else
945 conn->tmf_state = TMF_FAILED;
946 wake_up(&conn->ehwait);
947}
948
949static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
950{
951 struct iscsi_nopout hdr;
952 struct iscsi_task *task;
953
954 if (!rhdr && conn->ping_task)
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) {
962 hdr.lun = rhdr->lun;
963 hdr.ttt = rhdr->ttt;
964 hdr.itt = RESERVED_ITT;
965 } else
966 hdr.ttt = RESERVED_ITT;
967
968 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
969 if (!task)
970 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
971 else if (!rhdr) {
972 /* only track our nops */
973 conn->ping_task = task;
974 conn->last_ping = jiffies;
975 }
976}
977
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
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;
1003 int opcode, rc = 0;
1004
1005 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
1006
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",
1023 opcode, rejected_pdu.itt);
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",
1029 opcode, rejected_pdu.itt);
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;
1040
1041 if (rejected_pdu.itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
1042 /*
1043 * nop-out in response to target's nop-out rejected.
1044 * Just resend.
1045 */
1046 /* In RX path we are under back lock */
1047 spin_unlock(&conn->session->back_lock);
1048 spin_lock(&conn->session->frwd_lock);
1049 iscsi_send_nopout(conn,
1050 (struct iscsi_nopin*)&rejected_pdu);
1051 spin_unlock(&conn->session->frwd_lock);
1052 spin_lock(&conn->session->back_lock);
1053 } else {
1054 struct iscsi_task *task;
1055 /*
1056 * Our nop as ping got dropped. We know the target
1057 * and transport are ok so just clean up
1058 */
1059 task = iscsi_itt_to_task(conn, rejected_pdu.itt);
1060 if (!task) {
1061 iscsi_conn_printk(KERN_ERR, conn,
1062 "Invalid pdu reject. Could "
1063 "not lookup rejected task.\n");
1064 rc = ISCSI_ERR_BAD_ITT;
1065 } else
1066 rc = iscsi_nop_out_rsp(task,
1067 (struct iscsi_nopin*)&rejected_pdu,
1068 NULL, 0);
1069 }
1070 break;
1071 default:
1072 iscsi_conn_printk(KERN_ERR, conn,
1073 "pdu (op 0x%x itt 0x%x) rejected. Reason "
1074 "code 0x%x\n", rejected_pdu.opcode,
1075 rejected_pdu.itt, reject->reason);
1076 break;
1077 }
1078 return rc;
1079}
1080
1081/**
1082 * iscsi_itt_to_task - look up task by itt
1083 * @conn: iscsi connection
1084 * @itt: itt
1085 *
1086 * This should be used for mgmt tasks like login and nops, or if
1087 * the LDD's itt space does not include the session age.
1088 *
1089 * The session back_lock must be held.
1090 */
1091struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
1092{
1093 struct iscsi_session *session = conn->session;
1094 int i;
1095
1096 if (itt == RESERVED_ITT)
1097 return NULL;
1098
1099 if (session->tt->parse_pdu_itt)
1100 session->tt->parse_pdu_itt(conn, itt, &i, NULL);
1101 else
1102 i = get_itt(itt);
1103 if (i >= session->cmds_max)
1104 return NULL;
1105
1106 return session->cmds[i];
1107}
1108EXPORT_SYMBOL_GPL(iscsi_itt_to_task);
1109
1110/**
1111 * __iscsi_complete_pdu - complete pdu
1112 * @conn: iscsi conn
1113 * @hdr: iscsi header
1114 * @data: data buffer
1115 * @datalen: len of data buffer
1116 *
1117 * Completes pdu processing by freeing any resources allocated at
1118 * queuecommand or send generic. session back_lock must be held and verify
1119 * itt must have been called.
1120 */
1121int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1122 char *data, int datalen)
1123{
1124 struct iscsi_session *session = conn->session;
1125 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
1126 struct iscsi_task *task;
1127 uint32_t itt;
1128
1129 conn->last_recv = jiffies;
1130 rc = iscsi_verify_itt(conn, hdr->itt);
1131 if (rc)
1132 return rc;
1133
1134 if (hdr->itt != RESERVED_ITT)
1135 itt = get_itt(hdr->itt);
1136 else
1137 itt = ~0U;
1138
1139 ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n",
1140 opcode, conn->id, itt, datalen);
1141
1142 if (itt == ~0U) {
1143 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
1144
1145 switch(opcode) {
1146 case ISCSI_OP_NOOP_IN:
1147 if (datalen) {
1148 rc = ISCSI_ERR_PROTO;
1149 break;
1150 }
1151
1152 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
1153 break;
1154
1155 /* In RX path we are under back lock */
1156 spin_unlock(&session->back_lock);
1157 spin_lock(&session->frwd_lock);
1158 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
1159 spin_unlock(&session->frwd_lock);
1160 spin_lock(&session->back_lock);
1161 break;
1162 case ISCSI_OP_REJECT:
1163 rc = iscsi_handle_reject(conn, hdr, data, datalen);
1164 break;
1165 case ISCSI_OP_ASYNC_EVENT:
1166 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
1167 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1168 rc = ISCSI_ERR_CONN_FAILED;
1169 break;
1170 default:
1171 rc = ISCSI_ERR_BAD_OPCODE;
1172 break;
1173 }
1174 goto out;
1175 }
1176
1177 switch(opcode) {
1178 case ISCSI_OP_SCSI_CMD_RSP:
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;
1183 task->last_xfer = jiffies;
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:
1205 iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
1206 break;
1207 case ISCSI_OP_SCSI_DATA_IN:
1208 iscsi_data_in_rsp(conn, hdr, task);
1209 break;
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);
1234 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
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
1244 rc = iscsi_nop_out_rsp(task, (struct iscsi_nopin*)hdr,
1245 data, datalen);
1246 break;
1247 default:
1248 rc = ISCSI_ERR_BAD_OPCODE;
1249 break;
1250 }
1251
1252out:
1253 return rc;
1254recv_pdu:
1255 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
1256 rc = ISCSI_ERR_CONN_FAILED;
1257 iscsi_complete_task(task, ISCSI_TASK_COMPLETED);
1258 return rc;
1259}
1260EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
1261
1262int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
1263 char *data, int datalen)
1264{
1265 int rc;
1266
1267 spin_lock(&conn->session->back_lock);
1268 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
1269 spin_unlock(&conn->session->back_lock);
1270 return rc;
1271}
1272EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
1273
1274int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
1275{
1276 struct iscsi_session *session = conn->session;
1277 int age = 0, i = 0;
1278
1279 if (itt == RESERVED_ITT)
1280 return 0;
1281
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) {
1290 iscsi_conn_printk(KERN_ERR, conn,
1291 "received itt %x expected session age (%x)\n",
1292 (__force u32)itt, session->age);
1293 return ISCSI_ERR_BAD_ITT;
1294 }
1295
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;
1301 }
1302 return 0;
1303}
1304EXPORT_SYMBOL_GPL(iscsi_verify_itt);
1305
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 *
1313 * The session back_lock must be held.
1314 */
1315struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
1316{
1317 struct iscsi_task *task;
1318
1319 if (iscsi_verify_itt(conn, itt))
1320 return NULL;
1321
1322 task = iscsi_itt_to_task(conn, itt);
1323 if (!task || !task->sc)
1324 return NULL;
1325
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);
1330 return NULL;
1331 }
1332
1333 return task;
1334}
1335EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
1336
1337void iscsi_session_failure(struct iscsi_session *session,
1338 enum iscsi_err err)
1339{
1340 struct iscsi_conn *conn;
1341 struct device *dev;
1342
1343 spin_lock_bh(&session->frwd_lock);
1344 conn = session->leadconn;
1345 if (session->state == ISCSI_STATE_TERMINATE || !conn) {
1346 spin_unlock_bh(&session->frwd_lock);
1347 return;
1348 }
1349
1350 dev = get_device(&conn->cls_conn->dev);
1351 spin_unlock_bh(&session->frwd_lock);
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
1367void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
1368{
1369 struct iscsi_session *session = conn->session;
1370
1371 spin_lock_bh(&session->frwd_lock);
1372 if (session->state == ISCSI_STATE_FAILED) {
1373 spin_unlock_bh(&session->frwd_lock);
1374 return;
1375 }
1376
1377 if (conn->stop_stage == 0)
1378 session->state = ISCSI_STATE_FAILED;
1379 spin_unlock_bh(&session->frwd_lock);
1380
1381 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1382 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1383 iscsi_conn_error_event(conn->cls_conn, err);
1384}
1385EXPORT_SYMBOL_GPL(iscsi_conn_failure);
1386
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 */
1394 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
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);
1399 return -ENOSPC;
1400 }
1401 return 0;
1402}
1403
1404static int iscsi_xmit_task(struct iscsi_conn *conn)
1405{
1406 struct iscsi_task *task = conn->task;
1407 int rc;
1408
1409 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx))
1410 return -ENODATA;
1411
1412 __iscsi_get_task(task);
1413 spin_unlock_bh(&conn->session->frwd_lock);
1414 rc = conn->session->tt->xmit_task(task);
1415 spin_lock_bh(&conn->session->frwd_lock);
1416 if (!rc) {
1417 /* done with this task */
1418 task->last_xfer = jiffies;
1419 conn->task = NULL;
1420 }
1421 /* regular RX path uses back_lock */
1422 spin_lock_bh(&conn->session->back_lock);
1423 __iscsi_put_task(task);
1424 spin_unlock_bh(&conn->session->back_lock);
1425 return rc;
1426}
1427
1428/**
1429 * iscsi_requeue_task - requeue task to run from session workqueue
1430 * @task: task to requeue
1431 *
1432 * LLDs that need to run a task from the session workqueue should call
1433 * this. The session frwd_lock must be held. This should only be called
1434 * by software drivers.
1435 */
1436void iscsi_requeue_task(struct iscsi_task *task)
1437{
1438 struct iscsi_conn *conn = task->conn;
1439
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);
1446 iscsi_conn_queue_work(conn);
1447}
1448EXPORT_SYMBOL_GPL(iscsi_requeue_task);
1449
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{
1461 struct iscsi_task *task;
1462 int rc = 0;
1463
1464 spin_lock_bh(&conn->session->frwd_lock);
1465 if (test_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx)) {
1466 ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n");
1467 spin_unlock_bh(&conn->session->frwd_lock);
1468 return -ENODATA;
1469 }
1470
1471 if (conn->task) {
1472 rc = iscsi_xmit_task(conn);
1473 if (rc)
1474 goto done;
1475 }
1476
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:
1483 while (!list_empty(&conn->mgmtqueue)) {
1484 conn->task = list_entry(conn->mgmtqueue.next,
1485 struct iscsi_task, running);
1486 list_del_init(&conn->task->running);
1487 if (iscsi_prep_mgmt_task(conn, conn->task)) {
1488 /* regular RX path uses back_lock */
1489 spin_lock_bh(&conn->session->back_lock);
1490 __iscsi_put_task(conn->task);
1491 spin_unlock_bh(&conn->session->back_lock);
1492 conn->task = NULL;
1493 continue;
1494 }
1495 rc = iscsi_xmit_task(conn);
1496 if (rc)
1497 goto done;
1498 }
1499
1500 /* process pending command queue */
1501 while (!list_empty(&conn->cmdqueue)) {
1502 conn->task = list_entry(conn->cmdqueue.next, struct iscsi_task,
1503 running);
1504 list_del_init(&conn->task->running);
1505 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
1506 fail_scsi_task(conn->task, DID_IMM_RETRY);
1507 continue;
1508 }
1509 rc = iscsi_prep_scsi_cmd_pdu(conn->task);
1510 if (rc) {
1511 if (rc == -ENOMEM || rc == -EACCES) {
1512 list_add_tail(&conn->task->running,
1513 &conn->cmdqueue);
1514 conn->task = NULL;
1515 goto done;
1516 } else
1517 fail_scsi_task(conn->task, DID_ABORT);
1518 continue;
1519 }
1520 rc = iscsi_xmit_task(conn);
1521 if (rc)
1522 goto done;
1523 /*
1524 * we could continuously get new task requests so
1525 * we need to check the mgmt queue for nops that need to
1526 * be sent to aviod starvation
1527 */
1528 if (!list_empty(&conn->mgmtqueue))
1529 goto check_mgmt;
1530 }
1531
1532 while (!list_empty(&conn->requeue)) {
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
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;
1545 list_del_init(&conn->task->running);
1546 conn->task->state = ISCSI_TASK_RUNNING;
1547 rc = iscsi_xmit_task(conn);
1548 if (rc)
1549 goto done;
1550 if (!list_empty(&conn->mgmtqueue))
1551 goto check_mgmt;
1552 }
1553 spin_unlock_bh(&conn->session->frwd_lock);
1554 return -ENODATA;
1555
1556done:
1557 spin_unlock_bh(&conn->session->frwd_lock);
1558 return rc;
1559}
1560
1561static void iscsi_xmitworker(struct work_struct *work)
1562{
1563 struct iscsi_conn *conn =
1564 container_of(work, struct iscsi_conn, xmitwork);
1565 int rc;
1566 /*
1567 * serialize Xmit worker on a per-connection basis.
1568 */
1569 do {
1570 rc = iscsi_data_xmit(conn);
1571 } while (rc >= 0 || rc == -EAGAIN);
1572}
1573
1574static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
1575 struct scsi_cmnd *sc)
1576{
1577 struct iscsi_task *task;
1578
1579 if (!kfifo_out(&conn->session->cmdpool.queue,
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;
1590 task->have_checked_conn = false;
1591 task->last_timeout = jiffies;
1592 task->last_xfer = jiffies;
1593 INIT_LIST_HEAD(&task->running);
1594 return task;
1595}
1596
1597enum {
1598 FAILURE_BAD_HOST = 1,
1599 FAILURE_SESSION_FAILED,
1600 FAILURE_SESSION_FREED,
1601 FAILURE_WINDOW_CLOSED,
1602 FAILURE_OOM,
1603 FAILURE_SESSION_TERMINATE,
1604 FAILURE_SESSION_IN_RECOVERY,
1605 FAILURE_SESSION_RECOVERY_TIMEOUT,
1606 FAILURE_SESSION_LOGGING_OUT,
1607 FAILURE_SESSION_NOT_READY,
1608};
1609
1610int iscsi_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc)
1611{
1612 struct iscsi_cls_session *cls_session;
1613 struct iscsi_host *ihost;
1614 int reason = 0;
1615 struct iscsi_session *session;
1616 struct iscsi_conn *conn;
1617 struct iscsi_task *task = NULL;
1618
1619 sc->result = 0;
1620 sc->SCp.ptr = NULL;
1621
1622 ihost = shost_priv(host);
1623
1624 cls_session = starget_to_session(scsi_target(sc->device));
1625 session = cls_session->dd_data;
1626 spin_lock_bh(&session->frwd_lock);
1627
1628 reason = iscsi_session_chkready(cls_session);
1629 if (reason) {
1630 sc->result = reason;
1631 goto fault;
1632 }
1633
1634 if (session->state != ISCSI_STATE_LOGGED_IN) {
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 */
1641 switch (session->state) {
1642 case ISCSI_STATE_FAILED:
1643 case ISCSI_STATE_IN_RECOVERY:
1644 reason = FAILURE_SESSION_IN_RECOVERY;
1645 sc->result = DID_IMM_RETRY << 16;
1646 break;
1647 case ISCSI_STATE_LOGGING_OUT:
1648 reason = FAILURE_SESSION_LOGGING_OUT;
1649 sc->result = DID_IMM_RETRY << 16;
1650 break;
1651 case ISCSI_STATE_RECOVERY_FAILED:
1652 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
1653 sc->result = DID_TRANSPORT_FAILFAST << 16;
1654 break;
1655 case ISCSI_STATE_TERMINATE:
1656 reason = FAILURE_SESSION_TERMINATE;
1657 sc->result = DID_NO_CONNECT << 16;
1658 break;
1659 default:
1660 reason = FAILURE_SESSION_FREED;
1661 sc->result = DID_NO_CONNECT << 16;
1662 }
1663 goto fault;
1664 }
1665
1666 conn = session->leadconn;
1667 if (!conn) {
1668 reason = FAILURE_SESSION_FREED;
1669 sc->result = DID_NO_CONNECT << 16;
1670 goto fault;
1671 }
1672
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
1679 if (iscsi_check_cmdsn_window_closed(conn)) {
1680 reason = FAILURE_WINDOW_CLOSED;
1681 goto reject;
1682 }
1683
1684 task = iscsi_alloc_task(conn, sc);
1685 if (!task) {
1686 reason = FAILURE_OOM;
1687 goto reject;
1688 }
1689
1690 if (!ihost->workq) {
1691 reason = iscsi_prep_scsi_cmd_pdu(task);
1692 if (reason) {
1693 if (reason == -ENOMEM || reason == -EACCES) {
1694 reason = FAILURE_OOM;
1695 goto prepd_reject;
1696 } else {
1697 sc->result = DID_ABORT << 16;
1698 goto prepd_fault;
1699 }
1700 }
1701 if (session->tt->xmit_task(task)) {
1702 session->cmdsn--;
1703 reason = FAILURE_SESSION_NOT_READY;
1704 goto prepd_reject;
1705 }
1706 } else {
1707 list_add_tail(&task->running, &conn->cmdqueue);
1708 iscsi_conn_queue_work(conn);
1709 }
1710
1711 session->queued_cmdsn++;
1712 spin_unlock_bh(&session->frwd_lock);
1713 return 0;
1714
1715prepd_reject:
1716 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1717reject:
1718 spin_unlock_bh(&session->frwd_lock);
1719 ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n",
1720 sc->cmnd[0], reason);
1721 return SCSI_MLQUEUE_TARGET_BUSY;
1722
1723prepd_fault:
1724 iscsi_complete_task(task, ISCSI_TASK_REQUEUE_SCSIQ);
1725fault:
1726 spin_unlock_bh(&session->frwd_lock);
1727 ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n",
1728 sc->cmnd[0], reason);
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 }
1735 sc->scsi_done(sc);
1736 return 0;
1737}
1738EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1739
1740int iscsi_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
1741{
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:
1753 return -EOPNOTSUPP;
1754 }
1755 return sdev->queue_depth;
1756}
1757EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1758
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
1769static void iscsi_tmf_timedout(unsigned long data)
1770{
1771 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1772 struct iscsi_session *session = conn->session;
1773
1774 spin_lock(&session->frwd_lock);
1775 if (conn->tmf_state == TMF_QUEUED) {
1776 conn->tmf_state = TMF_TIMEDOUT;
1777 ISCSI_DBG_EH(session, "tmf timedout\n");
1778 /* unblock eh_abort() */
1779 wake_up(&conn->ehwait);
1780 }
1781 spin_unlock(&session->frwd_lock);
1782}
1783
1784static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1785 struct iscsi_tm *hdr, int age,
1786 int timeout)
1787{
1788 struct iscsi_session *session = conn->session;
1789 struct iscsi_task *task;
1790
1791 task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1792 NULL, 0);
1793 if (!task) {
1794 spin_unlock_bh(&session->frwd_lock);
1795 iscsi_conn_printk(KERN_ERR, conn, "Could not send TMF.\n");
1796 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1797 spin_lock_bh(&session->frwd_lock);
1798 return -EPERM;
1799 }
1800 conn->tmfcmd_pdus_cnt++;
1801 conn->tmf_timer.expires = timeout * HZ + jiffies;
1802 conn->tmf_timer.function = iscsi_tmf_timedout;
1803 conn->tmf_timer.data = (unsigned long)conn;
1804 add_timer(&conn->tmf_timer);
1805 ISCSI_DBG_EH(session, "tmf set timeout\n");
1806
1807 spin_unlock_bh(&session->frwd_lock);
1808 mutex_unlock(&session->eh_mutex);
1809
1810 /*
1811 * block eh thread until:
1812 *
1813 * 1) tmf response
1814 * 2) tmf timeout
1815 * 3) session is terminated or restarted or userspace has
1816 * given up on recovery
1817 */
1818 wait_event_interruptible(conn->ehwait, age != session->age ||
1819 session->state != ISCSI_STATE_LOGGED_IN ||
1820 conn->tmf_state != TMF_QUEUED);
1821 if (signal_pending(current))
1822 flush_signals(current);
1823 del_timer_sync(&conn->tmf_timer);
1824
1825 mutex_lock(&session->eh_mutex);
1826 spin_lock_bh(&session->frwd_lock);
1827 /* if the session drops it will clean up the task */
1828 if (age != session->age ||
1829 session->state != ISCSI_STATE_LOGGED_IN)
1830 return -ENOTCONN;
1831 return 0;
1832}
1833
1834/*
1835 * Fail commands. session lock held and recv side suspended and xmit
1836 * thread flushed
1837 */
1838static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun,
1839 int error)
1840{
1841 struct iscsi_task *task;
1842 int i;
1843
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;
1848
1849 if (lun != -1 && lun != task->sc->device->lun)
1850 continue;
1851
1852 ISCSI_DBG_SESSION(conn->session,
1853 "failing sc %p itt 0x%x state %d\n",
1854 task->sc, task->itt, task->state);
1855 fail_scsi_task(task, error);
1856 }
1857}
1858
1859/**
1860 * iscsi_suspend_queue - suspend iscsi_queuecommand
1861 * @conn: iscsi conn to stop queueing IO on
1862 *
1863 * This grabs the session frwd_lock to make sure no one is in
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{
1872 spin_lock_bh(&conn->session->frwd_lock);
1873 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1874 spin_unlock_bh(&conn->session->frwd_lock);
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 */
1886void iscsi_suspend_tx(struct iscsi_conn *conn)
1887{
1888 struct Scsi_Host *shost = conn->session->host;
1889 struct iscsi_host *ihost = shost_priv(shost);
1890
1891 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1892 if (ihost->workq)
1893 flush_workqueue(ihost->workq);
1894}
1895EXPORT_SYMBOL_GPL(iscsi_suspend_tx);
1896
1897static void iscsi_start_tx(struct iscsi_conn *conn)
1898{
1899 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1900 iscsi_conn_queue_work(conn);
1901}
1902
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
1919static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc)
1920{
1921 enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
1922 struct iscsi_task *task = NULL, *running_task;
1923 struct iscsi_cls_session *cls_session;
1924 struct iscsi_session *session;
1925 struct iscsi_conn *conn;
1926 int i;
1927
1928 cls_session = starget_to_session(scsi_target(sc->device));
1929 session = cls_session->dd_data;
1930
1931 ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc);
1932
1933 spin_lock(&session->frwd_lock);
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
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 */
1949 rc = BLK_EH_RESET_TIMER;
1950 goto done;
1951 }
1952
1953 conn = session->leadconn;
1954 if (!conn) {
1955 /* In the middle of shuting down */
1956 rc = BLK_EH_RESET_TIMER;
1957 goto done;
1958 }
1959
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 */
1967 if (time_after(task->last_xfer, task->last_timeout)) {
1968 ISCSI_DBG_EH(session, "Command making progress. Asking "
1969 "scsi-ml for more time to complete. "
1970 "Last data xfer at %lu. Last timeout was at "
1971 "%lu\n.", task->last_xfer, task->last_timeout);
1972 task->have_checked_conn = false;
1973 rc = BLK_EH_RESET_TIMER;
1974 goto done;
1975 }
1976
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 */
1983 if (iscsi_has_ping_timed_out(conn)) {
1984 rc = BLK_EH_RESET_TIMER;
1985 goto done;
1986 }
1987
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
2025 /* Assumes nop timeout is shorter than scsi cmd timeout */
2026 if (task->have_checked_conn)
2027 goto done;
2028
2029 /*
2030 * Checking the transport already or nop from a cmd timeout still
2031 * running
2032 */
2033 if (conn->ping_task) {
2034 task->have_checked_conn = true;
2035 rc = BLK_EH_RESET_TIMER;
2036 goto done;
2037 }
2038
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
2044done:
2045 if (task)
2046 task->last_timeout = jiffies;
2047 spin_unlock(&session->frwd_lock);
2048 ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ?
2049 "timer reset" : "nh");
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;
2057 unsigned long recv_timeout, next_timeout = 0, last_recv;
2058
2059 spin_lock(&session->frwd_lock);
2060 if (session->state != ISCSI_STATE_LOGGED_IN)
2061 goto done;
2062
2063 recv_timeout = conn->recv_timeout;
2064 if (!recv_timeout)
2065 goto done;
2066
2067 recv_timeout *= HZ;
2068 last_recv = conn->last_recv;
2069
2070 if (iscsi_has_ping_timed_out(conn)) {
2071 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
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);
2076 spin_unlock(&session->frwd_lock);
2077 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
2078 return;
2079 }
2080
2081 if (time_before_eq(last_recv + recv_timeout, jiffies)) {
2082 /* send a ping to try to provoke some traffic */
2083 ISCSI_DBG_CONN(conn, "Sending nopout as ping\n");
2084 iscsi_send_nopout(conn, NULL);
2085 next_timeout = conn->last_ping + (conn->ping_timeout * HZ);
2086 } else
2087 next_timeout = last_recv + recv_timeout;
2088
2089 ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout);
2090 mod_timer(&conn->transport_timer, next_timeout);
2091done:
2092 spin_unlock(&session->frwd_lock);
2093}
2094
2095static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
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;
2102 hdr->lun = task->lun;
2103 hdr->rtt = task->hdr_itt;
2104 hdr->refcmdsn = task->cmdsn;
2105}
2106
2107int iscsi_eh_abort(struct scsi_cmnd *sc)
2108{
2109 struct iscsi_cls_session *cls_session;
2110 struct iscsi_session *session;
2111 struct iscsi_conn *conn;
2112 struct iscsi_task *task;
2113 struct iscsi_tm *hdr;
2114 int rc, age;
2115
2116 cls_session = starget_to_session(scsi_target(sc->device));
2117 session = cls_session->dd_data;
2118
2119 ISCSI_DBG_EH(session, "aborting sc %p\n", sc);
2120
2121 mutex_lock(&session->eh_mutex);
2122 spin_lock_bh(&session->frwd_lock);
2123 /*
2124 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
2125 * got the command.
2126 */
2127 if (!sc->SCp.ptr) {
2128 ISCSI_DBG_EH(session, "sc never reached iscsi layer or "
2129 "it completed.\n");
2130 spin_unlock_bh(&session->frwd_lock);
2131 mutex_unlock(&session->eh_mutex);
2132 return SUCCESS;
2133 }
2134
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 */
2139 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
2140 sc->SCp.phase != session->age) {
2141 spin_unlock_bh(&session->frwd_lock);
2142 mutex_unlock(&session->eh_mutex);
2143 ISCSI_DBG_EH(session, "failing abort due to dropped "
2144 "session.\n");
2145 return FAILED;
2146 }
2147
2148 conn = session->leadconn;
2149 conn->eh_abort_cnt++;
2150 age = session->age;
2151
2152 task = (struct iscsi_task *)sc->SCp.ptr;
2153 ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n",
2154 sc, task->itt);
2155
2156 /* task completed before time out */
2157 if (!task->sc) {
2158 ISCSI_DBG_EH(session, "sc completed while abort in progress\n");
2159 goto success;
2160 }
2161
2162 if (task->state == ISCSI_TASK_PENDING) {
2163 fail_scsi_task(task, DID_ABORT);
2164 goto success;
2165 }
2166
2167 /* only have one tmf outstanding at a time */
2168 if (conn->tmf_state != TMF_INITIAL)
2169 goto failed;
2170 conn->tmf_state = TMF_QUEUED;
2171
2172 hdr = &conn->tmhdr;
2173 iscsi_prep_abort_task_pdu(task, hdr);
2174
2175 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
2176 rc = FAILED;
2177 goto failed;
2178 }
2179
2180 switch (conn->tmf_state) {
2181 case TMF_SUCCESS:
2182 spin_unlock_bh(&session->frwd_lock);
2183 /*
2184 * stop tx side incase the target had sent a abort rsp but
2185 * the initiator was still writing out data.
2186 */
2187 iscsi_suspend_tx(conn);
2188 /*
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.
2192 */
2193 spin_lock_bh(&session->frwd_lock);
2194 fail_scsi_task(task, DID_ABORT);
2195 conn->tmf_state = TMF_INITIAL;
2196 memset(hdr, 0, sizeof(*hdr));
2197 spin_unlock_bh(&session->frwd_lock);
2198 iscsi_start_tx(conn);
2199 goto success_unlocked;
2200 case TMF_TIMEDOUT:
2201 spin_unlock_bh(&session->frwd_lock);
2202 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2203 goto failed_unlocked;
2204 case TMF_NOT_FOUND:
2205 if (!sc->SCp.ptr) {
2206 conn->tmf_state = TMF_INITIAL;
2207 memset(hdr, 0, sizeof(*hdr));
2208 /* task completed before tmf abort response */
2209 ISCSI_DBG_EH(session, "sc completed while abort in "
2210 "progress\n");
2211 goto success;
2212 }
2213 /* fall through */
2214 default:
2215 conn->tmf_state = TMF_INITIAL;
2216 goto failed;
2217 }
2218
2219success:
2220 spin_unlock_bh(&session->frwd_lock);
2221success_unlocked:
2222 ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n",
2223 sc, task->itt);
2224 mutex_unlock(&session->eh_mutex);
2225 return SUCCESS;
2226
2227failed:
2228 spin_unlock_bh(&session->frwd_lock);
2229failed_unlocked:
2230 ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc,
2231 task ? task->itt : 0);
2232 mutex_unlock(&session->eh_mutex);
2233 return FAILED;
2234}
2235EXPORT_SYMBOL_GPL(iscsi_eh_abort);
2236
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;
2243 int_to_scsilun(sc->device->lun, &hdr->lun);
2244 hdr->rtt = RESERVED_ITT;
2245}
2246
2247int iscsi_eh_device_reset(struct scsi_cmnd *sc)
2248{
2249 struct iscsi_cls_session *cls_session;
2250 struct iscsi_session *session;
2251 struct iscsi_conn *conn;
2252 struct iscsi_tm *hdr;
2253 int rc = FAILED;
2254
2255 cls_session = starget_to_session(scsi_target(sc->device));
2256 session = cls_session->dd_data;
2257
2258 ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
2259
2260 mutex_lock(&session->eh_mutex);
2261 spin_lock_bh(&session->frwd_lock);
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
2278 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
2279 session->lu_reset_timeout)) {
2280 rc = FAILED;
2281 goto unlock;
2282 }
2283
2284 switch (conn->tmf_state) {
2285 case TMF_SUCCESS:
2286 break;
2287 case TMF_TIMEDOUT:
2288 spin_unlock_bh(&session->frwd_lock);
2289 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2290 goto done;
2291 default:
2292 conn->tmf_state = TMF_INITIAL;
2293 goto unlock;
2294 }
2295
2296 rc = SUCCESS;
2297 spin_unlock_bh(&session->frwd_lock);
2298
2299 iscsi_suspend_tx(conn);
2300
2301 spin_lock_bh(&session->frwd_lock);
2302 memset(hdr, 0, sizeof(*hdr));
2303 fail_scsi_tasks(conn, sc->device->lun, DID_ERROR);
2304 conn->tmf_state = TMF_INITIAL;
2305 spin_unlock_bh(&session->frwd_lock);
2306
2307 iscsi_start_tx(conn);
2308 goto done;
2309
2310unlock:
2311 spin_unlock_bh(&session->frwd_lock);
2312done:
2313 ISCSI_DBG_EH(session, "dev reset result = %s\n",
2314 rc == SUCCESS ? "SUCCESS" : "FAILED");
2315 mutex_unlock(&session->eh_mutex);
2316 return rc;
2317}
2318EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
2319
2320void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
2321{
2322 struct iscsi_session *session = cls_session->dd_data;
2323
2324 spin_lock_bh(&session->frwd_lock);
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 }
2330 spin_unlock_bh(&session->frwd_lock);
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 */
2341int iscsi_eh_session_reset(struct scsi_cmnd *sc)
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);
2352 spin_lock_bh(&session->frwd_lock);
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);
2359 spin_unlock_bh(&session->frwd_lock);
2360 mutex_unlock(&session->eh_mutex);
2361 return FAILED;
2362 }
2363
2364 spin_unlock_bh(&session->frwd_lock);
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 */
2370 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
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);
2381 spin_lock_bh(&session->frwd_lock);
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;
2388 spin_unlock_bh(&session->frwd_lock);
2389 mutex_unlock(&session->eh_mutex);
2390 return SUCCESS;
2391}
2392EXPORT_SYMBOL_GPL(iscsi_eh_session_reset);
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 *
2407 * This will attempt to send a warm target reset.
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);
2424 spin_lock_bh(&session->frwd_lock);
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:
2451 spin_unlock_bh(&session->frwd_lock);
2452 iscsi_conn_failure(conn, ISCSI_ERR_SCSI_EH_SESSION_RST);
2453 goto done;
2454 default:
2455 conn->tmf_state = TMF_INITIAL;
2456 goto unlock;
2457 }
2458
2459 rc = SUCCESS;
2460 spin_unlock_bh(&session->frwd_lock);
2461
2462 iscsi_suspend_tx(conn);
2463
2464 spin_lock_bh(&session->frwd_lock);
2465 memset(hdr, 0, sizeof(*hdr));
2466 fail_scsi_tasks(conn, -1, DID_ERROR);
2467 conn->tmf_state = TMF_INITIAL;
2468 spin_unlock_bh(&session->frwd_lock);
2469
2470 iscsi_start_tx(conn);
2471 goto done;
2472
2473unlock:
2474 spin_unlock_bh(&session->frwd_lock);
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);
2479 return rc;
2480}
2481EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
2482
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);
2495 if (rc == FAILED)
2496 rc = iscsi_eh_session_reset(sc);
2497 return rc;
2498}
2499EXPORT_SYMBOL_GPL(iscsi_eh_recover_target);
2500
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 */
2507int
2508iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
2509{
2510 int i, num_arrays = 1;
2511
2512 memset(q, 0, sizeof(*q));
2513
2514 q->max = max;
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)
2522 return -ENOMEM;
2523
2524 kfifo_init(&q->queue, (void*)q->pool, max * sizeof(void*));
2525
2526 for (i = 0; i < max; i++) {
2527 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
2528 if (q->pool[i] == NULL) {
2529 q->max = i;
2530 goto enomem;
2531 }
2532 kfifo_in(&q->queue, (void*)&q->pool[i], sizeof(void*));
2533 }
2534
2535 if (items) {
2536 *items = q->pool + max;
2537 memcpy(*items, q->pool, max * sizeof(void *));
2538 }
2539
2540 return 0;
2541
2542enomem:
2543 iscsi_pool_free(q);
2544 return -ENOMEM;
2545}
2546EXPORT_SYMBOL_GPL(iscsi_pool_init);
2547
2548void iscsi_pool_free(struct iscsi_pool *q)
2549{
2550 int i;
2551
2552 for (i = 0; i < q->max; i++)
2553 kfree(q->pool[i]);
2554 kfree(q->pool);
2555}
2556EXPORT_SYMBOL_GPL(iscsi_pool_free);
2557
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{
2568 if (!shost->can_queue)
2569 shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
2570
2571 if (!shost->cmd_per_lun)
2572 shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN;
2573
2574 if (!shost->transportt->eh_timed_out)
2575 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
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
2584 * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue
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,
2590 int dd_data_size, bool xmit_can_sleep)
2591{
2592 struct Scsi_Host *shost;
2593 struct iscsi_host *ihost;
2594
2595 shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
2596 if (!shost)
2597 return NULL;
2598 ihost = shost_priv(shost);
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
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);
2612 return shost;
2613
2614free_host:
2615 scsi_host_put(shost);
2616 return NULL;
2617}
2618EXPORT_SYMBOL_GPL(iscsi_host_alloc);
2619
2620static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
2621{
2622 iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST);
2623}
2624
2625/**
2626 * iscsi_host_remove - remove host and sessions
2627 * @shost: scsi host
2628 *
2629 * If there are any sessions left, this will initiate the removal and wait
2630 * for the completion.
2631 */
2632void iscsi_host_remove(struct Scsi_Host *shost)
2633{
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
2647 scsi_remove_host(shost);
2648 if (ihost->workq)
2649 destroy_workqueue(ihost->workq);
2650}
2651EXPORT_SYMBOL_GPL(iscsi_host_remove);
2652
2653void iscsi_host_free(struct Scsi_Host *shost)
2654{
2655 struct iscsi_host *ihost = shost_priv(shost);
2656
2657 kfree(ihost->netdev);
2658 kfree(ihost->hwaddress);
2659 kfree(ihost->initiatorname);
2660 scsi_host_put(shost);
2661}
2662EXPORT_SYMBOL_GPL(iscsi_host_free);
2663
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
2685/**
2686 * iscsi_session_setup - create iscsi cls session and host and session
2687 * @iscsit: iscsi transport template
2688 * @shost: scsi host
2689 * @cmds_max: session can queue
2690 * @cmd_task_size: LLD task private data size
2691 * @initial_cmdsn: initial CmdSN
2692 *
2693 * This can be used by software iscsi_transports that allocate
2694 * a session per scsi host.
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.
2699 */
2700struct iscsi_cls_session *
2701iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
2702 uint16_t cmds_max, int dd_size, int cmd_task_size,
2703 uint32_t initial_cmdsn, unsigned int id)
2704{
2705 struct iscsi_host *ihost = shost_priv(shost);
2706 struct iscsi_session *session;
2707 struct iscsi_cls_session *cls_session;
2708 int cmd_i, scsi_cmds, total_cmds = cmds_max;
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);
2718
2719 if (!total_cmds)
2720 total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
2721 /*
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.
2725 */
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);
2730 goto dec_session_count;
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);
2748 }
2749 scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
2750
2751 cls_session = iscsi_alloc_session(shost, iscsit,
2752 sizeof(struct iscsi_session) +
2753 dd_size);
2754 if (!cls_session)
2755 goto dec_session_count;
2756 session = cls_session->dd_data;
2757 session->cls_session = cls_session;
2758 session->host = shost;
2759 session->state = ISCSI_STATE_FREE;
2760 session->fast_abort = 1;
2761 session->tgt_reset_timeout = 30;
2762 session->lu_reset_timeout = 15;
2763 session->abort_timeout = 10;
2764 session->scsi_cmds_max = scsi_cmds;
2765 session->cmds_max = total_cmds;
2766 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
2767 session->exp_cmdsn = initial_cmdsn + 1;
2768 session->max_cmdsn = initial_cmdsn + 1;
2769 session->max_r2t = 1;
2770 session->tt = iscsit;
2771 session->dd_data = cls_session->dd_data + sizeof(*session);
2772
2773 mutex_init(&session->eh_mutex);
2774 spin_lock_init(&session->frwd_lock);
2775 spin_lock_init(&session->back_lock);
2776
2777 /* initialize SCSI PDU commands pool */
2778 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
2779 (void***)&session->cmds,
2780 cmd_task_size + sizeof(struct iscsi_task)))
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++) {
2785 struct iscsi_task *task = session->cmds[cmd_i];
2786
2787 if (cmd_task_size)
2788 task->dd_data = &task[1];
2789 task->itt = cmd_i;
2790 task->state = ISCSI_TASK_FREE;
2791 INIT_LIST_HEAD(&task->running);
2792 }
2793
2794 if (!try_module_get(iscsit->owner))
2795 goto module_get_fail;
2796
2797 if (iscsi_add_session(cls_session, id))
2798 goto cls_session_fail;
2799
2800 return cls_session;
2801
2802cls_session_fail:
2803 module_put(iscsit->owner);
2804module_get_fail:
2805 iscsi_pool_free(&session->cmdpool);
2806cmdpool_alloc_fail:
2807 iscsi_free_session(cls_session);
2808dec_session_count:
2809 iscsi_host_dec_session_cnt(shost);
2810 return NULL;
2811}
2812EXPORT_SYMBOL_GPL(iscsi_session_setup);
2813
2814/**
2815 * iscsi_session_teardown - destroy session, host, and cls_session
2816 * @cls_session: iscsi session
2817 *
2818 * The driver must have called iscsi_remove_session before
2819 * calling this.
2820 */
2821void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
2822{
2823 struct iscsi_session *session = cls_session->dd_data;
2824 struct module *owner = cls_session->transport->owner;
2825 struct Scsi_Host *shost = session->host;
2826
2827 iscsi_pool_free(&session->cmdpool);
2828
2829 kfree(session->password);
2830 kfree(session->password_in);
2831 kfree(session->username);
2832 kfree(session->username_in);
2833 kfree(session->targetname);
2834 kfree(session->targetalias);
2835 kfree(session->initiatorname);
2836 kfree(session->boot_root);
2837 kfree(session->boot_nic);
2838 kfree(session->boot_target);
2839 kfree(session->ifacename);
2840 kfree(session->portal_type);
2841 kfree(session->discovery_parent_type);
2842
2843 iscsi_destroy_session(cls_session);
2844 iscsi_host_dec_session_cnt(shost);
2845 module_put(owner);
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
2852 * @dd_size: private driver data size
2853 * @conn_idx: cid
2854 */
2855struct iscsi_cls_conn *
2856iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
2857 uint32_t conn_idx)
2858{
2859 struct iscsi_session *session = cls_session->dd_data;
2860 struct iscsi_conn *conn;
2861 struct iscsi_cls_conn *cls_conn;
2862 char *data;
2863
2864 cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size,
2865 conn_idx);
2866 if (!cls_conn)
2867 return NULL;
2868 conn = cls_conn->dd_data;
2869 memset(conn, 0, sizeof(*conn) + dd_size);
2870
2871 conn->dd_data = cls_conn->dd_data + sizeof(*conn);
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;
2877 conn->tmf_state = TMF_INITIAL;
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
2883 INIT_LIST_HEAD(&conn->mgmtqueue);
2884 INIT_LIST_HEAD(&conn->cmdqueue);
2885 INIT_LIST_HEAD(&conn->requeue);
2886 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
2887
2888 /* allocate login_task used for the login/text sequences */
2889 spin_lock_bh(&session->frwd_lock);
2890 if (!kfifo_out(&session->cmdpool.queue,
2891 (void*)&conn->login_task,
2892 sizeof(void*))) {
2893 spin_unlock_bh(&session->frwd_lock);
2894 goto login_task_alloc_fail;
2895 }
2896 spin_unlock_bh(&session->frwd_lock);
2897
2898 data = (char *) __get_free_pages(GFP_KERNEL,
2899 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2900 if (!data)
2901 goto login_task_data_alloc_fail;
2902 conn->login_task->data = conn->data = data;
2903
2904 init_timer(&conn->tmf_timer);
2905 init_waitqueue_head(&conn->ehwait);
2906
2907 return cls_conn;
2908
2909login_task_data_alloc_fail:
2910 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2911 sizeof(void*));
2912login_task_alloc_fail:
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
2931 del_timer_sync(&conn->transport_timer);
2932
2933 spin_lock_bh(&session->frwd_lock);
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 }
2942 spin_unlock_bh(&session->frwd_lock);
2943
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);
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);
2960 /*
2961 * force eh_abort() to unblock
2962 */
2963 wake_up(&conn->ehwait);
2964 }
2965
2966 /* flush queued up work because we free the connection below */
2967 iscsi_suspend_tx(conn);
2968
2969 spin_lock_bh(&session->frwd_lock);
2970 free_pages((unsigned long) conn->data,
2971 get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
2972 kfree(conn->persistent_address);
2973 kfree(conn->local_ipaddr);
2974 /* regular RX path uses back_lock */
2975 spin_lock_bh(&session->back_lock);
2976 kfifo_in(&session->cmdpool.queue, (void*)&conn->login_task,
2977 sizeof(void*));
2978 spin_unlock_bh(&session->back_lock);
2979 if (session->leadconn == conn)
2980 session->leadconn = NULL;
2981 spin_unlock_bh(&session->frwd_lock);
2982
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
2992 if (!session) {
2993 iscsi_conn_printk(KERN_ERR, conn,
2994 "can't start unbound connection\n");
2995 return -EPERM;
2996 }
2997
2998 if ((session->imm_data_en || !session->initial_r2t_en) &&
2999 session->first_burst > session->max_burst) {
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);
3003 return -EINVAL;
3004 }
3005
3006 if (conn->ping_timeout && !conn->recv_timeout) {
3007 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
3008 "zero. Using 5 seconds\n.");
3009 conn->recv_timeout = 5;
3010 }
3011
3012 if (conn->recv_timeout && !conn->ping_timeout) {
3013 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
3014 "zero. Using 5 seconds.\n");
3015 conn->ping_timeout = 5;
3016 }
3017
3018 spin_lock_bh(&session->frwd_lock);
3019 conn->c_stage = ISCSI_CONN_STARTED;
3020 session->state = ISCSI_STATE_LOGGED_IN;
3021 session->queued_cmdsn = session->cmdsn;
3022
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
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 */
3035 conn->stop_stage = 0;
3036 conn->tmf_state = TMF_INITIAL;
3037 session->age++;
3038 if (session->age == 16)
3039 session->age = 0;
3040 break;
3041 case STOP_CONN_TERM:
3042 conn->stop_stage = 0;
3043 break;
3044 default:
3045 break;
3046 }
3047 spin_unlock_bh(&session->frwd_lock);
3048
3049 iscsi_unblock_session(session->cls_session);
3050 wake_up(&conn->ehwait);
3051 return 0;
3052}
3053EXPORT_SYMBOL_GPL(iscsi_conn_start);
3054
3055static void
3056fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn)
3057{
3058 struct iscsi_task *task;
3059 int i, state;
3060
3061 for (i = 0; i < conn->session->cmds_max; i++) {
3062 task = conn->session->cmds[i];
3063 if (task->sc)
3064 continue;
3065
3066 if (task->state == ISCSI_TASK_FREE)
3067 continue;
3068
3069 ISCSI_DBG_SESSION(conn->session,
3070 "failing mgmt itt 0x%x state %d\n",
3071 task->itt, task->state);
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
3077 }
3078}
3079
3080static void iscsi_start_session_recovery(struct iscsi_session *session,
3081 struct iscsi_conn *conn, int flag)
3082{
3083 int old_stop_stage;
3084
3085 mutex_lock(&session->eh_mutex);
3086 spin_lock_bh(&session->frwd_lock);
3087 if (conn->stop_stage == STOP_CONN_TERM) {
3088 spin_unlock_bh(&session->frwd_lock);
3089 mutex_unlock(&session->eh_mutex);
3090 return;
3091 }
3092
3093 /*
3094 * When this is called for the in_login state, we only want to clean
3095 * up the login task and connection. We do not need to block and set
3096 * the recovery state again
3097 */
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;
3102
3103 old_stop_stage = conn->stop_stage;
3104 conn->stop_stage = flag;
3105 spin_unlock_bh(&session->frwd_lock);
3106
3107 del_timer_sync(&conn->transport_timer);
3108 iscsi_suspend_tx(conn);
3109
3110 spin_lock_bh(&session->frwd_lock);
3111 conn->c_stage = ISCSI_CONN_STOPPED;
3112 spin_unlock_bh(&session->frwd_lock);
3113
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;
3123 if (session->state == ISCSI_STATE_IN_RECOVERY &&
3124 old_stop_stage != STOP_CONN_RECOVER) {
3125 ISCSI_DBG_SESSION(session, "blocking session\n");
3126 iscsi_block_session(session->cls_session);
3127 }
3128 }
3129
3130 /*
3131 * flush queues.
3132 */
3133 spin_lock_bh(&session->frwd_lock);
3134 fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED);
3135 fail_mgmt_tasks(session, conn);
3136 memset(&conn->tmhdr, 0, sizeof(conn->tmhdr));
3137 spin_unlock_bh(&session->frwd_lock);
3138 mutex_unlock(&session->eh_mutex);
3139}
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);
3150 break;
3151 default:
3152 iscsi_conn_printk(KERN_ERR, conn,
3153 "invalid stop flag %d\n", flag);
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{
3161 struct iscsi_session *session = cls_session->dd_data;
3162 struct iscsi_conn *conn = cls_conn->dd_data;
3163
3164 spin_lock_bh(&session->frwd_lock);
3165 if (is_leading)
3166 session->leadconn = conn;
3167 spin_unlock_bh(&session->frwd_lock);
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
3178int iscsi_switch_str_param(char **param, char *new_val_buf)
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}
3195EXPORT_SYMBOL_GPL(iscsi_switch_str_param);
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;
3202 int val;
3203
3204 switch(param) {
3205 case ISCSI_PARAM_FAST_ABORT:
3206 sscanf(buf, "%d", &session->fast_abort);
3207 break;
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;
3214 case ISCSI_PARAM_TGT_RESET_TMO:
3215 sscanf(buf, "%d", &session->tgt_reset_timeout);
3216 break;
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;
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:
3239 sscanf(buf, "%hu", &session->max_r2t);
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;
3259 case ISCSI_PARAM_EXP_STATSN:
3260 sscanf(buf, "%u", &conn->exp_statsn);
3261 break;
3262 case ISCSI_PARAM_USERNAME:
3263 return iscsi_switch_str_param(&session->username, buf);
3264 case ISCSI_PARAM_USERNAME_IN:
3265 return iscsi_switch_str_param(&session->username_in, buf);
3266 case ISCSI_PARAM_PASSWORD:
3267 return iscsi_switch_str_param(&session->password, buf);
3268 case ISCSI_PARAM_PASSWORD_IN:
3269 return iscsi_switch_str_param(&session->password_in, buf);
3270 case ISCSI_PARAM_TARGET_NAME:
3271 return iscsi_switch_str_param(&session->targetname, buf);
3272 case ISCSI_PARAM_TARGET_ALIAS:
3273 return iscsi_switch_str_param(&session->targetalias, buf);
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:
3281 return iscsi_switch_str_param(&conn->persistent_address, buf);
3282 case ISCSI_PARAM_IFACE_NAME:
3283 return iscsi_switch_str_param(&session->ifacename, buf);
3284 case ISCSI_PARAM_INITIATOR_NAME:
3285 return iscsi_switch_str_param(&session->initiatorname, buf);
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);
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);
3297 case ISCSI_PARAM_DISCOVERY_SESS:
3298 sscanf(buf, "%d", &val);
3299 session->discovery_sess = !!val;
3300 break;
3301 case ISCSI_PARAM_LOCAL_IPADDR:
3302 return iscsi_switch_str_param(&conn->local_ipaddr, buf);
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{
3314 struct iscsi_session *session = cls_session->dd_data;
3315 int len;
3316
3317 switch(param) {
3318 case ISCSI_PARAM_FAST_ABORT:
3319 len = sprintf(buf, "%d\n", session->fast_abort);
3320 break;
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;
3327 case ISCSI_PARAM_TGT_RESET_TMO:
3328 len = sprintf(buf, "%d\n", session->tgt_reset_timeout);
3329 break;
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;
3351 case ISCSI_PARAM_DEF_TASKMGMT_TMO:
3352 len = sprintf(buf, "%d\n", session->def_taskmgmt_tmo);
3353 break;
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;
3360 case ISCSI_PARAM_TARGET_ALIAS:
3361 len = sprintf(buf, "%s\n", session->targetalias);
3362 break;
3363 case ISCSI_PARAM_TPGT:
3364 len = sprintf(buf, "%d\n", session->tpgt);
3365 break;
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;
3378 case ISCSI_PARAM_IFACE_NAME:
3379 len = sprintf(buf, "%s\n", session->ifacename);
3380 break;
3381 case ISCSI_PARAM_INITIATOR_NAME:
3382 len = sprintf(buf, "%s\n", session->initiatorname);
3383 break;
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);
3392 break;
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");
3438 break;
3439 default:
3440 return -ENOSYS;
3441 }
3442
3443 return len;
3444}
3445EXPORT_SYMBOL_GPL(iscsi_session_get_param);
3446
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
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) {
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;
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;
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;
3564 case ISCSI_PARAM_IPV6_FLOW_LABEL:
3565 len = sprintf(buf, "%u\n", conn->ipv6_flow_label);
3566 break;
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;
3576 case ISCSI_PARAM_LOCAL_IPADDR:
3577 len = sprintf(buf, "%s\n", conn->local_ipaddr);
3578 break;
3579 default:
3580 return -ENOSYS;
3581 }
3582
3583 return len;
3584}
3585EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
3586
3587int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
3588 char *buf)
3589{
3590 struct iscsi_host *ihost = shost_priv(shost);
3591 int len;
3592
3593 switch (param) {
3594 case ISCSI_HOST_PARAM_NETDEV_NAME:
3595 len = sprintf(buf, "%s\n", ihost->netdev);
3596 break;
3597 case ISCSI_HOST_PARAM_HWADDRESS:
3598 len = sprintf(buf, "%s\n", ihost->hwaddress);
3599 break;
3600 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3601 len = sprintf(buf, "%s\n", ihost->initiatorname);
3602 break;
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{
3614 struct iscsi_host *ihost = shost_priv(shost);
3615
3616 switch (param) {
3617 case ISCSI_HOST_PARAM_NETDEV_NAME:
3618 return iscsi_switch_str_param(&ihost->netdev, buf);
3619 case ISCSI_HOST_PARAM_HWADDRESS:
3620 return iscsi_switch_str_param(&ihost->hwaddress, buf);
3621 case ISCSI_HOST_PARAM_INITIATOR_NAME:
3622 return iscsi_switch_str_param(&ihost->initiatorname, buf);
3623 default:
3624 return -ENOSYS;
3625 }
3626
3627 return 0;
3628}
3629EXPORT_SYMBOL_GPL(iscsi_host_set_param);
3630
3631MODULE_AUTHOR("Mike Christie");
3632MODULE_DESCRIPTION("iSCSI library functions");
3633MODULE_LICENSE("GPL");