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