Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / target / iscsi / iscsi_target_util.c
1 /*******************************************************************************
2  * This file contains the iSCSI Target specific utility functions.
3  *
4  * (c) Copyright 2007-2013 Datera, Inc.
5  *
6  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  ******************************************************************************/
18
19 #include <linux/list.h>
20 #include <linux/percpu_ida.h>
21 #include <scsi/scsi_tcq.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_fabric.h>
25 #include <target/target_core_configfs.h>
26 #include <target/iscsi/iscsi_transport.h>
27
28 #include "iscsi_target_core.h"
29 #include "iscsi_target_parameters.h"
30 #include "iscsi_target_seq_pdu_list.h"
31 #include "iscsi_target_datain_values.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl1.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target_tpg.h"
36 #include "iscsi_target_tq.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39
40 #define PRINT_BUFF(buff, len)                                   \
41 {                                                               \
42         int zzz;                                                \
43                                                                 \
44         pr_debug("%d:\n", __LINE__);                            \
45         for (zzz = 0; zzz < len; zzz++) {                       \
46                 if (zzz % 16 == 0) {                            \
47                         if (zzz)                                \
48                                 pr_debug("\n");                 \
49                         pr_debug("%4i: ", zzz);                 \
50                 }                                               \
51                 pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
52         }                                                       \
53         if ((len + 1) % 16)                                     \
54                 pr_debug("\n");                                 \
55 }
56
57 extern struct list_head g_tiqn_list;
58 extern spinlock_t tiqn_lock;
59
60 /*
61  *      Called with cmd->r2t_lock held.
62  */
63 int iscsit_add_r2t_to_list(
64         struct iscsi_cmd *cmd,
65         u32 offset,
66         u32 xfer_len,
67         int recovery,
68         u32 r2t_sn)
69 {
70         struct iscsi_r2t *r2t;
71
72         r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
73         if (!r2t) {
74                 pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
75                 return -1;
76         }
77         INIT_LIST_HEAD(&r2t->r2t_list);
78
79         r2t->recovery_r2t = recovery;
80         r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
81         r2t->offset = offset;
82         r2t->xfer_len = xfer_len;
83         list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
84         spin_unlock_bh(&cmd->r2t_lock);
85
86         iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
87
88         spin_lock_bh(&cmd->r2t_lock);
89         return 0;
90 }
91
92 struct iscsi_r2t *iscsit_get_r2t_for_eos(
93         struct iscsi_cmd *cmd,
94         u32 offset,
95         u32 length)
96 {
97         struct iscsi_r2t *r2t;
98
99         spin_lock_bh(&cmd->r2t_lock);
100         list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
101                 if ((r2t->offset <= offset) &&
102                     (r2t->offset + r2t->xfer_len) >= (offset + length)) {
103                         spin_unlock_bh(&cmd->r2t_lock);
104                         return r2t;
105                 }
106         }
107         spin_unlock_bh(&cmd->r2t_lock);
108
109         pr_err("Unable to locate R2T for Offset: %u, Length:"
110                         " %u\n", offset, length);
111         return NULL;
112 }
113
114 struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
115 {
116         struct iscsi_r2t *r2t;
117
118         spin_lock_bh(&cmd->r2t_lock);
119         list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
120                 if (!r2t->sent_r2t) {
121                         spin_unlock_bh(&cmd->r2t_lock);
122                         return r2t;
123                 }
124         }
125         spin_unlock_bh(&cmd->r2t_lock);
126
127         pr_err("Unable to locate next R2T to send for ITT:"
128                         " 0x%08x.\n", cmd->init_task_tag);
129         return NULL;
130 }
131
132 /*
133  *      Called with cmd->r2t_lock held.
134  */
135 void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
136 {
137         list_del(&r2t->r2t_list);
138         kmem_cache_free(lio_r2t_cache, r2t);
139 }
140
141 void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
142 {
143         struct iscsi_r2t *r2t, *r2t_tmp;
144
145         spin_lock_bh(&cmd->r2t_lock);
146         list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
147                 iscsit_free_r2t(r2t, cmd);
148         spin_unlock_bh(&cmd->r2t_lock);
149 }
150
151 /*
152  * May be called from software interrupt (timer) context for allocating
153  * iSCSI NopINs.
154  */
155 struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
156 {
157         struct iscsi_cmd *cmd;
158         struct se_session *se_sess = conn->sess->se_sess;
159         int size, tag;
160
161         tag = percpu_ida_alloc(&se_sess->sess_tag_pool, gfp_mask);
162         size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
163         cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
164         memset(cmd, 0, size);
165
166         cmd->se_cmd.map_tag = tag;
167         cmd->conn = conn;
168         INIT_LIST_HEAD(&cmd->i_conn_node);
169         INIT_LIST_HEAD(&cmd->datain_list);
170         INIT_LIST_HEAD(&cmd->cmd_r2t_list);
171         spin_lock_init(&cmd->datain_lock);
172         spin_lock_init(&cmd->dataout_timeout_lock);
173         spin_lock_init(&cmd->istate_lock);
174         spin_lock_init(&cmd->error_lock);
175         spin_lock_init(&cmd->r2t_lock);
176
177         return cmd;
178 }
179 EXPORT_SYMBOL(iscsit_allocate_cmd);
180
181 struct iscsi_seq *iscsit_get_seq_holder_for_datain(
182         struct iscsi_cmd *cmd,
183         u32 seq_send_order)
184 {
185         u32 i;
186
187         for (i = 0; i < cmd->seq_count; i++)
188                 if (cmd->seq_list[i].seq_send_order == seq_send_order)
189                         return &cmd->seq_list[i];
190
191         return NULL;
192 }
193
194 struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
195 {
196         u32 i;
197
198         if (!cmd->seq_list) {
199                 pr_err("struct iscsi_cmd->seq_list is NULL!\n");
200                 return NULL;
201         }
202
203         for (i = 0; i < cmd->seq_count; i++) {
204                 if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
205                         continue;
206                 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
207                         cmd->seq_send_order++;
208                         return &cmd->seq_list[i];
209                 }
210         }
211
212         return NULL;
213 }
214
215 struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
216         struct iscsi_cmd *cmd,
217         u32 r2t_sn)
218 {
219         struct iscsi_r2t *r2t;
220
221         spin_lock_bh(&cmd->r2t_lock);
222         list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
223                 if (r2t->r2t_sn == r2t_sn) {
224                         spin_unlock_bh(&cmd->r2t_lock);
225                         return r2t;
226                 }
227         }
228         spin_unlock_bh(&cmd->r2t_lock);
229
230         return NULL;
231 }
232
233 static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
234 {
235         int ret;
236
237         /*
238          * This is the proper method of checking received CmdSN against
239          * ExpCmdSN and MaxCmdSN values, as well as accounting for out
240          * or order CmdSNs due to multiple connection sessions and/or
241          * CRC failures.
242          */
243         if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
244                 pr_err("Received CmdSN: 0x%08x is greater than"
245                        " MaxCmdSN: 0x%08x, ignoring.\n", cmdsn,
246                        sess->max_cmd_sn);
247                 ret = CMDSN_MAXCMDSN_OVERRUN;
248
249         } else if (cmdsn == sess->exp_cmd_sn) {
250                 sess->exp_cmd_sn++;
251                 pr_debug("Received CmdSN matches ExpCmdSN,"
252                       " incremented ExpCmdSN to: 0x%08x\n",
253                       sess->exp_cmd_sn);
254                 ret = CMDSN_NORMAL_OPERATION;
255
256         } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
257                 pr_debug("Received CmdSN: 0x%08x is greater"
258                       " than ExpCmdSN: 0x%08x, not acknowledging.\n",
259                       cmdsn, sess->exp_cmd_sn);
260                 ret = CMDSN_HIGHER_THAN_EXP;
261
262         } else {
263                 pr_err("Received CmdSN: 0x%08x is less than"
264                        " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
265                        sess->exp_cmd_sn);
266                 ret = CMDSN_LOWER_THAN_EXP;
267         }
268
269         return ret;
270 }
271
272 /*
273  * Commands may be received out of order if MC/S is in use.
274  * Ensure they are executed in CmdSN order.
275  */
276 int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
277                         unsigned char *buf, __be32 cmdsn)
278 {
279         int ret, cmdsn_ret;
280         bool reject = false;
281         u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
282
283         mutex_lock(&conn->sess->cmdsn_mutex);
284
285         cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
286         switch (cmdsn_ret) {
287         case CMDSN_NORMAL_OPERATION:
288                 ret = iscsit_execute_cmd(cmd, 0);
289                 if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
290                         iscsit_execute_ooo_cmdsns(conn->sess);
291                 else if (ret < 0) {
292                         reject = true;
293                         ret = CMDSN_ERROR_CANNOT_RECOVER;
294                 }
295                 break;
296         case CMDSN_HIGHER_THAN_EXP:
297                 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
298                 if (ret < 0) {
299                         reject = true;
300                         ret = CMDSN_ERROR_CANNOT_RECOVER;
301                         break;
302                 }
303                 ret = CMDSN_HIGHER_THAN_EXP;
304                 break;
305         case CMDSN_LOWER_THAN_EXP:
306         case CMDSN_MAXCMDSN_OVERRUN:
307         default:
308                 cmd->i_state = ISTATE_REMOVE;
309                 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
310                 /*
311                  * Existing callers for iscsit_sequence_cmd() will silently
312                  * ignore commands with CMDSN_LOWER_THAN_EXP, so force this
313                  * return for CMDSN_MAXCMDSN_OVERRUN as well..
314                  */
315                 ret = CMDSN_LOWER_THAN_EXP;
316                 break;
317         }
318         mutex_unlock(&conn->sess->cmdsn_mutex);
319
320         if (reject)
321                 iscsit_reject_cmd(cmd, reason, buf);
322
323         return ret;
324 }
325 EXPORT_SYMBOL(iscsit_sequence_cmd);
326
327 int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
328 {
329         struct iscsi_conn *conn = cmd->conn;
330         struct se_cmd *se_cmd = &cmd->se_cmd;
331         struct iscsi_data *hdr = (struct iscsi_data *) buf;
332         u32 payload_length = ntoh24(hdr->dlength);
333
334         if (conn->sess->sess_ops->InitialR2T) {
335                 pr_err("Received unexpected unsolicited data"
336                         " while InitialR2T=Yes, protocol error.\n");
337                 transport_send_check_condition_and_sense(se_cmd,
338                                 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
339                 return -1;
340         }
341
342         if ((cmd->first_burst_len + payload_length) >
343              conn->sess->sess_ops->FirstBurstLength) {
344                 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
345                         " for this Unsolicited DataOut Burst.\n",
346                         (cmd->first_burst_len + payload_length),
347                                 conn->sess->sess_ops->FirstBurstLength);
348                 transport_send_check_condition_and_sense(se_cmd,
349                                 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
350                 return -1;
351         }
352
353         if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
354                 return 0;
355
356         if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
357             ((cmd->first_burst_len + payload_length) !=
358               conn->sess->sess_ops->FirstBurstLength)) {
359                 pr_err("Unsolicited non-immediate data received %u"
360                         " does not equal FirstBurstLength: %u, and does"
361                         " not equal ExpXferLen %u.\n",
362                         (cmd->first_burst_len + payload_length),
363                         conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
364                 transport_send_check_condition_and_sense(se_cmd,
365                                 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
366                 return -1;
367         }
368         return 0;
369 }
370
371 struct iscsi_cmd *iscsit_find_cmd_from_itt(
372         struct iscsi_conn *conn,
373         itt_t init_task_tag)
374 {
375         struct iscsi_cmd *cmd;
376
377         spin_lock_bh(&conn->cmd_lock);
378         list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
379                 if (cmd->init_task_tag == init_task_tag) {
380                         spin_unlock_bh(&conn->cmd_lock);
381                         return cmd;
382                 }
383         }
384         spin_unlock_bh(&conn->cmd_lock);
385
386         pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
387                         init_task_tag, conn->cid);
388         return NULL;
389 }
390
391 struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
392         struct iscsi_conn *conn,
393         itt_t init_task_tag,
394         u32 length)
395 {
396         struct iscsi_cmd *cmd;
397
398         spin_lock_bh(&conn->cmd_lock);
399         list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
400                 if (cmd->init_task_tag == init_task_tag) {
401                         spin_unlock_bh(&conn->cmd_lock);
402                         return cmd;
403                 }
404         }
405         spin_unlock_bh(&conn->cmd_lock);
406
407         pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
408                         " dumping payload\n", init_task_tag, conn->cid);
409         if (length)
410                 iscsit_dump_data_payload(conn, length, 1);
411
412         return NULL;
413 }
414
415 struct iscsi_cmd *iscsit_find_cmd_from_ttt(
416         struct iscsi_conn *conn,
417         u32 targ_xfer_tag)
418 {
419         struct iscsi_cmd *cmd = NULL;
420
421         spin_lock_bh(&conn->cmd_lock);
422         list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
423                 if (cmd->targ_xfer_tag == targ_xfer_tag) {
424                         spin_unlock_bh(&conn->cmd_lock);
425                         return cmd;
426                 }
427         }
428         spin_unlock_bh(&conn->cmd_lock);
429
430         pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
431                         targ_xfer_tag, conn->cid);
432         return NULL;
433 }
434
435 int iscsit_find_cmd_for_recovery(
436         struct iscsi_session *sess,
437         struct iscsi_cmd **cmd_ptr,
438         struct iscsi_conn_recovery **cr_ptr,
439         itt_t init_task_tag)
440 {
441         struct iscsi_cmd *cmd = NULL;
442         struct iscsi_conn_recovery *cr;
443         /*
444          * Scan through the inactive connection recovery list's command list.
445          * If init_task_tag matches the command is still alligent.
446          */
447         spin_lock(&sess->cr_i_lock);
448         list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
449                 spin_lock(&cr->conn_recovery_cmd_lock);
450                 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
451                         if (cmd->init_task_tag == init_task_tag) {
452                                 spin_unlock(&cr->conn_recovery_cmd_lock);
453                                 spin_unlock(&sess->cr_i_lock);
454
455                                 *cr_ptr = cr;
456                                 *cmd_ptr = cmd;
457                                 return -2;
458                         }
459                 }
460                 spin_unlock(&cr->conn_recovery_cmd_lock);
461         }
462         spin_unlock(&sess->cr_i_lock);
463         /*
464          * Scan through the active connection recovery list's command list.
465          * If init_task_tag matches the command is ready to be reassigned.
466          */
467         spin_lock(&sess->cr_a_lock);
468         list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
469                 spin_lock(&cr->conn_recovery_cmd_lock);
470                 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
471                         if (cmd->init_task_tag == init_task_tag) {
472                                 spin_unlock(&cr->conn_recovery_cmd_lock);
473                                 spin_unlock(&sess->cr_a_lock);
474
475                                 *cr_ptr = cr;
476                                 *cmd_ptr = cmd;
477                                 return 0;
478                         }
479                 }
480                 spin_unlock(&cr->conn_recovery_cmd_lock);
481         }
482         spin_unlock(&sess->cr_a_lock);
483
484         return -1;
485 }
486
487 void iscsit_add_cmd_to_immediate_queue(
488         struct iscsi_cmd *cmd,
489         struct iscsi_conn *conn,
490         u8 state)
491 {
492         struct iscsi_queue_req *qr;
493
494         qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
495         if (!qr) {
496                 pr_err("Unable to allocate memory for"
497                                 " struct iscsi_queue_req\n");
498                 return;
499         }
500         INIT_LIST_HEAD(&qr->qr_list);
501         qr->cmd = cmd;
502         qr->state = state;
503
504         spin_lock_bh(&conn->immed_queue_lock);
505         list_add_tail(&qr->qr_list, &conn->immed_queue_list);
506         atomic_inc(&cmd->immed_queue_count);
507         atomic_set(&conn->check_immediate_queue, 1);
508         spin_unlock_bh(&conn->immed_queue_lock);
509
510         wake_up(&conn->queues_wq);
511 }
512
513 struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
514 {
515         struct iscsi_queue_req *qr;
516
517         spin_lock_bh(&conn->immed_queue_lock);
518         if (list_empty(&conn->immed_queue_list)) {
519                 spin_unlock_bh(&conn->immed_queue_lock);
520                 return NULL;
521         }
522         qr = list_first_entry(&conn->immed_queue_list,
523                               struct iscsi_queue_req, qr_list);
524
525         list_del(&qr->qr_list);
526         if (qr->cmd)
527                 atomic_dec(&qr->cmd->immed_queue_count);
528         spin_unlock_bh(&conn->immed_queue_lock);
529
530         return qr;
531 }
532
533 static void iscsit_remove_cmd_from_immediate_queue(
534         struct iscsi_cmd *cmd,
535         struct iscsi_conn *conn)
536 {
537         struct iscsi_queue_req *qr, *qr_tmp;
538
539         spin_lock_bh(&conn->immed_queue_lock);
540         if (!atomic_read(&cmd->immed_queue_count)) {
541                 spin_unlock_bh(&conn->immed_queue_lock);
542                 return;
543         }
544
545         list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
546                 if (qr->cmd != cmd)
547                         continue;
548
549                 atomic_dec(&qr->cmd->immed_queue_count);
550                 list_del(&qr->qr_list);
551                 kmem_cache_free(lio_qr_cache, qr);
552         }
553         spin_unlock_bh(&conn->immed_queue_lock);
554
555         if (atomic_read(&cmd->immed_queue_count)) {
556                 pr_err("ITT: 0x%08x immed_queue_count: %d\n",
557                         cmd->init_task_tag,
558                         atomic_read(&cmd->immed_queue_count));
559         }
560 }
561
562 void iscsit_add_cmd_to_response_queue(
563         struct iscsi_cmd *cmd,
564         struct iscsi_conn *conn,
565         u8 state)
566 {
567         struct iscsi_queue_req *qr;
568
569         qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
570         if (!qr) {
571                 pr_err("Unable to allocate memory for"
572                         " struct iscsi_queue_req\n");
573                 return;
574         }
575         INIT_LIST_HEAD(&qr->qr_list);
576         qr->cmd = cmd;
577         qr->state = state;
578
579         spin_lock_bh(&conn->response_queue_lock);
580         list_add_tail(&qr->qr_list, &conn->response_queue_list);
581         atomic_inc(&cmd->response_queue_count);
582         spin_unlock_bh(&conn->response_queue_lock);
583
584         wake_up(&conn->queues_wq);
585 }
586
587 struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
588 {
589         struct iscsi_queue_req *qr;
590
591         spin_lock_bh(&conn->response_queue_lock);
592         if (list_empty(&conn->response_queue_list)) {
593                 spin_unlock_bh(&conn->response_queue_lock);
594                 return NULL;
595         }
596
597         qr = list_first_entry(&conn->response_queue_list,
598                               struct iscsi_queue_req, qr_list);
599
600         list_del(&qr->qr_list);
601         if (qr->cmd)
602                 atomic_dec(&qr->cmd->response_queue_count);
603         spin_unlock_bh(&conn->response_queue_lock);
604
605         return qr;
606 }
607
608 static void iscsit_remove_cmd_from_response_queue(
609         struct iscsi_cmd *cmd,
610         struct iscsi_conn *conn)
611 {
612         struct iscsi_queue_req *qr, *qr_tmp;
613
614         spin_lock_bh(&conn->response_queue_lock);
615         if (!atomic_read(&cmd->response_queue_count)) {
616                 spin_unlock_bh(&conn->response_queue_lock);
617                 return;
618         }
619
620         list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
621                                 qr_list) {
622                 if (qr->cmd != cmd)
623                         continue;
624
625                 atomic_dec(&qr->cmd->response_queue_count);
626                 list_del(&qr->qr_list);
627                 kmem_cache_free(lio_qr_cache, qr);
628         }
629         spin_unlock_bh(&conn->response_queue_lock);
630
631         if (atomic_read(&cmd->response_queue_count)) {
632                 pr_err("ITT: 0x%08x response_queue_count: %d\n",
633                         cmd->init_task_tag,
634                         atomic_read(&cmd->response_queue_count));
635         }
636 }
637
638 bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
639 {
640         bool empty;
641
642         spin_lock_bh(&conn->immed_queue_lock);
643         empty = list_empty(&conn->immed_queue_list);
644         spin_unlock_bh(&conn->immed_queue_lock);
645
646         if (!empty)
647                 return empty;
648
649         spin_lock_bh(&conn->response_queue_lock);
650         empty = list_empty(&conn->response_queue_list);
651         spin_unlock_bh(&conn->response_queue_lock);
652
653         return empty;
654 }
655
656 void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
657 {
658         struct iscsi_queue_req *qr, *qr_tmp;
659
660         spin_lock_bh(&conn->immed_queue_lock);
661         list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
662                 list_del(&qr->qr_list);
663                 if (qr->cmd)
664                         atomic_dec(&qr->cmd->immed_queue_count);
665
666                 kmem_cache_free(lio_qr_cache, qr);
667         }
668         spin_unlock_bh(&conn->immed_queue_lock);
669
670         spin_lock_bh(&conn->response_queue_lock);
671         list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
672                         qr_list) {
673                 list_del(&qr->qr_list);
674                 if (qr->cmd)
675                         atomic_dec(&qr->cmd->response_queue_count);
676
677                 kmem_cache_free(lio_qr_cache, qr);
678         }
679         spin_unlock_bh(&conn->response_queue_lock);
680 }
681
682 void iscsit_release_cmd(struct iscsi_cmd *cmd)
683 {
684         struct iscsi_session *sess;
685         struct se_cmd *se_cmd = &cmd->se_cmd;
686
687         if (cmd->conn)
688                 sess = cmd->conn->sess;
689         else
690                 sess = cmd->sess;
691
692         BUG_ON(!sess || !sess->se_sess);
693
694         kfree(cmd->buf_ptr);
695         kfree(cmd->pdu_list);
696         kfree(cmd->seq_list);
697         kfree(cmd->tmr_req);
698         kfree(cmd->iov_data);
699         kfree(cmd->text_in_ptr);
700
701         percpu_ida_free(&sess->se_sess->sess_tag_pool, se_cmd->map_tag);
702 }
703 EXPORT_SYMBOL(iscsit_release_cmd);
704
705 static void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool scsi_cmd,
706                               bool check_queues)
707 {
708         struct iscsi_conn *conn = cmd->conn;
709
710         if (scsi_cmd) {
711                 if (cmd->data_direction == DMA_TO_DEVICE) {
712                         iscsit_stop_dataout_timer(cmd);
713                         iscsit_free_r2ts_from_list(cmd);
714                 }
715                 if (cmd->data_direction == DMA_FROM_DEVICE)
716                         iscsit_free_all_datain_reqs(cmd);
717         }
718
719         if (conn && check_queues) {
720                 iscsit_remove_cmd_from_immediate_queue(cmd, conn);
721                 iscsit_remove_cmd_from_response_queue(cmd, conn);
722         }
723 }
724
725 void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
726 {
727         struct se_cmd *se_cmd = NULL;
728         int rc;
729         /*
730          * Determine if a struct se_cmd is associated with
731          * this struct iscsi_cmd.
732          */
733         switch (cmd->iscsi_opcode) {
734         case ISCSI_OP_SCSI_CMD:
735                 se_cmd = &cmd->se_cmd;
736                 __iscsit_free_cmd(cmd, true, shutdown);
737                 /*
738                  * Fallthrough
739                  */
740         case ISCSI_OP_SCSI_TMFUNC:
741                 rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
742                 if (!rc && shutdown && se_cmd && se_cmd->se_sess) {
743                         __iscsit_free_cmd(cmd, true, shutdown);
744                         target_put_sess_cmd(se_cmd->se_sess, se_cmd);
745                 }
746                 break;
747         case ISCSI_OP_REJECT:
748                 /*
749                  * Handle special case for REJECT when iscsi_add_reject*() has
750                  * overwritten the original iscsi_opcode assignment, and the
751                  * associated cmd->se_cmd needs to be released.
752                  */
753                 if (cmd->se_cmd.se_tfo != NULL) {
754                         se_cmd = &cmd->se_cmd;
755                         __iscsit_free_cmd(cmd, true, shutdown);
756
757                         rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
758                         if (!rc && shutdown && se_cmd->se_sess) {
759                                 __iscsit_free_cmd(cmd, true, shutdown);
760                                 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
761                         }
762                         break;
763                 }
764                 /* Fall-through */
765         default:
766                 __iscsit_free_cmd(cmd, false, shutdown);
767                 iscsit_release_cmd(cmd);
768                 break;
769         }
770 }
771
772 int iscsit_check_session_usage_count(struct iscsi_session *sess)
773 {
774         spin_lock_bh(&sess->session_usage_lock);
775         if (sess->session_usage_count != 0) {
776                 sess->session_waiting_on_uc = 1;
777                 spin_unlock_bh(&sess->session_usage_lock);
778                 if (in_interrupt())
779                         return 2;
780
781                 wait_for_completion(&sess->session_waiting_on_uc_comp);
782                 return 1;
783         }
784         spin_unlock_bh(&sess->session_usage_lock);
785
786         return 0;
787 }
788
789 void iscsit_dec_session_usage_count(struct iscsi_session *sess)
790 {
791         spin_lock_bh(&sess->session_usage_lock);
792         sess->session_usage_count--;
793
794         if (!sess->session_usage_count && sess->session_waiting_on_uc)
795                 complete(&sess->session_waiting_on_uc_comp);
796
797         spin_unlock_bh(&sess->session_usage_lock);
798 }
799
800 void iscsit_inc_session_usage_count(struct iscsi_session *sess)
801 {
802         spin_lock_bh(&sess->session_usage_lock);
803         sess->session_usage_count++;
804         spin_unlock_bh(&sess->session_usage_lock);
805 }
806
807 /*
808  *      Setup conn->if_marker and conn->of_marker values based upon
809  *      the initial marker-less interval. (see iSCSI v19 A.2)
810  */
811 int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
812 {
813         int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
814         /*
815          * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
816          */
817         u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
818         u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
819
820         if (conn->conn_ops->OFMarker) {
821                 /*
822                  * Account for the first Login Command received not
823                  * via iscsi_recv_msg().
824                  */
825                 conn->of_marker += ISCSI_HDR_LEN;
826                 if (conn->of_marker <= OFMarkInt) {
827                         conn->of_marker = (OFMarkInt - conn->of_marker);
828                 } else {
829                         login_ofmarker_count = (conn->of_marker / OFMarkInt);
830                         next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
831                                         (login_ofmarker_count * MARKER_SIZE);
832                         conn->of_marker = (next_marker - conn->of_marker);
833                 }
834                 conn->of_marker_offset = 0;
835                 pr_debug("Setting OFMarker value to %u based on Initial"
836                         " Markerless Interval.\n", conn->of_marker);
837         }
838
839         if (conn->conn_ops->IFMarker) {
840                 if (conn->if_marker <= IFMarkInt) {
841                         conn->if_marker = (IFMarkInt - conn->if_marker);
842                 } else {
843                         login_ifmarker_count = (conn->if_marker / IFMarkInt);
844                         next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
845                                         (login_ifmarker_count * MARKER_SIZE);
846                         conn->if_marker = (next_marker - conn->if_marker);
847                 }
848                 pr_debug("Setting IFMarker value to %u based on Initial"
849                         " Markerless Interval.\n", conn->if_marker);
850         }
851
852         return 0;
853 }
854
855 struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
856 {
857         struct iscsi_conn *conn;
858
859         spin_lock_bh(&sess->conn_lock);
860         list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
861                 if ((conn->cid == cid) &&
862                     (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
863                         iscsit_inc_conn_usage_count(conn);
864                         spin_unlock_bh(&sess->conn_lock);
865                         return conn;
866                 }
867         }
868         spin_unlock_bh(&sess->conn_lock);
869
870         return NULL;
871 }
872
873 struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
874 {
875         struct iscsi_conn *conn;
876
877         spin_lock_bh(&sess->conn_lock);
878         list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
879                 if (conn->cid == cid) {
880                         iscsit_inc_conn_usage_count(conn);
881                         spin_lock(&conn->state_lock);
882                         atomic_set(&conn->connection_wait_rcfr, 1);
883                         spin_unlock(&conn->state_lock);
884                         spin_unlock_bh(&sess->conn_lock);
885                         return conn;
886                 }
887         }
888         spin_unlock_bh(&sess->conn_lock);
889
890         return NULL;
891 }
892
893 void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
894 {
895         spin_lock_bh(&conn->conn_usage_lock);
896         if (conn->conn_usage_count != 0) {
897                 conn->conn_waiting_on_uc = 1;
898                 spin_unlock_bh(&conn->conn_usage_lock);
899
900                 wait_for_completion(&conn->conn_waiting_on_uc_comp);
901                 return;
902         }
903         spin_unlock_bh(&conn->conn_usage_lock);
904 }
905
906 void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
907 {
908         spin_lock_bh(&conn->conn_usage_lock);
909         conn->conn_usage_count--;
910
911         if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
912                 complete(&conn->conn_waiting_on_uc_comp);
913
914         spin_unlock_bh(&conn->conn_usage_lock);
915 }
916
917 void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
918 {
919         spin_lock_bh(&conn->conn_usage_lock);
920         conn->conn_usage_count++;
921         spin_unlock_bh(&conn->conn_usage_lock);
922 }
923
924 static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
925 {
926         u8 state;
927         struct iscsi_cmd *cmd;
928
929         cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
930         if (!cmd)
931                 return -1;
932
933         cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
934         state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
935                                 ISTATE_SEND_NOPIN_NO_RESPONSE;
936         cmd->init_task_tag = RESERVED_ITT;
937         spin_lock_bh(&conn->sess->ttt_lock);
938         cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
939                         0xFFFFFFFF;
940         if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
941                 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
942         spin_unlock_bh(&conn->sess->ttt_lock);
943
944         spin_lock_bh(&conn->cmd_lock);
945         list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
946         spin_unlock_bh(&conn->cmd_lock);
947
948         if (want_response)
949                 iscsit_start_nopin_response_timer(conn);
950         iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
951
952         return 0;
953 }
954
955 static void iscsit_handle_nopin_response_timeout(unsigned long data)
956 {
957         struct iscsi_conn *conn = (struct iscsi_conn *) data;
958
959         iscsit_inc_conn_usage_count(conn);
960
961         spin_lock_bh(&conn->nopin_timer_lock);
962         if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
963                 spin_unlock_bh(&conn->nopin_timer_lock);
964                 iscsit_dec_conn_usage_count(conn);
965                 return;
966         }
967
968         pr_debug("Did not receive response to NOPIN on CID: %hu on"
969                 " SID: %u, failing connection.\n", conn->cid,
970                         conn->sess->sid);
971         conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
972         spin_unlock_bh(&conn->nopin_timer_lock);
973
974         {
975         struct iscsi_portal_group *tpg = conn->sess->tpg;
976         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
977
978         if (tiqn) {
979                 spin_lock_bh(&tiqn->sess_err_stats.lock);
980                 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
981                                 conn->sess->sess_ops->InitiatorName);
982                 tiqn->sess_err_stats.last_sess_failure_type =
983                                 ISCSI_SESS_ERR_CXN_TIMEOUT;
984                 tiqn->sess_err_stats.cxn_timeout_errors++;
985                 atomic_long_inc(&conn->sess->conn_timeout_errors);
986                 spin_unlock_bh(&tiqn->sess_err_stats.lock);
987         }
988         }
989
990         iscsit_cause_connection_reinstatement(conn, 0);
991         iscsit_dec_conn_usage_count(conn);
992 }
993
994 void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
995 {
996         struct iscsi_session *sess = conn->sess;
997         struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
998
999         spin_lock_bh(&conn->nopin_timer_lock);
1000         if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1001                 spin_unlock_bh(&conn->nopin_timer_lock);
1002                 return;
1003         }
1004
1005         mod_timer(&conn->nopin_response_timer,
1006                 (get_jiffies_64() + na->nopin_response_timeout * HZ));
1007         spin_unlock_bh(&conn->nopin_timer_lock);
1008 }
1009
1010 /*
1011  *      Called with conn->nopin_timer_lock held.
1012  */
1013 void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
1014 {
1015         struct iscsi_session *sess = conn->sess;
1016         struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1017
1018         spin_lock_bh(&conn->nopin_timer_lock);
1019         if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
1020                 spin_unlock_bh(&conn->nopin_timer_lock);
1021                 return;
1022         }
1023
1024         init_timer(&conn->nopin_response_timer);
1025         conn->nopin_response_timer.expires =
1026                 (get_jiffies_64() + na->nopin_response_timeout * HZ);
1027         conn->nopin_response_timer.data = (unsigned long)conn;
1028         conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
1029         conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
1030         conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
1031         add_timer(&conn->nopin_response_timer);
1032
1033         pr_debug("Started NOPIN Response Timer on CID: %d to %u"
1034                 " seconds\n", conn->cid, na->nopin_response_timeout);
1035         spin_unlock_bh(&conn->nopin_timer_lock);
1036 }
1037
1038 void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
1039 {
1040         spin_lock_bh(&conn->nopin_timer_lock);
1041         if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1042                 spin_unlock_bh(&conn->nopin_timer_lock);
1043                 return;
1044         }
1045         conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
1046         spin_unlock_bh(&conn->nopin_timer_lock);
1047
1048         del_timer_sync(&conn->nopin_response_timer);
1049
1050         spin_lock_bh(&conn->nopin_timer_lock);
1051         conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1052         spin_unlock_bh(&conn->nopin_timer_lock);
1053 }
1054
1055 static void iscsit_handle_nopin_timeout(unsigned long data)
1056 {
1057         struct iscsi_conn *conn = (struct iscsi_conn *) data;
1058
1059         iscsit_inc_conn_usage_count(conn);
1060
1061         spin_lock_bh(&conn->nopin_timer_lock);
1062         if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1063                 spin_unlock_bh(&conn->nopin_timer_lock);
1064                 iscsit_dec_conn_usage_count(conn);
1065                 return;
1066         }
1067         conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1068         spin_unlock_bh(&conn->nopin_timer_lock);
1069
1070         iscsit_add_nopin(conn, 1);
1071         iscsit_dec_conn_usage_count(conn);
1072 }
1073
1074 /*
1075  * Called with conn->nopin_timer_lock held.
1076  */
1077 void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1078 {
1079         struct iscsi_session *sess = conn->sess;
1080         struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1081         /*
1082         * NOPIN timeout is disabled.
1083          */
1084         if (!na->nopin_timeout)
1085                 return;
1086
1087         if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1088                 return;
1089
1090         init_timer(&conn->nopin_timer);
1091         conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1092         conn->nopin_timer.data = (unsigned long)conn;
1093         conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1094         conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1095         conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1096         add_timer(&conn->nopin_timer);
1097
1098         pr_debug("Started NOPIN Timer on CID: %d at %u second"
1099                 " interval\n", conn->cid, na->nopin_timeout);
1100 }
1101
1102 void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1103 {
1104         struct iscsi_session *sess = conn->sess;
1105         struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1106         /*
1107          * NOPIN timeout is disabled..
1108          */
1109         if (!na->nopin_timeout)
1110                 return;
1111
1112         spin_lock_bh(&conn->nopin_timer_lock);
1113         if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
1114                 spin_unlock_bh(&conn->nopin_timer_lock);
1115                 return;
1116         }
1117
1118         init_timer(&conn->nopin_timer);
1119         conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1120         conn->nopin_timer.data = (unsigned long)conn;
1121         conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1122         conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1123         conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1124         add_timer(&conn->nopin_timer);
1125
1126         pr_debug("Started NOPIN Timer on CID: %d at %u second"
1127                         " interval\n", conn->cid, na->nopin_timeout);
1128         spin_unlock_bh(&conn->nopin_timer_lock);
1129 }
1130
1131 void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1132 {
1133         spin_lock_bh(&conn->nopin_timer_lock);
1134         if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1135                 spin_unlock_bh(&conn->nopin_timer_lock);
1136                 return;
1137         }
1138         conn->nopin_timer_flags |= ISCSI_TF_STOP;
1139         spin_unlock_bh(&conn->nopin_timer_lock);
1140
1141         del_timer_sync(&conn->nopin_timer);
1142
1143         spin_lock_bh(&conn->nopin_timer_lock);
1144         conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1145         spin_unlock_bh(&conn->nopin_timer_lock);
1146 }
1147
1148 int iscsit_send_tx_data(
1149         struct iscsi_cmd *cmd,
1150         struct iscsi_conn *conn,
1151         int use_misc)
1152 {
1153         int tx_sent, tx_size;
1154         u32 iov_count;
1155         struct kvec *iov;
1156
1157 send_data:
1158         tx_size = cmd->tx_size;
1159
1160         if (!use_misc) {
1161                 iov = &cmd->iov_data[0];
1162                 iov_count = cmd->iov_data_count;
1163         } else {
1164                 iov = &cmd->iov_misc[0];
1165                 iov_count = cmd->iov_misc_count;
1166         }
1167
1168         tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1169         if (tx_size != tx_sent) {
1170                 if (tx_sent == -EAGAIN) {
1171                         pr_err("tx_data() returned -EAGAIN\n");
1172                         goto send_data;
1173                 } else
1174                         return -1;
1175         }
1176         cmd->tx_size = 0;
1177
1178         return 0;
1179 }
1180
1181 int iscsit_fe_sendpage_sg(
1182         struct iscsi_cmd *cmd,
1183         struct iscsi_conn *conn)
1184 {
1185         struct scatterlist *sg = cmd->first_data_sg;
1186         struct kvec iov;
1187         u32 tx_hdr_size, data_len;
1188         u32 offset = cmd->first_data_sg_off;
1189         int tx_sent, iov_off;
1190
1191 send_hdr:
1192         tx_hdr_size = ISCSI_HDR_LEN;
1193         if (conn->conn_ops->HeaderDigest)
1194                 tx_hdr_size += ISCSI_CRC_LEN;
1195
1196         iov.iov_base = cmd->pdu;
1197         iov.iov_len = tx_hdr_size;
1198
1199         tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1200         if (tx_hdr_size != tx_sent) {
1201                 if (tx_sent == -EAGAIN) {
1202                         pr_err("tx_data() returned -EAGAIN\n");
1203                         goto send_hdr;
1204                 }
1205                 return -1;
1206         }
1207
1208         data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
1209         /*
1210          * Set iov_off used by padding and data digest tx_data() calls below
1211          * in order to determine proper offset into cmd->iov_data[]
1212          */
1213         if (conn->conn_ops->DataDigest) {
1214                 data_len -= ISCSI_CRC_LEN;
1215                 if (cmd->padding)
1216                         iov_off = (cmd->iov_data_count - 2);
1217                 else
1218                         iov_off = (cmd->iov_data_count - 1);
1219         } else {
1220                 iov_off = (cmd->iov_data_count - 1);
1221         }
1222         /*
1223          * Perform sendpage() for each page in the scatterlist
1224          */
1225         while (data_len) {
1226                 u32 space = (sg->length - offset);
1227                 u32 sub_len = min_t(u32, data_len, space);
1228 send_pg:
1229                 tx_sent = conn->sock->ops->sendpage(conn->sock,
1230                                         sg_page(sg), sg->offset + offset, sub_len, 0);
1231                 if (tx_sent != sub_len) {
1232                         if (tx_sent == -EAGAIN) {
1233                                 pr_err("tcp_sendpage() returned"
1234                                                 " -EAGAIN\n");
1235                                 goto send_pg;
1236                         }
1237
1238                         pr_err("tcp_sendpage() failure: %d\n",
1239                                         tx_sent);
1240                         return -1;
1241                 }
1242
1243                 data_len -= sub_len;
1244                 offset = 0;
1245                 sg = sg_next(sg);
1246         }
1247
1248 send_padding:
1249         if (cmd->padding) {
1250                 struct kvec *iov_p = &cmd->iov_data[iov_off++];
1251
1252                 tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1253                 if (cmd->padding != tx_sent) {
1254                         if (tx_sent == -EAGAIN) {
1255                                 pr_err("tx_data() returned -EAGAIN\n");
1256                                 goto send_padding;
1257                         }
1258                         return -1;
1259                 }
1260         }
1261
1262 send_datacrc:
1263         if (conn->conn_ops->DataDigest) {
1264                 struct kvec *iov_d = &cmd->iov_data[iov_off];
1265
1266                 tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1267                 if (ISCSI_CRC_LEN != tx_sent) {
1268                         if (tx_sent == -EAGAIN) {
1269                                 pr_err("tx_data() returned -EAGAIN\n");
1270                                 goto send_datacrc;
1271                         }
1272                         return -1;
1273                 }
1274         }
1275
1276         return 0;
1277 }
1278
1279 /*
1280  *      This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1281  *      back to the Initiator when an expection condition occurs with the
1282  *      errors set in status_class and status_detail.
1283  *
1284  *      Parameters:     iSCSI Connection, Status Class, Status Detail.
1285  *      Returns:        0 on success, -1 on error.
1286  */
1287 int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1288 {
1289         struct iscsi_login_rsp *hdr;
1290         struct iscsi_login *login = conn->conn_login;
1291
1292         login->login_failed = 1;
1293         iscsit_collect_login_stats(conn, status_class, status_detail);
1294
1295         hdr     = (struct iscsi_login_rsp *)&login->rsp[0];
1296         hdr->opcode             = ISCSI_OP_LOGIN_RSP;
1297         hdr->status_class       = status_class;
1298         hdr->status_detail      = status_detail;
1299         hdr->itt                = conn->login_itt;
1300
1301         return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
1302 }
1303
1304 void iscsit_print_session_params(struct iscsi_session *sess)
1305 {
1306         struct iscsi_conn *conn;
1307
1308         pr_debug("-----------------------------[Session Params for"
1309                 " SID: %u]-----------------------------\n", sess->sid);
1310         spin_lock_bh(&sess->conn_lock);
1311         list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1312                 iscsi_dump_conn_ops(conn->conn_ops);
1313         spin_unlock_bh(&sess->conn_lock);
1314
1315         iscsi_dump_sess_ops(sess->sess_ops);
1316 }
1317
1318 static int iscsit_do_rx_data(
1319         struct iscsi_conn *conn,
1320         struct iscsi_data_count *count)
1321 {
1322         int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
1323         struct kvec *iov_p;
1324         struct msghdr msg;
1325
1326         if (!conn || !conn->sock || !conn->conn_ops)
1327                 return -1;
1328
1329         memset(&msg, 0, sizeof(struct msghdr));
1330
1331         iov_p = count->iov;
1332         iov_len = count->iov_count;
1333
1334         while (total_rx < data) {
1335                 rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
1336                                         (data - total_rx), MSG_WAITALL);
1337                 if (rx_loop <= 0) {
1338                         pr_debug("rx_loop: %d total_rx: %d\n",
1339                                 rx_loop, total_rx);
1340                         return rx_loop;
1341                 }
1342                 total_rx += rx_loop;
1343                 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1344                                 rx_loop, total_rx, data);
1345         }
1346
1347         return total_rx;
1348 }
1349
1350 static int iscsit_do_tx_data(
1351         struct iscsi_conn *conn,
1352         struct iscsi_data_count *count)
1353 {
1354         int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
1355         struct kvec *iov_p;
1356         struct msghdr msg;
1357
1358         if (!conn || !conn->sock || !conn->conn_ops)
1359                 return -1;
1360
1361         if (data <= 0) {
1362                 pr_err("Data length is: %d\n", data);
1363                 return -1;
1364         }
1365
1366         memset(&msg, 0, sizeof(struct msghdr));
1367
1368         iov_p = count->iov;
1369         iov_len = count->iov_count;
1370
1371         while (total_tx < data) {
1372                 tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
1373                                         (data - total_tx));
1374                 if (tx_loop <= 0) {
1375                         pr_debug("tx_loop: %d total_tx %d\n",
1376                                 tx_loop, total_tx);
1377                         return tx_loop;
1378                 }
1379                 total_tx += tx_loop;
1380                 pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
1381                                         tx_loop, total_tx, data);
1382         }
1383
1384         return total_tx;
1385 }
1386
1387 int rx_data(
1388         struct iscsi_conn *conn,
1389         struct kvec *iov,
1390         int iov_count,
1391         int data)
1392 {
1393         struct iscsi_data_count c;
1394
1395         if (!conn || !conn->sock || !conn->conn_ops)
1396                 return -1;
1397
1398         memset(&c, 0, sizeof(struct iscsi_data_count));
1399         c.iov = iov;
1400         c.iov_count = iov_count;
1401         c.data_length = data;
1402         c.type = ISCSI_RX_DATA;
1403
1404         return iscsit_do_rx_data(conn, &c);
1405 }
1406
1407 int tx_data(
1408         struct iscsi_conn *conn,
1409         struct kvec *iov,
1410         int iov_count,
1411         int data)
1412 {
1413         struct iscsi_data_count c;
1414
1415         if (!conn || !conn->sock || !conn->conn_ops)
1416                 return -1;
1417
1418         memset(&c, 0, sizeof(struct iscsi_data_count));
1419         c.iov = iov;
1420         c.iov_count = iov_count;
1421         c.data_length = data;
1422         c.type = ISCSI_TX_DATA;
1423
1424         return iscsit_do_tx_data(conn, &c);
1425 }
1426
1427 void iscsit_collect_login_stats(
1428         struct iscsi_conn *conn,
1429         u8 status_class,
1430         u8 status_detail)
1431 {
1432         struct iscsi_param *intrname = NULL;
1433         struct iscsi_tiqn *tiqn;
1434         struct iscsi_login_stats *ls;
1435
1436         tiqn = iscsit_snmp_get_tiqn(conn);
1437         if (!tiqn)
1438                 return;
1439
1440         ls = &tiqn->login_stats;
1441
1442         spin_lock(&ls->lock);
1443         if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
1444             ((get_jiffies_64() - ls->last_fail_time) < 10)) {
1445                 /* We already have the failure info for this login */
1446                 spin_unlock(&ls->lock);
1447                 return;
1448         }
1449
1450         if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1451                 ls->accepts++;
1452         else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1453                 ls->redirects++;
1454                 ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1455         } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1456                  (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1457                 ls->authenticate_fails++;
1458                 ls->last_fail_type =  ISCSI_LOGIN_FAIL_AUTHENTICATE;
1459         } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1460                  (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1461                 ls->authorize_fails++;
1462                 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1463         } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1464                  (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1465                 ls->negotiate_fails++;
1466                 ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1467         } else {
1468                 ls->other_fails++;
1469                 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1470         }
1471
1472         /* Save initiator name, ip address and time, if it is a failed login */
1473         if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1474                 if (conn->param_list)
1475                         intrname = iscsi_find_param_from_key(INITIATORNAME,
1476                                                              conn->param_list);
1477                 strcpy(ls->last_intr_fail_name,
1478                        (intrname ? intrname->value : "Unknown"));
1479
1480                 ls->last_intr_fail_ip_family = conn->login_family;
1481
1482                 snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
1483                                 "%s", conn->login_ip);
1484                 ls->last_fail_time = get_jiffies_64();
1485         }
1486
1487         spin_unlock(&ls->lock);
1488 }
1489
1490 struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1491 {
1492         struct iscsi_portal_group *tpg;
1493
1494         if (!conn || !conn->sess)
1495                 return NULL;
1496
1497         tpg = conn->sess->tpg;
1498         if (!tpg)
1499                 return NULL;
1500
1501         if (!tpg->tpg_tiqn)
1502                 return NULL;
1503
1504         return tpg->tpg_tiqn;
1505 }