blob: 1f5a456f26b4b0b3f0a0e251d7370045ed8f2ad7 [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
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>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050025#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000026#include <target/target_core_configfs.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
57extern struct list_head g_tiqn_list;
58extern spinlock_t tiqn_lock;
59
60/*
61 * Called with cmd->r2t_lock held.
62 */
63int 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
92struct 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
114struct 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 */
135void 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
141void 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 */
155struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
156{
157 struct iscsi_cmd *cmd;
158
159 cmd = kmem_cache_zalloc(lio_cmd_cache, gfp_mask);
160 if (!cmd) {
161 pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
162 return NULL;
163 }
164
165 cmd->conn = conn;
Andy Grover2fbb4712012-04-03 15:51:01 -0700166 INIT_LIST_HEAD(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000167 INIT_LIST_HEAD(&cmd->datain_list);
168 INIT_LIST_HEAD(&cmd->cmd_r2t_list);
169 init_completion(&cmd->reject_comp);
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
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000179int iscsit_decide_list_to_build(
180 struct iscsi_cmd *cmd,
181 u32 immediate_data_length)
182{
183 struct iscsi_build_list bl;
184 struct iscsi_conn *conn = cmd->conn;
185 struct iscsi_session *sess = conn->sess;
186 struct iscsi_node_attrib *na;
187
188 if (sess->sess_ops->DataSequenceInOrder &&
189 sess->sess_ops->DataPDUInOrder)
190 return 0;
191
192 if (cmd->data_direction == DMA_NONE)
193 return 0;
194
195 na = iscsit_tpg_get_node_attrib(sess);
196 memset(&bl, 0, sizeof(struct iscsi_build_list));
197
198 if (cmd->data_direction == DMA_FROM_DEVICE) {
199 bl.data_direction = ISCSI_PDU_READ;
200 bl.type = PDULIST_NORMAL;
201 if (na->random_datain_pdu_offsets)
202 bl.randomize |= RANDOM_DATAIN_PDU_OFFSETS;
203 if (na->random_datain_seq_offsets)
204 bl.randomize |= RANDOM_DATAIN_SEQ_OFFSETS;
205 } else {
206 bl.data_direction = ISCSI_PDU_WRITE;
207 bl.immediate_data_length = immediate_data_length;
208 if (na->random_r2t_offsets)
209 bl.randomize |= RANDOM_R2T_OFFSETS;
210
211 if (!cmd->immediate_data && !cmd->unsolicited_data)
212 bl.type = PDULIST_NORMAL;
213 else if (cmd->immediate_data && !cmd->unsolicited_data)
214 bl.type = PDULIST_IMMEDIATE;
215 else if (!cmd->immediate_data && cmd->unsolicited_data)
216 bl.type = PDULIST_UNSOLICITED;
217 else if (cmd->immediate_data && cmd->unsolicited_data)
218 bl.type = PDULIST_IMMEDIATE_AND_UNSOLICITED;
219 }
220
221 return iscsit_do_build_list(cmd, &bl);
222}
223
224struct iscsi_seq *iscsit_get_seq_holder_for_datain(
225 struct iscsi_cmd *cmd,
226 u32 seq_send_order)
227{
228 u32 i;
229
230 for (i = 0; i < cmd->seq_count; i++)
231 if (cmd->seq_list[i].seq_send_order == seq_send_order)
232 return &cmd->seq_list[i];
233
234 return NULL;
235}
236
237struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
238{
239 u32 i;
240
241 if (!cmd->seq_list) {
242 pr_err("struct iscsi_cmd->seq_list is NULL!\n");
243 return NULL;
244 }
245
246 for (i = 0; i < cmd->seq_count; i++) {
247 if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
248 continue;
249 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
250 cmd->seq_send_order++;
251 return &cmd->seq_list[i];
252 }
253 }
254
255 return NULL;
256}
257
258struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
259 struct iscsi_cmd *cmd,
260 u32 r2t_sn)
261{
262 struct iscsi_r2t *r2t;
263
264 spin_lock_bh(&cmd->r2t_lock);
265 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
266 if (r2t->r2t_sn == r2t_sn) {
267 spin_unlock_bh(&cmd->r2t_lock);
268 return r2t;
269 }
270 }
271 spin_unlock_bh(&cmd->r2t_lock);
272
273 return NULL;
274}
275
276static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
277{
278 int ret;
279
280 /*
281 * This is the proper method of checking received CmdSN against
282 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
283 * or order CmdSNs due to multiple connection sessions and/or
284 * CRC failures.
285 */
286 if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
287 pr_err("Received CmdSN: 0x%08x is greater than"
288 " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
289 sess->max_cmd_sn);
290 ret = CMDSN_ERROR_CANNOT_RECOVER;
291
292 } else if (cmdsn == sess->exp_cmd_sn) {
293 sess->exp_cmd_sn++;
294 pr_debug("Received CmdSN matches ExpCmdSN,"
295 " incremented ExpCmdSN to: 0x%08x\n",
296 sess->exp_cmd_sn);
297 ret = CMDSN_NORMAL_OPERATION;
298
299 } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
300 pr_debug("Received CmdSN: 0x%08x is greater"
301 " than ExpCmdSN: 0x%08x, not acknowledging.\n",
302 cmdsn, sess->exp_cmd_sn);
303 ret = CMDSN_HIGHER_THAN_EXP;
304
305 } else {
306 pr_err("Received CmdSN: 0x%08x is less than"
307 " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
308 sess->exp_cmd_sn);
309 ret = CMDSN_LOWER_THAN_EXP;
310 }
311
312 return ret;
313}
314
315/*
316 * Commands may be received out of order if MC/S is in use.
317 * Ensure they are executed in CmdSN order.
318 */
319int iscsit_sequence_cmd(
320 struct iscsi_conn *conn,
321 struct iscsi_cmd *cmd,
322 u32 cmdsn)
323{
324 int ret;
325 int cmdsn_ret;
326
327 mutex_lock(&conn->sess->cmdsn_mutex);
328
329 cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, cmdsn);
330 switch (cmdsn_ret) {
331 case CMDSN_NORMAL_OPERATION:
332 ret = iscsit_execute_cmd(cmd, 0);
333 if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
334 iscsit_execute_ooo_cmdsns(conn->sess);
335 break;
336 case CMDSN_HIGHER_THAN_EXP:
337 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, cmdsn);
338 break;
339 case CMDSN_LOWER_THAN_EXP:
340 cmd->i_state = ISTATE_REMOVE;
341 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
342 ret = cmdsn_ret;
343 break;
344 default:
345 ret = cmdsn_ret;
346 break;
347 }
348 mutex_unlock(&conn->sess->cmdsn_mutex);
349
350 return ret;
351}
352
353int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
354{
355 struct iscsi_conn *conn = cmd->conn;
356 struct se_cmd *se_cmd = &cmd->se_cmd;
357 struct iscsi_data *hdr = (struct iscsi_data *) buf;
358 u32 payload_length = ntoh24(hdr->dlength);
359
360 if (conn->sess->sess_ops->InitialR2T) {
361 pr_err("Received unexpected unsolicited data"
362 " while InitialR2T=Yes, protocol error.\n");
363 transport_send_check_condition_and_sense(se_cmd,
364 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
365 return -1;
366 }
367
368 if ((cmd->first_burst_len + payload_length) >
369 conn->sess->sess_ops->FirstBurstLength) {
370 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
371 " for this Unsolicited DataOut Burst.\n",
372 (cmd->first_burst_len + payload_length),
373 conn->sess->sess_ops->FirstBurstLength);
374 transport_send_check_condition_and_sense(se_cmd,
375 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
376 return -1;
377 }
378
379 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
380 return 0;
381
Andy Groverebf1d952012-04-03 15:51:24 -0700382 if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000383 ((cmd->first_burst_len + payload_length) !=
384 conn->sess->sess_ops->FirstBurstLength)) {
385 pr_err("Unsolicited non-immediate data received %u"
386 " does not equal FirstBurstLength: %u, and does"
387 " not equal ExpXferLen %u.\n",
388 (cmd->first_burst_len + payload_length),
Andy Groverebf1d952012-04-03 15:51:24 -0700389 conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000390 transport_send_check_condition_and_sense(se_cmd,
391 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
392 return -1;
393 }
394 return 0;
395}
396
397struct iscsi_cmd *iscsit_find_cmd_from_itt(
398 struct iscsi_conn *conn,
399 u32 init_task_tag)
400{
401 struct iscsi_cmd *cmd;
402
403 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700404 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000405 if (cmd->init_task_tag == init_task_tag) {
406 spin_unlock_bh(&conn->cmd_lock);
407 return cmd;
408 }
409 }
410 spin_unlock_bh(&conn->cmd_lock);
411
412 pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
413 init_task_tag, conn->cid);
414 return NULL;
415}
416
417struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
418 struct iscsi_conn *conn,
419 u32 init_task_tag,
420 u32 length)
421{
422 struct iscsi_cmd *cmd;
423
424 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700425 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000426 if (cmd->init_task_tag == init_task_tag) {
427 spin_unlock_bh(&conn->cmd_lock);
428 return cmd;
429 }
430 }
431 spin_unlock_bh(&conn->cmd_lock);
432
433 pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
434 " dumping payload\n", init_task_tag, conn->cid);
435 if (length)
436 iscsit_dump_data_payload(conn, length, 1);
437
438 return NULL;
439}
440
441struct iscsi_cmd *iscsit_find_cmd_from_ttt(
442 struct iscsi_conn *conn,
443 u32 targ_xfer_tag)
444{
445 struct iscsi_cmd *cmd = NULL;
446
447 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700448 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000449 if (cmd->targ_xfer_tag == targ_xfer_tag) {
450 spin_unlock_bh(&conn->cmd_lock);
451 return cmd;
452 }
453 }
454 spin_unlock_bh(&conn->cmd_lock);
455
456 pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
457 targ_xfer_tag, conn->cid);
458 return NULL;
459}
460
461int iscsit_find_cmd_for_recovery(
462 struct iscsi_session *sess,
463 struct iscsi_cmd **cmd_ptr,
464 struct iscsi_conn_recovery **cr_ptr,
465 u32 init_task_tag)
466{
467 struct iscsi_cmd *cmd = NULL;
468 struct iscsi_conn_recovery *cr;
469 /*
470 * Scan through the inactive connection recovery list's command list.
471 * If init_task_tag matches the command is still alligent.
472 */
473 spin_lock(&sess->cr_i_lock);
474 list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
475 spin_lock(&cr->conn_recovery_cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700476 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 if (cmd->init_task_tag == init_task_tag) {
478 spin_unlock(&cr->conn_recovery_cmd_lock);
479 spin_unlock(&sess->cr_i_lock);
480
481 *cr_ptr = cr;
482 *cmd_ptr = cmd;
483 return -2;
484 }
485 }
486 spin_unlock(&cr->conn_recovery_cmd_lock);
487 }
488 spin_unlock(&sess->cr_i_lock);
489 /*
490 * Scan through the active connection recovery list's command list.
491 * If init_task_tag matches the command is ready to be reassigned.
492 */
493 spin_lock(&sess->cr_a_lock);
494 list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
495 spin_lock(&cr->conn_recovery_cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700496 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000497 if (cmd->init_task_tag == init_task_tag) {
498 spin_unlock(&cr->conn_recovery_cmd_lock);
499 spin_unlock(&sess->cr_a_lock);
500
501 *cr_ptr = cr;
502 *cmd_ptr = cmd;
503 return 0;
504 }
505 }
506 spin_unlock(&cr->conn_recovery_cmd_lock);
507 }
508 spin_unlock(&sess->cr_a_lock);
509
510 return -1;
511}
512
513void iscsit_add_cmd_to_immediate_queue(
514 struct iscsi_cmd *cmd,
515 struct iscsi_conn *conn,
516 u8 state)
517{
518 struct iscsi_queue_req *qr;
519
520 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
521 if (!qr) {
522 pr_err("Unable to allocate memory for"
523 " struct iscsi_queue_req\n");
524 return;
525 }
526 INIT_LIST_HEAD(&qr->qr_list);
527 qr->cmd = cmd;
528 qr->state = state;
529
530 spin_lock_bh(&conn->immed_queue_lock);
531 list_add_tail(&qr->qr_list, &conn->immed_queue_list);
532 atomic_inc(&cmd->immed_queue_count);
533 atomic_set(&conn->check_immediate_queue, 1);
534 spin_unlock_bh(&conn->immed_queue_lock);
535
536 wake_up_process(conn->thread_set->tx_thread);
537}
538
539struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
540{
541 struct iscsi_queue_req *qr;
542
543 spin_lock_bh(&conn->immed_queue_lock);
544 if (list_empty(&conn->immed_queue_list)) {
545 spin_unlock_bh(&conn->immed_queue_lock);
546 return NULL;
547 }
548 list_for_each_entry(qr, &conn->immed_queue_list, qr_list)
549 break;
550
551 list_del(&qr->qr_list);
552 if (qr->cmd)
553 atomic_dec(&qr->cmd->immed_queue_count);
554 spin_unlock_bh(&conn->immed_queue_lock);
555
556 return qr;
557}
558
559static void iscsit_remove_cmd_from_immediate_queue(
560 struct iscsi_cmd *cmd,
561 struct iscsi_conn *conn)
562{
563 struct iscsi_queue_req *qr, *qr_tmp;
564
565 spin_lock_bh(&conn->immed_queue_lock);
566 if (!atomic_read(&cmd->immed_queue_count)) {
567 spin_unlock_bh(&conn->immed_queue_lock);
568 return;
569 }
570
571 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
572 if (qr->cmd != cmd)
573 continue;
574
575 atomic_dec(&qr->cmd->immed_queue_count);
576 list_del(&qr->qr_list);
577 kmem_cache_free(lio_qr_cache, qr);
578 }
579 spin_unlock_bh(&conn->immed_queue_lock);
580
581 if (atomic_read(&cmd->immed_queue_count)) {
582 pr_err("ITT: 0x%08x immed_queue_count: %d\n",
583 cmd->init_task_tag,
584 atomic_read(&cmd->immed_queue_count));
585 }
586}
587
588void iscsit_add_cmd_to_response_queue(
589 struct iscsi_cmd *cmd,
590 struct iscsi_conn *conn,
591 u8 state)
592{
593 struct iscsi_queue_req *qr;
594
595 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
596 if (!qr) {
597 pr_err("Unable to allocate memory for"
598 " struct iscsi_queue_req\n");
599 return;
600 }
601 INIT_LIST_HEAD(&qr->qr_list);
602 qr->cmd = cmd;
603 qr->state = state;
604
605 spin_lock_bh(&conn->response_queue_lock);
606 list_add_tail(&qr->qr_list, &conn->response_queue_list);
607 atomic_inc(&cmd->response_queue_count);
608 spin_unlock_bh(&conn->response_queue_lock);
609
610 wake_up_process(conn->thread_set->tx_thread);
611}
612
613struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
614{
615 struct iscsi_queue_req *qr;
616
617 spin_lock_bh(&conn->response_queue_lock);
618 if (list_empty(&conn->response_queue_list)) {
619 spin_unlock_bh(&conn->response_queue_lock);
620 return NULL;
621 }
622
623 list_for_each_entry(qr, &conn->response_queue_list, qr_list)
624 break;
625
626 list_del(&qr->qr_list);
627 if (qr->cmd)
628 atomic_dec(&qr->cmd->response_queue_count);
629 spin_unlock_bh(&conn->response_queue_lock);
630
631 return qr;
632}
633
634static void iscsit_remove_cmd_from_response_queue(
635 struct iscsi_cmd *cmd,
636 struct iscsi_conn *conn)
637{
638 struct iscsi_queue_req *qr, *qr_tmp;
639
640 spin_lock_bh(&conn->response_queue_lock);
641 if (!atomic_read(&cmd->response_queue_count)) {
642 spin_unlock_bh(&conn->response_queue_lock);
643 return;
644 }
645
646 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
647 qr_list) {
648 if (qr->cmd != cmd)
649 continue;
650
651 atomic_dec(&qr->cmd->response_queue_count);
652 list_del(&qr->qr_list);
653 kmem_cache_free(lio_qr_cache, qr);
654 }
655 spin_unlock_bh(&conn->response_queue_lock);
656
657 if (atomic_read(&cmd->response_queue_count)) {
658 pr_err("ITT: 0x%08x response_queue_count: %d\n",
659 cmd->init_task_tag,
660 atomic_read(&cmd->response_queue_count));
661 }
662}
663
664void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
665{
666 struct iscsi_queue_req *qr, *qr_tmp;
667
668 spin_lock_bh(&conn->immed_queue_lock);
669 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
670 list_del(&qr->qr_list);
671 if (qr->cmd)
672 atomic_dec(&qr->cmd->immed_queue_count);
673
674 kmem_cache_free(lio_qr_cache, qr);
675 }
676 spin_unlock_bh(&conn->immed_queue_lock);
677
678 spin_lock_bh(&conn->response_queue_lock);
679 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
680 qr_list) {
681 list_del(&qr->qr_list);
682 if (qr->cmd)
683 atomic_dec(&qr->cmd->response_queue_count);
684
685 kmem_cache_free(lio_qr_cache, qr);
686 }
687 spin_unlock_bh(&conn->response_queue_lock);
688}
689
690void iscsit_release_cmd(struct iscsi_cmd *cmd)
691{
692 struct iscsi_conn *conn = cmd->conn;
693 int i;
694
695 iscsit_free_r2ts_from_list(cmd);
696 iscsit_free_all_datain_reqs(cmd);
697
698 kfree(cmd->buf_ptr);
699 kfree(cmd->pdu_list);
700 kfree(cmd->seq_list);
701 kfree(cmd->tmr_req);
702 kfree(cmd->iov_data);
703
704 for (i = 0; i < cmd->t_mem_sg_nents; i++)
705 __free_page(sg_page(&cmd->t_mem_sg[i]));
706
707 kfree(cmd->t_mem_sg);
708
709 if (conn) {
710 iscsit_remove_cmd_from_immediate_queue(cmd, conn);
711 iscsit_remove_cmd_from_response_queue(cmd, conn);
712 }
713
714 kmem_cache_free(lio_cmd_cache, cmd);
715}
716
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700717void iscsit_free_cmd(struct iscsi_cmd *cmd)
718{
719 /*
720 * Determine if a struct se_cmd is assoicated with
721 * this struct iscsi_cmd.
722 */
723 switch (cmd->iscsi_opcode) {
724 case ISCSI_OP_SCSI_CMD:
725 case ISCSI_OP_SCSI_TMFUNC:
726 transport_generic_free_cmd(&cmd->se_cmd, 1);
727 break;
Nicholas Bellingerc1ce4bd2012-01-16 16:04:15 -0800728 case ISCSI_OP_REJECT:
729 /*
730 * Handle special case for REJECT when iscsi_add_reject*() has
731 * overwritten the original iscsi_opcode assignment, and the
732 * associated cmd->se_cmd needs to be released.
733 */
734 if (cmd->se_cmd.se_tfo != NULL) {
735 transport_generic_free_cmd(&cmd->se_cmd, 1);
736 break;
737 }
738 /* Fall-through */
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700739 default:
740 iscsit_release_cmd(cmd);
741 break;
742 }
743}
744
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000745int iscsit_check_session_usage_count(struct iscsi_session *sess)
746{
747 spin_lock_bh(&sess->session_usage_lock);
748 if (sess->session_usage_count != 0) {
749 sess->session_waiting_on_uc = 1;
750 spin_unlock_bh(&sess->session_usage_lock);
751 if (in_interrupt())
752 return 2;
753
754 wait_for_completion(&sess->session_waiting_on_uc_comp);
755 return 1;
756 }
757 spin_unlock_bh(&sess->session_usage_lock);
758
759 return 0;
760}
761
762void iscsit_dec_session_usage_count(struct iscsi_session *sess)
763{
764 spin_lock_bh(&sess->session_usage_lock);
765 sess->session_usage_count--;
766
767 if (!sess->session_usage_count && sess->session_waiting_on_uc)
768 complete(&sess->session_waiting_on_uc_comp);
769
770 spin_unlock_bh(&sess->session_usage_lock);
771}
772
773void iscsit_inc_session_usage_count(struct iscsi_session *sess)
774{
775 spin_lock_bh(&sess->session_usage_lock);
776 sess->session_usage_count++;
777 spin_unlock_bh(&sess->session_usage_lock);
778}
779
780/*
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000781 * Setup conn->if_marker and conn->of_marker values based upon
782 * the initial marker-less interval. (see iSCSI v19 A.2)
783 */
784int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
785{
786 int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
787 /*
788 * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
789 */
790 u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
791 u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
792
793 if (conn->conn_ops->OFMarker) {
794 /*
795 * Account for the first Login Command received not
796 * via iscsi_recv_msg().
797 */
798 conn->of_marker += ISCSI_HDR_LEN;
799 if (conn->of_marker <= OFMarkInt) {
800 conn->of_marker = (OFMarkInt - conn->of_marker);
801 } else {
802 login_ofmarker_count = (conn->of_marker / OFMarkInt);
803 next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
804 (login_ofmarker_count * MARKER_SIZE);
805 conn->of_marker = (next_marker - conn->of_marker);
806 }
807 conn->of_marker_offset = 0;
808 pr_debug("Setting OFMarker value to %u based on Initial"
809 " Markerless Interval.\n", conn->of_marker);
810 }
811
812 if (conn->conn_ops->IFMarker) {
813 if (conn->if_marker <= IFMarkInt) {
814 conn->if_marker = (IFMarkInt - conn->if_marker);
815 } else {
816 login_ifmarker_count = (conn->if_marker / IFMarkInt);
817 next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
818 (login_ifmarker_count * MARKER_SIZE);
819 conn->if_marker = (next_marker - conn->if_marker);
820 }
821 pr_debug("Setting IFMarker value to %u based on Initial"
822 " Markerless Interval.\n", conn->if_marker);
823 }
824
825 return 0;
826}
827
828struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
829{
830 struct iscsi_conn *conn;
831
832 spin_lock_bh(&sess->conn_lock);
833 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
834 if ((conn->cid == cid) &&
835 (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
836 iscsit_inc_conn_usage_count(conn);
837 spin_unlock_bh(&sess->conn_lock);
838 return conn;
839 }
840 }
841 spin_unlock_bh(&sess->conn_lock);
842
843 return NULL;
844}
845
846struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
847{
848 struct iscsi_conn *conn;
849
850 spin_lock_bh(&sess->conn_lock);
851 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
852 if (conn->cid == cid) {
853 iscsit_inc_conn_usage_count(conn);
854 spin_lock(&conn->state_lock);
855 atomic_set(&conn->connection_wait_rcfr, 1);
856 spin_unlock(&conn->state_lock);
857 spin_unlock_bh(&sess->conn_lock);
858 return conn;
859 }
860 }
861 spin_unlock_bh(&sess->conn_lock);
862
863 return NULL;
864}
865
866void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
867{
868 spin_lock_bh(&conn->conn_usage_lock);
869 if (conn->conn_usage_count != 0) {
870 conn->conn_waiting_on_uc = 1;
871 spin_unlock_bh(&conn->conn_usage_lock);
872
873 wait_for_completion(&conn->conn_waiting_on_uc_comp);
874 return;
875 }
876 spin_unlock_bh(&conn->conn_usage_lock);
877}
878
879void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
880{
881 spin_lock_bh(&conn->conn_usage_lock);
882 conn->conn_usage_count--;
883
884 if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
885 complete(&conn->conn_waiting_on_uc_comp);
886
887 spin_unlock_bh(&conn->conn_usage_lock);
888}
889
890void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
891{
892 spin_lock_bh(&conn->conn_usage_lock);
893 conn->conn_usage_count++;
894 spin_unlock_bh(&conn->conn_usage_lock);
895}
896
897static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
898{
899 u8 state;
900 struct iscsi_cmd *cmd;
901
902 cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
903 if (!cmd)
904 return -1;
905
906 cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
907 state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
908 ISTATE_SEND_NOPIN_NO_RESPONSE;
909 cmd->init_task_tag = 0xFFFFFFFF;
910 spin_lock_bh(&conn->sess->ttt_lock);
911 cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
912 0xFFFFFFFF;
913 if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
914 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
915 spin_unlock_bh(&conn->sess->ttt_lock);
916
917 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700918 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000919 spin_unlock_bh(&conn->cmd_lock);
920
921 if (want_response)
922 iscsit_start_nopin_response_timer(conn);
923 iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
924
925 return 0;
926}
927
928static void iscsit_handle_nopin_response_timeout(unsigned long data)
929{
930 struct iscsi_conn *conn = (struct iscsi_conn *) data;
931
932 iscsit_inc_conn_usage_count(conn);
933
934 spin_lock_bh(&conn->nopin_timer_lock);
935 if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
936 spin_unlock_bh(&conn->nopin_timer_lock);
937 iscsit_dec_conn_usage_count(conn);
938 return;
939 }
940
941 pr_debug("Did not receive response to NOPIN on CID: %hu on"
942 " SID: %u, failing connection.\n", conn->cid,
943 conn->sess->sid);
944 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
945 spin_unlock_bh(&conn->nopin_timer_lock);
946
947 {
948 struct iscsi_portal_group *tpg = conn->sess->tpg;
949 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
950
951 if (tiqn) {
952 spin_lock_bh(&tiqn->sess_err_stats.lock);
953 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
Jörn Engel8359cf42011-11-24 02:05:51 +0100954 conn->sess->sess_ops->InitiatorName);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000955 tiqn->sess_err_stats.last_sess_failure_type =
956 ISCSI_SESS_ERR_CXN_TIMEOUT;
957 tiqn->sess_err_stats.cxn_timeout_errors++;
958 conn->sess->conn_timeout_errors++;
959 spin_unlock_bh(&tiqn->sess_err_stats.lock);
960 }
961 }
962
963 iscsit_cause_connection_reinstatement(conn, 0);
964 iscsit_dec_conn_usage_count(conn);
965}
966
967void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
968{
969 struct iscsi_session *sess = conn->sess;
970 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
971
972 spin_lock_bh(&conn->nopin_timer_lock);
973 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
974 spin_unlock_bh(&conn->nopin_timer_lock);
975 return;
976 }
977
978 mod_timer(&conn->nopin_response_timer,
979 (get_jiffies_64() + na->nopin_response_timeout * HZ));
980 spin_unlock_bh(&conn->nopin_timer_lock);
981}
982
983/*
984 * Called with conn->nopin_timer_lock held.
985 */
986void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
987{
988 struct iscsi_session *sess = conn->sess;
989 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
990
991 spin_lock_bh(&conn->nopin_timer_lock);
992 if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
993 spin_unlock_bh(&conn->nopin_timer_lock);
994 return;
995 }
996
997 init_timer(&conn->nopin_response_timer);
998 conn->nopin_response_timer.expires =
999 (get_jiffies_64() + na->nopin_response_timeout * HZ);
1000 conn->nopin_response_timer.data = (unsigned long)conn;
1001 conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
1002 conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
1003 conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
1004 add_timer(&conn->nopin_response_timer);
1005
1006 pr_debug("Started NOPIN Response Timer on CID: %d to %u"
1007 " seconds\n", conn->cid, na->nopin_response_timeout);
1008 spin_unlock_bh(&conn->nopin_timer_lock);
1009}
1010
1011void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
1012{
1013 spin_lock_bh(&conn->nopin_timer_lock);
1014 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1015 spin_unlock_bh(&conn->nopin_timer_lock);
1016 return;
1017 }
1018 conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
1019 spin_unlock_bh(&conn->nopin_timer_lock);
1020
1021 del_timer_sync(&conn->nopin_response_timer);
1022
1023 spin_lock_bh(&conn->nopin_timer_lock);
1024 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1025 spin_unlock_bh(&conn->nopin_timer_lock);
1026}
1027
1028static void iscsit_handle_nopin_timeout(unsigned long data)
1029{
1030 struct iscsi_conn *conn = (struct iscsi_conn *) data;
1031
1032 iscsit_inc_conn_usage_count(conn);
1033
1034 spin_lock_bh(&conn->nopin_timer_lock);
1035 if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1036 spin_unlock_bh(&conn->nopin_timer_lock);
1037 iscsit_dec_conn_usage_count(conn);
1038 return;
1039 }
1040 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1041 spin_unlock_bh(&conn->nopin_timer_lock);
1042
1043 iscsit_add_nopin(conn, 1);
1044 iscsit_dec_conn_usage_count(conn);
1045}
1046
1047/*
1048 * Called with conn->nopin_timer_lock held.
1049 */
1050void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1051{
1052 struct iscsi_session *sess = conn->sess;
1053 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1054 /*
1055 * NOPIN timeout is disabled.
1056 */
1057 if (!na->nopin_timeout)
1058 return;
1059
1060 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1061 return;
1062
1063 init_timer(&conn->nopin_timer);
1064 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1065 conn->nopin_timer.data = (unsigned long)conn;
1066 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1067 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1068 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1069 add_timer(&conn->nopin_timer);
1070
1071 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1072 " interval\n", conn->cid, na->nopin_timeout);
1073}
1074
1075void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1076{
1077 struct iscsi_session *sess = conn->sess;
1078 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1079 /*
1080 * NOPIN timeout is disabled..
1081 */
1082 if (!na->nopin_timeout)
1083 return;
1084
1085 spin_lock_bh(&conn->nopin_timer_lock);
1086 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
1087 spin_unlock_bh(&conn->nopin_timer_lock);
1088 return;
1089 }
1090
1091 init_timer(&conn->nopin_timer);
1092 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1093 conn->nopin_timer.data = (unsigned long)conn;
1094 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1095 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1096 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1097 add_timer(&conn->nopin_timer);
1098
1099 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1100 " interval\n", conn->cid, na->nopin_timeout);
1101 spin_unlock_bh(&conn->nopin_timer_lock);
1102}
1103
1104void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1105{
1106 spin_lock_bh(&conn->nopin_timer_lock);
1107 if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1108 spin_unlock_bh(&conn->nopin_timer_lock);
1109 return;
1110 }
1111 conn->nopin_timer_flags |= ISCSI_TF_STOP;
1112 spin_unlock_bh(&conn->nopin_timer_lock);
1113
1114 del_timer_sync(&conn->nopin_timer);
1115
1116 spin_lock_bh(&conn->nopin_timer_lock);
1117 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1118 spin_unlock_bh(&conn->nopin_timer_lock);
1119}
1120
1121int iscsit_send_tx_data(
1122 struct iscsi_cmd *cmd,
1123 struct iscsi_conn *conn,
1124 int use_misc)
1125{
1126 int tx_sent, tx_size;
1127 u32 iov_count;
1128 struct kvec *iov;
1129
1130send_data:
1131 tx_size = cmd->tx_size;
1132
1133 if (!use_misc) {
1134 iov = &cmd->iov_data[0];
1135 iov_count = cmd->iov_data_count;
1136 } else {
1137 iov = &cmd->iov_misc[0];
1138 iov_count = cmd->iov_misc_count;
1139 }
1140
1141 tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1142 if (tx_size != tx_sent) {
1143 if (tx_sent == -EAGAIN) {
1144 pr_err("tx_data() returned -EAGAIN\n");
1145 goto send_data;
1146 } else
1147 return -1;
1148 }
1149 cmd->tx_size = 0;
1150
1151 return 0;
1152}
1153
1154int iscsit_fe_sendpage_sg(
1155 struct iscsi_cmd *cmd,
1156 struct iscsi_conn *conn)
1157{
1158 struct scatterlist *sg = cmd->first_data_sg;
1159 struct kvec iov;
1160 u32 tx_hdr_size, data_len;
1161 u32 offset = cmd->first_data_sg_off;
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001162 int tx_sent, iov_off;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001163
1164send_hdr:
1165 tx_hdr_size = ISCSI_HDR_LEN;
1166 if (conn->conn_ops->HeaderDigest)
1167 tx_hdr_size += ISCSI_CRC_LEN;
1168
1169 iov.iov_base = cmd->pdu;
1170 iov.iov_len = tx_hdr_size;
1171
1172 tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1173 if (tx_hdr_size != tx_sent) {
1174 if (tx_sent == -EAGAIN) {
1175 pr_err("tx_data() returned -EAGAIN\n");
1176 goto send_hdr;
1177 }
1178 return -1;
1179 }
1180
1181 data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001182 /*
1183 * Set iov_off used by padding and data digest tx_data() calls below
1184 * in order to determine proper offset into cmd->iov_data[]
1185 */
1186 if (conn->conn_ops->DataDigest) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001187 data_len -= ISCSI_CRC_LEN;
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001188 if (cmd->padding)
1189 iov_off = (cmd->iov_data_count - 2);
1190 else
1191 iov_off = (cmd->iov_data_count - 1);
1192 } else {
1193 iov_off = (cmd->iov_data_count - 1);
1194 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001195 /*
1196 * Perform sendpage() for each page in the scatterlist
1197 */
1198 while (data_len) {
1199 u32 space = (sg->length - offset);
1200 u32 sub_len = min_t(u32, data_len, space);
1201send_pg:
1202 tx_sent = conn->sock->ops->sendpage(conn->sock,
1203 sg_page(sg), sg->offset + offset, sub_len, 0);
1204 if (tx_sent != sub_len) {
1205 if (tx_sent == -EAGAIN) {
1206 pr_err("tcp_sendpage() returned"
1207 " -EAGAIN\n");
1208 goto send_pg;
1209 }
1210
1211 pr_err("tcp_sendpage() failure: %d\n",
1212 tx_sent);
1213 return -1;
1214 }
1215
1216 data_len -= sub_len;
1217 offset = 0;
1218 sg = sg_next(sg);
1219 }
1220
1221send_padding:
1222 if (cmd->padding) {
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001223 struct kvec *iov_p = &cmd->iov_data[iov_off++];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001224
1225 tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1226 if (cmd->padding != tx_sent) {
1227 if (tx_sent == -EAGAIN) {
1228 pr_err("tx_data() returned -EAGAIN\n");
1229 goto send_padding;
1230 }
1231 return -1;
1232 }
1233 }
1234
1235send_datacrc:
1236 if (conn->conn_ops->DataDigest) {
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001237 struct kvec *iov_d = &cmd->iov_data[iov_off];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001238
1239 tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1240 if (ISCSI_CRC_LEN != tx_sent) {
1241 if (tx_sent == -EAGAIN) {
1242 pr_err("tx_data() returned -EAGAIN\n");
1243 goto send_datacrc;
1244 }
1245 return -1;
1246 }
1247 }
1248
1249 return 0;
1250}
1251
1252/*
1253 * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1254 * back to the Initiator when an expection condition occurs with the
1255 * errors set in status_class and status_detail.
1256 *
1257 * Parameters: iSCSI Connection, Status Class, Status Detail.
1258 * Returns: 0 on success, -1 on error.
1259 */
1260int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1261{
1262 u8 iscsi_hdr[ISCSI_HDR_LEN];
1263 int err;
1264 struct kvec iov;
1265 struct iscsi_login_rsp *hdr;
1266
1267 iscsit_collect_login_stats(conn, status_class, status_detail);
1268
1269 memset(&iov, 0, sizeof(struct kvec));
1270 memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
1271
1272 hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
1273 hdr->opcode = ISCSI_OP_LOGIN_RSP;
1274 hdr->status_class = status_class;
1275 hdr->status_detail = status_detail;
1276 hdr->itt = cpu_to_be32(conn->login_itt);
1277
1278 iov.iov_base = &iscsi_hdr;
1279 iov.iov_len = ISCSI_HDR_LEN;
1280
1281 PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
1282
1283 err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
1284 if (err != ISCSI_HDR_LEN) {
1285 pr_err("tx_data returned less than expected\n");
1286 return -1;
1287 }
1288
1289 return 0;
1290}
1291
1292void iscsit_print_session_params(struct iscsi_session *sess)
1293{
1294 struct iscsi_conn *conn;
1295
1296 pr_debug("-----------------------------[Session Params for"
1297 " SID: %u]-----------------------------\n", sess->sid);
1298 spin_lock_bh(&sess->conn_lock);
1299 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1300 iscsi_dump_conn_ops(conn->conn_ops);
1301 spin_unlock_bh(&sess->conn_lock);
1302
1303 iscsi_dump_sess_ops(sess->sess_ops);
1304}
1305
1306static int iscsit_do_rx_data(
1307 struct iscsi_conn *conn,
1308 struct iscsi_data_count *count)
1309{
1310 int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001311 struct kvec *iov_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312 struct msghdr msg;
1313
1314 if (!conn || !conn->sock || !conn->conn_ops)
1315 return -1;
1316
1317 memset(&msg, 0, sizeof(struct msghdr));
1318
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001319 iov_p = count->iov;
1320 iov_len = count->iov_count;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001321
1322 while (total_rx < data) {
1323 rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
1324 (data - total_rx), MSG_WAITALL);
1325 if (rx_loop <= 0) {
1326 pr_debug("rx_loop: %d total_rx: %d\n",
1327 rx_loop, total_rx);
1328 return rx_loop;
1329 }
1330 total_rx += rx_loop;
1331 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1332 rx_loop, total_rx, data);
1333 }
1334
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001335 return total_rx;
1336}
1337
1338static int iscsit_do_tx_data(
1339 struct iscsi_conn *conn,
1340 struct iscsi_data_count *count)
1341{
1342 int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001343 struct kvec *iov_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001344 struct msghdr msg;
1345
1346 if (!conn || !conn->sock || !conn->conn_ops)
1347 return -1;
1348
1349 if (data <= 0) {
1350 pr_err("Data length is: %d\n", data);
1351 return -1;
1352 }
1353
1354 memset(&msg, 0, sizeof(struct msghdr));
1355
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001356 iov_p = count->iov;
1357 iov_len = count->iov_count;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001358
1359 while (total_tx < data) {
1360 tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
1361 (data - total_tx));
1362 if (tx_loop <= 0) {
1363 pr_debug("tx_loop: %d total_tx %d\n",
1364 tx_loop, total_tx);
1365 return tx_loop;
1366 }
1367 total_tx += tx_loop;
1368 pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
1369 tx_loop, total_tx, data);
1370 }
1371
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001372 return total_tx;
1373}
1374
1375int rx_data(
1376 struct iscsi_conn *conn,
1377 struct kvec *iov,
1378 int iov_count,
1379 int data)
1380{
1381 struct iscsi_data_count c;
1382
1383 if (!conn || !conn->sock || !conn->conn_ops)
1384 return -1;
1385
1386 memset(&c, 0, sizeof(struct iscsi_data_count));
1387 c.iov = iov;
1388 c.iov_count = iov_count;
1389 c.data_length = data;
1390 c.type = ISCSI_RX_DATA;
1391
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001392 return iscsit_do_rx_data(conn, &c);
1393}
1394
1395int tx_data(
1396 struct iscsi_conn *conn,
1397 struct kvec *iov,
1398 int iov_count,
1399 int data)
1400{
1401 struct iscsi_data_count c;
1402
1403 if (!conn || !conn->sock || !conn->conn_ops)
1404 return -1;
1405
1406 memset(&c, 0, sizeof(struct iscsi_data_count));
1407 c.iov = iov;
1408 c.iov_count = iov_count;
1409 c.data_length = data;
1410 c.type = ISCSI_TX_DATA;
1411
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001412 return iscsit_do_tx_data(conn, &c);
1413}
1414
1415void iscsit_collect_login_stats(
1416 struct iscsi_conn *conn,
1417 u8 status_class,
1418 u8 status_detail)
1419{
1420 struct iscsi_param *intrname = NULL;
1421 struct iscsi_tiqn *tiqn;
1422 struct iscsi_login_stats *ls;
1423
1424 tiqn = iscsit_snmp_get_tiqn(conn);
1425 if (!tiqn)
1426 return;
1427
1428 ls = &tiqn->login_stats;
1429
1430 spin_lock(&ls->lock);
1431 if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
1432 ((get_jiffies_64() - ls->last_fail_time) < 10)) {
1433 /* We already have the failure info for this login */
1434 spin_unlock(&ls->lock);
1435 return;
1436 }
1437
1438 if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1439 ls->accepts++;
1440 else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1441 ls->redirects++;
1442 ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1443 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1444 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1445 ls->authenticate_fails++;
1446 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
1447 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1448 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1449 ls->authorize_fails++;
1450 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1451 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1452 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1453 ls->negotiate_fails++;
1454 ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1455 } else {
1456 ls->other_fails++;
1457 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1458 }
1459
1460 /* Save initiator name, ip address and time, if it is a failed login */
1461 if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1462 if (conn->param_list)
1463 intrname = iscsi_find_param_from_key(INITIATORNAME,
1464 conn->param_list);
1465 strcpy(ls->last_intr_fail_name,
1466 (intrname ? intrname->value : "Unknown"));
1467
1468 ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
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
1477struct 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}