blob: a99637e9e82017757239db64673986b71adcffa7 [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
303 if ((ip_match == true) && (np->np_port == port) &&
304 (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);
328 if (match == true) {
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);
476 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477
478 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000479
Andy Groveree291e62014-01-24 16:18:54 -0800480 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000481 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800482 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483
484 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800485 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000486
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800487 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000488 kfree(np);
489 return 0;
490}
491
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700492static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
493static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
494
495static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
496{
497 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
498 return 0;
499}
500
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800501static struct iscsit_transport iscsi_target_transport = {
502 .name = "iSCSI/TCP",
503 .transport_type = ISCSI_TCP,
504 .owner = NULL,
505 .iscsit_setup_np = iscsit_setup_np,
506 .iscsit_accept_np = iscsit_accept_np,
507 .iscsit_free_np = iscsit_free_np,
508 .iscsit_get_login_rx = iscsit_get_login_rx,
509 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800510 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700511 .iscsit_immediate_queue = iscsit_immediate_queue,
512 .iscsit_response_queue = iscsit_response_queue,
513 .iscsit_queue_data_in = iscsit_queue_rsp,
514 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800515};
516
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000517static int __init iscsi_target_init_module(void)
518{
519 int ret = 0;
520
521 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
522
523 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
524 if (!iscsit_global) {
525 pr_err("Unable to allocate memory for iscsit_global\n");
526 return -1;
527 }
528 mutex_init(&auth_id_lock);
529 spin_lock_init(&sess_idr_lock);
530 idr_init(&tiqn_idr);
531 idr_init(&sess_idr);
532
533 ret = iscsi_target_register_configfs();
534 if (ret < 0)
535 goto out;
536
537 ret = iscsi_thread_set_init();
538 if (ret < 0)
539 goto configfs_out;
540
541 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
542 TARGET_THREAD_SET_COUNT) {
543 pr_err("iscsi_allocate_thread_sets() returned"
544 " unexpected value!\n");
545 goto ts_out1;
546 }
547
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000548 lio_qr_cache = kmem_cache_create("lio_qr_cache",
549 sizeof(struct iscsi_queue_req),
550 __alignof__(struct iscsi_queue_req), 0, NULL);
551 if (!lio_qr_cache) {
552 pr_err("nable to kmem_cache_create() for"
553 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700554 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000555 }
556
557 lio_dr_cache = kmem_cache_create("lio_dr_cache",
558 sizeof(struct iscsi_datain_req),
559 __alignof__(struct iscsi_datain_req), 0, NULL);
560 if (!lio_dr_cache) {
561 pr_err("Unable to kmem_cache_create() for"
562 " lio_dr_cache\n");
563 goto qr_out;
564 }
565
566 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
567 sizeof(struct iscsi_ooo_cmdsn),
568 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
569 if (!lio_ooo_cache) {
570 pr_err("Unable to kmem_cache_create() for"
571 " lio_ooo_cache\n");
572 goto dr_out;
573 }
574
575 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
576 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
577 0, NULL);
578 if (!lio_r2t_cache) {
579 pr_err("Unable to kmem_cache_create() for"
580 " lio_r2t_cache\n");
581 goto ooo_out;
582 }
583
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800584 iscsit_register_transport(&iscsi_target_transport);
585
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000586 if (iscsit_load_discovery_tpg() < 0)
587 goto r2t_out;
588
589 return ret;
590r2t_out:
591 kmem_cache_destroy(lio_r2t_cache);
592ooo_out:
593 kmem_cache_destroy(lio_ooo_cache);
594dr_out:
595 kmem_cache_destroy(lio_dr_cache);
596qr_out:
597 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000598ts_out2:
599 iscsi_deallocate_thread_sets();
600ts_out1:
601 iscsi_thread_set_free();
602configfs_out:
603 iscsi_target_deregister_configfs();
604out:
605 kfree(iscsit_global);
606 return -ENOMEM;
607}
608
609static void __exit iscsi_target_cleanup_module(void)
610{
611 iscsi_deallocate_thread_sets();
612 iscsi_thread_set_free();
613 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800614 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000615 kmem_cache_destroy(lio_qr_cache);
616 kmem_cache_destroy(lio_dr_cache);
617 kmem_cache_destroy(lio_ooo_cache);
618 kmem_cache_destroy(lio_r2t_cache);
619
620 iscsi_target_deregister_configfs();
621
622 kfree(iscsit_global);
623}
624
Andy Grover8b1e1242012-04-03 15:51:12 -0700625static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700626 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000627 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700628 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000629{
630 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000631
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000632 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000633 if (!cmd)
634 return -1;
635
636 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700637 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000638
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100639 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000640 if (!cmd->buf_ptr) {
641 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700642 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643 return -1;
644 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645
646 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700647 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000648 spin_unlock_bh(&conn->cmd_lock);
649
650 cmd->i_state = ISTATE_SEND_REJECT;
651 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
652
Nicholas Bellingerba159912013-07-03 03:48:24 -0700653 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654}
655
Nicholas Bellingerba159912013-07-03 03:48:24 -0700656static int iscsit_add_reject_from_cmd(
657 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000658 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700659 bool add_to_conn,
660 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000661{
662 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000663
664 if (!cmd->conn) {
665 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
666 cmd->init_task_tag);
667 return -1;
668 }
669 conn = cmd->conn;
670
671 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700672 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000673
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100674 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000675 if (!cmd->buf_ptr) {
676 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700677 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000678 return -1;
679 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000680
681 if (add_to_conn) {
682 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700683 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000684 spin_unlock_bh(&conn->cmd_lock);
685 }
686
687 cmd->i_state = ISTATE_SEND_REJECT;
688 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800689 /*
690 * Perform the kref_put now if se_cmd has already been setup by
691 * scsit_setup_scsi_cmd()
692 */
693 if (cmd->se_cmd.se_tfo != NULL) {
694 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
695 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
696 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700697 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700699
700static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
701 unsigned char *buf)
702{
703 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
704}
705
706int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
707{
708 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
709}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000710
711/*
712 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700713 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000714 */
715static int iscsit_map_iovec(
716 struct iscsi_cmd *cmd,
717 struct kvec *iov,
718 u32 data_offset,
719 u32 data_length)
720{
721 u32 i = 0;
722 struct scatterlist *sg;
723 unsigned int page_off;
724
725 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700726 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000727 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700728 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000729 page_off = (data_offset % PAGE_SIZE);
730
731 cmd->first_data_sg = sg;
732 cmd->first_data_sg_off = page_off;
733
734 while (data_length) {
735 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
736
737 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
738 iov[i].iov_len = cur_len;
739
740 data_length -= cur_len;
741 page_off = 0;
742 sg = sg_next(sg);
743 i++;
744 }
745
746 cmd->kmapped_nents = i;
747
748 return i;
749}
750
751static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
752{
753 u32 i;
754 struct scatterlist *sg;
755
756 sg = cmd->first_data_sg;
757
758 for (i = 0; i < cmd->kmapped_nents; i++)
759 kunmap(sg_page(&sg[i]));
760}
761
762static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
763{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700764 LIST_HEAD(ack_list);
765 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000766
767 conn->exp_statsn = exp_statsn;
768
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800769 if (conn->sess->sess_ops->RDMAExtensions)
770 return;
771
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000772 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700773 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000774 spin_lock(&cmd->istate_lock);
775 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800776 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000777 cmd->i_state = ISTATE_REMOVE;
778 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700779 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780 continue;
781 }
782 spin_unlock(&cmd->istate_lock);
783 }
784 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700785
786 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
787 list_del(&cmd->i_conn_node);
788 iscsit_free_cmd(cmd, false);
789 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000790}
791
792static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
793{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700794 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400796 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797
798 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
799 if (!cmd->iov_data) {
800 pr_err("Unable to allocate cmd->iov_data\n");
801 return -ENOMEM;
802 }
803
804 cmd->orig_iov_data_count = iov_count;
805 return 0;
806}
807
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800808int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
809 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000810{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800811 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000812 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700813 int iscsi_task_attr;
814 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000815
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800816 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000817
818 hdr = (struct iscsi_scsi_req *) buf;
819 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820
821 /* FIXME; Add checks for AdditionalHeaderSegment */
822
823 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
824 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
825 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
826 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700827 return iscsit_add_reject_cmd(cmd,
828 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000829 }
830
831 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
832 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
833 /*
834 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
835 * that adds support for RESERVE/RELEASE. There is a bug
836 * add with this new functionality that sets R/W bits when
837 * neither CDB carries any READ or WRITE datapayloads.
838 */
839 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
840 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
841 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
842 goto done;
843 }
844
845 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
846 " set when Expected Data Transfer Length is 0 for"
847 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700848 return iscsit_add_reject_cmd(cmd,
849 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000850 }
851done:
852
853 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
854 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
855 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
856 " MUST be set if Expected Data Transfer Length is not 0."
857 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700858 return iscsit_add_reject_cmd(cmd,
859 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000860 }
861
862 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
863 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
864 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700865 return iscsit_add_reject_cmd(cmd,
866 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000867 }
868
869 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
870 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
871 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700872 return iscsit_add_reject_cmd(cmd,
873 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000874 }
875
876 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
877 pr_err("ImmediateData=No but DataSegmentLength=%u,"
878 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return iscsit_add_reject_cmd(cmd,
880 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000881 }
882
Nicholas Bellingerba159912013-07-03 03:48:24 -0700883 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000884 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
885 pr_err("Expected Data Transfer Length and Length of"
886 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
887 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700888 return iscsit_add_reject_cmd(cmd,
889 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000890 }
891
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400892 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000893 pr_err("DataSegmentLength: %u is greater than"
894 " EDTL: %u, protocol error.\n", payload_length,
895 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700896 return iscsit_add_reject_cmd(cmd,
897 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000898 }
899
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700900 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000901 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700902 " MaxXmitDataSegmentLength: %u, protocol error.\n",
903 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700904 return iscsit_add_reject_cmd(cmd,
905 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000906 }
907
908 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
909 pr_err("DataSegmentLength: %u is greater than"
910 " FirstBurstLength: %u, protocol error.\n",
911 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700912 return iscsit_add_reject_cmd(cmd,
913 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000914 }
915
916 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
917 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
918 DMA_NONE;
919
Andy Groverd28b11692012-04-03 15:51:22 -0700920 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700921 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
922 /*
923 * Figure out the SAM Task Attribute for the incoming SCSI CDB
924 */
925 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
926 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
927 sam_task_attr = MSG_SIMPLE_TAG;
928 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
929 sam_task_attr = MSG_ORDERED_TAG;
930 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
931 sam_task_attr = MSG_HEAD_TAG;
932 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
933 sam_task_attr = MSG_ACA_TAG;
934 else {
935 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
936 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
937 sam_task_attr = MSG_SIMPLE_TAG;
938 }
939
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000940 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
941 cmd->i_state = ISTATE_NEW_CMD;
942 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
943 cmd->immediate_data = (payload_length) ? 1 : 0;
944 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
945 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
946 if (cmd->unsolicited_data)
947 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
948
949 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
950 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
951 spin_lock_bh(&conn->sess->ttt_lock);
952 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
953 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
954 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
955 spin_unlock_bh(&conn->sess->ttt_lock);
956 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
957 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400958 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
959 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000960 cmd->first_burst_len = payload_length;
961
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800962 if (!conn->sess->sess_ops->RDMAExtensions &&
963 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000964 struct iscsi_datain_req *dr;
965
966 dr = iscsit_allocate_datain_req();
967 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700968 return iscsit_add_reject_cmd(cmd,
969 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000970
971 iscsit_attach_datain_req(cmd, dr);
972 }
973
974 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700975 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
976 */
977 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400978 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
979 cmd->data_direction, sam_task_attr,
980 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700981
982 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
983 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800984 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
985 conn->cid);
986
987 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700988
Christoph Hellwigde103c92012-11-06 12:24:09 -0800989 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
990 scsilun_to_int(&hdr->lun));
991 if (cmd->sense_reason)
992 goto attach_cmd;
993
994 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
995 if (cmd->sense_reason) {
996 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -0700997 return iscsit_add_reject_cmd(cmd,
998 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000999 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001000
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001001 goto attach_cmd;
1002 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001003
Christoph Hellwigde103c92012-11-06 12:24:09 -08001004 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001005 return iscsit_add_reject_cmd(cmd,
1006 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001007 }
1008
1009attach_cmd:
1010 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001011 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001012 spin_unlock_bh(&conn->cmd_lock);
1013 /*
1014 * Check if we need to delay processing because of ALUA
1015 * Active/NonOptimized primary access state..
1016 */
1017 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001018
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001019 return 0;
1020}
1021EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001022
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001023void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1024{
1025 iscsit_set_dataout_sequence_values(cmd);
1026
1027 spin_lock_bh(&cmd->dataout_timeout_lock);
1028 iscsit_start_dataout_timer(cmd, cmd->conn);
1029 spin_unlock_bh(&cmd->dataout_timeout_lock);
1030}
1031EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1032
1033int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1034 struct iscsi_scsi_req *hdr)
1035{
1036 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001037 /*
1038 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1039 * the Immediate Bit is not set, and no Immediate
1040 * Data is attached.
1041 *
1042 * A PDU/CmdSN carrying Immediate Data can only
1043 * be processed after the DataCRC has passed.
1044 * If the DataCRC fails, the CmdSN MUST NOT
1045 * be acknowledged. (See below)
1046 */
1047 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001048 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1049 (unsigned char *)hdr, hdr->cmdsn);
1050 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1051 return -1;
1052 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001053 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001054 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001055 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001056 }
1057
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001058 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001059
1060 /*
1061 * If no Immediate Data is attached, it's OK to return now.
1062 */
1063 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001064 if (!cmd->sense_reason && cmd->unsolicited_data)
1065 iscsit_set_unsoliticed_dataout(cmd);
1066 if (!cmd->sense_reason)
1067 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001068
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001069 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001070 return 0;
1071 }
1072
1073 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001074 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1075 * execution. These exceptions are processed in CmdSN order using
1076 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001077 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001078 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001079 if (cmd->reject_reason)
1080 return 0;
1081
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001082 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001083 }
1084 /*
1085 * Call directly into transport_generic_new_cmd() to perform
1086 * the backend memory allocation.
1087 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001088 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001089 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001090 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001091
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001092 return 0;
1093}
1094EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1095
1096static int
1097iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1098 bool dump_payload)
1099{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001100 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001101 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1102 /*
1103 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1104 */
1105 if (dump_payload == true)
1106 goto after_immediate_data;
1107
1108 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1109 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001110after_immediate_data:
1111 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1112 /*
1113 * A PDU/CmdSN carrying Immediate Data passed
1114 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1115 * Immediate Bit is not set.
1116 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001117 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1118 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001119 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001120 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001121
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001122 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001123 int rc;
1124
1125 rc = iscsit_dump_data_payload(cmd->conn,
1126 cmd->first_burst_len, 1);
1127 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1128 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001129 } else if (cmd->unsolicited_data)
1130 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001131
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001132 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1133 /*
1134 * Immediate Data failed DataCRC and ERL>=1,
1135 * silently drop this PDU and let the initiator
1136 * plug the CmdSN gap.
1137 *
1138 * FIXME: Send Unsolicited NOPIN with reserved
1139 * TTT here to help the initiator figure out
1140 * the missing CmdSN, although they should be
1141 * intelligent enough to determine the missing
1142 * CmdSN and issue a retry to plug the sequence.
1143 */
1144 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001145 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001146 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1147 return -1;
1148
1149 return 0;
1150}
1151
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001152static int
1153iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1154 unsigned char *buf)
1155{
1156 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1157 int rc, immed_data;
1158 bool dump_payload = false;
1159
1160 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1161 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001162 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001163 /*
1164 * Allocation iovecs needed for struct socket operations for
1165 * traditional iSCSI block I/O.
1166 */
1167 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001168 return iscsit_add_reject_cmd(cmd,
1169 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001170 }
1171 immed_data = cmd->immediate_data;
1172
1173 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1174 if (rc < 0)
1175 return rc;
1176 else if (rc > 0)
1177 dump_payload = true;
1178
1179 if (!immed_data)
1180 return 0;
1181
1182 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1183}
1184
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001185static u32 iscsit_do_crypto_hash_sg(
1186 struct hash_desc *hash,
1187 struct iscsi_cmd *cmd,
1188 u32 data_offset,
1189 u32 data_length,
1190 u32 padding,
1191 u8 *pad_bytes)
1192{
1193 u32 data_crc;
1194 u32 i;
1195 struct scatterlist *sg;
1196 unsigned int page_off;
1197
1198 crypto_hash_init(hash);
1199
1200 sg = cmd->first_data_sg;
1201 page_off = cmd->first_data_sg_off;
1202
1203 i = 0;
1204 while (data_length) {
1205 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1206
1207 crypto_hash_update(hash, &sg[i], cur_len);
1208
1209 data_length -= cur_len;
1210 page_off = 0;
1211 i++;
1212 }
1213
1214 if (padding) {
1215 struct scatterlist pad_sg;
1216
1217 sg_init_one(&pad_sg, pad_bytes, padding);
1218 crypto_hash_update(hash, &pad_sg, padding);
1219 }
1220 crypto_hash_final(hash, (u8 *) &data_crc);
1221
1222 return data_crc;
1223}
1224
1225static void iscsit_do_crypto_hash_buf(
1226 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001227 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001228 u32 payload_length,
1229 u32 padding,
1230 u8 *pad_bytes,
1231 u8 *data_crc)
1232{
1233 struct scatterlist sg;
1234
1235 crypto_hash_init(hash);
1236
Jörn Engel8359cf42011-11-24 02:05:51 +01001237 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001238 crypto_hash_update(hash, &sg, payload_length);
1239
1240 if (padding) {
1241 sg_init_one(&sg, pad_bytes, padding);
1242 crypto_hash_update(hash, &sg, padding);
1243 }
1244 crypto_hash_final(hash, data_crc);
1245}
1246
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001247int
1248iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1249 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001250{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001251 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001252 struct iscsi_cmd *cmd = NULL;
1253 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001254 u32 payload_length = ntoh24(hdr->dlength);
1255 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001256
1257 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001258 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1259 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001260 }
1261
1262 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001263 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001264
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001265 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001266 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001267 " MaxXmitDataSegmentLength: %u\n", payload_length,
1268 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001269 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1270 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001271 }
1272
1273 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1274 payload_length);
1275 if (!cmd)
1276 return 0;
1277
1278 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1279 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001280 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001281 payload_length, conn->cid);
1282
1283 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1284 pr_err("Command ITT: 0x%08x received DataOUT after"
1285 " last DataOUT received, dumping payload\n",
1286 cmd->init_task_tag);
1287 return iscsit_dump_data_payload(conn, payload_length, 1);
1288 }
1289
1290 if (cmd->data_direction != DMA_TO_DEVICE) {
1291 pr_err("Command ITT: 0x%08x received DataOUT for a"
1292 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001293 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001294 }
1295 se_cmd = &cmd->se_cmd;
1296 iscsit_mod_dataout_timer(cmd);
1297
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001298 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001299 pr_err("DataOut Offset: %u, Length %u greater than"
1300 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001301 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001302 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001303 }
1304
1305 if (cmd->unsolicited_data) {
1306 int dump_unsolicited_data = 0;
1307
1308 if (conn->sess->sess_ops->InitialR2T) {
1309 pr_err("Received unexpected unsolicited data"
1310 " while InitialR2T=Yes, protocol error.\n");
1311 transport_send_check_condition_and_sense(&cmd->se_cmd,
1312 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1313 return -1;
1314 }
1315 /*
1316 * Special case for dealing with Unsolicited DataOUT
1317 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1318 * failures;
1319 */
1320
1321 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001322 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001323 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001324 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001325
1326 if (dump_unsolicited_data) {
1327 /*
1328 * Check if a delayed TASK_ABORTED status needs to
1329 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1330 * received with the unsolicitied data out.
1331 */
1332 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1333 iscsit_stop_dataout_timer(cmd);
1334
1335 transport_check_aborted_status(se_cmd,
1336 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1337 return iscsit_dump_data_payload(conn, payload_length, 1);
1338 }
1339 } else {
1340 /*
1341 * For the normal solicited data path:
1342 *
1343 * Check for a delayed TASK_ABORTED status and dump any
1344 * incoming data out payload if one exists. Also, when the
1345 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1346 * data out sequence, we decrement outstanding_r2ts. Once
1347 * outstanding_r2ts reaches zero, go ahead and send the delayed
1348 * TASK_ABORTED status.
1349 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001350 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001351 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1352 if (--cmd->outstanding_r2ts < 1) {
1353 iscsit_stop_dataout_timer(cmd);
1354 transport_check_aborted_status(
1355 se_cmd, 1);
1356 }
1357
1358 return iscsit_dump_data_payload(conn, payload_length, 1);
1359 }
1360 }
1361 /*
1362 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1363 * within-command recovery checks before receiving the payload.
1364 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001365 rc = iscsit_check_pre_dataout(cmd, buf);
1366 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001367 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001368 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001369 return -1;
1370
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001371 *out_cmd = cmd;
1372 return 0;
1373}
1374EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1375
1376static int
1377iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1378 struct iscsi_data *hdr)
1379{
1380 struct kvec *iov;
1381 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1382 u32 payload_length = ntoh24(hdr->dlength);
1383 int iov_ret, data_crc_failed = 0;
1384
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001385 rx_size += payload_length;
1386 iov = &cmd->iov_data[0];
1387
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001388 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1389 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001390 if (iov_ret < 0)
1391 return -1;
1392
1393 iov_count += iov_ret;
1394
1395 padding = ((-payload_length) & 3);
1396 if (padding != 0) {
1397 iov[iov_count].iov_base = cmd->pad_bytes;
1398 iov[iov_count++].iov_len = padding;
1399 rx_size += padding;
1400 pr_debug("Receiving %u padding bytes.\n", padding);
1401 }
1402
1403 if (conn->conn_ops->DataDigest) {
1404 iov[iov_count].iov_base = &checksum;
1405 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1406 rx_size += ISCSI_CRC_LEN;
1407 }
1408
1409 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1410
1411 iscsit_unmap_iovec(cmd);
1412
1413 if (rx_got != rx_size)
1414 return -1;
1415
1416 if (conn->conn_ops->DataDigest) {
1417 u32 data_crc;
1418
1419 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001420 be32_to_cpu(hdr->offset),
1421 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001422 cmd->pad_bytes);
1423
1424 if (checksum != data_crc) {
1425 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1426 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1427 " does not match computed 0x%08x\n",
1428 hdr->itt, hdr->offset, payload_length,
1429 hdr->datasn, checksum, data_crc);
1430 data_crc_failed = 1;
1431 } else {
1432 pr_debug("Got CRC32C DataDigest 0x%08x for"
1433 " %u bytes of Data Out\n", checksum,
1434 payload_length);
1435 }
1436 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001437
1438 return data_crc_failed;
1439}
1440
1441int
1442iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1443 bool data_crc_failed)
1444{
1445 struct iscsi_conn *conn = cmd->conn;
1446 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001447 /*
1448 * Increment post receive data and CRC values or perform
1449 * within-command recovery.
1450 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001451 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1452 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001453 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001454 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001455 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001456 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1457 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001458 /*
1459 * Handle extra special case for out of order
1460 * Unsolicited Data Out.
1461 */
1462 spin_lock_bh(&cmd->istate_lock);
1463 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1464 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1465 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1466 spin_unlock_bh(&cmd->istate_lock);
1467
1468 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001469 if (ooo_cmdsn)
1470 return 0;
1471 target_execute_cmd(&cmd->se_cmd);
1472 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001473 } else /* DATAOUT_CANNOT_RECOVER */
1474 return -1;
1475
1476 return 0;
1477}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001478EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001479
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001480static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1481{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001482 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001483 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1484 int rc;
1485 bool data_crc_failed = false;
1486
1487 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1488 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001489 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001490 else if (!cmd)
1491 return 0;
1492
1493 rc = iscsit_get_dataout(conn, cmd, hdr);
1494 if (rc < 0)
1495 return rc;
1496 else if (rc > 0)
1497 data_crc_failed = true;
1498
1499 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1500}
1501
Nicholas Bellinger778de362013-06-14 16:07:47 -07001502int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1503 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001504{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001505 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001506
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001507 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001508 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1509 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001510 if (!cmd)
1511 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1512 (unsigned char *)hdr);
1513
Nicholas Bellingerba159912013-07-03 03:48:24 -07001514 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1515 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001516 }
1517
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001518 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001519 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001520 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001521 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001522 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001523 if (!cmd)
1524 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1525 (unsigned char *)hdr);
1526
Nicholas Bellingerba159912013-07-03 03:48:24 -07001527 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1528 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001529 }
1530
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001531 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001532 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001533 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001534 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1535 payload_length);
1536 /*
1537 * This is not a response to a Unsolicited NopIN, which means
1538 * it can either be a NOPOUT ping request (with a valid ITT),
1539 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1540 * Either way, make sure we allocate an struct iscsi_cmd, as both
1541 * can contain ping data.
1542 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001543 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001544 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1545 cmd->i_state = ISTATE_SEND_NOPIN;
1546 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1547 1 : 0);
1548 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1549 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001550 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1551 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001552 cmd->data_direction = DMA_NONE;
1553 }
1554
Nicholas Bellinger778de362013-06-14 16:07:47 -07001555 return 0;
1556}
1557EXPORT_SYMBOL(iscsit_setup_nop_out);
1558
1559int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1560 struct iscsi_nopout *hdr)
1561{
1562 struct iscsi_cmd *cmd_p = NULL;
1563 int cmdsn_ret = 0;
1564 /*
1565 * Initiator is expecting a NopIN ping reply..
1566 */
1567 if (hdr->itt != RESERVED_ITT) {
1568 BUG_ON(!cmd);
1569
1570 spin_lock_bh(&conn->cmd_lock);
1571 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1572 spin_unlock_bh(&conn->cmd_lock);
1573
1574 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1575
1576 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1577 iscsit_add_cmd_to_response_queue(cmd, conn,
1578 cmd->i_state);
1579 return 0;
1580 }
1581
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001582 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1583 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001584 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1585 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001586 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001587 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001588
1589 return 0;
1590 }
1591 /*
1592 * This was a response to a unsolicited NOPIN ping.
1593 */
1594 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1595 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1596 if (!cmd_p)
1597 return -EINVAL;
1598
1599 iscsit_stop_nopin_response_timer(conn);
1600
1601 cmd_p->i_state = ISTATE_REMOVE;
1602 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1603
1604 iscsit_start_nopin_timer(conn);
1605 return 0;
1606 }
1607 /*
1608 * Otherwise, initiator is not expecting a NOPIN is response.
1609 * Just ignore for now.
1610 */
1611 return 0;
1612}
1613EXPORT_SYMBOL(iscsit_process_nop_out);
1614
1615static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1616 unsigned char *buf)
1617{
1618 unsigned char *ping_data = NULL;
1619 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1620 struct kvec *iov = NULL;
1621 u32 payload_length = ntoh24(hdr->dlength);
1622 int ret;
1623
1624 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1625 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001626 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001627 /*
1628 * Handle NOP-OUT payload for traditional iSCSI sockets
1629 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001630 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001631 u32 checksum, data_crc, padding = 0;
1632 int niov = 0, rx_got, rx_size = payload_length;
1633
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001634 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1635 if (!ping_data) {
1636 pr_err("Unable to allocate memory for"
1637 " NOPOUT ping data.\n");
1638 ret = -1;
1639 goto out;
1640 }
1641
1642 iov = &cmd->iov_misc[0];
1643 iov[niov].iov_base = ping_data;
1644 iov[niov++].iov_len = payload_length;
1645
1646 padding = ((-payload_length) & 3);
1647 if (padding != 0) {
1648 pr_debug("Receiving %u additional bytes"
1649 " for padding.\n", padding);
1650 iov[niov].iov_base = &cmd->pad_bytes;
1651 iov[niov++].iov_len = padding;
1652 rx_size += padding;
1653 }
1654 if (conn->conn_ops->DataDigest) {
1655 iov[niov].iov_base = &checksum;
1656 iov[niov++].iov_len = ISCSI_CRC_LEN;
1657 rx_size += ISCSI_CRC_LEN;
1658 }
1659
1660 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1661 if (rx_got != rx_size) {
1662 ret = -1;
1663 goto out;
1664 }
1665
1666 if (conn->conn_ops->DataDigest) {
1667 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1668 ping_data, payload_length,
1669 padding, cmd->pad_bytes,
1670 (u8 *)&data_crc);
1671
1672 if (checksum != data_crc) {
1673 pr_err("Ping data CRC32C DataDigest"
1674 " 0x%08x does not match computed 0x%08x\n",
1675 checksum, data_crc);
1676 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1677 pr_err("Unable to recover from"
1678 " NOPOUT Ping DataCRC failure while in"
1679 " ERL=0.\n");
1680 ret = -1;
1681 goto out;
1682 } else {
1683 /*
1684 * Silently drop this PDU and let the
1685 * initiator plug the CmdSN gap.
1686 */
1687 pr_debug("Dropping NOPOUT"
1688 " Command CmdSN: 0x%08x due to"
1689 " DataCRC error.\n", hdr->cmdsn);
1690 ret = 0;
1691 goto out;
1692 }
1693 } else {
1694 pr_debug("Got CRC32C DataDigest"
1695 " 0x%08x for %u bytes of ping data.\n",
1696 checksum, payload_length);
1697 }
1698 }
1699
1700 ping_data[payload_length] = '\0';
1701 /*
1702 * Attach ping data to struct iscsi_cmd->buf_ptr.
1703 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001704 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001705 cmd->buf_ptr_size = payload_length;
1706
1707 pr_debug("Got %u bytes of NOPOUT ping"
1708 " data.\n", payload_length);
1709 pr_debug("Ping Data: \"%s\"\n", ping_data);
1710 }
1711
Nicholas Bellinger778de362013-06-14 16:07:47 -07001712 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001713out:
1714 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001715 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001716
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001717 kfree(ping_data);
1718 return ret;
1719}
1720
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001721int
1722iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1723 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001724{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001725 struct se_tmr_req *se_tmr;
1726 struct iscsi_tmr_req *tmr_req;
1727 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001728 int out_of_order_cmdsn = 0, ret;
1729 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001730 u8 function;
1731
1732 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001733 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1734 function = hdr->flags;
1735
1736 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1737 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1738 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1739 hdr->rtt, hdr->refcmdsn, conn->cid);
1740
1741 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1742 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001743 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001744 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001745 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001746 }
1747
1748 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1749 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1750 pr_err("Task Management Request TASK_REASSIGN not"
1751 " issued as immediate command, bad iSCSI Initiator"
1752 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001753 return iscsit_add_reject_cmd(cmd,
1754 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 }
1756 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001757 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1758 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759
Andy Groverd28b11692012-04-03 15:51:22 -07001760 cmd->data_direction = DMA_NONE;
1761
1762 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1763 if (!cmd->tmr_req) {
1764 pr_err("Unable to allocate memory for"
1765 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001766 return iscsit_add_reject_cmd(cmd,
1767 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1768 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001769 }
1770
1771 /*
1772 * TASK_REASSIGN for ERL=2 / connection stays inside of
1773 * LIO-Target $FABRIC_MOD
1774 */
1775 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1776
1777 u8 tcm_function;
1778 int ret;
1779
1780 transport_init_se_cmd(&cmd->se_cmd,
1781 &lio_target_fabric_configfs->tf_ops,
1782 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001783 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001784
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001785 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1786 sess_ref = true;
1787
Andy Groverd28b11692012-04-03 15:51:22 -07001788 switch (function) {
1789 case ISCSI_TM_FUNC_ABORT_TASK:
1790 tcm_function = TMR_ABORT_TASK;
1791 break;
1792 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1793 tcm_function = TMR_ABORT_TASK_SET;
1794 break;
1795 case ISCSI_TM_FUNC_CLEAR_ACA:
1796 tcm_function = TMR_CLEAR_ACA;
1797 break;
1798 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1799 tcm_function = TMR_CLEAR_TASK_SET;
1800 break;
1801 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1802 tcm_function = TMR_LUN_RESET;
1803 break;
1804 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1805 tcm_function = TMR_TARGET_WARM_RESET;
1806 break;
1807 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1808 tcm_function = TMR_TARGET_COLD_RESET;
1809 break;
1810 default:
1811 pr_err("Unknown iSCSI TMR Function:"
1812 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001813 return iscsit_add_reject_cmd(cmd,
1814 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001815 }
1816
1817 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1818 tcm_function, GFP_KERNEL);
1819 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001820 return iscsit_add_reject_cmd(cmd,
1821 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001822
1823 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1824 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001825
1826 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1827 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1828 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1829 cmd->init_task_tag = hdr->itt;
1830 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001831 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1832 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001833 se_tmr = cmd->se_cmd.se_tmr_req;
1834 tmr_req = cmd->tmr_req;
1835 /*
1836 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1837 */
1838 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001839 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1840 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001841 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1843 goto attach;
1844 }
1845 }
1846
1847 switch (function) {
1848 case ISCSI_TM_FUNC_ABORT_TASK:
1849 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001850 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001852 break;
1853 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1854 case ISCSI_TM_FUNC_CLEAR_ACA:
1855 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1856 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1857 break;
1858 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1859 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001860 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1861 goto attach;
1862 }
1863 break;
1864 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1865 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001866 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1867 goto attach;
1868 }
1869 break;
1870 case ISCSI_TM_FUNC_TASK_REASSIGN:
1871 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1872 /*
1873 * Perform sanity checks on the ExpDataSN only if the
1874 * TASK_REASSIGN was successful.
1875 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001876 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001877 break;
1878
1879 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001880 return iscsit_add_reject_cmd(cmd,
1881 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001882 break;
1883 default:
1884 pr_err("Unknown TMR function: 0x%02x, protocol"
1885 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001886 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1887 goto attach;
1888 }
1889
1890 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1891 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1892 se_tmr->call_transport = 1;
1893attach:
1894 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001895 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001896 spin_unlock_bh(&conn->cmd_lock);
1897
1898 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001899 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001900 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1901 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001902 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001903 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001904 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001905 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001906 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001907 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001908
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001909 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001910 return 0;
1911 /*
1912 * Found the referenced task, send to transport for processing.
1913 */
1914 if (se_tmr->call_transport)
1915 return transport_generic_handle_tmr(&cmd->se_cmd);
1916
1917 /*
1918 * Could not find the referenced LUN, task, or Task Management
1919 * command not authorized or supported. Change state and
1920 * let the tx_thread send the response.
1921 *
1922 * For connection recovery, this is also the default action for
1923 * TMR TASK_REASSIGN.
1924 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001925 if (sess_ref) {
1926 pr_debug("Handle TMR, using sess_ref=true check\n");
1927 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1928 }
1929
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001930 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1931 return 0;
1932}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001933EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001934
1935/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001936int
1937iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1938 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001939{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001940 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001941
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001942 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001943 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001944 "greater than MaxXmitDataSegmentLength %u.\n",
1945 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001946 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1947 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001948 }
1949
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001950 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1951 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1952 pr_err("Multi sequence text commands currently not supported\n");
1953 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1954 (unsigned char *)hdr);
1955 }
1956
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001957 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1958 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1959 hdr->exp_statsn, payload_length);
1960
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001961 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1962 cmd->i_state = ISTATE_SEND_TEXTRSP;
1963 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1964 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1965 cmd->targ_xfer_tag = 0xFFFFFFFF;
1966 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1967 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1968 cmd->data_direction = DMA_NONE;
1969
1970 return 0;
1971}
1972EXPORT_SYMBOL(iscsit_setup_text_cmd);
1973
1974int
1975iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1976 struct iscsi_text *hdr)
1977{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001978 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001979 int cmdsn_ret;
1980
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001981 if (!text_in) {
1982 pr_err("Unable to locate text_in buffer for sendtargets"
1983 " discovery\n");
1984 goto reject;
1985 }
1986 if (strncmp("SendTargets", text_in, 11) != 0) {
1987 pr_err("Received Text Data that is not"
1988 " SendTargets, cannot continue.\n");
1989 goto reject;
1990 }
1991 text_ptr = strchr(text_in, '=');
1992 if (!text_ptr) {
1993 pr_err("No \"=\" separator found in Text Data,"
1994 " cannot continue.\n");
1995 goto reject;
1996 }
1997 if (!strncmp("=All", text_ptr, 4)) {
1998 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07001999 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2000 !strncmp("=eui.", text_ptr, 5)) {
2001 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002002 } else {
2003 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2004 goto reject;
2005 }
2006
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002007 spin_lock_bh(&conn->cmd_lock);
2008 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2009 spin_unlock_bh(&conn->cmd_lock);
2010
2011 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2012
2013 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002014 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2015 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002016 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002017 return -1;
2018
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002019 return 0;
2020 }
2021
2022 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002023
2024reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002025 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2026 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002027}
2028EXPORT_SYMBOL(iscsit_process_text_cmd);
2029
2030static int
2031iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2032 unsigned char *buf)
2033{
2034 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2035 char *text_in = NULL;
2036 u32 payload_length = ntoh24(hdr->dlength);
2037 int rx_size, rc;
2038
2039 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2040 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002041 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002042
2043 rx_size = payload_length;
2044 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002045 u32 checksum = 0, data_crc = 0;
2046 u32 padding = 0, pad_bytes = 0;
2047 int niov = 0, rx_got;
2048 struct kvec iov[3];
2049
2050 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002051 if (!text_in) {
2052 pr_err("Unable to allocate memory for"
2053 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002054 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002055 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002056 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002057
2058 memset(iov, 0, 3 * sizeof(struct kvec));
2059 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002060 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002061
2062 padding = ((-payload_length) & 3);
2063 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002064 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002065 iov[niov++].iov_len = padding;
2066 rx_size += padding;
2067 pr_debug("Receiving %u additional bytes"
2068 " for padding.\n", padding);
2069 }
2070 if (conn->conn_ops->DataDigest) {
2071 iov[niov].iov_base = &checksum;
2072 iov[niov++].iov_len = ISCSI_CRC_LEN;
2073 rx_size += ISCSI_CRC_LEN;
2074 }
2075
2076 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002077 if (rx_got != rx_size)
2078 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079
2080 if (conn->conn_ops->DataDigest) {
2081 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002082 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002083 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002084 (u8 *)&data_crc);
2085
2086 if (checksum != data_crc) {
2087 pr_err("Text data CRC32C DataDigest"
2088 " 0x%08x does not match computed"
2089 " 0x%08x\n", checksum, data_crc);
2090 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2091 pr_err("Unable to recover from"
2092 " Text Data digest failure while in"
2093 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002094 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002095 } else {
2096 /*
2097 * Silently drop this PDU and let the
2098 * initiator plug the CmdSN gap.
2099 */
2100 pr_debug("Dropping Text"
2101 " Command CmdSN: 0x%08x due to"
2102 " DataCRC error.\n", hdr->cmdsn);
2103 kfree(text_in);
2104 return 0;
2105 }
2106 } else {
2107 pr_debug("Got CRC32C DataDigest"
2108 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002109 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002110 }
2111 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002112 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002113 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002114 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002115 }
2116
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002117 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002118
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002119reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002120 kfree(cmd->text_in_ptr);
2121 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002122 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002123}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002124EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002125
2126int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2127{
2128 struct iscsi_conn *conn_p;
2129 struct iscsi_session *sess = conn->sess;
2130
2131 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2132 " for SID: %u.\n", conn->cid, conn->sess->sid);
2133
2134 atomic_set(&sess->session_logout, 1);
2135 atomic_set(&conn->conn_logout_remove, 1);
2136 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2137
2138 iscsit_inc_conn_usage_count(conn);
2139 iscsit_inc_session_usage_count(sess);
2140
2141 spin_lock_bh(&sess->conn_lock);
2142 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2143 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2144 continue;
2145
2146 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2147 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2148 }
2149 spin_unlock_bh(&sess->conn_lock);
2150
2151 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2152
2153 return 0;
2154}
2155
2156int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2157{
2158 struct iscsi_conn *l_conn;
2159 struct iscsi_session *sess = conn->sess;
2160
2161 pr_debug("Received logout request CLOSECONNECTION for CID:"
2162 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2163
2164 /*
2165 * A Logout Request with a CLOSECONNECTION reason code for a CID
2166 * can arrive on a connection with a differing CID.
2167 */
2168 if (conn->cid == cmd->logout_cid) {
2169 spin_lock_bh(&conn->state_lock);
2170 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2171 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2172
2173 atomic_set(&conn->conn_logout_remove, 1);
2174 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2175 iscsit_inc_conn_usage_count(conn);
2176
2177 spin_unlock_bh(&conn->state_lock);
2178 } else {
2179 /*
2180 * Handle all different cid CLOSECONNECTION requests in
2181 * iscsit_logout_post_handler_diffcid() as to give enough
2182 * time for any non immediate command's CmdSN to be
2183 * acknowledged on the connection in question.
2184 *
2185 * Here we simply make sure the CID is still around.
2186 */
2187 l_conn = iscsit_get_conn_from_cid(sess,
2188 cmd->logout_cid);
2189 if (!l_conn) {
2190 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2191 iscsit_add_cmd_to_response_queue(cmd, conn,
2192 cmd->i_state);
2193 return 0;
2194 }
2195
2196 iscsit_dec_conn_usage_count(l_conn);
2197 }
2198
2199 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2200
2201 return 0;
2202}
2203
2204int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2205{
2206 struct iscsi_session *sess = conn->sess;
2207
2208 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2209 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2210
2211 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2212 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2213 " while ERL!=2.\n");
2214 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2215 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2216 return 0;
2217 }
2218
2219 if (conn->cid == cmd->logout_cid) {
2220 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2221 " with CID: %hu on CID: %hu, implementation error.\n",
2222 cmd->logout_cid, conn->cid);
2223 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2224 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2225 return 0;
2226 }
2227
2228 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2229
2230 return 0;
2231}
2232
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002233int
2234iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2235 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002236{
2237 int cmdsn_ret, logout_remove = 0;
2238 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002239 struct iscsi_logout *hdr;
2240 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2241
2242 hdr = (struct iscsi_logout *) buf;
2243 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002244
2245 if (tiqn) {
2246 spin_lock(&tiqn->logout_stats.lock);
2247 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2248 tiqn->logout_stats.normal_logouts++;
2249 else
2250 tiqn->logout_stats.abnormal_logouts++;
2251 spin_unlock(&tiqn->logout_stats.lock);
2252 }
2253
2254 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2255 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2256 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2257 hdr->cid, conn->cid);
2258
2259 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2260 pr_err("Received logout request on connection that"
2261 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002262 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002263 return 0;
2264 }
2265
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2267 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2268 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2269 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2270 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002271 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2272 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2273 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002274 cmd->logout_reason = reason_code;
2275 cmd->data_direction = DMA_NONE;
2276
2277 /*
2278 * We need to sleep in these cases (by returning 1) until the Logout
2279 * Response gets sent in the tx thread.
2280 */
2281 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2282 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002283 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002284 logout_remove = 1;
2285
2286 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002287 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002288 spin_unlock_bh(&conn->cmd_lock);
2289
2290 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002291 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002292
2293 /*
2294 * Immediate commands are executed, well, immediately.
2295 * Non-Immediate Logout Commands are executed in CmdSN order.
2296 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002297 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002298 int ret = iscsit_execute_cmd(cmd, 0);
2299
2300 if (ret < 0)
2301 return ret;
2302 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002303 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002304 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002306 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2307 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002308 }
2309
2310 return logout_remove;
2311}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002312EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002313
2314static int iscsit_handle_snack(
2315 struct iscsi_conn *conn,
2316 unsigned char *buf)
2317{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002318 struct iscsi_snack *hdr;
2319
2320 hdr = (struct iscsi_snack *) buf;
2321 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322
2323 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2324 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2325 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2326 hdr->begrun, hdr->runlength, conn->cid);
2327
2328 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2329 pr_err("Initiator sent SNACK request while in"
2330 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002331 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2332 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002333 }
2334 /*
2335 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2336 * call from inside iscsi_send_recovery_datain_or_r2t().
2337 */
2338 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2339 case 0:
2340 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002341 hdr->itt,
2342 be32_to_cpu(hdr->ttt),
2343 be32_to_cpu(hdr->begrun),
2344 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002345 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002346 return iscsit_handle_status_snack(conn, hdr->itt,
2347 be32_to_cpu(hdr->ttt),
2348 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002349 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002350 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2351 be32_to_cpu(hdr->begrun),
2352 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002353 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2354 /* FIXME: Support R-Data SNACK */
2355 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002356 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2357 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002358 default:
2359 pr_err("Unknown SNACK type 0x%02x, protocol"
2360 " error.\n", hdr->flags & 0x0f);
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 return 0;
2366}
2367
2368static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2369{
2370 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2371 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2372 wait_for_completion_interruptible_timeout(
2373 &conn->rx_half_close_comp,
2374 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2375 }
2376}
2377
2378static int iscsit_handle_immediate_data(
2379 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002380 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002381 u32 length)
2382{
2383 int iov_ret, rx_got = 0, rx_size = 0;
2384 u32 checksum, iov_count = 0, padding = 0;
2385 struct iscsi_conn *conn = cmd->conn;
2386 struct kvec *iov;
2387
2388 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2389 if (iov_ret < 0)
2390 return IMMEDIATE_DATA_CANNOT_RECOVER;
2391
2392 rx_size = length;
2393 iov_count = iov_ret;
2394 iov = &cmd->iov_data[0];
2395
2396 padding = ((-length) & 3);
2397 if (padding != 0) {
2398 iov[iov_count].iov_base = cmd->pad_bytes;
2399 iov[iov_count++].iov_len = padding;
2400 rx_size += padding;
2401 }
2402
2403 if (conn->conn_ops->DataDigest) {
2404 iov[iov_count].iov_base = &checksum;
2405 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2406 rx_size += ISCSI_CRC_LEN;
2407 }
2408
2409 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2410
2411 iscsit_unmap_iovec(cmd);
2412
2413 if (rx_got != rx_size) {
2414 iscsit_rx_thread_wait_for_tcp(conn);
2415 return IMMEDIATE_DATA_CANNOT_RECOVER;
2416 }
2417
2418 if (conn->conn_ops->DataDigest) {
2419 u32 data_crc;
2420
2421 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2422 cmd->write_data_done, length, padding,
2423 cmd->pad_bytes);
2424
2425 if (checksum != data_crc) {
2426 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2427 " does not match computed 0x%08x\n", checksum,
2428 data_crc);
2429
2430 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2431 pr_err("Unable to recover from"
2432 " Immediate Data digest failure while"
2433 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002434 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002435 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002436 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002437 return IMMEDIATE_DATA_CANNOT_RECOVER;
2438 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002439 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002440 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002441 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002442 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2443 }
2444 } else {
2445 pr_debug("Got CRC32C DataDigest 0x%08x for"
2446 " %u bytes of Immediate Data\n", checksum,
2447 length);
2448 }
2449 }
2450
2451 cmd->write_data_done += length;
2452
Andy Groverebf1d952012-04-03 15:51:24 -07002453 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002454 spin_lock_bh(&cmd->istate_lock);
2455 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2456 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2457 spin_unlock_bh(&cmd->istate_lock);
2458 }
2459
2460 return IMMEDIATE_DATA_NORMAL_OPERATION;
2461}
2462
2463/*
2464 * Called with sess->conn_lock held.
2465 */
2466/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2467 with active network interface */
2468static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2469{
2470 struct iscsi_cmd *cmd;
2471 struct iscsi_conn *conn_p;
2472
2473 /*
2474 * Only send a Asynchronous Message on connections whos network
2475 * interface is still functional.
2476 */
2477 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2478 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2479 iscsit_inc_conn_usage_count(conn_p);
2480 break;
2481 }
2482 }
2483
2484 if (!conn_p)
2485 return;
2486
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002487 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002488 if (!cmd) {
2489 iscsit_dec_conn_usage_count(conn_p);
2490 return;
2491 }
2492
2493 cmd->logout_cid = conn->cid;
2494 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2495 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2496
2497 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002498 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002499 spin_unlock_bh(&conn_p->cmd_lock);
2500
2501 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2502 iscsit_dec_conn_usage_count(conn_p);
2503}
2504
2505static int iscsit_send_conn_drop_async_message(
2506 struct iscsi_cmd *cmd,
2507 struct iscsi_conn *conn)
2508{
2509 struct iscsi_async *hdr;
2510
2511 cmd->tx_size = ISCSI_HDR_LEN;
2512 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2513
2514 hdr = (struct iscsi_async *) cmd->pdu;
2515 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2516 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002517 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002518 cmd->targ_xfer_tag = 0xFFFFFFFF;
2519 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2520 cmd->stat_sn = conn->stat_sn++;
2521 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2522 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2523 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2524 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2525 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2526 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2527 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2528
2529 if (conn->conn_ops->HeaderDigest) {
2530 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2531
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002532 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2533 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002534
2535 cmd->tx_size += ISCSI_CRC_LEN;
2536 pr_debug("Attaching CRC32C HeaderDigest to"
2537 " Async Message 0x%08x\n", *header_digest);
2538 }
2539
2540 cmd->iov_misc[0].iov_base = cmd->pdu;
2541 cmd->iov_misc[0].iov_len = cmd->tx_size;
2542 cmd->iov_misc_count = 1;
2543
2544 pr_debug("Sending Connection Dropped Async Message StatSN:"
2545 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2546 cmd->logout_cid, conn->cid);
2547 return 0;
2548}
2549
Andy Grover6f3c0e62012-04-03 15:51:09 -07002550static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2551{
2552 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2553 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2554 wait_for_completion_interruptible_timeout(
2555 &conn->tx_half_close_comp,
2556 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2557 }
2558}
2559
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002560static void
2561iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2562 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2563 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002564{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002565 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2566 hdr->flags = datain->flags;
2567 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2568 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2569 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2570 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2571 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2572 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2573 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2574 }
2575 }
2576 hton24(hdr->dlength, datain->length);
2577 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2578 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2579 (struct scsi_lun *)&hdr->lun);
2580 else
2581 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2582
2583 hdr->itt = cmd->init_task_tag;
2584
2585 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2586 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2587 else
2588 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2589 if (set_statsn)
2590 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2591 else
2592 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2593
2594 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2595 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2596 hdr->datasn = cpu_to_be32(datain->data_sn);
2597 hdr->offset = cpu_to_be32(datain->offset);
2598
2599 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2600 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2601 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2602 ntohl(hdr->offset), datain->length, conn->cid);
2603}
2604
2605static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2606{
2607 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002608 struct iscsi_datain datain;
2609 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002610 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002611 u32 iov_count = 0, tx_size = 0;
2612 int eodr = 0, ret, iov_ret;
2613 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002614
2615 memset(&datain, 0, sizeof(struct iscsi_datain));
2616 dr = iscsit_get_datain_values(cmd, &datain);
2617 if (!dr) {
2618 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2619 cmd->init_task_tag);
2620 return -1;
2621 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002622 /*
2623 * Be paranoid and double check the logic for now.
2624 */
Andy Groverebf1d952012-04-03 15:51:24 -07002625 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002626 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2627 " datain.length: %u exceeds cmd->data_length: %u\n",
2628 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002629 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002630 return -1;
2631 }
2632
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002633 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 /*
2635 * Special case for successfully execution w/ both DATAIN
2636 * and Sense Data.
2637 */
2638 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2639 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2640 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2641 else {
2642 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2643 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2644 iscsit_increment_maxcmdsn(cmd, conn->sess);
2645 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002646 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002647 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002648 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2649 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002650 }
2651
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002652 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002653
2654 iov = &cmd->iov_data[0];
2655 iov[iov_count].iov_base = cmd->pdu;
2656 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2657 tx_size += ISCSI_HDR_LEN;
2658
2659 if (conn->conn_ops->HeaderDigest) {
2660 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2661
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002662 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2663 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002664
2665 iov[0].iov_len += ISCSI_CRC_LEN;
2666 tx_size += ISCSI_CRC_LEN;
2667
2668 pr_debug("Attaching CRC32 HeaderDigest"
2669 " for DataIN PDU 0x%08x\n", *header_digest);
2670 }
2671
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002672 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2673 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002674 if (iov_ret < 0)
2675 return -1;
2676
2677 iov_count += iov_ret;
2678 tx_size += datain.length;
2679
2680 cmd->padding = ((-datain.length) & 3);
2681 if (cmd->padding) {
2682 iov[iov_count].iov_base = cmd->pad_bytes;
2683 iov[iov_count++].iov_len = cmd->padding;
2684 tx_size += cmd->padding;
2685
2686 pr_debug("Attaching %u padding bytes\n",
2687 cmd->padding);
2688 }
2689 if (conn->conn_ops->DataDigest) {
2690 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2691 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2692
2693 iov[iov_count].iov_base = &cmd->data_crc;
2694 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2695 tx_size += ISCSI_CRC_LEN;
2696
2697 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2698 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2699 }
2700
2701 cmd->iov_data_count = iov_count;
2702 cmd->tx_size = tx_size;
2703
Andy Grover6f3c0e62012-04-03 15:51:09 -07002704 /* sendpage is preferred but can't insert markers */
2705 if (!conn->conn_ops->IFMarker)
2706 ret = iscsit_fe_sendpage_sg(cmd, conn);
2707 else
2708 ret = iscsit_send_tx_data(cmd, conn, 0);
2709
2710 iscsit_unmap_iovec(cmd);
2711
2712 if (ret < 0) {
2713 iscsit_tx_thread_wait_for_tcp(conn);
2714 return ret;
2715 }
2716
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002717 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002718 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002719 2 : 1;
2720 iscsit_free_datain_req(cmd, dr);
2721 }
2722
Andy Grover6f3c0e62012-04-03 15:51:09 -07002723 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002724}
2725
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002726int
2727iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2728 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002729{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002730 struct iscsi_conn *logout_conn = NULL;
2731 struct iscsi_conn_recovery *cr = NULL;
2732 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002733 /*
2734 * The actual shutting down of Sessions and/or Connections
2735 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2736 * is done in scsi_logout_post_handler().
2737 */
2738 switch (cmd->logout_reason) {
2739 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2740 pr_debug("iSCSI session logout successful, setting"
2741 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2742 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2743 break;
2744 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2745 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2746 break;
2747 /*
2748 * For CLOSECONNECTION logout requests carrying
2749 * a matching logout CID -> local CID, the reference
2750 * for the local CID will have been incremented in
2751 * iscsi_logout_closeconnection().
2752 *
2753 * For CLOSECONNECTION logout requests carrying
2754 * a different CID than the connection it arrived
2755 * on, the connection responding to cmd->logout_cid
2756 * is stopped in iscsit_logout_post_handler_diffcid().
2757 */
2758
2759 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2760 " successful.\n", cmd->logout_cid, conn->cid);
2761 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2762 break;
2763 case ISCSI_LOGOUT_REASON_RECOVERY:
2764 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2765 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2766 break;
2767 /*
2768 * If the connection is still active from our point of view
2769 * force connection recovery to occur.
2770 */
2771 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2772 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002773 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002774 iscsit_connection_reinstatement_rcfr(logout_conn);
2775 iscsit_dec_conn_usage_count(logout_conn);
2776 }
2777
2778 cr = iscsit_get_inactive_connection_recovery_entry(
2779 conn->sess, cmd->logout_cid);
2780 if (!cr) {
2781 pr_err("Unable to locate CID: %hu for"
2782 " REMOVECONNFORRECOVERY Logout Request.\n",
2783 cmd->logout_cid);
2784 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2785 break;
2786 }
2787
2788 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2789
2790 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2791 " for recovery for CID: %hu on CID: %hu successful.\n",
2792 cmd->logout_cid, conn->cid);
2793 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2794 break;
2795 default:
2796 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2797 cmd->logout_reason);
2798 return -1;
2799 }
2800
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002801 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2802 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2803 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002804 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002805 cmd->stat_sn = conn->stat_sn++;
2806 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2807
2808 iscsit_increment_maxcmdsn(cmd, conn->sess);
2809 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2810 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2811
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002812 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2813 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2814 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2815 cmd->logout_cid, conn->cid);
2816
2817 return 0;
2818}
2819EXPORT_SYMBOL(iscsit_build_logout_rsp);
2820
2821static int
2822iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2823{
2824 struct kvec *iov;
2825 int niov = 0, tx_size, rc;
2826
2827 rc = iscsit_build_logout_rsp(cmd, conn,
2828 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2829 if (rc < 0)
2830 return rc;
2831
2832 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002833 iov = &cmd->iov_misc[0];
2834 iov[niov].iov_base = cmd->pdu;
2835 iov[niov++].iov_len = ISCSI_HDR_LEN;
2836
2837 if (conn->conn_ops->HeaderDigest) {
2838 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2839
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002840 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2841 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002842
2843 iov[0].iov_len += ISCSI_CRC_LEN;
2844 tx_size += ISCSI_CRC_LEN;
2845 pr_debug("Attaching CRC32C HeaderDigest to"
2846 " Logout Response 0x%08x\n", *header_digest);
2847 }
2848 cmd->iov_misc_count = niov;
2849 cmd->tx_size = tx_size;
2850
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002851 return 0;
2852}
2853
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002854void
2855iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2856 struct iscsi_nopin *hdr, bool nopout_response)
2857{
2858 hdr->opcode = ISCSI_OP_NOOP_IN;
2859 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2860 hton24(hdr->dlength, cmd->buf_ptr_size);
2861 if (nopout_response)
2862 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2863 hdr->itt = cmd->init_task_tag;
2864 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2865 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2866 conn->stat_sn;
2867 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2868
2869 if (nopout_response)
2870 iscsit_increment_maxcmdsn(cmd, conn->sess);
2871
2872 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2873 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2874
2875 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2876 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2877 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2878 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2879}
2880EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2881
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002882/*
2883 * Unsolicited NOPIN, either requesting a response or not.
2884 */
2885static int iscsit_send_unsolicited_nopin(
2886 struct iscsi_cmd *cmd,
2887 struct iscsi_conn *conn,
2888 int want_response)
2889{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002890 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2891 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002892
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002893 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002894
2895 if (conn->conn_ops->HeaderDigest) {
2896 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2897
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002898 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2899 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002900
2901 tx_size += ISCSI_CRC_LEN;
2902 pr_debug("Attaching CRC32C HeaderDigest to"
2903 " NopIN 0x%08x\n", *header_digest);
2904 }
2905
2906 cmd->iov_misc[0].iov_base = cmd->pdu;
2907 cmd->iov_misc[0].iov_len = tx_size;
2908 cmd->iov_misc_count = 1;
2909 cmd->tx_size = tx_size;
2910
2911 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2912 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2913
Andy Grover6f3c0e62012-04-03 15:51:09 -07002914 ret = iscsit_send_tx_data(cmd, conn, 1);
2915 if (ret < 0) {
2916 iscsit_tx_thread_wait_for_tcp(conn);
2917 return ret;
2918 }
2919
2920 spin_lock_bh(&cmd->istate_lock);
2921 cmd->i_state = want_response ?
2922 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2923 spin_unlock_bh(&cmd->istate_lock);
2924
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002925 return 0;
2926}
2927
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002928static int
2929iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002930{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002931 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002932 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002933 u32 padding = 0;
2934 int niov = 0, tx_size;
2935
2936 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002937
2938 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002939 iov = &cmd->iov_misc[0];
2940 iov[niov].iov_base = cmd->pdu;
2941 iov[niov++].iov_len = ISCSI_HDR_LEN;
2942
2943 if (conn->conn_ops->HeaderDigest) {
2944 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2945
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002946 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2947 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002948
2949 iov[0].iov_len += ISCSI_CRC_LEN;
2950 tx_size += ISCSI_CRC_LEN;
2951 pr_debug("Attaching CRC32C HeaderDigest"
2952 " to NopIn 0x%08x\n", *header_digest);
2953 }
2954
2955 /*
2956 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2957 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2958 */
2959 if (cmd->buf_ptr_size) {
2960 iov[niov].iov_base = cmd->buf_ptr;
2961 iov[niov++].iov_len = cmd->buf_ptr_size;
2962 tx_size += cmd->buf_ptr_size;
2963
2964 pr_debug("Echoing back %u bytes of ping"
2965 " data.\n", cmd->buf_ptr_size);
2966
2967 padding = ((-cmd->buf_ptr_size) & 3);
2968 if (padding != 0) {
2969 iov[niov].iov_base = &cmd->pad_bytes;
2970 iov[niov++].iov_len = padding;
2971 tx_size += padding;
2972 pr_debug("Attaching %u additional"
2973 " padding bytes.\n", padding);
2974 }
2975 if (conn->conn_ops->DataDigest) {
2976 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2977 cmd->buf_ptr, cmd->buf_ptr_size,
2978 padding, (u8 *)&cmd->pad_bytes,
2979 (u8 *)&cmd->data_crc);
2980
2981 iov[niov].iov_base = &cmd->data_crc;
2982 iov[niov++].iov_len = ISCSI_CRC_LEN;
2983 tx_size += ISCSI_CRC_LEN;
2984 pr_debug("Attached DataDigest for %u"
2985 " bytes of ping data, CRC 0x%08x\n",
2986 cmd->buf_ptr_size, cmd->data_crc);
2987 }
2988 }
2989
2990 cmd->iov_misc_count = niov;
2991 cmd->tx_size = tx_size;
2992
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002993 return 0;
2994}
2995
Andy Grover6f3c0e62012-04-03 15:51:09 -07002996static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002997 struct iscsi_cmd *cmd,
2998 struct iscsi_conn *conn)
2999{
3000 int tx_size = 0;
3001 struct iscsi_r2t *r2t;
3002 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003003 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003004
3005 r2t = iscsit_get_r2t_from_list(cmd);
3006 if (!r2t)
3007 return -1;
3008
3009 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3010 memset(hdr, 0, ISCSI_HDR_LEN);
3011 hdr->opcode = ISCSI_OP_R2T;
3012 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3013 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3014 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003015 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003016 spin_lock_bh(&conn->sess->ttt_lock);
3017 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3018 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3019 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3020 spin_unlock_bh(&conn->sess->ttt_lock);
3021 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3022 hdr->statsn = cpu_to_be32(conn->stat_sn);
3023 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3024 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3025 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3026 hdr->data_offset = cpu_to_be32(r2t->offset);
3027 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3028
3029 cmd->iov_misc[0].iov_base = cmd->pdu;
3030 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3031 tx_size += ISCSI_HDR_LEN;
3032
3033 if (conn->conn_ops->HeaderDigest) {
3034 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3035
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003036 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3037 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003038
3039 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3040 tx_size += ISCSI_CRC_LEN;
3041 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3042 " PDU 0x%08x\n", *header_digest);
3043 }
3044
3045 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3046 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3047 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3048 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3049 r2t->offset, r2t->xfer_len, conn->cid);
3050
3051 cmd->iov_misc_count = 1;
3052 cmd->tx_size = tx_size;
3053
3054 spin_lock_bh(&cmd->r2t_lock);
3055 r2t->sent_r2t = 1;
3056 spin_unlock_bh(&cmd->r2t_lock);
3057
Andy Grover6f3c0e62012-04-03 15:51:09 -07003058 ret = iscsit_send_tx_data(cmd, conn, 1);
3059 if (ret < 0) {
3060 iscsit_tx_thread_wait_for_tcp(conn);
3061 return ret;
3062 }
3063
3064 spin_lock_bh(&cmd->dataout_timeout_lock);
3065 iscsit_start_dataout_timer(cmd, conn);
3066 spin_unlock_bh(&cmd->dataout_timeout_lock);
3067
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003068 return 0;
3069}
3070
3071/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003072 * @recovery: If called from iscsi_task_reassign_complete_write() for
3073 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003074 */
3075int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003076 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003077 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003078 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003079{
3080 int first_r2t = 1;
3081 u32 offset = 0, xfer_len = 0;
3082
3083 spin_lock_bh(&cmd->r2t_lock);
3084 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3085 spin_unlock_bh(&cmd->r2t_lock);
3086 return 0;
3087 }
3088
Andy Grover8b1e1242012-04-03 15:51:12 -07003089 if (conn->sess->sess_ops->DataSequenceInOrder &&
3090 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003091 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003092
3093 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3094 if (conn->sess->sess_ops->DataSequenceInOrder) {
3095 offset = cmd->r2t_offset;
3096
Andy Grover8b1e1242012-04-03 15:51:12 -07003097 if (first_r2t && recovery) {
3098 int new_data_end = offset +
3099 conn->sess->sess_ops->MaxBurstLength -
3100 cmd->next_burst_len;
3101
Andy Groverebf1d952012-04-03 15:51:24 -07003102 if (new_data_end > cmd->se_cmd.data_length)
3103 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003104 else
3105 xfer_len =
3106 conn->sess->sess_ops->MaxBurstLength -
3107 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003108 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003109 int new_data_end = offset +
3110 conn->sess->sess_ops->MaxBurstLength;
3111
Andy Groverebf1d952012-04-03 15:51:24 -07003112 if (new_data_end > cmd->se_cmd.data_length)
3113 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003114 else
3115 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003116 }
3117 cmd->r2t_offset += xfer_len;
3118
Andy Groverebf1d952012-04-03 15:51:24 -07003119 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003120 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3121 } else {
3122 struct iscsi_seq *seq;
3123
3124 seq = iscsit_get_seq_holder_for_r2t(cmd);
3125 if (!seq) {
3126 spin_unlock_bh(&cmd->r2t_lock);
3127 return -1;
3128 }
3129
3130 offset = seq->offset;
3131 xfer_len = seq->xfer_len;
3132
3133 if (cmd->seq_send_order == cmd->seq_count)
3134 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3135 }
3136 cmd->outstanding_r2ts++;
3137 first_r2t = 0;
3138
3139 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3140 spin_unlock_bh(&cmd->r2t_lock);
3141 return -1;
3142 }
3143
3144 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3145 break;
3146 }
3147 spin_unlock_bh(&cmd->r2t_lock);
3148
3149 return 0;
3150}
3151
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003152void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3153 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003154{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003155 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003156 cmd->stat_sn = conn->stat_sn++;
3157
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003158 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003159
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003160 memset(hdr, 0, ISCSI_HDR_LEN);
3161 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3162 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3163 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3164 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003165 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003166 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3167 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003168 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169 }
3170 hdr->response = cmd->iscsi_response;
3171 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003172 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003173 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3174
3175 iscsit_increment_maxcmdsn(cmd, conn->sess);
3176 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3177 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3178
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003179 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3180 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3181 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3182 cmd->se_cmd.scsi_status, conn->cid);
3183}
3184EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3185
3186static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3187{
3188 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3189 struct kvec *iov;
3190 u32 padding = 0, tx_size = 0;
3191 int iov_count = 0;
3192 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3193
3194 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3195
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003196 iov = &cmd->iov_misc[0];
3197 iov[iov_count].iov_base = cmd->pdu;
3198 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3199 tx_size += ISCSI_HDR_LEN;
3200
3201 /*
3202 * Attach SENSE DATA payload to iSCSI Response PDU
3203 */
3204 if (cmd->se_cmd.sense_buffer &&
3205 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3206 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003207 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3208 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3209
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003210 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003211 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003212 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003213 iov[iov_count++].iov_len =
3214 (cmd->se_cmd.scsi_sense_length + padding);
3215 tx_size += cmd->se_cmd.scsi_sense_length;
3216
3217 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003218 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003219 cmd->se_cmd.scsi_sense_length, 0, padding);
3220 tx_size += padding;
3221 pr_debug("Adding %u bytes of padding to"
3222 " SENSE.\n", padding);
3223 }
3224
3225 if (conn->conn_ops->DataDigest) {
3226 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003227 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003228 (cmd->se_cmd.scsi_sense_length + padding),
3229 0, NULL, (u8 *)&cmd->data_crc);
3230
3231 iov[iov_count].iov_base = &cmd->data_crc;
3232 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3233 tx_size += ISCSI_CRC_LEN;
3234
3235 pr_debug("Attaching CRC32 DataDigest for"
3236 " SENSE, %u bytes CRC 0x%08x\n",
3237 (cmd->se_cmd.scsi_sense_length + padding),
3238 cmd->data_crc);
3239 }
3240
3241 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3242 " Response PDU\n",
3243 cmd->se_cmd.scsi_sense_length);
3244 }
3245
3246 if (conn->conn_ops->HeaderDigest) {
3247 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3248
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003249 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3250 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003251
3252 iov[0].iov_len += ISCSI_CRC_LEN;
3253 tx_size += ISCSI_CRC_LEN;
3254 pr_debug("Attaching CRC32 HeaderDigest for Response"
3255 " PDU 0x%08x\n", *header_digest);
3256 }
3257
3258 cmd->iov_misc_count = iov_count;
3259 cmd->tx_size = tx_size;
3260
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003261 return 0;
3262}
3263
3264static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3265{
3266 switch (se_tmr->response) {
3267 case TMR_FUNCTION_COMPLETE:
3268 return ISCSI_TMF_RSP_COMPLETE;
3269 case TMR_TASK_DOES_NOT_EXIST:
3270 return ISCSI_TMF_RSP_NO_TASK;
3271 case TMR_LUN_DOES_NOT_EXIST:
3272 return ISCSI_TMF_RSP_NO_LUN;
3273 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3274 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003275 case TMR_FUNCTION_REJECTED:
3276 default:
3277 return ISCSI_TMF_RSP_REJECTED;
3278 }
3279}
3280
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003281void
3282iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3283 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003284{
3285 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003286
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003287 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003288 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003289 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003290 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003291 cmd->stat_sn = conn->stat_sn++;
3292 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3293
3294 iscsit_increment_maxcmdsn(cmd, conn->sess);
3295 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3296 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3297
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003298 pr_debug("Built Task Management Response ITT: 0x%08x,"
3299 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3300 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3301}
3302EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3303
3304static int
3305iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3306{
3307 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3308 u32 tx_size = 0;
3309
3310 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3311
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003312 cmd->iov_misc[0].iov_base = cmd->pdu;
3313 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3314 tx_size += ISCSI_HDR_LEN;
3315
3316 if (conn->conn_ops->HeaderDigest) {
3317 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3318
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003319 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3320 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003321
3322 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3323 tx_size += ISCSI_CRC_LEN;
3324 pr_debug("Attaching CRC32 HeaderDigest for Task"
3325 " Mgmt Response PDU 0x%08x\n", *header_digest);
3326 }
3327
3328 cmd->iov_misc_count = 1;
3329 cmd->tx_size = tx_size;
3330
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003331 return 0;
3332}
3333
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003334static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3335{
3336 bool ret = false;
3337
3338 if (np->np_sockaddr.ss_family == AF_INET6) {
3339 const struct sockaddr_in6 sin6 = {
3340 .sin6_addr = IN6ADDR_ANY_INIT };
3341 struct sockaddr_in6 *sock_in6 =
3342 (struct sockaddr_in6 *)&np->np_sockaddr;
3343
3344 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3345 sin6.sin6_addr.s6_addr, 16))
3346 ret = true;
3347 } else {
3348 struct sockaddr_in * sock_in =
3349 (struct sockaddr_in *)&np->np_sockaddr;
3350
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003351 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003352 ret = true;
3353 }
3354
3355 return ret;
3356}
3357
Andy Grover8b1e1242012-04-03 15:51:12 -07003358#define SENDTARGETS_BUF_LIMIT 32768U
3359
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003360static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3361{
3362 char *payload = NULL;
3363 struct iscsi_conn *conn = cmd->conn;
3364 struct iscsi_portal_group *tpg;
3365 struct iscsi_tiqn *tiqn;
3366 struct iscsi_tpg_np *tpg_np;
3367 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003368 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003369 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003370 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003371
Andy Grover8b1e1242012-04-03 15:51:12 -07003372 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3373 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003374
3375 payload = kzalloc(buffer_len, GFP_KERNEL);
3376 if (!payload) {
3377 pr_err("Unable to allocate memory for sendtargets"
3378 " response.\n");
3379 return -ENOMEM;
3380 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003381 /*
3382 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3383 * explicit case..
3384 */
3385 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3386 text_ptr = strchr(text_in, '=');
3387 if (!text_ptr) {
3388 pr_err("Unable to locate '=' string in text_in:"
3389 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003390 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003391 return -EINVAL;
3392 }
3393 /*
3394 * Skip over '=' character..
3395 */
3396 text_ptr += 1;
3397 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003398
3399 spin_lock(&tiqn_lock);
3400 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003401 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3402 strcmp(tiqn->tiqn, text_ptr)) {
3403 continue;
3404 }
3405
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003406 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003407
3408 spin_lock(&tiqn->tiqn_tpg_lock);
3409 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3410
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003411 /* If demo_mode_discovery=0 and generate_node_acls=0
3412 * (demo mode dislabed) do not return
3413 * TargetName+TargetAddress unless a NodeACL exists.
3414 */
3415
3416 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3417 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3418 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3419 cmd->conn->sess->sess_ops->InitiatorName))) {
3420 continue;
3421 }
3422
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003423 spin_lock(&tpg->tpg_state_lock);
3424 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3425 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3426 spin_unlock(&tpg->tpg_state_lock);
3427 continue;
3428 }
3429 spin_unlock(&tpg->tpg_state_lock);
3430
3431 spin_lock(&tpg->tpg_np_lock);
3432 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3433 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003434 struct iscsi_np *np = tpg_np->tpg_np;
3435 bool inaddr_any = iscsit_check_inaddr_any(np);
3436
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003437 if (!target_name_printed) {
3438 len = sprintf(buf, "TargetName=%s",
3439 tiqn->tiqn);
3440 len += 1;
3441
3442 if ((len + payload_len) > buffer_len) {
3443 spin_unlock(&tpg->tpg_np_lock);
3444 spin_unlock(&tiqn->tiqn_tpg_lock);
3445 end_of_buf = 1;
3446 goto eob;
3447 }
3448 memcpy(payload + payload_len, buf, len);
3449 payload_len += len;
3450 target_name_printed = 1;
3451 }
3452
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003453 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003454 "%s:%hu,%hu",
3455 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003456 np->np_ip : conn->local_ip,
Chris Leechdfecf612013-08-12 11:26:28 -07003457 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003458 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003459 tpg->tpgt);
3460 len += 1;
3461
3462 if ((len + payload_len) > buffer_len) {
3463 spin_unlock(&tpg->tpg_np_lock);
3464 spin_unlock(&tiqn->tiqn_tpg_lock);
3465 end_of_buf = 1;
3466 goto eob;
3467 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003468 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003469 payload_len += len;
3470 }
3471 spin_unlock(&tpg->tpg_np_lock);
3472 }
3473 spin_unlock(&tiqn->tiqn_tpg_lock);
3474eob:
3475 if (end_of_buf)
3476 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003477
3478 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3479 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003480 }
3481 spin_unlock(&tiqn_lock);
3482
3483 cmd->buf_ptr = payload;
3484
3485 return payload_len;
3486}
3487
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003488int
3489iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3490 struct iscsi_text_rsp *hdr)
3491{
3492 int text_length, padding;
3493
3494 text_length = iscsit_build_sendtargets_response(cmd);
3495 if (text_length < 0)
3496 return text_length;
3497
3498 hdr->opcode = ISCSI_OP_TEXT_RSP;
3499 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3500 padding = ((-text_length) & 3);
3501 hton24(hdr->dlength, text_length);
3502 hdr->itt = cmd->init_task_tag;
3503 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3504 cmd->stat_sn = conn->stat_sn++;
3505 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3506
3507 iscsit_increment_maxcmdsn(cmd, conn->sess);
3508 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3509 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3510
3511 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3512 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3513 text_length, conn->cid);
3514
3515 return text_length + padding;
3516}
3517EXPORT_SYMBOL(iscsit_build_text_rsp);
3518
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003519/*
3520 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3521 * MaxRecvDataSegmentLength.
3522 */
3523static int iscsit_send_text_rsp(
3524 struct iscsi_cmd *cmd,
3525 struct iscsi_conn *conn)
3526{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003527 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003528 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003529 u32 tx_size = 0;
3530 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003531
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003532 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3533 if (rc < 0)
3534 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003535
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003536 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003537 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003538 iov[iov_count].iov_base = cmd->pdu;
3539 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3540 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003541 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003542
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003543 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003544
3545 if (conn->conn_ops->HeaderDigest) {
3546 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3547
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003548 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3549 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003550
3551 iov[0].iov_len += ISCSI_CRC_LEN;
3552 tx_size += ISCSI_CRC_LEN;
3553 pr_debug("Attaching CRC32 HeaderDigest for"
3554 " Text Response PDU 0x%08x\n", *header_digest);
3555 }
3556
3557 if (conn->conn_ops->DataDigest) {
3558 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003559 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003560 0, NULL, (u8 *)&cmd->data_crc);
3561
3562 iov[iov_count].iov_base = &cmd->data_crc;
3563 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3564 tx_size += ISCSI_CRC_LEN;
3565
3566 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003567 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003568 cmd->data_crc);
3569 }
3570
3571 cmd->iov_misc_count = iov_count;
3572 cmd->tx_size = tx_size;
3573
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003574 return 0;
3575}
3576
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003577void
3578iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3579 struct iscsi_reject *hdr)
3580{
3581 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003582 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003583 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3584 hton24(hdr->dlength, ISCSI_HDR_LEN);
3585 hdr->ffffffff = cpu_to_be32(0xffffffff);
3586 cmd->stat_sn = conn->stat_sn++;
3587 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3588 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3589 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3590
3591}
3592EXPORT_SYMBOL(iscsit_build_reject);
3593
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003594static int iscsit_send_reject(
3595 struct iscsi_cmd *cmd,
3596 struct iscsi_conn *conn)
3597{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003598 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003599 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003600 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003601
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003602 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603
3604 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003605 iov[iov_count].iov_base = cmd->pdu;
3606 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3607 iov[iov_count].iov_base = cmd->buf_ptr;
3608 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3609
3610 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3611
3612 if (conn->conn_ops->HeaderDigest) {
3613 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3614
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003615 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3616 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003617
3618 iov[0].iov_len += ISCSI_CRC_LEN;
3619 tx_size += ISCSI_CRC_LEN;
3620 pr_debug("Attaching CRC32 HeaderDigest for"
3621 " REJECT PDU 0x%08x\n", *header_digest);
3622 }
3623
3624 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003625 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3626 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003627
3628 iov[iov_count].iov_base = &cmd->data_crc;
3629 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3630 tx_size += ISCSI_CRC_LEN;
3631 pr_debug("Attaching CRC32 DataDigest for REJECT"
3632 " PDU 0x%08x\n", cmd->data_crc);
3633 }
3634
3635 cmd->iov_misc_count = iov_count;
3636 cmd->tx_size = tx_size;
3637
3638 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3639 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3640
3641 return 0;
3642}
3643
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003644void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3645{
3646 struct iscsi_thread_set *ts = conn->thread_set;
3647 int ord, cpu;
3648 /*
3649 * thread_id is assigned from iscsit_global->ts_bitmap from
3650 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3651 *
3652 * Here we use thread_id to determine which CPU that this
3653 * iSCSI connection's iscsi_thread_set will be scheduled to
3654 * execute upon.
3655 */
3656 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003657 for_each_online_cpu(cpu) {
3658 if (ord-- == 0) {
3659 cpumask_set_cpu(cpu, conn->conn_cpumask);
3660 return;
3661 }
3662 }
3663 /*
3664 * This should never be reached..
3665 */
3666 dump_stack();
3667 cpumask_setall(conn->conn_cpumask);
3668}
3669
3670static inline void iscsit_thread_check_cpumask(
3671 struct iscsi_conn *conn,
3672 struct task_struct *p,
3673 int mode)
3674{
3675 char buf[128];
3676 /*
3677 * mode == 1 signals iscsi_target_tx_thread() usage.
3678 * mode == 0 signals iscsi_target_rx_thread() usage.
3679 */
3680 if (mode == 1) {
3681 if (!conn->conn_tx_reset_cpumask)
3682 return;
3683 conn->conn_tx_reset_cpumask = 0;
3684 } else {
3685 if (!conn->conn_rx_reset_cpumask)
3686 return;
3687 conn->conn_rx_reset_cpumask = 0;
3688 }
3689 /*
3690 * Update the CPU mask for this single kthread so that
3691 * both TX and RX kthreads are scheduled to run on the
3692 * same CPU.
3693 */
3694 memset(buf, 0, 128);
3695 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003696 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3697}
3698
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003699static int
3700iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003701{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003702 int ret;
3703
3704 switch (state) {
3705 case ISTATE_SEND_R2T:
3706 ret = iscsit_send_r2t(cmd, conn);
3707 if (ret < 0)
3708 goto err;
3709 break;
3710 case ISTATE_REMOVE:
3711 spin_lock_bh(&conn->cmd_lock);
3712 list_del(&cmd->i_conn_node);
3713 spin_unlock_bh(&conn->cmd_lock);
3714
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003715 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003716 break;
3717 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3718 iscsit_mod_nopin_response_timer(conn);
3719 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3720 if (ret < 0)
3721 goto err;
3722 break;
3723 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3724 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3725 if (ret < 0)
3726 goto err;
3727 break;
3728 default:
3729 pr_err("Unknown Opcode: 0x%02x ITT:"
3730 " 0x%08x, i_state: %d on CID: %hu\n",
3731 cmd->iscsi_opcode, cmd->init_task_tag, state,
3732 conn->cid);
3733 goto err;
3734 }
3735
3736 return 0;
3737
3738err:
3739 return -1;
3740}
3741
3742static int
3743iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3744{
3745 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003746 struct iscsi_queue_req *qr;
3747 struct iscsi_cmd *cmd;
3748 u8 state;
3749 int ret;
3750
3751 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3752 atomic_set(&conn->check_immediate_queue, 0);
3753 cmd = qr->cmd;
3754 state = qr->state;
3755 kmem_cache_free(lio_qr_cache, qr);
3756
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003757 ret = t->iscsit_immediate_queue(conn, cmd, state);
3758 if (ret < 0)
3759 return ret;
3760 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003761
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003762 return 0;
3763}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003764
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003765static int
3766iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3767{
3768 int ret;
3769
3770check_rsp_state:
3771 switch (state) {
3772 case ISTATE_SEND_DATAIN:
3773 ret = iscsit_send_datain(cmd, conn);
3774 if (ret < 0)
3775 goto err;
3776 else if (!ret)
3777 /* more drs */
3778 goto check_rsp_state;
3779 else if (ret == 1) {
3780 /* all done */
3781 spin_lock_bh(&cmd->istate_lock);
3782 cmd->i_state = ISTATE_SENT_STATUS;
3783 spin_unlock_bh(&cmd->istate_lock);
3784
3785 if (atomic_read(&conn->check_immediate_queue))
3786 return 1;
3787
3788 return 0;
3789 } else if (ret == 2) {
3790 /* Still must send status,
3791 SCF_TRANSPORT_TASK_SENSE was set */
3792 spin_lock_bh(&cmd->istate_lock);
3793 cmd->i_state = ISTATE_SEND_STATUS;
3794 spin_unlock_bh(&cmd->istate_lock);
3795 state = ISTATE_SEND_STATUS;
3796 goto check_rsp_state;
3797 }
3798
3799 break;
3800 case ISTATE_SEND_STATUS:
3801 case ISTATE_SEND_STATUS_RECOVERY:
3802 ret = iscsit_send_response(cmd, conn);
3803 break;
3804 case ISTATE_SEND_LOGOUTRSP:
3805 ret = iscsit_send_logout(cmd, conn);
3806 break;
3807 case ISTATE_SEND_ASYNCMSG:
3808 ret = iscsit_send_conn_drop_async_message(
3809 cmd, conn);
3810 break;
3811 case ISTATE_SEND_NOPIN:
3812 ret = iscsit_send_nopin(cmd, conn);
3813 break;
3814 case ISTATE_SEND_REJECT:
3815 ret = iscsit_send_reject(cmd, conn);
3816 break;
3817 case ISTATE_SEND_TASKMGTRSP:
3818 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3819 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003820 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003821 ret = iscsit_tmr_post_handler(cmd, conn);
3822 if (ret != 0)
3823 iscsit_fall_back_to_erl0(conn->sess);
3824 break;
3825 case ISTATE_SEND_TEXTRSP:
3826 ret = iscsit_send_text_rsp(cmd, conn);
3827 break;
3828 default:
3829 pr_err("Unknown Opcode: 0x%02x ITT:"
3830 " 0x%08x, i_state: %d on CID: %hu\n",
3831 cmd->iscsi_opcode, cmd->init_task_tag,
3832 state, conn->cid);
3833 goto err;
3834 }
3835 if (ret < 0)
3836 goto err;
3837
3838 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3839 iscsit_tx_thread_wait_for_tcp(conn);
3840 iscsit_unmap_iovec(cmd);
3841 goto err;
3842 }
3843 iscsit_unmap_iovec(cmd);
3844
3845 switch (state) {
3846 case ISTATE_SEND_LOGOUTRSP:
3847 if (!iscsit_logout_post_handler(cmd, conn))
3848 goto restart;
3849 /* fall through */
3850 case ISTATE_SEND_STATUS:
3851 case ISTATE_SEND_ASYNCMSG:
3852 case ISTATE_SEND_NOPIN:
3853 case ISTATE_SEND_STATUS_RECOVERY:
3854 case ISTATE_SEND_TEXTRSP:
3855 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003856 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003857 spin_lock_bh(&cmd->istate_lock);
3858 cmd->i_state = ISTATE_SENT_STATUS;
3859 spin_unlock_bh(&cmd->istate_lock);
3860 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003861 default:
3862 pr_err("Unknown Opcode: 0x%02x ITT:"
3863 " 0x%08x, i_state: %d on CID: %hu\n",
3864 cmd->iscsi_opcode, cmd->init_task_tag,
3865 cmd->i_state, conn->cid);
3866 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003867 }
3868
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003869 if (atomic_read(&conn->check_immediate_queue))
3870 return 1;
3871
Andy Grover6f3c0e62012-04-03 15:51:09 -07003872 return 0;
3873
3874err:
3875 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003876restart:
3877 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003878}
3879
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003880static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003881{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003882 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003883 struct iscsi_queue_req *qr;
3884 struct iscsi_cmd *cmd;
3885 u8 state;
3886 int ret;
3887
3888 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3889 cmd = qr->cmd;
3890 state = qr->state;
3891 kmem_cache_free(lio_qr_cache, qr);
3892
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003893 ret = t->iscsit_response_queue(conn, cmd, state);
3894 if (ret == 1 || ret < 0)
3895 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003896 }
3897
3898 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003899}
3900
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003901int iscsi_target_tx_thread(void *arg)
3902{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003903 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003904 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003905 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003906 /*
3907 * Allow ourselves to be interrupted by SIGINT so that a
3908 * connection recovery / failure event can be triggered externally.
3909 */
3910 allow_signal(SIGINT);
3911
3912restart:
3913 conn = iscsi_tx_thread_pre_handler(ts);
3914 if (!conn)
3915 goto out;
3916
Andy Grover6f3c0e62012-04-03 15:51:09 -07003917 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003918
3919 while (!kthread_should_stop()) {
3920 /*
3921 * Ensure that both TX and RX per connection kthreads
3922 * are scheduled to run on the same CPU.
3923 */
3924 iscsit_thread_check_cpumask(conn, current, 1);
3925
Roland Dreierd5627ac2012-10-31 09:16:46 -07003926 wait_event_interruptible(conn->queues_wq,
3927 !iscsit_conn_all_queues_empty(conn) ||
3928 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003929
3930 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3931 signal_pending(current))
3932 goto transport_err;
3933
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003934get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003935 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003936 if (ret < 0)
3937 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003938
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003939 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003940 if (ret == 1)
3941 goto get_immediate;
3942 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003943 goto restart;
3944 else if (ret < 0)
3945 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003946 }
3947
3948transport_err:
3949 iscsit_take_action_for_connection_exit(conn);
3950 goto restart;
3951out:
3952 return 0;
3953}
3954
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003955static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3956{
3957 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3958 struct iscsi_cmd *cmd;
3959 int ret = 0;
3960
3961 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3962 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003963 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003964 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003965 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003966
3967 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3968 break;
3969 case ISCSI_OP_SCSI_DATA_OUT:
3970 ret = iscsit_handle_data_out(conn, buf);
3971 break;
3972 case ISCSI_OP_NOOP_OUT:
3973 cmd = NULL;
3974 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003975 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003976 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003977 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003978 }
3979 ret = iscsit_handle_nop_out(conn, cmd, buf);
3980 break;
3981 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003982 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003983 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003984 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003985
3986 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3987 break;
3988 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003989 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003990 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003991 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003992
3993 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003994 break;
3995 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003996 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003997 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003998 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003999
4000 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4001 if (ret > 0)
4002 wait_for_completion_timeout(&conn->conn_logout_comp,
4003 SECONDS_FOR_LOGOUT_COMP * HZ);
4004 break;
4005 case ISCSI_OP_SNACK:
4006 ret = iscsit_handle_snack(conn, buf);
4007 break;
4008 default:
4009 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4010 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4011 pr_err("Cannot recover from unknown"
4012 " opcode while ERL=0, closing iSCSI connection.\n");
4013 return -1;
4014 }
4015 if (!conn->conn_ops->OFMarker) {
4016 pr_err("Unable to recover from unknown"
4017 " opcode while OFMarker=No, closing iSCSI"
4018 " connection.\n");
4019 return -1;
4020 }
4021 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4022 pr_err("Unable to recover from unknown"
4023 " opcode, closing iSCSI connection.\n");
4024 return -1;
4025 }
4026 break;
4027 }
4028
4029 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004030reject:
4031 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004032}
4033
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004034int iscsi_target_rx_thread(void *arg)
4035{
4036 int ret;
4037 u8 buffer[ISCSI_HDR_LEN], opcode;
4038 u32 checksum = 0, digest = 0;
4039 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004040 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004041 struct kvec iov;
4042 /*
4043 * Allow ourselves to be interrupted by SIGINT so that a
4044 * connection recovery / failure event can be triggered externally.
4045 */
4046 allow_signal(SIGINT);
4047
4048restart:
4049 conn = iscsi_rx_thread_pre_handler(ts);
4050 if (!conn)
4051 goto out;
4052
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004053 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4054 struct completion comp;
4055 int rc;
4056
4057 init_completion(&comp);
4058 rc = wait_for_completion_interruptible(&comp);
4059 if (rc < 0)
4060 goto transport_err;
4061
4062 goto out;
4063 }
4064
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004065 while (!kthread_should_stop()) {
4066 /*
4067 * Ensure that both TX and RX per connection kthreads
4068 * are scheduled to run on the same CPU.
4069 */
4070 iscsit_thread_check_cpumask(conn, current, 0);
4071
4072 memset(buffer, 0, ISCSI_HDR_LEN);
4073 memset(&iov, 0, sizeof(struct kvec));
4074
4075 iov.iov_base = buffer;
4076 iov.iov_len = ISCSI_HDR_LEN;
4077
4078 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4079 if (ret != ISCSI_HDR_LEN) {
4080 iscsit_rx_thread_wait_for_tcp(conn);
4081 goto transport_err;
4082 }
4083
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004084 if (conn->conn_ops->HeaderDigest) {
4085 iov.iov_base = &digest;
4086 iov.iov_len = ISCSI_CRC_LEN;
4087
4088 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4089 if (ret != ISCSI_CRC_LEN) {
4090 iscsit_rx_thread_wait_for_tcp(conn);
4091 goto transport_err;
4092 }
4093
4094 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4095 buffer, ISCSI_HDR_LEN,
4096 0, NULL, (u8 *)&checksum);
4097
4098 if (digest != checksum) {
4099 pr_err("HeaderDigest CRC32C failed,"
4100 " received 0x%08x, computed 0x%08x\n",
4101 digest, checksum);
4102 /*
4103 * Set the PDU to 0xff so it will intentionally
4104 * hit default in the switch below.
4105 */
4106 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004107 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004108 } else {
4109 pr_debug("Got HeaderDigest CRC32C"
4110 " 0x%08x\n", checksum);
4111 }
4112 }
4113
4114 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4115 goto transport_err;
4116
4117 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4118
4119 if (conn->sess->sess_ops->SessionType &&
4120 ((!(opcode & ISCSI_OP_TEXT)) ||
4121 (!(opcode & ISCSI_OP_LOGOUT)))) {
4122 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4123 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004124 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4125 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004126 goto transport_err;
4127 }
4128
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004129 ret = iscsi_target_rx_opcode(conn, buffer);
4130 if (ret < 0)
4131 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004132 }
4133
4134transport_err:
4135 if (!signal_pending(current))
4136 atomic_set(&conn->transport_failed, 1);
4137 iscsit_take_action_for_connection_exit(conn);
4138 goto restart;
4139out:
4140 return 0;
4141}
4142
4143static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4144{
4145 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4146 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004147 /*
4148 * We expect this function to only ever be called from either RX or TX
4149 * thread context via iscsit_close_connection() once the other context
4150 * has been reset -> returned sleeping pre-handler state.
4151 */
4152 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004153 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004154
Andy Grover2fbb4712012-04-03 15:51:01 -07004155 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004156 spin_unlock_bh(&conn->cmd_lock);
4157
4158 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004159
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004160 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004161
4162 spin_lock_bh(&conn->cmd_lock);
4163 }
4164 spin_unlock_bh(&conn->cmd_lock);
4165}
4166
4167static void iscsit_stop_timers_for_cmds(
4168 struct iscsi_conn *conn)
4169{
4170 struct iscsi_cmd *cmd;
4171
4172 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004173 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004174 if (cmd->data_direction == DMA_TO_DEVICE)
4175 iscsit_stop_dataout_timer(cmd);
4176 }
4177 spin_unlock_bh(&conn->cmd_lock);
4178}
4179
4180int iscsit_close_connection(
4181 struct iscsi_conn *conn)
4182{
4183 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4184 struct iscsi_session *sess = conn->sess;
4185
4186 pr_debug("Closing iSCSI connection CID %hu on SID:"
4187 " %u\n", conn->cid, sess->sid);
4188 /*
4189 * Always up conn_logout_comp just in case the RX Thread is sleeping
4190 * and the logout response never got sent because the connection
4191 * failed.
4192 */
4193 complete(&conn->conn_logout_comp);
4194
4195 iscsi_release_thread_set(conn);
4196
4197 iscsit_stop_timers_for_cmds(conn);
4198 iscsit_stop_nopin_response_timer(conn);
4199 iscsit_stop_nopin_timer(conn);
4200 iscsit_free_queue_reqs_for_conn(conn);
4201
4202 /*
4203 * During Connection recovery drop unacknowledged out of order
4204 * commands for this connection, and prepare the other commands
4205 * for realligence.
4206 *
4207 * During normal operation clear the out of order commands (but
4208 * do not free the struct iscsi_ooo_cmdsn's) and release all
4209 * struct iscsi_cmds.
4210 */
4211 if (atomic_read(&conn->connection_recovery)) {
4212 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4213 iscsit_prepare_cmds_for_realligance(conn);
4214 } else {
4215 iscsit_clear_ooo_cmdsns_for_conn(conn);
4216 iscsit_release_commands_from_conn(conn);
4217 }
4218
4219 /*
4220 * Handle decrementing session or connection usage count if
4221 * a logout response was not able to be sent because the
4222 * connection failed. Fall back to Session Recovery here.
4223 */
4224 if (atomic_read(&conn->conn_logout_remove)) {
4225 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4226 iscsit_dec_conn_usage_count(conn);
4227 iscsit_dec_session_usage_count(sess);
4228 }
4229 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4230 iscsit_dec_conn_usage_count(conn);
4231
4232 atomic_set(&conn->conn_logout_remove, 0);
4233 atomic_set(&sess->session_reinstatement, 0);
4234 atomic_set(&sess->session_fall_back_to_erl0, 1);
4235 }
4236
4237 spin_lock_bh(&sess->conn_lock);
4238 list_del(&conn->conn_list);
4239
4240 /*
4241 * Attempt to let the Initiator know this connection failed by
4242 * sending an Connection Dropped Async Message on another
4243 * active connection.
4244 */
4245 if (atomic_read(&conn->connection_recovery))
4246 iscsit_build_conn_drop_async_message(conn);
4247
4248 spin_unlock_bh(&sess->conn_lock);
4249
4250 /*
4251 * If connection reinstatement is being performed on this connection,
4252 * up the connection reinstatement semaphore that is being blocked on
4253 * in iscsit_cause_connection_reinstatement().
4254 */
4255 spin_lock_bh(&conn->state_lock);
4256 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4257 spin_unlock_bh(&conn->state_lock);
4258 complete(&conn->conn_wait_comp);
4259 wait_for_completion(&conn->conn_post_wait_comp);
4260 spin_lock_bh(&conn->state_lock);
4261 }
4262
4263 /*
4264 * If connection reinstatement is being performed on this connection
4265 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4266 * connection wait rcfr semaphore that is being blocked on
4267 * an iscsit_connection_reinstatement_rcfr().
4268 */
4269 if (atomic_read(&conn->connection_wait_rcfr)) {
4270 spin_unlock_bh(&conn->state_lock);
4271 complete(&conn->conn_wait_rcfr_comp);
4272 wait_for_completion(&conn->conn_post_wait_comp);
4273 spin_lock_bh(&conn->state_lock);
4274 }
4275 atomic_set(&conn->connection_reinstatement, 1);
4276 spin_unlock_bh(&conn->state_lock);
4277
4278 /*
4279 * If any other processes are accessing this connection pointer we
4280 * must wait until they have completed.
4281 */
4282 iscsit_check_conn_usage_count(conn);
4283
4284 if (conn->conn_rx_hash.tfm)
4285 crypto_free_hash(conn->conn_rx_hash.tfm);
4286 if (conn->conn_tx_hash.tfm)
4287 crypto_free_hash(conn->conn_tx_hash.tfm);
4288
4289 if (conn->conn_cpumask)
4290 free_cpumask_var(conn->conn_cpumask);
4291
4292 kfree(conn->conn_ops);
4293 conn->conn_ops = NULL;
4294
Al Virobf6932f2012-07-21 08:55:18 +01004295 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004296 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004297
4298 if (conn->conn_transport->iscsit_free_conn)
4299 conn->conn_transport->iscsit_free_conn(conn);
4300
4301 iscsit_put_transport(conn->conn_transport);
4302
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004303 conn->thread_set = NULL;
4304
4305 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4306 conn->conn_state = TARG_CONN_STATE_FREE;
4307 kfree(conn);
4308
4309 spin_lock_bh(&sess->conn_lock);
4310 atomic_dec(&sess->nconn);
4311 pr_debug("Decremented iSCSI connection count to %hu from node:"
4312 " %s\n", atomic_read(&sess->nconn),
4313 sess->sess_ops->InitiatorName);
4314 /*
4315 * Make sure that if one connection fails in an non ERL=2 iSCSI
4316 * Session that they all fail.
4317 */
4318 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4319 !atomic_read(&sess->session_logout))
4320 atomic_set(&sess->session_fall_back_to_erl0, 1);
4321
4322 /*
4323 * If this was not the last connection in the session, and we are
4324 * performing session reinstatement or falling back to ERL=0, call
4325 * iscsit_stop_session() without sleeping to shutdown the other
4326 * active connections.
4327 */
4328 if (atomic_read(&sess->nconn)) {
4329 if (!atomic_read(&sess->session_reinstatement) &&
4330 !atomic_read(&sess->session_fall_back_to_erl0)) {
4331 spin_unlock_bh(&sess->conn_lock);
4332 return 0;
4333 }
4334 if (!atomic_read(&sess->session_stop_active)) {
4335 atomic_set(&sess->session_stop_active, 1);
4336 spin_unlock_bh(&sess->conn_lock);
4337 iscsit_stop_session(sess, 0, 0);
4338 return 0;
4339 }
4340 spin_unlock_bh(&sess->conn_lock);
4341 return 0;
4342 }
4343
4344 /*
4345 * If this was the last connection in the session and one of the
4346 * following is occurring:
4347 *
4348 * Session Reinstatement is not being performed, and are falling back
4349 * to ERL=0 call iscsit_close_session().
4350 *
4351 * Session Logout was requested. iscsit_close_session() will be called
4352 * elsewhere.
4353 *
4354 * Session Continuation is not being performed, start the Time2Retain
4355 * handler and check if sleep_on_sess_wait_sem is active.
4356 */
4357 if (!atomic_read(&sess->session_reinstatement) &&
4358 atomic_read(&sess->session_fall_back_to_erl0)) {
4359 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004360 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004361
4362 return 0;
4363 } else if (atomic_read(&sess->session_logout)) {
4364 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4365 sess->session_state = TARG_SESS_STATE_FREE;
4366 spin_unlock_bh(&sess->conn_lock);
4367
4368 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4369 complete(&sess->session_wait_comp);
4370
4371 return 0;
4372 } else {
4373 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4374 sess->session_state = TARG_SESS_STATE_FAILED;
4375
4376 if (!atomic_read(&sess->session_continuation)) {
4377 spin_unlock_bh(&sess->conn_lock);
4378 iscsit_start_time2retain_handler(sess);
4379 } else
4380 spin_unlock_bh(&sess->conn_lock);
4381
4382 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4383 complete(&sess->session_wait_comp);
4384
4385 return 0;
4386 }
4387 spin_unlock_bh(&sess->conn_lock);
4388
4389 return 0;
4390}
4391
4392int iscsit_close_session(struct iscsi_session *sess)
4393{
Andy Grover60bfcf82013-10-09 11:05:58 -07004394 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004395 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4396
4397 if (atomic_read(&sess->nconn)) {
4398 pr_err("%d connection(s) still exist for iSCSI session"
4399 " to %s\n", atomic_read(&sess->nconn),
4400 sess->sess_ops->InitiatorName);
4401 BUG();
4402 }
4403
4404 spin_lock_bh(&se_tpg->session_lock);
4405 atomic_set(&sess->session_logout, 1);
4406 atomic_set(&sess->session_reinstatement, 1);
4407 iscsit_stop_time2retain_timer(sess);
4408 spin_unlock_bh(&se_tpg->session_lock);
4409
4410 /*
4411 * transport_deregister_session_configfs() will clear the
4412 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4413 * can be setting it again with __transport_register_session() in
4414 * iscsi_post_login_handler() again after the iscsit_stop_session()
4415 * completes in iscsi_np context.
4416 */
4417 transport_deregister_session_configfs(sess->se_sess);
4418
4419 /*
4420 * If any other processes are accessing this session pointer we must
4421 * wait until they have completed. If we are in an interrupt (the
4422 * time2retain handler) and contain and active session usage count we
4423 * restart the timer and exit.
4424 */
4425 if (!in_interrupt()) {
4426 if (iscsit_check_session_usage_count(sess) == 1)
4427 iscsit_stop_session(sess, 1, 1);
4428 } else {
4429 if (iscsit_check_session_usage_count(sess) == 2) {
4430 atomic_set(&sess->session_logout, 0);
4431 iscsit_start_time2retain_handler(sess);
4432 return 0;
4433 }
4434 }
4435
4436 transport_deregister_session(sess->se_sess);
4437
4438 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4439 iscsit_free_connection_recovery_entires(sess);
4440
4441 iscsit_free_all_ooo_cmdsns(sess);
4442
4443 spin_lock_bh(&se_tpg->session_lock);
4444 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4445 sess->session_state = TARG_SESS_STATE_FREE;
4446 pr_debug("Released iSCSI session from node: %s\n",
4447 sess->sess_ops->InitiatorName);
4448 tpg->nsessions--;
4449 if (tpg->tpg_tiqn)
4450 tpg->tpg_tiqn->tiqn_nsessions--;
4451
4452 pr_debug("Decremented number of active iSCSI Sessions on"
4453 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4454
4455 spin_lock(&sess_idr_lock);
4456 idr_remove(&sess_idr, sess->session_index);
4457 spin_unlock(&sess_idr_lock);
4458
4459 kfree(sess->sess_ops);
4460 sess->sess_ops = NULL;
4461 spin_unlock_bh(&se_tpg->session_lock);
4462
4463 kfree(sess);
4464 return 0;
4465}
4466
4467static void iscsit_logout_post_handler_closesession(
4468 struct iscsi_conn *conn)
4469{
4470 struct iscsi_session *sess = conn->sess;
4471
4472 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4473 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4474
4475 atomic_set(&conn->conn_logout_remove, 0);
4476 complete(&conn->conn_logout_comp);
4477
4478 iscsit_dec_conn_usage_count(conn);
4479 iscsit_stop_session(sess, 1, 1);
4480 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004481 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004482}
4483
4484static void iscsit_logout_post_handler_samecid(
4485 struct iscsi_conn *conn)
4486{
4487 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4488 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4489
4490 atomic_set(&conn->conn_logout_remove, 0);
4491 complete(&conn->conn_logout_comp);
4492
4493 iscsit_cause_connection_reinstatement(conn, 1);
4494 iscsit_dec_conn_usage_count(conn);
4495}
4496
4497static void iscsit_logout_post_handler_diffcid(
4498 struct iscsi_conn *conn,
4499 u16 cid)
4500{
4501 struct iscsi_conn *l_conn;
4502 struct iscsi_session *sess = conn->sess;
4503
4504 if (!sess)
4505 return;
4506
4507 spin_lock_bh(&sess->conn_lock);
4508 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4509 if (l_conn->cid == cid) {
4510 iscsit_inc_conn_usage_count(l_conn);
4511 break;
4512 }
4513 }
4514 spin_unlock_bh(&sess->conn_lock);
4515
4516 if (!l_conn)
4517 return;
4518
4519 if (l_conn->sock)
4520 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4521
4522 spin_lock_bh(&l_conn->state_lock);
4523 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4524 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4525 spin_unlock_bh(&l_conn->state_lock);
4526
4527 iscsit_cause_connection_reinstatement(l_conn, 1);
4528 iscsit_dec_conn_usage_count(l_conn);
4529}
4530
4531/*
4532 * Return of 0 causes the TX thread to restart.
4533 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004534int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004535 struct iscsi_cmd *cmd,
4536 struct iscsi_conn *conn)
4537{
4538 int ret = 0;
4539
4540 switch (cmd->logout_reason) {
4541 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4542 switch (cmd->logout_response) {
4543 case ISCSI_LOGOUT_SUCCESS:
4544 case ISCSI_LOGOUT_CLEANUP_FAILED:
4545 default:
4546 iscsit_logout_post_handler_closesession(conn);
4547 break;
4548 }
4549 ret = 0;
4550 break;
4551 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4552 if (conn->cid == cmd->logout_cid) {
4553 switch (cmd->logout_response) {
4554 case ISCSI_LOGOUT_SUCCESS:
4555 case ISCSI_LOGOUT_CLEANUP_FAILED:
4556 default:
4557 iscsit_logout_post_handler_samecid(conn);
4558 break;
4559 }
4560 ret = 0;
4561 } else {
4562 switch (cmd->logout_response) {
4563 case ISCSI_LOGOUT_SUCCESS:
4564 iscsit_logout_post_handler_diffcid(conn,
4565 cmd->logout_cid);
4566 break;
4567 case ISCSI_LOGOUT_CID_NOT_FOUND:
4568 case ISCSI_LOGOUT_CLEANUP_FAILED:
4569 default:
4570 break;
4571 }
4572 ret = 1;
4573 }
4574 break;
4575 case ISCSI_LOGOUT_REASON_RECOVERY:
4576 switch (cmd->logout_response) {
4577 case ISCSI_LOGOUT_SUCCESS:
4578 case ISCSI_LOGOUT_CID_NOT_FOUND:
4579 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4580 case ISCSI_LOGOUT_CLEANUP_FAILED:
4581 default:
4582 break;
4583 }
4584 ret = 1;
4585 break;
4586 default:
4587 break;
4588
4589 }
4590 return ret;
4591}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004592EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004593
4594void iscsit_fail_session(struct iscsi_session *sess)
4595{
4596 struct iscsi_conn *conn;
4597
4598 spin_lock_bh(&sess->conn_lock);
4599 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4600 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4601 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4602 }
4603 spin_unlock_bh(&sess->conn_lock);
4604
4605 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4606 sess->session_state = TARG_SESS_STATE_FAILED;
4607}
4608
4609int iscsit_free_session(struct iscsi_session *sess)
4610{
4611 u16 conn_count = atomic_read(&sess->nconn);
4612 struct iscsi_conn *conn, *conn_tmp = NULL;
4613 int is_last;
4614
4615 spin_lock_bh(&sess->conn_lock);
4616 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4617
4618 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4619 conn_list) {
4620 if (conn_count == 0)
4621 break;
4622
4623 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4624 is_last = 1;
4625 } else {
4626 iscsit_inc_conn_usage_count(conn_tmp);
4627 is_last = 0;
4628 }
4629 iscsit_inc_conn_usage_count(conn);
4630
4631 spin_unlock_bh(&sess->conn_lock);
4632 iscsit_cause_connection_reinstatement(conn, 1);
4633 spin_lock_bh(&sess->conn_lock);
4634
4635 iscsit_dec_conn_usage_count(conn);
4636 if (is_last == 0)
4637 iscsit_dec_conn_usage_count(conn_tmp);
4638
4639 conn_count--;
4640 }
4641
4642 if (atomic_read(&sess->nconn)) {
4643 spin_unlock_bh(&sess->conn_lock);
4644 wait_for_completion(&sess->session_wait_comp);
4645 } else
4646 spin_unlock_bh(&sess->conn_lock);
4647
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004648 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004649 return 0;
4650}
4651
4652void iscsit_stop_session(
4653 struct iscsi_session *sess,
4654 int session_sleep,
4655 int connection_sleep)
4656{
4657 u16 conn_count = atomic_read(&sess->nconn);
4658 struct iscsi_conn *conn, *conn_tmp = NULL;
4659 int is_last;
4660
4661 spin_lock_bh(&sess->conn_lock);
4662 if (session_sleep)
4663 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4664
4665 if (connection_sleep) {
4666 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4667 conn_list) {
4668 if (conn_count == 0)
4669 break;
4670
4671 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4672 is_last = 1;
4673 } else {
4674 iscsit_inc_conn_usage_count(conn_tmp);
4675 is_last = 0;
4676 }
4677 iscsit_inc_conn_usage_count(conn);
4678
4679 spin_unlock_bh(&sess->conn_lock);
4680 iscsit_cause_connection_reinstatement(conn, 1);
4681 spin_lock_bh(&sess->conn_lock);
4682
4683 iscsit_dec_conn_usage_count(conn);
4684 if (is_last == 0)
4685 iscsit_dec_conn_usage_count(conn_tmp);
4686 conn_count--;
4687 }
4688 } else {
4689 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4690 iscsit_cause_connection_reinstatement(conn, 0);
4691 }
4692
4693 if (session_sleep && atomic_read(&sess->nconn)) {
4694 spin_unlock_bh(&sess->conn_lock);
4695 wait_for_completion(&sess->session_wait_comp);
4696 } else
4697 spin_unlock_bh(&sess->conn_lock);
4698}
4699
4700int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4701{
4702 struct iscsi_session *sess;
4703 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4704 struct se_session *se_sess, *se_sess_tmp;
4705 int session_count = 0;
4706
4707 spin_lock_bh(&se_tpg->session_lock);
4708 if (tpg->nsessions && !force) {
4709 spin_unlock_bh(&se_tpg->session_lock);
4710 return -1;
4711 }
4712
4713 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4714 sess_list) {
4715 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4716
4717 spin_lock(&sess->conn_lock);
4718 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4719 atomic_read(&sess->session_logout) ||
4720 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4721 spin_unlock(&sess->conn_lock);
4722 continue;
4723 }
4724 atomic_set(&sess->session_reinstatement, 1);
4725 spin_unlock(&sess->conn_lock);
4726 spin_unlock_bh(&se_tpg->session_lock);
4727
4728 iscsit_free_session(sess);
4729 spin_lock_bh(&se_tpg->session_lock);
4730
4731 session_count++;
4732 }
4733 spin_unlock_bh(&se_tpg->session_lock);
4734
4735 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4736 " Group: %hu\n", session_count, tpg->tpgt);
4737 return 0;
4738}
4739
4740MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4741MODULE_VERSION("4.1.x");
4742MODULE_AUTHOR("nab@Linux-iSCSI.org");
4743MODULE_LICENSE("GPL");
4744
4745module_init(iscsi_target_init_module);
4746module_exit(iscsi_target_cleanup_module);