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