blob: 163773fb4f84fdf3a0309f401557ac6f16eac714 [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
19#include <linux/string.h>
20#include <linux/kthread.h>
21#include <linux/crypto.h>
22#include <linux/completion.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040023#include <linux/module.h>
Al Viro40401532012-02-13 03:58:52 +000024#include <linux/idr.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000025#include <asm/unaligned.h>
26#include <scsi/scsi_device.h>
27#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070028#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000029#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050030#include <target/target_core_fabric.h>
Andy Groverd28b11692012-04-03 15:51:22 -070031#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000032
Sagi Grimberg67f091f2015-01-07 14:57:31 +020033#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034#include "iscsi_target_parameters.h"
35#include "iscsi_target_seq_pdu_list.h"
36#include "iscsi_target_tq.h"
37#include "iscsi_target_configfs.h"
38#include "iscsi_target_datain_values.h"
39#include "iscsi_target_erl0.h"
40#include "iscsi_target_erl1.h"
41#include "iscsi_target_erl2.h"
42#include "iscsi_target_login.h"
43#include "iscsi_target_tmr.h"
44#include "iscsi_target_tpg.h"
45#include "iscsi_target_util.h"
46#include "iscsi_target.h"
47#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020048#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000049
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080050#include <target/iscsi/iscsi_transport.h>
51
Nicholas Bellingere48354c2011-07-23 06:43:04 +000052static LIST_HEAD(g_tiqn_list);
53static LIST_HEAD(g_np_list);
54static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080055static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000056
57static struct idr tiqn_idr;
58struct idr sess_idr;
59struct mutex auth_id_lock;
60spinlock_t sess_idr_lock;
61
62struct iscsit_global *iscsit_global;
63
Nicholas Bellingere48354c2011-07-23 06:43:04 +000064struct kmem_cache *lio_qr_cache;
65struct kmem_cache *lio_dr_cache;
66struct kmem_cache *lio_ooo_cache;
67struct kmem_cache *lio_r2t_cache;
68
69static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070070 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000071
72struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
73{
74 struct iscsi_tiqn *tiqn = NULL;
75
76 spin_lock(&tiqn_lock);
77 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
78 if (!strcmp(tiqn->tiqn, buf)) {
79
80 spin_lock(&tiqn->tiqn_state_lock);
81 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
82 tiqn->tiqn_access_count++;
83 spin_unlock(&tiqn->tiqn_state_lock);
84 spin_unlock(&tiqn_lock);
85 return tiqn;
86 }
87 spin_unlock(&tiqn->tiqn_state_lock);
88 }
89 }
90 spin_unlock(&tiqn_lock);
91
92 return NULL;
93}
94
95static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
96{
97 spin_lock(&tiqn->tiqn_state_lock);
98 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
99 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
100 spin_unlock(&tiqn->tiqn_state_lock);
101 return 0;
102 }
103 spin_unlock(&tiqn->tiqn_state_lock);
104
105 return -1;
106}
107
108void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
109{
110 spin_lock(&tiqn->tiqn_state_lock);
111 tiqn->tiqn_access_count--;
112 spin_unlock(&tiqn->tiqn_state_lock);
113}
114
115/*
116 * Note that IQN formatting is expected to be done in userspace, and
117 * no explict IQN format checks are done here.
118 */
119struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
120{
121 struct iscsi_tiqn *tiqn = NULL;
122 int ret;
123
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300124 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000125 pr_err("Target IQN exceeds %d bytes\n",
126 ISCSI_IQN_LEN);
127 return ERR_PTR(-EINVAL);
128 }
129
130 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
131 if (!tiqn) {
132 pr_err("Unable to allocate struct iscsi_tiqn\n");
133 return ERR_PTR(-ENOMEM);
134 }
135
136 sprintf(tiqn->tiqn, "%s", buf);
137 INIT_LIST_HEAD(&tiqn->tiqn_list);
138 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
139 spin_lock_init(&tiqn->tiqn_state_lock);
140 spin_lock_init(&tiqn->tiqn_tpg_lock);
141 spin_lock_init(&tiqn->sess_err_stats.lock);
142 spin_lock_init(&tiqn->login_stats.lock);
143 spin_lock_init(&tiqn->logout_stats.lock);
144
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000145 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
146
Tejun Heoc9365bd2013-02-27 17:04:43 -0800147 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000148 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800149
150 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800154 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000155 kfree(tiqn);
156 return ERR_PTR(ret);
157 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800162 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000163
164 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
165
166 return tiqn;
167
168}
169
170static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
171{
172 /*
173 * Wait for accesses to said struct iscsi_tiqn to end.
174 */
175 spin_lock(&tiqn->tiqn_state_lock);
176 while (tiqn->tiqn_access_count != 0) {
177 spin_unlock(&tiqn->tiqn_state_lock);
178 msleep(10);
179 spin_lock(&tiqn->tiqn_state_lock);
180 }
181 spin_unlock(&tiqn->tiqn_state_lock);
182}
183
184void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
185{
186 /*
187 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
188 * while holding tiqn->tiqn_state_lock. This means that all subsequent
189 * attempts to access this struct iscsi_tiqn will fail from both transport
190 * fabric and control code paths.
191 */
192 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
193 pr_err("iscsit_set_tiqn_shutdown() failed\n");
194 return;
195 }
196
197 iscsit_wait_for_tiqn(tiqn);
198
199 spin_lock(&tiqn_lock);
200 list_del(&tiqn->tiqn_list);
201 idr_remove(&tiqn_idr, tiqn->tiqn_index);
202 spin_unlock(&tiqn_lock);
203
204 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
205 tiqn->tiqn);
206 kfree(tiqn);
207}
208
209int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
210{
211 int ret;
212 /*
213 * Determine if the network portal is accepting storage traffic.
214 */
215 spin_lock_bh(&np->np_thread_lock);
216 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
217 spin_unlock_bh(&np->np_thread_lock);
218 return -1;
219 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000220 spin_unlock_bh(&np->np_thread_lock);
221 /*
222 * Determine if the portal group is accepting storage traffic.
223 */
224 spin_lock_bh(&tpg->tpg_state_lock);
225 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
226 spin_unlock_bh(&tpg->tpg_state_lock);
227 return -1;
228 }
229 spin_unlock_bh(&tpg->tpg_state_lock);
230
231 /*
232 * Here we serialize access across the TIQN+TPG Tuple.
233 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700234 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000235 if ((ret != 0) || signal_pending(current))
236 return -1;
237
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700238 spin_lock_bh(&tpg->tpg_state_lock);
239 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
240 spin_unlock_bh(&tpg->tpg_state_lock);
241 up(&tpg->np_login_sem);
242 return -1;
243 }
244 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000245
246 return 0;
247}
248
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700249void iscsit_login_kref_put(struct kref *kref)
250{
251 struct iscsi_tpg_np *tpg_np = container_of(kref,
252 struct iscsi_tpg_np, tpg_np_kref);
253
254 complete(&tpg_np->tpg_np_comp);
255}
256
257int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
258 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000259{
260 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
261
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700262 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000263
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700264 if (tpg_np)
265 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000266
267 if (tiqn)
268 iscsit_put_tiqn_for_login(tiqn);
269
270 return 0;
271}
272
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800275 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000276 int network_transport)
277{
278 struct sockaddr_in *sock_in, *sock_in_e;
279 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800280 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000281 u16 port;
282
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800283 if (sockaddr->ss_family == AF_INET6) {
284 sock_in6 = (struct sockaddr_in6 *)sockaddr;
285 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
286
287 if (!memcmp(&sock_in6->sin6_addr.in6_u,
288 &sock_in6_e->sin6_addr.in6_u,
289 sizeof(struct in6_addr)))
290 ip_match = true;
291
292 port = ntohs(sock_in6->sin6_port);
293 } 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);
301 }
302
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200303 if (ip_match && (np->np_port == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800304 (np->np_network_transport == network_transport))
305 return true;
306
307 return false;
308}
309
Andy Groveree291e62014-01-24 16:18:54 -0800310/*
311 * Called with mutex np_lock held
312 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800313static struct iscsi_np *iscsit_get_np(
314 struct __kernel_sockaddr_storage *sockaddr,
315 int network_transport)
316{
317 struct iscsi_np *np;
318 bool match;
319
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000320 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800321 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800323 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000324 continue;
325 }
326
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800327 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200328 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000329 /*
330 * Increment the np_exports reference count now to
331 * prevent iscsit_del_np() below from being called
332 * while iscsi_tpg_add_network_portal() is called.
333 */
334 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800335 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000336 return np;
337 }
Andy Groveree291e62014-01-24 16:18:54 -0800338 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000339 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340
341 return NULL;
342}
343
344struct iscsi_np *iscsit_add_np(
345 struct __kernel_sockaddr_storage *sockaddr,
346 char *ip_str,
347 int network_transport)
348{
349 struct sockaddr_in *sock_in;
350 struct sockaddr_in6 *sock_in6;
351 struct iscsi_np *np;
352 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800353
354 mutex_lock(&np_lock);
355
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000356 /*
357 * Locate the existing struct iscsi_np if already active..
358 */
359 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800360 if (np) {
361 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800363 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000364
365 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
366 if (!np) {
367 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800368 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000369 return ERR_PTR(-ENOMEM);
370 }
371
372 np->np_flags |= NPF_IP_NETWORK;
373 if (sockaddr->ss_family == AF_INET6) {
374 sock_in6 = (struct sockaddr_in6 *)sockaddr;
375 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
376 np->np_port = ntohs(sock_in6->sin6_port);
377 } else {
378 sock_in = (struct sockaddr_in *)sockaddr;
379 sprintf(np->np_ip, "%s", ip_str);
380 np->np_port = ntohs(sock_in->sin_port);
381 }
382
383 np->np_network_transport = network_transport;
384 spin_lock_init(&np->np_thread_lock);
385 init_completion(&np->np_restart_comp);
386 INIT_LIST_HEAD(&np->np_list);
387
388 ret = iscsi_target_setup_login_socket(np, sockaddr);
389 if (ret != 0) {
390 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800391 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000392 return ERR_PTR(ret);
393 }
394
395 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
396 if (IS_ERR(np->np_thread)) {
397 pr_err("Unable to create kthread: iscsi_np\n");
398 ret = PTR_ERR(np->np_thread);
399 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800400 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000401 return ERR_PTR(ret);
402 }
403 /*
404 * Increment the np_exports reference count now to prevent
405 * iscsit_del_np() below from being run while a new call to
406 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
407 * active. We don't need to hold np->np_thread_lock at this
408 * point because iscsi_np has not been added to g_np_list yet.
409 */
410 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800411 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000412
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000413 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800414 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000415
416 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800417 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000418
419 return np;
420}
421
422int iscsit_reset_np_thread(
423 struct iscsi_np *np,
424 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700425 struct iscsi_portal_group *tpg,
426 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000427{
428 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000429 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
430 spin_unlock_bh(&np->np_thread_lock);
431 return 0;
432 }
433 np->np_thread_state = ISCSI_NP_THREAD_RESET;
434
435 if (np->np_thread) {
436 spin_unlock_bh(&np->np_thread_lock);
437 send_sig(SIGINT, np->np_thread, 1);
438 wait_for_completion(&np->np_restart_comp);
439 spin_lock_bh(&np->np_thread_lock);
440 }
441 spin_unlock_bh(&np->np_thread_lock);
442
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700443 if (tpg_np && shutdown) {
444 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
445
446 wait_for_completion(&tpg_np->tpg_np_comp);
447 }
448
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000449 return 0;
450}
451
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800452static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000453{
Al Virobf6932f2012-07-21 08:55:18 +0100454 if (np->np_socket)
455 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000456}
457
458int iscsit_del_np(struct iscsi_np *np)
459{
460 spin_lock_bh(&np->np_thread_lock);
461 np->np_exports--;
462 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700463 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000464 spin_unlock_bh(&np->np_thread_lock);
465 return 0;
466 }
467 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
468 spin_unlock_bh(&np->np_thread_lock);
469
470 if (np->np_thread) {
471 /*
472 * We need to send the signal to wakeup Linux/Net
473 * which may be sleeping in sock_accept()..
474 */
475 send_sig(SIGINT, np->np_thread, 1);
476 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800477 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000478 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800479
480 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000481
Andy Groveree291e62014-01-24 16:18:54 -0800482 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800484 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000485
486 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800487 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000488
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800489 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000490 kfree(np);
491 return 0;
492}
493
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700494static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
495static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
496
497static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
498{
499 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
500 return 0;
501}
502
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700503static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
504{
505 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
506
507 spin_lock_bh(&conn->cmd_lock);
508 if (!list_empty(&cmd->i_conn_node))
509 list_del_init(&cmd->i_conn_node);
510 spin_unlock_bh(&conn->cmd_lock);
511
512 __iscsit_free_cmd(cmd, scsi_cmd, true);
513}
514
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700515static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
516{
517 return TARGET_PROT_NORMAL;
518}
519
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800520static struct iscsit_transport iscsi_target_transport = {
521 .name = "iSCSI/TCP",
522 .transport_type = ISCSI_TCP,
523 .owner = NULL,
524 .iscsit_setup_np = iscsit_setup_np,
525 .iscsit_accept_np = iscsit_accept_np,
526 .iscsit_free_np = iscsit_free_np,
527 .iscsit_get_login_rx = iscsit_get_login_rx,
528 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800529 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700530 .iscsit_immediate_queue = iscsit_immediate_queue,
531 .iscsit_response_queue = iscsit_response_queue,
532 .iscsit_queue_data_in = iscsit_queue_rsp,
533 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700534 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700535 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800536};
537
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000538static int __init iscsi_target_init_module(void)
539{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800540 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000541
542 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
543
544 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
545 if (!iscsit_global) {
546 pr_err("Unable to allocate memory for iscsit_global\n");
547 return -1;
548 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800549 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000550 mutex_init(&auth_id_lock);
551 spin_lock_init(&sess_idr_lock);
552 idr_init(&tiqn_idr);
553 idr_init(&sess_idr);
554
555 ret = iscsi_target_register_configfs();
556 if (ret < 0)
557 goto out;
558
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800559 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
560 iscsit_global->ts_bitmap = vzalloc(size);
561 if (!iscsit_global->ts_bitmap) {
562 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000563 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000564 }
565
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000566 lio_qr_cache = kmem_cache_create("lio_qr_cache",
567 sizeof(struct iscsi_queue_req),
568 __alignof__(struct iscsi_queue_req), 0, NULL);
569 if (!lio_qr_cache) {
570 pr_err("nable to kmem_cache_create() for"
571 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800572 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000573 }
574
575 lio_dr_cache = kmem_cache_create("lio_dr_cache",
576 sizeof(struct iscsi_datain_req),
577 __alignof__(struct iscsi_datain_req), 0, NULL);
578 if (!lio_dr_cache) {
579 pr_err("Unable to kmem_cache_create() for"
580 " lio_dr_cache\n");
581 goto qr_out;
582 }
583
584 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
585 sizeof(struct iscsi_ooo_cmdsn),
586 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
587 if (!lio_ooo_cache) {
588 pr_err("Unable to kmem_cache_create() for"
589 " lio_ooo_cache\n");
590 goto dr_out;
591 }
592
593 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
594 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
595 0, NULL);
596 if (!lio_r2t_cache) {
597 pr_err("Unable to kmem_cache_create() for"
598 " lio_r2t_cache\n");
599 goto ooo_out;
600 }
601
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800602 iscsit_register_transport(&iscsi_target_transport);
603
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000604 if (iscsit_load_discovery_tpg() < 0)
605 goto r2t_out;
606
607 return ret;
608r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100609 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000610 kmem_cache_destroy(lio_r2t_cache);
611ooo_out:
612 kmem_cache_destroy(lio_ooo_cache);
613dr_out:
614 kmem_cache_destroy(lio_dr_cache);
615qr_out:
616 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800617bitmap_out:
618 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000619configfs_out:
620 iscsi_target_deregister_configfs();
621out:
622 kfree(iscsit_global);
623 return -ENOMEM;
624}
625
626static void __exit iscsi_target_cleanup_module(void)
627{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000628 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800629 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000630 kmem_cache_destroy(lio_qr_cache);
631 kmem_cache_destroy(lio_dr_cache);
632 kmem_cache_destroy(lio_ooo_cache);
633 kmem_cache_destroy(lio_r2t_cache);
634
635 iscsi_target_deregister_configfs();
636
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800637 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000638 kfree(iscsit_global);
639}
640
Andy Grover8b1e1242012-04-03 15:51:12 -0700641static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700642 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700644 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645{
646 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000647
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000648 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000649 if (!cmd)
650 return -1;
651
652 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700653 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100655 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000656 if (!cmd->buf_ptr) {
657 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700658 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000659 return -1;
660 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000661
662 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700663 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000664 spin_unlock_bh(&conn->cmd_lock);
665
666 cmd->i_state = ISTATE_SEND_REJECT;
667 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
668
Nicholas Bellingerba159912013-07-03 03:48:24 -0700669 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000670}
671
Nicholas Bellingerba159912013-07-03 03:48:24 -0700672static int iscsit_add_reject_from_cmd(
673 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000674 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700675 bool add_to_conn,
676 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000677{
678 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000679
680 if (!cmd->conn) {
681 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
682 cmd->init_task_tag);
683 return -1;
684 }
685 conn = cmd->conn;
686
687 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700688 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000689
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100690 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000691 if (!cmd->buf_ptr) {
692 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700693 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000694 return -1;
695 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000696
697 if (add_to_conn) {
698 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700699 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000700 spin_unlock_bh(&conn->cmd_lock);
701 }
702
703 cmd->i_state = ISTATE_SEND_REJECT;
704 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800705 /*
706 * Perform the kref_put now if se_cmd has already been setup by
707 * scsit_setup_scsi_cmd()
708 */
709 if (cmd->se_cmd.se_tfo != NULL) {
710 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
711 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
712 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700713 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000714}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700715
716static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
717 unsigned char *buf)
718{
719 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
720}
721
722int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
723{
724 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
725}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000726
727/*
728 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700729 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000730 */
731static int iscsit_map_iovec(
732 struct iscsi_cmd *cmd,
733 struct kvec *iov,
734 u32 data_offset,
735 u32 data_length)
736{
737 u32 i = 0;
738 struct scatterlist *sg;
739 unsigned int page_off;
740
741 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700742 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000743 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700744 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000745 page_off = (data_offset % PAGE_SIZE);
746
747 cmd->first_data_sg = sg;
748 cmd->first_data_sg_off = page_off;
749
750 while (data_length) {
751 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
752
753 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
754 iov[i].iov_len = cur_len;
755
756 data_length -= cur_len;
757 page_off = 0;
758 sg = sg_next(sg);
759 i++;
760 }
761
762 cmd->kmapped_nents = i;
763
764 return i;
765}
766
767static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
768{
769 u32 i;
770 struct scatterlist *sg;
771
772 sg = cmd->first_data_sg;
773
774 for (i = 0; i < cmd->kmapped_nents; i++)
775 kunmap(sg_page(&sg[i]));
776}
777
778static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
779{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700780 LIST_HEAD(ack_list);
781 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000782
783 conn->exp_statsn = exp_statsn;
784
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800785 if (conn->sess->sess_ops->RDMAExtensions)
786 return;
787
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000788 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700789 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000790 spin_lock(&cmd->istate_lock);
791 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800792 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000793 cmd->i_state = ISTATE_REMOVE;
794 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700795 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000796 continue;
797 }
798 spin_unlock(&cmd->istate_lock);
799 }
800 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700801
802 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800803 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700804 iscsit_free_cmd(cmd, false);
805 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000806}
807
808static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
809{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700810 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000811
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400812 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813
814 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
815 if (!cmd->iov_data) {
816 pr_err("Unable to allocate cmd->iov_data\n");
817 return -ENOMEM;
818 }
819
820 cmd->orig_iov_data_count = iov_count;
821 return 0;
822}
823
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800824int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
825 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000826{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800827 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000828 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700829 int iscsi_task_attr;
830 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000831
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800832 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833
834 hdr = (struct iscsi_scsi_req *) buf;
835 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000836
837 /* FIXME; Add checks for AdditionalHeaderSegment */
838
839 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
840 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
841 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
842 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700843 return iscsit_add_reject_cmd(cmd,
844 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000845 }
846
847 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
848 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
849 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800850 * From RFC-3720 Section 10.3.1:
851 *
852 * "Either or both of R and W MAY be 1 when either the
853 * Expected Data Transfer Length and/or Bidirectional Read
854 * Expected Data Transfer Length are 0"
855 *
856 * For this case, go ahead and clear the unnecssary bits
857 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000858 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800859 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
860 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000861
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800862 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000863 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800864 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000865 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000866
867 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
868 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
869 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
870 " MUST be set if Expected Data Transfer Length is not 0."
871 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700872 return iscsit_add_reject_cmd(cmd,
873 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000874 }
875
876 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
877 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
878 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return iscsit_add_reject_cmd(cmd,
880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000881 }
882
883 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
884 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
885 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700886 return iscsit_add_reject_cmd(cmd,
887 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 }
889
890 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
891 pr_err("ImmediateData=No but DataSegmentLength=%u,"
892 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700893 return iscsit_add_reject_cmd(cmd,
894 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000895 }
896
Nicholas Bellingerba159912013-07-03 03:48:24 -0700897 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000898 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
899 pr_err("Expected Data Transfer Length and Length of"
900 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
901 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700902 return iscsit_add_reject_cmd(cmd,
903 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000904 }
905
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400906 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000907 pr_err("DataSegmentLength: %u is greater than"
908 " EDTL: %u, protocol error.\n", payload_length,
909 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700910 return iscsit_add_reject_cmd(cmd,
911 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000912 }
913
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700914 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000915 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700916 " MaxXmitDataSegmentLength: %u, protocol error.\n",
917 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700918 return iscsit_add_reject_cmd(cmd,
919 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000920 }
921
922 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
923 pr_err("DataSegmentLength: %u is greater than"
924 " FirstBurstLength: %u, protocol error.\n",
925 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700926 return iscsit_add_reject_cmd(cmd,
927 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000928 }
929
930 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
931 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
932 DMA_NONE;
933
Andy Groverd28b11692012-04-03 15:51:22 -0700934 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700935 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
936 /*
937 * Figure out the SAM Task Attribute for the incoming SCSI CDB
938 */
939 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
940 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800941 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700942 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800943 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700944 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800945 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700946 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800947 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700948 else {
949 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800950 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
951 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700952 }
953
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000954 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
955 cmd->i_state = ISTATE_NEW_CMD;
956 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
957 cmd->immediate_data = (payload_length) ? 1 : 0;
958 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
959 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
960 if (cmd->unsolicited_data)
961 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
962
963 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
964 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
Sagi Grimbergc1e34b62015-01-26 12:49:05 +0200965 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000966 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
967 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400968 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
969 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000970 cmd->first_burst_len = payload_length;
971
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800972 if (!conn->sess->sess_ops->RDMAExtensions &&
973 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000974 struct iscsi_datain_req *dr;
975
976 dr = iscsit_allocate_datain_req();
977 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700978 return iscsit_add_reject_cmd(cmd,
979 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000980
981 iscsit_attach_datain_req(cmd, dr);
982 }
983
984 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700985 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
986 */
987 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400988 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
989 cmd->data_direction, sam_task_attr,
990 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700991
992 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
993 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800994 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
995 conn->cid);
996
997 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700998
Christoph Hellwigde103c92012-11-06 12:24:09 -0800999 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1000 scsilun_to_int(&hdr->lun));
1001 if (cmd->sense_reason)
1002 goto attach_cmd;
1003
1004 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1005 if (cmd->sense_reason) {
1006 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001007 return iscsit_add_reject_cmd(cmd,
1008 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001009 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001010
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001011 goto attach_cmd;
1012 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001013
Christoph Hellwigde103c92012-11-06 12:24:09 -08001014 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001015 return iscsit_add_reject_cmd(cmd,
1016 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001017 }
1018
1019attach_cmd:
1020 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001021 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001022 spin_unlock_bh(&conn->cmd_lock);
1023 /*
1024 * Check if we need to delay processing because of ALUA
1025 * Active/NonOptimized primary access state..
1026 */
1027 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001028
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001029 return 0;
1030}
1031EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001032
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001033void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1034{
1035 iscsit_set_dataout_sequence_values(cmd);
1036
1037 spin_lock_bh(&cmd->dataout_timeout_lock);
1038 iscsit_start_dataout_timer(cmd, cmd->conn);
1039 spin_unlock_bh(&cmd->dataout_timeout_lock);
1040}
1041EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1042
1043int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1044 struct iscsi_scsi_req *hdr)
1045{
1046 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001047 /*
1048 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1049 * the Immediate Bit is not set, and no Immediate
1050 * Data is attached.
1051 *
1052 * A PDU/CmdSN carrying Immediate Data can only
1053 * be processed after the DataCRC has passed.
1054 * If the DataCRC fails, the CmdSN MUST NOT
1055 * be acknowledged. (See below)
1056 */
1057 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001058 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1059 (unsigned char *)hdr, hdr->cmdsn);
1060 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1061 return -1;
1062 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001063 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001064 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001065 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001066 }
1067
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001068 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001069
1070 /*
1071 * If no Immediate Data is attached, it's OK to return now.
1072 */
1073 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001074 if (!cmd->sense_reason && cmd->unsolicited_data)
1075 iscsit_set_unsoliticed_dataout(cmd);
1076 if (!cmd->sense_reason)
1077 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001079 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001080 return 0;
1081 }
1082
1083 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001084 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1085 * execution. These exceptions are processed in CmdSN order using
1086 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001087 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001088 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001089 if (cmd->reject_reason)
1090 return 0;
1091
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001092 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001093 }
1094 /*
1095 * Call directly into transport_generic_new_cmd() to perform
1096 * the backend memory allocation.
1097 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001098 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001099 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001100 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001101
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001102 return 0;
1103}
1104EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1105
1106static int
1107iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1108 bool dump_payload)
1109{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001110 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001111 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1112 /*
1113 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1114 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001115 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001116 goto after_immediate_data;
1117
1118 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1119 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001120after_immediate_data:
1121 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1122 /*
1123 * A PDU/CmdSN carrying Immediate Data passed
1124 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1125 * Immediate Bit is not set.
1126 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001127 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1128 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001129 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001130 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001131
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001132 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001133 int rc;
1134
1135 rc = iscsit_dump_data_payload(cmd->conn,
1136 cmd->first_burst_len, 1);
1137 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1138 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001139 } else if (cmd->unsolicited_data)
1140 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001141
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001142 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1143 /*
1144 * Immediate Data failed DataCRC and ERL>=1,
1145 * silently drop this PDU and let the initiator
1146 * plug the CmdSN gap.
1147 *
1148 * FIXME: Send Unsolicited NOPIN with reserved
1149 * TTT here to help the initiator figure out
1150 * the missing CmdSN, although they should be
1151 * intelligent enough to determine the missing
1152 * CmdSN and issue a retry to plug the sequence.
1153 */
1154 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001155 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001156 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1157 return -1;
1158
1159 return 0;
1160}
1161
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001162static int
1163iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1164 unsigned char *buf)
1165{
1166 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1167 int rc, immed_data;
1168 bool dump_payload = false;
1169
1170 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1171 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001172 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001173 /*
1174 * Allocation iovecs needed for struct socket operations for
1175 * traditional iSCSI block I/O.
1176 */
1177 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001178 return iscsit_add_reject_cmd(cmd,
1179 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001180 }
1181 immed_data = cmd->immediate_data;
1182
1183 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1184 if (rc < 0)
1185 return rc;
1186 else if (rc > 0)
1187 dump_payload = true;
1188
1189 if (!immed_data)
1190 return 0;
1191
1192 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1193}
1194
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001195static u32 iscsit_do_crypto_hash_sg(
1196 struct hash_desc *hash,
1197 struct iscsi_cmd *cmd,
1198 u32 data_offset,
1199 u32 data_length,
1200 u32 padding,
1201 u8 *pad_bytes)
1202{
1203 u32 data_crc;
1204 u32 i;
1205 struct scatterlist *sg;
1206 unsigned int page_off;
1207
1208 crypto_hash_init(hash);
1209
1210 sg = cmd->first_data_sg;
1211 page_off = cmd->first_data_sg_off;
1212
1213 i = 0;
1214 while (data_length) {
1215 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1216
1217 crypto_hash_update(hash, &sg[i], cur_len);
1218
1219 data_length -= cur_len;
1220 page_off = 0;
1221 i++;
1222 }
1223
1224 if (padding) {
1225 struct scatterlist pad_sg;
1226
1227 sg_init_one(&pad_sg, pad_bytes, padding);
1228 crypto_hash_update(hash, &pad_sg, padding);
1229 }
1230 crypto_hash_final(hash, (u8 *) &data_crc);
1231
1232 return data_crc;
1233}
1234
1235static void iscsit_do_crypto_hash_buf(
1236 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001237 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001238 u32 payload_length,
1239 u32 padding,
1240 u8 *pad_bytes,
1241 u8 *data_crc)
1242{
1243 struct scatterlist sg;
1244
1245 crypto_hash_init(hash);
1246
Jörn Engel8359cf42011-11-24 02:05:51 +01001247 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001248 crypto_hash_update(hash, &sg, payload_length);
1249
1250 if (padding) {
1251 sg_init_one(&sg, pad_bytes, padding);
1252 crypto_hash_update(hash, &sg, padding);
1253 }
1254 crypto_hash_final(hash, data_crc);
1255}
1256
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001257int
1258iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1259 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001260{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001261 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262 struct iscsi_cmd *cmd = NULL;
1263 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001264 u32 payload_length = ntoh24(hdr->dlength);
1265 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001266
1267 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001268 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1269 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001270 }
1271
1272 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001273 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001274
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001275 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001276 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001277 " MaxXmitDataSegmentLength: %u\n", payload_length,
1278 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001279 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1280 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001281 }
1282
1283 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1284 payload_length);
1285 if (!cmd)
1286 return 0;
1287
1288 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1289 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001290 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001291 payload_length, conn->cid);
1292
1293 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1294 pr_err("Command ITT: 0x%08x received DataOUT after"
1295 " last DataOUT received, dumping payload\n",
1296 cmd->init_task_tag);
1297 return iscsit_dump_data_payload(conn, payload_length, 1);
1298 }
1299
1300 if (cmd->data_direction != DMA_TO_DEVICE) {
1301 pr_err("Command ITT: 0x%08x received DataOUT for a"
1302 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001303 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001304 }
1305 se_cmd = &cmd->se_cmd;
1306 iscsit_mod_dataout_timer(cmd);
1307
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001308 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001309 pr_err("DataOut Offset: %u, Length %u greater than"
1310 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001311 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001312 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001313 }
1314
1315 if (cmd->unsolicited_data) {
1316 int dump_unsolicited_data = 0;
1317
1318 if (conn->sess->sess_ops->InitialR2T) {
1319 pr_err("Received unexpected unsolicited data"
1320 " while InitialR2T=Yes, protocol error.\n");
1321 transport_send_check_condition_and_sense(&cmd->se_cmd,
1322 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1323 return -1;
1324 }
1325 /*
1326 * Special case for dealing with Unsolicited DataOUT
1327 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1328 * failures;
1329 */
1330
1331 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001332 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001333 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001334 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001335
1336 if (dump_unsolicited_data) {
1337 /*
1338 * Check if a delayed TASK_ABORTED status needs to
1339 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1340 * received with the unsolicitied data out.
1341 */
1342 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1343 iscsit_stop_dataout_timer(cmd);
1344
1345 transport_check_aborted_status(se_cmd,
1346 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1347 return iscsit_dump_data_payload(conn, payload_length, 1);
1348 }
1349 } else {
1350 /*
1351 * For the normal solicited data path:
1352 *
1353 * Check for a delayed TASK_ABORTED status and dump any
1354 * incoming data out payload if one exists. Also, when the
1355 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1356 * data out sequence, we decrement outstanding_r2ts. Once
1357 * outstanding_r2ts reaches zero, go ahead and send the delayed
1358 * TASK_ABORTED status.
1359 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001360 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001361 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1362 if (--cmd->outstanding_r2ts < 1) {
1363 iscsit_stop_dataout_timer(cmd);
1364 transport_check_aborted_status(
1365 se_cmd, 1);
1366 }
1367
1368 return iscsit_dump_data_payload(conn, payload_length, 1);
1369 }
1370 }
1371 /*
1372 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1373 * within-command recovery checks before receiving the payload.
1374 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001375 rc = iscsit_check_pre_dataout(cmd, buf);
1376 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001377 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001378 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001379 return -1;
1380
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001381 *out_cmd = cmd;
1382 return 0;
1383}
1384EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1385
1386static int
1387iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1388 struct iscsi_data *hdr)
1389{
1390 struct kvec *iov;
1391 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1392 u32 payload_length = ntoh24(hdr->dlength);
1393 int iov_ret, data_crc_failed = 0;
1394
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001395 rx_size += payload_length;
1396 iov = &cmd->iov_data[0];
1397
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001398 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1399 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001400 if (iov_ret < 0)
1401 return -1;
1402
1403 iov_count += iov_ret;
1404
1405 padding = ((-payload_length) & 3);
1406 if (padding != 0) {
1407 iov[iov_count].iov_base = cmd->pad_bytes;
1408 iov[iov_count++].iov_len = padding;
1409 rx_size += padding;
1410 pr_debug("Receiving %u padding bytes.\n", padding);
1411 }
1412
1413 if (conn->conn_ops->DataDigest) {
1414 iov[iov_count].iov_base = &checksum;
1415 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1416 rx_size += ISCSI_CRC_LEN;
1417 }
1418
1419 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1420
1421 iscsit_unmap_iovec(cmd);
1422
1423 if (rx_got != rx_size)
1424 return -1;
1425
1426 if (conn->conn_ops->DataDigest) {
1427 u32 data_crc;
1428
1429 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001430 be32_to_cpu(hdr->offset),
1431 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001432 cmd->pad_bytes);
1433
1434 if (checksum != data_crc) {
1435 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1436 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1437 " does not match computed 0x%08x\n",
1438 hdr->itt, hdr->offset, payload_length,
1439 hdr->datasn, checksum, data_crc);
1440 data_crc_failed = 1;
1441 } else {
1442 pr_debug("Got CRC32C DataDigest 0x%08x for"
1443 " %u bytes of Data Out\n", checksum,
1444 payload_length);
1445 }
1446 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001447
1448 return data_crc_failed;
1449}
1450
1451int
1452iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1453 bool data_crc_failed)
1454{
1455 struct iscsi_conn *conn = cmd->conn;
1456 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001457 /*
1458 * Increment post receive data and CRC values or perform
1459 * within-command recovery.
1460 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001461 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1462 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001463 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001464 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001465 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001466 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1467 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001468 /*
1469 * Handle extra special case for out of order
1470 * Unsolicited Data Out.
1471 */
1472 spin_lock_bh(&cmd->istate_lock);
1473 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1474 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1475 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1476 spin_unlock_bh(&cmd->istate_lock);
1477
1478 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001479 if (ooo_cmdsn)
1480 return 0;
1481 target_execute_cmd(&cmd->se_cmd);
1482 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001483 } else /* DATAOUT_CANNOT_RECOVER */
1484 return -1;
1485
1486 return 0;
1487}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001488EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001489
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001490static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1491{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001492 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001493 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1494 int rc;
1495 bool data_crc_failed = false;
1496
1497 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1498 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001499 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001500 else if (!cmd)
1501 return 0;
1502
1503 rc = iscsit_get_dataout(conn, cmd, hdr);
1504 if (rc < 0)
1505 return rc;
1506 else if (rc > 0)
1507 data_crc_failed = true;
1508
1509 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1510}
1511
Nicholas Bellinger778de362013-06-14 16:07:47 -07001512int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1513 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001514{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001515 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001516
Arshad Hussaina3662602014-03-14 15:28:59 -07001517 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1518 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1519 if (!cmd)
1520 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1521 (unsigned char *)hdr);
1522
1523 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1524 (unsigned char *)hdr);
1525 }
1526
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001527 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001528 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1529 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001530 if (!cmd)
1531 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1532 (unsigned char *)hdr);
1533
Nicholas Bellingerba159912013-07-03 03:48:24 -07001534 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1535 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001536 }
1537
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001538 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001539 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001540 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001541 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001542 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001543 if (!cmd)
1544 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1545 (unsigned char *)hdr);
1546
Nicholas Bellingerba159912013-07-03 03:48:24 -07001547 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1548 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001549 }
1550
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001551 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001552 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001553 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001554 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1555 payload_length);
1556 /*
1557 * This is not a response to a Unsolicited NopIN, which means
1558 * it can either be a NOPOUT ping request (with a valid ITT),
1559 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1560 * Either way, make sure we allocate an struct iscsi_cmd, as both
1561 * can contain ping data.
1562 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001563 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001564 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1565 cmd->i_state = ISTATE_SEND_NOPIN;
1566 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1567 1 : 0);
1568 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1569 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001570 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1571 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001572 cmd->data_direction = DMA_NONE;
1573 }
1574
Nicholas Bellinger778de362013-06-14 16:07:47 -07001575 return 0;
1576}
1577EXPORT_SYMBOL(iscsit_setup_nop_out);
1578
1579int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1580 struct iscsi_nopout *hdr)
1581{
1582 struct iscsi_cmd *cmd_p = NULL;
1583 int cmdsn_ret = 0;
1584 /*
1585 * Initiator is expecting a NopIN ping reply..
1586 */
1587 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001588 if (!cmd)
1589 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1590 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001591
1592 spin_lock_bh(&conn->cmd_lock);
1593 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1594 spin_unlock_bh(&conn->cmd_lock);
1595
1596 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1597
1598 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1599 iscsit_add_cmd_to_response_queue(cmd, conn,
1600 cmd->i_state);
1601 return 0;
1602 }
1603
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001604 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1605 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001606 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1607 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001608 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001609 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001610
1611 return 0;
1612 }
1613 /*
1614 * This was a response to a unsolicited NOPIN ping.
1615 */
1616 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1617 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1618 if (!cmd_p)
1619 return -EINVAL;
1620
1621 iscsit_stop_nopin_response_timer(conn);
1622
1623 cmd_p->i_state = ISTATE_REMOVE;
1624 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1625
1626 iscsit_start_nopin_timer(conn);
1627 return 0;
1628 }
1629 /*
1630 * Otherwise, initiator is not expecting a NOPIN is response.
1631 * Just ignore for now.
1632 */
1633 return 0;
1634}
1635EXPORT_SYMBOL(iscsit_process_nop_out);
1636
1637static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1638 unsigned char *buf)
1639{
1640 unsigned char *ping_data = NULL;
1641 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1642 struct kvec *iov = NULL;
1643 u32 payload_length = ntoh24(hdr->dlength);
1644 int ret;
1645
1646 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1647 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001648 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001649 /*
1650 * Handle NOP-OUT payload for traditional iSCSI sockets
1651 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001652 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001653 u32 checksum, data_crc, padding = 0;
1654 int niov = 0, rx_got, rx_size = payload_length;
1655
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1657 if (!ping_data) {
1658 pr_err("Unable to allocate memory for"
1659 " NOPOUT ping data.\n");
1660 ret = -1;
1661 goto out;
1662 }
1663
1664 iov = &cmd->iov_misc[0];
1665 iov[niov].iov_base = ping_data;
1666 iov[niov++].iov_len = payload_length;
1667
1668 padding = ((-payload_length) & 3);
1669 if (padding != 0) {
1670 pr_debug("Receiving %u additional bytes"
1671 " for padding.\n", padding);
1672 iov[niov].iov_base = &cmd->pad_bytes;
1673 iov[niov++].iov_len = padding;
1674 rx_size += padding;
1675 }
1676 if (conn->conn_ops->DataDigest) {
1677 iov[niov].iov_base = &checksum;
1678 iov[niov++].iov_len = ISCSI_CRC_LEN;
1679 rx_size += ISCSI_CRC_LEN;
1680 }
1681
1682 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1683 if (rx_got != rx_size) {
1684 ret = -1;
1685 goto out;
1686 }
1687
1688 if (conn->conn_ops->DataDigest) {
1689 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1690 ping_data, payload_length,
1691 padding, cmd->pad_bytes,
1692 (u8 *)&data_crc);
1693
1694 if (checksum != data_crc) {
1695 pr_err("Ping data CRC32C DataDigest"
1696 " 0x%08x does not match computed 0x%08x\n",
1697 checksum, data_crc);
1698 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1699 pr_err("Unable to recover from"
1700 " NOPOUT Ping DataCRC failure while in"
1701 " ERL=0.\n");
1702 ret = -1;
1703 goto out;
1704 } else {
1705 /*
1706 * Silently drop this PDU and let the
1707 * initiator plug the CmdSN gap.
1708 */
1709 pr_debug("Dropping NOPOUT"
1710 " Command CmdSN: 0x%08x due to"
1711 " DataCRC error.\n", hdr->cmdsn);
1712 ret = 0;
1713 goto out;
1714 }
1715 } else {
1716 pr_debug("Got CRC32C DataDigest"
1717 " 0x%08x for %u bytes of ping data.\n",
1718 checksum, payload_length);
1719 }
1720 }
1721
1722 ping_data[payload_length] = '\0';
1723 /*
1724 * Attach ping data to struct iscsi_cmd->buf_ptr.
1725 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001726 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001727 cmd->buf_ptr_size = payload_length;
1728
1729 pr_debug("Got %u bytes of NOPOUT ping"
1730 " data.\n", payload_length);
1731 pr_debug("Ping Data: \"%s\"\n", ping_data);
1732 }
1733
Nicholas Bellinger778de362013-06-14 16:07:47 -07001734 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735out:
1736 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001737 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001738
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739 kfree(ping_data);
1740 return ret;
1741}
1742
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001743int
1744iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1745 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001746{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 struct se_tmr_req *se_tmr;
1748 struct iscsi_tmr_req *tmr_req;
1749 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001750 int out_of_order_cmdsn = 0, ret;
1751 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001752 u8 function;
1753
1754 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1756 function = hdr->flags;
1757
1758 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1759 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1760 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1761 hdr->rtt, hdr->refcmdsn, conn->cid);
1762
1763 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1764 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001765 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001766 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001767 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001768 }
1769
1770 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1771 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1772 pr_err("Task Management Request TASK_REASSIGN not"
1773 " issued as immediate command, bad iSCSI Initiator"
1774 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001775 return iscsit_add_reject_cmd(cmd,
1776 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001777 }
1778 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001779 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1780 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001781
Andy Groverd28b11692012-04-03 15:51:22 -07001782 cmd->data_direction = DMA_NONE;
1783
1784 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1785 if (!cmd->tmr_req) {
1786 pr_err("Unable to allocate memory for"
1787 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001788 return iscsit_add_reject_cmd(cmd,
1789 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1790 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001791 }
1792
1793 /*
1794 * TASK_REASSIGN for ERL=2 / connection stays inside of
1795 * LIO-Target $FABRIC_MOD
1796 */
1797 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1798
1799 u8 tcm_function;
1800 int ret;
1801
1802 transport_init_se_cmd(&cmd->se_cmd,
1803 &lio_target_fabric_configfs->tf_ops,
1804 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001805 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001806
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001807 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1808 sess_ref = true;
1809
Andy Groverd28b11692012-04-03 15:51:22 -07001810 switch (function) {
1811 case ISCSI_TM_FUNC_ABORT_TASK:
1812 tcm_function = TMR_ABORT_TASK;
1813 break;
1814 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1815 tcm_function = TMR_ABORT_TASK_SET;
1816 break;
1817 case ISCSI_TM_FUNC_CLEAR_ACA:
1818 tcm_function = TMR_CLEAR_ACA;
1819 break;
1820 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1821 tcm_function = TMR_CLEAR_TASK_SET;
1822 break;
1823 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1824 tcm_function = TMR_LUN_RESET;
1825 break;
1826 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1827 tcm_function = TMR_TARGET_WARM_RESET;
1828 break;
1829 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1830 tcm_function = TMR_TARGET_COLD_RESET;
1831 break;
1832 default:
1833 pr_err("Unknown iSCSI TMR Function:"
1834 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001835 return iscsit_add_reject_cmd(cmd,
1836 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001837 }
1838
1839 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1840 tcm_function, GFP_KERNEL);
1841 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001842 return iscsit_add_reject_cmd(cmd,
1843 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001844
1845 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1846 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001847
1848 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1849 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1850 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1851 cmd->init_task_tag = hdr->itt;
1852 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001853 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1854 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001855 se_tmr = cmd->se_cmd.se_tmr_req;
1856 tmr_req = cmd->tmr_req;
1857 /*
1858 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1859 */
1860 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001861 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1862 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001863 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001864 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1865 goto attach;
1866 }
1867 }
1868
1869 switch (function) {
1870 case ISCSI_TM_FUNC_ABORT_TASK:
1871 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001872 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001873 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001874 break;
1875 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1876 case ISCSI_TM_FUNC_CLEAR_ACA:
1877 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1878 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1879 break;
1880 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1881 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001882 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1883 goto attach;
1884 }
1885 break;
1886 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1887 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001888 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1889 goto attach;
1890 }
1891 break;
1892 case ISCSI_TM_FUNC_TASK_REASSIGN:
1893 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1894 /*
1895 * Perform sanity checks on the ExpDataSN only if the
1896 * TASK_REASSIGN was successful.
1897 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001898 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001899 break;
1900
1901 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001902 return iscsit_add_reject_cmd(cmd,
1903 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001904 break;
1905 default:
1906 pr_err("Unknown TMR function: 0x%02x, protocol"
1907 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001908 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1909 goto attach;
1910 }
1911
1912 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1913 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1914 se_tmr->call_transport = 1;
1915attach:
1916 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001917 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001918 spin_unlock_bh(&conn->cmd_lock);
1919
1920 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001921 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001922 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1923 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001924 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001925 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001926 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001927 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001929 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001930
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001931 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001932 return 0;
1933 /*
1934 * Found the referenced task, send to transport for processing.
1935 */
1936 if (se_tmr->call_transport)
1937 return transport_generic_handle_tmr(&cmd->se_cmd);
1938
1939 /*
1940 * Could not find the referenced LUN, task, or Task Management
1941 * command not authorized or supported. Change state and
1942 * let the tx_thread send the response.
1943 *
1944 * For connection recovery, this is also the default action for
1945 * TMR TASK_REASSIGN.
1946 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001947 if (sess_ref) {
1948 pr_debug("Handle TMR, using sess_ref=true check\n");
1949 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1950 }
1951
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001952 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1953 return 0;
1954}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001955EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001956
1957/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001958int
1959iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1960 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001961{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001962 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001963
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001964 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001965 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001966 "greater than MaxXmitDataSegmentLength %u.\n",
1967 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001968 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1969 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970 }
1971
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001972 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1973 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1974 pr_err("Multi sequence text commands currently not supported\n");
1975 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1976 (unsigned char *)hdr);
1977 }
1978
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001979 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1980 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1981 hdr->exp_statsn, payload_length);
1982
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001983 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1984 cmd->i_state = ISTATE_SEND_TEXTRSP;
1985 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1986 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1987 cmd->targ_xfer_tag = 0xFFFFFFFF;
1988 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1989 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1990 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001991 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001992
1993 return 0;
1994}
1995EXPORT_SYMBOL(iscsit_setup_text_cmd);
1996
1997int
1998iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1999 struct iscsi_text *hdr)
2000{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002001 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002002 int cmdsn_ret;
2003
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002004 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002005 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2006 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2007 pr_err("Unable to locate text_in buffer for sendtargets"
2008 " discovery\n");
2009 goto reject;
2010 }
2011 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002012 }
2013 if (strncmp("SendTargets", text_in, 11) != 0) {
2014 pr_err("Received Text Data that is not"
2015 " SendTargets, cannot continue.\n");
2016 goto reject;
2017 }
2018 text_ptr = strchr(text_in, '=');
2019 if (!text_ptr) {
2020 pr_err("No \"=\" separator found in Text Data,"
2021 " cannot continue.\n");
2022 goto reject;
2023 }
2024 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002025 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002026 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2027 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002028 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002029 } else {
2030 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2031 goto reject;
2032 }
2033
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002034 spin_lock_bh(&conn->cmd_lock);
2035 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2036 spin_unlock_bh(&conn->cmd_lock);
2037
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002038empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002039 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2040
2041 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002042 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2043 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002044 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002045 return -1;
2046
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002047 return 0;
2048 }
2049
2050 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002051
2052reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002053 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2054 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002055}
2056EXPORT_SYMBOL(iscsit_process_text_cmd);
2057
2058static int
2059iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2060 unsigned char *buf)
2061{
2062 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2063 char *text_in = NULL;
2064 u32 payload_length = ntoh24(hdr->dlength);
2065 int rx_size, rc;
2066
2067 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2068 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002069 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002070
2071 rx_size = payload_length;
2072 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002073 u32 checksum = 0, data_crc = 0;
2074 u32 padding = 0, pad_bytes = 0;
2075 int niov = 0, rx_got;
2076 struct kvec iov[3];
2077
2078 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079 if (!text_in) {
2080 pr_err("Unable to allocate memory for"
2081 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002082 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002083 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002084 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085
2086 memset(iov, 0, 3 * sizeof(struct kvec));
2087 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002088 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089
2090 padding = ((-payload_length) & 3);
2091 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002092 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002093 iov[niov++].iov_len = padding;
2094 rx_size += padding;
2095 pr_debug("Receiving %u additional bytes"
2096 " for padding.\n", padding);
2097 }
2098 if (conn->conn_ops->DataDigest) {
2099 iov[niov].iov_base = &checksum;
2100 iov[niov++].iov_len = ISCSI_CRC_LEN;
2101 rx_size += ISCSI_CRC_LEN;
2102 }
2103
2104 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002105 if (rx_got != rx_size)
2106 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107
2108 if (conn->conn_ops->DataDigest) {
2109 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002110 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002111 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112 (u8 *)&data_crc);
2113
2114 if (checksum != data_crc) {
2115 pr_err("Text data CRC32C DataDigest"
2116 " 0x%08x does not match computed"
2117 " 0x%08x\n", checksum, data_crc);
2118 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2119 pr_err("Unable to recover from"
2120 " Text Data digest failure while in"
2121 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002122 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002123 } else {
2124 /*
2125 * Silently drop this PDU and let the
2126 * initiator plug the CmdSN gap.
2127 */
2128 pr_debug("Dropping Text"
2129 " Command CmdSN: 0x%08x due to"
2130 " DataCRC error.\n", hdr->cmdsn);
2131 kfree(text_in);
2132 return 0;
2133 }
2134 } else {
2135 pr_debug("Got CRC32C DataDigest"
2136 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002137 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002138 }
2139 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002140 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002141 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002142 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002143 }
2144
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002145 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002146
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002147reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002148 kfree(cmd->text_in_ptr);
2149 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002150 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002151}
2152
2153int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2154{
2155 struct iscsi_conn *conn_p;
2156 struct iscsi_session *sess = conn->sess;
2157
2158 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2159 " for SID: %u.\n", conn->cid, conn->sess->sid);
2160
2161 atomic_set(&sess->session_logout, 1);
2162 atomic_set(&conn->conn_logout_remove, 1);
2163 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2164
2165 iscsit_inc_conn_usage_count(conn);
2166 iscsit_inc_session_usage_count(sess);
2167
2168 spin_lock_bh(&sess->conn_lock);
2169 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2170 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2171 continue;
2172
2173 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2174 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2175 }
2176 spin_unlock_bh(&sess->conn_lock);
2177
2178 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2179
2180 return 0;
2181}
2182
2183int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2184{
2185 struct iscsi_conn *l_conn;
2186 struct iscsi_session *sess = conn->sess;
2187
2188 pr_debug("Received logout request CLOSECONNECTION for CID:"
2189 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2190
2191 /*
2192 * A Logout Request with a CLOSECONNECTION reason code for a CID
2193 * can arrive on a connection with a differing CID.
2194 */
2195 if (conn->cid == cmd->logout_cid) {
2196 spin_lock_bh(&conn->state_lock);
2197 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2198 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2199
2200 atomic_set(&conn->conn_logout_remove, 1);
2201 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2202 iscsit_inc_conn_usage_count(conn);
2203
2204 spin_unlock_bh(&conn->state_lock);
2205 } else {
2206 /*
2207 * Handle all different cid CLOSECONNECTION requests in
2208 * iscsit_logout_post_handler_diffcid() as to give enough
2209 * time for any non immediate command's CmdSN to be
2210 * acknowledged on the connection in question.
2211 *
2212 * Here we simply make sure the CID is still around.
2213 */
2214 l_conn = iscsit_get_conn_from_cid(sess,
2215 cmd->logout_cid);
2216 if (!l_conn) {
2217 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2218 iscsit_add_cmd_to_response_queue(cmd, conn,
2219 cmd->i_state);
2220 return 0;
2221 }
2222
2223 iscsit_dec_conn_usage_count(l_conn);
2224 }
2225
2226 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2227
2228 return 0;
2229}
2230
2231int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2232{
2233 struct iscsi_session *sess = conn->sess;
2234
2235 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2236 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2237
2238 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2239 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2240 " while ERL!=2.\n");
2241 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2242 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2243 return 0;
2244 }
2245
2246 if (conn->cid == cmd->logout_cid) {
2247 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2248 " with CID: %hu on CID: %hu, implementation error.\n",
2249 cmd->logout_cid, conn->cid);
2250 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2251 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2252 return 0;
2253 }
2254
2255 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2256
2257 return 0;
2258}
2259
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002260int
2261iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2262 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002263{
2264 int cmdsn_ret, logout_remove = 0;
2265 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266 struct iscsi_logout *hdr;
2267 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2268
2269 hdr = (struct iscsi_logout *) buf;
2270 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002271
2272 if (tiqn) {
2273 spin_lock(&tiqn->logout_stats.lock);
2274 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2275 tiqn->logout_stats.normal_logouts++;
2276 else
2277 tiqn->logout_stats.abnormal_logouts++;
2278 spin_unlock(&tiqn->logout_stats.lock);
2279 }
2280
2281 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2282 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2283 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2284 hdr->cid, conn->cid);
2285
2286 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2287 pr_err("Received logout request on connection that"
2288 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002289 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002290 return 0;
2291 }
2292
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002293 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2294 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2295 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2296 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2297 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002298 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2299 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2300 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002301 cmd->logout_reason = reason_code;
2302 cmd->data_direction = DMA_NONE;
2303
2304 /*
2305 * We need to sleep in these cases (by returning 1) until the Logout
2306 * Response gets sent in the tx thread.
2307 */
2308 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2309 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002310 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002311 logout_remove = 1;
2312
2313 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002314 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002315 spin_unlock_bh(&conn->cmd_lock);
2316
2317 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002318 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002319
2320 /*
2321 * Immediate commands are executed, well, immediately.
2322 * Non-Immediate Logout Commands are executed in CmdSN order.
2323 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002324 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002325 int ret = iscsit_execute_cmd(cmd, 0);
2326
2327 if (ret < 0)
2328 return ret;
2329 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002330 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002331 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002332 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002333 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2334 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002335 }
2336
2337 return logout_remove;
2338}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002339EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002340
2341static int iscsit_handle_snack(
2342 struct iscsi_conn *conn,
2343 unsigned char *buf)
2344{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002345 struct iscsi_snack *hdr;
2346
2347 hdr = (struct iscsi_snack *) buf;
2348 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002349
2350 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2351 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2352 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2353 hdr->begrun, hdr->runlength, conn->cid);
2354
2355 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2356 pr_err("Initiator sent SNACK request while in"
2357 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002358 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2359 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002360 }
2361 /*
2362 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2363 * call from inside iscsi_send_recovery_datain_or_r2t().
2364 */
2365 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2366 case 0:
2367 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002368 hdr->itt,
2369 be32_to_cpu(hdr->ttt),
2370 be32_to_cpu(hdr->begrun),
2371 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002372 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002373 return iscsit_handle_status_snack(conn, hdr->itt,
2374 be32_to_cpu(hdr->ttt),
2375 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002376 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002377 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2378 be32_to_cpu(hdr->begrun),
2379 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002380 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2381 /* FIXME: Support R-Data SNACK */
2382 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002383 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2384 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002385 default:
2386 pr_err("Unknown SNACK type 0x%02x, protocol"
2387 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002388 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2389 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002390 }
2391
2392 return 0;
2393}
2394
2395static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2396{
2397 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2398 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2399 wait_for_completion_interruptible_timeout(
2400 &conn->rx_half_close_comp,
2401 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2402 }
2403}
2404
2405static int iscsit_handle_immediate_data(
2406 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002407 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002408 u32 length)
2409{
2410 int iov_ret, rx_got = 0, rx_size = 0;
2411 u32 checksum, iov_count = 0, padding = 0;
2412 struct iscsi_conn *conn = cmd->conn;
2413 struct kvec *iov;
2414
2415 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2416 if (iov_ret < 0)
2417 return IMMEDIATE_DATA_CANNOT_RECOVER;
2418
2419 rx_size = length;
2420 iov_count = iov_ret;
2421 iov = &cmd->iov_data[0];
2422
2423 padding = ((-length) & 3);
2424 if (padding != 0) {
2425 iov[iov_count].iov_base = cmd->pad_bytes;
2426 iov[iov_count++].iov_len = padding;
2427 rx_size += padding;
2428 }
2429
2430 if (conn->conn_ops->DataDigest) {
2431 iov[iov_count].iov_base = &checksum;
2432 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2433 rx_size += ISCSI_CRC_LEN;
2434 }
2435
2436 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2437
2438 iscsit_unmap_iovec(cmd);
2439
2440 if (rx_got != rx_size) {
2441 iscsit_rx_thread_wait_for_tcp(conn);
2442 return IMMEDIATE_DATA_CANNOT_RECOVER;
2443 }
2444
2445 if (conn->conn_ops->DataDigest) {
2446 u32 data_crc;
2447
2448 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2449 cmd->write_data_done, length, padding,
2450 cmd->pad_bytes);
2451
2452 if (checksum != data_crc) {
2453 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2454 " does not match computed 0x%08x\n", checksum,
2455 data_crc);
2456
2457 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2458 pr_err("Unable to recover from"
2459 " Immediate Data digest failure while"
2460 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002461 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002462 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002463 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002464 return IMMEDIATE_DATA_CANNOT_RECOVER;
2465 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002466 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002467 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002468 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002469 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2470 }
2471 } else {
2472 pr_debug("Got CRC32C DataDigest 0x%08x for"
2473 " %u bytes of Immediate Data\n", checksum,
2474 length);
2475 }
2476 }
2477
2478 cmd->write_data_done += length;
2479
Andy Groverebf1d952012-04-03 15:51:24 -07002480 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002481 spin_lock_bh(&cmd->istate_lock);
2482 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2483 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2484 spin_unlock_bh(&cmd->istate_lock);
2485 }
2486
2487 return IMMEDIATE_DATA_NORMAL_OPERATION;
2488}
2489
2490/*
2491 * Called with sess->conn_lock held.
2492 */
2493/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2494 with active network interface */
2495static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2496{
2497 struct iscsi_cmd *cmd;
2498 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002499 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002500
2501 /*
2502 * Only send a Asynchronous Message on connections whos network
2503 * interface is still functional.
2504 */
2505 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2506 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2507 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002508 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509 break;
2510 }
2511 }
2512
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002513 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002514 return;
2515
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002516 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002517 if (!cmd) {
2518 iscsit_dec_conn_usage_count(conn_p);
2519 return;
2520 }
2521
2522 cmd->logout_cid = conn->cid;
2523 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2524 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2525
2526 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002527 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002528 spin_unlock_bh(&conn_p->cmd_lock);
2529
2530 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2531 iscsit_dec_conn_usage_count(conn_p);
2532}
2533
2534static int iscsit_send_conn_drop_async_message(
2535 struct iscsi_cmd *cmd,
2536 struct iscsi_conn *conn)
2537{
2538 struct iscsi_async *hdr;
2539
2540 cmd->tx_size = ISCSI_HDR_LEN;
2541 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2542
2543 hdr = (struct iscsi_async *) cmd->pdu;
2544 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2545 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002546 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002547 cmd->targ_xfer_tag = 0xFFFFFFFF;
2548 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2549 cmd->stat_sn = conn->stat_sn++;
2550 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2551 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2552 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2553 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2554 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2555 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2556 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2557
2558 if (conn->conn_ops->HeaderDigest) {
2559 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2560
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002561 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2562 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002563
2564 cmd->tx_size += ISCSI_CRC_LEN;
2565 pr_debug("Attaching CRC32C HeaderDigest to"
2566 " Async Message 0x%08x\n", *header_digest);
2567 }
2568
2569 cmd->iov_misc[0].iov_base = cmd->pdu;
2570 cmd->iov_misc[0].iov_len = cmd->tx_size;
2571 cmd->iov_misc_count = 1;
2572
2573 pr_debug("Sending Connection Dropped Async Message StatSN:"
2574 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2575 cmd->logout_cid, conn->cid);
2576 return 0;
2577}
2578
Andy Grover6f3c0e62012-04-03 15:51:09 -07002579static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2580{
2581 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2582 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2583 wait_for_completion_interruptible_timeout(
2584 &conn->tx_half_close_comp,
2585 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2586 }
2587}
2588
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002589static void
2590iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2591 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2592 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002593{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002594 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2595 hdr->flags = datain->flags;
2596 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2597 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2598 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2599 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2600 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2601 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2602 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2603 }
2604 }
2605 hton24(hdr->dlength, datain->length);
2606 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2607 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2608 (struct scsi_lun *)&hdr->lun);
2609 else
2610 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2611
2612 hdr->itt = cmd->init_task_tag;
2613
2614 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2615 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2616 else
2617 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2618 if (set_statsn)
2619 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2620 else
2621 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2622
2623 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2624 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2625 hdr->datasn = cpu_to_be32(datain->data_sn);
2626 hdr->offset = cpu_to_be32(datain->offset);
2627
2628 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2629 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2630 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2631 ntohl(hdr->offset), datain->length, conn->cid);
2632}
2633
2634static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2635{
2636 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002637 struct iscsi_datain datain;
2638 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002639 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002640 u32 iov_count = 0, tx_size = 0;
2641 int eodr = 0, ret, iov_ret;
2642 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002643
2644 memset(&datain, 0, sizeof(struct iscsi_datain));
2645 dr = iscsit_get_datain_values(cmd, &datain);
2646 if (!dr) {
2647 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2648 cmd->init_task_tag);
2649 return -1;
2650 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002651 /*
2652 * Be paranoid and double check the logic for now.
2653 */
Andy Groverebf1d952012-04-03 15:51:24 -07002654 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002655 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2656 " datain.length: %u exceeds cmd->data_length: %u\n",
2657 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002658 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002659 return -1;
2660 }
2661
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002662 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002663 /*
2664 * Special case for successfully execution w/ both DATAIN
2665 * and Sense Data.
2666 */
2667 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2668 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2669 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2670 else {
2671 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2672 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2673 iscsit_increment_maxcmdsn(cmd, conn->sess);
2674 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002675 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002676 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002677 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2678 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002679 }
2680
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002681 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682
2683 iov = &cmd->iov_data[0];
2684 iov[iov_count].iov_base = cmd->pdu;
2685 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2686 tx_size += ISCSI_HDR_LEN;
2687
2688 if (conn->conn_ops->HeaderDigest) {
2689 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2690
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002691 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2692 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002693
2694 iov[0].iov_len += ISCSI_CRC_LEN;
2695 tx_size += ISCSI_CRC_LEN;
2696
2697 pr_debug("Attaching CRC32 HeaderDigest"
2698 " for DataIN PDU 0x%08x\n", *header_digest);
2699 }
2700
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002701 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2702 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002703 if (iov_ret < 0)
2704 return -1;
2705
2706 iov_count += iov_ret;
2707 tx_size += datain.length;
2708
2709 cmd->padding = ((-datain.length) & 3);
2710 if (cmd->padding) {
2711 iov[iov_count].iov_base = cmd->pad_bytes;
2712 iov[iov_count++].iov_len = cmd->padding;
2713 tx_size += cmd->padding;
2714
2715 pr_debug("Attaching %u padding bytes\n",
2716 cmd->padding);
2717 }
2718 if (conn->conn_ops->DataDigest) {
2719 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2720 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2721
2722 iov[iov_count].iov_base = &cmd->data_crc;
2723 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2724 tx_size += ISCSI_CRC_LEN;
2725
2726 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2727 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2728 }
2729
2730 cmd->iov_data_count = iov_count;
2731 cmd->tx_size = tx_size;
2732
Andy Grover6f3c0e62012-04-03 15:51:09 -07002733 /* sendpage is preferred but can't insert markers */
2734 if (!conn->conn_ops->IFMarker)
2735 ret = iscsit_fe_sendpage_sg(cmd, conn);
2736 else
2737 ret = iscsit_send_tx_data(cmd, conn, 0);
2738
2739 iscsit_unmap_iovec(cmd);
2740
2741 if (ret < 0) {
2742 iscsit_tx_thread_wait_for_tcp(conn);
2743 return ret;
2744 }
2745
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002746 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002747 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002748 2 : 1;
2749 iscsit_free_datain_req(cmd, dr);
2750 }
2751
Andy Grover6f3c0e62012-04-03 15:51:09 -07002752 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002753}
2754
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002755int
2756iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2757 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002758{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002759 struct iscsi_conn *logout_conn = NULL;
2760 struct iscsi_conn_recovery *cr = NULL;
2761 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002762 /*
2763 * The actual shutting down of Sessions and/or Connections
2764 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2765 * is done in scsi_logout_post_handler().
2766 */
2767 switch (cmd->logout_reason) {
2768 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2769 pr_debug("iSCSI session logout successful, setting"
2770 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2771 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2772 break;
2773 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2774 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2775 break;
2776 /*
2777 * For CLOSECONNECTION logout requests carrying
2778 * a matching logout CID -> local CID, the reference
2779 * for the local CID will have been incremented in
2780 * iscsi_logout_closeconnection().
2781 *
2782 * For CLOSECONNECTION logout requests carrying
2783 * a different CID than the connection it arrived
2784 * on, the connection responding to cmd->logout_cid
2785 * is stopped in iscsit_logout_post_handler_diffcid().
2786 */
2787
2788 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2789 " successful.\n", cmd->logout_cid, conn->cid);
2790 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2791 break;
2792 case ISCSI_LOGOUT_REASON_RECOVERY:
2793 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2794 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2795 break;
2796 /*
2797 * If the connection is still active from our point of view
2798 * force connection recovery to occur.
2799 */
2800 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2801 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002802 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002803 iscsit_connection_reinstatement_rcfr(logout_conn);
2804 iscsit_dec_conn_usage_count(logout_conn);
2805 }
2806
2807 cr = iscsit_get_inactive_connection_recovery_entry(
2808 conn->sess, cmd->logout_cid);
2809 if (!cr) {
2810 pr_err("Unable to locate CID: %hu for"
2811 " REMOVECONNFORRECOVERY Logout Request.\n",
2812 cmd->logout_cid);
2813 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2814 break;
2815 }
2816
2817 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2818
2819 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2820 " for recovery for CID: %hu on CID: %hu successful.\n",
2821 cmd->logout_cid, conn->cid);
2822 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2823 break;
2824 default:
2825 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2826 cmd->logout_reason);
2827 return -1;
2828 }
2829
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002830 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2831 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2832 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002833 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002834 cmd->stat_sn = conn->stat_sn++;
2835 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2836
2837 iscsit_increment_maxcmdsn(cmd, conn->sess);
2838 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2839 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2840
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002841 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2842 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2843 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2844 cmd->logout_cid, conn->cid);
2845
2846 return 0;
2847}
2848EXPORT_SYMBOL(iscsit_build_logout_rsp);
2849
2850static int
2851iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2852{
2853 struct kvec *iov;
2854 int niov = 0, tx_size, rc;
2855
2856 rc = iscsit_build_logout_rsp(cmd, conn,
2857 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2858 if (rc < 0)
2859 return rc;
2860
2861 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002862 iov = &cmd->iov_misc[0];
2863 iov[niov].iov_base = cmd->pdu;
2864 iov[niov++].iov_len = ISCSI_HDR_LEN;
2865
2866 if (conn->conn_ops->HeaderDigest) {
2867 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2868
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002869 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2870 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002871
2872 iov[0].iov_len += ISCSI_CRC_LEN;
2873 tx_size += ISCSI_CRC_LEN;
2874 pr_debug("Attaching CRC32C HeaderDigest to"
2875 " Logout Response 0x%08x\n", *header_digest);
2876 }
2877 cmd->iov_misc_count = niov;
2878 cmd->tx_size = tx_size;
2879
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002880 return 0;
2881}
2882
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002883void
2884iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2885 struct iscsi_nopin *hdr, bool nopout_response)
2886{
2887 hdr->opcode = ISCSI_OP_NOOP_IN;
2888 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2889 hton24(hdr->dlength, cmd->buf_ptr_size);
2890 if (nopout_response)
2891 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2892 hdr->itt = cmd->init_task_tag;
2893 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2894 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2895 conn->stat_sn;
2896 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2897
2898 if (nopout_response)
2899 iscsit_increment_maxcmdsn(cmd, conn->sess);
2900
2901 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2902 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2903
2904 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2905 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2906 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2907 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2908}
2909EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2910
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002911/*
2912 * Unsolicited NOPIN, either requesting a response or not.
2913 */
2914static int iscsit_send_unsolicited_nopin(
2915 struct iscsi_cmd *cmd,
2916 struct iscsi_conn *conn,
2917 int want_response)
2918{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002919 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2920 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002921
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002922 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002923
2924 if (conn->conn_ops->HeaderDigest) {
2925 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2926
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002927 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2928 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002929
2930 tx_size += ISCSI_CRC_LEN;
2931 pr_debug("Attaching CRC32C HeaderDigest to"
2932 " NopIN 0x%08x\n", *header_digest);
2933 }
2934
2935 cmd->iov_misc[0].iov_base = cmd->pdu;
2936 cmd->iov_misc[0].iov_len = tx_size;
2937 cmd->iov_misc_count = 1;
2938 cmd->tx_size = tx_size;
2939
2940 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2941 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2942
Andy Grover6f3c0e62012-04-03 15:51:09 -07002943 ret = iscsit_send_tx_data(cmd, conn, 1);
2944 if (ret < 0) {
2945 iscsit_tx_thread_wait_for_tcp(conn);
2946 return ret;
2947 }
2948
2949 spin_lock_bh(&cmd->istate_lock);
2950 cmd->i_state = want_response ?
2951 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2952 spin_unlock_bh(&cmd->istate_lock);
2953
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002954 return 0;
2955}
2956
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002957static int
2958iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002959{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002960 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002961 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002962 u32 padding = 0;
2963 int niov = 0, tx_size;
2964
2965 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002966
2967 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002968 iov = &cmd->iov_misc[0];
2969 iov[niov].iov_base = cmd->pdu;
2970 iov[niov++].iov_len = ISCSI_HDR_LEN;
2971
2972 if (conn->conn_ops->HeaderDigest) {
2973 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2974
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002975 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2976 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002977
2978 iov[0].iov_len += ISCSI_CRC_LEN;
2979 tx_size += ISCSI_CRC_LEN;
2980 pr_debug("Attaching CRC32C HeaderDigest"
2981 " to NopIn 0x%08x\n", *header_digest);
2982 }
2983
2984 /*
2985 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2986 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2987 */
2988 if (cmd->buf_ptr_size) {
2989 iov[niov].iov_base = cmd->buf_ptr;
2990 iov[niov++].iov_len = cmd->buf_ptr_size;
2991 tx_size += cmd->buf_ptr_size;
2992
2993 pr_debug("Echoing back %u bytes of ping"
2994 " data.\n", cmd->buf_ptr_size);
2995
2996 padding = ((-cmd->buf_ptr_size) & 3);
2997 if (padding != 0) {
2998 iov[niov].iov_base = &cmd->pad_bytes;
2999 iov[niov++].iov_len = padding;
3000 tx_size += padding;
3001 pr_debug("Attaching %u additional"
3002 " padding bytes.\n", padding);
3003 }
3004 if (conn->conn_ops->DataDigest) {
3005 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3006 cmd->buf_ptr, cmd->buf_ptr_size,
3007 padding, (u8 *)&cmd->pad_bytes,
3008 (u8 *)&cmd->data_crc);
3009
3010 iov[niov].iov_base = &cmd->data_crc;
3011 iov[niov++].iov_len = ISCSI_CRC_LEN;
3012 tx_size += ISCSI_CRC_LEN;
3013 pr_debug("Attached DataDigest for %u"
3014 " bytes of ping data, CRC 0x%08x\n",
3015 cmd->buf_ptr_size, cmd->data_crc);
3016 }
3017 }
3018
3019 cmd->iov_misc_count = niov;
3020 cmd->tx_size = tx_size;
3021
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003022 return 0;
3023}
3024
Andy Grover6f3c0e62012-04-03 15:51:09 -07003025static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003026 struct iscsi_cmd *cmd,
3027 struct iscsi_conn *conn)
3028{
3029 int tx_size = 0;
3030 struct iscsi_r2t *r2t;
3031 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003032 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003033
3034 r2t = iscsit_get_r2t_from_list(cmd);
3035 if (!r2t)
3036 return -1;
3037
3038 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3039 memset(hdr, 0, ISCSI_HDR_LEN);
3040 hdr->opcode = ISCSI_OP_R2T;
3041 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3042 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3043 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003044 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003045 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003046 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3047 hdr->statsn = cpu_to_be32(conn->stat_sn);
3048 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3049 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3050 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3051 hdr->data_offset = cpu_to_be32(r2t->offset);
3052 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3053
3054 cmd->iov_misc[0].iov_base = cmd->pdu;
3055 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3056 tx_size += ISCSI_HDR_LEN;
3057
3058 if (conn->conn_ops->HeaderDigest) {
3059 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3060
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003061 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3062 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003063
3064 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3065 tx_size += ISCSI_CRC_LEN;
3066 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3067 " PDU 0x%08x\n", *header_digest);
3068 }
3069
3070 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3071 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3072 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3073 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3074 r2t->offset, r2t->xfer_len, conn->cid);
3075
3076 cmd->iov_misc_count = 1;
3077 cmd->tx_size = tx_size;
3078
3079 spin_lock_bh(&cmd->r2t_lock);
3080 r2t->sent_r2t = 1;
3081 spin_unlock_bh(&cmd->r2t_lock);
3082
Andy Grover6f3c0e62012-04-03 15:51:09 -07003083 ret = iscsit_send_tx_data(cmd, conn, 1);
3084 if (ret < 0) {
3085 iscsit_tx_thread_wait_for_tcp(conn);
3086 return ret;
3087 }
3088
3089 spin_lock_bh(&cmd->dataout_timeout_lock);
3090 iscsit_start_dataout_timer(cmd, conn);
3091 spin_unlock_bh(&cmd->dataout_timeout_lock);
3092
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003093 return 0;
3094}
3095
3096/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003097 * @recovery: If called from iscsi_task_reassign_complete_write() for
3098 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 */
3100int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003101 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003102 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003103 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003104{
3105 int first_r2t = 1;
3106 u32 offset = 0, xfer_len = 0;
3107
3108 spin_lock_bh(&cmd->r2t_lock);
3109 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3110 spin_unlock_bh(&cmd->r2t_lock);
3111 return 0;
3112 }
3113
Andy Grover8b1e1242012-04-03 15:51:12 -07003114 if (conn->sess->sess_ops->DataSequenceInOrder &&
3115 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003116 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003117
3118 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3119 if (conn->sess->sess_ops->DataSequenceInOrder) {
3120 offset = cmd->r2t_offset;
3121
Andy Grover8b1e1242012-04-03 15:51:12 -07003122 if (first_r2t && recovery) {
3123 int new_data_end = offset +
3124 conn->sess->sess_ops->MaxBurstLength -
3125 cmd->next_burst_len;
3126
Andy Groverebf1d952012-04-03 15:51:24 -07003127 if (new_data_end > cmd->se_cmd.data_length)
3128 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003129 else
3130 xfer_len =
3131 conn->sess->sess_ops->MaxBurstLength -
3132 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003133 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003134 int new_data_end = offset +
3135 conn->sess->sess_ops->MaxBurstLength;
3136
Andy Groverebf1d952012-04-03 15:51:24 -07003137 if (new_data_end > cmd->se_cmd.data_length)
3138 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003139 else
3140 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003141 }
3142 cmd->r2t_offset += xfer_len;
3143
Andy Groverebf1d952012-04-03 15:51:24 -07003144 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003145 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3146 } else {
3147 struct iscsi_seq *seq;
3148
3149 seq = iscsit_get_seq_holder_for_r2t(cmd);
3150 if (!seq) {
3151 spin_unlock_bh(&cmd->r2t_lock);
3152 return -1;
3153 }
3154
3155 offset = seq->offset;
3156 xfer_len = seq->xfer_len;
3157
3158 if (cmd->seq_send_order == cmd->seq_count)
3159 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3160 }
3161 cmd->outstanding_r2ts++;
3162 first_r2t = 0;
3163
3164 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3165 spin_unlock_bh(&cmd->r2t_lock);
3166 return -1;
3167 }
3168
3169 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3170 break;
3171 }
3172 spin_unlock_bh(&cmd->r2t_lock);
3173
3174 return 0;
3175}
3176
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003177void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3178 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003179{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003180 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003181 cmd->stat_sn = conn->stat_sn++;
3182
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003183 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003184
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185 memset(hdr, 0, ISCSI_HDR_LEN);
3186 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3187 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3188 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3189 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003190 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3192 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003193 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003194 }
3195 hdr->response = cmd->iscsi_response;
3196 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003197 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003198 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3199
3200 iscsit_increment_maxcmdsn(cmd, conn->sess);
3201 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3202 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3203
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003204 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3205 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3206 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3207 cmd->se_cmd.scsi_status, conn->cid);
3208}
3209EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3210
3211static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3212{
3213 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3214 struct kvec *iov;
3215 u32 padding = 0, tx_size = 0;
3216 int iov_count = 0;
3217 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3218
3219 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3220
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003221 iov = &cmd->iov_misc[0];
3222 iov[iov_count].iov_base = cmd->pdu;
3223 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3224 tx_size += ISCSI_HDR_LEN;
3225
3226 /*
3227 * Attach SENSE DATA payload to iSCSI Response PDU
3228 */
3229 if (cmd->se_cmd.sense_buffer &&
3230 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3231 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003232 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3233 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3234
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003235 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003236 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003237 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003238 iov[iov_count++].iov_len =
3239 (cmd->se_cmd.scsi_sense_length + padding);
3240 tx_size += cmd->se_cmd.scsi_sense_length;
3241
3242 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003243 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003244 cmd->se_cmd.scsi_sense_length, 0, padding);
3245 tx_size += padding;
3246 pr_debug("Adding %u bytes of padding to"
3247 " SENSE.\n", padding);
3248 }
3249
3250 if (conn->conn_ops->DataDigest) {
3251 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003252 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003253 (cmd->se_cmd.scsi_sense_length + padding),
3254 0, NULL, (u8 *)&cmd->data_crc);
3255
3256 iov[iov_count].iov_base = &cmd->data_crc;
3257 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3258 tx_size += ISCSI_CRC_LEN;
3259
3260 pr_debug("Attaching CRC32 DataDigest for"
3261 " SENSE, %u bytes CRC 0x%08x\n",
3262 (cmd->se_cmd.scsi_sense_length + padding),
3263 cmd->data_crc);
3264 }
3265
3266 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3267 " Response PDU\n",
3268 cmd->se_cmd.scsi_sense_length);
3269 }
3270
3271 if (conn->conn_ops->HeaderDigest) {
3272 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3273
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003274 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3275 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003276
3277 iov[0].iov_len += ISCSI_CRC_LEN;
3278 tx_size += ISCSI_CRC_LEN;
3279 pr_debug("Attaching CRC32 HeaderDigest for Response"
3280 " PDU 0x%08x\n", *header_digest);
3281 }
3282
3283 cmd->iov_misc_count = iov_count;
3284 cmd->tx_size = tx_size;
3285
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003286 return 0;
3287}
3288
3289static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3290{
3291 switch (se_tmr->response) {
3292 case TMR_FUNCTION_COMPLETE:
3293 return ISCSI_TMF_RSP_COMPLETE;
3294 case TMR_TASK_DOES_NOT_EXIST:
3295 return ISCSI_TMF_RSP_NO_TASK;
3296 case TMR_LUN_DOES_NOT_EXIST:
3297 return ISCSI_TMF_RSP_NO_LUN;
3298 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3299 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003300 case TMR_FUNCTION_REJECTED:
3301 default:
3302 return ISCSI_TMF_RSP_REJECTED;
3303 }
3304}
3305
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003306void
3307iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3308 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003309{
3310 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003311
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003312 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003313 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003314 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003315 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003316 cmd->stat_sn = conn->stat_sn++;
3317 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3318
3319 iscsit_increment_maxcmdsn(cmd, conn->sess);
3320 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3321 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3322
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003323 pr_debug("Built Task Management Response ITT: 0x%08x,"
3324 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3325 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3326}
3327EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3328
3329static int
3330iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3331{
3332 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3333 u32 tx_size = 0;
3334
3335 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3336
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003337 cmd->iov_misc[0].iov_base = cmd->pdu;
3338 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3339 tx_size += ISCSI_HDR_LEN;
3340
3341 if (conn->conn_ops->HeaderDigest) {
3342 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3343
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003344 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3345 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003346
3347 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3348 tx_size += ISCSI_CRC_LEN;
3349 pr_debug("Attaching CRC32 HeaderDigest for Task"
3350 " Mgmt Response PDU 0x%08x\n", *header_digest);
3351 }
3352
3353 cmd->iov_misc_count = 1;
3354 cmd->tx_size = tx_size;
3355
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003356 return 0;
3357}
3358
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003359static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3360{
3361 bool ret = false;
3362
3363 if (np->np_sockaddr.ss_family == AF_INET6) {
3364 const struct sockaddr_in6 sin6 = {
3365 .sin6_addr = IN6ADDR_ANY_INIT };
3366 struct sockaddr_in6 *sock_in6 =
3367 (struct sockaddr_in6 *)&np->np_sockaddr;
3368
3369 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3370 sin6.sin6_addr.s6_addr, 16))
3371 ret = true;
3372 } else {
3373 struct sockaddr_in * sock_in =
3374 (struct sockaddr_in *)&np->np_sockaddr;
3375
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003376 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003377 ret = true;
3378 }
3379
3380 return ret;
3381}
3382
Andy Grover8b1e1242012-04-03 15:51:12 -07003383#define SENDTARGETS_BUF_LIMIT 32768U
3384
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003385static int
3386iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003387 enum iscsit_transport_type network_transport,
3388 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003389{
3390 char *payload = NULL;
3391 struct iscsi_conn *conn = cmd->conn;
3392 struct iscsi_portal_group *tpg;
3393 struct iscsi_tiqn *tiqn;
3394 struct iscsi_tpg_np *tpg_np;
3395 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003396 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003397 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003398 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003399
Sagi Grimbergbe7dcfb62015-01-26 12:49:06 +02003400 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003401 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003402
3403 payload = kzalloc(buffer_len, GFP_KERNEL);
3404 if (!payload) {
3405 pr_err("Unable to allocate memory for sendtargets"
3406 " response.\n");
3407 return -ENOMEM;
3408 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003409 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003410 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003411 * explicit case..
3412 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003413 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003414 text_ptr = strchr(text_in, '=');
3415 if (!text_ptr) {
3416 pr_err("Unable to locate '=' string in text_in:"
3417 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003418 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003419 return -EINVAL;
3420 }
3421 /*
3422 * Skip over '=' character..
3423 */
3424 text_ptr += 1;
3425 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003426
3427 spin_lock(&tiqn_lock);
3428 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003429 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003430 strcmp(tiqn->tiqn, text_ptr)) {
3431 continue;
3432 }
3433
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003434 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003435
3436 spin_lock(&tiqn->tiqn_tpg_lock);
3437 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3438
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003439 /* If demo_mode_discovery=0 and generate_node_acls=0
3440 * (demo mode dislabed) do not return
3441 * TargetName+TargetAddress unless a NodeACL exists.
3442 */
3443
3444 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3445 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3446 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3447 cmd->conn->sess->sess_ops->InitiatorName))) {
3448 continue;
3449 }
3450
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003451 spin_lock(&tpg->tpg_state_lock);
3452 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3453 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3454 spin_unlock(&tpg->tpg_state_lock);
3455 continue;
3456 }
3457 spin_unlock(&tpg->tpg_state_lock);
3458
3459 spin_lock(&tpg->tpg_np_lock);
3460 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3461 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003462 struct iscsi_np *np = tpg_np->tpg_np;
3463 bool inaddr_any = iscsit_check_inaddr_any(np);
3464
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003465 if (np->np_network_transport != network_transport)
3466 continue;
3467
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003468 if (!target_name_printed) {
3469 len = sprintf(buf, "TargetName=%s",
3470 tiqn->tiqn);
3471 len += 1;
3472
3473 if ((len + payload_len) > buffer_len) {
3474 spin_unlock(&tpg->tpg_np_lock);
3475 spin_unlock(&tiqn->tiqn_tpg_lock);
3476 end_of_buf = 1;
3477 goto eob;
3478 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003479
3480 if (skip_bytes && len <= skip_bytes) {
3481 skip_bytes -= len;
3482 } else {
3483 memcpy(payload + payload_len, buf, len);
3484 payload_len += len;
3485 target_name_printed = 1;
3486 if (len > skip_bytes)
3487 skip_bytes = 0;
3488 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003489 }
3490
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003491 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003492 "%s:%hu,%hu",
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02003493 inaddr_any ? conn->local_ip : np->np_ip,
Steven Allenf2774f42014-10-15 10:59:21 -07003494 np->np_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003495 tpg->tpgt);
3496 len += 1;
3497
3498 if ((len + payload_len) > buffer_len) {
3499 spin_unlock(&tpg->tpg_np_lock);
3500 spin_unlock(&tiqn->tiqn_tpg_lock);
3501 end_of_buf = 1;
3502 goto eob;
3503 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003504
3505 if (skip_bytes && len <= skip_bytes) {
3506 skip_bytes -= len;
3507 } else {
3508 memcpy(payload + payload_len, buf, len);
3509 payload_len += len;
3510 if (len > skip_bytes)
3511 skip_bytes = 0;
3512 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003513 }
3514 spin_unlock(&tpg->tpg_np_lock);
3515 }
3516 spin_unlock(&tiqn->tiqn_tpg_lock);
3517eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003518 if (end_of_buf) {
3519 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003520 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003521 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003522
Andy Grover8060b8d2015-01-09 15:13:08 -08003523 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003524 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003525 }
3526 spin_unlock(&tiqn_lock);
3527
3528 cmd->buf_ptr = payload;
3529
3530 return payload_len;
3531}
3532
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003533int
3534iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003535 struct iscsi_text_rsp *hdr,
3536 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003537{
3538 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003539 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003540
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003541 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3542 cmd->read_data_done,
3543 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003544 if (text_length < 0)
3545 return text_length;
3546
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003547 if (completed) {
3548 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3549 } else {
3550 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3551 cmd->read_data_done += text_length;
3552 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3553 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3554 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003555 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003556 padding = ((-text_length) & 3);
3557 hton24(hdr->dlength, text_length);
3558 hdr->itt = cmd->init_task_tag;
3559 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3560 cmd->stat_sn = conn->stat_sn++;
3561 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3562
3563 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003564 /*
3565 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3566 * correctly increment MaxCmdSN for each response answering a
3567 * non immediate text request with a valid CmdSN.
3568 */
3569 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003570 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3571 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3572
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003573 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3574 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3575 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3576 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3577 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003578
3579 return text_length + padding;
3580}
3581EXPORT_SYMBOL(iscsit_build_text_rsp);
3582
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583static int iscsit_send_text_rsp(
3584 struct iscsi_cmd *cmd,
3585 struct iscsi_conn *conn)
3586{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003587 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003588 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003589 u32 tx_size = 0;
3590 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003591
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003592 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003593 if (rc < 0)
3594 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003595
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003596 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003597 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003598 iov[iov_count].iov_base = cmd->pdu;
3599 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3600 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003601 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003602
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003603 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003604
3605 if (conn->conn_ops->HeaderDigest) {
3606 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3607
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003608 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3609 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003610
3611 iov[0].iov_len += ISCSI_CRC_LEN;
3612 tx_size += ISCSI_CRC_LEN;
3613 pr_debug("Attaching CRC32 HeaderDigest for"
3614 " Text Response PDU 0x%08x\n", *header_digest);
3615 }
3616
3617 if (conn->conn_ops->DataDigest) {
3618 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003619 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003620 0, NULL, (u8 *)&cmd->data_crc);
3621
3622 iov[iov_count].iov_base = &cmd->data_crc;
3623 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3624 tx_size += ISCSI_CRC_LEN;
3625
3626 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003627 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003628 cmd->data_crc);
3629 }
3630
3631 cmd->iov_misc_count = iov_count;
3632 cmd->tx_size = tx_size;
3633
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003634 return 0;
3635}
3636
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003637void
3638iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3639 struct iscsi_reject *hdr)
3640{
3641 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003642 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003643 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3644 hton24(hdr->dlength, ISCSI_HDR_LEN);
3645 hdr->ffffffff = cpu_to_be32(0xffffffff);
3646 cmd->stat_sn = conn->stat_sn++;
3647 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3648 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3649 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3650
3651}
3652EXPORT_SYMBOL(iscsit_build_reject);
3653
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003654static int iscsit_send_reject(
3655 struct iscsi_cmd *cmd,
3656 struct iscsi_conn *conn)
3657{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003658 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003659 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003660 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003661
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003662 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003663
3664 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003665 iov[iov_count].iov_base = cmd->pdu;
3666 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3667 iov[iov_count].iov_base = cmd->buf_ptr;
3668 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3669
3670 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3671
3672 if (conn->conn_ops->HeaderDigest) {
3673 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3674
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003675 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3676 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003677
3678 iov[0].iov_len += ISCSI_CRC_LEN;
3679 tx_size += ISCSI_CRC_LEN;
3680 pr_debug("Attaching CRC32 HeaderDigest for"
3681 " REJECT PDU 0x%08x\n", *header_digest);
3682 }
3683
3684 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003685 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3686 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003687
3688 iov[iov_count].iov_base = &cmd->data_crc;
3689 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3690 tx_size += ISCSI_CRC_LEN;
3691 pr_debug("Attaching CRC32 DataDigest for REJECT"
3692 " PDU 0x%08x\n", cmd->data_crc);
3693 }
3694
3695 cmd->iov_misc_count = iov_count;
3696 cmd->tx_size = tx_size;
3697
3698 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3699 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3700
3701 return 0;
3702}
3703
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003704void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3705{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003706 int ord, cpu;
3707 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003708 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3709 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003710 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003711 * Here we use bitmap_id to determine which CPU that this
3712 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003713 * execute upon.
3714 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003715 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003716 for_each_online_cpu(cpu) {
3717 if (ord-- == 0) {
3718 cpumask_set_cpu(cpu, conn->conn_cpumask);
3719 return;
3720 }
3721 }
3722 /*
3723 * This should never be reached..
3724 */
3725 dump_stack();
3726 cpumask_setall(conn->conn_cpumask);
3727}
3728
3729static inline void iscsit_thread_check_cpumask(
3730 struct iscsi_conn *conn,
3731 struct task_struct *p,
3732 int mode)
3733{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003734 /*
3735 * mode == 1 signals iscsi_target_tx_thread() usage.
3736 * mode == 0 signals iscsi_target_rx_thread() usage.
3737 */
3738 if (mode == 1) {
3739 if (!conn->conn_tx_reset_cpumask)
3740 return;
3741 conn->conn_tx_reset_cpumask = 0;
3742 } else {
3743 if (!conn->conn_rx_reset_cpumask)
3744 return;
3745 conn->conn_rx_reset_cpumask = 0;
3746 }
3747 /*
3748 * Update the CPU mask for this single kthread so that
3749 * both TX and RX kthreads are scheduled to run on the
3750 * same CPU.
3751 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003752 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3753}
3754
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003755static int
3756iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003757{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003758 int ret;
3759
3760 switch (state) {
3761 case ISTATE_SEND_R2T:
3762 ret = iscsit_send_r2t(cmd, conn);
3763 if (ret < 0)
3764 goto err;
3765 break;
3766 case ISTATE_REMOVE:
3767 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003768 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003769 spin_unlock_bh(&conn->cmd_lock);
3770
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003771 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003772 break;
3773 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3774 iscsit_mod_nopin_response_timer(conn);
3775 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3776 if (ret < 0)
3777 goto err;
3778 break;
3779 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3780 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3781 if (ret < 0)
3782 goto err;
3783 break;
3784 default:
3785 pr_err("Unknown Opcode: 0x%02x ITT:"
3786 " 0x%08x, i_state: %d on CID: %hu\n",
3787 cmd->iscsi_opcode, cmd->init_task_tag, state,
3788 conn->cid);
3789 goto err;
3790 }
3791
3792 return 0;
3793
3794err:
3795 return -1;
3796}
3797
3798static int
3799iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3800{
3801 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003802 struct iscsi_queue_req *qr;
3803 struct iscsi_cmd *cmd;
3804 u8 state;
3805 int ret;
3806
3807 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3808 atomic_set(&conn->check_immediate_queue, 0);
3809 cmd = qr->cmd;
3810 state = qr->state;
3811 kmem_cache_free(lio_qr_cache, qr);
3812
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003813 ret = t->iscsit_immediate_queue(conn, cmd, state);
3814 if (ret < 0)
3815 return ret;
3816 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003817
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003818 return 0;
3819}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003820
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003821static int
3822iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3823{
3824 int ret;
3825
3826check_rsp_state:
3827 switch (state) {
3828 case ISTATE_SEND_DATAIN:
3829 ret = iscsit_send_datain(cmd, conn);
3830 if (ret < 0)
3831 goto err;
3832 else if (!ret)
3833 /* more drs */
3834 goto check_rsp_state;
3835 else if (ret == 1) {
3836 /* all done */
3837 spin_lock_bh(&cmd->istate_lock);
3838 cmd->i_state = ISTATE_SENT_STATUS;
3839 spin_unlock_bh(&cmd->istate_lock);
3840
3841 if (atomic_read(&conn->check_immediate_queue))
3842 return 1;
3843
3844 return 0;
3845 } else if (ret == 2) {
3846 /* Still must send status,
3847 SCF_TRANSPORT_TASK_SENSE was set */
3848 spin_lock_bh(&cmd->istate_lock);
3849 cmd->i_state = ISTATE_SEND_STATUS;
3850 spin_unlock_bh(&cmd->istate_lock);
3851 state = ISTATE_SEND_STATUS;
3852 goto check_rsp_state;
3853 }
3854
3855 break;
3856 case ISTATE_SEND_STATUS:
3857 case ISTATE_SEND_STATUS_RECOVERY:
3858 ret = iscsit_send_response(cmd, conn);
3859 break;
3860 case ISTATE_SEND_LOGOUTRSP:
3861 ret = iscsit_send_logout(cmd, conn);
3862 break;
3863 case ISTATE_SEND_ASYNCMSG:
3864 ret = iscsit_send_conn_drop_async_message(
3865 cmd, conn);
3866 break;
3867 case ISTATE_SEND_NOPIN:
3868 ret = iscsit_send_nopin(cmd, conn);
3869 break;
3870 case ISTATE_SEND_REJECT:
3871 ret = iscsit_send_reject(cmd, conn);
3872 break;
3873 case ISTATE_SEND_TASKMGTRSP:
3874 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3875 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003876 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003877 ret = iscsit_tmr_post_handler(cmd, conn);
3878 if (ret != 0)
3879 iscsit_fall_back_to_erl0(conn->sess);
3880 break;
3881 case ISTATE_SEND_TEXTRSP:
3882 ret = iscsit_send_text_rsp(cmd, conn);
3883 break;
3884 default:
3885 pr_err("Unknown Opcode: 0x%02x ITT:"
3886 " 0x%08x, i_state: %d on CID: %hu\n",
3887 cmd->iscsi_opcode, cmd->init_task_tag,
3888 state, conn->cid);
3889 goto err;
3890 }
3891 if (ret < 0)
3892 goto err;
3893
3894 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3895 iscsit_tx_thread_wait_for_tcp(conn);
3896 iscsit_unmap_iovec(cmd);
3897 goto err;
3898 }
3899 iscsit_unmap_iovec(cmd);
3900
3901 switch (state) {
3902 case ISTATE_SEND_LOGOUTRSP:
3903 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003904 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003905 /* fall through */
3906 case ISTATE_SEND_STATUS:
3907 case ISTATE_SEND_ASYNCMSG:
3908 case ISTATE_SEND_NOPIN:
3909 case ISTATE_SEND_STATUS_RECOVERY:
3910 case ISTATE_SEND_TEXTRSP:
3911 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003912 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003913 spin_lock_bh(&cmd->istate_lock);
3914 cmd->i_state = ISTATE_SENT_STATUS;
3915 spin_unlock_bh(&cmd->istate_lock);
3916 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003917 default:
3918 pr_err("Unknown Opcode: 0x%02x ITT:"
3919 " 0x%08x, i_state: %d on CID: %hu\n",
3920 cmd->iscsi_opcode, cmd->init_task_tag,
3921 cmd->i_state, conn->cid);
3922 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003923 }
3924
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003925 if (atomic_read(&conn->check_immediate_queue))
3926 return 1;
3927
Andy Grover6f3c0e62012-04-03 15:51:09 -07003928 return 0;
3929
3930err:
3931 return -1;
3932}
3933
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003934static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003935{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003936 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003937 struct iscsi_queue_req *qr;
3938 struct iscsi_cmd *cmd;
3939 u8 state;
3940 int ret;
3941
3942 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3943 cmd = qr->cmd;
3944 state = qr->state;
3945 kmem_cache_free(lio_qr_cache, qr);
3946
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003947 ret = t->iscsit_response_queue(conn, cmd, state);
3948 if (ret == 1 || ret < 0)
3949 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003950 }
3951
3952 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003953}
3954
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003955int iscsi_target_tx_thread(void *arg)
3956{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003957 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003958 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003959 /*
3960 * Allow ourselves to be interrupted by SIGINT so that a
3961 * connection recovery / failure event can be triggered externally.
3962 */
3963 allow_signal(SIGINT);
3964
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003965 while (!kthread_should_stop()) {
3966 /*
3967 * Ensure that both TX and RX per connection kthreads
3968 * are scheduled to run on the same CPU.
3969 */
3970 iscsit_thread_check_cpumask(conn, current, 1);
3971
Roland Dreierd5627ac2012-10-31 09:16:46 -07003972 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003973 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003974
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003975 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003976 goto transport_err;
3977
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003978get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003979 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003980 if (ret < 0)
3981 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003982
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003983 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003984 if (ret == 1)
3985 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003986 else if (ret == -ECONNRESET)
3987 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003988 else if (ret < 0)
3989 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003990 }
3991
3992transport_err:
3993 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003994out:
3995 return 0;
3996}
3997
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003998static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3999{
4000 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4001 struct iscsi_cmd *cmd;
4002 int ret = 0;
4003
4004 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4005 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004006 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004007 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004008 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004009
4010 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4011 break;
4012 case ISCSI_OP_SCSI_DATA_OUT:
4013 ret = iscsit_handle_data_out(conn, buf);
4014 break;
4015 case ISCSI_OP_NOOP_OUT:
4016 cmd = NULL;
4017 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004018 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004019 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004020 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004021 }
4022 ret = iscsit_handle_nop_out(conn, cmd, buf);
4023 break;
4024 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004025 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004026 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004027 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004028
4029 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4030 break;
4031 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02004032 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4033 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4034 if (!cmd)
4035 goto reject;
4036 } else {
4037 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4038 if (!cmd)
4039 goto reject;
4040 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004041
4042 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004043 break;
4044 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004045 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004046 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004047 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004048
4049 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4050 if (ret > 0)
4051 wait_for_completion_timeout(&conn->conn_logout_comp,
4052 SECONDS_FOR_LOGOUT_COMP * HZ);
4053 break;
4054 case ISCSI_OP_SNACK:
4055 ret = iscsit_handle_snack(conn, buf);
4056 break;
4057 default:
4058 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4059 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4060 pr_err("Cannot recover from unknown"
4061 " opcode while ERL=0, closing iSCSI connection.\n");
4062 return -1;
4063 }
4064 if (!conn->conn_ops->OFMarker) {
4065 pr_err("Unable to recover from unknown"
4066 " opcode while OFMarker=No, closing iSCSI"
4067 " connection.\n");
4068 return -1;
4069 }
4070 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4071 pr_err("Unable to recover from unknown"
4072 " opcode, closing iSCSI connection.\n");
4073 return -1;
4074 }
4075 break;
4076 }
4077
4078 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004079reject:
4080 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004081}
4082
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004083int iscsi_target_rx_thread(void *arg)
4084{
4085 int ret;
4086 u8 buffer[ISCSI_HDR_LEN], opcode;
4087 u32 checksum = 0, digest = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004088 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004089 struct kvec iov;
4090 /*
4091 * Allow ourselves to be interrupted by SIGINT so that a
4092 * connection recovery / failure event can be triggered externally.
4093 */
4094 allow_signal(SIGINT);
4095
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004096 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4097 struct completion comp;
4098 int rc;
4099
4100 init_completion(&comp);
4101 rc = wait_for_completion_interruptible(&comp);
4102 if (rc < 0)
4103 goto transport_err;
4104
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004105 goto transport_err;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004106 }
4107
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004108 while (!kthread_should_stop()) {
4109 /*
4110 * Ensure that both TX and RX per connection kthreads
4111 * are scheduled to run on the same CPU.
4112 */
4113 iscsit_thread_check_cpumask(conn, current, 0);
4114
4115 memset(buffer, 0, ISCSI_HDR_LEN);
4116 memset(&iov, 0, sizeof(struct kvec));
4117
4118 iov.iov_base = buffer;
4119 iov.iov_len = ISCSI_HDR_LEN;
4120
4121 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4122 if (ret != ISCSI_HDR_LEN) {
4123 iscsit_rx_thread_wait_for_tcp(conn);
4124 goto transport_err;
4125 }
4126
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004127 if (conn->conn_ops->HeaderDigest) {
4128 iov.iov_base = &digest;
4129 iov.iov_len = ISCSI_CRC_LEN;
4130
4131 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4132 if (ret != ISCSI_CRC_LEN) {
4133 iscsit_rx_thread_wait_for_tcp(conn);
4134 goto transport_err;
4135 }
4136
4137 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4138 buffer, ISCSI_HDR_LEN,
4139 0, NULL, (u8 *)&checksum);
4140
4141 if (digest != checksum) {
4142 pr_err("HeaderDigest CRC32C failed,"
4143 " received 0x%08x, computed 0x%08x\n",
4144 digest, checksum);
4145 /*
4146 * Set the PDU to 0xff so it will intentionally
4147 * hit default in the switch below.
4148 */
4149 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004150 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004151 } else {
4152 pr_debug("Got HeaderDigest CRC32C"
4153 " 0x%08x\n", checksum);
4154 }
4155 }
4156
4157 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4158 goto transport_err;
4159
4160 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4161
4162 if (conn->sess->sess_ops->SessionType &&
4163 ((!(opcode & ISCSI_OP_TEXT)) ||
4164 (!(opcode & ISCSI_OP_LOGOUT)))) {
4165 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4166 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004167 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4168 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004169 goto transport_err;
4170 }
4171
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004172 ret = iscsi_target_rx_opcode(conn, buffer);
4173 if (ret < 0)
4174 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004175 }
4176
4177transport_err:
4178 if (!signal_pending(current))
4179 atomic_set(&conn->transport_failed, 1);
4180 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004181 return 0;
4182}
4183
4184static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4185{
4186 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4187 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004188 /*
4189 * We expect this function to only ever be called from either RX or TX
4190 * thread context via iscsit_close_connection() once the other context
4191 * has been reset -> returned sleeping pre-handler state.
4192 */
4193 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004194 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004195
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004196 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004197 spin_unlock_bh(&conn->cmd_lock);
4198
4199 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004200
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004201 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004202
4203 spin_lock_bh(&conn->cmd_lock);
4204 }
4205 spin_unlock_bh(&conn->cmd_lock);
4206}
4207
4208static void iscsit_stop_timers_for_cmds(
4209 struct iscsi_conn *conn)
4210{
4211 struct iscsi_cmd *cmd;
4212
4213 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004214 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004215 if (cmd->data_direction == DMA_TO_DEVICE)
4216 iscsit_stop_dataout_timer(cmd);
4217 }
4218 spin_unlock_bh(&conn->cmd_lock);
4219}
4220
4221int iscsit_close_connection(
4222 struct iscsi_conn *conn)
4223{
4224 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4225 struct iscsi_session *sess = conn->sess;
4226
4227 pr_debug("Closing iSCSI connection CID %hu on SID:"
4228 " %u\n", conn->cid, sess->sid);
4229 /*
4230 * Always up conn_logout_comp just in case the RX Thread is sleeping
4231 * and the logout response never got sent because the connection
4232 * failed.
4233 */
4234 complete(&conn->conn_logout_comp);
4235
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004236 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4237 if (conn->tx_thread &&
4238 cmpxchg(&conn->tx_thread_active, true, false)) {
4239 send_sig(SIGINT, conn->tx_thread, 1);
4240 kthread_stop(conn->tx_thread);
4241 }
4242 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4243 if (conn->rx_thread &&
4244 cmpxchg(&conn->rx_thread_active, true, false)) {
4245 send_sig(SIGINT, conn->rx_thread, 1);
4246 kthread_stop(conn->rx_thread);
4247 }
4248 }
4249
4250 spin_lock(&iscsit_global->ts_bitmap_lock);
4251 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4252 get_order(1));
4253 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004254
4255 iscsit_stop_timers_for_cmds(conn);
4256 iscsit_stop_nopin_response_timer(conn);
4257 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004258
4259 if (conn->conn_transport->iscsit_wait_conn)
4260 conn->conn_transport->iscsit_wait_conn(conn);
4261
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004262 /*
4263 * During Connection recovery drop unacknowledged out of order
4264 * commands for this connection, and prepare the other commands
4265 * for realligence.
4266 *
4267 * During normal operation clear the out of order commands (but
4268 * do not free the struct iscsi_ooo_cmdsn's) and release all
4269 * struct iscsi_cmds.
4270 */
4271 if (atomic_read(&conn->connection_recovery)) {
4272 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4273 iscsit_prepare_cmds_for_realligance(conn);
4274 } else {
4275 iscsit_clear_ooo_cmdsns_for_conn(conn);
4276 iscsit_release_commands_from_conn(conn);
4277 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004278 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004279
4280 /*
4281 * Handle decrementing session or connection usage count if
4282 * a logout response was not able to be sent because the
4283 * connection failed. Fall back to Session Recovery here.
4284 */
4285 if (atomic_read(&conn->conn_logout_remove)) {
4286 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4287 iscsit_dec_conn_usage_count(conn);
4288 iscsit_dec_session_usage_count(sess);
4289 }
4290 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4291 iscsit_dec_conn_usage_count(conn);
4292
4293 atomic_set(&conn->conn_logout_remove, 0);
4294 atomic_set(&sess->session_reinstatement, 0);
4295 atomic_set(&sess->session_fall_back_to_erl0, 1);
4296 }
4297
4298 spin_lock_bh(&sess->conn_lock);
4299 list_del(&conn->conn_list);
4300
4301 /*
4302 * Attempt to let the Initiator know this connection failed by
4303 * sending an Connection Dropped Async Message on another
4304 * active connection.
4305 */
4306 if (atomic_read(&conn->connection_recovery))
4307 iscsit_build_conn_drop_async_message(conn);
4308
4309 spin_unlock_bh(&sess->conn_lock);
4310
4311 /*
4312 * If connection reinstatement is being performed on this connection,
4313 * up the connection reinstatement semaphore that is being blocked on
4314 * in iscsit_cause_connection_reinstatement().
4315 */
4316 spin_lock_bh(&conn->state_lock);
4317 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4318 spin_unlock_bh(&conn->state_lock);
4319 complete(&conn->conn_wait_comp);
4320 wait_for_completion(&conn->conn_post_wait_comp);
4321 spin_lock_bh(&conn->state_lock);
4322 }
4323
4324 /*
4325 * If connection reinstatement is being performed on this connection
4326 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4327 * connection wait rcfr semaphore that is being blocked on
4328 * an iscsit_connection_reinstatement_rcfr().
4329 */
4330 if (atomic_read(&conn->connection_wait_rcfr)) {
4331 spin_unlock_bh(&conn->state_lock);
4332 complete(&conn->conn_wait_rcfr_comp);
4333 wait_for_completion(&conn->conn_post_wait_comp);
4334 spin_lock_bh(&conn->state_lock);
4335 }
4336 atomic_set(&conn->connection_reinstatement, 1);
4337 spin_unlock_bh(&conn->state_lock);
4338
4339 /*
4340 * If any other processes are accessing this connection pointer we
4341 * must wait until they have completed.
4342 */
4343 iscsit_check_conn_usage_count(conn);
4344
4345 if (conn->conn_rx_hash.tfm)
4346 crypto_free_hash(conn->conn_rx_hash.tfm);
4347 if (conn->conn_tx_hash.tfm)
4348 crypto_free_hash(conn->conn_tx_hash.tfm);
4349
Joern Engelfbecb652014-09-02 17:49:47 -04004350 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004351
4352 kfree(conn->conn_ops);
4353 conn->conn_ops = NULL;
4354
Al Virobf6932f2012-07-21 08:55:18 +01004355 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004356 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004357
4358 if (conn->conn_transport->iscsit_free_conn)
4359 conn->conn_transport->iscsit_free_conn(conn);
4360
4361 iscsit_put_transport(conn->conn_transport);
4362
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004363 conn->thread_set = NULL;
4364
4365 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4366 conn->conn_state = TARG_CONN_STATE_FREE;
4367 kfree(conn);
4368
4369 spin_lock_bh(&sess->conn_lock);
4370 atomic_dec(&sess->nconn);
4371 pr_debug("Decremented iSCSI connection count to %hu from node:"
4372 " %s\n", atomic_read(&sess->nconn),
4373 sess->sess_ops->InitiatorName);
4374 /*
4375 * Make sure that if one connection fails in an non ERL=2 iSCSI
4376 * Session that they all fail.
4377 */
4378 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4379 !atomic_read(&sess->session_logout))
4380 atomic_set(&sess->session_fall_back_to_erl0, 1);
4381
4382 /*
4383 * If this was not the last connection in the session, and we are
4384 * performing session reinstatement or falling back to ERL=0, call
4385 * iscsit_stop_session() without sleeping to shutdown the other
4386 * active connections.
4387 */
4388 if (atomic_read(&sess->nconn)) {
4389 if (!atomic_read(&sess->session_reinstatement) &&
4390 !atomic_read(&sess->session_fall_back_to_erl0)) {
4391 spin_unlock_bh(&sess->conn_lock);
4392 return 0;
4393 }
4394 if (!atomic_read(&sess->session_stop_active)) {
4395 atomic_set(&sess->session_stop_active, 1);
4396 spin_unlock_bh(&sess->conn_lock);
4397 iscsit_stop_session(sess, 0, 0);
4398 return 0;
4399 }
4400 spin_unlock_bh(&sess->conn_lock);
4401 return 0;
4402 }
4403
4404 /*
4405 * If this was the last connection in the session and one of the
4406 * following is occurring:
4407 *
4408 * Session Reinstatement is not being performed, and are falling back
4409 * to ERL=0 call iscsit_close_session().
4410 *
4411 * Session Logout was requested. iscsit_close_session() will be called
4412 * elsewhere.
4413 *
4414 * Session Continuation is not being performed, start the Time2Retain
4415 * handler and check if sleep_on_sess_wait_sem is active.
4416 */
4417 if (!atomic_read(&sess->session_reinstatement) &&
4418 atomic_read(&sess->session_fall_back_to_erl0)) {
4419 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004420 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004421
4422 return 0;
4423 } else if (atomic_read(&sess->session_logout)) {
4424 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4425 sess->session_state = TARG_SESS_STATE_FREE;
4426 spin_unlock_bh(&sess->conn_lock);
4427
4428 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4429 complete(&sess->session_wait_comp);
4430
4431 return 0;
4432 } else {
4433 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4434 sess->session_state = TARG_SESS_STATE_FAILED;
4435
4436 if (!atomic_read(&sess->session_continuation)) {
4437 spin_unlock_bh(&sess->conn_lock);
4438 iscsit_start_time2retain_handler(sess);
4439 } else
4440 spin_unlock_bh(&sess->conn_lock);
4441
4442 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4443 complete(&sess->session_wait_comp);
4444
4445 return 0;
4446 }
4447 spin_unlock_bh(&sess->conn_lock);
4448
4449 return 0;
4450}
4451
4452int iscsit_close_session(struct iscsi_session *sess)
4453{
Andy Grover60bfcf82013-10-09 11:05:58 -07004454 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004455 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4456
4457 if (atomic_read(&sess->nconn)) {
4458 pr_err("%d connection(s) still exist for iSCSI session"
4459 " to %s\n", atomic_read(&sess->nconn),
4460 sess->sess_ops->InitiatorName);
4461 BUG();
4462 }
4463
4464 spin_lock_bh(&se_tpg->session_lock);
4465 atomic_set(&sess->session_logout, 1);
4466 atomic_set(&sess->session_reinstatement, 1);
4467 iscsit_stop_time2retain_timer(sess);
4468 spin_unlock_bh(&se_tpg->session_lock);
4469
4470 /*
4471 * transport_deregister_session_configfs() will clear the
4472 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4473 * can be setting it again with __transport_register_session() in
4474 * iscsi_post_login_handler() again after the iscsit_stop_session()
4475 * completes in iscsi_np context.
4476 */
4477 transport_deregister_session_configfs(sess->se_sess);
4478
4479 /*
4480 * If any other processes are accessing this session pointer we must
4481 * wait until they have completed. If we are in an interrupt (the
4482 * time2retain handler) and contain and active session usage count we
4483 * restart the timer and exit.
4484 */
4485 if (!in_interrupt()) {
4486 if (iscsit_check_session_usage_count(sess) == 1)
4487 iscsit_stop_session(sess, 1, 1);
4488 } else {
4489 if (iscsit_check_session_usage_count(sess) == 2) {
4490 atomic_set(&sess->session_logout, 0);
4491 iscsit_start_time2retain_handler(sess);
4492 return 0;
4493 }
4494 }
4495
4496 transport_deregister_session(sess->se_sess);
4497
4498 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4499 iscsit_free_connection_recovery_entires(sess);
4500
4501 iscsit_free_all_ooo_cmdsns(sess);
4502
4503 spin_lock_bh(&se_tpg->session_lock);
4504 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4505 sess->session_state = TARG_SESS_STATE_FREE;
4506 pr_debug("Released iSCSI session from node: %s\n",
4507 sess->sess_ops->InitiatorName);
4508 tpg->nsessions--;
4509 if (tpg->tpg_tiqn)
4510 tpg->tpg_tiqn->tiqn_nsessions--;
4511
4512 pr_debug("Decremented number of active iSCSI Sessions on"
4513 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4514
4515 spin_lock(&sess_idr_lock);
4516 idr_remove(&sess_idr, sess->session_index);
4517 spin_unlock(&sess_idr_lock);
4518
4519 kfree(sess->sess_ops);
4520 sess->sess_ops = NULL;
4521 spin_unlock_bh(&se_tpg->session_lock);
4522
4523 kfree(sess);
4524 return 0;
4525}
4526
4527static void iscsit_logout_post_handler_closesession(
4528 struct iscsi_conn *conn)
4529{
4530 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004531 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004532
4533 atomic_set(&conn->conn_logout_remove, 0);
4534 complete(&conn->conn_logout_comp);
4535
4536 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004537 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004538 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004539 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004540}
4541
4542static void iscsit_logout_post_handler_samecid(
4543 struct iscsi_conn *conn)
4544{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004545 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004546
4547 atomic_set(&conn->conn_logout_remove, 0);
4548 complete(&conn->conn_logout_comp);
4549
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004550 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004551 iscsit_dec_conn_usage_count(conn);
4552}
4553
4554static void iscsit_logout_post_handler_diffcid(
4555 struct iscsi_conn *conn,
4556 u16 cid)
4557{
4558 struct iscsi_conn *l_conn;
4559 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004560 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004561
4562 if (!sess)
4563 return;
4564
4565 spin_lock_bh(&sess->conn_lock);
4566 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4567 if (l_conn->cid == cid) {
4568 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004569 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004570 break;
4571 }
4572 }
4573 spin_unlock_bh(&sess->conn_lock);
4574
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004575 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004576 return;
4577
4578 if (l_conn->sock)
4579 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4580
4581 spin_lock_bh(&l_conn->state_lock);
4582 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4583 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4584 spin_unlock_bh(&l_conn->state_lock);
4585
4586 iscsit_cause_connection_reinstatement(l_conn, 1);
4587 iscsit_dec_conn_usage_count(l_conn);
4588}
4589
4590/*
4591 * Return of 0 causes the TX thread to restart.
4592 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004593int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004594 struct iscsi_cmd *cmd,
4595 struct iscsi_conn *conn)
4596{
4597 int ret = 0;
4598
4599 switch (cmd->logout_reason) {
4600 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4601 switch (cmd->logout_response) {
4602 case ISCSI_LOGOUT_SUCCESS:
4603 case ISCSI_LOGOUT_CLEANUP_FAILED:
4604 default:
4605 iscsit_logout_post_handler_closesession(conn);
4606 break;
4607 }
4608 ret = 0;
4609 break;
4610 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4611 if (conn->cid == cmd->logout_cid) {
4612 switch (cmd->logout_response) {
4613 case ISCSI_LOGOUT_SUCCESS:
4614 case ISCSI_LOGOUT_CLEANUP_FAILED:
4615 default:
4616 iscsit_logout_post_handler_samecid(conn);
4617 break;
4618 }
4619 ret = 0;
4620 } else {
4621 switch (cmd->logout_response) {
4622 case ISCSI_LOGOUT_SUCCESS:
4623 iscsit_logout_post_handler_diffcid(conn,
4624 cmd->logout_cid);
4625 break;
4626 case ISCSI_LOGOUT_CID_NOT_FOUND:
4627 case ISCSI_LOGOUT_CLEANUP_FAILED:
4628 default:
4629 break;
4630 }
4631 ret = 1;
4632 }
4633 break;
4634 case ISCSI_LOGOUT_REASON_RECOVERY:
4635 switch (cmd->logout_response) {
4636 case ISCSI_LOGOUT_SUCCESS:
4637 case ISCSI_LOGOUT_CID_NOT_FOUND:
4638 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4639 case ISCSI_LOGOUT_CLEANUP_FAILED:
4640 default:
4641 break;
4642 }
4643 ret = 1;
4644 break;
4645 default:
4646 break;
4647
4648 }
4649 return ret;
4650}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004651EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004652
4653void iscsit_fail_session(struct iscsi_session *sess)
4654{
4655 struct iscsi_conn *conn;
4656
4657 spin_lock_bh(&sess->conn_lock);
4658 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4659 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4660 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4661 }
4662 spin_unlock_bh(&sess->conn_lock);
4663
4664 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4665 sess->session_state = TARG_SESS_STATE_FAILED;
4666}
4667
4668int iscsit_free_session(struct iscsi_session *sess)
4669{
4670 u16 conn_count = atomic_read(&sess->nconn);
4671 struct iscsi_conn *conn, *conn_tmp = NULL;
4672 int is_last;
4673
4674 spin_lock_bh(&sess->conn_lock);
4675 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4676
4677 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4678 conn_list) {
4679 if (conn_count == 0)
4680 break;
4681
4682 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4683 is_last = 1;
4684 } else {
4685 iscsit_inc_conn_usage_count(conn_tmp);
4686 is_last = 0;
4687 }
4688 iscsit_inc_conn_usage_count(conn);
4689
4690 spin_unlock_bh(&sess->conn_lock);
4691 iscsit_cause_connection_reinstatement(conn, 1);
4692 spin_lock_bh(&sess->conn_lock);
4693
4694 iscsit_dec_conn_usage_count(conn);
4695 if (is_last == 0)
4696 iscsit_dec_conn_usage_count(conn_tmp);
4697
4698 conn_count--;
4699 }
4700
4701 if (atomic_read(&sess->nconn)) {
4702 spin_unlock_bh(&sess->conn_lock);
4703 wait_for_completion(&sess->session_wait_comp);
4704 } else
4705 spin_unlock_bh(&sess->conn_lock);
4706
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004707 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004708 return 0;
4709}
4710
4711void iscsit_stop_session(
4712 struct iscsi_session *sess,
4713 int session_sleep,
4714 int connection_sleep)
4715{
4716 u16 conn_count = atomic_read(&sess->nconn);
4717 struct iscsi_conn *conn, *conn_tmp = NULL;
4718 int is_last;
4719
4720 spin_lock_bh(&sess->conn_lock);
4721 if (session_sleep)
4722 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4723
4724 if (connection_sleep) {
4725 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4726 conn_list) {
4727 if (conn_count == 0)
4728 break;
4729
4730 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4731 is_last = 1;
4732 } else {
4733 iscsit_inc_conn_usage_count(conn_tmp);
4734 is_last = 0;
4735 }
4736 iscsit_inc_conn_usage_count(conn);
4737
4738 spin_unlock_bh(&sess->conn_lock);
4739 iscsit_cause_connection_reinstatement(conn, 1);
4740 spin_lock_bh(&sess->conn_lock);
4741
4742 iscsit_dec_conn_usage_count(conn);
4743 if (is_last == 0)
4744 iscsit_dec_conn_usage_count(conn_tmp);
4745 conn_count--;
4746 }
4747 } else {
4748 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4749 iscsit_cause_connection_reinstatement(conn, 0);
4750 }
4751
4752 if (session_sleep && atomic_read(&sess->nconn)) {
4753 spin_unlock_bh(&sess->conn_lock);
4754 wait_for_completion(&sess->session_wait_comp);
4755 } else
4756 spin_unlock_bh(&sess->conn_lock);
4757}
4758
4759int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4760{
4761 struct iscsi_session *sess;
4762 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4763 struct se_session *se_sess, *se_sess_tmp;
4764 int session_count = 0;
4765
4766 spin_lock_bh(&se_tpg->session_lock);
4767 if (tpg->nsessions && !force) {
4768 spin_unlock_bh(&se_tpg->session_lock);
4769 return -1;
4770 }
4771
4772 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4773 sess_list) {
4774 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4775
4776 spin_lock(&sess->conn_lock);
4777 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4778 atomic_read(&sess->session_logout) ||
4779 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4780 spin_unlock(&sess->conn_lock);
4781 continue;
4782 }
4783 atomic_set(&sess->session_reinstatement, 1);
4784 spin_unlock(&sess->conn_lock);
4785 spin_unlock_bh(&se_tpg->session_lock);
4786
4787 iscsit_free_session(sess);
4788 spin_lock_bh(&se_tpg->session_lock);
4789
4790 session_count++;
4791 }
4792 spin_unlock_bh(&se_tpg->session_lock);
4793
4794 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4795 " Group: %hu\n", session_count, tpg->tpgt);
4796 return 0;
4797}
4798
4799MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4800MODULE_VERSION("4.1.x");
4801MODULE_AUTHOR("nab@Linux-iSCSI.org");
4802MODULE_LICENSE("GPL");
4803
4804module_init(iscsi_target_init_module);
4805module_exit(iscsi_target_cleanup_module);