blob: 96aee439c9fdb60b91a17b23534861c43eb799e6 [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);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800476 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000477 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800478
479 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000480
Andy Groveree291e62014-01-24 16:18:54 -0800481 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000482 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800483 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000484
485 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800486 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000487
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800488 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000489 kfree(np);
490 return 0;
491}
492
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700493static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
494static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
495
496static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
497{
498 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
499 return 0;
500}
501
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700502static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
503{
504 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
505
506 spin_lock_bh(&conn->cmd_lock);
507 if (!list_empty(&cmd->i_conn_node))
508 list_del_init(&cmd->i_conn_node);
509 spin_unlock_bh(&conn->cmd_lock);
510
511 __iscsit_free_cmd(cmd, scsi_cmd, true);
512}
513
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800514static struct iscsit_transport iscsi_target_transport = {
515 .name = "iSCSI/TCP",
516 .transport_type = ISCSI_TCP,
517 .owner = NULL,
518 .iscsit_setup_np = iscsit_setup_np,
519 .iscsit_accept_np = iscsit_accept_np,
520 .iscsit_free_np = iscsit_free_np,
521 .iscsit_get_login_rx = iscsit_get_login_rx,
522 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800523 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700524 .iscsit_immediate_queue = iscsit_immediate_queue,
525 .iscsit_response_queue = iscsit_response_queue,
526 .iscsit_queue_data_in = iscsit_queue_rsp,
527 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700528 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800529};
530
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000531static int __init iscsi_target_init_module(void)
532{
533 int ret = 0;
534
535 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
536
537 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
538 if (!iscsit_global) {
539 pr_err("Unable to allocate memory for iscsit_global\n");
540 return -1;
541 }
542 mutex_init(&auth_id_lock);
543 spin_lock_init(&sess_idr_lock);
544 idr_init(&tiqn_idr);
545 idr_init(&sess_idr);
546
547 ret = iscsi_target_register_configfs();
548 if (ret < 0)
549 goto out;
550
551 ret = iscsi_thread_set_init();
552 if (ret < 0)
553 goto configfs_out;
554
555 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
556 TARGET_THREAD_SET_COUNT) {
557 pr_err("iscsi_allocate_thread_sets() returned"
558 " unexpected value!\n");
559 goto ts_out1;
560 }
561
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000562 lio_qr_cache = kmem_cache_create("lio_qr_cache",
563 sizeof(struct iscsi_queue_req),
564 __alignof__(struct iscsi_queue_req), 0, NULL);
565 if (!lio_qr_cache) {
566 pr_err("nable to kmem_cache_create() for"
567 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700568 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000569 }
570
571 lio_dr_cache = kmem_cache_create("lio_dr_cache",
572 sizeof(struct iscsi_datain_req),
573 __alignof__(struct iscsi_datain_req), 0, NULL);
574 if (!lio_dr_cache) {
575 pr_err("Unable to kmem_cache_create() for"
576 " lio_dr_cache\n");
577 goto qr_out;
578 }
579
580 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
581 sizeof(struct iscsi_ooo_cmdsn),
582 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
583 if (!lio_ooo_cache) {
584 pr_err("Unable to kmem_cache_create() for"
585 " lio_ooo_cache\n");
586 goto dr_out;
587 }
588
589 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
590 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
591 0, NULL);
592 if (!lio_r2t_cache) {
593 pr_err("Unable to kmem_cache_create() for"
594 " lio_r2t_cache\n");
595 goto ooo_out;
596 }
597
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800598 iscsit_register_transport(&iscsi_target_transport);
599
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000600 if (iscsit_load_discovery_tpg() < 0)
601 goto r2t_out;
602
603 return ret;
604r2t_out:
605 kmem_cache_destroy(lio_r2t_cache);
606ooo_out:
607 kmem_cache_destroy(lio_ooo_cache);
608dr_out:
609 kmem_cache_destroy(lio_dr_cache);
610qr_out:
611 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000612ts_out2:
613 iscsi_deallocate_thread_sets();
614ts_out1:
615 iscsi_thread_set_free();
616configfs_out:
617 iscsi_target_deregister_configfs();
618out:
619 kfree(iscsit_global);
620 return -ENOMEM;
621}
622
623static void __exit iscsi_target_cleanup_module(void)
624{
625 iscsi_deallocate_thread_sets();
626 iscsi_thread_set_free();
627 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800628 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000629 kmem_cache_destroy(lio_qr_cache);
630 kmem_cache_destroy(lio_dr_cache);
631 kmem_cache_destroy(lio_ooo_cache);
632 kmem_cache_destroy(lio_r2t_cache);
633
634 iscsi_target_deregister_configfs();
635
636 kfree(iscsit_global);
637}
638
Andy Grover8b1e1242012-04-03 15:51:12 -0700639static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700640 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000641 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700642 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643{
644 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000646 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000647 if (!cmd)
648 return -1;
649
650 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700651 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100653 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654 if (!cmd->buf_ptr) {
655 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700656 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000657 return -1;
658 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000659
660 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700661 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000662 spin_unlock_bh(&conn->cmd_lock);
663
664 cmd->i_state = ISTATE_SEND_REJECT;
665 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
666
Nicholas Bellingerba159912013-07-03 03:48:24 -0700667 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000668}
669
Nicholas Bellingerba159912013-07-03 03:48:24 -0700670static int iscsit_add_reject_from_cmd(
671 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000672 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700673 bool add_to_conn,
674 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000675{
676 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000677
678 if (!cmd->conn) {
679 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
680 cmd->init_task_tag);
681 return -1;
682 }
683 conn = cmd->conn;
684
685 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700686 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000687
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100688 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000689 if (!cmd->buf_ptr) {
690 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700691 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000692 return -1;
693 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000694
695 if (add_to_conn) {
696 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700697 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 spin_unlock_bh(&conn->cmd_lock);
699 }
700
701 cmd->i_state = ISTATE_SEND_REJECT;
702 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800703 /*
704 * Perform the kref_put now if se_cmd has already been setup by
705 * scsit_setup_scsi_cmd()
706 */
707 if (cmd->se_cmd.se_tfo != NULL) {
708 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
709 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
710 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700711 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000712}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700713
714static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
715 unsigned char *buf)
716{
717 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
718}
719
720int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
721{
722 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
723}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000724
725/*
726 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700727 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000728 */
729static int iscsit_map_iovec(
730 struct iscsi_cmd *cmd,
731 struct kvec *iov,
732 u32 data_offset,
733 u32 data_length)
734{
735 u32 i = 0;
736 struct scatterlist *sg;
737 unsigned int page_off;
738
739 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700740 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000741 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700742 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000743 page_off = (data_offset % PAGE_SIZE);
744
745 cmd->first_data_sg = sg;
746 cmd->first_data_sg_off = page_off;
747
748 while (data_length) {
749 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
750
751 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
752 iov[i].iov_len = cur_len;
753
754 data_length -= cur_len;
755 page_off = 0;
756 sg = sg_next(sg);
757 i++;
758 }
759
760 cmd->kmapped_nents = i;
761
762 return i;
763}
764
765static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
766{
767 u32 i;
768 struct scatterlist *sg;
769
770 sg = cmd->first_data_sg;
771
772 for (i = 0; i < cmd->kmapped_nents; i++)
773 kunmap(sg_page(&sg[i]));
774}
775
776static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
777{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700778 LIST_HEAD(ack_list);
779 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000780
781 conn->exp_statsn = exp_statsn;
782
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800783 if (conn->sess->sess_ops->RDMAExtensions)
784 return;
785
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000786 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700787 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000788 spin_lock(&cmd->istate_lock);
789 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800790 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000791 cmd->i_state = ISTATE_REMOVE;
792 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700793 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000794 continue;
795 }
796 spin_unlock(&cmd->istate_lock);
797 }
798 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700799
800 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800801 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700802 iscsit_free_cmd(cmd, false);
803 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000804}
805
806static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
807{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700808 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000809
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400810 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000811
812 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
813 if (!cmd->iov_data) {
814 pr_err("Unable to allocate cmd->iov_data\n");
815 return -ENOMEM;
816 }
817
818 cmd->orig_iov_data_count = iov_count;
819 return 0;
820}
821
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800822int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
823 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000824{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800825 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000826 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700827 int iscsi_task_attr;
828 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000829
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800830 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000831
832 hdr = (struct iscsi_scsi_req *) buf;
833 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000834
835 /* FIXME; Add checks for AdditionalHeaderSegment */
836
837 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
838 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
839 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
840 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700841 return iscsit_add_reject_cmd(cmd,
842 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000843 }
844
845 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
846 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
847 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800848 * From RFC-3720 Section 10.3.1:
849 *
850 * "Either or both of R and W MAY be 1 when either the
851 * Expected Data Transfer Length and/or Bidirectional Read
852 * Expected Data Transfer Length are 0"
853 *
854 * For this case, go ahead and clear the unnecssary bits
855 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000856 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800857 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
858 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000859
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800860 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000861 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800862 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000863 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000864
865 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
866 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
867 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
868 " MUST be set if Expected Data Transfer Length is not 0."
869 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700870 return iscsit_add_reject_cmd(cmd,
871 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872 }
873
874 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
875 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
876 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700877 return iscsit_add_reject_cmd(cmd,
878 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000879 }
880
881 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
882 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
883 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700884 return iscsit_add_reject_cmd(cmd,
885 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000886 }
887
888 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
889 pr_err("ImmediateData=No but DataSegmentLength=%u,"
890 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700891 return iscsit_add_reject_cmd(cmd,
892 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000893 }
894
Nicholas Bellingerba159912013-07-03 03:48:24 -0700895 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000896 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
897 pr_err("Expected Data Transfer Length and Length of"
898 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
899 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700900 return iscsit_add_reject_cmd(cmd,
901 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 }
903
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400904 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000905 pr_err("DataSegmentLength: %u is greater than"
906 " EDTL: %u, protocol error.\n", payload_length,
907 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700908 return iscsit_add_reject_cmd(cmd,
909 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000910 }
911
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700912 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000913 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700914 " MaxXmitDataSegmentLength: %u, protocol error.\n",
915 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700916 return iscsit_add_reject_cmd(cmd,
917 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000918 }
919
920 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
921 pr_err("DataSegmentLength: %u is greater than"
922 " FirstBurstLength: %u, protocol error.\n",
923 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700924 return iscsit_add_reject_cmd(cmd,
925 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000926 }
927
928 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
929 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
930 DMA_NONE;
931
Andy Groverd28b11692012-04-03 15:51:22 -0700932 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700933 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
934 /*
935 * Figure out the SAM Task Attribute for the incoming SCSI CDB
936 */
937 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
938 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
939 sam_task_attr = MSG_SIMPLE_TAG;
940 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
941 sam_task_attr = MSG_ORDERED_TAG;
942 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
943 sam_task_attr = MSG_HEAD_TAG;
944 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
945 sam_task_attr = MSG_ACA_TAG;
946 else {
947 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
948 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
949 sam_task_attr = MSG_SIMPLE_TAG;
950 }
951
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000952 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
953 cmd->i_state = ISTATE_NEW_CMD;
954 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
955 cmd->immediate_data = (payload_length) ? 1 : 0;
956 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
957 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
958 if (cmd->unsolicited_data)
959 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
960
961 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
962 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
963 spin_lock_bh(&conn->sess->ttt_lock);
964 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
965 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
966 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
967 spin_unlock_bh(&conn->sess->ttt_lock);
968 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
969 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400970 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
971 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000972 cmd->first_burst_len = payload_length;
973
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800974 if (!conn->sess->sess_ops->RDMAExtensions &&
975 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000976 struct iscsi_datain_req *dr;
977
978 dr = iscsit_allocate_datain_req();
979 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700980 return iscsit_add_reject_cmd(cmd,
981 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000982
983 iscsit_attach_datain_req(cmd, dr);
984 }
985
986 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700987 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
988 */
989 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400990 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
991 cmd->data_direction, sam_task_attr,
992 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700993
994 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
995 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800996 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
997 conn->cid);
998
999 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001000
Christoph Hellwigde103c92012-11-06 12:24:09 -08001001 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1002 scsilun_to_int(&hdr->lun));
1003 if (cmd->sense_reason)
1004 goto attach_cmd;
1005
1006 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1007 if (cmd->sense_reason) {
1008 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001009 return iscsit_add_reject_cmd(cmd,
1010 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001011 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001012
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001013 goto attach_cmd;
1014 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001015
Christoph Hellwigde103c92012-11-06 12:24:09 -08001016 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001017 return iscsit_add_reject_cmd(cmd,
1018 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001019 }
1020
1021attach_cmd:
1022 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001023 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024 spin_unlock_bh(&conn->cmd_lock);
1025 /*
1026 * Check if we need to delay processing because of ALUA
1027 * Active/NonOptimized primary access state..
1028 */
1029 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001030
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001031 return 0;
1032}
1033EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001034
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001035void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1036{
1037 iscsit_set_dataout_sequence_values(cmd);
1038
1039 spin_lock_bh(&cmd->dataout_timeout_lock);
1040 iscsit_start_dataout_timer(cmd, cmd->conn);
1041 spin_unlock_bh(&cmd->dataout_timeout_lock);
1042}
1043EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1044
1045int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1046 struct iscsi_scsi_req *hdr)
1047{
1048 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001049 /*
1050 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1051 * the Immediate Bit is not set, and no Immediate
1052 * Data is attached.
1053 *
1054 * A PDU/CmdSN carrying Immediate Data can only
1055 * be processed after the DataCRC has passed.
1056 * If the DataCRC fails, the CmdSN MUST NOT
1057 * be acknowledged. (See below)
1058 */
1059 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001060 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1061 (unsigned char *)hdr, hdr->cmdsn);
1062 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1063 return -1;
1064 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001065 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001066 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001067 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001068 }
1069
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001070 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001071
1072 /*
1073 * If no Immediate Data is attached, it's OK to return now.
1074 */
1075 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001076 if (!cmd->sense_reason && cmd->unsolicited_data)
1077 iscsit_set_unsoliticed_dataout(cmd);
1078 if (!cmd->sense_reason)
1079 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001080
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001081 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001082 return 0;
1083 }
1084
1085 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001086 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1087 * execution. These exceptions are processed in CmdSN order using
1088 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001089 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001090 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001091 if (cmd->reject_reason)
1092 return 0;
1093
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001094 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001095 }
1096 /*
1097 * Call directly into transport_generic_new_cmd() to perform
1098 * the backend memory allocation.
1099 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001100 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001101 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001102 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001103
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001104 return 0;
1105}
1106EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1107
1108static int
1109iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1110 bool dump_payload)
1111{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001112 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001113 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1114 /*
1115 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1116 */
1117 if (dump_payload == true)
1118 goto after_immediate_data;
1119
1120 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1121 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001122after_immediate_data:
1123 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1124 /*
1125 * A PDU/CmdSN carrying Immediate Data passed
1126 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1127 * Immediate Bit is not set.
1128 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001129 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1130 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001131 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001132 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001133
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001134 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001135 int rc;
1136
1137 rc = iscsit_dump_data_payload(cmd->conn,
1138 cmd->first_burst_len, 1);
1139 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1140 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001141 } else if (cmd->unsolicited_data)
1142 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001143
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001144 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1145 /*
1146 * Immediate Data failed DataCRC and ERL>=1,
1147 * silently drop this PDU and let the initiator
1148 * plug the CmdSN gap.
1149 *
1150 * FIXME: Send Unsolicited NOPIN with reserved
1151 * TTT here to help the initiator figure out
1152 * the missing CmdSN, although they should be
1153 * intelligent enough to determine the missing
1154 * CmdSN and issue a retry to plug the sequence.
1155 */
1156 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001157 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001158 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1159 return -1;
1160
1161 return 0;
1162}
1163
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001164static int
1165iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1166 unsigned char *buf)
1167{
1168 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1169 int rc, immed_data;
1170 bool dump_payload = false;
1171
1172 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1173 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001174 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001175 /*
1176 * Allocation iovecs needed for struct socket operations for
1177 * traditional iSCSI block I/O.
1178 */
1179 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001180 return iscsit_add_reject_cmd(cmd,
1181 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001182 }
1183 immed_data = cmd->immediate_data;
1184
1185 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1186 if (rc < 0)
1187 return rc;
1188 else if (rc > 0)
1189 dump_payload = true;
1190
1191 if (!immed_data)
1192 return 0;
1193
1194 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1195}
1196
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001197static u32 iscsit_do_crypto_hash_sg(
1198 struct hash_desc *hash,
1199 struct iscsi_cmd *cmd,
1200 u32 data_offset,
1201 u32 data_length,
1202 u32 padding,
1203 u8 *pad_bytes)
1204{
1205 u32 data_crc;
1206 u32 i;
1207 struct scatterlist *sg;
1208 unsigned int page_off;
1209
1210 crypto_hash_init(hash);
1211
1212 sg = cmd->first_data_sg;
1213 page_off = cmd->first_data_sg_off;
1214
1215 i = 0;
1216 while (data_length) {
1217 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1218
1219 crypto_hash_update(hash, &sg[i], cur_len);
1220
1221 data_length -= cur_len;
1222 page_off = 0;
1223 i++;
1224 }
1225
1226 if (padding) {
1227 struct scatterlist pad_sg;
1228
1229 sg_init_one(&pad_sg, pad_bytes, padding);
1230 crypto_hash_update(hash, &pad_sg, padding);
1231 }
1232 crypto_hash_final(hash, (u8 *) &data_crc);
1233
1234 return data_crc;
1235}
1236
1237static void iscsit_do_crypto_hash_buf(
1238 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001239 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001240 u32 payload_length,
1241 u32 padding,
1242 u8 *pad_bytes,
1243 u8 *data_crc)
1244{
1245 struct scatterlist sg;
1246
1247 crypto_hash_init(hash);
1248
Jörn Engel8359cf42011-11-24 02:05:51 +01001249 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001250 crypto_hash_update(hash, &sg, payload_length);
1251
1252 if (padding) {
1253 sg_init_one(&sg, pad_bytes, padding);
1254 crypto_hash_update(hash, &sg, padding);
1255 }
1256 crypto_hash_final(hash, data_crc);
1257}
1258
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001259int
1260iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1261 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001262{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001263 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001264 struct iscsi_cmd *cmd = NULL;
1265 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001266 u32 payload_length = ntoh24(hdr->dlength);
1267 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001268
1269 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001270 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1271 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001272 }
1273
1274 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001275 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001276
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001277 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001278 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001279 " MaxXmitDataSegmentLength: %u\n", payload_length,
1280 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001281 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1282 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001283 }
1284
1285 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1286 payload_length);
1287 if (!cmd)
1288 return 0;
1289
1290 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1291 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001292 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001293 payload_length, conn->cid);
1294
1295 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1296 pr_err("Command ITT: 0x%08x received DataOUT after"
1297 " last DataOUT received, dumping payload\n",
1298 cmd->init_task_tag);
1299 return iscsit_dump_data_payload(conn, payload_length, 1);
1300 }
1301
1302 if (cmd->data_direction != DMA_TO_DEVICE) {
1303 pr_err("Command ITT: 0x%08x received DataOUT for a"
1304 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001305 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001306 }
1307 se_cmd = &cmd->se_cmd;
1308 iscsit_mod_dataout_timer(cmd);
1309
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001310 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001311 pr_err("DataOut Offset: %u, Length %u greater than"
1312 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001313 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001314 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001315 }
1316
1317 if (cmd->unsolicited_data) {
1318 int dump_unsolicited_data = 0;
1319
1320 if (conn->sess->sess_ops->InitialR2T) {
1321 pr_err("Received unexpected unsolicited data"
1322 " while InitialR2T=Yes, protocol error.\n");
1323 transport_send_check_condition_and_sense(&cmd->se_cmd,
1324 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1325 return -1;
1326 }
1327 /*
1328 * Special case for dealing with Unsolicited DataOUT
1329 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1330 * failures;
1331 */
1332
1333 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001334 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001335 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001336 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001337
1338 if (dump_unsolicited_data) {
1339 /*
1340 * Check if a delayed TASK_ABORTED status needs to
1341 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1342 * received with the unsolicitied data out.
1343 */
1344 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1345 iscsit_stop_dataout_timer(cmd);
1346
1347 transport_check_aborted_status(se_cmd,
1348 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1349 return iscsit_dump_data_payload(conn, payload_length, 1);
1350 }
1351 } else {
1352 /*
1353 * For the normal solicited data path:
1354 *
1355 * Check for a delayed TASK_ABORTED status and dump any
1356 * incoming data out payload if one exists. Also, when the
1357 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1358 * data out sequence, we decrement outstanding_r2ts. Once
1359 * outstanding_r2ts reaches zero, go ahead and send the delayed
1360 * TASK_ABORTED status.
1361 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001362 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001363 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1364 if (--cmd->outstanding_r2ts < 1) {
1365 iscsit_stop_dataout_timer(cmd);
1366 transport_check_aborted_status(
1367 se_cmd, 1);
1368 }
1369
1370 return iscsit_dump_data_payload(conn, payload_length, 1);
1371 }
1372 }
1373 /*
1374 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1375 * within-command recovery checks before receiving the payload.
1376 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001377 rc = iscsit_check_pre_dataout(cmd, buf);
1378 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001379 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001380 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001381 return -1;
1382
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001383 *out_cmd = cmd;
1384 return 0;
1385}
1386EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1387
1388static int
1389iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1390 struct iscsi_data *hdr)
1391{
1392 struct kvec *iov;
1393 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1394 u32 payload_length = ntoh24(hdr->dlength);
1395 int iov_ret, data_crc_failed = 0;
1396
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001397 rx_size += payload_length;
1398 iov = &cmd->iov_data[0];
1399
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001400 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1401 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001402 if (iov_ret < 0)
1403 return -1;
1404
1405 iov_count += iov_ret;
1406
1407 padding = ((-payload_length) & 3);
1408 if (padding != 0) {
1409 iov[iov_count].iov_base = cmd->pad_bytes;
1410 iov[iov_count++].iov_len = padding;
1411 rx_size += padding;
1412 pr_debug("Receiving %u padding bytes.\n", padding);
1413 }
1414
1415 if (conn->conn_ops->DataDigest) {
1416 iov[iov_count].iov_base = &checksum;
1417 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1418 rx_size += ISCSI_CRC_LEN;
1419 }
1420
1421 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1422
1423 iscsit_unmap_iovec(cmd);
1424
1425 if (rx_got != rx_size)
1426 return -1;
1427
1428 if (conn->conn_ops->DataDigest) {
1429 u32 data_crc;
1430
1431 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001432 be32_to_cpu(hdr->offset),
1433 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001434 cmd->pad_bytes);
1435
1436 if (checksum != data_crc) {
1437 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1438 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1439 " does not match computed 0x%08x\n",
1440 hdr->itt, hdr->offset, payload_length,
1441 hdr->datasn, checksum, data_crc);
1442 data_crc_failed = 1;
1443 } else {
1444 pr_debug("Got CRC32C DataDigest 0x%08x for"
1445 " %u bytes of Data Out\n", checksum,
1446 payload_length);
1447 }
1448 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001449
1450 return data_crc_failed;
1451}
1452
1453int
1454iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1455 bool data_crc_failed)
1456{
1457 struct iscsi_conn *conn = cmd->conn;
1458 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001459 /*
1460 * Increment post receive data and CRC values or perform
1461 * within-command recovery.
1462 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001463 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1464 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001465 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001466 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001467 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001468 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1469 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 /*
1471 * Handle extra special case for out of order
1472 * Unsolicited Data Out.
1473 */
1474 spin_lock_bh(&cmd->istate_lock);
1475 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1476 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1477 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1478 spin_unlock_bh(&cmd->istate_lock);
1479
1480 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001481 if (ooo_cmdsn)
1482 return 0;
1483 target_execute_cmd(&cmd->se_cmd);
1484 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001485 } else /* DATAOUT_CANNOT_RECOVER */
1486 return -1;
1487
1488 return 0;
1489}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001490EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001491
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001492static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1493{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001494 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001495 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1496 int rc;
1497 bool data_crc_failed = false;
1498
1499 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1500 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001501 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001502 else if (!cmd)
1503 return 0;
1504
1505 rc = iscsit_get_dataout(conn, cmd, hdr);
1506 if (rc < 0)
1507 return rc;
1508 else if (rc > 0)
1509 data_crc_failed = true;
1510
1511 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1512}
1513
Nicholas Bellinger778de362013-06-14 16:07:47 -07001514int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1515 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001516{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001517 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001518
Arshad Hussaina3662602014-03-14 15:28:59 -07001519 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1520 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1521 if (!cmd)
1522 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1523 (unsigned char *)hdr);
1524
1525 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1526 (unsigned char *)hdr);
1527 }
1528
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001529 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1531 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001532 if (!cmd)
1533 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1534 (unsigned char *)hdr);
1535
Nicholas Bellingerba159912013-07-03 03:48:24 -07001536 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1537 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001538 }
1539
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001540 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001541 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001542 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001543 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001544 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001545 if (!cmd)
1546 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1547 (unsigned char *)hdr);
1548
Nicholas Bellingerba159912013-07-03 03:48:24 -07001549 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1550 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001551 }
1552
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001553 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001554 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001555 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001556 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1557 payload_length);
1558 /*
1559 * This is not a response to a Unsolicited NopIN, which means
1560 * it can either be a NOPOUT ping request (with a valid ITT),
1561 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1562 * Either way, make sure we allocate an struct iscsi_cmd, as both
1563 * can contain ping data.
1564 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001565 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001566 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1567 cmd->i_state = ISTATE_SEND_NOPIN;
1568 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1569 1 : 0);
1570 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1571 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001572 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1573 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001574 cmd->data_direction = DMA_NONE;
1575 }
1576
Nicholas Bellinger778de362013-06-14 16:07:47 -07001577 return 0;
1578}
1579EXPORT_SYMBOL(iscsit_setup_nop_out);
1580
1581int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1582 struct iscsi_nopout *hdr)
1583{
1584 struct iscsi_cmd *cmd_p = NULL;
1585 int cmdsn_ret = 0;
1586 /*
1587 * Initiator is expecting a NopIN ping reply..
1588 */
1589 if (hdr->itt != RESERVED_ITT) {
1590 BUG_ON(!cmd);
1591
1592 spin_lock_bh(&conn->cmd_lock);
1593 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1594 spin_unlock_bh(&conn->cmd_lock);
1595
1596 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1597
1598 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1599 iscsit_add_cmd_to_response_queue(cmd, conn,
1600 cmd->i_state);
1601 return 0;
1602 }
1603
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001604 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1605 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001606 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1607 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001608 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001609 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001610
1611 return 0;
1612 }
1613 /*
1614 * This was a response to a unsolicited NOPIN ping.
1615 */
1616 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1617 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1618 if (!cmd_p)
1619 return -EINVAL;
1620
1621 iscsit_stop_nopin_response_timer(conn);
1622
1623 cmd_p->i_state = ISTATE_REMOVE;
1624 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1625
1626 iscsit_start_nopin_timer(conn);
1627 return 0;
1628 }
1629 /*
1630 * Otherwise, initiator is not expecting a NOPIN is response.
1631 * Just ignore for now.
1632 */
1633 return 0;
1634}
1635EXPORT_SYMBOL(iscsit_process_nop_out);
1636
1637static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1638 unsigned char *buf)
1639{
1640 unsigned char *ping_data = NULL;
1641 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1642 struct kvec *iov = NULL;
1643 u32 payload_length = ntoh24(hdr->dlength);
1644 int ret;
1645
1646 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1647 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001648 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001649 /*
1650 * Handle NOP-OUT payload for traditional iSCSI sockets
1651 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001652 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001653 u32 checksum, data_crc, padding = 0;
1654 int niov = 0, rx_got, rx_size = payload_length;
1655
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001656 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1657 if (!ping_data) {
1658 pr_err("Unable to allocate memory for"
1659 " NOPOUT ping data.\n");
1660 ret = -1;
1661 goto out;
1662 }
1663
1664 iov = &cmd->iov_misc[0];
1665 iov[niov].iov_base = ping_data;
1666 iov[niov++].iov_len = payload_length;
1667
1668 padding = ((-payload_length) & 3);
1669 if (padding != 0) {
1670 pr_debug("Receiving %u additional bytes"
1671 " for padding.\n", padding);
1672 iov[niov].iov_base = &cmd->pad_bytes;
1673 iov[niov++].iov_len = padding;
1674 rx_size += padding;
1675 }
1676 if (conn->conn_ops->DataDigest) {
1677 iov[niov].iov_base = &checksum;
1678 iov[niov++].iov_len = ISCSI_CRC_LEN;
1679 rx_size += ISCSI_CRC_LEN;
1680 }
1681
1682 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1683 if (rx_got != rx_size) {
1684 ret = -1;
1685 goto out;
1686 }
1687
1688 if (conn->conn_ops->DataDigest) {
1689 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1690 ping_data, payload_length,
1691 padding, cmd->pad_bytes,
1692 (u8 *)&data_crc);
1693
1694 if (checksum != data_crc) {
1695 pr_err("Ping data CRC32C DataDigest"
1696 " 0x%08x does not match computed 0x%08x\n",
1697 checksum, data_crc);
1698 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1699 pr_err("Unable to recover from"
1700 " NOPOUT Ping DataCRC failure while in"
1701 " ERL=0.\n");
1702 ret = -1;
1703 goto out;
1704 } else {
1705 /*
1706 * Silently drop this PDU and let the
1707 * initiator plug the CmdSN gap.
1708 */
1709 pr_debug("Dropping NOPOUT"
1710 " Command CmdSN: 0x%08x due to"
1711 " DataCRC error.\n", hdr->cmdsn);
1712 ret = 0;
1713 goto out;
1714 }
1715 } else {
1716 pr_debug("Got CRC32C DataDigest"
1717 " 0x%08x for %u bytes of ping data.\n",
1718 checksum, payload_length);
1719 }
1720 }
1721
1722 ping_data[payload_length] = '\0';
1723 /*
1724 * Attach ping data to struct iscsi_cmd->buf_ptr.
1725 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001726 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001727 cmd->buf_ptr_size = payload_length;
1728
1729 pr_debug("Got %u bytes of NOPOUT ping"
1730 " data.\n", payload_length);
1731 pr_debug("Ping Data: \"%s\"\n", ping_data);
1732 }
1733
Nicholas Bellinger778de362013-06-14 16:07:47 -07001734 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001735out:
1736 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001737 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001738
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739 kfree(ping_data);
1740 return ret;
1741}
1742
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001743int
1744iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1745 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001746{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001747 struct se_tmr_req *se_tmr;
1748 struct iscsi_tmr_req *tmr_req;
1749 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001750 int out_of_order_cmdsn = 0, ret;
1751 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001752 u8 function;
1753
1754 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1756 function = hdr->flags;
1757
1758 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1759 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1760 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1761 hdr->rtt, hdr->refcmdsn, conn->cid);
1762
1763 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1764 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001765 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001766 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001767 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001768 }
1769
1770 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1771 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1772 pr_err("Task Management Request TASK_REASSIGN not"
1773 " issued as immediate command, bad iSCSI Initiator"
1774 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001775 return iscsit_add_reject_cmd(cmd,
1776 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001777 }
1778 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001779 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1780 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001781
Andy Groverd28b11692012-04-03 15:51:22 -07001782 cmd->data_direction = DMA_NONE;
1783
1784 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1785 if (!cmd->tmr_req) {
1786 pr_err("Unable to allocate memory for"
1787 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001788 return iscsit_add_reject_cmd(cmd,
1789 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1790 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001791 }
1792
1793 /*
1794 * TASK_REASSIGN for ERL=2 / connection stays inside of
1795 * LIO-Target $FABRIC_MOD
1796 */
1797 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1798
1799 u8 tcm_function;
1800 int ret;
1801
1802 transport_init_se_cmd(&cmd->se_cmd,
1803 &lio_target_fabric_configfs->tf_ops,
1804 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001805 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001806
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001807 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1808 sess_ref = true;
1809
Andy Groverd28b11692012-04-03 15:51:22 -07001810 switch (function) {
1811 case ISCSI_TM_FUNC_ABORT_TASK:
1812 tcm_function = TMR_ABORT_TASK;
1813 break;
1814 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1815 tcm_function = TMR_ABORT_TASK_SET;
1816 break;
1817 case ISCSI_TM_FUNC_CLEAR_ACA:
1818 tcm_function = TMR_CLEAR_ACA;
1819 break;
1820 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1821 tcm_function = TMR_CLEAR_TASK_SET;
1822 break;
1823 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1824 tcm_function = TMR_LUN_RESET;
1825 break;
1826 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1827 tcm_function = TMR_TARGET_WARM_RESET;
1828 break;
1829 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1830 tcm_function = TMR_TARGET_COLD_RESET;
1831 break;
1832 default:
1833 pr_err("Unknown iSCSI TMR Function:"
1834 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001835 return iscsit_add_reject_cmd(cmd,
1836 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001837 }
1838
1839 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1840 tcm_function, GFP_KERNEL);
1841 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001842 return iscsit_add_reject_cmd(cmd,
1843 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001844
1845 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1846 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001847
1848 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1849 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1850 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1851 cmd->init_task_tag = hdr->itt;
1852 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001853 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1854 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001855 se_tmr = cmd->se_cmd.se_tmr_req;
1856 tmr_req = cmd->tmr_req;
1857 /*
1858 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1859 */
1860 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001861 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1862 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001863 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001864 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1865 goto attach;
1866 }
1867 }
1868
1869 switch (function) {
1870 case ISCSI_TM_FUNC_ABORT_TASK:
1871 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001872 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001873 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001874 break;
1875 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1876 case ISCSI_TM_FUNC_CLEAR_ACA:
1877 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1878 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1879 break;
1880 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1881 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001882 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1883 goto attach;
1884 }
1885 break;
1886 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1887 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001888 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1889 goto attach;
1890 }
1891 break;
1892 case ISCSI_TM_FUNC_TASK_REASSIGN:
1893 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1894 /*
1895 * Perform sanity checks on the ExpDataSN only if the
1896 * TASK_REASSIGN was successful.
1897 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001898 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001899 break;
1900
1901 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001902 return iscsit_add_reject_cmd(cmd,
1903 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001904 break;
1905 default:
1906 pr_err("Unknown TMR function: 0x%02x, protocol"
1907 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001908 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1909 goto attach;
1910 }
1911
1912 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1913 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1914 se_tmr->call_transport = 1;
1915attach:
1916 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001917 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001918 spin_unlock_bh(&conn->cmd_lock);
1919
1920 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001921 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001922 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1923 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001924 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001925 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001926 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001927 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001929 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001930
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001931 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001932 return 0;
1933 /*
1934 * Found the referenced task, send to transport for processing.
1935 */
1936 if (se_tmr->call_transport)
1937 return transport_generic_handle_tmr(&cmd->se_cmd);
1938
1939 /*
1940 * Could not find the referenced LUN, task, or Task Management
1941 * command not authorized or supported. Change state and
1942 * let the tx_thread send the response.
1943 *
1944 * For connection recovery, this is also the default action for
1945 * TMR TASK_REASSIGN.
1946 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001947 if (sess_ref) {
1948 pr_debug("Handle TMR, using sess_ref=true check\n");
1949 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1950 }
1951
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001952 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1953 return 0;
1954}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001955EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001956
1957/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001958int
1959iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1960 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001961{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001962 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001963
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001964 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001965 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001966 "greater than MaxXmitDataSegmentLength %u.\n",
1967 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001968 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1969 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001970 }
1971
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001972 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1973 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1974 pr_err("Multi sequence text commands currently not supported\n");
1975 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1976 (unsigned char *)hdr);
1977 }
1978
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001979 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1980 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1981 hdr->exp_statsn, payload_length);
1982
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001983 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1984 cmd->i_state = ISTATE_SEND_TEXTRSP;
1985 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1986 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1987 cmd->targ_xfer_tag = 0xFFFFFFFF;
1988 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1989 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1990 cmd->data_direction = DMA_NONE;
1991
1992 return 0;
1993}
1994EXPORT_SYMBOL(iscsit_setup_text_cmd);
1995
1996int
1997iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1998 struct iscsi_text *hdr)
1999{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002000 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002001 int cmdsn_ret;
2002
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002003 if (!text_in) {
2004 pr_err("Unable to locate text_in buffer for sendtargets"
2005 " discovery\n");
2006 goto reject;
2007 }
2008 if (strncmp("SendTargets", text_in, 11) != 0) {
2009 pr_err("Received Text Data that is not"
2010 " SendTargets, cannot continue.\n");
2011 goto reject;
2012 }
2013 text_ptr = strchr(text_in, '=');
2014 if (!text_ptr) {
2015 pr_err("No \"=\" separator found in Text Data,"
2016 " cannot continue.\n");
2017 goto reject;
2018 }
2019 if (!strncmp("=All", text_ptr, 4)) {
2020 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002021 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2022 !strncmp("=eui.", text_ptr, 5)) {
2023 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002024 } else {
2025 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2026 goto reject;
2027 }
2028
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002029 spin_lock_bh(&conn->cmd_lock);
2030 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2031 spin_unlock_bh(&conn->cmd_lock);
2032
2033 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2034
2035 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002036 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2037 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002038 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002039 return -1;
2040
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002041 return 0;
2042 }
2043
2044 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002045
2046reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002047 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2048 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002049}
2050EXPORT_SYMBOL(iscsit_process_text_cmd);
2051
2052static int
2053iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2054 unsigned char *buf)
2055{
2056 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2057 char *text_in = NULL;
2058 u32 payload_length = ntoh24(hdr->dlength);
2059 int rx_size, rc;
2060
2061 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2062 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002063 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002064
2065 rx_size = payload_length;
2066 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002067 u32 checksum = 0, data_crc = 0;
2068 u32 padding = 0, pad_bytes = 0;
2069 int niov = 0, rx_got;
2070 struct kvec iov[3];
2071
2072 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002073 if (!text_in) {
2074 pr_err("Unable to allocate memory for"
2075 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002076 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002077 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002078 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002079
2080 memset(iov, 0, 3 * sizeof(struct kvec));
2081 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002082 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002083
2084 padding = ((-payload_length) & 3);
2085 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002086 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002087 iov[niov++].iov_len = padding;
2088 rx_size += padding;
2089 pr_debug("Receiving %u additional bytes"
2090 " for padding.\n", padding);
2091 }
2092 if (conn->conn_ops->DataDigest) {
2093 iov[niov].iov_base = &checksum;
2094 iov[niov++].iov_len = ISCSI_CRC_LEN;
2095 rx_size += ISCSI_CRC_LEN;
2096 }
2097
2098 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002099 if (rx_got != rx_size)
2100 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002101
2102 if (conn->conn_ops->DataDigest) {
2103 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002104 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002105 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002106 (u8 *)&data_crc);
2107
2108 if (checksum != data_crc) {
2109 pr_err("Text data CRC32C DataDigest"
2110 " 0x%08x does not match computed"
2111 " 0x%08x\n", checksum, data_crc);
2112 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2113 pr_err("Unable to recover from"
2114 " Text Data digest failure while in"
2115 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002117 } else {
2118 /*
2119 * Silently drop this PDU and let the
2120 * initiator plug the CmdSN gap.
2121 */
2122 pr_debug("Dropping Text"
2123 " Command CmdSN: 0x%08x due to"
2124 " DataCRC error.\n", hdr->cmdsn);
2125 kfree(text_in);
2126 return 0;
2127 }
2128 } else {
2129 pr_debug("Got CRC32C DataDigest"
2130 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002131 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002132 }
2133 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002134 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002135 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002136 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002137 }
2138
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002139 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002140
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002141reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002142 kfree(cmd->text_in_ptr);
2143 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002144 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002145}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002146EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002147
2148int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2149{
2150 struct iscsi_conn *conn_p;
2151 struct iscsi_session *sess = conn->sess;
2152
2153 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2154 " for SID: %u.\n", conn->cid, conn->sess->sid);
2155
2156 atomic_set(&sess->session_logout, 1);
2157 atomic_set(&conn->conn_logout_remove, 1);
2158 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2159
2160 iscsit_inc_conn_usage_count(conn);
2161 iscsit_inc_session_usage_count(sess);
2162
2163 spin_lock_bh(&sess->conn_lock);
2164 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2165 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2166 continue;
2167
2168 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2169 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2170 }
2171 spin_unlock_bh(&sess->conn_lock);
2172
2173 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2174
2175 return 0;
2176}
2177
2178int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2179{
2180 struct iscsi_conn *l_conn;
2181 struct iscsi_session *sess = conn->sess;
2182
2183 pr_debug("Received logout request CLOSECONNECTION for CID:"
2184 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2185
2186 /*
2187 * A Logout Request with a CLOSECONNECTION reason code for a CID
2188 * can arrive on a connection with a differing CID.
2189 */
2190 if (conn->cid == cmd->logout_cid) {
2191 spin_lock_bh(&conn->state_lock);
2192 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2193 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2194
2195 atomic_set(&conn->conn_logout_remove, 1);
2196 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2197 iscsit_inc_conn_usage_count(conn);
2198
2199 spin_unlock_bh(&conn->state_lock);
2200 } else {
2201 /*
2202 * Handle all different cid CLOSECONNECTION requests in
2203 * iscsit_logout_post_handler_diffcid() as to give enough
2204 * time for any non immediate command's CmdSN to be
2205 * acknowledged on the connection in question.
2206 *
2207 * Here we simply make sure the CID is still around.
2208 */
2209 l_conn = iscsit_get_conn_from_cid(sess,
2210 cmd->logout_cid);
2211 if (!l_conn) {
2212 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2213 iscsit_add_cmd_to_response_queue(cmd, conn,
2214 cmd->i_state);
2215 return 0;
2216 }
2217
2218 iscsit_dec_conn_usage_count(l_conn);
2219 }
2220
2221 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2222
2223 return 0;
2224}
2225
2226int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2227{
2228 struct iscsi_session *sess = conn->sess;
2229
2230 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2231 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2232
2233 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2234 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2235 " while ERL!=2.\n");
2236 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2237 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2238 return 0;
2239 }
2240
2241 if (conn->cid == cmd->logout_cid) {
2242 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2243 " with CID: %hu on CID: %hu, implementation error.\n",
2244 cmd->logout_cid, conn->cid);
2245 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2246 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2247 return 0;
2248 }
2249
2250 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2251
2252 return 0;
2253}
2254
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002255int
2256iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2257 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002258{
2259 int cmdsn_ret, logout_remove = 0;
2260 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002261 struct iscsi_logout *hdr;
2262 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2263
2264 hdr = (struct iscsi_logout *) buf;
2265 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266
2267 if (tiqn) {
2268 spin_lock(&tiqn->logout_stats.lock);
2269 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2270 tiqn->logout_stats.normal_logouts++;
2271 else
2272 tiqn->logout_stats.abnormal_logouts++;
2273 spin_unlock(&tiqn->logout_stats.lock);
2274 }
2275
2276 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2277 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2278 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2279 hdr->cid, conn->cid);
2280
2281 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2282 pr_err("Received logout request on connection that"
2283 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002284 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002285 return 0;
2286 }
2287
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002288 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2289 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2290 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2291 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2292 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002293 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2294 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2295 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 cmd->logout_reason = reason_code;
2297 cmd->data_direction = DMA_NONE;
2298
2299 /*
2300 * We need to sleep in these cases (by returning 1) until the Logout
2301 * Response gets sent in the tx thread.
2302 */
2303 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2304 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002305 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002306 logout_remove = 1;
2307
2308 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002309 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002310 spin_unlock_bh(&conn->cmd_lock);
2311
2312 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002313 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314
2315 /*
2316 * Immediate commands are executed, well, immediately.
2317 * Non-Immediate Logout Commands are executed in CmdSN order.
2318 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002319 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002320 int ret = iscsit_execute_cmd(cmd, 0);
2321
2322 if (ret < 0)
2323 return ret;
2324 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002325 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002326 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002328 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2329 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002330 }
2331
2332 return logout_remove;
2333}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002334EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002335
2336static int iscsit_handle_snack(
2337 struct iscsi_conn *conn,
2338 unsigned char *buf)
2339{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002340 struct iscsi_snack *hdr;
2341
2342 hdr = (struct iscsi_snack *) buf;
2343 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002344
2345 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2346 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2347 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2348 hdr->begrun, hdr->runlength, conn->cid);
2349
2350 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2351 pr_err("Initiator sent SNACK request while in"
2352 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002353 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2354 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355 }
2356 /*
2357 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2358 * call from inside iscsi_send_recovery_datain_or_r2t().
2359 */
2360 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2361 case 0:
2362 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002363 hdr->itt,
2364 be32_to_cpu(hdr->ttt),
2365 be32_to_cpu(hdr->begrun),
2366 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002367 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002368 return iscsit_handle_status_snack(conn, hdr->itt,
2369 be32_to_cpu(hdr->ttt),
2370 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002371 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002372 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2373 be32_to_cpu(hdr->begrun),
2374 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002375 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2376 /* FIXME: Support R-Data SNACK */
2377 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002378 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2379 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002380 default:
2381 pr_err("Unknown SNACK type 0x%02x, protocol"
2382 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002383 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2384 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002385 }
2386
2387 return 0;
2388}
2389
2390static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2391{
2392 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2393 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2394 wait_for_completion_interruptible_timeout(
2395 &conn->rx_half_close_comp,
2396 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2397 }
2398}
2399
2400static int iscsit_handle_immediate_data(
2401 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002402 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002403 u32 length)
2404{
2405 int iov_ret, rx_got = 0, rx_size = 0;
2406 u32 checksum, iov_count = 0, padding = 0;
2407 struct iscsi_conn *conn = cmd->conn;
2408 struct kvec *iov;
2409
2410 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2411 if (iov_ret < 0)
2412 return IMMEDIATE_DATA_CANNOT_RECOVER;
2413
2414 rx_size = length;
2415 iov_count = iov_ret;
2416 iov = &cmd->iov_data[0];
2417
2418 padding = ((-length) & 3);
2419 if (padding != 0) {
2420 iov[iov_count].iov_base = cmd->pad_bytes;
2421 iov[iov_count++].iov_len = padding;
2422 rx_size += padding;
2423 }
2424
2425 if (conn->conn_ops->DataDigest) {
2426 iov[iov_count].iov_base = &checksum;
2427 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2428 rx_size += ISCSI_CRC_LEN;
2429 }
2430
2431 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2432
2433 iscsit_unmap_iovec(cmd);
2434
2435 if (rx_got != rx_size) {
2436 iscsit_rx_thread_wait_for_tcp(conn);
2437 return IMMEDIATE_DATA_CANNOT_RECOVER;
2438 }
2439
2440 if (conn->conn_ops->DataDigest) {
2441 u32 data_crc;
2442
2443 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2444 cmd->write_data_done, length, padding,
2445 cmd->pad_bytes);
2446
2447 if (checksum != data_crc) {
2448 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2449 " does not match computed 0x%08x\n", checksum,
2450 data_crc);
2451
2452 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2453 pr_err("Unable to recover from"
2454 " Immediate Data digest failure while"
2455 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002456 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002457 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002458 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002459 return IMMEDIATE_DATA_CANNOT_RECOVER;
2460 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002461 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002462 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002463 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002464 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2465 }
2466 } else {
2467 pr_debug("Got CRC32C DataDigest 0x%08x for"
2468 " %u bytes of Immediate Data\n", checksum,
2469 length);
2470 }
2471 }
2472
2473 cmd->write_data_done += length;
2474
Andy Groverebf1d952012-04-03 15:51:24 -07002475 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002476 spin_lock_bh(&cmd->istate_lock);
2477 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2478 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2479 spin_unlock_bh(&cmd->istate_lock);
2480 }
2481
2482 return IMMEDIATE_DATA_NORMAL_OPERATION;
2483}
2484
2485/*
2486 * Called with sess->conn_lock held.
2487 */
2488/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2489 with active network interface */
2490static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2491{
2492 struct iscsi_cmd *cmd;
2493 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002494 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002495
2496 /*
2497 * Only send a Asynchronous Message on connections whos network
2498 * interface is still functional.
2499 */
2500 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2501 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2502 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002503 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002504 break;
2505 }
2506 }
2507
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002508 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002509 return;
2510
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002511 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002512 if (!cmd) {
2513 iscsit_dec_conn_usage_count(conn_p);
2514 return;
2515 }
2516
2517 cmd->logout_cid = conn->cid;
2518 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2519 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2520
2521 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002522 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 spin_unlock_bh(&conn_p->cmd_lock);
2524
2525 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2526 iscsit_dec_conn_usage_count(conn_p);
2527}
2528
2529static int iscsit_send_conn_drop_async_message(
2530 struct iscsi_cmd *cmd,
2531 struct iscsi_conn *conn)
2532{
2533 struct iscsi_async *hdr;
2534
2535 cmd->tx_size = ISCSI_HDR_LEN;
2536 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2537
2538 hdr = (struct iscsi_async *) cmd->pdu;
2539 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2540 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002541 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002542 cmd->targ_xfer_tag = 0xFFFFFFFF;
2543 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2544 cmd->stat_sn = conn->stat_sn++;
2545 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2546 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2547 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2548 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2549 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2550 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2551 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2552
2553 if (conn->conn_ops->HeaderDigest) {
2554 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2555
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002556 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2557 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002558
2559 cmd->tx_size += ISCSI_CRC_LEN;
2560 pr_debug("Attaching CRC32C HeaderDigest to"
2561 " Async Message 0x%08x\n", *header_digest);
2562 }
2563
2564 cmd->iov_misc[0].iov_base = cmd->pdu;
2565 cmd->iov_misc[0].iov_len = cmd->tx_size;
2566 cmd->iov_misc_count = 1;
2567
2568 pr_debug("Sending Connection Dropped Async Message StatSN:"
2569 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2570 cmd->logout_cid, conn->cid);
2571 return 0;
2572}
2573
Andy Grover6f3c0e62012-04-03 15:51:09 -07002574static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2575{
2576 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2577 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2578 wait_for_completion_interruptible_timeout(
2579 &conn->tx_half_close_comp,
2580 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2581 }
2582}
2583
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002584static void
2585iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2586 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2587 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002588{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002589 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2590 hdr->flags = datain->flags;
2591 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2592 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2593 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2594 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2595 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2596 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2597 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2598 }
2599 }
2600 hton24(hdr->dlength, datain->length);
2601 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2602 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2603 (struct scsi_lun *)&hdr->lun);
2604 else
2605 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2606
2607 hdr->itt = cmd->init_task_tag;
2608
2609 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2610 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2611 else
2612 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2613 if (set_statsn)
2614 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2615 else
2616 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2617
2618 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2619 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2620 hdr->datasn = cpu_to_be32(datain->data_sn);
2621 hdr->offset = cpu_to_be32(datain->offset);
2622
2623 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2624 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2625 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2626 ntohl(hdr->offset), datain->length, conn->cid);
2627}
2628
2629static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2630{
2631 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002632 struct iscsi_datain datain;
2633 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002634 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002635 u32 iov_count = 0, tx_size = 0;
2636 int eodr = 0, ret, iov_ret;
2637 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002638
2639 memset(&datain, 0, sizeof(struct iscsi_datain));
2640 dr = iscsit_get_datain_values(cmd, &datain);
2641 if (!dr) {
2642 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2643 cmd->init_task_tag);
2644 return -1;
2645 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 /*
2647 * Be paranoid and double check the logic for now.
2648 */
Andy Groverebf1d952012-04-03 15:51:24 -07002649 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002650 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2651 " datain.length: %u exceeds cmd->data_length: %u\n",
2652 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002653 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002654 return -1;
2655 }
2656
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002657 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002658 /*
2659 * Special case for successfully execution w/ both DATAIN
2660 * and Sense Data.
2661 */
2662 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2663 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2664 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2665 else {
2666 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2667 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2668 iscsit_increment_maxcmdsn(cmd, conn->sess);
2669 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002670 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002671 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002672 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2673 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002674 }
2675
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002676 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002677
2678 iov = &cmd->iov_data[0];
2679 iov[iov_count].iov_base = cmd->pdu;
2680 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2681 tx_size += ISCSI_HDR_LEN;
2682
2683 if (conn->conn_ops->HeaderDigest) {
2684 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2685
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002686 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2687 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688
2689 iov[0].iov_len += ISCSI_CRC_LEN;
2690 tx_size += ISCSI_CRC_LEN;
2691
2692 pr_debug("Attaching CRC32 HeaderDigest"
2693 " for DataIN PDU 0x%08x\n", *header_digest);
2694 }
2695
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002696 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2697 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002698 if (iov_ret < 0)
2699 return -1;
2700
2701 iov_count += iov_ret;
2702 tx_size += datain.length;
2703
2704 cmd->padding = ((-datain.length) & 3);
2705 if (cmd->padding) {
2706 iov[iov_count].iov_base = cmd->pad_bytes;
2707 iov[iov_count++].iov_len = cmd->padding;
2708 tx_size += cmd->padding;
2709
2710 pr_debug("Attaching %u padding bytes\n",
2711 cmd->padding);
2712 }
2713 if (conn->conn_ops->DataDigest) {
2714 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2715 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2716
2717 iov[iov_count].iov_base = &cmd->data_crc;
2718 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2719 tx_size += ISCSI_CRC_LEN;
2720
2721 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2722 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2723 }
2724
2725 cmd->iov_data_count = iov_count;
2726 cmd->tx_size = tx_size;
2727
Andy Grover6f3c0e62012-04-03 15:51:09 -07002728 /* sendpage is preferred but can't insert markers */
2729 if (!conn->conn_ops->IFMarker)
2730 ret = iscsit_fe_sendpage_sg(cmd, conn);
2731 else
2732 ret = iscsit_send_tx_data(cmd, conn, 0);
2733
2734 iscsit_unmap_iovec(cmd);
2735
2736 if (ret < 0) {
2737 iscsit_tx_thread_wait_for_tcp(conn);
2738 return ret;
2739 }
2740
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002741 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002742 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002743 2 : 1;
2744 iscsit_free_datain_req(cmd, dr);
2745 }
2746
Andy Grover6f3c0e62012-04-03 15:51:09 -07002747 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002748}
2749
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002750int
2751iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2752 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002753{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002754 struct iscsi_conn *logout_conn = NULL;
2755 struct iscsi_conn_recovery *cr = NULL;
2756 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002757 /*
2758 * The actual shutting down of Sessions and/or Connections
2759 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2760 * is done in scsi_logout_post_handler().
2761 */
2762 switch (cmd->logout_reason) {
2763 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2764 pr_debug("iSCSI session logout successful, setting"
2765 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2766 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2767 break;
2768 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2769 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2770 break;
2771 /*
2772 * For CLOSECONNECTION logout requests carrying
2773 * a matching logout CID -> local CID, the reference
2774 * for the local CID will have been incremented in
2775 * iscsi_logout_closeconnection().
2776 *
2777 * For CLOSECONNECTION logout requests carrying
2778 * a different CID than the connection it arrived
2779 * on, the connection responding to cmd->logout_cid
2780 * is stopped in iscsit_logout_post_handler_diffcid().
2781 */
2782
2783 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2784 " successful.\n", cmd->logout_cid, conn->cid);
2785 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2786 break;
2787 case ISCSI_LOGOUT_REASON_RECOVERY:
2788 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2789 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2790 break;
2791 /*
2792 * If the connection is still active from our point of view
2793 * force connection recovery to occur.
2794 */
2795 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2796 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002797 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002798 iscsit_connection_reinstatement_rcfr(logout_conn);
2799 iscsit_dec_conn_usage_count(logout_conn);
2800 }
2801
2802 cr = iscsit_get_inactive_connection_recovery_entry(
2803 conn->sess, cmd->logout_cid);
2804 if (!cr) {
2805 pr_err("Unable to locate CID: %hu for"
2806 " REMOVECONNFORRECOVERY Logout Request.\n",
2807 cmd->logout_cid);
2808 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2809 break;
2810 }
2811
2812 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2813
2814 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2815 " for recovery for CID: %hu on CID: %hu successful.\n",
2816 cmd->logout_cid, conn->cid);
2817 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2818 break;
2819 default:
2820 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2821 cmd->logout_reason);
2822 return -1;
2823 }
2824
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002825 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2826 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2827 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002828 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002829 cmd->stat_sn = conn->stat_sn++;
2830 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2831
2832 iscsit_increment_maxcmdsn(cmd, conn->sess);
2833 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2834 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2835
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002836 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2837 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2838 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2839 cmd->logout_cid, conn->cid);
2840
2841 return 0;
2842}
2843EXPORT_SYMBOL(iscsit_build_logout_rsp);
2844
2845static int
2846iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2847{
2848 struct kvec *iov;
2849 int niov = 0, tx_size, rc;
2850
2851 rc = iscsit_build_logout_rsp(cmd, conn,
2852 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2853 if (rc < 0)
2854 return rc;
2855
2856 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002857 iov = &cmd->iov_misc[0];
2858 iov[niov].iov_base = cmd->pdu;
2859 iov[niov++].iov_len = ISCSI_HDR_LEN;
2860
2861 if (conn->conn_ops->HeaderDigest) {
2862 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2863
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002864 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2865 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002866
2867 iov[0].iov_len += ISCSI_CRC_LEN;
2868 tx_size += ISCSI_CRC_LEN;
2869 pr_debug("Attaching CRC32C HeaderDigest to"
2870 " Logout Response 0x%08x\n", *header_digest);
2871 }
2872 cmd->iov_misc_count = niov;
2873 cmd->tx_size = tx_size;
2874
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002875 return 0;
2876}
2877
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002878void
2879iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2880 struct iscsi_nopin *hdr, bool nopout_response)
2881{
2882 hdr->opcode = ISCSI_OP_NOOP_IN;
2883 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2884 hton24(hdr->dlength, cmd->buf_ptr_size);
2885 if (nopout_response)
2886 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2887 hdr->itt = cmd->init_task_tag;
2888 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2889 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2890 conn->stat_sn;
2891 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2892
2893 if (nopout_response)
2894 iscsit_increment_maxcmdsn(cmd, conn->sess);
2895
2896 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2897 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2898
2899 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2900 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2901 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2902 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2903}
2904EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2905
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002906/*
2907 * Unsolicited NOPIN, either requesting a response or not.
2908 */
2909static int iscsit_send_unsolicited_nopin(
2910 struct iscsi_cmd *cmd,
2911 struct iscsi_conn *conn,
2912 int want_response)
2913{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002914 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2915 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002916
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002917 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002918
2919 if (conn->conn_ops->HeaderDigest) {
2920 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2921
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002922 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2923 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924
2925 tx_size += ISCSI_CRC_LEN;
2926 pr_debug("Attaching CRC32C HeaderDigest to"
2927 " NopIN 0x%08x\n", *header_digest);
2928 }
2929
2930 cmd->iov_misc[0].iov_base = cmd->pdu;
2931 cmd->iov_misc[0].iov_len = tx_size;
2932 cmd->iov_misc_count = 1;
2933 cmd->tx_size = tx_size;
2934
2935 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2936 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2937
Andy Grover6f3c0e62012-04-03 15:51:09 -07002938 ret = iscsit_send_tx_data(cmd, conn, 1);
2939 if (ret < 0) {
2940 iscsit_tx_thread_wait_for_tcp(conn);
2941 return ret;
2942 }
2943
2944 spin_lock_bh(&cmd->istate_lock);
2945 cmd->i_state = want_response ?
2946 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2947 spin_unlock_bh(&cmd->istate_lock);
2948
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002949 return 0;
2950}
2951
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002952static int
2953iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002954{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002955 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002956 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002957 u32 padding = 0;
2958 int niov = 0, tx_size;
2959
2960 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002961
2962 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002963 iov = &cmd->iov_misc[0];
2964 iov[niov].iov_base = cmd->pdu;
2965 iov[niov++].iov_len = ISCSI_HDR_LEN;
2966
2967 if (conn->conn_ops->HeaderDigest) {
2968 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2969
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002970 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2971 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002972
2973 iov[0].iov_len += ISCSI_CRC_LEN;
2974 tx_size += ISCSI_CRC_LEN;
2975 pr_debug("Attaching CRC32C HeaderDigest"
2976 " to NopIn 0x%08x\n", *header_digest);
2977 }
2978
2979 /*
2980 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2981 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2982 */
2983 if (cmd->buf_ptr_size) {
2984 iov[niov].iov_base = cmd->buf_ptr;
2985 iov[niov++].iov_len = cmd->buf_ptr_size;
2986 tx_size += cmd->buf_ptr_size;
2987
2988 pr_debug("Echoing back %u bytes of ping"
2989 " data.\n", cmd->buf_ptr_size);
2990
2991 padding = ((-cmd->buf_ptr_size) & 3);
2992 if (padding != 0) {
2993 iov[niov].iov_base = &cmd->pad_bytes;
2994 iov[niov++].iov_len = padding;
2995 tx_size += padding;
2996 pr_debug("Attaching %u additional"
2997 " padding bytes.\n", padding);
2998 }
2999 if (conn->conn_ops->DataDigest) {
3000 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3001 cmd->buf_ptr, cmd->buf_ptr_size,
3002 padding, (u8 *)&cmd->pad_bytes,
3003 (u8 *)&cmd->data_crc);
3004
3005 iov[niov].iov_base = &cmd->data_crc;
3006 iov[niov++].iov_len = ISCSI_CRC_LEN;
3007 tx_size += ISCSI_CRC_LEN;
3008 pr_debug("Attached DataDigest for %u"
3009 " bytes of ping data, CRC 0x%08x\n",
3010 cmd->buf_ptr_size, cmd->data_crc);
3011 }
3012 }
3013
3014 cmd->iov_misc_count = niov;
3015 cmd->tx_size = tx_size;
3016
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003017 return 0;
3018}
3019
Andy Grover6f3c0e62012-04-03 15:51:09 -07003020static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003021 struct iscsi_cmd *cmd,
3022 struct iscsi_conn *conn)
3023{
3024 int tx_size = 0;
3025 struct iscsi_r2t *r2t;
3026 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003027 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028
3029 r2t = iscsit_get_r2t_from_list(cmd);
3030 if (!r2t)
3031 return -1;
3032
3033 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3034 memset(hdr, 0, ISCSI_HDR_LEN);
3035 hdr->opcode = ISCSI_OP_R2T;
3036 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3037 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3038 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003039 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003040 spin_lock_bh(&conn->sess->ttt_lock);
3041 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3042 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3043 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3044 spin_unlock_bh(&conn->sess->ttt_lock);
3045 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3046 hdr->statsn = cpu_to_be32(conn->stat_sn);
3047 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3048 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3049 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3050 hdr->data_offset = cpu_to_be32(r2t->offset);
3051 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3052
3053 cmd->iov_misc[0].iov_base = cmd->pdu;
3054 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3055 tx_size += ISCSI_HDR_LEN;
3056
3057 if (conn->conn_ops->HeaderDigest) {
3058 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3059
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003060 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3061 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003062
3063 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3064 tx_size += ISCSI_CRC_LEN;
3065 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3066 " PDU 0x%08x\n", *header_digest);
3067 }
3068
3069 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3070 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3071 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3072 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3073 r2t->offset, r2t->xfer_len, conn->cid);
3074
3075 cmd->iov_misc_count = 1;
3076 cmd->tx_size = tx_size;
3077
3078 spin_lock_bh(&cmd->r2t_lock);
3079 r2t->sent_r2t = 1;
3080 spin_unlock_bh(&cmd->r2t_lock);
3081
Andy Grover6f3c0e62012-04-03 15:51:09 -07003082 ret = iscsit_send_tx_data(cmd, conn, 1);
3083 if (ret < 0) {
3084 iscsit_tx_thread_wait_for_tcp(conn);
3085 return ret;
3086 }
3087
3088 spin_lock_bh(&cmd->dataout_timeout_lock);
3089 iscsit_start_dataout_timer(cmd, conn);
3090 spin_unlock_bh(&cmd->dataout_timeout_lock);
3091
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003092 return 0;
3093}
3094
3095/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003096 * @recovery: If called from iscsi_task_reassign_complete_write() for
3097 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003098 */
3099int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003100 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003101 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003102 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003103{
3104 int first_r2t = 1;
3105 u32 offset = 0, xfer_len = 0;
3106
3107 spin_lock_bh(&cmd->r2t_lock);
3108 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3109 spin_unlock_bh(&cmd->r2t_lock);
3110 return 0;
3111 }
3112
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 if (conn->sess->sess_ops->DataSequenceInOrder &&
3114 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003115 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003116
3117 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3118 if (conn->sess->sess_ops->DataSequenceInOrder) {
3119 offset = cmd->r2t_offset;
3120
Andy Grover8b1e1242012-04-03 15:51:12 -07003121 if (first_r2t && recovery) {
3122 int new_data_end = offset +
3123 conn->sess->sess_ops->MaxBurstLength -
3124 cmd->next_burst_len;
3125
Andy Groverebf1d952012-04-03 15:51:24 -07003126 if (new_data_end > cmd->se_cmd.data_length)
3127 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003128 else
3129 xfer_len =
3130 conn->sess->sess_ops->MaxBurstLength -
3131 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003132 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003133 int new_data_end = offset +
3134 conn->sess->sess_ops->MaxBurstLength;
3135
Andy Groverebf1d952012-04-03 15:51:24 -07003136 if (new_data_end > cmd->se_cmd.data_length)
3137 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003138 else
3139 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003140 }
3141 cmd->r2t_offset += xfer_len;
3142
Andy Groverebf1d952012-04-03 15:51:24 -07003143 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003144 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3145 } else {
3146 struct iscsi_seq *seq;
3147
3148 seq = iscsit_get_seq_holder_for_r2t(cmd);
3149 if (!seq) {
3150 spin_unlock_bh(&cmd->r2t_lock);
3151 return -1;
3152 }
3153
3154 offset = seq->offset;
3155 xfer_len = seq->xfer_len;
3156
3157 if (cmd->seq_send_order == cmd->seq_count)
3158 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3159 }
3160 cmd->outstanding_r2ts++;
3161 first_r2t = 0;
3162
3163 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3164 spin_unlock_bh(&cmd->r2t_lock);
3165 return -1;
3166 }
3167
3168 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3169 break;
3170 }
3171 spin_unlock_bh(&cmd->r2t_lock);
3172
3173 return 0;
3174}
3175
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003176void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3177 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003178{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003179 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003180 cmd->stat_sn = conn->stat_sn++;
3181
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003182 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003183
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003184 memset(hdr, 0, ISCSI_HDR_LEN);
3185 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3186 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3187 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3188 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003189 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003190 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3191 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003192 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003193 }
3194 hdr->response = cmd->iscsi_response;
3195 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003196 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3198
3199 iscsit_increment_maxcmdsn(cmd, conn->sess);
3200 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3201 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3202
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003203 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3204 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3205 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3206 cmd->se_cmd.scsi_status, conn->cid);
3207}
3208EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3209
3210static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3211{
3212 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3213 struct kvec *iov;
3214 u32 padding = 0, tx_size = 0;
3215 int iov_count = 0;
3216 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3217
3218 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3219
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003220 iov = &cmd->iov_misc[0];
3221 iov[iov_count].iov_base = cmd->pdu;
3222 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3223 tx_size += ISCSI_HDR_LEN;
3224
3225 /*
3226 * Attach SENSE DATA payload to iSCSI Response PDU
3227 */
3228 if (cmd->se_cmd.sense_buffer &&
3229 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3230 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003231 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3232 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3233
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003234 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003235 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003236 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003237 iov[iov_count++].iov_len =
3238 (cmd->se_cmd.scsi_sense_length + padding);
3239 tx_size += cmd->se_cmd.scsi_sense_length;
3240
3241 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003242 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003243 cmd->se_cmd.scsi_sense_length, 0, padding);
3244 tx_size += padding;
3245 pr_debug("Adding %u bytes of padding to"
3246 " SENSE.\n", padding);
3247 }
3248
3249 if (conn->conn_ops->DataDigest) {
3250 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003251 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003252 (cmd->se_cmd.scsi_sense_length + padding),
3253 0, NULL, (u8 *)&cmd->data_crc);
3254
3255 iov[iov_count].iov_base = &cmd->data_crc;
3256 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3257 tx_size += ISCSI_CRC_LEN;
3258
3259 pr_debug("Attaching CRC32 DataDigest for"
3260 " SENSE, %u bytes CRC 0x%08x\n",
3261 (cmd->se_cmd.scsi_sense_length + padding),
3262 cmd->data_crc);
3263 }
3264
3265 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3266 " Response PDU\n",
3267 cmd->se_cmd.scsi_sense_length);
3268 }
3269
3270 if (conn->conn_ops->HeaderDigest) {
3271 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3272
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003273 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3274 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003275
3276 iov[0].iov_len += ISCSI_CRC_LEN;
3277 tx_size += ISCSI_CRC_LEN;
3278 pr_debug("Attaching CRC32 HeaderDigest for Response"
3279 " PDU 0x%08x\n", *header_digest);
3280 }
3281
3282 cmd->iov_misc_count = iov_count;
3283 cmd->tx_size = tx_size;
3284
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003285 return 0;
3286}
3287
3288static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3289{
3290 switch (se_tmr->response) {
3291 case TMR_FUNCTION_COMPLETE:
3292 return ISCSI_TMF_RSP_COMPLETE;
3293 case TMR_TASK_DOES_NOT_EXIST:
3294 return ISCSI_TMF_RSP_NO_TASK;
3295 case TMR_LUN_DOES_NOT_EXIST:
3296 return ISCSI_TMF_RSP_NO_LUN;
3297 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3298 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003299 case TMR_FUNCTION_REJECTED:
3300 default:
3301 return ISCSI_TMF_RSP_REJECTED;
3302 }
3303}
3304
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003305void
3306iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3307 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003308{
3309 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003310
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003311 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003312 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003313 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003314 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003315 cmd->stat_sn = conn->stat_sn++;
3316 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3317
3318 iscsit_increment_maxcmdsn(cmd, conn->sess);
3319 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3320 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3321
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003322 pr_debug("Built Task Management Response ITT: 0x%08x,"
3323 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3324 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3325}
3326EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3327
3328static int
3329iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3330{
3331 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3332 u32 tx_size = 0;
3333
3334 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3335
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003336 cmd->iov_misc[0].iov_base = cmd->pdu;
3337 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3338 tx_size += ISCSI_HDR_LEN;
3339
3340 if (conn->conn_ops->HeaderDigest) {
3341 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3342
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003343 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3344 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003345
3346 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3347 tx_size += ISCSI_CRC_LEN;
3348 pr_debug("Attaching CRC32 HeaderDigest for Task"
3349 " Mgmt Response PDU 0x%08x\n", *header_digest);
3350 }
3351
3352 cmd->iov_misc_count = 1;
3353 cmd->tx_size = tx_size;
3354
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003355 return 0;
3356}
3357
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003358static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3359{
3360 bool ret = false;
3361
3362 if (np->np_sockaddr.ss_family == AF_INET6) {
3363 const struct sockaddr_in6 sin6 = {
3364 .sin6_addr = IN6ADDR_ANY_INIT };
3365 struct sockaddr_in6 *sock_in6 =
3366 (struct sockaddr_in6 *)&np->np_sockaddr;
3367
3368 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3369 sin6.sin6_addr.s6_addr, 16))
3370 ret = true;
3371 } else {
3372 struct sockaddr_in * sock_in =
3373 (struct sockaddr_in *)&np->np_sockaddr;
3374
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003375 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003376 ret = true;
3377 }
3378
3379 return ret;
3380}
3381
Andy Grover8b1e1242012-04-03 15:51:12 -07003382#define SENDTARGETS_BUF_LIMIT 32768U
3383
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003384static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3385{
3386 char *payload = NULL;
3387 struct iscsi_conn *conn = cmd->conn;
3388 struct iscsi_portal_group *tpg;
3389 struct iscsi_tiqn *tiqn;
3390 struct iscsi_tpg_np *tpg_np;
3391 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003392 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003393 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003394 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003395
Andy Grover8b1e1242012-04-03 15:51:12 -07003396 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3397 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003398
3399 payload = kzalloc(buffer_len, GFP_KERNEL);
3400 if (!payload) {
3401 pr_err("Unable to allocate memory for sendtargets"
3402 " response.\n");
3403 return -ENOMEM;
3404 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003405 /*
3406 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3407 * explicit case..
3408 */
3409 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3410 text_ptr = strchr(text_in, '=');
3411 if (!text_ptr) {
3412 pr_err("Unable to locate '=' string in text_in:"
3413 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003414 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003415 return -EINVAL;
3416 }
3417 /*
3418 * Skip over '=' character..
3419 */
3420 text_ptr += 1;
3421 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003422
3423 spin_lock(&tiqn_lock);
3424 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003425 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3426 strcmp(tiqn->tiqn, text_ptr)) {
3427 continue;
3428 }
3429
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003430 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003431
3432 spin_lock(&tiqn->tiqn_tpg_lock);
3433 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3434
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003435 /* If demo_mode_discovery=0 and generate_node_acls=0
3436 * (demo mode dislabed) do not return
3437 * TargetName+TargetAddress unless a NodeACL exists.
3438 */
3439
3440 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3441 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3442 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3443 cmd->conn->sess->sess_ops->InitiatorName))) {
3444 continue;
3445 }
3446
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003447 spin_lock(&tpg->tpg_state_lock);
3448 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3449 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3450 spin_unlock(&tpg->tpg_state_lock);
3451 continue;
3452 }
3453 spin_unlock(&tpg->tpg_state_lock);
3454
3455 spin_lock(&tpg->tpg_np_lock);
3456 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3457 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003458 struct iscsi_np *np = tpg_np->tpg_np;
3459 bool inaddr_any = iscsit_check_inaddr_any(np);
3460
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003461 if (!target_name_printed) {
3462 len = sprintf(buf, "TargetName=%s",
3463 tiqn->tiqn);
3464 len += 1;
3465
3466 if ((len + payload_len) > buffer_len) {
3467 spin_unlock(&tpg->tpg_np_lock);
3468 spin_unlock(&tiqn->tiqn_tpg_lock);
3469 end_of_buf = 1;
3470 goto eob;
3471 }
3472 memcpy(payload + payload_len, buf, len);
3473 payload_len += len;
3474 target_name_printed = 1;
3475 }
3476
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003477 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003478 "%s:%hu,%hu",
3479 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003480 np->np_ip : conn->local_ip,
Chris Leechdfecf612013-08-12 11:26:28 -07003481 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003482 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003483 tpg->tpgt);
3484 len += 1;
3485
3486 if ((len + payload_len) > buffer_len) {
3487 spin_unlock(&tpg->tpg_np_lock);
3488 spin_unlock(&tiqn->tiqn_tpg_lock);
3489 end_of_buf = 1;
3490 goto eob;
3491 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003492 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003493 payload_len += len;
3494 }
3495 spin_unlock(&tpg->tpg_np_lock);
3496 }
3497 spin_unlock(&tiqn->tiqn_tpg_lock);
3498eob:
3499 if (end_of_buf)
3500 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003501
3502 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3503 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003504 }
3505 spin_unlock(&tiqn_lock);
3506
3507 cmd->buf_ptr = payload;
3508
3509 return payload_len;
3510}
3511
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003512int
3513iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3514 struct iscsi_text_rsp *hdr)
3515{
3516 int text_length, padding;
3517
3518 text_length = iscsit_build_sendtargets_response(cmd);
3519 if (text_length < 0)
3520 return text_length;
3521
3522 hdr->opcode = ISCSI_OP_TEXT_RSP;
3523 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3524 padding = ((-text_length) & 3);
3525 hton24(hdr->dlength, text_length);
3526 hdr->itt = cmd->init_task_tag;
3527 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3528 cmd->stat_sn = conn->stat_sn++;
3529 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3530
3531 iscsit_increment_maxcmdsn(cmd, conn->sess);
3532 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3533 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3534
3535 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3536 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3537 text_length, conn->cid);
3538
3539 return text_length + padding;
3540}
3541EXPORT_SYMBOL(iscsit_build_text_rsp);
3542
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003543/*
3544 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3545 * MaxRecvDataSegmentLength.
3546 */
3547static int iscsit_send_text_rsp(
3548 struct iscsi_cmd *cmd,
3549 struct iscsi_conn *conn)
3550{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003551 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003552 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003553 u32 tx_size = 0;
3554 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003555
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003556 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3557 if (rc < 0)
3558 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003559
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003560 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003561 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003562 iov[iov_count].iov_base = cmd->pdu;
3563 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3564 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003565 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003566
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003567 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003568
3569 if (conn->conn_ops->HeaderDigest) {
3570 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3571
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003572 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3573 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003574
3575 iov[0].iov_len += ISCSI_CRC_LEN;
3576 tx_size += ISCSI_CRC_LEN;
3577 pr_debug("Attaching CRC32 HeaderDigest for"
3578 " Text Response PDU 0x%08x\n", *header_digest);
3579 }
3580
3581 if (conn->conn_ops->DataDigest) {
3582 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003583 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003584 0, NULL, (u8 *)&cmd->data_crc);
3585
3586 iov[iov_count].iov_base = &cmd->data_crc;
3587 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3588 tx_size += ISCSI_CRC_LEN;
3589
3590 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003591 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003592 cmd->data_crc);
3593 }
3594
3595 cmd->iov_misc_count = iov_count;
3596 cmd->tx_size = tx_size;
3597
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003598 return 0;
3599}
3600
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003601void
3602iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3603 struct iscsi_reject *hdr)
3604{
3605 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003606 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003607 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3608 hton24(hdr->dlength, ISCSI_HDR_LEN);
3609 hdr->ffffffff = cpu_to_be32(0xffffffff);
3610 cmd->stat_sn = conn->stat_sn++;
3611 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3612 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3613 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3614
3615}
3616EXPORT_SYMBOL(iscsit_build_reject);
3617
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003618static int iscsit_send_reject(
3619 struct iscsi_cmd *cmd,
3620 struct iscsi_conn *conn)
3621{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003622 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003623 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003624 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003625
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003626 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003627
3628 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003629 iov[iov_count].iov_base = cmd->pdu;
3630 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3631 iov[iov_count].iov_base = cmd->buf_ptr;
3632 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3633
3634 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3635
3636 if (conn->conn_ops->HeaderDigest) {
3637 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3638
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003639 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3640 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003641
3642 iov[0].iov_len += ISCSI_CRC_LEN;
3643 tx_size += ISCSI_CRC_LEN;
3644 pr_debug("Attaching CRC32 HeaderDigest for"
3645 " REJECT PDU 0x%08x\n", *header_digest);
3646 }
3647
3648 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003649 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3650 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003651
3652 iov[iov_count].iov_base = &cmd->data_crc;
3653 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3654 tx_size += ISCSI_CRC_LEN;
3655 pr_debug("Attaching CRC32 DataDigest for REJECT"
3656 " PDU 0x%08x\n", cmd->data_crc);
3657 }
3658
3659 cmd->iov_misc_count = iov_count;
3660 cmd->tx_size = tx_size;
3661
3662 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3663 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3664
3665 return 0;
3666}
3667
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003668void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3669{
3670 struct iscsi_thread_set *ts = conn->thread_set;
3671 int ord, cpu;
3672 /*
3673 * thread_id is assigned from iscsit_global->ts_bitmap from
3674 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3675 *
3676 * Here we use thread_id to determine which CPU that this
3677 * iSCSI connection's iscsi_thread_set will be scheduled to
3678 * execute upon.
3679 */
3680 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003681 for_each_online_cpu(cpu) {
3682 if (ord-- == 0) {
3683 cpumask_set_cpu(cpu, conn->conn_cpumask);
3684 return;
3685 }
3686 }
3687 /*
3688 * This should never be reached..
3689 */
3690 dump_stack();
3691 cpumask_setall(conn->conn_cpumask);
3692}
3693
3694static inline void iscsit_thread_check_cpumask(
3695 struct iscsi_conn *conn,
3696 struct task_struct *p,
3697 int mode)
3698{
3699 char buf[128];
3700 /*
3701 * mode == 1 signals iscsi_target_tx_thread() usage.
3702 * mode == 0 signals iscsi_target_rx_thread() usage.
3703 */
3704 if (mode == 1) {
3705 if (!conn->conn_tx_reset_cpumask)
3706 return;
3707 conn->conn_tx_reset_cpumask = 0;
3708 } else {
3709 if (!conn->conn_rx_reset_cpumask)
3710 return;
3711 conn->conn_rx_reset_cpumask = 0;
3712 }
3713 /*
3714 * Update the CPU mask for this single kthread so that
3715 * both TX and RX kthreads are scheduled to run on the
3716 * same CPU.
3717 */
3718 memset(buf, 0, 128);
3719 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003720 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3721}
3722
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003723static int
3724iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003725{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003726 int ret;
3727
3728 switch (state) {
3729 case ISTATE_SEND_R2T:
3730 ret = iscsit_send_r2t(cmd, conn);
3731 if (ret < 0)
3732 goto err;
3733 break;
3734 case ISTATE_REMOVE:
3735 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003736 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003737 spin_unlock_bh(&conn->cmd_lock);
3738
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003739 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003740 break;
3741 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3742 iscsit_mod_nopin_response_timer(conn);
3743 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3744 if (ret < 0)
3745 goto err;
3746 break;
3747 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3748 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3749 if (ret < 0)
3750 goto err;
3751 break;
3752 default:
3753 pr_err("Unknown Opcode: 0x%02x ITT:"
3754 " 0x%08x, i_state: %d on CID: %hu\n",
3755 cmd->iscsi_opcode, cmd->init_task_tag, state,
3756 conn->cid);
3757 goto err;
3758 }
3759
3760 return 0;
3761
3762err:
3763 return -1;
3764}
3765
3766static int
3767iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3768{
3769 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003770 struct iscsi_queue_req *qr;
3771 struct iscsi_cmd *cmd;
3772 u8 state;
3773 int ret;
3774
3775 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3776 atomic_set(&conn->check_immediate_queue, 0);
3777 cmd = qr->cmd;
3778 state = qr->state;
3779 kmem_cache_free(lio_qr_cache, qr);
3780
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003781 ret = t->iscsit_immediate_queue(conn, cmd, state);
3782 if (ret < 0)
3783 return ret;
3784 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003785
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003786 return 0;
3787}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003788
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003789static int
3790iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3791{
3792 int ret;
3793
3794check_rsp_state:
3795 switch (state) {
3796 case ISTATE_SEND_DATAIN:
3797 ret = iscsit_send_datain(cmd, conn);
3798 if (ret < 0)
3799 goto err;
3800 else if (!ret)
3801 /* more drs */
3802 goto check_rsp_state;
3803 else if (ret == 1) {
3804 /* all done */
3805 spin_lock_bh(&cmd->istate_lock);
3806 cmd->i_state = ISTATE_SENT_STATUS;
3807 spin_unlock_bh(&cmd->istate_lock);
3808
3809 if (atomic_read(&conn->check_immediate_queue))
3810 return 1;
3811
3812 return 0;
3813 } else if (ret == 2) {
3814 /* Still must send status,
3815 SCF_TRANSPORT_TASK_SENSE was set */
3816 spin_lock_bh(&cmd->istate_lock);
3817 cmd->i_state = ISTATE_SEND_STATUS;
3818 spin_unlock_bh(&cmd->istate_lock);
3819 state = ISTATE_SEND_STATUS;
3820 goto check_rsp_state;
3821 }
3822
3823 break;
3824 case ISTATE_SEND_STATUS:
3825 case ISTATE_SEND_STATUS_RECOVERY:
3826 ret = iscsit_send_response(cmd, conn);
3827 break;
3828 case ISTATE_SEND_LOGOUTRSP:
3829 ret = iscsit_send_logout(cmd, conn);
3830 break;
3831 case ISTATE_SEND_ASYNCMSG:
3832 ret = iscsit_send_conn_drop_async_message(
3833 cmd, conn);
3834 break;
3835 case ISTATE_SEND_NOPIN:
3836 ret = iscsit_send_nopin(cmd, conn);
3837 break;
3838 case ISTATE_SEND_REJECT:
3839 ret = iscsit_send_reject(cmd, conn);
3840 break;
3841 case ISTATE_SEND_TASKMGTRSP:
3842 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3843 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003844 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003845 ret = iscsit_tmr_post_handler(cmd, conn);
3846 if (ret != 0)
3847 iscsit_fall_back_to_erl0(conn->sess);
3848 break;
3849 case ISTATE_SEND_TEXTRSP:
3850 ret = iscsit_send_text_rsp(cmd, conn);
3851 break;
3852 default:
3853 pr_err("Unknown Opcode: 0x%02x ITT:"
3854 " 0x%08x, i_state: %d on CID: %hu\n",
3855 cmd->iscsi_opcode, cmd->init_task_tag,
3856 state, conn->cid);
3857 goto err;
3858 }
3859 if (ret < 0)
3860 goto err;
3861
3862 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3863 iscsit_tx_thread_wait_for_tcp(conn);
3864 iscsit_unmap_iovec(cmd);
3865 goto err;
3866 }
3867 iscsit_unmap_iovec(cmd);
3868
3869 switch (state) {
3870 case ISTATE_SEND_LOGOUTRSP:
3871 if (!iscsit_logout_post_handler(cmd, conn))
3872 goto restart;
3873 /* fall through */
3874 case ISTATE_SEND_STATUS:
3875 case ISTATE_SEND_ASYNCMSG:
3876 case ISTATE_SEND_NOPIN:
3877 case ISTATE_SEND_STATUS_RECOVERY:
3878 case ISTATE_SEND_TEXTRSP:
3879 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003880 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003881 spin_lock_bh(&cmd->istate_lock);
3882 cmd->i_state = ISTATE_SENT_STATUS;
3883 spin_unlock_bh(&cmd->istate_lock);
3884 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003885 default:
3886 pr_err("Unknown Opcode: 0x%02x ITT:"
3887 " 0x%08x, i_state: %d on CID: %hu\n",
3888 cmd->iscsi_opcode, cmd->init_task_tag,
3889 cmd->i_state, conn->cid);
3890 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003891 }
3892
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003893 if (atomic_read(&conn->check_immediate_queue))
3894 return 1;
3895
Andy Grover6f3c0e62012-04-03 15:51:09 -07003896 return 0;
3897
3898err:
3899 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003900restart:
3901 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003902}
3903
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003904static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003905{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003906 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003907 struct iscsi_queue_req *qr;
3908 struct iscsi_cmd *cmd;
3909 u8 state;
3910 int ret;
3911
3912 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3913 cmd = qr->cmd;
3914 state = qr->state;
3915 kmem_cache_free(lio_qr_cache, qr);
3916
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003917 ret = t->iscsit_response_queue(conn, cmd, state);
3918 if (ret == 1 || ret < 0)
3919 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003920 }
3921
3922 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003923}
3924
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003925int iscsi_target_tx_thread(void *arg)
3926{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003928 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003929 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003930 /*
3931 * Allow ourselves to be interrupted by SIGINT so that a
3932 * connection recovery / failure event can be triggered externally.
3933 */
3934 allow_signal(SIGINT);
3935
3936restart:
3937 conn = iscsi_tx_thread_pre_handler(ts);
3938 if (!conn)
3939 goto out;
3940
Andy Grover6f3c0e62012-04-03 15:51:09 -07003941 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003942
3943 while (!kthread_should_stop()) {
3944 /*
3945 * Ensure that both TX and RX per connection kthreads
3946 * are scheduled to run on the same CPU.
3947 */
3948 iscsit_thread_check_cpumask(conn, current, 1);
3949
Roland Dreierd5627ac2012-10-31 09:16:46 -07003950 wait_event_interruptible(conn->queues_wq,
3951 !iscsit_conn_all_queues_empty(conn) ||
3952 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003953
3954 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3955 signal_pending(current))
3956 goto transport_err;
3957
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003958get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003959 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003960 if (ret < 0)
3961 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003962
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003963 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003964 if (ret == 1)
3965 goto get_immediate;
3966 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003967 goto restart;
3968 else if (ret < 0)
3969 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003970 }
3971
3972transport_err:
3973 iscsit_take_action_for_connection_exit(conn);
3974 goto restart;
3975out:
3976 return 0;
3977}
3978
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003979static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3980{
3981 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3982 struct iscsi_cmd *cmd;
3983 int ret = 0;
3984
3985 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3986 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003987 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003988 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003989 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003990
3991 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3992 break;
3993 case ISCSI_OP_SCSI_DATA_OUT:
3994 ret = iscsit_handle_data_out(conn, buf);
3995 break;
3996 case ISCSI_OP_NOOP_OUT:
3997 cmd = NULL;
3998 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003999 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004000 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004001 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004002 }
4003 ret = iscsit_handle_nop_out(conn, cmd, buf);
4004 break;
4005 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004006 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004007 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004008 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004009
4010 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4011 break;
4012 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004013 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004014 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004015 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004016
4017 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004018 break;
4019 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004020 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004021 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004022 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004023
4024 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4025 if (ret > 0)
4026 wait_for_completion_timeout(&conn->conn_logout_comp,
4027 SECONDS_FOR_LOGOUT_COMP * HZ);
4028 break;
4029 case ISCSI_OP_SNACK:
4030 ret = iscsit_handle_snack(conn, buf);
4031 break;
4032 default:
4033 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4034 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4035 pr_err("Cannot recover from unknown"
4036 " opcode while ERL=0, closing iSCSI connection.\n");
4037 return -1;
4038 }
4039 if (!conn->conn_ops->OFMarker) {
4040 pr_err("Unable to recover from unknown"
4041 " opcode while OFMarker=No, closing iSCSI"
4042 " connection.\n");
4043 return -1;
4044 }
4045 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4046 pr_err("Unable to recover from unknown"
4047 " opcode, closing iSCSI connection.\n");
4048 return -1;
4049 }
4050 break;
4051 }
4052
4053 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004054reject:
4055 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004056}
4057
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004058int iscsi_target_rx_thread(void *arg)
4059{
4060 int ret;
4061 u8 buffer[ISCSI_HDR_LEN], opcode;
4062 u32 checksum = 0, digest = 0;
4063 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004064 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004065 struct kvec iov;
4066 /*
4067 * Allow ourselves to be interrupted by SIGINT so that a
4068 * connection recovery / failure event can be triggered externally.
4069 */
4070 allow_signal(SIGINT);
4071
4072restart:
4073 conn = iscsi_rx_thread_pre_handler(ts);
4074 if (!conn)
4075 goto out;
4076
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004077 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4078 struct completion comp;
4079 int rc;
4080
4081 init_completion(&comp);
4082 rc = wait_for_completion_interruptible(&comp);
4083 if (rc < 0)
4084 goto transport_err;
4085
4086 goto out;
4087 }
4088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004089 while (!kthread_should_stop()) {
4090 /*
4091 * Ensure that both TX and RX per connection kthreads
4092 * are scheduled to run on the same CPU.
4093 */
4094 iscsit_thread_check_cpumask(conn, current, 0);
4095
4096 memset(buffer, 0, ISCSI_HDR_LEN);
4097 memset(&iov, 0, sizeof(struct kvec));
4098
4099 iov.iov_base = buffer;
4100 iov.iov_len = ISCSI_HDR_LEN;
4101
4102 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4103 if (ret != ISCSI_HDR_LEN) {
4104 iscsit_rx_thread_wait_for_tcp(conn);
4105 goto transport_err;
4106 }
4107
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004108 if (conn->conn_ops->HeaderDigest) {
4109 iov.iov_base = &digest;
4110 iov.iov_len = ISCSI_CRC_LEN;
4111
4112 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4113 if (ret != ISCSI_CRC_LEN) {
4114 iscsit_rx_thread_wait_for_tcp(conn);
4115 goto transport_err;
4116 }
4117
4118 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4119 buffer, ISCSI_HDR_LEN,
4120 0, NULL, (u8 *)&checksum);
4121
4122 if (digest != checksum) {
4123 pr_err("HeaderDigest CRC32C failed,"
4124 " received 0x%08x, computed 0x%08x\n",
4125 digest, checksum);
4126 /*
4127 * Set the PDU to 0xff so it will intentionally
4128 * hit default in the switch below.
4129 */
4130 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004131 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004132 } else {
4133 pr_debug("Got HeaderDigest CRC32C"
4134 " 0x%08x\n", checksum);
4135 }
4136 }
4137
4138 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4139 goto transport_err;
4140
4141 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4142
4143 if (conn->sess->sess_ops->SessionType &&
4144 ((!(opcode & ISCSI_OP_TEXT)) ||
4145 (!(opcode & ISCSI_OP_LOGOUT)))) {
4146 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4147 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004148 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4149 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004150 goto transport_err;
4151 }
4152
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004153 ret = iscsi_target_rx_opcode(conn, buffer);
4154 if (ret < 0)
4155 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004156 }
4157
4158transport_err:
4159 if (!signal_pending(current))
4160 atomic_set(&conn->transport_failed, 1);
4161 iscsit_take_action_for_connection_exit(conn);
4162 goto restart;
4163out:
4164 return 0;
4165}
4166
4167static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4168{
4169 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4170 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004171 /*
4172 * We expect this function to only ever be called from either RX or TX
4173 * thread context via iscsit_close_connection() once the other context
4174 * has been reset -> returned sleeping pre-handler state.
4175 */
4176 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004177 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004178
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004179 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004180 spin_unlock_bh(&conn->cmd_lock);
4181
4182 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004183
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004184 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004185
4186 spin_lock_bh(&conn->cmd_lock);
4187 }
4188 spin_unlock_bh(&conn->cmd_lock);
4189}
4190
4191static void iscsit_stop_timers_for_cmds(
4192 struct iscsi_conn *conn)
4193{
4194 struct iscsi_cmd *cmd;
4195
4196 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004197 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004198 if (cmd->data_direction == DMA_TO_DEVICE)
4199 iscsit_stop_dataout_timer(cmd);
4200 }
4201 spin_unlock_bh(&conn->cmd_lock);
4202}
4203
4204int iscsit_close_connection(
4205 struct iscsi_conn *conn)
4206{
4207 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4208 struct iscsi_session *sess = conn->sess;
4209
4210 pr_debug("Closing iSCSI connection CID %hu on SID:"
4211 " %u\n", conn->cid, sess->sid);
4212 /*
4213 * Always up conn_logout_comp just in case the RX Thread is sleeping
4214 * and the logout response never got sent because the connection
4215 * failed.
4216 */
4217 complete(&conn->conn_logout_comp);
4218
4219 iscsi_release_thread_set(conn);
4220
4221 iscsit_stop_timers_for_cmds(conn);
4222 iscsit_stop_nopin_response_timer(conn);
4223 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004224
4225 if (conn->conn_transport->iscsit_wait_conn)
4226 conn->conn_transport->iscsit_wait_conn(conn);
4227
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004228 iscsit_free_queue_reqs_for_conn(conn);
4229
4230 /*
4231 * During Connection recovery drop unacknowledged out of order
4232 * commands for this connection, and prepare the other commands
4233 * for realligence.
4234 *
4235 * During normal operation clear the out of order commands (but
4236 * do not free the struct iscsi_ooo_cmdsn's) and release all
4237 * struct iscsi_cmds.
4238 */
4239 if (atomic_read(&conn->connection_recovery)) {
4240 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4241 iscsit_prepare_cmds_for_realligance(conn);
4242 } else {
4243 iscsit_clear_ooo_cmdsns_for_conn(conn);
4244 iscsit_release_commands_from_conn(conn);
4245 }
4246
4247 /*
4248 * Handle decrementing session or connection usage count if
4249 * a logout response was not able to be sent because the
4250 * connection failed. Fall back to Session Recovery here.
4251 */
4252 if (atomic_read(&conn->conn_logout_remove)) {
4253 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4254 iscsit_dec_conn_usage_count(conn);
4255 iscsit_dec_session_usage_count(sess);
4256 }
4257 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4258 iscsit_dec_conn_usage_count(conn);
4259
4260 atomic_set(&conn->conn_logout_remove, 0);
4261 atomic_set(&sess->session_reinstatement, 0);
4262 atomic_set(&sess->session_fall_back_to_erl0, 1);
4263 }
4264
4265 spin_lock_bh(&sess->conn_lock);
4266 list_del(&conn->conn_list);
4267
4268 /*
4269 * Attempt to let the Initiator know this connection failed by
4270 * sending an Connection Dropped Async Message on another
4271 * active connection.
4272 */
4273 if (atomic_read(&conn->connection_recovery))
4274 iscsit_build_conn_drop_async_message(conn);
4275
4276 spin_unlock_bh(&sess->conn_lock);
4277
4278 /*
4279 * If connection reinstatement is being performed on this connection,
4280 * up the connection reinstatement semaphore that is being blocked on
4281 * in iscsit_cause_connection_reinstatement().
4282 */
4283 spin_lock_bh(&conn->state_lock);
4284 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4285 spin_unlock_bh(&conn->state_lock);
4286 complete(&conn->conn_wait_comp);
4287 wait_for_completion(&conn->conn_post_wait_comp);
4288 spin_lock_bh(&conn->state_lock);
4289 }
4290
4291 /*
4292 * If connection reinstatement is being performed on this connection
4293 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4294 * connection wait rcfr semaphore that is being blocked on
4295 * an iscsit_connection_reinstatement_rcfr().
4296 */
4297 if (atomic_read(&conn->connection_wait_rcfr)) {
4298 spin_unlock_bh(&conn->state_lock);
4299 complete(&conn->conn_wait_rcfr_comp);
4300 wait_for_completion(&conn->conn_post_wait_comp);
4301 spin_lock_bh(&conn->state_lock);
4302 }
4303 atomic_set(&conn->connection_reinstatement, 1);
4304 spin_unlock_bh(&conn->state_lock);
4305
4306 /*
4307 * If any other processes are accessing this connection pointer we
4308 * must wait until they have completed.
4309 */
4310 iscsit_check_conn_usage_count(conn);
4311
4312 if (conn->conn_rx_hash.tfm)
4313 crypto_free_hash(conn->conn_rx_hash.tfm);
4314 if (conn->conn_tx_hash.tfm)
4315 crypto_free_hash(conn->conn_tx_hash.tfm);
4316
4317 if (conn->conn_cpumask)
4318 free_cpumask_var(conn->conn_cpumask);
4319
4320 kfree(conn->conn_ops);
4321 conn->conn_ops = NULL;
4322
Al Virobf6932f2012-07-21 08:55:18 +01004323 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004324 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004325
4326 if (conn->conn_transport->iscsit_free_conn)
4327 conn->conn_transport->iscsit_free_conn(conn);
4328
4329 iscsit_put_transport(conn->conn_transport);
4330
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004331 conn->thread_set = NULL;
4332
4333 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4334 conn->conn_state = TARG_CONN_STATE_FREE;
4335 kfree(conn);
4336
4337 spin_lock_bh(&sess->conn_lock);
4338 atomic_dec(&sess->nconn);
4339 pr_debug("Decremented iSCSI connection count to %hu from node:"
4340 " %s\n", atomic_read(&sess->nconn),
4341 sess->sess_ops->InitiatorName);
4342 /*
4343 * Make sure that if one connection fails in an non ERL=2 iSCSI
4344 * Session that they all fail.
4345 */
4346 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4347 !atomic_read(&sess->session_logout))
4348 atomic_set(&sess->session_fall_back_to_erl0, 1);
4349
4350 /*
4351 * If this was not the last connection in the session, and we are
4352 * performing session reinstatement or falling back to ERL=0, call
4353 * iscsit_stop_session() without sleeping to shutdown the other
4354 * active connections.
4355 */
4356 if (atomic_read(&sess->nconn)) {
4357 if (!atomic_read(&sess->session_reinstatement) &&
4358 !atomic_read(&sess->session_fall_back_to_erl0)) {
4359 spin_unlock_bh(&sess->conn_lock);
4360 return 0;
4361 }
4362 if (!atomic_read(&sess->session_stop_active)) {
4363 atomic_set(&sess->session_stop_active, 1);
4364 spin_unlock_bh(&sess->conn_lock);
4365 iscsit_stop_session(sess, 0, 0);
4366 return 0;
4367 }
4368 spin_unlock_bh(&sess->conn_lock);
4369 return 0;
4370 }
4371
4372 /*
4373 * If this was the last connection in the session and one of the
4374 * following is occurring:
4375 *
4376 * Session Reinstatement is not being performed, and are falling back
4377 * to ERL=0 call iscsit_close_session().
4378 *
4379 * Session Logout was requested. iscsit_close_session() will be called
4380 * elsewhere.
4381 *
4382 * Session Continuation is not being performed, start the Time2Retain
4383 * handler and check if sleep_on_sess_wait_sem is active.
4384 */
4385 if (!atomic_read(&sess->session_reinstatement) &&
4386 atomic_read(&sess->session_fall_back_to_erl0)) {
4387 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004388 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004389
4390 return 0;
4391 } else if (atomic_read(&sess->session_logout)) {
4392 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4393 sess->session_state = TARG_SESS_STATE_FREE;
4394 spin_unlock_bh(&sess->conn_lock);
4395
4396 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4397 complete(&sess->session_wait_comp);
4398
4399 return 0;
4400 } else {
4401 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4402 sess->session_state = TARG_SESS_STATE_FAILED;
4403
4404 if (!atomic_read(&sess->session_continuation)) {
4405 spin_unlock_bh(&sess->conn_lock);
4406 iscsit_start_time2retain_handler(sess);
4407 } else
4408 spin_unlock_bh(&sess->conn_lock);
4409
4410 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4411 complete(&sess->session_wait_comp);
4412
4413 return 0;
4414 }
4415 spin_unlock_bh(&sess->conn_lock);
4416
4417 return 0;
4418}
4419
4420int iscsit_close_session(struct iscsi_session *sess)
4421{
Andy Grover60bfcf82013-10-09 11:05:58 -07004422 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004423 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4424
4425 if (atomic_read(&sess->nconn)) {
4426 pr_err("%d connection(s) still exist for iSCSI session"
4427 " to %s\n", atomic_read(&sess->nconn),
4428 sess->sess_ops->InitiatorName);
4429 BUG();
4430 }
4431
4432 spin_lock_bh(&se_tpg->session_lock);
4433 atomic_set(&sess->session_logout, 1);
4434 atomic_set(&sess->session_reinstatement, 1);
4435 iscsit_stop_time2retain_timer(sess);
4436 spin_unlock_bh(&se_tpg->session_lock);
4437
4438 /*
4439 * transport_deregister_session_configfs() will clear the
4440 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4441 * can be setting it again with __transport_register_session() in
4442 * iscsi_post_login_handler() again after the iscsit_stop_session()
4443 * completes in iscsi_np context.
4444 */
4445 transport_deregister_session_configfs(sess->se_sess);
4446
4447 /*
4448 * If any other processes are accessing this session pointer we must
4449 * wait until they have completed. If we are in an interrupt (the
4450 * time2retain handler) and contain and active session usage count we
4451 * restart the timer and exit.
4452 */
4453 if (!in_interrupt()) {
4454 if (iscsit_check_session_usage_count(sess) == 1)
4455 iscsit_stop_session(sess, 1, 1);
4456 } else {
4457 if (iscsit_check_session_usage_count(sess) == 2) {
4458 atomic_set(&sess->session_logout, 0);
4459 iscsit_start_time2retain_handler(sess);
4460 return 0;
4461 }
4462 }
4463
4464 transport_deregister_session(sess->se_sess);
4465
4466 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4467 iscsit_free_connection_recovery_entires(sess);
4468
4469 iscsit_free_all_ooo_cmdsns(sess);
4470
4471 spin_lock_bh(&se_tpg->session_lock);
4472 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4473 sess->session_state = TARG_SESS_STATE_FREE;
4474 pr_debug("Released iSCSI session from node: %s\n",
4475 sess->sess_ops->InitiatorName);
4476 tpg->nsessions--;
4477 if (tpg->tpg_tiqn)
4478 tpg->tpg_tiqn->tiqn_nsessions--;
4479
4480 pr_debug("Decremented number of active iSCSI Sessions on"
4481 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4482
4483 spin_lock(&sess_idr_lock);
4484 idr_remove(&sess_idr, sess->session_index);
4485 spin_unlock(&sess_idr_lock);
4486
4487 kfree(sess->sess_ops);
4488 sess->sess_ops = NULL;
4489 spin_unlock_bh(&se_tpg->session_lock);
4490
4491 kfree(sess);
4492 return 0;
4493}
4494
4495static void iscsit_logout_post_handler_closesession(
4496 struct iscsi_conn *conn)
4497{
4498 struct iscsi_session *sess = conn->sess;
4499
4500 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4501 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4502
4503 atomic_set(&conn->conn_logout_remove, 0);
4504 complete(&conn->conn_logout_comp);
4505
4506 iscsit_dec_conn_usage_count(conn);
4507 iscsit_stop_session(sess, 1, 1);
4508 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004509 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004510}
4511
4512static void iscsit_logout_post_handler_samecid(
4513 struct iscsi_conn *conn)
4514{
4515 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4516 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4517
4518 atomic_set(&conn->conn_logout_remove, 0);
4519 complete(&conn->conn_logout_comp);
4520
4521 iscsit_cause_connection_reinstatement(conn, 1);
4522 iscsit_dec_conn_usage_count(conn);
4523}
4524
4525static void iscsit_logout_post_handler_diffcid(
4526 struct iscsi_conn *conn,
4527 u16 cid)
4528{
4529 struct iscsi_conn *l_conn;
4530 struct iscsi_session *sess = conn->sess;
4531
4532 if (!sess)
4533 return;
4534
4535 spin_lock_bh(&sess->conn_lock);
4536 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4537 if (l_conn->cid == cid) {
4538 iscsit_inc_conn_usage_count(l_conn);
4539 break;
4540 }
4541 }
4542 spin_unlock_bh(&sess->conn_lock);
4543
4544 if (!l_conn)
4545 return;
4546
4547 if (l_conn->sock)
4548 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4549
4550 spin_lock_bh(&l_conn->state_lock);
4551 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4552 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4553 spin_unlock_bh(&l_conn->state_lock);
4554
4555 iscsit_cause_connection_reinstatement(l_conn, 1);
4556 iscsit_dec_conn_usage_count(l_conn);
4557}
4558
4559/*
4560 * Return of 0 causes the TX thread to restart.
4561 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004562int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004563 struct iscsi_cmd *cmd,
4564 struct iscsi_conn *conn)
4565{
4566 int ret = 0;
4567
4568 switch (cmd->logout_reason) {
4569 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4570 switch (cmd->logout_response) {
4571 case ISCSI_LOGOUT_SUCCESS:
4572 case ISCSI_LOGOUT_CLEANUP_FAILED:
4573 default:
4574 iscsit_logout_post_handler_closesession(conn);
4575 break;
4576 }
4577 ret = 0;
4578 break;
4579 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4580 if (conn->cid == cmd->logout_cid) {
4581 switch (cmd->logout_response) {
4582 case ISCSI_LOGOUT_SUCCESS:
4583 case ISCSI_LOGOUT_CLEANUP_FAILED:
4584 default:
4585 iscsit_logout_post_handler_samecid(conn);
4586 break;
4587 }
4588 ret = 0;
4589 } else {
4590 switch (cmd->logout_response) {
4591 case ISCSI_LOGOUT_SUCCESS:
4592 iscsit_logout_post_handler_diffcid(conn,
4593 cmd->logout_cid);
4594 break;
4595 case ISCSI_LOGOUT_CID_NOT_FOUND:
4596 case ISCSI_LOGOUT_CLEANUP_FAILED:
4597 default:
4598 break;
4599 }
4600 ret = 1;
4601 }
4602 break;
4603 case ISCSI_LOGOUT_REASON_RECOVERY:
4604 switch (cmd->logout_response) {
4605 case ISCSI_LOGOUT_SUCCESS:
4606 case ISCSI_LOGOUT_CID_NOT_FOUND:
4607 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4608 case ISCSI_LOGOUT_CLEANUP_FAILED:
4609 default:
4610 break;
4611 }
4612 ret = 1;
4613 break;
4614 default:
4615 break;
4616
4617 }
4618 return ret;
4619}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004620EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004621
4622void iscsit_fail_session(struct iscsi_session *sess)
4623{
4624 struct iscsi_conn *conn;
4625
4626 spin_lock_bh(&sess->conn_lock);
4627 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4628 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4629 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4630 }
4631 spin_unlock_bh(&sess->conn_lock);
4632
4633 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4634 sess->session_state = TARG_SESS_STATE_FAILED;
4635}
4636
4637int iscsit_free_session(struct iscsi_session *sess)
4638{
4639 u16 conn_count = atomic_read(&sess->nconn);
4640 struct iscsi_conn *conn, *conn_tmp = NULL;
4641 int is_last;
4642
4643 spin_lock_bh(&sess->conn_lock);
4644 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4645
4646 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4647 conn_list) {
4648 if (conn_count == 0)
4649 break;
4650
4651 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4652 is_last = 1;
4653 } else {
4654 iscsit_inc_conn_usage_count(conn_tmp);
4655 is_last = 0;
4656 }
4657 iscsit_inc_conn_usage_count(conn);
4658
4659 spin_unlock_bh(&sess->conn_lock);
4660 iscsit_cause_connection_reinstatement(conn, 1);
4661 spin_lock_bh(&sess->conn_lock);
4662
4663 iscsit_dec_conn_usage_count(conn);
4664 if (is_last == 0)
4665 iscsit_dec_conn_usage_count(conn_tmp);
4666
4667 conn_count--;
4668 }
4669
4670 if (atomic_read(&sess->nconn)) {
4671 spin_unlock_bh(&sess->conn_lock);
4672 wait_for_completion(&sess->session_wait_comp);
4673 } else
4674 spin_unlock_bh(&sess->conn_lock);
4675
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004676 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004677 return 0;
4678}
4679
4680void iscsit_stop_session(
4681 struct iscsi_session *sess,
4682 int session_sleep,
4683 int connection_sleep)
4684{
4685 u16 conn_count = atomic_read(&sess->nconn);
4686 struct iscsi_conn *conn, *conn_tmp = NULL;
4687 int is_last;
4688
4689 spin_lock_bh(&sess->conn_lock);
4690 if (session_sleep)
4691 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4692
4693 if (connection_sleep) {
4694 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4695 conn_list) {
4696 if (conn_count == 0)
4697 break;
4698
4699 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4700 is_last = 1;
4701 } else {
4702 iscsit_inc_conn_usage_count(conn_tmp);
4703 is_last = 0;
4704 }
4705 iscsit_inc_conn_usage_count(conn);
4706
4707 spin_unlock_bh(&sess->conn_lock);
4708 iscsit_cause_connection_reinstatement(conn, 1);
4709 spin_lock_bh(&sess->conn_lock);
4710
4711 iscsit_dec_conn_usage_count(conn);
4712 if (is_last == 0)
4713 iscsit_dec_conn_usage_count(conn_tmp);
4714 conn_count--;
4715 }
4716 } else {
4717 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4718 iscsit_cause_connection_reinstatement(conn, 0);
4719 }
4720
4721 if (session_sleep && atomic_read(&sess->nconn)) {
4722 spin_unlock_bh(&sess->conn_lock);
4723 wait_for_completion(&sess->session_wait_comp);
4724 } else
4725 spin_unlock_bh(&sess->conn_lock);
4726}
4727
4728int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4729{
4730 struct iscsi_session *sess;
4731 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4732 struct se_session *se_sess, *se_sess_tmp;
4733 int session_count = 0;
4734
4735 spin_lock_bh(&se_tpg->session_lock);
4736 if (tpg->nsessions && !force) {
4737 spin_unlock_bh(&se_tpg->session_lock);
4738 return -1;
4739 }
4740
4741 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4742 sess_list) {
4743 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4744
4745 spin_lock(&sess->conn_lock);
4746 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4747 atomic_read(&sess->session_logout) ||
4748 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4749 spin_unlock(&sess->conn_lock);
4750 continue;
4751 }
4752 atomic_set(&sess->session_reinstatement, 1);
4753 spin_unlock(&sess->conn_lock);
4754 spin_unlock_bh(&se_tpg->session_lock);
4755
4756 iscsit_free_session(sess);
4757 spin_lock_bh(&se_tpg->session_lock);
4758
4759 session_count++;
4760 }
4761 spin_unlock_bh(&se_tpg->session_lock);
4762
4763 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4764 " Group: %hu\n", session_count, tpg->tpgt);
4765 return 0;
4766}
4767
4768MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4769MODULE_VERSION("4.1.x");
4770MODULE_AUTHOR("nab@Linux-iSCSI.org");
4771MODULE_LICENSE("GPL");
4772
4773module_init(iscsi_target_init_module);
4774module_exit(iscsi_target_cleanup_module);