blob: 47af86167b499fce0f56917c2bee48d7ebd2a104 [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{
540 int ret = 0;
541
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 }
549 mutex_init(&auth_id_lock);
550 spin_lock_init(&sess_idr_lock);
551 idr_init(&tiqn_idr);
552 idr_init(&sess_idr);
553
554 ret = iscsi_target_register_configfs();
555 if (ret < 0)
556 goto out;
557
558 ret = iscsi_thread_set_init();
559 if (ret < 0)
560 goto configfs_out;
561
562 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
563 TARGET_THREAD_SET_COUNT) {
564 pr_err("iscsi_allocate_thread_sets() returned"
565 " unexpected value!\n");
566 goto ts_out1;
567 }
568
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000569 lio_qr_cache = kmem_cache_create("lio_qr_cache",
570 sizeof(struct iscsi_queue_req),
571 __alignof__(struct iscsi_queue_req), 0, NULL);
572 if (!lio_qr_cache) {
573 pr_err("nable to kmem_cache_create() for"
574 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700575 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000576 }
577
578 lio_dr_cache = kmem_cache_create("lio_dr_cache",
579 sizeof(struct iscsi_datain_req),
580 __alignof__(struct iscsi_datain_req), 0, NULL);
581 if (!lio_dr_cache) {
582 pr_err("Unable to kmem_cache_create() for"
583 " lio_dr_cache\n");
584 goto qr_out;
585 }
586
587 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
588 sizeof(struct iscsi_ooo_cmdsn),
589 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
590 if (!lio_ooo_cache) {
591 pr_err("Unable to kmem_cache_create() for"
592 " lio_ooo_cache\n");
593 goto dr_out;
594 }
595
596 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
597 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
598 0, NULL);
599 if (!lio_r2t_cache) {
600 pr_err("Unable to kmem_cache_create() for"
601 " lio_r2t_cache\n");
602 goto ooo_out;
603 }
604
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800605 iscsit_register_transport(&iscsi_target_transport);
606
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000607 if (iscsit_load_discovery_tpg() < 0)
608 goto r2t_out;
609
610 return ret;
611r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100612 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000613 kmem_cache_destroy(lio_r2t_cache);
614ooo_out:
615 kmem_cache_destroy(lio_ooo_cache);
616dr_out:
617 kmem_cache_destroy(lio_dr_cache);
618qr_out:
619 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000620ts_out2:
621 iscsi_deallocate_thread_sets();
622ts_out1:
623 iscsi_thread_set_free();
624configfs_out:
625 iscsi_target_deregister_configfs();
626out:
627 kfree(iscsit_global);
628 return -ENOMEM;
629}
630
631static void __exit iscsi_target_cleanup_module(void)
632{
633 iscsi_deallocate_thread_sets();
634 iscsi_thread_set_free();
635 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800636 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000637 kmem_cache_destroy(lio_qr_cache);
638 kmem_cache_destroy(lio_dr_cache);
639 kmem_cache_destroy(lio_ooo_cache);
640 kmem_cache_destroy(lio_r2t_cache);
641
642 iscsi_target_deregister_configfs();
643
644 kfree(iscsit_global);
645}
646
Andy Grover8b1e1242012-04-03 15:51:12 -0700647static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700648 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000649 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700650 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000651{
652 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000653
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000654 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655 if (!cmd)
656 return -1;
657
658 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700659 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000660
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100661 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000662 if (!cmd->buf_ptr) {
663 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700664 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000665 return -1;
666 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000667
668 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700669 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000670 spin_unlock_bh(&conn->cmd_lock);
671
672 cmd->i_state = ISTATE_SEND_REJECT;
673 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
674
Nicholas Bellingerba159912013-07-03 03:48:24 -0700675 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000676}
677
Nicholas Bellingerba159912013-07-03 03:48:24 -0700678static int iscsit_add_reject_from_cmd(
679 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000680 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700681 bool add_to_conn,
682 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000683{
684 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000685
686 if (!cmd->conn) {
687 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
688 cmd->init_task_tag);
689 return -1;
690 }
691 conn = cmd->conn;
692
693 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700694 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000695
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100696 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000697 if (!cmd->buf_ptr) {
698 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700699 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000700 return -1;
701 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000702
703 if (add_to_conn) {
704 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700705 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000706 spin_unlock_bh(&conn->cmd_lock);
707 }
708
709 cmd->i_state = ISTATE_SEND_REJECT;
710 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800711 /*
712 * Perform the kref_put now if se_cmd has already been setup by
713 * scsit_setup_scsi_cmd()
714 */
715 if (cmd->se_cmd.se_tfo != NULL) {
716 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
717 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
718 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700719 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000720}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700721
722static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
723 unsigned char *buf)
724{
725 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
726}
727
728int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
729{
730 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
731}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000732
733/*
734 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700735 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000736 */
737static int iscsit_map_iovec(
738 struct iscsi_cmd *cmd,
739 struct kvec *iov,
740 u32 data_offset,
741 u32 data_length)
742{
743 u32 i = 0;
744 struct scatterlist *sg;
745 unsigned int page_off;
746
747 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700748 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000749 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700750 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000751 page_off = (data_offset % PAGE_SIZE);
752
753 cmd->first_data_sg = sg;
754 cmd->first_data_sg_off = page_off;
755
756 while (data_length) {
757 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
758
759 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
760 iov[i].iov_len = cur_len;
761
762 data_length -= cur_len;
763 page_off = 0;
764 sg = sg_next(sg);
765 i++;
766 }
767
768 cmd->kmapped_nents = i;
769
770 return i;
771}
772
773static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
774{
775 u32 i;
776 struct scatterlist *sg;
777
778 sg = cmd->first_data_sg;
779
780 for (i = 0; i < cmd->kmapped_nents; i++)
781 kunmap(sg_page(&sg[i]));
782}
783
784static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
785{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700786 LIST_HEAD(ack_list);
787 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000788
789 conn->exp_statsn = exp_statsn;
790
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800791 if (conn->sess->sess_ops->RDMAExtensions)
792 return;
793
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700795 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000796 spin_lock(&cmd->istate_lock);
797 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800798 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799 cmd->i_state = ISTATE_REMOVE;
800 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700801 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000802 continue;
803 }
804 spin_unlock(&cmd->istate_lock);
805 }
806 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700807
808 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800809 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700810 iscsit_free_cmd(cmd, false);
811 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812}
813
814static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
815{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700816 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400818 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000819
820 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
821 if (!cmd->iov_data) {
822 pr_err("Unable to allocate cmd->iov_data\n");
823 return -ENOMEM;
824 }
825
826 cmd->orig_iov_data_count = iov_count;
827 return 0;
828}
829
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800830int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
831 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000832{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800833 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000834 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700835 int iscsi_task_attr;
836 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000837
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800838 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000839
840 hdr = (struct iscsi_scsi_req *) buf;
841 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000842
843 /* FIXME; Add checks for AdditionalHeaderSegment */
844
845 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
846 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
847 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
848 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700849 return iscsit_add_reject_cmd(cmd,
850 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000851 }
852
853 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
854 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
855 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800856 * From RFC-3720 Section 10.3.1:
857 *
858 * "Either or both of R and W MAY be 1 when either the
859 * Expected Data Transfer Length and/or Bidirectional Read
860 * Expected Data Transfer Length are 0"
861 *
862 * For this case, go ahead and clear the unnecssary bits
863 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000864 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800865 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
866 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000867
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800868 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000869 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800870 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000871 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872
873 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
874 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
875 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
876 " MUST be set if Expected Data Transfer Length is not 0."
877 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700878 return iscsit_add_reject_cmd(cmd,
879 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000880 }
881
882 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
883 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
884 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700885 return iscsit_add_reject_cmd(cmd,
886 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000887 }
888
889 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
890 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
891 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700892 return iscsit_add_reject_cmd(cmd,
893 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000894 }
895
896 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
897 pr_err("ImmediateData=No but DataSegmentLength=%u,"
898 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700899 return iscsit_add_reject_cmd(cmd,
900 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000901 }
902
Nicholas Bellingerba159912013-07-03 03:48:24 -0700903 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000904 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
905 pr_err("Expected Data Transfer Length and Length of"
906 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
907 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700908 return iscsit_add_reject_cmd(cmd,
909 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000910 }
911
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400912 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000913 pr_err("DataSegmentLength: %u is greater than"
914 " EDTL: %u, protocol error.\n", payload_length,
915 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700916 return iscsit_add_reject_cmd(cmd,
917 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000918 }
919
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700920 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000921 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700922 " MaxXmitDataSegmentLength: %u, protocol error.\n",
923 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700924 return iscsit_add_reject_cmd(cmd,
925 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000926 }
927
928 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
929 pr_err("DataSegmentLength: %u is greater than"
930 " FirstBurstLength: %u, protocol error.\n",
931 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700932 return iscsit_add_reject_cmd(cmd,
933 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000934 }
935
936 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
937 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
938 DMA_NONE;
939
Andy Groverd28b11692012-04-03 15:51:22 -0700940 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700941 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
942 /*
943 * Figure out the SAM Task Attribute for the incoming SCSI CDB
944 */
945 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
946 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800947 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700948 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800949 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700950 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800951 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700952 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800953 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700954 else {
955 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800956 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
957 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700958 }
959
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000960 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
961 cmd->i_state = ISTATE_NEW_CMD;
962 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
963 cmd->immediate_data = (payload_length) ? 1 : 0;
964 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
965 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
966 if (cmd->unsolicited_data)
967 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
968
969 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
970 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
Sagi Grimbergc1e34b62015-01-26 12:49:05 +0200971 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000972 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
973 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400974 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
975 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000976 cmd->first_burst_len = payload_length;
977
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800978 if (!conn->sess->sess_ops->RDMAExtensions &&
979 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000980 struct iscsi_datain_req *dr;
981
982 dr = iscsit_allocate_datain_req();
983 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700984 return iscsit_add_reject_cmd(cmd,
985 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000986
987 iscsit_attach_datain_req(cmd, dr);
988 }
989
990 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700991 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
992 */
993 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400994 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
995 cmd->data_direction, sam_task_attr,
996 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700997
998 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
999 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001000 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1001 conn->cid);
1002
1003 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001004
Christoph Hellwigde103c92012-11-06 12:24:09 -08001005 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1006 scsilun_to_int(&hdr->lun));
1007 if (cmd->sense_reason)
1008 goto attach_cmd;
1009
1010 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1011 if (cmd->sense_reason) {
1012 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001013 return iscsit_add_reject_cmd(cmd,
1014 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001015 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001016
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001017 goto attach_cmd;
1018 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001019
Christoph Hellwigde103c92012-11-06 12:24:09 -08001020 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001021 return iscsit_add_reject_cmd(cmd,
1022 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001023 }
1024
1025attach_cmd:
1026 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001027 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001028 spin_unlock_bh(&conn->cmd_lock);
1029 /*
1030 * Check if we need to delay processing because of ALUA
1031 * Active/NonOptimized primary access state..
1032 */
1033 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001034
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001035 return 0;
1036}
1037EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001038
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001039void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1040{
1041 iscsit_set_dataout_sequence_values(cmd);
1042
1043 spin_lock_bh(&cmd->dataout_timeout_lock);
1044 iscsit_start_dataout_timer(cmd, cmd->conn);
1045 spin_unlock_bh(&cmd->dataout_timeout_lock);
1046}
1047EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1048
1049int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1050 struct iscsi_scsi_req *hdr)
1051{
1052 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001053 /*
1054 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1055 * the Immediate Bit is not set, and no Immediate
1056 * Data is attached.
1057 *
1058 * A PDU/CmdSN carrying Immediate Data can only
1059 * be processed after the DataCRC has passed.
1060 * If the DataCRC fails, the CmdSN MUST NOT
1061 * be acknowledged. (See below)
1062 */
1063 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001064 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1065 (unsigned char *)hdr, hdr->cmdsn);
1066 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1067 return -1;
1068 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001069 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001070 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001071 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001072 }
1073
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001074 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001075
1076 /*
1077 * If no Immediate Data is attached, it's OK to return now.
1078 */
1079 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001080 if (!cmd->sense_reason && cmd->unsolicited_data)
1081 iscsit_set_unsoliticed_dataout(cmd);
1082 if (!cmd->sense_reason)
1083 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001084
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001085 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001086 return 0;
1087 }
1088
1089 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001090 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1091 * execution. These exceptions are processed in CmdSN order using
1092 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001093 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001094 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001095 if (cmd->reject_reason)
1096 return 0;
1097
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001098 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001099 }
1100 /*
1101 * Call directly into transport_generic_new_cmd() to perform
1102 * the backend memory allocation.
1103 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001104 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001105 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001106 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001107
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001108 return 0;
1109}
1110EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1111
1112static int
1113iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1114 bool dump_payload)
1115{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001116 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001117 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1118 /*
1119 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1120 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001121 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001122 goto after_immediate_data;
1123
1124 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1125 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001126after_immediate_data:
1127 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1128 /*
1129 * A PDU/CmdSN carrying Immediate Data passed
1130 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1131 * Immediate Bit is not set.
1132 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001133 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1134 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001135 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001136 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001137
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001138 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001139 int rc;
1140
1141 rc = iscsit_dump_data_payload(cmd->conn,
1142 cmd->first_burst_len, 1);
1143 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1144 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001145 } else if (cmd->unsolicited_data)
1146 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001147
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001148 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1149 /*
1150 * Immediate Data failed DataCRC and ERL>=1,
1151 * silently drop this PDU and let the initiator
1152 * plug the CmdSN gap.
1153 *
1154 * FIXME: Send Unsolicited NOPIN with reserved
1155 * TTT here to help the initiator figure out
1156 * the missing CmdSN, although they should be
1157 * intelligent enough to determine the missing
1158 * CmdSN and issue a retry to plug the sequence.
1159 */
1160 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001161 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001162 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1163 return -1;
1164
1165 return 0;
1166}
1167
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001168static int
1169iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1170 unsigned char *buf)
1171{
1172 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1173 int rc, immed_data;
1174 bool dump_payload = false;
1175
1176 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1177 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001178 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001179 /*
1180 * Allocation iovecs needed for struct socket operations for
1181 * traditional iSCSI block I/O.
1182 */
1183 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001184 return iscsit_add_reject_cmd(cmd,
1185 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001186 }
1187 immed_data = cmd->immediate_data;
1188
1189 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1190 if (rc < 0)
1191 return rc;
1192 else if (rc > 0)
1193 dump_payload = true;
1194
1195 if (!immed_data)
1196 return 0;
1197
1198 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1199}
1200
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001201static u32 iscsit_do_crypto_hash_sg(
1202 struct hash_desc *hash,
1203 struct iscsi_cmd *cmd,
1204 u32 data_offset,
1205 u32 data_length,
1206 u32 padding,
1207 u8 *pad_bytes)
1208{
1209 u32 data_crc;
1210 u32 i;
1211 struct scatterlist *sg;
1212 unsigned int page_off;
1213
1214 crypto_hash_init(hash);
1215
1216 sg = cmd->first_data_sg;
1217 page_off = cmd->first_data_sg_off;
1218
1219 i = 0;
1220 while (data_length) {
1221 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1222
1223 crypto_hash_update(hash, &sg[i], cur_len);
1224
1225 data_length -= cur_len;
1226 page_off = 0;
1227 i++;
1228 }
1229
1230 if (padding) {
1231 struct scatterlist pad_sg;
1232
1233 sg_init_one(&pad_sg, pad_bytes, padding);
1234 crypto_hash_update(hash, &pad_sg, padding);
1235 }
1236 crypto_hash_final(hash, (u8 *) &data_crc);
1237
1238 return data_crc;
1239}
1240
1241static void iscsit_do_crypto_hash_buf(
1242 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001243 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001244 u32 payload_length,
1245 u32 padding,
1246 u8 *pad_bytes,
1247 u8 *data_crc)
1248{
1249 struct scatterlist sg;
1250
1251 crypto_hash_init(hash);
1252
Jörn Engel8359cf42011-11-24 02:05:51 +01001253 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001254 crypto_hash_update(hash, &sg, payload_length);
1255
1256 if (padding) {
1257 sg_init_one(&sg, pad_bytes, padding);
1258 crypto_hash_update(hash, &sg, padding);
1259 }
1260 crypto_hash_final(hash, data_crc);
1261}
1262
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263int
1264iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1265 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001266{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001267 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268 struct iscsi_cmd *cmd = NULL;
1269 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001270 u32 payload_length = ntoh24(hdr->dlength);
1271 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001272
1273 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001274 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1275 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001276 }
1277
1278 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001279 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001280
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001281 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001282 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001283 " MaxXmitDataSegmentLength: %u\n", payload_length,
1284 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001285 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1286 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001287 }
1288
1289 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1290 payload_length);
1291 if (!cmd)
1292 return 0;
1293
1294 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1295 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001296 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001297 payload_length, conn->cid);
1298
1299 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1300 pr_err("Command ITT: 0x%08x received DataOUT after"
1301 " last DataOUT received, dumping payload\n",
1302 cmd->init_task_tag);
1303 return iscsit_dump_data_payload(conn, payload_length, 1);
1304 }
1305
1306 if (cmd->data_direction != DMA_TO_DEVICE) {
1307 pr_err("Command ITT: 0x%08x received DataOUT for a"
1308 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001309 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001310 }
1311 se_cmd = &cmd->se_cmd;
1312 iscsit_mod_dataout_timer(cmd);
1313
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001314 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001315 pr_err("DataOut Offset: %u, Length %u greater than"
1316 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001317 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001318 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001319 }
1320
1321 if (cmd->unsolicited_data) {
1322 int dump_unsolicited_data = 0;
1323
1324 if (conn->sess->sess_ops->InitialR2T) {
1325 pr_err("Received unexpected unsolicited data"
1326 " while InitialR2T=Yes, protocol error.\n");
1327 transport_send_check_condition_and_sense(&cmd->se_cmd,
1328 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1329 return -1;
1330 }
1331 /*
1332 * Special case for dealing with Unsolicited DataOUT
1333 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1334 * failures;
1335 */
1336
1337 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001338 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001339 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001340 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001341
1342 if (dump_unsolicited_data) {
1343 /*
1344 * Check if a delayed TASK_ABORTED status needs to
1345 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1346 * received with the unsolicitied data out.
1347 */
1348 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1349 iscsit_stop_dataout_timer(cmd);
1350
1351 transport_check_aborted_status(se_cmd,
1352 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1353 return iscsit_dump_data_payload(conn, payload_length, 1);
1354 }
1355 } else {
1356 /*
1357 * For the normal solicited data path:
1358 *
1359 * Check for a delayed TASK_ABORTED status and dump any
1360 * incoming data out payload if one exists. Also, when the
1361 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1362 * data out sequence, we decrement outstanding_r2ts. Once
1363 * outstanding_r2ts reaches zero, go ahead and send the delayed
1364 * TASK_ABORTED status.
1365 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001366 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001367 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1368 if (--cmd->outstanding_r2ts < 1) {
1369 iscsit_stop_dataout_timer(cmd);
1370 transport_check_aborted_status(
1371 se_cmd, 1);
1372 }
1373
1374 return iscsit_dump_data_payload(conn, payload_length, 1);
1375 }
1376 }
1377 /*
1378 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1379 * within-command recovery checks before receiving the payload.
1380 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001381 rc = iscsit_check_pre_dataout(cmd, buf);
1382 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001383 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001384 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001385 return -1;
1386
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001387 *out_cmd = cmd;
1388 return 0;
1389}
1390EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1391
1392static int
1393iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1394 struct iscsi_data *hdr)
1395{
1396 struct kvec *iov;
1397 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1398 u32 payload_length = ntoh24(hdr->dlength);
1399 int iov_ret, data_crc_failed = 0;
1400
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001401 rx_size += payload_length;
1402 iov = &cmd->iov_data[0];
1403
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001404 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1405 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001406 if (iov_ret < 0)
1407 return -1;
1408
1409 iov_count += iov_ret;
1410
1411 padding = ((-payload_length) & 3);
1412 if (padding != 0) {
1413 iov[iov_count].iov_base = cmd->pad_bytes;
1414 iov[iov_count++].iov_len = padding;
1415 rx_size += padding;
1416 pr_debug("Receiving %u padding bytes.\n", padding);
1417 }
1418
1419 if (conn->conn_ops->DataDigest) {
1420 iov[iov_count].iov_base = &checksum;
1421 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1422 rx_size += ISCSI_CRC_LEN;
1423 }
1424
1425 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1426
1427 iscsit_unmap_iovec(cmd);
1428
1429 if (rx_got != rx_size)
1430 return -1;
1431
1432 if (conn->conn_ops->DataDigest) {
1433 u32 data_crc;
1434
1435 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001436 be32_to_cpu(hdr->offset),
1437 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001438 cmd->pad_bytes);
1439
1440 if (checksum != data_crc) {
1441 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1442 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1443 " does not match computed 0x%08x\n",
1444 hdr->itt, hdr->offset, payload_length,
1445 hdr->datasn, checksum, data_crc);
1446 data_crc_failed = 1;
1447 } else {
1448 pr_debug("Got CRC32C DataDigest 0x%08x for"
1449 " %u bytes of Data Out\n", checksum,
1450 payload_length);
1451 }
1452 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001453
1454 return data_crc_failed;
1455}
1456
1457int
1458iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1459 bool data_crc_failed)
1460{
1461 struct iscsi_conn *conn = cmd->conn;
1462 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001463 /*
1464 * Increment post receive data and CRC values or perform
1465 * within-command recovery.
1466 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001467 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1468 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001469 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001470 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001471 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001472 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1473 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001474 /*
1475 * Handle extra special case for out of order
1476 * Unsolicited Data Out.
1477 */
1478 spin_lock_bh(&cmd->istate_lock);
1479 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1480 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1481 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1482 spin_unlock_bh(&cmd->istate_lock);
1483
1484 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001485 if (ooo_cmdsn)
1486 return 0;
1487 target_execute_cmd(&cmd->se_cmd);
1488 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001489 } else /* DATAOUT_CANNOT_RECOVER */
1490 return -1;
1491
1492 return 0;
1493}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001494EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001495
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001496static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1497{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001498 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001499 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1500 int rc;
1501 bool data_crc_failed = false;
1502
1503 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1504 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001505 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001506 else if (!cmd)
1507 return 0;
1508
1509 rc = iscsit_get_dataout(conn, cmd, hdr);
1510 if (rc < 0)
1511 return rc;
1512 else if (rc > 0)
1513 data_crc_failed = true;
1514
1515 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1516}
1517
Nicholas Bellinger778de362013-06-14 16:07:47 -07001518int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1519 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001520{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001521 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001522
Arshad Hussaina3662602014-03-14 15:28:59 -07001523 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1524 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1525 if (!cmd)
1526 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1527 (unsigned char *)hdr);
1528
1529 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1530 (unsigned char *)hdr);
1531 }
1532
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001533 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001534 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1535 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001536 if (!cmd)
1537 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1538 (unsigned char *)hdr);
1539
Nicholas Bellingerba159912013-07-03 03:48:24 -07001540 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1541 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001542 }
1543
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001544 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001545 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001546 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001547 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001548 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001549 if (!cmd)
1550 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1551 (unsigned char *)hdr);
1552
Nicholas Bellingerba159912013-07-03 03:48:24 -07001553 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1554 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001555 }
1556
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001557 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001558 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001559 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001560 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1561 payload_length);
1562 /*
1563 * This is not a response to a Unsolicited NopIN, which means
1564 * it can either be a NOPOUT ping request (with a valid ITT),
1565 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1566 * Either way, make sure we allocate an struct iscsi_cmd, as both
1567 * can contain ping data.
1568 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001569 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001570 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1571 cmd->i_state = ISTATE_SEND_NOPIN;
1572 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1573 1 : 0);
1574 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1575 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001576 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1577 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001578 cmd->data_direction = DMA_NONE;
1579 }
1580
Nicholas Bellinger778de362013-06-14 16:07:47 -07001581 return 0;
1582}
1583EXPORT_SYMBOL(iscsit_setup_nop_out);
1584
1585int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1586 struct iscsi_nopout *hdr)
1587{
1588 struct iscsi_cmd *cmd_p = NULL;
1589 int cmdsn_ret = 0;
1590 /*
1591 * Initiator is expecting a NopIN ping reply..
1592 */
1593 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001594 if (!cmd)
1595 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1596 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001597
1598 spin_lock_bh(&conn->cmd_lock);
1599 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1600 spin_unlock_bh(&conn->cmd_lock);
1601
1602 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1603
1604 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1605 iscsit_add_cmd_to_response_queue(cmd, conn,
1606 cmd->i_state);
1607 return 0;
1608 }
1609
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001610 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1611 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001612 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1613 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001614 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001615 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001616
1617 return 0;
1618 }
1619 /*
1620 * This was a response to a unsolicited NOPIN ping.
1621 */
1622 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1623 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1624 if (!cmd_p)
1625 return -EINVAL;
1626
1627 iscsit_stop_nopin_response_timer(conn);
1628
1629 cmd_p->i_state = ISTATE_REMOVE;
1630 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1631
1632 iscsit_start_nopin_timer(conn);
1633 return 0;
1634 }
1635 /*
1636 * Otherwise, initiator is not expecting a NOPIN is response.
1637 * Just ignore for now.
1638 */
1639 return 0;
1640}
1641EXPORT_SYMBOL(iscsit_process_nop_out);
1642
1643static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1644 unsigned char *buf)
1645{
1646 unsigned char *ping_data = NULL;
1647 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1648 struct kvec *iov = NULL;
1649 u32 payload_length = ntoh24(hdr->dlength);
1650 int ret;
1651
1652 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1653 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001654 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001655 /*
1656 * Handle NOP-OUT payload for traditional iSCSI sockets
1657 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001658 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001659 u32 checksum, data_crc, padding = 0;
1660 int niov = 0, rx_got, rx_size = payload_length;
1661
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001662 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1663 if (!ping_data) {
1664 pr_err("Unable to allocate memory for"
1665 " NOPOUT ping data.\n");
1666 ret = -1;
1667 goto out;
1668 }
1669
1670 iov = &cmd->iov_misc[0];
1671 iov[niov].iov_base = ping_data;
1672 iov[niov++].iov_len = payload_length;
1673
1674 padding = ((-payload_length) & 3);
1675 if (padding != 0) {
1676 pr_debug("Receiving %u additional bytes"
1677 " for padding.\n", padding);
1678 iov[niov].iov_base = &cmd->pad_bytes;
1679 iov[niov++].iov_len = padding;
1680 rx_size += padding;
1681 }
1682 if (conn->conn_ops->DataDigest) {
1683 iov[niov].iov_base = &checksum;
1684 iov[niov++].iov_len = ISCSI_CRC_LEN;
1685 rx_size += ISCSI_CRC_LEN;
1686 }
1687
1688 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1689 if (rx_got != rx_size) {
1690 ret = -1;
1691 goto out;
1692 }
1693
1694 if (conn->conn_ops->DataDigest) {
1695 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1696 ping_data, payload_length,
1697 padding, cmd->pad_bytes,
1698 (u8 *)&data_crc);
1699
1700 if (checksum != data_crc) {
1701 pr_err("Ping data CRC32C DataDigest"
1702 " 0x%08x does not match computed 0x%08x\n",
1703 checksum, data_crc);
1704 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1705 pr_err("Unable to recover from"
1706 " NOPOUT Ping DataCRC failure while in"
1707 " ERL=0.\n");
1708 ret = -1;
1709 goto out;
1710 } else {
1711 /*
1712 * Silently drop this PDU and let the
1713 * initiator plug the CmdSN gap.
1714 */
1715 pr_debug("Dropping NOPOUT"
1716 " Command CmdSN: 0x%08x due to"
1717 " DataCRC error.\n", hdr->cmdsn);
1718 ret = 0;
1719 goto out;
1720 }
1721 } else {
1722 pr_debug("Got CRC32C DataDigest"
1723 " 0x%08x for %u bytes of ping data.\n",
1724 checksum, payload_length);
1725 }
1726 }
1727
1728 ping_data[payload_length] = '\0';
1729 /*
1730 * Attach ping data to struct iscsi_cmd->buf_ptr.
1731 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001732 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001733 cmd->buf_ptr_size = payload_length;
1734
1735 pr_debug("Got %u bytes of NOPOUT ping"
1736 " data.\n", payload_length);
1737 pr_debug("Ping Data: \"%s\"\n", ping_data);
1738 }
1739
Nicholas Bellinger778de362013-06-14 16:07:47 -07001740 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001741out:
1742 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001743 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001744
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 kfree(ping_data);
1746 return ret;
1747}
1748
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001749int
1750iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1751 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001752{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001753 struct se_tmr_req *se_tmr;
1754 struct iscsi_tmr_req *tmr_req;
1755 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001756 int out_of_order_cmdsn = 0, ret;
1757 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001758 u8 function;
1759
1760 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001761 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1762 function = hdr->flags;
1763
1764 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1765 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1766 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1767 hdr->rtt, hdr->refcmdsn, conn->cid);
1768
1769 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1770 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001771 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001772 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001773 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001774 }
1775
1776 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1777 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1778 pr_err("Task Management Request TASK_REASSIGN not"
1779 " issued as immediate command, bad iSCSI Initiator"
1780 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001781 return iscsit_add_reject_cmd(cmd,
1782 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001783 }
1784 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001785 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1786 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001787
Andy Groverd28b11692012-04-03 15:51:22 -07001788 cmd->data_direction = DMA_NONE;
1789
1790 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1791 if (!cmd->tmr_req) {
1792 pr_err("Unable to allocate memory for"
1793 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001794 return iscsit_add_reject_cmd(cmd,
1795 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1796 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001797 }
1798
1799 /*
1800 * TASK_REASSIGN for ERL=2 / connection stays inside of
1801 * LIO-Target $FABRIC_MOD
1802 */
1803 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1804
1805 u8 tcm_function;
1806 int ret;
1807
1808 transport_init_se_cmd(&cmd->se_cmd,
1809 &lio_target_fabric_configfs->tf_ops,
1810 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001811 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001812
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001813 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1814 sess_ref = true;
1815
Andy Groverd28b11692012-04-03 15:51:22 -07001816 switch (function) {
1817 case ISCSI_TM_FUNC_ABORT_TASK:
1818 tcm_function = TMR_ABORT_TASK;
1819 break;
1820 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1821 tcm_function = TMR_ABORT_TASK_SET;
1822 break;
1823 case ISCSI_TM_FUNC_CLEAR_ACA:
1824 tcm_function = TMR_CLEAR_ACA;
1825 break;
1826 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1827 tcm_function = TMR_CLEAR_TASK_SET;
1828 break;
1829 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1830 tcm_function = TMR_LUN_RESET;
1831 break;
1832 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1833 tcm_function = TMR_TARGET_WARM_RESET;
1834 break;
1835 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1836 tcm_function = TMR_TARGET_COLD_RESET;
1837 break;
1838 default:
1839 pr_err("Unknown iSCSI TMR Function:"
1840 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001841 return iscsit_add_reject_cmd(cmd,
1842 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001843 }
1844
1845 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1846 tcm_function, GFP_KERNEL);
1847 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001848 return iscsit_add_reject_cmd(cmd,
1849 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001850
1851 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1852 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001853
1854 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1855 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1856 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1857 cmd->init_task_tag = hdr->itt;
1858 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001859 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1860 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 se_tmr = cmd->se_cmd.se_tmr_req;
1862 tmr_req = cmd->tmr_req;
1863 /*
1864 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1865 */
1866 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001867 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1868 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001869 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001870 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1871 goto attach;
1872 }
1873 }
1874
1875 switch (function) {
1876 case ISCSI_TM_FUNC_ABORT_TASK:
1877 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001878 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001879 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001880 break;
1881 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1882 case ISCSI_TM_FUNC_CLEAR_ACA:
1883 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1884 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1885 break;
1886 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1887 if (iscsit_tmr_task_warm_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_TARGET_COLD_RESET:
1893 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001894 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1895 goto attach;
1896 }
1897 break;
1898 case ISCSI_TM_FUNC_TASK_REASSIGN:
1899 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1900 /*
1901 * Perform sanity checks on the ExpDataSN only if the
1902 * TASK_REASSIGN was successful.
1903 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001904 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 break;
1906
1907 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001908 return iscsit_add_reject_cmd(cmd,
1909 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001910 break;
1911 default:
1912 pr_err("Unknown TMR function: 0x%02x, protocol"
1913 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001914 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1915 goto attach;
1916 }
1917
1918 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1919 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1920 se_tmr->call_transport = 1;
1921attach:
1922 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001923 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001924 spin_unlock_bh(&conn->cmd_lock);
1925
1926 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001927 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1929 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001930 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001931 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001932 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001933 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001934 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001935 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001936
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001937 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938 return 0;
1939 /*
1940 * Found the referenced task, send to transport for processing.
1941 */
1942 if (se_tmr->call_transport)
1943 return transport_generic_handle_tmr(&cmd->se_cmd);
1944
1945 /*
1946 * Could not find the referenced LUN, task, or Task Management
1947 * command not authorized or supported. Change state and
1948 * let the tx_thread send the response.
1949 *
1950 * For connection recovery, this is also the default action for
1951 * TMR TASK_REASSIGN.
1952 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001953 if (sess_ref) {
1954 pr_debug("Handle TMR, using sess_ref=true check\n");
1955 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1956 }
1957
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1959 return 0;
1960}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001961EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001962
1963/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001964int
1965iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1966 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001967{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001968 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001969
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001970 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001971 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001972 "greater than MaxXmitDataSegmentLength %u.\n",
1973 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001974 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1975 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001976 }
1977
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001978 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1979 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1980 pr_err("Multi sequence text commands currently not supported\n");
1981 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1982 (unsigned char *)hdr);
1983 }
1984
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001985 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1986 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1987 hdr->exp_statsn, payload_length);
1988
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001989 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1990 cmd->i_state = ISTATE_SEND_TEXTRSP;
1991 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1992 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1993 cmd->targ_xfer_tag = 0xFFFFFFFF;
1994 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1995 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1996 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001997 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001998
1999 return 0;
2000}
2001EXPORT_SYMBOL(iscsit_setup_text_cmd);
2002
2003int
2004iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2005 struct iscsi_text *hdr)
2006{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002007 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002008 int cmdsn_ret;
2009
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002010 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002011 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2012 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2013 pr_err("Unable to locate text_in buffer for sendtargets"
2014 " discovery\n");
2015 goto reject;
2016 }
2017 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002018 }
2019 if (strncmp("SendTargets", text_in, 11) != 0) {
2020 pr_err("Received Text Data that is not"
2021 " SendTargets, cannot continue.\n");
2022 goto reject;
2023 }
2024 text_ptr = strchr(text_in, '=');
2025 if (!text_ptr) {
2026 pr_err("No \"=\" separator found in Text Data,"
2027 " cannot continue.\n");
2028 goto reject;
2029 }
2030 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002031 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002032 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2033 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002034 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002035 } else {
2036 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2037 goto reject;
2038 }
2039
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002040 spin_lock_bh(&conn->cmd_lock);
2041 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2042 spin_unlock_bh(&conn->cmd_lock);
2043
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002044empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002045 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2046
2047 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002048 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2049 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002050 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002051 return -1;
2052
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002053 return 0;
2054 }
2055
2056 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002057
2058reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002059 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2060 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002061}
2062EXPORT_SYMBOL(iscsit_process_text_cmd);
2063
2064static int
2065iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2066 unsigned char *buf)
2067{
2068 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2069 char *text_in = NULL;
2070 u32 payload_length = ntoh24(hdr->dlength);
2071 int rx_size, rc;
2072
2073 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2074 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002075 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002076
2077 rx_size = payload_length;
2078 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002079 u32 checksum = 0, data_crc = 0;
2080 u32 padding = 0, pad_bytes = 0;
2081 int niov = 0, rx_got;
2082 struct kvec iov[3];
2083
2084 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085 if (!text_in) {
2086 pr_err("Unable to allocate memory for"
2087 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002088 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002090 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002091
2092 memset(iov, 0, 3 * sizeof(struct kvec));
2093 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002094 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002095
2096 padding = ((-payload_length) & 3);
2097 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002098 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002099 iov[niov++].iov_len = padding;
2100 rx_size += padding;
2101 pr_debug("Receiving %u additional bytes"
2102 " for padding.\n", padding);
2103 }
2104 if (conn->conn_ops->DataDigest) {
2105 iov[niov].iov_base = &checksum;
2106 iov[niov++].iov_len = ISCSI_CRC_LEN;
2107 rx_size += ISCSI_CRC_LEN;
2108 }
2109
2110 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111 if (rx_got != rx_size)
2112 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002113
2114 if (conn->conn_ops->DataDigest) {
2115 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002117 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002118 (u8 *)&data_crc);
2119
2120 if (checksum != data_crc) {
2121 pr_err("Text data CRC32C DataDigest"
2122 " 0x%08x does not match computed"
2123 " 0x%08x\n", checksum, data_crc);
2124 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2125 pr_err("Unable to recover from"
2126 " Text Data digest failure while in"
2127 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002128 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002129 } else {
2130 /*
2131 * Silently drop this PDU and let the
2132 * initiator plug the CmdSN gap.
2133 */
2134 pr_debug("Dropping Text"
2135 " Command CmdSN: 0x%08x due to"
2136 " DataCRC error.\n", hdr->cmdsn);
2137 kfree(text_in);
2138 return 0;
2139 }
2140 } else {
2141 pr_debug("Got CRC32C DataDigest"
2142 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002143 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002144 }
2145 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002146 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002147 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002148 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002149 }
2150
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002151 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002152
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002153reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002154 kfree(cmd->text_in_ptr);
2155 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002156 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002157}
2158
2159int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2160{
2161 struct iscsi_conn *conn_p;
2162 struct iscsi_session *sess = conn->sess;
2163
2164 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2165 " for SID: %u.\n", conn->cid, conn->sess->sid);
2166
2167 atomic_set(&sess->session_logout, 1);
2168 atomic_set(&conn->conn_logout_remove, 1);
2169 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2170
2171 iscsit_inc_conn_usage_count(conn);
2172 iscsit_inc_session_usage_count(sess);
2173
2174 spin_lock_bh(&sess->conn_lock);
2175 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2176 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2177 continue;
2178
2179 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2180 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2181 }
2182 spin_unlock_bh(&sess->conn_lock);
2183
2184 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2185
2186 return 0;
2187}
2188
2189int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2190{
2191 struct iscsi_conn *l_conn;
2192 struct iscsi_session *sess = conn->sess;
2193
2194 pr_debug("Received logout request CLOSECONNECTION for CID:"
2195 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2196
2197 /*
2198 * A Logout Request with a CLOSECONNECTION reason code for a CID
2199 * can arrive on a connection with a differing CID.
2200 */
2201 if (conn->cid == cmd->logout_cid) {
2202 spin_lock_bh(&conn->state_lock);
2203 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2204 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2205
2206 atomic_set(&conn->conn_logout_remove, 1);
2207 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2208 iscsit_inc_conn_usage_count(conn);
2209
2210 spin_unlock_bh(&conn->state_lock);
2211 } else {
2212 /*
2213 * Handle all different cid CLOSECONNECTION requests in
2214 * iscsit_logout_post_handler_diffcid() as to give enough
2215 * time for any non immediate command's CmdSN to be
2216 * acknowledged on the connection in question.
2217 *
2218 * Here we simply make sure the CID is still around.
2219 */
2220 l_conn = iscsit_get_conn_from_cid(sess,
2221 cmd->logout_cid);
2222 if (!l_conn) {
2223 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2224 iscsit_add_cmd_to_response_queue(cmd, conn,
2225 cmd->i_state);
2226 return 0;
2227 }
2228
2229 iscsit_dec_conn_usage_count(l_conn);
2230 }
2231
2232 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2233
2234 return 0;
2235}
2236
2237int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2238{
2239 struct iscsi_session *sess = conn->sess;
2240
2241 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2242 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2243
2244 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2245 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2246 " while ERL!=2.\n");
2247 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2248 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2249 return 0;
2250 }
2251
2252 if (conn->cid == cmd->logout_cid) {
2253 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2254 " with CID: %hu on CID: %hu, implementation error.\n",
2255 cmd->logout_cid, conn->cid);
2256 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2257 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2258 return 0;
2259 }
2260
2261 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2262
2263 return 0;
2264}
2265
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002266int
2267iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2268 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002269{
2270 int cmdsn_ret, logout_remove = 0;
2271 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 struct iscsi_logout *hdr;
2273 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2274
2275 hdr = (struct iscsi_logout *) buf;
2276 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002277
2278 if (tiqn) {
2279 spin_lock(&tiqn->logout_stats.lock);
2280 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2281 tiqn->logout_stats.normal_logouts++;
2282 else
2283 tiqn->logout_stats.abnormal_logouts++;
2284 spin_unlock(&tiqn->logout_stats.lock);
2285 }
2286
2287 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2288 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2289 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2290 hdr->cid, conn->cid);
2291
2292 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2293 pr_err("Received logout request on connection that"
2294 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002295 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 return 0;
2297 }
2298
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002299 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2300 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2301 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2302 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2303 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002304 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2305 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2306 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307 cmd->logout_reason = reason_code;
2308 cmd->data_direction = DMA_NONE;
2309
2310 /*
2311 * We need to sleep in these cases (by returning 1) until the Logout
2312 * Response gets sent in the tx thread.
2313 */
2314 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2315 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002316 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 logout_remove = 1;
2318
2319 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002320 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002321 spin_unlock_bh(&conn->cmd_lock);
2322
2323 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002324 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002325
2326 /*
2327 * Immediate commands are executed, well, immediately.
2328 * Non-Immediate Logout Commands are executed in CmdSN order.
2329 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002330 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002331 int ret = iscsit_execute_cmd(cmd, 0);
2332
2333 if (ret < 0)
2334 return ret;
2335 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002336 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002337 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002338 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002339 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2340 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002341 }
2342
2343 return logout_remove;
2344}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002345EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002346
2347static int iscsit_handle_snack(
2348 struct iscsi_conn *conn,
2349 unsigned char *buf)
2350{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002351 struct iscsi_snack *hdr;
2352
2353 hdr = (struct iscsi_snack *) buf;
2354 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355
2356 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2357 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2358 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2359 hdr->begrun, hdr->runlength, conn->cid);
2360
2361 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2362 pr_err("Initiator sent SNACK request while in"
2363 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002364 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2365 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002366 }
2367 /*
2368 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2369 * call from inside iscsi_send_recovery_datain_or_r2t().
2370 */
2371 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2372 case 0:
2373 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002374 hdr->itt,
2375 be32_to_cpu(hdr->ttt),
2376 be32_to_cpu(hdr->begrun),
2377 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002378 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002379 return iscsit_handle_status_snack(conn, hdr->itt,
2380 be32_to_cpu(hdr->ttt),
2381 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002382 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002383 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2384 be32_to_cpu(hdr->begrun),
2385 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002386 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2387 /* FIXME: Support R-Data SNACK */
2388 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002389 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2390 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002391 default:
2392 pr_err("Unknown SNACK type 0x%02x, protocol"
2393 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002394 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2395 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002396 }
2397
2398 return 0;
2399}
2400
2401static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2402{
2403 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2404 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2405 wait_for_completion_interruptible_timeout(
2406 &conn->rx_half_close_comp,
2407 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2408 }
2409}
2410
2411static int iscsit_handle_immediate_data(
2412 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002413 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002414 u32 length)
2415{
2416 int iov_ret, rx_got = 0, rx_size = 0;
2417 u32 checksum, iov_count = 0, padding = 0;
2418 struct iscsi_conn *conn = cmd->conn;
2419 struct kvec *iov;
2420
2421 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2422 if (iov_ret < 0)
2423 return IMMEDIATE_DATA_CANNOT_RECOVER;
2424
2425 rx_size = length;
2426 iov_count = iov_ret;
2427 iov = &cmd->iov_data[0];
2428
2429 padding = ((-length) & 3);
2430 if (padding != 0) {
2431 iov[iov_count].iov_base = cmd->pad_bytes;
2432 iov[iov_count++].iov_len = padding;
2433 rx_size += padding;
2434 }
2435
2436 if (conn->conn_ops->DataDigest) {
2437 iov[iov_count].iov_base = &checksum;
2438 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2439 rx_size += ISCSI_CRC_LEN;
2440 }
2441
2442 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2443
2444 iscsit_unmap_iovec(cmd);
2445
2446 if (rx_got != rx_size) {
2447 iscsit_rx_thread_wait_for_tcp(conn);
2448 return IMMEDIATE_DATA_CANNOT_RECOVER;
2449 }
2450
2451 if (conn->conn_ops->DataDigest) {
2452 u32 data_crc;
2453
2454 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2455 cmd->write_data_done, length, padding,
2456 cmd->pad_bytes);
2457
2458 if (checksum != data_crc) {
2459 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2460 " does not match computed 0x%08x\n", checksum,
2461 data_crc);
2462
2463 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2464 pr_err("Unable to recover from"
2465 " Immediate Data digest failure while"
2466 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002467 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002468 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002469 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002470 return IMMEDIATE_DATA_CANNOT_RECOVER;
2471 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002472 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002473 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002474 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002475 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2476 }
2477 } else {
2478 pr_debug("Got CRC32C DataDigest 0x%08x for"
2479 " %u bytes of Immediate Data\n", checksum,
2480 length);
2481 }
2482 }
2483
2484 cmd->write_data_done += length;
2485
Andy Groverebf1d952012-04-03 15:51:24 -07002486 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002487 spin_lock_bh(&cmd->istate_lock);
2488 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2489 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2490 spin_unlock_bh(&cmd->istate_lock);
2491 }
2492
2493 return IMMEDIATE_DATA_NORMAL_OPERATION;
2494}
2495
2496/*
2497 * Called with sess->conn_lock held.
2498 */
2499/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2500 with active network interface */
2501static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2502{
2503 struct iscsi_cmd *cmd;
2504 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002505 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002506
2507 /*
2508 * Only send a Asynchronous Message on connections whos network
2509 * interface is still functional.
2510 */
2511 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2512 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2513 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002514 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002515 break;
2516 }
2517 }
2518
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002519 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520 return;
2521
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002522 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 if (!cmd) {
2524 iscsit_dec_conn_usage_count(conn_p);
2525 return;
2526 }
2527
2528 cmd->logout_cid = conn->cid;
2529 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2530 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2531
2532 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002533 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002534 spin_unlock_bh(&conn_p->cmd_lock);
2535
2536 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2537 iscsit_dec_conn_usage_count(conn_p);
2538}
2539
2540static int iscsit_send_conn_drop_async_message(
2541 struct iscsi_cmd *cmd,
2542 struct iscsi_conn *conn)
2543{
2544 struct iscsi_async *hdr;
2545
2546 cmd->tx_size = ISCSI_HDR_LEN;
2547 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2548
2549 hdr = (struct iscsi_async *) cmd->pdu;
2550 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2551 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002552 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002553 cmd->targ_xfer_tag = 0xFFFFFFFF;
2554 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2555 cmd->stat_sn = conn->stat_sn++;
2556 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2557 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2558 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2559 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2560 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2561 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2562 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2563
2564 if (conn->conn_ops->HeaderDigest) {
2565 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2566
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002567 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2568 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002569
2570 cmd->tx_size += ISCSI_CRC_LEN;
2571 pr_debug("Attaching CRC32C HeaderDigest to"
2572 " Async Message 0x%08x\n", *header_digest);
2573 }
2574
2575 cmd->iov_misc[0].iov_base = cmd->pdu;
2576 cmd->iov_misc[0].iov_len = cmd->tx_size;
2577 cmd->iov_misc_count = 1;
2578
2579 pr_debug("Sending Connection Dropped Async Message StatSN:"
2580 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2581 cmd->logout_cid, conn->cid);
2582 return 0;
2583}
2584
Andy Grover6f3c0e62012-04-03 15:51:09 -07002585static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2586{
2587 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2588 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2589 wait_for_completion_interruptible_timeout(
2590 &conn->tx_half_close_comp,
2591 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2592 }
2593}
2594
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002595static void
2596iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2597 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2598 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002599{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002600 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2601 hdr->flags = datain->flags;
2602 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2603 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2604 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2605 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2606 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2607 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2608 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2609 }
2610 }
2611 hton24(hdr->dlength, datain->length);
2612 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2613 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2614 (struct scsi_lun *)&hdr->lun);
2615 else
2616 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2617
2618 hdr->itt = cmd->init_task_tag;
2619
2620 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2621 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2622 else
2623 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2624 if (set_statsn)
2625 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2626 else
2627 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2628
2629 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2630 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2631 hdr->datasn = cpu_to_be32(datain->data_sn);
2632 hdr->offset = cpu_to_be32(datain->offset);
2633
2634 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2635 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2636 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2637 ntohl(hdr->offset), datain->length, conn->cid);
2638}
2639
2640static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2641{
2642 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002643 struct iscsi_datain datain;
2644 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002645 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002646 u32 iov_count = 0, tx_size = 0;
2647 int eodr = 0, ret, iov_ret;
2648 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649
2650 memset(&datain, 0, sizeof(struct iscsi_datain));
2651 dr = iscsit_get_datain_values(cmd, &datain);
2652 if (!dr) {
2653 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2654 cmd->init_task_tag);
2655 return -1;
2656 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002657 /*
2658 * Be paranoid and double check the logic for now.
2659 */
Andy Groverebf1d952012-04-03 15:51:24 -07002660 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002661 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2662 " datain.length: %u exceeds cmd->data_length: %u\n",
2663 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002664 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665 return -1;
2666 }
2667
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002668 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002669 /*
2670 * Special case for successfully execution w/ both DATAIN
2671 * and Sense Data.
2672 */
2673 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2674 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2675 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2676 else {
2677 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2678 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2679 iscsit_increment_maxcmdsn(cmd, conn->sess);
2680 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002681 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002683 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2684 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002685 }
2686
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002687 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688
2689 iov = &cmd->iov_data[0];
2690 iov[iov_count].iov_base = cmd->pdu;
2691 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2692 tx_size += ISCSI_HDR_LEN;
2693
2694 if (conn->conn_ops->HeaderDigest) {
2695 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2696
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002697 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2698 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002699
2700 iov[0].iov_len += ISCSI_CRC_LEN;
2701 tx_size += ISCSI_CRC_LEN;
2702
2703 pr_debug("Attaching CRC32 HeaderDigest"
2704 " for DataIN PDU 0x%08x\n", *header_digest);
2705 }
2706
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002707 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2708 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002709 if (iov_ret < 0)
2710 return -1;
2711
2712 iov_count += iov_ret;
2713 tx_size += datain.length;
2714
2715 cmd->padding = ((-datain.length) & 3);
2716 if (cmd->padding) {
2717 iov[iov_count].iov_base = cmd->pad_bytes;
2718 iov[iov_count++].iov_len = cmd->padding;
2719 tx_size += cmd->padding;
2720
2721 pr_debug("Attaching %u padding bytes\n",
2722 cmd->padding);
2723 }
2724 if (conn->conn_ops->DataDigest) {
2725 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2726 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2727
2728 iov[iov_count].iov_base = &cmd->data_crc;
2729 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2730 tx_size += ISCSI_CRC_LEN;
2731
2732 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2733 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2734 }
2735
2736 cmd->iov_data_count = iov_count;
2737 cmd->tx_size = tx_size;
2738
Andy Grover6f3c0e62012-04-03 15:51:09 -07002739 /* sendpage is preferred but can't insert markers */
2740 if (!conn->conn_ops->IFMarker)
2741 ret = iscsit_fe_sendpage_sg(cmd, conn);
2742 else
2743 ret = iscsit_send_tx_data(cmd, conn, 0);
2744
2745 iscsit_unmap_iovec(cmd);
2746
2747 if (ret < 0) {
2748 iscsit_tx_thread_wait_for_tcp(conn);
2749 return ret;
2750 }
2751
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002752 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002753 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002754 2 : 1;
2755 iscsit_free_datain_req(cmd, dr);
2756 }
2757
Andy Grover6f3c0e62012-04-03 15:51:09 -07002758 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002759}
2760
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002761int
2762iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2763 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002764{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002765 struct iscsi_conn *logout_conn = NULL;
2766 struct iscsi_conn_recovery *cr = NULL;
2767 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002768 /*
2769 * The actual shutting down of Sessions and/or Connections
2770 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2771 * is done in scsi_logout_post_handler().
2772 */
2773 switch (cmd->logout_reason) {
2774 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2775 pr_debug("iSCSI session logout successful, setting"
2776 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2777 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2778 break;
2779 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2780 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2781 break;
2782 /*
2783 * For CLOSECONNECTION logout requests carrying
2784 * a matching logout CID -> local CID, the reference
2785 * for the local CID will have been incremented in
2786 * iscsi_logout_closeconnection().
2787 *
2788 * For CLOSECONNECTION logout requests carrying
2789 * a different CID than the connection it arrived
2790 * on, the connection responding to cmd->logout_cid
2791 * is stopped in iscsit_logout_post_handler_diffcid().
2792 */
2793
2794 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2795 " successful.\n", cmd->logout_cid, conn->cid);
2796 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2797 break;
2798 case ISCSI_LOGOUT_REASON_RECOVERY:
2799 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2800 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2801 break;
2802 /*
2803 * If the connection is still active from our point of view
2804 * force connection recovery to occur.
2805 */
2806 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2807 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002808 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002809 iscsit_connection_reinstatement_rcfr(logout_conn);
2810 iscsit_dec_conn_usage_count(logout_conn);
2811 }
2812
2813 cr = iscsit_get_inactive_connection_recovery_entry(
2814 conn->sess, cmd->logout_cid);
2815 if (!cr) {
2816 pr_err("Unable to locate CID: %hu for"
2817 " REMOVECONNFORRECOVERY Logout Request.\n",
2818 cmd->logout_cid);
2819 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2820 break;
2821 }
2822
2823 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2824
2825 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2826 " for recovery for CID: %hu on CID: %hu successful.\n",
2827 cmd->logout_cid, conn->cid);
2828 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2829 break;
2830 default:
2831 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2832 cmd->logout_reason);
2833 return -1;
2834 }
2835
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002836 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2837 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2838 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002839 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002840 cmd->stat_sn = conn->stat_sn++;
2841 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2842
2843 iscsit_increment_maxcmdsn(cmd, conn->sess);
2844 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2845 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2846
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002847 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2848 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2849 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2850 cmd->logout_cid, conn->cid);
2851
2852 return 0;
2853}
2854EXPORT_SYMBOL(iscsit_build_logout_rsp);
2855
2856static int
2857iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2858{
2859 struct kvec *iov;
2860 int niov = 0, tx_size, rc;
2861
2862 rc = iscsit_build_logout_rsp(cmd, conn,
2863 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2864 if (rc < 0)
2865 return rc;
2866
2867 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002868 iov = &cmd->iov_misc[0];
2869 iov[niov].iov_base = cmd->pdu;
2870 iov[niov++].iov_len = ISCSI_HDR_LEN;
2871
2872 if (conn->conn_ops->HeaderDigest) {
2873 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2874
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002875 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2876 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002877
2878 iov[0].iov_len += ISCSI_CRC_LEN;
2879 tx_size += ISCSI_CRC_LEN;
2880 pr_debug("Attaching CRC32C HeaderDigest to"
2881 " Logout Response 0x%08x\n", *header_digest);
2882 }
2883 cmd->iov_misc_count = niov;
2884 cmd->tx_size = tx_size;
2885
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002886 return 0;
2887}
2888
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002889void
2890iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2891 struct iscsi_nopin *hdr, bool nopout_response)
2892{
2893 hdr->opcode = ISCSI_OP_NOOP_IN;
2894 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2895 hton24(hdr->dlength, cmd->buf_ptr_size);
2896 if (nopout_response)
2897 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2898 hdr->itt = cmd->init_task_tag;
2899 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2900 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2901 conn->stat_sn;
2902 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2903
2904 if (nopout_response)
2905 iscsit_increment_maxcmdsn(cmd, conn->sess);
2906
2907 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2908 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2909
2910 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2911 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2912 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2913 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2914}
2915EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2916
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002917/*
2918 * Unsolicited NOPIN, either requesting a response or not.
2919 */
2920static int iscsit_send_unsolicited_nopin(
2921 struct iscsi_cmd *cmd,
2922 struct iscsi_conn *conn,
2923 int want_response)
2924{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002925 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2926 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002927
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002928 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002929
2930 if (conn->conn_ops->HeaderDigest) {
2931 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2932
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002933 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2934 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002935
2936 tx_size += ISCSI_CRC_LEN;
2937 pr_debug("Attaching CRC32C HeaderDigest to"
2938 " NopIN 0x%08x\n", *header_digest);
2939 }
2940
2941 cmd->iov_misc[0].iov_base = cmd->pdu;
2942 cmd->iov_misc[0].iov_len = tx_size;
2943 cmd->iov_misc_count = 1;
2944 cmd->tx_size = tx_size;
2945
2946 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2947 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2948
Andy Grover6f3c0e62012-04-03 15:51:09 -07002949 ret = iscsit_send_tx_data(cmd, conn, 1);
2950 if (ret < 0) {
2951 iscsit_tx_thread_wait_for_tcp(conn);
2952 return ret;
2953 }
2954
2955 spin_lock_bh(&cmd->istate_lock);
2956 cmd->i_state = want_response ?
2957 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2958 spin_unlock_bh(&cmd->istate_lock);
2959
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002960 return 0;
2961}
2962
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002963static int
2964iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002965{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002966 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002967 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002968 u32 padding = 0;
2969 int niov = 0, tx_size;
2970
2971 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002972
2973 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002974 iov = &cmd->iov_misc[0];
2975 iov[niov].iov_base = cmd->pdu;
2976 iov[niov++].iov_len = ISCSI_HDR_LEN;
2977
2978 if (conn->conn_ops->HeaderDigest) {
2979 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2980
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002981 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2982 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002983
2984 iov[0].iov_len += ISCSI_CRC_LEN;
2985 tx_size += ISCSI_CRC_LEN;
2986 pr_debug("Attaching CRC32C HeaderDigest"
2987 " to NopIn 0x%08x\n", *header_digest);
2988 }
2989
2990 /*
2991 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2992 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2993 */
2994 if (cmd->buf_ptr_size) {
2995 iov[niov].iov_base = cmd->buf_ptr;
2996 iov[niov++].iov_len = cmd->buf_ptr_size;
2997 tx_size += cmd->buf_ptr_size;
2998
2999 pr_debug("Echoing back %u bytes of ping"
3000 " data.\n", cmd->buf_ptr_size);
3001
3002 padding = ((-cmd->buf_ptr_size) & 3);
3003 if (padding != 0) {
3004 iov[niov].iov_base = &cmd->pad_bytes;
3005 iov[niov++].iov_len = padding;
3006 tx_size += padding;
3007 pr_debug("Attaching %u additional"
3008 " padding bytes.\n", padding);
3009 }
3010 if (conn->conn_ops->DataDigest) {
3011 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3012 cmd->buf_ptr, cmd->buf_ptr_size,
3013 padding, (u8 *)&cmd->pad_bytes,
3014 (u8 *)&cmd->data_crc);
3015
3016 iov[niov].iov_base = &cmd->data_crc;
3017 iov[niov++].iov_len = ISCSI_CRC_LEN;
3018 tx_size += ISCSI_CRC_LEN;
3019 pr_debug("Attached DataDigest for %u"
3020 " bytes of ping data, CRC 0x%08x\n",
3021 cmd->buf_ptr_size, cmd->data_crc);
3022 }
3023 }
3024
3025 cmd->iov_misc_count = niov;
3026 cmd->tx_size = tx_size;
3027
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 return 0;
3029}
3030
Andy Grover6f3c0e62012-04-03 15:51:09 -07003031static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003032 struct iscsi_cmd *cmd,
3033 struct iscsi_conn *conn)
3034{
3035 int tx_size = 0;
3036 struct iscsi_r2t *r2t;
3037 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003038 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003039
3040 r2t = iscsit_get_r2t_from_list(cmd);
3041 if (!r2t)
3042 return -1;
3043
3044 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3045 memset(hdr, 0, ISCSI_HDR_LEN);
3046 hdr->opcode = ISCSI_OP_R2T;
3047 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3048 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3049 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003050 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003051 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003052 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3053 hdr->statsn = cpu_to_be32(conn->stat_sn);
3054 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3055 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3056 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3057 hdr->data_offset = cpu_to_be32(r2t->offset);
3058 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3059
3060 cmd->iov_misc[0].iov_base = cmd->pdu;
3061 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3062 tx_size += ISCSI_HDR_LEN;
3063
3064 if (conn->conn_ops->HeaderDigest) {
3065 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3066
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003067 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3068 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003069
3070 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3071 tx_size += ISCSI_CRC_LEN;
3072 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3073 " PDU 0x%08x\n", *header_digest);
3074 }
3075
3076 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3077 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3078 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3079 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3080 r2t->offset, r2t->xfer_len, conn->cid);
3081
3082 cmd->iov_misc_count = 1;
3083 cmd->tx_size = tx_size;
3084
3085 spin_lock_bh(&cmd->r2t_lock);
3086 r2t->sent_r2t = 1;
3087 spin_unlock_bh(&cmd->r2t_lock);
3088
Andy Grover6f3c0e62012-04-03 15:51:09 -07003089 ret = iscsit_send_tx_data(cmd, conn, 1);
3090 if (ret < 0) {
3091 iscsit_tx_thread_wait_for_tcp(conn);
3092 return ret;
3093 }
3094
3095 spin_lock_bh(&cmd->dataout_timeout_lock);
3096 iscsit_start_dataout_timer(cmd, conn);
3097 spin_unlock_bh(&cmd->dataout_timeout_lock);
3098
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 return 0;
3100}
3101
3102/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003103 * @recovery: If called from iscsi_task_reassign_complete_write() for
3104 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003105 */
3106int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003108 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003109 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003110{
3111 int first_r2t = 1;
3112 u32 offset = 0, xfer_len = 0;
3113
3114 spin_lock_bh(&cmd->r2t_lock);
3115 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3116 spin_unlock_bh(&cmd->r2t_lock);
3117 return 0;
3118 }
3119
Andy Grover8b1e1242012-04-03 15:51:12 -07003120 if (conn->sess->sess_ops->DataSequenceInOrder &&
3121 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003122 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003123
3124 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3125 if (conn->sess->sess_ops->DataSequenceInOrder) {
3126 offset = cmd->r2t_offset;
3127
Andy Grover8b1e1242012-04-03 15:51:12 -07003128 if (first_r2t && recovery) {
3129 int new_data_end = offset +
3130 conn->sess->sess_ops->MaxBurstLength -
3131 cmd->next_burst_len;
3132
Andy Groverebf1d952012-04-03 15:51:24 -07003133 if (new_data_end > cmd->se_cmd.data_length)
3134 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003135 else
3136 xfer_len =
3137 conn->sess->sess_ops->MaxBurstLength -
3138 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003139 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003140 int new_data_end = offset +
3141 conn->sess->sess_ops->MaxBurstLength;
3142
Andy Groverebf1d952012-04-03 15:51:24 -07003143 if (new_data_end > cmd->se_cmd.data_length)
3144 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003145 else
3146 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003147 }
3148 cmd->r2t_offset += xfer_len;
3149
Andy Groverebf1d952012-04-03 15:51:24 -07003150 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003151 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3152 } else {
3153 struct iscsi_seq *seq;
3154
3155 seq = iscsit_get_seq_holder_for_r2t(cmd);
3156 if (!seq) {
3157 spin_unlock_bh(&cmd->r2t_lock);
3158 return -1;
3159 }
3160
3161 offset = seq->offset;
3162 xfer_len = seq->xfer_len;
3163
3164 if (cmd->seq_send_order == cmd->seq_count)
3165 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3166 }
3167 cmd->outstanding_r2ts++;
3168 first_r2t = 0;
3169
3170 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3171 spin_unlock_bh(&cmd->r2t_lock);
3172 return -1;
3173 }
3174
3175 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3176 break;
3177 }
3178 spin_unlock_bh(&cmd->r2t_lock);
3179
3180 return 0;
3181}
3182
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003183void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3184 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003186 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003187 cmd->stat_sn = conn->stat_sn++;
3188
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003189 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003190
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191 memset(hdr, 0, ISCSI_HDR_LEN);
3192 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3193 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3194 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3195 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003196 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3198 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003199 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003200 }
3201 hdr->response = cmd->iscsi_response;
3202 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003203 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003204 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3205
3206 iscsit_increment_maxcmdsn(cmd, conn->sess);
3207 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3208 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3209
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003210 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3211 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3212 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3213 cmd->se_cmd.scsi_status, conn->cid);
3214}
3215EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3216
3217static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3218{
3219 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3220 struct kvec *iov;
3221 u32 padding = 0, tx_size = 0;
3222 int iov_count = 0;
3223 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3224
3225 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3226
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003227 iov = &cmd->iov_misc[0];
3228 iov[iov_count].iov_base = cmd->pdu;
3229 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3230 tx_size += ISCSI_HDR_LEN;
3231
3232 /*
3233 * Attach SENSE DATA payload to iSCSI Response PDU
3234 */
3235 if (cmd->se_cmd.sense_buffer &&
3236 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3237 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003238 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3239 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3240
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003241 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003242 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003243 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003244 iov[iov_count++].iov_len =
3245 (cmd->se_cmd.scsi_sense_length + padding);
3246 tx_size += cmd->se_cmd.scsi_sense_length;
3247
3248 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003249 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003250 cmd->se_cmd.scsi_sense_length, 0, padding);
3251 tx_size += padding;
3252 pr_debug("Adding %u bytes of padding to"
3253 " SENSE.\n", padding);
3254 }
3255
3256 if (conn->conn_ops->DataDigest) {
3257 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003258 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003259 (cmd->se_cmd.scsi_sense_length + padding),
3260 0, NULL, (u8 *)&cmd->data_crc);
3261
3262 iov[iov_count].iov_base = &cmd->data_crc;
3263 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3264 tx_size += ISCSI_CRC_LEN;
3265
3266 pr_debug("Attaching CRC32 DataDigest for"
3267 " SENSE, %u bytes CRC 0x%08x\n",
3268 (cmd->se_cmd.scsi_sense_length + padding),
3269 cmd->data_crc);
3270 }
3271
3272 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3273 " Response PDU\n",
3274 cmd->se_cmd.scsi_sense_length);
3275 }
3276
3277 if (conn->conn_ops->HeaderDigest) {
3278 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3279
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003280 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3281 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003282
3283 iov[0].iov_len += ISCSI_CRC_LEN;
3284 tx_size += ISCSI_CRC_LEN;
3285 pr_debug("Attaching CRC32 HeaderDigest for Response"
3286 " PDU 0x%08x\n", *header_digest);
3287 }
3288
3289 cmd->iov_misc_count = iov_count;
3290 cmd->tx_size = tx_size;
3291
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292 return 0;
3293}
3294
3295static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3296{
3297 switch (se_tmr->response) {
3298 case TMR_FUNCTION_COMPLETE:
3299 return ISCSI_TMF_RSP_COMPLETE;
3300 case TMR_TASK_DOES_NOT_EXIST:
3301 return ISCSI_TMF_RSP_NO_TASK;
3302 case TMR_LUN_DOES_NOT_EXIST:
3303 return ISCSI_TMF_RSP_NO_LUN;
3304 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3305 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003306 case TMR_FUNCTION_REJECTED:
3307 default:
3308 return ISCSI_TMF_RSP_REJECTED;
3309 }
3310}
3311
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003312void
3313iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3314 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003315{
3316 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003317
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003318 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003319 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003320 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003321 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003322 cmd->stat_sn = conn->stat_sn++;
3323 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3324
3325 iscsit_increment_maxcmdsn(cmd, conn->sess);
3326 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3327 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3328
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003329 pr_debug("Built Task Management Response ITT: 0x%08x,"
3330 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3331 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3332}
3333EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3334
3335static int
3336iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3337{
3338 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3339 u32 tx_size = 0;
3340
3341 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3342
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003343 cmd->iov_misc[0].iov_base = cmd->pdu;
3344 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3345 tx_size += ISCSI_HDR_LEN;
3346
3347 if (conn->conn_ops->HeaderDigest) {
3348 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3349
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003350 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3351 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003352
3353 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3354 tx_size += ISCSI_CRC_LEN;
3355 pr_debug("Attaching CRC32 HeaderDigest for Task"
3356 " Mgmt Response PDU 0x%08x\n", *header_digest);
3357 }
3358
3359 cmd->iov_misc_count = 1;
3360 cmd->tx_size = tx_size;
3361
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003362 return 0;
3363}
3364
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003365static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3366{
3367 bool ret = false;
3368
3369 if (np->np_sockaddr.ss_family == AF_INET6) {
3370 const struct sockaddr_in6 sin6 = {
3371 .sin6_addr = IN6ADDR_ANY_INIT };
3372 struct sockaddr_in6 *sock_in6 =
3373 (struct sockaddr_in6 *)&np->np_sockaddr;
3374
3375 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3376 sin6.sin6_addr.s6_addr, 16))
3377 ret = true;
3378 } else {
3379 struct sockaddr_in * sock_in =
3380 (struct sockaddr_in *)&np->np_sockaddr;
3381
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003382 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003383 ret = true;
3384 }
3385
3386 return ret;
3387}
3388
Andy Grover8b1e1242012-04-03 15:51:12 -07003389#define SENDTARGETS_BUF_LIMIT 32768U
3390
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003391static int
3392iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003393 enum iscsit_transport_type network_transport,
3394 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003395{
3396 char *payload = NULL;
3397 struct iscsi_conn *conn = cmd->conn;
3398 struct iscsi_portal_group *tpg;
3399 struct iscsi_tiqn *tiqn;
3400 struct iscsi_tpg_np *tpg_np;
3401 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003402 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003403 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003404 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003405
Sagi Grimbergbe7dcfb62015-01-26 12:49:06 +02003406 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003407 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003408
3409 payload = kzalloc(buffer_len, GFP_KERNEL);
3410 if (!payload) {
3411 pr_err("Unable to allocate memory for sendtargets"
3412 " response.\n");
3413 return -ENOMEM;
3414 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003415 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003416 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003417 * explicit case..
3418 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003419 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003420 text_ptr = strchr(text_in, '=');
3421 if (!text_ptr) {
3422 pr_err("Unable to locate '=' string in text_in:"
3423 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003424 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003425 return -EINVAL;
3426 }
3427 /*
3428 * Skip over '=' character..
3429 */
3430 text_ptr += 1;
3431 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003432
3433 spin_lock(&tiqn_lock);
3434 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003435 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003436 strcmp(tiqn->tiqn, text_ptr)) {
3437 continue;
3438 }
3439
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003440 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003441
3442 spin_lock(&tiqn->tiqn_tpg_lock);
3443 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3444
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003445 /* If demo_mode_discovery=0 and generate_node_acls=0
3446 * (demo mode dislabed) do not return
3447 * TargetName+TargetAddress unless a NodeACL exists.
3448 */
3449
3450 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3451 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3452 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3453 cmd->conn->sess->sess_ops->InitiatorName))) {
3454 continue;
3455 }
3456
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003457 spin_lock(&tpg->tpg_state_lock);
3458 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3459 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3460 spin_unlock(&tpg->tpg_state_lock);
3461 continue;
3462 }
3463 spin_unlock(&tpg->tpg_state_lock);
3464
3465 spin_lock(&tpg->tpg_np_lock);
3466 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3467 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003468 struct iscsi_np *np = tpg_np->tpg_np;
3469 bool inaddr_any = iscsit_check_inaddr_any(np);
3470
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003471 if (np->np_network_transport != network_transport)
3472 continue;
3473
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003474 if (!target_name_printed) {
3475 len = sprintf(buf, "TargetName=%s",
3476 tiqn->tiqn);
3477 len += 1;
3478
3479 if ((len + payload_len) > buffer_len) {
3480 spin_unlock(&tpg->tpg_np_lock);
3481 spin_unlock(&tiqn->tiqn_tpg_lock);
3482 end_of_buf = 1;
3483 goto eob;
3484 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003485
3486 if (skip_bytes && len <= skip_bytes) {
3487 skip_bytes -= len;
3488 } else {
3489 memcpy(payload + payload_len, buf, len);
3490 payload_len += len;
3491 target_name_printed = 1;
3492 if (len > skip_bytes)
3493 skip_bytes = 0;
3494 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003495 }
3496
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003497 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003498 "%s:%hu,%hu",
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02003499 inaddr_any ? conn->local_ip : np->np_ip,
Steven Allenf2774f42014-10-15 10:59:21 -07003500 np->np_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003501 tpg->tpgt);
3502 len += 1;
3503
3504 if ((len + payload_len) > buffer_len) {
3505 spin_unlock(&tpg->tpg_np_lock);
3506 spin_unlock(&tiqn->tiqn_tpg_lock);
3507 end_of_buf = 1;
3508 goto eob;
3509 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003510
3511 if (skip_bytes && len <= skip_bytes) {
3512 skip_bytes -= len;
3513 } else {
3514 memcpy(payload + payload_len, buf, len);
3515 payload_len += len;
3516 if (len > skip_bytes)
3517 skip_bytes = 0;
3518 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003519 }
3520 spin_unlock(&tpg->tpg_np_lock);
3521 }
3522 spin_unlock(&tiqn->tiqn_tpg_lock);
3523eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003524 if (end_of_buf) {
3525 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003527 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003528
Andy Grover8060b8d2015-01-09 15:13:08 -08003529 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003530 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003531 }
3532 spin_unlock(&tiqn_lock);
3533
3534 cmd->buf_ptr = payload;
3535
3536 return payload_len;
3537}
3538
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003539int
3540iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003541 struct iscsi_text_rsp *hdr,
3542 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003543{
3544 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003545 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003546
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003547 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3548 cmd->read_data_done,
3549 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003550 if (text_length < 0)
3551 return text_length;
3552
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003553 if (completed) {
3554 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3555 } else {
3556 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3557 cmd->read_data_done += text_length;
3558 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3559 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3560 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003561 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003562 padding = ((-text_length) & 3);
3563 hton24(hdr->dlength, text_length);
3564 hdr->itt = cmd->init_task_tag;
3565 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3566 cmd->stat_sn = conn->stat_sn++;
3567 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3568
3569 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003570 /*
3571 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3572 * correctly increment MaxCmdSN for each response answering a
3573 * non immediate text request with a valid CmdSN.
3574 */
3575 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003576 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3577 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3578
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003579 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3580 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3581 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3582 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3583 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003584
3585 return text_length + padding;
3586}
3587EXPORT_SYMBOL(iscsit_build_text_rsp);
3588
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003589static int iscsit_send_text_rsp(
3590 struct iscsi_cmd *cmd,
3591 struct iscsi_conn *conn)
3592{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003593 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003594 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003595 u32 tx_size = 0;
3596 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003597
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003598 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003599 if (rc < 0)
3600 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003601
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003602 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003604 iov[iov_count].iov_base = cmd->pdu;
3605 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3606 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003607 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003608
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003609 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003610
3611 if (conn->conn_ops->HeaderDigest) {
3612 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3613
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003614 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3615 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003616
3617 iov[0].iov_len += ISCSI_CRC_LEN;
3618 tx_size += ISCSI_CRC_LEN;
3619 pr_debug("Attaching CRC32 HeaderDigest for"
3620 " Text Response PDU 0x%08x\n", *header_digest);
3621 }
3622
3623 if (conn->conn_ops->DataDigest) {
3624 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003625 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003626 0, NULL, (u8 *)&cmd->data_crc);
3627
3628 iov[iov_count].iov_base = &cmd->data_crc;
3629 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3630 tx_size += ISCSI_CRC_LEN;
3631
3632 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003633 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003634 cmd->data_crc);
3635 }
3636
3637 cmd->iov_misc_count = iov_count;
3638 cmd->tx_size = tx_size;
3639
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003640 return 0;
3641}
3642
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003643void
3644iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3645 struct iscsi_reject *hdr)
3646{
3647 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003648 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003649 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3650 hton24(hdr->dlength, ISCSI_HDR_LEN);
3651 hdr->ffffffff = cpu_to_be32(0xffffffff);
3652 cmd->stat_sn = conn->stat_sn++;
3653 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3654 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3655 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3656
3657}
3658EXPORT_SYMBOL(iscsit_build_reject);
3659
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003660static int iscsit_send_reject(
3661 struct iscsi_cmd *cmd,
3662 struct iscsi_conn *conn)
3663{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003664 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003665 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003666 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003667
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003668 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003669
3670 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003671 iov[iov_count].iov_base = cmd->pdu;
3672 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3673 iov[iov_count].iov_base = cmd->buf_ptr;
3674 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3675
3676 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3677
3678 if (conn->conn_ops->HeaderDigest) {
3679 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3680
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003681 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3682 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003683
3684 iov[0].iov_len += ISCSI_CRC_LEN;
3685 tx_size += ISCSI_CRC_LEN;
3686 pr_debug("Attaching CRC32 HeaderDigest for"
3687 " REJECT PDU 0x%08x\n", *header_digest);
3688 }
3689
3690 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003691 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3692 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003693
3694 iov[iov_count].iov_base = &cmd->data_crc;
3695 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3696 tx_size += ISCSI_CRC_LEN;
3697 pr_debug("Attaching CRC32 DataDigest for REJECT"
3698 " PDU 0x%08x\n", cmd->data_crc);
3699 }
3700
3701 cmd->iov_misc_count = iov_count;
3702 cmd->tx_size = tx_size;
3703
3704 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3705 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3706
3707 return 0;
3708}
3709
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003710void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3711{
3712 struct iscsi_thread_set *ts = conn->thread_set;
3713 int ord, cpu;
3714 /*
3715 * thread_id is assigned from iscsit_global->ts_bitmap from
3716 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3717 *
3718 * Here we use thread_id to determine which CPU that this
3719 * iSCSI connection's iscsi_thread_set will be scheduled to
3720 * execute upon.
3721 */
3722 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003723 for_each_online_cpu(cpu) {
3724 if (ord-- == 0) {
3725 cpumask_set_cpu(cpu, conn->conn_cpumask);
3726 return;
3727 }
3728 }
3729 /*
3730 * This should never be reached..
3731 */
3732 dump_stack();
3733 cpumask_setall(conn->conn_cpumask);
3734}
3735
3736static inline void iscsit_thread_check_cpumask(
3737 struct iscsi_conn *conn,
3738 struct task_struct *p,
3739 int mode)
3740{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003741 /*
3742 * mode == 1 signals iscsi_target_tx_thread() usage.
3743 * mode == 0 signals iscsi_target_rx_thread() usage.
3744 */
3745 if (mode == 1) {
3746 if (!conn->conn_tx_reset_cpumask)
3747 return;
3748 conn->conn_tx_reset_cpumask = 0;
3749 } else {
3750 if (!conn->conn_rx_reset_cpumask)
3751 return;
3752 conn->conn_rx_reset_cpumask = 0;
3753 }
3754 /*
3755 * Update the CPU mask for this single kthread so that
3756 * both TX and RX kthreads are scheduled to run on the
3757 * same CPU.
3758 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003759 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3760}
3761
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003762static int
3763iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003764{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003765 int ret;
3766
3767 switch (state) {
3768 case ISTATE_SEND_R2T:
3769 ret = iscsit_send_r2t(cmd, conn);
3770 if (ret < 0)
3771 goto err;
3772 break;
3773 case ISTATE_REMOVE:
3774 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003775 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003776 spin_unlock_bh(&conn->cmd_lock);
3777
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003778 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003779 break;
3780 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3781 iscsit_mod_nopin_response_timer(conn);
3782 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3783 if (ret < 0)
3784 goto err;
3785 break;
3786 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3787 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3788 if (ret < 0)
3789 goto err;
3790 break;
3791 default:
3792 pr_err("Unknown Opcode: 0x%02x ITT:"
3793 " 0x%08x, i_state: %d on CID: %hu\n",
3794 cmd->iscsi_opcode, cmd->init_task_tag, state,
3795 conn->cid);
3796 goto err;
3797 }
3798
3799 return 0;
3800
3801err:
3802 return -1;
3803}
3804
3805static int
3806iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3807{
3808 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003809 struct iscsi_queue_req *qr;
3810 struct iscsi_cmd *cmd;
3811 u8 state;
3812 int ret;
3813
3814 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3815 atomic_set(&conn->check_immediate_queue, 0);
3816 cmd = qr->cmd;
3817 state = qr->state;
3818 kmem_cache_free(lio_qr_cache, qr);
3819
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003820 ret = t->iscsit_immediate_queue(conn, cmd, state);
3821 if (ret < 0)
3822 return ret;
3823 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003824
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003825 return 0;
3826}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003827
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003828static int
3829iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3830{
3831 int ret;
3832
3833check_rsp_state:
3834 switch (state) {
3835 case ISTATE_SEND_DATAIN:
3836 ret = iscsit_send_datain(cmd, conn);
3837 if (ret < 0)
3838 goto err;
3839 else if (!ret)
3840 /* more drs */
3841 goto check_rsp_state;
3842 else if (ret == 1) {
3843 /* all done */
3844 spin_lock_bh(&cmd->istate_lock);
3845 cmd->i_state = ISTATE_SENT_STATUS;
3846 spin_unlock_bh(&cmd->istate_lock);
3847
3848 if (atomic_read(&conn->check_immediate_queue))
3849 return 1;
3850
3851 return 0;
3852 } else if (ret == 2) {
3853 /* Still must send status,
3854 SCF_TRANSPORT_TASK_SENSE was set */
3855 spin_lock_bh(&cmd->istate_lock);
3856 cmd->i_state = ISTATE_SEND_STATUS;
3857 spin_unlock_bh(&cmd->istate_lock);
3858 state = ISTATE_SEND_STATUS;
3859 goto check_rsp_state;
3860 }
3861
3862 break;
3863 case ISTATE_SEND_STATUS:
3864 case ISTATE_SEND_STATUS_RECOVERY:
3865 ret = iscsit_send_response(cmd, conn);
3866 break;
3867 case ISTATE_SEND_LOGOUTRSP:
3868 ret = iscsit_send_logout(cmd, conn);
3869 break;
3870 case ISTATE_SEND_ASYNCMSG:
3871 ret = iscsit_send_conn_drop_async_message(
3872 cmd, conn);
3873 break;
3874 case ISTATE_SEND_NOPIN:
3875 ret = iscsit_send_nopin(cmd, conn);
3876 break;
3877 case ISTATE_SEND_REJECT:
3878 ret = iscsit_send_reject(cmd, conn);
3879 break;
3880 case ISTATE_SEND_TASKMGTRSP:
3881 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3882 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003883 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003884 ret = iscsit_tmr_post_handler(cmd, conn);
3885 if (ret != 0)
3886 iscsit_fall_back_to_erl0(conn->sess);
3887 break;
3888 case ISTATE_SEND_TEXTRSP:
3889 ret = iscsit_send_text_rsp(cmd, conn);
3890 break;
3891 default:
3892 pr_err("Unknown Opcode: 0x%02x ITT:"
3893 " 0x%08x, i_state: %d on CID: %hu\n",
3894 cmd->iscsi_opcode, cmd->init_task_tag,
3895 state, conn->cid);
3896 goto err;
3897 }
3898 if (ret < 0)
3899 goto err;
3900
3901 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3902 iscsit_tx_thread_wait_for_tcp(conn);
3903 iscsit_unmap_iovec(cmd);
3904 goto err;
3905 }
3906 iscsit_unmap_iovec(cmd);
3907
3908 switch (state) {
3909 case ISTATE_SEND_LOGOUTRSP:
3910 if (!iscsit_logout_post_handler(cmd, conn))
3911 goto restart;
3912 /* fall through */
3913 case ISTATE_SEND_STATUS:
3914 case ISTATE_SEND_ASYNCMSG:
3915 case ISTATE_SEND_NOPIN:
3916 case ISTATE_SEND_STATUS_RECOVERY:
3917 case ISTATE_SEND_TEXTRSP:
3918 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003919 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003920 spin_lock_bh(&cmd->istate_lock);
3921 cmd->i_state = ISTATE_SENT_STATUS;
3922 spin_unlock_bh(&cmd->istate_lock);
3923 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003924 default:
3925 pr_err("Unknown Opcode: 0x%02x ITT:"
3926 " 0x%08x, i_state: %d on CID: %hu\n",
3927 cmd->iscsi_opcode, cmd->init_task_tag,
3928 cmd->i_state, conn->cid);
3929 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003930 }
3931
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003932 if (atomic_read(&conn->check_immediate_queue))
3933 return 1;
3934
Andy Grover6f3c0e62012-04-03 15:51:09 -07003935 return 0;
3936
3937err:
3938 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003939restart:
3940 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003941}
3942
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003943static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003944{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003945 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003946 struct iscsi_queue_req *qr;
3947 struct iscsi_cmd *cmd;
3948 u8 state;
3949 int ret;
3950
3951 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3952 cmd = qr->cmd;
3953 state = qr->state;
3954 kmem_cache_free(lio_qr_cache, qr);
3955
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003956 ret = t->iscsit_response_queue(conn, cmd, state);
3957 if (ret == 1 || ret < 0)
3958 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003959 }
3960
3961 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003962}
3963
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003964int iscsi_target_tx_thread(void *arg)
3965{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003966 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003967 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003968 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003969 /*
3970 * Allow ourselves to be interrupted by SIGINT so that a
3971 * connection recovery / failure event can be triggered externally.
3972 */
3973 allow_signal(SIGINT);
3974
3975restart:
3976 conn = iscsi_tx_thread_pre_handler(ts);
3977 if (!conn)
3978 goto out;
3979
Andy Grover6f3c0e62012-04-03 15:51:09 -07003980 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003981
3982 while (!kthread_should_stop()) {
3983 /*
3984 * Ensure that both TX and RX per connection kthreads
3985 * are scheduled to run on the same CPU.
3986 */
3987 iscsit_thread_check_cpumask(conn, current, 1);
3988
Roland Dreierd5627ac2012-10-31 09:16:46 -07003989 wait_event_interruptible(conn->queues_wq,
3990 !iscsit_conn_all_queues_empty(conn) ||
3991 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003992
3993 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3994 signal_pending(current))
3995 goto transport_err;
3996
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003997get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003998 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003999 if (ret < 0)
4000 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004001
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004002 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08004003 if (ret == 1)
4004 goto get_immediate;
4005 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07004006 goto restart;
4007 else if (ret < 0)
4008 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004009 }
4010
4011transport_err:
4012 iscsit_take_action_for_connection_exit(conn);
4013 goto restart;
4014out:
4015 return 0;
4016}
4017
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004018static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4019{
4020 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4021 struct iscsi_cmd *cmd;
4022 int ret = 0;
4023
4024 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4025 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004026 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004027 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004028 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004029
4030 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4031 break;
4032 case ISCSI_OP_SCSI_DATA_OUT:
4033 ret = iscsit_handle_data_out(conn, buf);
4034 break;
4035 case ISCSI_OP_NOOP_OUT:
4036 cmd = NULL;
4037 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004038 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004039 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004040 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004041 }
4042 ret = iscsit_handle_nop_out(conn, cmd, buf);
4043 break;
4044 case ISCSI_OP_SCSI_TMFUNC:
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_task_mgt_cmd(conn, cmd, buf);
4050 break;
4051 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02004052 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4053 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4054 if (!cmd)
4055 goto reject;
4056 } else {
4057 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4058 if (!cmd)
4059 goto reject;
4060 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004061
4062 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004063 break;
4064 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004065 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004066 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004067 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004068
4069 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4070 if (ret > 0)
4071 wait_for_completion_timeout(&conn->conn_logout_comp,
4072 SECONDS_FOR_LOGOUT_COMP * HZ);
4073 break;
4074 case ISCSI_OP_SNACK:
4075 ret = iscsit_handle_snack(conn, buf);
4076 break;
4077 default:
4078 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4079 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4080 pr_err("Cannot recover from unknown"
4081 " opcode while ERL=0, closing iSCSI connection.\n");
4082 return -1;
4083 }
4084 if (!conn->conn_ops->OFMarker) {
4085 pr_err("Unable to recover from unknown"
4086 " opcode while OFMarker=No, closing iSCSI"
4087 " connection.\n");
4088 return -1;
4089 }
4090 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4091 pr_err("Unable to recover from unknown"
4092 " opcode, closing iSCSI connection.\n");
4093 return -1;
4094 }
4095 break;
4096 }
4097
4098 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004099reject:
4100 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004101}
4102
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004103int iscsi_target_rx_thread(void *arg)
4104{
4105 int ret;
4106 u8 buffer[ISCSI_HDR_LEN], opcode;
4107 u32 checksum = 0, digest = 0;
4108 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004109 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004110 struct kvec iov;
4111 /*
4112 * Allow ourselves to be interrupted by SIGINT so that a
4113 * connection recovery / failure event can be triggered externally.
4114 */
4115 allow_signal(SIGINT);
4116
4117restart:
4118 conn = iscsi_rx_thread_pre_handler(ts);
4119 if (!conn)
4120 goto out;
4121
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004122 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4123 struct completion comp;
4124 int rc;
4125
4126 init_completion(&comp);
4127 rc = wait_for_completion_interruptible(&comp);
4128 if (rc < 0)
4129 goto transport_err;
4130
4131 goto out;
4132 }
4133
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004134 while (!kthread_should_stop()) {
4135 /*
4136 * Ensure that both TX and RX per connection kthreads
4137 * are scheduled to run on the same CPU.
4138 */
4139 iscsit_thread_check_cpumask(conn, current, 0);
4140
4141 memset(buffer, 0, ISCSI_HDR_LEN);
4142 memset(&iov, 0, sizeof(struct kvec));
4143
4144 iov.iov_base = buffer;
4145 iov.iov_len = ISCSI_HDR_LEN;
4146
4147 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4148 if (ret != ISCSI_HDR_LEN) {
4149 iscsit_rx_thread_wait_for_tcp(conn);
4150 goto transport_err;
4151 }
4152
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004153 if (conn->conn_ops->HeaderDigest) {
4154 iov.iov_base = &digest;
4155 iov.iov_len = ISCSI_CRC_LEN;
4156
4157 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4158 if (ret != ISCSI_CRC_LEN) {
4159 iscsit_rx_thread_wait_for_tcp(conn);
4160 goto transport_err;
4161 }
4162
4163 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4164 buffer, ISCSI_HDR_LEN,
4165 0, NULL, (u8 *)&checksum);
4166
4167 if (digest != checksum) {
4168 pr_err("HeaderDigest CRC32C failed,"
4169 " received 0x%08x, computed 0x%08x\n",
4170 digest, checksum);
4171 /*
4172 * Set the PDU to 0xff so it will intentionally
4173 * hit default in the switch below.
4174 */
4175 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004176 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004177 } else {
4178 pr_debug("Got HeaderDigest CRC32C"
4179 " 0x%08x\n", checksum);
4180 }
4181 }
4182
4183 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4184 goto transport_err;
4185
4186 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4187
4188 if (conn->sess->sess_ops->SessionType &&
4189 ((!(opcode & ISCSI_OP_TEXT)) ||
4190 (!(opcode & ISCSI_OP_LOGOUT)))) {
4191 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4192 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004193 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4194 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004195 goto transport_err;
4196 }
4197
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004198 ret = iscsi_target_rx_opcode(conn, buffer);
4199 if (ret < 0)
4200 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004201 }
4202
4203transport_err:
4204 if (!signal_pending(current))
4205 atomic_set(&conn->transport_failed, 1);
4206 iscsit_take_action_for_connection_exit(conn);
4207 goto restart;
4208out:
4209 return 0;
4210}
4211
4212static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4213{
4214 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4215 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004216 /*
4217 * We expect this function to only ever be called from either RX or TX
4218 * thread context via iscsit_close_connection() once the other context
4219 * has been reset -> returned sleeping pre-handler state.
4220 */
4221 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004222 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004223
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004224 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004225 spin_unlock_bh(&conn->cmd_lock);
4226
4227 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004228
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004229 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004230
4231 spin_lock_bh(&conn->cmd_lock);
4232 }
4233 spin_unlock_bh(&conn->cmd_lock);
4234}
4235
4236static void iscsit_stop_timers_for_cmds(
4237 struct iscsi_conn *conn)
4238{
4239 struct iscsi_cmd *cmd;
4240
4241 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004242 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004243 if (cmd->data_direction == DMA_TO_DEVICE)
4244 iscsit_stop_dataout_timer(cmd);
4245 }
4246 spin_unlock_bh(&conn->cmd_lock);
4247}
4248
4249int iscsit_close_connection(
4250 struct iscsi_conn *conn)
4251{
4252 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4253 struct iscsi_session *sess = conn->sess;
4254
4255 pr_debug("Closing iSCSI connection CID %hu on SID:"
4256 " %u\n", conn->cid, sess->sid);
4257 /*
4258 * Always up conn_logout_comp just in case the RX Thread is sleeping
4259 * and the logout response never got sent because the connection
4260 * failed.
4261 */
4262 complete(&conn->conn_logout_comp);
4263
4264 iscsi_release_thread_set(conn);
4265
4266 iscsit_stop_timers_for_cmds(conn);
4267 iscsit_stop_nopin_response_timer(conn);
4268 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004269
4270 if (conn->conn_transport->iscsit_wait_conn)
4271 conn->conn_transport->iscsit_wait_conn(conn);
4272
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004273 /*
4274 * During Connection recovery drop unacknowledged out of order
4275 * commands for this connection, and prepare the other commands
4276 * for realligence.
4277 *
4278 * During normal operation clear the out of order commands (but
4279 * do not free the struct iscsi_ooo_cmdsn's) and release all
4280 * struct iscsi_cmds.
4281 */
4282 if (atomic_read(&conn->connection_recovery)) {
4283 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4284 iscsit_prepare_cmds_for_realligance(conn);
4285 } else {
4286 iscsit_clear_ooo_cmdsns_for_conn(conn);
4287 iscsit_release_commands_from_conn(conn);
4288 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004289 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004290
4291 /*
4292 * Handle decrementing session or connection usage count if
4293 * a logout response was not able to be sent because the
4294 * connection failed. Fall back to Session Recovery here.
4295 */
4296 if (atomic_read(&conn->conn_logout_remove)) {
4297 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4298 iscsit_dec_conn_usage_count(conn);
4299 iscsit_dec_session_usage_count(sess);
4300 }
4301 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4302 iscsit_dec_conn_usage_count(conn);
4303
4304 atomic_set(&conn->conn_logout_remove, 0);
4305 atomic_set(&sess->session_reinstatement, 0);
4306 atomic_set(&sess->session_fall_back_to_erl0, 1);
4307 }
4308
4309 spin_lock_bh(&sess->conn_lock);
4310 list_del(&conn->conn_list);
4311
4312 /*
4313 * Attempt to let the Initiator know this connection failed by
4314 * sending an Connection Dropped Async Message on another
4315 * active connection.
4316 */
4317 if (atomic_read(&conn->connection_recovery))
4318 iscsit_build_conn_drop_async_message(conn);
4319
4320 spin_unlock_bh(&sess->conn_lock);
4321
4322 /*
4323 * If connection reinstatement is being performed on this connection,
4324 * up the connection reinstatement semaphore that is being blocked on
4325 * in iscsit_cause_connection_reinstatement().
4326 */
4327 spin_lock_bh(&conn->state_lock);
4328 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4329 spin_unlock_bh(&conn->state_lock);
4330 complete(&conn->conn_wait_comp);
4331 wait_for_completion(&conn->conn_post_wait_comp);
4332 spin_lock_bh(&conn->state_lock);
4333 }
4334
4335 /*
4336 * If connection reinstatement is being performed on this connection
4337 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4338 * connection wait rcfr semaphore that is being blocked on
4339 * an iscsit_connection_reinstatement_rcfr().
4340 */
4341 if (atomic_read(&conn->connection_wait_rcfr)) {
4342 spin_unlock_bh(&conn->state_lock);
4343 complete(&conn->conn_wait_rcfr_comp);
4344 wait_for_completion(&conn->conn_post_wait_comp);
4345 spin_lock_bh(&conn->state_lock);
4346 }
4347 atomic_set(&conn->connection_reinstatement, 1);
4348 spin_unlock_bh(&conn->state_lock);
4349
4350 /*
4351 * If any other processes are accessing this connection pointer we
4352 * must wait until they have completed.
4353 */
4354 iscsit_check_conn_usage_count(conn);
4355
4356 if (conn->conn_rx_hash.tfm)
4357 crypto_free_hash(conn->conn_rx_hash.tfm);
4358 if (conn->conn_tx_hash.tfm)
4359 crypto_free_hash(conn->conn_tx_hash.tfm);
4360
Joern Engelfbecb652014-09-02 17:49:47 -04004361 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004362
4363 kfree(conn->conn_ops);
4364 conn->conn_ops = NULL;
4365
Al Virobf6932f2012-07-21 08:55:18 +01004366 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004367 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004368
4369 if (conn->conn_transport->iscsit_free_conn)
4370 conn->conn_transport->iscsit_free_conn(conn);
4371
4372 iscsit_put_transport(conn->conn_transport);
4373
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004374 conn->thread_set = NULL;
4375
4376 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4377 conn->conn_state = TARG_CONN_STATE_FREE;
4378 kfree(conn);
4379
4380 spin_lock_bh(&sess->conn_lock);
4381 atomic_dec(&sess->nconn);
4382 pr_debug("Decremented iSCSI connection count to %hu from node:"
4383 " %s\n", atomic_read(&sess->nconn),
4384 sess->sess_ops->InitiatorName);
4385 /*
4386 * Make sure that if one connection fails in an non ERL=2 iSCSI
4387 * Session that they all fail.
4388 */
4389 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4390 !atomic_read(&sess->session_logout))
4391 atomic_set(&sess->session_fall_back_to_erl0, 1);
4392
4393 /*
4394 * If this was not the last connection in the session, and we are
4395 * performing session reinstatement or falling back to ERL=0, call
4396 * iscsit_stop_session() without sleeping to shutdown the other
4397 * active connections.
4398 */
4399 if (atomic_read(&sess->nconn)) {
4400 if (!atomic_read(&sess->session_reinstatement) &&
4401 !atomic_read(&sess->session_fall_back_to_erl0)) {
4402 spin_unlock_bh(&sess->conn_lock);
4403 return 0;
4404 }
4405 if (!atomic_read(&sess->session_stop_active)) {
4406 atomic_set(&sess->session_stop_active, 1);
4407 spin_unlock_bh(&sess->conn_lock);
4408 iscsit_stop_session(sess, 0, 0);
4409 return 0;
4410 }
4411 spin_unlock_bh(&sess->conn_lock);
4412 return 0;
4413 }
4414
4415 /*
4416 * If this was the last connection in the session and one of the
4417 * following is occurring:
4418 *
4419 * Session Reinstatement is not being performed, and are falling back
4420 * to ERL=0 call iscsit_close_session().
4421 *
4422 * Session Logout was requested. iscsit_close_session() will be called
4423 * elsewhere.
4424 *
4425 * Session Continuation is not being performed, start the Time2Retain
4426 * handler and check if sleep_on_sess_wait_sem is active.
4427 */
4428 if (!atomic_read(&sess->session_reinstatement) &&
4429 atomic_read(&sess->session_fall_back_to_erl0)) {
4430 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004431 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004432
4433 return 0;
4434 } else if (atomic_read(&sess->session_logout)) {
4435 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4436 sess->session_state = TARG_SESS_STATE_FREE;
4437 spin_unlock_bh(&sess->conn_lock);
4438
4439 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4440 complete(&sess->session_wait_comp);
4441
4442 return 0;
4443 } else {
4444 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4445 sess->session_state = TARG_SESS_STATE_FAILED;
4446
4447 if (!atomic_read(&sess->session_continuation)) {
4448 spin_unlock_bh(&sess->conn_lock);
4449 iscsit_start_time2retain_handler(sess);
4450 } else
4451 spin_unlock_bh(&sess->conn_lock);
4452
4453 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4454 complete(&sess->session_wait_comp);
4455
4456 return 0;
4457 }
4458 spin_unlock_bh(&sess->conn_lock);
4459
4460 return 0;
4461}
4462
4463int iscsit_close_session(struct iscsi_session *sess)
4464{
Andy Grover60bfcf82013-10-09 11:05:58 -07004465 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004466 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4467
4468 if (atomic_read(&sess->nconn)) {
4469 pr_err("%d connection(s) still exist for iSCSI session"
4470 " to %s\n", atomic_read(&sess->nconn),
4471 sess->sess_ops->InitiatorName);
4472 BUG();
4473 }
4474
4475 spin_lock_bh(&se_tpg->session_lock);
4476 atomic_set(&sess->session_logout, 1);
4477 atomic_set(&sess->session_reinstatement, 1);
4478 iscsit_stop_time2retain_timer(sess);
4479 spin_unlock_bh(&se_tpg->session_lock);
4480
4481 /*
4482 * transport_deregister_session_configfs() will clear the
4483 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4484 * can be setting it again with __transport_register_session() in
4485 * iscsi_post_login_handler() again after the iscsit_stop_session()
4486 * completes in iscsi_np context.
4487 */
4488 transport_deregister_session_configfs(sess->se_sess);
4489
4490 /*
4491 * If any other processes are accessing this session pointer we must
4492 * wait until they have completed. If we are in an interrupt (the
4493 * time2retain handler) and contain and active session usage count we
4494 * restart the timer and exit.
4495 */
4496 if (!in_interrupt()) {
4497 if (iscsit_check_session_usage_count(sess) == 1)
4498 iscsit_stop_session(sess, 1, 1);
4499 } else {
4500 if (iscsit_check_session_usage_count(sess) == 2) {
4501 atomic_set(&sess->session_logout, 0);
4502 iscsit_start_time2retain_handler(sess);
4503 return 0;
4504 }
4505 }
4506
4507 transport_deregister_session(sess->se_sess);
4508
4509 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4510 iscsit_free_connection_recovery_entires(sess);
4511
4512 iscsit_free_all_ooo_cmdsns(sess);
4513
4514 spin_lock_bh(&se_tpg->session_lock);
4515 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4516 sess->session_state = TARG_SESS_STATE_FREE;
4517 pr_debug("Released iSCSI session from node: %s\n",
4518 sess->sess_ops->InitiatorName);
4519 tpg->nsessions--;
4520 if (tpg->tpg_tiqn)
4521 tpg->tpg_tiqn->tiqn_nsessions--;
4522
4523 pr_debug("Decremented number of active iSCSI Sessions on"
4524 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4525
4526 spin_lock(&sess_idr_lock);
4527 idr_remove(&sess_idr, sess->session_index);
4528 spin_unlock(&sess_idr_lock);
4529
4530 kfree(sess->sess_ops);
4531 sess->sess_ops = NULL;
4532 spin_unlock_bh(&se_tpg->session_lock);
4533
4534 kfree(sess);
4535 return 0;
4536}
4537
4538static void iscsit_logout_post_handler_closesession(
4539 struct iscsi_conn *conn)
4540{
4541 struct iscsi_session *sess = conn->sess;
4542
4543 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4544 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4545
4546 atomic_set(&conn->conn_logout_remove, 0);
4547 complete(&conn->conn_logout_comp);
4548
4549 iscsit_dec_conn_usage_count(conn);
4550 iscsit_stop_session(sess, 1, 1);
4551 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004552 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004553}
4554
4555static void iscsit_logout_post_handler_samecid(
4556 struct iscsi_conn *conn)
4557{
4558 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4559 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4560
4561 atomic_set(&conn->conn_logout_remove, 0);
4562 complete(&conn->conn_logout_comp);
4563
4564 iscsit_cause_connection_reinstatement(conn, 1);
4565 iscsit_dec_conn_usage_count(conn);
4566}
4567
4568static void iscsit_logout_post_handler_diffcid(
4569 struct iscsi_conn *conn,
4570 u16 cid)
4571{
4572 struct iscsi_conn *l_conn;
4573 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004574 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004575
4576 if (!sess)
4577 return;
4578
4579 spin_lock_bh(&sess->conn_lock);
4580 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4581 if (l_conn->cid == cid) {
4582 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004583 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004584 break;
4585 }
4586 }
4587 spin_unlock_bh(&sess->conn_lock);
4588
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004589 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004590 return;
4591
4592 if (l_conn->sock)
4593 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4594
4595 spin_lock_bh(&l_conn->state_lock);
4596 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4597 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4598 spin_unlock_bh(&l_conn->state_lock);
4599
4600 iscsit_cause_connection_reinstatement(l_conn, 1);
4601 iscsit_dec_conn_usage_count(l_conn);
4602}
4603
4604/*
4605 * Return of 0 causes the TX thread to restart.
4606 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004607int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004608 struct iscsi_cmd *cmd,
4609 struct iscsi_conn *conn)
4610{
4611 int ret = 0;
4612
4613 switch (cmd->logout_reason) {
4614 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4615 switch (cmd->logout_response) {
4616 case ISCSI_LOGOUT_SUCCESS:
4617 case ISCSI_LOGOUT_CLEANUP_FAILED:
4618 default:
4619 iscsit_logout_post_handler_closesession(conn);
4620 break;
4621 }
4622 ret = 0;
4623 break;
4624 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4625 if (conn->cid == cmd->logout_cid) {
4626 switch (cmd->logout_response) {
4627 case ISCSI_LOGOUT_SUCCESS:
4628 case ISCSI_LOGOUT_CLEANUP_FAILED:
4629 default:
4630 iscsit_logout_post_handler_samecid(conn);
4631 break;
4632 }
4633 ret = 0;
4634 } else {
4635 switch (cmd->logout_response) {
4636 case ISCSI_LOGOUT_SUCCESS:
4637 iscsit_logout_post_handler_diffcid(conn,
4638 cmd->logout_cid);
4639 break;
4640 case ISCSI_LOGOUT_CID_NOT_FOUND:
4641 case ISCSI_LOGOUT_CLEANUP_FAILED:
4642 default:
4643 break;
4644 }
4645 ret = 1;
4646 }
4647 break;
4648 case ISCSI_LOGOUT_REASON_RECOVERY:
4649 switch (cmd->logout_response) {
4650 case ISCSI_LOGOUT_SUCCESS:
4651 case ISCSI_LOGOUT_CID_NOT_FOUND:
4652 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4653 case ISCSI_LOGOUT_CLEANUP_FAILED:
4654 default:
4655 break;
4656 }
4657 ret = 1;
4658 break;
4659 default:
4660 break;
4661
4662 }
4663 return ret;
4664}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004665EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004666
4667void iscsit_fail_session(struct iscsi_session *sess)
4668{
4669 struct iscsi_conn *conn;
4670
4671 spin_lock_bh(&sess->conn_lock);
4672 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4673 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4674 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4675 }
4676 spin_unlock_bh(&sess->conn_lock);
4677
4678 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4679 sess->session_state = TARG_SESS_STATE_FAILED;
4680}
4681
4682int iscsit_free_session(struct iscsi_session *sess)
4683{
4684 u16 conn_count = atomic_read(&sess->nconn);
4685 struct iscsi_conn *conn, *conn_tmp = NULL;
4686 int is_last;
4687
4688 spin_lock_bh(&sess->conn_lock);
4689 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4690
4691 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4692 conn_list) {
4693 if (conn_count == 0)
4694 break;
4695
4696 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4697 is_last = 1;
4698 } else {
4699 iscsit_inc_conn_usage_count(conn_tmp);
4700 is_last = 0;
4701 }
4702 iscsit_inc_conn_usage_count(conn);
4703
4704 spin_unlock_bh(&sess->conn_lock);
4705 iscsit_cause_connection_reinstatement(conn, 1);
4706 spin_lock_bh(&sess->conn_lock);
4707
4708 iscsit_dec_conn_usage_count(conn);
4709 if (is_last == 0)
4710 iscsit_dec_conn_usage_count(conn_tmp);
4711
4712 conn_count--;
4713 }
4714
4715 if (atomic_read(&sess->nconn)) {
4716 spin_unlock_bh(&sess->conn_lock);
4717 wait_for_completion(&sess->session_wait_comp);
4718 } else
4719 spin_unlock_bh(&sess->conn_lock);
4720
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004721 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004722 return 0;
4723}
4724
4725void iscsit_stop_session(
4726 struct iscsi_session *sess,
4727 int session_sleep,
4728 int connection_sleep)
4729{
4730 u16 conn_count = atomic_read(&sess->nconn);
4731 struct iscsi_conn *conn, *conn_tmp = NULL;
4732 int is_last;
4733
4734 spin_lock_bh(&sess->conn_lock);
4735 if (session_sleep)
4736 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4737
4738 if (connection_sleep) {
4739 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4740 conn_list) {
4741 if (conn_count == 0)
4742 break;
4743
4744 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4745 is_last = 1;
4746 } else {
4747 iscsit_inc_conn_usage_count(conn_tmp);
4748 is_last = 0;
4749 }
4750 iscsit_inc_conn_usage_count(conn);
4751
4752 spin_unlock_bh(&sess->conn_lock);
4753 iscsit_cause_connection_reinstatement(conn, 1);
4754 spin_lock_bh(&sess->conn_lock);
4755
4756 iscsit_dec_conn_usage_count(conn);
4757 if (is_last == 0)
4758 iscsit_dec_conn_usage_count(conn_tmp);
4759 conn_count--;
4760 }
4761 } else {
4762 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4763 iscsit_cause_connection_reinstatement(conn, 0);
4764 }
4765
4766 if (session_sleep && atomic_read(&sess->nconn)) {
4767 spin_unlock_bh(&sess->conn_lock);
4768 wait_for_completion(&sess->session_wait_comp);
4769 } else
4770 spin_unlock_bh(&sess->conn_lock);
4771}
4772
4773int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4774{
4775 struct iscsi_session *sess;
4776 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4777 struct se_session *se_sess, *se_sess_tmp;
4778 int session_count = 0;
4779
4780 spin_lock_bh(&se_tpg->session_lock);
4781 if (tpg->nsessions && !force) {
4782 spin_unlock_bh(&se_tpg->session_lock);
4783 return -1;
4784 }
4785
4786 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4787 sess_list) {
4788 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4789
4790 spin_lock(&sess->conn_lock);
4791 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4792 atomic_read(&sess->session_logout) ||
4793 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4794 spin_unlock(&sess->conn_lock);
4795 continue;
4796 }
4797 atomic_set(&sess->session_reinstatement, 1);
4798 spin_unlock(&sess->conn_lock);
4799 spin_unlock_bh(&se_tpg->session_lock);
4800
4801 iscsit_free_session(sess);
4802 spin_lock_bh(&se_tpg->session_lock);
4803
4804 session_count++;
4805 }
4806 spin_unlock_bh(&se_tpg->session_lock);
4807
4808 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4809 " Group: %hu\n", session_count, tpg->tpgt);
4810 return 0;
4811}
4812
4813MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4814MODULE_VERSION("4.1.x");
4815MODULE_AUTHOR("nab@Linux-iSCSI.org");
4816MODULE_LICENSE("GPL");
4817
4818module_init(iscsi_target_init_module);
4819module_exit(iscsi_target_cleanup_module);