Merge branch 'iocb' into for-next
[firefly-linux-kernel-4.4.55.git] / drivers / target / iscsi / iscsi_target_erl2.c
1 /*******************************************************************************
2  * This file contains error recovery level two functions used by
3  * the iSCSI Target driver.
4  *
5  * (c) Copyright 2007-2013 Datera, Inc.
6  *
7  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  ******************************************************************************/
19
20 #include <scsi/iscsi_proto.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23
24 #include <target/iscsi/iscsi_target_core.h>
25 #include "iscsi_target_datain_values.h"
26 #include "iscsi_target_util.h"
27 #include "iscsi_target_erl0.h"
28 #include "iscsi_target_erl1.h"
29 #include "iscsi_target_erl2.h"
30 #include "iscsi_target.h"
31
32 /*
33  *      FIXME: Does RData SNACK apply here as well?
34  */
35 void iscsit_create_conn_recovery_datain_values(
36         struct iscsi_cmd *cmd,
37         __be32 exp_data_sn)
38 {
39         u32 data_sn = 0;
40         struct iscsi_conn *conn = cmd->conn;
41
42         cmd->next_burst_len = 0;
43         cmd->read_data_done = 0;
44
45         while (be32_to_cpu(exp_data_sn) > data_sn) {
46                 if ((cmd->next_burst_len +
47                      conn->conn_ops->MaxRecvDataSegmentLength) <
48                      conn->sess->sess_ops->MaxBurstLength) {
49                         cmd->read_data_done +=
50                                conn->conn_ops->MaxRecvDataSegmentLength;
51                         cmd->next_burst_len +=
52                                conn->conn_ops->MaxRecvDataSegmentLength;
53                 } else {
54                         cmd->read_data_done +=
55                                 (conn->sess->sess_ops->MaxBurstLength -
56                                 cmd->next_burst_len);
57                         cmd->next_burst_len = 0;
58                 }
59                 data_sn++;
60         }
61 }
62
63 void iscsit_create_conn_recovery_dataout_values(
64         struct iscsi_cmd *cmd)
65 {
66         u32 write_data_done = 0;
67         struct iscsi_conn *conn = cmd->conn;
68
69         cmd->data_sn = 0;
70         cmd->next_burst_len = 0;
71
72         while (cmd->write_data_done > write_data_done) {
73                 if ((write_data_done + conn->sess->sess_ops->MaxBurstLength) <=
74                      cmd->write_data_done)
75                         write_data_done += conn->sess->sess_ops->MaxBurstLength;
76                 else
77                         break;
78         }
79
80         cmd->write_data_done = write_data_done;
81 }
82
83 static int iscsit_attach_active_connection_recovery_entry(
84         struct iscsi_session *sess,
85         struct iscsi_conn_recovery *cr)
86 {
87         spin_lock(&sess->cr_a_lock);
88         list_add_tail(&cr->cr_list, &sess->cr_active_list);
89         spin_unlock(&sess->cr_a_lock);
90
91         return 0;
92 }
93
94 static int iscsit_attach_inactive_connection_recovery_entry(
95         struct iscsi_session *sess,
96         struct iscsi_conn_recovery *cr)
97 {
98         spin_lock(&sess->cr_i_lock);
99         list_add_tail(&cr->cr_list, &sess->cr_inactive_list);
100
101         sess->conn_recovery_count++;
102         pr_debug("Incremented connection recovery count to %u for"
103                 " SID: %u\n", sess->conn_recovery_count, sess->sid);
104         spin_unlock(&sess->cr_i_lock);
105
106         return 0;
107 }
108
109 struct iscsi_conn_recovery *iscsit_get_inactive_connection_recovery_entry(
110         struct iscsi_session *sess,
111         u16 cid)
112 {
113         struct iscsi_conn_recovery *cr;
114
115         spin_lock(&sess->cr_i_lock);
116         list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
117                 if (cr->cid == cid) {
118                         spin_unlock(&sess->cr_i_lock);
119                         return cr;
120                 }
121         }
122         spin_unlock(&sess->cr_i_lock);
123
124         return NULL;
125 }
126
127 void iscsit_free_connection_recovery_entires(struct iscsi_session *sess)
128 {
129         struct iscsi_cmd *cmd, *cmd_tmp;
130         struct iscsi_conn_recovery *cr, *cr_tmp;
131
132         spin_lock(&sess->cr_a_lock);
133         list_for_each_entry_safe(cr, cr_tmp, &sess->cr_active_list, cr_list) {
134                 list_del(&cr->cr_list);
135                 spin_unlock(&sess->cr_a_lock);
136
137                 spin_lock(&cr->conn_recovery_cmd_lock);
138                 list_for_each_entry_safe(cmd, cmd_tmp,
139                                 &cr->conn_recovery_cmd_list, i_conn_node) {
140
141                         list_del_init(&cmd->i_conn_node);
142                         cmd->conn = NULL;
143                         spin_unlock(&cr->conn_recovery_cmd_lock);
144                         iscsit_free_cmd(cmd, true);
145                         spin_lock(&cr->conn_recovery_cmd_lock);
146                 }
147                 spin_unlock(&cr->conn_recovery_cmd_lock);
148                 spin_lock(&sess->cr_a_lock);
149
150                 kfree(cr);
151         }
152         spin_unlock(&sess->cr_a_lock);
153
154         spin_lock(&sess->cr_i_lock);
155         list_for_each_entry_safe(cr, cr_tmp, &sess->cr_inactive_list, cr_list) {
156                 list_del(&cr->cr_list);
157                 spin_unlock(&sess->cr_i_lock);
158
159                 spin_lock(&cr->conn_recovery_cmd_lock);
160                 list_for_each_entry_safe(cmd, cmd_tmp,
161                                 &cr->conn_recovery_cmd_list, i_conn_node) {
162
163                         list_del_init(&cmd->i_conn_node);
164                         cmd->conn = NULL;
165                         spin_unlock(&cr->conn_recovery_cmd_lock);
166                         iscsit_free_cmd(cmd, true);
167                         spin_lock(&cr->conn_recovery_cmd_lock);
168                 }
169                 spin_unlock(&cr->conn_recovery_cmd_lock);
170                 spin_lock(&sess->cr_i_lock);
171
172                 kfree(cr);
173         }
174         spin_unlock(&sess->cr_i_lock);
175 }
176
177 int iscsit_remove_active_connection_recovery_entry(
178         struct iscsi_conn_recovery *cr,
179         struct iscsi_session *sess)
180 {
181         spin_lock(&sess->cr_a_lock);
182         list_del(&cr->cr_list);
183
184         sess->conn_recovery_count--;
185         pr_debug("Decremented connection recovery count to %u for"
186                 " SID: %u\n", sess->conn_recovery_count, sess->sid);
187         spin_unlock(&sess->cr_a_lock);
188
189         kfree(cr);
190
191         return 0;
192 }
193
194 static void iscsit_remove_inactive_connection_recovery_entry(
195         struct iscsi_conn_recovery *cr,
196         struct iscsi_session *sess)
197 {
198         spin_lock(&sess->cr_i_lock);
199         list_del(&cr->cr_list);
200         spin_unlock(&sess->cr_i_lock);
201 }
202
203 /*
204  *      Called with cr->conn_recovery_cmd_lock help.
205  */
206 int iscsit_remove_cmd_from_connection_recovery(
207         struct iscsi_cmd *cmd,
208         struct iscsi_session *sess)
209 {
210         struct iscsi_conn_recovery *cr;
211
212         if (!cmd->cr) {
213                 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
214                         " is NULL!\n", cmd->init_task_tag);
215                 BUG();
216         }
217         cr = cmd->cr;
218
219         list_del_init(&cmd->i_conn_node);
220         return --cr->cmd_count;
221 }
222
223 void iscsit_discard_cr_cmds_by_expstatsn(
224         struct iscsi_conn_recovery *cr,
225         u32 exp_statsn)
226 {
227         u32 dropped_count = 0;
228         struct iscsi_cmd *cmd, *cmd_tmp;
229         struct iscsi_session *sess = cr->sess;
230
231         spin_lock(&cr->conn_recovery_cmd_lock);
232         list_for_each_entry_safe(cmd, cmd_tmp,
233                         &cr->conn_recovery_cmd_list, i_conn_node) {
234
235                 if (((cmd->deferred_i_state != ISTATE_SENT_STATUS) &&
236                      (cmd->deferred_i_state != ISTATE_REMOVE)) ||
237                      (cmd->stat_sn >= exp_statsn)) {
238                         continue;
239                 }
240
241                 dropped_count++;
242                 pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
243                         " 0x%08x, CID: %hu.\n", cmd->init_task_tag,
244                                 cmd->stat_sn, cr->cid);
245
246                 iscsit_remove_cmd_from_connection_recovery(cmd, sess);
247
248                 spin_unlock(&cr->conn_recovery_cmd_lock);
249                 iscsit_free_cmd(cmd, true);
250                 spin_lock(&cr->conn_recovery_cmd_lock);
251         }
252         spin_unlock(&cr->conn_recovery_cmd_lock);
253
254         pr_debug("Dropped %u total acknowledged commands on"
255                 " CID: %hu less than old ExpStatSN: 0x%08x\n",
256                         dropped_count, cr->cid, exp_statsn);
257
258         if (!cr->cmd_count) {
259                 pr_debug("No commands to be reassigned for failed"
260                         " connection CID: %hu on SID: %u\n",
261                         cr->cid, sess->sid);
262                 iscsit_remove_inactive_connection_recovery_entry(cr, sess);
263                 iscsit_attach_active_connection_recovery_entry(sess, cr);
264                 pr_debug("iSCSI connection recovery successful for CID:"
265                         " %hu on SID: %u\n", cr->cid, sess->sid);
266                 iscsit_remove_active_connection_recovery_entry(cr, sess);
267         } else {
268                 iscsit_remove_inactive_connection_recovery_entry(cr, sess);
269                 iscsit_attach_active_connection_recovery_entry(sess, cr);
270         }
271 }
272
273 int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
274 {
275         u32 dropped_count = 0;
276         struct iscsi_cmd *cmd, *cmd_tmp;
277         struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
278         struct iscsi_session *sess = conn->sess;
279
280         mutex_lock(&sess->cmdsn_mutex);
281         list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
282                         &sess->sess_ooo_cmdsn_list, ooo_list) {
283
284                 if (ooo_cmdsn->cid != conn->cid)
285                         continue;
286
287                 dropped_count++;
288                 pr_debug("Dropping unacknowledged CmdSN:"
289                 " 0x%08x during connection recovery on CID: %hu\n",
290                         ooo_cmdsn->cmdsn, conn->cid);
291                 iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
292         }
293         mutex_unlock(&sess->cmdsn_mutex);
294
295         spin_lock_bh(&conn->cmd_lock);
296         list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
297                 if (!(cmd->cmd_flags & ICF_OOO_CMDSN))
298                         continue;
299
300                 list_del_init(&cmd->i_conn_node);
301
302                 spin_unlock_bh(&conn->cmd_lock);
303                 iscsit_free_cmd(cmd, true);
304                 spin_lock_bh(&conn->cmd_lock);
305         }
306         spin_unlock_bh(&conn->cmd_lock);
307
308         pr_debug("Dropped %u total unacknowledged commands on CID:"
309                 " %hu for ExpCmdSN: 0x%08x.\n", dropped_count, conn->cid,
310                                 sess->exp_cmd_sn);
311         return 0;
312 }
313
314 int iscsit_prepare_cmds_for_realligance(struct iscsi_conn *conn)
315 {
316         u32 cmd_count = 0;
317         struct iscsi_cmd *cmd, *cmd_tmp;
318         struct iscsi_conn_recovery *cr;
319
320         /*
321          * Allocate an struct iscsi_conn_recovery for this connection.
322          * Each struct iscsi_cmd contains an struct iscsi_conn_recovery pointer
323          * (struct iscsi_cmd->cr) so we need to allocate this before preparing the
324          * connection's command list for connection recovery.
325          */
326         cr = kzalloc(sizeof(struct iscsi_conn_recovery), GFP_KERNEL);
327         if (!cr) {
328                 pr_err("Unable to allocate memory for"
329                         " struct iscsi_conn_recovery.\n");
330                 return -1;
331         }
332         INIT_LIST_HEAD(&cr->cr_list);
333         INIT_LIST_HEAD(&cr->conn_recovery_cmd_list);
334         spin_lock_init(&cr->conn_recovery_cmd_lock);
335         /*
336          * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
337          * ISCSI_OP_NOOP_OUT opcodes.  For all other opcodes call
338          * list_del_init(&cmd->i_conn_node); to release the command to the
339          * session pool and remove it from the connection's list.
340          *
341          * Also stop the DataOUT timer, which will be restarted after
342          * sending the TMR response.
343          */
344         spin_lock_bh(&conn->cmd_lock);
345         list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
346
347                 if ((cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD) &&
348                     (cmd->iscsi_opcode != ISCSI_OP_NOOP_OUT)) {
349                         pr_debug("Not performing realligence on"
350                                 " Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
351                                 " CID: %hu\n", cmd->iscsi_opcode,
352                                 cmd->init_task_tag, cmd->cmd_sn, conn->cid);
353
354                         list_del_init(&cmd->i_conn_node);
355                         spin_unlock_bh(&conn->cmd_lock);
356                         iscsit_free_cmd(cmd, true);
357                         spin_lock_bh(&conn->cmd_lock);
358                         continue;
359                 }
360
361                 /*
362                  * Special case where commands greater than or equal to
363                  * the session's ExpCmdSN are attached to the connection
364                  * list but not to the out of order CmdSN list.  The one
365                  * obvious case is when a command with immediate data
366                  * attached must only check the CmdSN against ExpCmdSN
367                  * after the data is received.  The special case below
368                  * is when the connection fails before data is received,
369                  * but also may apply to other PDUs, so it has been
370                  * made generic here.
371                  */
372                 if (!(cmd->cmd_flags & ICF_OOO_CMDSN) && !cmd->immediate_cmd &&
373                      iscsi_sna_gte(cmd->cmd_sn, conn->sess->exp_cmd_sn)) {
374                         list_del_init(&cmd->i_conn_node);
375                         spin_unlock_bh(&conn->cmd_lock);
376                         iscsit_free_cmd(cmd, true);
377                         spin_lock_bh(&conn->cmd_lock);
378                         continue;
379                 }
380
381                 cmd_count++;
382                 pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
383                         " CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
384                         " realligence.\n", cmd->iscsi_opcode,
385                         cmd->init_task_tag, cmd->cmd_sn, cmd->stat_sn,
386                         conn->cid);
387
388                 cmd->deferred_i_state = cmd->i_state;
389                 cmd->i_state = ISTATE_IN_CONNECTION_RECOVERY;
390
391                 if (cmd->data_direction == DMA_TO_DEVICE)
392                         iscsit_stop_dataout_timer(cmd);
393
394                 cmd->sess = conn->sess;
395
396                 list_del_init(&cmd->i_conn_node);
397                 spin_unlock_bh(&conn->cmd_lock);
398
399                 iscsit_free_all_datain_reqs(cmd);
400
401                 transport_wait_for_tasks(&cmd->se_cmd);
402                 /*
403                  * Add the struct iscsi_cmd to the connection recovery cmd list
404                  */
405                 spin_lock(&cr->conn_recovery_cmd_lock);
406                 list_add_tail(&cmd->i_conn_node, &cr->conn_recovery_cmd_list);
407                 spin_unlock(&cr->conn_recovery_cmd_lock);
408
409                 spin_lock_bh(&conn->cmd_lock);
410                 cmd->cr = cr;
411                 cmd->conn = NULL;
412         }
413         spin_unlock_bh(&conn->cmd_lock);
414         /*
415          * Fill in the various values in the preallocated struct iscsi_conn_recovery.
416          */
417         cr->cid = conn->cid;
418         cr->cmd_count = cmd_count;
419         cr->maxrecvdatasegmentlength = conn->conn_ops->MaxRecvDataSegmentLength;
420         cr->maxxmitdatasegmentlength = conn->conn_ops->MaxXmitDataSegmentLength;
421         cr->sess = conn->sess;
422
423         iscsit_attach_inactive_connection_recovery_entry(conn->sess, cr);
424
425         return 0;
426 }
427
428 int iscsit_connection_recovery_transport_reset(struct iscsi_conn *conn)
429 {
430         atomic_set(&conn->connection_recovery, 1);
431
432         if (iscsit_close_connection(conn) < 0)
433                 return -1;
434
435         return 0;
436 }