blob: 5d75bb41869639de6c56d4353113c0d46b83fd86 [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
Sagi Grimberg67f091f2015-01-07 14:57:31 +020033#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000034#include "iscsi_target_parameters.h"
35#include "iscsi_target_seq_pdu_list.h"
Nicholas Bellingere48354c2011-07-23 06:43:04 +000036#include "iscsi_target_datain_values.h"
37#include "iscsi_target_erl0.h"
38#include "iscsi_target_erl1.h"
39#include "iscsi_target_erl2.h"
40#include "iscsi_target_login.h"
41#include "iscsi_target_tmr.h"
42#include "iscsi_target_tpg.h"
43#include "iscsi_target_util.h"
44#include "iscsi_target.h"
45#include "iscsi_target_device.h"
Sagi Grimberg67f091f2015-01-07 14:57:31 +020046#include <target/iscsi/iscsi_target_stat.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000047
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080048#include <target/iscsi/iscsi_transport.h>
49
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050static LIST_HEAD(g_tiqn_list);
51static LIST_HEAD(g_np_list);
52static DEFINE_SPINLOCK(tiqn_lock);
Andy Groveree291e62014-01-24 16:18:54 -080053static DEFINE_MUTEX(np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000054
55static struct idr tiqn_idr;
56struct idr sess_idr;
57struct mutex auth_id_lock;
58spinlock_t sess_idr_lock;
59
60struct iscsit_global *iscsit_global;
61
Nicholas Bellingere48354c2011-07-23 06:43:04 +000062struct kmem_cache *lio_qr_cache;
63struct kmem_cache *lio_dr_cache;
64struct kmem_cache *lio_ooo_cache;
65struct kmem_cache *lio_r2t_cache;
66
67static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070068 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000069
70struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
71{
72 struct iscsi_tiqn *tiqn = NULL;
73
74 spin_lock(&tiqn_lock);
75 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
76 if (!strcmp(tiqn->tiqn, buf)) {
77
78 spin_lock(&tiqn->tiqn_state_lock);
79 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
80 tiqn->tiqn_access_count++;
81 spin_unlock(&tiqn->tiqn_state_lock);
82 spin_unlock(&tiqn_lock);
83 return tiqn;
84 }
85 spin_unlock(&tiqn->tiqn_state_lock);
86 }
87 }
88 spin_unlock(&tiqn_lock);
89
90 return NULL;
91}
92
93static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
94{
95 spin_lock(&tiqn->tiqn_state_lock);
96 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
97 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
98 spin_unlock(&tiqn->tiqn_state_lock);
99 return 0;
100 }
101 spin_unlock(&tiqn->tiqn_state_lock);
102
103 return -1;
104}
105
106void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
107{
108 spin_lock(&tiqn->tiqn_state_lock);
109 tiqn->tiqn_access_count--;
110 spin_unlock(&tiqn->tiqn_state_lock);
111}
112
113/*
114 * Note that IQN formatting is expected to be done in userspace, and
115 * no explict IQN format checks are done here.
116 */
117struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
118{
119 struct iscsi_tiqn *tiqn = NULL;
120 int ret;
121
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300122 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000123 pr_err("Target IQN exceeds %d bytes\n",
124 ISCSI_IQN_LEN);
125 return ERR_PTR(-EINVAL);
126 }
127
128 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
129 if (!tiqn) {
130 pr_err("Unable to allocate struct iscsi_tiqn\n");
131 return ERR_PTR(-ENOMEM);
132 }
133
134 sprintf(tiqn->tiqn, "%s", buf);
135 INIT_LIST_HEAD(&tiqn->tiqn_list);
136 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
137 spin_lock_init(&tiqn->tiqn_state_lock);
138 spin_lock_init(&tiqn->tiqn_tpg_lock);
139 spin_lock_init(&tiqn->sess_err_stats.lock);
140 spin_lock_init(&tiqn->login_stats.lock);
141 spin_lock_init(&tiqn->logout_stats.lock);
142
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000143 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
144
Tejun Heoc9365bd2013-02-27 17:04:43 -0800145 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000146 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800147
148 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000149 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800150 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 kfree(tiqn);
154 return ERR_PTR(ret);
155 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800156 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000157 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161
162 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
163
164 return tiqn;
165
166}
167
168static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
169{
170 /*
171 * Wait for accesses to said struct iscsi_tiqn to end.
172 */
173 spin_lock(&tiqn->tiqn_state_lock);
174 while (tiqn->tiqn_access_count != 0) {
175 spin_unlock(&tiqn->tiqn_state_lock);
176 msleep(10);
177 spin_lock(&tiqn->tiqn_state_lock);
178 }
179 spin_unlock(&tiqn->tiqn_state_lock);
180}
181
182void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
183{
184 /*
185 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
186 * while holding tiqn->tiqn_state_lock. This means that all subsequent
187 * attempts to access this struct iscsi_tiqn will fail from both transport
188 * fabric and control code paths.
189 */
190 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
191 pr_err("iscsit_set_tiqn_shutdown() failed\n");
192 return;
193 }
194
195 iscsit_wait_for_tiqn(tiqn);
196
197 spin_lock(&tiqn_lock);
198 list_del(&tiqn->tiqn_list);
199 idr_remove(&tiqn_idr, tiqn->tiqn_index);
200 spin_unlock(&tiqn_lock);
201
202 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
203 tiqn->tiqn);
204 kfree(tiqn);
205}
206
207int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
208{
209 int ret;
210 /*
211 * Determine if the network portal is accepting storage traffic.
212 */
213 spin_lock_bh(&np->np_thread_lock);
214 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
215 spin_unlock_bh(&np->np_thread_lock);
216 return -1;
217 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000218 spin_unlock_bh(&np->np_thread_lock);
219 /*
220 * Determine if the portal group is accepting storage traffic.
221 */
222 spin_lock_bh(&tpg->tpg_state_lock);
223 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
224 spin_unlock_bh(&tpg->tpg_state_lock);
225 return -1;
226 }
227 spin_unlock_bh(&tpg->tpg_state_lock);
228
229 /*
230 * Here we serialize access across the TIQN+TPG Tuple.
231 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700232 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000233 if ((ret != 0) || signal_pending(current))
234 return -1;
235
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700236 spin_lock_bh(&tpg->tpg_state_lock);
237 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
238 spin_unlock_bh(&tpg->tpg_state_lock);
239 up(&tpg->np_login_sem);
240 return -1;
241 }
242 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000243
244 return 0;
245}
246
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700247void iscsit_login_kref_put(struct kref *kref)
248{
249 struct iscsi_tpg_np *tpg_np = container_of(kref,
250 struct iscsi_tpg_np, tpg_np_kref);
251
252 complete(&tpg_np->tpg_np_comp);
253}
254
255int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
256 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000257{
258 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
259
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700260 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000261
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700262 if (tpg_np)
263 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000264
265 if (tiqn)
266 iscsit_put_tiqn_for_login(tiqn);
267
268 return 0;
269}
270
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800271bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000272 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 int network_transport)
275{
276 struct sockaddr_in *sock_in, *sock_in_e;
277 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800278 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000279 u16 port;
280
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800281 if (sockaddr->ss_family == AF_INET6) {
282 sock_in6 = (struct sockaddr_in6 *)sockaddr;
283 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
284
285 if (!memcmp(&sock_in6->sin6_addr.in6_u,
286 &sock_in6_e->sin6_addr.in6_u,
287 sizeof(struct in6_addr)))
288 ip_match = true;
289
290 port = ntohs(sock_in6->sin6_port);
291 } else {
292 sock_in = (struct sockaddr_in *)sockaddr;
293 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
294
295 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
296 ip_match = true;
297
298 port = ntohs(sock_in->sin_port);
299 }
300
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200301 if (ip_match && (np->np_port == port) &&
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800302 (np->np_network_transport == network_transport))
303 return true;
304
305 return false;
306}
307
Andy Groveree291e62014-01-24 16:18:54 -0800308/*
309 * Called with mutex np_lock held
310 */
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800311static struct iscsi_np *iscsit_get_np(
312 struct __kernel_sockaddr_storage *sockaddr,
313 int network_transport)
314{
315 struct iscsi_np *np;
316 bool match;
317
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000318 list_for_each_entry(np, &g_np_list, np_list) {
Andy Groveree291e62014-01-24 16:18:54 -0800319 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
Andy Groveree291e62014-01-24 16:18:54 -0800321 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 continue;
323 }
324
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800325 match = iscsit_check_np_match(sockaddr, np, network_transport);
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +0200326 if (match) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000327 /*
328 * Increment the np_exports reference count now to
329 * prevent iscsit_del_np() below from being called
330 * while iscsi_tpg_add_network_portal() is called.
331 */
332 np->np_exports++;
Andy Groveree291e62014-01-24 16:18:54 -0800333 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000334 return np;
335 }
Andy Groveree291e62014-01-24 16:18:54 -0800336 spin_unlock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000337 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000338
339 return NULL;
340}
341
342struct iscsi_np *iscsit_add_np(
343 struct __kernel_sockaddr_storage *sockaddr,
344 char *ip_str,
345 int network_transport)
346{
347 struct sockaddr_in *sock_in;
348 struct sockaddr_in6 *sock_in6;
349 struct iscsi_np *np;
350 int ret;
Andy Groveree291e62014-01-24 16:18:54 -0800351
352 mutex_lock(&np_lock);
353
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000354 /*
355 * Locate the existing struct iscsi_np if already active..
356 */
357 np = iscsit_get_np(sockaddr, network_transport);
Andy Groveree291e62014-01-24 16:18:54 -0800358 if (np) {
359 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000360 return np;
Andy Groveree291e62014-01-24 16:18:54 -0800361 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000362
363 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
364 if (!np) {
365 pr_err("Unable to allocate memory for struct iscsi_np\n");
Andy Groveree291e62014-01-24 16:18:54 -0800366 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000367 return ERR_PTR(-ENOMEM);
368 }
369
370 np->np_flags |= NPF_IP_NETWORK;
371 if (sockaddr->ss_family == AF_INET6) {
372 sock_in6 = (struct sockaddr_in6 *)sockaddr;
373 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
374 np->np_port = ntohs(sock_in6->sin6_port);
375 } else {
376 sock_in = (struct sockaddr_in *)sockaddr;
377 sprintf(np->np_ip, "%s", ip_str);
378 np->np_port = ntohs(sock_in->sin_port);
379 }
380
381 np->np_network_transport = network_transport;
382 spin_lock_init(&np->np_thread_lock);
383 init_completion(&np->np_restart_comp);
384 INIT_LIST_HEAD(&np->np_list);
385
386 ret = iscsi_target_setup_login_socket(np, sockaddr);
387 if (ret != 0) {
388 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800389 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000390 return ERR_PTR(ret);
391 }
392
393 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
394 if (IS_ERR(np->np_thread)) {
395 pr_err("Unable to create kthread: iscsi_np\n");
396 ret = PTR_ERR(np->np_thread);
397 kfree(np);
Andy Groveree291e62014-01-24 16:18:54 -0800398 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000399 return ERR_PTR(ret);
400 }
401 /*
402 * Increment the np_exports reference count now to prevent
403 * iscsit_del_np() below from being run while a new call to
404 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
405 * active. We don't need to hold np->np_thread_lock at this
406 * point because iscsi_np has not been added to g_np_list yet.
407 */
408 np->np_exports = 1;
Andy Groveree291e62014-01-24 16:18:54 -0800409 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000410
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000411 list_add_tail(&np->np_list, &g_np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800412 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000413
414 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800415 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416
417 return np;
418}
419
420int iscsit_reset_np_thread(
421 struct iscsi_np *np,
422 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700423 struct iscsi_portal_group *tpg,
424 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000425{
426 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000427 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
428 spin_unlock_bh(&np->np_thread_lock);
429 return 0;
430 }
431 np->np_thread_state = ISCSI_NP_THREAD_RESET;
432
433 if (np->np_thread) {
434 spin_unlock_bh(&np->np_thread_lock);
435 send_sig(SIGINT, np->np_thread, 1);
436 wait_for_completion(&np->np_restart_comp);
437 spin_lock_bh(&np->np_thread_lock);
438 }
439 spin_unlock_bh(&np->np_thread_lock);
440
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700441 if (tpg_np && shutdown) {
442 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
443
444 wait_for_completion(&tpg_np->tpg_np_comp);
445 }
446
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000447 return 0;
448}
449
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800450static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000451{
Al Virobf6932f2012-07-21 08:55:18 +0100452 if (np->np_socket)
453 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000454}
455
456int iscsit_del_np(struct iscsi_np *np)
457{
458 spin_lock_bh(&np->np_thread_lock);
459 np->np_exports--;
460 if (np->np_exports) {
Nicholas Bellinger2363d192014-06-03 18:27:52 -0700461 np->enabled = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000462 spin_unlock_bh(&np->np_thread_lock);
463 return 0;
464 }
465 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
466 spin_unlock_bh(&np->np_thread_lock);
467
468 if (np->np_thread) {
469 /*
470 * We need to send the signal to wakeup Linux/Net
471 * which may be sleeping in sock_accept()..
472 */
473 send_sig(SIGINT, np->np_thread, 1);
474 kthread_stop(np->np_thread);
Nicholas Bellingerdb6077f2013-12-11 15:45:32 -0800475 np->np_thread = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000476 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477
478 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000479
Andy Groveree291e62014-01-24 16:18:54 -0800480 mutex_lock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000481 list_del(&np->np_list);
Andy Groveree291e62014-01-24 16:18:54 -0800482 mutex_unlock(&np_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000483
484 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800485 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000486
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800487 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000488 kfree(np);
489 return 0;
490}
491
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700492static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
493static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
494
495static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
496{
497 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
498 return 0;
499}
500
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700501static void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
502{
503 bool scsi_cmd = (cmd->iscsi_opcode == ISCSI_OP_SCSI_CMD);
504
505 spin_lock_bh(&conn->cmd_lock);
506 if (!list_empty(&cmd->i_conn_node))
507 list_del_init(&cmd->i_conn_node);
508 spin_unlock_bh(&conn->cmd_lock);
509
510 __iscsit_free_cmd(cmd, scsi_cmd, true);
511}
512
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700513static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
514{
515 return TARGET_PROT_NORMAL;
516}
517
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800518static struct iscsit_transport iscsi_target_transport = {
519 .name = "iSCSI/TCP",
520 .transport_type = ISCSI_TCP,
521 .owner = NULL,
522 .iscsit_setup_np = iscsit_setup_np,
523 .iscsit_accept_np = iscsit_accept_np,
524 .iscsit_free_np = iscsit_free_np,
525 .iscsit_get_login_rx = iscsit_get_login_rx,
526 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800527 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700528 .iscsit_immediate_queue = iscsit_immediate_queue,
529 .iscsit_response_queue = iscsit_response_queue,
530 .iscsit_queue_data_in = iscsit_queue_rsp,
531 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700532 .iscsit_aborted_task = iscsit_aborted_task,
Nicholas Bellingere70beee2014-04-02 12:52:38 -0700533 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800534};
535
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000536static int __init iscsi_target_init_module(void)
537{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800538 int ret = 0, size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000539
540 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
541
542 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
543 if (!iscsit_global) {
544 pr_err("Unable to allocate memory for iscsit_global\n");
545 return -1;
546 }
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800547 spin_lock_init(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000548 mutex_init(&auth_id_lock);
549 spin_lock_init(&sess_idr_lock);
550 idr_init(&tiqn_idr);
551 idr_init(&sess_idr);
552
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200553 ret = target_register_template(&iscsi_ops);
554 if (ret)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000555 goto out;
556
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800557 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
558 iscsit_global->ts_bitmap = vzalloc(size);
559 if (!iscsit_global->ts_bitmap) {
560 pr_err("Unable to allocate iscsit_global->ts_bitmap\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000561 goto configfs_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000562 }
563
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000564 lio_qr_cache = kmem_cache_create("lio_qr_cache",
565 sizeof(struct iscsi_queue_req),
566 __alignof__(struct iscsi_queue_req), 0, NULL);
567 if (!lio_qr_cache) {
568 pr_err("nable to kmem_cache_create() for"
569 " lio_qr_cache\n");
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800570 goto bitmap_out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000571 }
572
573 lio_dr_cache = kmem_cache_create("lio_dr_cache",
574 sizeof(struct iscsi_datain_req),
575 __alignof__(struct iscsi_datain_req), 0, NULL);
576 if (!lio_dr_cache) {
577 pr_err("Unable to kmem_cache_create() for"
578 " lio_dr_cache\n");
579 goto qr_out;
580 }
581
582 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
583 sizeof(struct iscsi_ooo_cmdsn),
584 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
585 if (!lio_ooo_cache) {
586 pr_err("Unable to kmem_cache_create() for"
587 " lio_ooo_cache\n");
588 goto dr_out;
589 }
590
591 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
592 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
593 0, NULL);
594 if (!lio_r2t_cache) {
595 pr_err("Unable to kmem_cache_create() for"
596 " lio_r2t_cache\n");
597 goto ooo_out;
598 }
599
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800600 iscsit_register_transport(&iscsi_target_transport);
601
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000602 if (iscsit_load_discovery_tpg() < 0)
603 goto r2t_out;
604
605 return ret;
606r2t_out:
Lino Sanfilippo7f2c53b2014-11-30 12:00:11 +0100607 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000608 kmem_cache_destroy(lio_r2t_cache);
609ooo_out:
610 kmem_cache_destroy(lio_ooo_cache);
611dr_out:
612 kmem_cache_destroy(lio_dr_cache);
613qr_out:
614 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800615bitmap_out:
616 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000617configfs_out:
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200618 /* XXX: this probably wants it to be it's own unwind step.. */
619 if (iscsit_global->discovery_tpg)
620 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
621 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000622out:
623 kfree(iscsit_global);
624 return -ENOMEM;
625}
626
627static void __exit iscsi_target_cleanup_module(void)
628{
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000629 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800630 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000631 kmem_cache_destroy(lio_qr_cache);
632 kmem_cache_destroy(lio_dr_cache);
633 kmem_cache_destroy(lio_ooo_cache);
634 kmem_cache_destroy(lio_r2t_cache);
635
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200636 /*
637 * Shutdown discovery sessions and disable discovery TPG
638 */
639 if (iscsit_global->discovery_tpg)
640 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
641
642 target_unregister_template(&iscsi_ops);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000643
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -0800644 vfree(iscsit_global->ts_bitmap);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000645 kfree(iscsit_global);
646}
647
Andy Grover8b1e1242012-04-03 15:51:12 -0700648static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700649 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000650 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700651 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000652{
653 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000654
Nicholas Bellinger676687c2014-01-20 03:36:44 +0000655 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000656 if (!cmd)
657 return -1;
658
659 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700660 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000661
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100662 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000663 if (!cmd->buf_ptr) {
664 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700665 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000666 return -1;
667 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000668
669 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700670 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000671 spin_unlock_bh(&conn->cmd_lock);
672
673 cmd->i_state = ISTATE_SEND_REJECT;
674 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
675
Nicholas Bellingerba159912013-07-03 03:48:24 -0700676 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000677}
678
Nicholas Bellingerba159912013-07-03 03:48:24 -0700679static int iscsit_add_reject_from_cmd(
680 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000681 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700682 bool add_to_conn,
683 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000684{
685 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000686
687 if (!cmd->conn) {
688 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
689 cmd->init_task_tag);
690 return -1;
691 }
692 conn = cmd->conn;
693
694 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700695 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000696
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100697 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000698 if (!cmd->buf_ptr) {
699 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700700 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000701 return -1;
702 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000703
704 if (add_to_conn) {
705 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700706 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000707 spin_unlock_bh(&conn->cmd_lock);
708 }
709
710 cmd->i_state = ISTATE_SEND_REJECT;
711 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800712 /*
713 * Perform the kref_put now if se_cmd has already been setup by
714 * scsit_setup_scsi_cmd()
715 */
716 if (cmd->se_cmd.se_tfo != NULL) {
717 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
718 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
719 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700720 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000721}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700722
723static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
724 unsigned char *buf)
725{
726 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
727}
728
729int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
730{
731 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
732}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000733
734/*
735 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700736 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000737 */
738static int iscsit_map_iovec(
739 struct iscsi_cmd *cmd,
740 struct kvec *iov,
741 u32 data_offset,
742 u32 data_length)
743{
744 u32 i = 0;
745 struct scatterlist *sg;
746 unsigned int page_off;
747
748 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700749 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000750 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700751 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000752 page_off = (data_offset % PAGE_SIZE);
753
754 cmd->first_data_sg = sg;
755 cmd->first_data_sg_off = page_off;
756
757 while (data_length) {
758 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
759
760 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
761 iov[i].iov_len = cur_len;
762
763 data_length -= cur_len;
764 page_off = 0;
765 sg = sg_next(sg);
766 i++;
767 }
768
769 cmd->kmapped_nents = i;
770
771 return i;
772}
773
774static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
775{
776 u32 i;
777 struct scatterlist *sg;
778
779 sg = cmd->first_data_sg;
780
781 for (i = 0; i < cmd->kmapped_nents; i++)
782 kunmap(sg_page(&sg[i]));
783}
784
785static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
786{
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700787 LIST_HEAD(ack_list);
788 struct iscsi_cmd *cmd, *cmd_p;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000789
790 conn->exp_statsn = exp_statsn;
791
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800792 if (conn->sess->sess_ops->RDMAExtensions)
793 return;
794
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000795 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700796 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797 spin_lock(&cmd->istate_lock);
798 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800799 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000800 cmd->i_state = ISTATE_REMOVE;
801 spin_unlock(&cmd->istate_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700802 list_move_tail(&cmd->i_conn_node, &ack_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000803 continue;
804 }
805 spin_unlock(&cmd->istate_lock);
806 }
807 spin_unlock_bh(&conn->cmd_lock);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700808
809 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
Nicholas Bellinger5159d762014-02-03 12:53:51 -0800810 list_del_init(&cmd->i_conn_node);
Nicholas Bellingerf56cbbb2013-10-03 13:56:14 -0700811 iscsit_free_cmd(cmd, false);
812 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000813}
814
815static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
816{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700817 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000818
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400819 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000820
821 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
822 if (!cmd->iov_data) {
823 pr_err("Unable to allocate cmd->iov_data\n");
824 return -ENOMEM;
825 }
826
827 cmd->orig_iov_data_count = iov_count;
828 return 0;
829}
830
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800831int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
832 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000833{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800834 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000835 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700836 int iscsi_task_attr;
837 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000838
Nicholas Bellinger04f3b312013-11-13 18:54:45 -0800839 atomic_long_inc(&conn->sess->cmd_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000840
841 hdr = (struct iscsi_scsi_req *) buf;
842 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000843
844 /* FIXME; Add checks for AdditionalHeaderSegment */
845
846 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
847 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
848 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
849 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700850 return iscsit_add_reject_cmd(cmd,
851 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000852 }
853
854 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
855 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
856 /*
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800857 * From RFC-3720 Section 10.3.1:
858 *
859 * "Either or both of R and W MAY be 1 when either the
860 * Expected Data Transfer Length and/or Bidirectional Read
861 * Expected Data Transfer Length are 0"
862 *
863 * For this case, go ahead and clear the unnecssary bits
864 * to avoid any confusion with ->data_direction.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000865 */
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800866 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
867 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000868
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800869 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000870 " set when Expected Data Transfer Length is 0 for"
Nicholas Bellinger4454b662013-11-25 14:53:57 -0800871 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000872 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000873
874 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
875 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
876 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
877 " MUST be set if Expected Data Transfer Length is not 0."
878 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700879 return iscsit_add_reject_cmd(cmd,
880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000881 }
882
883 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
884 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
885 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700886 return iscsit_add_reject_cmd(cmd,
887 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000888 }
889
890 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
891 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
892 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700893 return iscsit_add_reject_cmd(cmd,
894 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000895 }
896
897 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
898 pr_err("ImmediateData=No but DataSegmentLength=%u,"
899 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700900 return iscsit_add_reject_cmd(cmd,
901 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000902 }
903
Nicholas Bellingerba159912013-07-03 03:48:24 -0700904 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000905 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
906 pr_err("Expected Data Transfer Length and Length of"
907 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
908 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700909 return iscsit_add_reject_cmd(cmd,
910 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000911 }
912
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400913 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000914 pr_err("DataSegmentLength: %u is greater than"
915 " EDTL: %u, protocol error.\n", payload_length,
916 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700917 return iscsit_add_reject_cmd(cmd,
918 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000919 }
920
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700921 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000922 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700923 " MaxXmitDataSegmentLength: %u, protocol error.\n",
924 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700925 return iscsit_add_reject_cmd(cmd,
926 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000927 }
928
929 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
930 pr_err("DataSegmentLength: %u is greater than"
931 " FirstBurstLength: %u, protocol error.\n",
932 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700933 return iscsit_add_reject_cmd(cmd,
934 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000935 }
936
937 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
938 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
939 DMA_NONE;
940
Andy Groverd28b11692012-04-03 15:51:22 -0700941 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700942 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
943 /*
944 * Figure out the SAM Task Attribute for the incoming SCSI CDB
945 */
946 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
947 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800948 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700949 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800950 sam_task_attr = TCM_ORDERED_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700951 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800952 sam_task_attr = TCM_HEAD_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700953 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800954 sam_task_attr = TCM_ACA_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700955 else {
956 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
Christoph Hellwig68d81f42014-11-24 07:07:25 -0800957 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
958 sam_task_attr = TCM_SIMPLE_TAG;
Andy Groverd28b11692012-04-03 15:51:22 -0700959 }
960
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000961 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
962 cmd->i_state = ISTATE_NEW_CMD;
963 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
964 cmd->immediate_data = (payload_length) ? 1 : 0;
965 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
966 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
967 if (cmd->unsolicited_data)
968 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
969
970 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
971 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
Sagi Grimbergc1e34b62015-01-26 12:49:05 +0200972 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000973 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
974 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400975 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
976 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000977 cmd->first_burst_len = payload_length;
978
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800979 if (!conn->sess->sess_ops->RDMAExtensions &&
980 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000981 struct iscsi_datain_req *dr;
982
983 dr = iscsit_allocate_datain_req();
984 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700985 return iscsit_add_reject_cmd(cmd,
986 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000987
988 iscsit_attach_datain_req(cmd, dr);
989 }
990
991 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700992 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
993 */
Christoph Hellwig9ac89282015-04-08 20:01:35 +0200994 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400995 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
996 cmd->data_direction, sam_task_attr,
997 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700998
999 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1000 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001001 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1002 conn->cid);
1003
1004 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -07001005
Christoph Hellwigde103c92012-11-06 12:24:09 -08001006 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
1007 scsilun_to_int(&hdr->lun));
1008 if (cmd->sense_reason)
1009 goto attach_cmd;
1010
1011 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
1012 if (cmd->sense_reason) {
1013 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001014 return iscsit_add_reject_cmd(cmd,
1015 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001016 }
Christoph Hellwigde103c92012-11-06 12:24:09 -08001017
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001018 goto attach_cmd;
1019 }
Andy Grovera12f41f2012-04-03 15:51:20 -07001020
Christoph Hellwigde103c92012-11-06 12:24:09 -08001021 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001022 return iscsit_add_reject_cmd(cmd,
1023 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001024 }
1025
1026attach_cmd:
1027 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001028 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001029 spin_unlock_bh(&conn->cmd_lock);
1030 /*
1031 * Check if we need to delay processing because of ALUA
1032 * Active/NonOptimized primary access state..
1033 */
1034 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001035
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001036 return 0;
1037}
1038EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001039
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001040void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1041{
1042 iscsit_set_dataout_sequence_values(cmd);
1043
1044 spin_lock_bh(&cmd->dataout_timeout_lock);
1045 iscsit_start_dataout_timer(cmd, cmd->conn);
1046 spin_unlock_bh(&cmd->dataout_timeout_lock);
1047}
1048EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1049
1050int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1051 struct iscsi_scsi_req *hdr)
1052{
1053 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001054 /*
1055 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1056 * the Immediate Bit is not set, and no Immediate
1057 * Data is attached.
1058 *
1059 * A PDU/CmdSN carrying Immediate Data can only
1060 * be processed after the DataCRC has passed.
1061 * If the DataCRC fails, the CmdSN MUST NOT
1062 * be acknowledged. (See below)
1063 */
1064 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001065 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1066 (unsigned char *)hdr, hdr->cmdsn);
1067 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1068 return -1;
1069 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001070 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001071 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001072 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001073 }
1074
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001075 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001076
1077 /*
1078 * If no Immediate Data is attached, it's OK to return now.
1079 */
1080 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001081 if (!cmd->sense_reason && cmd->unsolicited_data)
1082 iscsit_set_unsoliticed_dataout(cmd);
1083 if (!cmd->sense_reason)
1084 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001085
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001086 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001087 return 0;
1088 }
1089
1090 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001091 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1092 * execution. These exceptions are processed in CmdSN order using
1093 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001094 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001095 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001096 if (cmd->reject_reason)
1097 return 0;
1098
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001099 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001100 }
1101 /*
1102 * Call directly into transport_generic_new_cmd() to perform
1103 * the backend memory allocation.
1104 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001105 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001106 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001107 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001108
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001109 return 0;
1110}
1111EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1112
1113static int
1114iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1115 bool dump_payload)
1116{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001117 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001118 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1119 /*
1120 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1121 */
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02001122 if (dump_payload)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001123 goto after_immediate_data;
1124
1125 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1126 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001127after_immediate_data:
1128 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1129 /*
1130 * A PDU/CmdSN carrying Immediate Data passed
1131 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1132 * Immediate Bit is not set.
1133 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001134 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1135 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001136 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001137 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001138
Nicholas Bellinger9d86a2b2013-08-22 00:05:45 -07001139 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001140 int rc;
1141
1142 rc = iscsit_dump_data_payload(cmd->conn,
1143 cmd->first_burst_len, 1);
1144 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1145 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001146 } else if (cmd->unsolicited_data)
1147 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001148
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001149 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1150 /*
1151 * Immediate Data failed DataCRC and ERL>=1,
1152 * silently drop this PDU and let the initiator
1153 * plug the CmdSN gap.
1154 *
1155 * FIXME: Send Unsolicited NOPIN with reserved
1156 * TTT here to help the initiator figure out
1157 * the missing CmdSN, although they should be
1158 * intelligent enough to determine the missing
1159 * CmdSN and issue a retry to plug the sequence.
1160 */
1161 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001162 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001163 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1164 return -1;
1165
1166 return 0;
1167}
1168
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001169static int
1170iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1171 unsigned char *buf)
1172{
1173 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1174 int rc, immed_data;
1175 bool dump_payload = false;
1176
1177 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1178 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001179 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001180 /*
1181 * Allocation iovecs needed for struct socket operations for
1182 * traditional iSCSI block I/O.
1183 */
1184 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001185 return iscsit_add_reject_cmd(cmd,
1186 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001187 }
1188 immed_data = cmd->immediate_data;
1189
1190 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1191 if (rc < 0)
1192 return rc;
1193 else if (rc > 0)
1194 dump_payload = true;
1195
1196 if (!immed_data)
1197 return 0;
1198
1199 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1200}
1201
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001202static u32 iscsit_do_crypto_hash_sg(
1203 struct hash_desc *hash,
1204 struct iscsi_cmd *cmd,
1205 u32 data_offset,
1206 u32 data_length,
1207 u32 padding,
1208 u8 *pad_bytes)
1209{
1210 u32 data_crc;
1211 u32 i;
1212 struct scatterlist *sg;
1213 unsigned int page_off;
1214
1215 crypto_hash_init(hash);
1216
1217 sg = cmd->first_data_sg;
1218 page_off = cmd->first_data_sg_off;
1219
1220 i = 0;
1221 while (data_length) {
1222 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1223
1224 crypto_hash_update(hash, &sg[i], cur_len);
1225
1226 data_length -= cur_len;
1227 page_off = 0;
1228 i++;
1229 }
1230
1231 if (padding) {
1232 struct scatterlist pad_sg;
1233
1234 sg_init_one(&pad_sg, pad_bytes, padding);
1235 crypto_hash_update(hash, &pad_sg, padding);
1236 }
1237 crypto_hash_final(hash, (u8 *) &data_crc);
1238
1239 return data_crc;
1240}
1241
1242static void iscsit_do_crypto_hash_buf(
1243 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001244 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001245 u32 payload_length,
1246 u32 padding,
1247 u8 *pad_bytes,
1248 u8 *data_crc)
1249{
1250 struct scatterlist sg;
1251
1252 crypto_hash_init(hash);
1253
Jörn Engel8359cf42011-11-24 02:05:51 +01001254 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255 crypto_hash_update(hash, &sg, payload_length);
1256
1257 if (padding) {
1258 sg_init_one(&sg, pad_bytes, padding);
1259 crypto_hash_update(hash, &sg, padding);
1260 }
1261 crypto_hash_final(hash, data_crc);
1262}
1263
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001264int
1265iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1266 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001267{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001268 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001269 struct iscsi_cmd *cmd = NULL;
1270 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001271 u32 payload_length = ntoh24(hdr->dlength);
1272 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001273
1274 if (!payload_length) {
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001275 pr_warn("DataOUT payload is ZERO, ignoring.\n");
1276 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001277 }
1278
1279 /* iSCSI write */
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08001280 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001281
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001282 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001283 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001284 " MaxXmitDataSegmentLength: %u\n", payload_length,
1285 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001286 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1287 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001288 }
1289
1290 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1291 payload_length);
1292 if (!cmd)
1293 return 0;
1294
1295 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1296 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001297 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001298 payload_length, conn->cid);
1299
1300 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1301 pr_err("Command ITT: 0x%08x received DataOUT after"
1302 " last DataOUT received, dumping payload\n",
1303 cmd->init_task_tag);
1304 return iscsit_dump_data_payload(conn, payload_length, 1);
1305 }
1306
1307 if (cmd->data_direction != DMA_TO_DEVICE) {
1308 pr_err("Command ITT: 0x%08x received DataOUT for a"
1309 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellinger97c99b472014-06-20 10:59:57 -07001310 return iscsit_dump_data_payload(conn, payload_length, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001311 }
1312 se_cmd = &cmd->se_cmd;
1313 iscsit_mod_dataout_timer(cmd);
1314
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001315 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001316 pr_err("DataOut Offset: %u, Length %u greater than"
1317 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001318 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001319 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001320 }
1321
1322 if (cmd->unsolicited_data) {
1323 int dump_unsolicited_data = 0;
1324
1325 if (conn->sess->sess_ops->InitialR2T) {
1326 pr_err("Received unexpected unsolicited data"
1327 " while InitialR2T=Yes, protocol error.\n");
1328 transport_send_check_condition_and_sense(&cmd->se_cmd,
1329 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1330 return -1;
1331 }
1332 /*
1333 * Special case for dealing with Unsolicited DataOUT
1334 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1335 * failures;
1336 */
1337
1338 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001339 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001340 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001341 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001342
1343 if (dump_unsolicited_data) {
1344 /*
1345 * Check if a delayed TASK_ABORTED status needs to
1346 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1347 * received with the unsolicitied data out.
1348 */
1349 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1350 iscsit_stop_dataout_timer(cmd);
1351
1352 transport_check_aborted_status(se_cmd,
1353 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1354 return iscsit_dump_data_payload(conn, payload_length, 1);
1355 }
1356 } else {
1357 /*
1358 * For the normal solicited data path:
1359 *
1360 * Check for a delayed TASK_ABORTED status and dump any
1361 * incoming data out payload if one exists. Also, when the
1362 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1363 * data out sequence, we decrement outstanding_r2ts. Once
1364 * outstanding_r2ts reaches zero, go ahead and send the delayed
1365 * TASK_ABORTED status.
1366 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001367 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001368 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1369 if (--cmd->outstanding_r2ts < 1) {
1370 iscsit_stop_dataout_timer(cmd);
1371 transport_check_aborted_status(
1372 se_cmd, 1);
1373 }
1374
1375 return iscsit_dump_data_payload(conn, payload_length, 1);
1376 }
1377 }
1378 /*
1379 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1380 * within-command recovery checks before receiving the payload.
1381 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001382 rc = iscsit_check_pre_dataout(cmd, buf);
1383 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001384 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001385 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001386 return -1;
1387
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001388 *out_cmd = cmd;
1389 return 0;
1390}
1391EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1392
1393static int
1394iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1395 struct iscsi_data *hdr)
1396{
1397 struct kvec *iov;
1398 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1399 u32 payload_length = ntoh24(hdr->dlength);
1400 int iov_ret, data_crc_failed = 0;
1401
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001402 rx_size += payload_length;
1403 iov = &cmd->iov_data[0];
1404
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001405 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1406 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001407 if (iov_ret < 0)
1408 return -1;
1409
1410 iov_count += iov_ret;
1411
1412 padding = ((-payload_length) & 3);
1413 if (padding != 0) {
1414 iov[iov_count].iov_base = cmd->pad_bytes;
1415 iov[iov_count++].iov_len = padding;
1416 rx_size += padding;
1417 pr_debug("Receiving %u padding bytes.\n", padding);
1418 }
1419
1420 if (conn->conn_ops->DataDigest) {
1421 iov[iov_count].iov_base = &checksum;
1422 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1423 rx_size += ISCSI_CRC_LEN;
1424 }
1425
1426 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1427
1428 iscsit_unmap_iovec(cmd);
1429
1430 if (rx_got != rx_size)
1431 return -1;
1432
1433 if (conn->conn_ops->DataDigest) {
1434 u32 data_crc;
1435
1436 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001437 be32_to_cpu(hdr->offset),
1438 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001439 cmd->pad_bytes);
1440
1441 if (checksum != data_crc) {
1442 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1443 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1444 " does not match computed 0x%08x\n",
1445 hdr->itt, hdr->offset, payload_length,
1446 hdr->datasn, checksum, data_crc);
1447 data_crc_failed = 1;
1448 } else {
1449 pr_debug("Got CRC32C DataDigest 0x%08x for"
1450 " %u bytes of Data Out\n", checksum,
1451 payload_length);
1452 }
1453 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001454
1455 return data_crc_failed;
1456}
1457
1458int
1459iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1460 bool data_crc_failed)
1461{
1462 struct iscsi_conn *conn = cmd->conn;
1463 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001464 /*
1465 * Increment post receive data and CRC values or perform
1466 * within-command recovery.
1467 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001468 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1469 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001470 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001471 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001472 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001473 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1474 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001475 /*
1476 * Handle extra special case for out of order
1477 * Unsolicited Data Out.
1478 */
1479 spin_lock_bh(&cmd->istate_lock);
1480 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1481 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1482 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1483 spin_unlock_bh(&cmd->istate_lock);
1484
1485 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001486 if (ooo_cmdsn)
1487 return 0;
1488 target_execute_cmd(&cmd->se_cmd);
1489 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001490 } else /* DATAOUT_CANNOT_RECOVER */
1491 return -1;
1492
1493 return 0;
1494}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001495EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001496
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001497static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1498{
Nicholas Bellingerdbcbc952013-11-06 20:55:39 -08001499 struct iscsi_cmd *cmd = NULL;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001500 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1501 int rc;
1502 bool data_crc_failed = false;
1503
1504 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1505 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001506 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001507 else if (!cmd)
1508 return 0;
1509
1510 rc = iscsit_get_dataout(conn, cmd, hdr);
1511 if (rc < 0)
1512 return rc;
1513 else if (rc > 0)
1514 data_crc_failed = true;
1515
1516 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1517}
1518
Nicholas Bellinger778de362013-06-14 16:07:47 -07001519int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1520 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001521{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001522 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001523
Arshad Hussaina3662602014-03-14 15:28:59 -07001524 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1525 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1526 if (!cmd)
1527 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1528 (unsigned char *)hdr);
1529
1530 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1531 (unsigned char *)hdr);
1532 }
1533
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001534 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001535 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1536 " not set, protocol error.\n");
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001537 if (!cmd)
1538 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1539 (unsigned char *)hdr);
1540
Nicholas Bellingerba159912013-07-03 03:48:24 -07001541 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1542 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001543 }
1544
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001545 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001546 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001547 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001548 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001549 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellinger28aaa952013-08-23 22:28:56 -07001550 if (!cmd)
1551 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1552 (unsigned char *)hdr);
1553
Nicholas Bellingerba159912013-07-03 03:48:24 -07001554 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1555 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001556 }
1557
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001558 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001559 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001560 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001561 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1562 payload_length);
1563 /*
1564 * This is not a response to a Unsolicited NopIN, which means
1565 * it can either be a NOPOUT ping request (with a valid ITT),
1566 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1567 * Either way, make sure we allocate an struct iscsi_cmd, as both
1568 * can contain ping data.
1569 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001570 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001571 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1572 cmd->i_state = ISTATE_SEND_NOPIN;
1573 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1574 1 : 0);
1575 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1576 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001577 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1578 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001579 cmd->data_direction = DMA_NONE;
1580 }
1581
Nicholas Bellinger778de362013-06-14 16:07:47 -07001582 return 0;
1583}
1584EXPORT_SYMBOL(iscsit_setup_nop_out);
1585
1586int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1587 struct iscsi_nopout *hdr)
1588{
1589 struct iscsi_cmd *cmd_p = NULL;
1590 int cmdsn_ret = 0;
1591 /*
1592 * Initiator is expecting a NopIN ping reply..
1593 */
1594 if (hdr->itt != RESERVED_ITT) {
Nicholas Bellinger7cbfcc92014-05-01 13:44:56 -07001595 if (!cmd)
1596 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1597 (unsigned char *)hdr);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001598
1599 spin_lock_bh(&conn->cmd_lock);
1600 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1601 spin_unlock_bh(&conn->cmd_lock);
1602
1603 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1604
1605 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1606 iscsit_add_cmd_to_response_queue(cmd, conn,
1607 cmd->i_state);
1608 return 0;
1609 }
1610
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001611 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1612 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001613 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1614 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001615 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001616 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001617
1618 return 0;
1619 }
1620 /*
1621 * This was a response to a unsolicited NOPIN ping.
1622 */
1623 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1624 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1625 if (!cmd_p)
1626 return -EINVAL;
1627
1628 iscsit_stop_nopin_response_timer(conn);
1629
1630 cmd_p->i_state = ISTATE_REMOVE;
1631 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1632
1633 iscsit_start_nopin_timer(conn);
1634 return 0;
1635 }
1636 /*
1637 * Otherwise, initiator is not expecting a NOPIN is response.
1638 * Just ignore for now.
1639 */
1640 return 0;
1641}
1642EXPORT_SYMBOL(iscsit_process_nop_out);
1643
1644static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1645 unsigned char *buf)
1646{
1647 unsigned char *ping_data = NULL;
1648 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1649 struct kvec *iov = NULL;
1650 u32 payload_length = ntoh24(hdr->dlength);
1651 int ret;
1652
1653 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1654 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001655 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001656 /*
1657 * Handle NOP-OUT payload for traditional iSCSI sockets
1658 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001659 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001660 u32 checksum, data_crc, padding = 0;
1661 int niov = 0, rx_got, rx_size = payload_length;
1662
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001663 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1664 if (!ping_data) {
1665 pr_err("Unable to allocate memory for"
1666 " NOPOUT ping data.\n");
1667 ret = -1;
1668 goto out;
1669 }
1670
1671 iov = &cmd->iov_misc[0];
1672 iov[niov].iov_base = ping_data;
1673 iov[niov++].iov_len = payload_length;
1674
1675 padding = ((-payload_length) & 3);
1676 if (padding != 0) {
1677 pr_debug("Receiving %u additional bytes"
1678 " for padding.\n", padding);
1679 iov[niov].iov_base = &cmd->pad_bytes;
1680 iov[niov++].iov_len = padding;
1681 rx_size += padding;
1682 }
1683 if (conn->conn_ops->DataDigest) {
1684 iov[niov].iov_base = &checksum;
1685 iov[niov++].iov_len = ISCSI_CRC_LEN;
1686 rx_size += ISCSI_CRC_LEN;
1687 }
1688
1689 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1690 if (rx_got != rx_size) {
1691 ret = -1;
1692 goto out;
1693 }
1694
1695 if (conn->conn_ops->DataDigest) {
1696 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1697 ping_data, payload_length,
1698 padding, cmd->pad_bytes,
1699 (u8 *)&data_crc);
1700
1701 if (checksum != data_crc) {
1702 pr_err("Ping data CRC32C DataDigest"
1703 " 0x%08x does not match computed 0x%08x\n",
1704 checksum, data_crc);
1705 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1706 pr_err("Unable to recover from"
1707 " NOPOUT Ping DataCRC failure while in"
1708 " ERL=0.\n");
1709 ret = -1;
1710 goto out;
1711 } else {
1712 /*
1713 * Silently drop this PDU and let the
1714 * initiator plug the CmdSN gap.
1715 */
1716 pr_debug("Dropping NOPOUT"
1717 " Command CmdSN: 0x%08x due to"
1718 " DataCRC error.\n", hdr->cmdsn);
1719 ret = 0;
1720 goto out;
1721 }
1722 } else {
1723 pr_debug("Got CRC32C DataDigest"
1724 " 0x%08x for %u bytes of ping data.\n",
1725 checksum, payload_length);
1726 }
1727 }
1728
1729 ping_data[payload_length] = '\0';
1730 /*
1731 * Attach ping data to struct iscsi_cmd->buf_ptr.
1732 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001733 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001734 cmd->buf_ptr_size = payload_length;
1735
1736 pr_debug("Got %u bytes of NOPOUT ping"
1737 " data.\n", payload_length);
1738 pr_debug("Ping Data: \"%s\"\n", ping_data);
1739 }
1740
Nicholas Bellinger778de362013-06-14 16:07:47 -07001741 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001742out:
1743 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001744 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001745
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001746 kfree(ping_data);
1747 return ret;
1748}
1749
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001750int
1751iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1752 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001753{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754 struct se_tmr_req *se_tmr;
1755 struct iscsi_tmr_req *tmr_req;
1756 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001757 int out_of_order_cmdsn = 0, ret;
1758 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001759 u8 function;
1760
1761 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001762 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1763 function = hdr->flags;
1764
1765 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1766 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1767 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1768 hdr->rtt, hdr->refcmdsn, conn->cid);
1769
1770 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1771 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001772 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001773 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001774 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001775 }
1776
1777 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1778 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1779 pr_err("Task Management Request TASK_REASSIGN not"
1780 " issued as immediate command, bad iSCSI Initiator"
1781 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001782 return iscsit_add_reject_cmd(cmd,
1783 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001784 }
1785 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001786 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1787 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001788
Andy Groverd28b11692012-04-03 15:51:22 -07001789 cmd->data_direction = DMA_NONE;
1790
1791 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1792 if (!cmd->tmr_req) {
1793 pr_err("Unable to allocate memory for"
1794 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001795 return iscsit_add_reject_cmd(cmd,
1796 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1797 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001798 }
1799
1800 /*
1801 * TASK_REASSIGN for ERL=2 / connection stays inside of
1802 * LIO-Target $FABRIC_MOD
1803 */
1804 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1805
1806 u8 tcm_function;
1807 int ret;
1808
Christoph Hellwig9ac89282015-04-08 20:01:35 +02001809 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
Andy Groverd28b11692012-04-03 15:51:22 -07001810 conn->sess->se_sess, 0, DMA_NONE,
Christoph Hellwig68d81f42014-11-24 07:07:25 -08001811 TCM_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001812
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001813 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1814 sess_ref = true;
1815
Andy Groverd28b11692012-04-03 15:51:22 -07001816 switch (function) {
1817 case ISCSI_TM_FUNC_ABORT_TASK:
1818 tcm_function = TMR_ABORT_TASK;
1819 break;
1820 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1821 tcm_function = TMR_ABORT_TASK_SET;
1822 break;
1823 case ISCSI_TM_FUNC_CLEAR_ACA:
1824 tcm_function = TMR_CLEAR_ACA;
1825 break;
1826 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1827 tcm_function = TMR_CLEAR_TASK_SET;
1828 break;
1829 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1830 tcm_function = TMR_LUN_RESET;
1831 break;
1832 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1833 tcm_function = TMR_TARGET_WARM_RESET;
1834 break;
1835 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1836 tcm_function = TMR_TARGET_COLD_RESET;
1837 break;
1838 default:
1839 pr_err("Unknown iSCSI TMR Function:"
1840 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001841 return iscsit_add_reject_cmd(cmd,
1842 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001843 }
1844
1845 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1846 tcm_function, GFP_KERNEL);
1847 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001848 return iscsit_add_reject_cmd(cmd,
1849 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001850
1851 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1852 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001853
1854 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1855 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1856 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1857 cmd->init_task_tag = hdr->itt;
1858 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001859 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1860 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001861 se_tmr = cmd->se_cmd.se_tmr_req;
1862 tmr_req = cmd->tmr_req;
1863 /*
1864 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1865 */
1866 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001867 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1868 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001869 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001870 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1871 goto attach;
1872 }
1873 }
1874
1875 switch (function) {
1876 case ISCSI_TM_FUNC_ABORT_TASK:
1877 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001878 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001879 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001880 break;
1881 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1882 case ISCSI_TM_FUNC_CLEAR_ACA:
1883 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1884 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1885 break;
1886 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1887 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001888 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1889 goto attach;
1890 }
1891 break;
1892 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1893 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001894 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1895 goto attach;
1896 }
1897 break;
1898 case ISCSI_TM_FUNC_TASK_REASSIGN:
1899 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1900 /*
1901 * Perform sanity checks on the ExpDataSN only if the
1902 * TASK_REASSIGN was successful.
1903 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001904 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 break;
1906
1907 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001908 return iscsit_add_reject_cmd(cmd,
1909 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001910 break;
1911 default:
1912 pr_err("Unknown TMR function: 0x%02x, protocol"
1913 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001914 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1915 goto attach;
1916 }
1917
1918 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1919 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1920 se_tmr->call_transport = 1;
1921attach:
1922 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001923 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001924 spin_unlock_bh(&conn->cmd_lock);
1925
1926 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001927 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001928 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1929 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001930 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001931 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001932 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001933 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001934 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001935 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001936
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001937 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938 return 0;
1939 /*
1940 * Found the referenced task, send to transport for processing.
1941 */
1942 if (se_tmr->call_transport)
1943 return transport_generic_handle_tmr(&cmd->se_cmd);
1944
1945 /*
1946 * Could not find the referenced LUN, task, or Task Management
1947 * command not authorized or supported. Change state and
1948 * let the tx_thread send the response.
1949 *
1950 * For connection recovery, this is also the default action for
1951 * TMR TASK_REASSIGN.
1952 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001953 if (sess_ref) {
1954 pr_debug("Handle TMR, using sess_ref=true check\n");
1955 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1956 }
1957
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001958 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1959 return 0;
1960}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001961EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001962
1963/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001964int
1965iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1966 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001967{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001968 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001969
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001970 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001971 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001972 "greater than MaxXmitDataSegmentLength %u.\n",
1973 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001974 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1975 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001976 }
1977
Nicholas Bellinger122f8af2013-11-13 14:33:24 -08001978 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
1979 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
1980 pr_err("Multi sequence text commands currently not supported\n");
1981 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
1982 (unsigned char *)hdr);
1983 }
1984
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001985 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1986 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1987 hdr->exp_statsn, payload_length);
1988
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001989 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1990 cmd->i_state = ISTATE_SEND_TEXTRSP;
1991 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1992 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1993 cmd->targ_xfer_tag = 0xFFFFFFFF;
1994 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1995 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1996 cmd->data_direction = DMA_NONE;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02001997 cmd->text_in_ptr = NULL;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001998
1999 return 0;
2000}
2001EXPORT_SYMBOL(iscsit_setup_text_cmd);
2002
2003int
2004iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2005 struct iscsi_text *hdr)
2006{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002007 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002008 int cmdsn_ret;
2009
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002010 if (!text_in) {
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002011 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2012 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2013 pr_err("Unable to locate text_in buffer for sendtargets"
2014 " discovery\n");
2015 goto reject;
2016 }
2017 goto empty_sendtargets;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002018 }
2019 if (strncmp("SendTargets", text_in, 11) != 0) {
2020 pr_err("Received Text Data that is not"
2021 " SendTargets, cannot continue.\n");
2022 goto reject;
2023 }
2024 text_ptr = strchr(text_in, '=');
2025 if (!text_ptr) {
2026 pr_err("No \"=\" separator found in Text Data,"
2027 " cannot continue.\n");
2028 goto reject;
2029 }
2030 if (!strncmp("=All", text_ptr, 4)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002031 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07002032 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2033 !strncmp("=eui.", text_ptr, 5)) {
Andy Grover8060b8d2015-01-09 15:13:08 -08002034 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002035 } else {
2036 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
2037 goto reject;
2038 }
2039
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002040 spin_lock_bh(&conn->cmd_lock);
2041 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2042 spin_unlock_bh(&conn->cmd_lock);
2043
Sagi Grimberge4f4e802015-02-09 18:07:25 +02002044empty_sendtargets:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002045 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2046
2047 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002048 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2049 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002050 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002051 return -1;
2052
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002053 return 0;
2054 }
2055
2056 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002057
2058reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002059 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2060 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002061}
2062EXPORT_SYMBOL(iscsit_process_text_cmd);
2063
2064static int
2065iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2066 unsigned char *buf)
2067{
2068 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2069 char *text_in = NULL;
2070 u32 payload_length = ntoh24(hdr->dlength);
2071 int rx_size, rc;
2072
2073 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2074 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002075 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002076
2077 rx_size = payload_length;
2078 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002079 u32 checksum = 0, data_crc = 0;
2080 u32 padding = 0, pad_bytes = 0;
2081 int niov = 0, rx_got;
2082 struct kvec iov[3];
2083
2084 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002085 if (!text_in) {
2086 pr_err("Unable to allocate memory for"
2087 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002088 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002089 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002090 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002091
2092 memset(iov, 0, 3 * sizeof(struct kvec));
2093 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002094 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002095
2096 padding = ((-payload_length) & 3);
2097 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002098 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002099 iov[niov++].iov_len = padding;
2100 rx_size += padding;
2101 pr_debug("Receiving %u additional bytes"
2102 " for padding.\n", padding);
2103 }
2104 if (conn->conn_ops->DataDigest) {
2105 iov[niov].iov_base = &checksum;
2106 iov[niov++].iov_len = ISCSI_CRC_LEN;
2107 rx_size += ISCSI_CRC_LEN;
2108 }
2109
2110 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111 if (rx_got != rx_size)
2112 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002113
2114 if (conn->conn_ops->DataDigest) {
2115 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002117 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002118 (u8 *)&data_crc);
2119
2120 if (checksum != data_crc) {
2121 pr_err("Text data CRC32C DataDigest"
2122 " 0x%08x does not match computed"
2123 " 0x%08x\n", checksum, data_crc);
2124 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2125 pr_err("Unable to recover from"
2126 " Text Data digest failure while in"
2127 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002128 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002129 } else {
2130 /*
2131 * Silently drop this PDU and let the
2132 * initiator plug the CmdSN gap.
2133 */
2134 pr_debug("Dropping Text"
2135 " Command CmdSN: 0x%08x due to"
2136 " DataCRC error.\n", hdr->cmdsn);
2137 kfree(text_in);
2138 return 0;
2139 }
2140 } else {
2141 pr_debug("Got CRC32C DataDigest"
2142 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002143 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002144 }
2145 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002146 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002147 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002148 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002149 }
2150
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002151 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002152
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002153reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002154 kfree(cmd->text_in_ptr);
2155 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002156 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002157}
2158
2159int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2160{
2161 struct iscsi_conn *conn_p;
2162 struct iscsi_session *sess = conn->sess;
2163
2164 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2165 " for SID: %u.\n", conn->cid, conn->sess->sid);
2166
2167 atomic_set(&sess->session_logout, 1);
2168 atomic_set(&conn->conn_logout_remove, 1);
2169 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2170
2171 iscsit_inc_conn_usage_count(conn);
2172 iscsit_inc_session_usage_count(sess);
2173
2174 spin_lock_bh(&sess->conn_lock);
2175 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2176 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2177 continue;
2178
2179 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2180 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2181 }
2182 spin_unlock_bh(&sess->conn_lock);
2183
2184 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2185
2186 return 0;
2187}
2188
2189int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2190{
2191 struct iscsi_conn *l_conn;
2192 struct iscsi_session *sess = conn->sess;
2193
2194 pr_debug("Received logout request CLOSECONNECTION for CID:"
2195 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2196
2197 /*
2198 * A Logout Request with a CLOSECONNECTION reason code for a CID
2199 * can arrive on a connection with a differing CID.
2200 */
2201 if (conn->cid == cmd->logout_cid) {
2202 spin_lock_bh(&conn->state_lock);
2203 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2204 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2205
2206 atomic_set(&conn->conn_logout_remove, 1);
2207 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2208 iscsit_inc_conn_usage_count(conn);
2209
2210 spin_unlock_bh(&conn->state_lock);
2211 } else {
2212 /*
2213 * Handle all different cid CLOSECONNECTION requests in
2214 * iscsit_logout_post_handler_diffcid() as to give enough
2215 * time for any non immediate command's CmdSN to be
2216 * acknowledged on the connection in question.
2217 *
2218 * Here we simply make sure the CID is still around.
2219 */
2220 l_conn = iscsit_get_conn_from_cid(sess,
2221 cmd->logout_cid);
2222 if (!l_conn) {
2223 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2224 iscsit_add_cmd_to_response_queue(cmd, conn,
2225 cmd->i_state);
2226 return 0;
2227 }
2228
2229 iscsit_dec_conn_usage_count(l_conn);
2230 }
2231
2232 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2233
2234 return 0;
2235}
2236
2237int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2238{
2239 struct iscsi_session *sess = conn->sess;
2240
2241 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2242 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2243
2244 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2245 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2246 " while ERL!=2.\n");
2247 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2248 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2249 return 0;
2250 }
2251
2252 if (conn->cid == cmd->logout_cid) {
2253 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2254 " with CID: %hu on CID: %hu, implementation error.\n",
2255 cmd->logout_cid, conn->cid);
2256 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2257 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2258 return 0;
2259 }
2260
2261 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2262
2263 return 0;
2264}
2265
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002266int
2267iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2268 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002269{
2270 int cmdsn_ret, logout_remove = 0;
2271 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002272 struct iscsi_logout *hdr;
2273 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2274
2275 hdr = (struct iscsi_logout *) buf;
2276 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002277
2278 if (tiqn) {
2279 spin_lock(&tiqn->logout_stats.lock);
2280 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2281 tiqn->logout_stats.normal_logouts++;
2282 else
2283 tiqn->logout_stats.abnormal_logouts++;
2284 spin_unlock(&tiqn->logout_stats.lock);
2285 }
2286
2287 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2288 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2289 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2290 hdr->cid, conn->cid);
2291
2292 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2293 pr_err("Received logout request on connection that"
2294 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002295 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002296 return 0;
2297 }
2298
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002299 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2300 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2301 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2302 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2303 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002304 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2305 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2306 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002307 cmd->logout_reason = reason_code;
2308 cmd->data_direction = DMA_NONE;
2309
2310 /*
2311 * We need to sleep in these cases (by returning 1) until the Logout
2312 * Response gets sent in the tx thread.
2313 */
2314 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2315 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002316 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002317 logout_remove = 1;
2318
2319 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002320 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002321 spin_unlock_bh(&conn->cmd_lock);
2322
2323 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002324 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002325
2326 /*
2327 * Immediate commands are executed, well, immediately.
2328 * Non-Immediate Logout Commands are executed in CmdSN order.
2329 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002330 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002331 int ret = iscsit_execute_cmd(cmd, 0);
2332
2333 if (ret < 0)
2334 return ret;
2335 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002336 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002337 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002338 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002339 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2340 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002341 }
2342
2343 return logout_remove;
2344}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002345EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002346
2347static int iscsit_handle_snack(
2348 struct iscsi_conn *conn,
2349 unsigned char *buf)
2350{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002351 struct iscsi_snack *hdr;
2352
2353 hdr = (struct iscsi_snack *) buf;
2354 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355
2356 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2357 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2358 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2359 hdr->begrun, hdr->runlength, conn->cid);
2360
2361 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2362 pr_err("Initiator sent SNACK request while in"
2363 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002364 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2365 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002366 }
2367 /*
2368 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2369 * call from inside iscsi_send_recovery_datain_or_r2t().
2370 */
2371 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2372 case 0:
2373 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002374 hdr->itt,
2375 be32_to_cpu(hdr->ttt),
2376 be32_to_cpu(hdr->begrun),
2377 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002378 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002379 return iscsit_handle_status_snack(conn, hdr->itt,
2380 be32_to_cpu(hdr->ttt),
2381 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002382 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002383 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2384 be32_to_cpu(hdr->begrun),
2385 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002386 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2387 /* FIXME: Support R-Data SNACK */
2388 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002389 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2390 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002391 default:
2392 pr_err("Unknown SNACK type 0x%02x, protocol"
2393 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002394 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2395 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002396 }
2397
2398 return 0;
2399}
2400
2401static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2402{
2403 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2404 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2405 wait_for_completion_interruptible_timeout(
2406 &conn->rx_half_close_comp,
2407 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2408 }
2409}
2410
2411static int iscsit_handle_immediate_data(
2412 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002413 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002414 u32 length)
2415{
2416 int iov_ret, rx_got = 0, rx_size = 0;
2417 u32 checksum, iov_count = 0, padding = 0;
2418 struct iscsi_conn *conn = cmd->conn;
2419 struct kvec *iov;
2420
2421 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2422 if (iov_ret < 0)
2423 return IMMEDIATE_DATA_CANNOT_RECOVER;
2424
2425 rx_size = length;
2426 iov_count = iov_ret;
2427 iov = &cmd->iov_data[0];
2428
2429 padding = ((-length) & 3);
2430 if (padding != 0) {
2431 iov[iov_count].iov_base = cmd->pad_bytes;
2432 iov[iov_count++].iov_len = padding;
2433 rx_size += padding;
2434 }
2435
2436 if (conn->conn_ops->DataDigest) {
2437 iov[iov_count].iov_base = &checksum;
2438 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2439 rx_size += ISCSI_CRC_LEN;
2440 }
2441
2442 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2443
2444 iscsit_unmap_iovec(cmd);
2445
2446 if (rx_got != rx_size) {
2447 iscsit_rx_thread_wait_for_tcp(conn);
2448 return IMMEDIATE_DATA_CANNOT_RECOVER;
2449 }
2450
2451 if (conn->conn_ops->DataDigest) {
2452 u32 data_crc;
2453
2454 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2455 cmd->write_data_done, length, padding,
2456 cmd->pad_bytes);
2457
2458 if (checksum != data_crc) {
2459 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2460 " does not match computed 0x%08x\n", checksum,
2461 data_crc);
2462
2463 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2464 pr_err("Unable to recover from"
2465 " Immediate Data digest failure while"
2466 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002467 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002468 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002469 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002470 return IMMEDIATE_DATA_CANNOT_RECOVER;
2471 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002472 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002473 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002474 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002475 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2476 }
2477 } else {
2478 pr_debug("Got CRC32C DataDigest 0x%08x for"
2479 " %u bytes of Immediate Data\n", checksum,
2480 length);
2481 }
2482 }
2483
2484 cmd->write_data_done += length;
2485
Andy Groverebf1d952012-04-03 15:51:24 -07002486 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002487 spin_lock_bh(&cmd->istate_lock);
2488 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2489 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2490 spin_unlock_bh(&cmd->istate_lock);
2491 }
2492
2493 return IMMEDIATE_DATA_NORMAL_OPERATION;
2494}
2495
2496/*
2497 * Called with sess->conn_lock held.
2498 */
2499/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2500 with active network interface */
2501static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2502{
2503 struct iscsi_cmd *cmd;
2504 struct iscsi_conn *conn_p;
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002505 bool found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002506
2507 /*
2508 * Only send a Asynchronous Message on connections whos network
2509 * interface is still functional.
2510 */
2511 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2512 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2513 iscsit_inc_conn_usage_count(conn_p);
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002514 found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002515 break;
2516 }
2517 }
2518
Nicholas Bellingerd444edc2014-02-19 23:32:14 +00002519 if (!found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002520 return;
2521
Nicholas Bellinger676687c2014-01-20 03:36:44 +00002522 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002523 if (!cmd) {
2524 iscsit_dec_conn_usage_count(conn_p);
2525 return;
2526 }
2527
2528 cmd->logout_cid = conn->cid;
2529 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2530 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2531
2532 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002533 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002534 spin_unlock_bh(&conn_p->cmd_lock);
2535
2536 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2537 iscsit_dec_conn_usage_count(conn_p);
2538}
2539
2540static int iscsit_send_conn_drop_async_message(
2541 struct iscsi_cmd *cmd,
2542 struct iscsi_conn *conn)
2543{
2544 struct iscsi_async *hdr;
2545
2546 cmd->tx_size = ISCSI_HDR_LEN;
2547 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2548
2549 hdr = (struct iscsi_async *) cmd->pdu;
2550 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2551 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002552 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002553 cmd->targ_xfer_tag = 0xFFFFFFFF;
2554 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2555 cmd->stat_sn = conn->stat_sn++;
2556 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2557 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2558 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2559 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2560 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2561 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2562 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2563
2564 if (conn->conn_ops->HeaderDigest) {
2565 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2566
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002567 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2568 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002569
2570 cmd->tx_size += ISCSI_CRC_LEN;
2571 pr_debug("Attaching CRC32C HeaderDigest to"
2572 " Async Message 0x%08x\n", *header_digest);
2573 }
2574
2575 cmd->iov_misc[0].iov_base = cmd->pdu;
2576 cmd->iov_misc[0].iov_len = cmd->tx_size;
2577 cmd->iov_misc_count = 1;
2578
2579 pr_debug("Sending Connection Dropped Async Message StatSN:"
2580 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2581 cmd->logout_cid, conn->cid);
2582 return 0;
2583}
2584
Andy Grover6f3c0e62012-04-03 15:51:09 -07002585static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2586{
2587 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2588 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2589 wait_for_completion_interruptible_timeout(
2590 &conn->tx_half_close_comp,
2591 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2592 }
2593}
2594
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002595static void
2596iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2597 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2598 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002599{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002600 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2601 hdr->flags = datain->flags;
2602 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2603 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2604 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2605 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2606 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2607 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2608 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2609 }
2610 }
2611 hton24(hdr->dlength, datain->length);
2612 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2613 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2614 (struct scsi_lun *)&hdr->lun);
2615 else
2616 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2617
2618 hdr->itt = cmd->init_task_tag;
2619
2620 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2621 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2622 else
2623 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2624 if (set_statsn)
2625 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2626 else
2627 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2628
2629 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2630 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2631 hdr->datasn = cpu_to_be32(datain->data_sn);
2632 hdr->offset = cpu_to_be32(datain->offset);
2633
2634 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2635 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2636 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2637 ntohl(hdr->offset), datain->length, conn->cid);
2638}
2639
2640static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2641{
2642 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002643 struct iscsi_datain datain;
2644 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002645 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002646 u32 iov_count = 0, tx_size = 0;
2647 int eodr = 0, ret, iov_ret;
2648 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649
2650 memset(&datain, 0, sizeof(struct iscsi_datain));
2651 dr = iscsit_get_datain_values(cmd, &datain);
2652 if (!dr) {
2653 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2654 cmd->init_task_tag);
2655 return -1;
2656 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002657 /*
2658 * Be paranoid and double check the logic for now.
2659 */
Andy Groverebf1d952012-04-03 15:51:24 -07002660 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002661 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2662 " datain.length: %u exceeds cmd->data_length: %u\n",
2663 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002664 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002665 return -1;
2666 }
2667
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08002668 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002669 /*
2670 * Special case for successfully execution w/ both DATAIN
2671 * and Sense Data.
2672 */
2673 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2674 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2675 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2676 else {
2677 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2678 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2679 iscsit_increment_maxcmdsn(cmd, conn->sess);
2680 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002681 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002682 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002683 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2684 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002685 }
2686
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002687 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002688
2689 iov = &cmd->iov_data[0];
2690 iov[iov_count].iov_base = cmd->pdu;
2691 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2692 tx_size += ISCSI_HDR_LEN;
2693
2694 if (conn->conn_ops->HeaderDigest) {
2695 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2696
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002697 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2698 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002699
2700 iov[0].iov_len += ISCSI_CRC_LEN;
2701 tx_size += ISCSI_CRC_LEN;
2702
2703 pr_debug("Attaching CRC32 HeaderDigest"
2704 " for DataIN PDU 0x%08x\n", *header_digest);
2705 }
2706
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002707 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2708 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002709 if (iov_ret < 0)
2710 return -1;
2711
2712 iov_count += iov_ret;
2713 tx_size += datain.length;
2714
2715 cmd->padding = ((-datain.length) & 3);
2716 if (cmd->padding) {
2717 iov[iov_count].iov_base = cmd->pad_bytes;
2718 iov[iov_count++].iov_len = cmd->padding;
2719 tx_size += cmd->padding;
2720
2721 pr_debug("Attaching %u padding bytes\n",
2722 cmd->padding);
2723 }
2724 if (conn->conn_ops->DataDigest) {
2725 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2726 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2727
2728 iov[iov_count].iov_base = &cmd->data_crc;
2729 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2730 tx_size += ISCSI_CRC_LEN;
2731
2732 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2733 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2734 }
2735
2736 cmd->iov_data_count = iov_count;
2737 cmd->tx_size = tx_size;
2738
Andy Grover6f3c0e62012-04-03 15:51:09 -07002739 /* sendpage is preferred but can't insert markers */
2740 if (!conn->conn_ops->IFMarker)
2741 ret = iscsit_fe_sendpage_sg(cmd, conn);
2742 else
2743 ret = iscsit_send_tx_data(cmd, conn, 0);
2744
2745 iscsit_unmap_iovec(cmd);
2746
2747 if (ret < 0) {
2748 iscsit_tx_thread_wait_for_tcp(conn);
2749 return ret;
2750 }
2751
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002752 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002753 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002754 2 : 1;
2755 iscsit_free_datain_req(cmd, dr);
2756 }
2757
Andy Grover6f3c0e62012-04-03 15:51:09 -07002758 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002759}
2760
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002761int
2762iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2763 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002764{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002765 struct iscsi_conn *logout_conn = NULL;
2766 struct iscsi_conn_recovery *cr = NULL;
2767 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002768 /*
2769 * The actual shutting down of Sessions and/or Connections
2770 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2771 * is done in scsi_logout_post_handler().
2772 */
2773 switch (cmd->logout_reason) {
2774 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2775 pr_debug("iSCSI session logout successful, setting"
2776 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2777 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2778 break;
2779 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2780 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2781 break;
2782 /*
2783 * For CLOSECONNECTION logout requests carrying
2784 * a matching logout CID -> local CID, the reference
2785 * for the local CID will have been incremented in
2786 * iscsi_logout_closeconnection().
2787 *
2788 * For CLOSECONNECTION logout requests carrying
2789 * a different CID than the connection it arrived
2790 * on, the connection responding to cmd->logout_cid
2791 * is stopped in iscsit_logout_post_handler_diffcid().
2792 */
2793
2794 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2795 " successful.\n", cmd->logout_cid, conn->cid);
2796 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2797 break;
2798 case ISCSI_LOGOUT_REASON_RECOVERY:
2799 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2800 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2801 break;
2802 /*
2803 * If the connection is still active from our point of view
2804 * force connection recovery to occur.
2805 */
2806 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2807 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002808 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002809 iscsit_connection_reinstatement_rcfr(logout_conn);
2810 iscsit_dec_conn_usage_count(logout_conn);
2811 }
2812
2813 cr = iscsit_get_inactive_connection_recovery_entry(
2814 conn->sess, cmd->logout_cid);
2815 if (!cr) {
2816 pr_err("Unable to locate CID: %hu for"
2817 " REMOVECONNFORRECOVERY Logout Request.\n",
2818 cmd->logout_cid);
2819 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2820 break;
2821 }
2822
2823 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2824
2825 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2826 " for recovery for CID: %hu on CID: %hu successful.\n",
2827 cmd->logout_cid, conn->cid);
2828 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2829 break;
2830 default:
2831 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2832 cmd->logout_reason);
2833 return -1;
2834 }
2835
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002836 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2837 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2838 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002839 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002840 cmd->stat_sn = conn->stat_sn++;
2841 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2842
2843 iscsit_increment_maxcmdsn(cmd, conn->sess);
2844 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2845 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2846
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002847 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2848 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2849 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2850 cmd->logout_cid, conn->cid);
2851
2852 return 0;
2853}
2854EXPORT_SYMBOL(iscsit_build_logout_rsp);
2855
2856static int
2857iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2858{
2859 struct kvec *iov;
2860 int niov = 0, tx_size, rc;
2861
2862 rc = iscsit_build_logout_rsp(cmd, conn,
2863 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2864 if (rc < 0)
2865 return rc;
2866
2867 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002868 iov = &cmd->iov_misc[0];
2869 iov[niov].iov_base = cmd->pdu;
2870 iov[niov++].iov_len = ISCSI_HDR_LEN;
2871
2872 if (conn->conn_ops->HeaderDigest) {
2873 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2874
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002875 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2876 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002877
2878 iov[0].iov_len += ISCSI_CRC_LEN;
2879 tx_size += ISCSI_CRC_LEN;
2880 pr_debug("Attaching CRC32C HeaderDigest to"
2881 " Logout Response 0x%08x\n", *header_digest);
2882 }
2883 cmd->iov_misc_count = niov;
2884 cmd->tx_size = tx_size;
2885
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002886 return 0;
2887}
2888
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002889void
2890iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2891 struct iscsi_nopin *hdr, bool nopout_response)
2892{
2893 hdr->opcode = ISCSI_OP_NOOP_IN;
2894 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2895 hton24(hdr->dlength, cmd->buf_ptr_size);
2896 if (nopout_response)
2897 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2898 hdr->itt = cmd->init_task_tag;
2899 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2900 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2901 conn->stat_sn;
2902 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2903
2904 if (nopout_response)
2905 iscsit_increment_maxcmdsn(cmd, conn->sess);
2906
2907 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2908 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2909
2910 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2911 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2912 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2913 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2914}
2915EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2916
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002917/*
2918 * Unsolicited NOPIN, either requesting a response or not.
2919 */
2920static int iscsit_send_unsolicited_nopin(
2921 struct iscsi_cmd *cmd,
2922 struct iscsi_conn *conn,
2923 int want_response)
2924{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002925 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2926 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002927
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002928 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002929
2930 if (conn->conn_ops->HeaderDigest) {
2931 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2932
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002933 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2934 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002935
2936 tx_size += ISCSI_CRC_LEN;
2937 pr_debug("Attaching CRC32C HeaderDigest to"
2938 " NopIN 0x%08x\n", *header_digest);
2939 }
2940
2941 cmd->iov_misc[0].iov_base = cmd->pdu;
2942 cmd->iov_misc[0].iov_len = tx_size;
2943 cmd->iov_misc_count = 1;
2944 cmd->tx_size = tx_size;
2945
2946 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2947 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2948
Andy Grover6f3c0e62012-04-03 15:51:09 -07002949 ret = iscsit_send_tx_data(cmd, conn, 1);
2950 if (ret < 0) {
2951 iscsit_tx_thread_wait_for_tcp(conn);
2952 return ret;
2953 }
2954
2955 spin_lock_bh(&cmd->istate_lock);
2956 cmd->i_state = want_response ?
2957 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2958 spin_unlock_bh(&cmd->istate_lock);
2959
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002960 return 0;
2961}
2962
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002963static int
2964iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002965{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002966 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002967 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002968 u32 padding = 0;
2969 int niov = 0, tx_size;
2970
2971 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002972
2973 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002974 iov = &cmd->iov_misc[0];
2975 iov[niov].iov_base = cmd->pdu;
2976 iov[niov++].iov_len = ISCSI_HDR_LEN;
2977
2978 if (conn->conn_ops->HeaderDigest) {
2979 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2980
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002981 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2982 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002983
2984 iov[0].iov_len += ISCSI_CRC_LEN;
2985 tx_size += ISCSI_CRC_LEN;
2986 pr_debug("Attaching CRC32C HeaderDigest"
2987 " to NopIn 0x%08x\n", *header_digest);
2988 }
2989
2990 /*
2991 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2992 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2993 */
2994 if (cmd->buf_ptr_size) {
2995 iov[niov].iov_base = cmd->buf_ptr;
2996 iov[niov++].iov_len = cmd->buf_ptr_size;
2997 tx_size += cmd->buf_ptr_size;
2998
2999 pr_debug("Echoing back %u bytes of ping"
3000 " data.\n", cmd->buf_ptr_size);
3001
3002 padding = ((-cmd->buf_ptr_size) & 3);
3003 if (padding != 0) {
3004 iov[niov].iov_base = &cmd->pad_bytes;
3005 iov[niov++].iov_len = padding;
3006 tx_size += padding;
3007 pr_debug("Attaching %u additional"
3008 " padding bytes.\n", padding);
3009 }
3010 if (conn->conn_ops->DataDigest) {
3011 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
3012 cmd->buf_ptr, cmd->buf_ptr_size,
3013 padding, (u8 *)&cmd->pad_bytes,
3014 (u8 *)&cmd->data_crc);
3015
3016 iov[niov].iov_base = &cmd->data_crc;
3017 iov[niov++].iov_len = ISCSI_CRC_LEN;
3018 tx_size += ISCSI_CRC_LEN;
3019 pr_debug("Attached DataDigest for %u"
3020 " bytes of ping data, CRC 0x%08x\n",
3021 cmd->buf_ptr_size, cmd->data_crc);
3022 }
3023 }
3024
3025 cmd->iov_misc_count = niov;
3026 cmd->tx_size = tx_size;
3027
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003028 return 0;
3029}
3030
Andy Grover6f3c0e62012-04-03 15:51:09 -07003031static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003032 struct iscsi_cmd *cmd,
3033 struct iscsi_conn *conn)
3034{
3035 int tx_size = 0;
3036 struct iscsi_r2t *r2t;
3037 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003038 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003039
3040 r2t = iscsit_get_r2t_from_list(cmd);
3041 if (!r2t)
3042 return -1;
3043
3044 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3045 memset(hdr, 0, ISCSI_HDR_LEN);
3046 hdr->opcode = ISCSI_OP_R2T;
3047 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3048 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3049 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003050 hdr->itt = cmd->init_task_tag;
Sagi Grimbergc1e34b62015-01-26 12:49:05 +02003051 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003052 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3053 hdr->statsn = cpu_to_be32(conn->stat_sn);
3054 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3055 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3056 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3057 hdr->data_offset = cpu_to_be32(r2t->offset);
3058 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3059
3060 cmd->iov_misc[0].iov_base = cmd->pdu;
3061 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3062 tx_size += ISCSI_HDR_LEN;
3063
3064 if (conn->conn_ops->HeaderDigest) {
3065 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3066
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003067 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3068 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003069
3070 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3071 tx_size += ISCSI_CRC_LEN;
3072 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3073 " PDU 0x%08x\n", *header_digest);
3074 }
3075
3076 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3077 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3078 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3079 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3080 r2t->offset, r2t->xfer_len, conn->cid);
3081
3082 cmd->iov_misc_count = 1;
3083 cmd->tx_size = tx_size;
3084
3085 spin_lock_bh(&cmd->r2t_lock);
3086 r2t->sent_r2t = 1;
3087 spin_unlock_bh(&cmd->r2t_lock);
3088
Andy Grover6f3c0e62012-04-03 15:51:09 -07003089 ret = iscsit_send_tx_data(cmd, conn, 1);
3090 if (ret < 0) {
3091 iscsit_tx_thread_wait_for_tcp(conn);
3092 return ret;
3093 }
3094
3095 spin_lock_bh(&cmd->dataout_timeout_lock);
3096 iscsit_start_dataout_timer(cmd, conn);
3097 spin_unlock_bh(&cmd->dataout_timeout_lock);
3098
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003099 return 0;
3100}
3101
3102/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003103 * @recovery: If called from iscsi_task_reassign_complete_write() for
3104 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003105 */
3106int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003108 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003109 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003110{
3111 int first_r2t = 1;
3112 u32 offset = 0, xfer_len = 0;
3113
3114 spin_lock_bh(&cmd->r2t_lock);
3115 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3116 spin_unlock_bh(&cmd->r2t_lock);
3117 return 0;
3118 }
3119
Andy Grover8b1e1242012-04-03 15:51:12 -07003120 if (conn->sess->sess_ops->DataSequenceInOrder &&
3121 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003122 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003123
3124 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3125 if (conn->sess->sess_ops->DataSequenceInOrder) {
3126 offset = cmd->r2t_offset;
3127
Andy Grover8b1e1242012-04-03 15:51:12 -07003128 if (first_r2t && recovery) {
3129 int new_data_end = offset +
3130 conn->sess->sess_ops->MaxBurstLength -
3131 cmd->next_burst_len;
3132
Andy Groverebf1d952012-04-03 15:51:24 -07003133 if (new_data_end > cmd->se_cmd.data_length)
3134 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003135 else
3136 xfer_len =
3137 conn->sess->sess_ops->MaxBurstLength -
3138 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003139 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003140 int new_data_end = offset +
3141 conn->sess->sess_ops->MaxBurstLength;
3142
Andy Groverebf1d952012-04-03 15:51:24 -07003143 if (new_data_end > cmd->se_cmd.data_length)
3144 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003145 else
3146 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003147 }
3148 cmd->r2t_offset += xfer_len;
3149
Andy Groverebf1d952012-04-03 15:51:24 -07003150 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003151 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3152 } else {
3153 struct iscsi_seq *seq;
3154
3155 seq = iscsit_get_seq_holder_for_r2t(cmd);
3156 if (!seq) {
3157 spin_unlock_bh(&cmd->r2t_lock);
3158 return -1;
3159 }
3160
3161 offset = seq->offset;
3162 xfer_len = seq->xfer_len;
3163
3164 if (cmd->seq_send_order == cmd->seq_count)
3165 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3166 }
3167 cmd->outstanding_r2ts++;
3168 first_r2t = 0;
3169
3170 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3171 spin_unlock_bh(&cmd->r2t_lock);
3172 return -1;
3173 }
3174
3175 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3176 break;
3177 }
3178 spin_unlock_bh(&cmd->r2t_lock);
3179
3180 return 0;
3181}
3182
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003183void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3184 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003185{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003186 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003187 cmd->stat_sn = conn->stat_sn++;
3188
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08003189 atomic_long_inc(&conn->sess->rsp_pdus);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003190
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003191 memset(hdr, 0, ISCSI_HDR_LEN);
3192 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3193 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3194 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3195 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003196 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3198 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003199 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003200 }
3201 hdr->response = cmd->iscsi_response;
3202 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003203 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003204 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3205
3206 iscsit_increment_maxcmdsn(cmd, conn->sess);
3207 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3208 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3209
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003210 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3211 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3212 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3213 cmd->se_cmd.scsi_status, conn->cid);
3214}
3215EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3216
3217static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3218{
3219 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3220 struct kvec *iov;
3221 u32 padding = 0, tx_size = 0;
3222 int iov_count = 0;
3223 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3224
3225 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3226
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003227 iov = &cmd->iov_misc[0];
3228 iov[iov_count].iov_base = cmd->pdu;
3229 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3230 tx_size += ISCSI_HDR_LEN;
3231
3232 /*
3233 * Attach SENSE DATA payload to iSCSI Response PDU
3234 */
3235 if (cmd->se_cmd.sense_buffer &&
3236 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3237 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003238 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3239 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3240
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003241 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003242 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003243 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003244 iov[iov_count++].iov_len =
3245 (cmd->se_cmd.scsi_sense_length + padding);
3246 tx_size += cmd->se_cmd.scsi_sense_length;
3247
3248 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003249 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003250 cmd->se_cmd.scsi_sense_length, 0, padding);
3251 tx_size += padding;
3252 pr_debug("Adding %u bytes of padding to"
3253 " SENSE.\n", padding);
3254 }
3255
3256 if (conn->conn_ops->DataDigest) {
3257 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003258 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003259 (cmd->se_cmd.scsi_sense_length + padding),
3260 0, NULL, (u8 *)&cmd->data_crc);
3261
3262 iov[iov_count].iov_base = &cmd->data_crc;
3263 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3264 tx_size += ISCSI_CRC_LEN;
3265
3266 pr_debug("Attaching CRC32 DataDigest for"
3267 " SENSE, %u bytes CRC 0x%08x\n",
3268 (cmd->se_cmd.scsi_sense_length + padding),
3269 cmd->data_crc);
3270 }
3271
3272 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3273 " Response PDU\n",
3274 cmd->se_cmd.scsi_sense_length);
3275 }
3276
3277 if (conn->conn_ops->HeaderDigest) {
3278 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3279
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003280 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3281 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003282
3283 iov[0].iov_len += ISCSI_CRC_LEN;
3284 tx_size += ISCSI_CRC_LEN;
3285 pr_debug("Attaching CRC32 HeaderDigest for Response"
3286 " PDU 0x%08x\n", *header_digest);
3287 }
3288
3289 cmd->iov_misc_count = iov_count;
3290 cmd->tx_size = tx_size;
3291
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292 return 0;
3293}
3294
3295static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3296{
3297 switch (se_tmr->response) {
3298 case TMR_FUNCTION_COMPLETE:
3299 return ISCSI_TMF_RSP_COMPLETE;
3300 case TMR_TASK_DOES_NOT_EXIST:
3301 return ISCSI_TMF_RSP_NO_TASK;
3302 case TMR_LUN_DOES_NOT_EXIST:
3303 return ISCSI_TMF_RSP_NO_LUN;
3304 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3305 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003306 case TMR_FUNCTION_REJECTED:
3307 default:
3308 return ISCSI_TMF_RSP_REJECTED;
3309 }
3310}
3311
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003312void
3313iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3314 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003315{
3316 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003317
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003318 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003319 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003320 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003321 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003322 cmd->stat_sn = conn->stat_sn++;
3323 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3324
3325 iscsit_increment_maxcmdsn(cmd, conn->sess);
3326 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3327 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3328
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003329 pr_debug("Built Task Management Response ITT: 0x%08x,"
3330 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3331 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3332}
3333EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3334
3335static int
3336iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3337{
3338 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3339 u32 tx_size = 0;
3340
3341 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3342
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003343 cmd->iov_misc[0].iov_base = cmd->pdu;
3344 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3345 tx_size += ISCSI_HDR_LEN;
3346
3347 if (conn->conn_ops->HeaderDigest) {
3348 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3349
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003350 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3351 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003352
3353 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3354 tx_size += ISCSI_CRC_LEN;
3355 pr_debug("Attaching CRC32 HeaderDigest for Task"
3356 " Mgmt Response PDU 0x%08x\n", *header_digest);
3357 }
3358
3359 cmd->iov_misc_count = 1;
3360 cmd->tx_size = tx_size;
3361
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003362 return 0;
3363}
3364
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003365static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3366{
3367 bool ret = false;
3368
3369 if (np->np_sockaddr.ss_family == AF_INET6) {
3370 const struct sockaddr_in6 sin6 = {
3371 .sin6_addr = IN6ADDR_ANY_INIT };
3372 struct sockaddr_in6 *sock_in6 =
3373 (struct sockaddr_in6 *)&np->np_sockaddr;
3374
3375 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3376 sin6.sin6_addr.s6_addr, 16))
3377 ret = true;
3378 } else {
3379 struct sockaddr_in * sock_in =
3380 (struct sockaddr_in *)&np->np_sockaddr;
3381
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003382 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003383 ret = true;
3384 }
3385
3386 return ret;
3387}
3388
Andy Grover8b1e1242012-04-03 15:51:12 -07003389#define SENDTARGETS_BUF_LIMIT 32768U
3390
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003391static int
3392iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003393 enum iscsit_transport_type network_transport,
3394 int skip_bytes, bool *completed)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003395{
3396 char *payload = NULL;
3397 struct iscsi_conn *conn = cmd->conn;
3398 struct iscsi_portal_group *tpg;
3399 struct iscsi_tiqn *tiqn;
3400 struct iscsi_tpg_np *tpg_np;
3401 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003402 int target_name_printed;
Andy Grover8b1e1242012-04-03 15:51:12 -07003403 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003404 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003405
Sagi Grimbergbe7dcfb62015-01-26 12:49:06 +02003406 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
Andy Grover8b1e1242012-04-03 15:51:12 -07003407 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003408
3409 payload = kzalloc(buffer_len, GFP_KERNEL);
3410 if (!payload) {
3411 pr_err("Unable to allocate memory for sendtargets"
3412 " response.\n");
3413 return -ENOMEM;
3414 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003415 /*
Andy Grover8060b8d2015-01-09 15:13:08 -08003416 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
Nicholas Bellinger66658892013-06-19 22:45:42 -07003417 * explicit case..
3418 */
Andy Grover8060b8d2015-01-09 15:13:08 -08003419 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003420 text_ptr = strchr(text_in, '=');
3421 if (!text_ptr) {
3422 pr_err("Unable to locate '=' string in text_in:"
3423 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003424 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003425 return -EINVAL;
3426 }
3427 /*
3428 * Skip over '=' character..
3429 */
3430 text_ptr += 1;
3431 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003432
3433 spin_lock(&tiqn_lock);
3434 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Andy Grover8060b8d2015-01-09 15:13:08 -08003435 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
Nicholas Bellinger66658892013-06-19 22:45:42 -07003436 strcmp(tiqn->tiqn, text_ptr)) {
3437 continue;
3438 }
3439
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003440 target_name_printed = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003441
3442 spin_lock(&tiqn->tiqn_tpg_lock);
3443 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3444
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003445 /* If demo_mode_discovery=0 and generate_node_acls=0
3446 * (demo mode dislabed) do not return
3447 * TargetName+TargetAddress unless a NodeACL exists.
3448 */
3449
3450 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3451 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3452 (!core_tpg_get_initiator_node_acl(&tpg->tpg_se_tpg,
3453 cmd->conn->sess->sess_ops->InitiatorName))) {
3454 continue;
3455 }
3456
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003457 spin_lock(&tpg->tpg_state_lock);
3458 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3459 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3460 spin_unlock(&tpg->tpg_state_lock);
3461 continue;
3462 }
3463 spin_unlock(&tpg->tpg_state_lock);
3464
3465 spin_lock(&tpg->tpg_np_lock);
3466 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3467 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003468 struct iscsi_np *np = tpg_np->tpg_np;
3469 bool inaddr_any = iscsit_check_inaddr_any(np);
3470
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003471 if (np->np_network_transport != network_transport)
3472 continue;
3473
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003474 if (!target_name_printed) {
3475 len = sprintf(buf, "TargetName=%s",
3476 tiqn->tiqn);
3477 len += 1;
3478
3479 if ((len + payload_len) > buffer_len) {
3480 spin_unlock(&tpg->tpg_np_lock);
3481 spin_unlock(&tiqn->tiqn_tpg_lock);
3482 end_of_buf = 1;
3483 goto eob;
3484 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003485
3486 if (skip_bytes && len <= skip_bytes) {
3487 skip_bytes -= len;
3488 } else {
3489 memcpy(payload + payload_len, buf, len);
3490 payload_len += len;
3491 target_name_printed = 1;
3492 if (len > skip_bytes)
3493 skip_bytes = 0;
3494 }
Thomas Glanzmann2dd1d532013-10-07 23:13:46 +02003495 }
3496
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003497 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003498 "%s:%hu,%hu",
Christophe Vu-Brugier0bcc2972014-06-06 17:15:16 +02003499 inaddr_any ? conn->local_ip : np->np_ip,
Steven Allenf2774f42014-10-15 10:59:21 -07003500 np->np_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003501 tpg->tpgt);
3502 len += 1;
3503
3504 if ((len + payload_len) > buffer_len) {
3505 spin_unlock(&tpg->tpg_np_lock);
3506 spin_unlock(&tiqn->tiqn_tpg_lock);
3507 end_of_buf = 1;
3508 goto eob;
3509 }
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003510
3511 if (skip_bytes && len <= skip_bytes) {
3512 skip_bytes -= len;
3513 } else {
3514 memcpy(payload + payload_len, buf, len);
3515 payload_len += len;
3516 if (len > skip_bytes)
3517 skip_bytes = 0;
3518 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003519 }
3520 spin_unlock(&tpg->tpg_np_lock);
3521 }
3522 spin_unlock(&tiqn->tiqn_tpg_lock);
3523eob:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003524 if (end_of_buf) {
3525 *completed = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003526 break;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003527 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003528
Andy Grover8060b8d2015-01-09 15:13:08 -08003529 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
Nicholas Bellinger66658892013-06-19 22:45:42 -07003530 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003531 }
3532 spin_unlock(&tiqn_lock);
3533
3534 cmd->buf_ptr = payload;
3535
3536 return payload_len;
3537}
3538
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003539int
3540iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003541 struct iscsi_text_rsp *hdr,
3542 enum iscsit_transport_type network_transport)
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003543{
3544 int text_length, padding;
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003545 bool completed = true;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003546
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003547 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3548 cmd->read_data_done,
3549 &completed);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003550 if (text_length < 0)
3551 return text_length;
3552
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003553 if (completed) {
3554 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3555 } else {
3556 hdr->flags |= ISCSI_FLAG_TEXT_CONTINUE;
3557 cmd->read_data_done += text_length;
3558 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3559 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3560 }
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003561 hdr->opcode = ISCSI_OP_TEXT_RSP;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003562 padding = ((-text_length) & 3);
3563 hton24(hdr->dlength, text_length);
3564 hdr->itt = cmd->init_task_tag;
3565 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3566 cmd->stat_sn = conn->stat_sn++;
3567 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3568
3569 iscsit_increment_maxcmdsn(cmd, conn->sess);
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003570 /*
3571 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3572 * correctly increment MaxCmdSN for each response answering a
3573 * non immediate text request with a valid CmdSN.
3574 */
3575 cmd->maxcmdsn_inc = 0;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003576 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3577 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3578
Sagi Grimberge4f4e802015-02-09 18:07:25 +02003579 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3580 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3581 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3582 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3583 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003584
3585 return text_length + padding;
3586}
3587EXPORT_SYMBOL(iscsit_build_text_rsp);
3588
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003589static int iscsit_send_text_rsp(
3590 struct iscsi_cmd *cmd,
3591 struct iscsi_conn *conn)
3592{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003593 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003594 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003595 u32 tx_size = 0;
3596 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003597
Sagi Grimberg22c7aaa2014-06-10 18:27:59 +03003598 rc = iscsit_build_text_rsp(cmd, conn, hdr, ISCSI_TCP);
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003599 if (rc < 0)
3600 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003601
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003602 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003603 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003604 iov[iov_count].iov_base = cmd->pdu;
3605 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3606 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003607 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003608
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003609 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003610
3611 if (conn->conn_ops->HeaderDigest) {
3612 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3613
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003614 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3615 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003616
3617 iov[0].iov_len += ISCSI_CRC_LEN;
3618 tx_size += ISCSI_CRC_LEN;
3619 pr_debug("Attaching CRC32 HeaderDigest for"
3620 " Text Response PDU 0x%08x\n", *header_digest);
3621 }
3622
3623 if (conn->conn_ops->DataDigest) {
3624 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003625 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003626 0, NULL, (u8 *)&cmd->data_crc);
3627
3628 iov[iov_count].iov_base = &cmd->data_crc;
3629 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3630 tx_size += ISCSI_CRC_LEN;
3631
3632 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003633 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003634 cmd->data_crc);
3635 }
3636
3637 cmd->iov_misc_count = iov_count;
3638 cmd->tx_size = tx_size;
3639
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003640 return 0;
3641}
3642
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003643void
3644iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3645 struct iscsi_reject *hdr)
3646{
3647 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003648 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003649 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3650 hton24(hdr->dlength, ISCSI_HDR_LEN);
3651 hdr->ffffffff = cpu_to_be32(0xffffffff);
3652 cmd->stat_sn = conn->stat_sn++;
3653 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3654 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3655 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3656
3657}
3658EXPORT_SYMBOL(iscsit_build_reject);
3659
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003660static int iscsit_send_reject(
3661 struct iscsi_cmd *cmd,
3662 struct iscsi_conn *conn)
3663{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003664 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003665 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003666 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003667
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003668 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003669
3670 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003671 iov[iov_count].iov_base = cmd->pdu;
3672 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3673 iov[iov_count].iov_base = cmd->buf_ptr;
3674 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3675
3676 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3677
3678 if (conn->conn_ops->HeaderDigest) {
3679 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3680
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003681 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3682 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003683
3684 iov[0].iov_len += ISCSI_CRC_LEN;
3685 tx_size += ISCSI_CRC_LEN;
3686 pr_debug("Attaching CRC32 HeaderDigest for"
3687 " REJECT PDU 0x%08x\n", *header_digest);
3688 }
3689
3690 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003691 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3692 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003693
3694 iov[iov_count].iov_base = &cmd->data_crc;
3695 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3696 tx_size += ISCSI_CRC_LEN;
3697 pr_debug("Attaching CRC32 DataDigest for REJECT"
3698 " PDU 0x%08x\n", cmd->data_crc);
3699 }
3700
3701 cmd->iov_misc_count = iov_count;
3702 cmd->tx_size = tx_size;
3703
3704 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3705 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3706
3707 return 0;
3708}
3709
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003710void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3711{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003712 int ord, cpu;
3713 /*
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003714 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3715 * within iscsit_start_kthreads()
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003716 *
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003717 * Here we use bitmap_id to determine which CPU that this
3718 * iSCSI connection's RX/TX threads will be scheduled to
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003719 * execute upon.
3720 */
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003721 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003722 for_each_online_cpu(cpu) {
3723 if (ord-- == 0) {
3724 cpumask_set_cpu(cpu, conn->conn_cpumask);
3725 return;
3726 }
3727 }
3728 /*
3729 * This should never be reached..
3730 */
3731 dump_stack();
3732 cpumask_setall(conn->conn_cpumask);
3733}
3734
3735static inline void iscsit_thread_check_cpumask(
3736 struct iscsi_conn *conn,
3737 struct task_struct *p,
3738 int mode)
3739{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003740 /*
3741 * mode == 1 signals iscsi_target_tx_thread() usage.
3742 * mode == 0 signals iscsi_target_rx_thread() usage.
3743 */
3744 if (mode == 1) {
3745 if (!conn->conn_tx_reset_cpumask)
3746 return;
3747 conn->conn_tx_reset_cpumask = 0;
3748 } else {
3749 if (!conn->conn_rx_reset_cpumask)
3750 return;
3751 conn->conn_rx_reset_cpumask = 0;
3752 }
3753 /*
3754 * Update the CPU mask for this single kthread so that
3755 * both TX and RX kthreads are scheduled to run on the
3756 * same CPU.
3757 */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003758 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3759}
3760
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003761static int
3762iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003763{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003764 int ret;
3765
3766 switch (state) {
3767 case ISTATE_SEND_R2T:
3768 ret = iscsit_send_r2t(cmd, conn);
3769 if (ret < 0)
3770 goto err;
3771 break;
3772 case ISTATE_REMOVE:
3773 spin_lock_bh(&conn->cmd_lock);
Nicholas Bellinger5159d762014-02-03 12:53:51 -08003774 list_del_init(&cmd->i_conn_node);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003775 spin_unlock_bh(&conn->cmd_lock);
3776
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003777 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003778 break;
3779 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3780 iscsit_mod_nopin_response_timer(conn);
3781 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3782 if (ret < 0)
3783 goto err;
3784 break;
3785 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3786 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3787 if (ret < 0)
3788 goto err;
3789 break;
3790 default:
3791 pr_err("Unknown Opcode: 0x%02x ITT:"
3792 " 0x%08x, i_state: %d on CID: %hu\n",
3793 cmd->iscsi_opcode, cmd->init_task_tag, state,
3794 conn->cid);
3795 goto err;
3796 }
3797
3798 return 0;
3799
3800err:
3801 return -1;
3802}
3803
3804static int
3805iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3806{
3807 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003808 struct iscsi_queue_req *qr;
3809 struct iscsi_cmd *cmd;
3810 u8 state;
3811 int ret;
3812
3813 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3814 atomic_set(&conn->check_immediate_queue, 0);
3815 cmd = qr->cmd;
3816 state = qr->state;
3817 kmem_cache_free(lio_qr_cache, qr);
3818
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003819 ret = t->iscsit_immediate_queue(conn, cmd, state);
3820 if (ret < 0)
3821 return ret;
3822 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003823
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003824 return 0;
3825}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003826
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003827static int
3828iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3829{
3830 int ret;
3831
3832check_rsp_state:
3833 switch (state) {
3834 case ISTATE_SEND_DATAIN:
3835 ret = iscsit_send_datain(cmd, conn);
3836 if (ret < 0)
3837 goto err;
3838 else if (!ret)
3839 /* more drs */
3840 goto check_rsp_state;
3841 else if (ret == 1) {
3842 /* all done */
3843 spin_lock_bh(&cmd->istate_lock);
3844 cmd->i_state = ISTATE_SENT_STATUS;
3845 spin_unlock_bh(&cmd->istate_lock);
3846
3847 if (atomic_read(&conn->check_immediate_queue))
3848 return 1;
3849
3850 return 0;
3851 } else if (ret == 2) {
3852 /* Still must send status,
3853 SCF_TRANSPORT_TASK_SENSE was set */
3854 spin_lock_bh(&cmd->istate_lock);
3855 cmd->i_state = ISTATE_SEND_STATUS;
3856 spin_unlock_bh(&cmd->istate_lock);
3857 state = ISTATE_SEND_STATUS;
3858 goto check_rsp_state;
3859 }
3860
3861 break;
3862 case ISTATE_SEND_STATUS:
3863 case ISTATE_SEND_STATUS_RECOVERY:
3864 ret = iscsit_send_response(cmd, conn);
3865 break;
3866 case ISTATE_SEND_LOGOUTRSP:
3867 ret = iscsit_send_logout(cmd, conn);
3868 break;
3869 case ISTATE_SEND_ASYNCMSG:
3870 ret = iscsit_send_conn_drop_async_message(
3871 cmd, conn);
3872 break;
3873 case ISTATE_SEND_NOPIN:
3874 ret = iscsit_send_nopin(cmd, conn);
3875 break;
3876 case ISTATE_SEND_REJECT:
3877 ret = iscsit_send_reject(cmd, conn);
3878 break;
3879 case ISTATE_SEND_TASKMGTRSP:
3880 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3881 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003882 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003883 ret = iscsit_tmr_post_handler(cmd, conn);
3884 if (ret != 0)
3885 iscsit_fall_back_to_erl0(conn->sess);
3886 break;
3887 case ISTATE_SEND_TEXTRSP:
3888 ret = iscsit_send_text_rsp(cmd, conn);
3889 break;
3890 default:
3891 pr_err("Unknown Opcode: 0x%02x ITT:"
3892 " 0x%08x, i_state: %d on CID: %hu\n",
3893 cmd->iscsi_opcode, cmd->init_task_tag,
3894 state, conn->cid);
3895 goto err;
3896 }
3897 if (ret < 0)
3898 goto err;
3899
3900 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3901 iscsit_tx_thread_wait_for_tcp(conn);
3902 iscsit_unmap_iovec(cmd);
3903 goto err;
3904 }
3905 iscsit_unmap_iovec(cmd);
3906
3907 switch (state) {
3908 case ISTATE_SEND_LOGOUTRSP:
3909 if (!iscsit_logout_post_handler(cmd, conn))
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003910 return -ECONNRESET;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003911 /* fall through */
3912 case ISTATE_SEND_STATUS:
3913 case ISTATE_SEND_ASYNCMSG:
3914 case ISTATE_SEND_NOPIN:
3915 case ISTATE_SEND_STATUS_RECOVERY:
3916 case ISTATE_SEND_TEXTRSP:
3917 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003918 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003919 spin_lock_bh(&cmd->istate_lock);
3920 cmd->i_state = ISTATE_SENT_STATUS;
3921 spin_unlock_bh(&cmd->istate_lock);
3922 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003923 default:
3924 pr_err("Unknown Opcode: 0x%02x ITT:"
3925 " 0x%08x, i_state: %d on CID: %hu\n",
3926 cmd->iscsi_opcode, cmd->init_task_tag,
3927 cmd->i_state, conn->cid);
3928 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003929 }
3930
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003931 if (atomic_read(&conn->check_immediate_queue))
3932 return 1;
3933
Andy Grover6f3c0e62012-04-03 15:51:09 -07003934 return 0;
3935
3936err:
3937 return -1;
3938}
3939
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003940static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003941{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003942 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003943 struct iscsi_queue_req *qr;
3944 struct iscsi_cmd *cmd;
3945 u8 state;
3946 int ret;
3947
3948 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3949 cmd = qr->cmd;
3950 state = qr->state;
3951 kmem_cache_free(lio_qr_cache, qr);
3952
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003953 ret = t->iscsit_response_queue(conn, cmd, state);
3954 if (ret == 1 || ret < 0)
3955 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003956 }
3957
3958 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003959}
3960
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003961int iscsi_target_tx_thread(void *arg)
3962{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003963 int ret = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003964 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003965 /*
3966 * Allow ourselves to be interrupted by SIGINT so that a
3967 * connection recovery / failure event can be triggered externally.
3968 */
3969 allow_signal(SIGINT);
3970
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003971 while (!kthread_should_stop()) {
3972 /*
3973 * Ensure that both TX and RX per connection kthreads
3974 * are scheduled to run on the same CPU.
3975 */
3976 iscsit_thread_check_cpumask(conn, current, 1);
3977
Roland Dreierd5627ac2012-10-31 09:16:46 -07003978 wait_event_interruptible(conn->queues_wq,
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003979 !iscsit_conn_all_queues_empty(conn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003980
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003981 if (signal_pending(current))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003982 goto transport_err;
3983
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003984get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003985 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003986 if (ret < 0)
3987 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003988
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003989 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003990 if (ret == 1)
3991 goto get_immediate;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08003992 else if (ret == -ECONNRESET)
3993 goto out;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003994 else if (ret < 0)
3995 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003996 }
3997
3998transport_err:
3999 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004000out:
4001 return 0;
4002}
4003
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004004static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
4005{
4006 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
4007 struct iscsi_cmd *cmd;
4008 int ret = 0;
4009
4010 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
4011 case ISCSI_OP_SCSI_CMD:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004012 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004013 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004014 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004015
4016 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
4017 break;
4018 case ISCSI_OP_SCSI_DATA_OUT:
4019 ret = iscsit_handle_data_out(conn, buf);
4020 break;
4021 case ISCSI_OP_NOOP_OUT:
4022 cmd = NULL;
4023 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004024 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004025 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004026 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004027 }
4028 ret = iscsit_handle_nop_out(conn, cmd, buf);
4029 break;
4030 case ISCSI_OP_SCSI_TMFUNC:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004031 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004032 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004033 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004034
4035 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
4036 break;
4037 case ISCSI_OP_TEXT:
Sagi Grimberge4f4e802015-02-09 18:07:25 +02004038 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
4039 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
4040 if (!cmd)
4041 goto reject;
4042 } else {
4043 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
4044 if (!cmd)
4045 goto reject;
4046 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07004047
4048 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004049 break;
4050 case ISCSI_OP_LOGOUT:
Nicholas Bellinger676687c2014-01-20 03:36:44 +00004051 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004052 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07004053 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004054
4055 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
4056 if (ret > 0)
4057 wait_for_completion_timeout(&conn->conn_logout_comp,
4058 SECONDS_FOR_LOGOUT_COMP * HZ);
4059 break;
4060 case ISCSI_OP_SNACK:
4061 ret = iscsit_handle_snack(conn, buf);
4062 break;
4063 default:
4064 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
4065 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
4066 pr_err("Cannot recover from unknown"
4067 " opcode while ERL=0, closing iSCSI connection.\n");
4068 return -1;
4069 }
4070 if (!conn->conn_ops->OFMarker) {
4071 pr_err("Unable to recover from unknown"
4072 " opcode while OFMarker=No, closing iSCSI"
4073 " connection.\n");
4074 return -1;
4075 }
4076 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4077 pr_err("Unable to recover from unknown"
4078 " opcode, closing iSCSI connection.\n");
4079 return -1;
4080 }
4081 break;
4082 }
4083
4084 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004085reject:
4086 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004087}
4088
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004089int iscsi_target_rx_thread(void *arg)
4090{
4091 int ret;
4092 u8 buffer[ISCSI_HDR_LEN], opcode;
4093 u32 checksum = 0, digest = 0;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004094 struct iscsi_conn *conn = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004095 struct kvec iov;
4096 /*
4097 * Allow ourselves to be interrupted by SIGINT so that a
4098 * connection recovery / failure event can be triggered externally.
4099 */
4100 allow_signal(SIGINT);
4101
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004102 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4103 struct completion comp;
4104 int rc;
4105
4106 init_completion(&comp);
4107 rc = wait_for_completion_interruptible(&comp);
4108 if (rc < 0)
4109 goto transport_err;
4110
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004111 goto transport_err;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004112 }
4113
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004114 while (!kthread_should_stop()) {
4115 /*
4116 * Ensure that both TX and RX per connection kthreads
4117 * are scheduled to run on the same CPU.
4118 */
4119 iscsit_thread_check_cpumask(conn, current, 0);
4120
4121 memset(buffer, 0, ISCSI_HDR_LEN);
4122 memset(&iov, 0, sizeof(struct kvec));
4123
4124 iov.iov_base = buffer;
4125 iov.iov_len = ISCSI_HDR_LEN;
4126
4127 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4128 if (ret != ISCSI_HDR_LEN) {
4129 iscsit_rx_thread_wait_for_tcp(conn);
4130 goto transport_err;
4131 }
4132
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004133 if (conn->conn_ops->HeaderDigest) {
4134 iov.iov_base = &digest;
4135 iov.iov_len = ISCSI_CRC_LEN;
4136
4137 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4138 if (ret != ISCSI_CRC_LEN) {
4139 iscsit_rx_thread_wait_for_tcp(conn);
4140 goto transport_err;
4141 }
4142
4143 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4144 buffer, ISCSI_HDR_LEN,
4145 0, NULL, (u8 *)&checksum);
4146
4147 if (digest != checksum) {
4148 pr_err("HeaderDigest CRC32C failed,"
4149 " received 0x%08x, computed 0x%08x\n",
4150 digest, checksum);
4151 /*
4152 * Set the PDU to 0xff so it will intentionally
4153 * hit default in the switch below.
4154 */
4155 memset(buffer, 0xff, ISCSI_HDR_LEN);
Nicholas Bellinger04f3b312013-11-13 18:54:45 -08004156 atomic_long_inc(&conn->sess->conn_digest_errors);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004157 } else {
4158 pr_debug("Got HeaderDigest CRC32C"
4159 " 0x%08x\n", checksum);
4160 }
4161 }
4162
4163 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4164 goto transport_err;
4165
4166 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4167
4168 if (conn->sess->sess_ops->SessionType &&
4169 ((!(opcode & ISCSI_OP_TEXT)) ||
4170 (!(opcode & ISCSI_OP_LOGOUT)))) {
4171 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4172 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004173 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4174 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004175 goto transport_err;
4176 }
4177
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004178 ret = iscsi_target_rx_opcode(conn, buffer);
4179 if (ret < 0)
4180 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004181 }
4182
4183transport_err:
4184 if (!signal_pending(current))
4185 atomic_set(&conn->transport_failed, 1);
4186 iscsit_take_action_for_connection_exit(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004187 return 0;
4188}
4189
4190static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4191{
4192 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4193 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004194 /*
4195 * We expect this function to only ever be called from either RX or TX
4196 * thread context via iscsit_close_connection() once the other context
4197 * has been reset -> returned sleeping pre-handler state.
4198 */
4199 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004200 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004201
Nicholas Bellinger5159d762014-02-03 12:53:51 -08004202 list_del_init(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004203 spin_unlock_bh(&conn->cmd_lock);
4204
4205 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004206
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004207 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004208
4209 spin_lock_bh(&conn->cmd_lock);
4210 }
4211 spin_unlock_bh(&conn->cmd_lock);
4212}
4213
4214static void iscsit_stop_timers_for_cmds(
4215 struct iscsi_conn *conn)
4216{
4217 struct iscsi_cmd *cmd;
4218
4219 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004220 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004221 if (cmd->data_direction == DMA_TO_DEVICE)
4222 iscsit_stop_dataout_timer(cmd);
4223 }
4224 spin_unlock_bh(&conn->cmd_lock);
4225}
4226
4227int iscsit_close_connection(
4228 struct iscsi_conn *conn)
4229{
4230 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4231 struct iscsi_session *sess = conn->sess;
4232
4233 pr_debug("Closing iSCSI connection CID %hu on SID:"
4234 " %u\n", conn->cid, sess->sid);
4235 /*
4236 * Always up conn_logout_comp just in case the RX Thread is sleeping
4237 * and the logout response never got sent because the connection
4238 * failed.
4239 */
4240 complete(&conn->conn_logout_comp);
4241
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004242 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4243 if (conn->tx_thread &&
4244 cmpxchg(&conn->tx_thread_active, true, false)) {
4245 send_sig(SIGINT, conn->tx_thread, 1);
4246 kthread_stop(conn->tx_thread);
4247 }
4248 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4249 if (conn->rx_thread &&
4250 cmpxchg(&conn->rx_thread_active, true, false)) {
4251 send_sig(SIGINT, conn->rx_thread, 1);
4252 kthread_stop(conn->rx_thread);
4253 }
4254 }
4255
4256 spin_lock(&iscsit_global->ts_bitmap_lock);
4257 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4258 get_order(1));
4259 spin_unlock(&iscsit_global->ts_bitmap_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004260
4261 iscsit_stop_timers_for_cmds(conn);
4262 iscsit_stop_nopin_response_timer(conn);
4263 iscsit_stop_nopin_timer(conn);
Nicholas Bellingerdefd8842014-02-03 12:54:39 -08004264
4265 if (conn->conn_transport->iscsit_wait_conn)
4266 conn->conn_transport->iscsit_wait_conn(conn);
4267
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004268 /*
4269 * During Connection recovery drop unacknowledged out of order
4270 * commands for this connection, and prepare the other commands
4271 * for realligence.
4272 *
4273 * During normal operation clear the out of order commands (but
4274 * do not free the struct iscsi_ooo_cmdsn's) and release all
4275 * struct iscsi_cmds.
4276 */
4277 if (atomic_read(&conn->connection_recovery)) {
4278 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4279 iscsit_prepare_cmds_for_realligance(conn);
4280 } else {
4281 iscsit_clear_ooo_cmdsns_for_conn(conn);
4282 iscsit_release_commands_from_conn(conn);
4283 }
Nicholas Bellingerbbc05042014-06-10 04:03:54 +00004284 iscsit_free_queue_reqs_for_conn(conn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004285
4286 /*
4287 * Handle decrementing session or connection usage count if
4288 * a logout response was not able to be sent because the
4289 * connection failed. Fall back to Session Recovery here.
4290 */
4291 if (atomic_read(&conn->conn_logout_remove)) {
4292 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4293 iscsit_dec_conn_usage_count(conn);
4294 iscsit_dec_session_usage_count(sess);
4295 }
4296 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4297 iscsit_dec_conn_usage_count(conn);
4298
4299 atomic_set(&conn->conn_logout_remove, 0);
4300 atomic_set(&sess->session_reinstatement, 0);
4301 atomic_set(&sess->session_fall_back_to_erl0, 1);
4302 }
4303
4304 spin_lock_bh(&sess->conn_lock);
4305 list_del(&conn->conn_list);
4306
4307 /*
4308 * Attempt to let the Initiator know this connection failed by
4309 * sending an Connection Dropped Async Message on another
4310 * active connection.
4311 */
4312 if (atomic_read(&conn->connection_recovery))
4313 iscsit_build_conn_drop_async_message(conn);
4314
4315 spin_unlock_bh(&sess->conn_lock);
4316
4317 /*
4318 * If connection reinstatement is being performed on this connection,
4319 * up the connection reinstatement semaphore that is being blocked on
4320 * in iscsit_cause_connection_reinstatement().
4321 */
4322 spin_lock_bh(&conn->state_lock);
4323 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4324 spin_unlock_bh(&conn->state_lock);
4325 complete(&conn->conn_wait_comp);
4326 wait_for_completion(&conn->conn_post_wait_comp);
4327 spin_lock_bh(&conn->state_lock);
4328 }
4329
4330 /*
4331 * If connection reinstatement is being performed on this connection
4332 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4333 * connection wait rcfr semaphore that is being blocked on
4334 * an iscsit_connection_reinstatement_rcfr().
4335 */
4336 if (atomic_read(&conn->connection_wait_rcfr)) {
4337 spin_unlock_bh(&conn->state_lock);
4338 complete(&conn->conn_wait_rcfr_comp);
4339 wait_for_completion(&conn->conn_post_wait_comp);
4340 spin_lock_bh(&conn->state_lock);
4341 }
4342 atomic_set(&conn->connection_reinstatement, 1);
4343 spin_unlock_bh(&conn->state_lock);
4344
4345 /*
4346 * If any other processes are accessing this connection pointer we
4347 * must wait until they have completed.
4348 */
4349 iscsit_check_conn_usage_count(conn);
4350
4351 if (conn->conn_rx_hash.tfm)
4352 crypto_free_hash(conn->conn_rx_hash.tfm);
4353 if (conn->conn_tx_hash.tfm)
4354 crypto_free_hash(conn->conn_tx_hash.tfm);
4355
Joern Engelfbecb652014-09-02 17:49:47 -04004356 free_cpumask_var(conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004357
4358 kfree(conn->conn_ops);
4359 conn->conn_ops = NULL;
4360
Al Virobf6932f2012-07-21 08:55:18 +01004361 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004362 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004363
4364 if (conn->conn_transport->iscsit_free_conn)
4365 conn->conn_transport->iscsit_free_conn(conn);
4366
4367 iscsit_put_transport(conn->conn_transport);
4368
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004369 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4370 conn->conn_state = TARG_CONN_STATE_FREE;
4371 kfree(conn);
4372
4373 spin_lock_bh(&sess->conn_lock);
4374 atomic_dec(&sess->nconn);
4375 pr_debug("Decremented iSCSI connection count to %hu from node:"
4376 " %s\n", atomic_read(&sess->nconn),
4377 sess->sess_ops->InitiatorName);
4378 /*
4379 * Make sure that if one connection fails in an non ERL=2 iSCSI
4380 * Session that they all fail.
4381 */
4382 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4383 !atomic_read(&sess->session_logout))
4384 atomic_set(&sess->session_fall_back_to_erl0, 1);
4385
4386 /*
4387 * If this was not the last connection in the session, and we are
4388 * performing session reinstatement or falling back to ERL=0, call
4389 * iscsit_stop_session() without sleeping to shutdown the other
4390 * active connections.
4391 */
4392 if (atomic_read(&sess->nconn)) {
4393 if (!atomic_read(&sess->session_reinstatement) &&
4394 !atomic_read(&sess->session_fall_back_to_erl0)) {
4395 spin_unlock_bh(&sess->conn_lock);
4396 return 0;
4397 }
4398 if (!atomic_read(&sess->session_stop_active)) {
4399 atomic_set(&sess->session_stop_active, 1);
4400 spin_unlock_bh(&sess->conn_lock);
4401 iscsit_stop_session(sess, 0, 0);
4402 return 0;
4403 }
4404 spin_unlock_bh(&sess->conn_lock);
4405 return 0;
4406 }
4407
4408 /*
4409 * If this was the last connection in the session and one of the
4410 * following is occurring:
4411 *
4412 * Session Reinstatement is not being performed, and are falling back
4413 * to ERL=0 call iscsit_close_session().
4414 *
4415 * Session Logout was requested. iscsit_close_session() will be called
4416 * elsewhere.
4417 *
4418 * Session Continuation is not being performed, start the Time2Retain
4419 * handler and check if sleep_on_sess_wait_sem is active.
4420 */
4421 if (!atomic_read(&sess->session_reinstatement) &&
4422 atomic_read(&sess->session_fall_back_to_erl0)) {
4423 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004424 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004425
4426 return 0;
4427 } else if (atomic_read(&sess->session_logout)) {
4428 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4429 sess->session_state = TARG_SESS_STATE_FREE;
4430 spin_unlock_bh(&sess->conn_lock);
4431
4432 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4433 complete(&sess->session_wait_comp);
4434
4435 return 0;
4436 } else {
4437 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4438 sess->session_state = TARG_SESS_STATE_FAILED;
4439
4440 if (!atomic_read(&sess->session_continuation)) {
4441 spin_unlock_bh(&sess->conn_lock);
4442 iscsit_start_time2retain_handler(sess);
4443 } else
4444 spin_unlock_bh(&sess->conn_lock);
4445
4446 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4447 complete(&sess->session_wait_comp);
4448
4449 return 0;
4450 }
4451 spin_unlock_bh(&sess->conn_lock);
4452
4453 return 0;
4454}
4455
4456int iscsit_close_session(struct iscsi_session *sess)
4457{
Andy Grover60bfcf82013-10-09 11:05:58 -07004458 struct iscsi_portal_group *tpg = sess->tpg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004459 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4460
4461 if (atomic_read(&sess->nconn)) {
4462 pr_err("%d connection(s) still exist for iSCSI session"
4463 " to %s\n", atomic_read(&sess->nconn),
4464 sess->sess_ops->InitiatorName);
4465 BUG();
4466 }
4467
4468 spin_lock_bh(&se_tpg->session_lock);
4469 atomic_set(&sess->session_logout, 1);
4470 atomic_set(&sess->session_reinstatement, 1);
4471 iscsit_stop_time2retain_timer(sess);
4472 spin_unlock_bh(&se_tpg->session_lock);
4473
4474 /*
4475 * transport_deregister_session_configfs() will clear the
4476 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4477 * can be setting it again with __transport_register_session() in
4478 * iscsi_post_login_handler() again after the iscsit_stop_session()
4479 * completes in iscsi_np context.
4480 */
4481 transport_deregister_session_configfs(sess->se_sess);
4482
4483 /*
4484 * If any other processes are accessing this session pointer we must
4485 * wait until they have completed. If we are in an interrupt (the
4486 * time2retain handler) and contain and active session usage count we
4487 * restart the timer and exit.
4488 */
4489 if (!in_interrupt()) {
4490 if (iscsit_check_session_usage_count(sess) == 1)
4491 iscsit_stop_session(sess, 1, 1);
4492 } else {
4493 if (iscsit_check_session_usage_count(sess) == 2) {
4494 atomic_set(&sess->session_logout, 0);
4495 iscsit_start_time2retain_handler(sess);
4496 return 0;
4497 }
4498 }
4499
4500 transport_deregister_session(sess->se_sess);
4501
4502 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4503 iscsit_free_connection_recovery_entires(sess);
4504
4505 iscsit_free_all_ooo_cmdsns(sess);
4506
4507 spin_lock_bh(&se_tpg->session_lock);
4508 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4509 sess->session_state = TARG_SESS_STATE_FREE;
4510 pr_debug("Released iSCSI session from node: %s\n",
4511 sess->sess_ops->InitiatorName);
4512 tpg->nsessions--;
4513 if (tpg->tpg_tiqn)
4514 tpg->tpg_tiqn->tiqn_nsessions--;
4515
4516 pr_debug("Decremented number of active iSCSI Sessions on"
4517 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4518
4519 spin_lock(&sess_idr_lock);
4520 idr_remove(&sess_idr, sess->session_index);
4521 spin_unlock(&sess_idr_lock);
4522
4523 kfree(sess->sess_ops);
4524 sess->sess_ops = NULL;
4525 spin_unlock_bh(&se_tpg->session_lock);
4526
4527 kfree(sess);
4528 return 0;
4529}
4530
4531static void iscsit_logout_post_handler_closesession(
4532 struct iscsi_conn *conn)
4533{
4534 struct iscsi_session *sess = conn->sess;
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004535 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004536
4537 atomic_set(&conn->conn_logout_remove, 0);
4538 complete(&conn->conn_logout_comp);
4539
4540 iscsit_dec_conn_usage_count(conn);
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004541 iscsit_stop_session(sess, sleep, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004542 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004543 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004544}
4545
4546static void iscsit_logout_post_handler_samecid(
4547 struct iscsi_conn *conn)
4548{
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004549 int sleep = cmpxchg(&conn->tx_thread_active, true, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004550
4551 atomic_set(&conn->conn_logout_remove, 0);
4552 complete(&conn->conn_logout_comp);
4553
Nicholas Bellinger88dcd2d2015-02-26 22:19:15 -08004554 iscsit_cause_connection_reinstatement(conn, sleep);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004555 iscsit_dec_conn_usage_count(conn);
4556}
4557
4558static void iscsit_logout_post_handler_diffcid(
4559 struct iscsi_conn *conn,
4560 u16 cid)
4561{
4562 struct iscsi_conn *l_conn;
4563 struct iscsi_session *sess = conn->sess;
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004564 bool conn_found = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004565
4566 if (!sess)
4567 return;
4568
4569 spin_lock_bh(&sess->conn_lock);
4570 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4571 if (l_conn->cid == cid) {
4572 iscsit_inc_conn_usage_count(l_conn);
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004573 conn_found = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004574 break;
4575 }
4576 }
4577 spin_unlock_bh(&sess->conn_lock);
4578
Nicholas Bellingerb53b0d992014-09-17 11:45:17 -07004579 if (!conn_found)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004580 return;
4581
4582 if (l_conn->sock)
4583 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4584
4585 spin_lock_bh(&l_conn->state_lock);
4586 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4587 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4588 spin_unlock_bh(&l_conn->state_lock);
4589
4590 iscsit_cause_connection_reinstatement(l_conn, 1);
4591 iscsit_dec_conn_usage_count(l_conn);
4592}
4593
4594/*
4595 * Return of 0 causes the TX thread to restart.
4596 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004597int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004598 struct iscsi_cmd *cmd,
4599 struct iscsi_conn *conn)
4600{
4601 int ret = 0;
4602
4603 switch (cmd->logout_reason) {
4604 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4605 switch (cmd->logout_response) {
4606 case ISCSI_LOGOUT_SUCCESS:
4607 case ISCSI_LOGOUT_CLEANUP_FAILED:
4608 default:
4609 iscsit_logout_post_handler_closesession(conn);
4610 break;
4611 }
4612 ret = 0;
4613 break;
4614 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4615 if (conn->cid == cmd->logout_cid) {
4616 switch (cmd->logout_response) {
4617 case ISCSI_LOGOUT_SUCCESS:
4618 case ISCSI_LOGOUT_CLEANUP_FAILED:
4619 default:
4620 iscsit_logout_post_handler_samecid(conn);
4621 break;
4622 }
4623 ret = 0;
4624 } else {
4625 switch (cmd->logout_response) {
4626 case ISCSI_LOGOUT_SUCCESS:
4627 iscsit_logout_post_handler_diffcid(conn,
4628 cmd->logout_cid);
4629 break;
4630 case ISCSI_LOGOUT_CID_NOT_FOUND:
4631 case ISCSI_LOGOUT_CLEANUP_FAILED:
4632 default:
4633 break;
4634 }
4635 ret = 1;
4636 }
4637 break;
4638 case ISCSI_LOGOUT_REASON_RECOVERY:
4639 switch (cmd->logout_response) {
4640 case ISCSI_LOGOUT_SUCCESS:
4641 case ISCSI_LOGOUT_CID_NOT_FOUND:
4642 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4643 case ISCSI_LOGOUT_CLEANUP_FAILED:
4644 default:
4645 break;
4646 }
4647 ret = 1;
4648 break;
4649 default:
4650 break;
4651
4652 }
4653 return ret;
4654}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004655EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004656
4657void iscsit_fail_session(struct iscsi_session *sess)
4658{
4659 struct iscsi_conn *conn;
4660
4661 spin_lock_bh(&sess->conn_lock);
4662 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4663 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4664 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4665 }
4666 spin_unlock_bh(&sess->conn_lock);
4667
4668 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4669 sess->session_state = TARG_SESS_STATE_FAILED;
4670}
4671
4672int iscsit_free_session(struct iscsi_session *sess)
4673{
4674 u16 conn_count = atomic_read(&sess->nconn);
4675 struct iscsi_conn *conn, *conn_tmp = NULL;
4676 int is_last;
4677
4678 spin_lock_bh(&sess->conn_lock);
4679 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4680
4681 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4682 conn_list) {
4683 if (conn_count == 0)
4684 break;
4685
4686 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4687 is_last = 1;
4688 } else {
4689 iscsit_inc_conn_usage_count(conn_tmp);
4690 is_last = 0;
4691 }
4692 iscsit_inc_conn_usage_count(conn);
4693
4694 spin_unlock_bh(&sess->conn_lock);
4695 iscsit_cause_connection_reinstatement(conn, 1);
4696 spin_lock_bh(&sess->conn_lock);
4697
4698 iscsit_dec_conn_usage_count(conn);
4699 if (is_last == 0)
4700 iscsit_dec_conn_usage_count(conn_tmp);
4701
4702 conn_count--;
4703 }
4704
4705 if (atomic_read(&sess->nconn)) {
4706 spin_unlock_bh(&sess->conn_lock);
4707 wait_for_completion(&sess->session_wait_comp);
4708 } else
4709 spin_unlock_bh(&sess->conn_lock);
4710
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004711 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004712 return 0;
4713}
4714
4715void iscsit_stop_session(
4716 struct iscsi_session *sess,
4717 int session_sleep,
4718 int connection_sleep)
4719{
4720 u16 conn_count = atomic_read(&sess->nconn);
4721 struct iscsi_conn *conn, *conn_tmp = NULL;
4722 int is_last;
4723
4724 spin_lock_bh(&sess->conn_lock);
4725 if (session_sleep)
4726 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4727
4728 if (connection_sleep) {
4729 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4730 conn_list) {
4731 if (conn_count == 0)
4732 break;
4733
4734 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4735 is_last = 1;
4736 } else {
4737 iscsit_inc_conn_usage_count(conn_tmp);
4738 is_last = 0;
4739 }
4740 iscsit_inc_conn_usage_count(conn);
4741
4742 spin_unlock_bh(&sess->conn_lock);
4743 iscsit_cause_connection_reinstatement(conn, 1);
4744 spin_lock_bh(&sess->conn_lock);
4745
4746 iscsit_dec_conn_usage_count(conn);
4747 if (is_last == 0)
4748 iscsit_dec_conn_usage_count(conn_tmp);
4749 conn_count--;
4750 }
4751 } else {
4752 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4753 iscsit_cause_connection_reinstatement(conn, 0);
4754 }
4755
4756 if (session_sleep && atomic_read(&sess->nconn)) {
4757 spin_unlock_bh(&sess->conn_lock);
4758 wait_for_completion(&sess->session_wait_comp);
4759 } else
4760 spin_unlock_bh(&sess->conn_lock);
4761}
4762
4763int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4764{
4765 struct iscsi_session *sess;
4766 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4767 struct se_session *se_sess, *se_sess_tmp;
4768 int session_count = 0;
4769
4770 spin_lock_bh(&se_tpg->session_lock);
4771 if (tpg->nsessions && !force) {
4772 spin_unlock_bh(&se_tpg->session_lock);
4773 return -1;
4774 }
4775
4776 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4777 sess_list) {
4778 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4779
4780 spin_lock(&sess->conn_lock);
4781 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4782 atomic_read(&sess->session_logout) ||
4783 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4784 spin_unlock(&sess->conn_lock);
4785 continue;
4786 }
4787 atomic_set(&sess->session_reinstatement, 1);
4788 spin_unlock(&sess->conn_lock);
4789 spin_unlock_bh(&se_tpg->session_lock);
4790
4791 iscsit_free_session(sess);
4792 spin_lock_bh(&se_tpg->session_lock);
4793
4794 session_count++;
4795 }
4796 spin_unlock_bh(&se_tpg->session_lock);
4797
4798 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4799 " Group: %hu\n", session_count, tpg->tpgt);
4800 return 0;
4801}
4802
4803MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4804MODULE_VERSION("4.1.x");
4805MODULE_AUTHOR("nab@Linux-iSCSI.org");
4806MODULE_LICENSE("GPL");
4807
4808module_init(iscsi_target_init_module);
4809module_exit(iscsi_target_cleanup_module);