blob: b83ec378d04f8f1ed5fbc820429c6f1657b4d7a0 [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 Bellingerbaa4d642013-03-06 21:54:13 -0800502static struct iscsit_transport iscsi_target_transport = {
503 .name = "iSCSI/TCP",
504 .transport_type = ISCSI_TCP,
505 .owner = NULL,
506 .iscsit_setup_np = iscsit_setup_np,
507 .iscsit_accept_np = iscsit_accept_np,
508 .iscsit_free_np = iscsit_free_np,
509 .iscsit_get_login_rx = iscsit_get_login_rx,
510 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800511 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700512 .iscsit_immediate_queue = iscsit_immediate_queue,
513 .iscsit_response_queue = iscsit_response_queue,
514 .iscsit_queue_data_in = iscsit_queue_rsp,
515 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800516};
517
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000518static int __init iscsi_target_init_module(void)
519{
520 int ret = 0;
521
522 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
523
524 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
525 if (!iscsit_global) {
526 pr_err("Unable to allocate memory for iscsit_global\n");
527 return -1;
528 }
529 mutex_init(&auth_id_lock);
530 spin_lock_init(&sess_idr_lock);
531 idr_init(&tiqn_idr);
532 idr_init(&sess_idr);
533
534 ret = iscsi_target_register_configfs();
535 if (ret < 0)
536 goto out;
537
538 ret = iscsi_thread_set_init();
539 if (ret < 0)
540 goto configfs_out;
541
542 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
543 TARGET_THREAD_SET_COUNT) {
544 pr_err("iscsi_allocate_thread_sets() returned"
545 " unexpected value!\n");
546 goto ts_out1;
547 }
548
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000549 lio_qr_cache = kmem_cache_create("lio_qr_cache",
550 sizeof(struct iscsi_queue_req),
551 __alignof__(struct iscsi_queue_req), 0, NULL);
552 if (!lio_qr_cache) {
553 pr_err("nable to kmem_cache_create() for"
554 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700555 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000556 }
557
558 lio_dr_cache = kmem_cache_create("lio_dr_cache",
559 sizeof(struct iscsi_datain_req),
560 __alignof__(struct iscsi_datain_req), 0, NULL);
561 if (!lio_dr_cache) {
562 pr_err("Unable to kmem_cache_create() for"
563 " lio_dr_cache\n");
564 goto qr_out;
565 }
566
567 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
568 sizeof(struct iscsi_ooo_cmdsn),
569 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
570 if (!lio_ooo_cache) {
571 pr_err("Unable to kmem_cache_create() for"
572 " lio_ooo_cache\n");
573 goto dr_out;
574 }
575
576 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
577 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
578 0, NULL);
579 if (!lio_r2t_cache) {
580 pr_err("Unable to kmem_cache_create() for"
581 " lio_r2t_cache\n");
582 goto ooo_out;
583 }
584
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800585 iscsit_register_transport(&iscsi_target_transport);
586
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000587 if (iscsit_load_discovery_tpg() < 0)
588 goto r2t_out;
589
590 return ret;
591r2t_out:
592 kmem_cache_destroy(lio_r2t_cache);
593ooo_out:
594 kmem_cache_destroy(lio_ooo_cache);
595dr_out:
596 kmem_cache_destroy(lio_dr_cache);
597qr_out:
598 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000599ts_out2:
600 iscsi_deallocate_thread_sets();
601ts_out1:
602 iscsi_thread_set_free();
603configfs_out:
604 iscsi_target_deregister_configfs();
605out:
606 kfree(iscsit_global);
607 return -ENOMEM;
608}
609
610static void __exit iscsi_target_cleanup_module(void)
611{
612 iscsi_deallocate_thread_sets();
613 iscsi_thread_set_free();
614 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800615 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000616 kmem_cache_destroy(lio_qr_cache);
617 kmem_cache_destroy(lio_dr_cache);
618 kmem_cache_destroy(lio_ooo_cache);
619 kmem_cache_destroy(lio_r2t_cache);
620
621 iscsi_target_deregister_configfs();
622
623 kfree(iscsit_global);
624}
625
Andy Grover8b1e1242012-04-03 15:51:12 -0700626static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700627 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000628 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700629 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000630{
631 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000632
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000633 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000634 if (!cmd)
635 return -1;
636
637 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700638 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000639
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100640 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000641 if (!cmd->buf_ptr) {
642 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700643 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000644 return -1;
645 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000646
647 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700648 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000649 spin_unlock_bh(&conn->cmd_lock);
650
651 cmd->i_state = ISTATE_SEND_REJECT;
652 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
653
Nicholas Bellingerba159912013-07-03 03:48:24 -0700654 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655}
656
Nicholas Bellingerba159912013-07-03 03:48:24 -0700657static int iscsit_add_reject_from_cmd(
658 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000659 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700660 bool add_to_conn,
661 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000662{
663 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000664
665 if (!cmd->conn) {
666 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
667 cmd->init_task_tag);
668 return -1;
669 }
670 conn = cmd->conn;
671
672 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700673 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000674
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100675 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000676 if (!cmd->buf_ptr) {
677 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700678 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000679 return -1;
680 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000681
682 if (add_to_conn) {
683 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700684 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000685 spin_unlock_bh(&conn->cmd_lock);
686 }
687
688 cmd->i_state = ISTATE_SEND_REJECT;
689 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800690 /*
691 * Perform the kref_put now if se_cmd has already been setup by
692 * scsit_setup_scsi_cmd()
693 */
694 if (cmd->se_cmd.se_tfo != NULL) {
695 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
696 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
697 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700698 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000699}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700700
701static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
702 unsigned char *buf)
703{
704 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
705}
706
707int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
708{
709 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
710}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000711
712/*
713 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700714 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000715 */
716static int iscsit_map_iovec(
717 struct iscsi_cmd *cmd,
718 struct kvec *iov,
719 u32 data_offset,
720 u32 data_length)
721{
722 u32 i = 0;
723 struct scatterlist *sg;
724 unsigned int page_off;
725
726 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700727 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000728 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700729 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000730 page_off = (data_offset % PAGE_SIZE);
731
732 cmd->first_data_sg = sg;
733 cmd->first_data_sg_off = page_off;
734
735 while (data_length) {
736 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
737
738 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
739 iov[i].iov_len = cur_len;
740
741 data_length -= cur_len;
742 page_off = 0;
743 sg = sg_next(sg);
744 i++;
745 }
746
747 cmd->kmapped_nents = i;
748
749 return i;
750}
751
752static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
753{
754 u32 i;
755 struct scatterlist *sg;
756
757 sg = cmd->first_data_sg;
758
759 for (i = 0; i < cmd->kmapped_nents; i++)
760 kunmap(sg_page(&sg[i]));
761}
762
763static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
764{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700765 LIST_HEAD(ack_list);
766 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000767
768 conn->exp_statsn = exp_statsn;
769
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800770 if (conn->sess->sess_ops->RDMAExtensions)
771 return;
772
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000773 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700774 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000775 spin_lock(&cmd->istate_lock);
776 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800777 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000778 cmd->i_state = ISTATE_REMOVE;
779 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700780 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000781 continue;
782 }
783 spin_unlock(&cmd->istate_lock);
784 }
785 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700786
787 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800788 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700789 iscsit_free_cmd(cmd, false);
790 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000791}
792
793static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
794{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700795 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000796
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400797 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000798
799 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
800 if (!cmd->iov_data) {
801 pr_err("Unable to allocate cmd->iov_data\n");
802 return -ENOMEM;
803 }
804
805 cmd->orig_iov_data_count = iov_count;
806 return 0;
807}
808
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800809int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
810 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000811{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800812 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700814 int iscsi_task_attr;
815 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000816
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800817 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000818
819 hdr = (struct iscsi_scsi_req *) buf;
820 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000821
822 /* FIXME; Add checks for AdditionalHeaderSegment */
823
824 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
825 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
826 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
827 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700828 return iscsit_add_reject_cmd(cmd,
829 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000830 }
831
832 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
833 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
834 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800835 * From RFC-3720 Section 10.3.1:
836 *
837 * "Either or both of R and W MAY be 1 when either the
838 * Expected Data Transfer Length and/or Bidirectional Read
839 * Expected Data Transfer Length are 0"
840 *
841 * For this case, go ahead and clear the unnecssary bits
842 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000843 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800844 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
845 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000846
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800847 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000848 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800849 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000850 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000851
852 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
853 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
854 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
855 " MUST be set if Expected Data Transfer Length is not 0."
856 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700857 return iscsit_add_reject_cmd(cmd,
858 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000859 }
860
861 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
862 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
863 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700864 return iscsit_add_reject_cmd(cmd,
865 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000866 }
867
868 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
869 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
870 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700871 return iscsit_add_reject_cmd(cmd,
872 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000873 }
874
875 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
876 pr_err("ImmediateData=No but DataSegmentLength=%u,"
877 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700878 return iscsit_add_reject_cmd(cmd,
879 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000880 }
881
Nicholas Bellingerba159912013-07-03 03:48:24 -0700882 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000883 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
884 pr_err("Expected Data Transfer Length and Length of"
885 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
886 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700887 return iscsit_add_reject_cmd(cmd,
888 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000889 }
890
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400891 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000892 pr_err("DataSegmentLength: %u is greater than"
893 " EDTL: %u, protocol error.\n", payload_length,
894 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700895 return iscsit_add_reject_cmd(cmd,
896 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000897 }
898
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700899 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000900 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700901 " MaxXmitDataSegmentLength: %u, protocol error.\n",
902 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700903 return iscsit_add_reject_cmd(cmd,
904 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000905 }
906
907 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
908 pr_err("DataSegmentLength: %u is greater than"
909 " FirstBurstLength: %u, protocol error.\n",
910 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700911 return iscsit_add_reject_cmd(cmd,
912 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000913 }
914
915 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
916 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
917 DMA_NONE;
918
Andy Groverd28b11692012-04-03 15:51:22 -0700919 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700920 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
921 /*
922 * Figure out the SAM Task Attribute for the incoming SCSI CDB
923 */
924 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
925 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
926 sam_task_attr = MSG_SIMPLE_TAG;
927 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
928 sam_task_attr = MSG_ORDERED_TAG;
929 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
930 sam_task_attr = MSG_HEAD_TAG;
931 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
932 sam_task_attr = MSG_ACA_TAG;
933 else {
934 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
935 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
936 sam_task_attr = MSG_SIMPLE_TAG;
937 }
938
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000939 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
940 cmd->i_state = ISTATE_NEW_CMD;
941 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
942 cmd->immediate_data = (payload_length) ? 1 : 0;
943 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
944 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
945 if (cmd->unsolicited_data)
946 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
947
948 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
949 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
950 spin_lock_bh(&conn->sess->ttt_lock);
951 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
952 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
953 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
954 spin_unlock_bh(&conn->sess->ttt_lock);
955 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
956 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400957 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
958 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000959 cmd->first_burst_len = payload_length;
960
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800961 if (!conn->sess->sess_ops->RDMAExtensions &&
962 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000963 struct iscsi_datain_req *dr;
964
965 dr = iscsit_allocate_datain_req();
966 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700967 return iscsit_add_reject_cmd(cmd,
968 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000969
970 iscsit_attach_datain_req(cmd, dr);
971 }
972
973 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700974 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
975 */
976 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400977 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
978 cmd->data_direction, sam_task_attr,
979 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700980
981 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
982 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800983 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
984 conn->cid);
985
986 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700987
Christoph Hellwigde103c92012-11-06 12:24:09 -0800988 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
989 scsilun_to_int(&hdr->lun));
990 if (cmd->sense_reason)
991 goto attach_cmd;
992
993 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
994 if (cmd->sense_reason) {
995 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -0700996 return iscsit_add_reject_cmd(cmd,
997 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000998 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800999
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001000 goto attach_cmd;
1001 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001002
Christoph Hellwigde103c92012-11-06 12:24:09 -08001003 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001004 return iscsit_add_reject_cmd(cmd,
1005 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001006 }
1007
1008attach_cmd:
1009 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001010 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001011 spin_unlock_bh(&conn->cmd_lock);
1012 /*
1013 * Check if we need to delay processing because of ALUA
1014 * Active/NonOptimized primary access state..
1015 */
1016 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001017
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001018 return 0;
1019}
1020EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001021
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001022void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1023{
1024 iscsit_set_dataout_sequence_values(cmd);
1025
1026 spin_lock_bh(&cmd->dataout_timeout_lock);
1027 iscsit_start_dataout_timer(cmd, cmd->conn);
1028 spin_unlock_bh(&cmd->dataout_timeout_lock);
1029}
1030EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1031
1032int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1033 struct iscsi_scsi_req *hdr)
1034{
1035 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001036 /*
1037 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1038 * the Immediate Bit is not set, and no Immediate
1039 * Data is attached.
1040 *
1041 * A PDU/CmdSN carrying Immediate Data can only
1042 * be processed after the DataCRC has passed.
1043 * If the DataCRC fails, the CmdSN MUST NOT
1044 * be acknowledged. (See below)
1045 */
1046 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001047 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1048 (unsigned char *)hdr, hdr->cmdsn);
1049 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1050 return -1;
1051 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001052 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001053 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001054 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001055 }
1056
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001057 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001058
1059 /*
1060 * If no Immediate Data is attached, it's OK to return now.
1061 */
1062 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001063 if (!cmd->sense_reason && cmd->unsolicited_data)
1064 iscsit_set_unsoliticed_dataout(cmd);
1065 if (!cmd->sense_reason)
1066 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001067
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001068 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001069 return 0;
1070 }
1071
1072 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001073 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1074 * execution. These exceptions are processed in CmdSN order using
1075 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001076 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001077 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001078 if (cmd->reject_reason)
1079 return 0;
1080
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001081 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001082 }
1083 /*
1084 * Call directly into transport_generic_new_cmd() to perform
1085 * the backend memory allocation.
1086 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001087 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001088 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001089 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001090
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001091 return 0;
1092}
1093EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1094
1095static int
1096iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1097 bool dump_payload)
1098{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001099 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001100 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1101 /*
1102 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1103 */
1104 if (dump_payload == true)
1105 goto after_immediate_data;
1106
1107 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1108 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001109after_immediate_data:
1110 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1111 /*
1112 * A PDU/CmdSN carrying Immediate Data passed
1113 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1114 * Immediate Bit is not set.
1115 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001116 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1117 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001118 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001119 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001120
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001121 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001122 int rc;
1123
1124 rc = iscsit_dump_data_payload(cmd->conn,
1125 cmd->first_burst_len, 1);
1126 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1127 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001128 } else if (cmd->unsolicited_data)
1129 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001130
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001131 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1132 /*
1133 * Immediate Data failed DataCRC and ERL>=1,
1134 * silently drop this PDU and let the initiator
1135 * plug the CmdSN gap.
1136 *
1137 * FIXME: Send Unsolicited NOPIN with reserved
1138 * TTT here to help the initiator figure out
1139 * the missing CmdSN, although they should be
1140 * intelligent enough to determine the missing
1141 * CmdSN and issue a retry to plug the sequence.
1142 */
1143 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001144 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001145 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1146 return -1;
1147
1148 return 0;
1149}
1150
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001151static int
1152iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1153 unsigned char *buf)
1154{
1155 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1156 int rc, immed_data;
1157 bool dump_payload = false;
1158
1159 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1160 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001161 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001162 /*
1163 * Allocation iovecs needed for struct socket operations for
1164 * traditional iSCSI block I/O.
1165 */
1166 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001167 return iscsit_add_reject_cmd(cmd,
1168 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001169 }
1170 immed_data = cmd->immediate_data;
1171
1172 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1173 if (rc < 0)
1174 return rc;
1175 else if (rc > 0)
1176 dump_payload = true;
1177
1178 if (!immed_data)
1179 return 0;
1180
1181 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1182}
1183
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001184static u32 iscsit_do_crypto_hash_sg(
1185 struct hash_desc *hash,
1186 struct iscsi_cmd *cmd,
1187 u32 data_offset,
1188 u32 data_length,
1189 u32 padding,
1190 u8 *pad_bytes)
1191{
1192 u32 data_crc;
1193 u32 i;
1194 struct scatterlist *sg;
1195 unsigned int page_off;
1196
1197 crypto_hash_init(hash);
1198
1199 sg = cmd->first_data_sg;
1200 page_off = cmd->first_data_sg_off;
1201
1202 i = 0;
1203 while (data_length) {
1204 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1205
1206 crypto_hash_update(hash, &sg[i], cur_len);
1207
1208 data_length -= cur_len;
1209 page_off = 0;
1210 i++;
1211 }
1212
1213 if (padding) {
1214 struct scatterlist pad_sg;
1215
1216 sg_init_one(&pad_sg, pad_bytes, padding);
1217 crypto_hash_update(hash, &pad_sg, padding);
1218 }
1219 crypto_hash_final(hash, (u8 *) &data_crc);
1220
1221 return data_crc;
1222}
1223
1224static void iscsit_do_crypto_hash_buf(
1225 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001226 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001227 u32 payload_length,
1228 u32 padding,
1229 u8 *pad_bytes,
1230 u8 *data_crc)
1231{
1232 struct scatterlist sg;
1233
1234 crypto_hash_init(hash);
1235
Jörn Engel8359cf42011-11-24 02:05:51 +01001236 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001237 crypto_hash_update(hash, &sg, payload_length);
1238
1239 if (padding) {
1240 sg_init_one(&sg, pad_bytes, padding);
1241 crypto_hash_update(hash, &sg, padding);
1242 }
1243 crypto_hash_final(hash, data_crc);
1244}
1245
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001246int
1247iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1248 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001249{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001250 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001251 struct iscsi_cmd *cmd = NULL;
1252 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001253 u32 payload_length = ntoh24(hdr->dlength);
1254 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255
1256 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001257 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1258 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001259 }
1260
1261 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001262 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001263
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001264 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001265 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001266 " MaxXmitDataSegmentLength: %u\n", payload_length,
1267 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001268 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1269 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001270 }
1271
1272 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1273 payload_length);
1274 if (!cmd)
1275 return 0;
1276
1277 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1278 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001279 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001280 payload_length, conn->cid);
1281
1282 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1283 pr_err("Command ITT: 0x%08x received DataOUT after"
1284 " last DataOUT received, dumping payload\n",
1285 cmd->init_task_tag);
1286 return iscsit_dump_data_payload(conn, payload_length, 1);
1287 }
1288
1289 if (cmd->data_direction != DMA_TO_DEVICE) {
1290 pr_err("Command ITT: 0x%08x received DataOUT for a"
1291 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001292 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001293 }
1294 se_cmd = &cmd->se_cmd;
1295 iscsit_mod_dataout_timer(cmd);
1296
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001297 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001298 pr_err("DataOut Offset: %u, Length %u greater than"
1299 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001300 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001301 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001302 }
1303
1304 if (cmd->unsolicited_data) {
1305 int dump_unsolicited_data = 0;
1306
1307 if (conn->sess->sess_ops->InitialR2T) {
1308 pr_err("Received unexpected unsolicited data"
1309 " while InitialR2T=Yes, protocol error.\n");
1310 transport_send_check_condition_and_sense(&cmd->se_cmd,
1311 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1312 return -1;
1313 }
1314 /*
1315 * Special case for dealing with Unsolicited DataOUT
1316 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1317 * failures;
1318 */
1319
1320 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001321 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001322 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001323 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001324
1325 if (dump_unsolicited_data) {
1326 /*
1327 * Check if a delayed TASK_ABORTED status needs to
1328 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1329 * received with the unsolicitied data out.
1330 */
1331 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1332 iscsit_stop_dataout_timer(cmd);
1333
1334 transport_check_aborted_status(se_cmd,
1335 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1336 return iscsit_dump_data_payload(conn, payload_length, 1);
1337 }
1338 } else {
1339 /*
1340 * For the normal solicited data path:
1341 *
1342 * Check for a delayed TASK_ABORTED status and dump any
1343 * incoming data out payload if one exists. Also, when the
1344 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1345 * data out sequence, we decrement outstanding_r2ts. Once
1346 * outstanding_r2ts reaches zero, go ahead and send the delayed
1347 * TASK_ABORTED status.
1348 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001349 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001350 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1351 if (--cmd->outstanding_r2ts < 1) {
1352 iscsit_stop_dataout_timer(cmd);
1353 transport_check_aborted_status(
1354 se_cmd, 1);
1355 }
1356
1357 return iscsit_dump_data_payload(conn, payload_length, 1);
1358 }
1359 }
1360 /*
1361 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1362 * within-command recovery checks before receiving the payload.
1363 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001364 rc = iscsit_check_pre_dataout(cmd, buf);
1365 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001366 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001367 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368 return -1;
1369
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001370 *out_cmd = cmd;
1371 return 0;
1372}
1373EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1374
1375static int
1376iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1377 struct iscsi_data *hdr)
1378{
1379 struct kvec *iov;
1380 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1381 u32 payload_length = ntoh24(hdr->dlength);
1382 int iov_ret, data_crc_failed = 0;
1383
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001384 rx_size += payload_length;
1385 iov = &cmd->iov_data[0];
1386
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001387 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1388 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001389 if (iov_ret < 0)
1390 return -1;
1391
1392 iov_count += iov_ret;
1393
1394 padding = ((-payload_length) & 3);
1395 if (padding != 0) {
1396 iov[iov_count].iov_base = cmd->pad_bytes;
1397 iov[iov_count++].iov_len = padding;
1398 rx_size += padding;
1399 pr_debug("Receiving %u padding bytes.\n", padding);
1400 }
1401
1402 if (conn->conn_ops->DataDigest) {
1403 iov[iov_count].iov_base = &checksum;
1404 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1405 rx_size += ISCSI_CRC_LEN;
1406 }
1407
1408 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1409
1410 iscsit_unmap_iovec(cmd);
1411
1412 if (rx_got != rx_size)
1413 return -1;
1414
1415 if (conn->conn_ops->DataDigest) {
1416 u32 data_crc;
1417
1418 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001419 be32_to_cpu(hdr->offset),
1420 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001421 cmd->pad_bytes);
1422
1423 if (checksum != data_crc) {
1424 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1425 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1426 " does not match computed 0x%08x\n",
1427 hdr->itt, hdr->offset, payload_length,
1428 hdr->datasn, checksum, data_crc);
1429 data_crc_failed = 1;
1430 } else {
1431 pr_debug("Got CRC32C DataDigest 0x%08x for"
1432 " %u bytes of Data Out\n", checksum,
1433 payload_length);
1434 }
1435 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001436
1437 return data_crc_failed;
1438}
1439
1440int
1441iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1442 bool data_crc_failed)
1443{
1444 struct iscsi_conn *conn = cmd->conn;
1445 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001446 /*
1447 * Increment post receive data and CRC values or perform
1448 * within-command recovery.
1449 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001450 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1451 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001452 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001453 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001454 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001455 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1456 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001457 /*
1458 * Handle extra special case for out of order
1459 * Unsolicited Data Out.
1460 */
1461 spin_lock_bh(&cmd->istate_lock);
1462 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1463 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1464 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1465 spin_unlock_bh(&cmd->istate_lock);
1466
1467 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001468 if (ooo_cmdsn)
1469 return 0;
1470 target_execute_cmd(&cmd->se_cmd);
1471 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001472 } else /* DATAOUT_CANNOT_RECOVER */
1473 return -1;
1474
1475 return 0;
1476}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001477EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001478
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001479static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1480{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001481 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001482 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1483 int rc;
1484 bool data_crc_failed = false;
1485
1486 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1487 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001488 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001489 else if (!cmd)
1490 return 0;
1491
1492 rc = iscsit_get_dataout(conn, cmd, hdr);
1493 if (rc < 0)
1494 return rc;
1495 else if (rc > 0)
1496 data_crc_failed = true;
1497
1498 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1499}
1500
Nicholas Bellinger778de362013-06-14 16:07:47 -07001501int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1502 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001503{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001504 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001505
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001506 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001507 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1508 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001509 if (!cmd)
1510 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1511 (unsigned char *)hdr);
1512
Nicholas Bellingerba159912013-07-03 03:48:24 -07001513 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1514 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001515 }
1516
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001517 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001518 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001519 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001520 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001521 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001522 if (!cmd)
1523 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1524 (unsigned char *)hdr);
1525
Nicholas Bellingerba159912013-07-03 03:48:24 -07001526 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1527 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001528 }
1529
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001530 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001531 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001532 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001533 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1534 payload_length);
1535 /*
1536 * This is not a response to a Unsolicited NopIN, which means
1537 * it can either be a NOPOUT ping request (with a valid ITT),
1538 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1539 * Either way, make sure we allocate an struct iscsi_cmd, as both
1540 * can contain ping data.
1541 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001542 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001543 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1544 cmd->i_state = ISTATE_SEND_NOPIN;
1545 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1546 1 : 0);
1547 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1548 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001549 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1550 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001551 cmd->data_direction = DMA_NONE;
1552 }
1553
Nicholas Bellinger778de362013-06-14 16:07:47 -07001554 return 0;
1555}
1556EXPORT_SYMBOL(iscsit_setup_nop_out);
1557
1558int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1559 struct iscsi_nopout *hdr)
1560{
1561 struct iscsi_cmd *cmd_p = NULL;
1562 int cmdsn_ret = 0;
1563 /*
1564 * Initiator is expecting a NopIN ping reply..
1565 */
1566 if (hdr->itt != RESERVED_ITT) {
1567 BUG_ON(!cmd);
1568
1569 spin_lock_bh(&conn->cmd_lock);
1570 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1571 spin_unlock_bh(&conn->cmd_lock);
1572
1573 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1574
1575 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1576 iscsit_add_cmd_to_response_queue(cmd, conn,
1577 cmd->i_state);
1578 return 0;
1579 }
1580
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001581 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1582 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001583 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1584 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001585 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001586 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001587
1588 return 0;
1589 }
1590 /*
1591 * This was a response to a unsolicited NOPIN ping.
1592 */
1593 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1594 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1595 if (!cmd_p)
1596 return -EINVAL;
1597
1598 iscsit_stop_nopin_response_timer(conn);
1599
1600 cmd_p->i_state = ISTATE_REMOVE;
1601 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1602
1603 iscsit_start_nopin_timer(conn);
1604 return 0;
1605 }
1606 /*
1607 * Otherwise, initiator is not expecting a NOPIN is response.
1608 * Just ignore for now.
1609 */
1610 return 0;
1611}
1612EXPORT_SYMBOL(iscsit_process_nop_out);
1613
1614static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1615 unsigned char *buf)
1616{
1617 unsigned char *ping_data = NULL;
1618 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1619 struct kvec *iov = NULL;
1620 u32 payload_length = ntoh24(hdr->dlength);
1621 int ret;
1622
1623 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1624 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001625 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001626 /*
1627 * Handle NOP-OUT payload for traditional iSCSI sockets
1628 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001629 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001630 u32 checksum, data_crc, padding = 0;
1631 int niov = 0, rx_got, rx_size = payload_length;
1632
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001633 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1634 if (!ping_data) {
1635 pr_err("Unable to allocate memory for"
1636 " NOPOUT ping data.\n");
1637 ret = -1;
1638 goto out;
1639 }
1640
1641 iov = &cmd->iov_misc[0];
1642 iov[niov].iov_base = ping_data;
1643 iov[niov++].iov_len = payload_length;
1644
1645 padding = ((-payload_length) & 3);
1646 if (padding != 0) {
1647 pr_debug("Receiving %u additional bytes"
1648 " for padding.\n", padding);
1649 iov[niov].iov_base = &cmd->pad_bytes;
1650 iov[niov++].iov_len = padding;
1651 rx_size += padding;
1652 }
1653 if (conn->conn_ops->DataDigest) {
1654 iov[niov].iov_base = &checksum;
1655 iov[niov++].iov_len = ISCSI_CRC_LEN;
1656 rx_size += ISCSI_CRC_LEN;
1657 }
1658
1659 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1660 if (rx_got != rx_size) {
1661 ret = -1;
1662 goto out;
1663 }
1664
1665 if (conn->conn_ops->DataDigest) {
1666 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1667 ping_data, payload_length,
1668 padding, cmd->pad_bytes,
1669 (u8 *)&data_crc);
1670
1671 if (checksum != data_crc) {
1672 pr_err("Ping data CRC32C DataDigest"
1673 " 0x%08x does not match computed 0x%08x\n",
1674 checksum, data_crc);
1675 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1676 pr_err("Unable to recover from"
1677 " NOPOUT Ping DataCRC failure while in"
1678 " ERL=0.\n");
1679 ret = -1;
1680 goto out;
1681 } else {
1682 /*
1683 * Silently drop this PDU and let the
1684 * initiator plug the CmdSN gap.
1685 */
1686 pr_debug("Dropping NOPOUT"
1687 " Command CmdSN: 0x%08x due to"
1688 " DataCRC error.\n", hdr->cmdsn);
1689 ret = 0;
1690 goto out;
1691 }
1692 } else {
1693 pr_debug("Got CRC32C DataDigest"
1694 " 0x%08x for %u bytes of ping data.\n",
1695 checksum, payload_length);
1696 }
1697 }
1698
1699 ping_data[payload_length] = '\0';
1700 /*
1701 * Attach ping data to struct iscsi_cmd->buf_ptr.
1702 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001703 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001704 cmd->buf_ptr_size = payload_length;
1705
1706 pr_debug("Got %u bytes of NOPOUT ping"
1707 " data.\n", payload_length);
1708 pr_debug("Ping Data: \"%s\"\n", ping_data);
1709 }
1710
Nicholas Bellinger778de362013-06-14 16:07:47 -07001711 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001712out:
1713 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001714 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001715
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001716 kfree(ping_data);
1717 return ret;
1718}
1719
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001720int
1721iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1722 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001723{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001724 struct se_tmr_req *se_tmr;
1725 struct iscsi_tmr_req *tmr_req;
1726 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001727 int out_of_order_cmdsn = 0, ret;
1728 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001729 u8 function;
1730
1731 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1733 function = hdr->flags;
1734
1735 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1736 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1737 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1738 hdr->rtt, hdr->refcmdsn, conn->cid);
1739
1740 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1741 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001742 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001744 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 }
1746
1747 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1748 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1749 pr_err("Task Management Request TASK_REASSIGN not"
1750 " issued as immediate command, bad iSCSI Initiator"
1751 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001752 return iscsit_add_reject_cmd(cmd,
1753 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754 }
1755 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001756 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1757 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001758
Andy Groverd28b11692012-04-03 15:51:22 -07001759 cmd->data_direction = DMA_NONE;
1760
1761 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1762 if (!cmd->tmr_req) {
1763 pr_err("Unable to allocate memory for"
1764 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001765 return iscsit_add_reject_cmd(cmd,
1766 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1767 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001768 }
1769
1770 /*
1771 * TASK_REASSIGN for ERL=2 / connection stays inside of
1772 * LIO-Target $FABRIC_MOD
1773 */
1774 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1775
1776 u8 tcm_function;
1777 int ret;
1778
1779 transport_init_se_cmd(&cmd->se_cmd,
1780 &lio_target_fabric_configfs->tf_ops,
1781 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001782 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001783
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001784 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1785 sess_ref = true;
1786
Andy Groverd28b11692012-04-03 15:51:22 -07001787 switch (function) {
1788 case ISCSI_TM_FUNC_ABORT_TASK:
1789 tcm_function = TMR_ABORT_TASK;
1790 break;
1791 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1792 tcm_function = TMR_ABORT_TASK_SET;
1793 break;
1794 case ISCSI_TM_FUNC_CLEAR_ACA:
1795 tcm_function = TMR_CLEAR_ACA;
1796 break;
1797 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1798 tcm_function = TMR_CLEAR_TASK_SET;
1799 break;
1800 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1801 tcm_function = TMR_LUN_RESET;
1802 break;
1803 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1804 tcm_function = TMR_TARGET_WARM_RESET;
1805 break;
1806 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1807 tcm_function = TMR_TARGET_COLD_RESET;
1808 break;
1809 default:
1810 pr_err("Unknown iSCSI TMR Function:"
1811 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001812 return iscsit_add_reject_cmd(cmd,
1813 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001814 }
1815
1816 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1817 tcm_function, GFP_KERNEL);
1818 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001819 return iscsit_add_reject_cmd(cmd,
1820 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001821
1822 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1823 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001824
1825 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1826 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1827 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1828 cmd->init_task_tag = hdr->itt;
1829 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001830 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1831 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001832 se_tmr = cmd->se_cmd.se_tmr_req;
1833 tmr_req = cmd->tmr_req;
1834 /*
1835 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1836 */
1837 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001838 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1839 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001840 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001841 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1842 goto attach;
1843 }
1844 }
1845
1846 switch (function) {
1847 case ISCSI_TM_FUNC_ABORT_TASK:
1848 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001849 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001850 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 break;
1852 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1853 case ISCSI_TM_FUNC_CLEAR_ACA:
1854 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1855 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1856 break;
1857 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1858 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001859 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1860 goto attach;
1861 }
1862 break;
1863 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1864 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001865 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1866 goto attach;
1867 }
1868 break;
1869 case ISCSI_TM_FUNC_TASK_REASSIGN:
1870 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1871 /*
1872 * Perform sanity checks on the ExpDataSN only if the
1873 * TASK_REASSIGN was successful.
1874 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001875 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001876 break;
1877
1878 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001879 return iscsit_add_reject_cmd(cmd,
1880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881 break;
1882 default:
1883 pr_err("Unknown TMR function: 0x%02x, protocol"
1884 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001885 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1886 goto attach;
1887 }
1888
1889 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1890 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1891 se_tmr->call_transport = 1;
1892attach:
1893 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001894 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001895 spin_unlock_bh(&conn->cmd_lock);
1896
1897 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001898 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001899 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1900 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001901 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001902 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001903 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001904 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001906 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001907
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001908 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001909 return 0;
1910 /*
1911 * Found the referenced task, send to transport for processing.
1912 */
1913 if (se_tmr->call_transport)
1914 return transport_generic_handle_tmr(&cmd->se_cmd);
1915
1916 /*
1917 * Could not find the referenced LUN, task, or Task Management
1918 * command not authorized or supported. Change state and
1919 * let the tx_thread send the response.
1920 *
1921 * For connection recovery, this is also the default action for
1922 * TMR TASK_REASSIGN.
1923 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001924 if (sess_ref) {
1925 pr_debug("Handle TMR, using sess_ref=true check\n");
1926 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1927 }
1928
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001929 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1930 return 0;
1931}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001932EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001933
1934/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001935int
1936iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1937 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001939 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001940
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001941 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001942 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001943 "greater than MaxXmitDataSegmentLength %u.\n",
1944 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001945 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1946 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001947 }
1948
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001949 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1950 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1951 pr_err("Multi sequence text commands currently not supported\n");
1952 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1953 (unsigned char *)hdr);
1954 }
1955
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001956 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1957 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1958 hdr->exp_statsn, payload_length);
1959
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001960 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1961 cmd->i_state = ISTATE_SEND_TEXTRSP;
1962 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1963 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1964 cmd->targ_xfer_tag = 0xFFFFFFFF;
1965 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1966 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1967 cmd->data_direction = DMA_NONE;
1968
1969 return 0;
1970}
1971EXPORT_SYMBOL(iscsit_setup_text_cmd);
1972
1973int
1974iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1975 struct iscsi_text *hdr)
1976{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001977 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001978 int cmdsn_ret;
1979
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001980 if (!text_in) {
1981 pr_err("Unable to locate text_in buffer for sendtargets"
1982 " discovery\n");
1983 goto reject;
1984 }
1985 if (strncmp("SendTargets", text_in, 11) != 0) {
1986 pr_err("Received Text Data that is not"
1987 " SendTargets, cannot continue.\n");
1988 goto reject;
1989 }
1990 text_ptr = strchr(text_in, '=');
1991 if (!text_ptr) {
1992 pr_err("No \"=\" separator found in Text Data,"
1993 " cannot continue.\n");
1994 goto reject;
1995 }
1996 if (!strncmp("=All", text_ptr, 4)) {
1997 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07001998 } else if (!strncmp("=iqn.", text_ptr, 5) ||
1999 !strncmp("=eui.", text_ptr, 5)) {
2000 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002001 } else {
2002 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2003 goto reject;
2004 }
2005
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002006 spin_lock_bh(&conn->cmd_lock);
2007 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2008 spin_unlock_bh(&conn->cmd_lock);
2009
2010 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2011
2012 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002013 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2014 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002015 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002016 return -1;
2017
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002018 return 0;
2019 }
2020
2021 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002022
2023reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002024 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2025 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002026}
2027EXPORT_SYMBOL(iscsit_process_text_cmd);
2028
2029static int
2030iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2031 unsigned char *buf)
2032{
2033 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2034 char *text_in = NULL;
2035 u32 payload_length = ntoh24(hdr->dlength);
2036 int rx_size, rc;
2037
2038 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2039 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002040 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002041
2042 rx_size = payload_length;
2043 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002044 u32 checksum = 0, data_crc = 0;
2045 u32 padding = 0, pad_bytes = 0;
2046 int niov = 0, rx_got;
2047 struct kvec iov[3];
2048
2049 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002050 if (!text_in) {
2051 pr_err("Unable to allocate memory for"
2052 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002053 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002054 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002055 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002056
2057 memset(iov, 0, 3 * sizeof(struct kvec));
2058 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002059 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002060
2061 padding = ((-payload_length) & 3);
2062 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002063 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002064 iov[niov++].iov_len = padding;
2065 rx_size += padding;
2066 pr_debug("Receiving %u additional bytes"
2067 " for padding.\n", padding);
2068 }
2069 if (conn->conn_ops->DataDigest) {
2070 iov[niov].iov_base = &checksum;
2071 iov[niov++].iov_len = ISCSI_CRC_LEN;
2072 rx_size += ISCSI_CRC_LEN;
2073 }
2074
2075 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002076 if (rx_got != rx_size)
2077 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002078
2079 if (conn->conn_ops->DataDigest) {
2080 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002081 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002082 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002083 (u8 *)&data_crc);
2084
2085 if (checksum != data_crc) {
2086 pr_err("Text data CRC32C DataDigest"
2087 " 0x%08x does not match computed"
2088 " 0x%08x\n", checksum, data_crc);
2089 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2090 pr_err("Unable to recover from"
2091 " Text Data digest failure while in"
2092 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002093 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002094 } else {
2095 /*
2096 * Silently drop this PDU and let the
2097 * initiator plug the CmdSN gap.
2098 */
2099 pr_debug("Dropping Text"
2100 " Command CmdSN: 0x%08x due to"
2101 " DataCRC error.\n", hdr->cmdsn);
2102 kfree(text_in);
2103 return 0;
2104 }
2105 } else {
2106 pr_debug("Got CRC32C DataDigest"
2107 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002108 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002109 }
2110 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002112 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002113 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002114 }
2115
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002117
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002118reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002119 kfree(cmd->text_in_ptr);
2120 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002121 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002122}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002123EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002124
2125int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2126{
2127 struct iscsi_conn *conn_p;
2128 struct iscsi_session *sess = conn->sess;
2129
2130 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2131 " for SID: %u.\n", conn->cid, conn->sess->sid);
2132
2133 atomic_set(&sess->session_logout, 1);
2134 atomic_set(&conn->conn_logout_remove, 1);
2135 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2136
2137 iscsit_inc_conn_usage_count(conn);
2138 iscsit_inc_session_usage_count(sess);
2139
2140 spin_lock_bh(&sess->conn_lock);
2141 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2142 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2143 continue;
2144
2145 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2146 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2147 }
2148 spin_unlock_bh(&sess->conn_lock);
2149
2150 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2151
2152 return 0;
2153}
2154
2155int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2156{
2157 struct iscsi_conn *l_conn;
2158 struct iscsi_session *sess = conn->sess;
2159
2160 pr_debug("Received logout request CLOSECONNECTION for CID:"
2161 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2162
2163 /*
2164 * A Logout Request with a CLOSECONNECTION reason code for a CID
2165 * can arrive on a connection with a differing CID.
2166 */
2167 if (conn->cid == cmd->logout_cid) {
2168 spin_lock_bh(&conn->state_lock);
2169 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2170 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2171
2172 atomic_set(&conn->conn_logout_remove, 1);
2173 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2174 iscsit_inc_conn_usage_count(conn);
2175
2176 spin_unlock_bh(&conn->state_lock);
2177 } else {
2178 /*
2179 * Handle all different cid CLOSECONNECTION requests in
2180 * iscsit_logout_post_handler_diffcid() as to give enough
2181 * time for any non immediate command's CmdSN to be
2182 * acknowledged on the connection in question.
2183 *
2184 * Here we simply make sure the CID is still around.
2185 */
2186 l_conn = iscsit_get_conn_from_cid(sess,
2187 cmd->logout_cid);
2188 if (!l_conn) {
2189 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2190 iscsit_add_cmd_to_response_queue(cmd, conn,
2191 cmd->i_state);
2192 return 0;
2193 }
2194
2195 iscsit_dec_conn_usage_count(l_conn);
2196 }
2197
2198 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2199
2200 return 0;
2201}
2202
2203int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2204{
2205 struct iscsi_session *sess = conn->sess;
2206
2207 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2208 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2209
2210 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2211 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2212 " while ERL!=2.\n");
2213 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2214 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2215 return 0;
2216 }
2217
2218 if (conn->cid == cmd->logout_cid) {
2219 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2220 " with CID: %hu on CID: %hu, implementation error.\n",
2221 cmd->logout_cid, conn->cid);
2222 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2223 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2224 return 0;
2225 }
2226
2227 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2228
2229 return 0;
2230}
2231
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002232int
2233iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2234 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002235{
2236 int cmdsn_ret, logout_remove = 0;
2237 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002238 struct iscsi_logout *hdr;
2239 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2240
2241 hdr = (struct iscsi_logout *) buf;
2242 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002243
2244 if (tiqn) {
2245 spin_lock(&tiqn->logout_stats.lock);
2246 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2247 tiqn->logout_stats.normal_logouts++;
2248 else
2249 tiqn->logout_stats.abnormal_logouts++;
2250 spin_unlock(&tiqn->logout_stats.lock);
2251 }
2252
2253 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2254 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2255 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2256 hdr->cid, conn->cid);
2257
2258 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2259 pr_err("Received logout request on connection that"
2260 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002261 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002262 return 0;
2263 }
2264
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002265 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2266 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2267 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2268 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2269 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002270 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2271 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2272 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002273 cmd->logout_reason = reason_code;
2274 cmd->data_direction = DMA_NONE;
2275
2276 /*
2277 * We need to sleep in these cases (by returning 1) until the Logout
2278 * Response gets sent in the tx thread.
2279 */
2280 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2281 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002282 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002283 logout_remove = 1;
2284
2285 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002286 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002287 spin_unlock_bh(&conn->cmd_lock);
2288
2289 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002290 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002291
2292 /*
2293 * Immediate commands are executed, well, immediately.
2294 * Non-Immediate Logout Commands are executed in CmdSN order.
2295 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002296 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002297 int ret = iscsit_execute_cmd(cmd, 0);
2298
2299 if (ret < 0)
2300 return ret;
2301 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002302 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002303 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002304 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002305 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2306 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307 }
2308
2309 return logout_remove;
2310}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002311EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002312
2313static int iscsit_handle_snack(
2314 struct iscsi_conn *conn,
2315 unsigned char *buf)
2316{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 struct iscsi_snack *hdr;
2318
2319 hdr = (struct iscsi_snack *) buf;
2320 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002321
2322 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2323 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2324 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2325 hdr->begrun, hdr->runlength, conn->cid);
2326
2327 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2328 pr_err("Initiator sent SNACK request while in"
2329 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002330 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2331 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002332 }
2333 /*
2334 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2335 * call from inside iscsi_send_recovery_datain_or_r2t().
2336 */
2337 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2338 case 0:
2339 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002340 hdr->itt,
2341 be32_to_cpu(hdr->ttt),
2342 be32_to_cpu(hdr->begrun),
2343 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002344 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002345 return iscsit_handle_status_snack(conn, hdr->itt,
2346 be32_to_cpu(hdr->ttt),
2347 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002348 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002349 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2350 be32_to_cpu(hdr->begrun),
2351 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002352 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2353 /* FIXME: Support R-Data SNACK */
2354 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002355 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2356 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002357 default:
2358 pr_err("Unknown SNACK type 0x%02x, protocol"
2359 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002360 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2361 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002362 }
2363
2364 return 0;
2365}
2366
2367static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2368{
2369 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2370 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2371 wait_for_completion_interruptible_timeout(
2372 &conn->rx_half_close_comp,
2373 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2374 }
2375}
2376
2377static int iscsit_handle_immediate_data(
2378 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002379 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002380 u32 length)
2381{
2382 int iov_ret, rx_got = 0, rx_size = 0;
2383 u32 checksum, iov_count = 0, padding = 0;
2384 struct iscsi_conn *conn = cmd->conn;
2385 struct kvec *iov;
2386
2387 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2388 if (iov_ret < 0)
2389 return IMMEDIATE_DATA_CANNOT_RECOVER;
2390
2391 rx_size = length;
2392 iov_count = iov_ret;
2393 iov = &cmd->iov_data[0];
2394
2395 padding = ((-length) & 3);
2396 if (padding != 0) {
2397 iov[iov_count].iov_base = cmd->pad_bytes;
2398 iov[iov_count++].iov_len = padding;
2399 rx_size += padding;
2400 }
2401
2402 if (conn->conn_ops->DataDigest) {
2403 iov[iov_count].iov_base = &checksum;
2404 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2405 rx_size += ISCSI_CRC_LEN;
2406 }
2407
2408 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2409
2410 iscsit_unmap_iovec(cmd);
2411
2412 if (rx_got != rx_size) {
2413 iscsit_rx_thread_wait_for_tcp(conn);
2414 return IMMEDIATE_DATA_CANNOT_RECOVER;
2415 }
2416
2417 if (conn->conn_ops->DataDigest) {
2418 u32 data_crc;
2419
2420 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2421 cmd->write_data_done, length, padding,
2422 cmd->pad_bytes);
2423
2424 if (checksum != data_crc) {
2425 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2426 " does not match computed 0x%08x\n", checksum,
2427 data_crc);
2428
2429 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2430 pr_err("Unable to recover from"
2431 " Immediate Data digest failure while"
2432 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002433 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002434 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002435 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002436 return IMMEDIATE_DATA_CANNOT_RECOVER;
2437 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002438 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002439 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002440 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002441 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2442 }
2443 } else {
2444 pr_debug("Got CRC32C DataDigest 0x%08x for"
2445 " %u bytes of Immediate Data\n", checksum,
2446 length);
2447 }
2448 }
2449
2450 cmd->write_data_done += length;
2451
Andy Groverebf1d952012-04-03 15:51:24 -07002452 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002453 spin_lock_bh(&cmd->istate_lock);
2454 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2455 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2456 spin_unlock_bh(&cmd->istate_lock);
2457 }
2458
2459 return IMMEDIATE_DATA_NORMAL_OPERATION;
2460}
2461
2462/*
2463 * Called with sess->conn_lock held.
2464 */
2465/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2466 with active network interface */
2467static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2468{
2469 struct iscsi_cmd *cmd;
2470 struct iscsi_conn *conn_p;
2471
2472 /*
2473 * Only send a Asynchronous Message on connections whos network
2474 * interface is still functional.
2475 */
2476 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2477 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2478 iscsit_inc_conn_usage_count(conn_p);
2479 break;
2480 }
2481 }
2482
2483 if (!conn_p)
2484 return;
2485
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002486 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002487 if (!cmd) {
2488 iscsit_dec_conn_usage_count(conn_p);
2489 return;
2490 }
2491
2492 cmd->logout_cid = conn->cid;
2493 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2494 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2495
2496 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002497 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002498 spin_unlock_bh(&conn_p->cmd_lock);
2499
2500 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2501 iscsit_dec_conn_usage_count(conn_p);
2502}
2503
2504static int iscsit_send_conn_drop_async_message(
2505 struct iscsi_cmd *cmd,
2506 struct iscsi_conn *conn)
2507{
2508 struct iscsi_async *hdr;
2509
2510 cmd->tx_size = ISCSI_HDR_LEN;
2511 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2512
2513 hdr = (struct iscsi_async *) cmd->pdu;
2514 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2515 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002516 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002517 cmd->targ_xfer_tag = 0xFFFFFFFF;
2518 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2519 cmd->stat_sn = conn->stat_sn++;
2520 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2521 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2522 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2523 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2524 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2525 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2526 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2527
2528 if (conn->conn_ops->HeaderDigest) {
2529 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2530
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002531 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2532 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002533
2534 cmd->tx_size += ISCSI_CRC_LEN;
2535 pr_debug("Attaching CRC32C HeaderDigest to"
2536 " Async Message 0x%08x\n", *header_digest);
2537 }
2538
2539 cmd->iov_misc[0].iov_base = cmd->pdu;
2540 cmd->iov_misc[0].iov_len = cmd->tx_size;
2541 cmd->iov_misc_count = 1;
2542
2543 pr_debug("Sending Connection Dropped Async Message StatSN:"
2544 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2545 cmd->logout_cid, conn->cid);
2546 return 0;
2547}
2548
Andy Grover6f3c0e62012-04-03 15:51:09 -07002549static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2550{
2551 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2552 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2553 wait_for_completion_interruptible_timeout(
2554 &conn->tx_half_close_comp,
2555 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2556 }
2557}
2558
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002559static void
2560iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2561 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2562 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002563{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002564 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2565 hdr->flags = datain->flags;
2566 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2567 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2568 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2569 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2570 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2571 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2572 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2573 }
2574 }
2575 hton24(hdr->dlength, datain->length);
2576 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2577 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2578 (struct scsi_lun *)&hdr->lun);
2579 else
2580 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2581
2582 hdr->itt = cmd->init_task_tag;
2583
2584 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2585 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2586 else
2587 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2588 if (set_statsn)
2589 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2590 else
2591 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2592
2593 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2594 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2595 hdr->datasn = cpu_to_be32(datain->data_sn);
2596 hdr->offset = cpu_to_be32(datain->offset);
2597
2598 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2599 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2600 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2601 ntohl(hdr->offset), datain->length, conn->cid);
2602}
2603
2604static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2605{
2606 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002607 struct iscsi_datain datain;
2608 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002609 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002610 u32 iov_count = 0, tx_size = 0;
2611 int eodr = 0, ret, iov_ret;
2612 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002613
2614 memset(&datain, 0, sizeof(struct iscsi_datain));
2615 dr = iscsit_get_datain_values(cmd, &datain);
2616 if (!dr) {
2617 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2618 cmd->init_task_tag);
2619 return -1;
2620 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002621 /*
2622 * Be paranoid and double check the logic for now.
2623 */
Andy Groverebf1d952012-04-03 15:51:24 -07002624 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002625 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2626 " datain.length: %u exceeds cmd->data_length: %u\n",
2627 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002628 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002629 return -1;
2630 }
2631
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002632 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002633 /*
2634 * Special case for successfully execution w/ both DATAIN
2635 * and Sense Data.
2636 */
2637 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2638 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2639 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2640 else {
2641 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2642 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2643 iscsit_increment_maxcmdsn(cmd, conn->sess);
2644 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002645 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002647 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2648 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649 }
2650
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002651 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002652
2653 iov = &cmd->iov_data[0];
2654 iov[iov_count].iov_base = cmd->pdu;
2655 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2656 tx_size += ISCSI_HDR_LEN;
2657
2658 if (conn->conn_ops->HeaderDigest) {
2659 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2660
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002661 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2662 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002663
2664 iov[0].iov_len += ISCSI_CRC_LEN;
2665 tx_size += ISCSI_CRC_LEN;
2666
2667 pr_debug("Attaching CRC32 HeaderDigest"
2668 " for DataIN PDU 0x%08x\n", *header_digest);
2669 }
2670
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002671 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2672 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002673 if (iov_ret < 0)
2674 return -1;
2675
2676 iov_count += iov_ret;
2677 tx_size += datain.length;
2678
2679 cmd->padding = ((-datain.length) & 3);
2680 if (cmd->padding) {
2681 iov[iov_count].iov_base = cmd->pad_bytes;
2682 iov[iov_count++].iov_len = cmd->padding;
2683 tx_size += cmd->padding;
2684
2685 pr_debug("Attaching %u padding bytes\n",
2686 cmd->padding);
2687 }
2688 if (conn->conn_ops->DataDigest) {
2689 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2690 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2691
2692 iov[iov_count].iov_base = &cmd->data_crc;
2693 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2694 tx_size += ISCSI_CRC_LEN;
2695
2696 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2697 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2698 }
2699
2700 cmd->iov_data_count = iov_count;
2701 cmd->tx_size = tx_size;
2702
Andy Grover6f3c0e62012-04-03 15:51:09 -07002703 /* sendpage is preferred but can't insert markers */
2704 if (!conn->conn_ops->IFMarker)
2705 ret = iscsit_fe_sendpage_sg(cmd, conn);
2706 else
2707 ret = iscsit_send_tx_data(cmd, conn, 0);
2708
2709 iscsit_unmap_iovec(cmd);
2710
2711 if (ret < 0) {
2712 iscsit_tx_thread_wait_for_tcp(conn);
2713 return ret;
2714 }
2715
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002716 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002717 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002718 2 : 1;
2719 iscsit_free_datain_req(cmd, dr);
2720 }
2721
Andy Grover6f3c0e62012-04-03 15:51:09 -07002722 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002723}
2724
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002725int
2726iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2727 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002728{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002729 struct iscsi_conn *logout_conn = NULL;
2730 struct iscsi_conn_recovery *cr = NULL;
2731 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002732 /*
2733 * The actual shutting down of Sessions and/or Connections
2734 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2735 * is done in scsi_logout_post_handler().
2736 */
2737 switch (cmd->logout_reason) {
2738 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2739 pr_debug("iSCSI session logout successful, setting"
2740 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2741 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2742 break;
2743 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2744 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2745 break;
2746 /*
2747 * For CLOSECONNECTION logout requests carrying
2748 * a matching logout CID -> local CID, the reference
2749 * for the local CID will have been incremented in
2750 * iscsi_logout_closeconnection().
2751 *
2752 * For CLOSECONNECTION logout requests carrying
2753 * a different CID than the connection it arrived
2754 * on, the connection responding to cmd->logout_cid
2755 * is stopped in iscsit_logout_post_handler_diffcid().
2756 */
2757
2758 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2759 " successful.\n", cmd->logout_cid, conn->cid);
2760 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2761 break;
2762 case ISCSI_LOGOUT_REASON_RECOVERY:
2763 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2764 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2765 break;
2766 /*
2767 * If the connection is still active from our point of view
2768 * force connection recovery to occur.
2769 */
2770 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2771 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002772 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002773 iscsit_connection_reinstatement_rcfr(logout_conn);
2774 iscsit_dec_conn_usage_count(logout_conn);
2775 }
2776
2777 cr = iscsit_get_inactive_connection_recovery_entry(
2778 conn->sess, cmd->logout_cid);
2779 if (!cr) {
2780 pr_err("Unable to locate CID: %hu for"
2781 " REMOVECONNFORRECOVERY Logout Request.\n",
2782 cmd->logout_cid);
2783 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2784 break;
2785 }
2786
2787 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2788
2789 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2790 " for recovery for CID: %hu on CID: %hu successful.\n",
2791 cmd->logout_cid, conn->cid);
2792 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2793 break;
2794 default:
2795 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2796 cmd->logout_reason);
2797 return -1;
2798 }
2799
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002800 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2801 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2802 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002803 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002804 cmd->stat_sn = conn->stat_sn++;
2805 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2806
2807 iscsit_increment_maxcmdsn(cmd, conn->sess);
2808 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2809 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2810
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002811 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2812 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2813 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2814 cmd->logout_cid, conn->cid);
2815
2816 return 0;
2817}
2818EXPORT_SYMBOL(iscsit_build_logout_rsp);
2819
2820static int
2821iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2822{
2823 struct kvec *iov;
2824 int niov = 0, tx_size, rc;
2825
2826 rc = iscsit_build_logout_rsp(cmd, conn,
2827 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2828 if (rc < 0)
2829 return rc;
2830
2831 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002832 iov = &cmd->iov_misc[0];
2833 iov[niov].iov_base = cmd->pdu;
2834 iov[niov++].iov_len = ISCSI_HDR_LEN;
2835
2836 if (conn->conn_ops->HeaderDigest) {
2837 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2838
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002839 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2840 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002841
2842 iov[0].iov_len += ISCSI_CRC_LEN;
2843 tx_size += ISCSI_CRC_LEN;
2844 pr_debug("Attaching CRC32C HeaderDigest to"
2845 " Logout Response 0x%08x\n", *header_digest);
2846 }
2847 cmd->iov_misc_count = niov;
2848 cmd->tx_size = tx_size;
2849
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850 return 0;
2851}
2852
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002853void
2854iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2855 struct iscsi_nopin *hdr, bool nopout_response)
2856{
2857 hdr->opcode = ISCSI_OP_NOOP_IN;
2858 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2859 hton24(hdr->dlength, cmd->buf_ptr_size);
2860 if (nopout_response)
2861 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2862 hdr->itt = cmd->init_task_tag;
2863 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2864 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2865 conn->stat_sn;
2866 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2867
2868 if (nopout_response)
2869 iscsit_increment_maxcmdsn(cmd, conn->sess);
2870
2871 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2872 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2873
2874 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2875 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2876 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2877 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2878}
2879EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2880
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002881/*
2882 * Unsolicited NOPIN, either requesting a response or not.
2883 */
2884static int iscsit_send_unsolicited_nopin(
2885 struct iscsi_cmd *cmd,
2886 struct iscsi_conn *conn,
2887 int want_response)
2888{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002889 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2890 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002891
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002892 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002893
2894 if (conn->conn_ops->HeaderDigest) {
2895 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2896
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002897 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2898 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002899
2900 tx_size += ISCSI_CRC_LEN;
2901 pr_debug("Attaching CRC32C HeaderDigest to"
2902 " NopIN 0x%08x\n", *header_digest);
2903 }
2904
2905 cmd->iov_misc[0].iov_base = cmd->pdu;
2906 cmd->iov_misc[0].iov_len = tx_size;
2907 cmd->iov_misc_count = 1;
2908 cmd->tx_size = tx_size;
2909
2910 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2911 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2912
Andy Grover6f3c0e62012-04-03 15:51:09 -07002913 ret = iscsit_send_tx_data(cmd, conn, 1);
2914 if (ret < 0) {
2915 iscsit_tx_thread_wait_for_tcp(conn);
2916 return ret;
2917 }
2918
2919 spin_lock_bh(&cmd->istate_lock);
2920 cmd->i_state = want_response ?
2921 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2922 spin_unlock_bh(&cmd->istate_lock);
2923
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924 return 0;
2925}
2926
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002927static int
2928iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002929{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002930 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002932 u32 padding = 0;
2933 int niov = 0, tx_size;
2934
2935 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002936
2937 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002938 iov = &cmd->iov_misc[0];
2939 iov[niov].iov_base = cmd->pdu;
2940 iov[niov++].iov_len = ISCSI_HDR_LEN;
2941
2942 if (conn->conn_ops->HeaderDigest) {
2943 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2944
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002945 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2946 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002947
2948 iov[0].iov_len += ISCSI_CRC_LEN;
2949 tx_size += ISCSI_CRC_LEN;
2950 pr_debug("Attaching CRC32C HeaderDigest"
2951 " to NopIn 0x%08x\n", *header_digest);
2952 }
2953
2954 /*
2955 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2956 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2957 */
2958 if (cmd->buf_ptr_size) {
2959 iov[niov].iov_base = cmd->buf_ptr;
2960 iov[niov++].iov_len = cmd->buf_ptr_size;
2961 tx_size += cmd->buf_ptr_size;
2962
2963 pr_debug("Echoing back %u bytes of ping"
2964 " data.\n", cmd->buf_ptr_size);
2965
2966 padding = ((-cmd->buf_ptr_size) & 3);
2967 if (padding != 0) {
2968 iov[niov].iov_base = &cmd->pad_bytes;
2969 iov[niov++].iov_len = padding;
2970 tx_size += padding;
2971 pr_debug("Attaching %u additional"
2972 " padding bytes.\n", padding);
2973 }
2974 if (conn->conn_ops->DataDigest) {
2975 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2976 cmd->buf_ptr, cmd->buf_ptr_size,
2977 padding, (u8 *)&cmd->pad_bytes,
2978 (u8 *)&cmd->data_crc);
2979
2980 iov[niov].iov_base = &cmd->data_crc;
2981 iov[niov++].iov_len = ISCSI_CRC_LEN;
2982 tx_size += ISCSI_CRC_LEN;
2983 pr_debug("Attached DataDigest for %u"
2984 " bytes of ping data, CRC 0x%08x\n",
2985 cmd->buf_ptr_size, cmd->data_crc);
2986 }
2987 }
2988
2989 cmd->iov_misc_count = niov;
2990 cmd->tx_size = tx_size;
2991
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002992 return 0;
2993}
2994
Andy Grover6f3c0e62012-04-03 15:51:09 -07002995static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002996 struct iscsi_cmd *cmd,
2997 struct iscsi_conn *conn)
2998{
2999 int tx_size = 0;
3000 struct iscsi_r2t *r2t;
3001 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003002 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003003
3004 r2t = iscsit_get_r2t_from_list(cmd);
3005 if (!r2t)
3006 return -1;
3007
3008 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3009 memset(hdr, 0, ISCSI_HDR_LEN);
3010 hdr->opcode = ISCSI_OP_R2T;
3011 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3012 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3013 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003014 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003015 spin_lock_bh(&conn->sess->ttt_lock);
3016 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3017 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3018 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3019 spin_unlock_bh(&conn->sess->ttt_lock);
3020 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3021 hdr->statsn = cpu_to_be32(conn->stat_sn);
3022 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3023 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3024 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3025 hdr->data_offset = cpu_to_be32(r2t->offset);
3026 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3027
3028 cmd->iov_misc[0].iov_base = cmd->pdu;
3029 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3030 tx_size += ISCSI_HDR_LEN;
3031
3032 if (conn->conn_ops->HeaderDigest) {
3033 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3034
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003035 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3036 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003037
3038 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3039 tx_size += ISCSI_CRC_LEN;
3040 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3041 " PDU 0x%08x\n", *header_digest);
3042 }
3043
3044 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3045 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3046 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3047 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3048 r2t->offset, r2t->xfer_len, conn->cid);
3049
3050 cmd->iov_misc_count = 1;
3051 cmd->tx_size = tx_size;
3052
3053 spin_lock_bh(&cmd->r2t_lock);
3054 r2t->sent_r2t = 1;
3055 spin_unlock_bh(&cmd->r2t_lock);
3056
Andy Grover6f3c0e62012-04-03 15:51:09 -07003057 ret = iscsit_send_tx_data(cmd, conn, 1);
3058 if (ret < 0) {
3059 iscsit_tx_thread_wait_for_tcp(conn);
3060 return ret;
3061 }
3062
3063 spin_lock_bh(&cmd->dataout_timeout_lock);
3064 iscsit_start_dataout_timer(cmd, conn);
3065 spin_unlock_bh(&cmd->dataout_timeout_lock);
3066
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003067 return 0;
3068}
3069
3070/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003071 * @recovery: If called from iscsi_task_reassign_complete_write() for
3072 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003073 */
3074int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003075 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003076 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003077 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003078{
3079 int first_r2t = 1;
3080 u32 offset = 0, xfer_len = 0;
3081
3082 spin_lock_bh(&cmd->r2t_lock);
3083 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3084 spin_unlock_bh(&cmd->r2t_lock);
3085 return 0;
3086 }
3087
Andy Grover8b1e1242012-04-03 15:51:12 -07003088 if (conn->sess->sess_ops->DataSequenceInOrder &&
3089 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003090 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003091
3092 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3093 if (conn->sess->sess_ops->DataSequenceInOrder) {
3094 offset = cmd->r2t_offset;
3095
Andy Grover8b1e1242012-04-03 15:51:12 -07003096 if (first_r2t && recovery) {
3097 int new_data_end = offset +
3098 conn->sess->sess_ops->MaxBurstLength -
3099 cmd->next_burst_len;
3100
Andy Groverebf1d952012-04-03 15:51:24 -07003101 if (new_data_end > cmd->se_cmd.data_length)
3102 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003103 else
3104 xfer_len =
3105 conn->sess->sess_ops->MaxBurstLength -
3106 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003108 int new_data_end = offset +
3109 conn->sess->sess_ops->MaxBurstLength;
3110
Andy Groverebf1d952012-04-03 15:51:24 -07003111 if (new_data_end > cmd->se_cmd.data_length)
3112 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 else
3114 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003115 }
3116 cmd->r2t_offset += xfer_len;
3117
Andy Groverebf1d952012-04-03 15:51:24 -07003118 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003119 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3120 } else {
3121 struct iscsi_seq *seq;
3122
3123 seq = iscsit_get_seq_holder_for_r2t(cmd);
3124 if (!seq) {
3125 spin_unlock_bh(&cmd->r2t_lock);
3126 return -1;
3127 }
3128
3129 offset = seq->offset;
3130 xfer_len = seq->xfer_len;
3131
3132 if (cmd->seq_send_order == cmd->seq_count)
3133 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3134 }
3135 cmd->outstanding_r2ts++;
3136 first_r2t = 0;
3137
3138 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3139 spin_unlock_bh(&cmd->r2t_lock);
3140 return -1;
3141 }
3142
3143 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3144 break;
3145 }
3146 spin_unlock_bh(&cmd->r2t_lock);
3147
3148 return 0;
3149}
3150
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003151void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3152 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003153{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003154 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003155 cmd->stat_sn = conn->stat_sn++;
3156
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003157 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003158
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003159 memset(hdr, 0, ISCSI_HDR_LEN);
3160 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3161 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3162 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3163 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003164 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003165 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3166 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003167 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003168 }
3169 hdr->response = cmd->iscsi_response;
3170 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003171 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003172 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3173
3174 iscsit_increment_maxcmdsn(cmd, conn->sess);
3175 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3176 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3177
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003178 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3179 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3180 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3181 cmd->se_cmd.scsi_status, conn->cid);
3182}
3183EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3184
3185static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3186{
3187 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3188 struct kvec *iov;
3189 u32 padding = 0, tx_size = 0;
3190 int iov_count = 0;
3191 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3192
3193 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3194
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003195 iov = &cmd->iov_misc[0];
3196 iov[iov_count].iov_base = cmd->pdu;
3197 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3198 tx_size += ISCSI_HDR_LEN;
3199
3200 /*
3201 * Attach SENSE DATA payload to iSCSI Response PDU
3202 */
3203 if (cmd->se_cmd.sense_buffer &&
3204 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3205 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003206 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3207 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3208
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003209 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003210 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003211 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003212 iov[iov_count++].iov_len =
3213 (cmd->se_cmd.scsi_sense_length + padding);
3214 tx_size += cmd->se_cmd.scsi_sense_length;
3215
3216 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003217 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003218 cmd->se_cmd.scsi_sense_length, 0, padding);
3219 tx_size += padding;
3220 pr_debug("Adding %u bytes of padding to"
3221 " SENSE.\n", padding);
3222 }
3223
3224 if (conn->conn_ops->DataDigest) {
3225 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003226 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003227 (cmd->se_cmd.scsi_sense_length + padding),
3228 0, NULL, (u8 *)&cmd->data_crc);
3229
3230 iov[iov_count].iov_base = &cmd->data_crc;
3231 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3232 tx_size += ISCSI_CRC_LEN;
3233
3234 pr_debug("Attaching CRC32 DataDigest for"
3235 " SENSE, %u bytes CRC 0x%08x\n",
3236 (cmd->se_cmd.scsi_sense_length + padding),
3237 cmd->data_crc);
3238 }
3239
3240 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3241 " Response PDU\n",
3242 cmd->se_cmd.scsi_sense_length);
3243 }
3244
3245 if (conn->conn_ops->HeaderDigest) {
3246 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3247
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003248 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3249 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003250
3251 iov[0].iov_len += ISCSI_CRC_LEN;
3252 tx_size += ISCSI_CRC_LEN;
3253 pr_debug("Attaching CRC32 HeaderDigest for Response"
3254 " PDU 0x%08x\n", *header_digest);
3255 }
3256
3257 cmd->iov_misc_count = iov_count;
3258 cmd->tx_size = tx_size;
3259
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260 return 0;
3261}
3262
3263static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3264{
3265 switch (se_tmr->response) {
3266 case TMR_FUNCTION_COMPLETE:
3267 return ISCSI_TMF_RSP_COMPLETE;
3268 case TMR_TASK_DOES_NOT_EXIST:
3269 return ISCSI_TMF_RSP_NO_TASK;
3270 case TMR_LUN_DOES_NOT_EXIST:
3271 return ISCSI_TMF_RSP_NO_LUN;
3272 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3273 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003274 case TMR_FUNCTION_REJECTED:
3275 default:
3276 return ISCSI_TMF_RSP_REJECTED;
3277 }
3278}
3279
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003280void
3281iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3282 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003283{
3284 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003285
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003286 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003287 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003288 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003289 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003290 cmd->stat_sn = conn->stat_sn++;
3291 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3292
3293 iscsit_increment_maxcmdsn(cmd, conn->sess);
3294 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3295 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3296
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003297 pr_debug("Built Task Management Response ITT: 0x%08x,"
3298 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3299 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3300}
3301EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3302
3303static int
3304iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3305{
3306 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3307 u32 tx_size = 0;
3308
3309 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3310
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003311 cmd->iov_misc[0].iov_base = cmd->pdu;
3312 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3313 tx_size += ISCSI_HDR_LEN;
3314
3315 if (conn->conn_ops->HeaderDigest) {
3316 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3317
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003318 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3319 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003320
3321 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3322 tx_size += ISCSI_CRC_LEN;
3323 pr_debug("Attaching CRC32 HeaderDigest for Task"
3324 " Mgmt Response PDU 0x%08x\n", *header_digest);
3325 }
3326
3327 cmd->iov_misc_count = 1;
3328 cmd->tx_size = tx_size;
3329
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003330 return 0;
3331}
3332
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003333static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3334{
3335 bool ret = false;
3336
3337 if (np->np_sockaddr.ss_family == AF_INET6) {
3338 const struct sockaddr_in6 sin6 = {
3339 .sin6_addr = IN6ADDR_ANY_INIT };
3340 struct sockaddr_in6 *sock_in6 =
3341 (struct sockaddr_in6 *)&np->np_sockaddr;
3342
3343 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3344 sin6.sin6_addr.s6_addr, 16))
3345 ret = true;
3346 } else {
3347 struct sockaddr_in * sock_in =
3348 (struct sockaddr_in *)&np->np_sockaddr;
3349
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003350 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003351 ret = true;
3352 }
3353
3354 return ret;
3355}
3356
Andy Grover8b1e1242012-04-03 15:51:12 -07003357#define SENDTARGETS_BUF_LIMIT 32768U
3358
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003359static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3360{
3361 char *payload = NULL;
3362 struct iscsi_conn *conn = cmd->conn;
3363 struct iscsi_portal_group *tpg;
3364 struct iscsi_tiqn *tiqn;
3365 struct iscsi_tpg_np *tpg_np;
3366 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003367 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003368 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003369 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003370
Andy Grover8b1e1242012-04-03 15:51:12 -07003371 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3372 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003373
3374 payload = kzalloc(buffer_len, GFP_KERNEL);
3375 if (!payload) {
3376 pr_err("Unable to allocate memory for sendtargets"
3377 " response.\n");
3378 return -ENOMEM;
3379 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003380 /*
3381 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3382 * explicit case..
3383 */
3384 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3385 text_ptr = strchr(text_in, '=');
3386 if (!text_ptr) {
3387 pr_err("Unable to locate '=' string in text_in:"
3388 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003389 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003390 return -EINVAL;
3391 }
3392 /*
3393 * Skip over '=' character..
3394 */
3395 text_ptr += 1;
3396 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003397
3398 spin_lock(&tiqn_lock);
3399 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003400 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3401 strcmp(tiqn->tiqn, text_ptr)) {
3402 continue;
3403 }
3404
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003405 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003406
3407 spin_lock(&tiqn->tiqn_tpg_lock);
3408 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3409
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003410 /* If demo_mode_discovery=0 and generate_node_acls=0
3411 * (demo mode dislabed) do not return
3412 * TargetName+TargetAddress unless a NodeACL exists.
3413 */
3414
3415 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3416 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3417 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3418 cmd->conn->sess->sess_ops->InitiatorName))) {
3419 continue;
3420 }
3421
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003422 spin_lock(&tpg->tpg_state_lock);
3423 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3424 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3425 spin_unlock(&tpg->tpg_state_lock);
3426 continue;
3427 }
3428 spin_unlock(&tpg->tpg_state_lock);
3429
3430 spin_lock(&tpg->tpg_np_lock);
3431 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3432 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003433 struct iscsi_np *np = tpg_np->tpg_np;
3434 bool inaddr_any = iscsit_check_inaddr_any(np);
3435
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003436 if (!target_name_printed) {
3437 len = sprintf(buf, "TargetName=%s",
3438 tiqn->tiqn);
3439 len += 1;
3440
3441 if ((len + payload_len) > buffer_len) {
3442 spin_unlock(&tpg->tpg_np_lock);
3443 spin_unlock(&tiqn->tiqn_tpg_lock);
3444 end_of_buf = 1;
3445 goto eob;
3446 }
3447 memcpy(payload + payload_len, buf, len);
3448 payload_len += len;
3449 target_name_printed = 1;
3450 }
3451
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003452 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003453 "%s:%hu,%hu",
3454 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003455 np->np_ip : conn->local_ip,
Chris Leechdfecf612013-08-12 11:26:28 -07003456 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003457 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003458 tpg->tpgt);
3459 len += 1;
3460
3461 if ((len + payload_len) > buffer_len) {
3462 spin_unlock(&tpg->tpg_np_lock);
3463 spin_unlock(&tiqn->tiqn_tpg_lock);
3464 end_of_buf = 1;
3465 goto eob;
3466 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003467 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003468 payload_len += len;
3469 }
3470 spin_unlock(&tpg->tpg_np_lock);
3471 }
3472 spin_unlock(&tiqn->tiqn_tpg_lock);
3473eob:
3474 if (end_of_buf)
3475 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003476
3477 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3478 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003479 }
3480 spin_unlock(&tiqn_lock);
3481
3482 cmd->buf_ptr = payload;
3483
3484 return payload_len;
3485}
3486
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003487int
3488iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3489 struct iscsi_text_rsp *hdr)
3490{
3491 int text_length, padding;
3492
3493 text_length = iscsit_build_sendtargets_response(cmd);
3494 if (text_length < 0)
3495 return text_length;
3496
3497 hdr->opcode = ISCSI_OP_TEXT_RSP;
3498 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3499 padding = ((-text_length) & 3);
3500 hton24(hdr->dlength, text_length);
3501 hdr->itt = cmd->init_task_tag;
3502 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3503 cmd->stat_sn = conn->stat_sn++;
3504 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3505
3506 iscsit_increment_maxcmdsn(cmd, conn->sess);
3507 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3508 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3509
3510 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3511 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3512 text_length, conn->cid);
3513
3514 return text_length + padding;
3515}
3516EXPORT_SYMBOL(iscsit_build_text_rsp);
3517
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003518/*
3519 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3520 * MaxRecvDataSegmentLength.
3521 */
3522static int iscsit_send_text_rsp(
3523 struct iscsi_cmd *cmd,
3524 struct iscsi_conn *conn)
3525{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003526 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003527 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003528 u32 tx_size = 0;
3529 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003530
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003531 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3532 if (rc < 0)
3533 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003534
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003535 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003536 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003537 iov[iov_count].iov_base = cmd->pdu;
3538 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3539 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003540 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003541
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003542 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003543
3544 if (conn->conn_ops->HeaderDigest) {
3545 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3546
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003547 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3548 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003549
3550 iov[0].iov_len += ISCSI_CRC_LEN;
3551 tx_size += ISCSI_CRC_LEN;
3552 pr_debug("Attaching CRC32 HeaderDigest for"
3553 " Text Response PDU 0x%08x\n", *header_digest);
3554 }
3555
3556 if (conn->conn_ops->DataDigest) {
3557 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003558 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003559 0, NULL, (u8 *)&cmd->data_crc);
3560
3561 iov[iov_count].iov_base = &cmd->data_crc;
3562 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3563 tx_size += ISCSI_CRC_LEN;
3564
3565 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003566 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003567 cmd->data_crc);
3568 }
3569
3570 cmd->iov_misc_count = iov_count;
3571 cmd->tx_size = tx_size;
3572
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003573 return 0;
3574}
3575
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003576void
3577iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3578 struct iscsi_reject *hdr)
3579{
3580 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003581 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003582 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3583 hton24(hdr->dlength, ISCSI_HDR_LEN);
3584 hdr->ffffffff = cpu_to_be32(0xffffffff);
3585 cmd->stat_sn = conn->stat_sn++;
3586 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3587 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3588 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3589
3590}
3591EXPORT_SYMBOL(iscsit_build_reject);
3592
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003593static int iscsit_send_reject(
3594 struct iscsi_cmd *cmd,
3595 struct iscsi_conn *conn)
3596{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003597 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003598 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003599 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003600
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003601 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003602
3603 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003604 iov[iov_count].iov_base = cmd->pdu;
3605 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3606 iov[iov_count].iov_base = cmd->buf_ptr;
3607 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3608
3609 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3610
3611 if (conn->conn_ops->HeaderDigest) {
3612 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3613
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003614 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3615 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003616
3617 iov[0].iov_len += ISCSI_CRC_LEN;
3618 tx_size += ISCSI_CRC_LEN;
3619 pr_debug("Attaching CRC32 HeaderDigest for"
3620 " REJECT PDU 0x%08x\n", *header_digest);
3621 }
3622
3623 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003624 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3625 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003626
3627 iov[iov_count].iov_base = &cmd->data_crc;
3628 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3629 tx_size += ISCSI_CRC_LEN;
3630 pr_debug("Attaching CRC32 DataDigest for REJECT"
3631 " PDU 0x%08x\n", cmd->data_crc);
3632 }
3633
3634 cmd->iov_misc_count = iov_count;
3635 cmd->tx_size = tx_size;
3636
3637 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3638 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3639
3640 return 0;
3641}
3642
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003643void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3644{
3645 struct iscsi_thread_set *ts = conn->thread_set;
3646 int ord, cpu;
3647 /*
3648 * thread_id is assigned from iscsit_global->ts_bitmap from
3649 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3650 *
3651 * Here we use thread_id to determine which CPU that this
3652 * iSCSI connection's iscsi_thread_set will be scheduled to
3653 * execute upon.
3654 */
3655 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003656 for_each_online_cpu(cpu) {
3657 if (ord-- == 0) {
3658 cpumask_set_cpu(cpu, conn->conn_cpumask);
3659 return;
3660 }
3661 }
3662 /*
3663 * This should never be reached..
3664 */
3665 dump_stack();
3666 cpumask_setall(conn->conn_cpumask);
3667}
3668
3669static inline void iscsit_thread_check_cpumask(
3670 struct iscsi_conn *conn,
3671 struct task_struct *p,
3672 int mode)
3673{
3674 char buf[128];
3675 /*
3676 * mode == 1 signals iscsi_target_tx_thread() usage.
3677 * mode == 0 signals iscsi_target_rx_thread() usage.
3678 */
3679 if (mode == 1) {
3680 if (!conn->conn_tx_reset_cpumask)
3681 return;
3682 conn->conn_tx_reset_cpumask = 0;
3683 } else {
3684 if (!conn->conn_rx_reset_cpumask)
3685 return;
3686 conn->conn_rx_reset_cpumask = 0;
3687 }
3688 /*
3689 * Update the CPU mask for this single kthread so that
3690 * both TX and RX kthreads are scheduled to run on the
3691 * same CPU.
3692 */
3693 memset(buf, 0, 128);
3694 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003695 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3696}
3697
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003698static int
3699iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003700{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003701 int ret;
3702
3703 switch (state) {
3704 case ISTATE_SEND_R2T:
3705 ret = iscsit_send_r2t(cmd, conn);
3706 if (ret < 0)
3707 goto err;
3708 break;
3709 case ISTATE_REMOVE:
3710 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003711 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003712 spin_unlock_bh(&conn->cmd_lock);
3713
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003714 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003715 break;
3716 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3717 iscsit_mod_nopin_response_timer(conn);
3718 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3719 if (ret < 0)
3720 goto err;
3721 break;
3722 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3723 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3724 if (ret < 0)
3725 goto err;
3726 break;
3727 default:
3728 pr_err("Unknown Opcode: 0x%02x ITT:"
3729 " 0x%08x, i_state: %d on CID: %hu\n",
3730 cmd->iscsi_opcode, cmd->init_task_tag, state,
3731 conn->cid);
3732 goto err;
3733 }
3734
3735 return 0;
3736
3737err:
3738 return -1;
3739}
3740
3741static int
3742iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3743{
3744 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003745 struct iscsi_queue_req *qr;
3746 struct iscsi_cmd *cmd;
3747 u8 state;
3748 int ret;
3749
3750 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3751 atomic_set(&conn->check_immediate_queue, 0);
3752 cmd = qr->cmd;
3753 state = qr->state;
3754 kmem_cache_free(lio_qr_cache, qr);
3755
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003756 ret = t->iscsit_immediate_queue(conn, cmd, state);
3757 if (ret < 0)
3758 return ret;
3759 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003760
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003761 return 0;
3762}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003763
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003764static int
3765iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3766{
3767 int ret;
3768
3769check_rsp_state:
3770 switch (state) {
3771 case ISTATE_SEND_DATAIN:
3772 ret = iscsit_send_datain(cmd, conn);
3773 if (ret < 0)
3774 goto err;
3775 else if (!ret)
3776 /* more drs */
3777 goto check_rsp_state;
3778 else if (ret == 1) {
3779 /* all done */
3780 spin_lock_bh(&cmd->istate_lock);
3781 cmd->i_state = ISTATE_SENT_STATUS;
3782 spin_unlock_bh(&cmd->istate_lock);
3783
3784 if (atomic_read(&conn->check_immediate_queue))
3785 return 1;
3786
3787 return 0;
3788 } else if (ret == 2) {
3789 /* Still must send status,
3790 SCF_TRANSPORT_TASK_SENSE was set */
3791 spin_lock_bh(&cmd->istate_lock);
3792 cmd->i_state = ISTATE_SEND_STATUS;
3793 spin_unlock_bh(&cmd->istate_lock);
3794 state = ISTATE_SEND_STATUS;
3795 goto check_rsp_state;
3796 }
3797
3798 break;
3799 case ISTATE_SEND_STATUS:
3800 case ISTATE_SEND_STATUS_RECOVERY:
3801 ret = iscsit_send_response(cmd, conn);
3802 break;
3803 case ISTATE_SEND_LOGOUTRSP:
3804 ret = iscsit_send_logout(cmd, conn);
3805 break;
3806 case ISTATE_SEND_ASYNCMSG:
3807 ret = iscsit_send_conn_drop_async_message(
3808 cmd, conn);
3809 break;
3810 case ISTATE_SEND_NOPIN:
3811 ret = iscsit_send_nopin(cmd, conn);
3812 break;
3813 case ISTATE_SEND_REJECT:
3814 ret = iscsit_send_reject(cmd, conn);
3815 break;
3816 case ISTATE_SEND_TASKMGTRSP:
3817 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3818 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003819 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003820 ret = iscsit_tmr_post_handler(cmd, conn);
3821 if (ret != 0)
3822 iscsit_fall_back_to_erl0(conn->sess);
3823 break;
3824 case ISTATE_SEND_TEXTRSP:
3825 ret = iscsit_send_text_rsp(cmd, conn);
3826 break;
3827 default:
3828 pr_err("Unknown Opcode: 0x%02x ITT:"
3829 " 0x%08x, i_state: %d on CID: %hu\n",
3830 cmd->iscsi_opcode, cmd->init_task_tag,
3831 state, conn->cid);
3832 goto err;
3833 }
3834 if (ret < 0)
3835 goto err;
3836
3837 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3838 iscsit_tx_thread_wait_for_tcp(conn);
3839 iscsit_unmap_iovec(cmd);
3840 goto err;
3841 }
3842 iscsit_unmap_iovec(cmd);
3843
3844 switch (state) {
3845 case ISTATE_SEND_LOGOUTRSP:
3846 if (!iscsit_logout_post_handler(cmd, conn))
3847 goto restart;
3848 /* fall through */
3849 case ISTATE_SEND_STATUS:
3850 case ISTATE_SEND_ASYNCMSG:
3851 case ISTATE_SEND_NOPIN:
3852 case ISTATE_SEND_STATUS_RECOVERY:
3853 case ISTATE_SEND_TEXTRSP:
3854 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003855 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003856 spin_lock_bh(&cmd->istate_lock);
3857 cmd->i_state = ISTATE_SENT_STATUS;
3858 spin_unlock_bh(&cmd->istate_lock);
3859 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003860 default:
3861 pr_err("Unknown Opcode: 0x%02x ITT:"
3862 " 0x%08x, i_state: %d on CID: %hu\n",
3863 cmd->iscsi_opcode, cmd->init_task_tag,
3864 cmd->i_state, conn->cid);
3865 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003866 }
3867
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003868 if (atomic_read(&conn->check_immediate_queue))
3869 return 1;
3870
Andy Grover6f3c0e62012-04-03 15:51:09 -07003871 return 0;
3872
3873err:
3874 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003875restart:
3876 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003877}
3878
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003879static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003880{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003881 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003882 struct iscsi_queue_req *qr;
3883 struct iscsi_cmd *cmd;
3884 u8 state;
3885 int ret;
3886
3887 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3888 cmd = qr->cmd;
3889 state = qr->state;
3890 kmem_cache_free(lio_qr_cache, qr);
3891
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003892 ret = t->iscsit_response_queue(conn, cmd, state);
3893 if (ret == 1 || ret < 0)
3894 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003895 }
3896
3897 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003898}
3899
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003900int iscsi_target_tx_thread(void *arg)
3901{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003902 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003903 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003904 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003905 /*
3906 * Allow ourselves to be interrupted by SIGINT so that a
3907 * connection recovery / failure event can be triggered externally.
3908 */
3909 allow_signal(SIGINT);
3910
3911restart:
3912 conn = iscsi_tx_thread_pre_handler(ts);
3913 if (!conn)
3914 goto out;
3915
Andy Grover6f3c0e62012-04-03 15:51:09 -07003916 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003917
3918 while (!kthread_should_stop()) {
3919 /*
3920 * Ensure that both TX and RX per connection kthreads
3921 * are scheduled to run on the same CPU.
3922 */
3923 iscsit_thread_check_cpumask(conn, current, 1);
3924
Roland Dreierd5627ac2012-10-31 09:16:46 -07003925 wait_event_interruptible(conn->queues_wq,
3926 !iscsit_conn_all_queues_empty(conn) ||
3927 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003928
3929 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3930 signal_pending(current))
3931 goto transport_err;
3932
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003933get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003934 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003935 if (ret < 0)
3936 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003937
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003938 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003939 if (ret == 1)
3940 goto get_immediate;
3941 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003942 goto restart;
3943 else if (ret < 0)
3944 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003945 }
3946
3947transport_err:
3948 iscsit_take_action_for_connection_exit(conn);
3949 goto restart;
3950out:
3951 return 0;
3952}
3953
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003954static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3955{
3956 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3957 struct iscsi_cmd *cmd;
3958 int ret = 0;
3959
3960 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3961 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003962 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003963 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003964 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003965
3966 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3967 break;
3968 case ISCSI_OP_SCSI_DATA_OUT:
3969 ret = iscsit_handle_data_out(conn, buf);
3970 break;
3971 case ISCSI_OP_NOOP_OUT:
3972 cmd = NULL;
3973 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003974 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003975 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003976 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003977 }
3978 ret = iscsit_handle_nop_out(conn, cmd, buf);
3979 break;
3980 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003981 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003982 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003983 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003984
3985 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3986 break;
3987 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003988 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003989 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003990 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003991
3992 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003993 break;
3994 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003995 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003996 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003997 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003998
3999 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4000 if (ret > 0)
4001 wait_for_completion_timeout(&conn->conn_logout_comp,
4002 SECONDS_FOR_LOGOUT_COMP * HZ);
4003 break;
4004 case ISCSI_OP_SNACK:
4005 ret = iscsit_handle_snack(conn, buf);
4006 break;
4007 default:
4008 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4009 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4010 pr_err("Cannot recover from unknown"
4011 " opcode while ERL=0, closing iSCSI connection.\n");
4012 return -1;
4013 }
4014 if (!conn->conn_ops->OFMarker) {
4015 pr_err("Unable to recover from unknown"
4016 " opcode while OFMarker=No, closing iSCSI"
4017 " connection.\n");
4018 return -1;
4019 }
4020 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4021 pr_err("Unable to recover from unknown"
4022 " opcode, closing iSCSI connection.\n");
4023 return -1;
4024 }
4025 break;
4026 }
4027
4028 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004029reject:
4030 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004031}
4032
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004033int iscsi_target_rx_thread(void *arg)
4034{
4035 int ret;
4036 u8 buffer[ISCSI_HDR_LEN], opcode;
4037 u32 checksum = 0, digest = 0;
4038 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004039 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004040 struct kvec iov;
4041 /*
4042 * Allow ourselves to be interrupted by SIGINT so that a
4043 * connection recovery / failure event can be triggered externally.
4044 */
4045 allow_signal(SIGINT);
4046
4047restart:
4048 conn = iscsi_rx_thread_pre_handler(ts);
4049 if (!conn)
4050 goto out;
4051
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004052 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4053 struct completion comp;
4054 int rc;
4055
4056 init_completion(&comp);
4057 rc = wait_for_completion_interruptible(&comp);
4058 if (rc < 0)
4059 goto transport_err;
4060
4061 goto out;
4062 }
4063
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004064 while (!kthread_should_stop()) {
4065 /*
4066 * Ensure that both TX and RX per connection kthreads
4067 * are scheduled to run on the same CPU.
4068 */
4069 iscsit_thread_check_cpumask(conn, current, 0);
4070
4071 memset(buffer, 0, ISCSI_HDR_LEN);
4072 memset(&iov, 0, sizeof(struct kvec));
4073
4074 iov.iov_base = buffer;
4075 iov.iov_len = ISCSI_HDR_LEN;
4076
4077 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4078 if (ret != ISCSI_HDR_LEN) {
4079 iscsit_rx_thread_wait_for_tcp(conn);
4080 goto transport_err;
4081 }
4082
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004083 if (conn->conn_ops->HeaderDigest) {
4084 iov.iov_base = &digest;
4085 iov.iov_len = ISCSI_CRC_LEN;
4086
4087 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4088 if (ret != ISCSI_CRC_LEN) {
4089 iscsit_rx_thread_wait_for_tcp(conn);
4090 goto transport_err;
4091 }
4092
4093 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4094 buffer, ISCSI_HDR_LEN,
4095 0, NULL, (u8 *)&checksum);
4096
4097 if (digest != checksum) {
4098 pr_err("HeaderDigest CRC32C failed,"
4099 " received 0x%08x, computed 0x%08x\n",
4100 digest, checksum);
4101 /*
4102 * Set the PDU to 0xff so it will intentionally
4103 * hit default in the switch below.
4104 */
4105 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004106 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004107 } else {
4108 pr_debug("Got HeaderDigest CRC32C"
4109 " 0x%08x\n", checksum);
4110 }
4111 }
4112
4113 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4114 goto transport_err;
4115
4116 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4117
4118 if (conn->sess->sess_ops->SessionType &&
4119 ((!(opcode & ISCSI_OP_TEXT)) ||
4120 (!(opcode & ISCSI_OP_LOGOUT)))) {
4121 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4122 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004123 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4124 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004125 goto transport_err;
4126 }
4127
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004128 ret = iscsi_target_rx_opcode(conn, buffer);
4129 if (ret < 0)
4130 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004131 }
4132
4133transport_err:
4134 if (!signal_pending(current))
4135 atomic_set(&conn->transport_failed, 1);
4136 iscsit_take_action_for_connection_exit(conn);
4137 goto restart;
4138out:
4139 return 0;
4140}
4141
4142static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4143{
4144 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4145 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004146 /*
4147 * We expect this function to only ever be called from either RX or TX
4148 * thread context via iscsit_close_connection() once the other context
4149 * has been reset -> returned sleeping pre-handler state.
4150 */
4151 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004152 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004153
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004154 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004155 spin_unlock_bh(&conn->cmd_lock);
4156
4157 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004158
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004159 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004160
4161 spin_lock_bh(&conn->cmd_lock);
4162 }
4163 spin_unlock_bh(&conn->cmd_lock);
4164}
4165
4166static void iscsit_stop_timers_for_cmds(
4167 struct iscsi_conn *conn)
4168{
4169 struct iscsi_cmd *cmd;
4170
4171 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004172 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004173 if (cmd->data_direction == DMA_TO_DEVICE)
4174 iscsit_stop_dataout_timer(cmd);
4175 }
4176 spin_unlock_bh(&conn->cmd_lock);
4177}
4178
4179int iscsit_close_connection(
4180 struct iscsi_conn *conn)
4181{
4182 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4183 struct iscsi_session *sess = conn->sess;
4184
4185 pr_debug("Closing iSCSI connection CID %hu on SID:"
4186 " %u\n", conn->cid, sess->sid);
4187 /*
4188 * Always up conn_logout_comp just in case the RX Thread is sleeping
4189 * and the logout response never got sent because the connection
4190 * failed.
4191 */
4192 complete(&conn->conn_logout_comp);
4193
4194 iscsi_release_thread_set(conn);
4195
4196 iscsit_stop_timers_for_cmds(conn);
4197 iscsit_stop_nopin_response_timer(conn);
4198 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004199
4200 if (conn->conn_transport->iscsit_wait_conn)
4201 conn->conn_transport->iscsit_wait_conn(conn);
4202
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004203 iscsit_free_queue_reqs_for_conn(conn);
4204
4205 /*
4206 * During Connection recovery drop unacknowledged out of order
4207 * commands for this connection, and prepare the other commands
4208 * for realligence.
4209 *
4210 * During normal operation clear the out of order commands (but
4211 * do not free the struct iscsi_ooo_cmdsn's) and release all
4212 * struct iscsi_cmds.
4213 */
4214 if (atomic_read(&conn->connection_recovery)) {
4215 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4216 iscsit_prepare_cmds_for_realligance(conn);
4217 } else {
4218 iscsit_clear_ooo_cmdsns_for_conn(conn);
4219 iscsit_release_commands_from_conn(conn);
4220 }
4221
4222 /*
4223 * Handle decrementing session or connection usage count if
4224 * a logout response was not able to be sent because the
4225 * connection failed. Fall back to Session Recovery here.
4226 */
4227 if (atomic_read(&conn->conn_logout_remove)) {
4228 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4229 iscsit_dec_conn_usage_count(conn);
4230 iscsit_dec_session_usage_count(sess);
4231 }
4232 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4233 iscsit_dec_conn_usage_count(conn);
4234
4235 atomic_set(&conn->conn_logout_remove, 0);
4236 atomic_set(&sess->session_reinstatement, 0);
4237 atomic_set(&sess->session_fall_back_to_erl0, 1);
4238 }
4239
4240 spin_lock_bh(&sess->conn_lock);
4241 list_del(&conn->conn_list);
4242
4243 /*
4244 * Attempt to let the Initiator know this connection failed by
4245 * sending an Connection Dropped Async Message on another
4246 * active connection.
4247 */
4248 if (atomic_read(&conn->connection_recovery))
4249 iscsit_build_conn_drop_async_message(conn);
4250
4251 spin_unlock_bh(&sess->conn_lock);
4252
4253 /*
4254 * If connection reinstatement is being performed on this connection,
4255 * up the connection reinstatement semaphore that is being blocked on
4256 * in iscsit_cause_connection_reinstatement().
4257 */
4258 spin_lock_bh(&conn->state_lock);
4259 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4260 spin_unlock_bh(&conn->state_lock);
4261 complete(&conn->conn_wait_comp);
4262 wait_for_completion(&conn->conn_post_wait_comp);
4263 spin_lock_bh(&conn->state_lock);
4264 }
4265
4266 /*
4267 * If connection reinstatement is being performed on this connection
4268 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4269 * connection wait rcfr semaphore that is being blocked on
4270 * an iscsit_connection_reinstatement_rcfr().
4271 */
4272 if (atomic_read(&conn->connection_wait_rcfr)) {
4273 spin_unlock_bh(&conn->state_lock);
4274 complete(&conn->conn_wait_rcfr_comp);
4275 wait_for_completion(&conn->conn_post_wait_comp);
4276 spin_lock_bh(&conn->state_lock);
4277 }
4278 atomic_set(&conn->connection_reinstatement, 1);
4279 spin_unlock_bh(&conn->state_lock);
4280
4281 /*
4282 * If any other processes are accessing this connection pointer we
4283 * must wait until they have completed.
4284 */
4285 iscsit_check_conn_usage_count(conn);
4286
4287 if (conn->conn_rx_hash.tfm)
4288 crypto_free_hash(conn->conn_rx_hash.tfm);
4289 if (conn->conn_tx_hash.tfm)
4290 crypto_free_hash(conn->conn_tx_hash.tfm);
4291
4292 if (conn->conn_cpumask)
4293 free_cpumask_var(conn->conn_cpumask);
4294
4295 kfree(conn->conn_ops);
4296 conn->conn_ops = NULL;
4297
Al Virobf6932f2012-07-21 08:55:18 +01004298 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004299 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004300
4301 if (conn->conn_transport->iscsit_free_conn)
4302 conn->conn_transport->iscsit_free_conn(conn);
4303
4304 iscsit_put_transport(conn->conn_transport);
4305
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004306 conn->thread_set = NULL;
4307
4308 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4309 conn->conn_state = TARG_CONN_STATE_FREE;
4310 kfree(conn);
4311
4312 spin_lock_bh(&sess->conn_lock);
4313 atomic_dec(&sess->nconn);
4314 pr_debug("Decremented iSCSI connection count to %hu from node:"
4315 " %s\n", atomic_read(&sess->nconn),
4316 sess->sess_ops->InitiatorName);
4317 /*
4318 * Make sure that if one connection fails in an non ERL=2 iSCSI
4319 * Session that they all fail.
4320 */
4321 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4322 !atomic_read(&sess->session_logout))
4323 atomic_set(&sess->session_fall_back_to_erl0, 1);
4324
4325 /*
4326 * If this was not the last connection in the session, and we are
4327 * performing session reinstatement or falling back to ERL=0, call
4328 * iscsit_stop_session() without sleeping to shutdown the other
4329 * active connections.
4330 */
4331 if (atomic_read(&sess->nconn)) {
4332 if (!atomic_read(&sess->session_reinstatement) &&
4333 !atomic_read(&sess->session_fall_back_to_erl0)) {
4334 spin_unlock_bh(&sess->conn_lock);
4335 return 0;
4336 }
4337 if (!atomic_read(&sess->session_stop_active)) {
4338 atomic_set(&sess->session_stop_active, 1);
4339 spin_unlock_bh(&sess->conn_lock);
4340 iscsit_stop_session(sess, 0, 0);
4341 return 0;
4342 }
4343 spin_unlock_bh(&sess->conn_lock);
4344 return 0;
4345 }
4346
4347 /*
4348 * If this was the last connection in the session and one of the
4349 * following is occurring:
4350 *
4351 * Session Reinstatement is not being performed, and are falling back
4352 * to ERL=0 call iscsit_close_session().
4353 *
4354 * Session Logout was requested. iscsit_close_session() will be called
4355 * elsewhere.
4356 *
4357 * Session Continuation is not being performed, start the Time2Retain
4358 * handler and check if sleep_on_sess_wait_sem is active.
4359 */
4360 if (!atomic_read(&sess->session_reinstatement) &&
4361 atomic_read(&sess->session_fall_back_to_erl0)) {
4362 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004363 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004364
4365 return 0;
4366 } else if (atomic_read(&sess->session_logout)) {
4367 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4368 sess->session_state = TARG_SESS_STATE_FREE;
4369 spin_unlock_bh(&sess->conn_lock);
4370
4371 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4372 complete(&sess->session_wait_comp);
4373
4374 return 0;
4375 } else {
4376 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4377 sess->session_state = TARG_SESS_STATE_FAILED;
4378
4379 if (!atomic_read(&sess->session_continuation)) {
4380 spin_unlock_bh(&sess->conn_lock);
4381 iscsit_start_time2retain_handler(sess);
4382 } else
4383 spin_unlock_bh(&sess->conn_lock);
4384
4385 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4386 complete(&sess->session_wait_comp);
4387
4388 return 0;
4389 }
4390 spin_unlock_bh(&sess->conn_lock);
4391
4392 return 0;
4393}
4394
4395int iscsit_close_session(struct iscsi_session *sess)
4396{
Andy Grover60bfcf82013-10-09 11:05:58 -07004397 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004398 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4399
4400 if (atomic_read(&sess->nconn)) {
4401 pr_err("%d connection(s) still exist for iSCSI session"
4402 " to %s\n", atomic_read(&sess->nconn),
4403 sess->sess_ops->InitiatorName);
4404 BUG();
4405 }
4406
4407 spin_lock_bh(&se_tpg->session_lock);
4408 atomic_set(&sess->session_logout, 1);
4409 atomic_set(&sess->session_reinstatement, 1);
4410 iscsit_stop_time2retain_timer(sess);
4411 spin_unlock_bh(&se_tpg->session_lock);
4412
4413 /*
4414 * transport_deregister_session_configfs() will clear the
4415 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4416 * can be setting it again with __transport_register_session() in
4417 * iscsi_post_login_handler() again after the iscsit_stop_session()
4418 * completes in iscsi_np context.
4419 */
4420 transport_deregister_session_configfs(sess->se_sess);
4421
4422 /*
4423 * If any other processes are accessing this session pointer we must
4424 * wait until they have completed. If we are in an interrupt (the
4425 * time2retain handler) and contain and active session usage count we
4426 * restart the timer and exit.
4427 */
4428 if (!in_interrupt()) {
4429 if (iscsit_check_session_usage_count(sess) == 1)
4430 iscsit_stop_session(sess, 1, 1);
4431 } else {
4432 if (iscsit_check_session_usage_count(sess) == 2) {
4433 atomic_set(&sess->session_logout, 0);
4434 iscsit_start_time2retain_handler(sess);
4435 return 0;
4436 }
4437 }
4438
4439 transport_deregister_session(sess->se_sess);
4440
4441 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4442 iscsit_free_connection_recovery_entires(sess);
4443
4444 iscsit_free_all_ooo_cmdsns(sess);
4445
4446 spin_lock_bh(&se_tpg->session_lock);
4447 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4448 sess->session_state = TARG_SESS_STATE_FREE;
4449 pr_debug("Released iSCSI session from node: %s\n",
4450 sess->sess_ops->InitiatorName);
4451 tpg->nsessions--;
4452 if (tpg->tpg_tiqn)
4453 tpg->tpg_tiqn->tiqn_nsessions--;
4454
4455 pr_debug("Decremented number of active iSCSI Sessions on"
4456 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4457
4458 spin_lock(&sess_idr_lock);
4459 idr_remove(&sess_idr, sess->session_index);
4460 spin_unlock(&sess_idr_lock);
4461
4462 kfree(sess->sess_ops);
4463 sess->sess_ops = NULL;
4464 spin_unlock_bh(&se_tpg->session_lock);
4465
4466 kfree(sess);
4467 return 0;
4468}
4469
4470static void iscsit_logout_post_handler_closesession(
4471 struct iscsi_conn *conn)
4472{
4473 struct iscsi_session *sess = conn->sess;
4474
4475 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4476 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4477
4478 atomic_set(&conn->conn_logout_remove, 0);
4479 complete(&conn->conn_logout_comp);
4480
4481 iscsit_dec_conn_usage_count(conn);
4482 iscsit_stop_session(sess, 1, 1);
4483 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004484 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004485}
4486
4487static void iscsit_logout_post_handler_samecid(
4488 struct iscsi_conn *conn)
4489{
4490 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4491 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4492
4493 atomic_set(&conn->conn_logout_remove, 0);
4494 complete(&conn->conn_logout_comp);
4495
4496 iscsit_cause_connection_reinstatement(conn, 1);
4497 iscsit_dec_conn_usage_count(conn);
4498}
4499
4500static void iscsit_logout_post_handler_diffcid(
4501 struct iscsi_conn *conn,
4502 u16 cid)
4503{
4504 struct iscsi_conn *l_conn;
4505 struct iscsi_session *sess = conn->sess;
4506
4507 if (!sess)
4508 return;
4509
4510 spin_lock_bh(&sess->conn_lock);
4511 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4512 if (l_conn->cid == cid) {
4513 iscsit_inc_conn_usage_count(l_conn);
4514 break;
4515 }
4516 }
4517 spin_unlock_bh(&sess->conn_lock);
4518
4519 if (!l_conn)
4520 return;
4521
4522 if (l_conn->sock)
4523 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4524
4525 spin_lock_bh(&l_conn->state_lock);
4526 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4527 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4528 spin_unlock_bh(&l_conn->state_lock);
4529
4530 iscsit_cause_connection_reinstatement(l_conn, 1);
4531 iscsit_dec_conn_usage_count(l_conn);
4532}
4533
4534/*
4535 * Return of 0 causes the TX thread to restart.
4536 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004537int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004538 struct iscsi_cmd *cmd,
4539 struct iscsi_conn *conn)
4540{
4541 int ret = 0;
4542
4543 switch (cmd->logout_reason) {
4544 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4545 switch (cmd->logout_response) {
4546 case ISCSI_LOGOUT_SUCCESS:
4547 case ISCSI_LOGOUT_CLEANUP_FAILED:
4548 default:
4549 iscsit_logout_post_handler_closesession(conn);
4550 break;
4551 }
4552 ret = 0;
4553 break;
4554 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4555 if (conn->cid == cmd->logout_cid) {
4556 switch (cmd->logout_response) {
4557 case ISCSI_LOGOUT_SUCCESS:
4558 case ISCSI_LOGOUT_CLEANUP_FAILED:
4559 default:
4560 iscsit_logout_post_handler_samecid(conn);
4561 break;
4562 }
4563 ret = 0;
4564 } else {
4565 switch (cmd->logout_response) {
4566 case ISCSI_LOGOUT_SUCCESS:
4567 iscsit_logout_post_handler_diffcid(conn,
4568 cmd->logout_cid);
4569 break;
4570 case ISCSI_LOGOUT_CID_NOT_FOUND:
4571 case ISCSI_LOGOUT_CLEANUP_FAILED:
4572 default:
4573 break;
4574 }
4575 ret = 1;
4576 }
4577 break;
4578 case ISCSI_LOGOUT_REASON_RECOVERY:
4579 switch (cmd->logout_response) {
4580 case ISCSI_LOGOUT_SUCCESS:
4581 case ISCSI_LOGOUT_CID_NOT_FOUND:
4582 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4583 case ISCSI_LOGOUT_CLEANUP_FAILED:
4584 default:
4585 break;
4586 }
4587 ret = 1;
4588 break;
4589 default:
4590 break;
4591
4592 }
4593 return ret;
4594}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004595EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004596
4597void iscsit_fail_session(struct iscsi_session *sess)
4598{
4599 struct iscsi_conn *conn;
4600
4601 spin_lock_bh(&sess->conn_lock);
4602 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4603 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4604 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4605 }
4606 spin_unlock_bh(&sess->conn_lock);
4607
4608 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4609 sess->session_state = TARG_SESS_STATE_FAILED;
4610}
4611
4612int iscsit_free_session(struct iscsi_session *sess)
4613{
4614 u16 conn_count = atomic_read(&sess->nconn);
4615 struct iscsi_conn *conn, *conn_tmp = NULL;
4616 int is_last;
4617
4618 spin_lock_bh(&sess->conn_lock);
4619 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4620
4621 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4622 conn_list) {
4623 if (conn_count == 0)
4624 break;
4625
4626 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4627 is_last = 1;
4628 } else {
4629 iscsit_inc_conn_usage_count(conn_tmp);
4630 is_last = 0;
4631 }
4632 iscsit_inc_conn_usage_count(conn);
4633
4634 spin_unlock_bh(&sess->conn_lock);
4635 iscsit_cause_connection_reinstatement(conn, 1);
4636 spin_lock_bh(&sess->conn_lock);
4637
4638 iscsit_dec_conn_usage_count(conn);
4639 if (is_last == 0)
4640 iscsit_dec_conn_usage_count(conn_tmp);
4641
4642 conn_count--;
4643 }
4644
4645 if (atomic_read(&sess->nconn)) {
4646 spin_unlock_bh(&sess->conn_lock);
4647 wait_for_completion(&sess->session_wait_comp);
4648 } else
4649 spin_unlock_bh(&sess->conn_lock);
4650
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004651 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004652 return 0;
4653}
4654
4655void iscsit_stop_session(
4656 struct iscsi_session *sess,
4657 int session_sleep,
4658 int connection_sleep)
4659{
4660 u16 conn_count = atomic_read(&sess->nconn);
4661 struct iscsi_conn *conn, *conn_tmp = NULL;
4662 int is_last;
4663
4664 spin_lock_bh(&sess->conn_lock);
4665 if (session_sleep)
4666 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4667
4668 if (connection_sleep) {
4669 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4670 conn_list) {
4671 if (conn_count == 0)
4672 break;
4673
4674 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4675 is_last = 1;
4676 } else {
4677 iscsit_inc_conn_usage_count(conn_tmp);
4678 is_last = 0;
4679 }
4680 iscsit_inc_conn_usage_count(conn);
4681
4682 spin_unlock_bh(&sess->conn_lock);
4683 iscsit_cause_connection_reinstatement(conn, 1);
4684 spin_lock_bh(&sess->conn_lock);
4685
4686 iscsit_dec_conn_usage_count(conn);
4687 if (is_last == 0)
4688 iscsit_dec_conn_usage_count(conn_tmp);
4689 conn_count--;
4690 }
4691 } else {
4692 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4693 iscsit_cause_connection_reinstatement(conn, 0);
4694 }
4695
4696 if (session_sleep && atomic_read(&sess->nconn)) {
4697 spin_unlock_bh(&sess->conn_lock);
4698 wait_for_completion(&sess->session_wait_comp);
4699 } else
4700 spin_unlock_bh(&sess->conn_lock);
4701}
4702
4703int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4704{
4705 struct iscsi_session *sess;
4706 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4707 struct se_session *se_sess, *se_sess_tmp;
4708 int session_count = 0;
4709
4710 spin_lock_bh(&se_tpg->session_lock);
4711 if (tpg->nsessions && !force) {
4712 spin_unlock_bh(&se_tpg->session_lock);
4713 return -1;
4714 }
4715
4716 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4717 sess_list) {
4718 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4719
4720 spin_lock(&sess->conn_lock);
4721 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4722 atomic_read(&sess->session_logout) ||
4723 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4724 spin_unlock(&sess->conn_lock);
4725 continue;
4726 }
4727 atomic_set(&sess->session_reinstatement, 1);
4728 spin_unlock(&sess->conn_lock);
4729 spin_unlock_bh(&se_tpg->session_lock);
4730
4731 iscsit_free_session(sess);
4732 spin_lock_bh(&se_tpg->session_lock);
4733
4734 session_count++;
4735 }
4736 spin_unlock_bh(&se_tpg->session_lock);
4737
4738 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4739 " Group: %hu\n", session_count, tpg->tpgt);
4740 return 0;
4741}
4742
4743MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4744MODULE_VERSION("4.1.x");
4745MODULE_AUTHOR("nab@Linux-iSCSI.org");
4746MODULE_LICENSE("GPL");
4747
4748module_init(iscsi_target_init_module);
4749module_exit(iscsi_target_cleanup_module);