blob: 0f7ade04b5831fea4f611e2688091b41062d21d0 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010027#include <linux/sched/signal.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000028#include <asm/unaligned.h>
Bart Van Assche8dcf07b2016-11-14 15:47:14 -080029#include <net/ipv6.h>
Bart Van Asscheba929992015-05-08 10:11:12 +020030#include <scsi/scsi_proto.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000031#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070032#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000033#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050034#include <target/target_core_fabric.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000035
Sagi Grimberg67f091f2015-01-07 14:57:31 +020036#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000037#include "iscsi_target_parameters.h"
38#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000039#include "iscsi_target_datain_values.h"
40#include "iscsi_target_erl0.h"
41#include "iscsi_target_erl1.h"
42#include "iscsi_target_erl2.h"
43#include "iscsi_target_login.h"
44#include "iscsi_target_tmr.h"
45#include "iscsi_target_tpg.h"
46#include "iscsi_target_util.h"
47#include "iscsi_target.h"
48#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020049#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080051#include <target/iscsi/iscsi_transport.h>
52
Nicholas Bellingere48354c2011-07-23 06:43:04 +000053static LIST_HEAD(g_tiqn_list);
54static LIST_HEAD(g_np_list);
55static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080056static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000057
58static struct idr tiqn_idr;
59struct idr sess_idr;
60struct mutex auth_id_lock;
61spinlock_t sess_idr_lock;
62
63struct iscsit_global *iscsit_global;
64
Nicholas Bellingere48354c2011-07-23 06:43:04 +000065struct kmem_cache *lio_qr_cache;
66struct kmem_cache *lio_dr_cache;
67struct kmem_cache *lio_ooo_cache;
68struct kmem_cache *lio_r2t_cache;
69
70static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070071 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000072
73struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
74{
75 struct iscsi_tiqn *tiqn = NULL;
76
77 spin_lock(&tiqn_lock);
78 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
79 if (!strcmp(tiqn->tiqn, buf)) {
80
81 spin_lock(&tiqn->tiqn_state_lock);
82 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
83 tiqn->tiqn_access_count++;
84 spin_unlock(&tiqn->tiqn_state_lock);
85 spin_unlock(&tiqn_lock);
86 return tiqn;
87 }
88 spin_unlock(&tiqn->tiqn_state_lock);
89 }
90 }
91 spin_unlock(&tiqn_lock);
92
93 return NULL;
94}
95
96static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
97{
98 spin_lock(&tiqn->tiqn_state_lock);
99 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
100 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
101 spin_unlock(&tiqn->tiqn_state_lock);
102 return 0;
103 }
104 spin_unlock(&tiqn->tiqn_state_lock);
105
106 return -1;
107}
108
109void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
110{
111 spin_lock(&tiqn->tiqn_state_lock);
112 tiqn->tiqn_access_count--;
113 spin_unlock(&tiqn->tiqn_state_lock);
114}
115
116/*
117 * Note that IQN formatting is expected to be done in userspace, and
118 * no explict IQN format checks are done here.
119 */
120struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
121{
122 struct iscsi_tiqn *tiqn = NULL;
123 int ret;
124
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300125 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000126 pr_err("Target IQN exceeds %d bytes\n",
127 ISCSI_IQN_LEN);
128 return ERR_PTR(-EINVAL);
129 }
130
Markus Elfring3829f382017-04-09 16:00:39 +0200131 tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200132 if (!tiqn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000133 return ERR_PTR(-ENOMEM);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000134
135 sprintf(tiqn->tiqn, "%s", buf);
136 INIT_LIST_HEAD(&tiqn->tiqn_list);
137 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
138 spin_lock_init(&tiqn->tiqn_state_lock);
139 spin_lock_init(&tiqn->tiqn_tpg_lock);
140 spin_lock_init(&tiqn->sess_err_stats.lock);
141 spin_lock_init(&tiqn->login_stats.lock);
142 spin_lock_init(&tiqn->logout_stats.lock);
143
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000144 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
145
Tejun Heoc9365bd2013-02-27 17:04:43 -0800146 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000147 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800148
149 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000150 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800151 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000152 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800153 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000154 kfree(tiqn);
155 return ERR_PTR(ret);
156 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800157 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000158 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800159
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000160 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800161 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000162
163 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
164
165 return tiqn;
166
167}
168
169static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
170{
171 /*
172 * Wait for accesses to said struct iscsi_tiqn to end.
173 */
174 spin_lock(&tiqn->tiqn_state_lock);
175 while (tiqn->tiqn_access_count != 0) {
176 spin_unlock(&tiqn->tiqn_state_lock);
177 msleep(10);
178 spin_lock(&tiqn->tiqn_state_lock);
179 }
180 spin_unlock(&tiqn->tiqn_state_lock);
181}
182
183void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
184{
185 /*
186 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
187 * while holding tiqn->tiqn_state_lock. This means that all subsequent
188 * attempts to access this struct iscsi_tiqn will fail from both transport
189 * fabric and control code paths.
190 */
191 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
192 pr_err("iscsit_set_tiqn_shutdown() failed\n");
193 return;
194 }
195
196 iscsit_wait_for_tiqn(tiqn);
197
198 spin_lock(&tiqn_lock);
199 list_del(&tiqn->tiqn_list);
200 idr_remove(&tiqn_idr, tiqn->tiqn_index);
201 spin_unlock(&tiqn_lock);
202
203 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
204 tiqn->tiqn);
205 kfree(tiqn);
206}
207
208int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
209{
210 int ret;
211 /*
212 * Determine if the network portal is accepting storage traffic.
213 */
214 spin_lock_bh(&np->np_thread_lock);
215 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
216 spin_unlock_bh(&np->np_thread_lock);
217 return -1;
218 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000219 spin_unlock_bh(&np->np_thread_lock);
220 /*
221 * Determine if the portal group is accepting storage traffic.
222 */
223 spin_lock_bh(&tpg->tpg_state_lock);
224 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
225 spin_unlock_bh(&tpg->tpg_state_lock);
226 return -1;
227 }
228 spin_unlock_bh(&tpg->tpg_state_lock);
229
230 /*
231 * Here we serialize access across the TIQN+TPG Tuple.
232 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700233 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingeree7619f2015-05-19 15:10:44 -0700234 if (ret != 0)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000235 return -1;
236
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700237 spin_lock_bh(&tpg->tpg_state_lock);
238 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
239 spin_unlock_bh(&tpg->tpg_state_lock);
240 up(&tpg->np_login_sem);
241 return -1;
242 }
243 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000244
245 return 0;
246}
247
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700248void iscsit_login_kref_put(struct kref *kref)
249{
250 struct iscsi_tpg_np *tpg_np = container_of(kref,
251 struct iscsi_tpg_np, tpg_np_kref);
252
253 complete(&tpg_np->tpg_np_comp);
254}
255
256int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
257 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000258{
259 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
260
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700261 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000262
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700263 if (tpg_np)
264 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000265
266 if (tiqn)
267 iscsit_put_tiqn_for_login(tiqn);
268
269 return 0;
270}
271
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800272bool iscsit_check_np_match(
Andy Grover13a3cf02015-08-24 10:26:06 -0700273 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800274 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000275 int network_transport)
276{
277 struct sockaddr_in *sock_in, *sock_in_e;
278 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800279 bool ip_match = false;
Andy Grover69d75572015-08-24 10:26:04 -0700280 u16 port, port_e;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000281
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800282 if (sockaddr->ss_family == AF_INET6) {
283 sock_in6 = (struct sockaddr_in6 *)sockaddr;
284 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
285
286 if (!memcmp(&sock_in6->sin6_addr.in6_u,
287 &sock_in6_e->sin6_addr.in6_u,
288 sizeof(struct in6_addr)))
289 ip_match = true;
290
291 port = ntohs(sock_in6->sin6_port);
Andy Grover69d75572015-08-24 10:26:04 -0700292 port_e = ntohs(sock_in6_e->sin6_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800293 } else {
294 sock_in = (struct sockaddr_in *)sockaddr;
295 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
296
297 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
298 ip_match = true;
299
300 port = ntohs(sock_in->sin_port);
Andy Grover69d75572015-08-24 10:26:04 -0700301 port_e = ntohs(sock_in_e->sin_port);
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800302 }
303
Andy Grover69d75572015-08-24 10:26:04 -0700304 if (ip_match && (port_e == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800305 (np->np_network_transport == network_transport))
306 return true;
307
308 return false;
309}
310
Andy Groveree291e62014-01-24 16:18:54 -0800311/*
312 * Called with mutex np_lock held
313 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800314static struct iscsi_np *iscsit_get_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700315 struct sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800316 int network_transport)
317{
318 struct iscsi_np *np;
319 bool match;
320
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000321 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800322 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000323 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800324 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000325 continue;
326 }
327
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800328 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200329 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000330 /*
331 * Increment the np_exports reference count now to
332 * prevent iscsit_del_np() below from being called
333 * while iscsi_tpg_add_network_portal() is called.
334 */
335 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800336 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000337 return np;
338 }
Andy Groveree291e62014-01-24 16:18:54 -0800339 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000341
342 return NULL;
343}
344
345struct iscsi_np *iscsit_add_np(
Andy Grover13a3cf02015-08-24 10:26:06 -0700346 struct sockaddr_storage *sockaddr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000347 int network_transport)
348{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000349 struct iscsi_np *np;
350 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800351
352 mutex_lock(&np_lock);
353
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354 /*
355 * Locate the existing struct iscsi_np if already active..
356 */
357 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800358 if (np) {
359 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000360 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800361 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362
Markus Elfring3829f382017-04-09 16:00:39 +0200363 np = kzalloc(sizeof(*np), GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000364 if (!np) {
Andy Groveree291e62014-01-24 16:18:54 -0800365 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000366 return ERR_PTR(-ENOMEM);
367 }
368
369 np->np_flags |= NPF_IP_NETWORK;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000370 np->np_network_transport = network_transport;
371 spin_lock_init(&np->np_thread_lock);
372 init_completion(&np->np_restart_comp);
373 INIT_LIST_HEAD(&np->np_list);
374
375 ret = iscsi_target_setup_login_socket(np, sockaddr);
376 if (ret != 0) {
377 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800378 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000379 return ERR_PTR(ret);
380 }
381
382 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
383 if (IS_ERR(np->np_thread)) {
384 pr_err("Unable to create kthread: iscsi_np\n");
385 ret = PTR_ERR(np->np_thread);
386 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800387 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000388 return ERR_PTR(ret);
389 }
390 /*
391 * Increment the np_exports reference count now to prevent
392 * iscsit_del_np() below from being run while a new call to
393 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
394 * active. We don't need to hold np->np_thread_lock at this
395 * point because iscsi_np has not been added to g_np_list yet.
396 */
397 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800398 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000400 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800401 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000402
Andy Grover69d75572015-08-24 10:26:04 -0700403 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
404 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000405
406 return np;
407}
408
409int iscsit_reset_np_thread(
410 struct iscsi_np *np,
411 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700412 struct iscsi_portal_group *tpg,
413 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000414{
415 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
417 spin_unlock_bh(&np->np_thread_lock);
418 return 0;
419 }
420 np->np_thread_state = ISCSI_NP_THREAD_RESET;
421
422 if (np->np_thread) {
423 spin_unlock_bh(&np->np_thread_lock);
424 send_sig(SIGINT, np->np_thread, 1);
425 wait_for_completion(&np->np_restart_comp);
426 spin_lock_bh(&np->np_thread_lock);
427 }
428 spin_unlock_bh(&np->np_thread_lock);
429
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700430 if (tpg_np && shutdown) {
431 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
432
433 wait_for_completion(&tpg_np->tpg_np_comp);
434 }
435
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000436 return 0;
437}
438
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800439static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000440{
Al Virobf6932f2012-07-21 08:55:18 +0100441 if (np->np_socket)
442 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000443}
444
445int iscsit_del_np(struct iscsi_np *np)
446{
447 spin_lock_bh(&np->np_thread_lock);
448 np->np_exports--;
449 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700450 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451 spin_unlock_bh(&np->np_thread_lock);
452 return 0;
453 }
454 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
455 spin_unlock_bh(&np->np_thread_lock);
456
457 if (np->np_thread) {
458 /*
459 * We need to send the signal to wakeup Linux/Net
460 * which may be sleeping in sock_accept()..
461 */
462 send_sig(SIGINT, np->np_thread, 1);
463 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800464 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000465 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800466
467 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000468
Andy Groveree291e62014-01-24 16:18:54 -0800469 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000470 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800471 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000472
Andy Grover69d75572015-08-24 10:26:04 -0700473 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
474 &np->np_sockaddr, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000475
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800476 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 kfree(np);
478 return 0;
479}
480
Varun Prakashe8205cc2016-04-20 00:00:11 +0530481static void iscsit_get_rx_pdu(struct iscsi_conn *);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700482
Varun Prakashd2faaef2016-04-20 00:00:19 +0530483int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700484{
Nicholas Bellingera4467012016-10-30 17:30:08 -0700485 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700486}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530487EXPORT_SYMBOL(iscsit_queue_rsp);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700488
Varun Prakashd2faaef2016-04-20 00:00:19 +0530489void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700490{
491 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
492
493 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -0700494 if (!list_empty(&cmd->i_conn_node) &&
495 !(cmd->se_cmd.transport_state & CMD_T_FABRIC_STOP))
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700496 list_del_init(&cmd->i_conn_node);
497 spin_unlock_bh(&conn->cmd_lock);
498
499 __iscsit_free_cmd(cmd, scsi_cmd, true);
500}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530501EXPORT_SYMBOL(iscsit_aborted_task);
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700502
Varun Prakash2854bb22016-04-20 00:00:08 +0530503static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
504 u32, u32, u8 *, u8 *);
505static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
506
507static int
508iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
509 const void *data_buf, u32 data_buf_len)
510{
511 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
512 struct kvec *iov;
513 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
514 int ret;
515
516 iov = &cmd->iov_misc[0];
517 iov[niov].iov_base = cmd->pdu;
518 iov[niov++].iov_len = ISCSI_HDR_LEN;
519
520 if (conn->conn_ops->HeaderDigest) {
521 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
522
523 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
524 ISCSI_HDR_LEN, 0, NULL,
525 (u8 *)header_digest);
526
527 iov[0].iov_len += ISCSI_CRC_LEN;
528 tx_size += ISCSI_CRC_LEN;
529 pr_debug("Attaching CRC32C HeaderDigest"
530 " to opcode 0x%x 0x%08x\n",
531 hdr->opcode, *header_digest);
532 }
533
534 if (data_buf_len) {
535 u32 padding = ((-data_buf_len) & 3);
536
537 iov[niov].iov_base = (void *)data_buf;
538 iov[niov++].iov_len = data_buf_len;
539 tx_size += data_buf_len;
540
541 if (padding != 0) {
542 iov[niov].iov_base = &cmd->pad_bytes;
543 iov[niov++].iov_len = padding;
544 tx_size += padding;
545 pr_debug("Attaching %u additional"
546 " padding bytes.\n", padding);
547 }
548
549 if (conn->conn_ops->DataDigest) {
550 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
551 data_buf, data_buf_len,
552 padding,
553 (u8 *)&cmd->pad_bytes,
554 (u8 *)&cmd->data_crc);
555
556 iov[niov].iov_base = &cmd->data_crc;
557 iov[niov++].iov_len = ISCSI_CRC_LEN;
558 tx_size += ISCSI_CRC_LEN;
559 pr_debug("Attached DataDigest for %u"
560 " bytes opcode 0x%x, CRC 0x%08x\n",
561 data_buf_len, hdr->opcode, cmd->data_crc);
562 }
563 }
564
565 cmd->iov_misc_count = niov;
566 cmd->tx_size = tx_size;
567
568 ret = iscsit_send_tx_data(cmd, conn, 1);
569 if (ret < 0) {
570 iscsit_tx_thread_wait_for_tcp(conn);
571 return ret;
572 }
573
574 return 0;
575}
576
577static int iscsit_map_iovec(struct iscsi_cmd *, struct kvec *, u32, u32);
578static void iscsit_unmap_iovec(struct iscsi_cmd *);
579static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
580 u32, u32, u32, u8 *);
581static int
582iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
583 const struct iscsi_datain *datain)
584{
585 struct kvec *iov;
586 u32 iov_count = 0, tx_size = 0;
587 int ret, iov_ret;
588
589 iov = &cmd->iov_data[0];
590 iov[iov_count].iov_base = cmd->pdu;
591 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
592 tx_size += ISCSI_HDR_LEN;
593
594 if (conn->conn_ops->HeaderDigest) {
595 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
596
597 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
598 ISCSI_HDR_LEN, 0, NULL,
599 (u8 *)header_digest);
600
601 iov[0].iov_len += ISCSI_CRC_LEN;
602 tx_size += ISCSI_CRC_LEN;
603
604 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
605 *header_digest);
606 }
607
608 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
609 datain->offset, datain->length);
610 if (iov_ret < 0)
611 return -1;
612
613 iov_count += iov_ret;
614 tx_size += datain->length;
615
616 cmd->padding = ((-datain->length) & 3);
617 if (cmd->padding) {
618 iov[iov_count].iov_base = cmd->pad_bytes;
619 iov[iov_count++].iov_len = cmd->padding;
620 tx_size += cmd->padding;
621
622 pr_debug("Attaching %u padding bytes\n", cmd->padding);
623 }
624
625 if (conn->conn_ops->DataDigest) {
626 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
627 cmd, datain->offset,
628 datain->length,
629 cmd->padding,
630 cmd->pad_bytes);
631
632 iov[iov_count].iov_base = &cmd->data_crc;
633 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
634 tx_size += ISCSI_CRC_LEN;
635
636 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
637 datain->length + cmd->padding, cmd->data_crc);
638 }
639
640 cmd->iov_data_count = iov_count;
641 cmd->tx_size = tx_size;
642
643 ret = iscsit_fe_sendpage_sg(cmd, conn);
644
645 iscsit_unmap_iovec(cmd);
646
647 if (ret < 0) {
648 iscsit_tx_thread_wait_for_tcp(conn);
649 return ret;
650 }
651
652 return 0;
653}
654
655static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
656 struct iscsi_datain_req *dr, const void *buf,
657 u32 buf_len)
658{
659 if (dr)
660 return iscsit_xmit_datain_pdu(conn, cmd, buf);
661 else
662 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
663}
664
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700665static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
666{
667 return TARGET_PROT_NORMAL;
668}
669
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800670static struct iscsit_transport iscsi_target_transport = {
671 .name = "iSCSI/TCP",
672 .transport_type = ISCSI_TCP,
Nicholas Bellingerbd027d82016-05-14 22:23:34 -0700673 .rdma_shutdown = false,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800674 .owner = NULL,
675 .iscsit_setup_np = iscsit_setup_np,
676 .iscsit_accept_np = iscsit_accept_np,
677 .iscsit_free_np = iscsit_free_np,
678 .iscsit_get_login_rx = iscsit_get_login_rx,
679 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800680 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700681 .iscsit_immediate_queue = iscsit_immediate_queue,
682 .iscsit_response_queue = iscsit_response_queue,
683 .iscsit_queue_data_in = iscsit_queue_rsp,
684 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700685 .iscsit_aborted_task = iscsit_aborted_task,
Varun Prakash2854bb22016-04-20 00:00:08 +0530686 .iscsit_xmit_pdu = iscsit_xmit_pdu,
Varun Prakashe8205cc2016-04-20 00:00:11 +0530687 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700688 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800689};
690
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000691static int __init iscsi_target_init_module(void)
692{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800693 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000694
695 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
Markus Elfring3829f382017-04-09 16:00:39 +0200696 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200697 if (!iscsit_global)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 return -1;
Markus Elfringc46e22f2017-04-09 15:34:50 +0200699
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800700 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000701 mutex_init(&auth_id_lock);
702 spin_lock_init(&sess_idr_lock);
703 idr_init(&tiqn_idr);
704 idr_init(&sess_idr);
705
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200706 ret = target_register_template(&iscsi_ops);
707 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000708 goto out;
709
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800710 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
711 iscsit_global->ts_bitmap = vzalloc(size);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200712 if (!iscsit_global->ts_bitmap)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000713 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000714
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000715 lio_qr_cache = kmem_cache_create("lio_qr_cache",
716 sizeof(struct iscsi_queue_req),
717 __alignof__(struct iscsi_queue_req), 0, NULL);
718 if (!lio_qr_cache) {
719 pr_err("nable to kmem_cache_create() for"
720 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800721 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000722 }
723
724 lio_dr_cache = kmem_cache_create("lio_dr_cache",
725 sizeof(struct iscsi_datain_req),
726 __alignof__(struct iscsi_datain_req), 0, NULL);
727 if (!lio_dr_cache) {
728 pr_err("Unable to kmem_cache_create() for"
729 " lio_dr_cache\n");
730 goto qr_out;
731 }
732
733 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
734 sizeof(struct iscsi_ooo_cmdsn),
735 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
736 if (!lio_ooo_cache) {
737 pr_err("Unable to kmem_cache_create() for"
738 " lio_ooo_cache\n");
739 goto dr_out;
740 }
741
742 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
743 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
744 0, NULL);
745 if (!lio_r2t_cache) {
746 pr_err("Unable to kmem_cache_create() for"
747 " lio_r2t_cache\n");
748 goto ooo_out;
749 }
750
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800751 iscsit_register_transport(&iscsi_target_transport);
752
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000753 if (iscsit_load_discovery_tpg() < 0)
754 goto r2t_out;
755
756 return ret;
757r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100758 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000759 kmem_cache_destroy(lio_r2t_cache);
760ooo_out:
761 kmem_cache_destroy(lio_ooo_cache);
762dr_out:
763 kmem_cache_destroy(lio_dr_cache);
764qr_out:
765 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800766bitmap_out:
767 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000768configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200769 /* XXX: this probably wants it to be it's own unwind step.. */
770 if (iscsit_global->discovery_tpg)
771 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
772 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000773out:
774 kfree(iscsit_global);
775 return -ENOMEM;
776}
777
778static void __exit iscsi_target_cleanup_module(void)
779{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800781 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000782 kmem_cache_destroy(lio_qr_cache);
783 kmem_cache_destroy(lio_dr_cache);
784 kmem_cache_destroy(lio_ooo_cache);
785 kmem_cache_destroy(lio_r2t_cache);
786
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200787 /*
788 * Shutdown discovery sessions and disable discovery TPG
789 */
790 if (iscsit_global->discovery_tpg)
791 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000792
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200793 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800795 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000796 kfree(iscsit_global);
797}
798
Varun Prakashd2faaef2016-04-20 00:00:19 +0530799int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700800 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000801 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700802 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803{
804 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000805
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000806 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000807 if (!cmd)
808 return -1;
809
810 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700811 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100813 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000814 if (!cmd->buf_ptr) {
815 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700816 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817 return -1;
818 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000819
820 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700821 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000822 spin_unlock_bh(&conn->cmd_lock);
823
824 cmd->i_state = ISTATE_SEND_REJECT;
825 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
826
Nicholas Bellingerba159912013-07-03 03:48:24 -0700827 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000828}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530829EXPORT_SYMBOL(iscsit_add_reject);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000830
Nicholas Bellingerba159912013-07-03 03:48:24 -0700831static int iscsit_add_reject_from_cmd(
832 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700834 bool add_to_conn,
835 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000836{
837 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000838
839 if (!cmd->conn) {
840 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
841 cmd->init_task_tag);
842 return -1;
843 }
844 conn = cmd->conn;
845
846 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700847 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000848
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100849 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000850 if (!cmd->buf_ptr) {
851 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700852 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000853 return -1;
854 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000855
856 if (add_to_conn) {
857 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700858 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000859 spin_unlock_bh(&conn->cmd_lock);
860 }
861
862 cmd->i_state = ISTATE_SEND_REJECT;
863 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800864 /*
865 * Perform the kref_put now if se_cmd has already been setup by
866 * scsit_setup_scsi_cmd()
867 */
868 if (cmd->se_cmd.se_tfo != NULL) {
869 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +0200870 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800871 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700872 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000873}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700874
875static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
876 unsigned char *buf)
877{
878 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
879}
880
881int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
882{
883 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
884}
Varun Prakashd2faaef2016-04-20 00:00:19 +0530885EXPORT_SYMBOL(iscsit_reject_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000886
887/*
888 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700889 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000890 */
891static int iscsit_map_iovec(
892 struct iscsi_cmd *cmd,
893 struct kvec *iov,
894 u32 data_offset,
895 u32 data_length)
896{
897 u32 i = 0;
898 struct scatterlist *sg;
899 unsigned int page_off;
900
901 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700902 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000903 */
Imran Haider2b165092016-05-08 11:17:54 -0400904 u32 ent = data_offset / PAGE_SIZE;
905
906 if (ent >= cmd->se_cmd.t_data_nents) {
907 pr_err("Initial page entry out-of-bounds\n");
908 return -1;
909 }
910
911 sg = &cmd->se_cmd.t_data_sg[ent];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000912 page_off = (data_offset % PAGE_SIZE);
913
914 cmd->first_data_sg = sg;
915 cmd->first_data_sg_off = page_off;
916
917 while (data_length) {
918 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
919
920 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
921 iov[i].iov_len = cur_len;
922
923 data_length -= cur_len;
924 page_off = 0;
925 sg = sg_next(sg);
926 i++;
927 }
928
929 cmd->kmapped_nents = i;
930
931 return i;
932}
933
934static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
935{
936 u32 i;
937 struct scatterlist *sg;
938
939 sg = cmd->first_data_sg;
940
941 for (i = 0; i < cmd->kmapped_nents; i++)
942 kunmap(sg_page(&sg[i]));
943}
944
945static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
946{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700947 LIST_HEAD(ack_list);
948 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000949
950 conn->exp_statsn = exp_statsn;
951
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800952 if (conn->sess->sess_ops->RDMAExtensions)
953 return;
954
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000955 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700956 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000957 spin_lock(&cmd->istate_lock);
958 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800959 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000960 cmd->i_state = ISTATE_REMOVE;
961 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700962 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000963 continue;
964 }
965 spin_unlock(&cmd->istate_lock);
966 }
967 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700968
969 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800970 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700971 iscsit_free_cmd(cmd, false);
972 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000973}
974
975static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
976{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700977 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000978
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400979 iov_count += ISCSI_IOV_DATA_BUFFER;
Markus Elfringf1725112017-04-09 15:06:00 +0200980 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +0200981 if (!cmd->iov_data)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000982 return -ENOMEM;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000983
984 cmd->orig_iov_data_count = iov_count;
985 return 0;
986}
987
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800988int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
989 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000990{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800991 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000992 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700993 int iscsi_task_attr;
994 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000995
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800996 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000997
998 hdr = (struct iscsi_scsi_req *) buf;
999 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001000
1001 /* FIXME; Add checks for AdditionalHeaderSegment */
1002
1003 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1004 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1005 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1006 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001007 return iscsit_add_reject_cmd(cmd,
1008 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001009 }
1010
1011 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1012 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1013 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001014 * From RFC-3720 Section 10.3.1:
1015 *
1016 * "Either or both of R and W MAY be 1 when either the
1017 * Expected Data Transfer Length and/or Bidirectional Read
1018 * Expected Data Transfer Length are 0"
1019 *
1020 * For this case, go ahead and clear the unnecssary bits
1021 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001022 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001023 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1024 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001026 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001027 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -08001028 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001029 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001030
1031 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1032 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1033 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1034 " MUST be set if Expected Data Transfer Length is not 0."
1035 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001036 return iscsit_add_reject_cmd(cmd,
1037 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001038 }
1039
1040 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1041 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1042 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001043 return iscsit_add_reject_cmd(cmd,
1044 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001045 }
1046
1047 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1048 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1049 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001050 return iscsit_add_reject_cmd(cmd,
1051 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001052 }
1053
1054 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1055 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1056 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001057 return iscsit_add_reject_cmd(cmd,
1058 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001059 }
1060
Nicholas Bellingerba159912013-07-03 03:48:24 -07001061 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001062 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1063 pr_err("Expected Data Transfer Length and Length of"
1064 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1065 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001066 return iscsit_add_reject_cmd(cmd,
1067 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001068 }
1069
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001070 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001071 pr_err("DataSegmentLength: %u is greater than"
1072 " EDTL: %u, protocol error.\n", payload_length,
1073 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001074 return iscsit_add_reject_cmd(cmd,
1075 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001076 }
1077
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001078 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001079 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001080 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1081 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001082 return iscsit_add_reject_cmd(cmd,
1083 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001084 }
1085
1086 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1087 pr_err("DataSegmentLength: %u is greater than"
1088 " FirstBurstLength: %u, protocol error.\n",
1089 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001090 return iscsit_add_reject_cmd(cmd,
1091 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001092 }
1093
1094 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1095 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1096 DMA_NONE;
1097
Andy Groverd28b11692012-04-03 15:51:22 -07001098 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -07001099 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1100 /*
1101 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1102 */
1103 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1104 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001105 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001106 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001107 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001108 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001109 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001110 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001111 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001112 else {
1113 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001114 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1115 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -07001116 }
1117
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001118 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1119 cmd->i_state = ISTATE_NEW_CMD;
1120 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1121 cmd->immediate_data = (payload_length) ? 1 : 0;
1122 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1123 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1124 if (cmd->unsolicited_data)
1125 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1126
1127 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
Alexei Potashnik95473082015-07-21 15:07:56 -07001128 if (hdr->flags & ISCSI_FLAG_CMD_READ)
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02001129 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Alexei Potashnik95473082015-07-21 15:07:56 -07001130 else
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001131 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001132 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1133 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001134 cmd->first_burst_len = payload_length;
1135
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001136 if (!conn->sess->sess_ops->RDMAExtensions &&
1137 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001138 struct iscsi_datain_req *dr;
1139
1140 dr = iscsit_allocate_datain_req();
1141 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001142 return iscsit_add_reject_cmd(cmd,
1143 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001144
1145 iscsit_attach_datain_req(cmd, dr);
1146 }
1147
1148 /*
Andy Grover065ca1e2012-04-03 15:51:23 -07001149 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1150 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001151 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001152 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1153 cmd->data_direction, sam_task_attr,
1154 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -07001155
1156 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1157 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001158 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1159 conn->cid);
1160
Bart Van Asscheafc16602015-04-27 13:52:36 +02001161 target_get_sess_cmd(&cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001162
Christoph Hellwigde103c92012-11-06 12:24:09 -08001163 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1164 scsilun_to_int(&hdr->lun));
1165 if (cmd->sense_reason)
1166 goto attach_cmd;
1167
Bart Van Assche649ee052015-04-14 13:26:44 +02001168 /* only used for printks or comparing with ->ref_task_tag */
1169 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
Christoph Hellwigde103c92012-11-06 12:24:09 -08001170 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1171 if (cmd->sense_reason) {
1172 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001173 return iscsit_add_reject_cmd(cmd,
1174 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001175 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001176
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001177 goto attach_cmd;
1178 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001179
Christoph Hellwigde103c92012-11-06 12:24:09 -08001180 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001181 return iscsit_add_reject_cmd(cmd,
1182 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001183 }
1184
1185attach_cmd:
1186 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001187 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001188 spin_unlock_bh(&conn->cmd_lock);
1189 /*
1190 * Check if we need to delay processing because of ALUA
1191 * Active/NonOptimized primary access state..
1192 */
1193 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001194
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001195 return 0;
1196}
1197EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001198
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001199void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1200{
1201 iscsit_set_dataout_sequence_values(cmd);
1202
1203 spin_lock_bh(&cmd->dataout_timeout_lock);
1204 iscsit_start_dataout_timer(cmd, cmd->conn);
1205 spin_unlock_bh(&cmd->dataout_timeout_lock);
1206}
1207EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1208
1209int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1210 struct iscsi_scsi_req *hdr)
1211{
1212 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001213 /*
1214 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1215 * the Immediate Bit is not set, and no Immediate
1216 * Data is attached.
1217 *
1218 * A PDU/CmdSN carrying Immediate Data can only
1219 * be processed after the DataCRC has passed.
1220 * If the DataCRC fails, the CmdSN MUST NOT
1221 * be acknowledged. (See below)
1222 */
1223 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001224 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1225 (unsigned char *)hdr, hdr->cmdsn);
1226 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1227 return -1;
1228 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Bart Van Asscheafc16602015-04-27 13:52:36 +02001229 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001230 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001231 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001232 }
1233
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001234 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001235
1236 /*
1237 * If no Immediate Data is attached, it's OK to return now.
1238 */
1239 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001240 if (!cmd->sense_reason && cmd->unsolicited_data)
1241 iscsit_set_unsoliticed_dataout(cmd);
1242 if (!cmd->sense_reason)
1243 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001244
Bart Van Asscheafc16602015-04-27 13:52:36 +02001245 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001246 return 0;
1247 }
1248
1249 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001250 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1251 * execution. These exceptions are processed in CmdSN order using
1252 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001253 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001254 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001255 if (cmd->reject_reason)
1256 return 0;
1257
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001258 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001259 }
1260 /*
1261 * Call directly into transport_generic_new_cmd() to perform
1262 * the backend memory allocation.
1263 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001264 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001265 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001266 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001267
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001268 return 0;
1269}
1270EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1271
1272static int
1273iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1274 bool dump_payload)
1275{
1276 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1277 /*
1278 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1279 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001280 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001281 goto after_immediate_data;
1282
1283 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1284 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001285after_immediate_data:
1286 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1287 /*
1288 * A PDU/CmdSN carrying Immediate Data passed
1289 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1290 * Immediate Bit is not set.
1291 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001292 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1293 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001294 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001295 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001296
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001297 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001298 int rc;
1299
1300 rc = iscsit_dump_data_payload(cmd->conn,
1301 cmd->first_burst_len, 1);
Bart Van Asscheafc16602015-04-27 13:52:36 +02001302 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001303 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001304 } else if (cmd->unsolicited_data)
1305 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001306
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001307 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1308 /*
1309 * Immediate Data failed DataCRC and ERL>=1,
1310 * silently drop this PDU and let the initiator
1311 * plug the CmdSN gap.
1312 *
1313 * FIXME: Send Unsolicited NOPIN with reserved
1314 * TTT here to help the initiator figure out
1315 * the missing CmdSN, although they should be
1316 * intelligent enough to determine the missing
1317 * CmdSN and issue a retry to plug the sequence.
1318 */
1319 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001320 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001321 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1322 return -1;
1323
1324 return 0;
1325}
1326
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001327static int
1328iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1329 unsigned char *buf)
1330{
1331 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1332 int rc, immed_data;
1333 bool dump_payload = false;
1334
1335 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1336 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001337 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001338 /*
1339 * Allocation iovecs needed for struct socket operations for
1340 * traditional iSCSI block I/O.
1341 */
1342 if (iscsit_allocate_iovecs(cmd) < 0) {
Mike Christieb815fc12015-04-10 02:47:27 -05001343 return iscsit_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07001344 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001345 }
1346 immed_data = cmd->immediate_data;
1347
1348 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1349 if (rc < 0)
1350 return rc;
1351 else if (rc > 0)
1352 dump_payload = true;
1353
1354 if (!immed_data)
1355 return 0;
1356
1357 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1358}
1359
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001360static u32 iscsit_do_crypto_hash_sg(
Herbert Xu69110e32016-01-24 21:19:52 +08001361 struct ahash_request *hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001362 struct iscsi_cmd *cmd,
1363 u32 data_offset,
1364 u32 data_length,
1365 u32 padding,
1366 u8 *pad_bytes)
1367{
1368 u32 data_crc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 struct scatterlist *sg;
1370 unsigned int page_off;
1371
Herbert Xu69110e32016-01-24 21:19:52 +08001372 crypto_ahash_init(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001373
1374 sg = cmd->first_data_sg;
1375 page_off = cmd->first_data_sg_off;
1376
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001377 while (data_length) {
Alexei Potashnikaa756792015-07-20 17:12:12 -07001378 u32 cur_len = min_t(u32, data_length, (sg->length - page_off));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001379
Herbert Xu69110e32016-01-24 21:19:52 +08001380 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1381 crypto_ahash_update(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001382
1383 data_length -= cur_len;
1384 page_off = 0;
Alexei Potashnikaa756792015-07-20 17:12:12 -07001385 /* iscsit_map_iovec has already checked for invalid sg pointers */
1386 sg = sg_next(sg);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 }
1388
1389 if (padding) {
1390 struct scatterlist pad_sg;
1391
1392 sg_init_one(&pad_sg, pad_bytes, padding);
Herbert Xu69110e32016-01-24 21:19:52 +08001393 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1394 padding);
1395 crypto_ahash_finup(hash);
1396 } else {
1397 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1398 crypto_ahash_final(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001399 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001400
1401 return data_crc;
1402}
1403
1404static void iscsit_do_crypto_hash_buf(
Herbert Xu69110e32016-01-24 21:19:52 +08001405 struct ahash_request *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001406 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001407 u32 payload_length,
1408 u32 padding,
1409 u8 *pad_bytes,
1410 u8 *data_crc)
1411{
Herbert Xu69110e32016-01-24 21:19:52 +08001412 struct scatterlist sg[2];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001413
Herbert Xu69110e32016-01-24 21:19:52 +08001414 sg_init_table(sg, ARRAY_SIZE(sg));
1415 sg_set_buf(sg, buf, payload_length);
1416 sg_set_buf(sg + 1, pad_bytes, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001417
Herbert Xu69110e32016-01-24 21:19:52 +08001418 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001419
Herbert Xu69110e32016-01-24 21:19:52 +08001420 crypto_ahash_digest(hash);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001421}
1422
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001423int
Varun Prakash9a584bf2017-01-13 20:53:21 +05301424__iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1425 struct iscsi_cmd *cmd, u32 payload_length,
1426 bool *success)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001427{
Varun Prakash9a584bf2017-01-13 20:53:21 +05301428 struct iscsi_data *hdr = buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001429 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001430 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001431
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001432 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001433 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001434
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001435 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1436 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001437 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438 payload_length, conn->cid);
1439
1440 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1441 pr_err("Command ITT: 0x%08x received DataOUT after"
1442 " last DataOUT received, dumping payload\n",
1443 cmd->init_task_tag);
1444 return iscsit_dump_data_payload(conn, payload_length, 1);
1445 }
1446
1447 if (cmd->data_direction != DMA_TO_DEVICE) {
1448 pr_err("Command ITT: 0x%08x received DataOUT for a"
1449 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001450 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001451 }
1452 se_cmd = &cmd->se_cmd;
1453 iscsit_mod_dataout_timer(cmd);
1454
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001455 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001456 pr_err("DataOut Offset: %u, Length %u greater than"
1457 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001458 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001459 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001460 }
1461
1462 if (cmd->unsolicited_data) {
1463 int dump_unsolicited_data = 0;
1464
1465 if (conn->sess->sess_ops->InitialR2T) {
1466 pr_err("Received unexpected unsolicited data"
1467 " while InitialR2T=Yes, protocol error.\n");
1468 transport_send_check_condition_and_sense(&cmd->se_cmd,
1469 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1470 return -1;
1471 }
1472 /*
1473 * Special case for dealing with Unsolicited DataOUT
1474 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1475 * failures;
1476 */
1477
1478 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001479 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001480 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001481 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001482
1483 if (dump_unsolicited_data) {
1484 /*
1485 * Check if a delayed TASK_ABORTED status needs to
1486 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
Bart Van Assche5a342522015-10-22 15:53:22 -07001487 * received with the unsolicited data out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001488 */
1489 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1490 iscsit_stop_dataout_timer(cmd);
1491
1492 transport_check_aborted_status(se_cmd,
1493 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1494 return iscsit_dump_data_payload(conn, payload_length, 1);
1495 }
1496 } else {
1497 /*
1498 * For the normal solicited data path:
1499 *
1500 * Check for a delayed TASK_ABORTED status and dump any
1501 * incoming data out payload if one exists. Also, when the
1502 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1503 * data out sequence, we decrement outstanding_r2ts. Once
1504 * outstanding_r2ts reaches zero, go ahead and send the delayed
1505 * TASK_ABORTED status.
1506 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001507 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001508 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1509 if (--cmd->outstanding_r2ts < 1) {
1510 iscsit_stop_dataout_timer(cmd);
1511 transport_check_aborted_status(
1512 se_cmd, 1);
1513 }
1514
1515 return iscsit_dump_data_payload(conn, payload_length, 1);
1516 }
1517 }
1518 /*
Bart Van Assche0d5efb82016-12-23 14:37:52 +01001519 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001520 * within-command recovery checks before receiving the payload.
1521 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001522 rc = iscsit_check_pre_dataout(cmd, buf);
1523 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001525 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001526 return -1;
Varun Prakash9a584bf2017-01-13 20:53:21 +05301527 *success = true;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001528 return 0;
1529}
Varun Prakash9a584bf2017-01-13 20:53:21 +05301530EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1531
1532int
1533iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1534 struct iscsi_cmd **out_cmd)
1535{
1536 struct iscsi_data *hdr = buf;
1537 struct iscsi_cmd *cmd;
1538 u32 payload_length = ntoh24(hdr->dlength);
1539 int rc;
1540 bool success = false;
1541
1542 if (!payload_length) {
1543 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1544 return 0;
1545 }
1546
1547 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1548 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1549 " MaxXmitDataSegmentLength: %u\n", payload_length,
1550 conn->conn_ops->MaxXmitDataSegmentLength);
1551 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1552 }
1553
1554 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1555 if (!cmd)
1556 return 0;
1557
1558 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1559
1560 if (success)
1561 *out_cmd = cmd;
1562
1563 return rc;
1564}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001565EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1566
1567static int
1568iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1569 struct iscsi_data *hdr)
1570{
1571 struct kvec *iov;
1572 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1573 u32 payload_length = ntoh24(hdr->dlength);
1574 int iov_ret, data_crc_failed = 0;
1575
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001576 rx_size += payload_length;
1577 iov = &cmd->iov_data[0];
1578
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001579 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1580 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001581 if (iov_ret < 0)
1582 return -1;
1583
1584 iov_count += iov_ret;
1585
1586 padding = ((-payload_length) & 3);
1587 if (padding != 0) {
1588 iov[iov_count].iov_base = cmd->pad_bytes;
1589 iov[iov_count++].iov_len = padding;
1590 rx_size += padding;
1591 pr_debug("Receiving %u padding bytes.\n", padding);
1592 }
1593
1594 if (conn->conn_ops->DataDigest) {
1595 iov[iov_count].iov_base = &checksum;
1596 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1597 rx_size += ISCSI_CRC_LEN;
1598 }
1599
1600 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1601
1602 iscsit_unmap_iovec(cmd);
1603
1604 if (rx_got != rx_size)
1605 return -1;
1606
1607 if (conn->conn_ops->DataDigest) {
1608 u32 data_crc;
1609
Herbert Xu69110e32016-01-24 21:19:52 +08001610 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001611 be32_to_cpu(hdr->offset),
1612 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001613 cmd->pad_bytes);
1614
1615 if (checksum != data_crc) {
1616 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1617 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1618 " does not match computed 0x%08x\n",
1619 hdr->itt, hdr->offset, payload_length,
1620 hdr->datasn, checksum, data_crc);
1621 data_crc_failed = 1;
1622 } else {
1623 pr_debug("Got CRC32C DataDigest 0x%08x for"
1624 " %u bytes of Data Out\n", checksum,
1625 payload_length);
1626 }
1627 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001628
1629 return data_crc_failed;
1630}
1631
1632int
1633iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1634 bool data_crc_failed)
1635{
1636 struct iscsi_conn *conn = cmd->conn;
1637 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001638 /*
1639 * Increment post receive data and CRC values or perform
1640 * within-command recovery.
1641 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001642 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1643 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001644 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001645 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001646 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001647 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1648 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001649 /*
1650 * Handle extra special case for out of order
1651 * Unsolicited Data Out.
1652 */
1653 spin_lock_bh(&cmd->istate_lock);
1654 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1655 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1656 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1657 spin_unlock_bh(&cmd->istate_lock);
1658
1659 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001660 if (ooo_cmdsn)
1661 return 0;
1662 target_execute_cmd(&cmd->se_cmd);
1663 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001664 } else /* DATAOUT_CANNOT_RECOVER */
1665 return -1;
1666
1667 return 0;
1668}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001669EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001670
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001671static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1672{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001673 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001674 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1675 int rc;
1676 bool data_crc_failed = false;
1677
1678 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1679 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001680 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001681 else if (!cmd)
1682 return 0;
1683
1684 rc = iscsit_get_dataout(conn, cmd, hdr);
1685 if (rc < 0)
1686 return rc;
1687 else if (rc > 0)
1688 data_crc_failed = true;
1689
1690 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1691}
1692
Nicholas Bellinger778de362013-06-14 16:07:47 -07001693int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1694 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001695{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001696 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001697
Arshad Hussaina3662602014-03-14 15:28:59 -07001698 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1699 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1700 if (!cmd)
1701 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1702 (unsigned char *)hdr);
1703
1704 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1705 (unsigned char *)hdr);
1706 }
1707
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001708 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001709 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1710 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001711 if (!cmd)
1712 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1713 (unsigned char *)hdr);
1714
Nicholas Bellingerba159912013-07-03 03:48:24 -07001715 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1716 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001717 }
1718
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001719 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001720 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001721 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001722 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001723 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001724 if (!cmd)
1725 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1726 (unsigned char *)hdr);
1727
Nicholas Bellingerba159912013-07-03 03:48:24 -07001728 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1729 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001730 }
1731
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001732 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001733 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001734 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1736 payload_length);
1737 /*
1738 * This is not a response to a Unsolicited NopIN, which means
1739 * it can either be a NOPOUT ping request (with a valid ITT),
1740 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1741 * Either way, make sure we allocate an struct iscsi_cmd, as both
1742 * can contain ping data.
1743 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001744 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1746 cmd->i_state = ISTATE_SEND_NOPIN;
1747 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1748 1 : 0);
1749 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1750 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001751 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1752 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001753 cmd->data_direction = DMA_NONE;
1754 }
1755
Nicholas Bellinger778de362013-06-14 16:07:47 -07001756 return 0;
1757}
1758EXPORT_SYMBOL(iscsit_setup_nop_out);
1759
1760int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1761 struct iscsi_nopout *hdr)
1762{
1763 struct iscsi_cmd *cmd_p = NULL;
1764 int cmdsn_ret = 0;
1765 /*
1766 * Initiator is expecting a NopIN ping reply..
1767 */
1768 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001769 if (!cmd)
1770 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1771 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001772
1773 spin_lock_bh(&conn->cmd_lock);
1774 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1775 spin_unlock_bh(&conn->cmd_lock);
1776
1777 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1778
1779 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1780 iscsit_add_cmd_to_response_queue(cmd, conn,
1781 cmd->i_state);
1782 return 0;
1783 }
1784
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001785 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1786 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001787 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1788 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001789 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001790 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001791
1792 return 0;
1793 }
1794 /*
1795 * This was a response to a unsolicited NOPIN ping.
1796 */
1797 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1798 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1799 if (!cmd_p)
1800 return -EINVAL;
1801
1802 iscsit_stop_nopin_response_timer(conn);
1803
1804 cmd_p->i_state = ISTATE_REMOVE;
1805 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1806
1807 iscsit_start_nopin_timer(conn);
1808 return 0;
1809 }
1810 /*
1811 * Otherwise, initiator is not expecting a NOPIN is response.
1812 * Just ignore for now.
1813 */
Varun Prakash1a40f0a2016-09-15 21:20:11 +05301814
1815 if (cmd)
1816 iscsit_free_cmd(cmd, false);
1817
Nicholas Bellinger778de362013-06-14 16:07:47 -07001818 return 0;
1819}
1820EXPORT_SYMBOL(iscsit_process_nop_out);
1821
1822static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1823 unsigned char *buf)
1824{
1825 unsigned char *ping_data = NULL;
1826 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1827 struct kvec *iov = NULL;
1828 u32 payload_length = ntoh24(hdr->dlength);
1829 int ret;
1830
1831 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1832 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001833 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001834 /*
1835 * Handle NOP-OUT payload for traditional iSCSI sockets
1836 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001837 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001838 u32 checksum, data_crc, padding = 0;
1839 int niov = 0, rx_got, rx_size = payload_length;
1840
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001841 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1842 if (!ping_data) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001843 ret = -1;
1844 goto out;
1845 }
1846
1847 iov = &cmd->iov_misc[0];
1848 iov[niov].iov_base = ping_data;
1849 iov[niov++].iov_len = payload_length;
1850
1851 padding = ((-payload_length) & 3);
1852 if (padding != 0) {
1853 pr_debug("Receiving %u additional bytes"
1854 " for padding.\n", padding);
1855 iov[niov].iov_base = &cmd->pad_bytes;
1856 iov[niov++].iov_len = padding;
1857 rx_size += padding;
1858 }
1859 if (conn->conn_ops->DataDigest) {
1860 iov[niov].iov_base = &checksum;
1861 iov[niov++].iov_len = ISCSI_CRC_LEN;
1862 rx_size += ISCSI_CRC_LEN;
1863 }
1864
1865 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1866 if (rx_got != rx_size) {
1867 ret = -1;
1868 goto out;
1869 }
1870
1871 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08001872 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001873 ping_data, payload_length,
1874 padding, cmd->pad_bytes,
1875 (u8 *)&data_crc);
1876
1877 if (checksum != data_crc) {
1878 pr_err("Ping data CRC32C DataDigest"
1879 " 0x%08x does not match computed 0x%08x\n",
1880 checksum, data_crc);
1881 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1882 pr_err("Unable to recover from"
1883 " NOPOUT Ping DataCRC failure while in"
1884 " ERL=0.\n");
1885 ret = -1;
1886 goto out;
1887 } else {
1888 /*
1889 * Silently drop this PDU and let the
1890 * initiator plug the CmdSN gap.
1891 */
1892 pr_debug("Dropping NOPOUT"
1893 " Command CmdSN: 0x%08x due to"
1894 " DataCRC error.\n", hdr->cmdsn);
1895 ret = 0;
1896 goto out;
1897 }
1898 } else {
1899 pr_debug("Got CRC32C DataDigest"
1900 " 0x%08x for %u bytes of ping data.\n",
1901 checksum, payload_length);
1902 }
1903 }
1904
1905 ping_data[payload_length] = '\0';
1906 /*
1907 * Attach ping data to struct iscsi_cmd->buf_ptr.
1908 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001909 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001910 cmd->buf_ptr_size = payload_length;
1911
1912 pr_debug("Got %u bytes of NOPOUT ping"
1913 " data.\n", payload_length);
1914 pr_debug("Ping Data: \"%s\"\n", ping_data);
1915 }
1916
Nicholas Bellinger778de362013-06-14 16:07:47 -07001917 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001918out:
1919 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001920 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001921
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001922 kfree(ping_data);
1923 return ret;
1924}
1925
Bart Van Asschee381fe92017-01-06 11:32:08 +01001926static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1927{
1928 switch (iscsi_tmf) {
1929 case ISCSI_TM_FUNC_ABORT_TASK:
1930 return TMR_ABORT_TASK;
1931 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1932 return TMR_ABORT_TASK_SET;
1933 case ISCSI_TM_FUNC_CLEAR_ACA:
1934 return TMR_CLEAR_ACA;
1935 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1936 return TMR_CLEAR_TASK_SET;
1937 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1938 return TMR_LUN_RESET;
1939 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1940 return TMR_TARGET_WARM_RESET;
1941 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1942 return TMR_TARGET_COLD_RESET;
1943 default:
1944 return TMR_UNKNOWN;
1945 }
1946}
1947
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001948int
1949iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1950 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001951{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001952 struct se_tmr_req *se_tmr;
1953 struct iscsi_tmr_req *tmr_req;
1954 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001955 int out_of_order_cmdsn = 0, ret;
1956 bool sess_ref = false;
Bart Van Assche59b69862017-01-05 12:39:57 +01001957 u8 function, tcm_function = TMR_UNKNOWN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958
1959 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001960 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1961 function = hdr->flags;
1962
1963 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1964 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1965 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1966 hdr->rtt, hdr->refcmdsn, conn->cid);
1967
1968 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1969 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001970 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001971 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001972 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001973 }
1974
1975 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1976 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1977 pr_err("Task Management Request TASK_REASSIGN not"
1978 " issued as immediate command, bad iSCSI Initiator"
1979 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001980 return iscsit_add_reject_cmd(cmd,
1981 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001982 }
1983 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001984 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1985 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001986
Andy Groverd28b11692012-04-03 15:51:22 -07001987 cmd->data_direction = DMA_NONE;
Markus Elfring3829f382017-04-09 16:00:39 +02001988 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02001989 if (!cmd->tmr_req)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001990 return iscsit_add_reject_cmd(cmd,
1991 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1992 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001993
1994 /*
1995 * TASK_REASSIGN for ERL=2 / connection stays inside of
1996 * LIO-Target $FABRIC_MOD
1997 */
1998 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001999 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07002000 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08002001 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07002002
Bart Van Asscheafc16602015-04-27 13:52:36 +02002003 target_get_sess_cmd(&cmd->se_cmd, true);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002004 sess_ref = true;
Bart Van Asschee381fe92017-01-06 11:32:08 +01002005 tcm_function = iscsit_convert_tmf(function);
2006 if (tcm_function == TMR_UNKNOWN) {
Andy Groverd28b11692012-04-03 15:51:22 -07002007 pr_err("Unknown iSCSI TMR Function:"
2008 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002009 return iscsit_add_reject_cmd(cmd,
2010 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002011 }
Bart Van Assche59b69862017-01-05 12:39:57 +01002012 }
2013 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2014 GFP_KERNEL);
2015 if (ret < 0)
2016 return iscsit_add_reject_cmd(cmd,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002017 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07002018
Bart Van Assche59b69862017-01-05 12:39:57 +01002019 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002020
2021 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2022 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2023 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2024 cmd->init_task_tag = hdr->itt;
2025 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002026 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2027 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002028 se_tmr = cmd->se_cmd.se_tmr_req;
2029 tmr_req = cmd->tmr_req;
2030 /*
2031 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2032 */
2033 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08002034 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
2035 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002036 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002037 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2038 goto attach;
2039 }
2040 }
2041
2042 switch (function) {
2043 case ISCSI_TM_FUNC_ABORT_TASK:
2044 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08002045 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002046 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002047 break;
2048 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2049 case ISCSI_TM_FUNC_CLEAR_ACA:
2050 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2051 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2052 break;
2053 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2054 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002055 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2056 goto attach;
2057 }
2058 break;
2059 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2060 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002061 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2062 goto attach;
2063 }
2064 break;
2065 case ISCSI_TM_FUNC_TASK_REASSIGN:
2066 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2067 /*
2068 * Perform sanity checks on the ExpDataSN only if the
2069 * TASK_REASSIGN was successful.
2070 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08002071 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002072 break;
2073
2074 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002075 return iscsit_add_reject_cmd(cmd,
2076 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002077 break;
2078 default:
2079 pr_err("Unknown TMR function: 0x%02x, protocol"
2080 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002081 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2082 goto attach;
2083 }
2084
2085 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2086 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2087 se_tmr->call_transport = 1;
2088attach:
2089 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002090 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002091 spin_unlock_bh(&conn->cmd_lock);
2092
2093 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002094 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002095 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
2096 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002097 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002098 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002099 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002100 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002101 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002102 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002103
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07002104 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002105 return 0;
2106 /*
2107 * Found the referenced task, send to transport for processing.
2108 */
2109 if (se_tmr->call_transport)
2110 return transport_generic_handle_tmr(&cmd->se_cmd);
2111
2112 /*
2113 * Could not find the referenced LUN, task, or Task Management
2114 * command not authorized or supported. Change state and
2115 * let the tx_thread send the response.
2116 *
2117 * For connection recovery, this is also the default action for
2118 * TMR TASK_REASSIGN.
2119 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002120 if (sess_ref) {
2121 pr_debug("Handle TMR, using sess_ref=true check\n");
Bart Van Asscheafc16602015-04-27 13:52:36 +02002122 target_put_sess_cmd(&cmd->se_cmd);
Nicholas Bellinger186a9642013-07-03 03:11:48 -07002123 }
2124
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2126 return 0;
2127}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002128EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002129
2130/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002131int
2132iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2133 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002134{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002135 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002136
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002137 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002138 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07002139 "greater than MaxXmitDataSegmentLength %u.\n",
2140 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002141 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2142 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002143 }
2144
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08002145 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2146 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2147 pr_err("Multi sequence text commands currently not supported\n");
2148 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2149 (unsigned char *)hdr);
2150 }
2151
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002152 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2153 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2154 hdr->exp_statsn, payload_length);
2155
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002156 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2157 cmd->i_state = ISTATE_SEND_TEXTRSP;
2158 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2159 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2160 cmd->targ_xfer_tag = 0xFFFFFFFF;
2161 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2162 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2163 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002164 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002165
2166 return 0;
2167}
2168EXPORT_SYMBOL(iscsit_setup_text_cmd);
2169
2170int
2171iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2172 struct iscsi_text *hdr)
2173{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002174 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002175 int cmdsn_ret;
2176
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002177 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002178 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2179 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2180 pr_err("Unable to locate text_in buffer for sendtargets"
2181 " discovery\n");
2182 goto reject;
2183 }
2184 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002185 }
2186 if (strncmp("SendTargets", text_in, 11) != 0) {
2187 pr_err("Received Text Data that is not"
2188 " SendTargets, cannot continue.\n");
2189 goto reject;
2190 }
2191 text_ptr = strchr(text_in, '=');
2192 if (!text_ptr) {
2193 pr_err("No \"=\" separator found in Text Data,"
2194 " cannot continue.\n");
2195 goto reject;
2196 }
2197 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002198 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002199 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2200 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002201 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002202 } else {
2203 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2204 goto reject;
2205 }
2206
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002207 spin_lock_bh(&conn->cmd_lock);
2208 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2209 spin_unlock_bh(&conn->cmd_lock);
2210
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002211empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002212 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2213
2214 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002215 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2216 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002217 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002218 return -1;
2219
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002220 return 0;
2221 }
2222
2223 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002224
2225reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002226 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2227 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002228}
2229EXPORT_SYMBOL(iscsit_process_text_cmd);
2230
2231static int
2232iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2233 unsigned char *buf)
2234{
2235 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2236 char *text_in = NULL;
2237 u32 payload_length = ntoh24(hdr->dlength);
2238 int rx_size, rc;
2239
2240 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2241 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002242 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002243
2244 rx_size = payload_length;
2245 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002246 u32 checksum = 0, data_crc = 0;
2247 u32 padding = 0, pad_bytes = 0;
2248 int niov = 0, rx_got;
2249 struct kvec iov[3];
2250
2251 text_in = kzalloc(payload_length, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02002252 if (!text_in)
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002253 goto reject;
Markus Elfringc46e22f2017-04-09 15:34:50 +02002254
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002255 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002256
2257 memset(iov, 0, 3 * sizeof(struct kvec));
2258 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002259 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002260
2261 padding = ((-payload_length) & 3);
2262 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002263 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002264 iov[niov++].iov_len = padding;
2265 rx_size += padding;
2266 pr_debug("Receiving %u additional bytes"
2267 " for padding.\n", padding);
2268 }
2269 if (conn->conn_ops->DataDigest) {
2270 iov[niov].iov_base = &checksum;
2271 iov[niov++].iov_len = ISCSI_CRC_LEN;
2272 rx_size += ISCSI_CRC_LEN;
2273 }
2274
2275 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002276 if (rx_got != rx_size)
2277 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002278
2279 if (conn->conn_ops->DataDigest) {
Herbert Xu69110e32016-01-24 21:19:52 +08002280 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002281 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002282 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002283 (u8 *)&data_crc);
2284
2285 if (checksum != data_crc) {
2286 pr_err("Text data CRC32C DataDigest"
2287 " 0x%08x does not match computed"
2288 " 0x%08x\n", checksum, data_crc);
2289 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2290 pr_err("Unable to recover from"
2291 " Text Data digest failure while in"
2292 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002293 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002294 } else {
2295 /*
2296 * Silently drop this PDU and let the
2297 * initiator plug the CmdSN gap.
2298 */
2299 pr_debug("Dropping Text"
2300 " Command CmdSN: 0x%08x due to"
2301 " DataCRC error.\n", hdr->cmdsn);
2302 kfree(text_in);
2303 return 0;
2304 }
2305 } else {
2306 pr_debug("Got CRC32C DataDigest"
2307 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002308 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002309 }
2310 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002311 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002312 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002313 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314 }
2315
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002316 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002318reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002319 kfree(cmd->text_in_ptr);
2320 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002321 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322}
2323
2324int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2325{
2326 struct iscsi_conn *conn_p;
2327 struct iscsi_session *sess = conn->sess;
2328
2329 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2330 " for SID: %u.\n", conn->cid, conn->sess->sid);
2331
2332 atomic_set(&sess->session_logout, 1);
2333 atomic_set(&conn->conn_logout_remove, 1);
2334 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2335
2336 iscsit_inc_conn_usage_count(conn);
2337 iscsit_inc_session_usage_count(sess);
2338
2339 spin_lock_bh(&sess->conn_lock);
2340 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2341 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2342 continue;
2343
2344 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2345 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2346 }
2347 spin_unlock_bh(&sess->conn_lock);
2348
2349 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2350
2351 return 0;
2352}
2353
2354int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2355{
2356 struct iscsi_conn *l_conn;
2357 struct iscsi_session *sess = conn->sess;
2358
2359 pr_debug("Received logout request CLOSECONNECTION for CID:"
2360 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2361
2362 /*
2363 * A Logout Request with a CLOSECONNECTION reason code for a CID
2364 * can arrive on a connection with a differing CID.
2365 */
2366 if (conn->cid == cmd->logout_cid) {
2367 spin_lock_bh(&conn->state_lock);
2368 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2369 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2370
2371 atomic_set(&conn->conn_logout_remove, 1);
2372 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2373 iscsit_inc_conn_usage_count(conn);
2374
2375 spin_unlock_bh(&conn->state_lock);
2376 } else {
2377 /*
2378 * Handle all different cid CLOSECONNECTION requests in
2379 * iscsit_logout_post_handler_diffcid() as to give enough
2380 * time for any non immediate command's CmdSN to be
2381 * acknowledged on the connection in question.
2382 *
2383 * Here we simply make sure the CID is still around.
2384 */
2385 l_conn = iscsit_get_conn_from_cid(sess,
2386 cmd->logout_cid);
2387 if (!l_conn) {
2388 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2389 iscsit_add_cmd_to_response_queue(cmd, conn,
2390 cmd->i_state);
2391 return 0;
2392 }
2393
2394 iscsit_dec_conn_usage_count(l_conn);
2395 }
2396
2397 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2398
2399 return 0;
2400}
2401
2402int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2403{
2404 struct iscsi_session *sess = conn->sess;
2405
2406 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2407 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2408
2409 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2410 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2411 " while ERL!=2.\n");
2412 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2413 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2414 return 0;
2415 }
2416
2417 if (conn->cid == cmd->logout_cid) {
2418 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2419 " with CID: %hu on CID: %hu, implementation error.\n",
2420 cmd->logout_cid, conn->cid);
2421 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2422 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2423 return 0;
2424 }
2425
2426 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2427
2428 return 0;
2429}
2430
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002431int
2432iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2433 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002434{
2435 int cmdsn_ret, logout_remove = 0;
2436 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002437 struct iscsi_logout *hdr;
2438 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2439
2440 hdr = (struct iscsi_logout *) buf;
2441 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442
2443 if (tiqn) {
2444 spin_lock(&tiqn->logout_stats.lock);
2445 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2446 tiqn->logout_stats.normal_logouts++;
2447 else
2448 tiqn->logout_stats.abnormal_logouts++;
2449 spin_unlock(&tiqn->logout_stats.lock);
2450 }
2451
2452 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2453 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2454 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2455 hdr->cid, conn->cid);
2456
2457 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2458 pr_err("Received logout request on connection that"
2459 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002460 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002461 return 0;
2462 }
2463
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002464 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2465 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2466 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2467 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2468 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002469 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2470 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2471 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002472 cmd->logout_reason = reason_code;
2473 cmd->data_direction = DMA_NONE;
2474
2475 /*
2476 * We need to sleep in these cases (by returning 1) until the Logout
2477 * Response gets sent in the tx thread.
2478 */
2479 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2480 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002481 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002482 logout_remove = 1;
2483
2484 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002485 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002486 spin_unlock_bh(&conn->cmd_lock);
2487
2488 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002489 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002490
2491 /*
2492 * Immediate commands are executed, well, immediately.
2493 * Non-Immediate Logout Commands are executed in CmdSN order.
2494 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002495 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002496 int ret = iscsit_execute_cmd(cmd, 0);
2497
2498 if (ret < 0)
2499 return ret;
2500 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002501 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002502 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002503 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002504 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2505 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002506 }
2507
2508 return logout_remove;
2509}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002510EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002511
Varun Prakashd2faaef2016-04-20 00:00:19 +05302512int iscsit_handle_snack(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002513 struct iscsi_conn *conn,
2514 unsigned char *buf)
2515{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002516 struct iscsi_snack *hdr;
2517
2518 hdr = (struct iscsi_snack *) buf;
2519 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520
2521 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2522 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2523 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2524 hdr->begrun, hdr->runlength, conn->cid);
2525
2526 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2527 pr_err("Initiator sent SNACK request while in"
2528 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002529 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2530 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002531 }
2532 /*
2533 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2534 * call from inside iscsi_send_recovery_datain_or_r2t().
2535 */
2536 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2537 case 0:
2538 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002539 hdr->itt,
2540 be32_to_cpu(hdr->ttt),
2541 be32_to_cpu(hdr->begrun),
2542 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002543 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002544 return iscsit_handle_status_snack(conn, hdr->itt,
2545 be32_to_cpu(hdr->ttt),
2546 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002547 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002548 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2549 be32_to_cpu(hdr->begrun),
2550 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002551 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2552 /* FIXME: Support R-Data SNACK */
2553 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002554 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2555 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002556 default:
2557 pr_err("Unknown SNACK type 0x%02x, protocol"
2558 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002559 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2560 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002561 }
2562
2563 return 0;
2564}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302565EXPORT_SYMBOL(iscsit_handle_snack);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002566
2567static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2568{
2569 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2570 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2571 wait_for_completion_interruptible_timeout(
2572 &conn->rx_half_close_comp,
2573 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2574 }
2575}
2576
2577static int iscsit_handle_immediate_data(
2578 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002579 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002580 u32 length)
2581{
2582 int iov_ret, rx_got = 0, rx_size = 0;
2583 u32 checksum, iov_count = 0, padding = 0;
2584 struct iscsi_conn *conn = cmd->conn;
2585 struct kvec *iov;
2586
2587 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2588 if (iov_ret < 0)
2589 return IMMEDIATE_DATA_CANNOT_RECOVER;
2590
2591 rx_size = length;
2592 iov_count = iov_ret;
2593 iov = &cmd->iov_data[0];
2594
2595 padding = ((-length) & 3);
2596 if (padding != 0) {
2597 iov[iov_count].iov_base = cmd->pad_bytes;
2598 iov[iov_count++].iov_len = padding;
2599 rx_size += padding;
2600 }
2601
2602 if (conn->conn_ops->DataDigest) {
2603 iov[iov_count].iov_base = &checksum;
2604 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2605 rx_size += ISCSI_CRC_LEN;
2606 }
2607
2608 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2609
2610 iscsit_unmap_iovec(cmd);
2611
2612 if (rx_got != rx_size) {
2613 iscsit_rx_thread_wait_for_tcp(conn);
2614 return IMMEDIATE_DATA_CANNOT_RECOVER;
2615 }
2616
2617 if (conn->conn_ops->DataDigest) {
2618 u32 data_crc;
2619
Herbert Xu69110e32016-01-24 21:19:52 +08002620 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002621 cmd->write_data_done, length, padding,
2622 cmd->pad_bytes);
2623
2624 if (checksum != data_crc) {
2625 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2626 " does not match computed 0x%08x\n", checksum,
2627 data_crc);
2628
2629 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2630 pr_err("Unable to recover from"
2631 " Immediate Data digest failure while"
2632 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002633 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002635 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002636 return IMMEDIATE_DATA_CANNOT_RECOVER;
2637 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002638 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002639 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002640 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002641 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2642 }
2643 } else {
2644 pr_debug("Got CRC32C DataDigest 0x%08x for"
2645 " %u bytes of Immediate Data\n", checksum,
2646 length);
2647 }
2648 }
2649
2650 cmd->write_data_done += length;
2651
Andy Groverebf1d952012-04-03 15:51:24 -07002652 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002653 spin_lock_bh(&cmd->istate_lock);
2654 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2655 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2656 spin_unlock_bh(&cmd->istate_lock);
2657 }
2658
2659 return IMMEDIATE_DATA_NORMAL_OPERATION;
2660}
2661
2662/*
2663 * Called with sess->conn_lock held.
2664 */
2665/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2666 with active network interface */
2667static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2668{
2669 struct iscsi_cmd *cmd;
2670 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002671 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002672
2673 /*
2674 * Only send a Asynchronous Message on connections whos network
2675 * interface is still functional.
2676 */
2677 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2678 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2679 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002680 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002681 break;
2682 }
2683 }
2684
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002685 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002686 return;
2687
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002688 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002689 if (!cmd) {
2690 iscsit_dec_conn_usage_count(conn_p);
2691 return;
2692 }
2693
2694 cmd->logout_cid = conn->cid;
2695 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2696 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2697
2698 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002699 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002700 spin_unlock_bh(&conn_p->cmd_lock);
2701
2702 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2703 iscsit_dec_conn_usage_count(conn_p);
2704}
2705
2706static int iscsit_send_conn_drop_async_message(
2707 struct iscsi_cmd *cmd,
2708 struct iscsi_conn *conn)
2709{
2710 struct iscsi_async *hdr;
2711
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002712 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2713
2714 hdr = (struct iscsi_async *) cmd->pdu;
2715 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2716 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002717 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002718 cmd->targ_xfer_tag = 0xFFFFFFFF;
2719 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2720 cmd->stat_sn = conn->stat_sn++;
2721 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2722 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002723 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002724 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2725 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2726 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2727 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2728
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002729 pr_debug("Sending Connection Dropped Async Message StatSN:"
2730 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2731 cmd->logout_cid, conn->cid);
Varun Prakash2854bb22016-04-20 00:00:08 +05302732
2733 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002734}
2735
Andy Grover6f3c0e62012-04-03 15:51:09 -07002736static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2737{
2738 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2739 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2740 wait_for_completion_interruptible_timeout(
2741 &conn->tx_half_close_comp,
2742 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2743 }
2744}
2745
Varun Prakashd2faaef2016-04-20 00:00:19 +05302746void
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002747iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2748 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2749 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002750{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002751 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2752 hdr->flags = datain->flags;
2753 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2754 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2755 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2756 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2757 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2758 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2759 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2760 }
2761 }
2762 hton24(hdr->dlength, datain->length);
2763 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2764 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2765 (struct scsi_lun *)&hdr->lun);
2766 else
2767 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2768
2769 hdr->itt = cmd->init_task_tag;
2770
2771 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2772 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2773 else
2774 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2775 if (set_statsn)
2776 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2777 else
2778 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2779
2780 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002781 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002782 hdr->datasn = cpu_to_be32(datain->data_sn);
2783 hdr->offset = cpu_to_be32(datain->offset);
2784
2785 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2786 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2787 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2788 ntohl(hdr->offset), datain->length, conn->cid);
2789}
Varun Prakashd2faaef2016-04-20 00:00:19 +05302790EXPORT_SYMBOL(iscsit_build_datain_pdu);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002791
2792static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2793{
2794 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002795 struct iscsi_datain datain;
2796 struct iscsi_datain_req *dr;
Varun Prakash2854bb22016-04-20 00:00:08 +05302797 int eodr = 0, ret;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002798 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002799
2800 memset(&datain, 0, sizeof(struct iscsi_datain));
2801 dr = iscsit_get_datain_values(cmd, &datain);
2802 if (!dr) {
2803 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2804 cmd->init_task_tag);
2805 return -1;
2806 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002807 /*
2808 * Be paranoid and double check the logic for now.
2809 */
Andy Groverebf1d952012-04-03 15:51:24 -07002810 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002811 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2812 " datain.length: %u exceeds cmd->data_length: %u\n",
2813 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002814 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002815 return -1;
2816 }
2817
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002818 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002819 /*
2820 * Special case for successfully execution w/ both DATAIN
2821 * and Sense Data.
2822 */
2823 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2824 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2825 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2826 else {
2827 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2828 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2829 iscsit_increment_maxcmdsn(cmd, conn->sess);
2830 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002831 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002832 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002833 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2834 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002835 }
2836
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002837 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002838
Varun Prakash2854bb22016-04-20 00:00:08 +05302839 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2840 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07002841 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07002842
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002843 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002844 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002845 2 : 1;
2846 iscsit_free_datain_req(cmd, dr);
2847 }
2848
Andy Grover6f3c0e62012-04-03 15:51:09 -07002849 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850}
2851
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002852int
2853iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2854 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002855{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002856 struct iscsi_conn *logout_conn = NULL;
2857 struct iscsi_conn_recovery *cr = NULL;
2858 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002859 /*
2860 * The actual shutting down of Sessions and/or Connections
2861 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2862 * is done in scsi_logout_post_handler().
2863 */
2864 switch (cmd->logout_reason) {
2865 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2866 pr_debug("iSCSI session logout successful, setting"
2867 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2868 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2869 break;
2870 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2871 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2872 break;
2873 /*
2874 * For CLOSECONNECTION logout requests carrying
2875 * a matching logout CID -> local CID, the reference
2876 * for the local CID will have been incremented in
2877 * iscsi_logout_closeconnection().
2878 *
2879 * For CLOSECONNECTION logout requests carrying
2880 * a different CID than the connection it arrived
2881 * on, the connection responding to cmd->logout_cid
2882 * is stopped in iscsit_logout_post_handler_diffcid().
2883 */
2884
2885 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2886 " successful.\n", cmd->logout_cid, conn->cid);
2887 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2888 break;
2889 case ISCSI_LOGOUT_REASON_RECOVERY:
2890 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2891 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2892 break;
2893 /*
2894 * If the connection is still active from our point of view
2895 * force connection recovery to occur.
2896 */
2897 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2898 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002899 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002900 iscsit_connection_reinstatement_rcfr(logout_conn);
2901 iscsit_dec_conn_usage_count(logout_conn);
2902 }
2903
2904 cr = iscsit_get_inactive_connection_recovery_entry(
2905 conn->sess, cmd->logout_cid);
2906 if (!cr) {
2907 pr_err("Unable to locate CID: %hu for"
2908 " REMOVECONNFORRECOVERY Logout Request.\n",
2909 cmd->logout_cid);
2910 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2911 break;
2912 }
2913
2914 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2915
2916 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2917 " for recovery for CID: %hu on CID: %hu successful.\n",
2918 cmd->logout_cid, conn->cid);
2919 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2920 break;
2921 default:
2922 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2923 cmd->logout_reason);
2924 return -1;
2925 }
2926
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002927 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2928 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2929 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002930 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931 cmd->stat_sn = conn->stat_sn++;
2932 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2933
2934 iscsit_increment_maxcmdsn(cmd, conn->sess);
2935 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002936 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002937
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002938 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2939 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2940 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2941 cmd->logout_cid, conn->cid);
2942
2943 return 0;
2944}
2945EXPORT_SYMBOL(iscsit_build_logout_rsp);
2946
2947static int
2948iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2949{
Varun Prakash2854bb22016-04-20 00:00:08 +05302950 int rc;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002951
2952 rc = iscsit_build_logout_rsp(cmd, conn,
2953 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2954 if (rc < 0)
2955 return rc;
2956
Varun Prakash2854bb22016-04-20 00:00:08 +05302957 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002958}
2959
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002960void
2961iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2962 struct iscsi_nopin *hdr, bool nopout_response)
2963{
2964 hdr->opcode = ISCSI_OP_NOOP_IN;
2965 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2966 hton24(hdr->dlength, cmd->buf_ptr_size);
2967 if (nopout_response)
2968 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2969 hdr->itt = cmd->init_task_tag;
2970 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2971 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2972 conn->stat_sn;
2973 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2974
2975 if (nopout_response)
2976 iscsit_increment_maxcmdsn(cmd, conn->sess);
2977
2978 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07002979 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002980
2981 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2982 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
Colin Ian King3fc6a642016-09-02 15:30:34 +01002983 "Solicited" : "Unsolicited", cmd->init_task_tag,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002984 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2985}
2986EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2987
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002988/*
2989 * Unsolicited NOPIN, either requesting a response or not.
2990 */
2991static int iscsit_send_unsolicited_nopin(
2992 struct iscsi_cmd *cmd,
2993 struct iscsi_conn *conn,
2994 int want_response)
2995{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002996 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Varun Prakash2854bb22016-04-20 00:00:08 +05302997 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002998
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002999 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003000
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003001 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3002 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3003
Varun Prakash2854bb22016-04-20 00:00:08 +05303004 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3005 if (ret < 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003006 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003007
3008 spin_lock_bh(&cmd->istate_lock);
3009 cmd->i_state = want_response ?
3010 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3011 spin_unlock_bh(&cmd->istate_lock);
3012
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003013 return 0;
3014}
3015
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003016static int
3017iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003018{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003019 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003020
3021 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003022
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003023 /*
3024 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3025 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3026 */
Varun Prakash2854bb22016-04-20 00:00:08 +05303027 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028
Varun Prakash2854bb22016-04-20 00:00:08 +05303029 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3030 cmd->buf_ptr,
3031 cmd->buf_ptr_size);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003032}
3033
Andy Grover6f3c0e62012-04-03 15:51:09 -07003034static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003035 struct iscsi_cmd *cmd,
3036 struct iscsi_conn *conn)
3037{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003038 struct iscsi_r2t *r2t;
3039 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003040 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003041
3042 r2t = iscsit_get_r2t_from_list(cmd);
3043 if (!r2t)
3044 return -1;
3045
3046 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3047 memset(hdr, 0, ISCSI_HDR_LEN);
3048 hdr->opcode = ISCSI_OP_R2T;
3049 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3050 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3051 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003052 hdr->itt = cmd->init_task_tag;
Varun Prakash85672702016-04-20 00:00:13 +05303053 if (conn->conn_transport->iscsit_get_r2t_ttt)
3054 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3055 else
3056 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003057 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3058 hdr->statsn = cpu_to_be32(conn->stat_sn);
3059 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003060 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003061 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3062 hdr->data_offset = cpu_to_be32(r2t->offset);
3063 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3064
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003065 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3066 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3067 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3068 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3069 r2t->offset, r2t->xfer_len, conn->cid);
3070
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003071 spin_lock_bh(&cmd->r2t_lock);
3072 r2t->sent_r2t = 1;
3073 spin_unlock_bh(&cmd->r2t_lock);
3074
Varun Prakash2854bb22016-04-20 00:00:08 +05303075 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003076 if (ret < 0) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07003077 return ret;
3078 }
3079
3080 spin_lock_bh(&cmd->dataout_timeout_lock);
3081 iscsit_start_dataout_timer(cmd, conn);
3082 spin_unlock_bh(&cmd->dataout_timeout_lock);
3083
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003084 return 0;
3085}
3086
3087/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003088 * @recovery: If called from iscsi_task_reassign_complete_write() for
3089 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003090 */
3091int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003092 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003093 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003094 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003095{
3096 int first_r2t = 1;
3097 u32 offset = 0, xfer_len = 0;
3098
3099 spin_lock_bh(&cmd->r2t_lock);
3100 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3101 spin_unlock_bh(&cmd->r2t_lock);
3102 return 0;
3103 }
3104
Andy Grover8b1e1242012-04-03 15:51:12 -07003105 if (conn->sess->sess_ops->DataSequenceInOrder &&
3106 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003107 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108
3109 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3110 if (conn->sess->sess_ops->DataSequenceInOrder) {
3111 offset = cmd->r2t_offset;
3112
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 if (first_r2t && recovery) {
3114 int new_data_end = offset +
3115 conn->sess->sess_ops->MaxBurstLength -
3116 cmd->next_burst_len;
3117
Andy Groverebf1d952012-04-03 15:51:24 -07003118 if (new_data_end > cmd->se_cmd.data_length)
3119 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003120 else
3121 xfer_len =
3122 conn->sess->sess_ops->MaxBurstLength -
3123 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003124 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003125 int new_data_end = offset +
3126 conn->sess->sess_ops->MaxBurstLength;
3127
Andy Groverebf1d952012-04-03 15:51:24 -07003128 if (new_data_end > cmd->se_cmd.data_length)
3129 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003130 else
3131 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003132 }
3133 cmd->r2t_offset += xfer_len;
3134
Andy Groverebf1d952012-04-03 15:51:24 -07003135 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003136 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3137 } else {
3138 struct iscsi_seq *seq;
3139
3140 seq = iscsit_get_seq_holder_for_r2t(cmd);
3141 if (!seq) {
3142 spin_unlock_bh(&cmd->r2t_lock);
3143 return -1;
3144 }
3145
3146 offset = seq->offset;
3147 xfer_len = seq->xfer_len;
3148
3149 if (cmd->seq_send_order == cmd->seq_count)
3150 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3151 }
3152 cmd->outstanding_r2ts++;
3153 first_r2t = 0;
3154
3155 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3156 spin_unlock_bh(&cmd->r2t_lock);
3157 return -1;
3158 }
3159
3160 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3161 break;
3162 }
3163 spin_unlock_bh(&cmd->r2t_lock);
3164
3165 return 0;
3166}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303167EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003168
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003169void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3170 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003171{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003172 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003173 cmd->stat_sn = conn->stat_sn++;
3174
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003175 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003176
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003177 memset(hdr, 0, ISCSI_HDR_LEN);
3178 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3179 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3180 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3181 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003182 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3184 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003185 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186 }
3187 hdr->response = cmd->iscsi_response;
3188 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003189 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003190 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3191
3192 iscsit_increment_maxcmdsn(cmd, conn->sess);
3193 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003194 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003195
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003196 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3197 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3198 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3199 cmd->se_cmd.scsi_status, conn->cid);
3200}
3201EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3202
3203static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3204{
3205 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003206 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
Varun Prakash2854bb22016-04-20 00:00:08 +05303207 void *data_buf = NULL;
3208 u32 padding = 0, data_buf_len = 0;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003209
3210 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3211
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003212 /*
3213 * Attach SENSE DATA payload to iSCSI Response PDU
3214 */
3215 if (cmd->se_cmd.sense_buffer &&
3216 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3217 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003218 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3219 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3220
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003221 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003222 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Varun Prakash2854bb22016-04-20 00:00:08 +05303223 data_buf = cmd->sense_buffer;
3224 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003225
3226 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003227 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003228 cmd->se_cmd.scsi_sense_length, 0, padding);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 pr_debug("Adding %u bytes of padding to"
3230 " SENSE.\n", padding);
3231 }
3232
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003233 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3234 " Response PDU\n",
3235 cmd->se_cmd.scsi_sense_length);
3236 }
3237
Varun Prakash2854bb22016-04-20 00:00:08 +05303238 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3239 data_buf_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003240}
3241
3242static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3243{
3244 switch (se_tmr->response) {
3245 case TMR_FUNCTION_COMPLETE:
3246 return ISCSI_TMF_RSP_COMPLETE;
3247 case TMR_TASK_DOES_NOT_EXIST:
3248 return ISCSI_TMF_RSP_NO_TASK;
3249 case TMR_LUN_DOES_NOT_EXIST:
3250 return ISCSI_TMF_RSP_NO_LUN;
3251 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3252 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003253 case TMR_FUNCTION_REJECTED:
3254 default:
3255 return ISCSI_TMF_RSP_REJECTED;
3256 }
3257}
3258
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003259void
3260iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3261 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003262{
3263 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003264
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003265 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003266 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003267 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003268 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003269 cmd->stat_sn = conn->stat_sn++;
3270 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3271
3272 iscsit_increment_maxcmdsn(cmd, conn->sess);
3273 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003274 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003275
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003276 pr_debug("Built Task Management Response ITT: 0x%08x,"
3277 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3278 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3279}
3280EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3281
3282static int
3283iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3284{
3285 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003286
3287 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3288
Varun Prakash2854bb22016-04-20 00:00:08 +05303289 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003290}
3291
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003292static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3293{
3294 bool ret = false;
3295
3296 if (np->np_sockaddr.ss_family == AF_INET6) {
3297 const struct sockaddr_in6 sin6 = {
3298 .sin6_addr = IN6ADDR_ANY_INIT };
3299 struct sockaddr_in6 *sock_in6 =
3300 (struct sockaddr_in6 *)&np->np_sockaddr;
3301
3302 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3303 sin6.sin6_addr.s6_addr, 16))
3304 ret = true;
3305 } else {
3306 struct sockaddr_in * sock_in =
3307 (struct sockaddr_in *)&np->np_sockaddr;
3308
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003309 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003310 ret = true;
3311 }
3312
3313 return ret;
3314}
3315
Andy Grover8b1e1242012-04-03 15:51:12 -07003316#define SENDTARGETS_BUF_LIMIT 32768U
3317
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003318static int
3319iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003320 enum iscsit_transport_type network_transport,
3321 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003322{
3323 char *payload = NULL;
3324 struct iscsi_conn *conn = cmd->conn;
3325 struct iscsi_portal_group *tpg;
3326 struct iscsi_tiqn *tiqn;
3327 struct iscsi_tpg_np *tpg_np;
3328 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003329 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003330 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003331 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
David Disseldorpa6415cd2015-08-01 00:10:12 -07003332 bool active;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003333
Sagi Grimbergbe7dcfb2015-01-26 12:49:06 +02003334 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003335 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003336
3337 payload = kzalloc(buffer_len, GFP_KERNEL);
Markus Elfringc46e22f2017-04-09 15:34:50 +02003338 if (!payload)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003339 return -ENOMEM;
Markus Elfringc46e22f2017-04-09 15:34:50 +02003340
Nicholas Bellinger66658892013-06-19 22:45:42 -07003341 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003342 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003343 * explicit case..
3344 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003345 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003346 text_ptr = strchr(text_in, '=');
3347 if (!text_ptr) {
3348 pr_err("Unable to locate '=' string in text_in:"
3349 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003350 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003351 return -EINVAL;
3352 }
3353 /*
3354 * Skip over '=' character..
3355 */
3356 text_ptr += 1;
3357 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003358
3359 spin_lock(&tiqn_lock);
3360 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003361 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003362 strcmp(tiqn->tiqn, text_ptr)) {
3363 continue;
3364 }
3365
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003366 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003367
3368 spin_lock(&tiqn->tiqn_tpg_lock);
3369 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3370
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003371 /* If demo_mode_discovery=0 and generate_node_acls=0
3372 * (demo mode dislabed) do not return
3373 * TargetName+TargetAddress unless a NodeACL exists.
3374 */
3375
3376 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3377 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
Nicholas Bellinger21aaa232016-01-07 22:09:27 -08003378 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003379 cmd->conn->sess->sess_ops->InitiatorName))) {
3380 continue;
3381 }
3382
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003383 spin_lock(&tpg->tpg_state_lock);
David Disseldorpa6415cd2015-08-01 00:10:12 -07003384 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003385 spin_unlock(&tpg->tpg_state_lock);
3386
David Disseldorpa6415cd2015-08-01 00:10:12 -07003387 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3388 continue;
3389
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003390 spin_lock(&tpg->tpg_np_lock);
3391 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3392 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003393 struct iscsi_np *np = tpg_np->tpg_np;
3394 bool inaddr_any = iscsit_check_inaddr_any(np);
Andy Grover13a3cf02015-08-24 10:26:06 -07003395 struct sockaddr_storage *sockaddr;
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003396
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003397 if (np->np_network_transport != network_transport)
3398 continue;
3399
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003400 if (!target_name_printed) {
3401 len = sprintf(buf, "TargetName=%s",
3402 tiqn->tiqn);
3403 len += 1;
3404
3405 if ((len + payload_len) > buffer_len) {
3406 spin_unlock(&tpg->tpg_np_lock);
3407 spin_unlock(&tiqn->tiqn_tpg_lock);
3408 end_of_buf = 1;
3409 goto eob;
3410 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003411
3412 if (skip_bytes && len <= skip_bytes) {
3413 skip_bytes -= len;
3414 } else {
3415 memcpy(payload + payload_len, buf, len);
3416 payload_len += len;
3417 target_name_printed = 1;
3418 if (len > skip_bytes)
3419 skip_bytes = 0;
3420 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003421 }
3422
Andy Grover69d75572015-08-24 10:26:04 -07003423 if (inaddr_any)
3424 sockaddr = &conn->local_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003425 else
Andy Grover69d75572015-08-24 10:26:04 -07003426 sockaddr = &np->np_sockaddr;
Andy Grover1997e622015-03-31 10:43:18 -07003427
Andy Grover69d75572015-08-24 10:26:04 -07003428 len = sprintf(buf, "TargetAddress="
3429 "%pISpc,%hu",
3430 sockaddr,
3431 tpg->tpgt);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003432 len += 1;
3433
3434 if ((len + payload_len) > buffer_len) {
3435 spin_unlock(&tpg->tpg_np_lock);
3436 spin_unlock(&tiqn->tiqn_tpg_lock);
3437 end_of_buf = 1;
3438 goto eob;
3439 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003440
3441 if (skip_bytes && len <= skip_bytes) {
3442 skip_bytes -= len;
3443 } else {
3444 memcpy(payload + payload_len, buf, len);
3445 payload_len += len;
3446 if (len > skip_bytes)
3447 skip_bytes = 0;
3448 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003449 }
3450 spin_unlock(&tpg->tpg_np_lock);
3451 }
3452 spin_unlock(&tiqn->tiqn_tpg_lock);
3453eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003454 if (end_of_buf) {
3455 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003456 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003457 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003458
Andy Grover8060b8d2015-01-09 15:13:08 -08003459 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003460 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003461 }
3462 spin_unlock(&tiqn_lock);
3463
3464 cmd->buf_ptr = payload;
3465
3466 return payload_len;
3467}
3468
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003469int
3470iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003471 struct iscsi_text_rsp *hdr,
3472 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003473{
3474 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003475 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003476
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003477 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3478 cmd->read_data_done,
3479 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003480 if (text_length < 0)
3481 return text_length;
3482
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003483 if (completed) {
3484 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3485 } else {
3486 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3487 cmd->read_data_done += text_length;
3488 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3489 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3490 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003491 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003492 padding = ((-text_length) & 3);
3493 hton24(hdr->dlength, text_length);
3494 hdr->itt = cmd->init_task_tag;
3495 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3496 cmd->stat_sn = conn->stat_sn++;
3497 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3498
3499 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003500 /*
3501 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3502 * correctly increment MaxCmdSN for each response answering a
3503 * non immediate text request with a valid CmdSN.
3504 */
3505 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003506 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003507 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003508
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003509 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3510 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3511 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3512 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3513 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003514
3515 return text_length + padding;
3516}
3517EXPORT_SYMBOL(iscsit_build_text_rsp);
3518
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003519static int iscsit_send_text_rsp(
3520 struct iscsi_cmd *cmd,
3521 struct iscsi_conn *conn)
3522{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003523 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Varun Prakash2854bb22016-04-20 00:00:08 +05303524 int text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003525
Varun Prakash864e5042016-04-20 00:00:15 +05303526 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3527 conn->conn_transport->transport_type);
Varun Prakash2854bb22016-04-20 00:00:08 +05303528 if (text_length < 0)
3529 return text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003530
Varun Prakash2854bb22016-04-20 00:00:08 +05303531 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3532 cmd->buf_ptr,
3533 text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003534}
3535
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003536void
3537iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3538 struct iscsi_reject *hdr)
3539{
3540 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003541 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003542 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3543 hton24(hdr->dlength, ISCSI_HDR_LEN);
3544 hdr->ffffffff = cpu_to_be32(0xffffffff);
3545 cmd->stat_sn = conn->stat_sn++;
3546 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3547 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
Roland Dreier109e2382015-07-23 14:53:32 -07003548 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003549
3550}
3551EXPORT_SYMBOL(iscsit_build_reject);
3552
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003553static int iscsit_send_reject(
3554 struct iscsi_cmd *cmd,
3555 struct iscsi_conn *conn)
3556{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003557 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003558
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003559 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003560
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003561 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3562 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3563
Varun Prakash2854bb22016-04-20 00:00:08 +05303564 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3565 cmd->buf_ptr,
3566 ISCSI_HDR_LEN);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003567}
3568
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003569void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3570{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003571 int ord, cpu;
3572 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003573 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3574 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003575 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003576 * Here we use bitmap_id to determine which CPU that this
3577 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003578 * execute upon.
3579 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003580 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003581 for_each_online_cpu(cpu) {
3582 if (ord-- == 0) {
3583 cpumask_set_cpu(cpu, conn->conn_cpumask);
3584 return;
3585 }
3586 }
3587 /*
3588 * This should never be reached..
3589 */
3590 dump_stack();
3591 cpumask_setall(conn->conn_cpumask);
3592}
3593
Varun Prakashd2faaef2016-04-20 00:00:19 +05303594int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003595iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003596{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003597 int ret;
3598
3599 switch (state) {
3600 case ISTATE_SEND_R2T:
3601 ret = iscsit_send_r2t(cmd, conn);
3602 if (ret < 0)
3603 goto err;
3604 break;
3605 case ISTATE_REMOVE:
3606 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003607 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003608 spin_unlock_bh(&conn->cmd_lock);
3609
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003610 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003611 break;
3612 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3613 iscsit_mod_nopin_response_timer(conn);
3614 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3615 if (ret < 0)
3616 goto err;
3617 break;
3618 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3619 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3620 if (ret < 0)
3621 goto err;
3622 break;
3623 default:
3624 pr_err("Unknown Opcode: 0x%02x ITT:"
3625 " 0x%08x, i_state: %d on CID: %hu\n",
3626 cmd->iscsi_opcode, cmd->init_task_tag, state,
3627 conn->cid);
3628 goto err;
3629 }
3630
3631 return 0;
3632
3633err:
3634 return -1;
3635}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303636EXPORT_SYMBOL(iscsit_immediate_queue);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003637
3638static int
3639iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3640{
3641 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003642 struct iscsi_queue_req *qr;
3643 struct iscsi_cmd *cmd;
3644 u8 state;
3645 int ret;
3646
3647 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3648 atomic_set(&conn->check_immediate_queue, 0);
3649 cmd = qr->cmd;
3650 state = qr->state;
3651 kmem_cache_free(lio_qr_cache, qr);
3652
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003653 ret = t->iscsit_immediate_queue(conn, cmd, state);
3654 if (ret < 0)
3655 return ret;
3656 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003657
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003658 return 0;
3659}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003660
Varun Prakashd2faaef2016-04-20 00:00:19 +05303661int
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003662iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3663{
3664 int ret;
3665
3666check_rsp_state:
3667 switch (state) {
3668 case ISTATE_SEND_DATAIN:
3669 ret = iscsit_send_datain(cmd, conn);
3670 if (ret < 0)
3671 goto err;
3672 else if (!ret)
3673 /* more drs */
3674 goto check_rsp_state;
3675 else if (ret == 1) {
3676 /* all done */
3677 spin_lock_bh(&cmd->istate_lock);
3678 cmd->i_state = ISTATE_SENT_STATUS;
3679 spin_unlock_bh(&cmd->istate_lock);
3680
3681 if (atomic_read(&conn->check_immediate_queue))
3682 return 1;
3683
3684 return 0;
3685 } else if (ret == 2) {
3686 /* Still must send status,
3687 SCF_TRANSPORT_TASK_SENSE was set */
3688 spin_lock_bh(&cmd->istate_lock);
3689 cmd->i_state = ISTATE_SEND_STATUS;
3690 spin_unlock_bh(&cmd->istate_lock);
3691 state = ISTATE_SEND_STATUS;
3692 goto check_rsp_state;
3693 }
3694
3695 break;
3696 case ISTATE_SEND_STATUS:
3697 case ISTATE_SEND_STATUS_RECOVERY:
3698 ret = iscsit_send_response(cmd, conn);
3699 break;
3700 case ISTATE_SEND_LOGOUTRSP:
3701 ret = iscsit_send_logout(cmd, conn);
3702 break;
3703 case ISTATE_SEND_ASYNCMSG:
3704 ret = iscsit_send_conn_drop_async_message(
3705 cmd, conn);
3706 break;
3707 case ISTATE_SEND_NOPIN:
3708 ret = iscsit_send_nopin(cmd, conn);
3709 break;
3710 case ISTATE_SEND_REJECT:
3711 ret = iscsit_send_reject(cmd, conn);
3712 break;
3713 case ISTATE_SEND_TASKMGTRSP:
3714 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3715 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003716 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003717 ret = iscsit_tmr_post_handler(cmd, conn);
3718 if (ret != 0)
3719 iscsit_fall_back_to_erl0(conn->sess);
3720 break;
3721 case ISTATE_SEND_TEXTRSP:
3722 ret = iscsit_send_text_rsp(cmd, conn);
3723 break;
3724 default:
3725 pr_err("Unknown Opcode: 0x%02x ITT:"
3726 " 0x%08x, i_state: %d on CID: %hu\n",
3727 cmd->iscsi_opcode, cmd->init_task_tag,
3728 state, conn->cid);
3729 goto err;
3730 }
3731 if (ret < 0)
3732 goto err;
3733
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003734 switch (state) {
3735 case ISTATE_SEND_LOGOUTRSP:
3736 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003737 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003738 /* fall through */
3739 case ISTATE_SEND_STATUS:
3740 case ISTATE_SEND_ASYNCMSG:
3741 case ISTATE_SEND_NOPIN:
3742 case ISTATE_SEND_STATUS_RECOVERY:
3743 case ISTATE_SEND_TEXTRSP:
3744 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003745 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003746 spin_lock_bh(&cmd->istate_lock);
3747 cmd->i_state = ISTATE_SENT_STATUS;
3748 spin_unlock_bh(&cmd->istate_lock);
3749 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003750 default:
3751 pr_err("Unknown Opcode: 0x%02x ITT:"
3752 " 0x%08x, i_state: %d on CID: %hu\n",
3753 cmd->iscsi_opcode, cmd->init_task_tag,
3754 cmd->i_state, conn->cid);
3755 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003756 }
3757
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003758 if (atomic_read(&conn->check_immediate_queue))
3759 return 1;
3760
Andy Grover6f3c0e62012-04-03 15:51:09 -07003761 return 0;
3762
3763err:
3764 return -1;
3765}
Varun Prakashd2faaef2016-04-20 00:00:19 +05303766EXPORT_SYMBOL(iscsit_response_queue);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003767
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003768static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003769{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003770 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003771 struct iscsi_queue_req *qr;
3772 struct iscsi_cmd *cmd;
3773 u8 state;
3774 int ret;
3775
3776 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3777 cmd = qr->cmd;
3778 state = qr->state;
3779 kmem_cache_free(lio_qr_cache, qr);
3780
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003781 ret = t->iscsit_response_queue(conn, cmd, state);
3782 if (ret == 1 || ret < 0)
3783 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003784 }
3785
3786 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003787}
3788
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003789int iscsi_target_tx_thread(void *arg)
3790{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003791 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003792 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003793 /*
3794 * Allow ourselves to be interrupted by SIGINT so that a
3795 * connection recovery / failure event can be triggered externally.
3796 */
3797 allow_signal(SIGINT);
3798
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003799 while (!kthread_should_stop()) {
3800 /*
3801 * Ensure that both TX and RX per connection kthreads
3802 * are scheduled to run on the same CPU.
3803 */
3804 iscsit_thread_check_cpumask(conn, current, 1);
3805
Roland Dreierd5627ac2012-10-31 09:16:46 -07003806 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003807 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003808
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003809 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003810 goto transport_err;
3811
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003812get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003813 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003814 if (ret < 0)
3815 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003816
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003817 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003818 if (ret == 1)
3819 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003820 else if (ret == -ECONNRESET)
3821 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003822 else if (ret < 0)
3823 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003824 }
3825
3826transport_err:
Nicholas Bellingere5419862015-07-22 23:14:19 -07003827 /*
3828 * Avoid the normal connection failure code-path if this connection
3829 * is still within LOGIN mode, and iscsi_np process context is
3830 * responsible for cleaning up the early connection failure.
3831 */
3832 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3833 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003834out:
3835 return 0;
3836}
3837
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003838static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3839{
3840 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3841 struct iscsi_cmd *cmd;
3842 int ret = 0;
3843
3844 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3845 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003846 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003847 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003848 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003849
3850 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3851 break;
3852 case ISCSI_OP_SCSI_DATA_OUT:
3853 ret = iscsit_handle_data_out(conn, buf);
3854 break;
3855 case ISCSI_OP_NOOP_OUT:
3856 cmd = NULL;
3857 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003858 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003859 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003860 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003861 }
3862 ret = iscsit_handle_nop_out(conn, cmd, buf);
3863 break;
3864 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003865 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003866 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003867 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003868
3869 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3870 break;
3871 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003872 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3873 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3874 if (!cmd)
3875 goto reject;
3876 } else {
3877 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3878 if (!cmd)
3879 goto reject;
3880 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003881
3882 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003883 break;
3884 case ISCSI_OP_LOGOUT:
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_logout_cmd(conn, cmd, buf);
3890 if (ret > 0)
3891 wait_for_completion_timeout(&conn->conn_logout_comp,
3892 SECONDS_FOR_LOGOUT_COMP * HZ);
3893 break;
3894 case ISCSI_OP_SNACK:
3895 ret = iscsit_handle_snack(conn, buf);
3896 break;
3897 default:
3898 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3899 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3900 pr_err("Cannot recover from unknown"
3901 " opcode while ERL=0, closing iSCSI connection.\n");
3902 return -1;
3903 }
Christophe Vu-Brugierc04a6092015-04-19 22:18:33 +02003904 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3905 " closing iSCSI connection.\n");
3906 ret = -1;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003907 break;
3908 }
3909
3910 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003911reject:
3912 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003913}
3914
Nicholas Bellingerca82c2b2015-11-05 14:11:59 -08003915static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3916{
3917 bool ret;
3918
3919 spin_lock_bh(&conn->state_lock);
3920 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3921 spin_unlock_bh(&conn->state_lock);
3922
3923 return ret;
3924}
3925
Varun Prakashe8205cc2016-04-20 00:00:11 +05303926static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927{
Varun Prakashe8205cc2016-04-20 00:00:11 +05303928 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003929 u8 buffer[ISCSI_HDR_LEN], opcode;
3930 u32 checksum = 0, digest = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003931 struct kvec iov;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003932
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003933 while (!kthread_should_stop()) {
3934 /*
3935 * Ensure that both TX and RX per connection kthreads
3936 * are scheduled to run on the same CPU.
3937 */
3938 iscsit_thread_check_cpumask(conn, current, 0);
3939
3940 memset(buffer, 0, ISCSI_HDR_LEN);
3941 memset(&iov, 0, sizeof(struct kvec));
3942
3943 iov.iov_base = buffer;
3944 iov.iov_len = ISCSI_HDR_LEN;
3945
3946 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3947 if (ret != ISCSI_HDR_LEN) {
3948 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303949 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003950 }
3951
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003952 if (conn->conn_ops->HeaderDigest) {
3953 iov.iov_base = &digest;
3954 iov.iov_len = ISCSI_CRC_LEN;
3955
3956 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3957 if (ret != ISCSI_CRC_LEN) {
3958 iscsit_rx_thread_wait_for_tcp(conn);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303959 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003960 }
3961
Herbert Xu69110e32016-01-24 21:19:52 +08003962 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003963 buffer, ISCSI_HDR_LEN,
3964 0, NULL, (u8 *)&checksum);
3965
3966 if (digest != checksum) {
3967 pr_err("HeaderDigest CRC32C failed,"
3968 " received 0x%08x, computed 0x%08x\n",
3969 digest, checksum);
3970 /*
3971 * Set the PDU to 0xff so it will intentionally
3972 * hit default in the switch below.
3973 */
3974 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003975 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003976 } else {
3977 pr_debug("Got HeaderDigest CRC32C"
3978 " 0x%08x\n", checksum);
3979 }
3980 }
3981
3982 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
Varun Prakashe8205cc2016-04-20 00:00:11 +05303983 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003984
3985 opcode = buffer[0] & ISCSI_OPCODE_MASK;
3986
3987 if (conn->sess->sess_ops->SessionType &&
3988 ((!(opcode & ISCSI_OP_TEXT)) ||
3989 (!(opcode & ISCSI_OP_LOGOUT)))) {
3990 pr_err("Received illegal iSCSI Opcode: 0x%02x"
3991 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07003992 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
3993 buffer);
Varun Prakashe8205cc2016-04-20 00:00:11 +05303994 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003995 }
3996
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003997 ret = iscsi_target_rx_opcode(conn, buffer);
3998 if (ret < 0)
Varun Prakashe8205cc2016-04-20 00:00:11 +05303999 return;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004000 }
Varun Prakashe8205cc2016-04-20 00:00:11 +05304001}
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004002
Varun Prakashe8205cc2016-04-20 00:00:11 +05304003int iscsi_target_rx_thread(void *arg)
4004{
4005 int rc;
4006 struct iscsi_conn *conn = arg;
4007
4008 /*
4009 * Allow ourselves to be interrupted by SIGINT so that a
4010 * connection recovery / failure event can be triggered externally.
4011 */
4012 allow_signal(SIGINT);
4013 /*
4014 * Wait for iscsi_post_login_handler() to complete before allowing
4015 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4016 */
4017 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4018 if (rc < 0 || iscsi_target_check_conn_state(conn))
4019 return 0;
4020
4021 if (!conn->conn_transport->iscsit_get_rx_pdu)
4022 return 0;
4023
4024 conn->conn_transport->iscsit_get_rx_pdu(conn);
4025
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004026 if (!signal_pending(current))
4027 atomic_set(&conn->transport_failed, 1);
4028 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004029 return 0;
4030}
4031
4032static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4033{
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004034 LIST_HEAD(tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004035 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4036 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004037 /*
4038 * We expect this function to only ever be called from either RX or TX
4039 * thread context via iscsit_close_connection() once the other context
4040 * has been reset -> returned sleeping pre-handler state.
4041 */
4042 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004043 list_splice_init(&conn->conn_cmd_list, &tmp_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004044
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004045 list_for_each_entry(cmd, &tmp_list, i_conn_node) {
4046 struct se_cmd *se_cmd = &cmd->se_cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004047
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004048 if (se_cmd->se_tfo != NULL) {
4049 spin_lock(&se_cmd->t_state_lock);
4050 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4051 spin_unlock(&se_cmd->t_state_lock);
4052 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004053 }
4054 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellinger064cdd22016-06-02 14:56:45 -07004055
4056 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4057 list_del_init(&cmd->i_conn_node);
4058
4059 iscsit_increment_maxcmdsn(cmd, sess);
4060 iscsit_free_cmd(cmd, true);
4061
4062 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004063}
4064
4065static void iscsit_stop_timers_for_cmds(
4066 struct iscsi_conn *conn)
4067{
4068 struct iscsi_cmd *cmd;
4069
4070 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004071 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004072 if (cmd->data_direction == DMA_TO_DEVICE)
4073 iscsit_stop_dataout_timer(cmd);
4074 }
4075 spin_unlock_bh(&conn->cmd_lock);
4076}
4077
4078int iscsit_close_connection(
4079 struct iscsi_conn *conn)
4080{
4081 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4082 struct iscsi_session *sess = conn->sess;
4083
4084 pr_debug("Closing iSCSI connection CID %hu on SID:"
4085 " %u\n", conn->cid, sess->sid);
4086 /*
Varun Prakashb4869ee2016-04-20 00:00:18 +05304087 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4088 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4089 * sleeping and the logout response never got sent because the
4090 * connection failed.
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004091 *
4092 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4093 * to signal logout response TX interrupt completion. Go ahead and skip
4094 * this for iser since isert_rx_opcode() does not wait on logout failure,
4095 * and to avoid iscsi_conn pointer dereference in iser-target code.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004096 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004097 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellingerf068fbc2015-02-23 00:57:51 -08004098 complete(&conn->conn_logout_comp);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004099
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004100 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4101 if (conn->tx_thread &&
4102 cmpxchg(&conn->tx_thread_active, true, false)) {
4103 send_sig(SIGINT, conn->tx_thread, 1);
4104 kthread_stop(conn->tx_thread);
4105 }
4106 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4107 if (conn->rx_thread &&
4108 cmpxchg(&conn->rx_thread_active, true, false)) {
4109 send_sig(SIGINT, conn->rx_thread, 1);
4110 kthread_stop(conn->rx_thread);
4111 }
4112 }
4113
4114 spin_lock(&iscsit_global->ts_bitmap_lock);
4115 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4116 get_order(1));
4117 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004118
4119 iscsit_stop_timers_for_cmds(conn);
4120 iscsit_stop_nopin_response_timer(conn);
4121 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004122
4123 if (conn->conn_transport->iscsit_wait_conn)
4124 conn->conn_transport->iscsit_wait_conn(conn);
4125
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004126 /*
4127 * During Connection recovery drop unacknowledged out of order
4128 * commands for this connection, and prepare the other commands
Bart Van Assche53c561d2016-12-23 14:40:24 +01004129 * for reallegiance.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004130 *
4131 * During normal operation clear the out of order commands (but
4132 * do not free the struct iscsi_ooo_cmdsn's) and release all
4133 * struct iscsi_cmds.
4134 */
4135 if (atomic_read(&conn->connection_recovery)) {
4136 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
Bart Van Assche53c561d2016-12-23 14:40:24 +01004137 iscsit_prepare_cmds_for_reallegiance(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004138 } else {
4139 iscsit_clear_ooo_cmdsns_for_conn(conn);
4140 iscsit_release_commands_from_conn(conn);
4141 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004142 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004143
4144 /*
4145 * Handle decrementing session or connection usage count if
4146 * a logout response was not able to be sent because the
4147 * connection failed. Fall back to Session Recovery here.
4148 */
4149 if (atomic_read(&conn->conn_logout_remove)) {
4150 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4151 iscsit_dec_conn_usage_count(conn);
4152 iscsit_dec_session_usage_count(sess);
4153 }
4154 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4155 iscsit_dec_conn_usage_count(conn);
4156
4157 atomic_set(&conn->conn_logout_remove, 0);
4158 atomic_set(&sess->session_reinstatement, 0);
4159 atomic_set(&sess->session_fall_back_to_erl0, 1);
4160 }
4161
4162 spin_lock_bh(&sess->conn_lock);
4163 list_del(&conn->conn_list);
4164
4165 /*
4166 * Attempt to let the Initiator know this connection failed by
4167 * sending an Connection Dropped Async Message on another
4168 * active connection.
4169 */
4170 if (atomic_read(&conn->connection_recovery))
4171 iscsit_build_conn_drop_async_message(conn);
4172
4173 spin_unlock_bh(&sess->conn_lock);
4174
4175 /*
4176 * If connection reinstatement is being performed on this connection,
4177 * up the connection reinstatement semaphore that is being blocked on
4178 * in iscsit_cause_connection_reinstatement().
4179 */
4180 spin_lock_bh(&conn->state_lock);
4181 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4182 spin_unlock_bh(&conn->state_lock);
4183 complete(&conn->conn_wait_comp);
4184 wait_for_completion(&conn->conn_post_wait_comp);
4185 spin_lock_bh(&conn->state_lock);
4186 }
4187
4188 /*
4189 * If connection reinstatement is being performed on this connection
4190 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4191 * connection wait rcfr semaphore that is being blocked on
4192 * an iscsit_connection_reinstatement_rcfr().
4193 */
4194 if (atomic_read(&conn->connection_wait_rcfr)) {
4195 spin_unlock_bh(&conn->state_lock);
4196 complete(&conn->conn_wait_rcfr_comp);
4197 wait_for_completion(&conn->conn_post_wait_comp);
4198 spin_lock_bh(&conn->state_lock);
4199 }
4200 atomic_set(&conn->connection_reinstatement, 1);
4201 spin_unlock_bh(&conn->state_lock);
4202
4203 /*
4204 * If any other processes are accessing this connection pointer we
4205 * must wait until they have completed.
4206 */
4207 iscsit_check_conn_usage_count(conn);
4208
Herbert Xu69110e32016-01-24 21:19:52 +08004209 ahash_request_free(conn->conn_tx_hash);
4210 if (conn->conn_rx_hash) {
4211 struct crypto_ahash *tfm;
4212
4213 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4214 ahash_request_free(conn->conn_rx_hash);
4215 crypto_free_ahash(tfm);
4216 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004217
Joern Engelfbecb652014-09-02 17:49:47 -04004218 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004219
4220 kfree(conn->conn_ops);
4221 conn->conn_ops = NULL;
4222
Al Virobf6932f2012-07-21 08:55:18 +01004223 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004224 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004225
4226 if (conn->conn_transport->iscsit_free_conn)
4227 conn->conn_transport->iscsit_free_conn(conn);
4228
4229 iscsit_put_transport(conn->conn_transport);
4230
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004231 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4232 conn->conn_state = TARG_CONN_STATE_FREE;
4233 kfree(conn);
4234
4235 spin_lock_bh(&sess->conn_lock);
4236 atomic_dec(&sess->nconn);
4237 pr_debug("Decremented iSCSI connection count to %hu from node:"
4238 " %s\n", atomic_read(&sess->nconn),
4239 sess->sess_ops->InitiatorName);
4240 /*
4241 * Make sure that if one connection fails in an non ERL=2 iSCSI
4242 * Session that they all fail.
4243 */
4244 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4245 !atomic_read(&sess->session_logout))
4246 atomic_set(&sess->session_fall_back_to_erl0, 1);
4247
4248 /*
4249 * If this was not the last connection in the session, and we are
4250 * performing session reinstatement or falling back to ERL=0, call
4251 * iscsit_stop_session() without sleeping to shutdown the other
4252 * active connections.
4253 */
4254 if (atomic_read(&sess->nconn)) {
4255 if (!atomic_read(&sess->session_reinstatement) &&
4256 !atomic_read(&sess->session_fall_back_to_erl0)) {
4257 spin_unlock_bh(&sess->conn_lock);
4258 return 0;
4259 }
4260 if (!atomic_read(&sess->session_stop_active)) {
4261 atomic_set(&sess->session_stop_active, 1);
4262 spin_unlock_bh(&sess->conn_lock);
4263 iscsit_stop_session(sess, 0, 0);
4264 return 0;
4265 }
4266 spin_unlock_bh(&sess->conn_lock);
4267 return 0;
4268 }
4269
4270 /*
4271 * If this was the last connection in the session and one of the
4272 * following is occurring:
4273 *
4274 * Session Reinstatement is not being performed, and are falling back
4275 * to ERL=0 call iscsit_close_session().
4276 *
4277 * Session Logout was requested. iscsit_close_session() will be called
4278 * elsewhere.
4279 *
4280 * Session Continuation is not being performed, start the Time2Retain
4281 * handler and check if sleep_on_sess_wait_sem is active.
4282 */
4283 if (!atomic_read(&sess->session_reinstatement) &&
4284 atomic_read(&sess->session_fall_back_to_erl0)) {
4285 spin_unlock_bh(&sess->conn_lock);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004286 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004287
4288 return 0;
4289 } else if (atomic_read(&sess->session_logout)) {
4290 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4291 sess->session_state = TARG_SESS_STATE_FREE;
4292 spin_unlock_bh(&sess->conn_lock);
4293
4294 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4295 complete(&sess->session_wait_comp);
4296
4297 return 0;
4298 } else {
4299 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4300 sess->session_state = TARG_SESS_STATE_FAILED;
4301
4302 if (!atomic_read(&sess->session_continuation)) {
4303 spin_unlock_bh(&sess->conn_lock);
4304 iscsit_start_time2retain_handler(sess);
4305 } else
4306 spin_unlock_bh(&sess->conn_lock);
4307
4308 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4309 complete(&sess->session_wait_comp);
4310
4311 return 0;
4312 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004313}
4314
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004315/*
4316 * If the iSCSI Session for the iSCSI Initiator Node exists,
4317 * forcefully shutdown the iSCSI NEXUS.
4318 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004319int iscsit_close_session(struct iscsi_session *sess)
4320{
Andy Grover60bfcf82013-10-09 11:05:58 -07004321 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004322 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4323
4324 if (atomic_read(&sess->nconn)) {
4325 pr_err("%d connection(s) still exist for iSCSI session"
4326 " to %s\n", atomic_read(&sess->nconn),
4327 sess->sess_ops->InitiatorName);
4328 BUG();
4329 }
4330
4331 spin_lock_bh(&se_tpg->session_lock);
4332 atomic_set(&sess->session_logout, 1);
4333 atomic_set(&sess->session_reinstatement, 1);
4334 iscsit_stop_time2retain_timer(sess);
4335 spin_unlock_bh(&se_tpg->session_lock);
4336
4337 /*
4338 * transport_deregister_session_configfs() will clear the
4339 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4340 * can be setting it again with __transport_register_session() in
4341 * iscsi_post_login_handler() again after the iscsit_stop_session()
4342 * completes in iscsi_np context.
4343 */
4344 transport_deregister_session_configfs(sess->se_sess);
4345
4346 /*
4347 * If any other processes are accessing this session pointer we must
4348 * wait until they have completed. If we are in an interrupt (the
4349 * time2retain handler) and contain and active session usage count we
4350 * restart the timer and exit.
4351 */
4352 if (!in_interrupt()) {
4353 if (iscsit_check_session_usage_count(sess) == 1)
4354 iscsit_stop_session(sess, 1, 1);
4355 } else {
4356 if (iscsit_check_session_usage_count(sess) == 2) {
4357 atomic_set(&sess->session_logout, 0);
4358 iscsit_start_time2retain_handler(sess);
4359 return 0;
4360 }
4361 }
4362
4363 transport_deregister_session(sess->se_sess);
4364
4365 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4366 iscsit_free_connection_recovery_entires(sess);
4367
4368 iscsit_free_all_ooo_cmdsns(sess);
4369
4370 spin_lock_bh(&se_tpg->session_lock);
4371 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4372 sess->session_state = TARG_SESS_STATE_FREE;
4373 pr_debug("Released iSCSI session from node: %s\n",
4374 sess->sess_ops->InitiatorName);
4375 tpg->nsessions--;
4376 if (tpg->tpg_tiqn)
4377 tpg->tpg_tiqn->tiqn_nsessions--;
4378
4379 pr_debug("Decremented number of active iSCSI Sessions on"
4380 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4381
4382 spin_lock(&sess_idr_lock);
4383 idr_remove(&sess_idr, sess->session_index);
4384 spin_unlock(&sess_idr_lock);
4385
4386 kfree(sess->sess_ops);
4387 sess->sess_ops = NULL;
4388 spin_unlock_bh(&se_tpg->session_lock);
4389
4390 kfree(sess);
4391 return 0;
4392}
4393
4394static void iscsit_logout_post_handler_closesession(
4395 struct iscsi_conn *conn)
4396{
4397 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004398 int sleep = 1;
4399 /*
4400 * Traditional iscsi/tcp will invoke this logic from TX thread
4401 * context during session logout, so clear tx_thread_active and
4402 * sleep if iscsit_close_connection() has not already occured.
4403 *
4404 * Since iser-target invokes this logic from it's own workqueue,
4405 * always sleep waiting for RX/TX thread shutdown to complete
4406 * within iscsit_close_connection().
4407 */
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004408 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004409 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004410
4411 atomic_set(&conn->conn_logout_remove, 0);
4412 complete(&conn->conn_logout_comp);
4413
4414 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004415 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004416 iscsit_dec_session_usage_count(sess);
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004417 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004418}
4419
4420static void iscsit_logout_post_handler_samecid(
4421 struct iscsi_conn *conn)
4422{
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004423 int sleep = 1;
4424
Nicholas Bellingerbd027d82016-05-14 22:23:34 -07004425 if (!conn->conn_transport->rdma_shutdown)
Nicholas Bellinger007d0382015-07-23 22:30:31 +00004426 sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004427
4428 atomic_set(&conn->conn_logout_remove, 0);
4429 complete(&conn->conn_logout_comp);
4430
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004431 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004432 iscsit_dec_conn_usage_count(conn);
4433}
4434
4435static void iscsit_logout_post_handler_diffcid(
4436 struct iscsi_conn *conn,
4437 u16 cid)
4438{
4439 struct iscsi_conn *l_conn;
4440 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004441 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004442
4443 if (!sess)
4444 return;
4445
4446 spin_lock_bh(&sess->conn_lock);
4447 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4448 if (l_conn->cid == cid) {
4449 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004450 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004451 break;
4452 }
4453 }
4454 spin_unlock_bh(&sess->conn_lock);
4455
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004456 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004457 return;
4458
4459 if (l_conn->sock)
4460 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4461
4462 spin_lock_bh(&l_conn->state_lock);
4463 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4464 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4465 spin_unlock_bh(&l_conn->state_lock);
4466
4467 iscsit_cause_connection_reinstatement(l_conn, 1);
4468 iscsit_dec_conn_usage_count(l_conn);
4469}
4470
4471/*
4472 * Return of 0 causes the TX thread to restart.
4473 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004474int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004475 struct iscsi_cmd *cmd,
4476 struct iscsi_conn *conn)
4477{
4478 int ret = 0;
4479
4480 switch (cmd->logout_reason) {
4481 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4482 switch (cmd->logout_response) {
4483 case ISCSI_LOGOUT_SUCCESS:
4484 case ISCSI_LOGOUT_CLEANUP_FAILED:
4485 default:
4486 iscsit_logout_post_handler_closesession(conn);
4487 break;
4488 }
4489 ret = 0;
4490 break;
4491 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4492 if (conn->cid == cmd->logout_cid) {
4493 switch (cmd->logout_response) {
4494 case ISCSI_LOGOUT_SUCCESS:
4495 case ISCSI_LOGOUT_CLEANUP_FAILED:
4496 default:
4497 iscsit_logout_post_handler_samecid(conn);
4498 break;
4499 }
4500 ret = 0;
4501 } else {
4502 switch (cmd->logout_response) {
4503 case ISCSI_LOGOUT_SUCCESS:
4504 iscsit_logout_post_handler_diffcid(conn,
4505 cmd->logout_cid);
4506 break;
4507 case ISCSI_LOGOUT_CID_NOT_FOUND:
4508 case ISCSI_LOGOUT_CLEANUP_FAILED:
4509 default:
4510 break;
4511 }
4512 ret = 1;
4513 }
4514 break;
4515 case ISCSI_LOGOUT_REASON_RECOVERY:
4516 switch (cmd->logout_response) {
4517 case ISCSI_LOGOUT_SUCCESS:
4518 case ISCSI_LOGOUT_CID_NOT_FOUND:
4519 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4520 case ISCSI_LOGOUT_CLEANUP_FAILED:
4521 default:
4522 break;
4523 }
4524 ret = 1;
4525 break;
4526 default:
4527 break;
4528
4529 }
4530 return ret;
4531}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004532EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004533
4534void iscsit_fail_session(struct iscsi_session *sess)
4535{
4536 struct iscsi_conn *conn;
4537
4538 spin_lock_bh(&sess->conn_lock);
4539 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4540 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4541 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4542 }
4543 spin_unlock_bh(&sess->conn_lock);
4544
4545 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4546 sess->session_state = TARG_SESS_STATE_FAILED;
4547}
4548
4549int iscsit_free_session(struct iscsi_session *sess)
4550{
4551 u16 conn_count = atomic_read(&sess->nconn);
4552 struct iscsi_conn *conn, *conn_tmp = NULL;
4553 int is_last;
4554
4555 spin_lock_bh(&sess->conn_lock);
4556 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4557
4558 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4559 conn_list) {
4560 if (conn_count == 0)
4561 break;
4562
4563 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4564 is_last = 1;
4565 } else {
4566 iscsit_inc_conn_usage_count(conn_tmp);
4567 is_last = 0;
4568 }
4569 iscsit_inc_conn_usage_count(conn);
4570
4571 spin_unlock_bh(&sess->conn_lock);
4572 iscsit_cause_connection_reinstatement(conn, 1);
4573 spin_lock_bh(&sess->conn_lock);
4574
4575 iscsit_dec_conn_usage_count(conn);
4576 if (is_last == 0)
4577 iscsit_dec_conn_usage_count(conn_tmp);
4578
4579 conn_count--;
4580 }
4581
4582 if (atomic_read(&sess->nconn)) {
4583 spin_unlock_bh(&sess->conn_lock);
4584 wait_for_completion(&sess->session_wait_comp);
4585 } else
4586 spin_unlock_bh(&sess->conn_lock);
4587
Christoph Hellwig44f33d02016-05-02 15:45:24 +02004588 iscsit_close_session(sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004589 return 0;
4590}
4591
4592void iscsit_stop_session(
4593 struct iscsi_session *sess,
4594 int session_sleep,
4595 int connection_sleep)
4596{
4597 u16 conn_count = atomic_read(&sess->nconn);
4598 struct iscsi_conn *conn, *conn_tmp = NULL;
4599 int is_last;
4600
4601 spin_lock_bh(&sess->conn_lock);
4602 if (session_sleep)
4603 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4604
4605 if (connection_sleep) {
4606 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4607 conn_list) {
4608 if (conn_count == 0)
4609 break;
4610
4611 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4612 is_last = 1;
4613 } else {
4614 iscsit_inc_conn_usage_count(conn_tmp);
4615 is_last = 0;
4616 }
4617 iscsit_inc_conn_usage_count(conn);
4618
4619 spin_unlock_bh(&sess->conn_lock);
4620 iscsit_cause_connection_reinstatement(conn, 1);
4621 spin_lock_bh(&sess->conn_lock);
4622
4623 iscsit_dec_conn_usage_count(conn);
4624 if (is_last == 0)
4625 iscsit_dec_conn_usage_count(conn_tmp);
4626 conn_count--;
4627 }
4628 } else {
4629 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4630 iscsit_cause_connection_reinstatement(conn, 0);
4631 }
4632
4633 if (session_sleep && atomic_read(&sess->nconn)) {
4634 spin_unlock_bh(&sess->conn_lock);
4635 wait_for_completion(&sess->session_wait_comp);
4636 } else
4637 spin_unlock_bh(&sess->conn_lock);
4638}
4639
4640int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4641{
4642 struct iscsi_session *sess;
4643 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4644 struct se_session *se_sess, *se_sess_tmp;
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004645 LIST_HEAD(free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004646 int session_count = 0;
4647
4648 spin_lock_bh(&se_tpg->session_lock);
4649 if (tpg->nsessions && !force) {
4650 spin_unlock_bh(&se_tpg->session_lock);
4651 return -1;
4652 }
4653
4654 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4655 sess_list) {
4656 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4657
4658 spin_lock(&sess->conn_lock);
4659 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4660 atomic_read(&sess->session_logout) ||
4661 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4662 spin_unlock(&sess->conn_lock);
4663 continue;
4664 }
4665 atomic_set(&sess->session_reinstatement, 1);
4666 spin_unlock(&sess->conn_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004667
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004668 list_move_tail(&se_sess->sess_list, &free_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004669 }
4670 spin_unlock_bh(&se_tpg->session_lock);
4671
Nicholas Bellinger417c20a2015-07-22 00:24:09 -07004672 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4673 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4674
4675 iscsit_free_session(sess);
4676 session_count++;
4677 }
4678
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004679 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4680 " Group: %hu\n", session_count, tpg->tpgt);
4681 return 0;
4682}
4683
4684MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4685MODULE_VERSION("4.1.x");
4686MODULE_AUTHOR("nab@Linux-iSCSI.org");
4687MODULE_LICENSE("GPL");
4688
4689module_init(iscsi_target_init_module);
4690module_exit(iscsi_target_cleanup_module);