target: simplify alua support
[deliverable/linux.git] / drivers / target / target_core_transport.c
1 /*******************************************************************************
2 * Filename: target_core_transport.c
3 *
4 * This file contains the Generic Target Engine Core.
5 *
6 * Copyright (c) 2002, 2003, 2004, 2005 PyX Technologies, Inc.
7 * Copyright (c) 2005, 2006, 2007 SBE, Inc.
8 * Copyright (c) 2007-2010 Rising Tide Systems
9 * Copyright (c) 2008-2010 Linux-iSCSI.org
10 *
11 * Nicholas A. Bellinger <nab@kernel.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 *
27 ******************************************************************************/
28
29 #include <linux/net.h>
30 #include <linux/delay.h>
31 #include <linux/string.h>
32 #include <linux/timer.h>
33 #include <linux/slab.h>
34 #include <linux/blkdev.h>
35 #include <linux/spinlock.h>
36 #include <linux/kthread.h>
37 #include <linux/in.h>
38 #include <linux/cdrom.h>
39 #include <linux/module.h>
40 #include <linux/ratelimit.h>
41 #include <asm/unaligned.h>
42 #include <net/sock.h>
43 #include <net/tcp.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_tcq.h>
47
48 #include <target/target_core_base.h>
49 #include <target/target_core_backend.h>
50 #include <target/target_core_fabric.h>
51 #include <target/target_core_configfs.h>
52
53 #include "target_core_internal.h"
54 #include "target_core_alua.h"
55 #include "target_core_pr.h"
56 #include "target_core_ua.h"
57
58 static struct workqueue_struct *target_completion_wq;
59 static struct kmem_cache *se_sess_cache;
60 struct kmem_cache *se_ua_cache;
61 struct kmem_cache *t10_pr_reg_cache;
62 struct kmem_cache *t10_alua_lu_gp_cache;
63 struct kmem_cache *t10_alua_lu_gp_mem_cache;
64 struct kmem_cache *t10_alua_tg_pt_gp_cache;
65 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
66
67 static void transport_complete_task_attr(struct se_cmd *cmd);
68 static void transport_handle_queue_full(struct se_cmd *cmd,
69 struct se_device *dev);
70 static int transport_generic_get_mem(struct se_cmd *cmd);
71 static int target_get_sess_cmd(struct se_session *, struct se_cmd *, bool);
72 static void transport_put_cmd(struct se_cmd *cmd);
73 static void target_complete_ok_work(struct work_struct *work);
74
75 int init_se_kmem_caches(void)
76 {
77 se_sess_cache = kmem_cache_create("se_sess_cache",
78 sizeof(struct se_session), __alignof__(struct se_session),
79 0, NULL);
80 if (!se_sess_cache) {
81 pr_err("kmem_cache_create() for struct se_session"
82 " failed\n");
83 goto out;
84 }
85 se_ua_cache = kmem_cache_create("se_ua_cache",
86 sizeof(struct se_ua), __alignof__(struct se_ua),
87 0, NULL);
88 if (!se_ua_cache) {
89 pr_err("kmem_cache_create() for struct se_ua failed\n");
90 goto out_free_sess_cache;
91 }
92 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
93 sizeof(struct t10_pr_registration),
94 __alignof__(struct t10_pr_registration), 0, NULL);
95 if (!t10_pr_reg_cache) {
96 pr_err("kmem_cache_create() for struct t10_pr_registration"
97 " failed\n");
98 goto out_free_ua_cache;
99 }
100 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
101 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
102 0, NULL);
103 if (!t10_alua_lu_gp_cache) {
104 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
105 " failed\n");
106 goto out_free_pr_reg_cache;
107 }
108 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
109 sizeof(struct t10_alua_lu_gp_member),
110 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
111 if (!t10_alua_lu_gp_mem_cache) {
112 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
113 "cache failed\n");
114 goto out_free_lu_gp_cache;
115 }
116 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
117 sizeof(struct t10_alua_tg_pt_gp),
118 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
119 if (!t10_alua_tg_pt_gp_cache) {
120 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
121 "cache failed\n");
122 goto out_free_lu_gp_mem_cache;
123 }
124 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
125 "t10_alua_tg_pt_gp_mem_cache",
126 sizeof(struct t10_alua_tg_pt_gp_member),
127 __alignof__(struct t10_alua_tg_pt_gp_member),
128 0, NULL);
129 if (!t10_alua_tg_pt_gp_mem_cache) {
130 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
131 "mem_t failed\n");
132 goto out_free_tg_pt_gp_cache;
133 }
134
135 target_completion_wq = alloc_workqueue("target_completion",
136 WQ_MEM_RECLAIM, 0);
137 if (!target_completion_wq)
138 goto out_free_tg_pt_gp_mem_cache;
139
140 return 0;
141
142 out_free_tg_pt_gp_mem_cache:
143 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
144 out_free_tg_pt_gp_cache:
145 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
146 out_free_lu_gp_mem_cache:
147 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
148 out_free_lu_gp_cache:
149 kmem_cache_destroy(t10_alua_lu_gp_cache);
150 out_free_pr_reg_cache:
151 kmem_cache_destroy(t10_pr_reg_cache);
152 out_free_ua_cache:
153 kmem_cache_destroy(se_ua_cache);
154 out_free_sess_cache:
155 kmem_cache_destroy(se_sess_cache);
156 out:
157 return -ENOMEM;
158 }
159
160 void release_se_kmem_caches(void)
161 {
162 destroy_workqueue(target_completion_wq);
163 kmem_cache_destroy(se_sess_cache);
164 kmem_cache_destroy(se_ua_cache);
165 kmem_cache_destroy(t10_pr_reg_cache);
166 kmem_cache_destroy(t10_alua_lu_gp_cache);
167 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
168 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
169 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
170 }
171
172 /* This code ensures unique mib indexes are handed out. */
173 static DEFINE_SPINLOCK(scsi_mib_index_lock);
174 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
175
176 /*
177 * Allocate a new row index for the entry type specified
178 */
179 u32 scsi_get_new_index(scsi_index_t type)
180 {
181 u32 new_index;
182
183 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
184
185 spin_lock(&scsi_mib_index_lock);
186 new_index = ++scsi_mib_index[type];
187 spin_unlock(&scsi_mib_index_lock);
188
189 return new_index;
190 }
191
192 void transport_subsystem_check_init(void)
193 {
194 int ret;
195 static int sub_api_initialized;
196
197 if (sub_api_initialized)
198 return;
199
200 ret = request_module("target_core_iblock");
201 if (ret != 0)
202 pr_err("Unable to load target_core_iblock\n");
203
204 ret = request_module("target_core_file");
205 if (ret != 0)
206 pr_err("Unable to load target_core_file\n");
207
208 ret = request_module("target_core_pscsi");
209 if (ret != 0)
210 pr_err("Unable to load target_core_pscsi\n");
211
212 sub_api_initialized = 1;
213 }
214
215 struct se_session *transport_init_session(void)
216 {
217 struct se_session *se_sess;
218
219 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
220 if (!se_sess) {
221 pr_err("Unable to allocate struct se_session from"
222 " se_sess_cache\n");
223 return ERR_PTR(-ENOMEM);
224 }
225 INIT_LIST_HEAD(&se_sess->sess_list);
226 INIT_LIST_HEAD(&se_sess->sess_acl_list);
227 INIT_LIST_HEAD(&se_sess->sess_cmd_list);
228 spin_lock_init(&se_sess->sess_cmd_lock);
229 kref_init(&se_sess->sess_kref);
230
231 return se_sess;
232 }
233 EXPORT_SYMBOL(transport_init_session);
234
235 /*
236 * Called with spin_lock_irqsave(&struct se_portal_group->session_lock called.
237 */
238 void __transport_register_session(
239 struct se_portal_group *se_tpg,
240 struct se_node_acl *se_nacl,
241 struct se_session *se_sess,
242 void *fabric_sess_ptr)
243 {
244 unsigned char buf[PR_REG_ISID_LEN];
245
246 se_sess->se_tpg = se_tpg;
247 se_sess->fabric_sess_ptr = fabric_sess_ptr;
248 /*
249 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
250 *
251 * Only set for struct se_session's that will actually be moving I/O.
252 * eg: *NOT* discovery sessions.
253 */
254 if (se_nacl) {
255 /*
256 * If the fabric module supports an ISID based TransportID,
257 * save this value in binary from the fabric I_T Nexus now.
258 */
259 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
260 memset(&buf[0], 0, PR_REG_ISID_LEN);
261 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
262 &buf[0], PR_REG_ISID_LEN);
263 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
264 }
265 kref_get(&se_nacl->acl_kref);
266
267 spin_lock_irq(&se_nacl->nacl_sess_lock);
268 /*
269 * The se_nacl->nacl_sess pointer will be set to the
270 * last active I_T Nexus for each struct se_node_acl.
271 */
272 se_nacl->nacl_sess = se_sess;
273
274 list_add_tail(&se_sess->sess_acl_list,
275 &se_nacl->acl_sess_list);
276 spin_unlock_irq(&se_nacl->nacl_sess_lock);
277 }
278 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
279
280 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
281 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
282 }
283 EXPORT_SYMBOL(__transport_register_session);
284
285 void transport_register_session(
286 struct se_portal_group *se_tpg,
287 struct se_node_acl *se_nacl,
288 struct se_session *se_sess,
289 void *fabric_sess_ptr)
290 {
291 unsigned long flags;
292
293 spin_lock_irqsave(&se_tpg->session_lock, flags);
294 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
295 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
296 }
297 EXPORT_SYMBOL(transport_register_session);
298
299 static void target_release_session(struct kref *kref)
300 {
301 struct se_session *se_sess = container_of(kref,
302 struct se_session, sess_kref);
303 struct se_portal_group *se_tpg = se_sess->se_tpg;
304
305 se_tpg->se_tpg_tfo->close_session(se_sess);
306 }
307
308 void target_get_session(struct se_session *se_sess)
309 {
310 kref_get(&se_sess->sess_kref);
311 }
312 EXPORT_SYMBOL(target_get_session);
313
314 void target_put_session(struct se_session *se_sess)
315 {
316 struct se_portal_group *tpg = se_sess->se_tpg;
317
318 if (tpg->se_tpg_tfo->put_session != NULL) {
319 tpg->se_tpg_tfo->put_session(se_sess);
320 return;
321 }
322 kref_put(&se_sess->sess_kref, target_release_session);
323 }
324 EXPORT_SYMBOL(target_put_session);
325
326 static void target_complete_nacl(struct kref *kref)
327 {
328 struct se_node_acl *nacl = container_of(kref,
329 struct se_node_acl, acl_kref);
330
331 complete(&nacl->acl_free_comp);
332 }
333
334 void target_put_nacl(struct se_node_acl *nacl)
335 {
336 kref_put(&nacl->acl_kref, target_complete_nacl);
337 }
338
339 void transport_deregister_session_configfs(struct se_session *se_sess)
340 {
341 struct se_node_acl *se_nacl;
342 unsigned long flags;
343 /*
344 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
345 */
346 se_nacl = se_sess->se_node_acl;
347 if (se_nacl) {
348 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
349 if (se_nacl->acl_stop == 0)
350 list_del(&se_sess->sess_acl_list);
351 /*
352 * If the session list is empty, then clear the pointer.
353 * Otherwise, set the struct se_session pointer from the tail
354 * element of the per struct se_node_acl active session list.
355 */
356 if (list_empty(&se_nacl->acl_sess_list))
357 se_nacl->nacl_sess = NULL;
358 else {
359 se_nacl->nacl_sess = container_of(
360 se_nacl->acl_sess_list.prev,
361 struct se_session, sess_acl_list);
362 }
363 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
364 }
365 }
366 EXPORT_SYMBOL(transport_deregister_session_configfs);
367
368 void transport_free_session(struct se_session *se_sess)
369 {
370 kmem_cache_free(se_sess_cache, se_sess);
371 }
372 EXPORT_SYMBOL(transport_free_session);
373
374 void transport_deregister_session(struct se_session *se_sess)
375 {
376 struct se_portal_group *se_tpg = se_sess->se_tpg;
377 struct target_core_fabric_ops *se_tfo;
378 struct se_node_acl *se_nacl;
379 unsigned long flags;
380 bool comp_nacl = true;
381
382 if (!se_tpg) {
383 transport_free_session(se_sess);
384 return;
385 }
386 se_tfo = se_tpg->se_tpg_tfo;
387
388 spin_lock_irqsave(&se_tpg->session_lock, flags);
389 list_del(&se_sess->sess_list);
390 se_sess->se_tpg = NULL;
391 se_sess->fabric_sess_ptr = NULL;
392 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
393
394 /*
395 * Determine if we need to do extra work for this initiator node's
396 * struct se_node_acl if it had been previously dynamically generated.
397 */
398 se_nacl = se_sess->se_node_acl;
399
400 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
401 if (se_nacl && se_nacl->dynamic_node_acl) {
402 if (!se_tfo->tpg_check_demo_mode_cache(se_tpg)) {
403 list_del(&se_nacl->acl_list);
404 se_tpg->num_node_acls--;
405 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
406 core_tpg_wait_for_nacl_pr_ref(se_nacl);
407 core_free_device_list_for_node(se_nacl, se_tpg);
408 se_tfo->tpg_release_fabric_acl(se_tpg, se_nacl);
409
410 comp_nacl = false;
411 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
412 }
413 }
414 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
415
416 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
417 se_tpg->se_tpg_tfo->get_fabric_name());
418 /*
419 * If last kref is dropping now for an explict NodeACL, awake sleeping
420 * ->acl_free_comp caller to wakeup configfs se_node_acl->acl_group
421 * removal context.
422 */
423 if (se_nacl && comp_nacl == true)
424 target_put_nacl(se_nacl);
425
426 transport_free_session(se_sess);
427 }
428 EXPORT_SYMBOL(transport_deregister_session);
429
430 /*
431 * Called with cmd->t_state_lock held.
432 */
433 static void target_remove_from_state_list(struct se_cmd *cmd)
434 {
435 struct se_device *dev = cmd->se_dev;
436 unsigned long flags;
437
438 if (!dev)
439 return;
440
441 if (cmd->transport_state & CMD_T_BUSY)
442 return;
443
444 spin_lock_irqsave(&dev->execute_task_lock, flags);
445 if (cmd->state_active) {
446 list_del(&cmd->state_list);
447 cmd->state_active = false;
448 }
449 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
450 }
451
452 static int transport_cmd_check_stop(struct se_cmd *cmd, bool remove_from_lists)
453 {
454 unsigned long flags;
455
456 spin_lock_irqsave(&cmd->t_state_lock, flags);
457 /*
458 * Determine if IOCTL context caller in requesting the stopping of this
459 * command for LUN shutdown purposes.
460 */
461 if (cmd->transport_state & CMD_T_LUN_STOP) {
462 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
463 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
464
465 cmd->transport_state &= ~CMD_T_ACTIVE;
466 if (remove_from_lists)
467 target_remove_from_state_list(cmd);
468 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
469
470 complete(&cmd->transport_lun_stop_comp);
471 return 1;
472 }
473
474 if (remove_from_lists) {
475 target_remove_from_state_list(cmd);
476
477 /*
478 * Clear struct se_cmd->se_lun before the handoff to FE.
479 */
480 cmd->se_lun = NULL;
481 }
482
483 /*
484 * Determine if frontend context caller is requesting the stopping of
485 * this command for frontend exceptions.
486 */
487 if (cmd->transport_state & CMD_T_STOP) {
488 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
489 __func__, __LINE__,
490 cmd->se_tfo->get_task_tag(cmd));
491
492 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
493
494 complete(&cmd->t_transport_stop_comp);
495 return 1;
496 }
497
498 cmd->transport_state &= ~CMD_T_ACTIVE;
499 if (remove_from_lists) {
500 /*
501 * Some fabric modules like tcm_loop can release
502 * their internally allocated I/O reference now and
503 * struct se_cmd now.
504 *
505 * Fabric modules are expected to return '1' here if the
506 * se_cmd being passed is released at this point,
507 * or zero if not being released.
508 */
509 if (cmd->se_tfo->check_stop_free != NULL) {
510 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
511 return cmd->se_tfo->check_stop_free(cmd);
512 }
513 }
514
515 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
516 return 0;
517 }
518
519 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
520 {
521 return transport_cmd_check_stop(cmd, true);
522 }
523
524 static void transport_lun_remove_cmd(struct se_cmd *cmd)
525 {
526 struct se_lun *lun = cmd->se_lun;
527 unsigned long flags;
528
529 if (!lun)
530 return;
531
532 spin_lock_irqsave(&cmd->t_state_lock, flags);
533 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
534 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
535 target_remove_from_state_list(cmd);
536 }
537 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
538
539 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
540 if (!list_empty(&cmd->se_lun_node))
541 list_del_init(&cmd->se_lun_node);
542 spin_unlock_irqrestore(&lun->lun_cmd_lock, flags);
543 }
544
545 void transport_cmd_finish_abort(struct se_cmd *cmd, int remove)
546 {
547 if (!(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
548 transport_lun_remove_cmd(cmd);
549
550 if (transport_cmd_check_stop_to_fabric(cmd))
551 return;
552 if (remove)
553 transport_put_cmd(cmd);
554 }
555
556 static void target_complete_failure_work(struct work_struct *work)
557 {
558 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
559
560 transport_generic_request_failure(cmd);
561 }
562
563 /*
564 * Used when asking transport to copy Sense Data from the underlying
565 * Linux/SCSI struct scsi_cmnd
566 */
567 static unsigned char *transport_get_sense_buffer(struct se_cmd *cmd)
568 {
569 struct se_device *dev = cmd->se_dev;
570
571 WARN_ON(!cmd->se_lun);
572
573 if (!dev)
574 return NULL;
575
576 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION)
577 return NULL;
578
579 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
580
581 pr_debug("HBA_[%u]_PLUG[%s]: Requesting sense for SAM STATUS: 0x%02x\n",
582 dev->se_hba->hba_id, dev->transport->name, cmd->scsi_status);
583 return cmd->sense_buffer;
584 }
585
586 void target_complete_cmd(struct se_cmd *cmd, u8 scsi_status)
587 {
588 struct se_device *dev = cmd->se_dev;
589 int success = scsi_status == GOOD;
590 unsigned long flags;
591
592 cmd->scsi_status = scsi_status;
593
594
595 spin_lock_irqsave(&cmd->t_state_lock, flags);
596 cmd->transport_state &= ~CMD_T_BUSY;
597
598 if (dev && dev->transport->transport_complete) {
599 dev->transport->transport_complete(cmd,
600 cmd->t_data_sg,
601 transport_get_sense_buffer(cmd));
602 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE)
603 success = 1;
604 }
605
606 /*
607 * See if we are waiting to complete for an exception condition.
608 */
609 if (cmd->transport_state & CMD_T_REQUEST_STOP) {
610 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
611 complete(&cmd->task_stop_comp);
612 return;
613 }
614
615 if (!success)
616 cmd->transport_state |= CMD_T_FAILED;
617
618 /*
619 * Check for case where an explict ABORT_TASK has been received
620 * and transport_wait_for_tasks() will be waiting for completion..
621 */
622 if (cmd->transport_state & CMD_T_ABORTED &&
623 cmd->transport_state & CMD_T_STOP) {
624 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
625 complete(&cmd->t_transport_stop_comp);
626 return;
627 } else if (cmd->transport_state & CMD_T_FAILED) {
628 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
629 INIT_WORK(&cmd->work, target_complete_failure_work);
630 } else {
631 INIT_WORK(&cmd->work, target_complete_ok_work);
632 }
633
634 cmd->t_state = TRANSPORT_COMPLETE;
635 cmd->transport_state |= (CMD_T_COMPLETE | CMD_T_ACTIVE);
636 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
637
638 queue_work(target_completion_wq, &cmd->work);
639 }
640 EXPORT_SYMBOL(target_complete_cmd);
641
642 static void target_add_to_state_list(struct se_cmd *cmd)
643 {
644 struct se_device *dev = cmd->se_dev;
645 unsigned long flags;
646
647 spin_lock_irqsave(&dev->execute_task_lock, flags);
648 if (!cmd->state_active) {
649 list_add_tail(&cmd->state_list, &dev->state_list);
650 cmd->state_active = true;
651 }
652 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
653 }
654
655 /*
656 * Handle QUEUE_FULL / -EAGAIN and -ENOMEM status
657 */
658 static void transport_write_pending_qf(struct se_cmd *cmd);
659 static void transport_complete_qf(struct se_cmd *cmd);
660
661 void target_qf_do_work(struct work_struct *work)
662 {
663 struct se_device *dev = container_of(work, struct se_device,
664 qf_work_queue);
665 LIST_HEAD(qf_cmd_list);
666 struct se_cmd *cmd, *cmd_tmp;
667
668 spin_lock_irq(&dev->qf_cmd_lock);
669 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
670 spin_unlock_irq(&dev->qf_cmd_lock);
671
672 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
673 list_del(&cmd->se_qf_node);
674 atomic_dec(&dev->dev_qf_count);
675 smp_mb__after_atomic_dec();
676
677 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
678 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
679 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
680 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
681 : "UNKNOWN");
682
683 if (cmd->t_state == TRANSPORT_COMPLETE_QF_WP)
684 transport_write_pending_qf(cmd);
685 else if (cmd->t_state == TRANSPORT_COMPLETE_QF_OK)
686 transport_complete_qf(cmd);
687 }
688 }
689
690 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
691 {
692 switch (cmd->data_direction) {
693 case DMA_NONE:
694 return "NONE";
695 case DMA_FROM_DEVICE:
696 return "READ";
697 case DMA_TO_DEVICE:
698 return "WRITE";
699 case DMA_BIDIRECTIONAL:
700 return "BIDI";
701 default:
702 break;
703 }
704
705 return "UNKNOWN";
706 }
707
708 void transport_dump_dev_state(
709 struct se_device *dev,
710 char *b,
711 int *bl)
712 {
713 *bl += sprintf(b + *bl, "Status: ");
714 if (dev->export_count)
715 *bl += sprintf(b + *bl, "ACTIVATED");
716 else
717 *bl += sprintf(b + *bl, "DEACTIVATED");
718
719 *bl += sprintf(b + *bl, " Max Queue Depth: %d", dev->queue_depth);
720 *bl += sprintf(b + *bl, " SectorSize: %u HwMaxSectors: %u\n",
721 dev->dev_attrib.block_size,
722 dev->dev_attrib.hw_max_sectors);
723 *bl += sprintf(b + *bl, " ");
724 }
725
726 void transport_dump_vpd_proto_id(
727 struct t10_vpd *vpd,
728 unsigned char *p_buf,
729 int p_buf_len)
730 {
731 unsigned char buf[VPD_TMP_BUF_SIZE];
732 int len;
733
734 memset(buf, 0, VPD_TMP_BUF_SIZE);
735 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
736
737 switch (vpd->protocol_identifier) {
738 case 0x00:
739 sprintf(buf+len, "Fibre Channel\n");
740 break;
741 case 0x10:
742 sprintf(buf+len, "Parallel SCSI\n");
743 break;
744 case 0x20:
745 sprintf(buf+len, "SSA\n");
746 break;
747 case 0x30:
748 sprintf(buf+len, "IEEE 1394\n");
749 break;
750 case 0x40:
751 sprintf(buf+len, "SCSI Remote Direct Memory Access"
752 " Protocol\n");
753 break;
754 case 0x50:
755 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
756 break;
757 case 0x60:
758 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
759 break;
760 case 0x70:
761 sprintf(buf+len, "Automation/Drive Interface Transport"
762 " Protocol\n");
763 break;
764 case 0x80:
765 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
766 break;
767 default:
768 sprintf(buf+len, "Unknown 0x%02x\n",
769 vpd->protocol_identifier);
770 break;
771 }
772
773 if (p_buf)
774 strncpy(p_buf, buf, p_buf_len);
775 else
776 pr_debug("%s", buf);
777 }
778
779 void
780 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
781 {
782 /*
783 * Check if the Protocol Identifier Valid (PIV) bit is set..
784 *
785 * from spc3r23.pdf section 7.5.1
786 */
787 if (page_83[1] & 0x80) {
788 vpd->protocol_identifier = (page_83[0] & 0xf0);
789 vpd->protocol_identifier_set = 1;
790 transport_dump_vpd_proto_id(vpd, NULL, 0);
791 }
792 }
793 EXPORT_SYMBOL(transport_set_vpd_proto_id);
794
795 int transport_dump_vpd_assoc(
796 struct t10_vpd *vpd,
797 unsigned char *p_buf,
798 int p_buf_len)
799 {
800 unsigned char buf[VPD_TMP_BUF_SIZE];
801 int ret = 0;
802 int len;
803
804 memset(buf, 0, VPD_TMP_BUF_SIZE);
805 len = sprintf(buf, "T10 VPD Identifier Association: ");
806
807 switch (vpd->association) {
808 case 0x00:
809 sprintf(buf+len, "addressed logical unit\n");
810 break;
811 case 0x10:
812 sprintf(buf+len, "target port\n");
813 break;
814 case 0x20:
815 sprintf(buf+len, "SCSI target device\n");
816 break;
817 default:
818 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
819 ret = -EINVAL;
820 break;
821 }
822
823 if (p_buf)
824 strncpy(p_buf, buf, p_buf_len);
825 else
826 pr_debug("%s", buf);
827
828 return ret;
829 }
830
831 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
832 {
833 /*
834 * The VPD identification association..
835 *
836 * from spc3r23.pdf Section 7.6.3.1 Table 297
837 */
838 vpd->association = (page_83[1] & 0x30);
839 return transport_dump_vpd_assoc(vpd, NULL, 0);
840 }
841 EXPORT_SYMBOL(transport_set_vpd_assoc);
842
843 int transport_dump_vpd_ident_type(
844 struct t10_vpd *vpd,
845 unsigned char *p_buf,
846 int p_buf_len)
847 {
848 unsigned char buf[VPD_TMP_BUF_SIZE];
849 int ret = 0;
850 int len;
851
852 memset(buf, 0, VPD_TMP_BUF_SIZE);
853 len = sprintf(buf, "T10 VPD Identifier Type: ");
854
855 switch (vpd->device_identifier_type) {
856 case 0x00:
857 sprintf(buf+len, "Vendor specific\n");
858 break;
859 case 0x01:
860 sprintf(buf+len, "T10 Vendor ID based\n");
861 break;
862 case 0x02:
863 sprintf(buf+len, "EUI-64 based\n");
864 break;
865 case 0x03:
866 sprintf(buf+len, "NAA\n");
867 break;
868 case 0x04:
869 sprintf(buf+len, "Relative target port identifier\n");
870 break;
871 case 0x08:
872 sprintf(buf+len, "SCSI name string\n");
873 break;
874 default:
875 sprintf(buf+len, "Unsupported: 0x%02x\n",
876 vpd->device_identifier_type);
877 ret = -EINVAL;
878 break;
879 }
880
881 if (p_buf) {
882 if (p_buf_len < strlen(buf)+1)
883 return -EINVAL;
884 strncpy(p_buf, buf, p_buf_len);
885 } else {
886 pr_debug("%s", buf);
887 }
888
889 return ret;
890 }
891
892 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
893 {
894 /*
895 * The VPD identifier type..
896 *
897 * from spc3r23.pdf Section 7.6.3.1 Table 298
898 */
899 vpd->device_identifier_type = (page_83[1] & 0x0f);
900 return transport_dump_vpd_ident_type(vpd, NULL, 0);
901 }
902 EXPORT_SYMBOL(transport_set_vpd_ident_type);
903
904 int transport_dump_vpd_ident(
905 struct t10_vpd *vpd,
906 unsigned char *p_buf,
907 int p_buf_len)
908 {
909 unsigned char buf[VPD_TMP_BUF_SIZE];
910 int ret = 0;
911
912 memset(buf, 0, VPD_TMP_BUF_SIZE);
913
914 switch (vpd->device_identifier_code_set) {
915 case 0x01: /* Binary */
916 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
917 &vpd->device_identifier[0]);
918 break;
919 case 0x02: /* ASCII */
920 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
921 &vpd->device_identifier[0]);
922 break;
923 case 0x03: /* UTF-8 */
924 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
925 &vpd->device_identifier[0]);
926 break;
927 default:
928 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
929 " 0x%02x", vpd->device_identifier_code_set);
930 ret = -EINVAL;
931 break;
932 }
933
934 if (p_buf)
935 strncpy(p_buf, buf, p_buf_len);
936 else
937 pr_debug("%s", buf);
938
939 return ret;
940 }
941
942 int
943 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
944 {
945 static const char hex_str[] = "0123456789abcdef";
946 int j = 0, i = 4; /* offset to start of the identifier */
947
948 /*
949 * The VPD Code Set (encoding)
950 *
951 * from spc3r23.pdf Section 7.6.3.1 Table 296
952 */
953 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
954 switch (vpd->device_identifier_code_set) {
955 case 0x01: /* Binary */
956 vpd->device_identifier[j++] =
957 hex_str[vpd->device_identifier_type];
958 while (i < (4 + page_83[3])) {
959 vpd->device_identifier[j++] =
960 hex_str[(page_83[i] & 0xf0) >> 4];
961 vpd->device_identifier[j++] =
962 hex_str[page_83[i] & 0x0f];
963 i++;
964 }
965 break;
966 case 0x02: /* ASCII */
967 case 0x03: /* UTF-8 */
968 while (i < (4 + page_83[3]))
969 vpd->device_identifier[j++] = page_83[i++];
970 break;
971 default:
972 break;
973 }
974
975 return transport_dump_vpd_ident(vpd, NULL, 0);
976 }
977 EXPORT_SYMBOL(transport_set_vpd_ident);
978
979 int target_cmd_size_check(struct se_cmd *cmd, unsigned int size)
980 {
981 struct se_device *dev = cmd->se_dev;
982
983 if (cmd->unknown_data_length) {
984 cmd->data_length = size;
985 } else if (size != cmd->data_length) {
986 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
987 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
988 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
989 cmd->data_length, size, cmd->t_task_cdb[0]);
990
991 if (cmd->data_direction == DMA_TO_DEVICE) {
992 pr_err("Rejecting underflow/overflow"
993 " WRITE data\n");
994 goto out_invalid_cdb_field;
995 }
996 /*
997 * Reject READ_* or WRITE_* with overflow/underflow for
998 * type SCF_SCSI_DATA_CDB.
999 */
1000 if (dev->dev_attrib.block_size != 512) {
1001 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
1002 " CDB on non 512-byte sector setup subsystem"
1003 " plugin: %s\n", dev->transport->name);
1004 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
1005 goto out_invalid_cdb_field;
1006 }
1007 /*
1008 * For the overflow case keep the existing fabric provided
1009 * ->data_length. Otherwise for the underflow case, reset
1010 * ->data_length to the smaller SCSI expected data transfer
1011 * length.
1012 */
1013 if (size > cmd->data_length) {
1014 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
1015 cmd->residual_count = (size - cmd->data_length);
1016 } else {
1017 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
1018 cmd->residual_count = (cmd->data_length - size);
1019 cmd->data_length = size;
1020 }
1021 }
1022
1023 return 0;
1024
1025 out_invalid_cdb_field:
1026 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1027 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1028 return -EINVAL;
1029 }
1030
1031 /*
1032 * Used by fabric modules containing a local struct se_cmd within their
1033 * fabric dependent per I/O descriptor.
1034 */
1035 void transport_init_se_cmd(
1036 struct se_cmd *cmd,
1037 struct target_core_fabric_ops *tfo,
1038 struct se_session *se_sess,
1039 u32 data_length,
1040 int data_direction,
1041 int task_attr,
1042 unsigned char *sense_buffer)
1043 {
1044 INIT_LIST_HEAD(&cmd->se_lun_node);
1045 INIT_LIST_HEAD(&cmd->se_delayed_node);
1046 INIT_LIST_HEAD(&cmd->se_qf_node);
1047 INIT_LIST_HEAD(&cmd->se_cmd_list);
1048 INIT_LIST_HEAD(&cmd->state_list);
1049 init_completion(&cmd->transport_lun_fe_stop_comp);
1050 init_completion(&cmd->transport_lun_stop_comp);
1051 init_completion(&cmd->t_transport_stop_comp);
1052 init_completion(&cmd->cmd_wait_comp);
1053 init_completion(&cmd->task_stop_comp);
1054 spin_lock_init(&cmd->t_state_lock);
1055 cmd->transport_state = CMD_T_DEV_ACTIVE;
1056
1057 cmd->se_tfo = tfo;
1058 cmd->se_sess = se_sess;
1059 cmd->data_length = data_length;
1060 cmd->data_direction = data_direction;
1061 cmd->sam_task_attr = task_attr;
1062 cmd->sense_buffer = sense_buffer;
1063
1064 cmd->state_active = false;
1065 }
1066 EXPORT_SYMBOL(transport_init_se_cmd);
1067
1068 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1069 {
1070 struct se_device *dev = cmd->se_dev;
1071
1072 /*
1073 * Check if SAM Task Attribute emulation is enabled for this
1074 * struct se_device storage object
1075 */
1076 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1077 return 0;
1078
1079 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1080 pr_debug("SAM Task Attribute ACA"
1081 " emulation is not supported\n");
1082 return -EINVAL;
1083 }
1084 /*
1085 * Used to determine when ORDERED commands should go from
1086 * Dormant to Active status.
1087 */
1088 cmd->se_ordered_id = atomic_inc_return(&dev->dev_ordered_id);
1089 smp_mb__after_atomic_inc();
1090 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1091 cmd->se_ordered_id, cmd->sam_task_attr,
1092 dev->transport->name);
1093 return 0;
1094 }
1095
1096 /* target_setup_cmd_from_cdb():
1097 *
1098 * Called from fabric RX Thread.
1099 */
1100 int target_setup_cmd_from_cdb(
1101 struct se_cmd *cmd,
1102 unsigned char *cdb)
1103 {
1104 struct se_device *dev = cmd->se_dev;
1105 unsigned long flags;
1106 int ret;
1107
1108 /*
1109 * Ensure that the received CDB is less than the max (252 + 8) bytes
1110 * for VARIABLE_LENGTH_CMD
1111 */
1112 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1113 pr_err("Received SCSI CDB with command_size: %d that"
1114 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1115 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1116 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1117 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1118 return -EINVAL;
1119 }
1120 /*
1121 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1122 * allocate the additional extended CDB buffer now.. Otherwise
1123 * setup the pointer from __t_task_cdb to t_task_cdb.
1124 */
1125 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1126 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1127 GFP_KERNEL);
1128 if (!cmd->t_task_cdb) {
1129 pr_err("Unable to allocate cmd->t_task_cdb"
1130 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1131 scsi_command_size(cdb),
1132 (unsigned long)sizeof(cmd->__t_task_cdb));
1133 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1134 cmd->scsi_sense_reason =
1135 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1136 return -ENOMEM;
1137 }
1138 } else
1139 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1140 /*
1141 * Copy the original CDB into cmd->
1142 */
1143 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1144
1145 /*
1146 * Check for an existing UNIT ATTENTION condition
1147 */
1148 if (core_scsi3_ua_check(cmd, cdb) < 0) {
1149 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1150 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
1151 return -EINVAL;
1152 }
1153
1154 ret = target_alua_state_check(cmd);
1155 if (ret) {
1156 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1157 if (ret > 0)
1158 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
1159 else
1160 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1161 return -EINVAL;
1162 }
1163
1164 /*
1165 * Check status for SPC-3 Persistent Reservations
1166 */
1167 ret = target_check_reservation(cmd);
1168 if (ret) {
1169 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1170 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
1171 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1172 cmd->scsi_sense_reason = TCM_RESERVATION_CONFLICT;
1173 return ret;
1174 }
1175
1176 ret = dev->transport->parse_cdb(cmd);
1177 if (ret < 0)
1178 return ret;
1179
1180 spin_lock_irqsave(&cmd->t_state_lock, flags);
1181 cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
1182 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1183
1184 /*
1185 * Check for SAM Task Attribute Emulation
1186 */
1187 if (transport_check_alloc_task_attr(cmd) < 0) {
1188 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1189 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1190 return -EINVAL;
1191 }
1192 spin_lock(&cmd->se_lun->lun_sep_lock);
1193 if (cmd->se_lun->lun_sep)
1194 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1195 spin_unlock(&cmd->se_lun->lun_sep_lock);
1196 return 0;
1197 }
1198 EXPORT_SYMBOL(target_setup_cmd_from_cdb);
1199
1200 /*
1201 * Used by fabric module frontends to queue tasks directly.
1202 * Many only be used from process context only
1203 */
1204 int transport_handle_cdb_direct(
1205 struct se_cmd *cmd)
1206 {
1207 int ret;
1208
1209 if (!cmd->se_lun) {
1210 dump_stack();
1211 pr_err("cmd->se_lun is NULL\n");
1212 return -EINVAL;
1213 }
1214 if (in_interrupt()) {
1215 dump_stack();
1216 pr_err("transport_generic_handle_cdb cannot be called"
1217 " from interrupt context\n");
1218 return -EINVAL;
1219 }
1220 /*
1221 * Set TRANSPORT_NEW_CMD state and CMD_T_ACTIVE to ensure that
1222 * outstanding descriptors are handled correctly during shutdown via
1223 * transport_wait_for_tasks()
1224 *
1225 * Also, we don't take cmd->t_state_lock here as we only expect
1226 * this to be called for initial descriptor submission.
1227 */
1228 cmd->t_state = TRANSPORT_NEW_CMD;
1229 cmd->transport_state |= CMD_T_ACTIVE;
1230
1231 /*
1232 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1233 * so follow TRANSPORT_NEW_CMD processing thread context usage
1234 * and call transport_generic_request_failure() if necessary..
1235 */
1236 ret = transport_generic_new_cmd(cmd);
1237 if (ret < 0)
1238 transport_generic_request_failure(cmd);
1239
1240 return 0;
1241 }
1242 EXPORT_SYMBOL(transport_handle_cdb_direct);
1243
1244 /*
1245 * target_submit_cmd_map_sgls - lookup unpacked lun and submit uninitialized
1246 * se_cmd + use pre-allocated SGL memory.
1247 *
1248 * @se_cmd: command descriptor to submit
1249 * @se_sess: associated se_sess for endpoint
1250 * @cdb: pointer to SCSI CDB
1251 * @sense: pointer to SCSI sense buffer
1252 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1253 * @data_length: fabric expected data transfer length
1254 * @task_addr: SAM task attribute
1255 * @data_dir: DMA data direction
1256 * @flags: flags for command submission from target_sc_flags_tables
1257 * @sgl: struct scatterlist memory for unidirectional mapping
1258 * @sgl_count: scatterlist count for unidirectional mapping
1259 * @sgl_bidi: struct scatterlist memory for bidirectional READ mapping
1260 * @sgl_bidi_count: scatterlist count for bidirectional READ mapping
1261 *
1262 * Returns non zero to signal active I/O shutdown failure. All other
1263 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1264 * but still return zero here.
1265 *
1266 * This may only be called from process context, and also currently
1267 * assumes internal allocation of fabric payload buffer by target-core.
1268 */
1269 int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess,
1270 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1271 u32 data_length, int task_attr, int data_dir, int flags,
1272 struct scatterlist *sgl, u32 sgl_count,
1273 struct scatterlist *sgl_bidi, u32 sgl_bidi_count)
1274 {
1275 struct se_portal_group *se_tpg;
1276 int rc;
1277
1278 se_tpg = se_sess->se_tpg;
1279 BUG_ON(!se_tpg);
1280 BUG_ON(se_cmd->se_tfo || se_cmd->se_sess);
1281 BUG_ON(in_interrupt());
1282 /*
1283 * Initialize se_cmd for target operation. From this point
1284 * exceptions are handled by sending exception status via
1285 * target_core_fabric_ops->queue_status() callback
1286 */
1287 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1288 data_length, data_dir, task_attr, sense);
1289 if (flags & TARGET_SCF_UNKNOWN_SIZE)
1290 se_cmd->unknown_data_length = 1;
1291 /*
1292 * Obtain struct se_cmd->cmd_kref reference and add new cmd to
1293 * se_sess->sess_cmd_list. A second kref_get here is necessary
1294 * for fabrics using TARGET_SCF_ACK_KREF that expect a second
1295 * kref_put() to happen during fabric packet acknowledgement.
1296 */
1297 rc = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1298 if (rc)
1299 return rc;
1300 /*
1301 * Signal bidirectional data payloads to target-core
1302 */
1303 if (flags & TARGET_SCF_BIDI_OP)
1304 se_cmd->se_cmd_flags |= SCF_BIDI;
1305 /*
1306 * Locate se_lun pointer and attach it to struct se_cmd
1307 */
1308 if (transport_lookup_cmd_lun(se_cmd, unpacked_lun) < 0) {
1309 transport_send_check_condition_and_sense(se_cmd,
1310 se_cmd->scsi_sense_reason, 0);
1311 target_put_sess_cmd(se_sess, se_cmd);
1312 return 0;
1313 }
1314
1315 rc = target_setup_cmd_from_cdb(se_cmd, cdb);
1316 if (rc != 0) {
1317 transport_generic_request_failure(se_cmd);
1318 return 0;
1319 }
1320 /*
1321 * When a non zero sgl_count has been passed perform SGL passthrough
1322 * mapping for pre-allocated fabric memory instead of having target
1323 * core perform an internal SGL allocation..
1324 */
1325 if (sgl_count != 0) {
1326 BUG_ON(!sgl);
1327
1328 /*
1329 * A work-around for tcm_loop as some userspace code via
1330 * scsi-generic do not memset their associated read buffers,
1331 * so go ahead and do that here for type non-data CDBs. Also
1332 * note that this is currently guaranteed to be a single SGL
1333 * for this case by target core in target_setup_cmd_from_cdb()
1334 * -> transport_generic_cmd_sequencer().
1335 */
1336 if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
1337 se_cmd->data_direction == DMA_FROM_DEVICE) {
1338 unsigned char *buf = NULL;
1339
1340 if (sgl)
1341 buf = kmap(sg_page(sgl)) + sgl->offset;
1342
1343 if (buf) {
1344 memset(buf, 0, sgl->length);
1345 kunmap(sg_page(sgl));
1346 }
1347 }
1348
1349 rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
1350 sgl_bidi, sgl_bidi_count);
1351 if (rc != 0) {
1352 transport_generic_request_failure(se_cmd);
1353 return 0;
1354 }
1355 }
1356 /*
1357 * Check if we need to delay processing because of ALUA
1358 * Active/NonOptimized primary access state..
1359 */
1360 core_alua_check_nonop_delay(se_cmd);
1361
1362 transport_handle_cdb_direct(se_cmd);
1363 return 0;
1364 }
1365 EXPORT_SYMBOL(target_submit_cmd_map_sgls);
1366
1367 /*
1368 * target_submit_cmd - lookup unpacked lun and submit uninitialized se_cmd
1369 *
1370 * @se_cmd: command descriptor to submit
1371 * @se_sess: associated se_sess for endpoint
1372 * @cdb: pointer to SCSI CDB
1373 * @sense: pointer to SCSI sense buffer
1374 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1375 * @data_length: fabric expected data transfer length
1376 * @task_addr: SAM task attribute
1377 * @data_dir: DMA data direction
1378 * @flags: flags for command submission from target_sc_flags_tables
1379 *
1380 * Returns non zero to signal active I/O shutdown failure. All other
1381 * setup exceptions will be returned as a SCSI CHECK_CONDITION response,
1382 * but still return zero here.
1383 *
1384 * This may only be called from process context, and also currently
1385 * assumes internal allocation of fabric payload buffer by target-core.
1386 *
1387 * It also assumes interal target core SGL memory allocation.
1388 */
1389 int target_submit_cmd(struct se_cmd *se_cmd, struct se_session *se_sess,
1390 unsigned char *cdb, unsigned char *sense, u32 unpacked_lun,
1391 u32 data_length, int task_attr, int data_dir, int flags)
1392 {
1393 return target_submit_cmd_map_sgls(se_cmd, se_sess, cdb, sense,
1394 unpacked_lun, data_length, task_attr, data_dir,
1395 flags, NULL, 0, NULL, 0);
1396 }
1397 EXPORT_SYMBOL(target_submit_cmd);
1398
1399 static void target_complete_tmr_failure(struct work_struct *work)
1400 {
1401 struct se_cmd *se_cmd = container_of(work, struct se_cmd, work);
1402
1403 se_cmd->se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
1404 se_cmd->se_tfo->queue_tm_rsp(se_cmd);
1405 }
1406
1407 /**
1408 * target_submit_tmr - lookup unpacked lun and submit uninitialized se_cmd
1409 * for TMR CDBs
1410 *
1411 * @se_cmd: command descriptor to submit
1412 * @se_sess: associated se_sess for endpoint
1413 * @sense: pointer to SCSI sense buffer
1414 * @unpacked_lun: unpacked LUN to reference for struct se_lun
1415 * @fabric_context: fabric context for TMR req
1416 * @tm_type: Type of TM request
1417 * @gfp: gfp type for caller
1418 * @tag: referenced task tag for TMR_ABORT_TASK
1419 * @flags: submit cmd flags
1420 *
1421 * Callable from all contexts.
1422 **/
1423
1424 int target_submit_tmr(struct se_cmd *se_cmd, struct se_session *se_sess,
1425 unsigned char *sense, u32 unpacked_lun,
1426 void *fabric_tmr_ptr, unsigned char tm_type,
1427 gfp_t gfp, unsigned int tag, int flags)
1428 {
1429 struct se_portal_group *se_tpg;
1430 int ret;
1431
1432 se_tpg = se_sess->se_tpg;
1433 BUG_ON(!se_tpg);
1434
1435 transport_init_se_cmd(se_cmd, se_tpg->se_tpg_tfo, se_sess,
1436 0, DMA_NONE, MSG_SIMPLE_TAG, sense);
1437 /*
1438 * FIXME: Currently expect caller to handle se_cmd->se_tmr_req
1439 * allocation failure.
1440 */
1441 ret = core_tmr_alloc_req(se_cmd, fabric_tmr_ptr, tm_type, gfp);
1442 if (ret < 0)
1443 return -ENOMEM;
1444
1445 if (tm_type == TMR_ABORT_TASK)
1446 se_cmd->se_tmr_req->ref_task_tag = tag;
1447
1448 /* See target_submit_cmd for commentary */
1449 ret = target_get_sess_cmd(se_sess, se_cmd, (flags & TARGET_SCF_ACK_KREF));
1450 if (ret) {
1451 core_tmr_release_req(se_cmd->se_tmr_req);
1452 return ret;
1453 }
1454
1455 ret = transport_lookup_tmr_lun(se_cmd, unpacked_lun);
1456 if (ret) {
1457 /*
1458 * For callback during failure handling, push this work off
1459 * to process context with TMR_LUN_DOES_NOT_EXIST status.
1460 */
1461 INIT_WORK(&se_cmd->work, target_complete_tmr_failure);
1462 schedule_work(&se_cmd->work);
1463 return 0;
1464 }
1465 transport_generic_handle_tmr(se_cmd);
1466 return 0;
1467 }
1468 EXPORT_SYMBOL(target_submit_tmr);
1469
1470 /*
1471 * If the cmd is active, request it to be stopped and sleep until it
1472 * has completed.
1473 */
1474 bool target_stop_cmd(struct se_cmd *cmd, unsigned long *flags)
1475 {
1476 bool was_active = false;
1477
1478 if (cmd->transport_state & CMD_T_BUSY) {
1479 cmd->transport_state |= CMD_T_REQUEST_STOP;
1480 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
1481
1482 pr_debug("cmd %p waiting to complete\n", cmd);
1483 wait_for_completion(&cmd->task_stop_comp);
1484 pr_debug("cmd %p stopped successfully\n", cmd);
1485
1486 spin_lock_irqsave(&cmd->t_state_lock, *flags);
1487 cmd->transport_state &= ~CMD_T_REQUEST_STOP;
1488 cmd->transport_state &= ~CMD_T_BUSY;
1489 was_active = true;
1490 }
1491
1492 return was_active;
1493 }
1494
1495 /*
1496 * Handle SAM-esque emulation for generic transport request failures.
1497 */
1498 void transport_generic_request_failure(struct se_cmd *cmd)
1499 {
1500 int ret = 0;
1501
1502 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1503 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1504 cmd->t_task_cdb[0]);
1505 pr_debug("-----[ i_state: %d t_state: %d scsi_sense_reason: %d\n",
1506 cmd->se_tfo->get_cmd_state(cmd),
1507 cmd->t_state, cmd->scsi_sense_reason);
1508 pr_debug("-----[ CMD_T_ACTIVE: %d CMD_T_STOP: %d CMD_T_SENT: %d\n",
1509 (cmd->transport_state & CMD_T_ACTIVE) != 0,
1510 (cmd->transport_state & CMD_T_STOP) != 0,
1511 (cmd->transport_state & CMD_T_SENT) != 0);
1512
1513 /*
1514 * For SAM Task Attribute emulation for failed struct se_cmd
1515 */
1516 transport_complete_task_attr(cmd);
1517
1518 switch (cmd->scsi_sense_reason) {
1519 case TCM_NON_EXISTENT_LUN:
1520 case TCM_UNSUPPORTED_SCSI_OPCODE:
1521 case TCM_INVALID_CDB_FIELD:
1522 case TCM_INVALID_PARAMETER_LIST:
1523 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
1524 case TCM_UNKNOWN_MODE_PAGE:
1525 case TCM_WRITE_PROTECTED:
1526 case TCM_ADDRESS_OUT_OF_RANGE:
1527 case TCM_CHECK_CONDITION_ABORT_CMD:
1528 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
1529 case TCM_CHECK_CONDITION_NOT_READY:
1530 break;
1531 case TCM_RESERVATION_CONFLICT:
1532 /*
1533 * No SENSE Data payload for this case, set SCSI Status
1534 * and queue the response to $FABRIC_MOD.
1535 *
1536 * Uses linux/include/scsi/scsi.h SAM status codes defs
1537 */
1538 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1539 /*
1540 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1541 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1542 * CONFLICT STATUS.
1543 *
1544 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1545 */
1546 if (cmd->se_sess &&
1547 cmd->se_dev->dev_attrib.emulate_ua_intlck_ctrl == 2)
1548 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1549 cmd->orig_fe_lun, 0x2C,
1550 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1551
1552 ret = cmd->se_tfo->queue_status(cmd);
1553 if (ret == -EAGAIN || ret == -ENOMEM)
1554 goto queue_full;
1555 goto check_stop;
1556 default:
1557 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1558 cmd->t_task_cdb[0], cmd->scsi_sense_reason);
1559 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1560 break;
1561 }
1562
1563 ret = transport_send_check_condition_and_sense(cmd,
1564 cmd->scsi_sense_reason, 0);
1565 if (ret == -EAGAIN || ret == -ENOMEM)
1566 goto queue_full;
1567
1568 check_stop:
1569 transport_lun_remove_cmd(cmd);
1570 if (!transport_cmd_check_stop_to_fabric(cmd))
1571 ;
1572 return;
1573
1574 queue_full:
1575 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1576 transport_handle_queue_full(cmd, cmd->se_dev);
1577 }
1578 EXPORT_SYMBOL(transport_generic_request_failure);
1579
1580 static void __target_execute_cmd(struct se_cmd *cmd)
1581 {
1582 int error = 0;
1583
1584 spin_lock_irq(&cmd->t_state_lock);
1585 cmd->transport_state |= (CMD_T_BUSY|CMD_T_SENT);
1586 spin_unlock_irq(&cmd->t_state_lock);
1587
1588 if (cmd->execute_cmd)
1589 error = cmd->execute_cmd(cmd);
1590
1591 if (error) {
1592 spin_lock_irq(&cmd->t_state_lock);
1593 cmd->transport_state &= ~(CMD_T_BUSY|CMD_T_SENT);
1594 spin_unlock_irq(&cmd->t_state_lock);
1595
1596 transport_generic_request_failure(cmd);
1597 }
1598 }
1599
1600 static bool target_handle_task_attr(struct se_cmd *cmd)
1601 {
1602 struct se_device *dev = cmd->se_dev;
1603
1604 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1605 return false;
1606
1607 /*
1608 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
1609 * to allow the passed struct se_cmd list of tasks to the front of the list.
1610 */
1611 switch (cmd->sam_task_attr) {
1612 case MSG_HEAD_TAG:
1613 pr_debug("Added HEAD_OF_QUEUE for CDB: 0x%02x, "
1614 "se_ordered_id: %u\n",
1615 cmd->t_task_cdb[0], cmd->se_ordered_id);
1616 return false;
1617 case MSG_ORDERED_TAG:
1618 atomic_inc(&dev->dev_ordered_sync);
1619 smp_mb__after_atomic_inc();
1620
1621 pr_debug("Added ORDERED for CDB: 0x%02x to ordered list, "
1622 " se_ordered_id: %u\n",
1623 cmd->t_task_cdb[0], cmd->se_ordered_id);
1624
1625 /*
1626 * Execute an ORDERED command if no other older commands
1627 * exist that need to be completed first.
1628 */
1629 if (!atomic_read(&dev->simple_cmds))
1630 return false;
1631 break;
1632 default:
1633 /*
1634 * For SIMPLE and UNTAGGED Task Attribute commands
1635 */
1636 atomic_inc(&dev->simple_cmds);
1637 smp_mb__after_atomic_inc();
1638 break;
1639 }
1640
1641 if (atomic_read(&dev->dev_ordered_sync) == 0)
1642 return false;
1643
1644 spin_lock(&dev->delayed_cmd_lock);
1645 list_add_tail(&cmd->se_delayed_node, &dev->delayed_cmd_list);
1646 spin_unlock(&dev->delayed_cmd_lock);
1647
1648 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
1649 " delayed CMD list, se_ordered_id: %u\n",
1650 cmd->t_task_cdb[0], cmd->sam_task_attr,
1651 cmd->se_ordered_id);
1652 return true;
1653 }
1654
1655 void target_execute_cmd(struct se_cmd *cmd)
1656 {
1657 /*
1658 * If the received CDB has aleady been aborted stop processing it here.
1659 */
1660 if (transport_check_aborted_status(cmd, 1))
1661 return;
1662
1663 /*
1664 * Determine if IOCTL context caller in requesting the stopping of this
1665 * command for LUN shutdown purposes.
1666 */
1667 spin_lock_irq(&cmd->t_state_lock);
1668 if (cmd->transport_state & CMD_T_LUN_STOP) {
1669 pr_debug("%s:%d CMD_T_LUN_STOP for ITT: 0x%08x\n",
1670 __func__, __LINE__, cmd->se_tfo->get_task_tag(cmd));
1671
1672 cmd->transport_state &= ~CMD_T_ACTIVE;
1673 spin_unlock_irq(&cmd->t_state_lock);
1674 complete(&cmd->transport_lun_stop_comp);
1675 return;
1676 }
1677 /*
1678 * Determine if frontend context caller is requesting the stopping of
1679 * this command for frontend exceptions.
1680 */
1681 if (cmd->transport_state & CMD_T_STOP) {
1682 pr_debug("%s:%d CMD_T_STOP for ITT: 0x%08x\n",
1683 __func__, __LINE__,
1684 cmd->se_tfo->get_task_tag(cmd));
1685
1686 spin_unlock_irq(&cmd->t_state_lock);
1687 complete(&cmd->t_transport_stop_comp);
1688 return;
1689 }
1690
1691 cmd->t_state = TRANSPORT_PROCESSING;
1692 spin_unlock_irq(&cmd->t_state_lock);
1693
1694 if (!target_handle_task_attr(cmd))
1695 __target_execute_cmd(cmd);
1696 }
1697 EXPORT_SYMBOL(target_execute_cmd);
1698
1699 /*
1700 * Process all commands up to the last received ORDERED task attribute which
1701 * requires another blocking boundary
1702 */
1703 static void target_restart_delayed_cmds(struct se_device *dev)
1704 {
1705 for (;;) {
1706 struct se_cmd *cmd;
1707
1708 spin_lock(&dev->delayed_cmd_lock);
1709 if (list_empty(&dev->delayed_cmd_list)) {
1710 spin_unlock(&dev->delayed_cmd_lock);
1711 break;
1712 }
1713
1714 cmd = list_entry(dev->delayed_cmd_list.next,
1715 struct se_cmd, se_delayed_node);
1716 list_del(&cmd->se_delayed_node);
1717 spin_unlock(&dev->delayed_cmd_lock);
1718
1719 __target_execute_cmd(cmd);
1720
1721 if (cmd->sam_task_attr == MSG_ORDERED_TAG)
1722 break;
1723 }
1724 }
1725
1726 /*
1727 * Called from I/O completion to determine which dormant/delayed
1728 * and ordered cmds need to have their tasks added to the execution queue.
1729 */
1730 static void transport_complete_task_attr(struct se_cmd *cmd)
1731 {
1732 struct se_device *dev = cmd->se_dev;
1733
1734 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
1735 return;
1736
1737 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
1738 atomic_dec(&dev->simple_cmds);
1739 smp_mb__after_atomic_dec();
1740 dev->dev_cur_ordered_id++;
1741 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
1742 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
1743 cmd->se_ordered_id);
1744 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
1745 dev->dev_cur_ordered_id++;
1746 pr_debug("Incremented dev_cur_ordered_id: %u for"
1747 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
1748 cmd->se_ordered_id);
1749 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
1750 atomic_dec(&dev->dev_ordered_sync);
1751 smp_mb__after_atomic_dec();
1752
1753 dev->dev_cur_ordered_id++;
1754 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
1755 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
1756 }
1757
1758 target_restart_delayed_cmds(dev);
1759 }
1760
1761 static void transport_complete_qf(struct se_cmd *cmd)
1762 {
1763 int ret = 0;
1764
1765 transport_complete_task_attr(cmd);
1766
1767 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1768 ret = cmd->se_tfo->queue_status(cmd);
1769 if (ret)
1770 goto out;
1771 }
1772
1773 switch (cmd->data_direction) {
1774 case DMA_FROM_DEVICE:
1775 ret = cmd->se_tfo->queue_data_in(cmd);
1776 break;
1777 case DMA_TO_DEVICE:
1778 if (cmd->t_bidi_data_sg) {
1779 ret = cmd->se_tfo->queue_data_in(cmd);
1780 if (ret < 0)
1781 break;
1782 }
1783 /* Fall through for DMA_TO_DEVICE */
1784 case DMA_NONE:
1785 ret = cmd->se_tfo->queue_status(cmd);
1786 break;
1787 default:
1788 break;
1789 }
1790
1791 out:
1792 if (ret < 0) {
1793 transport_handle_queue_full(cmd, cmd->se_dev);
1794 return;
1795 }
1796 transport_lun_remove_cmd(cmd);
1797 transport_cmd_check_stop_to_fabric(cmd);
1798 }
1799
1800 static void transport_handle_queue_full(
1801 struct se_cmd *cmd,
1802 struct se_device *dev)
1803 {
1804 spin_lock_irq(&dev->qf_cmd_lock);
1805 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
1806 atomic_inc(&dev->dev_qf_count);
1807 smp_mb__after_atomic_inc();
1808 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
1809
1810 schedule_work(&cmd->se_dev->qf_work_queue);
1811 }
1812
1813 static void target_complete_ok_work(struct work_struct *work)
1814 {
1815 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
1816 int ret;
1817
1818 /*
1819 * Check if we need to move delayed/dormant tasks from cmds on the
1820 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
1821 * Attribute.
1822 */
1823 transport_complete_task_attr(cmd);
1824
1825 /*
1826 * Check to schedule QUEUE_FULL work, or execute an existing
1827 * cmd->transport_qf_callback()
1828 */
1829 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
1830 schedule_work(&cmd->se_dev->qf_work_queue);
1831
1832 /*
1833 * Check if we need to send a sense buffer from
1834 * the struct se_cmd in question.
1835 */
1836 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
1837 WARN_ON(!cmd->scsi_status);
1838 ret = transport_send_check_condition_and_sense(
1839 cmd, 0, 1);
1840 if (ret == -EAGAIN || ret == -ENOMEM)
1841 goto queue_full;
1842
1843 transport_lun_remove_cmd(cmd);
1844 transport_cmd_check_stop_to_fabric(cmd);
1845 return;
1846 }
1847 /*
1848 * Check for a callback, used by amongst other things
1849 * XDWRITE_READ_10 emulation.
1850 */
1851 if (cmd->transport_complete_callback)
1852 cmd->transport_complete_callback(cmd);
1853
1854 switch (cmd->data_direction) {
1855 case DMA_FROM_DEVICE:
1856 spin_lock(&cmd->se_lun->lun_sep_lock);
1857 if (cmd->se_lun->lun_sep) {
1858 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
1859 cmd->data_length;
1860 }
1861 spin_unlock(&cmd->se_lun->lun_sep_lock);
1862
1863 ret = cmd->se_tfo->queue_data_in(cmd);
1864 if (ret == -EAGAIN || ret == -ENOMEM)
1865 goto queue_full;
1866 break;
1867 case DMA_TO_DEVICE:
1868 spin_lock(&cmd->se_lun->lun_sep_lock);
1869 if (cmd->se_lun->lun_sep) {
1870 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
1871 cmd->data_length;
1872 }
1873 spin_unlock(&cmd->se_lun->lun_sep_lock);
1874 /*
1875 * Check if we need to send READ payload for BIDI-COMMAND
1876 */
1877 if (cmd->t_bidi_data_sg) {
1878 spin_lock(&cmd->se_lun->lun_sep_lock);
1879 if (cmd->se_lun->lun_sep) {
1880 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
1881 cmd->data_length;
1882 }
1883 spin_unlock(&cmd->se_lun->lun_sep_lock);
1884 ret = cmd->se_tfo->queue_data_in(cmd);
1885 if (ret == -EAGAIN || ret == -ENOMEM)
1886 goto queue_full;
1887 break;
1888 }
1889 /* Fall through for DMA_TO_DEVICE */
1890 case DMA_NONE:
1891 ret = cmd->se_tfo->queue_status(cmd);
1892 if (ret == -EAGAIN || ret == -ENOMEM)
1893 goto queue_full;
1894 break;
1895 default:
1896 break;
1897 }
1898
1899 transport_lun_remove_cmd(cmd);
1900 transport_cmd_check_stop_to_fabric(cmd);
1901 return;
1902
1903 queue_full:
1904 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
1905 " data_direction: %d\n", cmd, cmd->data_direction);
1906 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1907 transport_handle_queue_full(cmd, cmd->se_dev);
1908 }
1909
1910 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
1911 {
1912 struct scatterlist *sg;
1913 int count;
1914
1915 for_each_sg(sgl, sg, nents, count)
1916 __free_page(sg_page(sg));
1917
1918 kfree(sgl);
1919 }
1920
1921 static inline void transport_free_pages(struct se_cmd *cmd)
1922 {
1923 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
1924 return;
1925
1926 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
1927 cmd->t_data_sg = NULL;
1928 cmd->t_data_nents = 0;
1929
1930 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
1931 cmd->t_bidi_data_sg = NULL;
1932 cmd->t_bidi_data_nents = 0;
1933 }
1934
1935 /**
1936 * transport_release_cmd - free a command
1937 * @cmd: command to free
1938 *
1939 * This routine unconditionally frees a command, and reference counting
1940 * or list removal must be done in the caller.
1941 */
1942 static void transport_release_cmd(struct se_cmd *cmd)
1943 {
1944 BUG_ON(!cmd->se_tfo);
1945
1946 if (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
1947 core_tmr_release_req(cmd->se_tmr_req);
1948 if (cmd->t_task_cdb != cmd->__t_task_cdb)
1949 kfree(cmd->t_task_cdb);
1950 /*
1951 * If this cmd has been setup with target_get_sess_cmd(), drop
1952 * the kref and call ->release_cmd() in kref callback.
1953 */
1954 if (cmd->check_release != 0) {
1955 target_put_sess_cmd(cmd->se_sess, cmd);
1956 return;
1957 }
1958 cmd->se_tfo->release_cmd(cmd);
1959 }
1960
1961 /**
1962 * transport_put_cmd - release a reference to a command
1963 * @cmd: command to release
1964 *
1965 * This routine releases our reference to the command and frees it if possible.
1966 */
1967 static void transport_put_cmd(struct se_cmd *cmd)
1968 {
1969 unsigned long flags;
1970
1971 spin_lock_irqsave(&cmd->t_state_lock, flags);
1972 if (atomic_read(&cmd->t_fe_count)) {
1973 if (!atomic_dec_and_test(&cmd->t_fe_count))
1974 goto out_busy;
1975 }
1976
1977 if (cmd->transport_state & CMD_T_DEV_ACTIVE) {
1978 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
1979 target_remove_from_state_list(cmd);
1980 }
1981 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1982
1983 transport_free_pages(cmd);
1984 transport_release_cmd(cmd);
1985 return;
1986 out_busy:
1987 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1988 }
1989
1990 /*
1991 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
1992 * allocating in the core.
1993 * @cmd: Associated se_cmd descriptor
1994 * @mem: SGL style memory for TCM WRITE / READ
1995 * @sg_mem_num: Number of SGL elements
1996 * @mem_bidi_in: SGL style memory for TCM BIDI READ
1997 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
1998 *
1999 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
2000 * of parameters.
2001 */
2002 int transport_generic_map_mem_to_cmd(
2003 struct se_cmd *cmd,
2004 struct scatterlist *sgl,
2005 u32 sgl_count,
2006 struct scatterlist *sgl_bidi,
2007 u32 sgl_bidi_count)
2008 {
2009 if (!sgl || !sgl_count)
2010 return 0;
2011
2012 /*
2013 * Reject SCSI data overflow with map_mem_to_cmd() as incoming
2014 * scatterlists already have been set to follow what the fabric
2015 * passes for the original expected data transfer length.
2016 */
2017 if (cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2018 pr_warn("Rejecting SCSI DATA overflow for fabric using"
2019 " SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC\n");
2020 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2021 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
2022 return -EINVAL;
2023 }
2024
2025 cmd->t_data_sg = sgl;
2026 cmd->t_data_nents = sgl_count;
2027
2028 if (sgl_bidi && sgl_bidi_count) {
2029 cmd->t_bidi_data_sg = sgl_bidi;
2030 cmd->t_bidi_data_nents = sgl_bidi_count;
2031 }
2032 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
2033 return 0;
2034 }
2035 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
2036
2037 void *transport_kmap_data_sg(struct se_cmd *cmd)
2038 {
2039 struct scatterlist *sg = cmd->t_data_sg;
2040 struct page **pages;
2041 int i;
2042
2043 /*
2044 * We need to take into account a possible offset here for fabrics like
2045 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
2046 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
2047 */
2048 if (!cmd->t_data_nents)
2049 return NULL;
2050
2051 BUG_ON(!sg);
2052 if (cmd->t_data_nents == 1)
2053 return kmap(sg_page(sg)) + sg->offset;
2054
2055 /* >1 page. use vmap */
2056 pages = kmalloc(sizeof(*pages) * cmd->t_data_nents, GFP_KERNEL);
2057 if (!pages) {
2058 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2059 return NULL;
2060 }
2061
2062 /* convert sg[] to pages[] */
2063 for_each_sg(cmd->t_data_sg, sg, cmd->t_data_nents, i) {
2064 pages[i] = sg_page(sg);
2065 }
2066
2067 cmd->t_data_vmap = vmap(pages, cmd->t_data_nents, VM_MAP, PAGE_KERNEL);
2068 kfree(pages);
2069 if (!cmd->t_data_vmap) {
2070 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2071 return NULL;
2072 }
2073
2074 return cmd->t_data_vmap + cmd->t_data_sg[0].offset;
2075 }
2076 EXPORT_SYMBOL(transport_kmap_data_sg);
2077
2078 void transport_kunmap_data_sg(struct se_cmd *cmd)
2079 {
2080 if (!cmd->t_data_nents) {
2081 return;
2082 } else if (cmd->t_data_nents == 1) {
2083 kunmap(sg_page(cmd->t_data_sg));
2084 return;
2085 }
2086
2087 vunmap(cmd->t_data_vmap);
2088 cmd->t_data_vmap = NULL;
2089 }
2090 EXPORT_SYMBOL(transport_kunmap_data_sg);
2091
2092 static int
2093 transport_generic_get_mem(struct se_cmd *cmd)
2094 {
2095 u32 length = cmd->data_length;
2096 unsigned int nents;
2097 struct page *page;
2098 gfp_t zero_flag;
2099 int i = 0;
2100
2101 nents = DIV_ROUND_UP(length, PAGE_SIZE);
2102 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
2103 if (!cmd->t_data_sg)
2104 return -ENOMEM;
2105
2106 cmd->t_data_nents = nents;
2107 sg_init_table(cmd->t_data_sg, nents);
2108
2109 zero_flag = cmd->se_cmd_flags & SCF_SCSI_DATA_CDB ? 0 : __GFP_ZERO;
2110
2111 while (length) {
2112 u32 page_len = min_t(u32, length, PAGE_SIZE);
2113 page = alloc_page(GFP_KERNEL | zero_flag);
2114 if (!page)
2115 goto out;
2116
2117 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
2118 length -= page_len;
2119 i++;
2120 }
2121 return 0;
2122
2123 out:
2124 while (i > 0) {
2125 i--;
2126 __free_page(sg_page(&cmd->t_data_sg[i]));
2127 }
2128 kfree(cmd->t_data_sg);
2129 cmd->t_data_sg = NULL;
2130 return -ENOMEM;
2131 }
2132
2133 /*
2134 * Allocate any required resources to execute the command. For writes we
2135 * might not have the payload yet, so notify the fabric via a call to
2136 * ->write_pending instead. Otherwise place it on the execution queue.
2137 */
2138 int transport_generic_new_cmd(struct se_cmd *cmd)
2139 {
2140 int ret = 0;
2141
2142 /*
2143 * Determine is the TCM fabric module has already allocated physical
2144 * memory, and is directly calling transport_generic_map_mem_to_cmd()
2145 * beforehand.
2146 */
2147 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
2148 cmd->data_length) {
2149 ret = transport_generic_get_mem(cmd);
2150 if (ret < 0)
2151 goto out_fail;
2152 }
2153
2154 atomic_inc(&cmd->t_fe_count);
2155
2156 /*
2157 * If this command is not a write we can execute it right here,
2158 * for write buffers we need to notify the fabric driver first
2159 * and let it call back once the write buffers are ready.
2160 */
2161 target_add_to_state_list(cmd);
2162 if (cmd->data_direction != DMA_TO_DEVICE) {
2163 target_execute_cmd(cmd);
2164 return 0;
2165 }
2166
2167 spin_lock_irq(&cmd->t_state_lock);
2168 cmd->t_state = TRANSPORT_WRITE_PENDING;
2169 spin_unlock_irq(&cmd->t_state_lock);
2170
2171 transport_cmd_check_stop(cmd, false);
2172
2173 ret = cmd->se_tfo->write_pending(cmd);
2174 if (ret == -EAGAIN || ret == -ENOMEM)
2175 goto queue_full;
2176
2177 if (ret < 0)
2178 return ret;
2179 return 1;
2180
2181 out_fail:
2182 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2183 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
2184 return -EINVAL;
2185 queue_full:
2186 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
2187 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
2188 transport_handle_queue_full(cmd, cmd->se_dev);
2189 return 0;
2190 }
2191 EXPORT_SYMBOL(transport_generic_new_cmd);
2192
2193 static void transport_write_pending_qf(struct se_cmd *cmd)
2194 {
2195 int ret;
2196
2197 ret = cmd->se_tfo->write_pending(cmd);
2198 if (ret == -EAGAIN || ret == -ENOMEM) {
2199 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
2200 cmd);
2201 transport_handle_queue_full(cmd, cmd->se_dev);
2202 }
2203 }
2204
2205 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
2206 {
2207 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
2208 if (wait_for_tasks && (cmd->se_cmd_flags & SCF_SCSI_TMR_CDB))
2209 transport_wait_for_tasks(cmd);
2210
2211 transport_release_cmd(cmd);
2212 } else {
2213 if (wait_for_tasks)
2214 transport_wait_for_tasks(cmd);
2215
2216 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
2217
2218 if (cmd->se_lun)
2219 transport_lun_remove_cmd(cmd);
2220
2221 transport_put_cmd(cmd);
2222 }
2223 }
2224 EXPORT_SYMBOL(transport_generic_free_cmd);
2225
2226 /* target_get_sess_cmd - Add command to active ->sess_cmd_list
2227 * @se_sess: session to reference
2228 * @se_cmd: command descriptor to add
2229 * @ack_kref: Signal that fabric will perform an ack target_put_sess_cmd()
2230 */
2231 static int target_get_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd,
2232 bool ack_kref)
2233 {
2234 unsigned long flags;
2235 int ret = 0;
2236
2237 kref_init(&se_cmd->cmd_kref);
2238 /*
2239 * Add a second kref if the fabric caller is expecting to handle
2240 * fabric acknowledgement that requires two target_put_sess_cmd()
2241 * invocations before se_cmd descriptor release.
2242 */
2243 if (ack_kref == true) {
2244 kref_get(&se_cmd->cmd_kref);
2245 se_cmd->se_cmd_flags |= SCF_ACK_KREF;
2246 }
2247
2248 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2249 if (se_sess->sess_tearing_down) {
2250 ret = -ESHUTDOWN;
2251 goto out;
2252 }
2253 list_add_tail(&se_cmd->se_cmd_list, &se_sess->sess_cmd_list);
2254 se_cmd->check_release = 1;
2255
2256 out:
2257 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2258 return ret;
2259 }
2260
2261 static void target_release_cmd_kref(struct kref *kref)
2262 {
2263 struct se_cmd *se_cmd = container_of(kref, struct se_cmd, cmd_kref);
2264 struct se_session *se_sess = se_cmd->se_sess;
2265 unsigned long flags;
2266
2267 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2268 if (list_empty(&se_cmd->se_cmd_list)) {
2269 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2270 se_cmd->se_tfo->release_cmd(se_cmd);
2271 return;
2272 }
2273 if (se_sess->sess_tearing_down && se_cmd->cmd_wait_set) {
2274 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2275 complete(&se_cmd->cmd_wait_comp);
2276 return;
2277 }
2278 list_del(&se_cmd->se_cmd_list);
2279 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2280
2281 se_cmd->se_tfo->release_cmd(se_cmd);
2282 }
2283
2284 /* target_put_sess_cmd - Check for active I/O shutdown via kref_put
2285 * @se_sess: session to reference
2286 * @se_cmd: command descriptor to drop
2287 */
2288 int target_put_sess_cmd(struct se_session *se_sess, struct se_cmd *se_cmd)
2289 {
2290 return kref_put(&se_cmd->cmd_kref, target_release_cmd_kref);
2291 }
2292 EXPORT_SYMBOL(target_put_sess_cmd);
2293
2294 /* target_sess_cmd_list_set_waiting - Flag all commands in
2295 * sess_cmd_list to complete cmd_wait_comp. Set
2296 * sess_tearing_down so no more commands are queued.
2297 * @se_sess: session to flag
2298 */
2299 void target_sess_cmd_list_set_waiting(struct se_session *se_sess)
2300 {
2301 struct se_cmd *se_cmd;
2302 unsigned long flags;
2303
2304 spin_lock_irqsave(&se_sess->sess_cmd_lock, flags);
2305
2306 WARN_ON(se_sess->sess_tearing_down);
2307 se_sess->sess_tearing_down = 1;
2308
2309 list_for_each_entry(se_cmd, &se_sess->sess_cmd_list, se_cmd_list)
2310 se_cmd->cmd_wait_set = 1;
2311
2312 spin_unlock_irqrestore(&se_sess->sess_cmd_lock, flags);
2313 }
2314 EXPORT_SYMBOL(target_sess_cmd_list_set_waiting);
2315
2316 /* target_wait_for_sess_cmds - Wait for outstanding descriptors
2317 * @se_sess: session to wait for active I/O
2318 * @wait_for_tasks: Make extra transport_wait_for_tasks call
2319 */
2320 void target_wait_for_sess_cmds(
2321 struct se_session *se_sess,
2322 int wait_for_tasks)
2323 {
2324 struct se_cmd *se_cmd, *tmp_cmd;
2325 bool rc = false;
2326
2327 list_for_each_entry_safe(se_cmd, tmp_cmd,
2328 &se_sess->sess_cmd_list, se_cmd_list) {
2329 list_del(&se_cmd->se_cmd_list);
2330
2331 pr_debug("Waiting for se_cmd: %p t_state: %d, fabric state:"
2332 " %d\n", se_cmd, se_cmd->t_state,
2333 se_cmd->se_tfo->get_cmd_state(se_cmd));
2334
2335 if (wait_for_tasks) {
2336 pr_debug("Calling transport_wait_for_tasks se_cmd: %p t_state: %d,"
2337 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2338 se_cmd->se_tfo->get_cmd_state(se_cmd));
2339
2340 rc = transport_wait_for_tasks(se_cmd);
2341
2342 pr_debug("After transport_wait_for_tasks se_cmd: %p t_state: %d,"
2343 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2344 se_cmd->se_tfo->get_cmd_state(se_cmd));
2345 }
2346
2347 if (!rc) {
2348 wait_for_completion(&se_cmd->cmd_wait_comp);
2349 pr_debug("After cmd_wait_comp: se_cmd: %p t_state: %d"
2350 " fabric state: %d\n", se_cmd, se_cmd->t_state,
2351 se_cmd->se_tfo->get_cmd_state(se_cmd));
2352 }
2353
2354 se_cmd->se_tfo->release_cmd(se_cmd);
2355 }
2356 }
2357 EXPORT_SYMBOL(target_wait_for_sess_cmds);
2358
2359 /* transport_lun_wait_for_tasks():
2360 *
2361 * Called from ConfigFS context to stop the passed struct se_cmd to allow
2362 * an struct se_lun to be successfully shutdown.
2363 */
2364 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
2365 {
2366 unsigned long flags;
2367 int ret = 0;
2368
2369 /*
2370 * If the frontend has already requested this struct se_cmd to
2371 * be stopped, we can safely ignore this struct se_cmd.
2372 */
2373 spin_lock_irqsave(&cmd->t_state_lock, flags);
2374 if (cmd->transport_state & CMD_T_STOP) {
2375 cmd->transport_state &= ~CMD_T_LUN_STOP;
2376
2377 pr_debug("ConfigFS ITT[0x%08x] - CMD_T_STOP, skipping\n",
2378 cmd->se_tfo->get_task_tag(cmd));
2379 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2380 transport_cmd_check_stop(cmd, false);
2381 return -EPERM;
2382 }
2383 cmd->transport_state |= CMD_T_LUN_FE_STOP;
2384 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2385
2386 // XXX: audit task_flags checks.
2387 spin_lock_irqsave(&cmd->t_state_lock, flags);
2388 if ((cmd->transport_state & CMD_T_BUSY) &&
2389 (cmd->transport_state & CMD_T_SENT)) {
2390 if (!target_stop_cmd(cmd, &flags))
2391 ret++;
2392 }
2393 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2394
2395 pr_debug("ConfigFS: cmd: %p stop tasks ret:"
2396 " %d\n", cmd, ret);
2397 if (!ret) {
2398 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
2399 cmd->se_tfo->get_task_tag(cmd));
2400 wait_for_completion(&cmd->transport_lun_stop_comp);
2401 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
2402 cmd->se_tfo->get_task_tag(cmd));
2403 }
2404
2405 return 0;
2406 }
2407
2408 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
2409 {
2410 struct se_cmd *cmd = NULL;
2411 unsigned long lun_flags, cmd_flags;
2412 /*
2413 * Do exception processing and return CHECK_CONDITION status to the
2414 * Initiator Port.
2415 */
2416 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2417 while (!list_empty(&lun->lun_cmd_list)) {
2418 cmd = list_first_entry(&lun->lun_cmd_list,
2419 struct se_cmd, se_lun_node);
2420 list_del_init(&cmd->se_lun_node);
2421
2422 spin_lock(&cmd->t_state_lock);
2423 pr_debug("SE_LUN[%d] - Setting cmd->transport"
2424 "_lun_stop for ITT: 0x%08x\n",
2425 cmd->se_lun->unpacked_lun,
2426 cmd->se_tfo->get_task_tag(cmd));
2427 cmd->transport_state |= CMD_T_LUN_STOP;
2428 spin_unlock(&cmd->t_state_lock);
2429
2430 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2431
2432 if (!cmd->se_lun) {
2433 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
2434 cmd->se_tfo->get_task_tag(cmd),
2435 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
2436 BUG();
2437 }
2438 /*
2439 * If the Storage engine still owns the iscsi_cmd_t, determine
2440 * and/or stop its context.
2441 */
2442 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
2443 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
2444 cmd->se_tfo->get_task_tag(cmd));
2445
2446 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
2447 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2448 continue;
2449 }
2450
2451 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
2452 "_wait_for_tasks(): SUCCESS\n",
2453 cmd->se_lun->unpacked_lun,
2454 cmd->se_tfo->get_task_tag(cmd));
2455
2456 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
2457 if (!(cmd->transport_state & CMD_T_DEV_ACTIVE)) {
2458 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
2459 goto check_cond;
2460 }
2461 cmd->transport_state &= ~CMD_T_DEV_ACTIVE;
2462 target_remove_from_state_list(cmd);
2463 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
2464
2465 /*
2466 * The Storage engine stopped this struct se_cmd before it was
2467 * send to the fabric frontend for delivery back to the
2468 * Initiator Node. Return this SCSI CDB back with an
2469 * CHECK_CONDITION status.
2470 */
2471 check_cond:
2472 transport_send_check_condition_and_sense(cmd,
2473 TCM_NON_EXISTENT_LUN, 0);
2474 /*
2475 * If the fabric frontend is waiting for this iscsi_cmd_t to
2476 * be released, notify the waiting thread now that LU has
2477 * finished accessing it.
2478 */
2479 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
2480 if (cmd->transport_state & CMD_T_LUN_FE_STOP) {
2481 pr_debug("SE_LUN[%d] - Detected FE stop for"
2482 " struct se_cmd: %p ITT: 0x%08x\n",
2483 lun->unpacked_lun,
2484 cmd, cmd->se_tfo->get_task_tag(cmd));
2485
2486 spin_unlock_irqrestore(&cmd->t_state_lock,
2487 cmd_flags);
2488 transport_cmd_check_stop(cmd, false);
2489 complete(&cmd->transport_lun_fe_stop_comp);
2490 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2491 continue;
2492 }
2493 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
2494 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
2495
2496 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
2497 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
2498 }
2499 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
2500 }
2501
2502 static int transport_clear_lun_thread(void *p)
2503 {
2504 struct se_lun *lun = p;
2505
2506 __transport_clear_lun_from_sessions(lun);
2507 complete(&lun->lun_shutdown_comp);
2508
2509 return 0;
2510 }
2511
2512 int transport_clear_lun_from_sessions(struct se_lun *lun)
2513 {
2514 struct task_struct *kt;
2515
2516 kt = kthread_run(transport_clear_lun_thread, lun,
2517 "tcm_cl_%u", lun->unpacked_lun);
2518 if (IS_ERR(kt)) {
2519 pr_err("Unable to start clear_lun thread\n");
2520 return PTR_ERR(kt);
2521 }
2522 wait_for_completion(&lun->lun_shutdown_comp);
2523
2524 return 0;
2525 }
2526
2527 /**
2528 * transport_wait_for_tasks - wait for completion to occur
2529 * @cmd: command to wait
2530 *
2531 * Called from frontend fabric context to wait for storage engine
2532 * to pause and/or release frontend generated struct se_cmd.
2533 */
2534 bool transport_wait_for_tasks(struct se_cmd *cmd)
2535 {
2536 unsigned long flags;
2537
2538 spin_lock_irqsave(&cmd->t_state_lock, flags);
2539 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) &&
2540 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
2541 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2542 return false;
2543 }
2544
2545 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) &&
2546 !(cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
2547 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2548 return false;
2549 }
2550 /*
2551 * If we are already stopped due to an external event (ie: LUN shutdown)
2552 * sleep until the connection can have the passed struct se_cmd back.
2553 * The cmd->transport_lun_stopped_sem will be upped by
2554 * transport_clear_lun_from_sessions() once the ConfigFS context caller
2555 * has completed its operation on the struct se_cmd.
2556 */
2557 if (cmd->transport_state & CMD_T_LUN_STOP) {
2558 pr_debug("wait_for_tasks: Stopping"
2559 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
2560 "_stop_comp); for ITT: 0x%08x\n",
2561 cmd->se_tfo->get_task_tag(cmd));
2562 /*
2563 * There is a special case for WRITES where a FE exception +
2564 * LUN shutdown means ConfigFS context is still sleeping on
2565 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
2566 * We go ahead and up transport_lun_stop_comp just to be sure
2567 * here.
2568 */
2569 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2570 complete(&cmd->transport_lun_stop_comp);
2571 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
2572 spin_lock_irqsave(&cmd->t_state_lock, flags);
2573
2574 target_remove_from_state_list(cmd);
2575 /*
2576 * At this point, the frontend who was the originator of this
2577 * struct se_cmd, now owns the structure and can be released through
2578 * normal means below.
2579 */
2580 pr_debug("wait_for_tasks: Stopped"
2581 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
2582 "stop_comp); for ITT: 0x%08x\n",
2583 cmd->se_tfo->get_task_tag(cmd));
2584
2585 cmd->transport_state &= ~CMD_T_LUN_STOP;
2586 }
2587
2588 if (!(cmd->transport_state & CMD_T_ACTIVE)) {
2589 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2590 return false;
2591 }
2592
2593 cmd->transport_state |= CMD_T_STOP;
2594
2595 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
2596 " i_state: %d, t_state: %d, CMD_T_STOP\n",
2597 cmd, cmd->se_tfo->get_task_tag(cmd),
2598 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
2599
2600 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2601
2602 wait_for_completion(&cmd->t_transport_stop_comp);
2603
2604 spin_lock_irqsave(&cmd->t_state_lock, flags);
2605 cmd->transport_state &= ~(CMD_T_ACTIVE | CMD_T_STOP);
2606
2607 pr_debug("wait_for_tasks: Stopped wait_for_completion("
2608 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
2609 cmd->se_tfo->get_task_tag(cmd));
2610
2611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2612
2613 return true;
2614 }
2615 EXPORT_SYMBOL(transport_wait_for_tasks);
2616
2617 static int transport_get_sense_codes(
2618 struct se_cmd *cmd,
2619 u8 *asc,
2620 u8 *ascq)
2621 {
2622 *asc = cmd->scsi_asc;
2623 *ascq = cmd->scsi_ascq;
2624
2625 return 0;
2626 }
2627
2628 int transport_send_check_condition_and_sense(
2629 struct se_cmd *cmd,
2630 u8 reason,
2631 int from_transport)
2632 {
2633 unsigned char *buffer = cmd->sense_buffer;
2634 unsigned long flags;
2635 u8 asc = 0, ascq = 0;
2636
2637 spin_lock_irqsave(&cmd->t_state_lock, flags);
2638 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2639 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2640 return 0;
2641 }
2642 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
2643 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2644
2645 if (!reason && from_transport)
2646 goto after_reason;
2647
2648 if (!from_transport)
2649 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
2650
2651 /*
2652 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
2653 * SENSE KEY values from include/scsi/scsi.h
2654 */
2655 switch (reason) {
2656 case TCM_NON_EXISTENT_LUN:
2657 /* CURRENT ERROR */
2658 buffer[0] = 0x70;
2659 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2660 /* ILLEGAL REQUEST */
2661 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2662 /* LOGICAL UNIT NOT SUPPORTED */
2663 buffer[SPC_ASC_KEY_OFFSET] = 0x25;
2664 break;
2665 case TCM_UNSUPPORTED_SCSI_OPCODE:
2666 case TCM_SECTOR_COUNT_TOO_MANY:
2667 /* CURRENT ERROR */
2668 buffer[0] = 0x70;
2669 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2670 /* ILLEGAL REQUEST */
2671 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2672 /* INVALID COMMAND OPERATION CODE */
2673 buffer[SPC_ASC_KEY_OFFSET] = 0x20;
2674 break;
2675 case TCM_UNKNOWN_MODE_PAGE:
2676 /* CURRENT ERROR */
2677 buffer[0] = 0x70;
2678 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2679 /* ILLEGAL REQUEST */
2680 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2681 /* INVALID FIELD IN CDB */
2682 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
2683 break;
2684 case TCM_CHECK_CONDITION_ABORT_CMD:
2685 /* CURRENT ERROR */
2686 buffer[0] = 0x70;
2687 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2688 /* ABORTED COMMAND */
2689 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2690 /* BUS DEVICE RESET FUNCTION OCCURRED */
2691 buffer[SPC_ASC_KEY_OFFSET] = 0x29;
2692 buffer[SPC_ASCQ_KEY_OFFSET] = 0x03;
2693 break;
2694 case TCM_INCORRECT_AMOUNT_OF_DATA:
2695 /* CURRENT ERROR */
2696 buffer[0] = 0x70;
2697 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2698 /* ABORTED COMMAND */
2699 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2700 /* WRITE ERROR */
2701 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
2702 /* NOT ENOUGH UNSOLICITED DATA */
2703 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0d;
2704 break;
2705 case TCM_INVALID_CDB_FIELD:
2706 /* CURRENT ERROR */
2707 buffer[0] = 0x70;
2708 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2709 /* ILLEGAL REQUEST */
2710 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2711 /* INVALID FIELD IN CDB */
2712 buffer[SPC_ASC_KEY_OFFSET] = 0x24;
2713 break;
2714 case TCM_INVALID_PARAMETER_LIST:
2715 /* CURRENT ERROR */
2716 buffer[0] = 0x70;
2717 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2718 /* ILLEGAL REQUEST */
2719 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2720 /* INVALID FIELD IN PARAMETER LIST */
2721 buffer[SPC_ASC_KEY_OFFSET] = 0x26;
2722 break;
2723 case TCM_UNEXPECTED_UNSOLICITED_DATA:
2724 /* CURRENT ERROR */
2725 buffer[0] = 0x70;
2726 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2727 /* ABORTED COMMAND */
2728 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2729 /* WRITE ERROR */
2730 buffer[SPC_ASC_KEY_OFFSET] = 0x0c;
2731 /* UNEXPECTED_UNSOLICITED_DATA */
2732 buffer[SPC_ASCQ_KEY_OFFSET] = 0x0c;
2733 break;
2734 case TCM_SERVICE_CRC_ERROR:
2735 /* CURRENT ERROR */
2736 buffer[0] = 0x70;
2737 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2738 /* ABORTED COMMAND */
2739 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2740 /* PROTOCOL SERVICE CRC ERROR */
2741 buffer[SPC_ASC_KEY_OFFSET] = 0x47;
2742 /* N/A */
2743 buffer[SPC_ASCQ_KEY_OFFSET] = 0x05;
2744 break;
2745 case TCM_SNACK_REJECTED:
2746 /* CURRENT ERROR */
2747 buffer[0] = 0x70;
2748 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2749 /* ABORTED COMMAND */
2750 buffer[SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
2751 /* READ ERROR */
2752 buffer[SPC_ASC_KEY_OFFSET] = 0x11;
2753 /* FAILED RETRANSMISSION REQUEST */
2754 buffer[SPC_ASCQ_KEY_OFFSET] = 0x13;
2755 break;
2756 case TCM_WRITE_PROTECTED:
2757 /* CURRENT ERROR */
2758 buffer[0] = 0x70;
2759 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2760 /* DATA PROTECT */
2761 buffer[SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
2762 /* WRITE PROTECTED */
2763 buffer[SPC_ASC_KEY_OFFSET] = 0x27;
2764 break;
2765 case TCM_ADDRESS_OUT_OF_RANGE:
2766 /* CURRENT ERROR */
2767 buffer[0] = 0x70;
2768 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2769 /* ILLEGAL REQUEST */
2770 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2771 /* LOGICAL BLOCK ADDRESS OUT OF RANGE */
2772 buffer[SPC_ASC_KEY_OFFSET] = 0x21;
2773 break;
2774 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
2775 /* CURRENT ERROR */
2776 buffer[0] = 0x70;
2777 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2778 /* UNIT ATTENTION */
2779 buffer[SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
2780 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
2781 buffer[SPC_ASC_KEY_OFFSET] = asc;
2782 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
2783 break;
2784 case TCM_CHECK_CONDITION_NOT_READY:
2785 /* CURRENT ERROR */
2786 buffer[0] = 0x70;
2787 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2788 /* Not Ready */
2789 buffer[SPC_SENSE_KEY_OFFSET] = NOT_READY;
2790 transport_get_sense_codes(cmd, &asc, &ascq);
2791 buffer[SPC_ASC_KEY_OFFSET] = asc;
2792 buffer[SPC_ASCQ_KEY_OFFSET] = ascq;
2793 break;
2794 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
2795 default:
2796 /* CURRENT ERROR */
2797 buffer[0] = 0x70;
2798 buffer[SPC_ADD_SENSE_LEN_OFFSET] = 10;
2799 /* ILLEGAL REQUEST */
2800 buffer[SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
2801 /* LOGICAL UNIT COMMUNICATION FAILURE */
2802 buffer[SPC_ASC_KEY_OFFSET] = 0x80;
2803 break;
2804 }
2805 /*
2806 * This code uses linux/include/scsi/scsi.h SAM status codes!
2807 */
2808 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
2809 /*
2810 * Automatically padded, this value is encoded in the fabric's
2811 * data_length response PDU containing the SCSI defined sense data.
2812 */
2813 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER;
2814
2815 after_reason:
2816 return cmd->se_tfo->queue_status(cmd);
2817 }
2818 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
2819
2820 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
2821 {
2822 int ret = 0;
2823
2824 if (cmd->transport_state & CMD_T_ABORTED) {
2825 if (!send_status ||
2826 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
2827 return 1;
2828
2829 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
2830 " status for CDB: 0x%02x ITT: 0x%08x\n",
2831 cmd->t_task_cdb[0],
2832 cmd->se_tfo->get_task_tag(cmd));
2833
2834 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
2835 cmd->se_tfo->queue_status(cmd);
2836 ret = 1;
2837 }
2838 return ret;
2839 }
2840 EXPORT_SYMBOL(transport_check_aborted_status);
2841
2842 void transport_send_task_abort(struct se_cmd *cmd)
2843 {
2844 unsigned long flags;
2845
2846 spin_lock_irqsave(&cmd->t_state_lock, flags);
2847 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2848 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2849 return;
2850 }
2851 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2852
2853 /*
2854 * If there are still expected incoming fabric WRITEs, we wait
2855 * until until they have completed before sending a TASK_ABORTED
2856 * response. This response with TASK_ABORTED status will be
2857 * queued back to fabric module by transport_check_aborted_status().
2858 */
2859 if (cmd->data_direction == DMA_TO_DEVICE) {
2860 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
2861 cmd->transport_state |= CMD_T_ABORTED;
2862 smp_mb__after_atomic_inc();
2863 }
2864 }
2865 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
2866
2867 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
2868 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
2869 cmd->se_tfo->get_task_tag(cmd));
2870
2871 cmd->se_tfo->queue_status(cmd);
2872 }
2873
2874 static void target_tmr_work(struct work_struct *work)
2875 {
2876 struct se_cmd *cmd = container_of(work, struct se_cmd, work);
2877 struct se_device *dev = cmd->se_dev;
2878 struct se_tmr_req *tmr = cmd->se_tmr_req;
2879 int ret;
2880
2881 switch (tmr->function) {
2882 case TMR_ABORT_TASK:
2883 core_tmr_abort_task(dev, tmr, cmd->se_sess);
2884 break;
2885 case TMR_ABORT_TASK_SET:
2886 case TMR_CLEAR_ACA:
2887 case TMR_CLEAR_TASK_SET:
2888 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2889 break;
2890 case TMR_LUN_RESET:
2891 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
2892 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
2893 TMR_FUNCTION_REJECTED;
2894 break;
2895 case TMR_TARGET_WARM_RESET:
2896 tmr->response = TMR_FUNCTION_REJECTED;
2897 break;
2898 case TMR_TARGET_COLD_RESET:
2899 tmr->response = TMR_FUNCTION_REJECTED;
2900 break;
2901 default:
2902 pr_err("Uknown TMR function: 0x%02x.\n",
2903 tmr->function);
2904 tmr->response = TMR_FUNCTION_REJECTED;
2905 break;
2906 }
2907
2908 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
2909 cmd->se_tfo->queue_tm_rsp(cmd);
2910
2911 transport_cmd_check_stop_to_fabric(cmd);
2912 }
2913
2914 int transport_generic_handle_tmr(
2915 struct se_cmd *cmd)
2916 {
2917 INIT_WORK(&cmd->work, target_tmr_work);
2918 queue_work(cmd->se_dev->tmr_wq, &cmd->work);
2919 return 0;
2920 }
2921 EXPORT_SYMBOL(transport_generic_handle_tmr);
This page took 0.126893 seconds and 5 git commands to generate.