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