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