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