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