blob: df0456abc4fd3fb1972f0e3614a266dce791325f [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
Arshad Hussaina3662602014-03-14 15:28:59 -07001506 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1507 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1508 if (!cmd)
1509 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1510 (unsigned char *)hdr);
1511
1512 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1513 (unsigned char *)hdr);
1514 }
1515
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001516 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001517 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1518 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001519 if (!cmd)
1520 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1521 (unsigned char *)hdr);
1522
Nicholas Bellingerba159912013-07-03 03:48:24 -07001523 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1524 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001525 }
1526
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001527 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001528 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001529 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001530 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001531 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001532 if (!cmd)
1533 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1534 (unsigned char *)hdr);
1535
Nicholas Bellingerba159912013-07-03 03:48:24 -07001536 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1537 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001538 }
1539
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001540 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001541 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001542 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001543 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1544 payload_length);
1545 /*
1546 * This is not a response to a Unsolicited NopIN, which means
1547 * it can either be a NOPOUT ping request (with a valid ITT),
1548 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1549 * Either way, make sure we allocate an struct iscsi_cmd, as both
1550 * can contain ping data.
1551 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001552 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001553 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1554 cmd->i_state = ISTATE_SEND_NOPIN;
1555 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1556 1 : 0);
1557 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1558 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001559 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1560 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001561 cmd->data_direction = DMA_NONE;
1562 }
1563
Nicholas Bellinger778de362013-06-14 16:07:47 -07001564 return 0;
1565}
1566EXPORT_SYMBOL(iscsit_setup_nop_out);
1567
1568int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1569 struct iscsi_nopout *hdr)
1570{
1571 struct iscsi_cmd *cmd_p = NULL;
1572 int cmdsn_ret = 0;
1573 /*
1574 * Initiator is expecting a NopIN ping reply..
1575 */
1576 if (hdr->itt != RESERVED_ITT) {
1577 BUG_ON(!cmd);
1578
1579 spin_lock_bh(&conn->cmd_lock);
1580 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1581 spin_unlock_bh(&conn->cmd_lock);
1582
1583 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1584
1585 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1586 iscsit_add_cmd_to_response_queue(cmd, conn,
1587 cmd->i_state);
1588 return 0;
1589 }
1590
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001591 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1592 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001593 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1594 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001595 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001596 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001597
1598 return 0;
1599 }
1600 /*
1601 * This was a response to a unsolicited NOPIN ping.
1602 */
1603 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1604 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1605 if (!cmd_p)
1606 return -EINVAL;
1607
1608 iscsit_stop_nopin_response_timer(conn);
1609
1610 cmd_p->i_state = ISTATE_REMOVE;
1611 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1612
1613 iscsit_start_nopin_timer(conn);
1614 return 0;
1615 }
1616 /*
1617 * Otherwise, initiator is not expecting a NOPIN is response.
1618 * Just ignore for now.
1619 */
1620 return 0;
1621}
1622EXPORT_SYMBOL(iscsit_process_nop_out);
1623
1624static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1625 unsigned char *buf)
1626{
1627 unsigned char *ping_data = NULL;
1628 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1629 struct kvec *iov = NULL;
1630 u32 payload_length = ntoh24(hdr->dlength);
1631 int ret;
1632
1633 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1634 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001635 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001636 /*
1637 * Handle NOP-OUT payload for traditional iSCSI sockets
1638 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001639 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001640 u32 checksum, data_crc, padding = 0;
1641 int niov = 0, rx_got, rx_size = payload_length;
1642
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001643 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1644 if (!ping_data) {
1645 pr_err("Unable to allocate memory for"
1646 " NOPOUT ping data.\n");
1647 ret = -1;
1648 goto out;
1649 }
1650
1651 iov = &cmd->iov_misc[0];
1652 iov[niov].iov_base = ping_data;
1653 iov[niov++].iov_len = payload_length;
1654
1655 padding = ((-payload_length) & 3);
1656 if (padding != 0) {
1657 pr_debug("Receiving %u additional bytes"
1658 " for padding.\n", padding);
1659 iov[niov].iov_base = &cmd->pad_bytes;
1660 iov[niov++].iov_len = padding;
1661 rx_size += padding;
1662 }
1663 if (conn->conn_ops->DataDigest) {
1664 iov[niov].iov_base = &checksum;
1665 iov[niov++].iov_len = ISCSI_CRC_LEN;
1666 rx_size += ISCSI_CRC_LEN;
1667 }
1668
1669 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1670 if (rx_got != rx_size) {
1671 ret = -1;
1672 goto out;
1673 }
1674
1675 if (conn->conn_ops->DataDigest) {
1676 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1677 ping_data, payload_length,
1678 padding, cmd->pad_bytes,
1679 (u8 *)&data_crc);
1680
1681 if (checksum != data_crc) {
1682 pr_err("Ping data CRC32C DataDigest"
1683 " 0x%08x does not match computed 0x%08x\n",
1684 checksum, data_crc);
1685 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1686 pr_err("Unable to recover from"
1687 " NOPOUT Ping DataCRC failure while in"
1688 " ERL=0.\n");
1689 ret = -1;
1690 goto out;
1691 } else {
1692 /*
1693 * Silently drop this PDU and let the
1694 * initiator plug the CmdSN gap.
1695 */
1696 pr_debug("Dropping NOPOUT"
1697 " Command CmdSN: 0x%08x due to"
1698 " DataCRC error.\n", hdr->cmdsn);
1699 ret = 0;
1700 goto out;
1701 }
1702 } else {
1703 pr_debug("Got CRC32C DataDigest"
1704 " 0x%08x for %u bytes of ping data.\n",
1705 checksum, payload_length);
1706 }
1707 }
1708
1709 ping_data[payload_length] = '\0';
1710 /*
1711 * Attach ping data to struct iscsi_cmd->buf_ptr.
1712 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001713 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001714 cmd->buf_ptr_size = payload_length;
1715
1716 pr_debug("Got %u bytes of NOPOUT ping"
1717 " data.\n", payload_length);
1718 pr_debug("Ping Data: \"%s\"\n", ping_data);
1719 }
1720
Nicholas Bellinger778de362013-06-14 16:07:47 -07001721 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001722out:
1723 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001724 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001725
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001726 kfree(ping_data);
1727 return ret;
1728}
1729
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001730int
1731iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1732 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001733{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001734 struct se_tmr_req *se_tmr;
1735 struct iscsi_tmr_req *tmr_req;
1736 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001737 int out_of_order_cmdsn = 0, ret;
1738 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001739 u8 function;
1740
1741 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001742 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1743 function = hdr->flags;
1744
1745 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1746 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1747 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1748 hdr->rtt, hdr->refcmdsn, conn->cid);
1749
1750 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1751 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001752 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001753 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001754 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001755 }
1756
1757 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1758 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1759 pr_err("Task Management Request TASK_REASSIGN not"
1760 " issued as immediate command, bad iSCSI Initiator"
1761 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001762 return iscsit_add_reject_cmd(cmd,
1763 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001764 }
1765 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001766 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1767 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001768
Andy Groverd28b11692012-04-03 15:51:22 -07001769 cmd->data_direction = DMA_NONE;
1770
1771 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1772 if (!cmd->tmr_req) {
1773 pr_err("Unable to allocate memory for"
1774 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001775 return iscsit_add_reject_cmd(cmd,
1776 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1777 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001778 }
1779
1780 /*
1781 * TASK_REASSIGN for ERL=2 / connection stays inside of
1782 * LIO-Target $FABRIC_MOD
1783 */
1784 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1785
1786 u8 tcm_function;
1787 int ret;
1788
1789 transport_init_se_cmd(&cmd->se_cmd,
1790 &lio_target_fabric_configfs->tf_ops,
1791 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001792 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001793
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001794 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1795 sess_ref = true;
1796
Andy Groverd28b11692012-04-03 15:51:22 -07001797 switch (function) {
1798 case ISCSI_TM_FUNC_ABORT_TASK:
1799 tcm_function = TMR_ABORT_TASK;
1800 break;
1801 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1802 tcm_function = TMR_ABORT_TASK_SET;
1803 break;
1804 case ISCSI_TM_FUNC_CLEAR_ACA:
1805 tcm_function = TMR_CLEAR_ACA;
1806 break;
1807 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1808 tcm_function = TMR_CLEAR_TASK_SET;
1809 break;
1810 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1811 tcm_function = TMR_LUN_RESET;
1812 break;
1813 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1814 tcm_function = TMR_TARGET_WARM_RESET;
1815 break;
1816 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1817 tcm_function = TMR_TARGET_COLD_RESET;
1818 break;
1819 default:
1820 pr_err("Unknown iSCSI TMR Function:"
1821 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001822 return iscsit_add_reject_cmd(cmd,
1823 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001824 }
1825
1826 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1827 tcm_function, GFP_KERNEL);
1828 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001829 return iscsit_add_reject_cmd(cmd,
1830 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001831
1832 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1833 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001834
1835 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1836 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1837 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1838 cmd->init_task_tag = hdr->itt;
1839 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001840 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1841 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001842 se_tmr = cmd->se_cmd.se_tmr_req;
1843 tmr_req = cmd->tmr_req;
1844 /*
1845 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1846 */
1847 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001848 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1849 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001850 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1852 goto attach;
1853 }
1854 }
1855
1856 switch (function) {
1857 case ISCSI_TM_FUNC_ABORT_TASK:
1858 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001859 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001860 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 break;
1862 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1863 case ISCSI_TM_FUNC_CLEAR_ACA:
1864 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1865 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1866 break;
1867 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1868 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001869 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1870 goto attach;
1871 }
1872 break;
1873 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1874 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001875 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1876 goto attach;
1877 }
1878 break;
1879 case ISCSI_TM_FUNC_TASK_REASSIGN:
1880 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1881 /*
1882 * Perform sanity checks on the ExpDataSN only if the
1883 * TASK_REASSIGN was successful.
1884 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001885 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001886 break;
1887
1888 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001889 return iscsit_add_reject_cmd(cmd,
1890 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001891 break;
1892 default:
1893 pr_err("Unknown TMR function: 0x%02x, protocol"
1894 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001895 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1896 goto attach;
1897 }
1898
1899 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1900 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1901 se_tmr->call_transport = 1;
1902attach:
1903 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001904 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 spin_unlock_bh(&conn->cmd_lock);
1906
1907 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001908 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001909 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1910 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001911 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001912 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001913 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001914 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001915 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001916 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001917
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001918 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001919 return 0;
1920 /*
1921 * Found the referenced task, send to transport for processing.
1922 */
1923 if (se_tmr->call_transport)
1924 return transport_generic_handle_tmr(&cmd->se_cmd);
1925
1926 /*
1927 * Could not find the referenced LUN, task, or Task Management
1928 * command not authorized or supported. Change state and
1929 * let the tx_thread send the response.
1930 *
1931 * For connection recovery, this is also the default action for
1932 * TMR TASK_REASSIGN.
1933 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001934 if (sess_ref) {
1935 pr_debug("Handle TMR, using sess_ref=true check\n");
1936 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1937 }
1938
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001939 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1940 return 0;
1941}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001942EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001943
1944/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001945int
1946iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1947 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001948{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001949 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001950
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001951 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001952 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001953 "greater than MaxXmitDataSegmentLength %u.\n",
1954 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001955 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1956 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001957 }
1958
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001959 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1960 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1961 pr_err("Multi sequence text commands currently not supported\n");
1962 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1963 (unsigned char *)hdr);
1964 }
1965
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001966 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1967 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1968 hdr->exp_statsn, payload_length);
1969
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001970 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1971 cmd->i_state = ISTATE_SEND_TEXTRSP;
1972 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1973 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1974 cmd->targ_xfer_tag = 0xFFFFFFFF;
1975 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1976 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1977 cmd->data_direction = DMA_NONE;
1978
1979 return 0;
1980}
1981EXPORT_SYMBOL(iscsit_setup_text_cmd);
1982
1983int
1984iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1985 struct iscsi_text *hdr)
1986{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001987 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001988 int cmdsn_ret;
1989
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001990 if (!text_in) {
1991 pr_err("Unable to locate text_in buffer for sendtargets"
1992 " discovery\n");
1993 goto reject;
1994 }
1995 if (strncmp("SendTargets", text_in, 11) != 0) {
1996 pr_err("Received Text Data that is not"
1997 " SendTargets, cannot continue.\n");
1998 goto reject;
1999 }
2000 text_ptr = strchr(text_in, '=');
2001 if (!text_ptr) {
2002 pr_err("No \"=\" separator found in Text Data,"
2003 " cannot continue.\n");
2004 goto reject;
2005 }
2006 if (!strncmp("=All", text_ptr, 4)) {
2007 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002008 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2009 !strncmp("=eui.", text_ptr, 5)) {
2010 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002011 } else {
2012 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2013 goto reject;
2014 }
2015
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002016 spin_lock_bh(&conn->cmd_lock);
2017 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2018 spin_unlock_bh(&conn->cmd_lock);
2019
2020 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2021
2022 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002023 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2024 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002025 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002026 return -1;
2027
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002028 return 0;
2029 }
2030
2031 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002032
2033reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002034 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2035 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002036}
2037EXPORT_SYMBOL(iscsit_process_text_cmd);
2038
2039static int
2040iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2041 unsigned char *buf)
2042{
2043 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2044 char *text_in = NULL;
2045 u32 payload_length = ntoh24(hdr->dlength);
2046 int rx_size, rc;
2047
2048 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2049 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002050 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002051
2052 rx_size = payload_length;
2053 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002054 u32 checksum = 0, data_crc = 0;
2055 u32 padding = 0, pad_bytes = 0;
2056 int niov = 0, rx_got;
2057 struct kvec iov[3];
2058
2059 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002060 if (!text_in) {
2061 pr_err("Unable to allocate memory for"
2062 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002063 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002064 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002065 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002066
2067 memset(iov, 0, 3 * sizeof(struct kvec));
2068 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002069 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002070
2071 padding = ((-payload_length) & 3);
2072 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002073 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002074 iov[niov++].iov_len = padding;
2075 rx_size += padding;
2076 pr_debug("Receiving %u additional bytes"
2077 " for padding.\n", padding);
2078 }
2079 if (conn->conn_ops->DataDigest) {
2080 iov[niov].iov_base = &checksum;
2081 iov[niov++].iov_len = ISCSI_CRC_LEN;
2082 rx_size += ISCSI_CRC_LEN;
2083 }
2084
2085 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002086 if (rx_got != rx_size)
2087 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002088
2089 if (conn->conn_ops->DataDigest) {
2090 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002091 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002092 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002093 (u8 *)&data_crc);
2094
2095 if (checksum != data_crc) {
2096 pr_err("Text data CRC32C DataDigest"
2097 " 0x%08x does not match computed"
2098 " 0x%08x\n", checksum, data_crc);
2099 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2100 pr_err("Unable to recover from"
2101 " Text Data digest failure while in"
2102 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002103 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002104 } else {
2105 /*
2106 * Silently drop this PDU and let the
2107 * initiator plug the CmdSN gap.
2108 */
2109 pr_debug("Dropping Text"
2110 " Command CmdSN: 0x%08x due to"
2111 " DataCRC error.\n", hdr->cmdsn);
2112 kfree(text_in);
2113 return 0;
2114 }
2115 } else {
2116 pr_debug("Got CRC32C DataDigest"
2117 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002118 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002119 }
2120 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002121 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002122 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002123 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002124 }
2125
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002126 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002127
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002128reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002129 kfree(cmd->text_in_ptr);
2130 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002131 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002132}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002133EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002134
2135int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2136{
2137 struct iscsi_conn *conn_p;
2138 struct iscsi_session *sess = conn->sess;
2139
2140 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2141 " for SID: %u.\n", conn->cid, conn->sess->sid);
2142
2143 atomic_set(&sess->session_logout, 1);
2144 atomic_set(&conn->conn_logout_remove, 1);
2145 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2146
2147 iscsit_inc_conn_usage_count(conn);
2148 iscsit_inc_session_usage_count(sess);
2149
2150 spin_lock_bh(&sess->conn_lock);
2151 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2152 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2153 continue;
2154
2155 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2156 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2157 }
2158 spin_unlock_bh(&sess->conn_lock);
2159
2160 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2161
2162 return 0;
2163}
2164
2165int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2166{
2167 struct iscsi_conn *l_conn;
2168 struct iscsi_session *sess = conn->sess;
2169
2170 pr_debug("Received logout request CLOSECONNECTION for CID:"
2171 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2172
2173 /*
2174 * A Logout Request with a CLOSECONNECTION reason code for a CID
2175 * can arrive on a connection with a differing CID.
2176 */
2177 if (conn->cid == cmd->logout_cid) {
2178 spin_lock_bh(&conn->state_lock);
2179 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2180 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2181
2182 atomic_set(&conn->conn_logout_remove, 1);
2183 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2184 iscsit_inc_conn_usage_count(conn);
2185
2186 spin_unlock_bh(&conn->state_lock);
2187 } else {
2188 /*
2189 * Handle all different cid CLOSECONNECTION requests in
2190 * iscsit_logout_post_handler_diffcid() as to give enough
2191 * time for any non immediate command's CmdSN to be
2192 * acknowledged on the connection in question.
2193 *
2194 * Here we simply make sure the CID is still around.
2195 */
2196 l_conn = iscsit_get_conn_from_cid(sess,
2197 cmd->logout_cid);
2198 if (!l_conn) {
2199 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2200 iscsit_add_cmd_to_response_queue(cmd, conn,
2201 cmd->i_state);
2202 return 0;
2203 }
2204
2205 iscsit_dec_conn_usage_count(l_conn);
2206 }
2207
2208 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2209
2210 return 0;
2211}
2212
2213int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2214{
2215 struct iscsi_session *sess = conn->sess;
2216
2217 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2218 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2219
2220 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2221 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2222 " while ERL!=2.\n");
2223 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2224 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2225 return 0;
2226 }
2227
2228 if (conn->cid == cmd->logout_cid) {
2229 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2230 " with CID: %hu on CID: %hu, implementation error.\n",
2231 cmd->logout_cid, conn->cid);
2232 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2233 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2234 return 0;
2235 }
2236
2237 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2238
2239 return 0;
2240}
2241
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002242int
2243iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2244 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002245{
2246 int cmdsn_ret, logout_remove = 0;
2247 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002248 struct iscsi_logout *hdr;
2249 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2250
2251 hdr = (struct iscsi_logout *) buf;
2252 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002253
2254 if (tiqn) {
2255 spin_lock(&tiqn->logout_stats.lock);
2256 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2257 tiqn->logout_stats.normal_logouts++;
2258 else
2259 tiqn->logout_stats.abnormal_logouts++;
2260 spin_unlock(&tiqn->logout_stats.lock);
2261 }
2262
2263 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2264 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2265 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2266 hdr->cid, conn->cid);
2267
2268 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2269 pr_err("Received logout request on connection that"
2270 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002271 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 return 0;
2273 }
2274
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002275 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2276 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2277 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2278 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2279 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002280 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2281 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2282 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002283 cmd->logout_reason = reason_code;
2284 cmd->data_direction = DMA_NONE;
2285
2286 /*
2287 * We need to sleep in these cases (by returning 1) until the Logout
2288 * Response gets sent in the tx thread.
2289 */
2290 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2291 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002292 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002293 logout_remove = 1;
2294
2295 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002296 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002297 spin_unlock_bh(&conn->cmd_lock);
2298
2299 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002300 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002301
2302 /*
2303 * Immediate commands are executed, well, immediately.
2304 * Non-Immediate Logout Commands are executed in CmdSN order.
2305 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002306 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307 int ret = iscsit_execute_cmd(cmd, 0);
2308
2309 if (ret < 0)
2310 return ret;
2311 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002312 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002313 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002315 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2316 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 }
2318
2319 return logout_remove;
2320}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002321EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002322
2323static int iscsit_handle_snack(
2324 struct iscsi_conn *conn,
2325 unsigned char *buf)
2326{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002327 struct iscsi_snack *hdr;
2328
2329 hdr = (struct iscsi_snack *) buf;
2330 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002331
2332 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2333 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2334 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2335 hdr->begrun, hdr->runlength, conn->cid);
2336
2337 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2338 pr_err("Initiator sent SNACK request while in"
2339 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002340 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2341 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002342 }
2343 /*
2344 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2345 * call from inside iscsi_send_recovery_datain_or_r2t().
2346 */
2347 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2348 case 0:
2349 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002350 hdr->itt,
2351 be32_to_cpu(hdr->ttt),
2352 be32_to_cpu(hdr->begrun),
2353 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002354 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002355 return iscsit_handle_status_snack(conn, hdr->itt,
2356 be32_to_cpu(hdr->ttt),
2357 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002358 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002359 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2360 be32_to_cpu(hdr->begrun),
2361 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002362 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2363 /* FIXME: Support R-Data SNACK */
2364 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002365 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2366 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002367 default:
2368 pr_err("Unknown SNACK type 0x%02x, protocol"
2369 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002370 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2371 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002372 }
2373
2374 return 0;
2375}
2376
2377static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2378{
2379 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2380 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2381 wait_for_completion_interruptible_timeout(
2382 &conn->rx_half_close_comp,
2383 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2384 }
2385}
2386
2387static int iscsit_handle_immediate_data(
2388 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002389 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002390 u32 length)
2391{
2392 int iov_ret, rx_got = 0, rx_size = 0;
2393 u32 checksum, iov_count = 0, padding = 0;
2394 struct iscsi_conn *conn = cmd->conn;
2395 struct kvec *iov;
2396
2397 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2398 if (iov_ret < 0)
2399 return IMMEDIATE_DATA_CANNOT_RECOVER;
2400
2401 rx_size = length;
2402 iov_count = iov_ret;
2403 iov = &cmd->iov_data[0];
2404
2405 padding = ((-length) & 3);
2406 if (padding != 0) {
2407 iov[iov_count].iov_base = cmd->pad_bytes;
2408 iov[iov_count++].iov_len = padding;
2409 rx_size += padding;
2410 }
2411
2412 if (conn->conn_ops->DataDigest) {
2413 iov[iov_count].iov_base = &checksum;
2414 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2415 rx_size += ISCSI_CRC_LEN;
2416 }
2417
2418 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2419
2420 iscsit_unmap_iovec(cmd);
2421
2422 if (rx_got != rx_size) {
2423 iscsit_rx_thread_wait_for_tcp(conn);
2424 return IMMEDIATE_DATA_CANNOT_RECOVER;
2425 }
2426
2427 if (conn->conn_ops->DataDigest) {
2428 u32 data_crc;
2429
2430 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2431 cmd->write_data_done, length, padding,
2432 cmd->pad_bytes);
2433
2434 if (checksum != data_crc) {
2435 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2436 " does not match computed 0x%08x\n", checksum,
2437 data_crc);
2438
2439 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2440 pr_err("Unable to recover from"
2441 " Immediate Data digest failure while"
2442 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002443 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002444 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002445 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002446 return IMMEDIATE_DATA_CANNOT_RECOVER;
2447 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002448 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002449 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002450 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002451 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2452 }
2453 } else {
2454 pr_debug("Got CRC32C DataDigest 0x%08x for"
2455 " %u bytes of Immediate Data\n", checksum,
2456 length);
2457 }
2458 }
2459
2460 cmd->write_data_done += length;
2461
Andy Groverebf1d952012-04-03 15:51:24 -07002462 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002463 spin_lock_bh(&cmd->istate_lock);
2464 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2465 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2466 spin_unlock_bh(&cmd->istate_lock);
2467 }
2468
2469 return IMMEDIATE_DATA_NORMAL_OPERATION;
2470}
2471
2472/*
2473 * Called with sess->conn_lock held.
2474 */
2475/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2476 with active network interface */
2477static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2478{
2479 struct iscsi_cmd *cmd;
2480 struct iscsi_conn *conn_p;
2481
2482 /*
2483 * Only send a Asynchronous Message on connections whos network
2484 * interface is still functional.
2485 */
2486 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2487 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2488 iscsit_inc_conn_usage_count(conn_p);
2489 break;
2490 }
2491 }
2492
2493 if (!conn_p)
2494 return;
2495
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002496 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002497 if (!cmd) {
2498 iscsit_dec_conn_usage_count(conn_p);
2499 return;
2500 }
2501
2502 cmd->logout_cid = conn->cid;
2503 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2504 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2505
2506 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002507 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002508 spin_unlock_bh(&conn_p->cmd_lock);
2509
2510 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2511 iscsit_dec_conn_usage_count(conn_p);
2512}
2513
2514static int iscsit_send_conn_drop_async_message(
2515 struct iscsi_cmd *cmd,
2516 struct iscsi_conn *conn)
2517{
2518 struct iscsi_async *hdr;
2519
2520 cmd->tx_size = ISCSI_HDR_LEN;
2521 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2522
2523 hdr = (struct iscsi_async *) cmd->pdu;
2524 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2525 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002526 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002527 cmd->targ_xfer_tag = 0xFFFFFFFF;
2528 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2529 cmd->stat_sn = conn->stat_sn++;
2530 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2531 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2532 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2533 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2534 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2535 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2536 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2537
2538 if (conn->conn_ops->HeaderDigest) {
2539 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2540
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002541 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2542 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002543
2544 cmd->tx_size += ISCSI_CRC_LEN;
2545 pr_debug("Attaching CRC32C HeaderDigest to"
2546 " Async Message 0x%08x\n", *header_digest);
2547 }
2548
2549 cmd->iov_misc[0].iov_base = cmd->pdu;
2550 cmd->iov_misc[0].iov_len = cmd->tx_size;
2551 cmd->iov_misc_count = 1;
2552
2553 pr_debug("Sending Connection Dropped Async Message StatSN:"
2554 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2555 cmd->logout_cid, conn->cid);
2556 return 0;
2557}
2558
Andy Grover6f3c0e62012-04-03 15:51:09 -07002559static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2560{
2561 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2562 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2563 wait_for_completion_interruptible_timeout(
2564 &conn->tx_half_close_comp,
2565 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2566 }
2567}
2568
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002569static void
2570iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2571 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2572 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002573{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002574 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2575 hdr->flags = datain->flags;
2576 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2577 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2578 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2579 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2580 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2581 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2582 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2583 }
2584 }
2585 hton24(hdr->dlength, datain->length);
2586 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2587 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2588 (struct scsi_lun *)&hdr->lun);
2589 else
2590 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2591
2592 hdr->itt = cmd->init_task_tag;
2593
2594 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2595 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2596 else
2597 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2598 if (set_statsn)
2599 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2600 else
2601 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2602
2603 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2604 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2605 hdr->datasn = cpu_to_be32(datain->data_sn);
2606 hdr->offset = cpu_to_be32(datain->offset);
2607
2608 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2609 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2610 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2611 ntohl(hdr->offset), datain->length, conn->cid);
2612}
2613
2614static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2615{
2616 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002617 struct iscsi_datain datain;
2618 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002619 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002620 u32 iov_count = 0, tx_size = 0;
2621 int eodr = 0, ret, iov_ret;
2622 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002623
2624 memset(&datain, 0, sizeof(struct iscsi_datain));
2625 dr = iscsit_get_datain_values(cmd, &datain);
2626 if (!dr) {
2627 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2628 cmd->init_task_tag);
2629 return -1;
2630 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002631 /*
2632 * Be paranoid and double check the logic for now.
2633 */
Andy Groverebf1d952012-04-03 15:51:24 -07002634 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002635 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2636 " datain.length: %u exceeds cmd->data_length: %u\n",
2637 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002638 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002639 return -1;
2640 }
2641
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002642 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002643 /*
2644 * Special case for successfully execution w/ both DATAIN
2645 * and Sense Data.
2646 */
2647 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2648 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2649 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2650 else {
2651 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2652 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2653 iscsit_increment_maxcmdsn(cmd, conn->sess);
2654 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002655 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002656 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002657 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2658 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002659 }
2660
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002661 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002662
2663 iov = &cmd->iov_data[0];
2664 iov[iov_count].iov_base = cmd->pdu;
2665 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2666 tx_size += ISCSI_HDR_LEN;
2667
2668 if (conn->conn_ops->HeaderDigest) {
2669 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2670
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002671 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2672 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002673
2674 iov[0].iov_len += ISCSI_CRC_LEN;
2675 tx_size += ISCSI_CRC_LEN;
2676
2677 pr_debug("Attaching CRC32 HeaderDigest"
2678 " for DataIN PDU 0x%08x\n", *header_digest);
2679 }
2680
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002681 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2682 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002683 if (iov_ret < 0)
2684 return -1;
2685
2686 iov_count += iov_ret;
2687 tx_size += datain.length;
2688
2689 cmd->padding = ((-datain.length) & 3);
2690 if (cmd->padding) {
2691 iov[iov_count].iov_base = cmd->pad_bytes;
2692 iov[iov_count++].iov_len = cmd->padding;
2693 tx_size += cmd->padding;
2694
2695 pr_debug("Attaching %u padding bytes\n",
2696 cmd->padding);
2697 }
2698 if (conn->conn_ops->DataDigest) {
2699 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2700 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2701
2702 iov[iov_count].iov_base = &cmd->data_crc;
2703 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2704 tx_size += ISCSI_CRC_LEN;
2705
2706 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2707 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2708 }
2709
2710 cmd->iov_data_count = iov_count;
2711 cmd->tx_size = tx_size;
2712
Andy Grover6f3c0e62012-04-03 15:51:09 -07002713 /* sendpage is preferred but can't insert markers */
2714 if (!conn->conn_ops->IFMarker)
2715 ret = iscsit_fe_sendpage_sg(cmd, conn);
2716 else
2717 ret = iscsit_send_tx_data(cmd, conn, 0);
2718
2719 iscsit_unmap_iovec(cmd);
2720
2721 if (ret < 0) {
2722 iscsit_tx_thread_wait_for_tcp(conn);
2723 return ret;
2724 }
2725
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002726 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002727 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002728 2 : 1;
2729 iscsit_free_datain_req(cmd, dr);
2730 }
2731
Andy Grover6f3c0e62012-04-03 15:51:09 -07002732 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002733}
2734
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002735int
2736iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2737 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002738{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002739 struct iscsi_conn *logout_conn = NULL;
2740 struct iscsi_conn_recovery *cr = NULL;
2741 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002742 /*
2743 * The actual shutting down of Sessions and/or Connections
2744 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2745 * is done in scsi_logout_post_handler().
2746 */
2747 switch (cmd->logout_reason) {
2748 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2749 pr_debug("iSCSI session logout successful, setting"
2750 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2751 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2752 break;
2753 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2754 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2755 break;
2756 /*
2757 * For CLOSECONNECTION logout requests carrying
2758 * a matching logout CID -> local CID, the reference
2759 * for the local CID will have been incremented in
2760 * iscsi_logout_closeconnection().
2761 *
2762 * For CLOSECONNECTION logout requests carrying
2763 * a different CID than the connection it arrived
2764 * on, the connection responding to cmd->logout_cid
2765 * is stopped in iscsit_logout_post_handler_diffcid().
2766 */
2767
2768 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2769 " successful.\n", cmd->logout_cid, conn->cid);
2770 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2771 break;
2772 case ISCSI_LOGOUT_REASON_RECOVERY:
2773 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2774 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2775 break;
2776 /*
2777 * If the connection is still active from our point of view
2778 * force connection recovery to occur.
2779 */
2780 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2781 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002782 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002783 iscsit_connection_reinstatement_rcfr(logout_conn);
2784 iscsit_dec_conn_usage_count(logout_conn);
2785 }
2786
2787 cr = iscsit_get_inactive_connection_recovery_entry(
2788 conn->sess, cmd->logout_cid);
2789 if (!cr) {
2790 pr_err("Unable to locate CID: %hu for"
2791 " REMOVECONNFORRECOVERY Logout Request.\n",
2792 cmd->logout_cid);
2793 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2794 break;
2795 }
2796
2797 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2798
2799 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2800 " for recovery for CID: %hu on CID: %hu successful.\n",
2801 cmd->logout_cid, conn->cid);
2802 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2803 break;
2804 default:
2805 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2806 cmd->logout_reason);
2807 return -1;
2808 }
2809
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002810 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2811 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2812 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002813 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002814 cmd->stat_sn = conn->stat_sn++;
2815 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2816
2817 iscsit_increment_maxcmdsn(cmd, conn->sess);
2818 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2819 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2820
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002821 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2822 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2823 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2824 cmd->logout_cid, conn->cid);
2825
2826 return 0;
2827}
2828EXPORT_SYMBOL(iscsit_build_logout_rsp);
2829
2830static int
2831iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2832{
2833 struct kvec *iov;
2834 int niov = 0, tx_size, rc;
2835
2836 rc = iscsit_build_logout_rsp(cmd, conn,
2837 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2838 if (rc < 0)
2839 return rc;
2840
2841 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002842 iov = &cmd->iov_misc[0];
2843 iov[niov].iov_base = cmd->pdu;
2844 iov[niov++].iov_len = ISCSI_HDR_LEN;
2845
2846 if (conn->conn_ops->HeaderDigest) {
2847 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2848
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002849 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2850 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002851
2852 iov[0].iov_len += ISCSI_CRC_LEN;
2853 tx_size += ISCSI_CRC_LEN;
2854 pr_debug("Attaching CRC32C HeaderDigest to"
2855 " Logout Response 0x%08x\n", *header_digest);
2856 }
2857 cmd->iov_misc_count = niov;
2858 cmd->tx_size = tx_size;
2859
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002860 return 0;
2861}
2862
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002863void
2864iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2865 struct iscsi_nopin *hdr, bool nopout_response)
2866{
2867 hdr->opcode = ISCSI_OP_NOOP_IN;
2868 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2869 hton24(hdr->dlength, cmd->buf_ptr_size);
2870 if (nopout_response)
2871 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2872 hdr->itt = cmd->init_task_tag;
2873 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2874 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2875 conn->stat_sn;
2876 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2877
2878 if (nopout_response)
2879 iscsit_increment_maxcmdsn(cmd, conn->sess);
2880
2881 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2882 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2883
2884 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2885 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2886 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2887 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2888}
2889EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2890
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002891/*
2892 * Unsolicited NOPIN, either requesting a response or not.
2893 */
2894static int iscsit_send_unsolicited_nopin(
2895 struct iscsi_cmd *cmd,
2896 struct iscsi_conn *conn,
2897 int want_response)
2898{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002899 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2900 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002901
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002902 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002903
2904 if (conn->conn_ops->HeaderDigest) {
2905 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2906
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002907 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2908 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002909
2910 tx_size += ISCSI_CRC_LEN;
2911 pr_debug("Attaching CRC32C HeaderDigest to"
2912 " NopIN 0x%08x\n", *header_digest);
2913 }
2914
2915 cmd->iov_misc[0].iov_base = cmd->pdu;
2916 cmd->iov_misc[0].iov_len = tx_size;
2917 cmd->iov_misc_count = 1;
2918 cmd->tx_size = tx_size;
2919
2920 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2921 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2922
Andy Grover6f3c0e62012-04-03 15:51:09 -07002923 ret = iscsit_send_tx_data(cmd, conn, 1);
2924 if (ret < 0) {
2925 iscsit_tx_thread_wait_for_tcp(conn);
2926 return ret;
2927 }
2928
2929 spin_lock_bh(&cmd->istate_lock);
2930 cmd->i_state = want_response ?
2931 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2932 spin_unlock_bh(&cmd->istate_lock);
2933
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002934 return 0;
2935}
2936
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002937static int
2938iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002939{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002940 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002941 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002942 u32 padding = 0;
2943 int niov = 0, tx_size;
2944
2945 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002946
2947 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002948 iov = &cmd->iov_misc[0];
2949 iov[niov].iov_base = cmd->pdu;
2950 iov[niov++].iov_len = ISCSI_HDR_LEN;
2951
2952 if (conn->conn_ops->HeaderDigest) {
2953 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2954
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002955 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2956 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002957
2958 iov[0].iov_len += ISCSI_CRC_LEN;
2959 tx_size += ISCSI_CRC_LEN;
2960 pr_debug("Attaching CRC32C HeaderDigest"
2961 " to NopIn 0x%08x\n", *header_digest);
2962 }
2963
2964 /*
2965 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2966 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2967 */
2968 if (cmd->buf_ptr_size) {
2969 iov[niov].iov_base = cmd->buf_ptr;
2970 iov[niov++].iov_len = cmd->buf_ptr_size;
2971 tx_size += cmd->buf_ptr_size;
2972
2973 pr_debug("Echoing back %u bytes of ping"
2974 " data.\n", cmd->buf_ptr_size);
2975
2976 padding = ((-cmd->buf_ptr_size) & 3);
2977 if (padding != 0) {
2978 iov[niov].iov_base = &cmd->pad_bytes;
2979 iov[niov++].iov_len = padding;
2980 tx_size += padding;
2981 pr_debug("Attaching %u additional"
2982 " padding bytes.\n", padding);
2983 }
2984 if (conn->conn_ops->DataDigest) {
2985 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2986 cmd->buf_ptr, cmd->buf_ptr_size,
2987 padding, (u8 *)&cmd->pad_bytes,
2988 (u8 *)&cmd->data_crc);
2989
2990 iov[niov].iov_base = &cmd->data_crc;
2991 iov[niov++].iov_len = ISCSI_CRC_LEN;
2992 tx_size += ISCSI_CRC_LEN;
2993 pr_debug("Attached DataDigest for %u"
2994 " bytes of ping data, CRC 0x%08x\n",
2995 cmd->buf_ptr_size, cmd->data_crc);
2996 }
2997 }
2998
2999 cmd->iov_misc_count = niov;
3000 cmd->tx_size = tx_size;
3001
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003002 return 0;
3003}
3004
Andy Grover6f3c0e62012-04-03 15:51:09 -07003005static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003006 struct iscsi_cmd *cmd,
3007 struct iscsi_conn *conn)
3008{
3009 int tx_size = 0;
3010 struct iscsi_r2t *r2t;
3011 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003012 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003013
3014 r2t = iscsit_get_r2t_from_list(cmd);
3015 if (!r2t)
3016 return -1;
3017
3018 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3019 memset(hdr, 0, ISCSI_HDR_LEN);
3020 hdr->opcode = ISCSI_OP_R2T;
3021 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3022 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3023 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003024 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003025 spin_lock_bh(&conn->sess->ttt_lock);
3026 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3027 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3028 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3029 spin_unlock_bh(&conn->sess->ttt_lock);
3030 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3031 hdr->statsn = cpu_to_be32(conn->stat_sn);
3032 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3033 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3034 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3035 hdr->data_offset = cpu_to_be32(r2t->offset);
3036 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3037
3038 cmd->iov_misc[0].iov_base = cmd->pdu;
3039 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3040 tx_size += ISCSI_HDR_LEN;
3041
3042 if (conn->conn_ops->HeaderDigest) {
3043 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3044
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003045 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3046 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003047
3048 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3049 tx_size += ISCSI_CRC_LEN;
3050 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3051 " PDU 0x%08x\n", *header_digest);
3052 }
3053
3054 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3055 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3056 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3057 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3058 r2t->offset, r2t->xfer_len, conn->cid);
3059
3060 cmd->iov_misc_count = 1;
3061 cmd->tx_size = tx_size;
3062
3063 spin_lock_bh(&cmd->r2t_lock);
3064 r2t->sent_r2t = 1;
3065 spin_unlock_bh(&cmd->r2t_lock);
3066
Andy Grover6f3c0e62012-04-03 15:51:09 -07003067 ret = iscsit_send_tx_data(cmd, conn, 1);
3068 if (ret < 0) {
3069 iscsit_tx_thread_wait_for_tcp(conn);
3070 return ret;
3071 }
3072
3073 spin_lock_bh(&cmd->dataout_timeout_lock);
3074 iscsit_start_dataout_timer(cmd, conn);
3075 spin_unlock_bh(&cmd->dataout_timeout_lock);
3076
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003077 return 0;
3078}
3079
3080/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003081 * @recovery: If called from iscsi_task_reassign_complete_write() for
3082 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003083 */
3084int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003085 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003086 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003087 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003088{
3089 int first_r2t = 1;
3090 u32 offset = 0, xfer_len = 0;
3091
3092 spin_lock_bh(&cmd->r2t_lock);
3093 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3094 spin_unlock_bh(&cmd->r2t_lock);
3095 return 0;
3096 }
3097
Andy Grover8b1e1242012-04-03 15:51:12 -07003098 if (conn->sess->sess_ops->DataSequenceInOrder &&
3099 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003100 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003101
3102 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3103 if (conn->sess->sess_ops->DataSequenceInOrder) {
3104 offset = cmd->r2t_offset;
3105
Andy Grover8b1e1242012-04-03 15:51:12 -07003106 if (first_r2t && recovery) {
3107 int new_data_end = offset +
3108 conn->sess->sess_ops->MaxBurstLength -
3109 cmd->next_burst_len;
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 =
3115 conn->sess->sess_ops->MaxBurstLength -
3116 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003117 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003118 int new_data_end = offset +
3119 conn->sess->sess_ops->MaxBurstLength;
3120
Andy Groverebf1d952012-04-03 15:51:24 -07003121 if (new_data_end > cmd->se_cmd.data_length)
3122 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003123 else
3124 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003125 }
3126 cmd->r2t_offset += xfer_len;
3127
Andy Groverebf1d952012-04-03 15:51:24 -07003128 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003129 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3130 } else {
3131 struct iscsi_seq *seq;
3132
3133 seq = iscsit_get_seq_holder_for_r2t(cmd);
3134 if (!seq) {
3135 spin_unlock_bh(&cmd->r2t_lock);
3136 return -1;
3137 }
3138
3139 offset = seq->offset;
3140 xfer_len = seq->xfer_len;
3141
3142 if (cmd->seq_send_order == cmd->seq_count)
3143 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3144 }
3145 cmd->outstanding_r2ts++;
3146 first_r2t = 0;
3147
3148 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3149 spin_unlock_bh(&cmd->r2t_lock);
3150 return -1;
3151 }
3152
3153 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3154 break;
3155 }
3156 spin_unlock_bh(&cmd->r2t_lock);
3157
3158 return 0;
3159}
3160
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003161void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3162 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003163{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003164 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003165 cmd->stat_sn = conn->stat_sn++;
3166
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003167 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003168
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003169 memset(hdr, 0, ISCSI_HDR_LEN);
3170 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3171 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3172 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3173 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003174 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003175 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3176 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003177 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003178 }
3179 hdr->response = cmd->iscsi_response;
3180 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003181 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003182 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3183
3184 iscsit_increment_maxcmdsn(cmd, conn->sess);
3185 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3186 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3187
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003188 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3189 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3190 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3191 cmd->se_cmd.scsi_status, conn->cid);
3192}
3193EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3194
3195static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3196{
3197 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3198 struct kvec *iov;
3199 u32 padding = 0, tx_size = 0;
3200 int iov_count = 0;
3201 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3202
3203 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3204
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003205 iov = &cmd->iov_misc[0];
3206 iov[iov_count].iov_base = cmd->pdu;
3207 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3208 tx_size += ISCSI_HDR_LEN;
3209
3210 /*
3211 * Attach SENSE DATA payload to iSCSI Response PDU
3212 */
3213 if (cmd->se_cmd.sense_buffer &&
3214 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3215 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003216 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3217 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3218
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003219 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003220 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003221 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003222 iov[iov_count++].iov_len =
3223 (cmd->se_cmd.scsi_sense_length + padding);
3224 tx_size += cmd->se_cmd.scsi_sense_length;
3225
3226 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003227 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003228 cmd->se_cmd.scsi_sense_length, 0, padding);
3229 tx_size += padding;
3230 pr_debug("Adding %u bytes of padding to"
3231 " SENSE.\n", padding);
3232 }
3233
3234 if (conn->conn_ops->DataDigest) {
3235 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003236 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003237 (cmd->se_cmd.scsi_sense_length + padding),
3238 0, NULL, (u8 *)&cmd->data_crc);
3239
3240 iov[iov_count].iov_base = &cmd->data_crc;
3241 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3242 tx_size += ISCSI_CRC_LEN;
3243
3244 pr_debug("Attaching CRC32 DataDigest for"
3245 " SENSE, %u bytes CRC 0x%08x\n",
3246 (cmd->se_cmd.scsi_sense_length + padding),
3247 cmd->data_crc);
3248 }
3249
3250 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3251 " Response PDU\n",
3252 cmd->se_cmd.scsi_sense_length);
3253 }
3254
3255 if (conn->conn_ops->HeaderDigest) {
3256 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3257
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003258 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3259 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003260
3261 iov[0].iov_len += ISCSI_CRC_LEN;
3262 tx_size += ISCSI_CRC_LEN;
3263 pr_debug("Attaching CRC32 HeaderDigest for Response"
3264 " PDU 0x%08x\n", *header_digest);
3265 }
3266
3267 cmd->iov_misc_count = iov_count;
3268 cmd->tx_size = tx_size;
3269
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003270 return 0;
3271}
3272
3273static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3274{
3275 switch (se_tmr->response) {
3276 case TMR_FUNCTION_COMPLETE:
3277 return ISCSI_TMF_RSP_COMPLETE;
3278 case TMR_TASK_DOES_NOT_EXIST:
3279 return ISCSI_TMF_RSP_NO_TASK;
3280 case TMR_LUN_DOES_NOT_EXIST:
3281 return ISCSI_TMF_RSP_NO_LUN;
3282 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3283 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003284 case TMR_FUNCTION_REJECTED:
3285 default:
3286 return ISCSI_TMF_RSP_REJECTED;
3287 }
3288}
3289
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003290void
3291iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3292 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003293{
3294 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003295
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003296 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003297 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003298 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003299 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003300 cmd->stat_sn = conn->stat_sn++;
3301 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3302
3303 iscsit_increment_maxcmdsn(cmd, conn->sess);
3304 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3305 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3306
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003307 pr_debug("Built Task Management Response ITT: 0x%08x,"
3308 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3309 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3310}
3311EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3312
3313static int
3314iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3315{
3316 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3317 u32 tx_size = 0;
3318
3319 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3320
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003321 cmd->iov_misc[0].iov_base = cmd->pdu;
3322 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3323 tx_size += ISCSI_HDR_LEN;
3324
3325 if (conn->conn_ops->HeaderDigest) {
3326 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3327
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003328 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3329 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003330
3331 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3332 tx_size += ISCSI_CRC_LEN;
3333 pr_debug("Attaching CRC32 HeaderDigest for Task"
3334 " Mgmt Response PDU 0x%08x\n", *header_digest);
3335 }
3336
3337 cmd->iov_misc_count = 1;
3338 cmd->tx_size = tx_size;
3339
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003340 return 0;
3341}
3342
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003343static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3344{
3345 bool ret = false;
3346
3347 if (np->np_sockaddr.ss_family == AF_INET6) {
3348 const struct sockaddr_in6 sin6 = {
3349 .sin6_addr = IN6ADDR_ANY_INIT };
3350 struct sockaddr_in6 *sock_in6 =
3351 (struct sockaddr_in6 *)&np->np_sockaddr;
3352
3353 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3354 sin6.sin6_addr.s6_addr, 16))
3355 ret = true;
3356 } else {
3357 struct sockaddr_in * sock_in =
3358 (struct sockaddr_in *)&np->np_sockaddr;
3359
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003360 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003361 ret = true;
3362 }
3363
3364 return ret;
3365}
3366
Andy Grover8b1e1242012-04-03 15:51:12 -07003367#define SENDTARGETS_BUF_LIMIT 32768U
3368
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003369static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3370{
3371 char *payload = NULL;
3372 struct iscsi_conn *conn = cmd->conn;
3373 struct iscsi_portal_group *tpg;
3374 struct iscsi_tiqn *tiqn;
3375 struct iscsi_tpg_np *tpg_np;
3376 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003377 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003378 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003379 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003380
Andy Grover8b1e1242012-04-03 15:51:12 -07003381 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3382 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003383
3384 payload = kzalloc(buffer_len, GFP_KERNEL);
3385 if (!payload) {
3386 pr_err("Unable to allocate memory for sendtargets"
3387 " response.\n");
3388 return -ENOMEM;
3389 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003390 /*
3391 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3392 * explicit case..
3393 */
3394 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3395 text_ptr = strchr(text_in, '=');
3396 if (!text_ptr) {
3397 pr_err("Unable to locate '=' string in text_in:"
3398 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003399 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003400 return -EINVAL;
3401 }
3402 /*
3403 * Skip over '=' character..
3404 */
3405 text_ptr += 1;
3406 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003407
3408 spin_lock(&tiqn_lock);
3409 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003410 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3411 strcmp(tiqn->tiqn, text_ptr)) {
3412 continue;
3413 }
3414
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003415 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003416
3417 spin_lock(&tiqn->tiqn_tpg_lock);
3418 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3419
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003420 /* If demo_mode_discovery=0 and generate_node_acls=0
3421 * (demo mode dislabed) do not return
3422 * TargetName+TargetAddress unless a NodeACL exists.
3423 */
3424
3425 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3426 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3427 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3428 cmd->conn->sess->sess_ops->InitiatorName))) {
3429 continue;
3430 }
3431
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003432 spin_lock(&tpg->tpg_state_lock);
3433 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3434 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3435 spin_unlock(&tpg->tpg_state_lock);
3436 continue;
3437 }
3438 spin_unlock(&tpg->tpg_state_lock);
3439
3440 spin_lock(&tpg->tpg_np_lock);
3441 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3442 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003443 struct iscsi_np *np = tpg_np->tpg_np;
3444 bool inaddr_any = iscsit_check_inaddr_any(np);
3445
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003446 if (!target_name_printed) {
3447 len = sprintf(buf, "TargetName=%s",
3448 tiqn->tiqn);
3449 len += 1;
3450
3451 if ((len + payload_len) > buffer_len) {
3452 spin_unlock(&tpg->tpg_np_lock);
3453 spin_unlock(&tiqn->tiqn_tpg_lock);
3454 end_of_buf = 1;
3455 goto eob;
3456 }
3457 memcpy(payload + payload_len, buf, len);
3458 payload_len += len;
3459 target_name_printed = 1;
3460 }
3461
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003462 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003463 "%s:%hu,%hu",
3464 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003465 np->np_ip : conn->local_ip,
Chris Leechdfecf612013-08-12 11:26:28 -07003466 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003467 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003468 tpg->tpgt);
3469 len += 1;
3470
3471 if ((len + payload_len) > buffer_len) {
3472 spin_unlock(&tpg->tpg_np_lock);
3473 spin_unlock(&tiqn->tiqn_tpg_lock);
3474 end_of_buf = 1;
3475 goto eob;
3476 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003477 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003478 payload_len += len;
3479 }
3480 spin_unlock(&tpg->tpg_np_lock);
3481 }
3482 spin_unlock(&tiqn->tiqn_tpg_lock);
3483eob:
3484 if (end_of_buf)
3485 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003486
3487 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3488 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003489 }
3490 spin_unlock(&tiqn_lock);
3491
3492 cmd->buf_ptr = payload;
3493
3494 return payload_len;
3495}
3496
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003497int
3498iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3499 struct iscsi_text_rsp *hdr)
3500{
3501 int text_length, padding;
3502
3503 text_length = iscsit_build_sendtargets_response(cmd);
3504 if (text_length < 0)
3505 return text_length;
3506
3507 hdr->opcode = ISCSI_OP_TEXT_RSP;
3508 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3509 padding = ((-text_length) & 3);
3510 hton24(hdr->dlength, text_length);
3511 hdr->itt = cmd->init_task_tag;
3512 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3513 cmd->stat_sn = conn->stat_sn++;
3514 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3515
3516 iscsit_increment_maxcmdsn(cmd, conn->sess);
3517 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3518 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3519
3520 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3521 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3522 text_length, conn->cid);
3523
3524 return text_length + padding;
3525}
3526EXPORT_SYMBOL(iscsit_build_text_rsp);
3527
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003528/*
3529 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3530 * MaxRecvDataSegmentLength.
3531 */
3532static int iscsit_send_text_rsp(
3533 struct iscsi_cmd *cmd,
3534 struct iscsi_conn *conn)
3535{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003536 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003537 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003538 u32 tx_size = 0;
3539 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003540
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003541 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3542 if (rc < 0)
3543 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003544
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003545 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003546 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003547 iov[iov_count].iov_base = cmd->pdu;
3548 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3549 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003550 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003551
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003552 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003553
3554 if (conn->conn_ops->HeaderDigest) {
3555 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3556
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003557 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3558 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003559
3560 iov[0].iov_len += ISCSI_CRC_LEN;
3561 tx_size += ISCSI_CRC_LEN;
3562 pr_debug("Attaching CRC32 HeaderDigest for"
3563 " Text Response PDU 0x%08x\n", *header_digest);
3564 }
3565
3566 if (conn->conn_ops->DataDigest) {
3567 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003568 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003569 0, NULL, (u8 *)&cmd->data_crc);
3570
3571 iov[iov_count].iov_base = &cmd->data_crc;
3572 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3573 tx_size += ISCSI_CRC_LEN;
3574
3575 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003576 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003577 cmd->data_crc);
3578 }
3579
3580 cmd->iov_misc_count = iov_count;
3581 cmd->tx_size = tx_size;
3582
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583 return 0;
3584}
3585
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003586void
3587iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3588 struct iscsi_reject *hdr)
3589{
3590 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003591 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003592 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3593 hton24(hdr->dlength, ISCSI_HDR_LEN);
3594 hdr->ffffffff = cpu_to_be32(0xffffffff);
3595 cmd->stat_sn = conn->stat_sn++;
3596 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3597 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3598 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3599
3600}
3601EXPORT_SYMBOL(iscsit_build_reject);
3602
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603static int iscsit_send_reject(
3604 struct iscsi_cmd *cmd,
3605 struct iscsi_conn *conn)
3606{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003607 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003608 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003609 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003610
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003611 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003612
3613 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003614 iov[iov_count].iov_base = cmd->pdu;
3615 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3616 iov[iov_count].iov_base = cmd->buf_ptr;
3617 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3618
3619 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3620
3621 if (conn->conn_ops->HeaderDigest) {
3622 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3623
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003624 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3625 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003626
3627 iov[0].iov_len += ISCSI_CRC_LEN;
3628 tx_size += ISCSI_CRC_LEN;
3629 pr_debug("Attaching CRC32 HeaderDigest for"
3630 " REJECT PDU 0x%08x\n", *header_digest);
3631 }
3632
3633 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003634 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3635 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003636
3637 iov[iov_count].iov_base = &cmd->data_crc;
3638 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3639 tx_size += ISCSI_CRC_LEN;
3640 pr_debug("Attaching CRC32 DataDigest for REJECT"
3641 " PDU 0x%08x\n", cmd->data_crc);
3642 }
3643
3644 cmd->iov_misc_count = iov_count;
3645 cmd->tx_size = tx_size;
3646
3647 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3648 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3649
3650 return 0;
3651}
3652
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003653void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3654{
3655 struct iscsi_thread_set *ts = conn->thread_set;
3656 int ord, cpu;
3657 /*
3658 * thread_id is assigned from iscsit_global->ts_bitmap from
3659 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3660 *
3661 * Here we use thread_id to determine which CPU that this
3662 * iSCSI connection's iscsi_thread_set will be scheduled to
3663 * execute upon.
3664 */
3665 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003666 for_each_online_cpu(cpu) {
3667 if (ord-- == 0) {
3668 cpumask_set_cpu(cpu, conn->conn_cpumask);
3669 return;
3670 }
3671 }
3672 /*
3673 * This should never be reached..
3674 */
3675 dump_stack();
3676 cpumask_setall(conn->conn_cpumask);
3677}
3678
3679static inline void iscsit_thread_check_cpumask(
3680 struct iscsi_conn *conn,
3681 struct task_struct *p,
3682 int mode)
3683{
3684 char buf[128];
3685 /*
3686 * mode == 1 signals iscsi_target_tx_thread() usage.
3687 * mode == 0 signals iscsi_target_rx_thread() usage.
3688 */
3689 if (mode == 1) {
3690 if (!conn->conn_tx_reset_cpumask)
3691 return;
3692 conn->conn_tx_reset_cpumask = 0;
3693 } else {
3694 if (!conn->conn_rx_reset_cpumask)
3695 return;
3696 conn->conn_rx_reset_cpumask = 0;
3697 }
3698 /*
3699 * Update the CPU mask for this single kthread so that
3700 * both TX and RX kthreads are scheduled to run on the
3701 * same CPU.
3702 */
3703 memset(buf, 0, 128);
3704 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003705 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3706}
3707
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003708static int
3709iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003710{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003711 int ret;
3712
3713 switch (state) {
3714 case ISTATE_SEND_R2T:
3715 ret = iscsit_send_r2t(cmd, conn);
3716 if (ret < 0)
3717 goto err;
3718 break;
3719 case ISTATE_REMOVE:
3720 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003721 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003722 spin_unlock_bh(&conn->cmd_lock);
3723
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003724 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003725 break;
3726 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3727 iscsit_mod_nopin_response_timer(conn);
3728 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3729 if (ret < 0)
3730 goto err;
3731 break;
3732 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3733 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3734 if (ret < 0)
3735 goto err;
3736 break;
3737 default:
3738 pr_err("Unknown Opcode: 0x%02x ITT:"
3739 " 0x%08x, i_state: %d on CID: %hu\n",
3740 cmd->iscsi_opcode, cmd->init_task_tag, state,
3741 conn->cid);
3742 goto err;
3743 }
3744
3745 return 0;
3746
3747err:
3748 return -1;
3749}
3750
3751static int
3752iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3753{
3754 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003755 struct iscsi_queue_req *qr;
3756 struct iscsi_cmd *cmd;
3757 u8 state;
3758 int ret;
3759
3760 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3761 atomic_set(&conn->check_immediate_queue, 0);
3762 cmd = qr->cmd;
3763 state = qr->state;
3764 kmem_cache_free(lio_qr_cache, qr);
3765
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003766 ret = t->iscsit_immediate_queue(conn, cmd, state);
3767 if (ret < 0)
3768 return ret;
3769 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003770
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003771 return 0;
3772}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003773
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003774static int
3775iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3776{
3777 int ret;
3778
3779check_rsp_state:
3780 switch (state) {
3781 case ISTATE_SEND_DATAIN:
3782 ret = iscsit_send_datain(cmd, conn);
3783 if (ret < 0)
3784 goto err;
3785 else if (!ret)
3786 /* more drs */
3787 goto check_rsp_state;
3788 else if (ret == 1) {
3789 /* all done */
3790 spin_lock_bh(&cmd->istate_lock);
3791 cmd->i_state = ISTATE_SENT_STATUS;
3792 spin_unlock_bh(&cmd->istate_lock);
3793
3794 if (atomic_read(&conn->check_immediate_queue))
3795 return 1;
3796
3797 return 0;
3798 } else if (ret == 2) {
3799 /* Still must send status,
3800 SCF_TRANSPORT_TASK_SENSE was set */
3801 spin_lock_bh(&cmd->istate_lock);
3802 cmd->i_state = ISTATE_SEND_STATUS;
3803 spin_unlock_bh(&cmd->istate_lock);
3804 state = ISTATE_SEND_STATUS;
3805 goto check_rsp_state;
3806 }
3807
3808 break;
3809 case ISTATE_SEND_STATUS:
3810 case ISTATE_SEND_STATUS_RECOVERY:
3811 ret = iscsit_send_response(cmd, conn);
3812 break;
3813 case ISTATE_SEND_LOGOUTRSP:
3814 ret = iscsit_send_logout(cmd, conn);
3815 break;
3816 case ISTATE_SEND_ASYNCMSG:
3817 ret = iscsit_send_conn_drop_async_message(
3818 cmd, conn);
3819 break;
3820 case ISTATE_SEND_NOPIN:
3821 ret = iscsit_send_nopin(cmd, conn);
3822 break;
3823 case ISTATE_SEND_REJECT:
3824 ret = iscsit_send_reject(cmd, conn);
3825 break;
3826 case ISTATE_SEND_TASKMGTRSP:
3827 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3828 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003829 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003830 ret = iscsit_tmr_post_handler(cmd, conn);
3831 if (ret != 0)
3832 iscsit_fall_back_to_erl0(conn->sess);
3833 break;
3834 case ISTATE_SEND_TEXTRSP:
3835 ret = iscsit_send_text_rsp(cmd, conn);
3836 break;
3837 default:
3838 pr_err("Unknown Opcode: 0x%02x ITT:"
3839 " 0x%08x, i_state: %d on CID: %hu\n",
3840 cmd->iscsi_opcode, cmd->init_task_tag,
3841 state, conn->cid);
3842 goto err;
3843 }
3844 if (ret < 0)
3845 goto err;
3846
3847 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3848 iscsit_tx_thread_wait_for_tcp(conn);
3849 iscsit_unmap_iovec(cmd);
3850 goto err;
3851 }
3852 iscsit_unmap_iovec(cmd);
3853
3854 switch (state) {
3855 case ISTATE_SEND_LOGOUTRSP:
3856 if (!iscsit_logout_post_handler(cmd, conn))
3857 goto restart;
3858 /* fall through */
3859 case ISTATE_SEND_STATUS:
3860 case ISTATE_SEND_ASYNCMSG:
3861 case ISTATE_SEND_NOPIN:
3862 case ISTATE_SEND_STATUS_RECOVERY:
3863 case ISTATE_SEND_TEXTRSP:
3864 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003865 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003866 spin_lock_bh(&cmd->istate_lock);
3867 cmd->i_state = ISTATE_SENT_STATUS;
3868 spin_unlock_bh(&cmd->istate_lock);
3869 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003870 default:
3871 pr_err("Unknown Opcode: 0x%02x ITT:"
3872 " 0x%08x, i_state: %d on CID: %hu\n",
3873 cmd->iscsi_opcode, cmd->init_task_tag,
3874 cmd->i_state, conn->cid);
3875 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003876 }
3877
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003878 if (atomic_read(&conn->check_immediate_queue))
3879 return 1;
3880
Andy Grover6f3c0e62012-04-03 15:51:09 -07003881 return 0;
3882
3883err:
3884 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003885restart:
3886 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003887}
3888
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003889static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003890{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003891 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003892 struct iscsi_queue_req *qr;
3893 struct iscsi_cmd *cmd;
3894 u8 state;
3895 int ret;
3896
3897 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3898 cmd = qr->cmd;
3899 state = qr->state;
3900 kmem_cache_free(lio_qr_cache, qr);
3901
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003902 ret = t->iscsit_response_queue(conn, cmd, state);
3903 if (ret == 1 || ret < 0)
3904 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003905 }
3906
3907 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003908}
3909
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003910int iscsi_target_tx_thread(void *arg)
3911{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003912 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003913 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003914 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003915 /*
3916 * Allow ourselves to be interrupted by SIGINT so that a
3917 * connection recovery / failure event can be triggered externally.
3918 */
3919 allow_signal(SIGINT);
3920
3921restart:
3922 conn = iscsi_tx_thread_pre_handler(ts);
3923 if (!conn)
3924 goto out;
3925
Andy Grover6f3c0e62012-04-03 15:51:09 -07003926 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003927
3928 while (!kthread_should_stop()) {
3929 /*
3930 * Ensure that both TX and RX per connection kthreads
3931 * are scheduled to run on the same CPU.
3932 */
3933 iscsit_thread_check_cpumask(conn, current, 1);
3934
Roland Dreierd5627ac2012-10-31 09:16:46 -07003935 wait_event_interruptible(conn->queues_wq,
3936 !iscsit_conn_all_queues_empty(conn) ||
3937 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003938
3939 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3940 signal_pending(current))
3941 goto transport_err;
3942
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003943get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003944 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003945 if (ret < 0)
3946 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003947
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003948 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003949 if (ret == 1)
3950 goto get_immediate;
3951 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003952 goto restart;
3953 else if (ret < 0)
3954 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003955 }
3956
3957transport_err:
3958 iscsit_take_action_for_connection_exit(conn);
3959 goto restart;
3960out:
3961 return 0;
3962}
3963
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003964static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3965{
3966 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3967 struct iscsi_cmd *cmd;
3968 int ret = 0;
3969
3970 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3971 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003972 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003973 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003974 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003975
3976 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3977 break;
3978 case ISCSI_OP_SCSI_DATA_OUT:
3979 ret = iscsit_handle_data_out(conn, buf);
3980 break;
3981 case ISCSI_OP_NOOP_OUT:
3982 cmd = NULL;
3983 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003984 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003985 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003986 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003987 }
3988 ret = iscsit_handle_nop_out(conn, cmd, buf);
3989 break;
3990 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003991 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003992 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003993 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003994
3995 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3996 break;
3997 case ISCSI_OP_TEXT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00003998 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003999 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004000 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004001
4002 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004003 break;
4004 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004005 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004006 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004007 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004008
4009 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4010 if (ret > 0)
4011 wait_for_completion_timeout(&conn->conn_logout_comp,
4012 SECONDS_FOR_LOGOUT_COMP * HZ);
4013 break;
4014 case ISCSI_OP_SNACK:
4015 ret = iscsit_handle_snack(conn, buf);
4016 break;
4017 default:
4018 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4019 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4020 pr_err("Cannot recover from unknown"
4021 " opcode while ERL=0, closing iSCSI connection.\n");
4022 return -1;
4023 }
4024 if (!conn->conn_ops->OFMarker) {
4025 pr_err("Unable to recover from unknown"
4026 " opcode while OFMarker=No, closing iSCSI"
4027 " connection.\n");
4028 return -1;
4029 }
4030 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4031 pr_err("Unable to recover from unknown"
4032 " opcode, closing iSCSI connection.\n");
4033 return -1;
4034 }
4035 break;
4036 }
4037
4038 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004039reject:
4040 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004041}
4042
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004043int iscsi_target_rx_thread(void *arg)
4044{
4045 int ret;
4046 u8 buffer[ISCSI_HDR_LEN], opcode;
4047 u32 checksum = 0, digest = 0;
4048 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004049 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004050 struct kvec iov;
4051 /*
4052 * Allow ourselves to be interrupted by SIGINT so that a
4053 * connection recovery / failure event can be triggered externally.
4054 */
4055 allow_signal(SIGINT);
4056
4057restart:
4058 conn = iscsi_rx_thread_pre_handler(ts);
4059 if (!conn)
4060 goto out;
4061
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004062 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4063 struct completion comp;
4064 int rc;
4065
4066 init_completion(&comp);
4067 rc = wait_for_completion_interruptible(&comp);
4068 if (rc < 0)
4069 goto transport_err;
4070
4071 goto out;
4072 }
4073
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004074 while (!kthread_should_stop()) {
4075 /*
4076 * Ensure that both TX and RX per connection kthreads
4077 * are scheduled to run on the same CPU.
4078 */
4079 iscsit_thread_check_cpumask(conn, current, 0);
4080
4081 memset(buffer, 0, ISCSI_HDR_LEN);
4082 memset(&iov, 0, sizeof(struct kvec));
4083
4084 iov.iov_base = buffer;
4085 iov.iov_len = ISCSI_HDR_LEN;
4086
4087 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4088 if (ret != ISCSI_HDR_LEN) {
4089 iscsit_rx_thread_wait_for_tcp(conn);
4090 goto transport_err;
4091 }
4092
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004093 if (conn->conn_ops->HeaderDigest) {
4094 iov.iov_base = &digest;
4095 iov.iov_len = ISCSI_CRC_LEN;
4096
4097 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4098 if (ret != ISCSI_CRC_LEN) {
4099 iscsit_rx_thread_wait_for_tcp(conn);
4100 goto transport_err;
4101 }
4102
4103 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4104 buffer, ISCSI_HDR_LEN,
4105 0, NULL, (u8 *)&checksum);
4106
4107 if (digest != checksum) {
4108 pr_err("HeaderDigest CRC32C failed,"
4109 " received 0x%08x, computed 0x%08x\n",
4110 digest, checksum);
4111 /*
4112 * Set the PDU to 0xff so it will intentionally
4113 * hit default in the switch below.
4114 */
4115 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004116 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004117 } else {
4118 pr_debug("Got HeaderDigest CRC32C"
4119 " 0x%08x\n", checksum);
4120 }
4121 }
4122
4123 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4124 goto transport_err;
4125
4126 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4127
4128 if (conn->sess->sess_ops->SessionType &&
4129 ((!(opcode & ISCSI_OP_TEXT)) ||
4130 (!(opcode & ISCSI_OP_LOGOUT)))) {
4131 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4132 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004133 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4134 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004135 goto transport_err;
4136 }
4137
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004138 ret = iscsi_target_rx_opcode(conn, buffer);
4139 if (ret < 0)
4140 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004141 }
4142
4143transport_err:
4144 if (!signal_pending(current))
4145 atomic_set(&conn->transport_failed, 1);
4146 iscsit_take_action_for_connection_exit(conn);
4147 goto restart;
4148out:
4149 return 0;
4150}
4151
4152static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4153{
4154 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4155 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004156 /*
4157 * We expect this function to only ever be called from either RX or TX
4158 * thread context via iscsit_close_connection() once the other context
4159 * has been reset -> returned sleeping pre-handler state.
4160 */
4161 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004162 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004163
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004164 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004165 spin_unlock_bh(&conn->cmd_lock);
4166
4167 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004168
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004169 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004170
4171 spin_lock_bh(&conn->cmd_lock);
4172 }
4173 spin_unlock_bh(&conn->cmd_lock);
4174}
4175
4176static void iscsit_stop_timers_for_cmds(
4177 struct iscsi_conn *conn)
4178{
4179 struct iscsi_cmd *cmd;
4180
4181 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004182 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004183 if (cmd->data_direction == DMA_TO_DEVICE)
4184 iscsit_stop_dataout_timer(cmd);
4185 }
4186 spin_unlock_bh(&conn->cmd_lock);
4187}
4188
4189int iscsit_close_connection(
4190 struct iscsi_conn *conn)
4191{
4192 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4193 struct iscsi_session *sess = conn->sess;
4194
4195 pr_debug("Closing iSCSI connection CID %hu on SID:"
4196 " %u\n", conn->cid, sess->sid);
4197 /*
4198 * Always up conn_logout_comp just in case the RX Thread is sleeping
4199 * and the logout response never got sent because the connection
4200 * failed.
4201 */
4202 complete(&conn->conn_logout_comp);
4203
4204 iscsi_release_thread_set(conn);
4205
4206 iscsit_stop_timers_for_cmds(conn);
4207 iscsit_stop_nopin_response_timer(conn);
4208 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004209
4210 if (conn->conn_transport->iscsit_wait_conn)
4211 conn->conn_transport->iscsit_wait_conn(conn);
4212
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004213 iscsit_free_queue_reqs_for_conn(conn);
4214
4215 /*
4216 * During Connection recovery drop unacknowledged out of order
4217 * commands for this connection, and prepare the other commands
4218 * for realligence.
4219 *
4220 * During normal operation clear the out of order commands (but
4221 * do not free the struct iscsi_ooo_cmdsn's) and release all
4222 * struct iscsi_cmds.
4223 */
4224 if (atomic_read(&conn->connection_recovery)) {
4225 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4226 iscsit_prepare_cmds_for_realligance(conn);
4227 } else {
4228 iscsit_clear_ooo_cmdsns_for_conn(conn);
4229 iscsit_release_commands_from_conn(conn);
4230 }
4231
4232 /*
4233 * Handle decrementing session or connection usage count if
4234 * a logout response was not able to be sent because the
4235 * connection failed. Fall back to Session Recovery here.
4236 */
4237 if (atomic_read(&conn->conn_logout_remove)) {
4238 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4239 iscsit_dec_conn_usage_count(conn);
4240 iscsit_dec_session_usage_count(sess);
4241 }
4242 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4243 iscsit_dec_conn_usage_count(conn);
4244
4245 atomic_set(&conn->conn_logout_remove, 0);
4246 atomic_set(&sess->session_reinstatement, 0);
4247 atomic_set(&sess->session_fall_back_to_erl0, 1);
4248 }
4249
4250 spin_lock_bh(&sess->conn_lock);
4251 list_del(&conn->conn_list);
4252
4253 /*
4254 * Attempt to let the Initiator know this connection failed by
4255 * sending an Connection Dropped Async Message on another
4256 * active connection.
4257 */
4258 if (atomic_read(&conn->connection_recovery))
4259 iscsit_build_conn_drop_async_message(conn);
4260
4261 spin_unlock_bh(&sess->conn_lock);
4262
4263 /*
4264 * If connection reinstatement is being performed on this connection,
4265 * up the connection reinstatement semaphore that is being blocked on
4266 * in iscsit_cause_connection_reinstatement().
4267 */
4268 spin_lock_bh(&conn->state_lock);
4269 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4270 spin_unlock_bh(&conn->state_lock);
4271 complete(&conn->conn_wait_comp);
4272 wait_for_completion(&conn->conn_post_wait_comp);
4273 spin_lock_bh(&conn->state_lock);
4274 }
4275
4276 /*
4277 * If connection reinstatement is being performed on this connection
4278 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4279 * connection wait rcfr semaphore that is being blocked on
4280 * an iscsit_connection_reinstatement_rcfr().
4281 */
4282 if (atomic_read(&conn->connection_wait_rcfr)) {
4283 spin_unlock_bh(&conn->state_lock);
4284 complete(&conn->conn_wait_rcfr_comp);
4285 wait_for_completion(&conn->conn_post_wait_comp);
4286 spin_lock_bh(&conn->state_lock);
4287 }
4288 atomic_set(&conn->connection_reinstatement, 1);
4289 spin_unlock_bh(&conn->state_lock);
4290
4291 /*
4292 * If any other processes are accessing this connection pointer we
4293 * must wait until they have completed.
4294 */
4295 iscsit_check_conn_usage_count(conn);
4296
4297 if (conn->conn_rx_hash.tfm)
4298 crypto_free_hash(conn->conn_rx_hash.tfm);
4299 if (conn->conn_tx_hash.tfm)
4300 crypto_free_hash(conn->conn_tx_hash.tfm);
4301
4302 if (conn->conn_cpumask)
4303 free_cpumask_var(conn->conn_cpumask);
4304
4305 kfree(conn->conn_ops);
4306 conn->conn_ops = NULL;
4307
Al Virobf6932f2012-07-21 08:55:18 +01004308 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004309 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004310
4311 if (conn->conn_transport->iscsit_free_conn)
4312 conn->conn_transport->iscsit_free_conn(conn);
4313
4314 iscsit_put_transport(conn->conn_transport);
4315
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004316 conn->thread_set = NULL;
4317
4318 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4319 conn->conn_state = TARG_CONN_STATE_FREE;
4320 kfree(conn);
4321
4322 spin_lock_bh(&sess->conn_lock);
4323 atomic_dec(&sess->nconn);
4324 pr_debug("Decremented iSCSI connection count to %hu from node:"
4325 " %s\n", atomic_read(&sess->nconn),
4326 sess->sess_ops->InitiatorName);
4327 /*
4328 * Make sure that if one connection fails in an non ERL=2 iSCSI
4329 * Session that they all fail.
4330 */
4331 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4332 !atomic_read(&sess->session_logout))
4333 atomic_set(&sess->session_fall_back_to_erl0, 1);
4334
4335 /*
4336 * If this was not the last connection in the session, and we are
4337 * performing session reinstatement or falling back to ERL=0, call
4338 * iscsit_stop_session() without sleeping to shutdown the other
4339 * active connections.
4340 */
4341 if (atomic_read(&sess->nconn)) {
4342 if (!atomic_read(&sess->session_reinstatement) &&
4343 !atomic_read(&sess->session_fall_back_to_erl0)) {
4344 spin_unlock_bh(&sess->conn_lock);
4345 return 0;
4346 }
4347 if (!atomic_read(&sess->session_stop_active)) {
4348 atomic_set(&sess->session_stop_active, 1);
4349 spin_unlock_bh(&sess->conn_lock);
4350 iscsit_stop_session(sess, 0, 0);
4351 return 0;
4352 }
4353 spin_unlock_bh(&sess->conn_lock);
4354 return 0;
4355 }
4356
4357 /*
4358 * If this was the last connection in the session and one of the
4359 * following is occurring:
4360 *
4361 * Session Reinstatement is not being performed, and are falling back
4362 * to ERL=0 call iscsit_close_session().
4363 *
4364 * Session Logout was requested. iscsit_close_session() will be called
4365 * elsewhere.
4366 *
4367 * Session Continuation is not being performed, start the Time2Retain
4368 * handler and check if sleep_on_sess_wait_sem is active.
4369 */
4370 if (!atomic_read(&sess->session_reinstatement) &&
4371 atomic_read(&sess->session_fall_back_to_erl0)) {
4372 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004373 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004374
4375 return 0;
4376 } else if (atomic_read(&sess->session_logout)) {
4377 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4378 sess->session_state = TARG_SESS_STATE_FREE;
4379 spin_unlock_bh(&sess->conn_lock);
4380
4381 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4382 complete(&sess->session_wait_comp);
4383
4384 return 0;
4385 } else {
4386 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4387 sess->session_state = TARG_SESS_STATE_FAILED;
4388
4389 if (!atomic_read(&sess->session_continuation)) {
4390 spin_unlock_bh(&sess->conn_lock);
4391 iscsit_start_time2retain_handler(sess);
4392 } else
4393 spin_unlock_bh(&sess->conn_lock);
4394
4395 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4396 complete(&sess->session_wait_comp);
4397
4398 return 0;
4399 }
4400 spin_unlock_bh(&sess->conn_lock);
4401
4402 return 0;
4403}
4404
4405int iscsit_close_session(struct iscsi_session *sess)
4406{
Andy Grover60bfcf82013-10-09 11:05:58 -07004407 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004408 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4409
4410 if (atomic_read(&sess->nconn)) {
4411 pr_err("%d connection(s) still exist for iSCSI session"
4412 " to %s\n", atomic_read(&sess->nconn),
4413 sess->sess_ops->InitiatorName);
4414 BUG();
4415 }
4416
4417 spin_lock_bh(&se_tpg->session_lock);
4418 atomic_set(&sess->session_logout, 1);
4419 atomic_set(&sess->session_reinstatement, 1);
4420 iscsit_stop_time2retain_timer(sess);
4421 spin_unlock_bh(&se_tpg->session_lock);
4422
4423 /*
4424 * transport_deregister_session_configfs() will clear the
4425 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4426 * can be setting it again with __transport_register_session() in
4427 * iscsi_post_login_handler() again after the iscsit_stop_session()
4428 * completes in iscsi_np context.
4429 */
4430 transport_deregister_session_configfs(sess->se_sess);
4431
4432 /*
4433 * If any other processes are accessing this session pointer we must
4434 * wait until they have completed. If we are in an interrupt (the
4435 * time2retain handler) and contain and active session usage count we
4436 * restart the timer and exit.
4437 */
4438 if (!in_interrupt()) {
4439 if (iscsit_check_session_usage_count(sess) == 1)
4440 iscsit_stop_session(sess, 1, 1);
4441 } else {
4442 if (iscsit_check_session_usage_count(sess) == 2) {
4443 atomic_set(&sess->session_logout, 0);
4444 iscsit_start_time2retain_handler(sess);
4445 return 0;
4446 }
4447 }
4448
4449 transport_deregister_session(sess->se_sess);
4450
4451 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4452 iscsit_free_connection_recovery_entires(sess);
4453
4454 iscsit_free_all_ooo_cmdsns(sess);
4455
4456 spin_lock_bh(&se_tpg->session_lock);
4457 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4458 sess->session_state = TARG_SESS_STATE_FREE;
4459 pr_debug("Released iSCSI session from node: %s\n",
4460 sess->sess_ops->InitiatorName);
4461 tpg->nsessions--;
4462 if (tpg->tpg_tiqn)
4463 tpg->tpg_tiqn->tiqn_nsessions--;
4464
4465 pr_debug("Decremented number of active iSCSI Sessions on"
4466 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4467
4468 spin_lock(&sess_idr_lock);
4469 idr_remove(&sess_idr, sess->session_index);
4470 spin_unlock(&sess_idr_lock);
4471
4472 kfree(sess->sess_ops);
4473 sess->sess_ops = NULL;
4474 spin_unlock_bh(&se_tpg->session_lock);
4475
4476 kfree(sess);
4477 return 0;
4478}
4479
4480static void iscsit_logout_post_handler_closesession(
4481 struct iscsi_conn *conn)
4482{
4483 struct iscsi_session *sess = conn->sess;
4484
4485 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4486 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4487
4488 atomic_set(&conn->conn_logout_remove, 0);
4489 complete(&conn->conn_logout_comp);
4490
4491 iscsit_dec_conn_usage_count(conn);
4492 iscsit_stop_session(sess, 1, 1);
4493 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004494 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004495}
4496
4497static void iscsit_logout_post_handler_samecid(
4498 struct iscsi_conn *conn)
4499{
4500 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4501 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4502
4503 atomic_set(&conn->conn_logout_remove, 0);
4504 complete(&conn->conn_logout_comp);
4505
4506 iscsit_cause_connection_reinstatement(conn, 1);
4507 iscsit_dec_conn_usage_count(conn);
4508}
4509
4510static void iscsit_logout_post_handler_diffcid(
4511 struct iscsi_conn *conn,
4512 u16 cid)
4513{
4514 struct iscsi_conn *l_conn;
4515 struct iscsi_session *sess = conn->sess;
4516
4517 if (!sess)
4518 return;
4519
4520 spin_lock_bh(&sess->conn_lock);
4521 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4522 if (l_conn->cid == cid) {
4523 iscsit_inc_conn_usage_count(l_conn);
4524 break;
4525 }
4526 }
4527 spin_unlock_bh(&sess->conn_lock);
4528
4529 if (!l_conn)
4530 return;
4531
4532 if (l_conn->sock)
4533 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4534
4535 spin_lock_bh(&l_conn->state_lock);
4536 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4537 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4538 spin_unlock_bh(&l_conn->state_lock);
4539
4540 iscsit_cause_connection_reinstatement(l_conn, 1);
4541 iscsit_dec_conn_usage_count(l_conn);
4542}
4543
4544/*
4545 * Return of 0 causes the TX thread to restart.
4546 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004547int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004548 struct iscsi_cmd *cmd,
4549 struct iscsi_conn *conn)
4550{
4551 int ret = 0;
4552
4553 switch (cmd->logout_reason) {
4554 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4555 switch (cmd->logout_response) {
4556 case ISCSI_LOGOUT_SUCCESS:
4557 case ISCSI_LOGOUT_CLEANUP_FAILED:
4558 default:
4559 iscsit_logout_post_handler_closesession(conn);
4560 break;
4561 }
4562 ret = 0;
4563 break;
4564 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4565 if (conn->cid == cmd->logout_cid) {
4566 switch (cmd->logout_response) {
4567 case ISCSI_LOGOUT_SUCCESS:
4568 case ISCSI_LOGOUT_CLEANUP_FAILED:
4569 default:
4570 iscsit_logout_post_handler_samecid(conn);
4571 break;
4572 }
4573 ret = 0;
4574 } else {
4575 switch (cmd->logout_response) {
4576 case ISCSI_LOGOUT_SUCCESS:
4577 iscsit_logout_post_handler_diffcid(conn,
4578 cmd->logout_cid);
4579 break;
4580 case ISCSI_LOGOUT_CID_NOT_FOUND:
4581 case ISCSI_LOGOUT_CLEANUP_FAILED:
4582 default:
4583 break;
4584 }
4585 ret = 1;
4586 }
4587 break;
4588 case ISCSI_LOGOUT_REASON_RECOVERY:
4589 switch (cmd->logout_response) {
4590 case ISCSI_LOGOUT_SUCCESS:
4591 case ISCSI_LOGOUT_CID_NOT_FOUND:
4592 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4593 case ISCSI_LOGOUT_CLEANUP_FAILED:
4594 default:
4595 break;
4596 }
4597 ret = 1;
4598 break;
4599 default:
4600 break;
4601
4602 }
4603 return ret;
4604}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004605EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004606
4607void iscsit_fail_session(struct iscsi_session *sess)
4608{
4609 struct iscsi_conn *conn;
4610
4611 spin_lock_bh(&sess->conn_lock);
4612 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4613 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4614 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4615 }
4616 spin_unlock_bh(&sess->conn_lock);
4617
4618 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4619 sess->session_state = TARG_SESS_STATE_FAILED;
4620}
4621
4622int iscsit_free_session(struct iscsi_session *sess)
4623{
4624 u16 conn_count = atomic_read(&sess->nconn);
4625 struct iscsi_conn *conn, *conn_tmp = NULL;
4626 int is_last;
4627
4628 spin_lock_bh(&sess->conn_lock);
4629 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4630
4631 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4632 conn_list) {
4633 if (conn_count == 0)
4634 break;
4635
4636 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4637 is_last = 1;
4638 } else {
4639 iscsit_inc_conn_usage_count(conn_tmp);
4640 is_last = 0;
4641 }
4642 iscsit_inc_conn_usage_count(conn);
4643
4644 spin_unlock_bh(&sess->conn_lock);
4645 iscsit_cause_connection_reinstatement(conn, 1);
4646 spin_lock_bh(&sess->conn_lock);
4647
4648 iscsit_dec_conn_usage_count(conn);
4649 if (is_last == 0)
4650 iscsit_dec_conn_usage_count(conn_tmp);
4651
4652 conn_count--;
4653 }
4654
4655 if (atomic_read(&sess->nconn)) {
4656 spin_unlock_bh(&sess->conn_lock);
4657 wait_for_completion(&sess->session_wait_comp);
4658 } else
4659 spin_unlock_bh(&sess->conn_lock);
4660
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004661 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004662 return 0;
4663}
4664
4665void iscsit_stop_session(
4666 struct iscsi_session *sess,
4667 int session_sleep,
4668 int connection_sleep)
4669{
4670 u16 conn_count = atomic_read(&sess->nconn);
4671 struct iscsi_conn *conn, *conn_tmp = NULL;
4672 int is_last;
4673
4674 spin_lock_bh(&sess->conn_lock);
4675 if (session_sleep)
4676 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4677
4678 if (connection_sleep) {
4679 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4680 conn_list) {
4681 if (conn_count == 0)
4682 break;
4683
4684 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4685 is_last = 1;
4686 } else {
4687 iscsit_inc_conn_usage_count(conn_tmp);
4688 is_last = 0;
4689 }
4690 iscsit_inc_conn_usage_count(conn);
4691
4692 spin_unlock_bh(&sess->conn_lock);
4693 iscsit_cause_connection_reinstatement(conn, 1);
4694 spin_lock_bh(&sess->conn_lock);
4695
4696 iscsit_dec_conn_usage_count(conn);
4697 if (is_last == 0)
4698 iscsit_dec_conn_usage_count(conn_tmp);
4699 conn_count--;
4700 }
4701 } else {
4702 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4703 iscsit_cause_connection_reinstatement(conn, 0);
4704 }
4705
4706 if (session_sleep && atomic_read(&sess->nconn)) {
4707 spin_unlock_bh(&sess->conn_lock);
4708 wait_for_completion(&sess->session_wait_comp);
4709 } else
4710 spin_unlock_bh(&sess->conn_lock);
4711}
4712
4713int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4714{
4715 struct iscsi_session *sess;
4716 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4717 struct se_session *se_sess, *se_sess_tmp;
4718 int session_count = 0;
4719
4720 spin_lock_bh(&se_tpg->session_lock);
4721 if (tpg->nsessions && !force) {
4722 spin_unlock_bh(&se_tpg->session_lock);
4723 return -1;
4724 }
4725
4726 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4727 sess_list) {
4728 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4729
4730 spin_lock(&sess->conn_lock);
4731 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4732 atomic_read(&sess->session_logout) ||
4733 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4734 spin_unlock(&sess->conn_lock);
4735 continue;
4736 }
4737 atomic_set(&sess->session_reinstatement, 1);
4738 spin_unlock(&sess->conn_lock);
4739 spin_unlock_bh(&se_tpg->session_lock);
4740
4741 iscsit_free_session(sess);
4742 spin_lock_bh(&se_tpg->session_lock);
4743
4744 session_count++;
4745 }
4746 spin_unlock_bh(&se_tpg->session_lock);
4747
4748 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4749 " Group: %hu\n", session_count, tpg->tpgt);
4750 return 0;
4751}
4752
4753MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4754MODULE_VERSION("4.1.x");
4755MODULE_AUTHOR("nab@Linux-iSCSI.org");
4756MODULE_LICENSE("GPL");
4757
4758module_init(iscsi_target_init_module);
4759module_exit(iscsi_target_cleanup_module);