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