blob: 2285988c209b00ba1f845c8c7afff52ca1fb9825 [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2007-2013 Datera, Inc.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00005 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
18
Herbert Xu69110e32016-01-24 21:19:52 +080019#include <crypto/hash.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000020#include <linux/string.h>
21#include <linux/kthread.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000022#include <linux/completion.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040023#include <linux/module.h>
David S. Miller5538d292015-05-28 11:35:41 -070024#include <linux/vmalloc.h>
Al Viro40401532012-02-13 03:58:52 +000025#include <linux/idr.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080026#include <linux/delay.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000027#include <asm/unaligned.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080028#include <net/ipv6.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020029#include <scsi/scsi_proto.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000030#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070031#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000032#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050033#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034
Sagi Grimberg67f091f2015-01-07 14:57:31 +020035#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000036#include "iscsi_target_parameters.h"
37#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000038#include "iscsi_target_datain_values.h"
39#include "iscsi_target_erl0.h"
40#include "iscsi_target_erl1.h"
41#include "iscsi_target_erl2.h"
42#include "iscsi_target_login.h"
43#include "iscsi_target_tmr.h"
44#include "iscsi_target_tpg.h"
45#include "iscsi_target_util.h"
46#include "iscsi_target.h"
47#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020048#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000049
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080050#include <target/iscsi/iscsi_transport.h>
51
Nicholas Bellingere48354c2011-07-23 06:43:04 +000052static LIST_HEAD(g_tiqn_list);
53static LIST_HEAD(g_np_list);
54static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080055static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000056
57static struct idr tiqn_idr;
58struct idr sess_idr;
59struct mutex auth_id_lock;
60spinlock_t sess_idr_lock;
61
62struct iscsit_global *iscsit_global;
63
Nicholas Bellingere48354c2011-07-23 06:43:04 +000064struct kmem_cache *lio_qr_cache;
65struct kmem_cache *lio_dr_cache;
66struct kmem_cache *lio_ooo_cache;
67struct kmem_cache *lio_r2t_cache;
68
69static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070070 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000071
72struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
73{
74 struct iscsi_tiqn *tiqn = NULL;
75
76 spin_lock(&tiqn_lock);
77 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
78 if (!strcmp(tiqn->tiqn, buf)) {
79
80 spin_lock(&tiqn->tiqn_state_lock);
81 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
82 tiqn->tiqn_access_count++;
83 spin_unlock(&tiqn->tiqn_state_lock);
84 spin_unlock(&tiqn_lock);
85 return tiqn;
86 }
87 spin_unlock(&tiqn->tiqn_state_lock);
88 }
89 }
90 spin_unlock(&tiqn_lock);
91
92 return NULL;
93}
94
95static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
96{
97 spin_lock(&tiqn->tiqn_state_lock);
98 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
99 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
100 spin_unlock(&tiqn->tiqn_state_lock);
101 return 0;
102 }
103 spin_unlock(&tiqn->tiqn_state_lock);
104
105 return -1;
106}
107
108void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
109{
110 spin_lock(&tiqn->tiqn_state_lock);
111 tiqn->tiqn_access_count--;
112 spin_unlock(&tiqn->tiqn_state_lock);
113}
114
115/*
116 * Note that IQN formatting is expected to be done in userspace, and
117 * no explict IQN format checks are done here.
118 */
119struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
120{
121 struct iscsi_tiqn *tiqn = NULL;
122 int ret;
123
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300124 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000125 pr_err("Target IQN exceeds %d bytes\n",
126 ISCSI_IQN_LEN);
127 return ERR_PTR(-EINVAL);
128 }
129
130 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
131 if (!tiqn) {
132 pr_err("Unable to allocate struct iscsi_tiqn\n");
133 return ERR_PTR(-ENOMEM);
134 }
135
136 sprintf(tiqn->tiqn, "%s", buf);
137 INIT_LIST_HEAD(&tiqn->tiqn_list);
138 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
139 spin_lock_init(&tiqn->tiqn_state_lock);
140 spin_lock_init(&tiqn->tiqn_tpg_lock);
141 spin_lock_init(&tiqn->sess_err_stats.lock);
142 spin_lock_init(&tiqn->login_stats.lock);
143 spin_lock_init(&tiqn->logout_stats.lock);
144
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000145 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
146
Tejun Heoc9365bd2013-02-27 17:04:43 -0800147 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000148 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800149
150 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800154 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000155 kfree(tiqn);
156 return ERR_PTR(ret);
157 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800162 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000163
164 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
165
166 return tiqn;
167
168}
169
170static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
171{
172 /*
173 * Wait for accesses to said struct iscsi_tiqn to end.
174 */
175 spin_lock(&tiqn->tiqn_state_lock);
176 while (tiqn->tiqn_access_count != 0) {
177 spin_unlock(&tiqn->tiqn_state_lock);
178 msleep(10);
179 spin_lock(&tiqn->tiqn_state_lock);
180 }
181 spin_unlock(&tiqn->tiqn_state_lock);
182}
183
184void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
185{
186 /*
187 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
188 * while holding tiqn->tiqn_state_lock. This means that all subsequent
189 * attempts to access this struct iscsi_tiqn will fail from both transport
190 * fabric and control code paths.
191 */
192 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
193 pr_err("iscsit_set_tiqn_shutdown() failed\n");
194 return;
195 }
196
197 iscsit_wait_for_tiqn(tiqn);
198
199 spin_lock(&tiqn_lock);
200 list_del(&tiqn->tiqn_list);
201 idr_remove(&tiqn_idr, tiqn->tiqn_index);
202 spin_unlock(&tiqn_lock);
203
204 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
205 tiqn->tiqn);
206 kfree(tiqn);
207}
208
209int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
210{
211 int ret;
212 /*
213 * Determine if the network portal is accepting storage traffic.
214 */
215 spin_lock_bh(&np->np_thread_lock);
216 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
217 spin_unlock_bh(&np->np_thread_lock);
218 return -1;
219 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000220 spin_unlock_bh(&np->np_thread_lock);
221 /*
222 * Determine if the portal group is accepting storage traffic.
223 */
224 spin_lock_bh(&tpg->tpg_state_lock);
225 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
226 spin_unlock_bh(&tpg->tpg_state_lock);
227 return -1;
228 }
229 spin_unlock_bh(&tpg->tpg_state_lock);
230
231 /*
232 * Here we serialize access across the TIQN+TPG Tuple.
233 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700234 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingeree7619f2015-05-19 15:10:44 -0700235 if (ret != 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000236 return -1;
237
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700238 spin_lock_bh(&tpg->tpg_state_lock);
239 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
240 spin_unlock_bh(&tpg->tpg_state_lock);
241 up(&tpg->np_login_sem);
242 return -1;
243 }
244 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000245
246 return 0;
247}
248
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700249void iscsit_login_kref_put(struct kref *kref)
250{
251 struct iscsi_tpg_np *tpg_np = container_of(kref,
252 struct iscsi_tpg_np, tpg_np_kref);
253
254 complete(&tpg_np->tpg_np_comp);
255}
256
257int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
258 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000259{
260 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
261
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700262 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000263
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700264 if (tpg_np)
265 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000266
267 if (tiqn)
268 iscsit_put_tiqn_for_login(tiqn);
269
270 return 0;
271}
272
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273bool iscsit_check_np_match(
Andy Grover13a3cf02015-08-24 10:26:06 -0700274 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800275 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000276 int network_transport)
277{
278 struct sockaddr_in *sock_in, *sock_in_e;
279 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800280 bool ip_match = false;
Andy Grover69d75572015-08-24 10:26:04 -0700281 u16 port, port_e;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000282
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800283 if (sockaddr->ss_family == AF_INET6) {
284 sock_in6 = (struct sockaddr_in6 *)sockaddr;
285 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
286
287 if (!memcmp(&sock_in6->sin6_addr.in6_u,
288 &sock_in6_e->sin6_addr.in6_u,
289 sizeof(struct in6_addr)))
290 ip_match = true;
291
292 port = ntohs(sock_in6->sin6_port);
Andy Grover69d75572015-08-24 10:26:04 -0700293 port_e = ntohs(sock_in6_e->sin6_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800294 } else {
295 sock_in = (struct sockaddr_in *)sockaddr;
296 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
297
298 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
299 ip_match = true;
300
301 port = ntohs(sock_in->sin_port);
Andy Grover69d75572015-08-24 10:26:04 -0700302 port_e = ntohs(sock_in_e->sin_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800303 }
304
Andy Grover69d75572015-08-24 10:26:04 -0700305 if (ip_match && (port_e == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800306 (np->np_network_transport == network_transport))
307 return true;
308
309 return false;
310}
311
Andy Groveree291e62014-01-24 16:18:54 -0800312/*
313 * Called with mutex np_lock held
314 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800315static struct iscsi_np *iscsit_get_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700316 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800317 int network_transport)
318{
319 struct iscsi_np *np;
320 bool match;
321
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800323 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000324 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800325 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000326 continue;
327 }
328
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800329 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200330 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000331 /*
332 * Increment the np_exports reference count now to
333 * prevent iscsit_del_np() below from being called
334 * while iscsi_tpg_add_network_portal() is called.
335 */
336 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800337 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000338 return np;
339 }
Andy Groveree291e62014-01-24 16:18:54 -0800340 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000341 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000342
343 return NULL;
344}
345
346struct iscsi_np *iscsit_add_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700347 struct sockaddr_storage *sockaddr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000348 int network_transport)
349{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000350 struct iscsi_np *np;
351 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800352
353 mutex_lock(&np_lock);
354
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000355 /*
356 * Locate the existing struct iscsi_np if already active..
357 */
358 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800359 if (np) {
360 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000361 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800362 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000363
364 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
365 if (!np) {
366 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800367 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000368 return ERR_PTR(-ENOMEM);
369 }
370
371 np->np_flags |= NPF_IP_NETWORK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000372 np->np_network_transport = network_transport;
373 spin_lock_init(&np->np_thread_lock);
374 init_completion(&np->np_restart_comp);
375 INIT_LIST_HEAD(&np->np_list);
376
377 ret = iscsi_target_setup_login_socket(np, sockaddr);
378 if (ret != 0) {
379 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800380 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000381 return ERR_PTR(ret);
382 }
383
384 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
385 if (IS_ERR(np->np_thread)) {
386 pr_err("Unable to create kthread: iscsi_np\n");
387 ret = PTR_ERR(np->np_thread);
388 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800389 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000390 return ERR_PTR(ret);
391 }
392 /*
393 * Increment the np_exports reference count now to prevent
394 * iscsit_del_np() below from being run while a new call to
395 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
396 * active. We don't need to hold np->np_thread_lock at this
397 * point because iscsi_np has not been added to g_np_list yet.
398 */
399 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800400 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000401
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000402 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800403 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000404
Andy Grover69d75572015-08-24 10:26:04 -0700405 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
406 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000407
408 return np;
409}
410
411int iscsit_reset_np_thread(
412 struct iscsi_np *np,
413 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700414 struct iscsi_portal_group *tpg,
415 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416{
417 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000418 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
419 spin_unlock_bh(&np->np_thread_lock);
420 return 0;
421 }
422 np->np_thread_state = ISCSI_NP_THREAD_RESET;
423
424 if (np->np_thread) {
425 spin_unlock_bh(&np->np_thread_lock);
426 send_sig(SIGINT, np->np_thread, 1);
427 wait_for_completion(&np->np_restart_comp);
428 spin_lock_bh(&np->np_thread_lock);
429 }
430 spin_unlock_bh(&np->np_thread_lock);
431
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700432 if (tpg_np && shutdown) {
433 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
434
435 wait_for_completion(&tpg_np->tpg_np_comp);
436 }
437
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000438 return 0;
439}
440
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800441static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000442{
Al Virobf6932f2012-07-21 08:55:18 +0100443 if (np->np_socket)
444 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000445}
446
447int iscsit_del_np(struct iscsi_np *np)
448{
449 spin_lock_bh(&np->np_thread_lock);
450 np->np_exports--;
451 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700452 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000453 spin_unlock_bh(&np->np_thread_lock);
454 return 0;
455 }
456 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
457 spin_unlock_bh(&np->np_thread_lock);
458
459 if (np->np_thread) {
460 /*
461 * We need to send the signal to wakeup Linux/Net
462 * which may be sleeping in sock_accept()..
463 */
464 send_sig(SIGINT, np->np_thread, 1);
465 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800466 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000467 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800468
469 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000470
Andy Groveree291e62014-01-24 16:18:54 -0800471 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000472 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800473 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000474
Andy Grover69d75572015-08-24 10:26:04 -0700475 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
476 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800478 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000479 kfree(np);
480 return 0;
481}
482
Varun Prakashe8205cc2016-04-20 00:00:11 +0530483static void iscsit_get_rx_pdu(struct iscsi_conn *);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700484
Varun Prakashd2faaef2016-04-20 00:00:19 +0530485int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700486{
487 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
488 return 0;
489}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530490EXPORT_SYMBOL(iscsit_queue_rsp);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700491
Varun Prakashd2faaef2016-04-20 00:00:19 +0530492void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700493{
494 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
495
496 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -0700497 if (!list_empty(&cmd->i_conn_node) &&
498 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700499 list_del_init(&cmd->i_conn_node);
500 spin_unlock_bh(&conn->cmd_lock);
501
502 __iscsit_free_cmd(cmd, scsi_cmd, true);
503}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530504EXPORT_SYMBOL(iscsit_aborted_task);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700505
Varun Prakash2854bb22016-04-20 00:00:08 +0530506static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
507 u32, u32, u8 *, u8 *);
508static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
509
510static int
511iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
512 const void *data_buf, u32 data_buf_len)
513{
514 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
515 struct kvec *iov;
516 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
517 int ret;
518
519 iov = &cmd->iov_misc[0];
520 iov[niov].iov_base = cmd->pdu;
521 iov[niov++].iov_len = ISCSI_HDR_LEN;
522
523 if (conn->conn_ops->HeaderDigest) {
524 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
525
526 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
527 ISCSI_HDR_LEN, 0, NULL,
528 (u8 *)header_digest);
529
530 iov[0].iov_len += ISCSI_CRC_LEN;
531 tx_size += ISCSI_CRC_LEN;
532 pr_debug("Attaching CRC32C HeaderDigest"
533 " to opcode 0x%x 0x%08x\n",
534 hdr->opcode, *header_digest);
535 }
536
537 if (data_buf_len) {
538 u32 padding = ((-data_buf_len) & 3);
539
540 iov[niov].iov_base = (void *)data_buf;
541 iov[niov++].iov_len = data_buf_len;
542 tx_size += data_buf_len;
543
544 if (padding != 0) {
545 iov[niov].iov_base = &cmd->pad_bytes;
546 iov[niov++].iov_len = padding;
547 tx_size += padding;
548 pr_debug("Attaching %u additional"
549 " padding bytes.\n", padding);
550 }
551
552 if (conn->conn_ops->DataDigest) {
553 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
554 data_buf, data_buf_len,
555 padding,
556 (u8 *)&cmd->pad_bytes,
557 (u8 *)&cmd->data_crc);
558
559 iov[niov].iov_base = &cmd->data_crc;
560 iov[niov++].iov_len = ISCSI_CRC_LEN;
561 tx_size += ISCSI_CRC_LEN;
562 pr_debug("Attached DataDigest for %u"
563 " bytes opcode 0x%x, CRC 0x%08x\n",
564 data_buf_len, hdr->opcode, cmd->data_crc);
565 }
566 }
567
568 cmd->iov_misc_count = niov;
569 cmd->tx_size = tx_size;
570
571 ret = iscsit_send_tx_data(cmd, conn, 1);
572 if (ret < 0) {
573 iscsit_tx_thread_wait_for_tcp(conn);
574 return ret;
575 }
576
577 return 0;
578}
579
580static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
581static void iscsit_unmap_iovec(struct iscsi_cmd *);
582static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
583 u32, u32, u32, u8 *);
584static int
585iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
586 const struct iscsi_datain *datain)
587{
588 struct kvec *iov;
589 u32 iov_count = 0, tx_size = 0;
590 int ret, iov_ret;
591
592 iov = &cmd->iov_data[0];
593 iov[iov_count].iov_base = cmd->pdu;
594 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
595 tx_size += ISCSI_HDR_LEN;
596
597 if (conn->conn_ops->HeaderDigest) {
598 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
599
600 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
601 ISCSI_HDR_LEN, 0, NULL,
602 (u8 *)header_digest);
603
604 iov[0].iov_len += ISCSI_CRC_LEN;
605 tx_size += ISCSI_CRC_LEN;
606
607 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
608 *header_digest);
609 }
610
611 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
612 datain->offset, datain->length);
613 if (iov_ret < 0)
614 return -1;
615
616 iov_count += iov_ret;
617 tx_size += datain->length;
618
619 cmd->padding = ((-datain->length) & 3);
620 if (cmd->padding) {
621 iov[iov_count].iov_base = cmd->pad_bytes;
622 iov[iov_count++].iov_len = cmd->padding;
623 tx_size += cmd->padding;
624
625 pr_debug("Attaching %u padding bytes\n", cmd->padding);
626 }
627
628 if (conn->conn_ops->DataDigest) {
629 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
630 cmd, datain->offset,
631 datain->length,
632 cmd->padding,
633 cmd->pad_bytes);
634
635 iov[iov_count].iov_base = &cmd->data_crc;
636 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
637 tx_size += ISCSI_CRC_LEN;
638
639 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
640 datain->length + cmd->padding, cmd->data_crc);
641 }
642
643 cmd->iov_data_count = iov_count;
644 cmd->tx_size = tx_size;
645
646 ret = iscsit_fe_sendpage_sg(cmd, conn);
647
648 iscsit_unmap_iovec(cmd);
649
650 if (ret < 0) {
651 iscsit_tx_thread_wait_for_tcp(conn);
652 return ret;
653 }
654
655 return 0;
656}
657
658static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
659 struct iscsi_datain_req *dr, const void *buf,
660 u32 buf_len)
661{
662 if (dr)
663 return iscsit_xmit_datain_pdu(conn, cmd, buf);
664 else
665 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
666}
667
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700668static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
669{
670 return TARGET_PROT_NORMAL;
671}
672
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800673static struct iscsit_transport iscsi_target_transport = {
674 .name = "iSCSI/TCP",
675 .transport_type = ISCSI_TCP,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -0700676 .rdma_shutdown = false,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800677 .owner = NULL,
678 .iscsit_setup_np = iscsit_setup_np,
679 .iscsit_accept_np = iscsit_accept_np,
680 .iscsit_free_np = iscsit_free_np,
681 .iscsit_get_login_rx = iscsit_get_login_rx,
682 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800683 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700684 .iscsit_immediate_queue = iscsit_immediate_queue,
685 .iscsit_response_queue = iscsit_response_queue,
686 .iscsit_queue_data_in = iscsit_queue_rsp,
687 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700688 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530689 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Varun Prakashe8205cc2016-04-20 00:00:11 +0530690 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700691 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800692};
693
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000694static int __init iscsi_target_init_module(void)
695{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800696 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000697
698 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
699
700 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
701 if (!iscsit_global) {
702 pr_err("Unable to allocate memory for iscsit_global\n");
703 return -1;
704 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800705 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000706 mutex_init(&auth_id_lock);
707 spin_lock_init(&sess_idr_lock);
708 idr_init(&tiqn_idr);
709 idr_init(&sess_idr);
710
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200711 ret = target_register_template(&iscsi_ops);
712 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000713 goto out;
714
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800715 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
716 iscsit_global->ts_bitmap = vzalloc(size);
717 if (!iscsit_global->ts_bitmap) {
718 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000719 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000720 }
721
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000722 lio_qr_cache = kmem_cache_create("lio_qr_cache",
723 sizeof(struct iscsi_queue_req),
724 __alignof__(struct iscsi_queue_req), 0, NULL);
725 if (!lio_qr_cache) {
726 pr_err("nable to kmem_cache_create() for"
727 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800728 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000729 }
730
731 lio_dr_cache = kmem_cache_create("lio_dr_cache",
732 sizeof(struct iscsi_datain_req),
733 __alignof__(struct iscsi_datain_req), 0, NULL);
734 if (!lio_dr_cache) {
735 pr_err("Unable to kmem_cache_create() for"
736 " lio_dr_cache\n");
737 goto qr_out;
738 }
739
740 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
741 sizeof(struct iscsi_ooo_cmdsn),
742 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
743 if (!lio_ooo_cache) {
744 pr_err("Unable to kmem_cache_create() for"
745 " lio_ooo_cache\n");
746 goto dr_out;
747 }
748
749 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
750 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
751 0, NULL);
752 if (!lio_r2t_cache) {
753 pr_err("Unable to kmem_cache_create() for"
754 " lio_r2t_cache\n");
755 goto ooo_out;
756 }
757
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800758 iscsit_register_transport(&iscsi_target_transport);
759
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000760 if (iscsit_load_discovery_tpg() < 0)
761 goto r2t_out;
762
763 return ret;
764r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100765 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766 kmem_cache_destroy(lio_r2t_cache);
767ooo_out:
768 kmem_cache_destroy(lio_ooo_cache);
769dr_out:
770 kmem_cache_destroy(lio_dr_cache);
771qr_out:
772 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800773bitmap_out:
774 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000775configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200776 /* XXX: this probably wants it to be it's own unwind step.. */
777 if (iscsit_global->discovery_tpg)
778 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
779 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780out:
781 kfree(iscsit_global);
782 return -ENOMEM;
783}
784
785static void __exit iscsi_target_cleanup_module(void)
786{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000787 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800788 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789 kmem_cache_destroy(lio_qr_cache);
790 kmem_cache_destroy(lio_dr_cache);
791 kmem_cache_destroy(lio_ooo_cache);
792 kmem_cache_destroy(lio_r2t_cache);
793
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200794 /*
795 * Shutdown discovery sessions and disable discovery TPG
796 */
797 if (iscsit_global->discovery_tpg)
798 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200800 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000801
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800802 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803 kfree(iscsit_global);
804}
805
Varun Prakashd2faaef2016-04-20 00:00:19 +0530806int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700807 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000808 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700809 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810{
811 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000813 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000814 if (!cmd)
815 return -1;
816
817 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700818 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000819
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100820 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000821 if (!cmd->buf_ptr) {
822 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700823 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000824 return -1;
825 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000826
827 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700828 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000829 spin_unlock_bh(&conn->cmd_lock);
830
831 cmd->i_state = ISTATE_SEND_REJECT;
832 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
833
Nicholas Bellingerba159912013-07-03 03:48:24 -0700834 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000835}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530836EXPORT_SYMBOL(iscsit_add_reject);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000837
Nicholas Bellingerba159912013-07-03 03:48:24 -0700838static int iscsit_add_reject_from_cmd(
839 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000840 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700841 bool add_to_conn,
842 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000843{
844 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000845
846 if (!cmd->conn) {
847 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
848 cmd->init_task_tag);
849 return -1;
850 }
851 conn = cmd->conn;
852
853 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700854 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000855
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100856 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000857 if (!cmd->buf_ptr) {
858 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700859 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000860 return -1;
861 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000862
863 if (add_to_conn) {
864 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700865 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000866 spin_unlock_bh(&conn->cmd_lock);
867 }
868
869 cmd->i_state = ISTATE_SEND_REJECT;
870 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800871 /*
872 * Perform the kref_put now if se_cmd has already been setup by
873 * scsit_setup_scsi_cmd()
874 */
875 if (cmd->se_cmd.se_tfo != NULL) {
876 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200877 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800878 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000880}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700881
882static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
883 unsigned char *buf)
884{
885 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
886}
887
888int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
889{
890 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
891}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530892EXPORT_SYMBOL(iscsit_reject_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000893
894/*
895 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700896 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000897 */
898static int iscsit_map_iovec(
899 struct iscsi_cmd *cmd,
900 struct kvec *iov,
901 u32 data_offset,
902 u32 data_length)
903{
904 u32 i = 0;
905 struct scatterlist *sg;
906 unsigned int page_off;
907
908 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700909 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000910 */
Imran Haider2b165092016-05-08 11:17:54 -0400911 u32 ent = data_offset / PAGE_SIZE;
912
913 if (ent >= cmd->se_cmd.t_data_nents) {
914 pr_err("Initial page entry out-of-bounds\n");
915 return -1;
916 }
917
918 sg = &cmd->se_cmd.t_data_sg[ent];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000919 page_off = (data_offset % PAGE_SIZE);
920
921 cmd->first_data_sg = sg;
922 cmd->first_data_sg_off = page_off;
923
924 while (data_length) {
925 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
926
927 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
928 iov[i].iov_len = cur_len;
929
930 data_length -= cur_len;
931 page_off = 0;
932 sg = sg_next(sg);
933 i++;
934 }
935
936 cmd->kmapped_nents = i;
937
938 return i;
939}
940
941static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
942{
943 u32 i;
944 struct scatterlist *sg;
945
946 sg = cmd->first_data_sg;
947
948 for (i = 0; i < cmd->kmapped_nents; i++)
949 kunmap(sg_page(&sg[i]));
950}
951
952static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
953{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700954 LIST_HEAD(ack_list);
955 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000956
957 conn->exp_statsn = exp_statsn;
958
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800959 if (conn->sess->sess_ops->RDMAExtensions)
960 return;
961
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000962 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700963 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000964 spin_lock(&cmd->istate_lock);
965 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800966 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000967 cmd->i_state = ISTATE_REMOVE;
968 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700969 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000970 continue;
971 }
972 spin_unlock(&cmd->istate_lock);
973 }
974 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700975
976 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800977 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700978 iscsit_free_cmd(cmd, false);
979 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000980}
981
982static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
983{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700984 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000985
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400986 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000987
988 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
989 if (!cmd->iov_data) {
990 pr_err("Unable to allocate cmd->iov_data\n");
991 return -ENOMEM;
992 }
993
994 cmd->orig_iov_data_count = iov_count;
995 return 0;
996}
997
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800998int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
999 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001000{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001001 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001002 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -07001003 int iscsi_task_attr;
1004 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001005
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001006 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001007
1008 hdr = (struct iscsi_scsi_req *) buf;
1009 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001010
1011 /* FIXME; Add checks for AdditionalHeaderSegment */
1012
1013 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1014 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1015 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1016 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001017 return iscsit_add_reject_cmd(cmd,
1018 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001019 }
1020
1021 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1022 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1023 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001024 * From RFC-3720 Section 10.3.1:
1025 *
1026 * "Either or both of R and W MAY be 1 when either the
1027 * Expected Data Transfer Length and/or Bidirectional Read
1028 * Expected Data Transfer Length are 0"
1029 *
1030 * For this case, go ahead and clear the unnecssary bits
1031 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001032 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001033 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1034 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001035
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001036 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001038 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001039 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001040
1041 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1042 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1043 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1044 " MUST be set if Expected Data Transfer Length is not 0."
1045 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001046 return iscsit_add_reject_cmd(cmd,
1047 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001048 }
1049
1050 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1051 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1052 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001053 return iscsit_add_reject_cmd(cmd,
1054 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001055 }
1056
1057 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1058 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1059 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001060 return iscsit_add_reject_cmd(cmd,
1061 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001062 }
1063
1064 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1065 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1066 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001067 return iscsit_add_reject_cmd(cmd,
1068 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001069 }
1070
Nicholas Bellingerba159912013-07-03 03:48:24 -07001071 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001072 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1073 pr_err("Expected Data Transfer Length and Length of"
1074 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1075 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001076 return iscsit_add_reject_cmd(cmd,
1077 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 }
1079
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001080 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001081 pr_err("DataSegmentLength: %u is greater than"
1082 " EDTL: %u, protocol error.\n", payload_length,
1083 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001084 return iscsit_add_reject_cmd(cmd,
1085 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001086 }
1087
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001088 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001089 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001090 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1091 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001092 return iscsit_add_reject_cmd(cmd,
1093 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001094 }
1095
1096 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1097 pr_err("DataSegmentLength: %u is greater than"
1098 " FirstBurstLength: %u, protocol error.\n",
1099 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001100 return iscsit_add_reject_cmd(cmd,
1101 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001102 }
1103
1104 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1105 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1106 DMA_NONE;
1107
Andy Groverd28b11692012-04-03 15:51:22 -07001108 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001109 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1110 /*
1111 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1112 */
1113 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1114 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001115 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001116 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001117 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001118 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001119 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001120 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001121 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001122 else {
1123 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001124 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1125 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001126 }
1127
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001128 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1129 cmd->i_state = ISTATE_NEW_CMD;
1130 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1131 cmd->immediate_data = (payload_length) ? 1 : 0;
1132 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1133 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1134 if (cmd->unsolicited_data)
1135 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1136
1137 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001138 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001139 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001140 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001141 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001142 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1143 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001144 cmd->first_burst_len = payload_length;
1145
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001146 if (!conn->sess->sess_ops->RDMAExtensions &&
1147 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001148 struct iscsi_datain_req *dr;
1149
1150 dr = iscsit_allocate_datain_req();
1151 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001152 return iscsit_add_reject_cmd(cmd,
1153 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001154
1155 iscsit_attach_datain_req(cmd, dr);
1156 }
1157
1158 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001159 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1160 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001161 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001162 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1163 cmd->data_direction, sam_task_attr,
1164 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001165
1166 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1167 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001168 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1169 conn->cid);
1170
Bart Van Asscheafc16602015-04-27 13:52:36 +02001171 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001172
Christoph Hellwigde103c92012-11-06 12:24:09 -08001173 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1174 scsilun_to_int(&hdr->lun));
1175 if (cmd->sense_reason)
1176 goto attach_cmd;
1177
Bart Van Assche649ee052015-04-14 13:26:44 +02001178 /* only used for printks or comparing with ->ref_task_tag */
1179 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001180 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1181 if (cmd->sense_reason) {
1182 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001183 return iscsit_add_reject_cmd(cmd,
1184 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001185 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001186
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001187 goto attach_cmd;
1188 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001189
Christoph Hellwigde103c92012-11-06 12:24:09 -08001190 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001191 return iscsit_add_reject_cmd(cmd,
1192 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001193 }
1194
1195attach_cmd:
1196 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001197 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001198 spin_unlock_bh(&conn->cmd_lock);
1199 /*
1200 * Check if we need to delay processing because of ALUA
1201 * Active/NonOptimized primary access state..
1202 */
1203 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001204
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001205 return 0;
1206}
1207EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001208
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001209void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1210{
1211 iscsit_set_dataout_sequence_values(cmd);
1212
1213 spin_lock_bh(&cmd->dataout_timeout_lock);
1214 iscsit_start_dataout_timer(cmd, cmd->conn);
1215 spin_unlock_bh(&cmd->dataout_timeout_lock);
1216}
1217EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1218
1219int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1220 struct iscsi_scsi_req *hdr)
1221{
1222 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001223 /*
1224 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1225 * the Immediate Bit is not set, and no Immediate
1226 * Data is attached.
1227 *
1228 * A PDU/CmdSN carrying Immediate Data can only
1229 * be processed after the DataCRC has passed.
1230 * If the DataCRC fails, the CmdSN MUST NOT
1231 * be acknowledged. (See below)
1232 */
1233 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001234 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1235 (unsigned char *)hdr, hdr->cmdsn);
1236 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1237 return -1;
1238 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001239 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001240 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001241 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001242 }
1243
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001244 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245
1246 /*
1247 * If no Immediate Data is attached, it's OK to return now.
1248 */
1249 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001250 if (!cmd->sense_reason && cmd->unsolicited_data)
1251 iscsit_set_unsoliticed_dataout(cmd);
1252 if (!cmd->sense_reason)
1253 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001254
Bart Van Asscheafc16602015-04-27 13:52:36 +02001255 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001256 return 0;
1257 }
1258
1259 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001260 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1261 * execution. These exceptions are processed in CmdSN order using
1262 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001263 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001264 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001265 if (cmd->reject_reason)
1266 return 0;
1267
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001268 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001269 }
1270 /*
1271 * Call directly into transport_generic_new_cmd() to perform
1272 * the backend memory allocation.
1273 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001274 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001275 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001276 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001277
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001278 return 0;
1279}
1280EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1281
1282static int
1283iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1284 bool dump_payload)
1285{
1286 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1287 /*
1288 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1289 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001290 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001291 goto after_immediate_data;
1292
1293 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1294 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001295after_immediate_data:
1296 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1297 /*
1298 * A PDU/CmdSN carrying Immediate Data passed
1299 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1300 * Immediate Bit is not set.
1301 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001302 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1303 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001304 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001305 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001306
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001307 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001308 int rc;
1309
1310 rc = iscsit_dump_data_payload(cmd->conn,
1311 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001312 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001313 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001314 } else if (cmd->unsolicited_data)
1315 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001317 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1318 /*
1319 * Immediate Data failed DataCRC and ERL>=1,
1320 * silently drop this PDU and let the initiator
1321 * plug the CmdSN gap.
1322 *
1323 * FIXME: Send Unsolicited NOPIN with reserved
1324 * TTT here to help the initiator figure out
1325 * the missing CmdSN, although they should be
1326 * intelligent enough to determine the missing
1327 * CmdSN and issue a retry to plug the sequence.
1328 */
1329 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001330 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001331 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1332 return -1;
1333
1334 return 0;
1335}
1336
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001337static int
1338iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1339 unsigned char *buf)
1340{
1341 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1342 int rc, immed_data;
1343 bool dump_payload = false;
1344
1345 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1346 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001347 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001348 /*
1349 * Allocation iovecs needed for struct socket operations for
1350 * traditional iSCSI block I/O.
1351 */
1352 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001353 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001354 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001355 }
1356 immed_data = cmd->immediate_data;
1357
1358 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1359 if (rc < 0)
1360 return rc;
1361 else if (rc > 0)
1362 dump_payload = true;
1363
1364 if (!immed_data)
1365 return 0;
1366
1367 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1368}
1369
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001370static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001371 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001372 struct iscsi_cmd *cmd,
1373 u32 data_offset,
1374 u32 data_length,
1375 u32 padding,
1376 u8 *pad_bytes)
1377{
1378 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001379 struct scatterlist *sg;
1380 unsigned int page_off;
1381
Herbert Xu69110e32016-01-24 21:19:52 +08001382 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001383
1384 sg = cmd->first_data_sg;
1385 page_off = cmd->first_data_sg_off;
1386
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001388 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001389
Herbert Xu69110e32016-01-24 21:19:52 +08001390 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1391 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001392
1393 data_length -= cur_len;
1394 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001395 /* iscsit_map_iovec has already checked for invalid sg pointers */
1396 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001397 }
1398
1399 if (padding) {
1400 struct scatterlist pad_sg;
1401
1402 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001403 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1404 padding);
1405 crypto_ahash_finup(hash);
1406 } else {
1407 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1408 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001409 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001410
1411 return data_crc;
1412}
1413
1414static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001415 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001416 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001417 u32 payload_length,
1418 u32 padding,
1419 u8 *pad_bytes,
1420 u8 *data_crc)
1421{
Herbert Xu69110e32016-01-24 21:19:52 +08001422 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001423
Herbert Xu69110e32016-01-24 21:19:52 +08001424 sg_init_table(sg, ARRAY_SIZE(sg));
1425 sg_set_buf(sg, buf, payload_length);
1426 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427
Herbert Xu69110e32016-01-24 21:19:52 +08001428 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001429
Herbert Xu69110e32016-01-24 21:19:52 +08001430 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431}
1432
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001433int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301434__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1435 struct iscsi_cmd *cmd, u32 payload_length,
1436 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001437{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301438 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001440 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001441
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001442 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001443 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001444
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001445 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1446 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001447 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001448 payload_length, conn->cid);
1449
1450 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1451 pr_err("Command ITT: 0x%08x received DataOUT after"
1452 " last DataOUT received, dumping payload\n",
1453 cmd->init_task_tag);
1454 return iscsit_dump_data_payload(conn, payload_length, 1);
1455 }
1456
1457 if (cmd->data_direction != DMA_TO_DEVICE) {
1458 pr_err("Command ITT: 0x%08x received DataOUT for a"
1459 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001460 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001461 }
1462 se_cmd = &cmd->se_cmd;
1463 iscsit_mod_dataout_timer(cmd);
1464
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001465 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001466 pr_err("DataOut Offset: %u, Length %u greater than"
1467 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001468 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001469 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 }
1471
1472 if (cmd->unsolicited_data) {
1473 int dump_unsolicited_data = 0;
1474
1475 if (conn->sess->sess_ops->InitialR2T) {
1476 pr_err("Received unexpected unsolicited data"
1477 " while InitialR2T=Yes, protocol error.\n");
1478 transport_send_check_condition_and_sense(&cmd->se_cmd,
1479 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1480 return -1;
1481 }
1482 /*
1483 * Special case for dealing with Unsolicited DataOUT
1484 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1485 * failures;
1486 */
1487
1488 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001489 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001490 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001491 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001492
1493 if (dump_unsolicited_data) {
1494 /*
1495 * Check if a delayed TASK_ABORTED status needs to
1496 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001497 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001498 */
1499 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1500 iscsit_stop_dataout_timer(cmd);
1501
1502 transport_check_aborted_status(se_cmd,
1503 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1504 return iscsit_dump_data_payload(conn, payload_length, 1);
1505 }
1506 } else {
1507 /*
1508 * For the normal solicited data path:
1509 *
1510 * Check for a delayed TASK_ABORTED status and dump any
1511 * incoming data out payload if one exists. Also, when the
1512 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1513 * data out sequence, we decrement outstanding_r2ts. Once
1514 * outstanding_r2ts reaches zero, go ahead and send the delayed
1515 * TASK_ABORTED status.
1516 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001517 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001518 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1519 if (--cmd->outstanding_r2ts < 1) {
1520 iscsit_stop_dataout_timer(cmd);
1521 transport_check_aborted_status(
1522 se_cmd, 1);
1523 }
1524
1525 return iscsit_dump_data_payload(conn, payload_length, 1);
1526 }
1527 }
1528 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001529 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 * within-command recovery checks before receiving the payload.
1531 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001532 rc = iscsit_check_pre_dataout(cmd, buf);
1533 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001534 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001535 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001536 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301537 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001538 return 0;
1539}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301540EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1541
1542int
1543iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1544 struct iscsi_cmd **out_cmd)
1545{
1546 struct iscsi_data *hdr = buf;
1547 struct iscsi_cmd *cmd;
1548 u32 payload_length = ntoh24(hdr->dlength);
1549 int rc;
1550 bool success = false;
1551
1552 if (!payload_length) {
1553 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1554 return 0;
1555 }
1556
1557 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1558 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1559 " MaxXmitDataSegmentLength: %u\n", payload_length,
1560 conn->conn_ops->MaxXmitDataSegmentLength);
1561 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1562 }
1563
1564 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1565 if (!cmd)
1566 return 0;
1567
1568 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1569
1570 if (success)
1571 *out_cmd = cmd;
1572
1573 return rc;
1574}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001575EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1576
1577static int
1578iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1579 struct iscsi_data *hdr)
1580{
1581 struct kvec *iov;
1582 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1583 u32 payload_length = ntoh24(hdr->dlength);
1584 int iov_ret, data_crc_failed = 0;
1585
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001586 rx_size += payload_length;
1587 iov = &cmd->iov_data[0];
1588
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001589 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1590 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001591 if (iov_ret < 0)
1592 return -1;
1593
1594 iov_count += iov_ret;
1595
1596 padding = ((-payload_length) & 3);
1597 if (padding != 0) {
1598 iov[iov_count].iov_base = cmd->pad_bytes;
1599 iov[iov_count++].iov_len = padding;
1600 rx_size += padding;
1601 pr_debug("Receiving %u padding bytes.\n", padding);
1602 }
1603
1604 if (conn->conn_ops->DataDigest) {
1605 iov[iov_count].iov_base = &checksum;
1606 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1607 rx_size += ISCSI_CRC_LEN;
1608 }
1609
1610 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1611
1612 iscsit_unmap_iovec(cmd);
1613
1614 if (rx_got != rx_size)
1615 return -1;
1616
1617 if (conn->conn_ops->DataDigest) {
1618 u32 data_crc;
1619
Herbert Xu69110e32016-01-24 21:19:52 +08001620 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001621 be32_to_cpu(hdr->offset),
1622 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001623 cmd->pad_bytes);
1624
1625 if (checksum != data_crc) {
1626 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1627 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1628 " does not match computed 0x%08x\n",
1629 hdr->itt, hdr->offset, payload_length,
1630 hdr->datasn, checksum, data_crc);
1631 data_crc_failed = 1;
1632 } else {
1633 pr_debug("Got CRC32C DataDigest 0x%08x for"
1634 " %u bytes of Data Out\n", checksum,
1635 payload_length);
1636 }
1637 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001638
1639 return data_crc_failed;
1640}
1641
1642int
1643iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1644 bool data_crc_failed)
1645{
1646 struct iscsi_conn *conn = cmd->conn;
1647 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001648 /*
1649 * Increment post receive data and CRC values or perform
1650 * within-command recovery.
1651 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001652 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1653 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001654 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001655 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001657 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1658 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001659 /*
1660 * Handle extra special case for out of order
1661 * Unsolicited Data Out.
1662 */
1663 spin_lock_bh(&cmd->istate_lock);
1664 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1665 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1666 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1667 spin_unlock_bh(&cmd->istate_lock);
1668
1669 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001670 if (ooo_cmdsn)
1671 return 0;
1672 target_execute_cmd(&cmd->se_cmd);
1673 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001674 } else /* DATAOUT_CANNOT_RECOVER */
1675 return -1;
1676
1677 return 0;
1678}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001679EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001680
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001681static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1682{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001683 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001684 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1685 int rc;
1686 bool data_crc_failed = false;
1687
1688 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1689 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001690 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001691 else if (!cmd)
1692 return 0;
1693
1694 rc = iscsit_get_dataout(conn, cmd, hdr);
1695 if (rc < 0)
1696 return rc;
1697 else if (rc > 0)
1698 data_crc_failed = true;
1699
1700 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1701}
1702
Nicholas Bellinger778de362013-06-14 16:07:47 -07001703int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1704 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001705{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001706 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001707
Arshad Hussaina3662602014-03-14 15:28:59 -07001708 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1709 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1710 if (!cmd)
1711 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1712 (unsigned char *)hdr);
1713
1714 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1715 (unsigned char *)hdr);
1716 }
1717
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001718 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001719 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1720 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001721 if (!cmd)
1722 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1723 (unsigned char *)hdr);
1724
Nicholas Bellingerba159912013-07-03 03:48:24 -07001725 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1726 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001727 }
1728
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001729 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001730 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001731 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001733 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001734 if (!cmd)
1735 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1736 (unsigned char *)hdr);
1737
Nicholas Bellingerba159912013-07-03 03:48:24 -07001738 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1739 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001740 }
1741
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001742 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001744 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1746 payload_length);
1747 /*
1748 * This is not a response to a Unsolicited NopIN, which means
1749 * it can either be a NOPOUT ping request (with a valid ITT),
1750 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1751 * Either way, make sure we allocate an struct iscsi_cmd, as both
1752 * can contain ping data.
1753 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001754 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1756 cmd->i_state = ISTATE_SEND_NOPIN;
1757 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1758 1 : 0);
1759 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1760 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001761 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1762 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001763 cmd->data_direction = DMA_NONE;
1764 }
1765
Nicholas Bellinger778de362013-06-14 16:07:47 -07001766 return 0;
1767}
1768EXPORT_SYMBOL(iscsit_setup_nop_out);
1769
1770int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1771 struct iscsi_nopout *hdr)
1772{
1773 struct iscsi_cmd *cmd_p = NULL;
1774 int cmdsn_ret = 0;
1775 /*
1776 * Initiator is expecting a NopIN ping reply..
1777 */
1778 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001779 if (!cmd)
1780 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1781 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001782
1783 spin_lock_bh(&conn->cmd_lock);
1784 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1785 spin_unlock_bh(&conn->cmd_lock);
1786
1787 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1788
1789 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1790 iscsit_add_cmd_to_response_queue(cmd, conn,
1791 cmd->i_state);
1792 return 0;
1793 }
1794
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001795 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1796 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001797 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1798 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001799 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001800 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001801
1802 return 0;
1803 }
1804 /*
1805 * This was a response to a unsolicited NOPIN ping.
1806 */
1807 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1808 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1809 if (!cmd_p)
1810 return -EINVAL;
1811
1812 iscsit_stop_nopin_response_timer(conn);
1813
1814 cmd_p->i_state = ISTATE_REMOVE;
1815 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1816
1817 iscsit_start_nopin_timer(conn);
1818 return 0;
1819 }
1820 /*
1821 * Otherwise, initiator is not expecting a NOPIN is response.
1822 * Just ignore for now.
1823 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301824
1825 if (cmd)
1826 iscsit_free_cmd(cmd, false);
1827
Nicholas Bellinger778de362013-06-14 16:07:47 -07001828 return 0;
1829}
1830EXPORT_SYMBOL(iscsit_process_nop_out);
1831
1832static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1833 unsigned char *buf)
1834{
1835 unsigned char *ping_data = NULL;
1836 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1837 struct kvec *iov = NULL;
1838 u32 payload_length = ntoh24(hdr->dlength);
1839 int ret;
1840
1841 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1842 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001843 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001844 /*
1845 * Handle NOP-OUT payload for traditional iSCSI sockets
1846 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001847 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001848 u32 checksum, data_crc, padding = 0;
1849 int niov = 0, rx_got, rx_size = payload_length;
1850
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1852 if (!ping_data) {
1853 pr_err("Unable to allocate memory for"
1854 " NOPOUT ping data.\n");
1855 ret = -1;
1856 goto out;
1857 }
1858
1859 iov = &cmd->iov_misc[0];
1860 iov[niov].iov_base = ping_data;
1861 iov[niov++].iov_len = payload_length;
1862
1863 padding = ((-payload_length) & 3);
1864 if (padding != 0) {
1865 pr_debug("Receiving %u additional bytes"
1866 " for padding.\n", padding);
1867 iov[niov].iov_base = &cmd->pad_bytes;
1868 iov[niov++].iov_len = padding;
1869 rx_size += padding;
1870 }
1871 if (conn->conn_ops->DataDigest) {
1872 iov[niov].iov_base = &checksum;
1873 iov[niov++].iov_len = ISCSI_CRC_LEN;
1874 rx_size += ISCSI_CRC_LEN;
1875 }
1876
1877 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1878 if (rx_got != rx_size) {
1879 ret = -1;
1880 goto out;
1881 }
1882
1883 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08001884 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001885 ping_data, payload_length,
1886 padding, cmd->pad_bytes,
1887 (u8 *)&data_crc);
1888
1889 if (checksum != data_crc) {
1890 pr_err("Ping data CRC32C DataDigest"
1891 " 0x%08x does not match computed 0x%08x\n",
1892 checksum, data_crc);
1893 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1894 pr_err("Unable to recover from"
1895 " NOPOUT Ping DataCRC failure while in"
1896 " ERL=0.\n");
1897 ret = -1;
1898 goto out;
1899 } else {
1900 /*
1901 * Silently drop this PDU and let the
1902 * initiator plug the CmdSN gap.
1903 */
1904 pr_debug("Dropping NOPOUT"
1905 " Command CmdSN: 0x%08x due to"
1906 " DataCRC error.\n", hdr->cmdsn);
1907 ret = 0;
1908 goto out;
1909 }
1910 } else {
1911 pr_debug("Got CRC32C DataDigest"
1912 " 0x%08x for %u bytes of ping data.\n",
1913 checksum, payload_length);
1914 }
1915 }
1916
1917 ping_data[payload_length] = '\0';
1918 /*
1919 * Attach ping data to struct iscsi_cmd->buf_ptr.
1920 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001921 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001922 cmd->buf_ptr_size = payload_length;
1923
1924 pr_debug("Got %u bytes of NOPOUT ping"
1925 " data.\n", payload_length);
1926 pr_debug("Ping Data: \"%s\"\n", ping_data);
1927 }
1928
Nicholas Bellinger778de362013-06-14 16:07:47 -07001929 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001930out:
1931 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001932 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001933
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001934 kfree(ping_data);
1935 return ret;
1936}
1937
Bart Van Asschee381fe92017-01-06 11:32:08 +01001938static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1939{
1940 switch (iscsi_tmf) {
1941 case ISCSI_TM_FUNC_ABORT_TASK:
1942 return TMR_ABORT_TASK;
1943 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1944 return TMR_ABORT_TASK_SET;
1945 case ISCSI_TM_FUNC_CLEAR_ACA:
1946 return TMR_CLEAR_ACA;
1947 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1948 return TMR_CLEAR_TASK_SET;
1949 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1950 return TMR_LUN_RESET;
1951 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1952 return TMR_TARGET_WARM_RESET;
1953 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1954 return TMR_TARGET_COLD_RESET;
1955 default:
1956 return TMR_UNKNOWN;
1957 }
1958}
1959
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001960int
1961iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1962 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001963{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001964 struct se_tmr_req *se_tmr;
1965 struct iscsi_tmr_req *tmr_req;
1966 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001967 int out_of_order_cmdsn = 0, ret;
1968 bool sess_ref = false;
Bart Van Assche59b69862017-01-05 12:39:57 +01001969 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970
1971 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001972 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1973 function = hdr->flags;
1974
1975 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1976 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1977 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1978 hdr->rtt, hdr->refcmdsn, conn->cid);
1979
1980 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1981 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001982 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001983 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001984 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001985 }
1986
1987 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1988 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1989 pr_err("Task Management Request TASK_REASSIGN not"
1990 " issued as immediate command, bad iSCSI Initiator"
1991 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001992 return iscsit_add_reject_cmd(cmd,
1993 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001994 }
1995 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001996 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1997 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001998
Andy Groverd28b11692012-04-03 15:51:22 -07001999 cmd->data_direction = DMA_NONE;
2000
2001 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
2002 if (!cmd->tmr_req) {
2003 pr_err("Unable to allocate memory for"
2004 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002005 return iscsit_add_reject_cmd(cmd,
2006 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
2007 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002008 }
2009
2010 /*
2011 * TASK_REASSIGN for ERL=2 / connection stays inside of
2012 * LIO-Target $FABRIC_MOD
2013 */
2014 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02002015 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07002016 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08002017 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07002018
Bart Van Asscheafc16602015-04-27 13:52:36 +02002019 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002020 sess_ref = true;
Bart Van Asschee381fe92017-01-06 11:32:08 +01002021 tcm_function = iscsit_convert_tmf(function);
2022 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002023 pr_err("Unknown iSCSI TMR Function:"
2024 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002025 return iscsit_add_reject_cmd(cmd,
2026 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002027 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002028 }
2029 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2030 GFP_KERNEL);
2031 if (ret < 0)
2032 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002033 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002034
Bart Van Assche59b69862017-01-05 12:39:57 +01002035 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002036
2037 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2038 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2039 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2040 cmd->init_task_tag = hdr->itt;
2041 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002042 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2043 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002044 se_tmr = cmd->se_cmd.se_tmr_req;
2045 tmr_req = cmd->tmr_req;
2046 /*
2047 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2048 */
2049 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002050 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2051 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002052 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002053 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2054 goto attach;
2055 }
2056 }
2057
2058 switch (function) {
2059 case ISCSI_TM_FUNC_ABORT_TASK:
2060 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002061 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002062 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002063 break;
2064 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2065 case ISCSI_TM_FUNC_CLEAR_ACA:
2066 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2067 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2068 break;
2069 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2070 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002071 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2072 goto attach;
2073 }
2074 break;
2075 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2076 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002077 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2078 goto attach;
2079 }
2080 break;
2081 case ISCSI_TM_FUNC_TASK_REASSIGN:
2082 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2083 /*
2084 * Perform sanity checks on the ExpDataSN only if the
2085 * TASK_REASSIGN was successful.
2086 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002087 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002088 break;
2089
2090 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002091 return iscsit_add_reject_cmd(cmd,
2092 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002093 break;
2094 default:
2095 pr_err("Unknown TMR function: 0x%02x, protocol"
2096 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002097 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2098 goto attach;
2099 }
2100
2101 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2102 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2103 se_tmr->call_transport = 1;
2104attach:
2105 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002106 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107 spin_unlock_bh(&conn->cmd_lock);
2108
2109 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002110 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002111 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2112 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002113 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002114 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002115 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002116 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002117 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002118 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002119
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002120 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002121 return 0;
2122 /*
2123 * Found the referenced task, send to transport for processing.
2124 */
2125 if (se_tmr->call_transport)
2126 return transport_generic_handle_tmr(&cmd->se_cmd);
2127
2128 /*
2129 * Could not find the referenced LUN, task, or Task Management
2130 * command not authorized or supported. Change state and
2131 * let the tx_thread send the response.
2132 *
2133 * For connection recovery, this is also the default action for
2134 * TMR TASK_REASSIGN.
2135 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002136 if (sess_ref) {
2137 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002138 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002139 }
2140
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002141 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2142 return 0;
2143}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002144EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002145
2146/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002147int
2148iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2149 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002150{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002151 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002152
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002153 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002154 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002155 "greater than MaxXmitDataSegmentLength %u.\n",
2156 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002157 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2158 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002159 }
2160
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002161 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2162 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2163 pr_err("Multi sequence text commands currently not supported\n");
2164 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2165 (unsigned char *)hdr);
2166 }
2167
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002168 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2169 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2170 hdr->exp_statsn, payload_length);
2171
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002172 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2173 cmd->i_state = ISTATE_SEND_TEXTRSP;
2174 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2175 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2176 cmd->targ_xfer_tag = 0xFFFFFFFF;
2177 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2178 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2179 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002180 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002181
2182 return 0;
2183}
2184EXPORT_SYMBOL(iscsit_setup_text_cmd);
2185
2186int
2187iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2188 struct iscsi_text *hdr)
2189{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002190 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002191 int cmdsn_ret;
2192
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002193 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002194 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2195 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2196 pr_err("Unable to locate text_in buffer for sendtargets"
2197 " discovery\n");
2198 goto reject;
2199 }
2200 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002201 }
2202 if (strncmp("SendTargets", text_in, 11) != 0) {
2203 pr_err("Received Text Data that is not"
2204 " SendTargets, cannot continue.\n");
2205 goto reject;
2206 }
2207 text_ptr = strchr(text_in, '=');
2208 if (!text_ptr) {
2209 pr_err("No \"=\" separator found in Text Data,"
2210 " cannot continue.\n");
2211 goto reject;
2212 }
2213 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002214 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002215 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2216 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002217 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002218 } else {
2219 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2220 goto reject;
2221 }
2222
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002223 spin_lock_bh(&conn->cmd_lock);
2224 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2225 spin_unlock_bh(&conn->cmd_lock);
2226
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002227empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002228 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2229
2230 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002231 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2232 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002233 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002234 return -1;
2235
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002236 return 0;
2237 }
2238
2239 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002240
2241reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002242 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2243 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002244}
2245EXPORT_SYMBOL(iscsit_process_text_cmd);
2246
2247static int
2248iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2249 unsigned char *buf)
2250{
2251 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2252 char *text_in = NULL;
2253 u32 payload_length = ntoh24(hdr->dlength);
2254 int rx_size, rc;
2255
2256 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2257 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002258 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002259
2260 rx_size = payload_length;
2261 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002262 u32 checksum = 0, data_crc = 0;
2263 u32 padding = 0, pad_bytes = 0;
2264 int niov = 0, rx_got;
2265 struct kvec iov[3];
2266
2267 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002268 if (!text_in) {
2269 pr_err("Unable to allocate memory for"
2270 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002271 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002273 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002274
2275 memset(iov, 0, 3 * sizeof(struct kvec));
2276 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002277 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002278
2279 padding = ((-payload_length) & 3);
2280 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002281 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002282 iov[niov++].iov_len = padding;
2283 rx_size += padding;
2284 pr_debug("Receiving %u additional bytes"
2285 " for padding.\n", padding);
2286 }
2287 if (conn->conn_ops->DataDigest) {
2288 iov[niov].iov_base = &checksum;
2289 iov[niov++].iov_len = ISCSI_CRC_LEN;
2290 rx_size += ISCSI_CRC_LEN;
2291 }
2292
2293 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002294 if (rx_got != rx_size)
2295 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296
2297 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002298 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002299 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002300 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002301 (u8 *)&data_crc);
2302
2303 if (checksum != data_crc) {
2304 pr_err("Text data CRC32C DataDigest"
2305 " 0x%08x does not match computed"
2306 " 0x%08x\n", checksum, data_crc);
2307 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2308 pr_err("Unable to recover from"
2309 " Text Data digest failure while in"
2310 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002311 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002312 } else {
2313 /*
2314 * Silently drop this PDU and let the
2315 * initiator plug the CmdSN gap.
2316 */
2317 pr_debug("Dropping Text"
2318 " Command CmdSN: 0x%08x due to"
2319 " DataCRC error.\n", hdr->cmdsn);
2320 kfree(text_in);
2321 return 0;
2322 }
2323 } else {
2324 pr_debug("Got CRC32C DataDigest"
2325 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002326 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327 }
2328 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002329 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002330 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002331 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002332 }
2333
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002334 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002335
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002336reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002337 kfree(cmd->text_in_ptr);
2338 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002339 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002340}
2341
2342int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2343{
2344 struct iscsi_conn *conn_p;
2345 struct iscsi_session *sess = conn->sess;
2346
2347 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2348 " for SID: %u.\n", conn->cid, conn->sess->sid);
2349
2350 atomic_set(&sess->session_logout, 1);
2351 atomic_set(&conn->conn_logout_remove, 1);
2352 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2353
2354 iscsit_inc_conn_usage_count(conn);
2355 iscsit_inc_session_usage_count(sess);
2356
2357 spin_lock_bh(&sess->conn_lock);
2358 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2359 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2360 continue;
2361
2362 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2363 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2364 }
2365 spin_unlock_bh(&sess->conn_lock);
2366
2367 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2368
2369 return 0;
2370}
2371
2372int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2373{
2374 struct iscsi_conn *l_conn;
2375 struct iscsi_session *sess = conn->sess;
2376
2377 pr_debug("Received logout request CLOSECONNECTION for CID:"
2378 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2379
2380 /*
2381 * A Logout Request with a CLOSECONNECTION reason code for a CID
2382 * can arrive on a connection with a differing CID.
2383 */
2384 if (conn->cid == cmd->logout_cid) {
2385 spin_lock_bh(&conn->state_lock);
2386 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2387 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2388
2389 atomic_set(&conn->conn_logout_remove, 1);
2390 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2391 iscsit_inc_conn_usage_count(conn);
2392
2393 spin_unlock_bh(&conn->state_lock);
2394 } else {
2395 /*
2396 * Handle all different cid CLOSECONNECTION requests in
2397 * iscsit_logout_post_handler_diffcid() as to give enough
2398 * time for any non immediate command's CmdSN to be
2399 * acknowledged on the connection in question.
2400 *
2401 * Here we simply make sure the CID is still around.
2402 */
2403 l_conn = iscsit_get_conn_from_cid(sess,
2404 cmd->logout_cid);
2405 if (!l_conn) {
2406 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2407 iscsit_add_cmd_to_response_queue(cmd, conn,
2408 cmd->i_state);
2409 return 0;
2410 }
2411
2412 iscsit_dec_conn_usage_count(l_conn);
2413 }
2414
2415 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2416
2417 return 0;
2418}
2419
2420int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2421{
2422 struct iscsi_session *sess = conn->sess;
2423
2424 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2425 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2426
2427 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2428 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2429 " while ERL!=2.\n");
2430 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2431 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2432 return 0;
2433 }
2434
2435 if (conn->cid == cmd->logout_cid) {
2436 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2437 " with CID: %hu on CID: %hu, implementation error.\n",
2438 cmd->logout_cid, conn->cid);
2439 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2440 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2441 return 0;
2442 }
2443
2444 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2445
2446 return 0;
2447}
2448
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002449int
2450iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2451 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002452{
2453 int cmdsn_ret, logout_remove = 0;
2454 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002455 struct iscsi_logout *hdr;
2456 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2457
2458 hdr = (struct iscsi_logout *) buf;
2459 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002460
2461 if (tiqn) {
2462 spin_lock(&tiqn->logout_stats.lock);
2463 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2464 tiqn->logout_stats.normal_logouts++;
2465 else
2466 tiqn->logout_stats.abnormal_logouts++;
2467 spin_unlock(&tiqn->logout_stats.lock);
2468 }
2469
2470 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2471 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2472 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2473 hdr->cid, conn->cid);
2474
2475 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2476 pr_err("Received logout request on connection that"
2477 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002478 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002479 return 0;
2480 }
2481
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2483 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2484 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2485 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2486 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002487 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2488 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2489 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002490 cmd->logout_reason = reason_code;
2491 cmd->data_direction = DMA_NONE;
2492
2493 /*
2494 * We need to sleep in these cases (by returning 1) until the Logout
2495 * Response gets sent in the tx thread.
2496 */
2497 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2498 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002499 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002500 logout_remove = 1;
2501
2502 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002503 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002504 spin_unlock_bh(&conn->cmd_lock);
2505
2506 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002507 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002508
2509 /*
2510 * Immediate commands are executed, well, immediately.
2511 * Non-Immediate Logout Commands are executed in CmdSN order.
2512 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002513 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002514 int ret = iscsit_execute_cmd(cmd, 0);
2515
2516 if (ret < 0)
2517 return ret;
2518 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002519 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002520 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002521 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002522 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2523 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002524 }
2525
2526 return logout_remove;
2527}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002528EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002529
Varun Prakashd2faaef2016-04-20 00:00:19 +05302530int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002531 struct iscsi_conn *conn,
2532 unsigned char *buf)
2533{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002534 struct iscsi_snack *hdr;
2535
2536 hdr = (struct iscsi_snack *) buf;
2537 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002538
2539 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2540 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2541 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2542 hdr->begrun, hdr->runlength, conn->cid);
2543
2544 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2545 pr_err("Initiator sent SNACK request while in"
2546 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002547 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2548 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002549 }
2550 /*
2551 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2552 * call from inside iscsi_send_recovery_datain_or_r2t().
2553 */
2554 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2555 case 0:
2556 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002557 hdr->itt,
2558 be32_to_cpu(hdr->ttt),
2559 be32_to_cpu(hdr->begrun),
2560 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002561 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002562 return iscsit_handle_status_snack(conn, hdr->itt,
2563 be32_to_cpu(hdr->ttt),
2564 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002565 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002566 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2567 be32_to_cpu(hdr->begrun),
2568 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002569 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2570 /* FIXME: Support R-Data SNACK */
2571 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002572 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2573 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002574 default:
2575 pr_err("Unknown SNACK type 0x%02x, protocol"
2576 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002577 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2578 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002579 }
2580
2581 return 0;
2582}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302583EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002584
2585static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2586{
2587 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2588 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2589 wait_for_completion_interruptible_timeout(
2590 &conn->rx_half_close_comp,
2591 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2592 }
2593}
2594
2595static int iscsit_handle_immediate_data(
2596 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002597 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002598 u32 length)
2599{
2600 int iov_ret, rx_got = 0, rx_size = 0;
2601 u32 checksum, iov_count = 0, padding = 0;
2602 struct iscsi_conn *conn = cmd->conn;
2603 struct kvec *iov;
2604
2605 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2606 if (iov_ret < 0)
2607 return IMMEDIATE_DATA_CANNOT_RECOVER;
2608
2609 rx_size = length;
2610 iov_count = iov_ret;
2611 iov = &cmd->iov_data[0];
2612
2613 padding = ((-length) & 3);
2614 if (padding != 0) {
2615 iov[iov_count].iov_base = cmd->pad_bytes;
2616 iov[iov_count++].iov_len = padding;
2617 rx_size += padding;
2618 }
2619
2620 if (conn->conn_ops->DataDigest) {
2621 iov[iov_count].iov_base = &checksum;
2622 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2623 rx_size += ISCSI_CRC_LEN;
2624 }
2625
2626 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2627
2628 iscsit_unmap_iovec(cmd);
2629
2630 if (rx_got != rx_size) {
2631 iscsit_rx_thread_wait_for_tcp(conn);
2632 return IMMEDIATE_DATA_CANNOT_RECOVER;
2633 }
2634
2635 if (conn->conn_ops->DataDigest) {
2636 u32 data_crc;
2637
Herbert Xu69110e32016-01-24 21:19:52 +08002638 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002639 cmd->write_data_done, length, padding,
2640 cmd->pad_bytes);
2641
2642 if (checksum != data_crc) {
2643 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2644 " does not match computed 0x%08x\n", checksum,
2645 data_crc);
2646
2647 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2648 pr_err("Unable to recover from"
2649 " Immediate Data digest failure while"
2650 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002651 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002652 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002653 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654 return IMMEDIATE_DATA_CANNOT_RECOVER;
2655 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002656 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002657 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002658 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002659 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2660 }
2661 } else {
2662 pr_debug("Got CRC32C DataDigest 0x%08x for"
2663 " %u bytes of Immediate Data\n", checksum,
2664 length);
2665 }
2666 }
2667
2668 cmd->write_data_done += length;
2669
Andy Groverebf1d952012-04-03 15:51:24 -07002670 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002671 spin_lock_bh(&cmd->istate_lock);
2672 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2673 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2674 spin_unlock_bh(&cmd->istate_lock);
2675 }
2676
2677 return IMMEDIATE_DATA_NORMAL_OPERATION;
2678}
2679
2680/*
2681 * Called with sess->conn_lock held.
2682 */
2683/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2684 with active network interface */
2685static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2686{
2687 struct iscsi_cmd *cmd;
2688 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002689 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002690
2691 /*
2692 * Only send a Asynchronous Message on connections whos network
2693 * interface is still functional.
2694 */
2695 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2696 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2697 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002698 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002699 break;
2700 }
2701 }
2702
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002703 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002704 return;
2705
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002706 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002707 if (!cmd) {
2708 iscsit_dec_conn_usage_count(conn_p);
2709 return;
2710 }
2711
2712 cmd->logout_cid = conn->cid;
2713 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2714 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2715
2716 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002717 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002718 spin_unlock_bh(&conn_p->cmd_lock);
2719
2720 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2721 iscsit_dec_conn_usage_count(conn_p);
2722}
2723
2724static int iscsit_send_conn_drop_async_message(
2725 struct iscsi_cmd *cmd,
2726 struct iscsi_conn *conn)
2727{
2728 struct iscsi_async *hdr;
2729
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002730 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2731
2732 hdr = (struct iscsi_async *) cmd->pdu;
2733 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2734 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002735 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002736 cmd->targ_xfer_tag = 0xFFFFFFFF;
2737 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2738 cmd->stat_sn = conn->stat_sn++;
2739 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2740 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002741 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002742 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2743 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2744 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2745 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2746
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002747 pr_debug("Sending Connection Dropped Async Message StatSN:"
2748 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2749 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302750
2751 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002752}
2753
Andy Grover6f3c0e62012-04-03 15:51:09 -07002754static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2755{
2756 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2757 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2758 wait_for_completion_interruptible_timeout(
2759 &conn->tx_half_close_comp,
2760 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2761 }
2762}
2763
Varun Prakashd2faaef2016-04-20 00:00:19 +05302764void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002765iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2766 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2767 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002768{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002769 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2770 hdr->flags = datain->flags;
2771 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2772 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2773 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2774 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2775 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2776 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2777 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2778 }
2779 }
2780 hton24(hdr->dlength, datain->length);
2781 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2782 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2783 (struct scsi_lun *)&hdr->lun);
2784 else
2785 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2786
2787 hdr->itt = cmd->init_task_tag;
2788
2789 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2790 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2791 else
2792 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2793 if (set_statsn)
2794 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2795 else
2796 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2797
2798 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002799 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002800 hdr->datasn = cpu_to_be32(datain->data_sn);
2801 hdr->offset = cpu_to_be32(datain->offset);
2802
2803 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2804 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2805 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2806 ntohl(hdr->offset), datain->length, conn->cid);
2807}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302808EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002809
2810static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2811{
2812 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002813 struct iscsi_datain datain;
2814 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302815 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002816 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002817
2818 memset(&datain, 0, sizeof(struct iscsi_datain));
2819 dr = iscsit_get_datain_values(cmd, &datain);
2820 if (!dr) {
2821 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2822 cmd->init_task_tag);
2823 return -1;
2824 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002825 /*
2826 * Be paranoid and double check the logic for now.
2827 */
Andy Groverebf1d952012-04-03 15:51:24 -07002828 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002829 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2830 " datain.length: %u exceeds cmd->data_length: %u\n",
2831 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002832 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002833 return -1;
2834 }
2835
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002836 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002837 /*
2838 * Special case for successfully execution w/ both DATAIN
2839 * and Sense Data.
2840 */
2841 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2842 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2843 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2844 else {
2845 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2846 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2847 iscsit_increment_maxcmdsn(cmd, conn->sess);
2848 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002849 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002851 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2852 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002853 }
2854
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002855 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002856
Varun Prakash2854bb22016-04-20 00:00:08 +05302857 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2858 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002859 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002860
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002861 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002862 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002863 2 : 1;
2864 iscsit_free_datain_req(cmd, dr);
2865 }
2866
Andy Grover6f3c0e62012-04-03 15:51:09 -07002867 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002868}
2869
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002870int
2871iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2872 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002873{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002874 struct iscsi_conn *logout_conn = NULL;
2875 struct iscsi_conn_recovery *cr = NULL;
2876 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002877 /*
2878 * The actual shutting down of Sessions and/or Connections
2879 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2880 * is done in scsi_logout_post_handler().
2881 */
2882 switch (cmd->logout_reason) {
2883 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2884 pr_debug("iSCSI session logout successful, setting"
2885 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2886 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2887 break;
2888 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2889 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2890 break;
2891 /*
2892 * For CLOSECONNECTION logout requests carrying
2893 * a matching logout CID -> local CID, the reference
2894 * for the local CID will have been incremented in
2895 * iscsi_logout_closeconnection().
2896 *
2897 * For CLOSECONNECTION logout requests carrying
2898 * a different CID than the connection it arrived
2899 * on, the connection responding to cmd->logout_cid
2900 * is stopped in iscsit_logout_post_handler_diffcid().
2901 */
2902
2903 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2904 " successful.\n", cmd->logout_cid, conn->cid);
2905 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2906 break;
2907 case ISCSI_LOGOUT_REASON_RECOVERY:
2908 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2909 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2910 break;
2911 /*
2912 * If the connection is still active from our point of view
2913 * force connection recovery to occur.
2914 */
2915 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2916 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002917 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002918 iscsit_connection_reinstatement_rcfr(logout_conn);
2919 iscsit_dec_conn_usage_count(logout_conn);
2920 }
2921
2922 cr = iscsit_get_inactive_connection_recovery_entry(
2923 conn->sess, cmd->logout_cid);
2924 if (!cr) {
2925 pr_err("Unable to locate CID: %hu for"
2926 " REMOVECONNFORRECOVERY Logout Request.\n",
2927 cmd->logout_cid);
2928 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2929 break;
2930 }
2931
2932 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2933
2934 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2935 " for recovery for CID: %hu on CID: %hu successful.\n",
2936 cmd->logout_cid, conn->cid);
2937 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2938 break;
2939 default:
2940 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2941 cmd->logout_reason);
2942 return -1;
2943 }
2944
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002945 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2946 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2947 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002948 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002949 cmd->stat_sn = conn->stat_sn++;
2950 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2951
2952 iscsit_increment_maxcmdsn(cmd, conn->sess);
2953 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002954 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002955
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002956 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2957 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2958 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2959 cmd->logout_cid, conn->cid);
2960
2961 return 0;
2962}
2963EXPORT_SYMBOL(iscsit_build_logout_rsp);
2964
2965static int
2966iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2967{
Varun Prakash2854bb22016-04-20 00:00:08 +05302968 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002969
2970 rc = iscsit_build_logout_rsp(cmd, conn,
2971 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2972 if (rc < 0)
2973 return rc;
2974
Varun Prakash2854bb22016-04-20 00:00:08 +05302975 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002976}
2977
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002978void
2979iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2980 struct iscsi_nopin *hdr, bool nopout_response)
2981{
2982 hdr->opcode = ISCSI_OP_NOOP_IN;
2983 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2984 hton24(hdr->dlength, cmd->buf_ptr_size);
2985 if (nopout_response)
2986 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2987 hdr->itt = cmd->init_task_tag;
2988 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2989 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2990 conn->stat_sn;
2991 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2992
2993 if (nopout_response)
2994 iscsit_increment_maxcmdsn(cmd, conn->sess);
2995
2996 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002997 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002998
2999 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
3000 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01003001 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003002 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
3003}
3004EXPORT_SYMBOL(iscsit_build_nopin_rsp);
3005
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003006/*
3007 * Unsolicited NOPIN, either requesting a response or not.
3008 */
3009static int iscsit_send_unsolicited_nopin(
3010 struct iscsi_cmd *cmd,
3011 struct iscsi_conn *conn,
3012 int want_response)
3013{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003014 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05303015 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003016
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003017 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003018
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003019 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3020 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3021
Varun Prakash2854bb22016-04-20 00:00:08 +05303022 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3023 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003024 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003025
3026 spin_lock_bh(&cmd->istate_lock);
3027 cmd->i_state = want_response ?
3028 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3029 spin_unlock_bh(&cmd->istate_lock);
3030
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003031 return 0;
3032}
3033
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003034static int
3035iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003036{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003037 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003038
3039 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003040
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003041 /*
3042 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3043 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3044 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303045 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003046
Varun Prakash2854bb22016-04-20 00:00:08 +05303047 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3048 cmd->buf_ptr,
3049 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003050}
3051
Andy Grover6f3c0e62012-04-03 15:51:09 -07003052static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003053 struct iscsi_cmd *cmd,
3054 struct iscsi_conn *conn)
3055{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003056 struct iscsi_r2t *r2t;
3057 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003058 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003059
3060 r2t = iscsit_get_r2t_from_list(cmd);
3061 if (!r2t)
3062 return -1;
3063
3064 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3065 memset(hdr, 0, ISCSI_HDR_LEN);
3066 hdr->opcode = ISCSI_OP_R2T;
3067 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3068 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3069 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003070 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303071 if (conn->conn_transport->iscsit_get_r2t_ttt)
3072 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3073 else
3074 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003075 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3076 hdr->statsn = cpu_to_be32(conn->stat_sn);
3077 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003078 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003079 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3080 hdr->data_offset = cpu_to_be32(r2t->offset);
3081 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3082
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003083 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3084 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3085 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3086 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3087 r2t->offset, r2t->xfer_len, conn->cid);
3088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003089 spin_lock_bh(&cmd->r2t_lock);
3090 r2t->sent_r2t = 1;
3091 spin_unlock_bh(&cmd->r2t_lock);
3092
Varun Prakash2854bb22016-04-20 00:00:08 +05303093 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003094 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003095 return ret;
3096 }
3097
3098 spin_lock_bh(&cmd->dataout_timeout_lock);
3099 iscsit_start_dataout_timer(cmd, conn);
3100 spin_unlock_bh(&cmd->dataout_timeout_lock);
3101
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003102 return 0;
3103}
3104
3105/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003106 * @recovery: If called from iscsi_task_reassign_complete_write() for
3107 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108 */
3109int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003110 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003111 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003112 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003113{
3114 int first_r2t = 1;
3115 u32 offset = 0, xfer_len = 0;
3116
3117 spin_lock_bh(&cmd->r2t_lock);
3118 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3119 spin_unlock_bh(&cmd->r2t_lock);
3120 return 0;
3121 }
3122
Andy Grover8b1e1242012-04-03 15:51:12 -07003123 if (conn->sess->sess_ops->DataSequenceInOrder &&
3124 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003125 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003126
3127 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3128 if (conn->sess->sess_ops->DataSequenceInOrder) {
3129 offset = cmd->r2t_offset;
3130
Andy Grover8b1e1242012-04-03 15:51:12 -07003131 if (first_r2t && recovery) {
3132 int new_data_end = offset +
3133 conn->sess->sess_ops->MaxBurstLength -
3134 cmd->next_burst_len;
3135
Andy Groverebf1d952012-04-03 15:51:24 -07003136 if (new_data_end > cmd->se_cmd.data_length)
3137 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003138 else
3139 xfer_len =
3140 conn->sess->sess_ops->MaxBurstLength -
3141 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003142 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003143 int new_data_end = offset +
3144 conn->sess->sess_ops->MaxBurstLength;
3145
Andy Groverebf1d952012-04-03 15:51:24 -07003146 if (new_data_end > cmd->se_cmd.data_length)
3147 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003148 else
3149 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003150 }
3151 cmd->r2t_offset += xfer_len;
3152
Andy Groverebf1d952012-04-03 15:51:24 -07003153 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003154 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3155 } else {
3156 struct iscsi_seq *seq;
3157
3158 seq = iscsit_get_seq_holder_for_r2t(cmd);
3159 if (!seq) {
3160 spin_unlock_bh(&cmd->r2t_lock);
3161 return -1;
3162 }
3163
3164 offset = seq->offset;
3165 xfer_len = seq->xfer_len;
3166
3167 if (cmd->seq_send_order == cmd->seq_count)
3168 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3169 }
3170 cmd->outstanding_r2ts++;
3171 first_r2t = 0;
3172
3173 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3174 spin_unlock_bh(&cmd->r2t_lock);
3175 return -1;
3176 }
3177
3178 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3179 break;
3180 }
3181 spin_unlock_bh(&cmd->r2t_lock);
3182
3183 return 0;
3184}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303185EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003187void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3188 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003189{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003190 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191 cmd->stat_sn = conn->stat_sn++;
3192
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003193 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003194
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003195 memset(hdr, 0, ISCSI_HDR_LEN);
3196 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3197 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3198 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3199 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003200 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003201 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3202 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003203 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003204 }
3205 hdr->response = cmd->iscsi_response;
3206 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003207 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003208 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3209
3210 iscsit_increment_maxcmdsn(cmd, conn->sess);
3211 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003212 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003213
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003214 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3215 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3216 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3217 cmd->se_cmd.scsi_status, conn->cid);
3218}
3219EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3220
3221static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3222{
3223 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003224 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303225 void *data_buf = NULL;
3226 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003227
3228 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3229
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003230 /*
3231 * Attach SENSE DATA payload to iSCSI Response PDU
3232 */
3233 if (cmd->se_cmd.sense_buffer &&
3234 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3235 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003236 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3237 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3238
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003239 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003240 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303241 data_buf = cmd->sense_buffer;
3242 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003243
3244 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003245 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003246 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003247 pr_debug("Adding %u bytes of padding to"
3248 " SENSE.\n", padding);
3249 }
3250
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003251 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3252 " Response PDU\n",
3253 cmd->se_cmd.scsi_sense_length);
3254 }
3255
Varun Prakash2854bb22016-04-20 00:00:08 +05303256 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3257 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003258}
3259
3260static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3261{
3262 switch (se_tmr->response) {
3263 case TMR_FUNCTION_COMPLETE:
3264 return ISCSI_TMF_RSP_COMPLETE;
3265 case TMR_TASK_DOES_NOT_EXIST:
3266 return ISCSI_TMF_RSP_NO_TASK;
3267 case TMR_LUN_DOES_NOT_EXIST:
3268 return ISCSI_TMF_RSP_NO_LUN;
3269 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3270 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003271 case TMR_FUNCTION_REJECTED:
3272 default:
3273 return ISCSI_TMF_RSP_REJECTED;
3274 }
3275}
3276
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003277void
3278iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3279 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003280{
3281 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003282
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003283 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003284 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003285 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003286 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003287 cmd->stat_sn = conn->stat_sn++;
3288 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3289
3290 iscsit_increment_maxcmdsn(cmd, conn->sess);
3291 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003292 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003293
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003294 pr_debug("Built Task Management Response ITT: 0x%08x,"
3295 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3296 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3297}
3298EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3299
3300static int
3301iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3302{
3303 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003304
3305 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3306
Varun Prakash2854bb22016-04-20 00:00:08 +05303307 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003308}
3309
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003310static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3311{
3312 bool ret = false;
3313
3314 if (np->np_sockaddr.ss_family == AF_INET6) {
3315 const struct sockaddr_in6 sin6 = {
3316 .sin6_addr = IN6ADDR_ANY_INIT };
3317 struct sockaddr_in6 *sock_in6 =
3318 (struct sockaddr_in6 *)&np->np_sockaddr;
3319
3320 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3321 sin6.sin6_addr.s6_addr, 16))
3322 ret = true;
3323 } else {
3324 struct sockaddr_in * sock_in =
3325 (struct sockaddr_in *)&np->np_sockaddr;
3326
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003327 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003328 ret = true;
3329 }
3330
3331 return ret;
3332}
3333
Andy Grover8b1e1242012-04-03 15:51:12 -07003334#define SENDTARGETS_BUF_LIMIT 32768U
3335
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003336static int
3337iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003338 enum iscsit_transport_type network_transport,
3339 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003340{
3341 char *payload = NULL;
3342 struct iscsi_conn *conn = cmd->conn;
3343 struct iscsi_portal_group *tpg;
3344 struct iscsi_tiqn *tiqn;
3345 struct iscsi_tpg_np *tpg_np;
3346 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003347 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003348 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003349 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003350 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003351
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003352 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003353 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003354
3355 payload = kzalloc(buffer_len, GFP_KERNEL);
3356 if (!payload) {
3357 pr_err("Unable to allocate memory for sendtargets"
3358 " response.\n");
3359 return -ENOMEM;
3360 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003361 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003362 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003363 * explicit case..
3364 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003365 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003366 text_ptr = strchr(text_in, '=');
3367 if (!text_ptr) {
3368 pr_err("Unable to locate '=' string in text_in:"
3369 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003370 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003371 return -EINVAL;
3372 }
3373 /*
3374 * Skip over '=' character..
3375 */
3376 text_ptr += 1;
3377 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003378
3379 spin_lock(&tiqn_lock);
3380 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003381 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003382 strcmp(tiqn->tiqn, text_ptr)) {
3383 continue;
3384 }
3385
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003386 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003387
3388 spin_lock(&tiqn->tiqn_tpg_lock);
3389 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3390
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003391 /* If demo_mode_discovery=0 and generate_node_acls=0
3392 * (demo mode dislabed) do not return
3393 * TargetName+TargetAddress unless a NodeACL exists.
3394 */
3395
3396 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3397 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003398 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003399 cmd->conn->sess->sess_ops->InitiatorName))) {
3400 continue;
3401 }
3402
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003403 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003404 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003405 spin_unlock(&tpg->tpg_state_lock);
3406
David Disseldorpa6415cd2015-08-01 00:10:12 -07003407 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3408 continue;
3409
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003410 spin_lock(&tpg->tpg_np_lock);
3411 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3412 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003413 struct iscsi_np *np = tpg_np->tpg_np;
3414 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003415 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003416
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003417 if (np->np_network_transport != network_transport)
3418 continue;
3419
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003420 if (!target_name_printed) {
3421 len = sprintf(buf, "TargetName=%s",
3422 tiqn->tiqn);
3423 len += 1;
3424
3425 if ((len + payload_len) > buffer_len) {
3426 spin_unlock(&tpg->tpg_np_lock);
3427 spin_unlock(&tiqn->tiqn_tpg_lock);
3428 end_of_buf = 1;
3429 goto eob;
3430 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003431
3432 if (skip_bytes && len <= skip_bytes) {
3433 skip_bytes -= len;
3434 } else {
3435 memcpy(payload + payload_len, buf, len);
3436 payload_len += len;
3437 target_name_printed = 1;
3438 if (len > skip_bytes)
3439 skip_bytes = 0;
3440 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003441 }
3442
Andy Grover69d75572015-08-24 10:26:04 -07003443 if (inaddr_any)
3444 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003445 else
Andy Grover69d75572015-08-24 10:26:04 -07003446 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003447
Andy Grover69d75572015-08-24 10:26:04 -07003448 len = sprintf(buf, "TargetAddress="
3449 "%pISpc,%hu",
3450 sockaddr,
3451 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003452 len += 1;
3453
3454 if ((len + payload_len) > buffer_len) {
3455 spin_unlock(&tpg->tpg_np_lock);
3456 spin_unlock(&tiqn->tiqn_tpg_lock);
3457 end_of_buf = 1;
3458 goto eob;
3459 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003460
3461 if (skip_bytes && len <= skip_bytes) {
3462 skip_bytes -= len;
3463 } else {
3464 memcpy(payload + payload_len, buf, len);
3465 payload_len += len;
3466 if (len > skip_bytes)
3467 skip_bytes = 0;
3468 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003469 }
3470 spin_unlock(&tpg->tpg_np_lock);
3471 }
3472 spin_unlock(&tiqn->tiqn_tpg_lock);
3473eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003474 if (end_of_buf) {
3475 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003476 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003477 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003478
Andy Grover8060b8d2015-01-09 15:13:08 -08003479 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003480 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003481 }
3482 spin_unlock(&tiqn_lock);
3483
3484 cmd->buf_ptr = payload;
3485
3486 return payload_len;
3487}
3488
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003489int
3490iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003491 struct iscsi_text_rsp *hdr,
3492 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003493{
3494 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003495 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003496
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003497 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3498 cmd->read_data_done,
3499 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003500 if (text_length < 0)
3501 return text_length;
3502
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003503 if (completed) {
3504 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3505 } else {
3506 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3507 cmd->read_data_done += text_length;
3508 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3509 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3510 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003511 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003512 padding = ((-text_length) & 3);
3513 hton24(hdr->dlength, text_length);
3514 hdr->itt = cmd->init_task_tag;
3515 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3516 cmd->stat_sn = conn->stat_sn++;
3517 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3518
3519 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003520 /*
3521 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3522 * correctly increment MaxCmdSN for each response answering a
3523 * non immediate text request with a valid CmdSN.
3524 */
3525 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003526 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003527 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003528
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003529 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3530 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3531 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3532 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3533 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003534
3535 return text_length + padding;
3536}
3537EXPORT_SYMBOL(iscsit_build_text_rsp);
3538
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003539static int iscsit_send_text_rsp(
3540 struct iscsi_cmd *cmd,
3541 struct iscsi_conn *conn)
3542{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003543 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303544 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003545
Varun Prakash864e5042016-04-20 00:00:15 +05303546 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3547 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303548 if (text_length < 0)
3549 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003550
Varun Prakash2854bb22016-04-20 00:00:08 +05303551 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3552 cmd->buf_ptr,
3553 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003554}
3555
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003556void
3557iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3558 struct iscsi_reject *hdr)
3559{
3560 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003561 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003562 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3563 hton24(hdr->dlength, ISCSI_HDR_LEN);
3564 hdr->ffffffff = cpu_to_be32(0xffffffff);
3565 cmd->stat_sn = conn->stat_sn++;
3566 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3567 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003568 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003569
3570}
3571EXPORT_SYMBOL(iscsit_build_reject);
3572
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003573static int iscsit_send_reject(
3574 struct iscsi_cmd *cmd,
3575 struct iscsi_conn *conn)
3576{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003577 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003578
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003579 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003580
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003581 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3582 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3583
Varun Prakash2854bb22016-04-20 00:00:08 +05303584 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3585 cmd->buf_ptr,
3586 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003587}
3588
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003589void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3590{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003591 int ord, cpu;
3592 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003593 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3594 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003595 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003596 * Here we use bitmap_id to determine which CPU that this
3597 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003598 * execute upon.
3599 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003600 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003601 for_each_online_cpu(cpu) {
3602 if (ord-- == 0) {
3603 cpumask_set_cpu(cpu, conn->conn_cpumask);
3604 return;
3605 }
3606 }
3607 /*
3608 * This should never be reached..
3609 */
3610 dump_stack();
3611 cpumask_setall(conn->conn_cpumask);
3612}
3613
Varun Prakashd2faaef2016-04-20 00:00:19 +05303614int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003615iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003616{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003617 int ret;
3618
3619 switch (state) {
3620 case ISTATE_SEND_R2T:
3621 ret = iscsit_send_r2t(cmd, conn);
3622 if (ret < 0)
3623 goto err;
3624 break;
3625 case ISTATE_REMOVE:
3626 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003627 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003628 spin_unlock_bh(&conn->cmd_lock);
3629
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003630 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003631 break;
3632 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3633 iscsit_mod_nopin_response_timer(conn);
3634 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3635 if (ret < 0)
3636 goto err;
3637 break;
3638 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3639 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3640 if (ret < 0)
3641 goto err;
3642 break;
3643 default:
3644 pr_err("Unknown Opcode: 0x%02x ITT:"
3645 " 0x%08x, i_state: %d on CID: %hu\n",
3646 cmd->iscsi_opcode, cmd->init_task_tag, state,
3647 conn->cid);
3648 goto err;
3649 }
3650
3651 return 0;
3652
3653err:
3654 return -1;
3655}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303656EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003657
3658static int
3659iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3660{
3661 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003662 struct iscsi_queue_req *qr;
3663 struct iscsi_cmd *cmd;
3664 u8 state;
3665 int ret;
3666
3667 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3668 atomic_set(&conn->check_immediate_queue, 0);
3669 cmd = qr->cmd;
3670 state = qr->state;
3671 kmem_cache_free(lio_qr_cache, qr);
3672
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003673 ret = t->iscsit_immediate_queue(conn, cmd, state);
3674 if (ret < 0)
3675 return ret;
3676 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003677
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003678 return 0;
3679}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003680
Varun Prakashd2faaef2016-04-20 00:00:19 +05303681int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003682iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3683{
3684 int ret;
3685
3686check_rsp_state:
3687 switch (state) {
3688 case ISTATE_SEND_DATAIN:
3689 ret = iscsit_send_datain(cmd, conn);
3690 if (ret < 0)
3691 goto err;
3692 else if (!ret)
3693 /* more drs */
3694 goto check_rsp_state;
3695 else if (ret == 1) {
3696 /* all done */
3697 spin_lock_bh(&cmd->istate_lock);
3698 cmd->i_state = ISTATE_SENT_STATUS;
3699 spin_unlock_bh(&cmd->istate_lock);
3700
3701 if (atomic_read(&conn->check_immediate_queue))
3702 return 1;
3703
3704 return 0;
3705 } else if (ret == 2) {
3706 /* Still must send status,
3707 SCF_TRANSPORT_TASK_SENSE was set */
3708 spin_lock_bh(&cmd->istate_lock);
3709 cmd->i_state = ISTATE_SEND_STATUS;
3710 spin_unlock_bh(&cmd->istate_lock);
3711 state = ISTATE_SEND_STATUS;
3712 goto check_rsp_state;
3713 }
3714
3715 break;
3716 case ISTATE_SEND_STATUS:
3717 case ISTATE_SEND_STATUS_RECOVERY:
3718 ret = iscsit_send_response(cmd, conn);
3719 break;
3720 case ISTATE_SEND_LOGOUTRSP:
3721 ret = iscsit_send_logout(cmd, conn);
3722 break;
3723 case ISTATE_SEND_ASYNCMSG:
3724 ret = iscsit_send_conn_drop_async_message(
3725 cmd, conn);
3726 break;
3727 case ISTATE_SEND_NOPIN:
3728 ret = iscsit_send_nopin(cmd, conn);
3729 break;
3730 case ISTATE_SEND_REJECT:
3731 ret = iscsit_send_reject(cmd, conn);
3732 break;
3733 case ISTATE_SEND_TASKMGTRSP:
3734 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3735 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003736 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003737 ret = iscsit_tmr_post_handler(cmd, conn);
3738 if (ret != 0)
3739 iscsit_fall_back_to_erl0(conn->sess);
3740 break;
3741 case ISTATE_SEND_TEXTRSP:
3742 ret = iscsit_send_text_rsp(cmd, conn);
3743 break;
3744 default:
3745 pr_err("Unknown Opcode: 0x%02x ITT:"
3746 " 0x%08x, i_state: %d on CID: %hu\n",
3747 cmd->iscsi_opcode, cmd->init_task_tag,
3748 state, conn->cid);
3749 goto err;
3750 }
3751 if (ret < 0)
3752 goto err;
3753
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003754 switch (state) {
3755 case ISTATE_SEND_LOGOUTRSP:
3756 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003757 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003758 /* fall through */
3759 case ISTATE_SEND_STATUS:
3760 case ISTATE_SEND_ASYNCMSG:
3761 case ISTATE_SEND_NOPIN:
3762 case ISTATE_SEND_STATUS_RECOVERY:
3763 case ISTATE_SEND_TEXTRSP:
3764 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003765 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003766 spin_lock_bh(&cmd->istate_lock);
3767 cmd->i_state = ISTATE_SENT_STATUS;
3768 spin_unlock_bh(&cmd->istate_lock);
3769 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003770 default:
3771 pr_err("Unknown Opcode: 0x%02x ITT:"
3772 " 0x%08x, i_state: %d on CID: %hu\n",
3773 cmd->iscsi_opcode, cmd->init_task_tag,
3774 cmd->i_state, conn->cid);
3775 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003776 }
3777
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003778 if (atomic_read(&conn->check_immediate_queue))
3779 return 1;
3780
Andy Grover6f3c0e62012-04-03 15:51:09 -07003781 return 0;
3782
3783err:
3784 return -1;
3785}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303786EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003787
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003788static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003789{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003790 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003791 struct iscsi_queue_req *qr;
3792 struct iscsi_cmd *cmd;
3793 u8 state;
3794 int ret;
3795
3796 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3797 cmd = qr->cmd;
3798 state = qr->state;
3799 kmem_cache_free(lio_qr_cache, qr);
3800
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003801 ret = t->iscsit_response_queue(conn, cmd, state);
3802 if (ret == 1 || ret < 0)
3803 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003804 }
3805
3806 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003807}
3808
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003809int iscsi_target_tx_thread(void *arg)
3810{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003811 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003812 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003813 /*
3814 * Allow ourselves to be interrupted by SIGINT so that a
3815 * connection recovery / failure event can be triggered externally.
3816 */
3817 allow_signal(SIGINT);
3818
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003819 while (!kthread_should_stop()) {
3820 /*
3821 * Ensure that both TX and RX per connection kthreads
3822 * are scheduled to run on the same CPU.
3823 */
3824 iscsit_thread_check_cpumask(conn, current, 1);
3825
Roland Dreierd5627ac2012-10-31 09:16:46 -07003826 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003827 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003828
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003829 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003830 goto transport_err;
3831
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003832get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003833 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003834 if (ret < 0)
3835 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003836
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003837 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003838 if (ret == 1)
3839 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003840 else if (ret == -ECONNRESET)
3841 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003842 else if (ret < 0)
3843 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003844 }
3845
3846transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003847 /*
3848 * Avoid the normal connection failure code-path if this connection
3849 * is still within LOGIN mode, and iscsi_np process context is
3850 * responsible for cleaning up the early connection failure.
3851 */
3852 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3853 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003854out:
3855 return 0;
3856}
3857
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003858static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3859{
3860 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3861 struct iscsi_cmd *cmd;
3862 int ret = 0;
3863
3864 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3865 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003866 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003867 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003868 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003869
3870 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3871 break;
3872 case ISCSI_OP_SCSI_DATA_OUT:
3873 ret = iscsit_handle_data_out(conn, buf);
3874 break;
3875 case ISCSI_OP_NOOP_OUT:
3876 cmd = NULL;
3877 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003878 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003879 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003880 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003881 }
3882 ret = iscsit_handle_nop_out(conn, cmd, buf);
3883 break;
3884 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003885 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003886 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003887 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003888
3889 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3890 break;
3891 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003892 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3893 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3894 if (!cmd)
3895 goto reject;
3896 } else {
3897 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3898 if (!cmd)
3899 goto reject;
3900 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003901
3902 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003903 break;
3904 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003905 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003906 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003907 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003908
3909 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3910 if (ret > 0)
3911 wait_for_completion_timeout(&conn->conn_logout_comp,
3912 SECONDS_FOR_LOGOUT_COMP * HZ);
3913 break;
3914 case ISCSI_OP_SNACK:
3915 ret = iscsit_handle_snack(conn, buf);
3916 break;
3917 default:
3918 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3919 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3920 pr_err("Cannot recover from unknown"
3921 " opcode while ERL=0, closing iSCSI connection.\n");
3922 return -1;
3923 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003924 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3925 " closing iSCSI connection.\n");
3926 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003927 break;
3928 }
3929
3930 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003931reject:
3932 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003933}
3934
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003935static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3936{
3937 bool ret;
3938
3939 spin_lock_bh(&conn->state_lock);
3940 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3941 spin_unlock_bh(&conn->state_lock);
3942
3943 return ret;
3944}
3945
Varun Prakashe8205cc2016-04-20 00:00:11 +05303946static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003947{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303948 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003949 u8 buffer[ISCSI_HDR_LEN], opcode;
3950 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003951 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003952
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003953 while (!kthread_should_stop()) {
3954 /*
3955 * Ensure that both TX and RX per connection kthreads
3956 * are scheduled to run on the same CPU.
3957 */
3958 iscsit_thread_check_cpumask(conn, current, 0);
3959
3960 memset(buffer, 0, ISCSI_HDR_LEN);
3961 memset(&iov, 0, sizeof(struct kvec));
3962
3963 iov.iov_base = buffer;
3964 iov.iov_len = ISCSI_HDR_LEN;
3965
3966 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3967 if (ret != ISCSI_HDR_LEN) {
3968 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303969 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003970 }
3971
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003972 if (conn->conn_ops->HeaderDigest) {
3973 iov.iov_base = &digest;
3974 iov.iov_len = ISCSI_CRC_LEN;
3975
3976 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3977 if (ret != ISCSI_CRC_LEN) {
3978 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303979 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003980 }
3981
Herbert Xu69110e32016-01-24 21:19:52 +08003982 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003983 buffer, ISCSI_HDR_LEN,
3984 0, NULL, (u8 *)&checksum);
3985
3986 if (digest != checksum) {
3987 pr_err("HeaderDigest CRC32C failed,"
3988 " received 0x%08x, computed 0x%08x\n",
3989 digest, checksum);
3990 /*
3991 * Set the PDU to 0xff so it will intentionally
3992 * hit default in the switch below.
3993 */
3994 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003995 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003996 } else {
3997 pr_debug("Got HeaderDigest CRC32C"
3998 " 0x%08x\n", checksum);
3999 }
4000 }
4001
4002 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304003 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004004
4005 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4006
4007 if (conn->sess->sess_ops->SessionType &&
4008 ((!(opcode & ISCSI_OP_TEXT)) ||
4009 (!(opcode & ISCSI_OP_LOGOUT)))) {
4010 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4011 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004012 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4013 buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05304014 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004015 }
4016
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004017 ret = iscsi_target_rx_opcode(conn, buffer);
4018 if (ret < 0)
Varun Prakashe8205cc2016-04-20 00:00:11 +05304019 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004020 }
Varun Prakashe8205cc2016-04-20 00:00:11 +05304021}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004022
Varun Prakashe8205cc2016-04-20 00:00:11 +05304023int iscsi_target_rx_thread(void *arg)
4024{
4025 int rc;
4026 struct iscsi_conn *conn = arg;
4027
4028 /*
4029 * Allow ourselves to be interrupted by SIGINT so that a
4030 * connection recovery / failure event can be triggered externally.
4031 */
4032 allow_signal(SIGINT);
4033 /*
4034 * Wait for iscsi_post_login_handler() to complete before allowing
4035 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4036 */
4037 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4038 if (rc < 0 || iscsi_target_check_conn_state(conn))
4039 return 0;
4040
4041 if (!conn->conn_transport->iscsit_get_rx_pdu)
4042 return 0;
4043
4044 conn->conn_transport->iscsit_get_rx_pdu(conn);
4045
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004046 if (!signal_pending(current))
4047 atomic_set(&conn->transport_failed, 1);
4048 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004049 return 0;
4050}
4051
4052static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4053{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004054 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004055 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4056 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004057 /*
4058 * We expect this function to only ever be called from either RX or TX
4059 * thread context via iscsit_close_connection() once the other context
4060 * has been reset -> returned sleeping pre-handler state.
4061 */
4062 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004063 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004064
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004065 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4066 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004067
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004068 if (se_cmd->se_tfo != NULL) {
4069 spin_lock(&se_cmd->t_state_lock);
4070 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4071 spin_unlock(&se_cmd->t_state_lock);
4072 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004073 }
4074 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004075
4076 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4077 list_del_init(&cmd->i_conn_node);
4078
4079 iscsit_increment_maxcmdsn(cmd, sess);
4080 iscsit_free_cmd(cmd, true);
4081
4082 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004083}
4084
4085static void iscsit_stop_timers_for_cmds(
4086 struct iscsi_conn *conn)
4087{
4088 struct iscsi_cmd *cmd;
4089
4090 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004091 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004092 if (cmd->data_direction == DMA_TO_DEVICE)
4093 iscsit_stop_dataout_timer(cmd);
4094 }
4095 spin_unlock_bh(&conn->cmd_lock);
4096}
4097
4098int iscsit_close_connection(
4099 struct iscsi_conn *conn)
4100{
4101 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4102 struct iscsi_session *sess = conn->sess;
4103
4104 pr_debug("Closing iSCSI connection CID %hu on SID:"
4105 " %u\n", conn->cid, sess->sid);
4106 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304107 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4108 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4109 * sleeping and the logout response never got sent because the
4110 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004111 *
4112 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4113 * to signal logout response TX interrupt completion. Go ahead and skip
4114 * this for iser since isert_rx_opcode() does not wait on logout failure,
4115 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004116 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004117 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004118 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004119
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004120 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4121 if (conn->tx_thread &&
4122 cmpxchg(&conn->tx_thread_active, true, false)) {
4123 send_sig(SIGINT, conn->tx_thread, 1);
4124 kthread_stop(conn->tx_thread);
4125 }
4126 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4127 if (conn->rx_thread &&
4128 cmpxchg(&conn->rx_thread_active, true, false)) {
4129 send_sig(SIGINT, conn->rx_thread, 1);
4130 kthread_stop(conn->rx_thread);
4131 }
4132 }
4133
4134 spin_lock(&iscsit_global->ts_bitmap_lock);
4135 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4136 get_order(1));
4137 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004138
4139 iscsit_stop_timers_for_cmds(conn);
4140 iscsit_stop_nopin_response_timer(conn);
4141 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004142
4143 if (conn->conn_transport->iscsit_wait_conn)
4144 conn->conn_transport->iscsit_wait_conn(conn);
4145
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004146 /*
4147 * During Connection recovery drop unacknowledged out of order
4148 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004149 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004150 *
4151 * During normal operation clear the out of order commands (but
4152 * do not free the struct iscsi_ooo_cmdsn's) and release all
4153 * struct iscsi_cmds.
4154 */
4155 if (atomic_read(&conn->connection_recovery)) {
4156 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004157 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004158 } else {
4159 iscsit_clear_ooo_cmdsns_for_conn(conn);
4160 iscsit_release_commands_from_conn(conn);
4161 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004162 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004163
4164 /*
4165 * Handle decrementing session or connection usage count if
4166 * a logout response was not able to be sent because the
4167 * connection failed. Fall back to Session Recovery here.
4168 */
4169 if (atomic_read(&conn->conn_logout_remove)) {
4170 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4171 iscsit_dec_conn_usage_count(conn);
4172 iscsit_dec_session_usage_count(sess);
4173 }
4174 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4175 iscsit_dec_conn_usage_count(conn);
4176
4177 atomic_set(&conn->conn_logout_remove, 0);
4178 atomic_set(&sess->session_reinstatement, 0);
4179 atomic_set(&sess->session_fall_back_to_erl0, 1);
4180 }
4181
4182 spin_lock_bh(&sess->conn_lock);
4183 list_del(&conn->conn_list);
4184
4185 /*
4186 * Attempt to let the Initiator know this connection failed by
4187 * sending an Connection Dropped Async Message on another
4188 * active connection.
4189 */
4190 if (atomic_read(&conn->connection_recovery))
4191 iscsit_build_conn_drop_async_message(conn);
4192
4193 spin_unlock_bh(&sess->conn_lock);
4194
4195 /*
4196 * If connection reinstatement is being performed on this connection,
4197 * up the connection reinstatement semaphore that is being blocked on
4198 * in iscsit_cause_connection_reinstatement().
4199 */
4200 spin_lock_bh(&conn->state_lock);
4201 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4202 spin_unlock_bh(&conn->state_lock);
4203 complete(&conn->conn_wait_comp);
4204 wait_for_completion(&conn->conn_post_wait_comp);
4205 spin_lock_bh(&conn->state_lock);
4206 }
4207
4208 /*
4209 * If connection reinstatement is being performed on this connection
4210 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4211 * connection wait rcfr semaphore that is being blocked on
4212 * an iscsit_connection_reinstatement_rcfr().
4213 */
4214 if (atomic_read(&conn->connection_wait_rcfr)) {
4215 spin_unlock_bh(&conn->state_lock);
4216 complete(&conn->conn_wait_rcfr_comp);
4217 wait_for_completion(&conn->conn_post_wait_comp);
4218 spin_lock_bh(&conn->state_lock);
4219 }
4220 atomic_set(&conn->connection_reinstatement, 1);
4221 spin_unlock_bh(&conn->state_lock);
4222
4223 /*
4224 * If any other processes are accessing this connection pointer we
4225 * must wait until they have completed.
4226 */
4227 iscsit_check_conn_usage_count(conn);
4228
Herbert Xu69110e32016-01-24 21:19:52 +08004229 ahash_request_free(conn->conn_tx_hash);
4230 if (conn->conn_rx_hash) {
4231 struct crypto_ahash *tfm;
4232
4233 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4234 ahash_request_free(conn->conn_rx_hash);
4235 crypto_free_ahash(tfm);
4236 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004237
Joern Engelfbecb652014-09-02 17:49:47 -04004238 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004239
4240 kfree(conn->conn_ops);
4241 conn->conn_ops = NULL;
4242
Al Virobf6932f2012-07-21 08:55:18 +01004243 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004244 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004245
4246 if (conn->conn_transport->iscsit_free_conn)
4247 conn->conn_transport->iscsit_free_conn(conn);
4248
4249 iscsit_put_transport(conn->conn_transport);
4250
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004251 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4252 conn->conn_state = TARG_CONN_STATE_FREE;
4253 kfree(conn);
4254
4255 spin_lock_bh(&sess->conn_lock);
4256 atomic_dec(&sess->nconn);
4257 pr_debug("Decremented iSCSI connection count to %hu from node:"
4258 " %s\n", atomic_read(&sess->nconn),
4259 sess->sess_ops->InitiatorName);
4260 /*
4261 * Make sure that if one connection fails in an non ERL=2 iSCSI
4262 * Session that they all fail.
4263 */
4264 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4265 !atomic_read(&sess->session_logout))
4266 atomic_set(&sess->session_fall_back_to_erl0, 1);
4267
4268 /*
4269 * If this was not the last connection in the session, and we are
4270 * performing session reinstatement or falling back to ERL=0, call
4271 * iscsit_stop_session() without sleeping to shutdown the other
4272 * active connections.
4273 */
4274 if (atomic_read(&sess->nconn)) {
4275 if (!atomic_read(&sess->session_reinstatement) &&
4276 !atomic_read(&sess->session_fall_back_to_erl0)) {
4277 spin_unlock_bh(&sess->conn_lock);
4278 return 0;
4279 }
4280 if (!atomic_read(&sess->session_stop_active)) {
4281 atomic_set(&sess->session_stop_active, 1);
4282 spin_unlock_bh(&sess->conn_lock);
4283 iscsit_stop_session(sess, 0, 0);
4284 return 0;
4285 }
4286 spin_unlock_bh(&sess->conn_lock);
4287 return 0;
4288 }
4289
4290 /*
4291 * If this was the last connection in the session and one of the
4292 * following is occurring:
4293 *
4294 * Session Reinstatement is not being performed, and are falling back
4295 * to ERL=0 call iscsit_close_session().
4296 *
4297 * Session Logout was requested. iscsit_close_session() will be called
4298 * elsewhere.
4299 *
4300 * Session Continuation is not being performed, start the Time2Retain
4301 * handler and check if sleep_on_sess_wait_sem is active.
4302 */
4303 if (!atomic_read(&sess->session_reinstatement) &&
4304 atomic_read(&sess->session_fall_back_to_erl0)) {
4305 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004306 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004307
4308 return 0;
4309 } else if (atomic_read(&sess->session_logout)) {
4310 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4311 sess->session_state = TARG_SESS_STATE_FREE;
4312 spin_unlock_bh(&sess->conn_lock);
4313
4314 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4315 complete(&sess->session_wait_comp);
4316
4317 return 0;
4318 } else {
4319 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4320 sess->session_state = TARG_SESS_STATE_FAILED;
4321
4322 if (!atomic_read(&sess->session_continuation)) {
4323 spin_unlock_bh(&sess->conn_lock);
4324 iscsit_start_time2retain_handler(sess);
4325 } else
4326 spin_unlock_bh(&sess->conn_lock);
4327
4328 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4329 complete(&sess->session_wait_comp);
4330
4331 return 0;
4332 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004333}
4334
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004335/*
4336 * If the iSCSI Session for the iSCSI Initiator Node exists,
4337 * forcefully shutdown the iSCSI NEXUS.
4338 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004339int iscsit_close_session(struct iscsi_session *sess)
4340{
Andy Grover60bfcf82013-10-09 11:05:58 -07004341 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004342 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4343
4344 if (atomic_read(&sess->nconn)) {
4345 pr_err("%d connection(s) still exist for iSCSI session"
4346 " to %s\n", atomic_read(&sess->nconn),
4347 sess->sess_ops->InitiatorName);
4348 BUG();
4349 }
4350
4351 spin_lock_bh(&se_tpg->session_lock);
4352 atomic_set(&sess->session_logout, 1);
4353 atomic_set(&sess->session_reinstatement, 1);
4354 iscsit_stop_time2retain_timer(sess);
4355 spin_unlock_bh(&se_tpg->session_lock);
4356
4357 /*
4358 * transport_deregister_session_configfs() will clear the
4359 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4360 * can be setting it again with __transport_register_session() in
4361 * iscsi_post_login_handler() again after the iscsit_stop_session()
4362 * completes in iscsi_np context.
4363 */
4364 transport_deregister_session_configfs(sess->se_sess);
4365
4366 /*
4367 * If any other processes are accessing this session pointer we must
4368 * wait until they have completed. If we are in an interrupt (the
4369 * time2retain handler) and contain and active session usage count we
4370 * restart the timer and exit.
4371 */
4372 if (!in_interrupt()) {
4373 if (iscsit_check_session_usage_count(sess) == 1)
4374 iscsit_stop_session(sess, 1, 1);
4375 } else {
4376 if (iscsit_check_session_usage_count(sess) == 2) {
4377 atomic_set(&sess->session_logout, 0);
4378 iscsit_start_time2retain_handler(sess);
4379 return 0;
4380 }
4381 }
4382
4383 transport_deregister_session(sess->se_sess);
4384
4385 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4386 iscsit_free_connection_recovery_entires(sess);
4387
4388 iscsit_free_all_ooo_cmdsns(sess);
4389
4390 spin_lock_bh(&se_tpg->session_lock);
4391 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4392 sess->session_state = TARG_SESS_STATE_FREE;
4393 pr_debug("Released iSCSI session from node: %s\n",
4394 sess->sess_ops->InitiatorName);
4395 tpg->nsessions--;
4396 if (tpg->tpg_tiqn)
4397 tpg->tpg_tiqn->tiqn_nsessions--;
4398
4399 pr_debug("Decremented number of active iSCSI Sessions on"
4400 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4401
4402 spin_lock(&sess_idr_lock);
4403 idr_remove(&sess_idr, sess->session_index);
4404 spin_unlock(&sess_idr_lock);
4405
4406 kfree(sess->sess_ops);
4407 sess->sess_ops = NULL;
4408 spin_unlock_bh(&se_tpg->session_lock);
4409
4410 kfree(sess);
4411 return 0;
4412}
4413
4414static void iscsit_logout_post_handler_closesession(
4415 struct iscsi_conn *conn)
4416{
4417 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004418 int sleep = 1;
4419 /*
4420 * Traditional iscsi/tcp will invoke this logic from TX thread
4421 * context during session logout, so clear tx_thread_active and
4422 * sleep if iscsit_close_connection() has not already occured.
4423 *
4424 * Since iser-target invokes this logic from it's own workqueue,
4425 * always sleep waiting for RX/TX thread shutdown to complete
4426 * within iscsit_close_connection().
4427 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004428 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004429 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004430
4431 atomic_set(&conn->conn_logout_remove, 0);
4432 complete(&conn->conn_logout_comp);
4433
4434 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004435 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004436 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004437 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004438}
4439
4440static void iscsit_logout_post_handler_samecid(
4441 struct iscsi_conn *conn)
4442{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004443 int sleep = 1;
4444
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004445 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004446 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004447
4448 atomic_set(&conn->conn_logout_remove, 0);
4449 complete(&conn->conn_logout_comp);
4450
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004451 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004452 iscsit_dec_conn_usage_count(conn);
4453}
4454
4455static void iscsit_logout_post_handler_diffcid(
4456 struct iscsi_conn *conn,
4457 u16 cid)
4458{
4459 struct iscsi_conn *l_conn;
4460 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004461 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004462
4463 if (!sess)
4464 return;
4465
4466 spin_lock_bh(&sess->conn_lock);
4467 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4468 if (l_conn->cid == cid) {
4469 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004470 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004471 break;
4472 }
4473 }
4474 spin_unlock_bh(&sess->conn_lock);
4475
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004476 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004477 return;
4478
4479 if (l_conn->sock)
4480 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4481
4482 spin_lock_bh(&l_conn->state_lock);
4483 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4484 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4485 spin_unlock_bh(&l_conn->state_lock);
4486
4487 iscsit_cause_connection_reinstatement(l_conn, 1);
4488 iscsit_dec_conn_usage_count(l_conn);
4489}
4490
4491/*
4492 * Return of 0 causes the TX thread to restart.
4493 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004494int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004495 struct iscsi_cmd *cmd,
4496 struct iscsi_conn *conn)
4497{
4498 int ret = 0;
4499
4500 switch (cmd->logout_reason) {
4501 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4502 switch (cmd->logout_response) {
4503 case ISCSI_LOGOUT_SUCCESS:
4504 case ISCSI_LOGOUT_CLEANUP_FAILED:
4505 default:
4506 iscsit_logout_post_handler_closesession(conn);
4507 break;
4508 }
4509 ret = 0;
4510 break;
4511 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4512 if (conn->cid == cmd->logout_cid) {
4513 switch (cmd->logout_response) {
4514 case ISCSI_LOGOUT_SUCCESS:
4515 case ISCSI_LOGOUT_CLEANUP_FAILED:
4516 default:
4517 iscsit_logout_post_handler_samecid(conn);
4518 break;
4519 }
4520 ret = 0;
4521 } else {
4522 switch (cmd->logout_response) {
4523 case ISCSI_LOGOUT_SUCCESS:
4524 iscsit_logout_post_handler_diffcid(conn,
4525 cmd->logout_cid);
4526 break;
4527 case ISCSI_LOGOUT_CID_NOT_FOUND:
4528 case ISCSI_LOGOUT_CLEANUP_FAILED:
4529 default:
4530 break;
4531 }
4532 ret = 1;
4533 }
4534 break;
4535 case ISCSI_LOGOUT_REASON_RECOVERY:
4536 switch (cmd->logout_response) {
4537 case ISCSI_LOGOUT_SUCCESS:
4538 case ISCSI_LOGOUT_CID_NOT_FOUND:
4539 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4540 case ISCSI_LOGOUT_CLEANUP_FAILED:
4541 default:
4542 break;
4543 }
4544 ret = 1;
4545 break;
4546 default:
4547 break;
4548
4549 }
4550 return ret;
4551}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004552EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004553
4554void iscsit_fail_session(struct iscsi_session *sess)
4555{
4556 struct iscsi_conn *conn;
4557
4558 spin_lock_bh(&sess->conn_lock);
4559 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4560 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4561 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4562 }
4563 spin_unlock_bh(&sess->conn_lock);
4564
4565 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4566 sess->session_state = TARG_SESS_STATE_FAILED;
4567}
4568
4569int iscsit_free_session(struct iscsi_session *sess)
4570{
4571 u16 conn_count = atomic_read(&sess->nconn);
4572 struct iscsi_conn *conn, *conn_tmp = NULL;
4573 int is_last;
4574
4575 spin_lock_bh(&sess->conn_lock);
4576 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4577
4578 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4579 conn_list) {
4580 if (conn_count == 0)
4581 break;
4582
4583 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4584 is_last = 1;
4585 } else {
4586 iscsit_inc_conn_usage_count(conn_tmp);
4587 is_last = 0;
4588 }
4589 iscsit_inc_conn_usage_count(conn);
4590
4591 spin_unlock_bh(&sess->conn_lock);
4592 iscsit_cause_connection_reinstatement(conn, 1);
4593 spin_lock_bh(&sess->conn_lock);
4594
4595 iscsit_dec_conn_usage_count(conn);
4596 if (is_last == 0)
4597 iscsit_dec_conn_usage_count(conn_tmp);
4598
4599 conn_count--;
4600 }
4601
4602 if (atomic_read(&sess->nconn)) {
4603 spin_unlock_bh(&sess->conn_lock);
4604 wait_for_completion(&sess->session_wait_comp);
4605 } else
4606 spin_unlock_bh(&sess->conn_lock);
4607
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004608 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004609 return 0;
4610}
4611
4612void iscsit_stop_session(
4613 struct iscsi_session *sess,
4614 int session_sleep,
4615 int connection_sleep)
4616{
4617 u16 conn_count = atomic_read(&sess->nconn);
4618 struct iscsi_conn *conn, *conn_tmp = NULL;
4619 int is_last;
4620
4621 spin_lock_bh(&sess->conn_lock);
4622 if (session_sleep)
4623 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4624
4625 if (connection_sleep) {
4626 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4627 conn_list) {
4628 if (conn_count == 0)
4629 break;
4630
4631 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4632 is_last = 1;
4633 } else {
4634 iscsit_inc_conn_usage_count(conn_tmp);
4635 is_last = 0;
4636 }
4637 iscsit_inc_conn_usage_count(conn);
4638
4639 spin_unlock_bh(&sess->conn_lock);
4640 iscsit_cause_connection_reinstatement(conn, 1);
4641 spin_lock_bh(&sess->conn_lock);
4642
4643 iscsit_dec_conn_usage_count(conn);
4644 if (is_last == 0)
4645 iscsit_dec_conn_usage_count(conn_tmp);
4646 conn_count--;
4647 }
4648 } else {
4649 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4650 iscsit_cause_connection_reinstatement(conn, 0);
4651 }
4652
4653 if (session_sleep && atomic_read(&sess->nconn)) {
4654 spin_unlock_bh(&sess->conn_lock);
4655 wait_for_completion(&sess->session_wait_comp);
4656 } else
4657 spin_unlock_bh(&sess->conn_lock);
4658}
4659
4660int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4661{
4662 struct iscsi_session *sess;
4663 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4664 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004665 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004666 int session_count = 0;
4667
4668 spin_lock_bh(&se_tpg->session_lock);
4669 if (tpg->nsessions && !force) {
4670 spin_unlock_bh(&se_tpg->session_lock);
4671 return -1;
4672 }
4673
4674 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4675 sess_list) {
4676 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4677
4678 spin_lock(&sess->conn_lock);
4679 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4680 atomic_read(&sess->session_logout) ||
4681 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4682 spin_unlock(&sess->conn_lock);
4683 continue;
4684 }
4685 atomic_set(&sess->session_reinstatement, 1);
4686 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004687
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004688 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004689 }
4690 spin_unlock_bh(&se_tpg->session_lock);
4691
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004692 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4693 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4694
4695 iscsit_free_session(sess);
4696 session_count++;
4697 }
4698
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004699 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4700 " Group: %hu\n", session_count, tpg->tpgt);
4701 return 0;
4702}
4703
4704MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4705MODULE_VERSION("4.1.x");
4706MODULE_AUTHOR("nab@Linux-iSCSI.org");
4707MODULE_LICENSE("GPL");
4708
4709module_init(iscsi_target_init_module);
4710module_exit(iscsi_target_cleanup_module);