blob: db740e7e9ba2be2a11f9499b22264f8190b6eb29 [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file contains main functions related to the iSCSI Target Core Driver.
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2007-2013 Datera, Inc.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00005 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
18
19#include <linux/string.h>
20#include <linux/kthread.h>
21#include <linux/crypto.h>
22#include <linux/completion.h>
Paul Gortmaker827509e2011-08-30 14:20:44 -040023#include <linux/module.h>
Al Viro40401532012-02-13 03:58:52 +000024#include <linux/idr.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000025#include <asm/unaligned.h>
26#include <scsi/scsi_device.h>
27#include <scsi/iscsi_proto.h>
Andy Groverd28b11692012-04-03 15:51:22 -070028#include <scsi/scsi_tcq.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000029#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050030#include <target/target_core_fabric.h>
Andy Groverd28b11692012-04-03 15:51:22 -070031#include <target/target_core_configfs.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000032
33#include "iscsi_target_core.h"
34#include "iscsi_target_parameters.h"
35#include "iscsi_target_seq_pdu_list.h"
36#include "iscsi_target_tq.h"
37#include "iscsi_target_configfs.h"
38#include "iscsi_target_datain_values.h"
39#include "iscsi_target_erl0.h"
40#include "iscsi_target_erl1.h"
41#include "iscsi_target_erl2.h"
42#include "iscsi_target_login.h"
43#include "iscsi_target_tmr.h"
44#include "iscsi_target_tpg.h"
45#include "iscsi_target_util.h"
46#include "iscsi_target.h"
47#include "iscsi_target_device.h"
48#include "iscsi_target_stat.h"
49
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -080050#include <target/iscsi/iscsi_transport.h>
51
Nicholas Bellingere48354c2011-07-23 06:43:04 +000052static LIST_HEAD(g_tiqn_list);
53static LIST_HEAD(g_np_list);
54static DEFINE_SPINLOCK(tiqn_lock);
55static DEFINE_SPINLOCK(np_lock);
56
57static struct idr tiqn_idr;
58struct idr sess_idr;
59struct mutex auth_id_lock;
60spinlock_t sess_idr_lock;
61
62struct iscsit_global *iscsit_global;
63
Nicholas Bellingere48354c2011-07-23 06:43:04 +000064struct kmem_cache *lio_qr_cache;
65struct kmem_cache *lio_dr_cache;
66struct kmem_cache *lio_ooo_cache;
67struct kmem_cache *lio_r2t_cache;
68
69static int iscsit_handle_immediate_data(struct iscsi_cmd *,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -070070 struct iscsi_scsi_req *, u32);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000071
72struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
73{
74 struct iscsi_tiqn *tiqn = NULL;
75
76 spin_lock(&tiqn_lock);
77 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
78 if (!strcmp(tiqn->tiqn, buf)) {
79
80 spin_lock(&tiqn->tiqn_state_lock);
81 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
82 tiqn->tiqn_access_count++;
83 spin_unlock(&tiqn->tiqn_state_lock);
84 spin_unlock(&tiqn_lock);
85 return tiqn;
86 }
87 spin_unlock(&tiqn->tiqn_state_lock);
88 }
89 }
90 spin_unlock(&tiqn_lock);
91
92 return NULL;
93}
94
95static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
96{
97 spin_lock(&tiqn->tiqn_state_lock);
98 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
99 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
100 spin_unlock(&tiqn->tiqn_state_lock);
101 return 0;
102 }
103 spin_unlock(&tiqn->tiqn_state_lock);
104
105 return -1;
106}
107
108void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
109{
110 spin_lock(&tiqn->tiqn_state_lock);
111 tiqn->tiqn_access_count--;
112 spin_unlock(&tiqn->tiqn_state_lock);
113}
114
115/*
116 * Note that IQN formatting is expected to be done in userspace, and
117 * no explict IQN format checks are done here.
118 */
119struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
120{
121 struct iscsi_tiqn *tiqn = NULL;
122 int ret;
123
Dan Carpenter8f50c7f2011-07-27 14:11:43 +0300124 if (strlen(buf) >= ISCSI_IQN_LEN) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000125 pr_err("Target IQN exceeds %d bytes\n",
126 ISCSI_IQN_LEN);
127 return ERR_PTR(-EINVAL);
128 }
129
130 tiqn = kzalloc(sizeof(struct iscsi_tiqn), GFP_KERNEL);
131 if (!tiqn) {
132 pr_err("Unable to allocate struct iscsi_tiqn\n");
133 return ERR_PTR(-ENOMEM);
134 }
135
136 sprintf(tiqn->tiqn, "%s", buf);
137 INIT_LIST_HEAD(&tiqn->tiqn_list);
138 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
139 spin_lock_init(&tiqn->tiqn_state_lock);
140 spin_lock_init(&tiqn->tiqn_tpg_lock);
141 spin_lock_init(&tiqn->sess_err_stats.lock);
142 spin_lock_init(&tiqn->login_stats.lock);
143 spin_lock_init(&tiqn->logout_stats.lock);
144
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000145 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
146
Tejun Heoc9365bd2013-02-27 17:04:43 -0800147 idr_preload(GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000148 spin_lock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800149
150 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000151 if (ret < 0) {
Tejun Heoc9365bd2013-02-27 17:04:43 -0800152 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000153 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800154 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000155 kfree(tiqn);
156 return ERR_PTR(ret);
157 }
Tejun Heoc9365bd2013-02-27 17:04:43 -0800158 tiqn->tiqn_index = ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000159 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800160
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000161 spin_unlock(&tiqn_lock);
Tejun Heoc9365bd2013-02-27 17:04:43 -0800162 idr_preload_end();
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000163
164 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
165
166 return tiqn;
167
168}
169
170static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
171{
172 /*
173 * Wait for accesses to said struct iscsi_tiqn to end.
174 */
175 spin_lock(&tiqn->tiqn_state_lock);
176 while (tiqn->tiqn_access_count != 0) {
177 spin_unlock(&tiqn->tiqn_state_lock);
178 msleep(10);
179 spin_lock(&tiqn->tiqn_state_lock);
180 }
181 spin_unlock(&tiqn->tiqn_state_lock);
182}
183
184void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
185{
186 /*
187 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
188 * while holding tiqn->tiqn_state_lock. This means that all subsequent
189 * attempts to access this struct iscsi_tiqn will fail from both transport
190 * fabric and control code paths.
191 */
192 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
193 pr_err("iscsit_set_tiqn_shutdown() failed\n");
194 return;
195 }
196
197 iscsit_wait_for_tiqn(tiqn);
198
199 spin_lock(&tiqn_lock);
200 list_del(&tiqn->tiqn_list);
201 idr_remove(&tiqn_idr, tiqn->tiqn_index);
202 spin_unlock(&tiqn_lock);
203
204 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
205 tiqn->tiqn);
206 kfree(tiqn);
207}
208
209int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
210{
211 int ret;
212 /*
213 * Determine if the network portal is accepting storage traffic.
214 */
215 spin_lock_bh(&np->np_thread_lock);
216 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
217 spin_unlock_bh(&np->np_thread_lock);
218 return -1;
219 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000220 spin_unlock_bh(&np->np_thread_lock);
221 /*
222 * Determine if the portal group is accepting storage traffic.
223 */
224 spin_lock_bh(&tpg->tpg_state_lock);
225 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
226 spin_unlock_bh(&tpg->tpg_state_lock);
227 return -1;
228 }
229 spin_unlock_bh(&tpg->tpg_state_lock);
230
231 /*
232 * Here we serialize access across the TIQN+TPG Tuple.
233 */
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700234 ret = down_interruptible(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000235 if ((ret != 0) || signal_pending(current))
236 return -1;
237
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700238 spin_lock_bh(&tpg->tpg_state_lock);
239 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
240 spin_unlock_bh(&tpg->tpg_state_lock);
241 up(&tpg->np_login_sem);
242 return -1;
243 }
244 spin_unlock_bh(&tpg->tpg_state_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000245
246 return 0;
247}
248
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700249void iscsit_login_kref_put(struct kref *kref)
250{
251 struct iscsi_tpg_np *tpg_np = container_of(kref,
252 struct iscsi_tpg_np, tpg_np_kref);
253
254 complete(&tpg_np->tpg_np_comp);
255}
256
257int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
258 struct iscsi_tpg_np *tpg_np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000259{
260 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
261
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700262 up(&tpg->np_login_sem);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000263
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700264 if (tpg_np)
265 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000266
267 if (tiqn)
268 iscsit_put_tiqn_for_login(tiqn);
269
270 return 0;
271}
272
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800273bool iscsit_check_np_match(
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 struct __kernel_sockaddr_storage *sockaddr,
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800275 struct iscsi_np *np,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000276 int network_transport)
277{
278 struct sockaddr_in *sock_in, *sock_in_e;
279 struct sockaddr_in6 *sock_in6, *sock_in6_e;
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800280 bool ip_match = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000281 u16 port;
282
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800283 if (sockaddr->ss_family == AF_INET6) {
284 sock_in6 = (struct sockaddr_in6 *)sockaddr;
285 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
286
287 if (!memcmp(&sock_in6->sin6_addr.in6_u,
288 &sock_in6_e->sin6_addr.in6_u,
289 sizeof(struct in6_addr)))
290 ip_match = true;
291
292 port = ntohs(sock_in6->sin6_port);
293 } else {
294 sock_in = (struct sockaddr_in *)sockaddr;
295 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
296
297 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
298 ip_match = true;
299
300 port = ntohs(sock_in->sin_port);
301 }
302
303 if ((ip_match == true) && (np->np_port == port) &&
304 (np->np_network_transport == network_transport))
305 return true;
306
307 return false;
308}
309
310static struct iscsi_np *iscsit_get_np(
311 struct __kernel_sockaddr_storage *sockaddr,
312 int network_transport)
313{
314 struct iscsi_np *np;
315 bool match;
316
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000317 spin_lock_bh(&np_lock);
318 list_for_each_entry(np, &g_np_list, np_list) {
319 spin_lock(&np->np_thread_lock);
320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
321 spin_unlock(&np->np_thread_lock);
322 continue;
323 }
324
Nicholas Bellinger05b96892013-02-18 20:59:27 -0800325 match = iscsit_check_np_match(sockaddr, np, network_transport);
326 if (match == true) {
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++;
333 spin_unlock(&np->np_thread_lock);
334 spin_unlock_bh(&np_lock);
335 return np;
336 }
337 spin_unlock(&np->np_thread_lock);
338 }
339 spin_unlock_bh(&np_lock);
340
341 return NULL;
342}
343
344struct iscsi_np *iscsit_add_np(
345 struct __kernel_sockaddr_storage *sockaddr,
346 char *ip_str,
347 int network_transport)
348{
349 struct sockaddr_in *sock_in;
350 struct sockaddr_in6 *sock_in6;
351 struct iscsi_np *np;
352 int ret;
353 /*
354 * Locate the existing struct iscsi_np if already active..
355 */
356 np = iscsit_get_np(sockaddr, network_transport);
357 if (np)
358 return np;
359
360 np = kzalloc(sizeof(struct iscsi_np), GFP_KERNEL);
361 if (!np) {
362 pr_err("Unable to allocate memory for struct iscsi_np\n");
363 return ERR_PTR(-ENOMEM);
364 }
365
366 np->np_flags |= NPF_IP_NETWORK;
367 if (sockaddr->ss_family == AF_INET6) {
368 sock_in6 = (struct sockaddr_in6 *)sockaddr;
369 snprintf(np->np_ip, IPV6_ADDRESS_SPACE, "%s", ip_str);
370 np->np_port = ntohs(sock_in6->sin6_port);
371 } else {
372 sock_in = (struct sockaddr_in *)sockaddr;
373 sprintf(np->np_ip, "%s", ip_str);
374 np->np_port = ntohs(sock_in->sin_port);
375 }
376
377 np->np_network_transport = network_transport;
378 spin_lock_init(&np->np_thread_lock);
379 init_completion(&np->np_restart_comp);
380 INIT_LIST_HEAD(&np->np_list);
381
382 ret = iscsi_target_setup_login_socket(np, sockaddr);
383 if (ret != 0) {
384 kfree(np);
385 return ERR_PTR(ret);
386 }
387
388 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
389 if (IS_ERR(np->np_thread)) {
390 pr_err("Unable to create kthread: iscsi_np\n");
391 ret = PTR_ERR(np->np_thread);
392 kfree(np);
393 return ERR_PTR(ret);
394 }
395 /*
396 * Increment the np_exports reference count now to prevent
397 * iscsit_del_np() below from being run while a new call to
398 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
399 * active. We don't need to hold np->np_thread_lock at this
400 * point because iscsi_np has not been added to g_np_list yet.
401 */
402 np->np_exports = 1;
403
404 spin_lock_bh(&np_lock);
405 list_add_tail(&np->np_list, &g_np_list);
406 spin_unlock_bh(&np_lock);
407
408 pr_debug("CORE[0] - Added Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800409 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000410
411 return np;
412}
413
414int iscsit_reset_np_thread(
415 struct iscsi_np *np,
416 struct iscsi_tpg_np *tpg_np,
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700417 struct iscsi_portal_group *tpg,
418 bool shutdown)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000419{
420 spin_lock_bh(&np->np_thread_lock);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000421 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
422 spin_unlock_bh(&np->np_thread_lock);
423 return 0;
424 }
425 np->np_thread_state = ISCSI_NP_THREAD_RESET;
426
427 if (np->np_thread) {
428 spin_unlock_bh(&np->np_thread_lock);
429 send_sig(SIGINT, np->np_thread, 1);
430 wait_for_completion(&np->np_restart_comp);
431 spin_lock_bh(&np->np_thread_lock);
432 }
433 spin_unlock_bh(&np->np_thread_lock);
434
Nicholas Bellingera91eb7d2013-08-15 12:49:02 -0700435 if (tpg_np && shutdown) {
436 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
437
438 wait_for_completion(&tpg_np->tpg_np_comp);
439 }
440
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000441 return 0;
442}
443
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800444static void iscsit_free_np(struct iscsi_np *np)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000445{
Al Virobf6932f2012-07-21 08:55:18 +0100446 if (np->np_socket)
447 sock_release(np->np_socket);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000448}
449
450int iscsit_del_np(struct iscsi_np *np)
451{
452 spin_lock_bh(&np->np_thread_lock);
453 np->np_exports--;
454 if (np->np_exports) {
455 spin_unlock_bh(&np->np_thread_lock);
456 return 0;
457 }
458 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
459 spin_unlock_bh(&np->np_thread_lock);
460
461 if (np->np_thread) {
462 /*
463 * We need to send the signal to wakeup Linux/Net
464 * which may be sleeping in sock_accept()..
465 */
466 send_sig(SIGINT, np->np_thread, 1);
467 kthread_stop(np->np_thread);
468 }
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800469
470 np->np_transport->iscsit_free_np(np);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000471
472 spin_lock_bh(&np_lock);
473 list_del(&np->np_list);
474 spin_unlock_bh(&np_lock);
475
476 pr_debug("CORE[0] - Removed Network Portal: %s:%hu on %s\n",
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800477 np->np_ip, np->np_port, np->np_transport->name);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000478
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800479 iscsit_put_transport(np->np_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000480 kfree(np);
481 return 0;
482}
483
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700484static int iscsit_immediate_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
485static int iscsit_response_queue(struct iscsi_conn *, struct iscsi_cmd *, int);
486
487static int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
488{
489 iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
490 return 0;
491}
492
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800493static struct iscsit_transport iscsi_target_transport = {
494 .name = "iSCSI/TCP",
495 .transport_type = ISCSI_TCP,
496 .owner = NULL,
497 .iscsit_setup_np = iscsit_setup_np,
498 .iscsit_accept_np = iscsit_accept_np,
499 .iscsit_free_np = iscsit_free_np,
500 .iscsit_get_login_rx = iscsit_get_login_rx,
501 .iscsit_put_login_tx = iscsit_put_login_tx,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800502 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -0700503 .iscsit_immediate_queue = iscsit_immediate_queue,
504 .iscsit_response_queue = iscsit_response_queue,
505 .iscsit_queue_data_in = iscsit_queue_rsp,
506 .iscsit_queue_status = iscsit_queue_rsp,
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800507};
508
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000509static int __init iscsi_target_init_module(void)
510{
511 int ret = 0;
512
513 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
514
515 iscsit_global = kzalloc(sizeof(struct iscsit_global), GFP_KERNEL);
516 if (!iscsit_global) {
517 pr_err("Unable to allocate memory for iscsit_global\n");
518 return -1;
519 }
520 mutex_init(&auth_id_lock);
521 spin_lock_init(&sess_idr_lock);
522 idr_init(&tiqn_idr);
523 idr_init(&sess_idr);
524
525 ret = iscsi_target_register_configfs();
526 if (ret < 0)
527 goto out;
528
529 ret = iscsi_thread_set_init();
530 if (ret < 0)
531 goto configfs_out;
532
533 if (iscsi_allocate_thread_sets(TARGET_THREAD_SET_COUNT) !=
534 TARGET_THREAD_SET_COUNT) {
535 pr_err("iscsi_allocate_thread_sets() returned"
536 " unexpected value!\n");
537 goto ts_out1;
538 }
539
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000540 lio_qr_cache = kmem_cache_create("lio_qr_cache",
541 sizeof(struct iscsi_queue_req),
542 __alignof__(struct iscsi_queue_req), 0, NULL);
543 if (!lio_qr_cache) {
544 pr_err("nable to kmem_cache_create() for"
545 " lio_qr_cache\n");
Nicholas Bellingerd703ce22013-08-17 14:27:56 -0700546 goto ts_out2;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000547 }
548
549 lio_dr_cache = kmem_cache_create("lio_dr_cache",
550 sizeof(struct iscsi_datain_req),
551 __alignof__(struct iscsi_datain_req), 0, NULL);
552 if (!lio_dr_cache) {
553 pr_err("Unable to kmem_cache_create() for"
554 " lio_dr_cache\n");
555 goto qr_out;
556 }
557
558 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
559 sizeof(struct iscsi_ooo_cmdsn),
560 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
561 if (!lio_ooo_cache) {
562 pr_err("Unable to kmem_cache_create() for"
563 " lio_ooo_cache\n");
564 goto dr_out;
565 }
566
567 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
568 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
569 0, NULL);
570 if (!lio_r2t_cache) {
571 pr_err("Unable to kmem_cache_create() for"
572 " lio_r2t_cache\n");
573 goto ooo_out;
574 }
575
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800576 iscsit_register_transport(&iscsi_target_transport);
577
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000578 if (iscsit_load_discovery_tpg() < 0)
579 goto r2t_out;
580
581 return ret;
582r2t_out:
583 kmem_cache_destroy(lio_r2t_cache);
584ooo_out:
585 kmem_cache_destroy(lio_ooo_cache);
586dr_out:
587 kmem_cache_destroy(lio_dr_cache);
588qr_out:
589 kmem_cache_destroy(lio_qr_cache);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000590ts_out2:
591 iscsi_deallocate_thread_sets();
592ts_out1:
593 iscsi_thread_set_free();
594configfs_out:
595 iscsi_target_deregister_configfs();
596out:
597 kfree(iscsit_global);
598 return -ENOMEM;
599}
600
601static void __exit iscsi_target_cleanup_module(void)
602{
603 iscsi_deallocate_thread_sets();
604 iscsi_thread_set_free();
605 iscsit_release_discovery_tpg();
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -0800606 iscsit_unregister_transport(&iscsi_target_transport);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000607 kmem_cache_destroy(lio_qr_cache);
608 kmem_cache_destroy(lio_dr_cache);
609 kmem_cache_destroy(lio_ooo_cache);
610 kmem_cache_destroy(lio_r2t_cache);
611
612 iscsi_target_deregister_configfs();
613
614 kfree(iscsit_global);
615}
616
Andy Grover8b1e1242012-04-03 15:51:12 -0700617static int iscsit_add_reject(
Nicholas Bellingerba159912013-07-03 03:48:24 -0700618 struct iscsi_conn *conn,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000619 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700620 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000621{
622 struct iscsi_cmd *cmd;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000623
624 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
625 if (!cmd)
626 return -1;
627
628 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700629 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000630
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100631 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000632 if (!cmd->buf_ptr) {
633 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700634 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000635 return -1;
636 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000637
638 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700639 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000640 spin_unlock_bh(&conn->cmd_lock);
641
642 cmd->i_state = ISTATE_SEND_REJECT;
643 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
644
Nicholas Bellingerba159912013-07-03 03:48:24 -0700645 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000646}
647
Nicholas Bellingerba159912013-07-03 03:48:24 -0700648static int iscsit_add_reject_from_cmd(
649 struct iscsi_cmd *cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000650 u8 reason,
Nicholas Bellingerba159912013-07-03 03:48:24 -0700651 bool add_to_conn,
652 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000653{
654 struct iscsi_conn *conn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000655
656 if (!cmd->conn) {
657 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
658 cmd->init_task_tag);
659 return -1;
660 }
661 conn = cmd->conn;
662
663 cmd->iscsi_opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -0700664 cmd->reject_reason = reason;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000665
Thomas Meyer1c3d5792011-11-17 23:43:40 +0100666 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000667 if (!cmd->buf_ptr) {
668 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -0700669 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000670 return -1;
671 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000672
673 if (add_to_conn) {
674 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700675 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000676 spin_unlock_bh(&conn->cmd_lock);
677 }
678
679 cmd->i_state = ISTATE_SEND_REJECT;
680 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800681 /*
682 * Perform the kref_put now if se_cmd has already been setup by
683 * scsit_setup_scsi_cmd()
684 */
685 if (cmd->se_cmd.se_tfo != NULL) {
686 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
687 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
688 }
Nicholas Bellingerba159912013-07-03 03:48:24 -0700689 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000690}
Nicholas Bellingerba159912013-07-03 03:48:24 -0700691
692static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
693 unsigned char *buf)
694{
695 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
696}
697
698int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
699{
700 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
701}
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000702
703/*
704 * Map some portion of the allocated scatterlist to an iovec, suitable for
Andy Groverbfb79ea2012-04-03 15:51:29 -0700705 * kernel sockets to copy data in/out.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000706 */
707static int iscsit_map_iovec(
708 struct iscsi_cmd *cmd,
709 struct kvec *iov,
710 u32 data_offset,
711 u32 data_length)
712{
713 u32 i = 0;
714 struct scatterlist *sg;
715 unsigned int page_off;
716
717 /*
Andy Groverbfb79ea2012-04-03 15:51:29 -0700718 * We know each entry in t_data_sg contains a page.
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000719 */
Andy Groverbfb79ea2012-04-03 15:51:29 -0700720 sg = &cmd->se_cmd.t_data_sg[data_offset / PAGE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000721 page_off = (data_offset % PAGE_SIZE);
722
723 cmd->first_data_sg = sg;
724 cmd->first_data_sg_off = page_off;
725
726 while (data_length) {
727 u32 cur_len = min_t(u32, data_length, sg->length - page_off);
728
729 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
730 iov[i].iov_len = cur_len;
731
732 data_length -= cur_len;
733 page_off = 0;
734 sg = sg_next(sg);
735 i++;
736 }
737
738 cmd->kmapped_nents = i;
739
740 return i;
741}
742
743static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
744{
745 u32 i;
746 struct scatterlist *sg;
747
748 sg = cmd->first_data_sg;
749
750 for (i = 0; i < cmd->kmapped_nents; i++)
751 kunmap(sg_page(&sg[i]));
752}
753
754static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
755{
756 struct iscsi_cmd *cmd;
757
758 conn->exp_statsn = exp_statsn;
759
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800760 if (conn->sess->sess_ops->RDMAExtensions)
761 return;
762
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000763 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -0700764 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000765 spin_lock(&cmd->istate_lock);
766 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
Steve Hodgson64c133302012-11-05 18:02:41 -0800767 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000768 cmd->i_state = ISTATE_REMOVE;
769 spin_unlock(&cmd->istate_lock);
770 iscsit_add_cmd_to_immediate_queue(cmd, conn,
771 cmd->i_state);
772 continue;
773 }
774 spin_unlock(&cmd->istate_lock);
775 }
776 spin_unlock_bh(&conn->cmd_lock);
777}
778
779static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
780{
Nicholas Bellingerf80e8ed2012-05-20 17:10:29 -0700781 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000782
Christoph Hellwigc0427f12011-10-12 11:06:56 -0400783 iov_count += ISCSI_IOV_DATA_BUFFER;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000784
785 cmd->iov_data = kzalloc(iov_count * sizeof(struct kvec), GFP_KERNEL);
786 if (!cmd->iov_data) {
787 pr_err("Unable to allocate cmd->iov_data\n");
788 return -ENOMEM;
789 }
790
791 cmd->orig_iov_data_count = iov_count;
792 return 0;
793}
794
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800795int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
796 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000797{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800798 int data_direction, payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000799 struct iscsi_scsi_req *hdr;
Andy Groverd28b11692012-04-03 15:51:22 -0700800 int iscsi_task_attr;
801 int sam_task_attr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000802
803 spin_lock_bh(&conn->sess->session_stats_lock);
804 conn->sess->cmd_pdus++;
805 if (conn->sess->se_sess->se_node_acl) {
806 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
807 conn->sess->se_sess->se_node_acl->num_cmds++;
808 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
809 }
810 spin_unlock_bh(&conn->sess->session_stats_lock);
811
812 hdr = (struct iscsi_scsi_req *) buf;
813 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000814
815 /* FIXME; Add checks for AdditionalHeaderSegment */
816
817 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
818 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
819 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
820 " not set. Bad iSCSI Initiator.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700821 return iscsit_add_reject_cmd(cmd,
822 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000823 }
824
825 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
826 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
827 /*
828 * Vmware ESX v3.0 uses a modified Cisco Initiator (v3.4.2)
829 * that adds support for RESERVE/RELEASE. There is a bug
830 * add with this new functionality that sets R/W bits when
831 * neither CDB carries any READ or WRITE datapayloads.
832 */
833 if ((hdr->cdb[0] == 0x16) || (hdr->cdb[0] == 0x17)) {
834 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
835 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
836 goto done;
837 }
838
839 pr_err("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
840 " set when Expected Data Transfer Length is 0 for"
841 " CDB: 0x%02x. Bad iSCSI Initiator.\n", hdr->cdb[0]);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700842 return iscsit_add_reject_cmd(cmd,
843 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000844 }
845done:
846
847 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
848 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
849 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
850 " MUST be set if Expected Data Transfer Length is not 0."
851 " Bad iSCSI Initiator\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700852 return iscsit_add_reject_cmd(cmd,
853 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000854 }
855
856 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
857 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
858 pr_err("Bidirectional operations not supported!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700859 return iscsit_add_reject_cmd(cmd,
860 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000861 }
862
863 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
864 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
865 " Scsi Command PDU.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700866 return iscsit_add_reject_cmd(cmd,
867 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000868 }
869
870 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
871 pr_err("ImmediateData=No but DataSegmentLength=%u,"
872 " protocol error.\n", payload_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700873 return iscsit_add_reject_cmd(cmd,
874 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000875 }
876
Nicholas Bellingerba159912013-07-03 03:48:24 -0700877 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000878 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
879 pr_err("Expected Data Transfer Length and Length of"
880 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
881 " bit is not set protocol error\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -0700882 return iscsit_add_reject_cmd(cmd,
883 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000884 }
885
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400886 if (payload_length > be32_to_cpu(hdr->data_length)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000887 pr_err("DataSegmentLength: %u is greater than"
888 " EDTL: %u, protocol error.\n", payload_length,
889 hdr->data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700890 return iscsit_add_reject_cmd(cmd,
891 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000892 }
893
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700894 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000895 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -0700896 " MaxXmitDataSegmentLength: %u, protocol error.\n",
897 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700898 return iscsit_add_reject_cmd(cmd,
899 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000900 }
901
902 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
903 pr_err("DataSegmentLength: %u is greater than"
904 " FirstBurstLength: %u, protocol error.\n",
905 payload_length, conn->sess->sess_ops->FirstBurstLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -0700906 return iscsit_add_reject_cmd(cmd,
907 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000908 }
909
910 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
911 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
912 DMA_NONE;
913
Andy Groverd28b11692012-04-03 15:51:22 -0700914 cmd->data_direction = data_direction;
Andy Groverd28b11692012-04-03 15:51:22 -0700915 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
916 /*
917 * Figure out the SAM Task Attribute for the incoming SCSI CDB
918 */
919 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
920 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
921 sam_task_attr = MSG_SIMPLE_TAG;
922 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
923 sam_task_attr = MSG_ORDERED_TAG;
924 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
925 sam_task_attr = MSG_HEAD_TAG;
926 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
927 sam_task_attr = MSG_ACA_TAG;
928 else {
929 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
930 " MSG_SIMPLE_TAG\n", iscsi_task_attr);
931 sam_task_attr = MSG_SIMPLE_TAG;
932 }
933
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000934 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
935 cmd->i_state = ISTATE_NEW_CMD;
936 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
937 cmd->immediate_data = (payload_length) ? 1 : 0;
938 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
939 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
940 if (cmd->unsolicited_data)
941 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
942
943 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
944 if (hdr->flags & ISCSI_FLAG_CMD_READ) {
945 spin_lock_bh(&conn->sess->ttt_lock);
946 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
947 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
948 cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
949 spin_unlock_bh(&conn->sess->ttt_lock);
950 } else if (hdr->flags & ISCSI_FLAG_CMD_WRITE)
951 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400952 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
953 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000954 cmd->first_burst_len = payload_length;
955
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800956 if (!conn->sess->sess_ops->RDMAExtensions &&
957 cmd->data_direction == DMA_FROM_DEVICE) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000958 struct iscsi_datain_req *dr;
959
960 dr = iscsit_allocate_datain_req();
961 if (!dr)
Nicholas Bellingerba159912013-07-03 03:48:24 -0700962 return iscsit_add_reject_cmd(cmd,
963 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000964
965 iscsit_attach_datain_req(cmd, dr);
966 }
967
968 /*
Andy Grover065ca1e2012-04-03 15:51:23 -0700969 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
970 */
971 transport_init_se_cmd(&cmd->se_cmd, &lio_target_fabric_configfs->tf_ops,
Christoph Hellwig50e5c872012-09-26 08:00:40 -0400972 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
973 cmd->data_direction, sam_task_attr,
974 cmd->sense_buffer + 2);
Andy Grover065ca1e2012-04-03 15:51:23 -0700975
976 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
977 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -0800978 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
979 conn->cid);
980
981 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
Andy Grover065ca1e2012-04-03 15:51:23 -0700982
Christoph Hellwigde103c92012-11-06 12:24:09 -0800983 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd,
984 scsilun_to_int(&hdr->lun));
985 if (cmd->sense_reason)
986 goto attach_cmd;
987
988 cmd->sense_reason = target_setup_cmd_from_cdb(&cmd->se_cmd, hdr->cdb);
989 if (cmd->sense_reason) {
990 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
Nicholas Bellingerba159912013-07-03 03:48:24 -0700991 return iscsit_add_reject_cmd(cmd,
992 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000993 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800994
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000995 goto attach_cmd;
996 }
Andy Grovera12f41f2012-04-03 15:51:20 -0700997
Christoph Hellwigde103c92012-11-06 12:24:09 -0800998 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -0700999 return iscsit_add_reject_cmd(cmd,
1000 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001001 }
1002
1003attach_cmd:
1004 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001005 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001006 spin_unlock_bh(&conn->cmd_lock);
1007 /*
1008 * Check if we need to delay processing because of ALUA
1009 * Active/NonOptimized primary access state..
1010 */
1011 core_alua_check_nonop_delay(&cmd->se_cmd);
Andy Groverbfb79ea2012-04-03 15:51:29 -07001012
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001013 return 0;
1014}
1015EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001016
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001017void iscsit_set_unsoliticed_dataout(struct iscsi_cmd *cmd)
1018{
1019 iscsit_set_dataout_sequence_values(cmd);
1020
1021 spin_lock_bh(&cmd->dataout_timeout_lock);
1022 iscsit_start_dataout_timer(cmd, cmd->conn);
1023 spin_unlock_bh(&cmd->dataout_timeout_lock);
1024}
1025EXPORT_SYMBOL(iscsit_set_unsoliticed_dataout);
1026
1027int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1028 struct iscsi_scsi_req *hdr)
1029{
1030 int cmdsn_ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001031 /*
1032 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1033 * the Immediate Bit is not set, and no Immediate
1034 * Data is attached.
1035 *
1036 * A PDU/CmdSN carrying Immediate Data can only
1037 * be processed after the DataCRC has passed.
1038 * If the DataCRC fails, the CmdSN MUST NOT
1039 * be acknowledged. (See below)
1040 */
1041 if (!cmd->immediate_data) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001042 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1043 (unsigned char *)hdr, hdr->cmdsn);
1044 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1045 return -1;
1046 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001047 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellinger7e32da52011-10-28 13:32:35 -07001048 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001049 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001050 }
1051
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001052 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001053
1054 /*
1055 * If no Immediate Data is attached, it's OK to return now.
1056 */
1057 if (!cmd->immediate_data) {
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001058 if (!cmd->sense_reason && cmd->unsolicited_data)
1059 iscsit_set_unsoliticed_dataout(cmd);
1060 if (!cmd->sense_reason)
1061 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001062
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001063 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001064 return 0;
1065 }
1066
1067 /*
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001068 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1069 * execution. These exceptions are processed in CmdSN order using
1070 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001071 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001072 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001073 if (cmd->reject_reason)
1074 return 0;
1075
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001076 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1077 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001078 }
1079 /*
1080 * Call directly into transport_generic_new_cmd() to perform
1081 * the backend memory allocation.
1082 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001083 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001084 if (cmd->sense_reason)
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001085 return 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001086
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001087 return 0;
1088}
1089EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1090
1091static int
1092iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1093 bool dump_payload)
1094{
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001095 struct iscsi_conn *conn = cmd->conn;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001096 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1097 /*
1098 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1099 */
1100 if (dump_payload == true)
1101 goto after_immediate_data;
1102
1103 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1104 cmd->first_burst_len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001105after_immediate_data:
1106 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1107 /*
1108 * A PDU/CmdSN carrying Immediate Data passed
1109 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1110 * Immediate Bit is not set.
1111 */
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001112 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1113 (unsigned char *)hdr, hdr->cmdsn);
1114 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
1115 return -1;
1116 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1117 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1118 return 0;
1119 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001120
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001121 if (cmd->sense_reason) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001122 int rc;
1123
1124 rc = iscsit_dump_data_payload(cmd->conn,
1125 cmd->first_burst_len, 1);
1126 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1127 return rc;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001128 } else if (cmd->unsolicited_data)
1129 iscsit_set_unsoliticed_dataout(cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001130
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001131 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1132 /*
1133 * Immediate Data failed DataCRC and ERL>=1,
1134 * silently drop this PDU and let the initiator
1135 * plug the CmdSN gap.
1136 *
1137 * FIXME: Send Unsolicited NOPIN with reserved
1138 * TTT here to help the initiator figure out
1139 * the missing CmdSN, although they should be
1140 * intelligent enough to determine the missing
1141 * CmdSN and issue a retry to plug the sequence.
1142 */
1143 cmd->i_state = ISTATE_REMOVE;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001144 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001145 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1146 return -1;
1147
1148 return 0;
1149}
1150
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001151static int
1152iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1153 unsigned char *buf)
1154{
1155 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1156 int rc, immed_data;
1157 bool dump_payload = false;
1158
1159 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1160 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001161 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001162 /*
1163 * Allocation iovecs needed for struct socket operations for
1164 * traditional iSCSI block I/O.
1165 */
1166 if (iscsit_allocate_iovecs(cmd) < 0) {
Nicholas Bellingerba159912013-07-03 03:48:24 -07001167 return iscsit_add_reject_cmd(cmd,
1168 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001169 }
1170 immed_data = cmd->immediate_data;
1171
1172 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1173 if (rc < 0)
1174 return rc;
1175 else if (rc > 0)
1176 dump_payload = true;
1177
1178 if (!immed_data)
1179 return 0;
1180
1181 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1182}
1183
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001184static u32 iscsit_do_crypto_hash_sg(
1185 struct hash_desc *hash,
1186 struct iscsi_cmd *cmd,
1187 u32 data_offset,
1188 u32 data_length,
1189 u32 padding,
1190 u8 *pad_bytes)
1191{
1192 u32 data_crc;
1193 u32 i;
1194 struct scatterlist *sg;
1195 unsigned int page_off;
1196
1197 crypto_hash_init(hash);
1198
1199 sg = cmd->first_data_sg;
1200 page_off = cmd->first_data_sg_off;
1201
1202 i = 0;
1203 while (data_length) {
1204 u32 cur_len = min_t(u32, data_length, (sg[i].length - page_off));
1205
1206 crypto_hash_update(hash, &sg[i], cur_len);
1207
1208 data_length -= cur_len;
1209 page_off = 0;
1210 i++;
1211 }
1212
1213 if (padding) {
1214 struct scatterlist pad_sg;
1215
1216 sg_init_one(&pad_sg, pad_bytes, padding);
1217 crypto_hash_update(hash, &pad_sg, padding);
1218 }
1219 crypto_hash_final(hash, (u8 *) &data_crc);
1220
1221 return data_crc;
1222}
1223
1224static void iscsit_do_crypto_hash_buf(
1225 struct hash_desc *hash,
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02001226 const void *buf,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001227 u32 payload_length,
1228 u32 padding,
1229 u8 *pad_bytes,
1230 u8 *data_crc)
1231{
1232 struct scatterlist sg;
1233
1234 crypto_hash_init(hash);
1235
Jörn Engel8359cf42011-11-24 02:05:51 +01001236 sg_init_one(&sg, buf, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001237 crypto_hash_update(hash, &sg, payload_length);
1238
1239 if (padding) {
1240 sg_init_one(&sg, pad_bytes, padding);
1241 crypto_hash_update(hash, &sg, padding);
1242 }
1243 crypto_hash_final(hash, data_crc);
1244}
1245
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001246int
1247iscsit_check_dataout_hdr(struct iscsi_conn *conn, unsigned char *buf,
1248 struct iscsi_cmd **out_cmd)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001249{
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001250 struct iscsi_data *hdr = (struct iscsi_data *)buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001251 struct iscsi_cmd *cmd = NULL;
1252 struct se_cmd *se_cmd;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001253 u32 payload_length = ntoh24(hdr->dlength);
1254 int rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001255
1256 if (!payload_length) {
1257 pr_err("DataOUT payload is ZERO, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001258 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1259 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001260 }
1261
1262 /* iSCSI write */
1263 spin_lock_bh(&conn->sess->session_stats_lock);
1264 conn->sess->rx_data_octets += payload_length;
1265 if (conn->sess->se_sess->se_node_acl) {
1266 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
1267 conn->sess->se_sess->se_node_acl->write_bytes += payload_length;
1268 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
1269 }
1270 spin_unlock_bh(&conn->sess->session_stats_lock);
1271
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001272 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001273 pr_err("DataSegmentLength: %u is greater than"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001274 " MaxXmitDataSegmentLength: %u\n", payload_length,
1275 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001276 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1277 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001278 }
1279
1280 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt,
1281 payload_length);
1282 if (!cmd)
1283 return 0;
1284
1285 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1286 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001287 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001288 payload_length, conn->cid);
1289
1290 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1291 pr_err("Command ITT: 0x%08x received DataOUT after"
1292 " last DataOUT received, dumping payload\n",
1293 cmd->init_task_tag);
1294 return iscsit_dump_data_payload(conn, payload_length, 1);
1295 }
1296
1297 if (cmd->data_direction != DMA_TO_DEVICE) {
1298 pr_err("Command ITT: 0x%08x received DataOUT for a"
1299 " NON-WRITE command.\n", cmd->init_task_tag);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001300 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001301 }
1302 se_cmd = &cmd->se_cmd;
1303 iscsit_mod_dataout_timer(cmd);
1304
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001305 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001306 pr_err("DataOut Offset: %u, Length %u greater than"
1307 " iSCSI Command EDTL %u, protocol error.\n",
Andy Groverebf1d952012-04-03 15:51:24 -07001308 hdr->offset, payload_length, cmd->se_cmd.data_length);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001309 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001310 }
1311
1312 if (cmd->unsolicited_data) {
1313 int dump_unsolicited_data = 0;
1314
1315 if (conn->sess->sess_ops->InitialR2T) {
1316 pr_err("Received unexpected unsolicited data"
1317 " while InitialR2T=Yes, protocol error.\n");
1318 transport_send_check_condition_and_sense(&cmd->se_cmd,
1319 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1320 return -1;
1321 }
1322 /*
1323 * Special case for dealing with Unsolicited DataOUT
1324 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1325 * failures;
1326 */
1327
1328 /* Something's amiss if we're not in WRITE_PENDING state... */
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001329 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001330 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001331 dump_unsolicited_data = 1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001332
1333 if (dump_unsolicited_data) {
1334 /*
1335 * Check if a delayed TASK_ABORTED status needs to
1336 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1337 * received with the unsolicitied data out.
1338 */
1339 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1340 iscsit_stop_dataout_timer(cmd);
1341
1342 transport_check_aborted_status(se_cmd,
1343 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
1344 return iscsit_dump_data_payload(conn, payload_length, 1);
1345 }
1346 } else {
1347 /*
1348 * For the normal solicited data path:
1349 *
1350 * Check for a delayed TASK_ABORTED status and dump any
1351 * incoming data out payload if one exists. Also, when the
1352 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1353 * data out sequence, we decrement outstanding_r2ts. Once
1354 * outstanding_r2ts reaches zero, go ahead and send the delayed
1355 * TASK_ABORTED status.
1356 */
Christoph Hellwig7d680f32011-12-21 14:13:47 -05001357 if (se_cmd->transport_state & CMD_T_ABORTED) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001358 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1359 if (--cmd->outstanding_r2ts < 1) {
1360 iscsit_stop_dataout_timer(cmd);
1361 transport_check_aborted_status(
1362 se_cmd, 1);
1363 }
1364
1365 return iscsit_dump_data_payload(conn, payload_length, 1);
1366 }
1367 }
1368 /*
1369 * Preform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1370 * within-command recovery checks before receiving the payload.
1371 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001372 rc = iscsit_check_pre_dataout(cmd, buf);
1373 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001374 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001375 else if (rc == DATAOUT_CANNOT_RECOVER)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001376 return -1;
1377
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001378 *out_cmd = cmd;
1379 return 0;
1380}
1381EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1382
1383static int
1384iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1385 struct iscsi_data *hdr)
1386{
1387 struct kvec *iov;
1388 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1389 u32 payload_length = ntoh24(hdr->dlength);
1390 int iov_ret, data_crc_failed = 0;
1391
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001392 rx_size += payload_length;
1393 iov = &cmd->iov_data[0];
1394
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001395 iov_ret = iscsit_map_iovec(cmd, iov, be32_to_cpu(hdr->offset),
1396 payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001397 if (iov_ret < 0)
1398 return -1;
1399
1400 iov_count += iov_ret;
1401
1402 padding = ((-payload_length) & 3);
1403 if (padding != 0) {
1404 iov[iov_count].iov_base = cmd->pad_bytes;
1405 iov[iov_count++].iov_len = padding;
1406 rx_size += padding;
1407 pr_debug("Receiving %u padding bytes.\n", padding);
1408 }
1409
1410 if (conn->conn_ops->DataDigest) {
1411 iov[iov_count].iov_base = &checksum;
1412 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1413 rx_size += ISCSI_CRC_LEN;
1414 }
1415
1416 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1417
1418 iscsit_unmap_iovec(cmd);
1419
1420 if (rx_got != rx_size)
1421 return -1;
1422
1423 if (conn->conn_ops->DataDigest) {
1424 u32 data_crc;
1425
1426 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001427 be32_to_cpu(hdr->offset),
1428 payload_length, padding,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001429 cmd->pad_bytes);
1430
1431 if (checksum != data_crc) {
1432 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1433 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1434 " does not match computed 0x%08x\n",
1435 hdr->itt, hdr->offset, payload_length,
1436 hdr->datasn, checksum, data_crc);
1437 data_crc_failed = 1;
1438 } else {
1439 pr_debug("Got CRC32C DataDigest 0x%08x for"
1440 " %u bytes of Data Out\n", checksum,
1441 payload_length);
1442 }
1443 }
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001444
1445 return data_crc_failed;
1446}
1447
1448int
1449iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1450 bool data_crc_failed)
1451{
1452 struct iscsi_conn *conn = cmd->conn;
1453 int rc, ooo_cmdsn;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001454 /*
1455 * Increment post receive data and CRC values or perform
1456 * within-command recovery.
1457 */
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001458 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1459 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001460 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001461 else if (rc == DATAOUT_SEND_R2T) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001462 iscsit_set_dataout_sequence_values(cmd);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001463 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1464 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001465 /*
1466 * Handle extra special case for out of order
1467 * Unsolicited Data Out.
1468 */
1469 spin_lock_bh(&cmd->istate_lock);
1470 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1471 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1472 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1473 spin_unlock_bh(&cmd->istate_lock);
1474
1475 iscsit_stop_dataout_timer(cmd);
Christoph Hellwig67441b62012-07-08 15:58:42 -04001476 if (ooo_cmdsn)
1477 return 0;
1478 target_execute_cmd(&cmd->se_cmd);
1479 return 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001480 } else /* DATAOUT_CANNOT_RECOVER */
1481 return -1;
1482
1483 return 0;
1484}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001485EXPORT_SYMBOL(iscsit_check_dataout_payload);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001486
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001487static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1488{
1489 struct iscsi_cmd *cmd;
1490 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1491 int rc;
1492 bool data_crc_failed = false;
1493
1494 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1495 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001496 return 0;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001497 else if (!cmd)
1498 return 0;
1499
1500 rc = iscsit_get_dataout(conn, cmd, hdr);
1501 if (rc < 0)
1502 return rc;
1503 else if (rc > 0)
1504 data_crc_failed = true;
1505
1506 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1507}
1508
Nicholas Bellinger778de362013-06-14 16:07:47 -07001509int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1510 struct iscsi_nopout *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001511{
Nicholas Bellinger778de362013-06-14 16:07:47 -07001512 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001513
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001514 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001515 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1516 " not set, protocol error.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001517 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1518 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001519 }
1520
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001521 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001522 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001523 " greater than MaxXmitDataSegmentLength: %u, protocol"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001524 " error.\n", payload_length,
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001525 conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001526 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1527 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001528 }
1529
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001530 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001531 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001532 hdr->itt == RESERVED_ITT ? "Response" : "Request",
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001533 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1534 payload_length);
1535 /*
1536 * This is not a response to a Unsolicited NopIN, which means
1537 * it can either be a NOPOUT ping request (with a valid ITT),
1538 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1539 * Either way, make sure we allocate an struct iscsi_cmd, as both
1540 * can contain ping data.
1541 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001542 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001543 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1544 cmd->i_state = ISTATE_SEND_NOPIN;
1545 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1546 1 : 0);
1547 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1548 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001549 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1550 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001551 cmd->data_direction = DMA_NONE;
1552 }
1553
Nicholas Bellinger778de362013-06-14 16:07:47 -07001554 return 0;
1555}
1556EXPORT_SYMBOL(iscsit_setup_nop_out);
1557
1558int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1559 struct iscsi_nopout *hdr)
1560{
1561 struct iscsi_cmd *cmd_p = NULL;
1562 int cmdsn_ret = 0;
1563 /*
1564 * Initiator is expecting a NopIN ping reply..
1565 */
1566 if (hdr->itt != RESERVED_ITT) {
1567 BUG_ON(!cmd);
1568
1569 spin_lock_bh(&conn->cmd_lock);
1570 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1571 spin_unlock_bh(&conn->cmd_lock);
1572
1573 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1574
1575 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1576 iscsit_add_cmd_to_response_queue(cmd, conn,
1577 cmd->i_state);
1578 return 0;
1579 }
1580
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001581 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1582 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001583 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1584 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001585 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001586 return -1;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001587
1588 return 0;
1589 }
1590 /*
1591 * This was a response to a unsolicited NOPIN ping.
1592 */
1593 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1594 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1595 if (!cmd_p)
1596 return -EINVAL;
1597
1598 iscsit_stop_nopin_response_timer(conn);
1599
1600 cmd_p->i_state = ISTATE_REMOVE;
1601 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1602
1603 iscsit_start_nopin_timer(conn);
1604 return 0;
1605 }
1606 /*
1607 * Otherwise, initiator is not expecting a NOPIN is response.
1608 * Just ignore for now.
1609 */
1610 return 0;
1611}
1612EXPORT_SYMBOL(iscsit_process_nop_out);
1613
1614static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1615 unsigned char *buf)
1616{
1617 unsigned char *ping_data = NULL;
1618 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1619 struct kvec *iov = NULL;
1620 u32 payload_length = ntoh24(hdr->dlength);
1621 int ret;
1622
1623 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1624 if (ret < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001625 return 0;
Nicholas Bellinger778de362013-06-14 16:07:47 -07001626 /*
1627 * Handle NOP-OUT payload for traditional iSCSI sockets
1628 */
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001629 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
Nicholas Bellinger778de362013-06-14 16:07:47 -07001630 u32 checksum, data_crc, padding = 0;
1631 int niov = 0, rx_got, rx_size = payload_length;
1632
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001633 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1634 if (!ping_data) {
1635 pr_err("Unable to allocate memory for"
1636 " NOPOUT ping data.\n");
1637 ret = -1;
1638 goto out;
1639 }
1640
1641 iov = &cmd->iov_misc[0];
1642 iov[niov].iov_base = ping_data;
1643 iov[niov++].iov_len = payload_length;
1644
1645 padding = ((-payload_length) & 3);
1646 if (padding != 0) {
1647 pr_debug("Receiving %u additional bytes"
1648 " for padding.\n", padding);
1649 iov[niov].iov_base = &cmd->pad_bytes;
1650 iov[niov++].iov_len = padding;
1651 rx_size += padding;
1652 }
1653 if (conn->conn_ops->DataDigest) {
1654 iov[niov].iov_base = &checksum;
1655 iov[niov++].iov_len = ISCSI_CRC_LEN;
1656 rx_size += ISCSI_CRC_LEN;
1657 }
1658
1659 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1660 if (rx_got != rx_size) {
1661 ret = -1;
1662 goto out;
1663 }
1664
1665 if (conn->conn_ops->DataDigest) {
1666 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
1667 ping_data, payload_length,
1668 padding, cmd->pad_bytes,
1669 (u8 *)&data_crc);
1670
1671 if (checksum != data_crc) {
1672 pr_err("Ping data CRC32C DataDigest"
1673 " 0x%08x does not match computed 0x%08x\n",
1674 checksum, data_crc);
1675 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1676 pr_err("Unable to recover from"
1677 " NOPOUT Ping DataCRC failure while in"
1678 " ERL=0.\n");
1679 ret = -1;
1680 goto out;
1681 } else {
1682 /*
1683 * Silently drop this PDU and let the
1684 * initiator plug the CmdSN gap.
1685 */
1686 pr_debug("Dropping NOPOUT"
1687 " Command CmdSN: 0x%08x due to"
1688 " DataCRC error.\n", hdr->cmdsn);
1689 ret = 0;
1690 goto out;
1691 }
1692 } else {
1693 pr_debug("Got CRC32C DataDigest"
1694 " 0x%08x for %u bytes of ping data.\n",
1695 checksum, payload_length);
1696 }
1697 }
1698
1699 ping_data[payload_length] = '\0';
1700 /*
1701 * Attach ping data to struct iscsi_cmd->buf_ptr.
1702 */
Jörn Engel8359cf42011-11-24 02:05:51 +01001703 cmd->buf_ptr = ping_data;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001704 cmd->buf_ptr_size = payload_length;
1705
1706 pr_debug("Got %u bytes of NOPOUT ping"
1707 " data.\n", payload_length);
1708 pr_debug("Ping Data: \"%s\"\n", ping_data);
1709 }
1710
Nicholas Bellinger778de362013-06-14 16:07:47 -07001711 return iscsit_process_nop_out(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001712out:
1713 if (cmd)
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07001714 iscsit_free_cmd(cmd, false);
Nicholas Bellinger778de362013-06-14 16:07:47 -07001715
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001716 kfree(ping_data);
1717 return ret;
1718}
1719
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001720int
1721iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1722 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001723{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001724 struct se_tmr_req *se_tmr;
1725 struct iscsi_tmr_req *tmr_req;
1726 struct iscsi_tm *hdr;
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001727 int out_of_order_cmdsn = 0, ret;
1728 bool sess_ref = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001729 u8 function;
1730
1731 hdr = (struct iscsi_tm *) buf;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001732 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1733 function = hdr->flags;
1734
1735 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1736 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1737 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1738 hdr->rtt, hdr->refcmdsn, conn->cid);
1739
1740 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1741 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001742 hdr->rtt != RESERVED_ITT)) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001743 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
Christoph Hellwig66c7db62012-09-26 08:00:39 -04001744 hdr->rtt = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001745 }
1746
1747 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1748 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1749 pr_err("Task Management Request TASK_REASSIGN not"
1750 " issued as immediate command, bad iSCSI Initiator"
1751 "implementation\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001752 return iscsit_add_reject_cmd(cmd,
1753 ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001754 }
1755 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001756 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
1757 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001758
Andy Groverd28b11692012-04-03 15:51:22 -07001759 cmd->data_direction = DMA_NONE;
1760
1761 cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
1762 if (!cmd->tmr_req) {
1763 pr_err("Unable to allocate memory for"
1764 " Task Management command!\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07001765 return iscsit_add_reject_cmd(cmd,
1766 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
1767 buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001768 }
1769
1770 /*
1771 * TASK_REASSIGN for ERL=2 / connection stays inside of
1772 * LIO-Target $FABRIC_MOD
1773 */
1774 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
1775
1776 u8 tcm_function;
1777 int ret;
1778
1779 transport_init_se_cmd(&cmd->se_cmd,
1780 &lio_target_fabric_configfs->tf_ops,
1781 conn->sess->se_sess, 0, DMA_NONE,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07001782 MSG_SIMPLE_TAG, cmd->sense_buffer + 2);
Andy Groverd28b11692012-04-03 15:51:22 -07001783
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001784 target_get_sess_cmd(conn->sess->se_sess, &cmd->se_cmd, true);
1785 sess_ref = true;
1786
Andy Groverd28b11692012-04-03 15:51:22 -07001787 switch (function) {
1788 case ISCSI_TM_FUNC_ABORT_TASK:
1789 tcm_function = TMR_ABORT_TASK;
1790 break;
1791 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1792 tcm_function = TMR_ABORT_TASK_SET;
1793 break;
1794 case ISCSI_TM_FUNC_CLEAR_ACA:
1795 tcm_function = TMR_CLEAR_ACA;
1796 break;
1797 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1798 tcm_function = TMR_CLEAR_TASK_SET;
1799 break;
1800 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1801 tcm_function = TMR_LUN_RESET;
1802 break;
1803 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1804 tcm_function = TMR_TARGET_WARM_RESET;
1805 break;
1806 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1807 tcm_function = TMR_TARGET_COLD_RESET;
1808 break;
1809 default:
1810 pr_err("Unknown iSCSI TMR Function:"
1811 " 0x%02x\n", function);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001812 return iscsit_add_reject_cmd(cmd,
1813 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001814 }
1815
1816 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req,
1817 tcm_function, GFP_KERNEL);
1818 if (ret < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001819 return iscsit_add_reject_cmd(cmd,
1820 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Andy Groverd28b11692012-04-03 15:51:22 -07001821
1822 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
1823 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001824
1825 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
1826 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
1827 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1828 cmd->init_task_tag = hdr->itt;
1829 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001830 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1831 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001832 se_tmr = cmd->se_cmd.se_tmr_req;
1833 tmr_req = cmd->tmr_req;
1834 /*
1835 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
1836 */
1837 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
Andy Grover4f269982012-01-19 13:39:14 -08001838 ret = transport_lookup_tmr_lun(&cmd->se_cmd,
1839 scsilun_to_int(&hdr->lun));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001840 if (ret < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001841 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
1842 goto attach;
1843 }
1844 }
1845
1846 switch (function) {
1847 case ISCSI_TM_FUNC_ABORT_TASK:
1848 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
Christoph Hellwigde103c92012-11-06 12:24:09 -08001849 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001850 goto attach;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001851 break;
1852 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1853 case ISCSI_TM_FUNC_CLEAR_ACA:
1854 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1855 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1856 break;
1857 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1858 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001859 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1860 goto attach;
1861 }
1862 break;
1863 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1864 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001865 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
1866 goto attach;
1867 }
1868 break;
1869 case ISCSI_TM_FUNC_TASK_REASSIGN:
1870 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
1871 /*
1872 * Perform sanity checks on the ExpDataSN only if the
1873 * TASK_REASSIGN was successful.
1874 */
Christoph Hellwigde103c92012-11-06 12:24:09 -08001875 if (se_tmr->response)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001876 break;
1877
1878 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001879 return iscsit_add_reject_cmd(cmd,
1880 ISCSI_REASON_BOOKMARK_INVALID, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001881 break;
1882 default:
1883 pr_err("Unknown TMR function: 0x%02x, protocol"
1884 " error.\n", function);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001885 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
1886 goto attach;
1887 }
1888
1889 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1890 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
1891 se_tmr->call_transport = 1;
1892attach:
1893 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07001894 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001895 spin_unlock_bh(&conn->cmd_lock);
1896
1897 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07001898 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001899 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP)
1900 out_of_order_cmdsn = 1;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001901 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001902 return 0;
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001903 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07001904 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001905 }
Christoph Hellwig50e5c872012-09-26 08:00:40 -04001906 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001907
Nicholas Bellinger5a4c8662011-10-28 13:37:19 -07001908 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001909 return 0;
1910 /*
1911 * Found the referenced task, send to transport for processing.
1912 */
1913 if (se_tmr->call_transport)
1914 return transport_generic_handle_tmr(&cmd->se_cmd);
1915
1916 /*
1917 * Could not find the referenced LUN, task, or Task Management
1918 * command not authorized or supported. Change state and
1919 * let the tx_thread send the response.
1920 *
1921 * For connection recovery, this is also the default action for
1922 * TMR TASK_REASSIGN.
1923 */
Nicholas Bellinger186a9642013-07-03 03:11:48 -07001924 if (sess_ref) {
1925 pr_debug("Handle TMR, using sess_ref=true check\n");
1926 target_put_sess_cmd(conn->sess->se_sess, &cmd->se_cmd);
1927 }
1928
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001929 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
1930 return 0;
1931}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08001932EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001933
1934/* #warning FIXME: Support Text Command parameters besides SendTargets */
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001935int
1936iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1937 struct iscsi_text *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001938{
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001939 u32 payload_length = ntoh24(hdr->dlength);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001940
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001941 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001942 pr_err("Unable to accept text parameter length: %u"
Nicholas Bellinger21f5aa72012-09-29 21:51:26 -07001943 "greater than MaxXmitDataSegmentLength %u.\n",
1944 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
Nicholas Bellingerba159912013-07-03 03:48:24 -07001945 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1946 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001947 }
1948
1949 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
1950 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
1951 hdr->exp_statsn, payload_length);
1952
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001953 cmd->iscsi_opcode = ISCSI_OP_TEXT;
1954 cmd->i_state = ISTATE_SEND_TEXTRSP;
1955 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1956 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1957 cmd->targ_xfer_tag = 0xFFFFFFFF;
1958 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1959 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1960 cmd->data_direction = DMA_NONE;
1961
1962 return 0;
1963}
1964EXPORT_SYMBOL(iscsit_setup_text_cmd);
1965
1966int
1967iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1968 struct iscsi_text *hdr)
1969{
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001970 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001971 int cmdsn_ret;
1972
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001973 if (!text_in) {
1974 pr_err("Unable to locate text_in buffer for sendtargets"
1975 " discovery\n");
1976 goto reject;
1977 }
1978 if (strncmp("SendTargets", text_in, 11) != 0) {
1979 pr_err("Received Text Data that is not"
1980 " SendTargets, cannot continue.\n");
1981 goto reject;
1982 }
1983 text_ptr = strchr(text_in, '=');
1984 if (!text_ptr) {
1985 pr_err("No \"=\" separator found in Text Data,"
1986 " cannot continue.\n");
1987 goto reject;
1988 }
1989 if (!strncmp("=All", text_ptr, 4)) {
1990 cmd->cmd_flags |= IFC_SENDTARGETS_ALL;
Nicholas Bellinger66658892013-06-19 22:45:42 -07001991 } else if (!strncmp("=iqn.", text_ptr, 5) ||
1992 !strncmp("=eui.", text_ptr, 5)) {
1993 cmd->cmd_flags |= IFC_SENDTARGETS_SINGLE;
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07001994 } else {
1995 pr_err("Unable to locate valid SendTargets=%s value\n", text_ptr);
1996 goto reject;
1997 }
1998
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07001999 spin_lock_bh(&conn->cmd_lock);
2000 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2001 spin_unlock_bh(&conn->cmd_lock);
2002
2003 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2004
2005 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002006 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2007 (unsigned char *)hdr, hdr->cmdsn);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002008 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
Nicholas Bellingerba159912013-07-03 03:48:24 -07002009 return -1;
2010
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002011 return 0;
2012 }
2013
2014 return iscsit_execute_cmd(cmd, 0);
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002015
2016reject:
Nicholas Bellingerba159912013-07-03 03:48:24 -07002017 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2018 (unsigned char *)hdr);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002019}
2020EXPORT_SYMBOL(iscsit_process_text_cmd);
2021
2022static int
2023iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2024 unsigned char *buf)
2025{
2026 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2027 char *text_in = NULL;
2028 u32 payload_length = ntoh24(hdr->dlength);
2029 int rx_size, rc;
2030
2031 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2032 if (rc < 0)
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002033 return 0;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002034
2035 rx_size = payload_length;
2036 if (payload_length) {
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002037 u32 checksum = 0, data_crc = 0;
2038 u32 padding = 0, pad_bytes = 0;
2039 int niov = 0, rx_got;
2040 struct kvec iov[3];
2041
2042 text_in = kzalloc(payload_length, GFP_KERNEL);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002043 if (!text_in) {
2044 pr_err("Unable to allocate memory for"
2045 " incoming text parameters\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002046 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002047 }
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002048 cmd->text_in_ptr = text_in;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002049
2050 memset(iov, 0, 3 * sizeof(struct kvec));
2051 iov[niov].iov_base = text_in;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002052 iov[niov++].iov_len = payload_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002053
2054 padding = ((-payload_length) & 3);
2055 if (padding != 0) {
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002056 iov[niov].iov_base = &pad_bytes;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002057 iov[niov++].iov_len = padding;
2058 rx_size += padding;
2059 pr_debug("Receiving %u additional bytes"
2060 " for padding.\n", padding);
2061 }
2062 if (conn->conn_ops->DataDigest) {
2063 iov[niov].iov_base = &checksum;
2064 iov[niov++].iov_len = ISCSI_CRC_LEN;
2065 rx_size += ISCSI_CRC_LEN;
2066 }
2067
2068 rx_got = rx_data(conn, &iov[0], niov, rx_size);
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002069 if (rx_got != rx_size)
2070 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002071
2072 if (conn->conn_ops->DataDigest) {
2073 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002074 text_in, payload_length,
Nicholas Bellinger76f19282011-07-27 12:16:22 -07002075 padding, (u8 *)&pad_bytes,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002076 (u8 *)&data_crc);
2077
2078 if (checksum != data_crc) {
2079 pr_err("Text data CRC32C DataDigest"
2080 " 0x%08x does not match computed"
2081 " 0x%08x\n", checksum, data_crc);
2082 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2083 pr_err("Unable to recover from"
2084 " Text Data digest failure while in"
2085 " ERL=0.\n");
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002086 goto reject;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002087 } else {
2088 /*
2089 * Silently drop this PDU and let the
2090 * initiator plug the CmdSN gap.
2091 */
2092 pr_debug("Dropping Text"
2093 " Command CmdSN: 0x%08x due to"
2094 " DataCRC error.\n", hdr->cmdsn);
2095 kfree(text_in);
2096 return 0;
2097 }
2098 } else {
2099 pr_debug("Got CRC32C DataDigest"
2100 " 0x%08x for %u bytes of text data.\n",
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002101 checksum, payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002102 }
2103 }
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002104 text_in[payload_length - 1] = '\0';
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002105 pr_debug("Successfully read %d bytes of text"
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002106 " data.\n", payload_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002107 }
2108
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002109 return iscsit_process_text_cmd(conn, cmd, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002110
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002111reject:
Nicholas Bellinger9864ca92013-06-19 22:43:11 -07002112 kfree(cmd->text_in_ptr);
2113 cmd->text_in_ptr = NULL;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002114 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002115}
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07002116EXPORT_SYMBOL(iscsit_handle_text_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002117
2118int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2119{
2120 struct iscsi_conn *conn_p;
2121 struct iscsi_session *sess = conn->sess;
2122
2123 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2124 " for SID: %u.\n", conn->cid, conn->sess->sid);
2125
2126 atomic_set(&sess->session_logout, 1);
2127 atomic_set(&conn->conn_logout_remove, 1);
2128 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2129
2130 iscsit_inc_conn_usage_count(conn);
2131 iscsit_inc_session_usage_count(sess);
2132
2133 spin_lock_bh(&sess->conn_lock);
2134 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2135 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2136 continue;
2137
2138 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2139 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2140 }
2141 spin_unlock_bh(&sess->conn_lock);
2142
2143 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2144
2145 return 0;
2146}
2147
2148int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2149{
2150 struct iscsi_conn *l_conn;
2151 struct iscsi_session *sess = conn->sess;
2152
2153 pr_debug("Received logout request CLOSECONNECTION for CID:"
2154 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2155
2156 /*
2157 * A Logout Request with a CLOSECONNECTION reason code for a CID
2158 * can arrive on a connection with a differing CID.
2159 */
2160 if (conn->cid == cmd->logout_cid) {
2161 spin_lock_bh(&conn->state_lock);
2162 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2163 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2164
2165 atomic_set(&conn->conn_logout_remove, 1);
2166 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2167 iscsit_inc_conn_usage_count(conn);
2168
2169 spin_unlock_bh(&conn->state_lock);
2170 } else {
2171 /*
2172 * Handle all different cid CLOSECONNECTION requests in
2173 * iscsit_logout_post_handler_diffcid() as to give enough
2174 * time for any non immediate command's CmdSN to be
2175 * acknowledged on the connection in question.
2176 *
2177 * Here we simply make sure the CID is still around.
2178 */
2179 l_conn = iscsit_get_conn_from_cid(sess,
2180 cmd->logout_cid);
2181 if (!l_conn) {
2182 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2183 iscsit_add_cmd_to_response_queue(cmd, conn,
2184 cmd->i_state);
2185 return 0;
2186 }
2187
2188 iscsit_dec_conn_usage_count(l_conn);
2189 }
2190
2191 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2192
2193 return 0;
2194}
2195
2196int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2197{
2198 struct iscsi_session *sess = conn->sess;
2199
2200 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2201 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2202
2203 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2204 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2205 " while ERL!=2.\n");
2206 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2207 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2208 return 0;
2209 }
2210
2211 if (conn->cid == cmd->logout_cid) {
2212 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2213 " with CID: %hu on CID: %hu, implementation error.\n",
2214 cmd->logout_cid, conn->cid);
2215 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2216 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2217 return 0;
2218 }
2219
2220 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2221
2222 return 0;
2223}
2224
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002225int
2226iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2227 unsigned char *buf)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002228{
2229 int cmdsn_ret, logout_remove = 0;
2230 u8 reason_code = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002231 struct iscsi_logout *hdr;
2232 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2233
2234 hdr = (struct iscsi_logout *) buf;
2235 reason_code = (hdr->flags & 0x7f);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002236
2237 if (tiqn) {
2238 spin_lock(&tiqn->logout_stats.lock);
2239 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2240 tiqn->logout_stats.normal_logouts++;
2241 else
2242 tiqn->logout_stats.abnormal_logouts++;
2243 spin_unlock(&tiqn->logout_stats.lock);
2244 }
2245
2246 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2247 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2248 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2249 hdr->cid, conn->cid);
2250
2251 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2252 pr_err("Received logout request on connection that"
2253 " is not in logged in state, ignoring request.\n");
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07002254 iscsit_free_cmd(cmd, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002255 return 0;
2256 }
2257
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002258 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2259 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2260 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2261 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2262 cmd->targ_xfer_tag = 0xFFFFFFFF;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002263 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2264 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2265 cmd->logout_cid = be16_to_cpu(hdr->cid);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002266 cmd->logout_reason = reason_code;
2267 cmd->data_direction = DMA_NONE;
2268
2269 /*
2270 * We need to sleep in these cases (by returning 1) until the Logout
2271 * Response gets sent in the tx thread.
2272 */
2273 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2274 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002275 be16_to_cpu(hdr->cid) == conn->cid))
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002276 logout_remove = 1;
2277
2278 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002279 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002280 spin_unlock_bh(&conn->cmd_lock);
2281
2282 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002283 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002284
2285 /*
2286 * Immediate commands are executed, well, immediately.
2287 * Non-Immediate Logout Commands are executed in CmdSN order.
2288 */
Andy Groverc6037cc2012-04-03 15:51:02 -07002289 if (cmd->immediate_cmd) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002290 int ret = iscsit_execute_cmd(cmd, 0);
2291
2292 if (ret < 0)
2293 return ret;
2294 } else {
Nicholas Bellinger561bf152013-07-03 03:58:58 -07002295 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002296 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002297 logout_remove = 0;
Nicholas Bellingerba159912013-07-03 03:48:24 -07002298 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2299 return -1;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002300 }
2301
2302 return logout_remove;
2303}
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002304EXPORT_SYMBOL(iscsit_handle_logout_cmd);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002305
2306static int iscsit_handle_snack(
2307 struct iscsi_conn *conn,
2308 unsigned char *buf)
2309{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002310 struct iscsi_snack *hdr;
2311
2312 hdr = (struct iscsi_snack *) buf;
2313 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002314
2315 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2316 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2317 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2318 hdr->begrun, hdr->runlength, conn->cid);
2319
2320 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2321 pr_err("Initiator sent SNACK request while in"
2322 " ErrorRecoveryLevel=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002323 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2324 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002325 }
2326 /*
2327 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2328 * call from inside iscsi_send_recovery_datain_or_r2t().
2329 */
2330 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2331 case 0:
2332 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002333 hdr->itt,
2334 be32_to_cpu(hdr->ttt),
2335 be32_to_cpu(hdr->begrun),
2336 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002337 case ISCSI_FLAG_SNACK_TYPE_STATUS:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002338 return iscsit_handle_status_snack(conn, hdr->itt,
2339 be32_to_cpu(hdr->ttt),
2340 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002341 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
Christoph Hellwig50e5c872012-09-26 08:00:40 -04002342 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2343 be32_to_cpu(hdr->begrun),
2344 be32_to_cpu(hdr->runlength));
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002345 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2346 /* FIXME: Support R-Data SNACK */
2347 pr_err("R-Data SNACK Not Supported.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002348 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2349 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002350 default:
2351 pr_err("Unknown SNACK type 0x%02x, protocol"
2352 " error.\n", hdr->flags & 0x0f);
Nicholas Bellingerba159912013-07-03 03:48:24 -07002353 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2354 buf);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002355 }
2356
2357 return 0;
2358}
2359
2360static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2361{
2362 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2363 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2364 wait_for_completion_interruptible_timeout(
2365 &conn->rx_half_close_comp,
2366 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2367 }
2368}
2369
2370static int iscsit_handle_immediate_data(
2371 struct iscsi_cmd *cmd,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08002372 struct iscsi_scsi_req *hdr,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002373 u32 length)
2374{
2375 int iov_ret, rx_got = 0, rx_size = 0;
2376 u32 checksum, iov_count = 0, padding = 0;
2377 struct iscsi_conn *conn = cmd->conn;
2378 struct kvec *iov;
2379
2380 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data, cmd->write_data_done, length);
2381 if (iov_ret < 0)
2382 return IMMEDIATE_DATA_CANNOT_RECOVER;
2383
2384 rx_size = length;
2385 iov_count = iov_ret;
2386 iov = &cmd->iov_data[0];
2387
2388 padding = ((-length) & 3);
2389 if (padding != 0) {
2390 iov[iov_count].iov_base = cmd->pad_bytes;
2391 iov[iov_count++].iov_len = padding;
2392 rx_size += padding;
2393 }
2394
2395 if (conn->conn_ops->DataDigest) {
2396 iov[iov_count].iov_base = &checksum;
2397 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2398 rx_size += ISCSI_CRC_LEN;
2399 }
2400
2401 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2402
2403 iscsit_unmap_iovec(cmd);
2404
2405 if (rx_got != rx_size) {
2406 iscsit_rx_thread_wait_for_tcp(conn);
2407 return IMMEDIATE_DATA_CANNOT_RECOVER;
2408 }
2409
2410 if (conn->conn_ops->DataDigest) {
2411 u32 data_crc;
2412
2413 data_crc = iscsit_do_crypto_hash_sg(&conn->conn_rx_hash, cmd,
2414 cmd->write_data_done, length, padding,
2415 cmd->pad_bytes);
2416
2417 if (checksum != data_crc) {
2418 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2419 " does not match computed 0x%08x\n", checksum,
2420 data_crc);
2421
2422 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2423 pr_err("Unable to recover from"
2424 " Immediate Data digest failure while"
2425 " in ERL=0.\n");
Nicholas Bellingerba159912013-07-03 03:48:24 -07002426 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002427 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002428 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002429 return IMMEDIATE_DATA_CANNOT_RECOVER;
2430 } else {
Nicholas Bellingerba159912013-07-03 03:48:24 -07002431 iscsit_reject_cmd(cmd,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002432 ISCSI_REASON_DATA_DIGEST_ERROR,
Nicholas Bellingerba159912013-07-03 03:48:24 -07002433 (unsigned char *)hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002434 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2435 }
2436 } else {
2437 pr_debug("Got CRC32C DataDigest 0x%08x for"
2438 " %u bytes of Immediate Data\n", checksum,
2439 length);
2440 }
2441 }
2442
2443 cmd->write_data_done += length;
2444
Andy Groverebf1d952012-04-03 15:51:24 -07002445 if (cmd->write_data_done == cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002446 spin_lock_bh(&cmd->istate_lock);
2447 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2448 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2449 spin_unlock_bh(&cmd->istate_lock);
2450 }
2451
2452 return IMMEDIATE_DATA_NORMAL_OPERATION;
2453}
2454
2455/*
2456 * Called with sess->conn_lock held.
2457 */
2458/* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2459 with active network interface */
2460static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2461{
2462 struct iscsi_cmd *cmd;
2463 struct iscsi_conn *conn_p;
2464
2465 /*
2466 * Only send a Asynchronous Message on connections whos network
2467 * interface is still functional.
2468 */
2469 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2470 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2471 iscsit_inc_conn_usage_count(conn_p);
2472 break;
2473 }
2474 }
2475
2476 if (!conn_p)
2477 return;
2478
Wei Yongjun3c989d72012-11-23 12:07:39 +08002479 cmd = iscsit_allocate_cmd(conn_p, GFP_ATOMIC);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002480 if (!cmd) {
2481 iscsit_dec_conn_usage_count(conn_p);
2482 return;
2483 }
2484
2485 cmd->logout_cid = conn->cid;
2486 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2487 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2488
2489 spin_lock_bh(&conn_p->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07002490 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002491 spin_unlock_bh(&conn_p->cmd_lock);
2492
2493 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2494 iscsit_dec_conn_usage_count(conn_p);
2495}
2496
2497static int iscsit_send_conn_drop_async_message(
2498 struct iscsi_cmd *cmd,
2499 struct iscsi_conn *conn)
2500{
2501 struct iscsi_async *hdr;
2502
2503 cmd->tx_size = ISCSI_HDR_LEN;
2504 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2505
2506 hdr = (struct iscsi_async *) cmd->pdu;
2507 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2508 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002509 cmd->init_task_tag = RESERVED_ITT;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002510 cmd->targ_xfer_tag = 0xFFFFFFFF;
2511 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2512 cmd->stat_sn = conn->stat_sn++;
2513 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2514 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2515 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2516 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2517 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2518 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2519 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2520
2521 if (conn->conn_ops->HeaderDigest) {
2522 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2523
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002524 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2525 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002526
2527 cmd->tx_size += ISCSI_CRC_LEN;
2528 pr_debug("Attaching CRC32C HeaderDigest to"
2529 " Async Message 0x%08x\n", *header_digest);
2530 }
2531
2532 cmd->iov_misc[0].iov_base = cmd->pdu;
2533 cmd->iov_misc[0].iov_len = cmd->tx_size;
2534 cmd->iov_misc_count = 1;
2535
2536 pr_debug("Sending Connection Dropped Async Message StatSN:"
2537 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2538 cmd->logout_cid, conn->cid);
2539 return 0;
2540}
2541
Andy Grover6f3c0e62012-04-03 15:51:09 -07002542static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2543{
2544 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2545 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2546 wait_for_completion_interruptible_timeout(
2547 &conn->tx_half_close_comp,
2548 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2549 }
2550}
2551
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002552static void
2553iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2554 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2555 bool set_statsn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002556{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002557 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2558 hdr->flags = datain->flags;
2559 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2560 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2561 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2562 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2563 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2564 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2565 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2566 }
2567 }
2568 hton24(hdr->dlength, datain->length);
2569 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2570 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2571 (struct scsi_lun *)&hdr->lun);
2572 else
2573 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2574
2575 hdr->itt = cmd->init_task_tag;
2576
2577 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2578 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2579 else
2580 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2581 if (set_statsn)
2582 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2583 else
2584 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2585
2586 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2587 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2588 hdr->datasn = cpu_to_be32(datain->data_sn);
2589 hdr->offset = cpu_to_be32(datain->offset);
2590
2591 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2592 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2593 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2594 ntohl(hdr->offset), datain->length, conn->cid);
2595}
2596
2597static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2598{
2599 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002600 struct iscsi_datain datain;
2601 struct iscsi_datain_req *dr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002602 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002603 u32 iov_count = 0, tx_size = 0;
2604 int eodr = 0, ret, iov_ret;
2605 bool set_statsn = false;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002606
2607 memset(&datain, 0, sizeof(struct iscsi_datain));
2608 dr = iscsit_get_datain_values(cmd, &datain);
2609 if (!dr) {
2610 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2611 cmd->init_task_tag);
2612 return -1;
2613 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002614 /*
2615 * Be paranoid and double check the logic for now.
2616 */
Andy Groverebf1d952012-04-03 15:51:24 -07002617 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002618 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2619 " datain.length: %u exceeds cmd->data_length: %u\n",
2620 cmd->init_task_tag, datain.offset, datain.length,
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002621 cmd->se_cmd.data_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002622 return -1;
2623 }
2624
2625 spin_lock_bh(&conn->sess->session_stats_lock);
2626 conn->sess->tx_data_octets += datain.length;
2627 if (conn->sess->se_sess->se_node_acl) {
2628 spin_lock(&conn->sess->se_sess->se_node_acl->stats_lock);
2629 conn->sess->se_sess->se_node_acl->read_bytes += datain.length;
2630 spin_unlock(&conn->sess->se_sess->se_node_acl->stats_lock);
2631 }
2632 spin_unlock_bh(&conn->sess->session_stats_lock);
2633 /*
2634 * Special case for successfully execution w/ both DATAIN
2635 * and Sense Data.
2636 */
2637 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2638 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2639 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2640 else {
2641 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2642 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2643 iscsit_increment_maxcmdsn(cmd, conn->sess);
2644 cmd->stat_sn = conn->stat_sn++;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002645 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002646 } else if (dr->dr_complete ==
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002647 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2648 set_statsn = true;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002649 }
2650
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002651 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002652
2653 iov = &cmd->iov_data[0];
2654 iov[iov_count].iov_base = cmd->pdu;
2655 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
2656 tx_size += ISCSI_HDR_LEN;
2657
2658 if (conn->conn_ops->HeaderDigest) {
2659 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2660
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002661 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
2662 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002663
2664 iov[0].iov_len += ISCSI_CRC_LEN;
2665 tx_size += ISCSI_CRC_LEN;
2666
2667 pr_debug("Attaching CRC32 HeaderDigest"
2668 " for DataIN PDU 0x%08x\n", *header_digest);
2669 }
2670
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002671 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[1],
2672 datain.offset, datain.length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002673 if (iov_ret < 0)
2674 return -1;
2675
2676 iov_count += iov_ret;
2677 tx_size += datain.length;
2678
2679 cmd->padding = ((-datain.length) & 3);
2680 if (cmd->padding) {
2681 iov[iov_count].iov_base = cmd->pad_bytes;
2682 iov[iov_count++].iov_len = cmd->padding;
2683 tx_size += cmd->padding;
2684
2685 pr_debug("Attaching %u padding bytes\n",
2686 cmd->padding);
2687 }
2688 if (conn->conn_ops->DataDigest) {
2689 cmd->data_crc = iscsit_do_crypto_hash_sg(&conn->conn_tx_hash, cmd,
2690 datain.offset, datain.length, cmd->padding, cmd->pad_bytes);
2691
2692 iov[iov_count].iov_base = &cmd->data_crc;
2693 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2694 tx_size += ISCSI_CRC_LEN;
2695
2696 pr_debug("Attached CRC32C DataDigest %d bytes, crc"
2697 " 0x%08x\n", datain.length+cmd->padding, cmd->data_crc);
2698 }
2699
2700 cmd->iov_data_count = iov_count;
2701 cmd->tx_size = tx_size;
2702
Andy Grover6f3c0e62012-04-03 15:51:09 -07002703 /* sendpage is preferred but can't insert markers */
2704 if (!conn->conn_ops->IFMarker)
2705 ret = iscsit_fe_sendpage_sg(cmd, conn);
2706 else
2707 ret = iscsit_send_tx_data(cmd, conn, 0);
2708
2709 iscsit_unmap_iovec(cmd);
2710
2711 if (ret < 0) {
2712 iscsit_tx_thread_wait_for_tcp(conn);
2713 return ret;
2714 }
2715
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002716 if (dr->dr_complete) {
Andy Grover6f3c0e62012-04-03 15:51:09 -07002717 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002718 2 : 1;
2719 iscsit_free_datain_req(cmd, dr);
2720 }
2721
Andy Grover6f3c0e62012-04-03 15:51:09 -07002722 return eodr;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002723}
2724
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002725int
2726iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2727 struct iscsi_logout_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002728{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002729 struct iscsi_conn *logout_conn = NULL;
2730 struct iscsi_conn_recovery *cr = NULL;
2731 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002732 /*
2733 * The actual shutting down of Sessions and/or Connections
2734 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2735 * is done in scsi_logout_post_handler().
2736 */
2737 switch (cmd->logout_reason) {
2738 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2739 pr_debug("iSCSI session logout successful, setting"
2740 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2741 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2742 break;
2743 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2744 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2745 break;
2746 /*
2747 * For CLOSECONNECTION logout requests carrying
2748 * a matching logout CID -> local CID, the reference
2749 * for the local CID will have been incremented in
2750 * iscsi_logout_closeconnection().
2751 *
2752 * For CLOSECONNECTION logout requests carrying
2753 * a different CID than the connection it arrived
2754 * on, the connection responding to cmd->logout_cid
2755 * is stopped in iscsit_logout_post_handler_diffcid().
2756 */
2757
2758 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2759 " successful.\n", cmd->logout_cid, conn->cid);
2760 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2761 break;
2762 case ISCSI_LOGOUT_REASON_RECOVERY:
2763 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2764 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2765 break;
2766 /*
2767 * If the connection is still active from our point of view
2768 * force connection recovery to occur.
2769 */
2770 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2771 cmd->logout_cid);
Andy Groveree1b1b92012-07-12 17:34:54 -07002772 if (logout_conn) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002773 iscsit_connection_reinstatement_rcfr(logout_conn);
2774 iscsit_dec_conn_usage_count(logout_conn);
2775 }
2776
2777 cr = iscsit_get_inactive_connection_recovery_entry(
2778 conn->sess, cmd->logout_cid);
2779 if (!cr) {
2780 pr_err("Unable to locate CID: %hu for"
2781 " REMOVECONNFORRECOVERY Logout Request.\n",
2782 cmd->logout_cid);
2783 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2784 break;
2785 }
2786
2787 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2788
2789 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2790 " for recovery for CID: %hu on CID: %hu successful.\n",
2791 cmd->logout_cid, conn->cid);
2792 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2793 break;
2794 default:
2795 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2796 cmd->logout_reason);
2797 return -1;
2798 }
2799
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002800 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2801 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2802 hdr->response = cmd->logout_response;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04002803 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002804 cmd->stat_sn = conn->stat_sn++;
2805 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2806
2807 iscsit_increment_maxcmdsn(cmd, conn->sess);
2808 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2809 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2810
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002811 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2812 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2813 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2814 cmd->logout_cid, conn->cid);
2815
2816 return 0;
2817}
2818EXPORT_SYMBOL(iscsit_build_logout_rsp);
2819
2820static int
2821iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2822{
2823 struct kvec *iov;
2824 int niov = 0, tx_size, rc;
2825
2826 rc = iscsit_build_logout_rsp(cmd, conn,
2827 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2828 if (rc < 0)
2829 return rc;
2830
2831 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002832 iov = &cmd->iov_misc[0];
2833 iov[niov].iov_base = cmd->pdu;
2834 iov[niov++].iov_len = ISCSI_HDR_LEN;
2835
2836 if (conn->conn_ops->HeaderDigest) {
2837 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2838
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002839 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, &cmd->pdu[0],
2840 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002841
2842 iov[0].iov_len += ISCSI_CRC_LEN;
2843 tx_size += ISCSI_CRC_LEN;
2844 pr_debug("Attaching CRC32C HeaderDigest to"
2845 " Logout Response 0x%08x\n", *header_digest);
2846 }
2847 cmd->iov_misc_count = niov;
2848 cmd->tx_size = tx_size;
2849
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002850 return 0;
2851}
2852
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002853void
2854iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2855 struct iscsi_nopin *hdr, bool nopout_response)
2856{
2857 hdr->opcode = ISCSI_OP_NOOP_IN;
2858 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2859 hton24(hdr->dlength, cmd->buf_ptr_size);
2860 if (nopout_response)
2861 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2862 hdr->itt = cmd->init_task_tag;
2863 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2864 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
2865 conn->stat_sn;
2866 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2867
2868 if (nopout_response)
2869 iscsit_increment_maxcmdsn(cmd, conn->sess);
2870
2871 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2872 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
2873
2874 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
2875 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
2876 "Solicitied" : "Unsolicitied", cmd->init_task_tag,
2877 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
2878}
2879EXPORT_SYMBOL(iscsit_build_nopin_rsp);
2880
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002881/*
2882 * Unsolicited NOPIN, either requesting a response or not.
2883 */
2884static int iscsit_send_unsolicited_nopin(
2885 struct iscsi_cmd *cmd,
2886 struct iscsi_conn *conn,
2887 int want_response)
2888{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002889 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
2890 int tx_size = ISCSI_HDR_LEN, ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002891
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002892 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002893
2894 if (conn->conn_ops->HeaderDigest) {
2895 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2896
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002897 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2898 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002899
2900 tx_size += ISCSI_CRC_LEN;
2901 pr_debug("Attaching CRC32C HeaderDigest to"
2902 " NopIN 0x%08x\n", *header_digest);
2903 }
2904
2905 cmd->iov_misc[0].iov_base = cmd->pdu;
2906 cmd->iov_misc[0].iov_len = tx_size;
2907 cmd->iov_misc_count = 1;
2908 cmd->tx_size = tx_size;
2909
2910 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
2911 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
2912
Andy Grover6f3c0e62012-04-03 15:51:09 -07002913 ret = iscsit_send_tx_data(cmd, conn, 1);
2914 if (ret < 0) {
2915 iscsit_tx_thread_wait_for_tcp(conn);
2916 return ret;
2917 }
2918
2919 spin_lock_bh(&cmd->istate_lock);
2920 cmd->i_state = want_response ?
2921 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
2922 spin_unlock_bh(&cmd->istate_lock);
2923
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002924 return 0;
2925}
2926
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002927static int
2928iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002929{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002930 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002931 struct kvec *iov;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07002932 u32 padding = 0;
2933 int niov = 0, tx_size;
2934
2935 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002936
2937 tx_size = ISCSI_HDR_LEN;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002938 iov = &cmd->iov_misc[0];
2939 iov[niov].iov_base = cmd->pdu;
2940 iov[niov++].iov_len = ISCSI_HDR_LEN;
2941
2942 if (conn->conn_ops->HeaderDigest) {
2943 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
2944
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02002945 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
2946 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002947
2948 iov[0].iov_len += ISCSI_CRC_LEN;
2949 tx_size += ISCSI_CRC_LEN;
2950 pr_debug("Attaching CRC32C HeaderDigest"
2951 " to NopIn 0x%08x\n", *header_digest);
2952 }
2953
2954 /*
2955 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
2956 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
2957 */
2958 if (cmd->buf_ptr_size) {
2959 iov[niov].iov_base = cmd->buf_ptr;
2960 iov[niov++].iov_len = cmd->buf_ptr_size;
2961 tx_size += cmd->buf_ptr_size;
2962
2963 pr_debug("Echoing back %u bytes of ping"
2964 " data.\n", cmd->buf_ptr_size);
2965
2966 padding = ((-cmd->buf_ptr_size) & 3);
2967 if (padding != 0) {
2968 iov[niov].iov_base = &cmd->pad_bytes;
2969 iov[niov++].iov_len = padding;
2970 tx_size += padding;
2971 pr_debug("Attaching %u additional"
2972 " padding bytes.\n", padding);
2973 }
2974 if (conn->conn_ops->DataDigest) {
2975 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
2976 cmd->buf_ptr, cmd->buf_ptr_size,
2977 padding, (u8 *)&cmd->pad_bytes,
2978 (u8 *)&cmd->data_crc);
2979
2980 iov[niov].iov_base = &cmd->data_crc;
2981 iov[niov++].iov_len = ISCSI_CRC_LEN;
2982 tx_size += ISCSI_CRC_LEN;
2983 pr_debug("Attached DataDigest for %u"
2984 " bytes of ping data, CRC 0x%08x\n",
2985 cmd->buf_ptr_size, cmd->data_crc);
2986 }
2987 }
2988
2989 cmd->iov_misc_count = niov;
2990 cmd->tx_size = tx_size;
2991
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002992 return 0;
2993}
2994
Andy Grover6f3c0e62012-04-03 15:51:09 -07002995static int iscsit_send_r2t(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00002996 struct iscsi_cmd *cmd,
2997 struct iscsi_conn *conn)
2998{
2999 int tx_size = 0;
3000 struct iscsi_r2t *r2t;
3001 struct iscsi_r2t_rsp *hdr;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003002 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003003
3004 r2t = iscsit_get_r2t_from_list(cmd);
3005 if (!r2t)
3006 return -1;
3007
3008 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3009 memset(hdr, 0, ISCSI_HDR_LEN);
3010 hdr->opcode = ISCSI_OP_R2T;
3011 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3012 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3013 (struct scsi_lun *)&hdr->lun);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003014 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003015 spin_lock_bh(&conn->sess->ttt_lock);
3016 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3017 if (r2t->targ_xfer_tag == 0xFFFFFFFF)
3018 r2t->targ_xfer_tag = conn->sess->targ_xfer_tag++;
3019 spin_unlock_bh(&conn->sess->ttt_lock);
3020 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3021 hdr->statsn = cpu_to_be32(conn->stat_sn);
3022 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3023 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3024 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3025 hdr->data_offset = cpu_to_be32(r2t->offset);
3026 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3027
3028 cmd->iov_misc[0].iov_base = cmd->pdu;
3029 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3030 tx_size += ISCSI_HDR_LEN;
3031
3032 if (conn->conn_ops->HeaderDigest) {
3033 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3034
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003035 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3036 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003037
3038 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3039 tx_size += ISCSI_CRC_LEN;
3040 pr_debug("Attaching CRC32 HeaderDigest for R2T"
3041 " PDU 0x%08x\n", *header_digest);
3042 }
3043
3044 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3045 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3046 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3047 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3048 r2t->offset, r2t->xfer_len, conn->cid);
3049
3050 cmd->iov_misc_count = 1;
3051 cmd->tx_size = tx_size;
3052
3053 spin_lock_bh(&cmd->r2t_lock);
3054 r2t->sent_r2t = 1;
3055 spin_unlock_bh(&cmd->r2t_lock);
3056
Andy Grover6f3c0e62012-04-03 15:51:09 -07003057 ret = iscsit_send_tx_data(cmd, conn, 1);
3058 if (ret < 0) {
3059 iscsit_tx_thread_wait_for_tcp(conn);
3060 return ret;
3061 }
3062
3063 spin_lock_bh(&cmd->dataout_timeout_lock);
3064 iscsit_start_dataout_timer(cmd, conn);
3065 spin_unlock_bh(&cmd->dataout_timeout_lock);
3066
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003067 return 0;
3068}
3069
3070/*
Andy Grover8b1e1242012-04-03 15:51:12 -07003071 * @recovery: If called from iscsi_task_reassign_complete_write() for
3072 * connection recovery.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003073 */
3074int iscsit_build_r2ts_for_cmd(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003075 struct iscsi_conn *conn,
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003076 struct iscsi_cmd *cmd,
Andy Grover8b1e1242012-04-03 15:51:12 -07003077 bool recovery)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003078{
3079 int first_r2t = 1;
3080 u32 offset = 0, xfer_len = 0;
3081
3082 spin_lock_bh(&cmd->r2t_lock);
3083 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3084 spin_unlock_bh(&cmd->r2t_lock);
3085 return 0;
3086 }
3087
Andy Grover8b1e1242012-04-03 15:51:12 -07003088 if (conn->sess->sess_ops->DataSequenceInOrder &&
3089 !recovery)
Andy Groverc6037cc2012-04-03 15:51:02 -07003090 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003091
3092 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3093 if (conn->sess->sess_ops->DataSequenceInOrder) {
3094 offset = cmd->r2t_offset;
3095
Andy Grover8b1e1242012-04-03 15:51:12 -07003096 if (first_r2t && recovery) {
3097 int new_data_end = offset +
3098 conn->sess->sess_ops->MaxBurstLength -
3099 cmd->next_burst_len;
3100
Andy Groverebf1d952012-04-03 15:51:24 -07003101 if (new_data_end > cmd->se_cmd.data_length)
3102 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003103 else
3104 xfer_len =
3105 conn->sess->sess_ops->MaxBurstLength -
3106 cmd->next_burst_len;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003107 } else {
Andy Grover8b1e1242012-04-03 15:51:12 -07003108 int new_data_end = offset +
3109 conn->sess->sess_ops->MaxBurstLength;
3110
Andy Groverebf1d952012-04-03 15:51:24 -07003111 if (new_data_end > cmd->se_cmd.data_length)
3112 xfer_len = cmd->se_cmd.data_length - offset;
Andy Grover8b1e1242012-04-03 15:51:12 -07003113 else
3114 xfer_len = conn->sess->sess_ops->MaxBurstLength;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003115 }
3116 cmd->r2t_offset += xfer_len;
3117
Andy Groverebf1d952012-04-03 15:51:24 -07003118 if (cmd->r2t_offset == cmd->se_cmd.data_length)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003119 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3120 } else {
3121 struct iscsi_seq *seq;
3122
3123 seq = iscsit_get_seq_holder_for_r2t(cmd);
3124 if (!seq) {
3125 spin_unlock_bh(&cmd->r2t_lock);
3126 return -1;
3127 }
3128
3129 offset = seq->offset;
3130 xfer_len = seq->xfer_len;
3131
3132 if (cmd->seq_send_order == cmd->seq_count)
3133 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3134 }
3135 cmd->outstanding_r2ts++;
3136 first_r2t = 0;
3137
3138 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3139 spin_unlock_bh(&cmd->r2t_lock);
3140 return -1;
3141 }
3142
3143 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3144 break;
3145 }
3146 spin_unlock_bh(&cmd->r2t_lock);
3147
3148 return 0;
3149}
3150
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003151void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3152 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003153{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003154 if (inc_stat_sn)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003155 cmd->stat_sn = conn->stat_sn++;
3156
3157 spin_lock_bh(&conn->sess->session_stats_lock);
3158 conn->sess->rsp_pdus++;
3159 spin_unlock_bh(&conn->sess->session_stats_lock);
3160
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003161 memset(hdr, 0, ISCSI_HDR_LEN);
3162 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3163 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3164 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3165 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003166 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003167 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3168 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
Nicholas Bellinger7e46cf02011-11-15 23:59:00 -08003169 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003170 }
3171 hdr->response = cmd->iscsi_response;
3172 hdr->cmd_status = cmd->se_cmd.scsi_status;
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003173 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003174 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3175
3176 iscsit_increment_maxcmdsn(cmd, conn->sess);
3177 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3178 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3179
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003180 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3181 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3182 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3183 cmd->se_cmd.scsi_status, conn->cid);
3184}
3185EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3186
3187static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3188{
3189 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3190 struct kvec *iov;
3191 u32 padding = 0, tx_size = 0;
3192 int iov_count = 0;
3193 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3194
3195 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3196
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003197 iov = &cmd->iov_misc[0];
3198 iov[iov_count].iov_base = cmd->pdu;
3199 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3200 tx_size += ISCSI_HDR_LEN;
3201
3202 /*
3203 * Attach SENSE DATA payload to iSCSI Response PDU
3204 */
3205 if (cmd->se_cmd.sense_buffer &&
3206 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3207 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003208 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3209 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3210
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003211 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
Christoph Hellwig50e5c872012-09-26 08:00:40 -04003212 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003213 iov[iov_count].iov_base = cmd->sense_buffer;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003214 iov[iov_count++].iov_len =
3215 (cmd->se_cmd.scsi_sense_length + padding);
3216 tx_size += cmd->se_cmd.scsi_sense_length;
3217
3218 if (padding) {
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003219 memset(cmd->sense_buffer +
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003220 cmd->se_cmd.scsi_sense_length, 0, padding);
3221 tx_size += padding;
3222 pr_debug("Adding %u bytes of padding to"
3223 " SENSE.\n", padding);
3224 }
3225
3226 if (conn->conn_ops->DataDigest) {
3227 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Roland Dreier9c58b7d2012-08-15 14:35:25 -07003228 cmd->sense_buffer,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003229 (cmd->se_cmd.scsi_sense_length + padding),
3230 0, NULL, (u8 *)&cmd->data_crc);
3231
3232 iov[iov_count].iov_base = &cmd->data_crc;
3233 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3234 tx_size += ISCSI_CRC_LEN;
3235
3236 pr_debug("Attaching CRC32 DataDigest for"
3237 " SENSE, %u bytes CRC 0x%08x\n",
3238 (cmd->se_cmd.scsi_sense_length + padding),
3239 cmd->data_crc);
3240 }
3241
3242 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3243 " Response PDU\n",
3244 cmd->se_cmd.scsi_sense_length);
3245 }
3246
3247 if (conn->conn_ops->HeaderDigest) {
3248 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3249
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003250 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->pdu,
3251 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003252
3253 iov[0].iov_len += ISCSI_CRC_LEN;
3254 tx_size += ISCSI_CRC_LEN;
3255 pr_debug("Attaching CRC32 HeaderDigest for Response"
3256 " PDU 0x%08x\n", *header_digest);
3257 }
3258
3259 cmd->iov_misc_count = iov_count;
3260 cmd->tx_size = tx_size;
3261
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003262 return 0;
3263}
3264
3265static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3266{
3267 switch (se_tmr->response) {
3268 case TMR_FUNCTION_COMPLETE:
3269 return ISCSI_TMF_RSP_COMPLETE;
3270 case TMR_TASK_DOES_NOT_EXIST:
3271 return ISCSI_TMF_RSP_NO_TASK;
3272 case TMR_LUN_DOES_NOT_EXIST:
3273 return ISCSI_TMF_RSP_NO_LUN;
3274 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3275 return ISCSI_TMF_RSP_NOT_SUPPORTED;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003276 case TMR_FUNCTION_REJECTED:
3277 default:
3278 return ISCSI_TMF_RSP_REJECTED;
3279 }
3280}
3281
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003282void
3283iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3284 struct iscsi_tm_rsp *hdr)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003285{
3286 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003287
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003288 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Nicholas Bellinger7ae0b102011-11-27 22:25:14 -08003289 hdr->flags = ISCSI_FLAG_CMD_FINAL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003290 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
Christoph Hellwig66c7db62012-09-26 08:00:39 -04003291 hdr->itt = cmd->init_task_tag;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003292 cmd->stat_sn = conn->stat_sn++;
3293 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3294
3295 iscsit_increment_maxcmdsn(cmd, conn->sess);
3296 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3297 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3298
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003299 pr_debug("Built Task Management Response ITT: 0x%08x,"
3300 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3301 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3302}
3303EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3304
3305static int
3306iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3307{
3308 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3309 u32 tx_size = 0;
3310
3311 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3312
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003313 cmd->iov_misc[0].iov_base = cmd->pdu;
3314 cmd->iov_misc[0].iov_len = ISCSI_HDR_LEN;
3315 tx_size += ISCSI_HDR_LEN;
3316
3317 if (conn->conn_ops->HeaderDigest) {
3318 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3319
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003320 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3321 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003322
3323 cmd->iov_misc[0].iov_len += ISCSI_CRC_LEN;
3324 tx_size += ISCSI_CRC_LEN;
3325 pr_debug("Attaching CRC32 HeaderDigest for Task"
3326 " Mgmt Response PDU 0x%08x\n", *header_digest);
3327 }
3328
3329 cmd->iov_misc_count = 1;
3330 cmd->tx_size = tx_size;
3331
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003332 return 0;
3333}
3334
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003335static bool iscsit_check_inaddr_any(struct iscsi_np *np)
3336{
3337 bool ret = false;
3338
3339 if (np->np_sockaddr.ss_family == AF_INET6) {
3340 const struct sockaddr_in6 sin6 = {
3341 .sin6_addr = IN6ADDR_ANY_INIT };
3342 struct sockaddr_in6 *sock_in6 =
3343 (struct sockaddr_in6 *)&np->np_sockaddr;
3344
3345 if (!memcmp(sock_in6->sin6_addr.s6_addr,
3346 sin6.sin6_addr.s6_addr, 16))
3347 ret = true;
3348 } else {
3349 struct sockaddr_in * sock_in =
3350 (struct sockaddr_in *)&np->np_sockaddr;
3351
Christoph Hellwigcea0b4c2012-09-26 08:00:38 -04003352 if (sock_in->sin_addr.s_addr == htonl(INADDR_ANY))
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003353 ret = true;
3354 }
3355
3356 return ret;
3357}
3358
Andy Grover8b1e1242012-04-03 15:51:12 -07003359#define SENDTARGETS_BUF_LIMIT 32768U
3360
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003361static int iscsit_build_sendtargets_response(struct iscsi_cmd *cmd)
3362{
3363 char *payload = NULL;
3364 struct iscsi_conn *conn = cmd->conn;
3365 struct iscsi_portal_group *tpg;
3366 struct iscsi_tiqn *tiqn;
3367 struct iscsi_tpg_np *tpg_np;
3368 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
Andy Grover8b1e1242012-04-03 15:51:12 -07003369 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
Nicholas Bellinger66658892013-06-19 22:45:42 -07003370 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003371
Andy Grover8b1e1242012-04-03 15:51:12 -07003372 buffer_len = max(conn->conn_ops->MaxRecvDataSegmentLength,
3373 SENDTARGETS_BUF_LIMIT);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003374
3375 payload = kzalloc(buffer_len, GFP_KERNEL);
3376 if (!payload) {
3377 pr_err("Unable to allocate memory for sendtargets"
3378 " response.\n");
3379 return -ENOMEM;
3380 }
Nicholas Bellinger66658892013-06-19 22:45:42 -07003381 /*
3382 * Locate pointer to iqn./eui. string for IFC_SENDTARGETS_SINGLE
3383 * explicit case..
3384 */
3385 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) {
3386 text_ptr = strchr(text_in, '=');
3387 if (!text_ptr) {
3388 pr_err("Unable to locate '=' string in text_in:"
3389 " %s\n", text_in);
Dan Carpenter4f45d322013-06-24 18:46:57 +03003390 kfree(payload);
Nicholas Bellinger66658892013-06-19 22:45:42 -07003391 return -EINVAL;
3392 }
3393 /*
3394 * Skip over '=' character..
3395 */
3396 text_ptr += 1;
3397 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003398
3399 spin_lock(&tiqn_lock);
3400 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
Nicholas Bellinger66658892013-06-19 22:45:42 -07003401 if ((cmd->cmd_flags & IFC_SENDTARGETS_SINGLE) &&
3402 strcmp(tiqn->tiqn, text_ptr)) {
3403 continue;
3404 }
3405
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003406 len = sprintf(buf, "TargetName=%s", tiqn->tiqn);
3407 len += 1;
3408
3409 if ((len + payload_len) > buffer_len) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003410 end_of_buf = 1;
3411 goto eob;
3412 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003413 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003414 payload_len += len;
3415
3416 spin_lock(&tiqn->tiqn_tpg_lock);
3417 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3418
3419 spin_lock(&tpg->tpg_state_lock);
3420 if ((tpg->tpg_state == TPG_STATE_FREE) ||
3421 (tpg->tpg_state == TPG_STATE_INACTIVE)) {
3422 spin_unlock(&tpg->tpg_state_lock);
3423 continue;
3424 }
3425 spin_unlock(&tpg->tpg_state_lock);
3426
3427 spin_lock(&tpg->tpg_np_lock);
3428 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3429 tpg_np_list) {
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003430 struct iscsi_np *np = tpg_np->tpg_np;
3431 bool inaddr_any = iscsit_check_inaddr_any(np);
3432
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003433 len = sprintf(buf, "TargetAddress="
Chris Leechdfecf612013-08-12 11:26:28 -07003434 "%s:%hu,%hu",
3435 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003436 np->np_ip : conn->local_ip,
Chris Leechdfecf612013-08-12 11:26:28 -07003437 (inaddr_any == false) ?
Nicholas Bellinger2f9bc892012-01-16 23:33:48 -08003438 np->np_port : conn->local_port,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003439 tpg->tpgt);
3440 len += 1;
3441
3442 if ((len + payload_len) > buffer_len) {
3443 spin_unlock(&tpg->tpg_np_lock);
3444 spin_unlock(&tiqn->tiqn_tpg_lock);
3445 end_of_buf = 1;
3446 goto eob;
3447 }
Jörn Engel8359cf42011-11-24 02:05:51 +01003448 memcpy(payload + payload_len, buf, len);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003449 payload_len += len;
3450 }
3451 spin_unlock(&tpg->tpg_np_lock);
3452 }
3453 spin_unlock(&tiqn->tiqn_tpg_lock);
3454eob:
3455 if (end_of_buf)
3456 break;
Nicholas Bellinger66658892013-06-19 22:45:42 -07003457
3458 if (cmd->cmd_flags & IFC_SENDTARGETS_SINGLE)
3459 break;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003460 }
3461 spin_unlock(&tiqn_lock);
3462
3463 cmd->buf_ptr = payload;
3464
3465 return payload_len;
3466}
3467
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003468int
3469iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3470 struct iscsi_text_rsp *hdr)
3471{
3472 int text_length, padding;
3473
3474 text_length = iscsit_build_sendtargets_response(cmd);
3475 if (text_length < 0)
3476 return text_length;
3477
3478 hdr->opcode = ISCSI_OP_TEXT_RSP;
3479 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3480 padding = ((-text_length) & 3);
3481 hton24(hdr->dlength, text_length);
3482 hdr->itt = cmd->init_task_tag;
3483 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3484 cmd->stat_sn = conn->stat_sn++;
3485 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3486
3487 iscsit_increment_maxcmdsn(cmd, conn->sess);
3488 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3489 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3490
3491 pr_debug("Built Text Response: ITT: 0x%08x, StatSN: 0x%08x,"
3492 " Length: %u, CID: %hu\n", cmd->init_task_tag, cmd->stat_sn,
3493 text_length, conn->cid);
3494
3495 return text_length + padding;
3496}
3497EXPORT_SYMBOL(iscsit_build_text_rsp);
3498
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003499/*
3500 * FIXME: Add support for F_BIT and C_BIT when the length is longer than
3501 * MaxRecvDataSegmentLength.
3502 */
3503static int iscsit_send_text_rsp(
3504 struct iscsi_cmd *cmd,
3505 struct iscsi_conn *conn)
3506{
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003507 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003508 struct kvec *iov;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003509 u32 tx_size = 0;
3510 int text_length, iov_count = 0, rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003511
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003512 rc = iscsit_build_text_rsp(cmd, conn, hdr);
3513 if (rc < 0)
3514 return rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003515
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003516 text_length = rc;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003517 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003518 iov[iov_count].iov_base = cmd->pdu;
3519 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3520 iov[iov_count].iov_base = cmd->buf_ptr;
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003521 iov[iov_count++].iov_len = text_length;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003522
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003523 tx_size += (ISCSI_HDR_LEN + text_length);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003524
3525 if (conn->conn_ops->HeaderDigest) {
3526 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3527
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003528 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3529 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003530
3531 iov[0].iov_len += ISCSI_CRC_LEN;
3532 tx_size += ISCSI_CRC_LEN;
3533 pr_debug("Attaching CRC32 HeaderDigest for"
3534 " Text Response PDU 0x%08x\n", *header_digest);
3535 }
3536
3537 if (conn->conn_ops->DataDigest) {
3538 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash,
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003539 cmd->buf_ptr, text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003540 0, NULL, (u8 *)&cmd->data_crc);
3541
3542 iov[iov_count].iov_base = &cmd->data_crc;
3543 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3544 tx_size += ISCSI_CRC_LEN;
3545
3546 pr_debug("Attaching DataDigest for %u bytes of text"
Nicholas Bellinger889c8a62013-06-14 18:49:55 -07003547 " data, CRC 0x%08x\n", text_length,
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003548 cmd->data_crc);
3549 }
3550
3551 cmd->iov_misc_count = iov_count;
3552 cmd->tx_size = tx_size;
3553
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003554 return 0;
3555}
3556
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003557void
3558iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3559 struct iscsi_reject *hdr)
3560{
3561 hdr->opcode = ISCSI_OP_REJECT;
Nicholas Bellingerba159912013-07-03 03:48:24 -07003562 hdr->reason = cmd->reject_reason;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003563 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3564 hton24(hdr->dlength, ISCSI_HDR_LEN);
3565 hdr->ffffffff = cpu_to_be32(0xffffffff);
3566 cmd->stat_sn = conn->stat_sn++;
3567 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3568 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3569 hdr->max_cmdsn = cpu_to_be32(conn->sess->max_cmd_sn);
3570
3571}
3572EXPORT_SYMBOL(iscsit_build_reject);
3573
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003574static int iscsit_send_reject(
3575 struct iscsi_cmd *cmd,
3576 struct iscsi_conn *conn)
3577{
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003578 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003579 struct kvec *iov;
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003580 u32 iov_count = 0, tx_size;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003581
Nicholas Bellingerbfbdb312013-05-03 16:46:41 -07003582 iscsit_build_reject(cmd, conn, hdr);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003583
3584 iov = &cmd->iov_misc[0];
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003585 iov[iov_count].iov_base = cmd->pdu;
3586 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3587 iov[iov_count].iov_base = cmd->buf_ptr;
3588 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
3589
3590 tx_size = (ISCSI_HDR_LEN + ISCSI_HDR_LEN);
3591
3592 if (conn->conn_ops->HeaderDigest) {
3593 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
3594
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003595 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, hdr,
3596 ISCSI_HDR_LEN, 0, NULL, (u8 *)header_digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003597
3598 iov[0].iov_len += ISCSI_CRC_LEN;
3599 tx_size += ISCSI_CRC_LEN;
3600 pr_debug("Attaching CRC32 HeaderDigest for"
3601 " REJECT PDU 0x%08x\n", *header_digest);
3602 }
3603
3604 if (conn->conn_ops->DataDigest) {
Geert Uytterhoeven80690fd2013-05-03 23:15:57 +02003605 iscsit_do_crypto_hash_buf(&conn->conn_tx_hash, cmd->buf_ptr,
3606 ISCSI_HDR_LEN, 0, NULL, (u8 *)&cmd->data_crc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003607
3608 iov[iov_count].iov_base = &cmd->data_crc;
3609 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
3610 tx_size += ISCSI_CRC_LEN;
3611 pr_debug("Attaching CRC32 DataDigest for REJECT"
3612 " PDU 0x%08x\n", cmd->data_crc);
3613 }
3614
3615 cmd->iov_misc_count = iov_count;
3616 cmd->tx_size = tx_size;
3617
3618 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3619 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3620
3621 return 0;
3622}
3623
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003624void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3625{
3626 struct iscsi_thread_set *ts = conn->thread_set;
3627 int ord, cpu;
3628 /*
3629 * thread_id is assigned from iscsit_global->ts_bitmap from
3630 * within iscsi_thread_set.c:iscsi_allocate_thread_sets()
3631 *
3632 * Here we use thread_id to determine which CPU that this
3633 * iSCSI connection's iscsi_thread_set will be scheduled to
3634 * execute upon.
3635 */
3636 ord = ts->thread_id % cpumask_weight(cpu_online_mask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003637 for_each_online_cpu(cpu) {
3638 if (ord-- == 0) {
3639 cpumask_set_cpu(cpu, conn->conn_cpumask);
3640 return;
3641 }
3642 }
3643 /*
3644 * This should never be reached..
3645 */
3646 dump_stack();
3647 cpumask_setall(conn->conn_cpumask);
3648}
3649
3650static inline void iscsit_thread_check_cpumask(
3651 struct iscsi_conn *conn,
3652 struct task_struct *p,
3653 int mode)
3654{
3655 char buf[128];
3656 /*
3657 * mode == 1 signals iscsi_target_tx_thread() usage.
3658 * mode == 0 signals iscsi_target_rx_thread() usage.
3659 */
3660 if (mode == 1) {
3661 if (!conn->conn_tx_reset_cpumask)
3662 return;
3663 conn->conn_tx_reset_cpumask = 0;
3664 } else {
3665 if (!conn->conn_rx_reset_cpumask)
3666 return;
3667 conn->conn_rx_reset_cpumask = 0;
3668 }
3669 /*
3670 * Update the CPU mask for this single kthread so that
3671 * both TX and RX kthreads are scheduled to run on the
3672 * same CPU.
3673 */
3674 memset(buf, 0, 128);
3675 cpumask_scnprintf(buf, 128, conn->conn_cpumask);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003676 set_cpus_allowed_ptr(p, conn->conn_cpumask);
3677}
3678
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003679static int
3680iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003681{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003682 int ret;
3683
3684 switch (state) {
3685 case ISTATE_SEND_R2T:
3686 ret = iscsit_send_r2t(cmd, conn);
3687 if (ret < 0)
3688 goto err;
3689 break;
3690 case ISTATE_REMOVE:
3691 spin_lock_bh(&conn->cmd_lock);
3692 list_del(&cmd->i_conn_node);
3693 spin_unlock_bh(&conn->cmd_lock);
3694
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07003695 iscsit_free_cmd(cmd, false);
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003696 break;
3697 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3698 iscsit_mod_nopin_response_timer(conn);
3699 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3700 if (ret < 0)
3701 goto err;
3702 break;
3703 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3704 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3705 if (ret < 0)
3706 goto err;
3707 break;
3708 default:
3709 pr_err("Unknown Opcode: 0x%02x ITT:"
3710 " 0x%08x, i_state: %d on CID: %hu\n",
3711 cmd->iscsi_opcode, cmd->init_task_tag, state,
3712 conn->cid);
3713 goto err;
3714 }
3715
3716 return 0;
3717
3718err:
3719 return -1;
3720}
3721
3722static int
3723iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3724{
3725 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003726 struct iscsi_queue_req *qr;
3727 struct iscsi_cmd *cmd;
3728 u8 state;
3729 int ret;
3730
3731 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3732 atomic_set(&conn->check_immediate_queue, 0);
3733 cmd = qr->cmd;
3734 state = qr->state;
3735 kmem_cache_free(lio_qr_cache, qr);
3736
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003737 ret = t->iscsit_immediate_queue(conn, cmd, state);
3738 if (ret < 0)
3739 return ret;
3740 }
Andy Grover6f3c0e62012-04-03 15:51:09 -07003741
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003742 return 0;
3743}
Andy Grover6f3c0e62012-04-03 15:51:09 -07003744
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003745static int
3746iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3747{
3748 int ret;
3749
3750check_rsp_state:
3751 switch (state) {
3752 case ISTATE_SEND_DATAIN:
3753 ret = iscsit_send_datain(cmd, conn);
3754 if (ret < 0)
3755 goto err;
3756 else if (!ret)
3757 /* more drs */
3758 goto check_rsp_state;
3759 else if (ret == 1) {
3760 /* all done */
3761 spin_lock_bh(&cmd->istate_lock);
3762 cmd->i_state = ISTATE_SENT_STATUS;
3763 spin_unlock_bh(&cmd->istate_lock);
3764
3765 if (atomic_read(&conn->check_immediate_queue))
3766 return 1;
3767
3768 return 0;
3769 } else if (ret == 2) {
3770 /* Still must send status,
3771 SCF_TRANSPORT_TASK_SENSE was set */
3772 spin_lock_bh(&cmd->istate_lock);
3773 cmd->i_state = ISTATE_SEND_STATUS;
3774 spin_unlock_bh(&cmd->istate_lock);
3775 state = ISTATE_SEND_STATUS;
3776 goto check_rsp_state;
3777 }
3778
3779 break;
3780 case ISTATE_SEND_STATUS:
3781 case ISTATE_SEND_STATUS_RECOVERY:
3782 ret = iscsit_send_response(cmd, conn);
3783 break;
3784 case ISTATE_SEND_LOGOUTRSP:
3785 ret = iscsit_send_logout(cmd, conn);
3786 break;
3787 case ISTATE_SEND_ASYNCMSG:
3788 ret = iscsit_send_conn_drop_async_message(
3789 cmd, conn);
3790 break;
3791 case ISTATE_SEND_NOPIN:
3792 ret = iscsit_send_nopin(cmd, conn);
3793 break;
3794 case ISTATE_SEND_REJECT:
3795 ret = iscsit_send_reject(cmd, conn);
3796 break;
3797 case ISTATE_SEND_TASKMGTRSP:
3798 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3799 if (ret != 0)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003800 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003801 ret = iscsit_tmr_post_handler(cmd, conn);
3802 if (ret != 0)
3803 iscsit_fall_back_to_erl0(conn->sess);
3804 break;
3805 case ISTATE_SEND_TEXTRSP:
3806 ret = iscsit_send_text_rsp(cmd, conn);
3807 break;
3808 default:
3809 pr_err("Unknown Opcode: 0x%02x ITT:"
3810 " 0x%08x, i_state: %d on CID: %hu\n",
3811 cmd->iscsi_opcode, cmd->init_task_tag,
3812 state, conn->cid);
3813 goto err;
3814 }
3815 if (ret < 0)
3816 goto err;
3817
3818 if (iscsit_send_tx_data(cmd, conn, 1) < 0) {
3819 iscsit_tx_thread_wait_for_tcp(conn);
3820 iscsit_unmap_iovec(cmd);
3821 goto err;
3822 }
3823 iscsit_unmap_iovec(cmd);
3824
3825 switch (state) {
3826 case ISTATE_SEND_LOGOUTRSP:
3827 if (!iscsit_logout_post_handler(cmd, conn))
3828 goto restart;
3829 /* fall through */
3830 case ISTATE_SEND_STATUS:
3831 case ISTATE_SEND_ASYNCMSG:
3832 case ISTATE_SEND_NOPIN:
3833 case ISTATE_SEND_STATUS_RECOVERY:
3834 case ISTATE_SEND_TEXTRSP:
3835 case ISTATE_SEND_TASKMGTRSP:
Nicholas Bellingerba159912013-07-03 03:48:24 -07003836 case ISTATE_SEND_REJECT:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003837 spin_lock_bh(&cmd->istate_lock);
3838 cmd->i_state = ISTATE_SENT_STATUS;
3839 spin_unlock_bh(&cmd->istate_lock);
3840 break;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003841 default:
3842 pr_err("Unknown Opcode: 0x%02x ITT:"
3843 " 0x%08x, i_state: %d on CID: %hu\n",
3844 cmd->iscsi_opcode, cmd->init_task_tag,
3845 cmd->i_state, conn->cid);
3846 goto err;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003847 }
3848
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003849 if (atomic_read(&conn->check_immediate_queue))
3850 return 1;
3851
Andy Grover6f3c0e62012-04-03 15:51:09 -07003852 return 0;
3853
3854err:
3855 return -1;
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003856restart:
3857 return -EAGAIN;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003858}
3859
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003860static int iscsit_handle_response_queue(struct iscsi_conn *conn)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003861{
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003862 struct iscsit_transport *t = conn->conn_transport;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003863 struct iscsi_queue_req *qr;
3864 struct iscsi_cmd *cmd;
3865 u8 state;
3866 int ret;
3867
3868 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3869 cmd = qr->cmd;
3870 state = qr->state;
3871 kmem_cache_free(lio_qr_cache, qr);
3872
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003873 ret = t->iscsit_response_queue(conn, cmd, state);
3874 if (ret == 1 || ret < 0)
3875 return ret;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003876 }
3877
3878 return 0;
Andy Grover6f3c0e62012-04-03 15:51:09 -07003879}
3880
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003881int iscsi_target_tx_thread(void *arg)
3882{
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003883 int ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003884 struct iscsi_conn *conn;
Jörn Engel8359cf42011-11-24 02:05:51 +01003885 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003886 /*
3887 * Allow ourselves to be interrupted by SIGINT so that a
3888 * connection recovery / failure event can be triggered externally.
3889 */
3890 allow_signal(SIGINT);
3891
3892restart:
3893 conn = iscsi_tx_thread_pre_handler(ts);
3894 if (!conn)
3895 goto out;
3896
Andy Grover6f3c0e62012-04-03 15:51:09 -07003897 ret = 0;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003898
3899 while (!kthread_should_stop()) {
3900 /*
3901 * Ensure that both TX and RX per connection kthreads
3902 * are scheduled to run on the same CPU.
3903 */
3904 iscsit_thread_check_cpumask(conn, current, 1);
3905
Roland Dreierd5627ac2012-10-31 09:16:46 -07003906 wait_event_interruptible(conn->queues_wq,
3907 !iscsit_conn_all_queues_empty(conn) ||
3908 ts->status == ISCSI_THREAD_SET_RESET);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003909
3910 if ((ts->status == ISCSI_THREAD_SET_RESET) ||
3911 signal_pending(current))
3912 goto transport_err;
3913
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003914get_immediate:
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003915 ret = iscsit_handle_immediate_queue(conn);
Andy Grover6f3c0e62012-04-03 15:51:09 -07003916 if (ret < 0)
3917 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003918
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07003919 ret = iscsit_handle_response_queue(conn);
Nicholas Bellingerfd3a9022013-02-27 17:53:52 -08003920 if (ret == 1)
3921 goto get_immediate;
3922 else if (ret == -EAGAIN)
Andy Grover6f3c0e62012-04-03 15:51:09 -07003923 goto restart;
3924 else if (ret < 0)
3925 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00003926 }
3927
3928transport_err:
3929 iscsit_take_action_for_connection_exit(conn);
3930 goto restart;
3931out:
3932 return 0;
3933}
3934
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003935static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3936{
3937 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3938 struct iscsi_cmd *cmd;
3939 int ret = 0;
3940
3941 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3942 case ISCSI_OP_SCSI_CMD:
3943 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3944 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003945 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003946
3947 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3948 break;
3949 case ISCSI_OP_SCSI_DATA_OUT:
3950 ret = iscsit_handle_data_out(conn, buf);
3951 break;
3952 case ISCSI_OP_NOOP_OUT:
3953 cmd = NULL;
3954 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3955 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3956 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003957 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003958 }
3959 ret = iscsit_handle_nop_out(conn, cmd, buf);
3960 break;
3961 case ISCSI_OP_SCSI_TMFUNC:
3962 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3963 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003964 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003965
3966 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3967 break;
3968 case ISCSI_OP_TEXT:
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003969 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3970 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003971 goto reject;
Nicholas Bellinger64534aa2013-06-14 16:46:16 -07003972
3973 ret = iscsit_handle_text_cmd(conn, cmd, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003974 break;
3975 case ISCSI_OP_LOGOUT:
3976 cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
3977 if (!cmd)
Nicholas Bellingerba159912013-07-03 03:48:24 -07003978 goto reject;
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08003979
3980 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3981 if (ret > 0)
3982 wait_for_completion_timeout(&conn->conn_logout_comp,
3983 SECONDS_FOR_LOGOUT_COMP * HZ);
3984 break;
3985 case ISCSI_OP_SNACK:
3986 ret = iscsit_handle_snack(conn, buf);
3987 break;
3988 default:
3989 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3990 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3991 pr_err("Cannot recover from unknown"
3992 " opcode while ERL=0, closing iSCSI connection.\n");
3993 return -1;
3994 }
3995 if (!conn->conn_ops->OFMarker) {
3996 pr_err("Unable to recover from unknown"
3997 " opcode while OFMarker=No, closing iSCSI"
3998 " connection.\n");
3999 return -1;
4000 }
4001 if (iscsit_recover_from_unknown_opcode(conn) < 0) {
4002 pr_err("Unable to recover from unknown"
4003 " opcode, closing iSCSI connection.\n");
4004 return -1;
4005 }
4006 break;
4007 }
4008
4009 return ret;
Nicholas Bellingerba159912013-07-03 03:48:24 -07004010reject:
4011 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004012}
4013
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004014int iscsi_target_rx_thread(void *arg)
4015{
4016 int ret;
4017 u8 buffer[ISCSI_HDR_LEN], opcode;
4018 u32 checksum = 0, digest = 0;
4019 struct iscsi_conn *conn = NULL;
Jörn Engel8359cf42011-11-24 02:05:51 +01004020 struct iscsi_thread_set *ts = arg;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004021 struct kvec iov;
4022 /*
4023 * Allow ourselves to be interrupted by SIGINT so that a
4024 * connection recovery / failure event can be triggered externally.
4025 */
4026 allow_signal(SIGINT);
4027
4028restart:
4029 conn = iscsi_rx_thread_pre_handler(ts);
4030 if (!conn)
4031 goto out;
4032
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004033 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND) {
4034 struct completion comp;
4035 int rc;
4036
4037 init_completion(&comp);
4038 rc = wait_for_completion_interruptible(&comp);
4039 if (rc < 0)
4040 goto transport_err;
4041
4042 goto out;
4043 }
4044
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004045 while (!kthread_should_stop()) {
4046 /*
4047 * Ensure that both TX and RX per connection kthreads
4048 * are scheduled to run on the same CPU.
4049 */
4050 iscsit_thread_check_cpumask(conn, current, 0);
4051
4052 memset(buffer, 0, ISCSI_HDR_LEN);
4053 memset(&iov, 0, sizeof(struct kvec));
4054
4055 iov.iov_base = buffer;
4056 iov.iov_len = ISCSI_HDR_LEN;
4057
4058 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
4059 if (ret != ISCSI_HDR_LEN) {
4060 iscsit_rx_thread_wait_for_tcp(conn);
4061 goto transport_err;
4062 }
4063
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004064 if (conn->conn_ops->HeaderDigest) {
4065 iov.iov_base = &digest;
4066 iov.iov_len = ISCSI_CRC_LEN;
4067
4068 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
4069 if (ret != ISCSI_CRC_LEN) {
4070 iscsit_rx_thread_wait_for_tcp(conn);
4071 goto transport_err;
4072 }
4073
4074 iscsit_do_crypto_hash_buf(&conn->conn_rx_hash,
4075 buffer, ISCSI_HDR_LEN,
4076 0, NULL, (u8 *)&checksum);
4077
4078 if (digest != checksum) {
4079 pr_err("HeaderDigest CRC32C failed,"
4080 " received 0x%08x, computed 0x%08x\n",
4081 digest, checksum);
4082 /*
4083 * Set the PDU to 0xff so it will intentionally
4084 * hit default in the switch below.
4085 */
4086 memset(buffer, 0xff, ISCSI_HDR_LEN);
4087 spin_lock_bh(&conn->sess->session_stats_lock);
4088 conn->sess->conn_digest_errors++;
4089 spin_unlock_bh(&conn->sess->session_stats_lock);
4090 } else {
4091 pr_debug("Got HeaderDigest CRC32C"
4092 " 0x%08x\n", checksum);
4093 }
4094 }
4095
4096 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4097 goto transport_err;
4098
4099 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4100
4101 if (conn->sess->sess_ops->SessionType &&
4102 ((!(opcode & ISCSI_OP_TEXT)) ||
4103 (!(opcode & ISCSI_OP_LOGOUT)))) {
4104 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4105 " while in Discovery Session, rejecting.\n", opcode);
Nicholas Bellingerba159912013-07-03 03:48:24 -07004106 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4107 buffer);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004108 goto transport_err;
4109 }
4110
Nicholas Bellinger3e1c81a2013-03-06 22:18:24 -08004111 ret = iscsi_target_rx_opcode(conn, buffer);
4112 if (ret < 0)
4113 goto transport_err;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004114 }
4115
4116transport_err:
4117 if (!signal_pending(current))
4118 atomic_set(&conn->transport_failed, 1);
4119 iscsit_take_action_for_connection_exit(conn);
4120 goto restart;
4121out:
4122 return 0;
4123}
4124
4125static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4126{
4127 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4128 struct iscsi_session *sess = conn->sess;
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004129 /*
4130 * We expect this function to only ever be called from either RX or TX
4131 * thread context via iscsit_close_connection() once the other context
4132 * has been reset -> returned sleeping pre-handler state.
4133 */
4134 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004135 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004136
Andy Grover2fbb4712012-04-03 15:51:01 -07004137 list_del(&cmd->i_conn_node);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004138 spin_unlock_bh(&conn->cmd_lock);
4139
4140 iscsit_increment_maxcmdsn(cmd, sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004141
Nicholas Bellingeraafc9d12013-05-31 00:49:41 -07004142 iscsit_free_cmd(cmd, true);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004143
4144 spin_lock_bh(&conn->cmd_lock);
4145 }
4146 spin_unlock_bh(&conn->cmd_lock);
4147}
4148
4149static void iscsit_stop_timers_for_cmds(
4150 struct iscsi_conn *conn)
4151{
4152 struct iscsi_cmd *cmd;
4153
4154 spin_lock_bh(&conn->cmd_lock);
Andy Grover2fbb4712012-04-03 15:51:01 -07004155 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004156 if (cmd->data_direction == DMA_TO_DEVICE)
4157 iscsit_stop_dataout_timer(cmd);
4158 }
4159 spin_unlock_bh(&conn->cmd_lock);
4160}
4161
4162int iscsit_close_connection(
4163 struct iscsi_conn *conn)
4164{
4165 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4166 struct iscsi_session *sess = conn->sess;
4167
4168 pr_debug("Closing iSCSI connection CID %hu on SID:"
4169 " %u\n", conn->cid, sess->sid);
4170 /*
4171 * Always up conn_logout_comp just in case the RX Thread is sleeping
4172 * and the logout response never got sent because the connection
4173 * failed.
4174 */
4175 complete(&conn->conn_logout_comp);
4176
4177 iscsi_release_thread_set(conn);
4178
4179 iscsit_stop_timers_for_cmds(conn);
4180 iscsit_stop_nopin_response_timer(conn);
4181 iscsit_stop_nopin_timer(conn);
4182 iscsit_free_queue_reqs_for_conn(conn);
4183
4184 /*
4185 * During Connection recovery drop unacknowledged out of order
4186 * commands for this connection, and prepare the other commands
4187 * for realligence.
4188 *
4189 * During normal operation clear the out of order commands (but
4190 * do not free the struct iscsi_ooo_cmdsn's) and release all
4191 * struct iscsi_cmds.
4192 */
4193 if (atomic_read(&conn->connection_recovery)) {
4194 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4195 iscsit_prepare_cmds_for_realligance(conn);
4196 } else {
4197 iscsit_clear_ooo_cmdsns_for_conn(conn);
4198 iscsit_release_commands_from_conn(conn);
4199 }
4200
4201 /*
4202 * Handle decrementing session or connection usage count if
4203 * a logout response was not able to be sent because the
4204 * connection failed. Fall back to Session Recovery here.
4205 */
4206 if (atomic_read(&conn->conn_logout_remove)) {
4207 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4208 iscsit_dec_conn_usage_count(conn);
4209 iscsit_dec_session_usage_count(sess);
4210 }
4211 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4212 iscsit_dec_conn_usage_count(conn);
4213
4214 atomic_set(&conn->conn_logout_remove, 0);
4215 atomic_set(&sess->session_reinstatement, 0);
4216 atomic_set(&sess->session_fall_back_to_erl0, 1);
4217 }
4218
4219 spin_lock_bh(&sess->conn_lock);
4220 list_del(&conn->conn_list);
4221
4222 /*
4223 * Attempt to let the Initiator know this connection failed by
4224 * sending an Connection Dropped Async Message on another
4225 * active connection.
4226 */
4227 if (atomic_read(&conn->connection_recovery))
4228 iscsit_build_conn_drop_async_message(conn);
4229
4230 spin_unlock_bh(&sess->conn_lock);
4231
4232 /*
4233 * If connection reinstatement is being performed on this connection,
4234 * up the connection reinstatement semaphore that is being blocked on
4235 * in iscsit_cause_connection_reinstatement().
4236 */
4237 spin_lock_bh(&conn->state_lock);
4238 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4239 spin_unlock_bh(&conn->state_lock);
4240 complete(&conn->conn_wait_comp);
4241 wait_for_completion(&conn->conn_post_wait_comp);
4242 spin_lock_bh(&conn->state_lock);
4243 }
4244
4245 /*
4246 * If connection reinstatement is being performed on this connection
4247 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4248 * connection wait rcfr semaphore that is being blocked on
4249 * an iscsit_connection_reinstatement_rcfr().
4250 */
4251 if (atomic_read(&conn->connection_wait_rcfr)) {
4252 spin_unlock_bh(&conn->state_lock);
4253 complete(&conn->conn_wait_rcfr_comp);
4254 wait_for_completion(&conn->conn_post_wait_comp);
4255 spin_lock_bh(&conn->state_lock);
4256 }
4257 atomic_set(&conn->connection_reinstatement, 1);
4258 spin_unlock_bh(&conn->state_lock);
4259
4260 /*
4261 * If any other processes are accessing this connection pointer we
4262 * must wait until they have completed.
4263 */
4264 iscsit_check_conn_usage_count(conn);
4265
4266 if (conn->conn_rx_hash.tfm)
4267 crypto_free_hash(conn->conn_rx_hash.tfm);
4268 if (conn->conn_tx_hash.tfm)
4269 crypto_free_hash(conn->conn_tx_hash.tfm);
4270
4271 if (conn->conn_cpumask)
4272 free_cpumask_var(conn->conn_cpumask);
4273
4274 kfree(conn->conn_ops);
4275 conn->conn_ops = NULL;
4276
Al Virobf6932f2012-07-21 08:55:18 +01004277 if (conn->sock)
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004278 sock_release(conn->sock);
Nicholas Bellingerbaa4d642013-03-06 21:54:13 -08004279
4280 if (conn->conn_transport->iscsit_free_conn)
4281 conn->conn_transport->iscsit_free_conn(conn);
4282
4283 iscsit_put_transport(conn->conn_transport);
4284
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004285 conn->thread_set = NULL;
4286
4287 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4288 conn->conn_state = TARG_CONN_STATE_FREE;
4289 kfree(conn);
4290
4291 spin_lock_bh(&sess->conn_lock);
4292 atomic_dec(&sess->nconn);
4293 pr_debug("Decremented iSCSI connection count to %hu from node:"
4294 " %s\n", atomic_read(&sess->nconn),
4295 sess->sess_ops->InitiatorName);
4296 /*
4297 * Make sure that if one connection fails in an non ERL=2 iSCSI
4298 * Session that they all fail.
4299 */
4300 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4301 !atomic_read(&sess->session_logout))
4302 atomic_set(&sess->session_fall_back_to_erl0, 1);
4303
4304 /*
4305 * If this was not the last connection in the session, and we are
4306 * performing session reinstatement or falling back to ERL=0, call
4307 * iscsit_stop_session() without sleeping to shutdown the other
4308 * active connections.
4309 */
4310 if (atomic_read(&sess->nconn)) {
4311 if (!atomic_read(&sess->session_reinstatement) &&
4312 !atomic_read(&sess->session_fall_back_to_erl0)) {
4313 spin_unlock_bh(&sess->conn_lock);
4314 return 0;
4315 }
4316 if (!atomic_read(&sess->session_stop_active)) {
4317 atomic_set(&sess->session_stop_active, 1);
4318 spin_unlock_bh(&sess->conn_lock);
4319 iscsit_stop_session(sess, 0, 0);
4320 return 0;
4321 }
4322 spin_unlock_bh(&sess->conn_lock);
4323 return 0;
4324 }
4325
4326 /*
4327 * If this was the last connection in the session and one of the
4328 * following is occurring:
4329 *
4330 * Session Reinstatement is not being performed, and are falling back
4331 * to ERL=0 call iscsit_close_session().
4332 *
4333 * Session Logout was requested. iscsit_close_session() will be called
4334 * elsewhere.
4335 *
4336 * Session Continuation is not being performed, start the Time2Retain
4337 * handler and check if sleep_on_sess_wait_sem is active.
4338 */
4339 if (!atomic_read(&sess->session_reinstatement) &&
4340 atomic_read(&sess->session_fall_back_to_erl0)) {
4341 spin_unlock_bh(&sess->conn_lock);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004342 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004343
4344 return 0;
4345 } else if (atomic_read(&sess->session_logout)) {
4346 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4347 sess->session_state = TARG_SESS_STATE_FREE;
4348 spin_unlock_bh(&sess->conn_lock);
4349
4350 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4351 complete(&sess->session_wait_comp);
4352
4353 return 0;
4354 } else {
4355 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4356 sess->session_state = TARG_SESS_STATE_FAILED;
4357
4358 if (!atomic_read(&sess->session_continuation)) {
4359 spin_unlock_bh(&sess->conn_lock);
4360 iscsit_start_time2retain_handler(sess);
4361 } else
4362 spin_unlock_bh(&sess->conn_lock);
4363
4364 if (atomic_read(&sess->sleep_on_sess_wait_comp))
4365 complete(&sess->session_wait_comp);
4366
4367 return 0;
4368 }
4369 spin_unlock_bh(&sess->conn_lock);
4370
4371 return 0;
4372}
4373
4374int iscsit_close_session(struct iscsi_session *sess)
4375{
4376 struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
4377 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4378
4379 if (atomic_read(&sess->nconn)) {
4380 pr_err("%d connection(s) still exist for iSCSI session"
4381 " to %s\n", atomic_read(&sess->nconn),
4382 sess->sess_ops->InitiatorName);
4383 BUG();
4384 }
4385
4386 spin_lock_bh(&se_tpg->session_lock);
4387 atomic_set(&sess->session_logout, 1);
4388 atomic_set(&sess->session_reinstatement, 1);
4389 iscsit_stop_time2retain_timer(sess);
4390 spin_unlock_bh(&se_tpg->session_lock);
4391
4392 /*
4393 * transport_deregister_session_configfs() will clear the
4394 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4395 * can be setting it again with __transport_register_session() in
4396 * iscsi_post_login_handler() again after the iscsit_stop_session()
4397 * completes in iscsi_np context.
4398 */
4399 transport_deregister_session_configfs(sess->se_sess);
4400
4401 /*
4402 * If any other processes are accessing this session pointer we must
4403 * wait until they have completed. If we are in an interrupt (the
4404 * time2retain handler) and contain and active session usage count we
4405 * restart the timer and exit.
4406 */
4407 if (!in_interrupt()) {
4408 if (iscsit_check_session_usage_count(sess) == 1)
4409 iscsit_stop_session(sess, 1, 1);
4410 } else {
4411 if (iscsit_check_session_usage_count(sess) == 2) {
4412 atomic_set(&sess->session_logout, 0);
4413 iscsit_start_time2retain_handler(sess);
4414 return 0;
4415 }
4416 }
4417
4418 transport_deregister_session(sess->se_sess);
4419
4420 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4421 iscsit_free_connection_recovery_entires(sess);
4422
4423 iscsit_free_all_ooo_cmdsns(sess);
4424
4425 spin_lock_bh(&se_tpg->session_lock);
4426 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4427 sess->session_state = TARG_SESS_STATE_FREE;
4428 pr_debug("Released iSCSI session from node: %s\n",
4429 sess->sess_ops->InitiatorName);
4430 tpg->nsessions--;
4431 if (tpg->tpg_tiqn)
4432 tpg->tpg_tiqn->tiqn_nsessions--;
4433
4434 pr_debug("Decremented number of active iSCSI Sessions on"
4435 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4436
4437 spin_lock(&sess_idr_lock);
4438 idr_remove(&sess_idr, sess->session_index);
4439 spin_unlock(&sess_idr_lock);
4440
4441 kfree(sess->sess_ops);
4442 sess->sess_ops = NULL;
4443 spin_unlock_bh(&se_tpg->session_lock);
4444
4445 kfree(sess);
4446 return 0;
4447}
4448
4449static void iscsit_logout_post_handler_closesession(
4450 struct iscsi_conn *conn)
4451{
4452 struct iscsi_session *sess = conn->sess;
4453
4454 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4455 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4456
4457 atomic_set(&conn->conn_logout_remove, 0);
4458 complete(&conn->conn_logout_comp);
4459
4460 iscsit_dec_conn_usage_count(conn);
4461 iscsit_stop_session(sess, 1, 1);
4462 iscsit_dec_session_usage_count(sess);
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004463 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004464}
4465
4466static void iscsit_logout_post_handler_samecid(
4467 struct iscsi_conn *conn)
4468{
4469 iscsi_set_thread_clear(conn, ISCSI_CLEAR_TX_THREAD);
4470 iscsi_set_thread_set_signal(conn, ISCSI_SIGNAL_TX_THREAD);
4471
4472 atomic_set(&conn->conn_logout_remove, 0);
4473 complete(&conn->conn_logout_comp);
4474
4475 iscsit_cause_connection_reinstatement(conn, 1);
4476 iscsit_dec_conn_usage_count(conn);
4477}
4478
4479static void iscsit_logout_post_handler_diffcid(
4480 struct iscsi_conn *conn,
4481 u16 cid)
4482{
4483 struct iscsi_conn *l_conn;
4484 struct iscsi_session *sess = conn->sess;
4485
4486 if (!sess)
4487 return;
4488
4489 spin_lock_bh(&sess->conn_lock);
4490 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4491 if (l_conn->cid == cid) {
4492 iscsit_inc_conn_usage_count(l_conn);
4493 break;
4494 }
4495 }
4496 spin_unlock_bh(&sess->conn_lock);
4497
4498 if (!l_conn)
4499 return;
4500
4501 if (l_conn->sock)
4502 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4503
4504 spin_lock_bh(&l_conn->state_lock);
4505 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4506 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4507 spin_unlock_bh(&l_conn->state_lock);
4508
4509 iscsit_cause_connection_reinstatement(l_conn, 1);
4510 iscsit_dec_conn_usage_count(l_conn);
4511}
4512
4513/*
4514 * Return of 0 causes the TX thread to restart.
4515 */
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004516int iscsit_logout_post_handler(
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004517 struct iscsi_cmd *cmd,
4518 struct iscsi_conn *conn)
4519{
4520 int ret = 0;
4521
4522 switch (cmd->logout_reason) {
4523 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4524 switch (cmd->logout_response) {
4525 case ISCSI_LOGOUT_SUCCESS:
4526 case ISCSI_LOGOUT_CLEANUP_FAILED:
4527 default:
4528 iscsit_logout_post_handler_closesession(conn);
4529 break;
4530 }
4531 ret = 0;
4532 break;
4533 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4534 if (conn->cid == cmd->logout_cid) {
4535 switch (cmd->logout_response) {
4536 case ISCSI_LOGOUT_SUCCESS:
4537 case ISCSI_LOGOUT_CLEANUP_FAILED:
4538 default:
4539 iscsit_logout_post_handler_samecid(conn);
4540 break;
4541 }
4542 ret = 0;
4543 } else {
4544 switch (cmd->logout_response) {
4545 case ISCSI_LOGOUT_SUCCESS:
4546 iscsit_logout_post_handler_diffcid(conn,
4547 cmd->logout_cid);
4548 break;
4549 case ISCSI_LOGOUT_CID_NOT_FOUND:
4550 case ISCSI_LOGOUT_CLEANUP_FAILED:
4551 default:
4552 break;
4553 }
4554 ret = 1;
4555 }
4556 break;
4557 case ISCSI_LOGOUT_REASON_RECOVERY:
4558 switch (cmd->logout_response) {
4559 case ISCSI_LOGOUT_SUCCESS:
4560 case ISCSI_LOGOUT_CID_NOT_FOUND:
4561 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4562 case ISCSI_LOGOUT_CLEANUP_FAILED:
4563 default:
4564 break;
4565 }
4566 ret = 1;
4567 break;
4568 default:
4569 break;
4570
4571 }
4572 return ret;
4573}
Nicholas Bellinger2ec5a8c2013-03-20 15:29:15 -07004574EXPORT_SYMBOL(iscsit_logout_post_handler);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004575
4576void iscsit_fail_session(struct iscsi_session *sess)
4577{
4578 struct iscsi_conn *conn;
4579
4580 spin_lock_bh(&sess->conn_lock);
4581 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4582 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4583 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4584 }
4585 spin_unlock_bh(&sess->conn_lock);
4586
4587 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4588 sess->session_state = TARG_SESS_STATE_FAILED;
4589}
4590
4591int iscsit_free_session(struct iscsi_session *sess)
4592{
4593 u16 conn_count = atomic_read(&sess->nconn);
4594 struct iscsi_conn *conn, *conn_tmp = NULL;
4595 int is_last;
4596
4597 spin_lock_bh(&sess->conn_lock);
4598 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4599
4600 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4601 conn_list) {
4602 if (conn_count == 0)
4603 break;
4604
4605 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4606 is_last = 1;
4607 } else {
4608 iscsit_inc_conn_usage_count(conn_tmp);
4609 is_last = 0;
4610 }
4611 iscsit_inc_conn_usage_count(conn);
4612
4613 spin_unlock_bh(&sess->conn_lock);
4614 iscsit_cause_connection_reinstatement(conn, 1);
4615 spin_lock_bh(&sess->conn_lock);
4616
4617 iscsit_dec_conn_usage_count(conn);
4618 if (is_last == 0)
4619 iscsit_dec_conn_usage_count(conn_tmp);
4620
4621 conn_count--;
4622 }
4623
4624 if (atomic_read(&sess->nconn)) {
4625 spin_unlock_bh(&sess->conn_lock);
4626 wait_for_completion(&sess->session_wait_comp);
4627 } else
4628 spin_unlock_bh(&sess->conn_lock);
4629
Nicholas Bellinger99367f02012-02-27 01:43:32 -08004630 target_put_session(sess->se_sess);
Nicholas Bellingere48354c2011-07-23 06:43:04 +00004631 return 0;
4632}
4633
4634void iscsit_stop_session(
4635 struct iscsi_session *sess,
4636 int session_sleep,
4637 int connection_sleep)
4638{
4639 u16 conn_count = atomic_read(&sess->nconn);
4640 struct iscsi_conn *conn, *conn_tmp = NULL;
4641 int is_last;
4642
4643 spin_lock_bh(&sess->conn_lock);
4644 if (session_sleep)
4645 atomic_set(&sess->sleep_on_sess_wait_comp, 1);
4646
4647 if (connection_sleep) {
4648 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4649 conn_list) {
4650 if (conn_count == 0)
4651 break;
4652
4653 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4654 is_last = 1;
4655 } else {
4656 iscsit_inc_conn_usage_count(conn_tmp);
4657 is_last = 0;
4658 }
4659 iscsit_inc_conn_usage_count(conn);
4660
4661 spin_unlock_bh(&sess->conn_lock);
4662 iscsit_cause_connection_reinstatement(conn, 1);
4663 spin_lock_bh(&sess->conn_lock);
4664
4665 iscsit_dec_conn_usage_count(conn);
4666 if (is_last == 0)
4667 iscsit_dec_conn_usage_count(conn_tmp);
4668 conn_count--;
4669 }
4670 } else {
4671 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4672 iscsit_cause_connection_reinstatement(conn, 0);
4673 }
4674
4675 if (session_sleep && atomic_read(&sess->nconn)) {
4676 spin_unlock_bh(&sess->conn_lock);
4677 wait_for_completion(&sess->session_wait_comp);
4678 } else
4679 spin_unlock_bh(&sess->conn_lock);
4680}
4681
4682int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4683{
4684 struct iscsi_session *sess;
4685 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4686 struct se_session *se_sess, *se_sess_tmp;
4687 int session_count = 0;
4688
4689 spin_lock_bh(&se_tpg->session_lock);
4690 if (tpg->nsessions && !force) {
4691 spin_unlock_bh(&se_tpg->session_lock);
4692 return -1;
4693 }
4694
4695 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4696 sess_list) {
4697 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4698
4699 spin_lock(&sess->conn_lock);
4700 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4701 atomic_read(&sess->session_logout) ||
4702 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4703 spin_unlock(&sess->conn_lock);
4704 continue;
4705 }
4706 atomic_set(&sess->session_reinstatement, 1);
4707 spin_unlock(&sess->conn_lock);
4708 spin_unlock_bh(&se_tpg->session_lock);
4709
4710 iscsit_free_session(sess);
4711 spin_lock_bh(&se_tpg->session_lock);
4712
4713 session_count++;
4714 }
4715 spin_unlock_bh(&se_tpg->session_lock);
4716
4717 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4718 " Group: %hu\n", session_count, tpg->tpgt);
4719 return 0;
4720}
4721
4722MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4723MODULE_VERSION("4.1.x");
4724MODULE_AUTHOR("nab@Linux-iSCSI.org");
4725MODULE_LICENSE("GPL");
4726
4727module_init(iscsi_target_init_module);
4728module_exit(iscsi_target_cleanup_module);