blob: fe712d6cc478a73c994ec6b7b4720e35adc8c3da [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>
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080027#include <target/iscsi/iscsi_transport.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000028
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
58extern struct list_head g_tiqn_list;
59extern spinlock_t tiqn_lock;
60
61/*
62 * Called with cmd->r2t_lock held.
63 */
64int 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
93struct 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
115struct 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 */
136void 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
142void 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
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800152struct iscsi_cmd *iscsit_alloc_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
153{
154 struct iscsi_cmd *cmd;
155
156 cmd = kmem_cache_zalloc(lio_cmd_cache, gfp_mask);
157 if (!cmd)
158 return NULL;
159
160 cmd->release_cmd = &iscsit_release_cmd;
161 return cmd;
162}
163
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000164/*
165 * May be called from software interrupt (timer) context for allocating
166 * iSCSI NopINs.
167 */
168struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
169{
170 struct iscsi_cmd *cmd;
171
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800172 cmd = conn->conn_transport->iscsit_alloc_cmd(conn, gfp_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000173 if (!cmd) {
174 pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
175 return NULL;
176 }
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800177 cmd->conn = conn;
Andy Grover2fbb4712012-04-03 15:51:01 -0700178 INIT_LIST_HEAD(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000179 INIT_LIST_HEAD(&cmd->datain_list);
180 INIT_LIST_HEAD(&cmd->cmd_r2t_list);
181 init_completion(&cmd->reject_comp);
182 spin_lock_init(&cmd->datain_lock);
183 spin_lock_init(&cmd->dataout_timeout_lock);
184 spin_lock_init(&cmd->istate_lock);
185 spin_lock_init(&cmd->error_lock);
186 spin_lock_init(&cmd->r2t_lock);
187
188 return cmd;
189}
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800190EXPORT_SYMBOL(iscsit_allocate_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000191
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000192struct iscsi_seq *iscsit_get_seq_holder_for_datain(
193 struct iscsi_cmd *cmd,
194 u32 seq_send_order)
195{
196 u32 i;
197
198 for (i = 0; i < cmd->seq_count; i++)
199 if (cmd->seq_list[i].seq_send_order == seq_send_order)
200 return &cmd->seq_list[i];
201
202 return NULL;
203}
204
205struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
206{
207 u32 i;
208
209 if (!cmd->seq_list) {
210 pr_err("struct iscsi_cmd->seq_list is NULL!\n");
211 return NULL;
212 }
213
214 for (i = 0; i < cmd->seq_count; i++) {
215 if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
216 continue;
217 if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
218 cmd->seq_send_order++;
219 return &cmd->seq_list[i];
220 }
221 }
222
223 return NULL;
224}
225
226struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
227 struct iscsi_cmd *cmd,
228 u32 r2t_sn)
229{
230 struct iscsi_r2t *r2t;
231
232 spin_lock_bh(&cmd->r2t_lock);
233 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
234 if (r2t->r2t_sn == r2t_sn) {
235 spin_unlock_bh(&cmd->r2t_lock);
236 return r2t;
237 }
238 }
239 spin_unlock_bh(&cmd->r2t_lock);
240
241 return NULL;
242}
243
244static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
245{
246 int ret;
247
248 /*
249 * This is the proper method of checking received CmdSN against
250 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
251 * or order CmdSNs due to multiple connection sessions and/or
252 * CRC failures.
253 */
254 if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
255 pr_err("Received CmdSN: 0x%08x is greater than"
256 " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
257 sess->max_cmd_sn);
258 ret = CMDSN_ERROR_CANNOT_RECOVER;
259
260 } else if (cmdsn == sess->exp_cmd_sn) {
261 sess->exp_cmd_sn++;
262 pr_debug("Received CmdSN matches ExpCmdSN,"
263 " incremented ExpCmdSN to: 0x%08x\n",
264 sess->exp_cmd_sn);
265 ret = CMDSN_NORMAL_OPERATION;
266
267 } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
268 pr_debug("Received CmdSN: 0x%08x is greater"
269 " than ExpCmdSN: 0x%08x, not acknowledging.\n",
270 cmdsn, sess->exp_cmd_sn);
271 ret = CMDSN_HIGHER_THAN_EXP;
272
273 } else {
274 pr_err("Received CmdSN: 0x%08x is less than"
275 " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
276 sess->exp_cmd_sn);
277 ret = CMDSN_LOWER_THAN_EXP;
278 }
279
280 return ret;
281}
282
283/*
284 * Commands may be received out of order if MC/S is in use.
285 * Ensure they are executed in CmdSN order.
286 */
287int iscsit_sequence_cmd(
288 struct iscsi_conn *conn,
289 struct iscsi_cmd *cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400290 __be32 cmdsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000291{
292 int ret;
293 int cmdsn_ret;
294
295 mutex_lock(&conn->sess->cmdsn_mutex);
296
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400297 cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000298 switch (cmdsn_ret) {
299 case CMDSN_NORMAL_OPERATION:
300 ret = iscsit_execute_cmd(cmd, 0);
301 if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
302 iscsit_execute_ooo_cmdsns(conn->sess);
303 break;
304 case CMDSN_HIGHER_THAN_EXP:
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400305 ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000306 break;
307 case CMDSN_LOWER_THAN_EXP:
308 cmd->i_state = ISTATE_REMOVE;
309 iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
310 ret = cmdsn_ret;
311 break;
312 default:
313 ret = cmdsn_ret;
314 break;
315 }
316 mutex_unlock(&conn->sess->cmdsn_mutex);
317
318 return ret;
319}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800320EXPORT_SYMBOL(iscsit_sequence_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000321
322int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
323{
324 struct iscsi_conn *conn = cmd->conn;
325 struct se_cmd *se_cmd = &cmd->se_cmd;
326 struct iscsi_data *hdr = (struct iscsi_data *) buf;
327 u32 payload_length = ntoh24(hdr->dlength);
328
329 if (conn->sess->sess_ops->InitialR2T) {
330 pr_err("Received unexpected unsolicited data"
331 " while InitialR2T=Yes, protocol error.\n");
332 transport_send_check_condition_and_sense(se_cmd,
333 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
334 return -1;
335 }
336
337 if ((cmd->first_burst_len + payload_length) >
338 conn->sess->sess_ops->FirstBurstLength) {
339 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
340 " for this Unsolicited DataOut Burst.\n",
341 (cmd->first_burst_len + payload_length),
342 conn->sess->sess_ops->FirstBurstLength);
343 transport_send_check_condition_and_sense(se_cmd,
344 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
345 return -1;
346 }
347
348 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
349 return 0;
350
Andy Groverebf1d952012-04-03 15:51:24 -0700351 if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000352 ((cmd->first_burst_len + payload_length) !=
353 conn->sess->sess_ops->FirstBurstLength)) {
354 pr_err("Unsolicited non-immediate data received %u"
355 " does not equal FirstBurstLength: %u, and does"
356 " not equal ExpXferLen %u.\n",
357 (cmd->first_burst_len + payload_length),
Andy Groverebf1d952012-04-03 15:51:24 -0700358 conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000359 transport_send_check_condition_and_sense(se_cmd,
360 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
361 return -1;
362 }
363 return 0;
364}
365
366struct iscsi_cmd *iscsit_find_cmd_from_itt(
367 struct iscsi_conn *conn,
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400368 itt_t init_task_tag)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000369{
370 struct iscsi_cmd *cmd;
371
372 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700373 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000374 if (cmd->init_task_tag == init_task_tag) {
375 spin_unlock_bh(&conn->cmd_lock);
376 return cmd;
377 }
378 }
379 spin_unlock_bh(&conn->cmd_lock);
380
381 pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
382 init_task_tag, conn->cid);
383 return NULL;
384}
385
386struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
387 struct iscsi_conn *conn,
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400388 itt_t init_task_tag,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000389 u32 length)
390{
391 struct iscsi_cmd *cmd;
392
393 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700394 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000395 if (cmd->init_task_tag == init_task_tag) {
396 spin_unlock_bh(&conn->cmd_lock);
397 return cmd;
398 }
399 }
400 spin_unlock_bh(&conn->cmd_lock);
401
402 pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
403 " dumping payload\n", init_task_tag, conn->cid);
404 if (length)
405 iscsit_dump_data_payload(conn, length, 1);
406
407 return NULL;
408}
409
410struct iscsi_cmd *iscsit_find_cmd_from_ttt(
411 struct iscsi_conn *conn,
412 u32 targ_xfer_tag)
413{
414 struct iscsi_cmd *cmd = NULL;
415
416 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700417 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000418 if (cmd->targ_xfer_tag == targ_xfer_tag) {
419 spin_unlock_bh(&conn->cmd_lock);
420 return cmd;
421 }
422 }
423 spin_unlock_bh(&conn->cmd_lock);
424
425 pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
426 targ_xfer_tag, conn->cid);
427 return NULL;
428}
429
430int iscsit_find_cmd_for_recovery(
431 struct iscsi_session *sess,
432 struct iscsi_cmd **cmd_ptr,
433 struct iscsi_conn_recovery **cr_ptr,
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400434 itt_t init_task_tag)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000435{
436 struct iscsi_cmd *cmd = NULL;
437 struct iscsi_conn_recovery *cr;
438 /*
439 * Scan through the inactive connection recovery list's command list.
440 * If init_task_tag matches the command is still alligent.
441 */
442 spin_lock(&sess->cr_i_lock);
443 list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
444 spin_lock(&cr->conn_recovery_cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700445 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000446 if (cmd->init_task_tag == init_task_tag) {
447 spin_unlock(&cr->conn_recovery_cmd_lock);
448 spin_unlock(&sess->cr_i_lock);
449
450 *cr_ptr = cr;
451 *cmd_ptr = cmd;
452 return -2;
453 }
454 }
455 spin_unlock(&cr->conn_recovery_cmd_lock);
456 }
457 spin_unlock(&sess->cr_i_lock);
458 /*
459 * Scan through the active connection recovery list's command list.
460 * If init_task_tag matches the command is ready to be reassigned.
461 */
462 spin_lock(&sess->cr_a_lock);
463 list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
464 spin_lock(&cr->conn_recovery_cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700465 list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000466 if (cmd->init_task_tag == init_task_tag) {
467 spin_unlock(&cr->conn_recovery_cmd_lock);
468 spin_unlock(&sess->cr_a_lock);
469
470 *cr_ptr = cr;
471 *cmd_ptr = cmd;
472 return 0;
473 }
474 }
475 spin_unlock(&cr->conn_recovery_cmd_lock);
476 }
477 spin_unlock(&sess->cr_a_lock);
478
479 return -1;
480}
481
482void iscsit_add_cmd_to_immediate_queue(
483 struct iscsi_cmd *cmd,
484 struct iscsi_conn *conn,
485 u8 state)
486{
487 struct iscsi_queue_req *qr;
488
489 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
490 if (!qr) {
491 pr_err("Unable to allocate memory for"
492 " struct iscsi_queue_req\n");
493 return;
494 }
495 INIT_LIST_HEAD(&qr->qr_list);
496 qr->cmd = cmd;
497 qr->state = state;
498
499 spin_lock_bh(&conn->immed_queue_lock);
500 list_add_tail(&qr->qr_list, &conn->immed_queue_list);
501 atomic_inc(&cmd->immed_queue_count);
502 atomic_set(&conn->check_immediate_queue, 1);
503 spin_unlock_bh(&conn->immed_queue_lock);
504
Roland Dreierd5627ac2012-10-31 09:16:46 -0700505 wake_up(&conn->queues_wq);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000506}
507
508struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
509{
510 struct iscsi_queue_req *qr;
511
512 spin_lock_bh(&conn->immed_queue_lock);
513 if (list_empty(&conn->immed_queue_list)) {
514 spin_unlock_bh(&conn->immed_queue_lock);
515 return NULL;
516 }
Roland Dreier1f981de2012-10-31 09:16:47 -0700517 qr = list_first_entry(&conn->immed_queue_list,
518 struct iscsi_queue_req, qr_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000519
520 list_del(&qr->qr_list);
521 if (qr->cmd)
522 atomic_dec(&qr->cmd->immed_queue_count);
523 spin_unlock_bh(&conn->immed_queue_lock);
524
525 return qr;
526}
527
528static void iscsit_remove_cmd_from_immediate_queue(
529 struct iscsi_cmd *cmd,
530 struct iscsi_conn *conn)
531{
532 struct iscsi_queue_req *qr, *qr_tmp;
533
534 spin_lock_bh(&conn->immed_queue_lock);
535 if (!atomic_read(&cmd->immed_queue_count)) {
536 spin_unlock_bh(&conn->immed_queue_lock);
537 return;
538 }
539
540 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
541 if (qr->cmd != cmd)
542 continue;
543
544 atomic_dec(&qr->cmd->immed_queue_count);
545 list_del(&qr->qr_list);
546 kmem_cache_free(lio_qr_cache, qr);
547 }
548 spin_unlock_bh(&conn->immed_queue_lock);
549
550 if (atomic_read(&cmd->immed_queue_count)) {
551 pr_err("ITT: 0x%08x immed_queue_count: %d\n",
552 cmd->init_task_tag,
553 atomic_read(&cmd->immed_queue_count));
554 }
555}
556
557void iscsit_add_cmd_to_response_queue(
558 struct iscsi_cmd *cmd,
559 struct iscsi_conn *conn,
560 u8 state)
561{
562 struct iscsi_queue_req *qr;
563
564 qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
565 if (!qr) {
566 pr_err("Unable to allocate memory for"
567 " struct iscsi_queue_req\n");
568 return;
569 }
570 INIT_LIST_HEAD(&qr->qr_list);
571 qr->cmd = cmd;
572 qr->state = state;
573
574 spin_lock_bh(&conn->response_queue_lock);
575 list_add_tail(&qr->qr_list, &conn->response_queue_list);
576 atomic_inc(&cmd->response_queue_count);
577 spin_unlock_bh(&conn->response_queue_lock);
578
Roland Dreierd5627ac2012-10-31 09:16:46 -0700579 wake_up(&conn->queues_wq);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000580}
581
582struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
583{
584 struct iscsi_queue_req *qr;
585
586 spin_lock_bh(&conn->response_queue_lock);
587 if (list_empty(&conn->response_queue_list)) {
588 spin_unlock_bh(&conn->response_queue_lock);
589 return NULL;
590 }
591
Roland Dreier1f981de2012-10-31 09:16:47 -0700592 qr = list_first_entry(&conn->response_queue_list,
593 struct iscsi_queue_req, qr_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000594
595 list_del(&qr->qr_list);
596 if (qr->cmd)
597 atomic_dec(&qr->cmd->response_queue_count);
598 spin_unlock_bh(&conn->response_queue_lock);
599
600 return qr;
601}
602
603static void iscsit_remove_cmd_from_response_queue(
604 struct iscsi_cmd *cmd,
605 struct iscsi_conn *conn)
606{
607 struct iscsi_queue_req *qr, *qr_tmp;
608
609 spin_lock_bh(&conn->response_queue_lock);
610 if (!atomic_read(&cmd->response_queue_count)) {
611 spin_unlock_bh(&conn->response_queue_lock);
612 return;
613 }
614
615 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
616 qr_list) {
617 if (qr->cmd != cmd)
618 continue;
619
620 atomic_dec(&qr->cmd->response_queue_count);
621 list_del(&qr->qr_list);
622 kmem_cache_free(lio_qr_cache, qr);
623 }
624 spin_unlock_bh(&conn->response_queue_lock);
625
626 if (atomic_read(&cmd->response_queue_count)) {
627 pr_err("ITT: 0x%08x response_queue_count: %d\n",
628 cmd->init_task_tag,
629 atomic_read(&cmd->response_queue_count));
630 }
631}
632
Roland Dreierd5627ac2012-10-31 09:16:46 -0700633bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
634{
635 bool empty;
636
637 spin_lock_bh(&conn->immed_queue_lock);
638 empty = list_empty(&conn->immed_queue_list);
639 spin_unlock_bh(&conn->immed_queue_lock);
640
641 if (!empty)
642 return empty;
643
644 spin_lock_bh(&conn->response_queue_lock);
645 empty = list_empty(&conn->response_queue_list);
646 spin_unlock_bh(&conn->response_queue_lock);
647
648 return empty;
649}
650
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000651void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
652{
653 struct iscsi_queue_req *qr, *qr_tmp;
654
655 spin_lock_bh(&conn->immed_queue_lock);
656 list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
657 list_del(&qr->qr_list);
658 if (qr->cmd)
659 atomic_dec(&qr->cmd->immed_queue_count);
660
661 kmem_cache_free(lio_qr_cache, qr);
662 }
663 spin_unlock_bh(&conn->immed_queue_lock);
664
665 spin_lock_bh(&conn->response_queue_lock);
666 list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
667 qr_list) {
668 list_del(&qr->qr_list);
669 if (qr->cmd)
670 atomic_dec(&qr->cmd->response_queue_count);
671
672 kmem_cache_free(lio_qr_cache, qr);
673 }
674 spin_unlock_bh(&conn->response_queue_lock);
675}
676
677void iscsit_release_cmd(struct iscsi_cmd *cmd)
678{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000679 kfree(cmd->buf_ptr);
680 kfree(cmd->pdu_list);
681 kfree(cmd->seq_list);
682 kfree(cmd->tmr_req);
683 kfree(cmd->iov_data);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -0700684 kfree(cmd->text_in_ptr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000685
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000686 kmem_cache_free(lio_cmd_cache, cmd);
687}
688
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700689static void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool scsi_cmd,
690 bool check_queues)
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700691{
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700692 struct iscsi_conn *conn = cmd->conn;
693
694 if (scsi_cmd) {
695 if (cmd->data_direction == DMA_TO_DEVICE) {
696 iscsit_stop_dataout_timer(cmd);
697 iscsit_free_r2ts_from_list(cmd);
698 }
699 if (cmd->data_direction == DMA_FROM_DEVICE)
700 iscsit_free_all_datain_reqs(cmd);
701 }
702
703 if (conn && check_queues) {
704 iscsit_remove_cmd_from_immediate_queue(cmd, conn);
705 iscsit_remove_cmd_from_response_queue(cmd, conn);
706 }
707}
708
709void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
710{
711 struct se_cmd *se_cmd = NULL;
712 int rc;
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700713 /*
Masanari Iida20879692012-08-27 22:46:00 +0900714 * Determine if a struct se_cmd is associated with
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700715 * this struct iscsi_cmd.
716 */
717 switch (cmd->iscsi_opcode) {
718 case ISCSI_OP_SCSI_CMD:
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700719 se_cmd = &cmd->se_cmd;
720 __iscsit_free_cmd(cmd, true, shutdown);
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800721 /*
722 * Fallthrough
723 */
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700724 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700725 rc = transport_generic_free_cmd(&cmd->se_cmd, 1);
726 if (!rc && shutdown && se_cmd && se_cmd->se_sess) {
727 __iscsit_free_cmd(cmd, true, shutdown);
728 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
729 }
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700730 break;
Nicholas Bellingerc1ce4bd2012-01-16 16:04:15 -0800731 case ISCSI_OP_REJECT:
732 /*
733 * Handle special case for REJECT when iscsi_add_reject*() has
734 * overwritten the original iscsi_opcode assignment, and the
735 * associated cmd->se_cmd needs to be released.
736 */
737 if (cmd->se_cmd.se_tfo != NULL) {
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700738 se_cmd = &cmd->se_cmd;
739 __iscsit_free_cmd(cmd, true, shutdown);
740
741 rc = transport_generic_free_cmd(&cmd->se_cmd, 1);
742 if (!rc && shutdown && se_cmd->se_sess) {
743 __iscsit_free_cmd(cmd, true, shutdown);
744 target_put_sess_cmd(se_cmd->se_sess, se_cmd);
745 }
Nicholas Bellingerc1ce4bd2012-01-16 16:04:15 -0800746 break;
747 }
748 /* Fall-through */
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700749 default:
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700750 __iscsit_free_cmd(cmd, false, shutdown);
Nicholas Bellingercdb72662013-03-06 22:09:17 -0800751 cmd->release_cmd(cmd);
Nicholas Bellingerd2701902011-10-09 01:48:14 -0700752 break;
753 }
754}
755
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000756int iscsit_check_session_usage_count(struct iscsi_session *sess)
757{
758 spin_lock_bh(&sess->session_usage_lock);
759 if (sess->session_usage_count != 0) {
760 sess->session_waiting_on_uc = 1;
761 spin_unlock_bh(&sess->session_usage_lock);
762 if (in_interrupt())
763 return 2;
764
765 wait_for_completion(&sess->session_waiting_on_uc_comp);
766 return 1;
767 }
768 spin_unlock_bh(&sess->session_usage_lock);
769
770 return 0;
771}
772
773void iscsit_dec_session_usage_count(struct iscsi_session *sess)
774{
775 spin_lock_bh(&sess->session_usage_lock);
776 sess->session_usage_count--;
777
778 if (!sess->session_usage_count && sess->session_waiting_on_uc)
779 complete(&sess->session_waiting_on_uc_comp);
780
781 spin_unlock_bh(&sess->session_usage_lock);
782}
783
784void iscsit_inc_session_usage_count(struct iscsi_session *sess)
785{
786 spin_lock_bh(&sess->session_usage_lock);
787 sess->session_usage_count++;
788 spin_unlock_bh(&sess->session_usage_lock);
789}
790
791/*
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000792 * Setup conn->if_marker and conn->of_marker values based upon
793 * the initial marker-less interval. (see iSCSI v19 A.2)
794 */
795int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
796{
797 int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
798 /*
799 * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
800 */
801 u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
802 u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
803
804 if (conn->conn_ops->OFMarker) {
805 /*
806 * Account for the first Login Command received not
807 * via iscsi_recv_msg().
808 */
809 conn->of_marker += ISCSI_HDR_LEN;
810 if (conn->of_marker <= OFMarkInt) {
811 conn->of_marker = (OFMarkInt - conn->of_marker);
812 } else {
813 login_ofmarker_count = (conn->of_marker / OFMarkInt);
814 next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
815 (login_ofmarker_count * MARKER_SIZE);
816 conn->of_marker = (next_marker - conn->of_marker);
817 }
818 conn->of_marker_offset = 0;
819 pr_debug("Setting OFMarker value to %u based on Initial"
820 " Markerless Interval.\n", conn->of_marker);
821 }
822
823 if (conn->conn_ops->IFMarker) {
824 if (conn->if_marker <= IFMarkInt) {
825 conn->if_marker = (IFMarkInt - conn->if_marker);
826 } else {
827 login_ifmarker_count = (conn->if_marker / IFMarkInt);
828 next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
829 (login_ifmarker_count * MARKER_SIZE);
830 conn->if_marker = (next_marker - conn->if_marker);
831 }
832 pr_debug("Setting IFMarker value to %u based on Initial"
833 " Markerless Interval.\n", conn->if_marker);
834 }
835
836 return 0;
837}
838
839struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
840{
841 struct iscsi_conn *conn;
842
843 spin_lock_bh(&sess->conn_lock);
844 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
845 if ((conn->cid == cid) &&
846 (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
847 iscsit_inc_conn_usage_count(conn);
848 spin_unlock_bh(&sess->conn_lock);
849 return conn;
850 }
851 }
852 spin_unlock_bh(&sess->conn_lock);
853
854 return NULL;
855}
856
857struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
858{
859 struct iscsi_conn *conn;
860
861 spin_lock_bh(&sess->conn_lock);
862 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
863 if (conn->cid == cid) {
864 iscsit_inc_conn_usage_count(conn);
865 spin_lock(&conn->state_lock);
866 atomic_set(&conn->connection_wait_rcfr, 1);
867 spin_unlock(&conn->state_lock);
868 spin_unlock_bh(&sess->conn_lock);
869 return conn;
870 }
871 }
872 spin_unlock_bh(&sess->conn_lock);
873
874 return NULL;
875}
876
877void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
878{
879 spin_lock_bh(&conn->conn_usage_lock);
880 if (conn->conn_usage_count != 0) {
881 conn->conn_waiting_on_uc = 1;
882 spin_unlock_bh(&conn->conn_usage_lock);
883
884 wait_for_completion(&conn->conn_waiting_on_uc_comp);
885 return;
886 }
887 spin_unlock_bh(&conn->conn_usage_lock);
888}
889
890void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
891{
892 spin_lock_bh(&conn->conn_usage_lock);
893 conn->conn_usage_count--;
894
895 if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
896 complete(&conn->conn_waiting_on_uc_comp);
897
898 spin_unlock_bh(&conn->conn_usage_lock);
899}
900
901void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
902{
903 spin_lock_bh(&conn->conn_usage_lock);
904 conn->conn_usage_count++;
905 spin_unlock_bh(&conn->conn_usage_lock);
906}
907
908static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
909{
910 u8 state;
911 struct iscsi_cmd *cmd;
912
913 cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
914 if (!cmd)
915 return -1;
916
917 cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
918 state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
919 ISTATE_SEND_NOPIN_NO_RESPONSE;
Christoph Hellwig66c7db62012-09-26 08:00:39 -0400920 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000921 spin_lock_bh(&conn->sess->ttt_lock);
922 cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
923 0xFFFFFFFF;
924 if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
925 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
926 spin_unlock_bh(&conn->sess->ttt_lock);
927
928 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700929 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000930 spin_unlock_bh(&conn->cmd_lock);
931
932 if (want_response)
933 iscsit_start_nopin_response_timer(conn);
934 iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
935
936 return 0;
937}
938
939static void iscsit_handle_nopin_response_timeout(unsigned long data)
940{
941 struct iscsi_conn *conn = (struct iscsi_conn *) data;
942
943 iscsit_inc_conn_usage_count(conn);
944
945 spin_lock_bh(&conn->nopin_timer_lock);
946 if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
947 spin_unlock_bh(&conn->nopin_timer_lock);
948 iscsit_dec_conn_usage_count(conn);
949 return;
950 }
951
952 pr_debug("Did not receive response to NOPIN on CID: %hu on"
953 " SID: %u, failing connection.\n", conn->cid,
954 conn->sess->sid);
955 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
956 spin_unlock_bh(&conn->nopin_timer_lock);
957
958 {
959 struct iscsi_portal_group *tpg = conn->sess->tpg;
960 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
961
962 if (tiqn) {
963 spin_lock_bh(&tiqn->sess_err_stats.lock);
964 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
Jörn Engel8359cf42011-11-24 02:05:51 +0100965 conn->sess->sess_ops->InitiatorName);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000966 tiqn->sess_err_stats.last_sess_failure_type =
967 ISCSI_SESS_ERR_CXN_TIMEOUT;
968 tiqn->sess_err_stats.cxn_timeout_errors++;
969 conn->sess->conn_timeout_errors++;
970 spin_unlock_bh(&tiqn->sess_err_stats.lock);
971 }
972 }
973
974 iscsit_cause_connection_reinstatement(conn, 0);
975 iscsit_dec_conn_usage_count(conn);
976}
977
978void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
979{
980 struct iscsi_session *sess = conn->sess;
981 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
982
983 spin_lock_bh(&conn->nopin_timer_lock);
984 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
985 spin_unlock_bh(&conn->nopin_timer_lock);
986 return;
987 }
988
989 mod_timer(&conn->nopin_response_timer,
990 (get_jiffies_64() + na->nopin_response_timeout * HZ));
991 spin_unlock_bh(&conn->nopin_timer_lock);
992}
993
994/*
995 * Called with conn->nopin_timer_lock held.
996 */
997void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
998{
999 struct iscsi_session *sess = conn->sess;
1000 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1001
1002 spin_lock_bh(&conn->nopin_timer_lock);
1003 if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
1004 spin_unlock_bh(&conn->nopin_timer_lock);
1005 return;
1006 }
1007
1008 init_timer(&conn->nopin_response_timer);
1009 conn->nopin_response_timer.expires =
1010 (get_jiffies_64() + na->nopin_response_timeout * HZ);
1011 conn->nopin_response_timer.data = (unsigned long)conn;
1012 conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
1013 conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
1014 conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
1015 add_timer(&conn->nopin_response_timer);
1016
1017 pr_debug("Started NOPIN Response Timer on CID: %d to %u"
1018 " seconds\n", conn->cid, na->nopin_response_timeout);
1019 spin_unlock_bh(&conn->nopin_timer_lock);
1020}
1021
1022void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
1023{
1024 spin_lock_bh(&conn->nopin_timer_lock);
1025 if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
1026 spin_unlock_bh(&conn->nopin_timer_lock);
1027 return;
1028 }
1029 conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
1030 spin_unlock_bh(&conn->nopin_timer_lock);
1031
1032 del_timer_sync(&conn->nopin_response_timer);
1033
1034 spin_lock_bh(&conn->nopin_timer_lock);
1035 conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1036 spin_unlock_bh(&conn->nopin_timer_lock);
1037}
1038
1039static void iscsit_handle_nopin_timeout(unsigned long data)
1040{
1041 struct iscsi_conn *conn = (struct iscsi_conn *) data;
1042
1043 iscsit_inc_conn_usage_count(conn);
1044
1045 spin_lock_bh(&conn->nopin_timer_lock);
1046 if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1047 spin_unlock_bh(&conn->nopin_timer_lock);
1048 iscsit_dec_conn_usage_count(conn);
1049 return;
1050 }
1051 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1052 spin_unlock_bh(&conn->nopin_timer_lock);
1053
1054 iscsit_add_nopin(conn, 1);
1055 iscsit_dec_conn_usage_count(conn);
1056}
1057
1058/*
1059 * Called with conn->nopin_timer_lock held.
1060 */
1061void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1062{
1063 struct iscsi_session *sess = conn->sess;
1064 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1065 /*
1066 * NOPIN timeout is disabled.
1067 */
1068 if (!na->nopin_timeout)
1069 return;
1070
1071 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1072 return;
1073
1074 init_timer(&conn->nopin_timer);
1075 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1076 conn->nopin_timer.data = (unsigned long)conn;
1077 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1078 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1079 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1080 add_timer(&conn->nopin_timer);
1081
1082 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1083 " interval\n", conn->cid, na->nopin_timeout);
1084}
1085
1086void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1087{
1088 struct iscsi_session *sess = conn->sess;
1089 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1090 /*
1091 * NOPIN timeout is disabled..
1092 */
1093 if (!na->nopin_timeout)
1094 return;
1095
1096 spin_lock_bh(&conn->nopin_timer_lock);
1097 if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
1098 spin_unlock_bh(&conn->nopin_timer_lock);
1099 return;
1100 }
1101
1102 init_timer(&conn->nopin_timer);
1103 conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1104 conn->nopin_timer.data = (unsigned long)conn;
1105 conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1106 conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1107 conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1108 add_timer(&conn->nopin_timer);
1109
1110 pr_debug("Started NOPIN Timer on CID: %d at %u second"
1111 " interval\n", conn->cid, na->nopin_timeout);
1112 spin_unlock_bh(&conn->nopin_timer_lock);
1113}
1114
1115void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1116{
1117 spin_lock_bh(&conn->nopin_timer_lock);
1118 if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1119 spin_unlock_bh(&conn->nopin_timer_lock);
1120 return;
1121 }
1122 conn->nopin_timer_flags |= ISCSI_TF_STOP;
1123 spin_unlock_bh(&conn->nopin_timer_lock);
1124
1125 del_timer_sync(&conn->nopin_timer);
1126
1127 spin_lock_bh(&conn->nopin_timer_lock);
1128 conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1129 spin_unlock_bh(&conn->nopin_timer_lock);
1130}
1131
1132int iscsit_send_tx_data(
1133 struct iscsi_cmd *cmd,
1134 struct iscsi_conn *conn,
1135 int use_misc)
1136{
1137 int tx_sent, tx_size;
1138 u32 iov_count;
1139 struct kvec *iov;
1140
1141send_data:
1142 tx_size = cmd->tx_size;
1143
1144 if (!use_misc) {
1145 iov = &cmd->iov_data[0];
1146 iov_count = cmd->iov_data_count;
1147 } else {
1148 iov = &cmd->iov_misc[0];
1149 iov_count = cmd->iov_misc_count;
1150 }
1151
1152 tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1153 if (tx_size != tx_sent) {
1154 if (tx_sent == -EAGAIN) {
1155 pr_err("tx_data() returned -EAGAIN\n");
1156 goto send_data;
1157 } else
1158 return -1;
1159 }
1160 cmd->tx_size = 0;
1161
1162 return 0;
1163}
1164
1165int iscsit_fe_sendpage_sg(
1166 struct iscsi_cmd *cmd,
1167 struct iscsi_conn *conn)
1168{
1169 struct scatterlist *sg = cmd->first_data_sg;
1170 struct kvec iov;
1171 u32 tx_hdr_size, data_len;
1172 u32 offset = cmd->first_data_sg_off;
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001173 int tx_sent, iov_off;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001174
1175send_hdr:
1176 tx_hdr_size = ISCSI_HDR_LEN;
1177 if (conn->conn_ops->HeaderDigest)
1178 tx_hdr_size += ISCSI_CRC_LEN;
1179
1180 iov.iov_base = cmd->pdu;
1181 iov.iov_len = tx_hdr_size;
1182
1183 tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1184 if (tx_hdr_size != tx_sent) {
1185 if (tx_sent == -EAGAIN) {
1186 pr_err("tx_data() returned -EAGAIN\n");
1187 goto send_hdr;
1188 }
1189 return -1;
1190 }
1191
1192 data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001193 /*
1194 * Set iov_off used by padding and data digest tx_data() calls below
1195 * in order to determine proper offset into cmd->iov_data[]
1196 */
1197 if (conn->conn_ops->DataDigest) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001198 data_len -= ISCSI_CRC_LEN;
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001199 if (cmd->padding)
1200 iov_off = (cmd->iov_data_count - 2);
1201 else
1202 iov_off = (cmd->iov_data_count - 1);
1203 } else {
1204 iov_off = (cmd->iov_data_count - 1);
1205 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001206 /*
1207 * Perform sendpage() for each page in the scatterlist
1208 */
1209 while (data_len) {
1210 u32 space = (sg->length - offset);
1211 u32 sub_len = min_t(u32, data_len, space);
1212send_pg:
1213 tx_sent = conn->sock->ops->sendpage(conn->sock,
1214 sg_page(sg), sg->offset + offset, sub_len, 0);
1215 if (tx_sent != sub_len) {
1216 if (tx_sent == -EAGAIN) {
1217 pr_err("tcp_sendpage() returned"
1218 " -EAGAIN\n");
1219 goto send_pg;
1220 }
1221
1222 pr_err("tcp_sendpage() failure: %d\n",
1223 tx_sent);
1224 return -1;
1225 }
1226
1227 data_len -= sub_len;
1228 offset = 0;
1229 sg = sg_next(sg);
1230 }
1231
1232send_padding:
1233 if (cmd->padding) {
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001234 struct kvec *iov_p = &cmd->iov_data[iov_off++];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001235
1236 tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1237 if (cmd->padding != tx_sent) {
1238 if (tx_sent == -EAGAIN) {
1239 pr_err("tx_data() returned -EAGAIN\n");
1240 goto send_padding;
1241 }
1242 return -1;
1243 }
1244 }
1245
1246send_datacrc:
1247 if (conn->conn_ops->DataDigest) {
Nicholas Bellinger40b05492011-09-16 16:55:47 -07001248 struct kvec *iov_d = &cmd->iov_data[iov_off];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001249
1250 tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1251 if (ISCSI_CRC_LEN != tx_sent) {
1252 if (tx_sent == -EAGAIN) {
1253 pr_err("tx_data() returned -EAGAIN\n");
1254 goto send_datacrc;
1255 }
1256 return -1;
1257 }
1258 }
1259
1260 return 0;
1261}
1262
1263/*
1264 * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1265 * back to the Initiator when an expection condition occurs with the
1266 * errors set in status_class and status_detail.
1267 *
1268 * Parameters: iSCSI Connection, Status Class, Status Detail.
1269 * Returns: 0 on success, -1 on error.
1270 */
1271int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1272{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001273 struct iscsi_login_rsp *hdr;
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001274 struct iscsi_login *login = conn->conn_login;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001275
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001276 login->login_failed = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001277 iscsit_collect_login_stats(conn, status_class, status_detail);
1278
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001279 hdr = (struct iscsi_login_rsp *)&login->rsp[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001280 hdr->opcode = ISCSI_OP_LOGIN_RSP;
1281 hdr->status_class = status_class;
1282 hdr->status_detail = status_detail;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001283 hdr->itt = conn->login_itt;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001284
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001285 return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001286}
1287
1288void iscsit_print_session_params(struct iscsi_session *sess)
1289{
1290 struct iscsi_conn *conn;
1291
1292 pr_debug("-----------------------------[Session Params for"
1293 " SID: %u]-----------------------------\n", sess->sid);
1294 spin_lock_bh(&sess->conn_lock);
1295 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1296 iscsi_dump_conn_ops(conn->conn_ops);
1297 spin_unlock_bh(&sess->conn_lock);
1298
1299 iscsi_dump_sess_ops(sess->sess_ops);
1300}
1301
1302static int iscsit_do_rx_data(
1303 struct iscsi_conn *conn,
1304 struct iscsi_data_count *count)
1305{
1306 int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001307 struct kvec *iov_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001308 struct msghdr msg;
1309
1310 if (!conn || !conn->sock || !conn->conn_ops)
1311 return -1;
1312
1313 memset(&msg, 0, sizeof(struct msghdr));
1314
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001315 iov_p = count->iov;
1316 iov_len = count->iov_count;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001317
1318 while (total_rx < data) {
1319 rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
1320 (data - total_rx), MSG_WAITALL);
1321 if (rx_loop <= 0) {
1322 pr_debug("rx_loop: %d total_rx: %d\n",
1323 rx_loop, total_rx);
1324 return rx_loop;
1325 }
1326 total_rx += rx_loop;
1327 pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1328 rx_loop, total_rx, data);
1329 }
1330
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001331 return total_rx;
1332}
1333
1334static int iscsit_do_tx_data(
1335 struct iscsi_conn *conn,
1336 struct iscsi_data_count *count)
1337{
1338 int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001339 struct kvec *iov_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001340 struct msghdr msg;
1341
1342 if (!conn || !conn->sock || !conn->conn_ops)
1343 return -1;
1344
1345 if (data <= 0) {
1346 pr_err("Data length is: %d\n", data);
1347 return -1;
1348 }
1349
1350 memset(&msg, 0, sizeof(struct msghdr));
1351
Nicholas Bellinger2ff017f2011-09-16 01:44:54 -07001352 iov_p = count->iov;
1353 iov_len = count->iov_count;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001354
1355 while (total_tx < data) {
1356 tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
1357 (data - total_tx));
1358 if (tx_loop <= 0) {
1359 pr_debug("tx_loop: %d total_tx %d\n",
1360 tx_loop, total_tx);
1361 return tx_loop;
1362 }
1363 total_tx += tx_loop;
1364 pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
1365 tx_loop, total_tx, data);
1366 }
1367
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368 return total_tx;
1369}
1370
1371int rx_data(
1372 struct iscsi_conn *conn,
1373 struct kvec *iov,
1374 int iov_count,
1375 int data)
1376{
1377 struct iscsi_data_count c;
1378
1379 if (!conn || !conn->sock || !conn->conn_ops)
1380 return -1;
1381
1382 memset(&c, 0, sizeof(struct iscsi_data_count));
1383 c.iov = iov;
1384 c.iov_count = iov_count;
1385 c.data_length = data;
1386 c.type = ISCSI_RX_DATA;
1387
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001388 return iscsit_do_rx_data(conn, &c);
1389}
1390
1391int tx_data(
1392 struct iscsi_conn *conn,
1393 struct kvec *iov,
1394 int iov_count,
1395 int data)
1396{
1397 struct iscsi_data_count c;
1398
1399 if (!conn || !conn->sock || !conn->conn_ops)
1400 return -1;
1401
1402 memset(&c, 0, sizeof(struct iscsi_data_count));
1403 c.iov = iov;
1404 c.iov_count = iov_count;
1405 c.data_length = data;
1406 c.type = ISCSI_TX_DATA;
1407
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001408 return iscsit_do_tx_data(conn, &c);
1409}
1410
1411void iscsit_collect_login_stats(
1412 struct iscsi_conn *conn,
1413 u8 status_class,
1414 u8 status_detail)
1415{
1416 struct iscsi_param *intrname = NULL;
1417 struct iscsi_tiqn *tiqn;
1418 struct iscsi_login_stats *ls;
1419
1420 tiqn = iscsit_snmp_get_tiqn(conn);
1421 if (!tiqn)
1422 return;
1423
1424 ls = &tiqn->login_stats;
1425
1426 spin_lock(&ls->lock);
1427 if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
1428 ((get_jiffies_64() - ls->last_fail_time) < 10)) {
1429 /* We already have the failure info for this login */
1430 spin_unlock(&ls->lock);
1431 return;
1432 }
1433
1434 if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1435 ls->accepts++;
1436 else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1437 ls->redirects++;
1438 ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1439 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1440 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1441 ls->authenticate_fails++;
1442 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
1443 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1444 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1445 ls->authorize_fails++;
1446 ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1447 } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1448 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1449 ls->negotiate_fails++;
1450 ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1451 } else {
1452 ls->other_fails++;
1453 ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1454 }
1455
1456 /* Save initiator name, ip address and time, if it is a failed login */
1457 if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1458 if (conn->param_list)
1459 intrname = iscsi_find_param_from_key(INITIATORNAME,
1460 conn->param_list);
1461 strcpy(ls->last_intr_fail_name,
1462 (intrname ? intrname->value : "Unknown"));
1463
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08001464 ls->last_intr_fail_ip_family = conn->login_family;
1465
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
1467 "%s", conn->login_ip);
1468 ls->last_fail_time = get_jiffies_64();
1469 }
1470
1471 spin_unlock(&ls->lock);
1472}
1473
1474struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1475{
1476 struct iscsi_portal_group *tpg;
1477
1478 if (!conn || !conn->sess)
1479 return NULL;
1480
1481 tpg = conn->sess->tpg;
1482 if (!tpg)
1483 return NULL;
1484
1485 if (!tpg->tpg_tiqn)
1486 return NULL;
1487
1488 return tpg->tpg_tiqn;
1489}