1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains main functions related to the iSCSI Target Core Driver.
4 *
5 * (c) Copyright 2007-2013 Datera, Inc.
6 *
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 *
9 ******************************************************************************/
10
11 #include <crypto/hash.h>
12 #include <linux/string.h>
13 #include <linux/kthread.h>
14 #include <linux/completion.h>
15 #include <linux/module.h>
16 #include <linux/vmalloc.h>
17 #include <linux/idr.h>
18 #include <linux/delay.h>
19 #include <linux/sched/signal.h>
20 #include <asm/unaligned.h>
21 #include <linux/inet.h>
22 #include <net/ipv6.h>
23 #include <scsi/scsi_proto.h>
24 #include <scsi/iscsi_proto.h>
25 #include <scsi/scsi_tcq.h>
26 #include <target/target_core_base.h>
27 #include <target/target_core_fabric.h>
28
29 #include <target/iscsi/iscsi_target_core.h>
30 #include "iscsi_target_parameters.h"
31 #include "iscsi_target_seq_pdu_list.h"
32 #include "iscsi_target_datain_values.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl1.h"
35 #include "iscsi_target_erl2.h"
36 #include "iscsi_target_login.h"
37 #include "iscsi_target_tmr.h"
38 #include "iscsi_target_tpg.h"
39 #include "iscsi_target_util.h"
40 #include "iscsi_target.h"
41 #include "iscsi_target_device.h"
42 #include <target/iscsi/iscsi_target_stat.h>
43
44 #include <target/iscsi/iscsi_transport.h>
45
46 static LIST_HEAD(g_tiqn_list);
47 static LIST_HEAD(g_np_list);
48 static DEFINE_SPINLOCK(tiqn_lock);
49 static DEFINE_MUTEX(np_lock);
50
51 static struct idr tiqn_idr;
52 DEFINE_IDA(sess_ida);
53 struct mutex auth_id_lock;
54
55 struct iscsit_global *iscsit_global;
56
57 struct kmem_cache *lio_qr_cache;
58 struct kmem_cache *lio_dr_cache;
59 struct kmem_cache *lio_ooo_cache;
60 struct kmem_cache *lio_r2t_cache;
61
62 static int iscsit_handle_immediate_data(struct iscsi_cmd *,
63 struct iscsi_scsi_req *, u32);
64
iscsit_get_tiqn_for_login(unsigned char * buf)65 struct iscsi_tiqn *iscsit_get_tiqn_for_login(unsigned char *buf)
66 {
67 struct iscsi_tiqn *tiqn = NULL;
68
69 spin_lock(&tiqn_lock);
70 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
71 if (!strcmp(tiqn->tiqn, buf)) {
72
73 spin_lock(&tiqn->tiqn_state_lock);
74 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
75 tiqn->tiqn_access_count++;
76 spin_unlock(&tiqn->tiqn_state_lock);
77 spin_unlock(&tiqn_lock);
78 return tiqn;
79 }
80 spin_unlock(&tiqn->tiqn_state_lock);
81 }
82 }
83 spin_unlock(&tiqn_lock);
84
85 return NULL;
86 }
87
iscsit_set_tiqn_shutdown(struct iscsi_tiqn * tiqn)88 static int iscsit_set_tiqn_shutdown(struct iscsi_tiqn *tiqn)
89 {
90 spin_lock(&tiqn->tiqn_state_lock);
91 if (tiqn->tiqn_state == TIQN_STATE_ACTIVE) {
92 tiqn->tiqn_state = TIQN_STATE_SHUTDOWN;
93 spin_unlock(&tiqn->tiqn_state_lock);
94 return 0;
95 }
96 spin_unlock(&tiqn->tiqn_state_lock);
97
98 return -1;
99 }
100
iscsit_put_tiqn_for_login(struct iscsi_tiqn * tiqn)101 void iscsit_put_tiqn_for_login(struct iscsi_tiqn *tiqn)
102 {
103 spin_lock(&tiqn->tiqn_state_lock);
104 tiqn->tiqn_access_count--;
105 spin_unlock(&tiqn->tiqn_state_lock);
106 }
107
108 /*
109 * Note that IQN formatting is expected to be done in userspace, and
110 * no explict IQN format checks are done here.
111 */
iscsit_add_tiqn(unsigned char * buf)112 struct iscsi_tiqn *iscsit_add_tiqn(unsigned char *buf)
113 {
114 struct iscsi_tiqn *tiqn = NULL;
115 int ret;
116
117 if (strlen(buf) >= ISCSI_IQN_LEN) {
118 pr_err("Target IQN exceeds %d bytes\n",
119 ISCSI_IQN_LEN);
120 return ERR_PTR(-EINVAL);
121 }
122
123 tiqn = kzalloc(sizeof(*tiqn), GFP_KERNEL);
124 if (!tiqn)
125 return ERR_PTR(-ENOMEM);
126
127 sprintf(tiqn->tiqn, "%s", buf);
128 INIT_LIST_HEAD(&tiqn->tiqn_list);
129 INIT_LIST_HEAD(&tiqn->tiqn_tpg_list);
130 spin_lock_init(&tiqn->tiqn_state_lock);
131 spin_lock_init(&tiqn->tiqn_tpg_lock);
132 spin_lock_init(&tiqn->sess_err_stats.lock);
133 spin_lock_init(&tiqn->login_stats.lock);
134 spin_lock_init(&tiqn->logout_stats.lock);
135
136 tiqn->tiqn_state = TIQN_STATE_ACTIVE;
137
138 idr_preload(GFP_KERNEL);
139 spin_lock(&tiqn_lock);
140
141 ret = idr_alloc(&tiqn_idr, NULL, 0, 0, GFP_NOWAIT);
142 if (ret < 0) {
143 pr_err("idr_alloc() failed for tiqn->tiqn_index\n");
144 spin_unlock(&tiqn_lock);
145 idr_preload_end();
146 kfree(tiqn);
147 return ERR_PTR(ret);
148 }
149 tiqn->tiqn_index = ret;
150 list_add_tail(&tiqn->tiqn_list, &g_tiqn_list);
151
152 spin_unlock(&tiqn_lock);
153 idr_preload_end();
154
155 pr_debug("CORE[0] - Added iSCSI Target IQN: %s\n", tiqn->tiqn);
156
157 return tiqn;
158
159 }
160
iscsit_wait_for_tiqn(struct iscsi_tiqn * tiqn)161 static void iscsit_wait_for_tiqn(struct iscsi_tiqn *tiqn)
162 {
163 /*
164 * Wait for accesses to said struct iscsi_tiqn to end.
165 */
166 spin_lock(&tiqn->tiqn_state_lock);
167 while (tiqn->tiqn_access_count != 0) {
168 spin_unlock(&tiqn->tiqn_state_lock);
169 msleep(10);
170 spin_lock(&tiqn->tiqn_state_lock);
171 }
172 spin_unlock(&tiqn->tiqn_state_lock);
173 }
174
iscsit_del_tiqn(struct iscsi_tiqn * tiqn)175 void iscsit_del_tiqn(struct iscsi_tiqn *tiqn)
176 {
177 /*
178 * iscsit_set_tiqn_shutdown sets tiqn->tiqn_state = TIQN_STATE_SHUTDOWN
179 * while holding tiqn->tiqn_state_lock. This means that all subsequent
180 * attempts to access this struct iscsi_tiqn will fail from both transport
181 * fabric and control code paths.
182 */
183 if (iscsit_set_tiqn_shutdown(tiqn) < 0) {
184 pr_err("iscsit_set_tiqn_shutdown() failed\n");
185 return;
186 }
187
188 iscsit_wait_for_tiqn(tiqn);
189
190 spin_lock(&tiqn_lock);
191 list_del(&tiqn->tiqn_list);
192 idr_remove(&tiqn_idr, tiqn->tiqn_index);
193 spin_unlock(&tiqn_lock);
194
195 pr_debug("CORE[0] - Deleted iSCSI Target IQN: %s\n",
196 tiqn->tiqn);
197 kfree(tiqn);
198 }
199
iscsit_access_np(struct iscsi_np * np,struct iscsi_portal_group * tpg)200 int iscsit_access_np(struct iscsi_np *np, struct iscsi_portal_group *tpg)
201 {
202 int ret;
203 /*
204 * Determine if the network portal is accepting storage traffic.
205 */
206 spin_lock_bh(&np->np_thread_lock);
207 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
208 spin_unlock_bh(&np->np_thread_lock);
209 return -1;
210 }
211 spin_unlock_bh(&np->np_thread_lock);
212 /*
213 * Determine if the portal group is accepting storage traffic.
214 */
215 spin_lock_bh(&tpg->tpg_state_lock);
216 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
217 spin_unlock_bh(&tpg->tpg_state_lock);
218 return -1;
219 }
220 spin_unlock_bh(&tpg->tpg_state_lock);
221
222 /*
223 * Here we serialize access across the TIQN+TPG Tuple.
224 */
225 ret = down_interruptible(&tpg->np_login_sem);
226 if (ret != 0)
227 return -1;
228
229 spin_lock_bh(&tpg->tpg_state_lock);
230 if (tpg->tpg_state != TPG_STATE_ACTIVE) {
231 spin_unlock_bh(&tpg->tpg_state_lock);
232 up(&tpg->np_login_sem);
233 return -1;
234 }
235 spin_unlock_bh(&tpg->tpg_state_lock);
236
237 return 0;
238 }
239
iscsit_login_kref_put(struct kref * kref)240 void iscsit_login_kref_put(struct kref *kref)
241 {
242 struct iscsi_tpg_np *tpg_np = container_of(kref,
243 struct iscsi_tpg_np, tpg_np_kref);
244
245 complete(&tpg_np->tpg_np_comp);
246 }
247
iscsit_deaccess_np(struct iscsi_np * np,struct iscsi_portal_group * tpg,struct iscsi_tpg_np * tpg_np)248 int iscsit_deaccess_np(struct iscsi_np *np, struct iscsi_portal_group *tpg,
249 struct iscsi_tpg_np *tpg_np)
250 {
251 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
252
253 up(&tpg->np_login_sem);
254
255 if (tpg_np)
256 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
257
258 if (tiqn)
259 iscsit_put_tiqn_for_login(tiqn);
260
261 return 0;
262 }
263
iscsit_check_np_match(struct sockaddr_storage * sockaddr,struct iscsi_np * np,int network_transport)264 bool iscsit_check_np_match(
265 struct sockaddr_storage *sockaddr,
266 struct iscsi_np *np,
267 int network_transport)
268 {
269 struct sockaddr_in *sock_in, *sock_in_e;
270 struct sockaddr_in6 *sock_in6, *sock_in6_e;
271 bool ip_match = false;
272 u16 port, port_e;
273
274 if (sockaddr->ss_family == AF_INET6) {
275 sock_in6 = (struct sockaddr_in6 *)sockaddr;
276 sock_in6_e = (struct sockaddr_in6 *)&np->np_sockaddr;
277
278 if (!memcmp(&sock_in6->sin6_addr.in6_u,
279 &sock_in6_e->sin6_addr.in6_u,
280 sizeof(struct in6_addr)))
281 ip_match = true;
282
283 port = ntohs(sock_in6->sin6_port);
284 port_e = ntohs(sock_in6_e->sin6_port);
285 } else {
286 sock_in = (struct sockaddr_in *)sockaddr;
287 sock_in_e = (struct sockaddr_in *)&np->np_sockaddr;
288
289 if (sock_in->sin_addr.s_addr == sock_in_e->sin_addr.s_addr)
290 ip_match = true;
291
292 port = ntohs(sock_in->sin_port);
293 port_e = ntohs(sock_in_e->sin_port);
294 }
295
296 if (ip_match && (port_e == port) &&
297 (np->np_network_transport == network_transport))
298 return true;
299
300 return false;
301 }
302
iscsit_get_np(struct sockaddr_storage * sockaddr,int network_transport)303 static struct iscsi_np *iscsit_get_np(
304 struct sockaddr_storage *sockaddr,
305 int network_transport)
306 {
307 struct iscsi_np *np;
308 bool match;
309
310 lockdep_assert_held(&np_lock);
311
312 list_for_each_entry(np, &g_np_list, np_list) {
313 spin_lock_bh(&np->np_thread_lock);
314 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
315 spin_unlock_bh(&np->np_thread_lock);
316 continue;
317 }
318
319 match = iscsit_check_np_match(sockaddr, np, network_transport);
320 if (match) {
321 /*
322 * Increment the np_exports reference count now to
323 * prevent iscsit_del_np() below from being called
324 * while iscsi_tpg_add_network_portal() is called.
325 */
326 np->np_exports++;
327 spin_unlock_bh(&np->np_thread_lock);
328 return np;
329 }
330 spin_unlock_bh(&np->np_thread_lock);
331 }
332
333 return NULL;
334 }
335
iscsit_add_np(struct sockaddr_storage * sockaddr,int network_transport)336 struct iscsi_np *iscsit_add_np(
337 struct sockaddr_storage *sockaddr,
338 int network_transport)
339 {
340 struct iscsi_np *np;
341 int ret;
342
343 mutex_lock(&np_lock);
344
345 /*
346 * Locate the existing struct iscsi_np if already active..
347 */
348 np = iscsit_get_np(sockaddr, network_transport);
349 if (np) {
350 mutex_unlock(&np_lock);
351 return np;
352 }
353
354 np = kzalloc(sizeof(*np), GFP_KERNEL);
355 if (!np) {
356 mutex_unlock(&np_lock);
357 return ERR_PTR(-ENOMEM);
358 }
359
360 np->np_flags |= NPF_IP_NETWORK;
361 np->np_network_transport = network_transport;
362 spin_lock_init(&np->np_thread_lock);
363 init_completion(&np->np_restart_comp);
364 INIT_LIST_HEAD(&np->np_list);
365
366 timer_setup(&np->np_login_timer, iscsi_handle_login_thread_timeout, 0);
367
368 ret = iscsi_target_setup_login_socket(np, sockaddr);
369 if (ret != 0) {
370 kfree(np);
371 mutex_unlock(&np_lock);
372 return ERR_PTR(ret);
373 }
374
375 np->np_thread = kthread_run(iscsi_target_login_thread, np, "iscsi_np");
376 if (IS_ERR(np->np_thread)) {
377 pr_err("Unable to create kthread: iscsi_np\n");
378 ret = PTR_ERR(np->np_thread);
379 kfree(np);
380 mutex_unlock(&np_lock);
381 return ERR_PTR(ret);
382 }
383 /*
384 * Increment the np_exports reference count now to prevent
385 * iscsit_del_np() below from being run while a new call to
386 * iscsi_tpg_add_network_portal() for a matching iscsi_np is
387 * active. We don't need to hold np->np_thread_lock at this
388 * point because iscsi_np has not been added to g_np_list yet.
389 */
390 np->np_exports = 1;
391 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
392
393 list_add_tail(&np->np_list, &g_np_list);
394 mutex_unlock(&np_lock);
395
396 pr_debug("CORE[0] - Added Network Portal: %pISpc on %s\n",
397 &np->np_sockaddr, np->np_transport->name);
398
399 return np;
400 }
401
iscsit_reset_np_thread(struct iscsi_np * np,struct iscsi_tpg_np * tpg_np,struct iscsi_portal_group * tpg,bool shutdown)402 int iscsit_reset_np_thread(
403 struct iscsi_np *np,
404 struct iscsi_tpg_np *tpg_np,
405 struct iscsi_portal_group *tpg,
406 bool shutdown)
407 {
408 spin_lock_bh(&np->np_thread_lock);
409 if (np->np_thread_state == ISCSI_NP_THREAD_INACTIVE) {
410 spin_unlock_bh(&np->np_thread_lock);
411 return 0;
412 }
413 np->np_thread_state = ISCSI_NP_THREAD_RESET;
414 atomic_inc(&np->np_reset_count);
415
416 if (np->np_thread) {
417 spin_unlock_bh(&np->np_thread_lock);
418 send_sig(SIGINT, np->np_thread, 1);
419 wait_for_completion(&np->np_restart_comp);
420 spin_lock_bh(&np->np_thread_lock);
421 }
422 spin_unlock_bh(&np->np_thread_lock);
423
424 if (tpg_np && shutdown) {
425 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
426
427 wait_for_completion(&tpg_np->tpg_np_comp);
428 }
429
430 return 0;
431 }
432
iscsit_free_np(struct iscsi_np * np)433 static void iscsit_free_np(struct iscsi_np *np)
434 {
435 if (np->np_socket)
436 sock_release(np->np_socket);
437 }
438
iscsit_del_np(struct iscsi_np * np)439 int iscsit_del_np(struct iscsi_np *np)
440 {
441 spin_lock_bh(&np->np_thread_lock);
442 np->np_exports--;
443 if (np->np_exports) {
444 np->enabled = true;
445 spin_unlock_bh(&np->np_thread_lock);
446 return 0;
447 }
448 np->np_thread_state = ISCSI_NP_THREAD_SHUTDOWN;
449 spin_unlock_bh(&np->np_thread_lock);
450
451 if (np->np_thread) {
452 /*
453 * We need to send the signal to wakeup Linux/Net
454 * which may be sleeping in sock_accept()..
455 */
456 send_sig(SIGINT, np->np_thread, 1);
457 kthread_stop(np->np_thread);
458 np->np_thread = NULL;
459 }
460
461 np->np_transport->iscsit_free_np(np);
462
463 mutex_lock(&np_lock);
464 list_del(&np->np_list);
465 mutex_unlock(&np_lock);
466
467 pr_debug("CORE[0] - Removed Network Portal: %pISpc on %s\n",
468 &np->np_sockaddr, np->np_transport->name);
469
470 iscsit_put_transport(np->np_transport);
471 kfree(np);
472 return 0;
473 }
474
475 static void iscsit_get_rx_pdu(struct iscsi_conn *);
476
iscsit_queue_rsp(struct iscsi_conn * conn,struct iscsi_cmd * cmd)477 int iscsit_queue_rsp(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
478 {
479 return iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
480 }
481 EXPORT_SYMBOL(iscsit_queue_rsp);
482
iscsit_aborted_task(struct iscsi_conn * conn,struct iscsi_cmd * cmd)483 void iscsit_aborted_task(struct iscsi_conn *conn, struct iscsi_cmd *cmd)
484 {
485 spin_lock_bh(&conn->cmd_lock);
486 if (!list_empty(&cmd->i_conn_node))
487 list_del_init(&cmd->i_conn_node);
488 spin_unlock_bh(&conn->cmd_lock);
489
490 __iscsit_free_cmd(cmd, true);
491 }
492 EXPORT_SYMBOL(iscsit_aborted_task);
493
494 static void iscsit_do_crypto_hash_buf(struct ahash_request *, const void *,
495 u32, u32, const void *, void *);
496 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *);
497
498 static int
iscsit_xmit_nondatain_pdu(struct iscsi_conn * conn,struct iscsi_cmd * cmd,const void * data_buf,u32 data_buf_len)499 iscsit_xmit_nondatain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
500 const void *data_buf, u32 data_buf_len)
501 {
502 struct iscsi_hdr *hdr = (struct iscsi_hdr *)cmd->pdu;
503 struct kvec *iov;
504 u32 niov = 0, tx_size = ISCSI_HDR_LEN;
505 int ret;
506
507 iov = &cmd->iov_misc[0];
508 iov[niov].iov_base = cmd->pdu;
509 iov[niov++].iov_len = ISCSI_HDR_LEN;
510
511 if (conn->conn_ops->HeaderDigest) {
512 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
513
514 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, hdr,
515 ISCSI_HDR_LEN, 0, NULL,
516 header_digest);
517
518 iov[0].iov_len += ISCSI_CRC_LEN;
519 tx_size += ISCSI_CRC_LEN;
520 pr_debug("Attaching CRC32C HeaderDigest"
521 " to opcode 0x%x 0x%08x\n",
522 hdr->opcode, *header_digest);
523 }
524
525 if (data_buf_len) {
526 u32 padding = ((-data_buf_len) & 3);
527
528 iov[niov].iov_base = (void *)data_buf;
529 iov[niov++].iov_len = data_buf_len;
530 tx_size += data_buf_len;
531
532 if (padding != 0) {
533 iov[niov].iov_base = &cmd->pad_bytes;
534 iov[niov++].iov_len = padding;
535 tx_size += padding;
536 pr_debug("Attaching %u additional"
537 " padding bytes.\n", padding);
538 }
539
540 if (conn->conn_ops->DataDigest) {
541 iscsit_do_crypto_hash_buf(conn->conn_tx_hash,
542 data_buf, data_buf_len,
543 padding, &cmd->pad_bytes,
544 &cmd->data_crc);
545
546 iov[niov].iov_base = &cmd->data_crc;
547 iov[niov++].iov_len = ISCSI_CRC_LEN;
548 tx_size += ISCSI_CRC_LEN;
549 pr_debug("Attached DataDigest for %u"
550 " bytes opcode 0x%x, CRC 0x%08x\n",
551 data_buf_len, hdr->opcode, cmd->data_crc);
552 }
553 }
554
555 cmd->iov_misc_count = niov;
556 cmd->tx_size = tx_size;
557
558 ret = iscsit_send_tx_data(cmd, conn, 1);
559 if (ret < 0) {
560 iscsit_tx_thread_wait_for_tcp(conn);
561 return ret;
562 }
563
564 return 0;
565 }
566
567 static int iscsit_map_iovec(struct iscsi_cmd *cmd, struct kvec *iov, int nvec,
568 u32 data_offset, u32 data_length);
569 static void iscsit_unmap_iovec(struct iscsi_cmd *);
570 static u32 iscsit_do_crypto_hash_sg(struct ahash_request *, struct iscsi_cmd *,
571 u32, u32, u32, u8 *);
572 static int
iscsit_xmit_datain_pdu(struct iscsi_conn * conn,struct iscsi_cmd * cmd,const struct iscsi_datain * datain)573 iscsit_xmit_datain_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
574 const struct iscsi_datain *datain)
575 {
576 struct kvec *iov;
577 u32 iov_count = 0, tx_size = 0;
578 int ret, iov_ret;
579
580 iov = &cmd->iov_data[0];
581 iov[iov_count].iov_base = cmd->pdu;
582 iov[iov_count++].iov_len = ISCSI_HDR_LEN;
583 tx_size += ISCSI_HDR_LEN;
584
585 if (conn->conn_ops->HeaderDigest) {
586 u32 *header_digest = (u32 *)&cmd->pdu[ISCSI_HDR_LEN];
587
588 iscsit_do_crypto_hash_buf(conn->conn_tx_hash, cmd->pdu,
589 ISCSI_HDR_LEN, 0, NULL,
590 header_digest);
591
592 iov[0].iov_len += ISCSI_CRC_LEN;
593 tx_size += ISCSI_CRC_LEN;
594
595 pr_debug("Attaching CRC32 HeaderDigest for DataIN PDU 0x%08x\n",
596 *header_digest);
597 }
598
599 iov_ret = iscsit_map_iovec(cmd, &cmd->iov_data[iov_count],
600 cmd->orig_iov_data_count - (iov_count + 2),
601 datain->offset, datain->length);
602 if (iov_ret < 0)
603 return -1;
604
605 iov_count += iov_ret;
606 tx_size += datain->length;
607
608 cmd->padding = ((-datain->length) & 3);
609 if (cmd->padding) {
610 iov[iov_count].iov_base = cmd->pad_bytes;
611 iov[iov_count++].iov_len = cmd->padding;
612 tx_size += cmd->padding;
613
614 pr_debug("Attaching %u padding bytes\n", cmd->padding);
615 }
616
617 if (conn->conn_ops->DataDigest) {
618 cmd->data_crc = iscsit_do_crypto_hash_sg(conn->conn_tx_hash,
619 cmd, datain->offset,
620 datain->length,
621 cmd->padding,
622 cmd->pad_bytes);
623
624 iov[iov_count].iov_base = &cmd->data_crc;
625 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
626 tx_size += ISCSI_CRC_LEN;
627
628 pr_debug("Attached CRC32C DataDigest %d bytes, crc 0x%08x\n",
629 datain->length + cmd->padding, cmd->data_crc);
630 }
631
632 cmd->iov_data_count = iov_count;
633 cmd->tx_size = tx_size;
634
635 ret = iscsit_fe_sendpage_sg(cmd, conn);
636
637 iscsit_unmap_iovec(cmd);
638
639 if (ret < 0) {
640 iscsit_tx_thread_wait_for_tcp(conn);
641 return ret;
642 }
643
644 return 0;
645 }
646
iscsit_xmit_pdu(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_datain_req * dr,const void * buf,u32 buf_len)647 static int iscsit_xmit_pdu(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
648 struct iscsi_datain_req *dr, const void *buf,
649 u32 buf_len)
650 {
651 if (dr)
652 return iscsit_xmit_datain_pdu(conn, cmd, buf);
653 else
654 return iscsit_xmit_nondatain_pdu(conn, cmd, buf, buf_len);
655 }
656
iscsit_get_sup_prot_ops(struct iscsi_conn * conn)657 static enum target_prot_op iscsit_get_sup_prot_ops(struct iscsi_conn *conn)
658 {
659 return TARGET_PROT_NORMAL;
660 }
661
662 static struct iscsit_transport iscsi_target_transport = {
663 .name = "iSCSI/TCP",
664 .transport_type = ISCSI_TCP,
665 .rdma_shutdown = false,
666 .owner = NULL,
667 .iscsit_setup_np = iscsit_setup_np,
668 .iscsit_accept_np = iscsit_accept_np,
669 .iscsit_free_np = iscsit_free_np,
670 .iscsit_get_login_rx = iscsit_get_login_rx,
671 .iscsit_put_login_tx = iscsit_put_login_tx,
672 .iscsit_get_dataout = iscsit_build_r2ts_for_cmd,
673 .iscsit_immediate_queue = iscsit_immediate_queue,
674 .iscsit_response_queue = iscsit_response_queue,
675 .iscsit_queue_data_in = iscsit_queue_rsp,
676 .iscsit_queue_status = iscsit_queue_rsp,
677 .iscsit_aborted_task = iscsit_aborted_task,
678 .iscsit_xmit_pdu = iscsit_xmit_pdu,
679 .iscsit_get_rx_pdu = iscsit_get_rx_pdu,
680 .iscsit_get_sup_prot_ops = iscsit_get_sup_prot_ops,
681 };
682
iscsi_target_init_module(void)683 static int __init iscsi_target_init_module(void)
684 {
685 int ret = 0, size;
686
687 pr_debug("iSCSI-Target "ISCSIT_VERSION"\n");
688 iscsit_global = kzalloc(sizeof(*iscsit_global), GFP_KERNEL);
689 if (!iscsit_global)
690 return -1;
691
692 spin_lock_init(&iscsit_global->ts_bitmap_lock);
693 mutex_init(&auth_id_lock);
694 idr_init(&tiqn_idr);
695
696 ret = target_register_template(&iscsi_ops);
697 if (ret)
698 goto out;
699
700 size = BITS_TO_LONGS(ISCSIT_BITMAP_BITS) * sizeof(long);
701 iscsit_global->ts_bitmap = vzalloc(size);
702 if (!iscsit_global->ts_bitmap)
703 goto configfs_out;
704
705 lio_qr_cache = kmem_cache_create("lio_qr_cache",
706 sizeof(struct iscsi_queue_req),
707 __alignof__(struct iscsi_queue_req), 0, NULL);
708 if (!lio_qr_cache) {
709 pr_err("Unable to kmem_cache_create() for"
710 " lio_qr_cache\n");
711 goto bitmap_out;
712 }
713
714 lio_dr_cache = kmem_cache_create("lio_dr_cache",
715 sizeof(struct iscsi_datain_req),
716 __alignof__(struct iscsi_datain_req), 0, NULL);
717 if (!lio_dr_cache) {
718 pr_err("Unable to kmem_cache_create() for"
719 " lio_dr_cache\n");
720 goto qr_out;
721 }
722
723 lio_ooo_cache = kmem_cache_create("lio_ooo_cache",
724 sizeof(struct iscsi_ooo_cmdsn),
725 __alignof__(struct iscsi_ooo_cmdsn), 0, NULL);
726 if (!lio_ooo_cache) {
727 pr_err("Unable to kmem_cache_create() for"
728 " lio_ooo_cache\n");
729 goto dr_out;
730 }
731
732 lio_r2t_cache = kmem_cache_create("lio_r2t_cache",
733 sizeof(struct iscsi_r2t), __alignof__(struct iscsi_r2t),
734 0, NULL);
735 if (!lio_r2t_cache) {
736 pr_err("Unable to kmem_cache_create() for"
737 " lio_r2t_cache\n");
738 goto ooo_out;
739 }
740
741 iscsit_register_transport(&iscsi_target_transport);
742
743 if (iscsit_load_discovery_tpg() < 0)
744 goto r2t_out;
745
746 return ret;
747 r2t_out:
748 iscsit_unregister_transport(&iscsi_target_transport);
749 kmem_cache_destroy(lio_r2t_cache);
750 ooo_out:
751 kmem_cache_destroy(lio_ooo_cache);
752 dr_out:
753 kmem_cache_destroy(lio_dr_cache);
754 qr_out:
755 kmem_cache_destroy(lio_qr_cache);
756 bitmap_out:
757 vfree(iscsit_global->ts_bitmap);
758 configfs_out:
759 /* XXX: this probably wants it to be it's own unwind step.. */
760 if (iscsit_global->discovery_tpg)
761 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
762 target_unregister_template(&iscsi_ops);
763 out:
764 kfree(iscsit_global);
765 return -ENOMEM;
766 }
767
iscsi_target_cleanup_module(void)768 static void __exit iscsi_target_cleanup_module(void)
769 {
770 iscsit_release_discovery_tpg();
771 iscsit_unregister_transport(&iscsi_target_transport);
772 kmem_cache_destroy(lio_qr_cache);
773 kmem_cache_destroy(lio_dr_cache);
774 kmem_cache_destroy(lio_ooo_cache);
775 kmem_cache_destroy(lio_r2t_cache);
776
777 /*
778 * Shutdown discovery sessions and disable discovery TPG
779 */
780 if (iscsit_global->discovery_tpg)
781 iscsit_tpg_disable_portal_group(iscsit_global->discovery_tpg, 1);
782
783 target_unregister_template(&iscsi_ops);
784
785 vfree(iscsit_global->ts_bitmap);
786 kfree(iscsit_global);
787 }
788
iscsit_add_reject(struct iscsi_conn * conn,u8 reason,unsigned char * buf)789 int iscsit_add_reject(
790 struct iscsi_conn *conn,
791 u8 reason,
792 unsigned char *buf)
793 {
794 struct iscsi_cmd *cmd;
795
796 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
797 if (!cmd)
798 return -1;
799
800 cmd->iscsi_opcode = ISCSI_OP_REJECT;
801 cmd->reject_reason = reason;
802
803 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
804 if (!cmd->buf_ptr) {
805 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
806 iscsit_free_cmd(cmd, false);
807 return -1;
808 }
809
810 spin_lock_bh(&conn->cmd_lock);
811 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
812 spin_unlock_bh(&conn->cmd_lock);
813
814 cmd->i_state = ISTATE_SEND_REJECT;
815 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
816
817 return -1;
818 }
819 EXPORT_SYMBOL(iscsit_add_reject);
820
iscsit_add_reject_from_cmd(struct iscsi_cmd * cmd,u8 reason,bool add_to_conn,unsigned char * buf)821 static int iscsit_add_reject_from_cmd(
822 struct iscsi_cmd *cmd,
823 u8 reason,
824 bool add_to_conn,
825 unsigned char *buf)
826 {
827 struct iscsi_conn *conn;
828 const bool do_put = cmd->se_cmd.se_tfo != NULL;
829
830 if (!cmd->conn) {
831 pr_err("cmd->conn is NULL for ITT: 0x%08x\n",
832 cmd->init_task_tag);
833 return -1;
834 }
835 conn = cmd->conn;
836
837 cmd->iscsi_opcode = ISCSI_OP_REJECT;
838 cmd->reject_reason = reason;
839
840 cmd->buf_ptr = kmemdup(buf, ISCSI_HDR_LEN, GFP_KERNEL);
841 if (!cmd->buf_ptr) {
842 pr_err("Unable to allocate memory for cmd->buf_ptr\n");
843 iscsit_free_cmd(cmd, false);
844 return -1;
845 }
846
847 if (add_to_conn) {
848 spin_lock_bh(&conn->cmd_lock);
849 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
850 spin_unlock_bh(&conn->cmd_lock);
851 }
852
853 cmd->i_state = ISTATE_SEND_REJECT;
854 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
855 /*
856 * Perform the kref_put now if se_cmd has already been setup by
857 * scsit_setup_scsi_cmd()
858 */
859 if (do_put) {
860 pr_debug("iscsi reject: calling target_put_sess_cmd >>>>>>\n");
861 target_put_sess_cmd(&cmd->se_cmd);
862 }
863 return -1;
864 }
865
iscsit_add_reject_cmd(struct iscsi_cmd * cmd,u8 reason,unsigned char * buf)866 static int iscsit_add_reject_cmd(struct iscsi_cmd *cmd, u8 reason,
867 unsigned char *buf)
868 {
869 return iscsit_add_reject_from_cmd(cmd, reason, true, buf);
870 }
871
iscsit_reject_cmd(struct iscsi_cmd * cmd,u8 reason,unsigned char * buf)872 int iscsit_reject_cmd(struct iscsi_cmd *cmd, u8 reason, unsigned char *buf)
873 {
874 return iscsit_add_reject_from_cmd(cmd, reason, false, buf);
875 }
876 EXPORT_SYMBOL(iscsit_reject_cmd);
877
878 /*
879 * Map some portion of the allocated scatterlist to an iovec, suitable for
880 * kernel sockets to copy data in/out.
881 */
iscsit_map_iovec(struct iscsi_cmd * cmd,struct kvec * iov,int nvec,u32 data_offset,u32 data_length)882 static int iscsit_map_iovec(struct iscsi_cmd *cmd, struct kvec *iov, int nvec,
883 u32 data_offset, u32 data_length)
884 {
885 u32 i = 0, orig_data_length = data_length;
886 struct scatterlist *sg;
887 unsigned int page_off;
888
889 /*
890 * We know each entry in t_data_sg contains a page.
891 */
892 u32 ent = data_offset / PAGE_SIZE;
893
894 if (!data_length)
895 return 0;
896
897 if (ent >= cmd->se_cmd.t_data_nents) {
898 pr_err("Initial page entry out-of-bounds\n");
899 goto overflow;
900 }
901
902 sg = &cmd->se_cmd.t_data_sg[ent];
903 page_off = (data_offset % PAGE_SIZE);
904
905 cmd->first_data_sg = sg;
906 cmd->first_data_sg_off = page_off;
907
908 while (data_length) {
909 u32 cur_len;
910
911 if (WARN_ON_ONCE(!sg || i >= nvec))
912 goto overflow;
913
914 cur_len = min_t(u32, data_length, sg->length - page_off);
915
916 iov[i].iov_base = kmap(sg_page(sg)) + sg->offset + page_off;
917 iov[i].iov_len = cur_len;
918
919 data_length -= cur_len;
920 page_off = 0;
921 sg = sg_next(sg);
922 i++;
923 }
924
925 cmd->kmapped_nents = i;
926
927 return i;
928
929 overflow:
930 pr_err("offset %d + length %d overflow; %d/%d; sg-list:\n",
931 data_offset, orig_data_length, i, nvec);
932 for_each_sg(cmd->se_cmd.t_data_sg, sg,
933 cmd->se_cmd.t_data_nents, i) {
934 pr_err("[%d] off %d len %d\n",
935 i, sg->offset, sg->length);
936 }
937 return -1;
938 }
939
iscsit_unmap_iovec(struct iscsi_cmd * cmd)940 static void iscsit_unmap_iovec(struct iscsi_cmd *cmd)
941 {
942 u32 i;
943 struct scatterlist *sg;
944
945 sg = cmd->first_data_sg;
946
947 for (i = 0; i < cmd->kmapped_nents; i++)
948 kunmap(sg_page(&sg[i]));
949 }
950
iscsit_ack_from_expstatsn(struct iscsi_conn * conn,u32 exp_statsn)951 static void iscsit_ack_from_expstatsn(struct iscsi_conn *conn, u32 exp_statsn)
952 {
953 LIST_HEAD(ack_list);
954 struct iscsi_cmd *cmd, *cmd_p;
955
956 conn->exp_statsn = exp_statsn;
957
958 if (conn->sess->sess_ops->RDMAExtensions)
959 return;
960
961 spin_lock_bh(&conn->cmd_lock);
962 list_for_each_entry_safe(cmd, cmd_p, &conn->conn_cmd_list, i_conn_node) {
963 spin_lock(&cmd->istate_lock);
964 if ((cmd->i_state == ISTATE_SENT_STATUS) &&
965 iscsi_sna_lt(cmd->stat_sn, exp_statsn)) {
966 cmd->i_state = ISTATE_REMOVE;
967 spin_unlock(&cmd->istate_lock);
968 list_move_tail(&cmd->i_conn_node, &ack_list);
969 continue;
970 }
971 spin_unlock(&cmd->istate_lock);
972 }
973 spin_unlock_bh(&conn->cmd_lock);
974
975 list_for_each_entry_safe(cmd, cmd_p, &ack_list, i_conn_node) {
976 list_del_init(&cmd->i_conn_node);
977 iscsit_free_cmd(cmd, false);
978 }
979 }
980
iscsit_allocate_iovecs(struct iscsi_cmd * cmd)981 static int iscsit_allocate_iovecs(struct iscsi_cmd *cmd)
982 {
983 u32 iov_count = max(1UL, DIV_ROUND_UP(cmd->se_cmd.data_length, PAGE_SIZE));
984
985 iov_count += ISCSI_IOV_DATA_BUFFER;
986 cmd->iov_data = kcalloc(iov_count, sizeof(*cmd->iov_data), GFP_KERNEL);
987 if (!cmd->iov_data)
988 return -ENOMEM;
989
990 cmd->orig_iov_data_count = iov_count;
991 return 0;
992 }
993
iscsit_setup_scsi_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf)994 int iscsit_setup_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
995 unsigned char *buf)
996 {
997 int data_direction, payload_length;
998 struct iscsi_scsi_req *hdr;
999 int iscsi_task_attr;
1000 int sam_task_attr;
1001
1002 atomic_long_inc(&conn->sess->cmd_pdus);
1003
1004 hdr = (struct iscsi_scsi_req *) buf;
1005 payload_length = ntoh24(hdr->dlength);
1006
1007 /* FIXME; Add checks for AdditionalHeaderSegment */
1008
1009 if (!(hdr->flags & ISCSI_FLAG_CMD_WRITE) &&
1010 !(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1011 pr_err("ISCSI_FLAG_CMD_WRITE & ISCSI_FLAG_CMD_FINAL"
1012 " not set. Bad iSCSI Initiator.\n");
1013 return iscsit_add_reject_cmd(cmd,
1014 ISCSI_REASON_BOOKMARK_INVALID, buf);
1015 }
1016
1017 if (((hdr->flags & ISCSI_FLAG_CMD_READ) ||
1018 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) && !hdr->data_length) {
1019 /*
1020 * From RFC-3720 Section 10.3.1:
1021 *
1022 * "Either or both of R and W MAY be 1 when either the
1023 * Expected Data Transfer Length and/or Bidirectional Read
1024 * Expected Data Transfer Length are 0"
1025 *
1026 * For this case, go ahead and clear the unnecssary bits
1027 * to avoid any confusion with ->data_direction.
1028 */
1029 hdr->flags &= ~ISCSI_FLAG_CMD_READ;
1030 hdr->flags &= ~ISCSI_FLAG_CMD_WRITE;
1031
1032 pr_warn("ISCSI_FLAG_CMD_READ or ISCSI_FLAG_CMD_WRITE"
1033 " set when Expected Data Transfer Length is 0 for"
1034 " CDB: 0x%02x, Fixing up flags\n", hdr->cdb[0]);
1035 }
1036
1037 if (!(hdr->flags & ISCSI_FLAG_CMD_READ) &&
1038 !(hdr->flags & ISCSI_FLAG_CMD_WRITE) && (hdr->data_length != 0)) {
1039 pr_err("ISCSI_FLAG_CMD_READ and/or ISCSI_FLAG_CMD_WRITE"
1040 " MUST be set if Expected Data Transfer Length is not 0."
1041 " Bad iSCSI Initiator\n");
1042 return iscsit_add_reject_cmd(cmd,
1043 ISCSI_REASON_BOOKMARK_INVALID, buf);
1044 }
1045
1046 if ((hdr->flags & ISCSI_FLAG_CMD_READ) &&
1047 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) {
1048 pr_err("Bidirectional operations not supported!\n");
1049 return iscsit_add_reject_cmd(cmd,
1050 ISCSI_REASON_BOOKMARK_INVALID, buf);
1051 }
1052
1053 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1054 pr_err("Illegally set Immediate Bit in iSCSI Initiator"
1055 " Scsi Command PDU.\n");
1056 return iscsit_add_reject_cmd(cmd,
1057 ISCSI_REASON_BOOKMARK_INVALID, buf);
1058 }
1059
1060 if (payload_length && !conn->sess->sess_ops->ImmediateData) {
1061 pr_err("ImmediateData=No but DataSegmentLength=%u,"
1062 " protocol error.\n", payload_length);
1063 return iscsit_add_reject_cmd(cmd,
1064 ISCSI_REASON_PROTOCOL_ERROR, buf);
1065 }
1066
1067 if ((be32_to_cpu(hdr->data_length) == payload_length) &&
1068 (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))) {
1069 pr_err("Expected Data Transfer Length and Length of"
1070 " Immediate Data are the same, but ISCSI_FLAG_CMD_FINAL"
1071 " bit is not set protocol error\n");
1072 return iscsit_add_reject_cmd(cmd,
1073 ISCSI_REASON_PROTOCOL_ERROR, buf);
1074 }
1075
1076 if (payload_length > be32_to_cpu(hdr->data_length)) {
1077 pr_err("DataSegmentLength: %u is greater than"
1078 " EDTL: %u, protocol error.\n", payload_length,
1079 hdr->data_length);
1080 return iscsit_add_reject_cmd(cmd,
1081 ISCSI_REASON_PROTOCOL_ERROR, buf);
1082 }
1083
1084 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1085 pr_err("DataSegmentLength: %u is greater than"
1086 " MaxXmitDataSegmentLength: %u, protocol error.\n",
1087 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
1088 return iscsit_add_reject_cmd(cmd,
1089 ISCSI_REASON_PROTOCOL_ERROR, buf);
1090 }
1091
1092 if (payload_length > conn->sess->sess_ops->FirstBurstLength) {
1093 pr_err("DataSegmentLength: %u is greater than"
1094 " FirstBurstLength: %u, protocol error.\n",
1095 payload_length, conn->sess->sess_ops->FirstBurstLength);
1096 return iscsit_add_reject_cmd(cmd,
1097 ISCSI_REASON_BOOKMARK_INVALID, buf);
1098 }
1099
1100 data_direction = (hdr->flags & ISCSI_FLAG_CMD_WRITE) ? DMA_TO_DEVICE :
1101 (hdr->flags & ISCSI_FLAG_CMD_READ) ? DMA_FROM_DEVICE :
1102 DMA_NONE;
1103
1104 cmd->data_direction = data_direction;
1105 iscsi_task_attr = hdr->flags & ISCSI_FLAG_CMD_ATTR_MASK;
1106 /*
1107 * Figure out the SAM Task Attribute for the incoming SCSI CDB
1108 */
1109 if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
1110 (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
1111 sam_task_attr = TCM_SIMPLE_TAG;
1112 else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
1113 sam_task_attr = TCM_ORDERED_TAG;
1114 else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
1115 sam_task_attr = TCM_HEAD_TAG;
1116 else if (iscsi_task_attr == ISCSI_ATTR_ACA)
1117 sam_task_attr = TCM_ACA_TAG;
1118 else {
1119 pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
1120 " TCM_SIMPLE_TAG\n", iscsi_task_attr);
1121 sam_task_attr = TCM_SIMPLE_TAG;
1122 }
1123
1124 cmd->iscsi_opcode = ISCSI_OP_SCSI_CMD;
1125 cmd->i_state = ISTATE_NEW_CMD;
1126 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
1127 cmd->immediate_data = (payload_length) ? 1 : 0;
1128 cmd->unsolicited_data = ((!(hdr->flags & ISCSI_FLAG_CMD_FINAL) &&
1129 (hdr->flags & ISCSI_FLAG_CMD_WRITE)) ? 1 : 0);
1130 if (cmd->unsolicited_data)
1131 cmd->cmd_flags |= ICF_NON_IMMEDIATE_UNSOLICITED_DATA;
1132
1133 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1134 if (hdr->flags & ISCSI_FLAG_CMD_READ)
1135 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
1136 else
1137 cmd->targ_xfer_tag = 0xFFFFFFFF;
1138 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1139 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1140 cmd->first_burst_len = payload_length;
1141
1142 if (!conn->sess->sess_ops->RDMAExtensions &&
1143 cmd->data_direction == DMA_FROM_DEVICE) {
1144 struct iscsi_datain_req *dr;
1145
1146 dr = iscsit_allocate_datain_req();
1147 if (!dr)
1148 return iscsit_add_reject_cmd(cmd,
1149 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1150
1151 iscsit_attach_datain_req(cmd, dr);
1152 }
1153
1154 /*
1155 * Initialize struct se_cmd descriptor from target_core_mod infrastructure
1156 */
1157 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
1158 conn->sess->se_sess, be32_to_cpu(hdr->data_length),
1159 cmd->data_direction, sam_task_attr,
1160 cmd->sense_buffer + 2, scsilun_to_int(&hdr->lun));
1161
1162 pr_debug("Got SCSI Command, ITT: 0x%08x, CmdSN: 0x%08x,"
1163 " ExpXferLen: %u, Length: %u, CID: %hu\n", hdr->itt,
1164 hdr->cmdsn, be32_to_cpu(hdr->data_length), payload_length,
1165 conn->cid);
1166
1167 target_get_sess_cmd(&cmd->se_cmd, true);
1168
1169 cmd->se_cmd.tag = (__force u32)cmd->init_task_tag;
1170 cmd->sense_reason = target_cmd_init_cdb(&cmd->se_cmd, hdr->cdb);
1171 if (cmd->sense_reason) {
1172 if (cmd->sense_reason == TCM_OUT_OF_RESOURCES) {
1173 return iscsit_add_reject_cmd(cmd,
1174 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1175 }
1176
1177 goto attach_cmd;
1178 }
1179
1180 cmd->sense_reason = transport_lookup_cmd_lun(&cmd->se_cmd);
1181 if (cmd->sense_reason)
1182 goto attach_cmd;
1183
1184 cmd->sense_reason = target_cmd_parse_cdb(&cmd->se_cmd);
1185 if (cmd->sense_reason)
1186 goto attach_cmd;
1187
1188 if (iscsit_build_pdu_and_seq_lists(cmd, payload_length) < 0) {
1189 return iscsit_add_reject_cmd(cmd,
1190 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1191 }
1192
1193 attach_cmd:
1194 spin_lock_bh(&conn->cmd_lock);
1195 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1196 spin_unlock_bh(&conn->cmd_lock);
1197 /*
1198 * Check if we need to delay processing because of ALUA
1199 * Active/NonOptimized primary access state..
1200 */
1201 core_alua_check_nonop_delay(&cmd->se_cmd);
1202
1203 return 0;
1204 }
1205 EXPORT_SYMBOL(iscsit_setup_scsi_cmd);
1206
iscsit_set_unsolicited_dataout(struct iscsi_cmd * cmd)1207 void iscsit_set_unsolicited_dataout(struct iscsi_cmd *cmd)
1208 {
1209 iscsit_set_dataout_sequence_values(cmd);
1210
1211 spin_lock_bh(&cmd->dataout_timeout_lock);
1212 iscsit_start_dataout_timer(cmd, cmd->conn);
1213 spin_unlock_bh(&cmd->dataout_timeout_lock);
1214 }
1215 EXPORT_SYMBOL(iscsit_set_unsolicited_dataout);
1216
iscsit_process_scsi_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_scsi_req * hdr)1217 int iscsit_process_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1218 struct iscsi_scsi_req *hdr)
1219 {
1220 int cmdsn_ret = 0;
1221 /*
1222 * Check the CmdSN against ExpCmdSN/MaxCmdSN here if
1223 * the Immediate Bit is not set, and no Immediate
1224 * Data is attached.
1225 *
1226 * A PDU/CmdSN carrying Immediate Data can only
1227 * be processed after the DataCRC has passed.
1228 * If the DataCRC fails, the CmdSN MUST NOT
1229 * be acknowledged. (See below)
1230 */
1231 if (!cmd->immediate_data) {
1232 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1233 (unsigned char *)hdr, hdr->cmdsn);
1234 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1235 return -1;
1236 else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1237 target_put_sess_cmd(&cmd->se_cmd);
1238 return 0;
1239 }
1240 }
1241
1242 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1243
1244 /*
1245 * If no Immediate Data is attached, it's OK to return now.
1246 */
1247 if (!cmd->immediate_data) {
1248 if (!cmd->sense_reason && cmd->unsolicited_data)
1249 iscsit_set_unsolicited_dataout(cmd);
1250 if (!cmd->sense_reason)
1251 return 0;
1252
1253 target_put_sess_cmd(&cmd->se_cmd);
1254 return 0;
1255 }
1256
1257 /*
1258 * Early CHECK_CONDITIONs with ImmediateData never make it to command
1259 * execution. These exceptions are processed in CmdSN order using
1260 * iscsit_check_received_cmdsn() in iscsit_get_immediate_data() below.
1261 */
1262 if (cmd->sense_reason)
1263 return 1;
1264 /*
1265 * Call directly into transport_generic_new_cmd() to perform
1266 * the backend memory allocation.
1267 */
1268 cmd->sense_reason = transport_generic_new_cmd(&cmd->se_cmd);
1269 if (cmd->sense_reason)
1270 return 1;
1271
1272 return 0;
1273 }
1274 EXPORT_SYMBOL(iscsit_process_scsi_cmd);
1275
1276 static int
iscsit_get_immediate_data(struct iscsi_cmd * cmd,struct iscsi_scsi_req * hdr,bool dump_payload)1277 iscsit_get_immediate_data(struct iscsi_cmd *cmd, struct iscsi_scsi_req *hdr,
1278 bool dump_payload)
1279 {
1280 int cmdsn_ret = 0, immed_ret = IMMEDIATE_DATA_NORMAL_OPERATION;
1281 int rc;
1282
1283 /*
1284 * Special case for Unsupported SAM WRITE Opcodes and ImmediateData=Yes.
1285 */
1286 if (dump_payload) {
1287 u32 length = min(cmd->se_cmd.data_length - cmd->write_data_done,
1288 cmd->first_burst_len);
1289
1290 pr_debug("Dumping min(%d - %d, %d) = %d bytes of immediate data\n",
1291 cmd->se_cmd.data_length, cmd->write_data_done,
1292 cmd->first_burst_len, length);
1293 rc = iscsit_dump_data_payload(cmd->conn, length, 1);
1294 pr_debug("Finished dumping immediate data\n");
1295 if (rc < 0)
1296 immed_ret = IMMEDIATE_DATA_CANNOT_RECOVER;
1297 } else {
1298 immed_ret = iscsit_handle_immediate_data(cmd, hdr,
1299 cmd->first_burst_len);
1300 }
1301
1302 if (immed_ret == IMMEDIATE_DATA_NORMAL_OPERATION) {
1303 /*
1304 * A PDU/CmdSN carrying Immediate Data passed
1305 * DataCRC, check against ExpCmdSN/MaxCmdSN if
1306 * Immediate Bit is not set.
1307 */
1308 cmdsn_ret = iscsit_sequence_cmd(cmd->conn, cmd,
1309 (unsigned char *)hdr, hdr->cmdsn);
1310 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1311 return -1;
1312
1313 if (cmd->sense_reason || cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
1314 target_put_sess_cmd(&cmd->se_cmd);
1315
1316 return 0;
1317 } else if (cmd->unsolicited_data)
1318 iscsit_set_unsolicited_dataout(cmd);
1319
1320 } else if (immed_ret == IMMEDIATE_DATA_ERL1_CRC_FAILURE) {
1321 /*
1322 * Immediate Data failed DataCRC and ERL>=1,
1323 * silently drop this PDU and let the initiator
1324 * plug the CmdSN gap.
1325 *
1326 * FIXME: Send Unsolicited NOPIN with reserved
1327 * TTT here to help the initiator figure out
1328 * the missing CmdSN, although they should be
1329 * intelligent enough to determine the missing
1330 * CmdSN and issue a retry to plug the sequence.
1331 */
1332 cmd->i_state = ISTATE_REMOVE;
1333 iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, cmd->i_state);
1334 } else /* immed_ret == IMMEDIATE_DATA_CANNOT_RECOVER */
1335 return -1;
1336
1337 return 0;
1338 }
1339
1340 static int
iscsit_handle_scsi_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf)1341 iscsit_handle_scsi_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1342 unsigned char *buf)
1343 {
1344 struct iscsi_scsi_req *hdr = (struct iscsi_scsi_req *)buf;
1345 int rc, immed_data;
1346 bool dump_payload = false;
1347
1348 rc = iscsit_setup_scsi_cmd(conn, cmd, buf);
1349 if (rc < 0)
1350 return 0;
1351 /*
1352 * Allocation iovecs needed for struct socket operations for
1353 * traditional iSCSI block I/O.
1354 */
1355 if (iscsit_allocate_iovecs(cmd) < 0) {
1356 return iscsit_reject_cmd(cmd,
1357 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
1358 }
1359 immed_data = cmd->immediate_data;
1360
1361 rc = iscsit_process_scsi_cmd(conn, cmd, hdr);
1362 if (rc < 0)
1363 return rc;
1364 else if (rc > 0)
1365 dump_payload = true;
1366
1367 if (!immed_data)
1368 return 0;
1369
1370 return iscsit_get_immediate_data(cmd, hdr, dump_payload);
1371 }
1372
iscsit_do_crypto_hash_sg(struct ahash_request * hash,struct iscsi_cmd * cmd,u32 data_offset,u32 data_length,u32 padding,u8 * pad_bytes)1373 static u32 iscsit_do_crypto_hash_sg(
1374 struct ahash_request *hash,
1375 struct iscsi_cmd *cmd,
1376 u32 data_offset,
1377 u32 data_length,
1378 u32 padding,
1379 u8 *pad_bytes)
1380 {
1381 u32 data_crc;
1382 struct scatterlist *sg;
1383 unsigned int page_off;
1384
1385 crypto_ahash_init(hash);
1386
1387 sg = cmd->first_data_sg;
1388 page_off = cmd->first_data_sg_off;
1389
1390 if (data_length && page_off) {
1391 struct scatterlist first_sg;
1392 u32 len = min_t(u32, data_length, sg->length - page_off);
1393
1394 sg_init_table(&first_sg, 1);
1395 sg_set_page(&first_sg, sg_page(sg), len, sg->offset + page_off);
1396
1397 ahash_request_set_crypt(hash, &first_sg, NULL, len);
1398 crypto_ahash_update(hash);
1399
1400 data_length -= len;
1401 sg = sg_next(sg);
1402 }
1403
1404 while (data_length) {
1405 u32 cur_len = min_t(u32, data_length, sg->length);
1406
1407 ahash_request_set_crypt(hash, sg, NULL, cur_len);
1408 crypto_ahash_update(hash);
1409
1410 data_length -= cur_len;
1411 /* iscsit_map_iovec has already checked for invalid sg pointers */
1412 sg = sg_next(sg);
1413 }
1414
1415 if (padding) {
1416 struct scatterlist pad_sg;
1417
1418 sg_init_one(&pad_sg, pad_bytes, padding);
1419 ahash_request_set_crypt(hash, &pad_sg, (u8 *)&data_crc,
1420 padding);
1421 crypto_ahash_finup(hash);
1422 } else {
1423 ahash_request_set_crypt(hash, NULL, (u8 *)&data_crc, 0);
1424 crypto_ahash_final(hash);
1425 }
1426
1427 return data_crc;
1428 }
1429
iscsit_do_crypto_hash_buf(struct ahash_request * hash,const void * buf,u32 payload_length,u32 padding,const void * pad_bytes,void * data_crc)1430 static void iscsit_do_crypto_hash_buf(struct ahash_request *hash,
1431 const void *buf, u32 payload_length, u32 padding,
1432 const void *pad_bytes, void *data_crc)
1433 {
1434 struct scatterlist sg[2];
1435
1436 sg_init_table(sg, ARRAY_SIZE(sg));
1437 sg_set_buf(sg, buf, payload_length);
1438 if (padding)
1439 sg_set_buf(sg + 1, pad_bytes, padding);
1440
1441 ahash_request_set_crypt(hash, sg, data_crc, payload_length + padding);
1442
1443 crypto_ahash_digest(hash);
1444 }
1445
1446 int
__iscsit_check_dataout_hdr(struct iscsi_conn * conn,void * buf,struct iscsi_cmd * cmd,u32 payload_length,bool * success)1447 __iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1448 struct iscsi_cmd *cmd, u32 payload_length,
1449 bool *success)
1450 {
1451 struct iscsi_data *hdr = buf;
1452 struct se_cmd *se_cmd;
1453 int rc;
1454
1455 /* iSCSI write */
1456 atomic_long_add(payload_length, &conn->sess->rx_data_octets);
1457
1458 pr_debug("Got DataOut ITT: 0x%08x, TTT: 0x%08x,"
1459 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
1460 hdr->itt, hdr->ttt, hdr->datasn, ntohl(hdr->offset),
1461 payload_length, conn->cid);
1462
1463 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
1464 pr_err("Command ITT: 0x%08x received DataOUT after"
1465 " last DataOUT received, dumping payload\n",
1466 cmd->init_task_tag);
1467 return iscsit_dump_data_payload(conn, payload_length, 1);
1468 }
1469
1470 if (cmd->data_direction != DMA_TO_DEVICE) {
1471 pr_err("Command ITT: 0x%08x received DataOUT for a"
1472 " NON-WRITE command.\n", cmd->init_task_tag);
1473 return iscsit_dump_data_payload(conn, payload_length, 1);
1474 }
1475 se_cmd = &cmd->se_cmd;
1476 iscsit_mod_dataout_timer(cmd);
1477
1478 if ((be32_to_cpu(hdr->offset) + payload_length) > cmd->se_cmd.data_length) {
1479 pr_err("DataOut Offset: %u, Length %u greater than iSCSI Command EDTL %u, protocol error.\n",
1480 be32_to_cpu(hdr->offset), payload_length,
1481 cmd->se_cmd.data_length);
1482 return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID, buf);
1483 }
1484
1485 if (cmd->unsolicited_data) {
1486 int dump_unsolicited_data = 0;
1487
1488 if (conn->sess->sess_ops->InitialR2T) {
1489 pr_err("Received unexpected unsolicited data"
1490 " while InitialR2T=Yes, protocol error.\n");
1491 transport_send_check_condition_and_sense(&cmd->se_cmd,
1492 TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
1493 return -1;
1494 }
1495 /*
1496 * Special case for dealing with Unsolicited DataOUT
1497 * and Unsupported SAM WRITE Opcodes and SE resource allocation
1498 * failures;
1499 */
1500
1501 /* Something's amiss if we're not in WRITE_PENDING state... */
1502 WARN_ON(se_cmd->t_state != TRANSPORT_WRITE_PENDING);
1503 if (!(se_cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE))
1504 dump_unsolicited_data = 1;
1505
1506 if (dump_unsolicited_data) {
1507 /*
1508 * Check if a delayed TASK_ABORTED status needs to
1509 * be sent now if the ISCSI_FLAG_CMD_FINAL has been
1510 * received with the unsolicited data out.
1511 */
1512 if (hdr->flags & ISCSI_FLAG_CMD_FINAL)
1513 iscsit_stop_dataout_timer(cmd);
1514
1515 return iscsit_dump_data_payload(conn, payload_length, 1);
1516 }
1517 } else {
1518 /*
1519 * For the normal solicited data path:
1520 *
1521 * Check for a delayed TASK_ABORTED status and dump any
1522 * incoming data out payload if one exists. Also, when the
1523 * ISCSI_FLAG_CMD_FINAL is set to denote the end of the current
1524 * data out sequence, we decrement outstanding_r2ts. Once
1525 * outstanding_r2ts reaches zero, go ahead and send the delayed
1526 * TASK_ABORTED status.
1527 */
1528 if (se_cmd->transport_state & CMD_T_ABORTED) {
1529 if (hdr->flags & ISCSI_FLAG_CMD_FINAL &&
1530 --cmd->outstanding_r2ts < 1)
1531 iscsit_stop_dataout_timer(cmd);
1532
1533 return iscsit_dump_data_payload(conn, payload_length, 1);
1534 }
1535 }
1536 /*
1537 * Perform DataSN, DataSequenceInOrder, DataPDUInOrder, and
1538 * within-command recovery checks before receiving the payload.
1539 */
1540 rc = iscsit_check_pre_dataout(cmd, buf);
1541 if (rc == DATAOUT_WITHIN_COMMAND_RECOVERY)
1542 return 0;
1543 else if (rc == DATAOUT_CANNOT_RECOVER)
1544 return -1;
1545 *success = true;
1546 return 0;
1547 }
1548 EXPORT_SYMBOL(__iscsit_check_dataout_hdr);
1549
1550 int
iscsit_check_dataout_hdr(struct iscsi_conn * conn,void * buf,struct iscsi_cmd ** out_cmd)1551 iscsit_check_dataout_hdr(struct iscsi_conn *conn, void *buf,
1552 struct iscsi_cmd **out_cmd)
1553 {
1554 struct iscsi_data *hdr = buf;
1555 struct iscsi_cmd *cmd;
1556 u32 payload_length = ntoh24(hdr->dlength);
1557 int rc;
1558 bool success = false;
1559
1560 if (!payload_length) {
1561 pr_warn_ratelimited("DataOUT payload is ZERO, ignoring.\n");
1562 return 0;
1563 }
1564
1565 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1566 pr_err_ratelimited("DataSegmentLength: %u is greater than"
1567 " MaxXmitDataSegmentLength: %u\n", payload_length,
1568 conn->conn_ops->MaxXmitDataSegmentLength);
1569 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR, buf);
1570 }
1571
1572 cmd = iscsit_find_cmd_from_itt_or_dump(conn, hdr->itt, payload_length);
1573 if (!cmd)
1574 return 0;
1575
1576 rc = __iscsit_check_dataout_hdr(conn, buf, cmd, payload_length, &success);
1577
1578 if (success)
1579 *out_cmd = cmd;
1580
1581 return rc;
1582 }
1583 EXPORT_SYMBOL(iscsit_check_dataout_hdr);
1584
1585 static int
iscsit_get_dataout(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_data * hdr)1586 iscsit_get_dataout(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1587 struct iscsi_data *hdr)
1588 {
1589 struct kvec *iov;
1590 u32 checksum, iov_count = 0, padding = 0, rx_got = 0, rx_size = 0;
1591 u32 payload_length;
1592 int iov_ret, data_crc_failed = 0;
1593
1594 payload_length = min_t(u32, cmd->se_cmd.data_length,
1595 ntoh24(hdr->dlength));
1596 rx_size += payload_length;
1597 iov = &cmd->iov_data[0];
1598
1599 iov_ret = iscsit_map_iovec(cmd, iov, cmd->orig_iov_data_count - 2,
1600 be32_to_cpu(hdr->offset), payload_length);
1601 if (iov_ret < 0)
1602 return -1;
1603
1604 iov_count += iov_ret;
1605
1606 padding = ((-payload_length) & 3);
1607 if (padding != 0) {
1608 iov[iov_count].iov_base = cmd->pad_bytes;
1609 iov[iov_count++].iov_len = padding;
1610 rx_size += padding;
1611 pr_debug("Receiving %u padding bytes.\n", padding);
1612 }
1613
1614 if (conn->conn_ops->DataDigest) {
1615 iov[iov_count].iov_base = &checksum;
1616 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
1617 rx_size += ISCSI_CRC_LEN;
1618 }
1619
1620 WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
1621 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
1622
1623 iscsit_unmap_iovec(cmd);
1624
1625 if (rx_got != rx_size)
1626 return -1;
1627
1628 if (conn->conn_ops->DataDigest) {
1629 u32 data_crc;
1630
1631 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
1632 be32_to_cpu(hdr->offset),
1633 payload_length, padding,
1634 cmd->pad_bytes);
1635
1636 if (checksum != data_crc) {
1637 pr_err("ITT: 0x%08x, Offset: %u, Length: %u,"
1638 " DataSN: 0x%08x, CRC32C DataDigest 0x%08x"
1639 " does not match computed 0x%08x\n",
1640 hdr->itt, hdr->offset, payload_length,
1641 hdr->datasn, checksum, data_crc);
1642 data_crc_failed = 1;
1643 } else {
1644 pr_debug("Got CRC32C DataDigest 0x%08x for"
1645 " %u bytes of Data Out\n", checksum,
1646 payload_length);
1647 }
1648 }
1649
1650 return data_crc_failed;
1651 }
1652
1653 int
iscsit_check_dataout_payload(struct iscsi_cmd * cmd,struct iscsi_data * hdr,bool data_crc_failed)1654 iscsit_check_dataout_payload(struct iscsi_cmd *cmd, struct iscsi_data *hdr,
1655 bool data_crc_failed)
1656 {
1657 struct iscsi_conn *conn = cmd->conn;
1658 int rc, ooo_cmdsn;
1659 /*
1660 * Increment post receive data and CRC values or perform
1661 * within-command recovery.
1662 */
1663 rc = iscsit_check_post_dataout(cmd, (unsigned char *)hdr, data_crc_failed);
1664 if ((rc == DATAOUT_NORMAL) || (rc == DATAOUT_WITHIN_COMMAND_RECOVERY))
1665 return 0;
1666 else if (rc == DATAOUT_SEND_R2T) {
1667 iscsit_set_dataout_sequence_values(cmd);
1668 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
1669 } else if (rc == DATAOUT_SEND_TO_TRANSPORT) {
1670 /*
1671 * Handle extra special case for out of order
1672 * Unsolicited Data Out.
1673 */
1674 spin_lock_bh(&cmd->istate_lock);
1675 ooo_cmdsn = (cmd->cmd_flags & ICF_OOO_CMDSN);
1676 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
1677 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
1678 spin_unlock_bh(&cmd->istate_lock);
1679
1680 iscsit_stop_dataout_timer(cmd);
1681 if (ooo_cmdsn)
1682 return 0;
1683 target_execute_cmd(&cmd->se_cmd);
1684 return 0;
1685 } else /* DATAOUT_CANNOT_RECOVER */
1686 return -1;
1687
1688 return 0;
1689 }
1690 EXPORT_SYMBOL(iscsit_check_dataout_payload);
1691
iscsit_handle_data_out(struct iscsi_conn * conn,unsigned char * buf)1692 static int iscsit_handle_data_out(struct iscsi_conn *conn, unsigned char *buf)
1693 {
1694 struct iscsi_cmd *cmd = NULL;
1695 struct iscsi_data *hdr = (struct iscsi_data *)buf;
1696 int rc;
1697 bool data_crc_failed = false;
1698
1699 rc = iscsit_check_dataout_hdr(conn, buf, &cmd);
1700 if (rc < 0)
1701 return 0;
1702 else if (!cmd)
1703 return 0;
1704
1705 rc = iscsit_get_dataout(conn, cmd, hdr);
1706 if (rc < 0)
1707 return rc;
1708 else if (rc > 0)
1709 data_crc_failed = true;
1710
1711 return iscsit_check_dataout_payload(cmd, hdr, data_crc_failed);
1712 }
1713
iscsit_setup_nop_out(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_nopout * hdr)1714 int iscsit_setup_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1715 struct iscsi_nopout *hdr)
1716 {
1717 u32 payload_length = ntoh24(hdr->dlength);
1718
1719 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL)) {
1720 pr_err("NopOUT Flag's, Left Most Bit not set, protocol error.\n");
1721 if (!cmd)
1722 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1723 (unsigned char *)hdr);
1724
1725 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1726 (unsigned char *)hdr);
1727 }
1728
1729 if (hdr->itt == RESERVED_ITT && !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1730 pr_err("NOPOUT ITT is reserved, but Immediate Bit is"
1731 " not set, protocol error.\n");
1732 if (!cmd)
1733 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1734 (unsigned char *)hdr);
1735
1736 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1737 (unsigned char *)hdr);
1738 }
1739
1740 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
1741 pr_err("NOPOUT Ping Data DataSegmentLength: %u is"
1742 " greater than MaxXmitDataSegmentLength: %u, protocol"
1743 " error.\n", payload_length,
1744 conn->conn_ops->MaxXmitDataSegmentLength);
1745 if (!cmd)
1746 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1747 (unsigned char *)hdr);
1748
1749 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
1750 (unsigned char *)hdr);
1751 }
1752
1753 pr_debug("Got NOPOUT Ping %s ITT: 0x%08x, TTT: 0x%08x,"
1754 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
1755 hdr->itt == RESERVED_ITT ? "Response" : "Request",
1756 hdr->itt, hdr->ttt, hdr->cmdsn, hdr->exp_statsn,
1757 payload_length);
1758 /*
1759 * This is not a response to a Unsolicited NopIN, which means
1760 * it can either be a NOPOUT ping request (with a valid ITT),
1761 * or a NOPOUT not requesting a NOPIN (with a reserved ITT).
1762 * Either way, make sure we allocate an struct iscsi_cmd, as both
1763 * can contain ping data.
1764 */
1765 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1766 cmd->iscsi_opcode = ISCSI_OP_NOOP_OUT;
1767 cmd->i_state = ISTATE_SEND_NOPIN;
1768 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ?
1769 1 : 0);
1770 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
1771 cmd->targ_xfer_tag = 0xFFFFFFFF;
1772 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
1773 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
1774 cmd->data_direction = DMA_NONE;
1775 }
1776
1777 return 0;
1778 }
1779 EXPORT_SYMBOL(iscsit_setup_nop_out);
1780
iscsit_process_nop_out(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_nopout * hdr)1781 int iscsit_process_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1782 struct iscsi_nopout *hdr)
1783 {
1784 struct iscsi_cmd *cmd_p = NULL;
1785 int cmdsn_ret = 0;
1786 /*
1787 * Initiator is expecting a NopIN ping reply..
1788 */
1789 if (hdr->itt != RESERVED_ITT) {
1790 if (!cmd)
1791 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
1792 (unsigned char *)hdr);
1793
1794 spin_lock_bh(&conn->cmd_lock);
1795 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
1796 spin_unlock_bh(&conn->cmd_lock);
1797
1798 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
1799
1800 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
1801 iscsit_add_cmd_to_response_queue(cmd, conn,
1802 cmd->i_state);
1803 return 0;
1804 }
1805
1806 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
1807 (unsigned char *)hdr, hdr->cmdsn);
1808 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
1809 return 0;
1810 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
1811 return -1;
1812
1813 return 0;
1814 }
1815 /*
1816 * This was a response to a unsolicited NOPIN ping.
1817 */
1818 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
1819 cmd_p = iscsit_find_cmd_from_ttt(conn, be32_to_cpu(hdr->ttt));
1820 if (!cmd_p)
1821 return -EINVAL;
1822
1823 iscsit_stop_nopin_response_timer(conn);
1824
1825 cmd_p->i_state = ISTATE_REMOVE;
1826 iscsit_add_cmd_to_immediate_queue(cmd_p, conn, cmd_p->i_state);
1827
1828 iscsit_start_nopin_timer(conn);
1829 return 0;
1830 }
1831 /*
1832 * Otherwise, initiator is not expecting a NOPIN is response.
1833 * Just ignore for now.
1834 */
1835
1836 if (cmd)
1837 iscsit_free_cmd(cmd, false);
1838
1839 return 0;
1840 }
1841 EXPORT_SYMBOL(iscsit_process_nop_out);
1842
iscsit_handle_nop_out(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf)1843 static int iscsit_handle_nop_out(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1844 unsigned char *buf)
1845 {
1846 unsigned char *ping_data = NULL;
1847 struct iscsi_nopout *hdr = (struct iscsi_nopout *)buf;
1848 struct kvec *iov = NULL;
1849 u32 payload_length = ntoh24(hdr->dlength);
1850 int ret;
1851
1852 ret = iscsit_setup_nop_out(conn, cmd, hdr);
1853 if (ret < 0)
1854 return 0;
1855 /*
1856 * Handle NOP-OUT payload for traditional iSCSI sockets
1857 */
1858 if (payload_length && hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
1859 u32 checksum, data_crc, padding = 0;
1860 int niov = 0, rx_got, rx_size = payload_length;
1861
1862 ping_data = kzalloc(payload_length + 1, GFP_KERNEL);
1863 if (!ping_data) {
1864 ret = -1;
1865 goto out;
1866 }
1867
1868 iov = &cmd->iov_misc[0];
1869 iov[niov].iov_base = ping_data;
1870 iov[niov++].iov_len = payload_length;
1871
1872 padding = ((-payload_length) & 3);
1873 if (padding != 0) {
1874 pr_debug("Receiving %u additional bytes"
1875 " for padding.\n", padding);
1876 iov[niov].iov_base = &cmd->pad_bytes;
1877 iov[niov++].iov_len = padding;
1878 rx_size += padding;
1879 }
1880 if (conn->conn_ops->DataDigest) {
1881 iov[niov].iov_base = &checksum;
1882 iov[niov++].iov_len = ISCSI_CRC_LEN;
1883 rx_size += ISCSI_CRC_LEN;
1884 }
1885
1886 WARN_ON_ONCE(niov > ARRAY_SIZE(cmd->iov_misc));
1887 rx_got = rx_data(conn, &cmd->iov_misc[0], niov, rx_size);
1888 if (rx_got != rx_size) {
1889 ret = -1;
1890 goto out;
1891 }
1892
1893 if (conn->conn_ops->DataDigest) {
1894 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, ping_data,
1895 payload_length, padding,
1896 cmd->pad_bytes, &data_crc);
1897
1898 if (checksum != data_crc) {
1899 pr_err("Ping data CRC32C DataDigest"
1900 " 0x%08x does not match computed 0x%08x\n",
1901 checksum, data_crc);
1902 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
1903 pr_err("Unable to recover from"
1904 " NOPOUT Ping DataCRC failure while in"
1905 " ERL=0.\n");
1906 ret = -1;
1907 goto out;
1908 } else {
1909 /*
1910 * Silently drop this PDU and let the
1911 * initiator plug the CmdSN gap.
1912 */
1913 pr_debug("Dropping NOPOUT"
1914 " Command CmdSN: 0x%08x due to"
1915 " DataCRC error.\n", hdr->cmdsn);
1916 ret = 0;
1917 goto out;
1918 }
1919 } else {
1920 pr_debug("Got CRC32C DataDigest"
1921 " 0x%08x for %u bytes of ping data.\n",
1922 checksum, payload_length);
1923 }
1924 }
1925
1926 ping_data[payload_length] = '\0';
1927 /*
1928 * Attach ping data to struct iscsi_cmd->buf_ptr.
1929 */
1930 cmd->buf_ptr = ping_data;
1931 cmd->buf_ptr_size = payload_length;
1932
1933 pr_debug("Got %u bytes of NOPOUT ping"
1934 " data.\n", payload_length);
1935 pr_debug("Ping Data: \"%s\"\n", ping_data);
1936 }
1937
1938 return iscsit_process_nop_out(conn, cmd, hdr);
1939 out:
1940 if (cmd)
1941 iscsit_free_cmd(cmd, false);
1942
1943 kfree(ping_data);
1944 return ret;
1945 }
1946
iscsit_convert_tmf(u8 iscsi_tmf)1947 static enum tcm_tmreq_table iscsit_convert_tmf(u8 iscsi_tmf)
1948 {
1949 switch (iscsi_tmf) {
1950 case ISCSI_TM_FUNC_ABORT_TASK:
1951 return TMR_ABORT_TASK;
1952 case ISCSI_TM_FUNC_ABORT_TASK_SET:
1953 return TMR_ABORT_TASK_SET;
1954 case ISCSI_TM_FUNC_CLEAR_ACA:
1955 return TMR_CLEAR_ACA;
1956 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
1957 return TMR_CLEAR_TASK_SET;
1958 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
1959 return TMR_LUN_RESET;
1960 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
1961 return TMR_TARGET_WARM_RESET;
1962 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
1963 return TMR_TARGET_COLD_RESET;
1964 default:
1965 return TMR_UNKNOWN;
1966 }
1967 }
1968
1969 int
iscsit_handle_task_mgt_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf)1970 iscsit_handle_task_mgt_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
1971 unsigned char *buf)
1972 {
1973 struct se_tmr_req *se_tmr;
1974 struct iscsi_tmr_req *tmr_req;
1975 struct iscsi_tm *hdr;
1976 int out_of_order_cmdsn = 0, ret;
1977 u8 function, tcm_function = TMR_UNKNOWN;
1978
1979 hdr = (struct iscsi_tm *) buf;
1980 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
1981 function = hdr->flags;
1982
1983 pr_debug("Got Task Management Request ITT: 0x%08x, CmdSN:"
1984 " 0x%08x, Function: 0x%02x, RefTaskTag: 0x%08x, RefCmdSN:"
1985 " 0x%08x, CID: %hu\n", hdr->itt, hdr->cmdsn, function,
1986 hdr->rtt, hdr->refcmdsn, conn->cid);
1987
1988 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
1989 ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
1990 hdr->rtt != RESERVED_ITT)) {
1991 pr_err("RefTaskTag should be set to 0xFFFFFFFF.\n");
1992 hdr->rtt = RESERVED_ITT;
1993 }
1994
1995 if ((function == ISCSI_TM_FUNC_TASK_REASSIGN) &&
1996 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
1997 pr_err("Task Management Request TASK_REASSIGN not"
1998 " issued as immediate command, bad iSCSI Initiator"
1999 "implementation\n");
2000 return iscsit_add_reject_cmd(cmd,
2001 ISCSI_REASON_PROTOCOL_ERROR, buf);
2002 }
2003 if ((function != ISCSI_TM_FUNC_ABORT_TASK) &&
2004 be32_to_cpu(hdr->refcmdsn) != ISCSI_RESERVED_TAG)
2005 hdr->refcmdsn = cpu_to_be32(ISCSI_RESERVED_TAG);
2006
2007 cmd->data_direction = DMA_NONE;
2008 cmd->tmr_req = kzalloc(sizeof(*cmd->tmr_req), GFP_KERNEL);
2009 if (!cmd->tmr_req) {
2010 return iscsit_add_reject_cmd(cmd,
2011 ISCSI_REASON_BOOKMARK_NO_RESOURCES,
2012 buf);
2013 }
2014
2015 transport_init_se_cmd(&cmd->se_cmd, &iscsi_ops,
2016 conn->sess->se_sess, 0, DMA_NONE,
2017 TCM_SIMPLE_TAG, cmd->sense_buffer + 2,
2018 scsilun_to_int(&hdr->lun));
2019
2020 target_get_sess_cmd(&cmd->se_cmd, true);
2021
2022 /*
2023 * TASK_REASSIGN for ERL=2 / connection stays inside of
2024 * LIO-Target $FABRIC_MOD
2025 */
2026 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2027 tcm_function = iscsit_convert_tmf(function);
2028 if (tcm_function == TMR_UNKNOWN) {
2029 pr_err("Unknown iSCSI TMR Function:"
2030 " 0x%02x\n", function);
2031 return iscsit_add_reject_cmd(cmd,
2032 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2033 }
2034 }
2035 ret = core_tmr_alloc_req(&cmd->se_cmd, cmd->tmr_req, tcm_function,
2036 GFP_KERNEL);
2037 if (ret < 0)
2038 return iscsit_add_reject_cmd(cmd,
2039 ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
2040
2041 cmd->tmr_req->se_tmr_req = cmd->se_cmd.se_tmr_req;
2042
2043 cmd->iscsi_opcode = ISCSI_OP_SCSI_TMFUNC;
2044 cmd->i_state = ISTATE_SEND_TASKMGTRSP;
2045 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2046 cmd->init_task_tag = hdr->itt;
2047 cmd->targ_xfer_tag = 0xFFFFFFFF;
2048 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2049 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2050 se_tmr = cmd->se_cmd.se_tmr_req;
2051 tmr_req = cmd->tmr_req;
2052 /*
2053 * Locate the struct se_lun for all TMRs not related to ERL=2 TASK_REASSIGN
2054 */
2055 if (function != ISCSI_TM_FUNC_TASK_REASSIGN) {
2056 ret = transport_lookup_tmr_lun(&cmd->se_cmd);
2057 if (ret < 0) {
2058 se_tmr->response = ISCSI_TMF_RSP_NO_LUN;
2059 goto attach;
2060 }
2061 }
2062
2063 switch (function) {
2064 case ISCSI_TM_FUNC_ABORT_TASK:
2065 se_tmr->response = iscsit_tmr_abort_task(cmd, buf);
2066 if (se_tmr->response)
2067 goto attach;
2068 break;
2069 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2070 case ISCSI_TM_FUNC_CLEAR_ACA:
2071 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2072 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2073 break;
2074 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2075 if (iscsit_tmr_task_warm_reset(conn, tmr_req, buf) < 0) {
2076 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2077 goto attach;
2078 }
2079 break;
2080 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2081 if (iscsit_tmr_task_cold_reset(conn, tmr_req, buf) < 0) {
2082 se_tmr->response = ISCSI_TMF_RSP_AUTH_FAILED;
2083 goto attach;
2084 }
2085 break;
2086 case ISCSI_TM_FUNC_TASK_REASSIGN:
2087 se_tmr->response = iscsit_tmr_task_reassign(cmd, buf);
2088 /*
2089 * Perform sanity checks on the ExpDataSN only if the
2090 * TASK_REASSIGN was successful.
2091 */
2092 if (se_tmr->response)
2093 break;
2094
2095 if (iscsit_check_task_reassign_expdatasn(tmr_req, conn) < 0)
2096 return iscsit_add_reject_cmd(cmd,
2097 ISCSI_REASON_BOOKMARK_INVALID, buf);
2098 break;
2099 default:
2100 pr_err("Unknown TMR function: 0x%02x, protocol"
2101 " error.\n", function);
2102 se_tmr->response = ISCSI_TMF_RSP_NOT_SUPPORTED;
2103 goto attach;
2104 }
2105
2106 if ((function != ISCSI_TM_FUNC_TASK_REASSIGN) &&
2107 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
2108 se_tmr->call_transport = 1;
2109 attach:
2110 spin_lock_bh(&conn->cmd_lock);
2111 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2112 spin_unlock_bh(&conn->cmd_lock);
2113
2114 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2115 int cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2116 if (cmdsn_ret == CMDSN_HIGHER_THAN_EXP) {
2117 out_of_order_cmdsn = 1;
2118 } else if (cmdsn_ret == CMDSN_LOWER_THAN_EXP) {
2119 target_put_sess_cmd(&cmd->se_cmd);
2120 return 0;
2121 } else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER) {
2122 return -1;
2123 }
2124 }
2125 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2126
2127 if (out_of_order_cmdsn || !(hdr->opcode & ISCSI_OP_IMMEDIATE))
2128 return 0;
2129 /*
2130 * Found the referenced task, send to transport for processing.
2131 */
2132 if (se_tmr->call_transport)
2133 return transport_generic_handle_tmr(&cmd->se_cmd);
2134
2135 /*
2136 * Could not find the referenced LUN, task, or Task Management
2137 * command not authorized or supported. Change state and
2138 * let the tx_thread send the response.
2139 *
2140 * For connection recovery, this is also the default action for
2141 * TMR TASK_REASSIGN.
2142 */
2143 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2144 target_put_sess_cmd(&cmd->se_cmd);
2145 return 0;
2146 }
2147 EXPORT_SYMBOL(iscsit_handle_task_mgt_cmd);
2148
2149 /* #warning FIXME: Support Text Command parameters besides SendTargets */
2150 int
iscsit_setup_text_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_text * hdr)2151 iscsit_setup_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2152 struct iscsi_text *hdr)
2153 {
2154 u32 payload_length = ntoh24(hdr->dlength);
2155
2156 if (payload_length > conn->conn_ops->MaxXmitDataSegmentLength) {
2157 pr_err("Unable to accept text parameter length: %u"
2158 "greater than MaxXmitDataSegmentLength %u.\n",
2159 payload_length, conn->conn_ops->MaxXmitDataSegmentLength);
2160 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2161 (unsigned char *)hdr);
2162 }
2163
2164 if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL) ||
2165 (hdr->flags & ISCSI_FLAG_TEXT_CONTINUE)) {
2166 pr_err("Multi sequence text commands currently not supported\n");
2167 return iscsit_reject_cmd(cmd, ISCSI_REASON_CMD_NOT_SUPPORTED,
2168 (unsigned char *)hdr);
2169 }
2170
2171 pr_debug("Got Text Request: ITT: 0x%08x, CmdSN: 0x%08x,"
2172 " ExpStatSN: 0x%08x, Length: %u\n", hdr->itt, hdr->cmdsn,
2173 hdr->exp_statsn, payload_length);
2174
2175 cmd->iscsi_opcode = ISCSI_OP_TEXT;
2176 cmd->i_state = ISTATE_SEND_TEXTRSP;
2177 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2178 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2179 cmd->targ_xfer_tag = 0xFFFFFFFF;
2180 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2181 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2182 cmd->data_direction = DMA_NONE;
2183 kfree(cmd->text_in_ptr);
2184 cmd->text_in_ptr = NULL;
2185
2186 return 0;
2187 }
2188 EXPORT_SYMBOL(iscsit_setup_text_cmd);
2189
2190 int
iscsit_process_text_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,struct iscsi_text * hdr)2191 iscsit_process_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2192 struct iscsi_text *hdr)
2193 {
2194 unsigned char *text_in = cmd->text_in_ptr, *text_ptr;
2195 int cmdsn_ret;
2196
2197 if (!text_in) {
2198 cmd->targ_xfer_tag = be32_to_cpu(hdr->ttt);
2199 if (cmd->targ_xfer_tag == 0xFFFFFFFF) {
2200 pr_err("Unable to locate text_in buffer for sendtargets"
2201 " discovery\n");
2202 goto reject;
2203 }
2204 goto empty_sendtargets;
2205 }
2206 if (strncmp("SendTargets=", text_in, 12) != 0) {
2207 pr_err("Received Text Data that is not"
2208 " SendTargets, cannot continue.\n");
2209 goto reject;
2210 }
2211 /* '=' confirmed in strncmp */
2212 text_ptr = strchr(text_in, '=');
2213 BUG_ON(!text_ptr);
2214 if (!strncmp("=All", text_ptr, 5)) {
2215 cmd->cmd_flags |= ICF_SENDTARGETS_ALL;
2216 } else if (!strncmp("=iqn.", text_ptr, 5) ||
2217 !strncmp("=eui.", text_ptr, 5)) {
2218 cmd->cmd_flags |= ICF_SENDTARGETS_SINGLE;
2219 } else {
2220 pr_err("Unable to locate valid SendTargets%s value\n",
2221 text_ptr);
2222 goto reject;
2223 }
2224
2225 spin_lock_bh(&conn->cmd_lock);
2226 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2227 spin_unlock_bh(&conn->cmd_lock);
2228
2229 empty_sendtargets:
2230 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2231
2232 if (!(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
2233 cmdsn_ret = iscsit_sequence_cmd(conn, cmd,
2234 (unsigned char *)hdr, hdr->cmdsn);
2235 if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2236 return -1;
2237
2238 return 0;
2239 }
2240
2241 return iscsit_execute_cmd(cmd, 0);
2242
2243 reject:
2244 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR,
2245 (unsigned char *)hdr);
2246 }
2247 EXPORT_SYMBOL(iscsit_process_text_cmd);
2248
2249 static int
iscsit_handle_text_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf)2250 iscsit_handle_text_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2251 unsigned char *buf)
2252 {
2253 struct iscsi_text *hdr = (struct iscsi_text *)buf;
2254 char *text_in = NULL;
2255 u32 payload_length = ntoh24(hdr->dlength);
2256 int rx_size, rc;
2257
2258 rc = iscsit_setup_text_cmd(conn, cmd, hdr);
2259 if (rc < 0)
2260 return 0;
2261
2262 rx_size = payload_length;
2263 if (payload_length) {
2264 u32 checksum = 0, data_crc = 0;
2265 u32 padding = 0;
2266 int niov = 0, rx_got;
2267 struct kvec iov[2];
2268
2269 rx_size = ALIGN(payload_length, 4);
2270 text_in = kzalloc(rx_size, GFP_KERNEL);
2271 if (!text_in)
2272 goto reject;
2273
2274 cmd->text_in_ptr = text_in;
2275
2276 memset(iov, 0, sizeof(iov));
2277 iov[niov].iov_base = text_in;
2278 iov[niov++].iov_len = rx_size;
2279
2280 padding = rx_size - payload_length;
2281 if (padding)
2282 pr_debug("Receiving %u additional bytes"
2283 " for padding.\n", padding);
2284 if (conn->conn_ops->DataDigest) {
2285 iov[niov].iov_base = &checksum;
2286 iov[niov++].iov_len = ISCSI_CRC_LEN;
2287 rx_size += ISCSI_CRC_LEN;
2288 }
2289
2290 WARN_ON_ONCE(niov > ARRAY_SIZE(iov));
2291 rx_got = rx_data(conn, &iov[0], niov, rx_size);
2292 if (rx_got != rx_size)
2293 goto reject;
2294
2295 if (conn->conn_ops->DataDigest) {
2296 iscsit_do_crypto_hash_buf(conn->conn_rx_hash,
2297 text_in, rx_size, 0, NULL,
2298 &data_crc);
2299
2300 if (checksum != data_crc) {
2301 pr_err("Text data CRC32C DataDigest"
2302 " 0x%08x does not match computed"
2303 " 0x%08x\n", checksum, data_crc);
2304 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2305 pr_err("Unable to recover from"
2306 " Text Data digest failure while in"
2307 " ERL=0.\n");
2308 goto reject;
2309 } else {
2310 /*
2311 * Silently drop this PDU and let the
2312 * initiator plug the CmdSN gap.
2313 */
2314 pr_debug("Dropping Text"
2315 " Command CmdSN: 0x%08x due to"
2316 " DataCRC error.\n", hdr->cmdsn);
2317 kfree(text_in);
2318 return 0;
2319 }
2320 } else {
2321 pr_debug("Got CRC32C DataDigest"
2322 " 0x%08x for %u bytes of text data.\n",
2323 checksum, payload_length);
2324 }
2325 }
2326 text_in[payload_length - 1] = '\0';
2327 pr_debug("Successfully read %d bytes of text"
2328 " data.\n", payload_length);
2329 }
2330
2331 return iscsit_process_text_cmd(conn, cmd, hdr);
2332
2333 reject:
2334 kfree(cmd->text_in_ptr);
2335 cmd->text_in_ptr = NULL;
2336 return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
2337 }
2338
iscsit_logout_closesession(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2339 int iscsit_logout_closesession(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2340 {
2341 struct iscsi_conn *conn_p;
2342 struct iscsi_session *sess = conn->sess;
2343
2344 pr_debug("Received logout request CLOSESESSION on CID: %hu"
2345 " for SID: %u.\n", conn->cid, conn->sess->sid);
2346
2347 atomic_set(&sess->session_logout, 1);
2348 atomic_set(&conn->conn_logout_remove, 1);
2349 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_SESSION;
2350
2351 iscsit_inc_conn_usage_count(conn);
2352 iscsit_inc_session_usage_count(sess);
2353
2354 spin_lock_bh(&sess->conn_lock);
2355 list_for_each_entry(conn_p, &sess->sess_conn_list, conn_list) {
2356 if (conn_p->conn_state != TARG_CONN_STATE_LOGGED_IN)
2357 continue;
2358
2359 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2360 conn_p->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2361 }
2362 spin_unlock_bh(&sess->conn_lock);
2363
2364 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2365
2366 return 0;
2367 }
2368
iscsit_logout_closeconnection(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2369 int iscsit_logout_closeconnection(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2370 {
2371 struct iscsi_conn *l_conn;
2372 struct iscsi_session *sess = conn->sess;
2373
2374 pr_debug("Received logout request CLOSECONNECTION for CID:"
2375 " %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2376
2377 /*
2378 * A Logout Request with a CLOSECONNECTION reason code for a CID
2379 * can arrive on a connection with a differing CID.
2380 */
2381 if (conn->cid == cmd->logout_cid) {
2382 spin_lock_bh(&conn->state_lock);
2383 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
2384 conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
2385
2386 atomic_set(&conn->conn_logout_remove, 1);
2387 conn->conn_logout_reason = ISCSI_LOGOUT_REASON_CLOSE_CONNECTION;
2388 iscsit_inc_conn_usage_count(conn);
2389
2390 spin_unlock_bh(&conn->state_lock);
2391 } else {
2392 /*
2393 * Handle all different cid CLOSECONNECTION requests in
2394 * iscsit_logout_post_handler_diffcid() as to give enough
2395 * time for any non immediate command's CmdSN to be
2396 * acknowledged on the connection in question.
2397 *
2398 * Here we simply make sure the CID is still around.
2399 */
2400 l_conn = iscsit_get_conn_from_cid(sess,
2401 cmd->logout_cid);
2402 if (!l_conn) {
2403 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2404 iscsit_add_cmd_to_response_queue(cmd, conn,
2405 cmd->i_state);
2406 return 0;
2407 }
2408
2409 iscsit_dec_conn_usage_count(l_conn);
2410 }
2411
2412 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2413
2414 return 0;
2415 }
2416
iscsit_logout_removeconnforrecovery(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2417 int iscsit_logout_removeconnforrecovery(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2418 {
2419 struct iscsi_session *sess = conn->sess;
2420
2421 pr_debug("Received explicit REMOVECONNFORRECOVERY logout for"
2422 " CID: %hu on CID: %hu.\n", cmd->logout_cid, conn->cid);
2423
2424 if (sess->sess_ops->ErrorRecoveryLevel != 2) {
2425 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2426 " while ERL!=2.\n");
2427 cmd->logout_response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2428 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2429 return 0;
2430 }
2431
2432 if (conn->cid == cmd->logout_cid) {
2433 pr_err("Received Logout Request REMOVECONNFORRECOVERY"
2434 " with CID: %hu on CID: %hu, implementation error.\n",
2435 cmd->logout_cid, conn->cid);
2436 cmd->logout_response = ISCSI_LOGOUT_CLEANUP_FAILED;
2437 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2438 return 0;
2439 }
2440
2441 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
2442
2443 return 0;
2444 }
2445
2446 int
iscsit_handle_logout_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf)2447 iscsit_handle_logout_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
2448 unsigned char *buf)
2449 {
2450 int cmdsn_ret, logout_remove = 0;
2451 u8 reason_code = 0;
2452 struct iscsi_logout *hdr;
2453 struct iscsi_tiqn *tiqn = iscsit_snmp_get_tiqn(conn);
2454
2455 hdr = (struct iscsi_logout *) buf;
2456 reason_code = (hdr->flags & 0x7f);
2457
2458 if (tiqn) {
2459 spin_lock(&tiqn->logout_stats.lock);
2460 if (reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION)
2461 tiqn->logout_stats.normal_logouts++;
2462 else
2463 tiqn->logout_stats.abnormal_logouts++;
2464 spin_unlock(&tiqn->logout_stats.lock);
2465 }
2466
2467 pr_debug("Got Logout Request ITT: 0x%08x CmdSN: 0x%08x"
2468 " ExpStatSN: 0x%08x Reason: 0x%02x CID: %hu on CID: %hu\n",
2469 hdr->itt, hdr->cmdsn, hdr->exp_statsn, reason_code,
2470 hdr->cid, conn->cid);
2471
2472 if (conn->conn_state != TARG_CONN_STATE_LOGGED_IN) {
2473 pr_err("Received logout request on connection that"
2474 " is not in logged in state, ignoring request.\n");
2475 iscsit_free_cmd(cmd, false);
2476 return 0;
2477 }
2478
2479 cmd->iscsi_opcode = ISCSI_OP_LOGOUT;
2480 cmd->i_state = ISTATE_SEND_LOGOUTRSP;
2481 cmd->immediate_cmd = ((hdr->opcode & ISCSI_OP_IMMEDIATE) ? 1 : 0);
2482 conn->sess->init_task_tag = cmd->init_task_tag = hdr->itt;
2483 cmd->targ_xfer_tag = 0xFFFFFFFF;
2484 cmd->cmd_sn = be32_to_cpu(hdr->cmdsn);
2485 cmd->exp_stat_sn = be32_to_cpu(hdr->exp_statsn);
2486 cmd->logout_cid = be16_to_cpu(hdr->cid);
2487 cmd->logout_reason = reason_code;
2488 cmd->data_direction = DMA_NONE;
2489
2490 /*
2491 * We need to sleep in these cases (by returning 1) until the Logout
2492 * Response gets sent in the tx thread.
2493 */
2494 if ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_SESSION) ||
2495 ((reason_code == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION) &&
2496 be16_to_cpu(hdr->cid) == conn->cid))
2497 logout_remove = 1;
2498
2499 spin_lock_bh(&conn->cmd_lock);
2500 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
2501 spin_unlock_bh(&conn->cmd_lock);
2502
2503 if (reason_code != ISCSI_LOGOUT_REASON_RECOVERY)
2504 iscsit_ack_from_expstatsn(conn, be32_to_cpu(hdr->exp_statsn));
2505
2506 /*
2507 * Immediate commands are executed, well, immediately.
2508 * Non-Immediate Logout Commands are executed in CmdSN order.
2509 */
2510 if (cmd->immediate_cmd) {
2511 int ret = iscsit_execute_cmd(cmd, 0);
2512
2513 if (ret < 0)
2514 return ret;
2515 } else {
2516 cmdsn_ret = iscsit_sequence_cmd(conn, cmd, buf, hdr->cmdsn);
2517 if (cmdsn_ret == CMDSN_LOWER_THAN_EXP)
2518 logout_remove = 0;
2519 else if (cmdsn_ret == CMDSN_ERROR_CANNOT_RECOVER)
2520 return -1;
2521 }
2522
2523 return logout_remove;
2524 }
2525 EXPORT_SYMBOL(iscsit_handle_logout_cmd);
2526
iscsit_handle_snack(struct iscsi_conn * conn,unsigned char * buf)2527 int iscsit_handle_snack(
2528 struct iscsi_conn *conn,
2529 unsigned char *buf)
2530 {
2531 struct iscsi_snack *hdr;
2532
2533 hdr = (struct iscsi_snack *) buf;
2534 hdr->flags &= ~ISCSI_FLAG_CMD_FINAL;
2535
2536 pr_debug("Got ISCSI_INIT_SNACK, ITT: 0x%08x, ExpStatSN:"
2537 " 0x%08x, Type: 0x%02x, BegRun: 0x%08x, RunLength: 0x%08x,"
2538 " CID: %hu\n", hdr->itt, hdr->exp_statsn, hdr->flags,
2539 hdr->begrun, hdr->runlength, conn->cid);
2540
2541 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2542 pr_err("Initiator sent SNACK request while in"
2543 " ErrorRecoveryLevel=0.\n");
2544 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2545 buf);
2546 }
2547 /*
2548 * SNACK_DATA and SNACK_R2T are both 0, so check which function to
2549 * call from inside iscsi_send_recovery_datain_or_r2t().
2550 */
2551 switch (hdr->flags & ISCSI_FLAG_SNACK_TYPE_MASK) {
2552 case 0:
2553 return iscsit_handle_recovery_datain_or_r2t(conn, buf,
2554 hdr->itt,
2555 be32_to_cpu(hdr->ttt),
2556 be32_to_cpu(hdr->begrun),
2557 be32_to_cpu(hdr->runlength));
2558 case ISCSI_FLAG_SNACK_TYPE_STATUS:
2559 return iscsit_handle_status_snack(conn, hdr->itt,
2560 be32_to_cpu(hdr->ttt),
2561 be32_to_cpu(hdr->begrun), be32_to_cpu(hdr->runlength));
2562 case ISCSI_FLAG_SNACK_TYPE_DATA_ACK:
2563 return iscsit_handle_data_ack(conn, be32_to_cpu(hdr->ttt),
2564 be32_to_cpu(hdr->begrun),
2565 be32_to_cpu(hdr->runlength));
2566 case ISCSI_FLAG_SNACK_TYPE_RDATA:
2567 /* FIXME: Support R-Data SNACK */
2568 pr_err("R-Data SNACK Not Supported.\n");
2569 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2570 buf);
2571 default:
2572 pr_err("Unknown SNACK type 0x%02x, protocol"
2573 " error.\n", hdr->flags & 0x0f);
2574 return iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
2575 buf);
2576 }
2577
2578 return 0;
2579 }
2580 EXPORT_SYMBOL(iscsit_handle_snack);
2581
iscsit_rx_thread_wait_for_tcp(struct iscsi_conn * conn)2582 static void iscsit_rx_thread_wait_for_tcp(struct iscsi_conn *conn)
2583 {
2584 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2585 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2586 wait_for_completion_interruptible_timeout(
2587 &conn->rx_half_close_comp,
2588 ISCSI_RX_THREAD_TCP_TIMEOUT * HZ);
2589 }
2590 }
2591
iscsit_handle_immediate_data(struct iscsi_cmd * cmd,struct iscsi_scsi_req * hdr,u32 length)2592 static int iscsit_handle_immediate_data(
2593 struct iscsi_cmd *cmd,
2594 struct iscsi_scsi_req *hdr,
2595 u32 length)
2596 {
2597 int iov_ret, rx_got = 0, rx_size = 0;
2598 u32 checksum, iov_count = 0, padding = 0;
2599 struct iscsi_conn *conn = cmd->conn;
2600 struct kvec *iov;
2601 void *overflow_buf = NULL;
2602
2603 BUG_ON(cmd->write_data_done > cmd->se_cmd.data_length);
2604 rx_size = min(cmd->se_cmd.data_length - cmd->write_data_done, length);
2605 iov_ret = iscsit_map_iovec(cmd, cmd->iov_data,
2606 cmd->orig_iov_data_count - 2,
2607 cmd->write_data_done, rx_size);
2608 if (iov_ret < 0)
2609 return IMMEDIATE_DATA_CANNOT_RECOVER;
2610
2611 iov_count = iov_ret;
2612 iov = &cmd->iov_data[0];
2613 if (rx_size < length) {
2614 /*
2615 * Special case: length of immediate data exceeds the data
2616 * buffer size derived from the CDB.
2617 */
2618 overflow_buf = kmalloc(length - rx_size, GFP_KERNEL);
2619 if (!overflow_buf) {
2620 iscsit_unmap_iovec(cmd);
2621 return IMMEDIATE_DATA_CANNOT_RECOVER;
2622 }
2623 cmd->overflow_buf = overflow_buf;
2624 iov[iov_count].iov_base = overflow_buf;
2625 iov[iov_count].iov_len = length - rx_size;
2626 iov_count++;
2627 rx_size = length;
2628 }
2629
2630 padding = ((-length) & 3);
2631 if (padding != 0) {
2632 iov[iov_count].iov_base = cmd->pad_bytes;
2633 iov[iov_count++].iov_len = padding;
2634 rx_size += padding;
2635 }
2636
2637 if (conn->conn_ops->DataDigest) {
2638 iov[iov_count].iov_base = &checksum;
2639 iov[iov_count++].iov_len = ISCSI_CRC_LEN;
2640 rx_size += ISCSI_CRC_LEN;
2641 }
2642
2643 WARN_ON_ONCE(iov_count > cmd->orig_iov_data_count);
2644 rx_got = rx_data(conn, &cmd->iov_data[0], iov_count, rx_size);
2645
2646 iscsit_unmap_iovec(cmd);
2647
2648 if (rx_got != rx_size) {
2649 iscsit_rx_thread_wait_for_tcp(conn);
2650 return IMMEDIATE_DATA_CANNOT_RECOVER;
2651 }
2652
2653 if (conn->conn_ops->DataDigest) {
2654 u32 data_crc;
2655
2656 data_crc = iscsit_do_crypto_hash_sg(conn->conn_rx_hash, cmd,
2657 cmd->write_data_done, length, padding,
2658 cmd->pad_bytes);
2659
2660 if (checksum != data_crc) {
2661 pr_err("ImmediateData CRC32C DataDigest 0x%08x"
2662 " does not match computed 0x%08x\n", checksum,
2663 data_crc);
2664
2665 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
2666 pr_err("Unable to recover from"
2667 " Immediate Data digest failure while"
2668 " in ERL=0.\n");
2669 iscsit_reject_cmd(cmd,
2670 ISCSI_REASON_DATA_DIGEST_ERROR,
2671 (unsigned char *)hdr);
2672 return IMMEDIATE_DATA_CANNOT_RECOVER;
2673 } else {
2674 iscsit_reject_cmd(cmd,
2675 ISCSI_REASON_DATA_DIGEST_ERROR,
2676 (unsigned char *)hdr);
2677 return IMMEDIATE_DATA_ERL1_CRC_FAILURE;
2678 }
2679 } else {
2680 pr_debug("Got CRC32C DataDigest 0x%08x for"
2681 " %u bytes of Immediate Data\n", checksum,
2682 length);
2683 }
2684 }
2685
2686 cmd->write_data_done += length;
2687
2688 if (cmd->write_data_done == cmd->se_cmd.data_length) {
2689 spin_lock_bh(&cmd->istate_lock);
2690 cmd->cmd_flags |= ICF_GOT_LAST_DATAOUT;
2691 cmd->i_state = ISTATE_RECEIVED_LAST_DATAOUT;
2692 spin_unlock_bh(&cmd->istate_lock);
2693 }
2694
2695 return IMMEDIATE_DATA_NORMAL_OPERATION;
2696 }
2697
2698 /* #warning iscsi_build_conn_drop_async_message() only sends out on connections
2699 with active network interface */
iscsit_build_conn_drop_async_message(struct iscsi_conn * conn)2700 static void iscsit_build_conn_drop_async_message(struct iscsi_conn *conn)
2701 {
2702 struct iscsi_cmd *cmd;
2703 struct iscsi_conn *conn_p;
2704 bool found = false;
2705
2706 lockdep_assert_held(&conn->sess->conn_lock);
2707
2708 /*
2709 * Only send a Asynchronous Message on connections whos network
2710 * interface is still functional.
2711 */
2712 list_for_each_entry(conn_p, &conn->sess->sess_conn_list, conn_list) {
2713 if (conn_p->conn_state == TARG_CONN_STATE_LOGGED_IN) {
2714 iscsit_inc_conn_usage_count(conn_p);
2715 found = true;
2716 break;
2717 }
2718 }
2719
2720 if (!found)
2721 return;
2722
2723 cmd = iscsit_allocate_cmd(conn_p, TASK_RUNNING);
2724 if (!cmd) {
2725 iscsit_dec_conn_usage_count(conn_p);
2726 return;
2727 }
2728
2729 cmd->logout_cid = conn->cid;
2730 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2731 cmd->i_state = ISTATE_SEND_ASYNCMSG;
2732
2733 spin_lock_bh(&conn_p->cmd_lock);
2734 list_add_tail(&cmd->i_conn_node, &conn_p->conn_cmd_list);
2735 spin_unlock_bh(&conn_p->cmd_lock);
2736
2737 iscsit_add_cmd_to_response_queue(cmd, conn_p, cmd->i_state);
2738 iscsit_dec_conn_usage_count(conn_p);
2739 }
2740
iscsit_send_conn_drop_async_message(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2741 static int iscsit_send_conn_drop_async_message(
2742 struct iscsi_cmd *cmd,
2743 struct iscsi_conn *conn)
2744 {
2745 struct iscsi_async *hdr;
2746
2747 cmd->iscsi_opcode = ISCSI_OP_ASYNC_EVENT;
2748
2749 hdr = (struct iscsi_async *) cmd->pdu;
2750 hdr->opcode = ISCSI_OP_ASYNC_EVENT;
2751 hdr->flags = ISCSI_FLAG_CMD_FINAL;
2752 cmd->init_task_tag = RESERVED_ITT;
2753 cmd->targ_xfer_tag = 0xFFFFFFFF;
2754 put_unaligned_be64(0xFFFFFFFFFFFFFFFFULL, &hdr->rsvd4[0]);
2755 cmd->stat_sn = conn->stat_sn++;
2756 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2757 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2758 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2759 hdr->async_event = ISCSI_ASYNC_MSG_DROPPING_CONNECTION;
2760 hdr->param1 = cpu_to_be16(cmd->logout_cid);
2761 hdr->param2 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Wait);
2762 hdr->param3 = cpu_to_be16(conn->sess->sess_ops->DefaultTime2Retain);
2763
2764 pr_debug("Sending Connection Dropped Async Message StatSN:"
2765 " 0x%08x, for CID: %hu on CID: %hu\n", cmd->stat_sn,
2766 cmd->logout_cid, conn->cid);
2767
2768 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2769 }
2770
iscsit_tx_thread_wait_for_tcp(struct iscsi_conn * conn)2771 static void iscsit_tx_thread_wait_for_tcp(struct iscsi_conn *conn)
2772 {
2773 if ((conn->sock->sk->sk_shutdown & SEND_SHUTDOWN) ||
2774 (conn->sock->sk->sk_shutdown & RCV_SHUTDOWN)) {
2775 wait_for_completion_interruptible_timeout(
2776 &conn->tx_half_close_comp,
2777 ISCSI_TX_THREAD_TCP_TIMEOUT * HZ);
2778 }
2779 }
2780
2781 void
iscsit_build_datain_pdu(struct iscsi_cmd * cmd,struct iscsi_conn * conn,struct iscsi_datain * datain,struct iscsi_data_rsp * hdr,bool set_statsn)2782 iscsit_build_datain_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2783 struct iscsi_datain *datain, struct iscsi_data_rsp *hdr,
2784 bool set_statsn)
2785 {
2786 hdr->opcode = ISCSI_OP_SCSI_DATA_IN;
2787 hdr->flags = datain->flags;
2788 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
2789 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
2790 hdr->flags |= ISCSI_FLAG_DATA_OVERFLOW;
2791 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2792 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
2793 hdr->flags |= ISCSI_FLAG_DATA_UNDERFLOW;
2794 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
2795 }
2796 }
2797 hton24(hdr->dlength, datain->length);
2798 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2799 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
2800 (struct scsi_lun *)&hdr->lun);
2801 else
2802 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
2803
2804 hdr->itt = cmd->init_task_tag;
2805
2806 if (hdr->flags & ISCSI_FLAG_DATA_ACK)
2807 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
2808 else
2809 hdr->ttt = cpu_to_be32(0xFFFFFFFF);
2810 if (set_statsn)
2811 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2812 else
2813 hdr->statsn = cpu_to_be32(0xFFFFFFFF);
2814
2815 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2816 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2817 hdr->datasn = cpu_to_be32(datain->data_sn);
2818 hdr->offset = cpu_to_be32(datain->offset);
2819
2820 pr_debug("Built DataIN ITT: 0x%08x, StatSN: 0x%08x,"
2821 " DataSN: 0x%08x, Offset: %u, Length: %u, CID: %hu\n",
2822 cmd->init_task_tag, ntohl(hdr->statsn), ntohl(hdr->datasn),
2823 ntohl(hdr->offset), datain->length, conn->cid);
2824 }
2825 EXPORT_SYMBOL(iscsit_build_datain_pdu);
2826
iscsit_send_datain(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2827 static int iscsit_send_datain(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2828 {
2829 struct iscsi_data_rsp *hdr = (struct iscsi_data_rsp *)&cmd->pdu[0];
2830 struct iscsi_datain datain;
2831 struct iscsi_datain_req *dr;
2832 int eodr = 0, ret;
2833 bool set_statsn = false;
2834
2835 memset(&datain, 0, sizeof(struct iscsi_datain));
2836 dr = iscsit_get_datain_values(cmd, &datain);
2837 if (!dr) {
2838 pr_err("iscsit_get_datain_values failed for ITT: 0x%08x\n",
2839 cmd->init_task_tag);
2840 return -1;
2841 }
2842 /*
2843 * Be paranoid and double check the logic for now.
2844 */
2845 if ((datain.offset + datain.length) > cmd->se_cmd.data_length) {
2846 pr_err("Command ITT: 0x%08x, datain.offset: %u and"
2847 " datain.length: %u exceeds cmd->data_length: %u\n",
2848 cmd->init_task_tag, datain.offset, datain.length,
2849 cmd->se_cmd.data_length);
2850 return -1;
2851 }
2852
2853 atomic_long_add(datain.length, &conn->sess->tx_data_octets);
2854 /*
2855 * Special case for successfully execution w/ both DATAIN
2856 * and Sense Data.
2857 */
2858 if ((datain.flags & ISCSI_FLAG_DATA_STATUS) &&
2859 (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE))
2860 datain.flags &= ~ISCSI_FLAG_DATA_STATUS;
2861 else {
2862 if ((dr->dr_complete == DATAIN_COMPLETE_NORMAL) ||
2863 (dr->dr_complete == DATAIN_COMPLETE_CONNECTION_RECOVERY)) {
2864 iscsit_increment_maxcmdsn(cmd, conn->sess);
2865 cmd->stat_sn = conn->stat_sn++;
2866 set_statsn = true;
2867 } else if (dr->dr_complete ==
2868 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY)
2869 set_statsn = true;
2870 }
2871
2872 iscsit_build_datain_pdu(cmd, conn, &datain, hdr, set_statsn);
2873
2874 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, dr, &datain, 0);
2875 if (ret < 0)
2876 return ret;
2877
2878 if (dr->dr_complete) {
2879 eodr = (cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ?
2880 2 : 1;
2881 iscsit_free_datain_req(cmd, dr);
2882 }
2883
2884 return eodr;
2885 }
2886
2887 int
iscsit_build_logout_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn,struct iscsi_logout_rsp * hdr)2888 iscsit_build_logout_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2889 struct iscsi_logout_rsp *hdr)
2890 {
2891 struct iscsi_conn *logout_conn = NULL;
2892 struct iscsi_conn_recovery *cr = NULL;
2893 struct iscsi_session *sess = conn->sess;
2894 /*
2895 * The actual shutting down of Sessions and/or Connections
2896 * for CLOSESESSION and CLOSECONNECTION Logout Requests
2897 * is done in scsi_logout_post_handler().
2898 */
2899 switch (cmd->logout_reason) {
2900 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
2901 pr_debug("iSCSI session logout successful, setting"
2902 " logout response to ISCSI_LOGOUT_SUCCESS.\n");
2903 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2904 break;
2905 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
2906 if (cmd->logout_response == ISCSI_LOGOUT_CID_NOT_FOUND)
2907 break;
2908 /*
2909 * For CLOSECONNECTION logout requests carrying
2910 * a matching logout CID -> local CID, the reference
2911 * for the local CID will have been incremented in
2912 * iscsi_logout_closeconnection().
2913 *
2914 * For CLOSECONNECTION logout requests carrying
2915 * a different CID than the connection it arrived
2916 * on, the connection responding to cmd->logout_cid
2917 * is stopped in iscsit_logout_post_handler_diffcid().
2918 */
2919
2920 pr_debug("iSCSI CID: %hu logout on CID: %hu"
2921 " successful.\n", cmd->logout_cid, conn->cid);
2922 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2923 break;
2924 case ISCSI_LOGOUT_REASON_RECOVERY:
2925 if ((cmd->logout_response == ISCSI_LOGOUT_RECOVERY_UNSUPPORTED) ||
2926 (cmd->logout_response == ISCSI_LOGOUT_CLEANUP_FAILED))
2927 break;
2928 /*
2929 * If the connection is still active from our point of view
2930 * force connection recovery to occur.
2931 */
2932 logout_conn = iscsit_get_conn_from_cid_rcfr(sess,
2933 cmd->logout_cid);
2934 if (logout_conn) {
2935 iscsit_connection_reinstatement_rcfr(logout_conn);
2936 iscsit_dec_conn_usage_count(logout_conn);
2937 }
2938
2939 cr = iscsit_get_inactive_connection_recovery_entry(
2940 conn->sess, cmd->logout_cid);
2941 if (!cr) {
2942 pr_err("Unable to locate CID: %hu for"
2943 " REMOVECONNFORRECOVERY Logout Request.\n",
2944 cmd->logout_cid);
2945 cmd->logout_response = ISCSI_LOGOUT_CID_NOT_FOUND;
2946 break;
2947 }
2948
2949 iscsit_discard_cr_cmds_by_expstatsn(cr, cmd->exp_stat_sn);
2950
2951 pr_debug("iSCSI REMOVECONNFORRECOVERY logout"
2952 " for recovery for CID: %hu on CID: %hu successful.\n",
2953 cmd->logout_cid, conn->cid);
2954 cmd->logout_response = ISCSI_LOGOUT_SUCCESS;
2955 break;
2956 default:
2957 pr_err("Unknown cmd->logout_reason: 0x%02x\n",
2958 cmd->logout_reason);
2959 return -1;
2960 }
2961
2962 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
2963 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
2964 hdr->response = cmd->logout_response;
2965 hdr->itt = cmd->init_task_tag;
2966 cmd->stat_sn = conn->stat_sn++;
2967 hdr->statsn = cpu_to_be32(cmd->stat_sn);
2968
2969 iscsit_increment_maxcmdsn(cmd, conn->sess);
2970 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
2971 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
2972
2973 pr_debug("Built Logout Response ITT: 0x%08x StatSN:"
2974 " 0x%08x Response: 0x%02x CID: %hu on CID: %hu\n",
2975 cmd->init_task_tag, cmd->stat_sn, hdr->response,
2976 cmd->logout_cid, conn->cid);
2977
2978 return 0;
2979 }
2980 EXPORT_SYMBOL(iscsit_build_logout_rsp);
2981
2982 static int
iscsit_send_logout(struct iscsi_cmd * cmd,struct iscsi_conn * conn)2983 iscsit_send_logout(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
2984 {
2985 int rc;
2986
2987 rc = iscsit_build_logout_rsp(cmd, conn,
2988 (struct iscsi_logout_rsp *)&cmd->pdu[0]);
2989 if (rc < 0)
2990 return rc;
2991
2992 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
2993 }
2994
2995 void
iscsit_build_nopin_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn,struct iscsi_nopin * hdr,bool nopout_response)2996 iscsit_build_nopin_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
2997 struct iscsi_nopin *hdr, bool nopout_response)
2998 {
2999 hdr->opcode = ISCSI_OP_NOOP_IN;
3000 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3001 hton24(hdr->dlength, cmd->buf_ptr_size);
3002 if (nopout_response)
3003 put_unaligned_le64(0xFFFFFFFFFFFFFFFFULL, &hdr->lun);
3004 hdr->itt = cmd->init_task_tag;
3005 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3006 cmd->stat_sn = (nopout_response) ? conn->stat_sn++ :
3007 conn->stat_sn;
3008 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3009
3010 if (nopout_response)
3011 iscsit_increment_maxcmdsn(cmd, conn->sess);
3012
3013 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3014 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3015
3016 pr_debug("Built NOPIN %s Response ITT: 0x%08x, TTT: 0x%08x,"
3017 " StatSN: 0x%08x, Length %u\n", (nopout_response) ?
3018 "Solicited" : "Unsolicited", cmd->init_task_tag,
3019 cmd->targ_xfer_tag, cmd->stat_sn, cmd->buf_ptr_size);
3020 }
3021 EXPORT_SYMBOL(iscsit_build_nopin_rsp);
3022
3023 /*
3024 * Unsolicited NOPIN, either requesting a response or not.
3025 */
iscsit_send_unsolicited_nopin(struct iscsi_cmd * cmd,struct iscsi_conn * conn,int want_response)3026 static int iscsit_send_unsolicited_nopin(
3027 struct iscsi_cmd *cmd,
3028 struct iscsi_conn *conn,
3029 int want_response)
3030 {
3031 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3032 int ret;
3033
3034 iscsit_build_nopin_rsp(cmd, conn, hdr, false);
3035
3036 pr_debug("Sending Unsolicited NOPIN TTT: 0x%08x StatSN:"
3037 " 0x%08x CID: %hu\n", hdr->ttt, cmd->stat_sn, conn->cid);
3038
3039 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3040 if (ret < 0)
3041 return ret;
3042
3043 spin_lock_bh(&cmd->istate_lock);
3044 cmd->i_state = want_response ?
3045 ISTATE_SENT_NOPIN_WANT_RESPONSE : ISTATE_SENT_STATUS;
3046 spin_unlock_bh(&cmd->istate_lock);
3047
3048 return 0;
3049 }
3050
3051 static int
iscsit_send_nopin(struct iscsi_cmd * cmd,struct iscsi_conn * conn)3052 iscsit_send_nopin(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3053 {
3054 struct iscsi_nopin *hdr = (struct iscsi_nopin *)&cmd->pdu[0];
3055
3056 iscsit_build_nopin_rsp(cmd, conn, hdr, true);
3057
3058 /*
3059 * NOPOUT Ping Data is attached to struct iscsi_cmd->buf_ptr.
3060 * NOPOUT DataSegmentLength is at struct iscsi_cmd->buf_ptr_size.
3061 */
3062 pr_debug("Echoing back %u bytes of ping data.\n", cmd->buf_ptr_size);
3063
3064 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3065 cmd->buf_ptr,
3066 cmd->buf_ptr_size);
3067 }
3068
iscsit_send_r2t(struct iscsi_cmd * cmd,struct iscsi_conn * conn)3069 static int iscsit_send_r2t(
3070 struct iscsi_cmd *cmd,
3071 struct iscsi_conn *conn)
3072 {
3073 struct iscsi_r2t *r2t;
3074 struct iscsi_r2t_rsp *hdr;
3075 int ret;
3076
3077 r2t = iscsit_get_r2t_from_list(cmd);
3078 if (!r2t)
3079 return -1;
3080
3081 hdr = (struct iscsi_r2t_rsp *) cmd->pdu;
3082 memset(hdr, 0, ISCSI_HDR_LEN);
3083 hdr->opcode = ISCSI_OP_R2T;
3084 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3085 int_to_scsilun(cmd->se_cmd.orig_fe_lun,
3086 (struct scsi_lun *)&hdr->lun);
3087 hdr->itt = cmd->init_task_tag;
3088 if (conn->conn_transport->iscsit_get_r2t_ttt)
3089 conn->conn_transport->iscsit_get_r2t_ttt(conn, cmd, r2t);
3090 else
3091 r2t->targ_xfer_tag = session_get_next_ttt(conn->sess);
3092 hdr->ttt = cpu_to_be32(r2t->targ_xfer_tag);
3093 hdr->statsn = cpu_to_be32(conn->stat_sn);
3094 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3095 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3096 hdr->r2tsn = cpu_to_be32(r2t->r2t_sn);
3097 hdr->data_offset = cpu_to_be32(r2t->offset);
3098 hdr->data_length = cpu_to_be32(r2t->xfer_len);
3099
3100 pr_debug("Built %sR2T, ITT: 0x%08x, TTT: 0x%08x, StatSN:"
3101 " 0x%08x, R2TSN: 0x%08x, Offset: %u, DDTL: %u, CID: %hu\n",
3102 (!r2t->recovery_r2t) ? "" : "Recovery ", cmd->init_task_tag,
3103 r2t->targ_xfer_tag, ntohl(hdr->statsn), r2t->r2t_sn,
3104 r2t->offset, r2t->xfer_len, conn->cid);
3105
3106 spin_lock_bh(&cmd->r2t_lock);
3107 r2t->sent_r2t = 1;
3108 spin_unlock_bh(&cmd->r2t_lock);
3109
3110 ret = conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3111 if (ret < 0) {
3112 return ret;
3113 }
3114
3115 spin_lock_bh(&cmd->dataout_timeout_lock);
3116 iscsit_start_dataout_timer(cmd, conn);
3117 spin_unlock_bh(&cmd->dataout_timeout_lock);
3118
3119 return 0;
3120 }
3121
3122 /*
3123 * @recovery: If called from iscsi_task_reassign_complete_write() for
3124 * connection recovery.
3125 */
iscsit_build_r2ts_for_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,bool recovery)3126 int iscsit_build_r2ts_for_cmd(
3127 struct iscsi_conn *conn,
3128 struct iscsi_cmd *cmd,
3129 bool recovery)
3130 {
3131 int first_r2t = 1;
3132 u32 offset = 0, xfer_len = 0;
3133
3134 spin_lock_bh(&cmd->r2t_lock);
3135 if (cmd->cmd_flags & ICF_SENT_LAST_R2T) {
3136 spin_unlock_bh(&cmd->r2t_lock);
3137 return 0;
3138 }
3139
3140 if (conn->sess->sess_ops->DataSequenceInOrder &&
3141 !recovery)
3142 cmd->r2t_offset = max(cmd->r2t_offset, cmd->write_data_done);
3143
3144 while (cmd->outstanding_r2ts < conn->sess->sess_ops->MaxOutstandingR2T) {
3145 if (conn->sess->sess_ops->DataSequenceInOrder) {
3146 offset = cmd->r2t_offset;
3147
3148 if (first_r2t && recovery) {
3149 int new_data_end = offset +
3150 conn->sess->sess_ops->MaxBurstLength -
3151 cmd->next_burst_len;
3152
3153 if (new_data_end > cmd->se_cmd.data_length)
3154 xfer_len = cmd->se_cmd.data_length - offset;
3155 else
3156 xfer_len =
3157 conn->sess->sess_ops->MaxBurstLength -
3158 cmd->next_burst_len;
3159 } else {
3160 int new_data_end = offset +
3161 conn->sess->sess_ops->MaxBurstLength;
3162
3163 if (new_data_end > cmd->se_cmd.data_length)
3164 xfer_len = cmd->se_cmd.data_length - offset;
3165 else
3166 xfer_len = conn->sess->sess_ops->MaxBurstLength;
3167 }
3168
3169 if ((s32)xfer_len < 0) {
3170 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3171 break;
3172 }
3173
3174 cmd->r2t_offset += xfer_len;
3175
3176 if (cmd->r2t_offset == cmd->se_cmd.data_length)
3177 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3178 } else {
3179 struct iscsi_seq *seq;
3180
3181 seq = iscsit_get_seq_holder_for_r2t(cmd);
3182 if (!seq) {
3183 spin_unlock_bh(&cmd->r2t_lock);
3184 return -1;
3185 }
3186
3187 offset = seq->offset;
3188 xfer_len = seq->xfer_len;
3189
3190 if (cmd->seq_send_order == cmd->seq_count)
3191 cmd->cmd_flags |= ICF_SENT_LAST_R2T;
3192 }
3193 cmd->outstanding_r2ts++;
3194 first_r2t = 0;
3195
3196 if (iscsit_add_r2t_to_list(cmd, offset, xfer_len, 0, 0) < 0) {
3197 spin_unlock_bh(&cmd->r2t_lock);
3198 return -1;
3199 }
3200
3201 if (cmd->cmd_flags & ICF_SENT_LAST_R2T)
3202 break;
3203 }
3204 spin_unlock_bh(&cmd->r2t_lock);
3205
3206 return 0;
3207 }
3208 EXPORT_SYMBOL(iscsit_build_r2ts_for_cmd);
3209
iscsit_build_rsp_pdu(struct iscsi_cmd * cmd,struct iscsi_conn * conn,bool inc_stat_sn,struct iscsi_scsi_rsp * hdr)3210 void iscsit_build_rsp_pdu(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3211 bool inc_stat_sn, struct iscsi_scsi_rsp *hdr)
3212 {
3213 if (inc_stat_sn)
3214 cmd->stat_sn = conn->stat_sn++;
3215
3216 atomic_long_inc(&conn->sess->rsp_pdus);
3217
3218 memset(hdr, 0, ISCSI_HDR_LEN);
3219 hdr->opcode = ISCSI_OP_SCSI_CMD_RSP;
3220 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3221 if (cmd->se_cmd.se_cmd_flags & SCF_OVERFLOW_BIT) {
3222 hdr->flags |= ISCSI_FLAG_CMD_OVERFLOW;
3223 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3224 } else if (cmd->se_cmd.se_cmd_flags & SCF_UNDERFLOW_BIT) {
3225 hdr->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
3226 hdr->residual_count = cpu_to_be32(cmd->se_cmd.residual_count);
3227 }
3228 hdr->response = cmd->iscsi_response;
3229 hdr->cmd_status = cmd->se_cmd.scsi_status;
3230 hdr->itt = cmd->init_task_tag;
3231 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3232
3233 iscsit_increment_maxcmdsn(cmd, conn->sess);
3234 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3235 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3236
3237 pr_debug("Built SCSI Response, ITT: 0x%08x, StatSN: 0x%08x,"
3238 " Response: 0x%02x, SAM Status: 0x%02x, CID: %hu\n",
3239 cmd->init_task_tag, cmd->stat_sn, cmd->se_cmd.scsi_status,
3240 cmd->se_cmd.scsi_status, conn->cid);
3241 }
3242 EXPORT_SYMBOL(iscsit_build_rsp_pdu);
3243
iscsit_send_response(struct iscsi_cmd * cmd,struct iscsi_conn * conn)3244 static int iscsit_send_response(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3245 {
3246 struct iscsi_scsi_rsp *hdr = (struct iscsi_scsi_rsp *)&cmd->pdu[0];
3247 bool inc_stat_sn = (cmd->i_state == ISTATE_SEND_STATUS);
3248 void *data_buf = NULL;
3249 u32 padding = 0, data_buf_len = 0;
3250
3251 iscsit_build_rsp_pdu(cmd, conn, inc_stat_sn, hdr);
3252
3253 /*
3254 * Attach SENSE DATA payload to iSCSI Response PDU
3255 */
3256 if (cmd->se_cmd.sense_buffer &&
3257 ((cmd->se_cmd.se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) ||
3258 (cmd->se_cmd.se_cmd_flags & SCF_EMULATED_TASK_SENSE))) {
3259 put_unaligned_be16(cmd->se_cmd.scsi_sense_length, cmd->sense_buffer);
3260 cmd->se_cmd.scsi_sense_length += sizeof (__be16);
3261
3262 padding = -(cmd->se_cmd.scsi_sense_length) & 3;
3263 hton24(hdr->dlength, (u32)cmd->se_cmd.scsi_sense_length);
3264 data_buf = cmd->sense_buffer;
3265 data_buf_len = cmd->se_cmd.scsi_sense_length + padding;
3266
3267 if (padding) {
3268 memset(cmd->sense_buffer +
3269 cmd->se_cmd.scsi_sense_length, 0, padding);
3270 pr_debug("Adding %u bytes of padding to"
3271 " SENSE.\n", padding);
3272 }
3273
3274 pr_debug("Attaching SENSE DATA: %u bytes to iSCSI"
3275 " Response PDU\n",
3276 cmd->se_cmd.scsi_sense_length);
3277 }
3278
3279 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, data_buf,
3280 data_buf_len);
3281 }
3282
iscsit_convert_tcm_tmr_rsp(struct se_tmr_req * se_tmr)3283 static u8 iscsit_convert_tcm_tmr_rsp(struct se_tmr_req *se_tmr)
3284 {
3285 switch (se_tmr->response) {
3286 case TMR_FUNCTION_COMPLETE:
3287 return ISCSI_TMF_RSP_COMPLETE;
3288 case TMR_TASK_DOES_NOT_EXIST:
3289 return ISCSI_TMF_RSP_NO_TASK;
3290 case TMR_LUN_DOES_NOT_EXIST:
3291 return ISCSI_TMF_RSP_NO_LUN;
3292 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
3293 return ISCSI_TMF_RSP_NOT_SUPPORTED;
3294 case TMR_FUNCTION_REJECTED:
3295 default:
3296 return ISCSI_TMF_RSP_REJECTED;
3297 }
3298 }
3299
3300 void
iscsit_build_task_mgt_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn,struct iscsi_tm_rsp * hdr)3301 iscsit_build_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3302 struct iscsi_tm_rsp *hdr)
3303 {
3304 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
3305
3306 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
3307 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3308 hdr->response = iscsit_convert_tcm_tmr_rsp(se_tmr);
3309 hdr->itt = cmd->init_task_tag;
3310 cmd->stat_sn = conn->stat_sn++;
3311 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3312
3313 iscsit_increment_maxcmdsn(cmd, conn->sess);
3314 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3315 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3316
3317 pr_debug("Built Task Management Response ITT: 0x%08x,"
3318 " StatSN: 0x%08x, Response: 0x%02x, CID: %hu\n",
3319 cmd->init_task_tag, cmd->stat_sn, hdr->response, conn->cid);
3320 }
3321 EXPORT_SYMBOL(iscsit_build_task_mgt_rsp);
3322
3323 static int
iscsit_send_task_mgt_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn)3324 iscsit_send_task_mgt_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
3325 {
3326 struct iscsi_tm_rsp *hdr = (struct iscsi_tm_rsp *)&cmd->pdu[0];
3327
3328 iscsit_build_task_mgt_rsp(cmd, conn, hdr);
3329
3330 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL, NULL, 0);
3331 }
3332
3333 #define SENDTARGETS_BUF_LIMIT 32768U
3334
3335 static int
iscsit_build_sendtargets_response(struct iscsi_cmd * cmd,enum iscsit_transport_type network_transport,int skip_bytes,bool * completed)3336 iscsit_build_sendtargets_response(struct iscsi_cmd *cmd,
3337 enum iscsit_transport_type network_transport,
3338 int skip_bytes, bool *completed)
3339 {
3340 char *payload = NULL;
3341 struct iscsi_conn *conn = cmd->conn;
3342 struct iscsi_portal_group *tpg;
3343 struct iscsi_tiqn *tiqn;
3344 struct iscsi_tpg_np *tpg_np;
3345 int buffer_len, end_of_buf = 0, len = 0, payload_len = 0;
3346 int target_name_printed;
3347 unsigned char buf[ISCSI_IQN_LEN+12]; /* iqn + "TargetName=" + \0 */
3348 unsigned char *text_in = cmd->text_in_ptr, *text_ptr = NULL;
3349 bool active;
3350
3351 buffer_len = min(conn->conn_ops->MaxRecvDataSegmentLength,
3352 SENDTARGETS_BUF_LIMIT);
3353
3354 payload = kzalloc(buffer_len, GFP_KERNEL);
3355 if (!payload)
3356 return -ENOMEM;
3357
3358 /*
3359 * Locate pointer to iqn./eui. string for ICF_SENDTARGETS_SINGLE
3360 * explicit case..
3361 */
3362 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) {
3363 text_ptr = strchr(text_in, '=');
3364 if (!text_ptr) {
3365 pr_err("Unable to locate '=' string in text_in:"
3366 " %s\n", text_in);
3367 kfree(payload);
3368 return -EINVAL;
3369 }
3370 /*
3371 * Skip over '=' character..
3372 */
3373 text_ptr += 1;
3374 }
3375
3376 spin_lock(&tiqn_lock);
3377 list_for_each_entry(tiqn, &g_tiqn_list, tiqn_list) {
3378 if ((cmd->cmd_flags & ICF_SENDTARGETS_SINGLE) &&
3379 strcmp(tiqn->tiqn, text_ptr)) {
3380 continue;
3381 }
3382
3383 target_name_printed = 0;
3384
3385 spin_lock(&tiqn->tiqn_tpg_lock);
3386 list_for_each_entry(tpg, &tiqn->tiqn_tpg_list, tpg_list) {
3387
3388 /* If demo_mode_discovery=0 and generate_node_acls=0
3389 * (demo mode dislabed) do not return
3390 * TargetName+TargetAddress unless a NodeACL exists.
3391 */
3392
3393 if ((tpg->tpg_attrib.generate_node_acls == 0) &&
3394 (tpg->tpg_attrib.demo_mode_discovery == 0) &&
3395 (!target_tpg_has_node_acl(&tpg->tpg_se_tpg,
3396 cmd->conn->sess->sess_ops->InitiatorName))) {
3397 continue;
3398 }
3399
3400 spin_lock(&tpg->tpg_state_lock);
3401 active = (tpg->tpg_state == TPG_STATE_ACTIVE);
3402 spin_unlock(&tpg->tpg_state_lock);
3403
3404 if (!active && tpg->tpg_attrib.tpg_enabled_sendtargets)
3405 continue;
3406
3407 spin_lock(&tpg->tpg_np_lock);
3408 list_for_each_entry(tpg_np, &tpg->tpg_gnp_list,
3409 tpg_np_list) {
3410 struct iscsi_np *np = tpg_np->tpg_np;
3411 struct sockaddr_storage *sockaddr;
3412
3413 if (np->np_network_transport != network_transport)
3414 continue;
3415
3416 if (!target_name_printed) {
3417 len = sprintf(buf, "TargetName=%s",
3418 tiqn->tiqn);
3419 len += 1;
3420
3421 if ((len + payload_len) > buffer_len) {
3422 spin_unlock(&tpg->tpg_np_lock);
3423 spin_unlock(&tiqn->tiqn_tpg_lock);
3424 end_of_buf = 1;
3425 goto eob;
3426 }
3427
3428 if (skip_bytes && len <= skip_bytes) {
3429 skip_bytes -= len;
3430 } else {
3431 memcpy(payload + payload_len, buf, len);
3432 payload_len += len;
3433 target_name_printed = 1;
3434 if (len > skip_bytes)
3435 skip_bytes = 0;
3436 }
3437 }
3438
3439 if (inet_addr_is_any((struct sockaddr *)&np->np_sockaddr))
3440 sockaddr = &conn->local_sockaddr;
3441 else
3442 sockaddr = &np->np_sockaddr;
3443
3444 len = sprintf(buf, "TargetAddress="
3445 "%pISpc,%hu",
3446 sockaddr,
3447 tpg->tpgt);
3448 len += 1;
3449
3450 if ((len + payload_len) > buffer_len) {
3451 spin_unlock(&tpg->tpg_np_lock);
3452 spin_unlock(&tiqn->tiqn_tpg_lock);
3453 end_of_buf = 1;
3454 goto eob;
3455 }
3456
3457 if (skip_bytes && len <= skip_bytes) {
3458 skip_bytes -= len;
3459 } else {
3460 memcpy(payload + payload_len, buf, len);
3461 payload_len += len;
3462 if (len > skip_bytes)
3463 skip_bytes = 0;
3464 }
3465 }
3466 spin_unlock(&tpg->tpg_np_lock);
3467 }
3468 spin_unlock(&tiqn->tiqn_tpg_lock);
3469 eob:
3470 if (end_of_buf) {
3471 *completed = false;
3472 break;
3473 }
3474
3475 if (cmd->cmd_flags & ICF_SENDTARGETS_SINGLE)
3476 break;
3477 }
3478 spin_unlock(&tiqn_lock);
3479
3480 cmd->buf_ptr = payload;
3481
3482 return payload_len;
3483 }
3484
3485 int
iscsit_build_text_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn,struct iscsi_text_rsp * hdr,enum iscsit_transport_type network_transport)3486 iscsit_build_text_rsp(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3487 struct iscsi_text_rsp *hdr,
3488 enum iscsit_transport_type network_transport)
3489 {
3490 int text_length, padding;
3491 bool completed = true;
3492
3493 text_length = iscsit_build_sendtargets_response(cmd, network_transport,
3494 cmd->read_data_done,
3495 &completed);
3496 if (text_length < 0)
3497 return text_length;
3498
3499 if (completed) {
3500 hdr->flags = ISCSI_FLAG_CMD_FINAL;
3501 } else {
3502 hdr->flags = ISCSI_FLAG_TEXT_CONTINUE;
3503 cmd->read_data_done += text_length;
3504 if (cmd->targ_xfer_tag == 0xFFFFFFFF)
3505 cmd->targ_xfer_tag = session_get_next_ttt(conn->sess);
3506 }
3507 hdr->opcode = ISCSI_OP_TEXT_RSP;
3508 padding = ((-text_length) & 3);
3509 hton24(hdr->dlength, text_length);
3510 hdr->itt = cmd->init_task_tag;
3511 hdr->ttt = cpu_to_be32(cmd->targ_xfer_tag);
3512 cmd->stat_sn = conn->stat_sn++;
3513 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3514
3515 iscsit_increment_maxcmdsn(cmd, conn->sess);
3516 /*
3517 * Reset maxcmdsn_inc in multi-part text payload exchanges to
3518 * correctly increment MaxCmdSN for each response answering a
3519 * non immediate text request with a valid CmdSN.
3520 */
3521 cmd->maxcmdsn_inc = 0;
3522 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3523 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3524
3525 pr_debug("Built Text Response: ITT: 0x%08x, TTT: 0x%08x, StatSN: 0x%08x,"
3526 " Length: %u, CID: %hu F: %d C: %d\n", cmd->init_task_tag,
3527 cmd->targ_xfer_tag, cmd->stat_sn, text_length, conn->cid,
3528 !!(hdr->flags & ISCSI_FLAG_CMD_FINAL),
3529 !!(hdr->flags & ISCSI_FLAG_TEXT_CONTINUE));
3530
3531 return text_length + padding;
3532 }
3533 EXPORT_SYMBOL(iscsit_build_text_rsp);
3534
iscsit_send_text_rsp(struct iscsi_cmd * cmd,struct iscsi_conn * conn)3535 static int iscsit_send_text_rsp(
3536 struct iscsi_cmd *cmd,
3537 struct iscsi_conn *conn)
3538 {
3539 struct iscsi_text_rsp *hdr = (struct iscsi_text_rsp *)cmd->pdu;
3540 int text_length;
3541
3542 text_length = iscsit_build_text_rsp(cmd, conn, hdr,
3543 conn->conn_transport->transport_type);
3544 if (text_length < 0)
3545 return text_length;
3546
3547 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3548 cmd->buf_ptr,
3549 text_length);
3550 }
3551
3552 void
iscsit_build_reject(struct iscsi_cmd * cmd,struct iscsi_conn * conn,struct iscsi_reject * hdr)3553 iscsit_build_reject(struct iscsi_cmd *cmd, struct iscsi_conn *conn,
3554 struct iscsi_reject *hdr)
3555 {
3556 hdr->opcode = ISCSI_OP_REJECT;
3557 hdr->reason = cmd->reject_reason;
3558 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
3559 hton24(hdr->dlength, ISCSI_HDR_LEN);
3560 hdr->ffffffff = cpu_to_be32(0xffffffff);
3561 cmd->stat_sn = conn->stat_sn++;
3562 hdr->statsn = cpu_to_be32(cmd->stat_sn);
3563 hdr->exp_cmdsn = cpu_to_be32(conn->sess->exp_cmd_sn);
3564 hdr->max_cmdsn = cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
3565
3566 }
3567 EXPORT_SYMBOL(iscsit_build_reject);
3568
iscsit_send_reject(struct iscsi_cmd * cmd,struct iscsi_conn * conn)3569 static int iscsit_send_reject(
3570 struct iscsi_cmd *cmd,
3571 struct iscsi_conn *conn)
3572 {
3573 struct iscsi_reject *hdr = (struct iscsi_reject *)&cmd->pdu[0];
3574
3575 iscsit_build_reject(cmd, conn, hdr);
3576
3577 pr_debug("Built Reject PDU StatSN: 0x%08x, Reason: 0x%02x,"
3578 " CID: %hu\n", ntohl(hdr->statsn), hdr->reason, conn->cid);
3579
3580 return conn->conn_transport->iscsit_xmit_pdu(conn, cmd, NULL,
3581 cmd->buf_ptr,
3582 ISCSI_HDR_LEN);
3583 }
3584
iscsit_thread_get_cpumask(struct iscsi_conn * conn)3585 void iscsit_thread_get_cpumask(struct iscsi_conn *conn)
3586 {
3587 int ord, cpu;
3588 /*
3589 * bitmap_id is assigned from iscsit_global->ts_bitmap from
3590 * within iscsit_start_kthreads()
3591 *
3592 * Here we use bitmap_id to determine which CPU that this
3593 * iSCSI connection's RX/TX threads will be scheduled to
3594 * execute upon.
3595 */
3596 ord = conn->bitmap_id % cpumask_weight(cpu_online_mask);
3597 for_each_online_cpu(cpu) {
3598 if (ord-- == 0) {
3599 cpumask_set_cpu(cpu, conn->conn_cpumask);
3600 return;
3601 }
3602 }
3603 /*
3604 * This should never be reached..
3605 */
3606 dump_stack();
3607 cpumask_setall(conn->conn_cpumask);
3608 }
3609
3610 int
iscsit_immediate_queue(struct iscsi_conn * conn,struct iscsi_cmd * cmd,int state)3611 iscsit_immediate_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3612 {
3613 int ret;
3614
3615 switch (state) {
3616 case ISTATE_SEND_R2T:
3617 ret = iscsit_send_r2t(cmd, conn);
3618 if (ret < 0)
3619 goto err;
3620 break;
3621 case ISTATE_REMOVE:
3622 spin_lock_bh(&conn->cmd_lock);
3623 list_del_init(&cmd->i_conn_node);
3624 spin_unlock_bh(&conn->cmd_lock);
3625
3626 iscsit_free_cmd(cmd, false);
3627 break;
3628 case ISTATE_SEND_NOPIN_WANT_RESPONSE:
3629 iscsit_mod_nopin_response_timer(conn);
3630 ret = iscsit_send_unsolicited_nopin(cmd, conn, 1);
3631 if (ret < 0)
3632 goto err;
3633 break;
3634 case ISTATE_SEND_NOPIN_NO_RESPONSE:
3635 ret = iscsit_send_unsolicited_nopin(cmd, conn, 0);
3636 if (ret < 0)
3637 goto err;
3638 break;
3639 default:
3640 pr_err("Unknown Opcode: 0x%02x ITT:"
3641 " 0x%08x, i_state: %d on CID: %hu\n",
3642 cmd->iscsi_opcode, cmd->init_task_tag, state,
3643 conn->cid);
3644 goto err;
3645 }
3646
3647 return 0;
3648
3649 err:
3650 return -1;
3651 }
3652 EXPORT_SYMBOL(iscsit_immediate_queue);
3653
3654 static int
iscsit_handle_immediate_queue(struct iscsi_conn * conn)3655 iscsit_handle_immediate_queue(struct iscsi_conn *conn)
3656 {
3657 struct iscsit_transport *t = conn->conn_transport;
3658 struct iscsi_queue_req *qr;
3659 struct iscsi_cmd *cmd;
3660 u8 state;
3661 int ret;
3662
3663 while ((qr = iscsit_get_cmd_from_immediate_queue(conn))) {
3664 atomic_set(&conn->check_immediate_queue, 0);
3665 cmd = qr->cmd;
3666 state = qr->state;
3667 kmem_cache_free(lio_qr_cache, qr);
3668
3669 ret = t->iscsit_immediate_queue(conn, cmd, state);
3670 if (ret < 0)
3671 return ret;
3672 }
3673
3674 return 0;
3675 }
3676
3677 int
iscsit_response_queue(struct iscsi_conn * conn,struct iscsi_cmd * cmd,int state)3678 iscsit_response_queue(struct iscsi_conn *conn, struct iscsi_cmd *cmd, int state)
3679 {
3680 int ret;
3681
3682 check_rsp_state:
3683 switch (state) {
3684 case ISTATE_SEND_DATAIN:
3685 ret = iscsit_send_datain(cmd, conn);
3686 if (ret < 0)
3687 goto err;
3688 else if (!ret)
3689 /* more drs */
3690 goto check_rsp_state;
3691 else if (ret == 1) {
3692 /* all done */
3693 spin_lock_bh(&cmd->istate_lock);
3694 cmd->i_state = ISTATE_SENT_STATUS;
3695 spin_unlock_bh(&cmd->istate_lock);
3696
3697 if (atomic_read(&conn->check_immediate_queue))
3698 return 1;
3699
3700 return 0;
3701 } else if (ret == 2) {
3702 /* Still must send status,
3703 SCF_TRANSPORT_TASK_SENSE was set */
3704 spin_lock_bh(&cmd->istate_lock);
3705 cmd->i_state = ISTATE_SEND_STATUS;
3706 spin_unlock_bh(&cmd->istate_lock);
3707 state = ISTATE_SEND_STATUS;
3708 goto check_rsp_state;
3709 }
3710
3711 break;
3712 case ISTATE_SEND_STATUS:
3713 case ISTATE_SEND_STATUS_RECOVERY:
3714 ret = iscsit_send_response(cmd, conn);
3715 break;
3716 case ISTATE_SEND_LOGOUTRSP:
3717 ret = iscsit_send_logout(cmd, conn);
3718 break;
3719 case ISTATE_SEND_ASYNCMSG:
3720 ret = iscsit_send_conn_drop_async_message(
3721 cmd, conn);
3722 break;
3723 case ISTATE_SEND_NOPIN:
3724 ret = iscsit_send_nopin(cmd, conn);
3725 break;
3726 case ISTATE_SEND_REJECT:
3727 ret = iscsit_send_reject(cmd, conn);
3728 break;
3729 case ISTATE_SEND_TASKMGTRSP:
3730 ret = iscsit_send_task_mgt_rsp(cmd, conn);
3731 if (ret != 0)
3732 break;
3733 ret = iscsit_tmr_post_handler(cmd, conn);
3734 if (ret != 0)
3735 iscsit_fall_back_to_erl0(conn->sess);
3736 break;
3737 case ISTATE_SEND_TEXTRSP:
3738 ret = iscsit_send_text_rsp(cmd, conn);
3739 break;
3740 default:
3741 pr_err("Unknown Opcode: 0x%02x ITT:"
3742 " 0x%08x, i_state: %d on CID: %hu\n",
3743 cmd->iscsi_opcode, cmd->init_task_tag,
3744 state, conn->cid);
3745 goto err;
3746 }
3747 if (ret < 0)
3748 goto err;
3749
3750 switch (state) {
3751 case ISTATE_SEND_LOGOUTRSP:
3752 if (!iscsit_logout_post_handler(cmd, conn))
3753 return -ECONNRESET;
3754 fallthrough;
3755 case ISTATE_SEND_STATUS:
3756 case ISTATE_SEND_ASYNCMSG:
3757 case ISTATE_SEND_NOPIN:
3758 case ISTATE_SEND_STATUS_RECOVERY:
3759 case ISTATE_SEND_TEXTRSP:
3760 case ISTATE_SEND_TASKMGTRSP:
3761 case ISTATE_SEND_REJECT:
3762 spin_lock_bh(&cmd->istate_lock);
3763 cmd->i_state = ISTATE_SENT_STATUS;
3764 spin_unlock_bh(&cmd->istate_lock);
3765 break;
3766 default:
3767 pr_err("Unknown Opcode: 0x%02x ITT:"
3768 " 0x%08x, i_state: %d on CID: %hu\n",
3769 cmd->iscsi_opcode, cmd->init_task_tag,
3770 cmd->i_state, conn->cid);
3771 goto err;
3772 }
3773
3774 if (atomic_read(&conn->check_immediate_queue))
3775 return 1;
3776
3777 return 0;
3778
3779 err:
3780 return -1;
3781 }
3782 EXPORT_SYMBOL(iscsit_response_queue);
3783
iscsit_handle_response_queue(struct iscsi_conn * conn)3784 static int iscsit_handle_response_queue(struct iscsi_conn *conn)
3785 {
3786 struct iscsit_transport *t = conn->conn_transport;
3787 struct iscsi_queue_req *qr;
3788 struct iscsi_cmd *cmd;
3789 u8 state;
3790 int ret;
3791
3792 while ((qr = iscsit_get_cmd_from_response_queue(conn))) {
3793 cmd = qr->cmd;
3794 state = qr->state;
3795 kmem_cache_free(lio_qr_cache, qr);
3796
3797 ret = t->iscsit_response_queue(conn, cmd, state);
3798 if (ret == 1 || ret < 0)
3799 return ret;
3800 }
3801
3802 return 0;
3803 }
3804
iscsi_target_tx_thread(void * arg)3805 int iscsi_target_tx_thread(void *arg)
3806 {
3807 int ret = 0;
3808 struct iscsi_conn *conn = arg;
3809 bool conn_freed = false;
3810
3811 /*
3812 * Allow ourselves to be interrupted by SIGINT so that a
3813 * connection recovery / failure event can be triggered externally.
3814 */
3815 allow_signal(SIGINT);
3816
3817 while (!kthread_should_stop()) {
3818 /*
3819 * Ensure that both TX and RX per connection kthreads
3820 * are scheduled to run on the same CPU.
3821 */
3822 iscsit_thread_check_cpumask(conn, current, 1);
3823
3824 wait_event_interruptible(conn->queues_wq,
3825 !iscsit_conn_all_queues_empty(conn));
3826
3827 if (signal_pending(current))
3828 goto transport_err;
3829
3830 get_immediate:
3831 ret = iscsit_handle_immediate_queue(conn);
3832 if (ret < 0)
3833 goto transport_err;
3834
3835 ret = iscsit_handle_response_queue(conn);
3836 if (ret == 1) {
3837 goto get_immediate;
3838 } else if (ret == -ECONNRESET) {
3839 conn_freed = true;
3840 goto out;
3841 } else if (ret < 0) {
3842 goto transport_err;
3843 }
3844 }
3845
3846 transport_err:
3847 /*
3848 * Avoid the normal connection failure code-path if this connection
3849 * is still within LOGIN mode, and iscsi_np process context is
3850 * responsible for cleaning up the early connection failure.
3851 */
3852 if (conn->conn_state != TARG_CONN_STATE_IN_LOGIN)
3853 iscsit_take_action_for_connection_exit(conn, &conn_freed);
3854 out:
3855 if (!conn_freed) {
3856 while (!kthread_should_stop()) {
3857 msleep(100);
3858 }
3859 }
3860 return 0;
3861 }
3862
iscsi_target_rx_opcode(struct iscsi_conn * conn,unsigned char * buf)3863 static int iscsi_target_rx_opcode(struct iscsi_conn *conn, unsigned char *buf)
3864 {
3865 struct iscsi_hdr *hdr = (struct iscsi_hdr *)buf;
3866 struct iscsi_cmd *cmd;
3867 int ret = 0;
3868
3869 switch (hdr->opcode & ISCSI_OPCODE_MASK) {
3870 case ISCSI_OP_SCSI_CMD:
3871 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3872 if (!cmd)
3873 goto reject;
3874
3875 ret = iscsit_handle_scsi_cmd(conn, cmd, buf);
3876 break;
3877 case ISCSI_OP_SCSI_DATA_OUT:
3878 ret = iscsit_handle_data_out(conn, buf);
3879 break;
3880 case ISCSI_OP_NOOP_OUT:
3881 cmd = NULL;
3882 if (hdr->ttt == cpu_to_be32(0xFFFFFFFF)) {
3883 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3884 if (!cmd)
3885 goto reject;
3886 }
3887 ret = iscsit_handle_nop_out(conn, cmd, buf);
3888 break;
3889 case ISCSI_OP_SCSI_TMFUNC:
3890 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3891 if (!cmd)
3892 goto reject;
3893
3894 ret = iscsit_handle_task_mgt_cmd(conn, cmd, buf);
3895 break;
3896 case ISCSI_OP_TEXT:
3897 if (hdr->ttt != cpu_to_be32(0xFFFFFFFF)) {
3898 cmd = iscsit_find_cmd_from_itt(conn, hdr->itt);
3899 if (!cmd)
3900 goto reject;
3901 } else {
3902 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3903 if (!cmd)
3904 goto reject;
3905 }
3906
3907 ret = iscsit_handle_text_cmd(conn, cmd, buf);
3908 break;
3909 case ISCSI_OP_LOGOUT:
3910 cmd = iscsit_allocate_cmd(conn, TASK_INTERRUPTIBLE);
3911 if (!cmd)
3912 goto reject;
3913
3914 ret = iscsit_handle_logout_cmd(conn, cmd, buf);
3915 if (ret > 0)
3916 wait_for_completion_timeout(&conn->conn_logout_comp,
3917 SECONDS_FOR_LOGOUT_COMP * HZ);
3918 break;
3919 case ISCSI_OP_SNACK:
3920 ret = iscsit_handle_snack(conn, buf);
3921 break;
3922 default:
3923 pr_err("Got unknown iSCSI OpCode: 0x%02x\n", hdr->opcode);
3924 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
3925 pr_err("Cannot recover from unknown"
3926 " opcode while ERL=0, closing iSCSI connection.\n");
3927 return -1;
3928 }
3929 pr_err("Unable to recover from unknown opcode while OFMarker=No,"
3930 " closing iSCSI connection.\n");
3931 ret = -1;
3932 break;
3933 }
3934
3935 return ret;
3936 reject:
3937 return iscsit_add_reject(conn, ISCSI_REASON_BOOKMARK_NO_RESOURCES, buf);
3938 }
3939
iscsi_target_check_conn_state(struct iscsi_conn * conn)3940 static bool iscsi_target_check_conn_state(struct iscsi_conn *conn)
3941 {
3942 bool ret;
3943
3944 spin_lock_bh(&conn->state_lock);
3945 ret = (conn->conn_state != TARG_CONN_STATE_LOGGED_IN);
3946 spin_unlock_bh(&conn->state_lock);
3947
3948 return ret;
3949 }
3950
iscsit_get_rx_pdu(struct iscsi_conn * conn)3951 static void iscsit_get_rx_pdu(struct iscsi_conn *conn)
3952 {
3953 int ret;
3954 u8 *buffer, opcode;
3955 u32 checksum = 0, digest = 0;
3956 struct kvec iov;
3957
3958 buffer = kcalloc(ISCSI_HDR_LEN, sizeof(*buffer), GFP_KERNEL);
3959 if (!buffer)
3960 return;
3961
3962 while (!kthread_should_stop()) {
3963 /*
3964 * Ensure that both TX and RX per connection kthreads
3965 * are scheduled to run on the same CPU.
3966 */
3967 iscsit_thread_check_cpumask(conn, current, 0);
3968
3969 memset(&iov, 0, sizeof(struct kvec));
3970
3971 iov.iov_base = buffer;
3972 iov.iov_len = ISCSI_HDR_LEN;
3973
3974 ret = rx_data(conn, &iov, 1, ISCSI_HDR_LEN);
3975 if (ret != ISCSI_HDR_LEN) {
3976 iscsit_rx_thread_wait_for_tcp(conn);
3977 break;
3978 }
3979
3980 if (conn->conn_ops->HeaderDigest) {
3981 iov.iov_base = &digest;
3982 iov.iov_len = ISCSI_CRC_LEN;
3983
3984 ret = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
3985 if (ret != ISCSI_CRC_LEN) {
3986 iscsit_rx_thread_wait_for_tcp(conn);
3987 break;
3988 }
3989
3990 iscsit_do_crypto_hash_buf(conn->conn_rx_hash, buffer,
3991 ISCSI_HDR_LEN, 0, NULL,
3992 &checksum);
3993
3994 if (digest != checksum) {
3995 pr_err("HeaderDigest CRC32C failed,"
3996 " received 0x%08x, computed 0x%08x\n",
3997 digest, checksum);
3998 /*
3999 * Set the PDU to 0xff so it will intentionally
4000 * hit default in the switch below.
4001 */
4002 memset(buffer, 0xff, ISCSI_HDR_LEN);
4003 atomic_long_inc(&conn->sess->conn_digest_errors);
4004 } else {
4005 pr_debug("Got HeaderDigest CRC32C"
4006 " 0x%08x\n", checksum);
4007 }
4008 }
4009
4010 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT)
4011 break;
4012
4013 opcode = buffer[0] & ISCSI_OPCODE_MASK;
4014
4015 if (conn->sess->sess_ops->SessionType &&
4016 ((!(opcode & ISCSI_OP_TEXT)) ||
4017 (!(opcode & ISCSI_OP_LOGOUT)))) {
4018 pr_err("Received illegal iSCSI Opcode: 0x%02x"
4019 " while in Discovery Session, rejecting.\n", opcode);
4020 iscsit_add_reject(conn, ISCSI_REASON_PROTOCOL_ERROR,
4021 buffer);
4022 break;
4023 }
4024
4025 ret = iscsi_target_rx_opcode(conn, buffer);
4026 if (ret < 0)
4027 break;
4028 }
4029
4030 kfree(buffer);
4031 }
4032
iscsi_target_rx_thread(void * arg)4033 int iscsi_target_rx_thread(void *arg)
4034 {
4035 int rc;
4036 struct iscsi_conn *conn = arg;
4037 bool conn_freed = false;
4038
4039 /*
4040 * Allow ourselves to be interrupted by SIGINT so that a
4041 * connection recovery / failure event can be triggered externally.
4042 */
4043 allow_signal(SIGINT);
4044 /*
4045 * Wait for iscsi_post_login_handler() to complete before allowing
4046 * incoming iscsi/tcp socket I/O, and/or failing the connection.
4047 */
4048 rc = wait_for_completion_interruptible(&conn->rx_login_comp);
4049 if (rc < 0 || iscsi_target_check_conn_state(conn))
4050 goto out;
4051
4052 if (!conn->conn_transport->iscsit_get_rx_pdu)
4053 return 0;
4054
4055 conn->conn_transport->iscsit_get_rx_pdu(conn);
4056
4057 if (!signal_pending(current))
4058 atomic_set(&conn->transport_failed, 1);
4059 iscsit_take_action_for_connection_exit(conn, &conn_freed);
4060
4061 out:
4062 if (!conn_freed) {
4063 while (!kthread_should_stop()) {
4064 msleep(100);
4065 }
4066 }
4067
4068 return 0;
4069 }
4070
iscsit_release_commands_from_conn(struct iscsi_conn * conn)4071 static void iscsit_release_commands_from_conn(struct iscsi_conn *conn)
4072 {
4073 LIST_HEAD(tmp_list);
4074 struct iscsi_cmd *cmd = NULL, *cmd_tmp = NULL;
4075 struct iscsi_session *sess = conn->sess;
4076 /*
4077 * We expect this function to only ever be called from either RX or TX
4078 * thread context via iscsit_close_connection() once the other context
4079 * has been reset -> returned sleeping pre-handler state.
4080 */
4081 spin_lock_bh(&conn->cmd_lock);
4082 list_splice_init(&conn->conn_cmd_list, &tmp_list);
4083
4084 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4085 struct se_cmd *se_cmd = &cmd->se_cmd;
4086
4087 if (!se_cmd->se_tfo)
4088 continue;
4089
4090 spin_lock_irq(&se_cmd->t_state_lock);
4091 if (se_cmd->transport_state & CMD_T_ABORTED) {
4092 if (!(se_cmd->transport_state & CMD_T_TAS))
4093 /*
4094 * LIO's abort path owns the cleanup for this,
4095 * so put it back on the list and let
4096 * aborted_task handle it.
4097 */
4098 list_move_tail(&cmd->i_conn_node,
4099 &conn->conn_cmd_list);
4100 } else {
4101 se_cmd->transport_state |= CMD_T_FABRIC_STOP;
4102 }
4103 spin_unlock_irq(&se_cmd->t_state_lock);
4104 }
4105 spin_unlock_bh(&conn->cmd_lock);
4106
4107 list_for_each_entry_safe(cmd, cmd_tmp, &tmp_list, i_conn_node) {
4108 list_del_init(&cmd->i_conn_node);
4109
4110 iscsit_increment_maxcmdsn(cmd, sess);
4111 iscsit_free_cmd(cmd, true);
4112
4113 }
4114 }
4115
iscsit_stop_timers_for_cmds(struct iscsi_conn * conn)4116 static void iscsit_stop_timers_for_cmds(
4117 struct iscsi_conn *conn)
4118 {
4119 struct iscsi_cmd *cmd;
4120
4121 spin_lock_bh(&conn->cmd_lock);
4122 list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4123 if (cmd->data_direction == DMA_TO_DEVICE)
4124 iscsit_stop_dataout_timer(cmd);
4125 }
4126 spin_unlock_bh(&conn->cmd_lock);
4127 }
4128
iscsit_close_connection(struct iscsi_conn * conn)4129 int iscsit_close_connection(
4130 struct iscsi_conn *conn)
4131 {
4132 int conn_logout = (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT);
4133 struct iscsi_session *sess = conn->sess;
4134
4135 pr_debug("Closing iSCSI connection CID %hu on SID:"
4136 " %u\n", conn->cid, sess->sid);
4137 /*
4138 * Always up conn_logout_comp for the traditional TCP and HW_OFFLOAD
4139 * case just in case the RX Thread in iscsi_target_rx_opcode() is
4140 * sleeping and the logout response never got sent because the
4141 * connection failed.
4142 *
4143 * However for iser-target, isert_wait4logout() is using conn_logout_comp
4144 * to signal logout response TX interrupt completion. Go ahead and skip
4145 * this for iser since isert_rx_opcode() does not wait on logout failure,
4146 * and to avoid iscsi_conn pointer dereference in iser-target code.
4147 */
4148 if (!conn->conn_transport->rdma_shutdown)
4149 complete(&conn->conn_logout_comp);
4150
4151 if (!strcmp(current->comm, ISCSI_RX_THREAD_NAME)) {
4152 if (conn->tx_thread &&
4153 cmpxchg(&conn->tx_thread_active, true, false)) {
4154 send_sig(SIGINT, conn->tx_thread, 1);
4155 kthread_stop(conn->tx_thread);
4156 }
4157 } else if (!strcmp(current->comm, ISCSI_TX_THREAD_NAME)) {
4158 if (conn->rx_thread &&
4159 cmpxchg(&conn->rx_thread_active, true, false)) {
4160 send_sig(SIGINT, conn->rx_thread, 1);
4161 kthread_stop(conn->rx_thread);
4162 }
4163 }
4164
4165 spin_lock(&iscsit_global->ts_bitmap_lock);
4166 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
4167 get_order(1));
4168 spin_unlock(&iscsit_global->ts_bitmap_lock);
4169
4170 iscsit_stop_timers_for_cmds(conn);
4171 iscsit_stop_nopin_response_timer(conn);
4172 iscsit_stop_nopin_timer(conn);
4173
4174 if (conn->conn_transport->iscsit_wait_conn)
4175 conn->conn_transport->iscsit_wait_conn(conn);
4176
4177 /*
4178 * During Connection recovery drop unacknowledged out of order
4179 * commands for this connection, and prepare the other commands
4180 * for reallegiance.
4181 *
4182 * During normal operation clear the out of order commands (but
4183 * do not free the struct iscsi_ooo_cmdsn's) and release all
4184 * struct iscsi_cmds.
4185 */
4186 if (atomic_read(&conn->connection_recovery)) {
4187 iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(conn);
4188 iscsit_prepare_cmds_for_reallegiance(conn);
4189 } else {
4190 iscsit_clear_ooo_cmdsns_for_conn(conn);
4191 iscsit_release_commands_from_conn(conn);
4192 }
4193 iscsit_free_queue_reqs_for_conn(conn);
4194
4195 /*
4196 * Handle decrementing session or connection usage count if
4197 * a logout response was not able to be sent because the
4198 * connection failed. Fall back to Session Recovery here.
4199 */
4200 if (atomic_read(&conn->conn_logout_remove)) {
4201 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_SESSION) {
4202 iscsit_dec_conn_usage_count(conn);
4203 iscsit_dec_session_usage_count(sess);
4204 }
4205 if (conn->conn_logout_reason == ISCSI_LOGOUT_REASON_CLOSE_CONNECTION)
4206 iscsit_dec_conn_usage_count(conn);
4207
4208 atomic_set(&conn->conn_logout_remove, 0);
4209 atomic_set(&sess->session_reinstatement, 0);
4210 atomic_set(&sess->session_fall_back_to_erl0, 1);
4211 }
4212
4213 spin_lock_bh(&sess->conn_lock);
4214 list_del(&conn->conn_list);
4215
4216 /*
4217 * Attempt to let the Initiator know this connection failed by
4218 * sending an Connection Dropped Async Message on another
4219 * active connection.
4220 */
4221 if (atomic_read(&conn->connection_recovery))
4222 iscsit_build_conn_drop_async_message(conn);
4223
4224 spin_unlock_bh(&sess->conn_lock);
4225
4226 /*
4227 * If connection reinstatement is being performed on this connection,
4228 * up the connection reinstatement semaphore that is being blocked on
4229 * in iscsit_cause_connection_reinstatement().
4230 */
4231 spin_lock_bh(&conn->state_lock);
4232 if (atomic_read(&conn->sleep_on_conn_wait_comp)) {
4233 spin_unlock_bh(&conn->state_lock);
4234 complete(&conn->conn_wait_comp);
4235 wait_for_completion(&conn->conn_post_wait_comp);
4236 spin_lock_bh(&conn->state_lock);
4237 }
4238
4239 /*
4240 * If connection reinstatement is being performed on this connection
4241 * by receiving a REMOVECONNFORRECOVERY logout request, up the
4242 * connection wait rcfr semaphore that is being blocked on
4243 * an iscsit_connection_reinstatement_rcfr().
4244 */
4245 if (atomic_read(&conn->connection_wait_rcfr)) {
4246 spin_unlock_bh(&conn->state_lock);
4247 complete(&conn->conn_wait_rcfr_comp);
4248 wait_for_completion(&conn->conn_post_wait_comp);
4249 spin_lock_bh(&conn->state_lock);
4250 }
4251 atomic_set(&conn->connection_reinstatement, 1);
4252 spin_unlock_bh(&conn->state_lock);
4253
4254 /*
4255 * If any other processes are accessing this connection pointer we
4256 * must wait until they have completed.
4257 */
4258 iscsit_check_conn_usage_count(conn);
4259
4260 ahash_request_free(conn->conn_tx_hash);
4261 if (conn->conn_rx_hash) {
4262 struct crypto_ahash *tfm;
4263
4264 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
4265 ahash_request_free(conn->conn_rx_hash);
4266 crypto_free_ahash(tfm);
4267 }
4268
4269 if (conn->sock)
4270 sock_release(conn->sock);
4271
4272 if (conn->conn_transport->iscsit_free_conn)
4273 conn->conn_transport->iscsit_free_conn(conn);
4274
4275 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
4276 conn->conn_state = TARG_CONN_STATE_FREE;
4277 iscsit_free_conn(conn);
4278
4279 spin_lock_bh(&sess->conn_lock);
4280 atomic_dec(&sess->nconn);
4281 pr_debug("Decremented iSCSI connection count to %hu from node:"
4282 " %s\n", atomic_read(&sess->nconn),
4283 sess->sess_ops->InitiatorName);
4284 /*
4285 * Make sure that if one connection fails in an non ERL=2 iSCSI
4286 * Session that they all fail.
4287 */
4288 if ((sess->sess_ops->ErrorRecoveryLevel != 2) && !conn_logout &&
4289 !atomic_read(&sess->session_logout))
4290 atomic_set(&sess->session_fall_back_to_erl0, 1);
4291
4292 /*
4293 * If this was not the last connection in the session, and we are
4294 * performing session reinstatement or falling back to ERL=0, call
4295 * iscsit_stop_session() without sleeping to shutdown the other
4296 * active connections.
4297 */
4298 if (atomic_read(&sess->nconn)) {
4299 if (!atomic_read(&sess->session_reinstatement) &&
4300 !atomic_read(&sess->session_fall_back_to_erl0)) {
4301 spin_unlock_bh(&sess->conn_lock);
4302 return 0;
4303 }
4304 if (!atomic_read(&sess->session_stop_active)) {
4305 atomic_set(&sess->session_stop_active, 1);
4306 spin_unlock_bh(&sess->conn_lock);
4307 iscsit_stop_session(sess, 0, 0);
4308 return 0;
4309 }
4310 spin_unlock_bh(&sess->conn_lock);
4311 return 0;
4312 }
4313
4314 /*
4315 * If this was the last connection in the session and one of the
4316 * following is occurring:
4317 *
4318 * Session Reinstatement is not being performed, and are falling back
4319 * to ERL=0 call iscsit_close_session().
4320 *
4321 * Session Logout was requested. iscsit_close_session() will be called
4322 * elsewhere.
4323 *
4324 * Session Continuation is not being performed, start the Time2Retain
4325 * handler and check if sleep_on_sess_wait_sem is active.
4326 */
4327 if (!atomic_read(&sess->session_reinstatement) &&
4328 atomic_read(&sess->session_fall_back_to_erl0)) {
4329 spin_unlock_bh(&sess->conn_lock);
4330 complete_all(&sess->session_wait_comp);
4331 iscsit_close_session(sess);
4332
4333 return 0;
4334 } else if (atomic_read(&sess->session_logout)) {
4335 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4336 sess->session_state = TARG_SESS_STATE_FREE;
4337
4338 if (atomic_read(&sess->session_close)) {
4339 spin_unlock_bh(&sess->conn_lock);
4340 complete_all(&sess->session_wait_comp);
4341 iscsit_close_session(sess);
4342 } else {
4343 spin_unlock_bh(&sess->conn_lock);
4344 }
4345
4346 return 0;
4347 } else {
4348 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4349 sess->session_state = TARG_SESS_STATE_FAILED;
4350
4351 if (!atomic_read(&sess->session_continuation))
4352 iscsit_start_time2retain_handler(sess);
4353
4354 if (atomic_read(&sess->session_close)) {
4355 spin_unlock_bh(&sess->conn_lock);
4356 complete_all(&sess->session_wait_comp);
4357 iscsit_close_session(sess);
4358 } else {
4359 spin_unlock_bh(&sess->conn_lock);
4360 }
4361
4362 return 0;
4363 }
4364 }
4365
4366 /*
4367 * If the iSCSI Session for the iSCSI Initiator Node exists,
4368 * forcefully shutdown the iSCSI NEXUS.
4369 */
iscsit_close_session(struct iscsi_session * sess)4370 int iscsit_close_session(struct iscsi_session *sess)
4371 {
4372 struct iscsi_portal_group *tpg = sess->tpg;
4373 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4374
4375 if (atomic_read(&sess->nconn)) {
4376 pr_err("%d connection(s) still exist for iSCSI session"
4377 " to %s\n", atomic_read(&sess->nconn),
4378 sess->sess_ops->InitiatorName);
4379 BUG();
4380 }
4381
4382 spin_lock_bh(&se_tpg->session_lock);
4383 atomic_set(&sess->session_logout, 1);
4384 atomic_set(&sess->session_reinstatement, 1);
4385 iscsit_stop_time2retain_timer(sess);
4386 spin_unlock_bh(&se_tpg->session_lock);
4387
4388 if (sess->sess_ops->ErrorRecoveryLevel == 2)
4389 iscsit_free_connection_recovery_entries(sess);
4390
4391 /*
4392 * transport_deregister_session_configfs() will clear the
4393 * struct se_node_acl->nacl_sess pointer now as a iscsi_np process context
4394 * can be setting it again with __transport_register_session() in
4395 * iscsi_post_login_handler() again after the iscsit_stop_session()
4396 * completes in iscsi_np context.
4397 */
4398 transport_deregister_session_configfs(sess->se_sess);
4399
4400 /*
4401 * If any other processes are accessing this session pointer we must
4402 * wait until they have completed. If we are in an interrupt (the
4403 * time2retain handler) and contain and active session usage count we
4404 * restart the timer and exit.
4405 */
4406 if (!in_interrupt()) {
4407 iscsit_check_session_usage_count(sess);
4408 } else {
4409 if (iscsit_check_session_usage_count(sess) == 2) {
4410 atomic_set(&sess->session_logout, 0);
4411 iscsit_start_time2retain_handler(sess);
4412 return 0;
4413 }
4414 }
4415
4416 transport_deregister_session(sess->se_sess);
4417
4418 iscsit_free_all_ooo_cmdsns(sess);
4419
4420 spin_lock_bh(&se_tpg->session_lock);
4421 pr_debug("Moving to TARG_SESS_STATE_FREE.\n");
4422 sess->session_state = TARG_SESS_STATE_FREE;
4423 pr_debug("Released iSCSI session from node: %s\n",
4424 sess->sess_ops->InitiatorName);
4425 tpg->nsessions--;
4426 if (tpg->tpg_tiqn)
4427 tpg->tpg_tiqn->tiqn_nsessions--;
4428
4429 pr_debug("Decremented number of active iSCSI Sessions on"
4430 " iSCSI TPG: %hu to %u\n", tpg->tpgt, tpg->nsessions);
4431
4432 ida_free(&sess_ida, sess->session_index);
4433 kfree(sess->sess_ops);
4434 sess->sess_ops = NULL;
4435 spin_unlock_bh(&se_tpg->session_lock);
4436
4437 kfree(sess);
4438 return 0;
4439 }
4440
iscsit_logout_post_handler_closesession(struct iscsi_conn * conn)4441 static void iscsit_logout_post_handler_closesession(
4442 struct iscsi_conn *conn)
4443 {
4444 struct iscsi_session *sess = conn->sess;
4445 int sleep = 1;
4446 /*
4447 * Traditional iscsi/tcp will invoke this logic from TX thread
4448 * context during session logout, so clear tx_thread_active and
4449 * sleep if iscsit_close_connection() has not already occured.
4450 *
4451 * Since iser-target invokes this logic from it's own workqueue,
4452 * always sleep waiting for RX/TX thread shutdown to complete
4453 * within iscsit_close_connection().
4454 */
4455 if (!conn->conn_transport->rdma_shutdown) {
4456 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4457 if (!sleep)
4458 return;
4459 }
4460
4461 atomic_set(&conn->conn_logout_remove, 0);
4462 complete(&conn->conn_logout_comp);
4463
4464 iscsit_dec_conn_usage_count(conn);
4465 atomic_set(&sess->session_close, 1);
4466 iscsit_stop_session(sess, sleep, sleep);
4467 iscsit_dec_session_usage_count(sess);
4468 }
4469
iscsit_logout_post_handler_samecid(struct iscsi_conn * conn)4470 static void iscsit_logout_post_handler_samecid(
4471 struct iscsi_conn *conn)
4472 {
4473 int sleep = 1;
4474
4475 if (!conn->conn_transport->rdma_shutdown) {
4476 sleep = cmpxchg(&conn->tx_thread_active, true, false);
4477 if (!sleep)
4478 return;
4479 }
4480
4481 atomic_set(&conn->conn_logout_remove, 0);
4482 complete(&conn->conn_logout_comp);
4483
4484 iscsit_cause_connection_reinstatement(conn, sleep);
4485 iscsit_dec_conn_usage_count(conn);
4486 }
4487
iscsit_logout_post_handler_diffcid(struct iscsi_conn * conn,u16 cid)4488 static void iscsit_logout_post_handler_diffcid(
4489 struct iscsi_conn *conn,
4490 u16 cid)
4491 {
4492 struct iscsi_conn *l_conn;
4493 struct iscsi_session *sess = conn->sess;
4494 bool conn_found = false;
4495
4496 if (!sess)
4497 return;
4498
4499 spin_lock_bh(&sess->conn_lock);
4500 list_for_each_entry(l_conn, &sess->sess_conn_list, conn_list) {
4501 if (l_conn->cid == cid) {
4502 iscsit_inc_conn_usage_count(l_conn);
4503 conn_found = true;
4504 break;
4505 }
4506 }
4507 spin_unlock_bh(&sess->conn_lock);
4508
4509 if (!conn_found)
4510 return;
4511
4512 if (l_conn->sock)
4513 l_conn->sock->ops->shutdown(l_conn->sock, RCV_SHUTDOWN);
4514
4515 spin_lock_bh(&l_conn->state_lock);
4516 pr_debug("Moving to TARG_CONN_STATE_IN_LOGOUT.\n");
4517 l_conn->conn_state = TARG_CONN_STATE_IN_LOGOUT;
4518 spin_unlock_bh(&l_conn->state_lock);
4519
4520 iscsit_cause_connection_reinstatement(l_conn, 1);
4521 iscsit_dec_conn_usage_count(l_conn);
4522 }
4523
4524 /*
4525 * Return of 0 causes the TX thread to restart.
4526 */
iscsit_logout_post_handler(struct iscsi_cmd * cmd,struct iscsi_conn * conn)4527 int iscsit_logout_post_handler(
4528 struct iscsi_cmd *cmd,
4529 struct iscsi_conn *conn)
4530 {
4531 int ret = 0;
4532
4533 switch (cmd->logout_reason) {
4534 case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
4535 switch (cmd->logout_response) {
4536 case ISCSI_LOGOUT_SUCCESS:
4537 case ISCSI_LOGOUT_CLEANUP_FAILED:
4538 default:
4539 iscsit_logout_post_handler_closesession(conn);
4540 break;
4541 }
4542 break;
4543 case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
4544 if (conn->cid == cmd->logout_cid) {
4545 switch (cmd->logout_response) {
4546 case ISCSI_LOGOUT_SUCCESS:
4547 case ISCSI_LOGOUT_CLEANUP_FAILED:
4548 default:
4549 iscsit_logout_post_handler_samecid(conn);
4550 break;
4551 }
4552 } else {
4553 switch (cmd->logout_response) {
4554 case ISCSI_LOGOUT_SUCCESS:
4555 iscsit_logout_post_handler_diffcid(conn,
4556 cmd->logout_cid);
4557 break;
4558 case ISCSI_LOGOUT_CID_NOT_FOUND:
4559 case ISCSI_LOGOUT_CLEANUP_FAILED:
4560 default:
4561 break;
4562 }
4563 ret = 1;
4564 }
4565 break;
4566 case ISCSI_LOGOUT_REASON_RECOVERY:
4567 switch (cmd->logout_response) {
4568 case ISCSI_LOGOUT_SUCCESS:
4569 case ISCSI_LOGOUT_CID_NOT_FOUND:
4570 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
4571 case ISCSI_LOGOUT_CLEANUP_FAILED:
4572 default:
4573 break;
4574 }
4575 ret = 1;
4576 break;
4577 default:
4578 break;
4579
4580 }
4581 return ret;
4582 }
4583 EXPORT_SYMBOL(iscsit_logout_post_handler);
4584
iscsit_fail_session(struct iscsi_session * sess)4585 void iscsit_fail_session(struct iscsi_session *sess)
4586 {
4587 struct iscsi_conn *conn;
4588
4589 spin_lock_bh(&sess->conn_lock);
4590 list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
4591 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
4592 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
4593 }
4594 spin_unlock_bh(&sess->conn_lock);
4595
4596 pr_debug("Moving to TARG_SESS_STATE_FAILED.\n");
4597 sess->session_state = TARG_SESS_STATE_FAILED;
4598 }
4599
iscsit_stop_session(struct iscsi_session * sess,int session_sleep,int connection_sleep)4600 void iscsit_stop_session(
4601 struct iscsi_session *sess,
4602 int session_sleep,
4603 int connection_sleep)
4604 {
4605 u16 conn_count = atomic_read(&sess->nconn);
4606 struct iscsi_conn *conn, *conn_tmp = NULL;
4607 int is_last;
4608
4609 spin_lock_bh(&sess->conn_lock);
4610
4611 if (connection_sleep) {
4612 list_for_each_entry_safe(conn, conn_tmp, &sess->sess_conn_list,
4613 conn_list) {
4614 if (conn_count == 0)
4615 break;
4616
4617 if (list_is_last(&conn->conn_list, &sess->sess_conn_list)) {
4618 is_last = 1;
4619 } else {
4620 iscsit_inc_conn_usage_count(conn_tmp);
4621 is_last = 0;
4622 }
4623 iscsit_inc_conn_usage_count(conn);
4624
4625 spin_unlock_bh(&sess->conn_lock);
4626 iscsit_cause_connection_reinstatement(conn, 1);
4627 spin_lock_bh(&sess->conn_lock);
4628
4629 iscsit_dec_conn_usage_count(conn);
4630 if (is_last == 0)
4631 iscsit_dec_conn_usage_count(conn_tmp);
4632 conn_count--;
4633 }
4634 } else {
4635 list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
4636 iscsit_cause_connection_reinstatement(conn, 0);
4637 }
4638
4639 if (session_sleep && atomic_read(&sess->nconn)) {
4640 spin_unlock_bh(&sess->conn_lock);
4641 wait_for_completion(&sess->session_wait_comp);
4642 } else
4643 spin_unlock_bh(&sess->conn_lock);
4644 }
4645
iscsit_release_sessions_for_tpg(struct iscsi_portal_group * tpg,int force)4646 int iscsit_release_sessions_for_tpg(struct iscsi_portal_group *tpg, int force)
4647 {
4648 struct iscsi_session *sess;
4649 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
4650 struct se_session *se_sess, *se_sess_tmp;
4651 LIST_HEAD(free_list);
4652 int session_count = 0;
4653
4654 spin_lock_bh(&se_tpg->session_lock);
4655 if (tpg->nsessions && !force) {
4656 spin_unlock_bh(&se_tpg->session_lock);
4657 return -1;
4658 }
4659
4660 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
4661 sess_list) {
4662 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4663
4664 spin_lock(&sess->conn_lock);
4665 if (atomic_read(&sess->session_fall_back_to_erl0) ||
4666 atomic_read(&sess->session_logout) ||
4667 atomic_read(&sess->session_close) ||
4668 (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
4669 spin_unlock(&sess->conn_lock);
4670 continue;
4671 }
4672 iscsit_inc_session_usage_count(sess);
4673 atomic_set(&sess->session_reinstatement, 1);
4674 atomic_set(&sess->session_fall_back_to_erl0, 1);
4675 atomic_set(&sess->session_close, 1);
4676 spin_unlock(&sess->conn_lock);
4677
4678 list_move_tail(&se_sess->sess_list, &free_list);
4679 }
4680 spin_unlock_bh(&se_tpg->session_lock);
4681
4682 list_for_each_entry_safe(se_sess, se_sess_tmp, &free_list, sess_list) {
4683 sess = (struct iscsi_session *)se_sess->fabric_sess_ptr;
4684
4685 list_del_init(&se_sess->sess_list);
4686 iscsit_stop_session(sess, 1, 1);
4687 iscsit_dec_session_usage_count(sess);
4688 session_count++;
4689 }
4690
4691 pr_debug("Released %d iSCSI Session(s) from Target Portal"
4692 " Group: %hu\n", session_count, tpg->tpgt);
4693 return 0;
4694 }
4695
4696 MODULE_DESCRIPTION("iSCSI-Target Driver for mainline target infrastructure");
4697 MODULE_VERSION("4.1.x");
4698 MODULE_AUTHOR("nab@Linux-iSCSI.org");
4699 MODULE_LICENSE("GPL");
4700
4701 module_init(iscsi_target_init_module);
4702 module_exit(iscsi_target_cleanup_module);
4703