blob: b87721a01b7464a17ca44f9212489cd6e46dc8ac [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
33#include "iscsi_target_core.h"
34#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"
48#include "iscsi_target_stat.h"
49
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) {
463 spin_unlock_bh(&np->np_thread_lock);
464 return 0;
465 }
466 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
467 spin_unlock_bh(&np->np_thread_lock);
468
469 if (np->np_thread) {
470 /*
471 * We need to send the signal to wakeup Linux/Net
472 * which may be sleeping in sock_accept()..
473 */
474 send_sig(SIGINT, np->np_thread, 1);
475 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800476 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800478
479 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000480
Andy Groveree291e62014-01-24 16:18:54 -0800481 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000482 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800483 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000484
485 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800486 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000487
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800488 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000489 kfree(np);
490 return 0;
491}
492
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700493static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
494static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
495
496static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
497{
498 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
499 return 0;
500}
501
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700502static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
503{
504 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
505
506 spin_lock_bh(&conn->cmd_lock);
507 if (!list_empty(&cmd->i_conn_node))
508 list_del_init(&cmd->i_conn_node);
509 spin_unlock_bh(&conn->cmd_lock);
510
511 __iscsit_free_cmd(cmd, scsi_cmd, true);
512}
513
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700514static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
515{
516 return TARGET_PROT_NORMAL;
517}
518
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800519static struct iscsit_transport iscsi_target_transport = {
520 .name = "iSCSI/TCP",
521 .transport_type = ISCSI_TCP,
522 .owner = NULL,
523 .iscsit_setup_np = iscsit_setup_np,
524 .iscsit_accept_np = iscsit_accept_np,
525 .iscsit_free_np = iscsit_free_np,
526 .iscsit_get_login_rx = iscsit_get_login_rx,
527 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800528 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700529 .iscsit_immediate_queue = iscsit_immediate_queue,
530 .iscsit_response_queue = iscsit_response_queue,
531 .iscsit_queue_data_in = iscsit_queue_rsp,
532 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700533 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700534 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800535};
536
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000537static int __init iscsi_target_init_module(void)
538{
539 int ret = 0;
540
541 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
542
543 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
544 if (!iscsit_global) {
545 pr_err("Unable to allocate memory for iscsit_global\n");
546 return -1;
547 }
548 mutex_init(&auth_id_lock);
549 spin_lock_init(&sess_idr_lock);
550 idr_init(&tiqn_idr);
551 idr_init(&sess_idr);
552
553 ret = iscsi_target_register_configfs();
554 if (ret < 0)
555 goto out;
556
557 ret = iscsi_thread_set_init();
558 if (ret < 0)
559 goto configfs_out;
560
561 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
562 TARGET_THREAD_SET_COUNT) {
563 pr_err("iscsi_allocate_thread_sets() returned"
564 " unexpected value!\n");
565 goto ts_out1;
566 }
567
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000568 lio_qr_cache = kmem_cache_create("lio_qr_cache",
569 sizeof(struct iscsi_queue_req),
570 __alignof__(struct iscsi_queue_req), 0, NULL);
571 if (!lio_qr_cache) {
572 pr_err("nable to kmem_cache_create() for"
573 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700574 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000575 }
576
577 lio_dr_cache = kmem_cache_create("lio_dr_cache",
578 sizeof(struct iscsi_datain_req),
579 __alignof__(struct iscsi_datain_req), 0, NULL);
580 if (!lio_dr_cache) {
581 pr_err("Unable to kmem_cache_create() for"
582 " lio_dr_cache\n");
583 goto qr_out;
584 }
585
586 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
587 sizeof(struct iscsi_ooo_cmdsn),
588 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
589 if (!lio_ooo_cache) {
590 pr_err("Unable to kmem_cache_create() for"
591 " lio_ooo_cache\n");
592 goto dr_out;
593 }
594
595 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
596 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
597 0, NULL);
598 if (!lio_r2t_cache) {
599 pr_err("Unable to kmem_cache_create() for"
600 " lio_r2t_cache\n");
601 goto ooo_out;
602 }
603
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800604 iscsit_register_transport(&iscsi_target_transport);
605
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000606 if (iscsit_load_discovery_tpg() < 0)
607 goto r2t_out;
608
609 return ret;
610r2t_out:
611 kmem_cache_destroy(lio_r2t_cache);
612ooo_out:
613 kmem_cache_destroy(lio_ooo_cache);
614dr_out:
615 kmem_cache_destroy(lio_dr_cache);
616qr_out:
617 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000618ts_out2:
619 iscsi_deallocate_thread_sets();
620ts_out1:
621 iscsi_thread_set_free();
622configfs_out:
623 iscsi_target_deregister_configfs();
624out:
625 kfree(iscsit_global);
626 return -ENOMEM;
627}
628
629static void __exit iscsi_target_cleanup_module(void)
630{
631 iscsi_deallocate_thread_sets();
632 iscsi_thread_set_free();
633 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800634 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000635 kmem_cache_destroy(lio_qr_cache);
636 kmem_cache_destroy(lio_dr_cache);
637 kmem_cache_destroy(lio_ooo_cache);
638 kmem_cache_destroy(lio_r2t_cache);
639
640 iscsi_target_deregister_configfs();
641
642 kfree(iscsit_global);
643}
644
Andy Grover8b1e1242012-04-03 15:51:12 -0700645static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700646 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000647 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700648 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000649{
650 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000651
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000652 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000653 if (!cmd)
654 return -1;
655
656 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700657 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000658
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100659 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000660 if (!cmd->buf_ptr) {
661 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700662 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000663 return -1;
664 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000665
666 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700667 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000668 spin_unlock_bh(&conn->cmd_lock);
669
670 cmd->i_state = ISTATE_SEND_REJECT;
671 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
672
Nicholas Bellingerba159912013-07-03 03:48:24 -0700673 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000674}
675
Nicholas Bellingerba159912013-07-03 03:48:24 -0700676static int iscsit_add_reject_from_cmd(
677 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000678 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700679 bool add_to_conn,
680 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000681{
682 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000683
684 if (!cmd->conn) {
685 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
686 cmd->init_task_tag);
687 return -1;
688 }
689 conn = cmd->conn;
690
691 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700692 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000693
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100694 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000695 if (!cmd->buf_ptr) {
696 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700697 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 return -1;
699 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000700
701 if (add_to_conn) {
702 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700703 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000704 spin_unlock_bh(&conn->cmd_lock);
705 }
706
707 cmd->i_state = ISTATE_SEND_REJECT;
708 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800709 /*
710 * Perform the kref_put now if se_cmd has already been setup by
711 * scsit_setup_scsi_cmd()
712 */
713 if (cmd->se_cmd.se_tfo != NULL) {
714 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
715 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
716 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700717 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000718}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700719
720static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
721 unsigned char *buf)
722{
723 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
724}
725
726int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
727{
728 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
729}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000730
731/*
732 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700733 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000734 */
735static int iscsit_map_iovec(
736 struct iscsi_cmd *cmd,
737 struct kvec *iov,
738 u32 data_offset,
739 u32 data_length)
740{
741 u32 i = 0;
742 struct scatterlist *sg;
743 unsigned int page_off;
744
745 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700746 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000747 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700748 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000749 page_off = (data_offset % PAGE_SIZE);
750
751 cmd->first_data_sg = sg;
752 cmd->first_data_sg_off = page_off;
753
754 while (data_length) {
755 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
756
757 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
758 iov[i].iov_len = cur_len;
759
760 data_length -= cur_len;
761 page_off = 0;
762 sg = sg_next(sg);
763 i++;
764 }
765
766 cmd->kmapped_nents = i;
767
768 return i;
769}
770
771static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
772{
773 u32 i;
774 struct scatterlist *sg;
775
776 sg = cmd->first_data_sg;
777
778 for (i = 0; i < cmd->kmapped_nents; i++)
779 kunmap(sg_page(&sg[i]));
780}
781
782static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
783{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700784 LIST_HEAD(ack_list);
785 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000786
787 conn->exp_statsn = exp_statsn;
788
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800789 if (conn->sess->sess_ops->RDMAExtensions)
790 return;
791
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000792 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700793 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 spin_lock(&cmd->istate_lock);
795 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800796 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797 cmd->i_state = ISTATE_REMOVE;
798 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700799 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000800 continue;
801 }
802 spin_unlock(&cmd->istate_lock);
803 }
804 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700805
806 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800807 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700808 iscsit_free_cmd(cmd, false);
809 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810}
811
812static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
813{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700814 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400816 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817
818 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
819 if (!cmd->iov_data) {
820 pr_err("Unable to allocate cmd->iov_data\n");
821 return -ENOMEM;
822 }
823
824 cmd->orig_iov_data_count = iov_count;
825 return 0;
826}
827
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800828int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
829 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000830{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800831 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000832 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700833 int iscsi_task_attr;
834 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000835
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800836 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000837
838 hdr = (struct iscsi_scsi_req *) buf;
839 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000840
841 /* FIXME; Add checks for AdditionalHeaderSegment */
842
843 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
844 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
845 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
846 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700847 return iscsit_add_reject_cmd(cmd,
848 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000849 }
850
851 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
852 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
853 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800854 * From RFC-3720 Section 10.3.1:
855 *
856 * "Either or both of R and W MAY be 1 when either the
857 * Expected Data Transfer Length and/or Bidirectional Read
858 * Expected Data Transfer Length are 0"
859 *
860 * For this case, go ahead and clear the unnecssary bits
861 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000862 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800863 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
864 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000865
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800866 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000867 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800868 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000869 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870
871 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
872 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
873 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
874 " MUST be set if Expected Data Transfer Length is not 0."
875 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700876 return iscsit_add_reject_cmd(cmd,
877 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000878 }
879
880 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
881 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
882 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700883 return iscsit_add_reject_cmd(cmd,
884 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000885 }
886
887 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
888 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
889 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700890 return iscsit_add_reject_cmd(cmd,
891 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000892 }
893
894 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
895 pr_err("ImmediateData=No but DataSegmentLength=%u,"
896 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700897 return iscsit_add_reject_cmd(cmd,
898 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000899 }
900
Nicholas Bellingerba159912013-07-03 03:48:24 -0700901 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
903 pr_err("Expected Data Transfer Length and Length of"
904 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
905 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700906 return iscsit_add_reject_cmd(cmd,
907 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000908 }
909
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400910 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 pr_err("DataSegmentLength: %u is greater than"
912 " EDTL: %u, protocol error.\n", payload_length,
913 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700914 return iscsit_add_reject_cmd(cmd,
915 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000916 }
917
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700918 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000919 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700920 " MaxXmitDataSegmentLength: %u, protocol error.\n",
921 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700922 return iscsit_add_reject_cmd(cmd,
923 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000924 }
925
926 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
927 pr_err("DataSegmentLength: %u is greater than"
928 " FirstBurstLength: %u, protocol error.\n",
929 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700930 return iscsit_add_reject_cmd(cmd,
931 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000932 }
933
934 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
935 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
936 DMA_NONE;
937
Andy Groverd28b11692012-04-03 15:51:22 -0700938 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700939 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
940 /*
941 * Figure out the SAM Task Attribute for the incoming SCSI CDB
942 */
943 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
944 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
945 sam_task_attr = MSG_SIMPLE_TAG;
946 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
947 sam_task_attr = MSG_ORDERED_TAG;
948 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
949 sam_task_attr = MSG_HEAD_TAG;
950 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
951 sam_task_attr = MSG_ACA_TAG;
952 else {
953 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
954 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
955 sam_task_attr = MSG_SIMPLE_TAG;
956 }
957
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000958 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
959 cmd->i_state = ISTATE_NEW_CMD;
960 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
961 cmd->immediate_data = (payload_length) ? 1 : 0;
962 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
963 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
964 if (cmd->unsolicited_data)
965 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
966
967 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
968 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
969 spin_lock_bh(&conn->sess->ttt_lock);
970 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
971 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
972 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
973 spin_unlock_bh(&conn->sess->ttt_lock);
974 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
975 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400976 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
977 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000978 cmd->first_burst_len = payload_length;
979
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800980 if (!conn->sess->sess_ops->RDMAExtensions &&
981 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000982 struct iscsi_datain_req *dr;
983
984 dr = iscsit_allocate_datain_req();
985 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700986 return iscsit_add_reject_cmd(cmd,
987 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000988
989 iscsit_attach_datain_req(cmd, dr);
990 }
991
992 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700993 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
994 */
995 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400996 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
997 cmd->data_direction, sam_task_attr,
998 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700999
1000 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1001 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001002 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1003 conn->cid);
1004
1005 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001006
Christoph Hellwigde103c92012-11-06 12:24:09 -08001007 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1008 scsilun_to_int(&hdr->lun));
1009 if (cmd->sense_reason)
1010 goto attach_cmd;
1011
1012 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1013 if (cmd->sense_reason) {
1014 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001015 return iscsit_add_reject_cmd(cmd,
1016 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001017 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001018
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001019 goto attach_cmd;
1020 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001021
Christoph Hellwigde103c92012-11-06 12:24:09 -08001022 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001023 return iscsit_add_reject_cmd(cmd,
1024 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001025 }
1026
1027attach_cmd:
1028 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001029 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001030 spin_unlock_bh(&conn->cmd_lock);
1031 /*
1032 * Check if we need to delay processing because of ALUA
1033 * Active/NonOptimized primary access state..
1034 */
1035 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001036
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001037 return 0;
1038}
1039EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001040
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001041void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1042{
1043 iscsit_set_dataout_sequence_values(cmd);
1044
1045 spin_lock_bh(&cmd->dataout_timeout_lock);
1046 iscsit_start_dataout_timer(cmd, cmd->conn);
1047 spin_unlock_bh(&cmd->dataout_timeout_lock);
1048}
1049EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1050
1051int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1052 struct iscsi_scsi_req *hdr)
1053{
1054 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001055 /*
1056 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1057 * the Immediate Bit is not set, and no Immediate
1058 * Data is attached.
1059 *
1060 * A PDU/CmdSN carrying Immediate Data can only
1061 * be processed after the DataCRC has passed.
1062 * If the DataCRC fails, the CmdSN MUST NOT
1063 * be acknowledged. (See below)
1064 */
1065 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001066 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1067 (unsigned char *)hdr, hdr->cmdsn);
1068 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1069 return -1;
1070 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001071 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001072 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001073 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001074 }
1075
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001076 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001077
1078 /*
1079 * If no Immediate Data is attached, it's OK to return now.
1080 */
1081 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001082 if (!cmd->sense_reason && cmd->unsolicited_data)
1083 iscsit_set_unsoliticed_dataout(cmd);
1084 if (!cmd->sense_reason)
1085 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001086
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001087 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001088 return 0;
1089 }
1090
1091 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001092 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1093 * execution. These exceptions are processed in CmdSN order using
1094 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001095 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001096 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001097 if (cmd->reject_reason)
1098 return 0;
1099
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001100 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001101 }
1102 /*
1103 * Call directly into transport_generic_new_cmd() to perform
1104 * the backend memory allocation.
1105 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001106 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001107 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001108 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001109
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001110 return 0;
1111}
1112EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1113
1114static int
1115iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1116 bool dump_payload)
1117{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001118 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001119 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1120 /*
1121 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1122 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001123 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001124 goto after_immediate_data;
1125
1126 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1127 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001128after_immediate_data:
1129 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1130 /*
1131 * A PDU/CmdSN carrying Immediate Data passed
1132 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1133 * Immediate Bit is not set.
1134 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001135 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1136 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001137 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001138 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001139
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001140 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001141 int rc;
1142
1143 rc = iscsit_dump_data_payload(cmd->conn,
1144 cmd->first_burst_len, 1);
1145 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1146 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001147 } else if (cmd->unsolicited_data)
1148 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001149
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001150 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1151 /*
1152 * Immediate Data failed DataCRC and ERL>=1,
1153 * silently drop this PDU and let the initiator
1154 * plug the CmdSN gap.
1155 *
1156 * FIXME: Send Unsolicited NOPIN with reserved
1157 * TTT here to help the initiator figure out
1158 * the missing CmdSN, although they should be
1159 * intelligent enough to determine the missing
1160 * CmdSN and issue a retry to plug the sequence.
1161 */
1162 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001163 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001164 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1165 return -1;
1166
1167 return 0;
1168}
1169
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170static int
1171iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1172 unsigned char *buf)
1173{
1174 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1175 int rc, immed_data;
1176 bool dump_payload = false;
1177
1178 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1179 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001180 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001181 /*
1182 * Allocation iovecs needed for struct socket operations for
1183 * traditional iSCSI block I/O.
1184 */
1185 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001186 return iscsit_add_reject_cmd(cmd,
1187 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001188 }
1189 immed_data = cmd->immediate_data;
1190
1191 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1192 if (rc < 0)
1193 return rc;
1194 else if (rc > 0)
1195 dump_payload = true;
1196
1197 if (!immed_data)
1198 return 0;
1199
1200 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1201}
1202
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001203static u32 iscsit_do_crypto_hash_sg(
1204 struct hash_desc *hash,
1205 struct iscsi_cmd *cmd,
1206 u32 data_offset,
1207 u32 data_length,
1208 u32 padding,
1209 u8 *pad_bytes)
1210{
1211 u32 data_crc;
1212 u32 i;
1213 struct scatterlist *sg;
1214 unsigned int page_off;
1215
1216 crypto_hash_init(hash);
1217
1218 sg = cmd->first_data_sg;
1219 page_off = cmd->first_data_sg_off;
1220
1221 i = 0;
1222 while (data_length) {
1223 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1224
1225 crypto_hash_update(hash, &sg[i], cur_len);
1226
1227 data_length -= cur_len;
1228 page_off = 0;
1229 i++;
1230 }
1231
1232 if (padding) {
1233 struct scatterlist pad_sg;
1234
1235 sg_init_one(&pad_sg, pad_bytes, padding);
1236 crypto_hash_update(hash, &pad_sg, padding);
1237 }
1238 crypto_hash_final(hash, (u8 *) &data_crc);
1239
1240 return data_crc;
1241}
1242
1243static void iscsit_do_crypto_hash_buf(
1244 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001245 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001246 u32 payload_length,
1247 u32 padding,
1248 u8 *pad_bytes,
1249 u8 *data_crc)
1250{
1251 struct scatterlist sg;
1252
1253 crypto_hash_init(hash);
1254
Jörn Engel8359cf42011-11-24 02:05:51 +01001255 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001256 crypto_hash_update(hash, &sg, payload_length);
1257
1258 if (padding) {
1259 sg_init_one(&sg, pad_bytes, padding);
1260 crypto_hash_update(hash, &sg, padding);
1261 }
1262 crypto_hash_final(hash, data_crc);
1263}
1264
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001265int
1266iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1267 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001269 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001270 struct iscsi_cmd *cmd = NULL;
1271 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001272 u32 payload_length = ntoh24(hdr->dlength);
1273 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001274
1275 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001276 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1277 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001278 }
1279
1280 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001281 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001282
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001283 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001284 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001285 " MaxXmitDataSegmentLength: %u\n", payload_length,
1286 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001287 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1288 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001289 }
1290
1291 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1292 payload_length);
1293 if (!cmd)
1294 return 0;
1295
1296 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1297 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001298 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001299 payload_length, conn->cid);
1300
1301 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1302 pr_err("Command ITT: 0x%08x received DataOUT after"
1303 " last DataOUT received, dumping payload\n",
1304 cmd->init_task_tag);
1305 return iscsit_dump_data_payload(conn, payload_length, 1);
1306 }
1307
1308 if (cmd->data_direction != DMA_TO_DEVICE) {
1309 pr_err("Command ITT: 0x%08x received DataOUT for a"
1310 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001311 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001312 }
1313 se_cmd = &cmd->se_cmd;
1314 iscsit_mod_dataout_timer(cmd);
1315
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001316 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001317 pr_err("DataOut Offset: %u, Length %u greater than"
1318 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001319 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001320 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001321 }
1322
1323 if (cmd->unsolicited_data) {
1324 int dump_unsolicited_data = 0;
1325
1326 if (conn->sess->sess_ops->InitialR2T) {
1327 pr_err("Received unexpected unsolicited data"
1328 " while InitialR2T=Yes, protocol error.\n");
1329 transport_send_check_condition_and_sense(&cmd->se_cmd,
1330 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1331 return -1;
1332 }
1333 /*
1334 * Special case for dealing with Unsolicited DataOUT
1335 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1336 * failures;
1337 */
1338
1339 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001340 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001341 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001342 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001343
1344 if (dump_unsolicited_data) {
1345 /*
1346 * Check if a delayed TASK_ABORTED status needs to
1347 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1348 * received with the unsolicitied data out.
1349 */
1350 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1351 iscsit_stop_dataout_timer(cmd);
1352
1353 transport_check_aborted_status(se_cmd,
1354 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1355 return iscsit_dump_data_payload(conn, payload_length, 1);
1356 }
1357 } else {
1358 /*
1359 * For the normal solicited data path:
1360 *
1361 * Check for a delayed TASK_ABORTED status and dump any
1362 * incoming data out payload if one exists. Also, when the
1363 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1364 * data out sequence, we decrement outstanding_r2ts. Once
1365 * outstanding_r2ts reaches zero, go ahead and send the delayed
1366 * TASK_ABORTED status.
1367 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001368 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1370 if (--cmd->outstanding_r2ts < 1) {
1371 iscsit_stop_dataout_timer(cmd);
1372 transport_check_aborted_status(
1373 se_cmd, 1);
1374 }
1375
1376 return iscsit_dump_data_payload(conn, payload_length, 1);
1377 }
1378 }
1379 /*
1380 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1381 * within-command recovery checks before receiving the payload.
1382 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001383 rc = iscsit_check_pre_dataout(cmd, buf);
1384 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001385 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001386 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001387 return -1;
1388
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001389 *out_cmd = cmd;
1390 return 0;
1391}
1392EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1393
1394static int
1395iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1396 struct iscsi_data *hdr)
1397{
1398 struct kvec *iov;
1399 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1400 u32 payload_length = ntoh24(hdr->dlength);
1401 int iov_ret, data_crc_failed = 0;
1402
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001403 rx_size += payload_length;
1404 iov = &cmd->iov_data[0];
1405
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001406 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1407 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001408 if (iov_ret < 0)
1409 return -1;
1410
1411 iov_count += iov_ret;
1412
1413 padding = ((-payload_length) & 3);
1414 if (padding != 0) {
1415 iov[iov_count].iov_base = cmd->pad_bytes;
1416 iov[iov_count++].iov_len = padding;
1417 rx_size += padding;
1418 pr_debug("Receiving %u padding bytes.\n", padding);
1419 }
1420
1421 if (conn->conn_ops->DataDigest) {
1422 iov[iov_count].iov_base = &checksum;
1423 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1424 rx_size += ISCSI_CRC_LEN;
1425 }
1426
1427 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1428
1429 iscsit_unmap_iovec(cmd);
1430
1431 if (rx_got != rx_size)
1432 return -1;
1433
1434 if (conn->conn_ops->DataDigest) {
1435 u32 data_crc;
1436
1437 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001438 be32_to_cpu(hdr->offset),
1439 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001440 cmd->pad_bytes);
1441
1442 if (checksum != data_crc) {
1443 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1444 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1445 " does not match computed 0x%08x\n",
1446 hdr->itt, hdr->offset, payload_length,
1447 hdr->datasn, checksum, data_crc);
1448 data_crc_failed = 1;
1449 } else {
1450 pr_debug("Got CRC32C DataDigest 0x%08x for"
1451 " %u bytes of Data Out\n", checksum,
1452 payload_length);
1453 }
1454 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001455
1456 return data_crc_failed;
1457}
1458
1459int
1460iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1461 bool data_crc_failed)
1462{
1463 struct iscsi_conn *conn = cmd->conn;
1464 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001465 /*
1466 * Increment post receive data and CRC values or perform
1467 * within-command recovery.
1468 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001469 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1470 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001471 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001472 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001473 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001474 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1475 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001476 /*
1477 * Handle extra special case for out of order
1478 * Unsolicited Data Out.
1479 */
1480 spin_lock_bh(&cmd->istate_lock);
1481 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1482 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1483 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1484 spin_unlock_bh(&cmd->istate_lock);
1485
1486 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001487 if (ooo_cmdsn)
1488 return 0;
1489 target_execute_cmd(&cmd->se_cmd);
1490 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001491 } else /* DATAOUT_CANNOT_RECOVER */
1492 return -1;
1493
1494 return 0;
1495}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001496EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001497
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001498static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1499{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001500 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001501 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1502 int rc;
1503 bool data_crc_failed = false;
1504
1505 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1506 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001507 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001508 else if (!cmd)
1509 return 0;
1510
1511 rc = iscsit_get_dataout(conn, cmd, hdr);
1512 if (rc < 0)
1513 return rc;
1514 else if (rc > 0)
1515 data_crc_failed = true;
1516
1517 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1518}
1519
Nicholas Bellinger778de362013-06-14 16:07:47 -07001520int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1521 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001522{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001523 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524
Arshad Hussaina3662602014-03-14 15:28:59 -07001525 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1526 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1527 if (!cmd)
1528 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1529 (unsigned char *)hdr);
1530
1531 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1532 (unsigned char *)hdr);
1533 }
1534
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001535 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001536 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1537 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001538 if (!cmd)
1539 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1540 (unsigned char *)hdr);
1541
Nicholas Bellingerba159912013-07-03 03:48:24 -07001542 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1543 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 }
1545
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001546 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001547 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001548 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001549 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001550 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001551 if (!cmd)
1552 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1553 (unsigned char *)hdr);
1554
Nicholas Bellingerba159912013-07-03 03:48:24 -07001555 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1556 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001557 }
1558
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001559 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001560 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001561 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001562 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1563 payload_length);
1564 /*
1565 * This is not a response to a Unsolicited NopIN, which means
1566 * it can either be a NOPOUT ping request (with a valid ITT),
1567 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1568 * Either way, make sure we allocate an struct iscsi_cmd, as both
1569 * can contain ping data.
1570 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001571 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001572 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1573 cmd->i_state = ISTATE_SEND_NOPIN;
1574 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1575 1 : 0);
1576 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1577 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001578 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1579 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001580 cmd->data_direction = DMA_NONE;
1581 }
1582
Nicholas Bellinger778de362013-06-14 16:07:47 -07001583 return 0;
1584}
1585EXPORT_SYMBOL(iscsit_setup_nop_out);
1586
1587int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1588 struct iscsi_nopout *hdr)
1589{
1590 struct iscsi_cmd *cmd_p = NULL;
1591 int cmdsn_ret = 0;
1592 /*
1593 * Initiator is expecting a NopIN ping reply..
1594 */
1595 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001596 if (!cmd)
1597 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1598 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001599
1600 spin_lock_bh(&conn->cmd_lock);
1601 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1602 spin_unlock_bh(&conn->cmd_lock);
1603
1604 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1605
1606 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1607 iscsit_add_cmd_to_response_queue(cmd, conn,
1608 cmd->i_state);
1609 return 0;
1610 }
1611
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001612 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1613 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001614 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1615 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001616 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001617 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001618
1619 return 0;
1620 }
1621 /*
1622 * This was a response to a unsolicited NOPIN ping.
1623 */
1624 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1625 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1626 if (!cmd_p)
1627 return -EINVAL;
1628
1629 iscsit_stop_nopin_response_timer(conn);
1630
1631 cmd_p->i_state = ISTATE_REMOVE;
1632 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1633
1634 iscsit_start_nopin_timer(conn);
1635 return 0;
1636 }
1637 /*
1638 * Otherwise, initiator is not expecting a NOPIN is response.
1639 * Just ignore for now.
1640 */
1641 return 0;
1642}
1643EXPORT_SYMBOL(iscsit_process_nop_out);
1644
1645static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1646 unsigned char *buf)
1647{
1648 unsigned char *ping_data = NULL;
1649 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1650 struct kvec *iov = NULL;
1651 u32 payload_length = ntoh24(hdr->dlength);
1652 int ret;
1653
1654 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1655 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001656 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001657 /*
1658 * Handle NOP-OUT payload for traditional iSCSI sockets
1659 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001660 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001661 u32 checksum, data_crc, padding = 0;
1662 int niov = 0, rx_got, rx_size = payload_length;
1663
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001664 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1665 if (!ping_data) {
1666 pr_err("Unable to allocate memory for"
1667 " NOPOUT ping data.\n");
1668 ret = -1;
1669 goto out;
1670 }
1671
1672 iov = &cmd->iov_misc[0];
1673 iov[niov].iov_base = ping_data;
1674 iov[niov++].iov_len = payload_length;
1675
1676 padding = ((-payload_length) & 3);
1677 if (padding != 0) {
1678 pr_debug("Receiving %u additional bytes"
1679 " for padding.\n", padding);
1680 iov[niov].iov_base = &cmd->pad_bytes;
1681 iov[niov++].iov_len = padding;
1682 rx_size += padding;
1683 }
1684 if (conn->conn_ops->DataDigest) {
1685 iov[niov].iov_base = &checksum;
1686 iov[niov++].iov_len = ISCSI_CRC_LEN;
1687 rx_size += ISCSI_CRC_LEN;
1688 }
1689
1690 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1691 if (rx_got != rx_size) {
1692 ret = -1;
1693 goto out;
1694 }
1695
1696 if (conn->conn_ops->DataDigest) {
1697 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1698 ping_data, payload_length,
1699 padding, cmd->pad_bytes,
1700 (u8 *)&data_crc);
1701
1702 if (checksum != data_crc) {
1703 pr_err("Ping data CRC32C DataDigest"
1704 " 0x%08x does not match computed 0x%08x\n",
1705 checksum, data_crc);
1706 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1707 pr_err("Unable to recover from"
1708 " NOPOUT Ping DataCRC failure while in"
1709 " ERL=0.\n");
1710 ret = -1;
1711 goto out;
1712 } else {
1713 /*
1714 * Silently drop this PDU and let the
1715 * initiator plug the CmdSN gap.
1716 */
1717 pr_debug("Dropping NOPOUT"
1718 " Command CmdSN: 0x%08x due to"
1719 " DataCRC error.\n", hdr->cmdsn);
1720 ret = 0;
1721 goto out;
1722 }
1723 } else {
1724 pr_debug("Got CRC32C DataDigest"
1725 " 0x%08x for %u bytes of ping data.\n",
1726 checksum, payload_length);
1727 }
1728 }
1729
1730 ping_data[payload_length] = '\0';
1731 /*
1732 * Attach ping data to struct iscsi_cmd->buf_ptr.
1733 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001734 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735 cmd->buf_ptr_size = payload_length;
1736
1737 pr_debug("Got %u bytes of NOPOUT ping"
1738 " data.\n", payload_length);
1739 pr_debug("Ping Data: \"%s\"\n", ping_data);
1740 }
1741
Nicholas Bellinger778de362013-06-14 16:07:47 -07001742 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743out:
1744 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001745 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001746
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 kfree(ping_data);
1748 return ret;
1749}
1750
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001751int
1752iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1753 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 struct se_tmr_req *se_tmr;
1756 struct iscsi_tmr_req *tmr_req;
1757 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001758 int out_of_order_cmdsn = 0, ret;
1759 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001760 u8 function;
1761
1762 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001763 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1764 function = hdr->flags;
1765
1766 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1767 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1768 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1769 hdr->rtt, hdr->refcmdsn, conn->cid);
1770
1771 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1772 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001773 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001774 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001775 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001776 }
1777
1778 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1779 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1780 pr_err("Task Management Request TASK_REASSIGN not"
1781 " issued as immediate command, bad iSCSI Initiator"
1782 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001783 return iscsit_add_reject_cmd(cmd,
1784 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001785 }
1786 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001787 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1788 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001789
Andy Groverd28b11692012-04-03 15:51:22 -07001790 cmd->data_direction = DMA_NONE;
1791
1792 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1793 if (!cmd->tmr_req) {
1794 pr_err("Unable to allocate memory for"
1795 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001796 return iscsit_add_reject_cmd(cmd,
1797 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1798 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001799 }
1800
1801 /*
1802 * TASK_REASSIGN for ERL=2 / connection stays inside of
1803 * LIO-Target $FABRIC_MOD
1804 */
1805 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1806
1807 u8 tcm_function;
1808 int ret;
1809
1810 transport_init_se_cmd(&cmd->se_cmd,
1811 &lio_target_fabric_configfs->tf_ops,
1812 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001813 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001814
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001815 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1816 sess_ref = true;
1817
Andy Groverd28b11692012-04-03 15:51:22 -07001818 switch (function) {
1819 case ISCSI_TM_FUNC_ABORT_TASK:
1820 tcm_function = TMR_ABORT_TASK;
1821 break;
1822 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1823 tcm_function = TMR_ABORT_TASK_SET;
1824 break;
1825 case ISCSI_TM_FUNC_CLEAR_ACA:
1826 tcm_function = TMR_CLEAR_ACA;
1827 break;
1828 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1829 tcm_function = TMR_CLEAR_TASK_SET;
1830 break;
1831 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1832 tcm_function = TMR_LUN_RESET;
1833 break;
1834 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1835 tcm_function = TMR_TARGET_WARM_RESET;
1836 break;
1837 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1838 tcm_function = TMR_TARGET_COLD_RESET;
1839 break;
1840 default:
1841 pr_err("Unknown iSCSI TMR Function:"
1842 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001843 return iscsit_add_reject_cmd(cmd,
1844 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001845 }
1846
1847 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1848 tcm_function, GFP_KERNEL);
1849 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001850 return iscsit_add_reject_cmd(cmd,
1851 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001852
1853 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1854 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001855
1856 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1857 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1858 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1859 cmd->init_task_tag = hdr->itt;
1860 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001861 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1862 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001863 se_tmr = cmd->se_cmd.se_tmr_req;
1864 tmr_req = cmd->tmr_req;
1865 /*
1866 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1867 */
1868 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001869 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1870 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001871 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001872 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1873 goto attach;
1874 }
1875 }
1876
1877 switch (function) {
1878 case ISCSI_TM_FUNC_ABORT_TASK:
1879 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001880 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001882 break;
1883 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1884 case ISCSI_TM_FUNC_CLEAR_ACA:
1885 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1886 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1887 break;
1888 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1889 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001890 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1891 goto attach;
1892 }
1893 break;
1894 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1895 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001896 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1897 goto attach;
1898 }
1899 break;
1900 case ISCSI_TM_FUNC_TASK_REASSIGN:
1901 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1902 /*
1903 * Perform sanity checks on the ExpDataSN only if the
1904 * TASK_REASSIGN was successful.
1905 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001906 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001907 break;
1908
1909 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001910 return iscsit_add_reject_cmd(cmd,
1911 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001912 break;
1913 default:
1914 pr_err("Unknown TMR function: 0x%02x, protocol"
1915 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001916 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1917 goto attach;
1918 }
1919
1920 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1921 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1922 se_tmr->call_transport = 1;
1923attach:
1924 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001925 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001926 spin_unlock_bh(&conn->cmd_lock);
1927
1928 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001929 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001930 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1931 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001932 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001933 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001934 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001935 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001936 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001937 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001939 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001940 return 0;
1941 /*
1942 * Found the referenced task, send to transport for processing.
1943 */
1944 if (se_tmr->call_transport)
1945 return transport_generic_handle_tmr(&cmd->se_cmd);
1946
1947 /*
1948 * Could not find the referenced LUN, task, or Task Management
1949 * command not authorized or supported. Change state and
1950 * let the tx_thread send the response.
1951 *
1952 * For connection recovery, this is also the default action for
1953 * TMR TASK_REASSIGN.
1954 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001955 if (sess_ref) {
1956 pr_debug("Handle TMR, using sess_ref=true check\n");
1957 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1958 }
1959
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001960 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1961 return 0;
1962}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001963EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001964
1965/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001966int
1967iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1968 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001969{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001970 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001971
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001972 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001973 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001974 "greater than MaxXmitDataSegmentLength %u.\n",
1975 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001976 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1977 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001978 }
1979
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001980 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1981 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1982 pr_err("Multi sequence text commands currently not supported\n");
1983 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1984 (unsigned char *)hdr);
1985 }
1986
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001987 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1988 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1989 hdr->exp_statsn, payload_length);
1990
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001991 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1992 cmd->i_state = ISTATE_SEND_TEXTRSP;
1993 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1994 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1995 cmd->targ_xfer_tag = 0xFFFFFFFF;
1996 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1997 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1998 cmd->data_direction = DMA_NONE;
1999
2000 return 0;
2001}
2002EXPORT_SYMBOL(iscsit_setup_text_cmd);
2003
2004int
2005iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2006 struct iscsi_text *hdr)
2007{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002008 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002009 int cmdsn_ret;
2010
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002011 if (!text_in) {
2012 pr_err("Unable to locate text_in buffer for sendtargets"
2013 " discovery\n");
2014 goto reject;
2015 }
2016 if (strncmp("SendTargets", text_in, 11) != 0) {
2017 pr_err("Received Text Data that is not"
2018 " SendTargets, cannot continue.\n");
2019 goto reject;
2020 }
2021 text_ptr = strchr(text_in, '=');
2022 if (!text_ptr) {
2023 pr_err("No \"=\" separator found in Text Data,"
2024 " cannot continue.\n");
2025 goto reject;
2026 }
2027 if (!strncmp("=All", text_ptr, 4)) {
2028 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002029 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2030 !strncmp("=eui.", text_ptr, 5)) {
2031 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002032 } else {
2033 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2034 goto reject;
2035 }
2036
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002037 spin_lock_bh(&conn->cmd_lock);
2038 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2039 spin_unlock_bh(&conn->cmd_lock);
2040
2041 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2042
2043 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002044 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2045 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002046 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002047 return -1;
2048
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002049 return 0;
2050 }
2051
2052 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002053
2054reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002055 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2056 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002057}
2058EXPORT_SYMBOL(iscsit_process_text_cmd);
2059
2060static int
2061iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2062 unsigned char *buf)
2063{
2064 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2065 char *text_in = NULL;
2066 u32 payload_length = ntoh24(hdr->dlength);
2067 int rx_size, rc;
2068
2069 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2070 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002071 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002072
2073 rx_size = payload_length;
2074 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002075 u32 checksum = 0, data_crc = 0;
2076 u32 padding = 0, pad_bytes = 0;
2077 int niov = 0, rx_got;
2078 struct kvec iov[3];
2079
2080 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002081 if (!text_in) {
2082 pr_err("Unable to allocate memory for"
2083 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002084 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002086 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002087
2088 memset(iov, 0, 3 * sizeof(struct kvec));
2089 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002090 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002091
2092 padding = ((-payload_length) & 3);
2093 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002094 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002095 iov[niov++].iov_len = padding;
2096 rx_size += padding;
2097 pr_debug("Receiving %u additional bytes"
2098 " for padding.\n", padding);
2099 }
2100 if (conn->conn_ops->DataDigest) {
2101 iov[niov].iov_base = &checksum;
2102 iov[niov++].iov_len = ISCSI_CRC_LEN;
2103 rx_size += ISCSI_CRC_LEN;
2104 }
2105
2106 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002107 if (rx_got != rx_size)
2108 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002109
2110 if (conn->conn_ops->DataDigest) {
2111 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002112 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002113 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002114 (u8 *)&data_crc);
2115
2116 if (checksum != data_crc) {
2117 pr_err("Text data CRC32C DataDigest"
2118 " 0x%08x does not match computed"
2119 " 0x%08x\n", checksum, data_crc);
2120 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2121 pr_err("Unable to recover from"
2122 " Text Data digest failure while in"
2123 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002124 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125 } else {
2126 /*
2127 * Silently drop this PDU and let the
2128 * initiator plug the CmdSN gap.
2129 */
2130 pr_debug("Dropping Text"
2131 " Command CmdSN: 0x%08x due to"
2132 " DataCRC error.\n", hdr->cmdsn);
2133 kfree(text_in);
2134 return 0;
2135 }
2136 } else {
2137 pr_debug("Got CRC32C DataDigest"
2138 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002139 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002140 }
2141 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002142 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002143 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002144 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002145 }
2146
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002147 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002148
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002149reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002150 kfree(cmd->text_in_ptr);
2151 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002152 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002153}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002154EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002155
2156int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2157{
2158 struct iscsi_conn *conn_p;
2159 struct iscsi_session *sess = conn->sess;
2160
2161 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2162 " for SID: %u.\n", conn->cid, conn->sess->sid);
2163
2164 atomic_set(&sess->session_logout, 1);
2165 atomic_set(&conn->conn_logout_remove, 1);
2166 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2167
2168 iscsit_inc_conn_usage_count(conn);
2169 iscsit_inc_session_usage_count(sess);
2170
2171 spin_lock_bh(&sess->conn_lock);
2172 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2173 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2174 continue;
2175
2176 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2177 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2178 }
2179 spin_unlock_bh(&sess->conn_lock);
2180
2181 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2182
2183 return 0;
2184}
2185
2186int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2187{
2188 struct iscsi_conn *l_conn;
2189 struct iscsi_session *sess = conn->sess;
2190
2191 pr_debug("Received logout request CLOSECONNECTION for CID:"
2192 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2193
2194 /*
2195 * A Logout Request with a CLOSECONNECTION reason code for a CID
2196 * can arrive on a connection with a differing CID.
2197 */
2198 if (conn->cid == cmd->logout_cid) {
2199 spin_lock_bh(&conn->state_lock);
2200 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2201 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2202
2203 atomic_set(&conn->conn_logout_remove, 1);
2204 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2205 iscsit_inc_conn_usage_count(conn);
2206
2207 spin_unlock_bh(&conn->state_lock);
2208 } else {
2209 /*
2210 * Handle all different cid CLOSECONNECTION requests in
2211 * iscsit_logout_post_handler_diffcid() as to give enough
2212 * time for any non immediate command's CmdSN to be
2213 * acknowledged on the connection in question.
2214 *
2215 * Here we simply make sure the CID is still around.
2216 */
2217 l_conn = iscsit_get_conn_from_cid(sess,
2218 cmd->logout_cid);
2219 if (!l_conn) {
2220 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2221 iscsit_add_cmd_to_response_queue(cmd, conn,
2222 cmd->i_state);
2223 return 0;
2224 }
2225
2226 iscsit_dec_conn_usage_count(l_conn);
2227 }
2228
2229 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2230
2231 return 0;
2232}
2233
2234int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2235{
2236 struct iscsi_session *sess = conn->sess;
2237
2238 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2239 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2240
2241 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2242 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2243 " while ERL!=2.\n");
2244 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2245 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2246 return 0;
2247 }
2248
2249 if (conn->cid == cmd->logout_cid) {
2250 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2251 " with CID: %hu on CID: %hu, implementation error.\n",
2252 cmd->logout_cid, conn->cid);
2253 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2254 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2255 return 0;
2256 }
2257
2258 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2259
2260 return 0;
2261}
2262
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002263int
2264iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2265 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266{
2267 int cmdsn_ret, logout_remove = 0;
2268 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002269 struct iscsi_logout *hdr;
2270 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2271
2272 hdr = (struct iscsi_logout *) buf;
2273 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002274
2275 if (tiqn) {
2276 spin_lock(&tiqn->logout_stats.lock);
2277 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2278 tiqn->logout_stats.normal_logouts++;
2279 else
2280 tiqn->logout_stats.abnormal_logouts++;
2281 spin_unlock(&tiqn->logout_stats.lock);
2282 }
2283
2284 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2285 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2286 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2287 hdr->cid, conn->cid);
2288
2289 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2290 pr_err("Received logout request on connection that"
2291 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002292 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002293 return 0;
2294 }
2295
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2297 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2298 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2299 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2300 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002301 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2302 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2303 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002304 cmd->logout_reason = reason_code;
2305 cmd->data_direction = DMA_NONE;
2306
2307 /*
2308 * We need to sleep in these cases (by returning 1) until the Logout
2309 * Response gets sent in the tx thread.
2310 */
2311 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2312 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002313 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314 logout_remove = 1;
2315
2316 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002317 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002318 spin_unlock_bh(&conn->cmd_lock);
2319
2320 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002321 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322
2323 /*
2324 * Immediate commands are executed, well, immediately.
2325 * Non-Immediate Logout Commands are executed in CmdSN order.
2326 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002327 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002328 int ret = iscsit_execute_cmd(cmd, 0);
2329
2330 if (ret < 0)
2331 return ret;
2332 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002333 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002334 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002335 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002336 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2337 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002338 }
2339
2340 return logout_remove;
2341}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002342EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002343
2344static int iscsit_handle_snack(
2345 struct iscsi_conn *conn,
2346 unsigned char *buf)
2347{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002348 struct iscsi_snack *hdr;
2349
2350 hdr = (struct iscsi_snack *) buf;
2351 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002352
2353 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2354 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2355 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2356 hdr->begrun, hdr->runlength, conn->cid);
2357
2358 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2359 pr_err("Initiator sent SNACK request while in"
2360 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002361 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2362 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002363 }
2364 /*
2365 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2366 * call from inside iscsi_send_recovery_datain_or_r2t().
2367 */
2368 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2369 case 0:
2370 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002371 hdr->itt,
2372 be32_to_cpu(hdr->ttt),
2373 be32_to_cpu(hdr->begrun),
2374 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002375 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002376 return iscsit_handle_status_snack(conn, hdr->itt,
2377 be32_to_cpu(hdr->ttt),
2378 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002379 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002380 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2381 be32_to_cpu(hdr->begrun),
2382 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002383 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2384 /* FIXME: Support R-Data SNACK */
2385 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002386 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2387 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002388 default:
2389 pr_err("Unknown SNACK type 0x%02x, protocol"
2390 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002391 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2392 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002393 }
2394
2395 return 0;
2396}
2397
2398static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2399{
2400 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2401 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2402 wait_for_completion_interruptible_timeout(
2403 &conn->rx_half_close_comp,
2404 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2405 }
2406}
2407
2408static int iscsit_handle_immediate_data(
2409 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002410 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002411 u32 length)
2412{
2413 int iov_ret, rx_got = 0, rx_size = 0;
2414 u32 checksum, iov_count = 0, padding = 0;
2415 struct iscsi_conn *conn = cmd->conn;
2416 struct kvec *iov;
2417
2418 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2419 if (iov_ret < 0)
2420 return IMMEDIATE_DATA_CANNOT_RECOVER;
2421
2422 rx_size = length;
2423 iov_count = iov_ret;
2424 iov = &cmd->iov_data[0];
2425
2426 padding = ((-length) & 3);
2427 if (padding != 0) {
2428 iov[iov_count].iov_base = cmd->pad_bytes;
2429 iov[iov_count++].iov_len = padding;
2430 rx_size += padding;
2431 }
2432
2433 if (conn->conn_ops->DataDigest) {
2434 iov[iov_count].iov_base = &checksum;
2435 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2436 rx_size += ISCSI_CRC_LEN;
2437 }
2438
2439 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2440
2441 iscsit_unmap_iovec(cmd);
2442
2443 if (rx_got != rx_size) {
2444 iscsit_rx_thread_wait_for_tcp(conn);
2445 return IMMEDIATE_DATA_CANNOT_RECOVER;
2446 }
2447
2448 if (conn->conn_ops->DataDigest) {
2449 u32 data_crc;
2450
2451 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2452 cmd->write_data_done, length, padding,
2453 cmd->pad_bytes);
2454
2455 if (checksum != data_crc) {
2456 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2457 " does not match computed 0x%08x\n", checksum,
2458 data_crc);
2459
2460 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2461 pr_err("Unable to recover from"
2462 " Immediate Data digest failure while"
2463 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002464 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002465 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002466 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002467 return IMMEDIATE_DATA_CANNOT_RECOVER;
2468 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002469 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002470 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002471 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002472 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2473 }
2474 } else {
2475 pr_debug("Got CRC32C DataDigest 0x%08x for"
2476 " %u bytes of Immediate Data\n", checksum,
2477 length);
2478 }
2479 }
2480
2481 cmd->write_data_done += length;
2482
Andy Groverebf1d952012-04-03 15:51:24 -07002483 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002484 spin_lock_bh(&cmd->istate_lock);
2485 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2486 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2487 spin_unlock_bh(&cmd->istate_lock);
2488 }
2489
2490 return IMMEDIATE_DATA_NORMAL_OPERATION;
2491}
2492
2493/*
2494 * Called with sess->conn_lock held.
2495 */
2496/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2497 with active network interface */
2498static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2499{
2500 struct iscsi_cmd *cmd;
2501 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002502 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002503
2504 /*
2505 * Only send a Asynchronous Message on connections whos network
2506 * interface is still functional.
2507 */
2508 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2509 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2510 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002511 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002512 break;
2513 }
2514 }
2515
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002516 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002517 return;
2518
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002519 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520 if (!cmd) {
2521 iscsit_dec_conn_usage_count(conn_p);
2522 return;
2523 }
2524
2525 cmd->logout_cid = conn->cid;
2526 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2527 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2528
2529 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002530 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002531 spin_unlock_bh(&conn_p->cmd_lock);
2532
2533 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2534 iscsit_dec_conn_usage_count(conn_p);
2535}
2536
2537static int iscsit_send_conn_drop_async_message(
2538 struct iscsi_cmd *cmd,
2539 struct iscsi_conn *conn)
2540{
2541 struct iscsi_async *hdr;
2542
2543 cmd->tx_size = ISCSI_HDR_LEN;
2544 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2545
2546 hdr = (struct iscsi_async *) cmd->pdu;
2547 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2548 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002549 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002550 cmd->targ_xfer_tag = 0xFFFFFFFF;
2551 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2552 cmd->stat_sn = conn->stat_sn++;
2553 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2554 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2555 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2556 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2557 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2558 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2559 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2560
2561 if (conn->conn_ops->HeaderDigest) {
2562 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2563
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002564 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2565 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002566
2567 cmd->tx_size += ISCSI_CRC_LEN;
2568 pr_debug("Attaching CRC32C HeaderDigest to"
2569 " Async Message 0x%08x\n", *header_digest);
2570 }
2571
2572 cmd->iov_misc[0].iov_base = cmd->pdu;
2573 cmd->iov_misc[0].iov_len = cmd->tx_size;
2574 cmd->iov_misc_count = 1;
2575
2576 pr_debug("Sending Connection Dropped Async Message StatSN:"
2577 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2578 cmd->logout_cid, conn->cid);
2579 return 0;
2580}
2581
Andy Grover6f3c0e62012-04-03 15:51:09 -07002582static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2583{
2584 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2585 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2586 wait_for_completion_interruptible_timeout(
2587 &conn->tx_half_close_comp,
2588 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2589 }
2590}
2591
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002592static void
2593iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2594 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2595 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002596{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002597 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2598 hdr->flags = datain->flags;
2599 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2600 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2601 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2602 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2603 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2604 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2605 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2606 }
2607 }
2608 hton24(hdr->dlength, datain->length);
2609 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2610 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2611 (struct scsi_lun *)&hdr->lun);
2612 else
2613 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2614
2615 hdr->itt = cmd->init_task_tag;
2616
2617 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2618 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2619 else
2620 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2621 if (set_statsn)
2622 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2623 else
2624 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2625
2626 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2627 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2628 hdr->datasn = cpu_to_be32(datain->data_sn);
2629 hdr->offset = cpu_to_be32(datain->offset);
2630
2631 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2632 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2633 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2634 ntohl(hdr->offset), datain->length, conn->cid);
2635}
2636
2637static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2638{
2639 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002640 struct iscsi_datain datain;
2641 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002642 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002643 u32 iov_count = 0, tx_size = 0;
2644 int eodr = 0, ret, iov_ret;
2645 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646
2647 memset(&datain, 0, sizeof(struct iscsi_datain));
2648 dr = iscsit_get_datain_values(cmd, &datain);
2649 if (!dr) {
2650 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2651 cmd->init_task_tag);
2652 return -1;
2653 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654 /*
2655 * Be paranoid and double check the logic for now.
2656 */
Andy Groverebf1d952012-04-03 15:51:24 -07002657 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002658 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2659 " datain.length: %u exceeds cmd->data_length: %u\n",
2660 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002661 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002662 return -1;
2663 }
2664
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002665 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002666 /*
2667 * Special case for successfully execution w/ both DATAIN
2668 * and Sense Data.
2669 */
2670 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2671 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2672 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2673 else {
2674 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2675 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2676 iscsit_increment_maxcmdsn(cmd, conn->sess);
2677 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002678 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002679 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002680 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2681 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682 }
2683
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002684 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002685
2686 iov = &cmd->iov_data[0];
2687 iov[iov_count].iov_base = cmd->pdu;
2688 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2689 tx_size += ISCSI_HDR_LEN;
2690
2691 if (conn->conn_ops->HeaderDigest) {
2692 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2693
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002694 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2695 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002696
2697 iov[0].iov_len += ISCSI_CRC_LEN;
2698 tx_size += ISCSI_CRC_LEN;
2699
2700 pr_debug("Attaching CRC32 HeaderDigest"
2701 " for DataIN PDU 0x%08x\n", *header_digest);
2702 }
2703
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002704 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2705 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002706 if (iov_ret < 0)
2707 return -1;
2708
2709 iov_count += iov_ret;
2710 tx_size += datain.length;
2711
2712 cmd->padding = ((-datain.length) & 3);
2713 if (cmd->padding) {
2714 iov[iov_count].iov_base = cmd->pad_bytes;
2715 iov[iov_count++].iov_len = cmd->padding;
2716 tx_size += cmd->padding;
2717
2718 pr_debug("Attaching %u padding bytes\n",
2719 cmd->padding);
2720 }
2721 if (conn->conn_ops->DataDigest) {
2722 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2723 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2724
2725 iov[iov_count].iov_base = &cmd->data_crc;
2726 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2727 tx_size += ISCSI_CRC_LEN;
2728
2729 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2730 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2731 }
2732
2733 cmd->iov_data_count = iov_count;
2734 cmd->tx_size = tx_size;
2735
Andy Grover6f3c0e62012-04-03 15:51:09 -07002736 /* sendpage is preferred but can't insert markers */
2737 if (!conn->conn_ops->IFMarker)
2738 ret = iscsit_fe_sendpage_sg(cmd, conn);
2739 else
2740 ret = iscsit_send_tx_data(cmd, conn, 0);
2741
2742 iscsit_unmap_iovec(cmd);
2743
2744 if (ret < 0) {
2745 iscsit_tx_thread_wait_for_tcp(conn);
2746 return ret;
2747 }
2748
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002749 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002750 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002751 2 : 1;
2752 iscsit_free_datain_req(cmd, dr);
2753 }
2754
Andy Grover6f3c0e62012-04-03 15:51:09 -07002755 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002756}
2757
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002758int
2759iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2760 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002761{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002762 struct iscsi_conn *logout_conn = NULL;
2763 struct iscsi_conn_recovery *cr = NULL;
2764 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002765 /*
2766 * The actual shutting down of Sessions and/or Connections
2767 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2768 * is done in scsi_logout_post_handler().
2769 */
2770 switch (cmd->logout_reason) {
2771 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2772 pr_debug("iSCSI session logout successful, setting"
2773 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2774 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2775 break;
2776 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2777 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2778 break;
2779 /*
2780 * For CLOSECONNECTION logout requests carrying
2781 * a matching logout CID -> local CID, the reference
2782 * for the local CID will have been incremented in
2783 * iscsi_logout_closeconnection().
2784 *
2785 * For CLOSECONNECTION logout requests carrying
2786 * a different CID than the connection it arrived
2787 * on, the connection responding to cmd->logout_cid
2788 * is stopped in iscsit_logout_post_handler_diffcid().
2789 */
2790
2791 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2792 " successful.\n", cmd->logout_cid, conn->cid);
2793 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2794 break;
2795 case ISCSI_LOGOUT_REASON_RECOVERY:
2796 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2797 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2798 break;
2799 /*
2800 * If the connection is still active from our point of view
2801 * force connection recovery to occur.
2802 */
2803 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2804 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002805 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002806 iscsit_connection_reinstatement_rcfr(logout_conn);
2807 iscsit_dec_conn_usage_count(logout_conn);
2808 }
2809
2810 cr = iscsit_get_inactive_connection_recovery_entry(
2811 conn->sess, cmd->logout_cid);
2812 if (!cr) {
2813 pr_err("Unable to locate CID: %hu for"
2814 " REMOVECONNFORRECOVERY Logout Request.\n",
2815 cmd->logout_cid);
2816 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2817 break;
2818 }
2819
2820 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2821
2822 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2823 " for recovery for CID: %hu on CID: %hu successful.\n",
2824 cmd->logout_cid, conn->cid);
2825 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2826 break;
2827 default:
2828 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2829 cmd->logout_reason);
2830 return -1;
2831 }
2832
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002833 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2834 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2835 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002836 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002837 cmd->stat_sn = conn->stat_sn++;
2838 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2839
2840 iscsit_increment_maxcmdsn(cmd, conn->sess);
2841 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2842 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2843
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002844 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2845 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2846 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2847 cmd->logout_cid, conn->cid);
2848
2849 return 0;
2850}
2851EXPORT_SYMBOL(iscsit_build_logout_rsp);
2852
2853static int
2854iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2855{
2856 struct kvec *iov;
2857 int niov = 0, tx_size, rc;
2858
2859 rc = iscsit_build_logout_rsp(cmd, conn,
2860 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2861 if (rc < 0)
2862 return rc;
2863
2864 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002865 iov = &cmd->iov_misc[0];
2866 iov[niov].iov_base = cmd->pdu;
2867 iov[niov++].iov_len = ISCSI_HDR_LEN;
2868
2869 if (conn->conn_ops->HeaderDigest) {
2870 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2871
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002872 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2873 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002874
2875 iov[0].iov_len += ISCSI_CRC_LEN;
2876 tx_size += ISCSI_CRC_LEN;
2877 pr_debug("Attaching CRC32C HeaderDigest to"
2878 " Logout Response 0x%08x\n", *header_digest);
2879 }
2880 cmd->iov_misc_count = niov;
2881 cmd->tx_size = tx_size;
2882
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002883 return 0;
2884}
2885
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002886void
2887iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2888 struct iscsi_nopin *hdr, bool nopout_response)
2889{
2890 hdr->opcode = ISCSI_OP_NOOP_IN;
2891 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2892 hton24(hdr->dlength, cmd->buf_ptr_size);
2893 if (nopout_response)
2894 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2895 hdr->itt = cmd->init_task_tag;
2896 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2897 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2898 conn->stat_sn;
2899 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2900
2901 if (nopout_response)
2902 iscsit_increment_maxcmdsn(cmd, conn->sess);
2903
2904 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2905 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2906
2907 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2908 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2909 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2910 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2911}
2912EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2913
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002914/*
2915 * Unsolicited NOPIN, either requesting a response or not.
2916 */
2917static int iscsit_send_unsolicited_nopin(
2918 struct iscsi_cmd *cmd,
2919 struct iscsi_conn *conn,
2920 int want_response)
2921{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002922 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2923 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002925 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002926
2927 if (conn->conn_ops->HeaderDigest) {
2928 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2929
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002930 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2931 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002932
2933 tx_size += ISCSI_CRC_LEN;
2934 pr_debug("Attaching CRC32C HeaderDigest to"
2935 " NopIN 0x%08x\n", *header_digest);
2936 }
2937
2938 cmd->iov_misc[0].iov_base = cmd->pdu;
2939 cmd->iov_misc[0].iov_len = tx_size;
2940 cmd->iov_misc_count = 1;
2941 cmd->tx_size = tx_size;
2942
2943 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2944 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2945
Andy Grover6f3c0e62012-04-03 15:51:09 -07002946 ret = iscsit_send_tx_data(cmd, conn, 1);
2947 if (ret < 0) {
2948 iscsit_tx_thread_wait_for_tcp(conn);
2949 return ret;
2950 }
2951
2952 spin_lock_bh(&cmd->istate_lock);
2953 cmd->i_state = want_response ?
2954 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2955 spin_unlock_bh(&cmd->istate_lock);
2956
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002957 return 0;
2958}
2959
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002960static int
2961iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002962{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002963 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002964 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002965 u32 padding = 0;
2966 int niov = 0, tx_size;
2967
2968 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002969
2970 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002971 iov = &cmd->iov_misc[0];
2972 iov[niov].iov_base = cmd->pdu;
2973 iov[niov++].iov_len = ISCSI_HDR_LEN;
2974
2975 if (conn->conn_ops->HeaderDigest) {
2976 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2977
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002978 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2979 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002980
2981 iov[0].iov_len += ISCSI_CRC_LEN;
2982 tx_size += ISCSI_CRC_LEN;
2983 pr_debug("Attaching CRC32C HeaderDigest"
2984 " to NopIn 0x%08x\n", *header_digest);
2985 }
2986
2987 /*
2988 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2989 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2990 */
2991 if (cmd->buf_ptr_size) {
2992 iov[niov].iov_base = cmd->buf_ptr;
2993 iov[niov++].iov_len = cmd->buf_ptr_size;
2994 tx_size += cmd->buf_ptr_size;
2995
2996 pr_debug("Echoing back %u bytes of ping"
2997 " data.\n", cmd->buf_ptr_size);
2998
2999 padding = ((-cmd->buf_ptr_size) & 3);
3000 if (padding != 0) {
3001 iov[niov].iov_base = &cmd->pad_bytes;
3002 iov[niov++].iov_len = padding;
3003 tx_size += padding;
3004 pr_debug("Attaching %u additional"
3005 " padding bytes.\n", padding);
3006 }
3007 if (conn->conn_ops->DataDigest) {
3008 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3009 cmd->buf_ptr, cmd->buf_ptr_size,
3010 padding, (u8 *)&cmd->pad_bytes,
3011 (u8 *)&cmd->data_crc);
3012
3013 iov[niov].iov_base = &cmd->data_crc;
3014 iov[niov++].iov_len = ISCSI_CRC_LEN;
3015 tx_size += ISCSI_CRC_LEN;
3016 pr_debug("Attached DataDigest for %u"
3017 " bytes of ping data, CRC 0x%08x\n",
3018 cmd->buf_ptr_size, cmd->data_crc);
3019 }
3020 }
3021
3022 cmd->iov_misc_count = niov;
3023 cmd->tx_size = tx_size;
3024
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003025 return 0;
3026}
3027
Andy Grover6f3c0e62012-04-03 15:51:09 -07003028static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003029 struct iscsi_cmd *cmd,
3030 struct iscsi_conn *conn)
3031{
3032 int tx_size = 0;
3033 struct iscsi_r2t *r2t;
3034 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003035 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003036
3037 r2t = iscsit_get_r2t_from_list(cmd);
3038 if (!r2t)
3039 return -1;
3040
3041 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3042 memset(hdr, 0, ISCSI_HDR_LEN);
3043 hdr->opcode = ISCSI_OP_R2T;
3044 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3045 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3046 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003047 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003048 spin_lock_bh(&conn->sess->ttt_lock);
3049 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3050 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3051 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3052 spin_unlock_bh(&conn->sess->ttt_lock);
3053 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3054 hdr->statsn = cpu_to_be32(conn->stat_sn);
3055 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3056 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3057 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3058 hdr->data_offset = cpu_to_be32(r2t->offset);
3059 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3060
3061 cmd->iov_misc[0].iov_base = cmd->pdu;
3062 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3063 tx_size += ISCSI_HDR_LEN;
3064
3065 if (conn->conn_ops->HeaderDigest) {
3066 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3067
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003068 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3069 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003070
3071 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3072 tx_size += ISCSI_CRC_LEN;
3073 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3074 " PDU 0x%08x\n", *header_digest);
3075 }
3076
3077 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3078 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3079 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3080 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3081 r2t->offset, r2t->xfer_len, conn->cid);
3082
3083 cmd->iov_misc_count = 1;
3084 cmd->tx_size = tx_size;
3085
3086 spin_lock_bh(&cmd->r2t_lock);
3087 r2t->sent_r2t = 1;
3088 spin_unlock_bh(&cmd->r2t_lock);
3089
Andy Grover6f3c0e62012-04-03 15:51:09 -07003090 ret = iscsit_send_tx_data(cmd, conn, 1);
3091 if (ret < 0) {
3092 iscsit_tx_thread_wait_for_tcp(conn);
3093 return ret;
3094 }
3095
3096 spin_lock_bh(&cmd->dataout_timeout_lock);
3097 iscsit_start_dataout_timer(cmd, conn);
3098 spin_unlock_bh(&cmd->dataout_timeout_lock);
3099
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003100 return 0;
3101}
3102
3103/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003104 * @recovery: If called from iscsi_task_reassign_complete_write() for
3105 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003106 */
3107int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003109 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003110 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003111{
3112 int first_r2t = 1;
3113 u32 offset = 0, xfer_len = 0;
3114
3115 spin_lock_bh(&cmd->r2t_lock);
3116 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3117 spin_unlock_bh(&cmd->r2t_lock);
3118 return 0;
3119 }
3120
Andy Grover8b1e1242012-04-03 15:51:12 -07003121 if (conn->sess->sess_ops->DataSequenceInOrder &&
3122 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003123 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003124
3125 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3126 if (conn->sess->sess_ops->DataSequenceInOrder) {
3127 offset = cmd->r2t_offset;
3128
Andy Grover8b1e1242012-04-03 15:51:12 -07003129 if (first_r2t && recovery) {
3130 int new_data_end = offset +
3131 conn->sess->sess_ops->MaxBurstLength -
3132 cmd->next_burst_len;
3133
Andy Groverebf1d952012-04-03 15:51:24 -07003134 if (new_data_end > cmd->se_cmd.data_length)
3135 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003136 else
3137 xfer_len =
3138 conn->sess->sess_ops->MaxBurstLength -
3139 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003140 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003141 int new_data_end = offset +
3142 conn->sess->sess_ops->MaxBurstLength;
3143
Andy Groverebf1d952012-04-03 15:51:24 -07003144 if (new_data_end > cmd->se_cmd.data_length)
3145 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003146 else
3147 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003148 }
3149 cmd->r2t_offset += xfer_len;
3150
Andy Groverebf1d952012-04-03 15:51:24 -07003151 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003152 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3153 } else {
3154 struct iscsi_seq *seq;
3155
3156 seq = iscsit_get_seq_holder_for_r2t(cmd);
3157 if (!seq) {
3158 spin_unlock_bh(&cmd->r2t_lock);
3159 return -1;
3160 }
3161
3162 offset = seq->offset;
3163 xfer_len = seq->xfer_len;
3164
3165 if (cmd->seq_send_order == cmd->seq_count)
3166 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3167 }
3168 cmd->outstanding_r2ts++;
3169 first_r2t = 0;
3170
3171 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3172 spin_unlock_bh(&cmd->r2t_lock);
3173 return -1;
3174 }
3175
3176 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3177 break;
3178 }
3179 spin_unlock_bh(&cmd->r2t_lock);
3180
3181 return 0;
3182}
3183
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003184void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3185 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003186{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003187 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003188 cmd->stat_sn = conn->stat_sn++;
3189
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003190 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003192 memset(hdr, 0, ISCSI_HDR_LEN);
3193 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3194 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3195 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3196 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003197 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003198 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3199 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003200 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003201 }
3202 hdr->response = cmd->iscsi_response;
3203 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003204 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003205 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3206
3207 iscsit_increment_maxcmdsn(cmd, conn->sess);
3208 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3209 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3210
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003211 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3212 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3213 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3214 cmd->se_cmd.scsi_status, conn->cid);
3215}
3216EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3217
3218static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3219{
3220 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3221 struct kvec *iov;
3222 u32 padding = 0, tx_size = 0;
3223 int iov_count = 0;
3224 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3225
3226 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3227
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003228 iov = &cmd->iov_misc[0];
3229 iov[iov_count].iov_base = cmd->pdu;
3230 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3231 tx_size += ISCSI_HDR_LEN;
3232
3233 /*
3234 * Attach SENSE DATA payload to iSCSI Response PDU
3235 */
3236 if (cmd->se_cmd.sense_buffer &&
3237 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3238 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003239 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3240 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3241
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003242 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003243 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003244 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003245 iov[iov_count++].iov_len =
3246 (cmd->se_cmd.scsi_sense_length + padding);
3247 tx_size += cmd->se_cmd.scsi_sense_length;
3248
3249 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003250 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003251 cmd->se_cmd.scsi_sense_length, 0, padding);
3252 tx_size += padding;
3253 pr_debug("Adding %u bytes of padding to"
3254 " SENSE.\n", padding);
3255 }
3256
3257 if (conn->conn_ops->DataDigest) {
3258 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003259 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260 (cmd->se_cmd.scsi_sense_length + padding),
3261 0, NULL, (u8 *)&cmd->data_crc);
3262
3263 iov[iov_count].iov_base = &cmd->data_crc;
3264 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3265 tx_size += ISCSI_CRC_LEN;
3266
3267 pr_debug("Attaching CRC32 DataDigest for"
3268 " SENSE, %u bytes CRC 0x%08x\n",
3269 (cmd->se_cmd.scsi_sense_length + padding),
3270 cmd->data_crc);
3271 }
3272
3273 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3274 " Response PDU\n",
3275 cmd->se_cmd.scsi_sense_length);
3276 }
3277
3278 if (conn->conn_ops->HeaderDigest) {
3279 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3280
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003281 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3282 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003283
3284 iov[0].iov_len += ISCSI_CRC_LEN;
3285 tx_size += ISCSI_CRC_LEN;
3286 pr_debug("Attaching CRC32 HeaderDigest for Response"
3287 " PDU 0x%08x\n", *header_digest);
3288 }
3289
3290 cmd->iov_misc_count = iov_count;
3291 cmd->tx_size = tx_size;
3292
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003293 return 0;
3294}
3295
3296static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3297{
3298 switch (se_tmr->response) {
3299 case TMR_FUNCTION_COMPLETE:
3300 return ISCSI_TMF_RSP_COMPLETE;
3301 case TMR_TASK_DOES_NOT_EXIST:
3302 return ISCSI_TMF_RSP_NO_TASK;
3303 case TMR_LUN_DOES_NOT_EXIST:
3304 return ISCSI_TMF_RSP_NO_LUN;
3305 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3306 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003307 case TMR_FUNCTION_REJECTED:
3308 default:
3309 return ISCSI_TMF_RSP_REJECTED;
3310 }
3311}
3312
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003313void
3314iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3315 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003316{
3317 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003318
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003319 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003320 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003321 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003322 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003323 cmd->stat_sn = conn->stat_sn++;
3324 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3325
3326 iscsit_increment_maxcmdsn(cmd, conn->sess);
3327 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3328 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3329
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003330 pr_debug("Built Task Management Response ITT: 0x%08x,"
3331 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3332 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3333}
3334EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3335
3336static int
3337iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3338{
3339 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3340 u32 tx_size = 0;
3341
3342 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3343
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003344 cmd->iov_misc[0].iov_base = cmd->pdu;
3345 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3346 tx_size += ISCSI_HDR_LEN;
3347
3348 if (conn->conn_ops->HeaderDigest) {
3349 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3350
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003351 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3352 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003353
3354 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3355 tx_size += ISCSI_CRC_LEN;
3356 pr_debug("Attaching CRC32 HeaderDigest for Task"
3357 " Mgmt Response PDU 0x%08x\n", *header_digest);
3358 }
3359
3360 cmd->iov_misc_count = 1;
3361 cmd->tx_size = tx_size;
3362
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003363 return 0;
3364}
3365
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003366static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3367{
3368 bool ret = false;
3369
3370 if (np->np_sockaddr.ss_family == AF_INET6) {
3371 const struct sockaddr_in6 sin6 = {
3372 .sin6_addr = IN6ADDR_ANY_INIT };
3373 struct sockaddr_in6 *sock_in6 =
3374 (struct sockaddr_in6 *)&np->np_sockaddr;
3375
3376 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3377 sin6.sin6_addr.s6_addr, 16))
3378 ret = true;
3379 } else {
3380 struct sockaddr_in * sock_in =
3381 (struct sockaddr_in *)&np->np_sockaddr;
3382
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003383 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003384 ret = true;
3385 }
3386
3387 return ret;
3388}
3389
Andy Grover8b1e1242012-04-03 15:51:12 -07003390#define SENDTARGETS_BUF_LIMIT 32768U
3391
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003392static int
3393iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3394 enum iscsit_transport_type network_transport)
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
Andy Grover8b1e1242012-04-03 15:51:12 -07003406 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3407 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 /*
3416 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3417 * explicit case..
3418 */
3419 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3420 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) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003435 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3436 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 }
3485 memcpy(payload + payload_len, buf, len);
3486 payload_len += len;
3487 target_name_printed = 1;
3488 }
3489
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003490 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003491 "%s:%hu,%hu",
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02003492 inaddr_any ? conn->local_ip : np->np_ip,
3493 inaddr_any ? conn->local_port : np->np_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003494 tpg->tpgt);
3495 len += 1;
3496
3497 if ((len + payload_len) > buffer_len) {
3498 spin_unlock(&tpg->tpg_np_lock);
3499 spin_unlock(&tiqn->tiqn_tpg_lock);
3500 end_of_buf = 1;
3501 goto eob;
3502 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003503 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003504 payload_len += len;
3505 }
3506 spin_unlock(&tpg->tpg_np_lock);
3507 }
3508 spin_unlock(&tiqn->tiqn_tpg_lock);
3509eob:
3510 if (end_of_buf)
3511 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003512
3513 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3514 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003515 }
3516 spin_unlock(&tiqn_lock);
3517
3518 cmd->buf_ptr = payload;
3519
3520 return payload_len;
3521}
3522
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003523int
3524iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003525 struct iscsi_text_rsp *hdr,
3526 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003527{
3528 int text_length, padding;
3529
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003530 text_length = iscsit_build_sendtargets_response(cmd, network_transport);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003531 if (text_length < 0)
3532 return text_length;
3533
3534 hdr->opcode = ISCSI_OP_TEXT_RSP;
3535 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3536 padding = ((-text_length) & 3);
3537 hton24(hdr->dlength, text_length);
3538 hdr->itt = cmd->init_task_tag;
3539 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3540 cmd->stat_sn = conn->stat_sn++;
3541 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3542
3543 iscsit_increment_maxcmdsn(cmd, conn->sess);
3544 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3545 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3546
3547 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3548 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3549 text_length, conn->cid);
3550
3551 return text_length + padding;
3552}
3553EXPORT_SYMBOL(iscsit_build_text_rsp);
3554
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003555/*
3556 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3557 * MaxRecvDataSegmentLength.
3558 */
3559static int iscsit_send_text_rsp(
3560 struct iscsi_cmd *cmd,
3561 struct iscsi_conn *conn)
3562{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003563 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003564 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003565 u32 tx_size = 0;
3566 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003567
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003568 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003569 if (rc < 0)
3570 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003571
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003572 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003573 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003574 iov[iov_count].iov_base = cmd->pdu;
3575 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3576 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003577 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003578
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003579 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003580
3581 if (conn->conn_ops->HeaderDigest) {
3582 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3583
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003584 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3585 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003586
3587 iov[0].iov_len += ISCSI_CRC_LEN;
3588 tx_size += ISCSI_CRC_LEN;
3589 pr_debug("Attaching CRC32 HeaderDigest for"
3590 " Text Response PDU 0x%08x\n", *header_digest);
3591 }
3592
3593 if (conn->conn_ops->DataDigest) {
3594 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003595 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003596 0, NULL, (u8 *)&cmd->data_crc);
3597
3598 iov[iov_count].iov_base = &cmd->data_crc;
3599 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3600 tx_size += ISCSI_CRC_LEN;
3601
3602 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003603 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003604 cmd->data_crc);
3605 }
3606
3607 cmd->iov_misc_count = iov_count;
3608 cmd->tx_size = tx_size;
3609
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003610 return 0;
3611}
3612
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003613void
3614iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3615 struct iscsi_reject *hdr)
3616{
3617 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003618 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003619 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3620 hton24(hdr->dlength, ISCSI_HDR_LEN);
3621 hdr->ffffffff = cpu_to_be32(0xffffffff);
3622 cmd->stat_sn = conn->stat_sn++;
3623 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3624 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3625 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3626
3627}
3628EXPORT_SYMBOL(iscsit_build_reject);
3629
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003630static int iscsit_send_reject(
3631 struct iscsi_cmd *cmd,
3632 struct iscsi_conn *conn)
3633{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003634 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003635 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003636 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003637
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003638 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003639
3640 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003641 iov[iov_count].iov_base = cmd->pdu;
3642 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3643 iov[iov_count].iov_base = cmd->buf_ptr;
3644 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3645
3646 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3647
3648 if (conn->conn_ops->HeaderDigest) {
3649 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3650
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003651 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3652 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003653
3654 iov[0].iov_len += ISCSI_CRC_LEN;
3655 tx_size += ISCSI_CRC_LEN;
3656 pr_debug("Attaching CRC32 HeaderDigest for"
3657 " REJECT PDU 0x%08x\n", *header_digest);
3658 }
3659
3660 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003661 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3662 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003663
3664 iov[iov_count].iov_base = &cmd->data_crc;
3665 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3666 tx_size += ISCSI_CRC_LEN;
3667 pr_debug("Attaching CRC32 DataDigest for REJECT"
3668 " PDU 0x%08x\n", cmd->data_crc);
3669 }
3670
3671 cmd->iov_misc_count = iov_count;
3672 cmd->tx_size = tx_size;
3673
3674 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3675 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3676
3677 return 0;
3678}
3679
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003680void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3681{
3682 struct iscsi_thread_set *ts = conn->thread_set;
3683 int ord, cpu;
3684 /*
3685 * thread_id is assigned from iscsit_global->ts_bitmap from
3686 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3687 *
3688 * Here we use thread_id to determine which CPU that this
3689 * iSCSI connection's iscsi_thread_set will be scheduled to
3690 * execute upon.
3691 */
3692 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003693 for_each_online_cpu(cpu) {
3694 if (ord-- == 0) {
3695 cpumask_set_cpu(cpu, conn->conn_cpumask);
3696 return;
3697 }
3698 }
3699 /*
3700 * This should never be reached..
3701 */
3702 dump_stack();
3703 cpumask_setall(conn->conn_cpumask);
3704}
3705
3706static inline void iscsit_thread_check_cpumask(
3707 struct iscsi_conn *conn,
3708 struct task_struct *p,
3709 int mode)
3710{
3711 char buf[128];
3712 /*
3713 * mode == 1 signals iscsi_target_tx_thread() usage.
3714 * mode == 0 signals iscsi_target_rx_thread() usage.
3715 */
3716 if (mode == 1) {
3717 if (!conn->conn_tx_reset_cpumask)
3718 return;
3719 conn->conn_tx_reset_cpumask = 0;
3720 } else {
3721 if (!conn->conn_rx_reset_cpumask)
3722 return;
3723 conn->conn_rx_reset_cpumask = 0;
3724 }
3725 /*
3726 * Update the CPU mask for this single kthread so that
3727 * both TX and RX kthreads are scheduled to run on the
3728 * same CPU.
3729 */
3730 memset(buf, 0, 128);
3731 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003732 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3733}
3734
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003735static int
3736iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003737{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003738 int ret;
3739
3740 switch (state) {
3741 case ISTATE_SEND_R2T:
3742 ret = iscsit_send_r2t(cmd, conn);
3743 if (ret < 0)
3744 goto err;
3745 break;
3746 case ISTATE_REMOVE:
3747 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003748 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003749 spin_unlock_bh(&conn->cmd_lock);
3750
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003751 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003752 break;
3753 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3754 iscsit_mod_nopin_response_timer(conn);
3755 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3756 if (ret < 0)
3757 goto err;
3758 break;
3759 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3760 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3761 if (ret < 0)
3762 goto err;
3763 break;
3764 default:
3765 pr_err("Unknown Opcode: 0x%02x ITT:"
3766 " 0x%08x, i_state: %d on CID: %hu\n",
3767 cmd->iscsi_opcode, cmd->init_task_tag, state,
3768 conn->cid);
3769 goto err;
3770 }
3771
3772 return 0;
3773
3774err:
3775 return -1;
3776}
3777
3778static int
3779iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3780{
3781 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003782 struct iscsi_queue_req *qr;
3783 struct iscsi_cmd *cmd;
3784 u8 state;
3785 int ret;
3786
3787 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3788 atomic_set(&conn->check_immediate_queue, 0);
3789 cmd = qr->cmd;
3790 state = qr->state;
3791 kmem_cache_free(lio_qr_cache, qr);
3792
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003793 ret = t->iscsit_immediate_queue(conn, cmd, state);
3794 if (ret < 0)
3795 return ret;
3796 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003797
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003798 return 0;
3799}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003800
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003801static int
3802iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3803{
3804 int ret;
3805
3806check_rsp_state:
3807 switch (state) {
3808 case ISTATE_SEND_DATAIN:
3809 ret = iscsit_send_datain(cmd, conn);
3810 if (ret < 0)
3811 goto err;
3812 else if (!ret)
3813 /* more drs */
3814 goto check_rsp_state;
3815 else if (ret == 1) {
3816 /* all done */
3817 spin_lock_bh(&cmd->istate_lock);
3818 cmd->i_state = ISTATE_SENT_STATUS;
3819 spin_unlock_bh(&cmd->istate_lock);
3820
3821 if (atomic_read(&conn->check_immediate_queue))
3822 return 1;
3823
3824 return 0;
3825 } else if (ret == 2) {
3826 /* Still must send status,
3827 SCF_TRANSPORT_TASK_SENSE was set */
3828 spin_lock_bh(&cmd->istate_lock);
3829 cmd->i_state = ISTATE_SEND_STATUS;
3830 spin_unlock_bh(&cmd->istate_lock);
3831 state = ISTATE_SEND_STATUS;
3832 goto check_rsp_state;
3833 }
3834
3835 break;
3836 case ISTATE_SEND_STATUS:
3837 case ISTATE_SEND_STATUS_RECOVERY:
3838 ret = iscsit_send_response(cmd, conn);
3839 break;
3840 case ISTATE_SEND_LOGOUTRSP:
3841 ret = iscsit_send_logout(cmd, conn);
3842 break;
3843 case ISTATE_SEND_ASYNCMSG:
3844 ret = iscsit_send_conn_drop_async_message(
3845 cmd, conn);
3846 break;
3847 case ISTATE_SEND_NOPIN:
3848 ret = iscsit_send_nopin(cmd, conn);
3849 break;
3850 case ISTATE_SEND_REJECT:
3851 ret = iscsit_send_reject(cmd, conn);
3852 break;
3853 case ISTATE_SEND_TASKMGTRSP:
3854 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3855 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003856 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003857 ret = iscsit_tmr_post_handler(cmd, conn);
3858 if (ret != 0)
3859 iscsit_fall_back_to_erl0(conn->sess);
3860 break;
3861 case ISTATE_SEND_TEXTRSP:
3862 ret = iscsit_send_text_rsp(cmd, conn);
3863 break;
3864 default:
3865 pr_err("Unknown Opcode: 0x%02x ITT:"
3866 " 0x%08x, i_state: %d on CID: %hu\n",
3867 cmd->iscsi_opcode, cmd->init_task_tag,
3868 state, conn->cid);
3869 goto err;
3870 }
3871 if (ret < 0)
3872 goto err;
3873
3874 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3875 iscsit_tx_thread_wait_for_tcp(conn);
3876 iscsit_unmap_iovec(cmd);
3877 goto err;
3878 }
3879 iscsit_unmap_iovec(cmd);
3880
3881 switch (state) {
3882 case ISTATE_SEND_LOGOUTRSP:
3883 if (!iscsit_logout_post_handler(cmd, conn))
3884 goto restart;
3885 /* fall through */
3886 case ISTATE_SEND_STATUS:
3887 case ISTATE_SEND_ASYNCMSG:
3888 case ISTATE_SEND_NOPIN:
3889 case ISTATE_SEND_STATUS_RECOVERY:
3890 case ISTATE_SEND_TEXTRSP:
3891 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003892 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003893 spin_lock_bh(&cmd->istate_lock);
3894 cmd->i_state = ISTATE_SENT_STATUS;
3895 spin_unlock_bh(&cmd->istate_lock);
3896 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003897 default:
3898 pr_err("Unknown Opcode: 0x%02x ITT:"
3899 " 0x%08x, i_state: %d on CID: %hu\n",
3900 cmd->iscsi_opcode, cmd->init_task_tag,
3901 cmd->i_state, conn->cid);
3902 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003903 }
3904
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003905 if (atomic_read(&conn->check_immediate_queue))
3906 return 1;
3907
Andy Grover6f3c0e62012-04-03 15:51:09 -07003908 return 0;
3909
3910err:
3911 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003912restart:
3913 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003914}
3915
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003916static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003917{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003918 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003919 struct iscsi_queue_req *qr;
3920 struct iscsi_cmd *cmd;
3921 u8 state;
3922 int ret;
3923
3924 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3925 cmd = qr->cmd;
3926 state = qr->state;
3927 kmem_cache_free(lio_qr_cache, qr);
3928
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003929 ret = t->iscsit_response_queue(conn, cmd, state);
3930 if (ret == 1 || ret < 0)
3931 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003932 }
3933
3934 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003935}
3936
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003937int iscsi_target_tx_thread(void *arg)
3938{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003939 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003940 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003941 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003942 /*
3943 * Allow ourselves to be interrupted by SIGINT so that a
3944 * connection recovery / failure event can be triggered externally.
3945 */
3946 allow_signal(SIGINT);
3947
3948restart:
3949 conn = iscsi_tx_thread_pre_handler(ts);
3950 if (!conn)
3951 goto out;
3952
Andy Grover6f3c0e62012-04-03 15:51:09 -07003953 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003954
3955 while (!kthread_should_stop()) {
3956 /*
3957 * Ensure that both TX and RX per connection kthreads
3958 * are scheduled to run on the same CPU.
3959 */
3960 iscsit_thread_check_cpumask(conn, current, 1);
3961
Roland Dreierd5627ac2012-10-31 09:16:46 -07003962 wait_event_interruptible(conn->queues_wq,
3963 !iscsit_conn_all_queues_empty(conn) ||
3964 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003965
3966 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3967 signal_pending(current))
3968 goto transport_err;
3969
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003970get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003971 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003972 if (ret < 0)
3973 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003974
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003975 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003976 if (ret == 1)
3977 goto get_immediate;
3978 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003979 goto restart;
3980 else if (ret < 0)
3981 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003982 }
3983
3984transport_err:
3985 iscsit_take_action_for_connection_exit(conn);
3986 goto restart;
3987out:
3988 return 0;
3989}
3990
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003991static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3992{
3993 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3994 struct iscsi_cmd *cmd;
3995 int ret = 0;
3996
3997 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3998 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003999 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004000 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004001 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004002
4003 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4004 break;
4005 case ISCSI_OP_SCSI_DATA_OUT:
4006 ret = iscsit_handle_data_out(conn, buf);
4007 break;
4008 case ISCSI_OP_NOOP_OUT:
4009 cmd = NULL;
4010 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004011 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004012 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004013 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004014 }
4015 ret = iscsit_handle_nop_out(conn, cmd, buf);
4016 break;
4017 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004018 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004019 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004020 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004021
4022 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4023 break;
4024 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004025 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004026 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004027 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004028
4029 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004030 break;
4031 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004032 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004033 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004034 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004035
4036 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4037 if (ret > 0)
4038 wait_for_completion_timeout(&conn->conn_logout_comp,
4039 SECONDS_FOR_LOGOUT_COMP * HZ);
4040 break;
4041 case ISCSI_OP_SNACK:
4042 ret = iscsit_handle_snack(conn, buf);
4043 break;
4044 default:
4045 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4046 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4047 pr_err("Cannot recover from unknown"
4048 " opcode while ERL=0, closing iSCSI connection.\n");
4049 return -1;
4050 }
4051 if (!conn->conn_ops->OFMarker) {
4052 pr_err("Unable to recover from unknown"
4053 " opcode while OFMarker=No, closing iSCSI"
4054 " connection.\n");
4055 return -1;
4056 }
4057 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4058 pr_err("Unable to recover from unknown"
4059 " opcode, closing iSCSI connection.\n");
4060 return -1;
4061 }
4062 break;
4063 }
4064
4065 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004066reject:
4067 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004068}
4069
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004070int iscsi_target_rx_thread(void *arg)
4071{
4072 int ret;
4073 u8 buffer[ISCSI_HDR_LEN], opcode;
4074 u32 checksum = 0, digest = 0;
4075 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004076 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004077 struct kvec iov;
4078 /*
4079 * Allow ourselves to be interrupted by SIGINT so that a
4080 * connection recovery / failure event can be triggered externally.
4081 */
4082 allow_signal(SIGINT);
4083
4084restart:
4085 conn = iscsi_rx_thread_pre_handler(ts);
4086 if (!conn)
4087 goto out;
4088
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004089 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4090 struct completion comp;
4091 int rc;
4092
4093 init_completion(&comp);
4094 rc = wait_for_completion_interruptible(&comp);
4095 if (rc < 0)
4096 goto transport_err;
4097
4098 goto out;
4099 }
4100
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004101 while (!kthread_should_stop()) {
4102 /*
4103 * Ensure that both TX and RX per connection kthreads
4104 * are scheduled to run on the same CPU.
4105 */
4106 iscsit_thread_check_cpumask(conn, current, 0);
4107
4108 memset(buffer, 0, ISCSI_HDR_LEN);
4109 memset(&iov, 0, sizeof(struct kvec));
4110
4111 iov.iov_base = buffer;
4112 iov.iov_len = ISCSI_HDR_LEN;
4113
4114 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4115 if (ret != ISCSI_HDR_LEN) {
4116 iscsit_rx_thread_wait_for_tcp(conn);
4117 goto transport_err;
4118 }
4119
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004120 if (conn->conn_ops->HeaderDigest) {
4121 iov.iov_base = &digest;
4122 iov.iov_len = ISCSI_CRC_LEN;
4123
4124 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4125 if (ret != ISCSI_CRC_LEN) {
4126 iscsit_rx_thread_wait_for_tcp(conn);
4127 goto transport_err;
4128 }
4129
4130 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4131 buffer, ISCSI_HDR_LEN,
4132 0, NULL, (u8 *)&checksum);
4133
4134 if (digest != checksum) {
4135 pr_err("HeaderDigest CRC32C failed,"
4136 " received 0x%08x, computed 0x%08x\n",
4137 digest, checksum);
4138 /*
4139 * Set the PDU to 0xff so it will intentionally
4140 * hit default in the switch below.
4141 */
4142 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004143 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004144 } else {
4145 pr_debug("Got HeaderDigest CRC32C"
4146 " 0x%08x\n", checksum);
4147 }
4148 }
4149
4150 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4151 goto transport_err;
4152
4153 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4154
4155 if (conn->sess->sess_ops->SessionType &&
4156 ((!(opcode & ISCSI_OP_TEXT)) ||
4157 (!(opcode & ISCSI_OP_LOGOUT)))) {
4158 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4159 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004160 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4161 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004162 goto transport_err;
4163 }
4164
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004165 ret = iscsi_target_rx_opcode(conn, buffer);
4166 if (ret < 0)
4167 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004168 }
4169
4170transport_err:
4171 if (!signal_pending(current))
4172 atomic_set(&conn->transport_failed, 1);
4173 iscsit_take_action_for_connection_exit(conn);
4174 goto restart;
4175out:
4176 return 0;
4177}
4178
4179static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4180{
4181 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4182 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004183 /*
4184 * We expect this function to only ever be called from either RX or TX
4185 * thread context via iscsit_close_connection() once the other context
4186 * has been reset -> returned sleeping pre-handler state.
4187 */
4188 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004189 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004190
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004191 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004192 spin_unlock_bh(&conn->cmd_lock);
4193
4194 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004195
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004196 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004197
4198 spin_lock_bh(&conn->cmd_lock);
4199 }
4200 spin_unlock_bh(&conn->cmd_lock);
4201}
4202
4203static void iscsit_stop_timers_for_cmds(
4204 struct iscsi_conn *conn)
4205{
4206 struct iscsi_cmd *cmd;
4207
4208 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004209 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004210 if (cmd->data_direction == DMA_TO_DEVICE)
4211 iscsit_stop_dataout_timer(cmd);
4212 }
4213 spin_unlock_bh(&conn->cmd_lock);
4214}
4215
4216int iscsit_close_connection(
4217 struct iscsi_conn *conn)
4218{
4219 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4220 struct iscsi_session *sess = conn->sess;
4221
4222 pr_debug("Closing iSCSI connection CID %hu on SID:"
4223 " %u\n", conn->cid, sess->sid);
4224 /*
4225 * Always up conn_logout_comp just in case the RX Thread is sleeping
4226 * and the logout response never got sent because the connection
4227 * failed.
4228 */
4229 complete(&conn->conn_logout_comp);
4230
4231 iscsi_release_thread_set(conn);
4232
4233 iscsit_stop_timers_for_cmds(conn);
4234 iscsit_stop_nopin_response_timer(conn);
4235 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004236
4237 if (conn->conn_transport->iscsit_wait_conn)
4238 conn->conn_transport->iscsit_wait_conn(conn);
4239
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004240 /*
4241 * During Connection recovery drop unacknowledged out of order
4242 * commands for this connection, and prepare the other commands
4243 * for realligence.
4244 *
4245 * During normal operation clear the out of order commands (but
4246 * do not free the struct iscsi_ooo_cmdsn's) and release all
4247 * struct iscsi_cmds.
4248 */
4249 if (atomic_read(&conn->connection_recovery)) {
4250 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4251 iscsit_prepare_cmds_for_realligance(conn);
4252 } else {
4253 iscsit_clear_ooo_cmdsns_for_conn(conn);
4254 iscsit_release_commands_from_conn(conn);
4255 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004256 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004257
4258 /*
4259 * Handle decrementing session or connection usage count if
4260 * a logout response was not able to be sent because the
4261 * connection failed. Fall back to Session Recovery here.
4262 */
4263 if (atomic_read(&conn->conn_logout_remove)) {
4264 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4265 iscsit_dec_conn_usage_count(conn);
4266 iscsit_dec_session_usage_count(sess);
4267 }
4268 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4269 iscsit_dec_conn_usage_count(conn);
4270
4271 atomic_set(&conn->conn_logout_remove, 0);
4272 atomic_set(&sess->session_reinstatement, 0);
4273 atomic_set(&sess->session_fall_back_to_erl0, 1);
4274 }
4275
4276 spin_lock_bh(&sess->conn_lock);
4277 list_del(&conn->conn_list);
4278
4279 /*
4280 * Attempt to let the Initiator know this connection failed by
4281 * sending an Connection Dropped Async Message on another
4282 * active connection.
4283 */
4284 if (atomic_read(&conn->connection_recovery))
4285 iscsit_build_conn_drop_async_message(conn);
4286
4287 spin_unlock_bh(&sess->conn_lock);
4288
4289 /*
4290 * If connection reinstatement is being performed on this connection,
4291 * up the connection reinstatement semaphore that is being blocked on
4292 * in iscsit_cause_connection_reinstatement().
4293 */
4294 spin_lock_bh(&conn->state_lock);
4295 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4296 spin_unlock_bh(&conn->state_lock);
4297 complete(&conn->conn_wait_comp);
4298 wait_for_completion(&conn->conn_post_wait_comp);
4299 spin_lock_bh(&conn->state_lock);
4300 }
4301
4302 /*
4303 * If connection reinstatement is being performed on this connection
4304 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4305 * connection wait rcfr semaphore that is being blocked on
4306 * an iscsit_connection_reinstatement_rcfr().
4307 */
4308 if (atomic_read(&conn->connection_wait_rcfr)) {
4309 spin_unlock_bh(&conn->state_lock);
4310 complete(&conn->conn_wait_rcfr_comp);
4311 wait_for_completion(&conn->conn_post_wait_comp);
4312 spin_lock_bh(&conn->state_lock);
4313 }
4314 atomic_set(&conn->connection_reinstatement, 1);
4315 spin_unlock_bh(&conn->state_lock);
4316
4317 /*
4318 * If any other processes are accessing this connection pointer we
4319 * must wait until they have completed.
4320 */
4321 iscsit_check_conn_usage_count(conn);
4322
4323 if (conn->conn_rx_hash.tfm)
4324 crypto_free_hash(conn->conn_rx_hash.tfm);
4325 if (conn->conn_tx_hash.tfm)
4326 crypto_free_hash(conn->conn_tx_hash.tfm);
4327
4328 if (conn->conn_cpumask)
4329 free_cpumask_var(conn->conn_cpumask);
4330
4331 kfree(conn->conn_ops);
4332 conn->conn_ops = NULL;
4333
Al Virobf6932f2012-07-21 08:55:18 +01004334 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004335 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004336
4337 if (conn->conn_transport->iscsit_free_conn)
4338 conn->conn_transport->iscsit_free_conn(conn);
4339
4340 iscsit_put_transport(conn->conn_transport);
4341
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004342 conn->thread_set = NULL;
4343
4344 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4345 conn->conn_state = TARG_CONN_STATE_FREE;
4346 kfree(conn);
4347
4348 spin_lock_bh(&sess->conn_lock);
4349 atomic_dec(&sess->nconn);
4350 pr_debug("Decremented iSCSI connection count to %hu from node:"
4351 " %s\n", atomic_read(&sess->nconn),
4352 sess->sess_ops->InitiatorName);
4353 /*
4354 * Make sure that if one connection fails in an non ERL=2 iSCSI
4355 * Session that they all fail.
4356 */
4357 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4358 !atomic_read(&sess->session_logout))
4359 atomic_set(&sess->session_fall_back_to_erl0, 1);
4360
4361 /*
4362 * If this was not the last connection in the session, and we are
4363 * performing session reinstatement or falling back to ERL=0, call
4364 * iscsit_stop_session() without sleeping to shutdown the other
4365 * active connections.
4366 */
4367 if (atomic_read(&sess->nconn)) {
4368 if (!atomic_read(&sess->session_reinstatement) &&
4369 !atomic_read(&sess->session_fall_back_to_erl0)) {
4370 spin_unlock_bh(&sess->conn_lock);
4371 return 0;
4372 }
4373 if (!atomic_read(&sess->session_stop_active)) {
4374 atomic_set(&sess->session_stop_active, 1);
4375 spin_unlock_bh(&sess->conn_lock);
4376 iscsit_stop_session(sess, 0, 0);
4377 return 0;
4378 }
4379 spin_unlock_bh(&sess->conn_lock);
4380 return 0;
4381 }
4382
4383 /*
4384 * If this was the last connection in the session and one of the
4385 * following is occurring:
4386 *
4387 * Session Reinstatement is not being performed, and are falling back
4388 * to ERL=0 call iscsit_close_session().
4389 *
4390 * Session Logout was requested. iscsit_close_session() will be called
4391 * elsewhere.
4392 *
4393 * Session Continuation is not being performed, start the Time2Retain
4394 * handler and check if sleep_on_sess_wait_sem is active.
4395 */
4396 if (!atomic_read(&sess->session_reinstatement) &&
4397 atomic_read(&sess->session_fall_back_to_erl0)) {
4398 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004399 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004400
4401 return 0;
4402 } else if (atomic_read(&sess->session_logout)) {
4403 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4404 sess->session_state = TARG_SESS_STATE_FREE;
4405 spin_unlock_bh(&sess->conn_lock);
4406
4407 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4408 complete(&sess->session_wait_comp);
4409
4410 return 0;
4411 } else {
4412 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4413 sess->session_state = TARG_SESS_STATE_FAILED;
4414
4415 if (!atomic_read(&sess->session_continuation)) {
4416 spin_unlock_bh(&sess->conn_lock);
4417 iscsit_start_time2retain_handler(sess);
4418 } else
4419 spin_unlock_bh(&sess->conn_lock);
4420
4421 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4422 complete(&sess->session_wait_comp);
4423
4424 return 0;
4425 }
4426 spin_unlock_bh(&sess->conn_lock);
4427
4428 return 0;
4429}
4430
4431int iscsit_close_session(struct iscsi_session *sess)
4432{
Andy Grover60bfcf82013-10-09 11:05:58 -07004433 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004434 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4435
4436 if (atomic_read(&sess->nconn)) {
4437 pr_err("%d connection(s) still exist for iSCSI session"
4438 " to %s\n", atomic_read(&sess->nconn),
4439 sess->sess_ops->InitiatorName);
4440 BUG();
4441 }
4442
4443 spin_lock_bh(&se_tpg->session_lock);
4444 atomic_set(&sess->session_logout, 1);
4445 atomic_set(&sess->session_reinstatement, 1);
4446 iscsit_stop_time2retain_timer(sess);
4447 spin_unlock_bh(&se_tpg->session_lock);
4448
4449 /*
4450 * transport_deregister_session_configfs() will clear the
4451 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4452 * can be setting it again with __transport_register_session() in
4453 * iscsi_post_login_handler() again after the iscsit_stop_session()
4454 * completes in iscsi_np context.
4455 */
4456 transport_deregister_session_configfs(sess->se_sess);
4457
4458 /*
4459 * If any other processes are accessing this session pointer we must
4460 * wait until they have completed. If we are in an interrupt (the
4461 * time2retain handler) and contain and active session usage count we
4462 * restart the timer and exit.
4463 */
4464 if (!in_interrupt()) {
4465 if (iscsit_check_session_usage_count(sess) == 1)
4466 iscsit_stop_session(sess, 1, 1);
4467 } else {
4468 if (iscsit_check_session_usage_count(sess) == 2) {
4469 atomic_set(&sess->session_logout, 0);
4470 iscsit_start_time2retain_handler(sess);
4471 return 0;
4472 }
4473 }
4474
4475 transport_deregister_session(sess->se_sess);
4476
4477 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4478 iscsit_free_connection_recovery_entires(sess);
4479
4480 iscsit_free_all_ooo_cmdsns(sess);
4481
4482 spin_lock_bh(&se_tpg->session_lock);
4483 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4484 sess->session_state = TARG_SESS_STATE_FREE;
4485 pr_debug("Released iSCSI session from node: %s\n",
4486 sess->sess_ops->InitiatorName);
4487 tpg->nsessions--;
4488 if (tpg->tpg_tiqn)
4489 tpg->tpg_tiqn->tiqn_nsessions--;
4490
4491 pr_debug("Decremented number of active iSCSI Sessions on"
4492 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4493
4494 spin_lock(&sess_idr_lock);
4495 idr_remove(&sess_idr, sess->session_index);
4496 spin_unlock(&sess_idr_lock);
4497
4498 kfree(sess->sess_ops);
4499 sess->sess_ops = NULL;
4500 spin_unlock_bh(&se_tpg->session_lock);
4501
4502 kfree(sess);
4503 return 0;
4504}
4505
4506static void iscsit_logout_post_handler_closesession(
4507 struct iscsi_conn *conn)
4508{
4509 struct iscsi_session *sess = conn->sess;
4510
4511 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4512 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4513
4514 atomic_set(&conn->conn_logout_remove, 0);
4515 complete(&conn->conn_logout_comp);
4516
4517 iscsit_dec_conn_usage_count(conn);
4518 iscsit_stop_session(sess, 1, 1);
4519 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004520 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004521}
4522
4523static void iscsit_logout_post_handler_samecid(
4524 struct iscsi_conn *conn)
4525{
4526 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4527 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4528
4529 atomic_set(&conn->conn_logout_remove, 0);
4530 complete(&conn->conn_logout_comp);
4531
4532 iscsit_cause_connection_reinstatement(conn, 1);
4533 iscsit_dec_conn_usage_count(conn);
4534}
4535
4536static void iscsit_logout_post_handler_diffcid(
4537 struct iscsi_conn *conn,
4538 u16 cid)
4539{
4540 struct iscsi_conn *l_conn;
4541 struct iscsi_session *sess = conn->sess;
4542
4543 if (!sess)
4544 return;
4545
4546 spin_lock_bh(&sess->conn_lock);
4547 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4548 if (l_conn->cid == cid) {
4549 iscsit_inc_conn_usage_count(l_conn);
4550 break;
4551 }
4552 }
4553 spin_unlock_bh(&sess->conn_lock);
4554
4555 if (!l_conn)
4556 return;
4557
4558 if (l_conn->sock)
4559 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4560
4561 spin_lock_bh(&l_conn->state_lock);
4562 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4563 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4564 spin_unlock_bh(&l_conn->state_lock);
4565
4566 iscsit_cause_connection_reinstatement(l_conn, 1);
4567 iscsit_dec_conn_usage_count(l_conn);
4568}
4569
4570/*
4571 * Return of 0 causes the TX thread to restart.
4572 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004573int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004574 struct iscsi_cmd *cmd,
4575 struct iscsi_conn *conn)
4576{
4577 int ret = 0;
4578
4579 switch (cmd->logout_reason) {
4580 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4581 switch (cmd->logout_response) {
4582 case ISCSI_LOGOUT_SUCCESS:
4583 case ISCSI_LOGOUT_CLEANUP_FAILED:
4584 default:
4585 iscsit_logout_post_handler_closesession(conn);
4586 break;
4587 }
4588 ret = 0;
4589 break;
4590 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4591 if (conn->cid == cmd->logout_cid) {
4592 switch (cmd->logout_response) {
4593 case ISCSI_LOGOUT_SUCCESS:
4594 case ISCSI_LOGOUT_CLEANUP_FAILED:
4595 default:
4596 iscsit_logout_post_handler_samecid(conn);
4597 break;
4598 }
4599 ret = 0;
4600 } else {
4601 switch (cmd->logout_response) {
4602 case ISCSI_LOGOUT_SUCCESS:
4603 iscsit_logout_post_handler_diffcid(conn,
4604 cmd->logout_cid);
4605 break;
4606 case ISCSI_LOGOUT_CID_NOT_FOUND:
4607 case ISCSI_LOGOUT_CLEANUP_FAILED:
4608 default:
4609 break;
4610 }
4611 ret = 1;
4612 }
4613 break;
4614 case ISCSI_LOGOUT_REASON_RECOVERY:
4615 switch (cmd->logout_response) {
4616 case ISCSI_LOGOUT_SUCCESS:
4617 case ISCSI_LOGOUT_CID_NOT_FOUND:
4618 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4619 case ISCSI_LOGOUT_CLEANUP_FAILED:
4620 default:
4621 break;
4622 }
4623 ret = 1;
4624 break;
4625 default:
4626 break;
4627
4628 }
4629 return ret;
4630}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004631EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004632
4633void iscsit_fail_session(struct iscsi_session *sess)
4634{
4635 struct iscsi_conn *conn;
4636
4637 spin_lock_bh(&sess->conn_lock);
4638 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4639 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4640 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4641 }
4642 spin_unlock_bh(&sess->conn_lock);
4643
4644 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4645 sess->session_state = TARG_SESS_STATE_FAILED;
4646}
4647
4648int iscsit_free_session(struct iscsi_session *sess)
4649{
4650 u16 conn_count = atomic_read(&sess->nconn);
4651 struct iscsi_conn *conn, *conn_tmp = NULL;
4652 int is_last;
4653
4654 spin_lock_bh(&sess->conn_lock);
4655 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4656
4657 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4658 conn_list) {
4659 if (conn_count == 0)
4660 break;
4661
4662 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4663 is_last = 1;
4664 } else {
4665 iscsit_inc_conn_usage_count(conn_tmp);
4666 is_last = 0;
4667 }
4668 iscsit_inc_conn_usage_count(conn);
4669
4670 spin_unlock_bh(&sess->conn_lock);
4671 iscsit_cause_connection_reinstatement(conn, 1);
4672 spin_lock_bh(&sess->conn_lock);
4673
4674 iscsit_dec_conn_usage_count(conn);
4675 if (is_last == 0)
4676 iscsit_dec_conn_usage_count(conn_tmp);
4677
4678 conn_count--;
4679 }
4680
4681 if (atomic_read(&sess->nconn)) {
4682 spin_unlock_bh(&sess->conn_lock);
4683 wait_for_completion(&sess->session_wait_comp);
4684 } else
4685 spin_unlock_bh(&sess->conn_lock);
4686
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004687 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004688 return 0;
4689}
4690
4691void iscsit_stop_session(
4692 struct iscsi_session *sess,
4693 int session_sleep,
4694 int connection_sleep)
4695{
4696 u16 conn_count = atomic_read(&sess->nconn);
4697 struct iscsi_conn *conn, *conn_tmp = NULL;
4698 int is_last;
4699
4700 spin_lock_bh(&sess->conn_lock);
4701 if (session_sleep)
4702 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4703
4704 if (connection_sleep) {
4705 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4706 conn_list) {
4707 if (conn_count == 0)
4708 break;
4709
4710 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4711 is_last = 1;
4712 } else {
4713 iscsit_inc_conn_usage_count(conn_tmp);
4714 is_last = 0;
4715 }
4716 iscsit_inc_conn_usage_count(conn);
4717
4718 spin_unlock_bh(&sess->conn_lock);
4719 iscsit_cause_connection_reinstatement(conn, 1);
4720 spin_lock_bh(&sess->conn_lock);
4721
4722 iscsit_dec_conn_usage_count(conn);
4723 if (is_last == 0)
4724 iscsit_dec_conn_usage_count(conn_tmp);
4725 conn_count--;
4726 }
4727 } else {
4728 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4729 iscsit_cause_connection_reinstatement(conn, 0);
4730 }
4731
4732 if (session_sleep && atomic_read(&sess->nconn)) {
4733 spin_unlock_bh(&sess->conn_lock);
4734 wait_for_completion(&sess->session_wait_comp);
4735 } else
4736 spin_unlock_bh(&sess->conn_lock);
4737}
4738
4739int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4740{
4741 struct iscsi_session *sess;
4742 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4743 struct se_session *se_sess, *se_sess_tmp;
4744 int session_count = 0;
4745
4746 spin_lock_bh(&se_tpg->session_lock);
4747 if (tpg->nsessions && !force) {
4748 spin_unlock_bh(&se_tpg->session_lock);
4749 return -1;
4750 }
4751
4752 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4753 sess_list) {
4754 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4755
4756 spin_lock(&sess->conn_lock);
4757 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4758 atomic_read(&sess->session_logout) ||
4759 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4760 spin_unlock(&sess->conn_lock);
4761 continue;
4762 }
4763 atomic_set(&sess->session_reinstatement, 1);
4764 spin_unlock(&sess->conn_lock);
4765 spin_unlock_bh(&se_tpg->session_lock);
4766
4767 iscsit_free_session(sess);
4768 spin_lock_bh(&se_tpg->session_lock);
4769
4770 session_count++;
4771 }
4772 spin_unlock_bh(&se_tpg->session_lock);
4773
4774 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4775 " Group: %hu\n", session_count, tpg->tpgt);
4776 return 0;
4777}
4778
4779MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4780MODULE_VERSION("4.1.x");
4781MODULE_AUTHOR("nab@Linux-iSCSI.org");
4782MODULE_LICENSE("GPL");
4783
4784module_init(iscsi_target_init_module);
4785module_exit(iscsi_target_cleanup_module);