4ce205040b154f7b95f240ab13a91283db9fae9b
[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 <asm/unaligned.h>
40 #include <net/sock.h>
41 #include <net/tcp.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_tcq.h>
45
46 #include <target/target_core_base.h>
47 #include <target/target_core_device.h>
48 #include <target/target_core_tmr.h>
49 #include <target/target_core_tpg.h>
50 #include <target/target_core_transport.h>
51 #include <target/target_core_fabric_ops.h>
52 #include <target/target_core_configfs.h>
53
54 #include "target_core_alua.h"
55 #include "target_core_hba.h"
56 #include "target_core_pr.h"
57 #include "target_core_ua.h"
58
59 static int sub_api_initialized;
60
61 static struct kmem_cache *se_cmd_cache;
62 static struct kmem_cache *se_sess_cache;
63 struct kmem_cache *se_tmr_req_cache;
64 struct kmem_cache *se_ua_cache;
65 struct kmem_cache *t10_pr_reg_cache;
66 struct kmem_cache *t10_alua_lu_gp_cache;
67 struct kmem_cache *t10_alua_lu_gp_mem_cache;
68 struct kmem_cache *t10_alua_tg_pt_gp_cache;
69 struct kmem_cache *t10_alua_tg_pt_gp_mem_cache;
70
71 static int transport_generic_write_pending(struct se_cmd *);
72 static int transport_processing_thread(void *param);
73 static int __transport_execute_tasks(struct se_device *dev);
74 static void transport_complete_task_attr(struct se_cmd *cmd);
75 static void transport_handle_queue_full(struct se_cmd *cmd,
76 struct se_device *dev);
77 static void transport_direct_request_timeout(struct se_cmd *cmd);
78 static void transport_free_dev_tasks(struct se_cmd *cmd);
79 static u32 transport_allocate_tasks(struct se_cmd *cmd,
80 unsigned long long starting_lba,
81 enum dma_data_direction data_direction,
82 struct scatterlist *sgl, unsigned int nents);
83 static int transport_generic_get_mem(struct se_cmd *cmd);
84 static void transport_put_cmd(struct se_cmd *cmd);
85 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
86 struct se_queue_obj *qobj);
87 static int transport_set_sense_codes(struct se_cmd *cmd, u8 asc, u8 ascq);
88 static void transport_stop_all_task_timers(struct se_cmd *cmd);
89
90 int init_se_kmem_caches(void)
91 {
92 se_cmd_cache = kmem_cache_create("se_cmd_cache",
93 sizeof(struct se_cmd), __alignof__(struct se_cmd), 0, NULL);
94 if (!se_cmd_cache) {
95 pr_err("kmem_cache_create for struct se_cmd failed\n");
96 goto out;
97 }
98 se_tmr_req_cache = kmem_cache_create("se_tmr_cache",
99 sizeof(struct se_tmr_req), __alignof__(struct se_tmr_req),
100 0, NULL);
101 if (!se_tmr_req_cache) {
102 pr_err("kmem_cache_create() for struct se_tmr_req"
103 " failed\n");
104 goto out;
105 }
106 se_sess_cache = kmem_cache_create("se_sess_cache",
107 sizeof(struct se_session), __alignof__(struct se_session),
108 0, NULL);
109 if (!se_sess_cache) {
110 pr_err("kmem_cache_create() for struct se_session"
111 " failed\n");
112 goto out;
113 }
114 se_ua_cache = kmem_cache_create("se_ua_cache",
115 sizeof(struct se_ua), __alignof__(struct se_ua),
116 0, NULL);
117 if (!se_ua_cache) {
118 pr_err("kmem_cache_create() for struct se_ua failed\n");
119 goto out;
120 }
121 t10_pr_reg_cache = kmem_cache_create("t10_pr_reg_cache",
122 sizeof(struct t10_pr_registration),
123 __alignof__(struct t10_pr_registration), 0, NULL);
124 if (!t10_pr_reg_cache) {
125 pr_err("kmem_cache_create() for struct t10_pr_registration"
126 " failed\n");
127 goto out;
128 }
129 t10_alua_lu_gp_cache = kmem_cache_create("t10_alua_lu_gp_cache",
130 sizeof(struct t10_alua_lu_gp), __alignof__(struct t10_alua_lu_gp),
131 0, NULL);
132 if (!t10_alua_lu_gp_cache) {
133 pr_err("kmem_cache_create() for t10_alua_lu_gp_cache"
134 " failed\n");
135 goto out;
136 }
137 t10_alua_lu_gp_mem_cache = kmem_cache_create("t10_alua_lu_gp_mem_cache",
138 sizeof(struct t10_alua_lu_gp_member),
139 __alignof__(struct t10_alua_lu_gp_member), 0, NULL);
140 if (!t10_alua_lu_gp_mem_cache) {
141 pr_err("kmem_cache_create() for t10_alua_lu_gp_mem_"
142 "cache failed\n");
143 goto out;
144 }
145 t10_alua_tg_pt_gp_cache = kmem_cache_create("t10_alua_tg_pt_gp_cache",
146 sizeof(struct t10_alua_tg_pt_gp),
147 __alignof__(struct t10_alua_tg_pt_gp), 0, NULL);
148 if (!t10_alua_tg_pt_gp_cache) {
149 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
150 "cache failed\n");
151 goto out;
152 }
153 t10_alua_tg_pt_gp_mem_cache = kmem_cache_create(
154 "t10_alua_tg_pt_gp_mem_cache",
155 sizeof(struct t10_alua_tg_pt_gp_member),
156 __alignof__(struct t10_alua_tg_pt_gp_member),
157 0, NULL);
158 if (!t10_alua_tg_pt_gp_mem_cache) {
159 pr_err("kmem_cache_create() for t10_alua_tg_pt_gp_"
160 "mem_t failed\n");
161 goto out;
162 }
163
164 return 0;
165 out:
166 if (se_cmd_cache)
167 kmem_cache_destroy(se_cmd_cache);
168 if (se_tmr_req_cache)
169 kmem_cache_destroy(se_tmr_req_cache);
170 if (se_sess_cache)
171 kmem_cache_destroy(se_sess_cache);
172 if (se_ua_cache)
173 kmem_cache_destroy(se_ua_cache);
174 if (t10_pr_reg_cache)
175 kmem_cache_destroy(t10_pr_reg_cache);
176 if (t10_alua_lu_gp_cache)
177 kmem_cache_destroy(t10_alua_lu_gp_cache);
178 if (t10_alua_lu_gp_mem_cache)
179 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
180 if (t10_alua_tg_pt_gp_cache)
181 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
182 if (t10_alua_tg_pt_gp_mem_cache)
183 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
184 return -ENOMEM;
185 }
186
187 void release_se_kmem_caches(void)
188 {
189 kmem_cache_destroy(se_cmd_cache);
190 kmem_cache_destroy(se_tmr_req_cache);
191 kmem_cache_destroy(se_sess_cache);
192 kmem_cache_destroy(se_ua_cache);
193 kmem_cache_destroy(t10_pr_reg_cache);
194 kmem_cache_destroy(t10_alua_lu_gp_cache);
195 kmem_cache_destroy(t10_alua_lu_gp_mem_cache);
196 kmem_cache_destroy(t10_alua_tg_pt_gp_cache);
197 kmem_cache_destroy(t10_alua_tg_pt_gp_mem_cache);
198 }
199
200 /* This code ensures unique mib indexes are handed out. */
201 static DEFINE_SPINLOCK(scsi_mib_index_lock);
202 static u32 scsi_mib_index[SCSI_INDEX_TYPE_MAX];
203
204 /*
205 * Allocate a new row index for the entry type specified
206 */
207 u32 scsi_get_new_index(scsi_index_t type)
208 {
209 u32 new_index;
210
211 BUG_ON((type < 0) || (type >= SCSI_INDEX_TYPE_MAX));
212
213 spin_lock(&scsi_mib_index_lock);
214 new_index = ++scsi_mib_index[type];
215 spin_unlock(&scsi_mib_index_lock);
216
217 return new_index;
218 }
219
220 void transport_init_queue_obj(struct se_queue_obj *qobj)
221 {
222 atomic_set(&qobj->queue_cnt, 0);
223 INIT_LIST_HEAD(&qobj->qobj_list);
224 init_waitqueue_head(&qobj->thread_wq);
225 spin_lock_init(&qobj->cmd_queue_lock);
226 }
227 EXPORT_SYMBOL(transport_init_queue_obj);
228
229 static int transport_subsystem_reqmods(void)
230 {
231 int ret;
232
233 ret = request_module("target_core_iblock");
234 if (ret != 0)
235 pr_err("Unable to load target_core_iblock\n");
236
237 ret = request_module("target_core_file");
238 if (ret != 0)
239 pr_err("Unable to load target_core_file\n");
240
241 ret = request_module("target_core_pscsi");
242 if (ret != 0)
243 pr_err("Unable to load target_core_pscsi\n");
244
245 ret = request_module("target_core_stgt");
246 if (ret != 0)
247 pr_err("Unable to load target_core_stgt\n");
248
249 return 0;
250 }
251
252 int transport_subsystem_check_init(void)
253 {
254 int ret;
255
256 if (sub_api_initialized)
257 return 0;
258 /*
259 * Request the loading of known TCM subsystem plugins..
260 */
261 ret = transport_subsystem_reqmods();
262 if (ret < 0)
263 return ret;
264
265 sub_api_initialized = 1;
266 return 0;
267 }
268
269 struct se_session *transport_init_session(void)
270 {
271 struct se_session *se_sess;
272
273 se_sess = kmem_cache_zalloc(se_sess_cache, GFP_KERNEL);
274 if (!se_sess) {
275 pr_err("Unable to allocate struct se_session from"
276 " se_sess_cache\n");
277 return ERR_PTR(-ENOMEM);
278 }
279 INIT_LIST_HEAD(&se_sess->sess_list);
280 INIT_LIST_HEAD(&se_sess->sess_acl_list);
281
282 return se_sess;
283 }
284 EXPORT_SYMBOL(transport_init_session);
285
286 /*
287 * Called with spin_lock_bh(&struct se_portal_group->session_lock called.
288 */
289 void __transport_register_session(
290 struct se_portal_group *se_tpg,
291 struct se_node_acl *se_nacl,
292 struct se_session *se_sess,
293 void *fabric_sess_ptr)
294 {
295 unsigned char buf[PR_REG_ISID_LEN];
296
297 se_sess->se_tpg = se_tpg;
298 se_sess->fabric_sess_ptr = fabric_sess_ptr;
299 /*
300 * Used by struct se_node_acl's under ConfigFS to locate active se_session-t
301 *
302 * Only set for struct se_session's that will actually be moving I/O.
303 * eg: *NOT* discovery sessions.
304 */
305 if (se_nacl) {
306 /*
307 * If the fabric module supports an ISID based TransportID,
308 * save this value in binary from the fabric I_T Nexus now.
309 */
310 if (se_tpg->se_tpg_tfo->sess_get_initiator_sid != NULL) {
311 memset(&buf[0], 0, PR_REG_ISID_LEN);
312 se_tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess,
313 &buf[0], PR_REG_ISID_LEN);
314 se_sess->sess_bin_isid = get_unaligned_be64(&buf[0]);
315 }
316 spin_lock_irq(&se_nacl->nacl_sess_lock);
317 /*
318 * The se_nacl->nacl_sess pointer will be set to the
319 * last active I_T Nexus for each struct se_node_acl.
320 */
321 se_nacl->nacl_sess = se_sess;
322
323 list_add_tail(&se_sess->sess_acl_list,
324 &se_nacl->acl_sess_list);
325 spin_unlock_irq(&se_nacl->nacl_sess_lock);
326 }
327 list_add_tail(&se_sess->sess_list, &se_tpg->tpg_sess_list);
328
329 pr_debug("TARGET_CORE[%s]: Registered fabric_sess_ptr: %p\n",
330 se_tpg->se_tpg_tfo->get_fabric_name(), se_sess->fabric_sess_ptr);
331 }
332 EXPORT_SYMBOL(__transport_register_session);
333
334 void transport_register_session(
335 struct se_portal_group *se_tpg,
336 struct se_node_acl *se_nacl,
337 struct se_session *se_sess,
338 void *fabric_sess_ptr)
339 {
340 spin_lock_bh(&se_tpg->session_lock);
341 __transport_register_session(se_tpg, se_nacl, se_sess, fabric_sess_ptr);
342 spin_unlock_bh(&se_tpg->session_lock);
343 }
344 EXPORT_SYMBOL(transport_register_session);
345
346 void transport_deregister_session_configfs(struct se_session *se_sess)
347 {
348 struct se_node_acl *se_nacl;
349 unsigned long flags;
350 /*
351 * Used by struct se_node_acl's under ConfigFS to locate active struct se_session
352 */
353 se_nacl = se_sess->se_node_acl;
354 if (se_nacl) {
355 spin_lock_irqsave(&se_nacl->nacl_sess_lock, flags);
356 list_del(&se_sess->sess_acl_list);
357 /*
358 * If the session list is empty, then clear the pointer.
359 * Otherwise, set the struct se_session pointer from the tail
360 * element of the per struct se_node_acl active session list.
361 */
362 if (list_empty(&se_nacl->acl_sess_list))
363 se_nacl->nacl_sess = NULL;
364 else {
365 se_nacl->nacl_sess = container_of(
366 se_nacl->acl_sess_list.prev,
367 struct se_session, sess_acl_list);
368 }
369 spin_unlock_irqrestore(&se_nacl->nacl_sess_lock, flags);
370 }
371 }
372 EXPORT_SYMBOL(transport_deregister_session_configfs);
373
374 void transport_free_session(struct se_session *se_sess)
375 {
376 kmem_cache_free(se_sess_cache, se_sess);
377 }
378 EXPORT_SYMBOL(transport_free_session);
379
380 void transport_deregister_session(struct se_session *se_sess)
381 {
382 struct se_portal_group *se_tpg = se_sess->se_tpg;
383 struct se_node_acl *se_nacl;
384 unsigned long flags;
385
386 if (!se_tpg) {
387 transport_free_session(se_sess);
388 return;
389 }
390
391 spin_lock_irqsave(&se_tpg->session_lock, flags);
392 list_del(&se_sess->sess_list);
393 se_sess->se_tpg = NULL;
394 se_sess->fabric_sess_ptr = NULL;
395 spin_unlock_irqrestore(&se_tpg->session_lock, flags);
396
397 /*
398 * Determine if we need to do extra work for this initiator node's
399 * struct se_node_acl if it had been previously dynamically generated.
400 */
401 se_nacl = se_sess->se_node_acl;
402 if (se_nacl) {
403 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
404 if (se_nacl->dynamic_node_acl) {
405 if (!se_tpg->se_tpg_tfo->tpg_check_demo_mode_cache(
406 se_tpg)) {
407 list_del(&se_nacl->acl_list);
408 se_tpg->num_node_acls--;
409 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
410
411 core_tpg_wait_for_nacl_pr_ref(se_nacl);
412 core_free_device_list_for_node(se_nacl, se_tpg);
413 se_tpg->se_tpg_tfo->tpg_release_fabric_acl(se_tpg,
414 se_nacl);
415 spin_lock_irqsave(&se_tpg->acl_node_lock, flags);
416 }
417 }
418 spin_unlock_irqrestore(&se_tpg->acl_node_lock, flags);
419 }
420
421 transport_free_session(se_sess);
422
423 pr_debug("TARGET_CORE[%s]: Deregistered fabric_sess\n",
424 se_tpg->se_tpg_tfo->get_fabric_name());
425 }
426 EXPORT_SYMBOL(transport_deregister_session);
427
428 /*
429 * Called with cmd->t_state_lock held.
430 */
431 static void transport_all_task_dev_remove_state(struct se_cmd *cmd)
432 {
433 struct se_device *dev = cmd->se_dev;
434 struct se_task *task;
435 unsigned long flags;
436
437 if (!dev)
438 return;
439
440 list_for_each_entry(task, &cmd->t_task_list, t_list) {
441 if (task->task_flags & TF_ACTIVE)
442 continue;
443
444 if (!atomic_read(&task->task_state_active))
445 continue;
446
447 spin_lock_irqsave(&dev->execute_task_lock, flags);
448 list_del(&task->t_state_list);
449 pr_debug("Removed ITT: 0x%08x dev: %p task[%p]\n",
450 cmd->se_tfo->get_task_tag(cmd), dev, task);
451 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
452
453 atomic_set(&task->task_state_active, 0);
454 atomic_dec(&cmd->t_task_cdbs_ex_left);
455 }
456 }
457
458 /* transport_cmd_check_stop():
459 *
460 * 'transport_off = 1' determines if t_transport_active should be cleared.
461 * 'transport_off = 2' determines if task_dev_state should be removed.
462 *
463 * A non-zero u8 t_state sets cmd->t_state.
464 * Returns 1 when command is stopped, else 0.
465 */
466 static int transport_cmd_check_stop(
467 struct se_cmd *cmd,
468 int transport_off,
469 u8 t_state)
470 {
471 unsigned long flags;
472
473 spin_lock_irqsave(&cmd->t_state_lock, flags);
474 /*
475 * Determine if IOCTL context caller in requesting the stopping of this
476 * command for LUN shutdown purposes.
477 */
478 if (atomic_read(&cmd->transport_lun_stop)) {
479 pr_debug("%s:%d atomic_read(&cmd->transport_lun_stop)"
480 " == TRUE for ITT: 0x%08x\n", __func__, __LINE__,
481 cmd->se_tfo->get_task_tag(cmd));
482
483 cmd->deferred_t_state = cmd->t_state;
484 cmd->t_state = TRANSPORT_DEFERRED_CMD;
485 atomic_set(&cmd->t_transport_active, 0);
486 if (transport_off == 2)
487 transport_all_task_dev_remove_state(cmd);
488 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
489
490 complete(&cmd->transport_lun_stop_comp);
491 return 1;
492 }
493 /*
494 * Determine if frontend context caller is requesting the stopping of
495 * this command for frontend exceptions.
496 */
497 if (atomic_read(&cmd->t_transport_stop)) {
498 pr_debug("%s:%d atomic_read(&cmd->t_transport_stop) =="
499 " TRUE for ITT: 0x%08x\n", __func__, __LINE__,
500 cmd->se_tfo->get_task_tag(cmd));
501
502 cmd->deferred_t_state = cmd->t_state;
503 cmd->t_state = TRANSPORT_DEFERRED_CMD;
504 if (transport_off == 2)
505 transport_all_task_dev_remove_state(cmd);
506
507 /*
508 * Clear struct se_cmd->se_lun before the transport_off == 2 handoff
509 * to FE.
510 */
511 if (transport_off == 2)
512 cmd->se_lun = NULL;
513 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
514
515 complete(&cmd->t_transport_stop_comp);
516 return 1;
517 }
518 if (transport_off) {
519 atomic_set(&cmd->t_transport_active, 0);
520 if (transport_off == 2) {
521 transport_all_task_dev_remove_state(cmd);
522 /*
523 * Clear struct se_cmd->se_lun before the transport_off == 2
524 * handoff to fabric module.
525 */
526 cmd->se_lun = NULL;
527 /*
528 * Some fabric modules like tcm_loop can release
529 * their internally allocated I/O reference now and
530 * struct se_cmd now.
531 */
532 if (cmd->se_tfo->check_stop_free != NULL) {
533 spin_unlock_irqrestore(
534 &cmd->t_state_lock, flags);
535
536 cmd->se_tfo->check_stop_free(cmd);
537 return 1;
538 }
539 }
540 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
541
542 return 0;
543 } else if (t_state)
544 cmd->t_state = t_state;
545 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
546
547 return 0;
548 }
549
550 static int transport_cmd_check_stop_to_fabric(struct se_cmd *cmd)
551 {
552 return transport_cmd_check_stop(cmd, 2, 0);
553 }
554
555 static void transport_lun_remove_cmd(struct se_cmd *cmd)
556 {
557 struct se_lun *lun = cmd->se_lun;
558 unsigned long flags;
559
560 if (!lun)
561 return;
562
563 spin_lock_irqsave(&cmd->t_state_lock, flags);
564 if (!atomic_read(&cmd->transport_dev_active)) {
565 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
566 goto check_lun;
567 }
568 atomic_set(&cmd->transport_dev_active, 0);
569 transport_all_task_dev_remove_state(cmd);
570 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
571
572
573 check_lun:
574 spin_lock_irqsave(&lun->lun_cmd_lock, flags);
575 if (atomic_read(&cmd->transport_lun_active)) {
576 list_del(&cmd->se_lun_node);
577 atomic_set(&cmd->transport_lun_active, 0);
578 #if 0
579 pr_debug("Removed ITT: 0x%08x from LUN LIST[%d]\n"
580 cmd->se_tfo->get_task_tag(cmd), lun->unpacked_lun);
581 #endif
582 }
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_tmr_req)
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, &cmd->se_dev->dev_queue_obj);
595 transport_put_cmd(cmd);
596 }
597 }
598
599 static void transport_add_cmd_to_queue(
600 struct se_cmd *cmd,
601 int t_state)
602 {
603 struct se_device *dev = cmd->se_dev;
604 struct se_queue_obj *qobj = &dev->dev_queue_obj;
605 unsigned long flags;
606
607 if (t_state) {
608 spin_lock_irqsave(&cmd->t_state_lock, flags);
609 cmd->t_state = t_state;
610 atomic_set(&cmd->t_transport_active, 1);
611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
612 }
613
614 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
615
616 /* If the cmd is already on the list, remove it before we add it */
617 if (!list_empty(&cmd->se_queue_node))
618 list_del(&cmd->se_queue_node);
619 else
620 atomic_inc(&qobj->queue_cnt);
621
622 if (cmd->se_cmd_flags & SCF_EMULATE_QUEUE_FULL) {
623 cmd->se_cmd_flags &= ~SCF_EMULATE_QUEUE_FULL;
624 list_add(&cmd->se_queue_node, &qobj->qobj_list);
625 } else
626 list_add_tail(&cmd->se_queue_node, &qobj->qobj_list);
627 atomic_set(&cmd->t_transport_queue_active, 1);
628 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
629
630 wake_up_interruptible(&qobj->thread_wq);
631 }
632
633 static struct se_cmd *
634 transport_get_cmd_from_queue(struct se_queue_obj *qobj)
635 {
636 struct se_cmd *cmd;
637 unsigned long flags;
638
639 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
640 if (list_empty(&qobj->qobj_list)) {
641 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
642 return NULL;
643 }
644 cmd = list_first_entry(&qobj->qobj_list, struct se_cmd, se_queue_node);
645
646 atomic_set(&cmd->t_transport_queue_active, 0);
647
648 list_del_init(&cmd->se_queue_node);
649 atomic_dec(&qobj->queue_cnt);
650 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
651
652 return cmd;
653 }
654
655 static void transport_remove_cmd_from_queue(struct se_cmd *cmd,
656 struct se_queue_obj *qobj)
657 {
658 unsigned long flags;
659
660 spin_lock_irqsave(&qobj->cmd_queue_lock, flags);
661 if (!atomic_read(&cmd->t_transport_queue_active)) {
662 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
663 return;
664 }
665 atomic_set(&cmd->t_transport_queue_active, 0);
666 atomic_dec(&qobj->queue_cnt);
667 list_del_init(&cmd->se_queue_node);
668 spin_unlock_irqrestore(&qobj->cmd_queue_lock, flags);
669
670 if (atomic_read(&cmd->t_transport_queue_active)) {
671 pr_err("ITT: 0x%08x t_transport_queue_active: %d\n",
672 cmd->se_tfo->get_task_tag(cmd),
673 atomic_read(&cmd->t_transport_queue_active));
674 }
675 }
676
677 /*
678 * Completion function used by TCM subsystem plugins (such as FILEIO)
679 * for queueing up response from struct se_subsystem_api->do_task()
680 */
681 void transport_complete_sync_cache(struct se_cmd *cmd, int good)
682 {
683 struct se_task *task = list_entry(cmd->t_task_list.next,
684 struct se_task, t_list);
685
686 if (good) {
687 cmd->scsi_status = SAM_STAT_GOOD;
688 task->task_scsi_status = GOOD;
689 } else {
690 task->task_scsi_status = SAM_STAT_CHECK_CONDITION;
691 task->task_error_status = PYX_TRANSPORT_ILLEGAL_REQUEST;
692 task->task_se_cmd->transport_error_status =
693 PYX_TRANSPORT_ILLEGAL_REQUEST;
694 }
695
696 transport_complete_task(task, good);
697 }
698 EXPORT_SYMBOL(transport_complete_sync_cache);
699
700 /* transport_complete_task():
701 *
702 * Called from interrupt and non interrupt context depending
703 * on the transport plugin.
704 */
705 void transport_complete_task(struct se_task *task, int success)
706 {
707 struct se_cmd *cmd = task->task_se_cmd;
708 struct se_device *dev = cmd->se_dev;
709 int t_state;
710 unsigned long flags;
711 #if 0
712 pr_debug("task: %p CDB: 0x%02x obj_ptr: %p\n", task,
713 cmd->t_task_cdb[0], dev);
714 #endif
715 if (dev)
716 atomic_inc(&dev->depth_left);
717
718 spin_lock_irqsave(&cmd->t_state_lock, flags);
719 task->task_flags &= ~TF_ACTIVE;
720
721 /*
722 * See if any sense data exists, if so set the TASK_SENSE flag.
723 * Also check for any other post completion work that needs to be
724 * done by the plugins.
725 */
726 if (dev && dev->transport->transport_complete) {
727 if (dev->transport->transport_complete(task) != 0) {
728 cmd->se_cmd_flags |= SCF_TRANSPORT_TASK_SENSE;
729 task->task_sense = 1;
730 success = 1;
731 }
732 }
733
734 /*
735 * See if we are waiting for outstanding struct se_task
736 * to complete for an exception condition
737 */
738 if (task->task_flags & TF_REQUEST_STOP) {
739 /*
740 * Decrement cmd->t_se_count if this task had
741 * previously thrown its timeout exception handler.
742 */
743 if (task->task_flags & TF_TIMEOUT) {
744 atomic_dec(&cmd->t_se_count);
745 task->task_flags &= ~TF_TIMEOUT;
746 }
747 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
748
749 complete(&task->task_stop_comp);
750 return;
751 }
752 /*
753 * If the task's timeout handler has fired, use the t_task_cdbs_timeout
754 * left counter to determine when the struct se_cmd is ready to be queued to
755 * the processing thread.
756 */
757 if (task->task_flags & TF_TIMEOUT) {
758 if (!atomic_dec_and_test(
759 &cmd->t_task_cdbs_timeout_left)) {
760 spin_unlock_irqrestore(&cmd->t_state_lock,
761 flags);
762 return;
763 }
764 t_state = TRANSPORT_COMPLETE_TIMEOUT;
765 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
766
767 transport_add_cmd_to_queue(cmd, t_state);
768 return;
769 }
770 atomic_dec(&cmd->t_task_cdbs_timeout_left);
771
772 /*
773 * Decrement the outstanding t_task_cdbs_left count. The last
774 * struct se_task from struct se_cmd will complete itself into the
775 * device queue depending upon int success.
776 */
777 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
778 if (!success)
779 cmd->t_tasks_failed = 1;
780
781 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
782 return;
783 }
784
785 if (!success || cmd->t_tasks_failed) {
786 t_state = TRANSPORT_COMPLETE_FAILURE;
787 if (!task->task_error_status) {
788 task->task_error_status =
789 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
790 cmd->transport_error_status =
791 PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
792 }
793 } else {
794 atomic_set(&cmd->t_transport_complete, 1);
795 t_state = TRANSPORT_COMPLETE_OK;
796 }
797 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
798
799 transport_add_cmd_to_queue(cmd, t_state);
800 }
801 EXPORT_SYMBOL(transport_complete_task);
802
803 /*
804 * Called by transport_add_tasks_from_cmd() once a struct se_cmd's
805 * struct se_task list are ready to be added to the active execution list
806 * struct se_device
807
808 * Called with se_dev_t->execute_task_lock called.
809 */
810 static inline int transport_add_task_check_sam_attr(
811 struct se_task *task,
812 struct se_task *task_prev,
813 struct se_device *dev)
814 {
815 /*
816 * No SAM Task attribute emulation enabled, add to tail of
817 * execution queue
818 */
819 if (dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED) {
820 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
821 return 0;
822 }
823 /*
824 * HEAD_OF_QUEUE attribute for received CDB, which means
825 * the first task that is associated with a struct se_cmd goes to
826 * head of the struct se_device->execute_task_list, and task_prev
827 * after that for each subsequent task
828 */
829 if (task->task_se_cmd->sam_task_attr == MSG_HEAD_TAG) {
830 list_add(&task->t_execute_list,
831 (task_prev != NULL) ?
832 &task_prev->t_execute_list :
833 &dev->execute_task_list);
834
835 pr_debug("Set HEAD_OF_QUEUE for task CDB: 0x%02x"
836 " in execution queue\n",
837 task->task_se_cmd->t_task_cdb[0]);
838 return 1;
839 }
840 /*
841 * For ORDERED, SIMPLE or UNTAGGED attribute tasks once they have been
842 * transitioned from Dermant -> Active state, and are added to the end
843 * of the struct se_device->execute_task_list
844 */
845 list_add_tail(&task->t_execute_list, &dev->execute_task_list);
846 return 0;
847 }
848
849 /* __transport_add_task_to_execute_queue():
850 *
851 * Called with se_dev_t->execute_task_lock called.
852 */
853 static void __transport_add_task_to_execute_queue(
854 struct se_task *task,
855 struct se_task *task_prev,
856 struct se_device *dev)
857 {
858 int head_of_queue;
859
860 head_of_queue = transport_add_task_check_sam_attr(task, task_prev, dev);
861 atomic_inc(&dev->execute_tasks);
862
863 if (atomic_read(&task->task_state_active))
864 return;
865 /*
866 * Determine if this task needs to go to HEAD_OF_QUEUE for the
867 * state list as well. Running with SAM Task Attribute emulation
868 * will always return head_of_queue == 0 here
869 */
870 if (head_of_queue)
871 list_add(&task->t_state_list, (task_prev) ?
872 &task_prev->t_state_list :
873 &dev->state_task_list);
874 else
875 list_add_tail(&task->t_state_list, &dev->state_task_list);
876
877 atomic_set(&task->task_state_active, 1);
878
879 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
880 task->task_se_cmd->se_tfo->get_task_tag(task->task_se_cmd),
881 task, dev);
882 }
883
884 static void transport_add_tasks_to_state_queue(struct se_cmd *cmd)
885 {
886 struct se_device *dev = cmd->se_dev;
887 struct se_task *task;
888 unsigned long flags;
889
890 spin_lock_irqsave(&cmd->t_state_lock, flags);
891 list_for_each_entry(task, &cmd->t_task_list, t_list) {
892 if (atomic_read(&task->task_state_active))
893 continue;
894
895 spin_lock(&dev->execute_task_lock);
896 list_add_tail(&task->t_state_list, &dev->state_task_list);
897 atomic_set(&task->task_state_active, 1);
898
899 pr_debug("Added ITT: 0x%08x task[%p] to dev: %p\n",
900 task->task_se_cmd->se_tfo->get_task_tag(
901 task->task_se_cmd), task, dev);
902
903 spin_unlock(&dev->execute_task_lock);
904 }
905 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
906 }
907
908 static void transport_add_tasks_from_cmd(struct se_cmd *cmd)
909 {
910 struct se_device *dev = cmd->se_dev;
911 struct se_task *task, *task_prev = NULL;
912 unsigned long flags;
913
914 spin_lock_irqsave(&dev->execute_task_lock, flags);
915 list_for_each_entry(task, &cmd->t_task_list, t_list) {
916 if (!list_empty(&task->t_execute_list))
917 continue;
918 /*
919 * __transport_add_task_to_execute_queue() handles the
920 * SAM Task Attribute emulation if enabled
921 */
922 __transport_add_task_to_execute_queue(task, task_prev, dev);
923 task_prev = task;
924 }
925 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
926 }
927
928 void __transport_remove_task_from_execute_queue(struct se_task *task,
929 struct se_device *dev)
930 {
931 list_del_init(&task->t_execute_list);
932 atomic_dec(&dev->execute_tasks);
933 }
934
935 void transport_remove_task_from_execute_queue(
936 struct se_task *task,
937 struct se_device *dev)
938 {
939 unsigned long flags;
940
941 if (WARN_ON(list_empty(&task->t_execute_list)))
942 return;
943
944 spin_lock_irqsave(&dev->execute_task_lock, flags);
945 __transport_remove_task_from_execute_queue(task, dev);
946 spin_unlock_irqrestore(&dev->execute_task_lock, flags);
947 }
948
949 /*
950 * Handle QUEUE_FULL / -EAGAIN status
951 */
952
953 static void target_qf_do_work(struct work_struct *work)
954 {
955 struct se_device *dev = container_of(work, struct se_device,
956 qf_work_queue);
957 LIST_HEAD(qf_cmd_list);
958 struct se_cmd *cmd, *cmd_tmp;
959
960 spin_lock_irq(&dev->qf_cmd_lock);
961 list_splice_init(&dev->qf_cmd_list, &qf_cmd_list);
962 spin_unlock_irq(&dev->qf_cmd_lock);
963
964 list_for_each_entry_safe(cmd, cmd_tmp, &qf_cmd_list, se_qf_node) {
965 list_del(&cmd->se_qf_node);
966 atomic_dec(&dev->dev_qf_count);
967 smp_mb__after_atomic_dec();
968
969 pr_debug("Processing %s cmd: %p QUEUE_FULL in work queue"
970 " context: %s\n", cmd->se_tfo->get_fabric_name(), cmd,
971 (cmd->t_state == TRANSPORT_COMPLETE_QF_OK) ? "COMPLETE_OK" :
972 (cmd->t_state == TRANSPORT_COMPLETE_QF_WP) ? "WRITE_PENDING"
973 : "UNKNOWN");
974 /*
975 * The SCF_EMULATE_QUEUE_FULL flag will be cleared once se_cmd
976 * has been added to head of queue
977 */
978 transport_add_cmd_to_queue(cmd, cmd->t_state);
979 }
980 }
981
982 unsigned char *transport_dump_cmd_direction(struct se_cmd *cmd)
983 {
984 switch (cmd->data_direction) {
985 case DMA_NONE:
986 return "NONE";
987 case DMA_FROM_DEVICE:
988 return "READ";
989 case DMA_TO_DEVICE:
990 return "WRITE";
991 case DMA_BIDIRECTIONAL:
992 return "BIDI";
993 default:
994 break;
995 }
996
997 return "UNKNOWN";
998 }
999
1000 void transport_dump_dev_state(
1001 struct se_device *dev,
1002 char *b,
1003 int *bl)
1004 {
1005 *bl += sprintf(b + *bl, "Status: ");
1006 switch (dev->dev_status) {
1007 case TRANSPORT_DEVICE_ACTIVATED:
1008 *bl += sprintf(b + *bl, "ACTIVATED");
1009 break;
1010 case TRANSPORT_DEVICE_DEACTIVATED:
1011 *bl += sprintf(b + *bl, "DEACTIVATED");
1012 break;
1013 case TRANSPORT_DEVICE_SHUTDOWN:
1014 *bl += sprintf(b + *bl, "SHUTDOWN");
1015 break;
1016 case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
1017 case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
1018 *bl += sprintf(b + *bl, "OFFLINE");
1019 break;
1020 default:
1021 *bl += sprintf(b + *bl, "UNKNOWN=%d", dev->dev_status);
1022 break;
1023 }
1024
1025 *bl += sprintf(b + *bl, " Execute/Left/Max Queue Depth: %d/%d/%d",
1026 atomic_read(&dev->execute_tasks), atomic_read(&dev->depth_left),
1027 dev->queue_depth);
1028 *bl += sprintf(b + *bl, " SectorSize: %u MaxSectors: %u\n",
1029 dev->se_sub_dev->se_dev_attrib.block_size, dev->se_sub_dev->se_dev_attrib.max_sectors);
1030 *bl += sprintf(b + *bl, " ");
1031 }
1032
1033 void transport_dump_vpd_proto_id(
1034 struct t10_vpd *vpd,
1035 unsigned char *p_buf,
1036 int p_buf_len)
1037 {
1038 unsigned char buf[VPD_TMP_BUF_SIZE];
1039 int len;
1040
1041 memset(buf, 0, VPD_TMP_BUF_SIZE);
1042 len = sprintf(buf, "T10 VPD Protocol Identifier: ");
1043
1044 switch (vpd->protocol_identifier) {
1045 case 0x00:
1046 sprintf(buf+len, "Fibre Channel\n");
1047 break;
1048 case 0x10:
1049 sprintf(buf+len, "Parallel SCSI\n");
1050 break;
1051 case 0x20:
1052 sprintf(buf+len, "SSA\n");
1053 break;
1054 case 0x30:
1055 sprintf(buf+len, "IEEE 1394\n");
1056 break;
1057 case 0x40:
1058 sprintf(buf+len, "SCSI Remote Direct Memory Access"
1059 " Protocol\n");
1060 break;
1061 case 0x50:
1062 sprintf(buf+len, "Internet SCSI (iSCSI)\n");
1063 break;
1064 case 0x60:
1065 sprintf(buf+len, "SAS Serial SCSI Protocol\n");
1066 break;
1067 case 0x70:
1068 sprintf(buf+len, "Automation/Drive Interface Transport"
1069 " Protocol\n");
1070 break;
1071 case 0x80:
1072 sprintf(buf+len, "AT Attachment Interface ATA/ATAPI\n");
1073 break;
1074 default:
1075 sprintf(buf+len, "Unknown 0x%02x\n",
1076 vpd->protocol_identifier);
1077 break;
1078 }
1079
1080 if (p_buf)
1081 strncpy(p_buf, buf, p_buf_len);
1082 else
1083 pr_debug("%s", buf);
1084 }
1085
1086 void
1087 transport_set_vpd_proto_id(struct t10_vpd *vpd, unsigned char *page_83)
1088 {
1089 /*
1090 * Check if the Protocol Identifier Valid (PIV) bit is set..
1091 *
1092 * from spc3r23.pdf section 7.5.1
1093 */
1094 if (page_83[1] & 0x80) {
1095 vpd->protocol_identifier = (page_83[0] & 0xf0);
1096 vpd->protocol_identifier_set = 1;
1097 transport_dump_vpd_proto_id(vpd, NULL, 0);
1098 }
1099 }
1100 EXPORT_SYMBOL(transport_set_vpd_proto_id);
1101
1102 int transport_dump_vpd_assoc(
1103 struct t10_vpd *vpd,
1104 unsigned char *p_buf,
1105 int p_buf_len)
1106 {
1107 unsigned char buf[VPD_TMP_BUF_SIZE];
1108 int ret = 0;
1109 int len;
1110
1111 memset(buf, 0, VPD_TMP_BUF_SIZE);
1112 len = sprintf(buf, "T10 VPD Identifier Association: ");
1113
1114 switch (vpd->association) {
1115 case 0x00:
1116 sprintf(buf+len, "addressed logical unit\n");
1117 break;
1118 case 0x10:
1119 sprintf(buf+len, "target port\n");
1120 break;
1121 case 0x20:
1122 sprintf(buf+len, "SCSI target device\n");
1123 break;
1124 default:
1125 sprintf(buf+len, "Unknown 0x%02x\n", vpd->association);
1126 ret = -EINVAL;
1127 break;
1128 }
1129
1130 if (p_buf)
1131 strncpy(p_buf, buf, p_buf_len);
1132 else
1133 pr_debug("%s", buf);
1134
1135 return ret;
1136 }
1137
1138 int transport_set_vpd_assoc(struct t10_vpd *vpd, unsigned char *page_83)
1139 {
1140 /*
1141 * The VPD identification association..
1142 *
1143 * from spc3r23.pdf Section 7.6.3.1 Table 297
1144 */
1145 vpd->association = (page_83[1] & 0x30);
1146 return transport_dump_vpd_assoc(vpd, NULL, 0);
1147 }
1148 EXPORT_SYMBOL(transport_set_vpd_assoc);
1149
1150 int transport_dump_vpd_ident_type(
1151 struct t10_vpd *vpd,
1152 unsigned char *p_buf,
1153 int p_buf_len)
1154 {
1155 unsigned char buf[VPD_TMP_BUF_SIZE];
1156 int ret = 0;
1157 int len;
1158
1159 memset(buf, 0, VPD_TMP_BUF_SIZE);
1160 len = sprintf(buf, "T10 VPD Identifier Type: ");
1161
1162 switch (vpd->device_identifier_type) {
1163 case 0x00:
1164 sprintf(buf+len, "Vendor specific\n");
1165 break;
1166 case 0x01:
1167 sprintf(buf+len, "T10 Vendor ID based\n");
1168 break;
1169 case 0x02:
1170 sprintf(buf+len, "EUI-64 based\n");
1171 break;
1172 case 0x03:
1173 sprintf(buf+len, "NAA\n");
1174 break;
1175 case 0x04:
1176 sprintf(buf+len, "Relative target port identifier\n");
1177 break;
1178 case 0x08:
1179 sprintf(buf+len, "SCSI name string\n");
1180 break;
1181 default:
1182 sprintf(buf+len, "Unsupported: 0x%02x\n",
1183 vpd->device_identifier_type);
1184 ret = -EINVAL;
1185 break;
1186 }
1187
1188 if (p_buf) {
1189 if (p_buf_len < strlen(buf)+1)
1190 return -EINVAL;
1191 strncpy(p_buf, buf, p_buf_len);
1192 } else {
1193 pr_debug("%s", buf);
1194 }
1195
1196 return ret;
1197 }
1198
1199 int transport_set_vpd_ident_type(struct t10_vpd *vpd, unsigned char *page_83)
1200 {
1201 /*
1202 * The VPD identifier type..
1203 *
1204 * from spc3r23.pdf Section 7.6.3.1 Table 298
1205 */
1206 vpd->device_identifier_type = (page_83[1] & 0x0f);
1207 return transport_dump_vpd_ident_type(vpd, NULL, 0);
1208 }
1209 EXPORT_SYMBOL(transport_set_vpd_ident_type);
1210
1211 int transport_dump_vpd_ident(
1212 struct t10_vpd *vpd,
1213 unsigned char *p_buf,
1214 int p_buf_len)
1215 {
1216 unsigned char buf[VPD_TMP_BUF_SIZE];
1217 int ret = 0;
1218
1219 memset(buf, 0, VPD_TMP_BUF_SIZE);
1220
1221 switch (vpd->device_identifier_code_set) {
1222 case 0x01: /* Binary */
1223 sprintf(buf, "T10 VPD Binary Device Identifier: %s\n",
1224 &vpd->device_identifier[0]);
1225 break;
1226 case 0x02: /* ASCII */
1227 sprintf(buf, "T10 VPD ASCII Device Identifier: %s\n",
1228 &vpd->device_identifier[0]);
1229 break;
1230 case 0x03: /* UTF-8 */
1231 sprintf(buf, "T10 VPD UTF-8 Device Identifier: %s\n",
1232 &vpd->device_identifier[0]);
1233 break;
1234 default:
1235 sprintf(buf, "T10 VPD Device Identifier encoding unsupported:"
1236 " 0x%02x", vpd->device_identifier_code_set);
1237 ret = -EINVAL;
1238 break;
1239 }
1240
1241 if (p_buf)
1242 strncpy(p_buf, buf, p_buf_len);
1243 else
1244 pr_debug("%s", buf);
1245
1246 return ret;
1247 }
1248
1249 int
1250 transport_set_vpd_ident(struct t10_vpd *vpd, unsigned char *page_83)
1251 {
1252 static const char hex_str[] = "0123456789abcdef";
1253 int j = 0, i = 4; /* offset to start of the identifer */
1254
1255 /*
1256 * The VPD Code Set (encoding)
1257 *
1258 * from spc3r23.pdf Section 7.6.3.1 Table 296
1259 */
1260 vpd->device_identifier_code_set = (page_83[0] & 0x0f);
1261 switch (vpd->device_identifier_code_set) {
1262 case 0x01: /* Binary */
1263 vpd->device_identifier[j++] =
1264 hex_str[vpd->device_identifier_type];
1265 while (i < (4 + page_83[3])) {
1266 vpd->device_identifier[j++] =
1267 hex_str[(page_83[i] & 0xf0) >> 4];
1268 vpd->device_identifier[j++] =
1269 hex_str[page_83[i] & 0x0f];
1270 i++;
1271 }
1272 break;
1273 case 0x02: /* ASCII */
1274 case 0x03: /* UTF-8 */
1275 while (i < (4 + page_83[3]))
1276 vpd->device_identifier[j++] = page_83[i++];
1277 break;
1278 default:
1279 break;
1280 }
1281
1282 return transport_dump_vpd_ident(vpd, NULL, 0);
1283 }
1284 EXPORT_SYMBOL(transport_set_vpd_ident);
1285
1286 static void core_setup_task_attr_emulation(struct se_device *dev)
1287 {
1288 /*
1289 * If this device is from Target_Core_Mod/pSCSI, disable the
1290 * SAM Task Attribute emulation.
1291 *
1292 * This is currently not available in upsream Linux/SCSI Target
1293 * mode code, and is assumed to be disabled while using TCM/pSCSI.
1294 */
1295 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1296 dev->dev_task_attr_type = SAM_TASK_ATTR_PASSTHROUGH;
1297 return;
1298 }
1299
1300 dev->dev_task_attr_type = SAM_TASK_ATTR_EMULATED;
1301 pr_debug("%s: Using SAM_TASK_ATTR_EMULATED for SPC: 0x%02x"
1302 " device\n", dev->transport->name,
1303 dev->transport->get_device_rev(dev));
1304 }
1305
1306 static void scsi_dump_inquiry(struct se_device *dev)
1307 {
1308 struct t10_wwn *wwn = &dev->se_sub_dev->t10_wwn;
1309 int i, device_type;
1310 /*
1311 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1312 */
1313 pr_debug(" Vendor: ");
1314 for (i = 0; i < 8; i++)
1315 if (wwn->vendor[i] >= 0x20)
1316 pr_debug("%c", wwn->vendor[i]);
1317 else
1318 pr_debug(" ");
1319
1320 pr_debug(" Model: ");
1321 for (i = 0; i < 16; i++)
1322 if (wwn->model[i] >= 0x20)
1323 pr_debug("%c", wwn->model[i]);
1324 else
1325 pr_debug(" ");
1326
1327 pr_debug(" Revision: ");
1328 for (i = 0; i < 4; i++)
1329 if (wwn->revision[i] >= 0x20)
1330 pr_debug("%c", wwn->revision[i]);
1331 else
1332 pr_debug(" ");
1333
1334 pr_debug("\n");
1335
1336 device_type = dev->transport->get_device_type(dev);
1337 pr_debug(" Type: %s ", scsi_device_type(device_type));
1338 pr_debug(" ANSI SCSI revision: %02x\n",
1339 dev->transport->get_device_rev(dev));
1340 }
1341
1342 struct se_device *transport_add_device_to_core_hba(
1343 struct se_hba *hba,
1344 struct se_subsystem_api *transport,
1345 struct se_subsystem_dev *se_dev,
1346 u32 device_flags,
1347 void *transport_dev,
1348 struct se_dev_limits *dev_limits,
1349 const char *inquiry_prod,
1350 const char *inquiry_rev)
1351 {
1352 int force_pt;
1353 struct se_device *dev;
1354
1355 dev = kzalloc(sizeof(struct se_device), GFP_KERNEL);
1356 if (!dev) {
1357 pr_err("Unable to allocate memory for se_dev_t\n");
1358 return NULL;
1359 }
1360
1361 transport_init_queue_obj(&dev->dev_queue_obj);
1362 dev->dev_flags = device_flags;
1363 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
1364 dev->dev_ptr = transport_dev;
1365 dev->se_hba = hba;
1366 dev->se_sub_dev = se_dev;
1367 dev->transport = transport;
1368 atomic_set(&dev->active_cmds, 0);
1369 INIT_LIST_HEAD(&dev->dev_list);
1370 INIT_LIST_HEAD(&dev->dev_sep_list);
1371 INIT_LIST_HEAD(&dev->dev_tmr_list);
1372 INIT_LIST_HEAD(&dev->execute_task_list);
1373 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1374 INIT_LIST_HEAD(&dev->ordered_cmd_list);
1375 INIT_LIST_HEAD(&dev->state_task_list);
1376 INIT_LIST_HEAD(&dev->qf_cmd_list);
1377 spin_lock_init(&dev->execute_task_lock);
1378 spin_lock_init(&dev->delayed_cmd_lock);
1379 spin_lock_init(&dev->ordered_cmd_lock);
1380 spin_lock_init(&dev->state_task_lock);
1381 spin_lock_init(&dev->dev_alua_lock);
1382 spin_lock_init(&dev->dev_reservation_lock);
1383 spin_lock_init(&dev->dev_status_lock);
1384 spin_lock_init(&dev->dev_status_thr_lock);
1385 spin_lock_init(&dev->se_port_lock);
1386 spin_lock_init(&dev->se_tmr_lock);
1387 spin_lock_init(&dev->qf_cmd_lock);
1388
1389 dev->queue_depth = dev_limits->queue_depth;
1390 atomic_set(&dev->depth_left, dev->queue_depth);
1391 atomic_set(&dev->dev_ordered_id, 0);
1392
1393 se_dev_set_default_attribs(dev, dev_limits);
1394
1395 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1396 dev->creation_time = get_jiffies_64();
1397 spin_lock_init(&dev->stats_lock);
1398
1399 spin_lock(&hba->device_lock);
1400 list_add_tail(&dev->dev_list, &hba->hba_dev_list);
1401 hba->dev_count++;
1402 spin_unlock(&hba->device_lock);
1403 /*
1404 * Setup the SAM Task Attribute emulation for struct se_device
1405 */
1406 core_setup_task_attr_emulation(dev);
1407 /*
1408 * Force PR and ALUA passthrough emulation with internal object use.
1409 */
1410 force_pt = (hba->hba_flags & HBA_FLAGS_INTERNAL_USE);
1411 /*
1412 * Setup the Reservations infrastructure for struct se_device
1413 */
1414 core_setup_reservations(dev, force_pt);
1415 /*
1416 * Setup the Asymmetric Logical Unit Assignment for struct se_device
1417 */
1418 if (core_setup_alua(dev, force_pt) < 0)
1419 goto out;
1420
1421 /*
1422 * Startup the struct se_device processing thread
1423 */
1424 dev->process_thread = kthread_run(transport_processing_thread, dev,
1425 "LIO_%s", dev->transport->name);
1426 if (IS_ERR(dev->process_thread)) {
1427 pr_err("Unable to create kthread: LIO_%s\n",
1428 dev->transport->name);
1429 goto out;
1430 }
1431 /*
1432 * Setup work_queue for QUEUE_FULL
1433 */
1434 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1435 /*
1436 * Preload the initial INQUIRY const values if we are doing
1437 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1438 * passthrough because this is being provided by the backend LLD.
1439 * This is required so that transport_get_inquiry() copies these
1440 * originals once back into DEV_T10_WWN(dev) for the virtual device
1441 * setup.
1442 */
1443 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1444 if (!inquiry_prod || !inquiry_rev) {
1445 pr_err("All non TCM/pSCSI plugins require"
1446 " INQUIRY consts\n");
1447 goto out;
1448 }
1449
1450 strncpy(&dev->se_sub_dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1451 strncpy(&dev->se_sub_dev->t10_wwn.model[0], inquiry_prod, 16);
1452 strncpy(&dev->se_sub_dev->t10_wwn.revision[0], inquiry_rev, 4);
1453 }
1454 scsi_dump_inquiry(dev);
1455
1456 return dev;
1457 out:
1458 kthread_stop(dev->process_thread);
1459
1460 spin_lock(&hba->device_lock);
1461 list_del(&dev->dev_list);
1462 hba->dev_count--;
1463 spin_unlock(&hba->device_lock);
1464
1465 se_release_vpd_for_dev(dev);
1466
1467 kfree(dev);
1468
1469 return NULL;
1470 }
1471 EXPORT_SYMBOL(transport_add_device_to_core_hba);
1472
1473 /* transport_generic_prepare_cdb():
1474 *
1475 * Since the Initiator sees iSCSI devices as LUNs, the SCSI CDB will
1476 * contain the iSCSI LUN in bits 7-5 of byte 1 as per SAM-2.
1477 * The point of this is since we are mapping iSCSI LUNs to
1478 * SCSI Target IDs having a non-zero LUN in the CDB will throw the
1479 * devices and HBAs for a loop.
1480 */
1481 static inline void transport_generic_prepare_cdb(
1482 unsigned char *cdb)
1483 {
1484 switch (cdb[0]) {
1485 case READ_10: /* SBC - RDProtect */
1486 case READ_12: /* SBC - RDProtect */
1487 case READ_16: /* SBC - RDProtect */
1488 case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
1489 case VERIFY: /* SBC - VRProtect */
1490 case VERIFY_16: /* SBC - VRProtect */
1491 case WRITE_VERIFY: /* SBC - VRProtect */
1492 case WRITE_VERIFY_12: /* SBC - VRProtect */
1493 break;
1494 default:
1495 cdb[1] &= 0x1f; /* clear logical unit number */
1496 break;
1497 }
1498 }
1499
1500 static struct se_task *
1501 transport_generic_get_task(struct se_cmd *cmd,
1502 enum dma_data_direction data_direction)
1503 {
1504 struct se_task *task;
1505 struct se_device *dev = cmd->se_dev;
1506
1507 task = dev->transport->alloc_task(cmd->t_task_cdb);
1508 if (!task) {
1509 pr_err("Unable to allocate struct se_task\n");
1510 return NULL;
1511 }
1512
1513 INIT_LIST_HEAD(&task->t_list);
1514 INIT_LIST_HEAD(&task->t_execute_list);
1515 INIT_LIST_HEAD(&task->t_state_list);
1516 init_completion(&task->task_stop_comp);
1517 task->task_se_cmd = cmd;
1518 task->task_data_direction = data_direction;
1519
1520 return task;
1521 }
1522
1523 static int transport_generic_cmd_sequencer(struct se_cmd *, unsigned char *);
1524
1525 /*
1526 * Used by fabric modules containing a local struct se_cmd within their
1527 * fabric dependent per I/O descriptor.
1528 */
1529 void transport_init_se_cmd(
1530 struct se_cmd *cmd,
1531 struct target_core_fabric_ops *tfo,
1532 struct se_session *se_sess,
1533 u32 data_length,
1534 int data_direction,
1535 int task_attr,
1536 unsigned char *sense_buffer)
1537 {
1538 INIT_LIST_HEAD(&cmd->se_lun_node);
1539 INIT_LIST_HEAD(&cmd->se_delayed_node);
1540 INIT_LIST_HEAD(&cmd->se_ordered_node);
1541 INIT_LIST_HEAD(&cmd->se_qf_node);
1542 INIT_LIST_HEAD(&cmd->se_queue_node);
1543
1544 INIT_LIST_HEAD(&cmd->t_task_list);
1545 init_completion(&cmd->transport_lun_fe_stop_comp);
1546 init_completion(&cmd->transport_lun_stop_comp);
1547 init_completion(&cmd->t_transport_stop_comp);
1548 spin_lock_init(&cmd->t_state_lock);
1549 atomic_set(&cmd->transport_dev_active, 1);
1550
1551 cmd->se_tfo = tfo;
1552 cmd->se_sess = se_sess;
1553 cmd->data_length = data_length;
1554 cmd->data_direction = data_direction;
1555 cmd->sam_task_attr = task_attr;
1556 cmd->sense_buffer = sense_buffer;
1557 }
1558 EXPORT_SYMBOL(transport_init_se_cmd);
1559
1560 static int transport_check_alloc_task_attr(struct se_cmd *cmd)
1561 {
1562 /*
1563 * Check if SAM Task Attribute emulation is enabled for this
1564 * struct se_device storage object
1565 */
1566 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
1567 return 0;
1568
1569 if (cmd->sam_task_attr == MSG_ACA_TAG) {
1570 pr_debug("SAM Task Attribute ACA"
1571 " emulation is not supported\n");
1572 return -EINVAL;
1573 }
1574 /*
1575 * Used to determine when ORDERED commands should go from
1576 * Dormant to Active status.
1577 */
1578 cmd->se_ordered_id = atomic_inc_return(&cmd->se_dev->dev_ordered_id);
1579 smp_mb__after_atomic_inc();
1580 pr_debug("Allocated se_ordered_id: %u for Task Attr: 0x%02x on %s\n",
1581 cmd->se_ordered_id, cmd->sam_task_attr,
1582 cmd->se_dev->transport->name);
1583 return 0;
1584 }
1585
1586 /* transport_generic_allocate_tasks():
1587 *
1588 * Called from fabric RX Thread.
1589 */
1590 int transport_generic_allocate_tasks(
1591 struct se_cmd *cmd,
1592 unsigned char *cdb)
1593 {
1594 int ret;
1595
1596 transport_generic_prepare_cdb(cdb);
1597 /*
1598 * Ensure that the received CDB is less than the max (252 + 8) bytes
1599 * for VARIABLE_LENGTH_CMD
1600 */
1601 if (scsi_command_size(cdb) > SCSI_MAX_VARLEN_CDB_SIZE) {
1602 pr_err("Received SCSI CDB with command_size: %d that"
1603 " exceeds SCSI_MAX_VARLEN_CDB_SIZE: %d\n",
1604 scsi_command_size(cdb), SCSI_MAX_VARLEN_CDB_SIZE);
1605 return -EINVAL;
1606 }
1607 /*
1608 * If the received CDB is larger than TCM_MAX_COMMAND_SIZE,
1609 * allocate the additional extended CDB buffer now.. Otherwise
1610 * setup the pointer from __t_task_cdb to t_task_cdb.
1611 */
1612 if (scsi_command_size(cdb) > sizeof(cmd->__t_task_cdb)) {
1613 cmd->t_task_cdb = kzalloc(scsi_command_size(cdb),
1614 GFP_KERNEL);
1615 if (!cmd->t_task_cdb) {
1616 pr_err("Unable to allocate cmd->t_task_cdb"
1617 " %u > sizeof(cmd->__t_task_cdb): %lu ops\n",
1618 scsi_command_size(cdb),
1619 (unsigned long)sizeof(cmd->__t_task_cdb));
1620 return -ENOMEM;
1621 }
1622 } else
1623 cmd->t_task_cdb = &cmd->__t_task_cdb[0];
1624 /*
1625 * Copy the original CDB into cmd->
1626 */
1627 memcpy(cmd->t_task_cdb, cdb, scsi_command_size(cdb));
1628 /*
1629 * Setup the received CDB based on SCSI defined opcodes and
1630 * perform unit attention, persistent reservations and ALUA
1631 * checks for virtual device backends. The cmd->t_task_cdb
1632 * pointer is expected to be setup before we reach this point.
1633 */
1634 ret = transport_generic_cmd_sequencer(cmd, cdb);
1635 if (ret < 0)
1636 return ret;
1637 /*
1638 * Check for SAM Task Attribute Emulation
1639 */
1640 if (transport_check_alloc_task_attr(cmd) < 0) {
1641 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
1642 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1643 return -EINVAL;
1644 }
1645 spin_lock(&cmd->se_lun->lun_sep_lock);
1646 if (cmd->se_lun->lun_sep)
1647 cmd->se_lun->lun_sep->sep_stats.cmd_pdus++;
1648 spin_unlock(&cmd->se_lun->lun_sep_lock);
1649 return 0;
1650 }
1651 EXPORT_SYMBOL(transport_generic_allocate_tasks);
1652
1653 static void transport_generic_request_failure(struct se_cmd *,
1654 struct se_device *, int, int);
1655 /*
1656 * Used by fabric module frontends to queue tasks directly.
1657 * Many only be used from process context only
1658 */
1659 int transport_handle_cdb_direct(
1660 struct se_cmd *cmd)
1661 {
1662 int ret;
1663
1664 if (!cmd->se_lun) {
1665 dump_stack();
1666 pr_err("cmd->se_lun is NULL\n");
1667 return -EINVAL;
1668 }
1669 if (in_interrupt()) {
1670 dump_stack();
1671 pr_err("transport_generic_handle_cdb cannot be called"
1672 " from interrupt context\n");
1673 return -EINVAL;
1674 }
1675 /*
1676 * Set TRANSPORT_NEW_CMD state and cmd->t_transport_active=1 following
1677 * transport_generic_handle_cdb*() -> transport_add_cmd_to_queue()
1678 * in existing usage to ensure that outstanding descriptors are handled
1679 * correctly during shutdown via transport_wait_for_tasks()
1680 *
1681 * Also, we don't take cmd->t_state_lock here as we only expect
1682 * this to be called for initial descriptor submission.
1683 */
1684 cmd->t_state = TRANSPORT_NEW_CMD;
1685 atomic_set(&cmd->t_transport_active, 1);
1686 /*
1687 * transport_generic_new_cmd() is already handling QUEUE_FULL,
1688 * so follow TRANSPORT_NEW_CMD processing thread context usage
1689 * and call transport_generic_request_failure() if necessary..
1690 */
1691 ret = transport_generic_new_cmd(cmd);
1692 if (ret == -EAGAIN)
1693 return 0;
1694 else if (ret < 0) {
1695 cmd->transport_error_status = ret;
1696 transport_generic_request_failure(cmd, NULL, 0,
1697 (cmd->data_direction != DMA_TO_DEVICE));
1698 }
1699 return 0;
1700 }
1701 EXPORT_SYMBOL(transport_handle_cdb_direct);
1702
1703 /*
1704 * Used by fabric module frontends defining a TFO->new_cmd_map() caller
1705 * to queue up a newly setup se_cmd w/ TRANSPORT_NEW_CMD_MAP in order to
1706 * complete setup in TCM process context w/ TFO->new_cmd_map().
1707 */
1708 int transport_generic_handle_cdb_map(
1709 struct se_cmd *cmd)
1710 {
1711 if (!cmd->se_lun) {
1712 dump_stack();
1713 pr_err("cmd->se_lun is NULL\n");
1714 return -EINVAL;
1715 }
1716
1717 transport_add_cmd_to_queue(cmd, TRANSPORT_NEW_CMD_MAP);
1718 return 0;
1719 }
1720 EXPORT_SYMBOL(transport_generic_handle_cdb_map);
1721
1722 /* transport_generic_handle_data():
1723 *
1724 *
1725 */
1726 int transport_generic_handle_data(
1727 struct se_cmd *cmd)
1728 {
1729 /*
1730 * For the software fabric case, then we assume the nexus is being
1731 * failed/shutdown when signals are pending from the kthread context
1732 * caller, so we return a failure. For the HW target mode case running
1733 * in interrupt code, the signal_pending() check is skipped.
1734 */
1735 if (!in_interrupt() && signal_pending(current))
1736 return -EPERM;
1737 /*
1738 * If the received CDB has aleady been ABORTED by the generic
1739 * target engine, we now call transport_check_aborted_status()
1740 * to queue any delated TASK_ABORTED status for the received CDB to the
1741 * fabric module as we are expecting no further incoming DATA OUT
1742 * sequences at this point.
1743 */
1744 if (transport_check_aborted_status(cmd, 1) != 0)
1745 return 0;
1746
1747 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_WRITE);
1748 return 0;
1749 }
1750 EXPORT_SYMBOL(transport_generic_handle_data);
1751
1752 /* transport_generic_handle_tmr():
1753 *
1754 *
1755 */
1756 int transport_generic_handle_tmr(
1757 struct se_cmd *cmd)
1758 {
1759 transport_add_cmd_to_queue(cmd, TRANSPORT_PROCESS_TMR);
1760 return 0;
1761 }
1762 EXPORT_SYMBOL(transport_generic_handle_tmr);
1763
1764 void transport_generic_free_cmd_intr(
1765 struct se_cmd *cmd)
1766 {
1767 transport_add_cmd_to_queue(cmd, TRANSPORT_FREE_CMD_INTR);
1768 }
1769 EXPORT_SYMBOL(transport_generic_free_cmd_intr);
1770
1771 static int transport_stop_tasks_for_cmd(struct se_cmd *cmd)
1772 {
1773 struct se_task *task, *task_tmp;
1774 unsigned long flags;
1775 int ret = 0;
1776
1777 pr_debug("ITT[0x%08x] - Stopping tasks\n",
1778 cmd->se_tfo->get_task_tag(cmd));
1779
1780 /*
1781 * No tasks remain in the execution queue
1782 */
1783 spin_lock_irqsave(&cmd->t_state_lock, flags);
1784 list_for_each_entry_safe(task, task_tmp,
1785 &cmd->t_task_list, t_list) {
1786 pr_debug("Processing task %p\n", task);
1787 /*
1788 * If the struct se_task has not been sent and is not active,
1789 * remove the struct se_task from the execution queue.
1790 */
1791 if (!(task->task_flags & (TF_ACTIVE | TF_SENT))) {
1792 spin_unlock_irqrestore(&cmd->t_state_lock,
1793 flags);
1794 transport_remove_task_from_execute_queue(task,
1795 cmd->se_dev);
1796
1797 pr_debug("Task %p removed from execute queue\n", task);
1798 spin_lock_irqsave(&cmd->t_state_lock, flags);
1799 continue;
1800 }
1801
1802 /*
1803 * If the struct se_task is active, sleep until it is returned
1804 * from the plugin.
1805 */
1806 if (task->task_flags & TF_ACTIVE) {
1807 task->task_flags |= TF_REQUEST_STOP;
1808 spin_unlock_irqrestore(&cmd->t_state_lock,
1809 flags);
1810
1811 pr_debug("Task %p waiting to complete\n", task);
1812 wait_for_completion(&task->task_stop_comp);
1813 pr_debug("Task %p stopped successfully\n", task);
1814
1815 spin_lock_irqsave(&cmd->t_state_lock, flags);
1816 atomic_dec(&cmd->t_task_cdbs_left);
1817 task->task_flags &= ~(TF_ACTIVE | TF_REQUEST_STOP);
1818 } else {
1819 pr_debug("Task %p - did nothing\n", task);
1820 ret++;
1821 }
1822
1823 __transport_stop_task_timer(task, &flags);
1824 }
1825 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1826
1827 return ret;
1828 }
1829
1830 /*
1831 * Handle SAM-esque emulation for generic transport request failures.
1832 */
1833 static void transport_generic_request_failure(
1834 struct se_cmd *cmd,
1835 struct se_device *dev,
1836 int complete,
1837 int sc)
1838 {
1839 int ret = 0;
1840
1841 pr_debug("-----[ Storage Engine Exception for cmd: %p ITT: 0x%08x"
1842 " CDB: 0x%02x\n", cmd, cmd->se_tfo->get_task_tag(cmd),
1843 cmd->t_task_cdb[0]);
1844 pr_debug("-----[ i_state: %d t_state/def_t_state:"
1845 " %d/%d transport_error_status: %d\n",
1846 cmd->se_tfo->get_cmd_state(cmd),
1847 cmd->t_state, cmd->deferred_t_state,
1848 cmd->transport_error_status);
1849 pr_debug("-----[ t_tasks: %d t_task_cdbs_left: %d"
1850 " t_task_cdbs_sent: %d t_task_cdbs_ex_left: %d --"
1851 " t_transport_active: %d t_transport_stop: %d"
1852 " t_transport_sent: %d\n", cmd->t_task_list_num,
1853 atomic_read(&cmd->t_task_cdbs_left),
1854 atomic_read(&cmd->t_task_cdbs_sent),
1855 atomic_read(&cmd->t_task_cdbs_ex_left),
1856 atomic_read(&cmd->t_transport_active),
1857 atomic_read(&cmd->t_transport_stop),
1858 atomic_read(&cmd->t_transport_sent));
1859
1860 transport_stop_all_task_timers(cmd);
1861
1862 if (dev)
1863 atomic_inc(&dev->depth_left);
1864 /*
1865 * For SAM Task Attribute emulation for failed struct se_cmd
1866 */
1867 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
1868 transport_complete_task_attr(cmd);
1869
1870 if (complete) {
1871 transport_direct_request_timeout(cmd);
1872 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
1873 }
1874
1875 switch (cmd->transport_error_status) {
1876 case PYX_TRANSPORT_UNKNOWN_SAM_OPCODE:
1877 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1878 break;
1879 case PYX_TRANSPORT_REQ_TOO_MANY_SECTORS:
1880 cmd->scsi_sense_reason = TCM_SECTOR_COUNT_TOO_MANY;
1881 break;
1882 case PYX_TRANSPORT_INVALID_CDB_FIELD:
1883 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
1884 break;
1885 case PYX_TRANSPORT_INVALID_PARAMETER_LIST:
1886 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
1887 break;
1888 case PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES:
1889 if (!sc)
1890 transport_new_cmd_failure(cmd);
1891 /*
1892 * Currently for PYX_TRANSPORT_OUT_OF_MEMORY_RESOURCES,
1893 * we force this session to fall back to session
1894 * recovery.
1895 */
1896 cmd->se_tfo->fall_back_to_erl0(cmd->se_sess);
1897 cmd->se_tfo->stop_session(cmd->se_sess, 0, 0);
1898
1899 goto check_stop;
1900 case PYX_TRANSPORT_LU_COMM_FAILURE:
1901 case PYX_TRANSPORT_ILLEGAL_REQUEST:
1902 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
1903 break;
1904 case PYX_TRANSPORT_UNKNOWN_MODE_PAGE:
1905 cmd->scsi_sense_reason = TCM_UNKNOWN_MODE_PAGE;
1906 break;
1907 case PYX_TRANSPORT_WRITE_PROTECTED:
1908 cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
1909 break;
1910 case PYX_TRANSPORT_RESERVATION_CONFLICT:
1911 /*
1912 * No SENSE Data payload for this case, set SCSI Status
1913 * and queue the response to $FABRIC_MOD.
1914 *
1915 * Uses linux/include/scsi/scsi.h SAM status codes defs
1916 */
1917 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
1918 /*
1919 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
1920 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
1921 * CONFLICT STATUS.
1922 *
1923 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
1924 */
1925 if (cmd->se_sess &&
1926 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
1927 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
1928 cmd->orig_fe_lun, 0x2C,
1929 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
1930
1931 ret = cmd->se_tfo->queue_status(cmd);
1932 if (ret == -EAGAIN)
1933 goto queue_full;
1934 goto check_stop;
1935 case PYX_TRANSPORT_USE_SENSE_REASON:
1936 /*
1937 * struct se_cmd->scsi_sense_reason already set
1938 */
1939 break;
1940 default:
1941 pr_err("Unknown transport error for CDB 0x%02x: %d\n",
1942 cmd->t_task_cdb[0],
1943 cmd->transport_error_status);
1944 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
1945 break;
1946 }
1947 /*
1948 * If a fabric does not define a cmd->se_tfo->new_cmd_map caller,
1949 * make the call to transport_send_check_condition_and_sense()
1950 * directly. Otherwise expect the fabric to make the call to
1951 * transport_send_check_condition_and_sense() after handling
1952 * possible unsoliticied write data payloads.
1953 */
1954 if (!sc && !cmd->se_tfo->new_cmd_map)
1955 transport_new_cmd_failure(cmd);
1956 else {
1957 ret = transport_send_check_condition_and_sense(cmd,
1958 cmd->scsi_sense_reason, 0);
1959 if (ret == -EAGAIN)
1960 goto queue_full;
1961 }
1962
1963 check_stop:
1964 transport_lun_remove_cmd(cmd);
1965 if (!transport_cmd_check_stop_to_fabric(cmd))
1966 ;
1967 return;
1968
1969 queue_full:
1970 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
1971 transport_handle_queue_full(cmd, cmd->se_dev);
1972 }
1973
1974 static void transport_direct_request_timeout(struct se_cmd *cmd)
1975 {
1976 unsigned long flags;
1977
1978 spin_lock_irqsave(&cmd->t_state_lock, flags);
1979 if (!atomic_read(&cmd->t_transport_timeout)) {
1980 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1981 return;
1982 }
1983 if (atomic_read(&cmd->t_task_cdbs_timeout_left)) {
1984 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1985 return;
1986 }
1987
1988 atomic_sub(atomic_read(&cmd->t_transport_timeout),
1989 &cmd->t_se_count);
1990 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
1991 }
1992
1993 static void transport_generic_request_timeout(struct se_cmd *cmd)
1994 {
1995 unsigned long flags;
1996
1997 /*
1998 * Reset cmd->t_se_count to allow transport_put_cmd()
1999 * to allow last call to free memory resources.
2000 */
2001 spin_lock_irqsave(&cmd->t_state_lock, flags);
2002 if (atomic_read(&cmd->t_transport_timeout) > 1) {
2003 int tmp = (atomic_read(&cmd->t_transport_timeout) - 1);
2004
2005 atomic_sub(tmp, &cmd->t_se_count);
2006 }
2007 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2008
2009 transport_put_cmd(cmd);
2010 }
2011
2012 static inline u32 transport_lba_21(unsigned char *cdb)
2013 {
2014 return ((cdb[1] & 0x1f) << 16) | (cdb[2] << 8) | cdb[3];
2015 }
2016
2017 static inline u32 transport_lba_32(unsigned char *cdb)
2018 {
2019 return (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2020 }
2021
2022 static inline unsigned long long transport_lba_64(unsigned char *cdb)
2023 {
2024 unsigned int __v1, __v2;
2025
2026 __v1 = (cdb[2] << 24) | (cdb[3] << 16) | (cdb[4] << 8) | cdb[5];
2027 __v2 = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
2028
2029 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2030 }
2031
2032 /*
2033 * For VARIABLE_LENGTH_CDB w/ 32 byte extended CDBs
2034 */
2035 static inline unsigned long long transport_lba_64_ext(unsigned char *cdb)
2036 {
2037 unsigned int __v1, __v2;
2038
2039 __v1 = (cdb[12] << 24) | (cdb[13] << 16) | (cdb[14] << 8) | cdb[15];
2040 __v2 = (cdb[16] << 24) | (cdb[17] << 16) | (cdb[18] << 8) | cdb[19];
2041
2042 return ((unsigned long long)__v2) | (unsigned long long)__v1 << 32;
2043 }
2044
2045 static void transport_set_supported_SAM_opcode(struct se_cmd *se_cmd)
2046 {
2047 unsigned long flags;
2048
2049 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2050 se_cmd->se_cmd_flags |= SCF_SUPPORTED_SAM_OPCODE;
2051 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2052 }
2053
2054 /*
2055 * Called from interrupt context.
2056 */
2057 static void transport_task_timeout_handler(unsigned long data)
2058 {
2059 struct se_task *task = (struct se_task *)data;
2060 struct se_cmd *cmd = task->task_se_cmd;
2061 unsigned long flags;
2062
2063 pr_debug("transport task timeout fired! task: %p cmd: %p\n", task, cmd);
2064
2065 spin_lock_irqsave(&cmd->t_state_lock, flags);
2066 if (task->task_flags & TF_TIMER_STOP) {
2067 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2068 return;
2069 }
2070 task->task_flags &= ~TF_TIMER_RUNNING;
2071
2072 /*
2073 * Determine if transport_complete_task() has already been called.
2074 */
2075 if (!(task->task_flags & TF_ACTIVE)) {
2076 pr_debug("transport task: %p cmd: %p timeout !TF_ACTIVE\n",
2077 task, cmd);
2078 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2079 return;
2080 }
2081
2082 atomic_inc(&cmd->t_se_count);
2083 atomic_inc(&cmd->t_transport_timeout);
2084 cmd->t_tasks_failed = 1;
2085
2086 task->task_flags |= TF_TIMEOUT;
2087 task->task_error_status = PYX_TRANSPORT_TASK_TIMEOUT;
2088 task->task_scsi_status = 1;
2089
2090 if (task->task_flags & TF_REQUEST_STOP) {
2091 pr_debug("transport task: %p cmd: %p timeout TF_REQUEST_STOP"
2092 " == 1\n", task, cmd);
2093 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2094 complete(&task->task_stop_comp);
2095 return;
2096 }
2097
2098 if (!atomic_dec_and_test(&cmd->t_task_cdbs_left)) {
2099 pr_debug("transport task: %p cmd: %p timeout non zero"
2100 " t_task_cdbs_left\n", task, cmd);
2101 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2102 return;
2103 }
2104 pr_debug("transport task: %p cmd: %p timeout ZERO t_task_cdbs_left\n",
2105 task, cmd);
2106
2107 cmd->t_state = TRANSPORT_COMPLETE_FAILURE;
2108 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2109
2110 transport_add_cmd_to_queue(cmd, TRANSPORT_COMPLETE_FAILURE);
2111 }
2112
2113 /*
2114 * Called with cmd->t_state_lock held.
2115 */
2116 static void transport_start_task_timer(struct se_task *task)
2117 {
2118 struct se_device *dev = task->task_se_cmd->se_dev;
2119 int timeout;
2120
2121 if (task->task_flags & TF_TIMER_RUNNING)
2122 return;
2123 /*
2124 * If the task_timeout is disabled, exit now.
2125 */
2126 timeout = dev->se_sub_dev->se_dev_attrib.task_timeout;
2127 if (!timeout)
2128 return;
2129
2130 init_timer(&task->task_timer);
2131 task->task_timer.expires = (get_jiffies_64() + timeout * HZ);
2132 task->task_timer.data = (unsigned long) task;
2133 task->task_timer.function = transport_task_timeout_handler;
2134
2135 task->task_flags |= TF_TIMER_RUNNING;
2136 add_timer(&task->task_timer);
2137 #if 0
2138 pr_debug("Starting task timer for cmd: %p task: %p seconds:"
2139 " %d\n", task->task_se_cmd, task, timeout);
2140 #endif
2141 }
2142
2143 /*
2144 * Called with spin_lock_irq(&cmd->t_state_lock) held.
2145 */
2146 void __transport_stop_task_timer(struct se_task *task, unsigned long *flags)
2147 {
2148 struct se_cmd *cmd = task->task_se_cmd;
2149
2150 if (!(task->task_flags & TF_TIMER_RUNNING))
2151 return;
2152
2153 task->task_flags |= TF_TIMER_STOP;
2154 spin_unlock_irqrestore(&cmd->t_state_lock, *flags);
2155
2156 del_timer_sync(&task->task_timer);
2157
2158 spin_lock_irqsave(&cmd->t_state_lock, *flags);
2159 task->task_flags &= ~TF_TIMER_RUNNING;
2160 task->task_flags &= ~TF_TIMER_STOP;
2161 }
2162
2163 static void transport_stop_all_task_timers(struct se_cmd *cmd)
2164 {
2165 struct se_task *task = NULL, *task_tmp;
2166 unsigned long flags;
2167
2168 spin_lock_irqsave(&cmd->t_state_lock, flags);
2169 list_for_each_entry_safe(task, task_tmp,
2170 &cmd->t_task_list, t_list)
2171 __transport_stop_task_timer(task, &flags);
2172 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2173 }
2174
2175 static inline int transport_tcq_window_closed(struct se_device *dev)
2176 {
2177 if (dev->dev_tcq_window_closed++ <
2178 PYX_TRANSPORT_WINDOW_CLOSED_THRESHOLD) {
2179 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_SHORT);
2180 } else
2181 msleep(PYX_TRANSPORT_WINDOW_CLOSED_WAIT_LONG);
2182
2183 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
2184 return 0;
2185 }
2186
2187 /*
2188 * Called from Fabric Module context from transport_execute_tasks()
2189 *
2190 * The return of this function determins if the tasks from struct se_cmd
2191 * get added to the execution queue in transport_execute_tasks(),
2192 * or are added to the delayed or ordered lists here.
2193 */
2194 static inline int transport_execute_task_attr(struct se_cmd *cmd)
2195 {
2196 if (cmd->se_dev->dev_task_attr_type != SAM_TASK_ATTR_EMULATED)
2197 return 1;
2198 /*
2199 * Check for the existence of HEAD_OF_QUEUE, and if true return 1
2200 * to allow the passed struct se_cmd list of tasks to the front of the list.
2201 */
2202 if (cmd->sam_task_attr == MSG_HEAD_TAG) {
2203 atomic_inc(&cmd->se_dev->dev_hoq_count);
2204 smp_mb__after_atomic_inc();
2205 pr_debug("Added HEAD_OF_QUEUE for CDB:"
2206 " 0x%02x, se_ordered_id: %u\n",
2207 cmd->t_task_cdb[0],
2208 cmd->se_ordered_id);
2209 return 1;
2210 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
2211 spin_lock(&cmd->se_dev->ordered_cmd_lock);
2212 list_add_tail(&cmd->se_ordered_node,
2213 &cmd->se_dev->ordered_cmd_list);
2214 spin_unlock(&cmd->se_dev->ordered_cmd_lock);
2215
2216 atomic_inc(&cmd->se_dev->dev_ordered_sync);
2217 smp_mb__after_atomic_inc();
2218
2219 pr_debug("Added ORDERED for CDB: 0x%02x to ordered"
2220 " list, se_ordered_id: %u\n",
2221 cmd->t_task_cdb[0],
2222 cmd->se_ordered_id);
2223 /*
2224 * Add ORDERED command to tail of execution queue if
2225 * no other older commands exist that need to be
2226 * completed first.
2227 */
2228 if (!atomic_read(&cmd->se_dev->simple_cmds))
2229 return 1;
2230 } else {
2231 /*
2232 * For SIMPLE and UNTAGGED Task Attribute commands
2233 */
2234 atomic_inc(&cmd->se_dev->simple_cmds);
2235 smp_mb__after_atomic_inc();
2236 }
2237 /*
2238 * Otherwise if one or more outstanding ORDERED task attribute exist,
2239 * add the dormant task(s) built for the passed struct se_cmd to the
2240 * execution queue and become in Active state for this struct se_device.
2241 */
2242 if (atomic_read(&cmd->se_dev->dev_ordered_sync) != 0) {
2243 /*
2244 * Otherwise, add cmd w/ tasks to delayed cmd queue that
2245 * will be drained upon completion of HEAD_OF_QUEUE task.
2246 */
2247 spin_lock(&cmd->se_dev->delayed_cmd_lock);
2248 cmd->se_cmd_flags |= SCF_DELAYED_CMD_FROM_SAM_ATTR;
2249 list_add_tail(&cmd->se_delayed_node,
2250 &cmd->se_dev->delayed_cmd_list);
2251 spin_unlock(&cmd->se_dev->delayed_cmd_lock);
2252
2253 pr_debug("Added CDB: 0x%02x Task Attr: 0x%02x to"
2254 " delayed CMD list, se_ordered_id: %u\n",
2255 cmd->t_task_cdb[0], cmd->sam_task_attr,
2256 cmd->se_ordered_id);
2257 /*
2258 * Return zero to let transport_execute_tasks() know
2259 * not to add the delayed tasks to the execution list.
2260 */
2261 return 0;
2262 }
2263 /*
2264 * Otherwise, no ORDERED task attributes exist..
2265 */
2266 return 1;
2267 }
2268
2269 /*
2270 * Called from fabric module context in transport_generic_new_cmd() and
2271 * transport_generic_process_write()
2272 */
2273 static int transport_execute_tasks(struct se_cmd *cmd)
2274 {
2275 int add_tasks;
2276
2277 if (se_dev_check_online(cmd->se_orig_obj_ptr) != 0) {
2278 cmd->transport_error_status = PYX_TRANSPORT_LU_COMM_FAILURE;
2279 transport_generic_request_failure(cmd, NULL, 0, 1);
2280 return 0;
2281 }
2282
2283 /*
2284 * Call transport_cmd_check_stop() to see if a fabric exception
2285 * has occurred that prevents execution.
2286 */
2287 if (!transport_cmd_check_stop(cmd, 0, TRANSPORT_PROCESSING)) {
2288 /*
2289 * Check for SAM Task Attribute emulation and HEAD_OF_QUEUE
2290 * attribute for the tasks of the received struct se_cmd CDB
2291 */
2292 add_tasks = transport_execute_task_attr(cmd);
2293 if (!add_tasks)
2294 goto execute_tasks;
2295 /*
2296 * This calls transport_add_tasks_from_cmd() to handle
2297 * HEAD_OF_QUEUE ordering for SAM Task Attribute emulation
2298 * (if enabled) in __transport_add_task_to_execute_queue() and
2299 * transport_add_task_check_sam_attr().
2300 */
2301 transport_add_tasks_from_cmd(cmd);
2302 }
2303 /*
2304 * Kick the execution queue for the cmd associated struct se_device
2305 * storage object.
2306 */
2307 execute_tasks:
2308 __transport_execute_tasks(cmd->se_dev);
2309 return 0;
2310 }
2311
2312 /*
2313 * Called to check struct se_device tcq depth window, and once open pull struct se_task
2314 * from struct se_device->execute_task_list and
2315 *
2316 * Called from transport_processing_thread()
2317 */
2318 static int __transport_execute_tasks(struct se_device *dev)
2319 {
2320 int error;
2321 struct se_cmd *cmd = NULL;
2322 struct se_task *task = NULL;
2323 unsigned long flags;
2324
2325 /*
2326 * Check if there is enough room in the device and HBA queue to send
2327 * struct se_tasks to the selected transport.
2328 */
2329 check_depth:
2330 if (!atomic_read(&dev->depth_left))
2331 return transport_tcq_window_closed(dev);
2332
2333 dev->dev_tcq_window_closed = 0;
2334
2335 spin_lock_irq(&dev->execute_task_lock);
2336 if (list_empty(&dev->execute_task_list)) {
2337 spin_unlock_irq(&dev->execute_task_lock);
2338 return 0;
2339 }
2340 task = list_first_entry(&dev->execute_task_list,
2341 struct se_task, t_execute_list);
2342 __transport_remove_task_from_execute_queue(task, dev);
2343 spin_unlock_irq(&dev->execute_task_lock);
2344
2345 atomic_dec(&dev->depth_left);
2346
2347 cmd = task->task_se_cmd;
2348
2349 spin_lock_irqsave(&cmd->t_state_lock, flags);
2350 task->task_flags |= (TF_ACTIVE | TF_SENT);
2351 atomic_inc(&cmd->t_task_cdbs_sent);
2352
2353 if (atomic_read(&cmd->t_task_cdbs_sent) ==
2354 cmd->t_task_list_num)
2355 atomic_set(&cmd->transport_sent, 1);
2356
2357 transport_start_task_timer(task);
2358 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2359 /*
2360 * The struct se_cmd->transport_emulate_cdb() function pointer is used
2361 * to grab REPORT_LUNS and other CDBs we want to handle before they hit the
2362 * struct se_subsystem_api->do_task() caller below.
2363 */
2364 if (cmd->transport_emulate_cdb) {
2365 error = cmd->transport_emulate_cdb(cmd);
2366 if (error != 0) {
2367 cmd->transport_error_status = error;
2368 spin_lock_irqsave(&cmd->t_state_lock, flags);
2369 task->task_flags &= ~TF_ACTIVE;
2370 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2371 atomic_set(&cmd->transport_sent, 0);
2372 transport_stop_tasks_for_cmd(cmd);
2373 transport_generic_request_failure(cmd, dev, 0, 1);
2374 goto check_depth;
2375 }
2376 /*
2377 * Handle the successful completion for transport_emulate_cdb()
2378 * for synchronous operation, following SCF_EMULATE_CDB_ASYNC
2379 * Otherwise the caller is expected to complete the task with
2380 * proper status.
2381 */
2382 if (!(cmd->se_cmd_flags & SCF_EMULATE_CDB_ASYNC)) {
2383 cmd->scsi_status = SAM_STAT_GOOD;
2384 task->task_scsi_status = GOOD;
2385 transport_complete_task(task, 1);
2386 }
2387 } else {
2388 /*
2389 * Currently for all virtual TCM plugins including IBLOCK, FILEIO and
2390 * RAMDISK we use the internal transport_emulate_control_cdb() logic
2391 * with struct se_subsystem_api callers for the primary SPC-3 TYPE_DISK
2392 * LUN emulation code.
2393 *
2394 * For TCM/pSCSI and all other SCF_SCSI_DATA_SG_IO_CDB I/O tasks we
2395 * call ->do_task() directly and let the underlying TCM subsystem plugin
2396 * code handle the CDB emulation.
2397 */
2398 if ((dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) &&
2399 (!(task->task_se_cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB)))
2400 error = transport_emulate_control_cdb(task);
2401 else
2402 error = dev->transport->do_task(task);
2403
2404 if (error != 0) {
2405 cmd->transport_error_status = error;
2406 spin_lock_irqsave(&cmd->t_state_lock, flags);
2407 task->task_flags &= ~TF_ACTIVE;
2408 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2409 atomic_set(&cmd->transport_sent, 0);
2410 transport_stop_tasks_for_cmd(cmd);
2411 transport_generic_request_failure(cmd, dev, 0, 1);
2412 }
2413 }
2414
2415 goto check_depth;
2416
2417 return 0;
2418 }
2419
2420 void transport_new_cmd_failure(struct se_cmd *se_cmd)
2421 {
2422 unsigned long flags;
2423 /*
2424 * Any unsolicited data will get dumped for failed command inside of
2425 * the fabric plugin
2426 */
2427 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
2428 se_cmd->se_cmd_flags |= SCF_SE_CMD_FAILED;
2429 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2430 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
2431 }
2432
2433 static inline u32 transport_get_sectors_6(
2434 unsigned char *cdb,
2435 struct se_cmd *cmd,
2436 int *ret)
2437 {
2438 struct se_device *dev = cmd->se_dev;
2439
2440 /*
2441 * Assume TYPE_DISK for non struct se_device objects.
2442 * Use 8-bit sector value.
2443 */
2444 if (!dev)
2445 goto type_disk;
2446
2447 /*
2448 * Use 24-bit allocation length for TYPE_TAPE.
2449 */
2450 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2451 return (u32)(cdb[2] << 16) + (cdb[3] << 8) + cdb[4];
2452
2453 /*
2454 * Everything else assume TYPE_DISK Sector CDB location.
2455 * Use 8-bit sector value.
2456 */
2457 type_disk:
2458 return (u32)cdb[4];
2459 }
2460
2461 static inline u32 transport_get_sectors_10(
2462 unsigned char *cdb,
2463 struct se_cmd *cmd,
2464 int *ret)
2465 {
2466 struct se_device *dev = cmd->se_dev;
2467
2468 /*
2469 * Assume TYPE_DISK for non struct se_device objects.
2470 * Use 16-bit sector value.
2471 */
2472 if (!dev)
2473 goto type_disk;
2474
2475 /*
2476 * XXX_10 is not defined in SSC, throw an exception
2477 */
2478 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2479 *ret = -EINVAL;
2480 return 0;
2481 }
2482
2483 /*
2484 * Everything else assume TYPE_DISK Sector CDB location.
2485 * Use 16-bit sector value.
2486 */
2487 type_disk:
2488 return (u32)(cdb[7] << 8) + cdb[8];
2489 }
2490
2491 static inline u32 transport_get_sectors_12(
2492 unsigned char *cdb,
2493 struct se_cmd *cmd,
2494 int *ret)
2495 {
2496 struct se_device *dev = cmd->se_dev;
2497
2498 /*
2499 * Assume TYPE_DISK for non struct se_device objects.
2500 * Use 32-bit sector value.
2501 */
2502 if (!dev)
2503 goto type_disk;
2504
2505 /*
2506 * XXX_12 is not defined in SSC, throw an exception
2507 */
2508 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2509 *ret = -EINVAL;
2510 return 0;
2511 }
2512
2513 /*
2514 * Everything else assume TYPE_DISK Sector CDB location.
2515 * Use 32-bit sector value.
2516 */
2517 type_disk:
2518 return (u32)(cdb[6] << 24) + (cdb[7] << 16) + (cdb[8] << 8) + cdb[9];
2519 }
2520
2521 static inline u32 transport_get_sectors_16(
2522 unsigned char *cdb,
2523 struct se_cmd *cmd,
2524 int *ret)
2525 {
2526 struct se_device *dev = cmd->se_dev;
2527
2528 /*
2529 * Assume TYPE_DISK for non struct se_device objects.
2530 * Use 32-bit sector value.
2531 */
2532 if (!dev)
2533 goto type_disk;
2534
2535 /*
2536 * Use 24-bit allocation length for TYPE_TAPE.
2537 */
2538 if (dev->transport->get_device_type(dev) == TYPE_TAPE)
2539 return (u32)(cdb[12] << 16) + (cdb[13] << 8) + cdb[14];
2540
2541 type_disk:
2542 return (u32)(cdb[10] << 24) + (cdb[11] << 16) +
2543 (cdb[12] << 8) + cdb[13];
2544 }
2545
2546 /*
2547 * Used for VARIABLE_LENGTH_CDB WRITE_32 and READ_32 variants
2548 */
2549 static inline u32 transport_get_sectors_32(
2550 unsigned char *cdb,
2551 struct se_cmd *cmd,
2552 int *ret)
2553 {
2554 /*
2555 * Assume TYPE_DISK for non struct se_device objects.
2556 * Use 32-bit sector value.
2557 */
2558 return (u32)(cdb[28] << 24) + (cdb[29] << 16) +
2559 (cdb[30] << 8) + cdb[31];
2560
2561 }
2562
2563 static inline u32 transport_get_size(
2564 u32 sectors,
2565 unsigned char *cdb,
2566 struct se_cmd *cmd)
2567 {
2568 struct se_device *dev = cmd->se_dev;
2569
2570 if (dev->transport->get_device_type(dev) == TYPE_TAPE) {
2571 if (cdb[1] & 1) { /* sectors */
2572 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2573 } else /* bytes */
2574 return sectors;
2575 }
2576 #if 0
2577 pr_debug("Returning block_size: %u, sectors: %u == %u for"
2578 " %s object\n", dev->se_sub_dev->se_dev_attrib.block_size, sectors,
2579 dev->se_sub_dev->se_dev_attrib.block_size * sectors,
2580 dev->transport->name);
2581 #endif
2582 return dev->se_sub_dev->se_dev_attrib.block_size * sectors;
2583 }
2584
2585 static void transport_xor_callback(struct se_cmd *cmd)
2586 {
2587 unsigned char *buf, *addr;
2588 struct scatterlist *sg;
2589 unsigned int offset;
2590 int i;
2591 int count;
2592 /*
2593 * From sbc3r22.pdf section 5.48 XDWRITEREAD (10) command
2594 *
2595 * 1) read the specified logical block(s);
2596 * 2) transfer logical blocks from the data-out buffer;
2597 * 3) XOR the logical blocks transferred from the data-out buffer with
2598 * the logical blocks read, storing the resulting XOR data in a buffer;
2599 * 4) if the DISABLE WRITE bit is set to zero, then write the logical
2600 * blocks transferred from the data-out buffer; and
2601 * 5) transfer the resulting XOR data to the data-in buffer.
2602 */
2603 buf = kmalloc(cmd->data_length, GFP_KERNEL);
2604 if (!buf) {
2605 pr_err("Unable to allocate xor_callback buf\n");
2606 return;
2607 }
2608 /*
2609 * Copy the scatterlist WRITE buffer located at cmd->t_data_sg
2610 * into the locally allocated *buf
2611 */
2612 sg_copy_to_buffer(cmd->t_data_sg,
2613 cmd->t_data_nents,
2614 buf,
2615 cmd->data_length);
2616
2617 /*
2618 * Now perform the XOR against the BIDI read memory located at
2619 * cmd->t_mem_bidi_list
2620 */
2621
2622 offset = 0;
2623 for_each_sg(cmd->t_bidi_data_sg, sg, cmd->t_bidi_data_nents, count) {
2624 addr = kmap_atomic(sg_page(sg), KM_USER0);
2625 if (!addr)
2626 goto out;
2627
2628 for (i = 0; i < sg->length; i++)
2629 *(addr + sg->offset + i) ^= *(buf + offset + i);
2630
2631 offset += sg->length;
2632 kunmap_atomic(addr, KM_USER0);
2633 }
2634
2635 out:
2636 kfree(buf);
2637 }
2638
2639 /*
2640 * Used to obtain Sense Data from underlying Linux/SCSI struct scsi_cmnd
2641 */
2642 static int transport_get_sense_data(struct se_cmd *cmd)
2643 {
2644 unsigned char *buffer = cmd->sense_buffer, *sense_buffer = NULL;
2645 struct se_device *dev = cmd->se_dev;
2646 struct se_task *task = NULL, *task_tmp;
2647 unsigned long flags;
2648 u32 offset = 0;
2649
2650 WARN_ON(!cmd->se_lun);
2651
2652 if (!dev)
2653 return 0;
2654
2655 spin_lock_irqsave(&cmd->t_state_lock, flags);
2656 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
2657 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2658 return 0;
2659 }
2660
2661 list_for_each_entry_safe(task, task_tmp,
2662 &cmd->t_task_list, t_list) {
2663 if (!task->task_sense)
2664 continue;
2665
2666 if (!dev->transport->get_sense_buffer) {
2667 pr_err("dev->transport->get_sense_buffer"
2668 " is NULL\n");
2669 continue;
2670 }
2671
2672 sense_buffer = dev->transport->get_sense_buffer(task);
2673 if (!sense_buffer) {
2674 pr_err("ITT[0x%08x]_TASK[%p]: Unable to locate"
2675 " sense buffer for task with sense\n",
2676 cmd->se_tfo->get_task_tag(cmd), task);
2677 continue;
2678 }
2679 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2680
2681 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
2682 TRANSPORT_SENSE_BUFFER);
2683
2684 memcpy(&buffer[offset], sense_buffer,
2685 TRANSPORT_SENSE_BUFFER);
2686 cmd->scsi_status = task->task_scsi_status;
2687 /* Automatically padded */
2688 cmd->scsi_sense_length =
2689 (TRANSPORT_SENSE_BUFFER + offset);
2690
2691 pr_debug("HBA_[%u]_PLUG[%s]: Set SAM STATUS: 0x%02x"
2692 " and sense\n",
2693 dev->se_hba->hba_id, dev->transport->name,
2694 cmd->scsi_status);
2695 return 0;
2696 }
2697 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
2698
2699 return -1;
2700 }
2701
2702 static int
2703 transport_handle_reservation_conflict(struct se_cmd *cmd)
2704 {
2705 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2706 cmd->se_cmd_flags |= SCF_SCSI_RESERVATION_CONFLICT;
2707 cmd->scsi_status = SAM_STAT_RESERVATION_CONFLICT;
2708 /*
2709 * For UA Interlock Code 11b, a RESERVATION CONFLICT will
2710 * establish a UNIT ATTENTION with PREVIOUS RESERVATION
2711 * CONFLICT STATUS.
2712 *
2713 * See spc4r17, section 7.4.6 Control Mode Page, Table 349
2714 */
2715 if (cmd->se_sess &&
2716 cmd->se_dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl == 2)
2717 core_scsi3_ua_allocate(cmd->se_sess->se_node_acl,
2718 cmd->orig_fe_lun, 0x2C,
2719 ASCQ_2CH_PREVIOUS_RESERVATION_CONFLICT_STATUS);
2720 return -EINVAL;
2721 }
2722
2723 static inline long long transport_dev_end_lba(struct se_device *dev)
2724 {
2725 return dev->transport->get_blocks(dev) + 1;
2726 }
2727
2728 static int transport_cmd_get_valid_sectors(struct se_cmd *cmd)
2729 {
2730 struct se_device *dev = cmd->se_dev;
2731 u32 sectors;
2732
2733 if (dev->transport->get_device_type(dev) != TYPE_DISK)
2734 return 0;
2735
2736 sectors = (cmd->data_length / dev->se_sub_dev->se_dev_attrib.block_size);
2737
2738 if ((cmd->t_task_lba + sectors) > transport_dev_end_lba(dev)) {
2739 pr_err("LBA: %llu Sectors: %u exceeds"
2740 " transport_dev_end_lba(): %llu\n",
2741 cmd->t_task_lba, sectors,
2742 transport_dev_end_lba(dev));
2743 return -EINVAL;
2744 }
2745
2746 return 0;
2747 }
2748
2749 static int target_check_write_same_discard(unsigned char *flags, struct se_device *dev)
2750 {
2751 /*
2752 * Determine if the received WRITE_SAME is used to for direct
2753 * passthrough into Linux/SCSI with struct request via TCM/pSCSI
2754 * or we are signaling the use of internal WRITE_SAME + UNMAP=1
2755 * emulation for -> Linux/BLOCK disbard with TCM/IBLOCK code.
2756 */
2757 int passthrough = (dev->transport->transport_type ==
2758 TRANSPORT_PLUGIN_PHBA_PDEV);
2759
2760 if (!passthrough) {
2761 if ((flags[0] & 0x04) || (flags[0] & 0x02)) {
2762 pr_err("WRITE_SAME PBDATA and LBDATA"
2763 " bits not supported for Block Discard"
2764 " Emulation\n");
2765 return -ENOSYS;
2766 }
2767 /*
2768 * Currently for the emulated case we only accept
2769 * tpws with the UNMAP=1 bit set.
2770 */
2771 if (!(flags[0] & 0x08)) {
2772 pr_err("WRITE_SAME w/o UNMAP bit not"
2773 " supported for Block Discard Emulation\n");
2774 return -ENOSYS;
2775 }
2776 }
2777
2778 return 0;
2779 }
2780
2781 /* transport_generic_cmd_sequencer():
2782 *
2783 * Generic Command Sequencer that should work for most DAS transport
2784 * drivers.
2785 *
2786 * Called from transport_generic_allocate_tasks() in the $FABRIC_MOD
2787 * RX Thread.
2788 *
2789 * FIXME: Need to support other SCSI OPCODES where as well.
2790 */
2791 static int transport_generic_cmd_sequencer(
2792 struct se_cmd *cmd,
2793 unsigned char *cdb)
2794 {
2795 struct se_device *dev = cmd->se_dev;
2796 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2797 int ret = 0, sector_ret = 0, passthrough;
2798 u32 sectors = 0, size = 0, pr_reg_type = 0;
2799 u16 service_action;
2800 u8 alua_ascq = 0;
2801 /*
2802 * Check for an existing UNIT ATTENTION condition
2803 */
2804 if (core_scsi3_ua_check(cmd, cdb) < 0) {
2805 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2806 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_UNIT_ATTENTION;
2807 return -EINVAL;
2808 }
2809 /*
2810 * Check status of Asymmetric Logical Unit Assignment port
2811 */
2812 ret = su_dev->t10_alua.alua_state_check(cmd, cdb, &alua_ascq);
2813 if (ret != 0) {
2814 /*
2815 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
2816 * The ALUA additional sense code qualifier (ASCQ) is determined
2817 * by the ALUA primary or secondary access state..
2818 */
2819 if (ret > 0) {
2820 #if 0
2821 pr_debug("[%s]: ALUA TG Port not available,"
2822 " SenseKey: NOT_READY, ASC/ASCQ: 0x04/0x%02x\n",
2823 cmd->se_tfo->get_fabric_name(), alua_ascq);
2824 #endif
2825 transport_set_sense_codes(cmd, 0x04, alua_ascq);
2826 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
2827 cmd->scsi_sense_reason = TCM_CHECK_CONDITION_NOT_READY;
2828 return -EINVAL;
2829 }
2830 goto out_invalid_cdb_field;
2831 }
2832 /*
2833 * Check status for SPC-3 Persistent Reservations
2834 */
2835 if (su_dev->t10_pr.pr_ops.t10_reservation_check(cmd, &pr_reg_type) != 0) {
2836 if (su_dev->t10_pr.pr_ops.t10_seq_non_holder(
2837 cmd, cdb, pr_reg_type) != 0)
2838 return transport_handle_reservation_conflict(cmd);
2839 /*
2840 * This means the CDB is allowed for the SCSI Initiator port
2841 * when said port is *NOT* holding the legacy SPC-2 or
2842 * SPC-3 Persistent Reservation.
2843 */
2844 }
2845
2846 switch (cdb[0]) {
2847 case READ_6:
2848 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2849 if (sector_ret)
2850 goto out_unsupported_cdb;
2851 size = transport_get_size(sectors, cdb, cmd);
2852 cmd->t_task_lba = transport_lba_21(cdb);
2853 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2854 break;
2855 case READ_10:
2856 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2857 if (sector_ret)
2858 goto out_unsupported_cdb;
2859 size = transport_get_size(sectors, cdb, cmd);
2860 cmd->t_task_lba = transport_lba_32(cdb);
2861 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2862 break;
2863 case READ_12:
2864 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2865 if (sector_ret)
2866 goto out_unsupported_cdb;
2867 size = transport_get_size(sectors, cdb, cmd);
2868 cmd->t_task_lba = transport_lba_32(cdb);
2869 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2870 break;
2871 case READ_16:
2872 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2873 if (sector_ret)
2874 goto out_unsupported_cdb;
2875 size = transport_get_size(sectors, cdb, cmd);
2876 cmd->t_task_lba = transport_lba_64(cdb);
2877 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2878 break;
2879 case WRITE_6:
2880 sectors = transport_get_sectors_6(cdb, cmd, &sector_ret);
2881 if (sector_ret)
2882 goto out_unsupported_cdb;
2883 size = transport_get_size(sectors, cdb, cmd);
2884 cmd->t_task_lba = transport_lba_21(cdb);
2885 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2886 break;
2887 case WRITE_10:
2888 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2889 if (sector_ret)
2890 goto out_unsupported_cdb;
2891 size = transport_get_size(sectors, cdb, cmd);
2892 cmd->t_task_lba = transport_lba_32(cdb);
2893 cmd->t_tasks_fua = (cdb[1] & 0x8);
2894 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2895 break;
2896 case WRITE_12:
2897 sectors = transport_get_sectors_12(cdb, cmd, &sector_ret);
2898 if (sector_ret)
2899 goto out_unsupported_cdb;
2900 size = transport_get_size(sectors, cdb, cmd);
2901 cmd->t_task_lba = transport_lba_32(cdb);
2902 cmd->t_tasks_fua = (cdb[1] & 0x8);
2903 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2904 break;
2905 case WRITE_16:
2906 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
2907 if (sector_ret)
2908 goto out_unsupported_cdb;
2909 size = transport_get_size(sectors, cdb, cmd);
2910 cmd->t_task_lba = transport_lba_64(cdb);
2911 cmd->t_tasks_fua = (cdb[1] & 0x8);
2912 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2913 break;
2914 case XDWRITEREAD_10:
2915 if ((cmd->data_direction != DMA_TO_DEVICE) ||
2916 !(cmd->t_tasks_bidi))
2917 goto out_invalid_cdb_field;
2918 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
2919 if (sector_ret)
2920 goto out_unsupported_cdb;
2921 size = transport_get_size(sectors, cdb, cmd);
2922 cmd->t_task_lba = transport_lba_32(cdb);
2923 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2924 passthrough = (dev->transport->transport_type ==
2925 TRANSPORT_PLUGIN_PHBA_PDEV);
2926 /*
2927 * Skip the remaining assignments for TCM/PSCSI passthrough
2928 */
2929 if (passthrough)
2930 break;
2931 /*
2932 * Setup BIDI XOR callback to be run during transport_generic_complete_ok()
2933 */
2934 cmd->transport_complete_callback = &transport_xor_callback;
2935 cmd->t_tasks_fua = (cdb[1] & 0x8);
2936 break;
2937 case VARIABLE_LENGTH_CMD:
2938 service_action = get_unaligned_be16(&cdb[8]);
2939 /*
2940 * Determine if this is TCM/PSCSI device and we should disable
2941 * internal emulation for this CDB.
2942 */
2943 passthrough = (dev->transport->transport_type ==
2944 TRANSPORT_PLUGIN_PHBA_PDEV);
2945
2946 switch (service_action) {
2947 case XDWRITEREAD_32:
2948 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2949 if (sector_ret)
2950 goto out_unsupported_cdb;
2951 size = transport_get_size(sectors, cdb, cmd);
2952 /*
2953 * Use WRITE_32 and READ_32 opcodes for the emulated
2954 * XDWRITE_READ_32 logic.
2955 */
2956 cmd->t_task_lba = transport_lba_64_ext(cdb);
2957 cmd->se_cmd_flags |= SCF_SCSI_DATA_SG_IO_CDB;
2958
2959 /*
2960 * Skip the remaining assignments for TCM/PSCSI passthrough
2961 */
2962 if (passthrough)
2963 break;
2964
2965 /*
2966 * Setup BIDI XOR callback to be run during
2967 * transport_generic_complete_ok()
2968 */
2969 cmd->transport_complete_callback = &transport_xor_callback;
2970 cmd->t_tasks_fua = (cdb[10] & 0x8);
2971 break;
2972 case WRITE_SAME_32:
2973 sectors = transport_get_sectors_32(cdb, cmd, &sector_ret);
2974 if (sector_ret)
2975 goto out_unsupported_cdb;
2976
2977 if (sectors)
2978 size = transport_get_size(1, cdb, cmd);
2979 else {
2980 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not"
2981 " supported\n");
2982 goto out_invalid_cdb_field;
2983 }
2984
2985 cmd->t_task_lba = get_unaligned_be64(&cdb[12]);
2986 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
2987
2988 if (target_check_write_same_discard(&cdb[10], dev) < 0)
2989 goto out_invalid_cdb_field;
2990
2991 break;
2992 default:
2993 pr_err("VARIABLE_LENGTH_CMD service action"
2994 " 0x%04x not supported\n", service_action);
2995 goto out_unsupported_cdb;
2996 }
2997 break;
2998 case MAINTENANCE_IN:
2999 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3000 /* MAINTENANCE_IN from SCC-2 */
3001 /*
3002 * Check for emulated MI_REPORT_TARGET_PGS.
3003 */
3004 if (cdb[1] == MI_REPORT_TARGET_PGS) {
3005 cmd->transport_emulate_cdb =
3006 (su_dev->t10_alua.alua_type ==
3007 SPC3_ALUA_EMULATED) ?
3008 core_emulate_report_target_port_groups :
3009 NULL;
3010 }
3011 size = (cdb[6] << 24) | (cdb[7] << 16) |
3012 (cdb[8] << 8) | cdb[9];
3013 } else {
3014 /* GPCMD_SEND_KEY from multi media commands */
3015 size = (cdb[8] << 8) + cdb[9];
3016 }
3017 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3018 break;
3019 case MODE_SELECT:
3020 size = cdb[4];
3021 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3022 break;
3023 case MODE_SELECT_10:
3024 size = (cdb[7] << 8) + cdb[8];
3025 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3026 break;
3027 case MODE_SENSE:
3028 size = cdb[4];
3029 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3030 break;
3031 case MODE_SENSE_10:
3032 case GPCMD_READ_BUFFER_CAPACITY:
3033 case GPCMD_SEND_OPC:
3034 case LOG_SELECT:
3035 case LOG_SENSE:
3036 size = (cdb[7] << 8) + cdb[8];
3037 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3038 break;
3039 case READ_BLOCK_LIMITS:
3040 size = READ_BLOCK_LEN;
3041 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3042 break;
3043 case GPCMD_GET_CONFIGURATION:
3044 case GPCMD_READ_FORMAT_CAPACITIES:
3045 case GPCMD_READ_DISC_INFO:
3046 case GPCMD_READ_TRACK_RZONE_INFO:
3047 size = (cdb[7] << 8) + cdb[8];
3048 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3049 break;
3050 case PERSISTENT_RESERVE_IN:
3051 case PERSISTENT_RESERVE_OUT:
3052 cmd->transport_emulate_cdb =
3053 (su_dev->t10_pr.res_type ==
3054 SPC3_PERSISTENT_RESERVATIONS) ?
3055 core_scsi3_emulate_pr : NULL;
3056 size = (cdb[7] << 8) + cdb[8];
3057 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3058 break;
3059 case GPCMD_MECHANISM_STATUS:
3060 case GPCMD_READ_DVD_STRUCTURE:
3061 size = (cdb[8] << 8) + cdb[9];
3062 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3063 break;
3064 case READ_POSITION:
3065 size = READ_POSITION_LEN;
3066 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3067 break;
3068 case MAINTENANCE_OUT:
3069 if (dev->transport->get_device_type(dev) != TYPE_ROM) {
3070 /* MAINTENANCE_OUT from SCC-2
3071 *
3072 * Check for emulated MO_SET_TARGET_PGS.
3073 */
3074 if (cdb[1] == MO_SET_TARGET_PGS) {
3075 cmd->transport_emulate_cdb =
3076 (su_dev->t10_alua.alua_type ==
3077 SPC3_ALUA_EMULATED) ?
3078 core_emulate_set_target_port_groups :
3079 NULL;
3080 }
3081
3082 size = (cdb[6] << 24) | (cdb[7] << 16) |
3083 (cdb[8] << 8) | cdb[9];
3084 } else {
3085 /* GPCMD_REPORT_KEY from multi media commands */
3086 size = (cdb[8] << 8) + cdb[9];
3087 }
3088 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3089 break;
3090 case INQUIRY:
3091 size = (cdb[3] << 8) + cdb[4];
3092 /*
3093 * Do implict HEAD_OF_QUEUE processing for INQUIRY.
3094 * See spc4r17 section 5.3
3095 */
3096 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3097 cmd->sam_task_attr = MSG_HEAD_TAG;
3098 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3099 break;
3100 case READ_BUFFER:
3101 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3102 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3103 break;
3104 case READ_CAPACITY:
3105 size = READ_CAP_LEN;
3106 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3107 break;
3108 case READ_MEDIA_SERIAL_NUMBER:
3109 case SECURITY_PROTOCOL_IN:
3110 case SECURITY_PROTOCOL_OUT:
3111 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3112 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3113 break;
3114 case SERVICE_ACTION_IN:
3115 case ACCESS_CONTROL_IN:
3116 case ACCESS_CONTROL_OUT:
3117 case EXTENDED_COPY:
3118 case READ_ATTRIBUTE:
3119 case RECEIVE_COPY_RESULTS:
3120 case WRITE_ATTRIBUTE:
3121 size = (cdb[10] << 24) | (cdb[11] << 16) |
3122 (cdb[12] << 8) | cdb[13];
3123 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3124 break;
3125 case RECEIVE_DIAGNOSTIC:
3126 case SEND_DIAGNOSTIC:
3127 size = (cdb[3] << 8) | cdb[4];
3128 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3129 break;
3130 /* #warning FIXME: Figure out correct GPCMD_READ_CD blocksize. */
3131 #if 0
3132 case GPCMD_READ_CD:
3133 sectors = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3134 size = (2336 * sectors);
3135 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3136 break;
3137 #endif
3138 case READ_TOC:
3139 size = cdb[8];
3140 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3141 break;
3142 case REQUEST_SENSE:
3143 size = cdb[4];
3144 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3145 break;
3146 case READ_ELEMENT_STATUS:
3147 size = 65536 * cdb[7] + 256 * cdb[8] + cdb[9];
3148 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3149 break;
3150 case WRITE_BUFFER:
3151 size = (cdb[6] << 16) + (cdb[7] << 8) + cdb[8];
3152 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3153 break;
3154 case RESERVE:
3155 case RESERVE_10:
3156 /*
3157 * The SPC-2 RESERVE does not contain a size in the SCSI CDB.
3158 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3159 */
3160 if (cdb[0] == RESERVE_10)
3161 size = (cdb[7] << 8) | cdb[8];
3162 else
3163 size = cmd->data_length;
3164
3165 /*
3166 * Setup the legacy emulated handler for SPC-2 and
3167 * >= SPC-3 compatible reservation handling (CRH=1)
3168 * Otherwise, we assume the underlying SCSI logic is
3169 * is running in SPC_PASSTHROUGH, and wants reservations
3170 * emulation disabled.
3171 */
3172 cmd->transport_emulate_cdb =
3173 (su_dev->t10_pr.res_type !=
3174 SPC_PASSTHROUGH) ?
3175 core_scsi2_emulate_crh : NULL;
3176 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3177 break;
3178 case RELEASE:
3179 case RELEASE_10:
3180 /*
3181 * The SPC-2 RELEASE does not contain a size in the SCSI CDB.
3182 * Assume the passthrough or $FABRIC_MOD will tell us about it.
3183 */
3184 if (cdb[0] == RELEASE_10)
3185 size = (cdb[7] << 8) | cdb[8];
3186 else
3187 size = cmd->data_length;
3188
3189 cmd->transport_emulate_cdb =
3190 (su_dev->t10_pr.res_type !=
3191 SPC_PASSTHROUGH) ?
3192 core_scsi2_emulate_crh : NULL;
3193 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3194 break;
3195 case SYNCHRONIZE_CACHE:
3196 case 0x91: /* SYNCHRONIZE_CACHE_16: */
3197 /*
3198 * Extract LBA and range to be flushed for emulated SYNCHRONIZE_CACHE
3199 */
3200 if (cdb[0] == SYNCHRONIZE_CACHE) {
3201 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3202 cmd->t_task_lba = transport_lba_32(cdb);
3203 } else {
3204 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3205 cmd->t_task_lba = transport_lba_64(cdb);
3206 }
3207 if (sector_ret)
3208 goto out_unsupported_cdb;
3209
3210 size = transport_get_size(sectors, cdb, cmd);
3211 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3212
3213 /*
3214 * For TCM/pSCSI passthrough, skip cmd->transport_emulate_cdb()
3215 */
3216 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
3217 break;
3218 /*
3219 * Set SCF_EMULATE_CDB_ASYNC to ensure asynchronous operation
3220 * for SYNCHRONIZE_CACHE* Immed=1 case in __transport_execute_tasks()
3221 */
3222 cmd->se_cmd_flags |= SCF_EMULATE_CDB_ASYNC;
3223 /*
3224 * Check to ensure that LBA + Range does not exceed past end of
3225 * device for IBLOCK and FILEIO ->do_sync_cache() backend calls
3226 */
3227 if ((cmd->t_task_lba != 0) || (sectors != 0)) {
3228 if (transport_cmd_get_valid_sectors(cmd) < 0)
3229 goto out_invalid_cdb_field;
3230 }
3231 break;
3232 case UNMAP:
3233 size = get_unaligned_be16(&cdb[7]);
3234 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3235 break;
3236 case WRITE_SAME_16:
3237 sectors = transport_get_sectors_16(cdb, cmd, &sector_ret);
3238 if (sector_ret)
3239 goto out_unsupported_cdb;
3240
3241 if (sectors)
3242 size = transport_get_size(1, cdb, cmd);
3243 else {
3244 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3245 goto out_invalid_cdb_field;
3246 }
3247
3248 cmd->t_task_lba = get_unaligned_be64(&cdb[2]);
3249 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3250
3251 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3252 goto out_invalid_cdb_field;
3253 break;
3254 case WRITE_SAME:
3255 sectors = transport_get_sectors_10(cdb, cmd, &sector_ret);
3256 if (sector_ret)
3257 goto out_unsupported_cdb;
3258
3259 if (sectors)
3260 size = transport_get_size(1, cdb, cmd);
3261 else {
3262 pr_err("WSNZ=1, WRITE_SAME w/sectors=0 not supported\n");
3263 goto out_invalid_cdb_field;
3264 }
3265
3266 cmd->t_task_lba = get_unaligned_be32(&cdb[2]);
3267 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3268 /*
3269 * Follow sbcr26 with WRITE_SAME (10) and check for the existence
3270 * of byte 1 bit 3 UNMAP instead of original reserved field
3271 */
3272 if (target_check_write_same_discard(&cdb[1], dev) < 0)
3273 goto out_invalid_cdb_field;
3274 break;
3275 case ALLOW_MEDIUM_REMOVAL:
3276 case GPCMD_CLOSE_TRACK:
3277 case ERASE:
3278 case INITIALIZE_ELEMENT_STATUS:
3279 case GPCMD_LOAD_UNLOAD:
3280 case REZERO_UNIT:
3281 case SEEK_10:
3282 case GPCMD_SET_SPEED:
3283 case SPACE:
3284 case START_STOP:
3285 case TEST_UNIT_READY:
3286 case VERIFY:
3287 case WRITE_FILEMARKS:
3288 case MOVE_MEDIUM:
3289 cmd->se_cmd_flags |= SCF_SCSI_NON_DATA_CDB;
3290 break;
3291 case REPORT_LUNS:
3292 cmd->transport_emulate_cdb =
3293 transport_core_report_lun_response;
3294 size = (cdb[6] << 24) | (cdb[7] << 16) | (cdb[8] << 8) | cdb[9];
3295 /*
3296 * Do implict HEAD_OF_QUEUE processing for REPORT_LUNS
3297 * See spc4r17 section 5.3
3298 */
3299 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3300 cmd->sam_task_attr = MSG_HEAD_TAG;
3301 cmd->se_cmd_flags |= SCF_SCSI_CONTROL_SG_IO_CDB;
3302 break;
3303 default:
3304 pr_warn("TARGET_CORE[%s]: Unsupported SCSI Opcode"
3305 " 0x%02x, sending CHECK_CONDITION.\n",
3306 cmd->se_tfo->get_fabric_name(), cdb[0]);
3307 goto out_unsupported_cdb;
3308 }
3309
3310 if (size != cmd->data_length) {
3311 pr_warn("TARGET_CORE[%s]: Expected Transfer Length:"
3312 " %u does not match SCSI CDB Length: %u for SAM Opcode:"
3313 " 0x%02x\n", cmd->se_tfo->get_fabric_name(),
3314 cmd->data_length, size, cdb[0]);
3315
3316 cmd->cmd_spdtl = size;
3317
3318 if (cmd->data_direction == DMA_TO_DEVICE) {
3319 pr_err("Rejecting underflow/overflow"
3320 " WRITE data\n");
3321 goto out_invalid_cdb_field;
3322 }
3323 /*
3324 * Reject READ_* or WRITE_* with overflow/underflow for
3325 * type SCF_SCSI_DATA_SG_IO_CDB.
3326 */
3327 if (!ret && (dev->se_sub_dev->se_dev_attrib.block_size != 512)) {
3328 pr_err("Failing OVERFLOW/UNDERFLOW for LBA op"
3329 " CDB on non 512-byte sector setup subsystem"
3330 " plugin: %s\n", dev->transport->name);
3331 /* Returns CHECK_CONDITION + INVALID_CDB_FIELD */
3332 goto out_invalid_cdb_field;
3333 }
3334
3335 if (size > cmd->data_length) {
3336 cmd->se_cmd_flags |= SCF_OVERFLOW_BIT;
3337 cmd->residual_count = (size - cmd->data_length);
3338 } else {
3339 cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
3340 cmd->residual_count = (cmd->data_length - size);
3341 }
3342 cmd->data_length = size;
3343 }
3344
3345 /* Let's limit control cdbs to a page, for simplicity's sake. */
3346 if ((cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB) &&
3347 size > PAGE_SIZE)
3348 goto out_invalid_cdb_field;
3349
3350 transport_set_supported_SAM_opcode(cmd);
3351 return ret;
3352
3353 out_unsupported_cdb:
3354 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3355 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
3356 return -EINVAL;
3357 out_invalid_cdb_field:
3358 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3359 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
3360 return -EINVAL;
3361 }
3362
3363 /*
3364 * Called from transport_generic_complete_ok() and
3365 * transport_generic_request_failure() to determine which dormant/delayed
3366 * and ordered cmds need to have their tasks added to the execution queue.
3367 */
3368 static void transport_complete_task_attr(struct se_cmd *cmd)
3369 {
3370 struct se_device *dev = cmd->se_dev;
3371 struct se_cmd *cmd_p, *cmd_tmp;
3372 int new_active_tasks = 0;
3373
3374 if (cmd->sam_task_attr == MSG_SIMPLE_TAG) {
3375 atomic_dec(&dev->simple_cmds);
3376 smp_mb__after_atomic_dec();
3377 dev->dev_cur_ordered_id++;
3378 pr_debug("Incremented dev->dev_cur_ordered_id: %u for"
3379 " SIMPLE: %u\n", dev->dev_cur_ordered_id,
3380 cmd->se_ordered_id);
3381 } else if (cmd->sam_task_attr == MSG_HEAD_TAG) {
3382 atomic_dec(&dev->dev_hoq_count);
3383 smp_mb__after_atomic_dec();
3384 dev->dev_cur_ordered_id++;
3385 pr_debug("Incremented dev_cur_ordered_id: %u for"
3386 " HEAD_OF_QUEUE: %u\n", dev->dev_cur_ordered_id,
3387 cmd->se_ordered_id);
3388 } else if (cmd->sam_task_attr == MSG_ORDERED_TAG) {
3389 spin_lock(&dev->ordered_cmd_lock);
3390 list_del(&cmd->se_ordered_node);
3391 atomic_dec(&dev->dev_ordered_sync);
3392 smp_mb__after_atomic_dec();
3393 spin_unlock(&dev->ordered_cmd_lock);
3394
3395 dev->dev_cur_ordered_id++;
3396 pr_debug("Incremented dev_cur_ordered_id: %u for ORDERED:"
3397 " %u\n", dev->dev_cur_ordered_id, cmd->se_ordered_id);
3398 }
3399 /*
3400 * Process all commands up to the last received
3401 * ORDERED task attribute which requires another blocking
3402 * boundary
3403 */
3404 spin_lock(&dev->delayed_cmd_lock);
3405 list_for_each_entry_safe(cmd_p, cmd_tmp,
3406 &dev->delayed_cmd_list, se_delayed_node) {
3407
3408 list_del(&cmd_p->se_delayed_node);
3409 spin_unlock(&dev->delayed_cmd_lock);
3410
3411 pr_debug("Calling add_tasks() for"
3412 " cmd_p: 0x%02x Task Attr: 0x%02x"
3413 " Dormant -> Active, se_ordered_id: %u\n",
3414 cmd_p->t_task_cdb[0],
3415 cmd_p->sam_task_attr, cmd_p->se_ordered_id);
3416
3417 transport_add_tasks_from_cmd(cmd_p);
3418 new_active_tasks++;
3419
3420 spin_lock(&dev->delayed_cmd_lock);
3421 if (cmd_p->sam_task_attr == MSG_ORDERED_TAG)
3422 break;
3423 }
3424 spin_unlock(&dev->delayed_cmd_lock);
3425 /*
3426 * If new tasks have become active, wake up the transport thread
3427 * to do the processing of the Active tasks.
3428 */
3429 if (new_active_tasks != 0)
3430 wake_up_interruptible(&dev->dev_queue_obj.thread_wq);
3431 }
3432
3433 static void transport_complete_qf(struct se_cmd *cmd)
3434 {
3435 int ret = 0;
3436
3437 transport_stop_all_task_timers(cmd);
3438 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3439 transport_complete_task_attr(cmd);
3440
3441 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3442 ret = cmd->se_tfo->queue_status(cmd);
3443 if (ret)
3444 goto out;
3445 }
3446
3447 switch (cmd->data_direction) {
3448 case DMA_FROM_DEVICE:
3449 ret = cmd->se_tfo->queue_data_in(cmd);
3450 break;
3451 case DMA_TO_DEVICE:
3452 if (cmd->t_bidi_data_sg) {
3453 ret = cmd->se_tfo->queue_data_in(cmd);
3454 if (ret < 0)
3455 break;
3456 }
3457 /* Fall through for DMA_TO_DEVICE */
3458 case DMA_NONE:
3459 ret = cmd->se_tfo->queue_status(cmd);
3460 break;
3461 default:
3462 break;
3463 }
3464
3465 out:
3466 if (ret < 0) {
3467 transport_handle_queue_full(cmd, cmd->se_dev);
3468 return;
3469 }
3470 transport_lun_remove_cmd(cmd);
3471 transport_cmd_check_stop_to_fabric(cmd);
3472 }
3473
3474 static void transport_handle_queue_full(
3475 struct se_cmd *cmd,
3476 struct se_device *dev)
3477 {
3478 spin_lock_irq(&dev->qf_cmd_lock);
3479 cmd->se_cmd_flags |= SCF_EMULATE_QUEUE_FULL;
3480 list_add_tail(&cmd->se_qf_node, &cmd->se_dev->qf_cmd_list);
3481 atomic_inc(&dev->dev_qf_count);
3482 smp_mb__after_atomic_inc();
3483 spin_unlock_irq(&cmd->se_dev->qf_cmd_lock);
3484
3485 schedule_work(&cmd->se_dev->qf_work_queue);
3486 }
3487
3488 static void transport_generic_complete_ok(struct se_cmd *cmd)
3489 {
3490 int reason = 0, ret;
3491 /*
3492 * Check if we need to move delayed/dormant tasks from cmds on the
3493 * delayed execution list after a HEAD_OF_QUEUE or ORDERED Task
3494 * Attribute.
3495 */
3496 if (cmd->se_dev->dev_task_attr_type == SAM_TASK_ATTR_EMULATED)
3497 transport_complete_task_attr(cmd);
3498 /*
3499 * Check to schedule QUEUE_FULL work, or execute an existing
3500 * cmd->transport_qf_callback()
3501 */
3502 if (atomic_read(&cmd->se_dev->dev_qf_count) != 0)
3503 schedule_work(&cmd->se_dev->qf_work_queue);
3504
3505 /*
3506 * Check if we need to retrieve a sense buffer from
3507 * the struct se_cmd in question.
3508 */
3509 if (cmd->se_cmd_flags & SCF_TRANSPORT_TASK_SENSE) {
3510 if (transport_get_sense_data(cmd) < 0)
3511 reason = TCM_NON_EXISTENT_LUN;
3512
3513 /*
3514 * Only set when an struct se_task->task_scsi_status returned
3515 * a non GOOD status.
3516 */
3517 if (cmd->scsi_status) {
3518 ret = transport_send_check_condition_and_sense(
3519 cmd, reason, 1);
3520 if (ret == -EAGAIN)
3521 goto queue_full;
3522
3523 transport_lun_remove_cmd(cmd);
3524 transport_cmd_check_stop_to_fabric(cmd);
3525 return;
3526 }
3527 }
3528 /*
3529 * Check for a callback, used by amongst other things
3530 * XDWRITE_READ_10 emulation.
3531 */
3532 if (cmd->transport_complete_callback)
3533 cmd->transport_complete_callback(cmd);
3534
3535 switch (cmd->data_direction) {
3536 case DMA_FROM_DEVICE:
3537 spin_lock(&cmd->se_lun->lun_sep_lock);
3538 if (cmd->se_lun->lun_sep) {
3539 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3540 cmd->data_length;
3541 }
3542 spin_unlock(&cmd->se_lun->lun_sep_lock);
3543
3544 ret = cmd->se_tfo->queue_data_in(cmd);
3545 if (ret == -EAGAIN)
3546 goto queue_full;
3547 break;
3548 case DMA_TO_DEVICE:
3549 spin_lock(&cmd->se_lun->lun_sep_lock);
3550 if (cmd->se_lun->lun_sep) {
3551 cmd->se_lun->lun_sep->sep_stats.rx_data_octets +=
3552 cmd->data_length;
3553 }
3554 spin_unlock(&cmd->se_lun->lun_sep_lock);
3555 /*
3556 * Check if we need to send READ payload for BIDI-COMMAND
3557 */
3558 if (cmd->t_bidi_data_sg) {
3559 spin_lock(&cmd->se_lun->lun_sep_lock);
3560 if (cmd->se_lun->lun_sep) {
3561 cmd->se_lun->lun_sep->sep_stats.tx_data_octets +=
3562 cmd->data_length;
3563 }
3564 spin_unlock(&cmd->se_lun->lun_sep_lock);
3565 ret = cmd->se_tfo->queue_data_in(cmd);
3566 if (ret == -EAGAIN)
3567 goto queue_full;
3568 break;
3569 }
3570 /* Fall through for DMA_TO_DEVICE */
3571 case DMA_NONE:
3572 ret = cmd->se_tfo->queue_status(cmd);
3573 if (ret == -EAGAIN)
3574 goto queue_full;
3575 break;
3576 default:
3577 break;
3578 }
3579
3580 transport_lun_remove_cmd(cmd);
3581 transport_cmd_check_stop_to_fabric(cmd);
3582 return;
3583
3584 queue_full:
3585 pr_debug("Handling complete_ok QUEUE_FULL: se_cmd: %p,"
3586 " data_direction: %d\n", cmd, cmd->data_direction);
3587 cmd->t_state = TRANSPORT_COMPLETE_QF_OK;
3588 transport_handle_queue_full(cmd, cmd->se_dev);
3589 }
3590
3591 static void transport_free_dev_tasks(struct se_cmd *cmd)
3592 {
3593 struct se_task *task, *task_tmp;
3594 unsigned long flags;
3595
3596 spin_lock_irqsave(&cmd->t_state_lock, flags);
3597 list_for_each_entry_safe(task, task_tmp,
3598 &cmd->t_task_list, t_list) {
3599 if (task->task_flags & TF_ACTIVE)
3600 continue;
3601
3602 kfree(task->task_sg_bidi);
3603 kfree(task->task_sg);
3604
3605 list_del(&task->t_list);
3606
3607 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3608 cmd->se_dev->transport->free_task(task);
3609 spin_lock_irqsave(&cmd->t_state_lock, flags);
3610 }
3611 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3612 }
3613
3614 static inline void transport_free_sgl(struct scatterlist *sgl, int nents)
3615 {
3616 struct scatterlist *sg;
3617 int count;
3618
3619 for_each_sg(sgl, sg, nents, count)
3620 __free_page(sg_page(sg));
3621
3622 kfree(sgl);
3623 }
3624
3625 static inline void transport_free_pages(struct se_cmd *cmd)
3626 {
3627 if (cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC)
3628 return;
3629
3630 transport_free_sgl(cmd->t_data_sg, cmd->t_data_nents);
3631 cmd->t_data_sg = NULL;
3632 cmd->t_data_nents = 0;
3633
3634 transport_free_sgl(cmd->t_bidi_data_sg, cmd->t_bidi_data_nents);
3635 cmd->t_bidi_data_sg = NULL;
3636 cmd->t_bidi_data_nents = 0;
3637 }
3638
3639 /**
3640 * transport_put_cmd - release a reference to a command
3641 * @cmd: command to release
3642 *
3643 * This routine releases our reference to the command and frees it if possible.
3644 */
3645 static void transport_put_cmd(struct se_cmd *cmd)
3646 {
3647 unsigned long flags;
3648 int free_tasks = 0;
3649
3650 spin_lock_irqsave(&cmd->t_state_lock, flags);
3651 if (atomic_read(&cmd->t_fe_count)) {
3652 if (!atomic_dec_and_test(&cmd->t_fe_count))
3653 goto out_busy;
3654 }
3655
3656 if (atomic_read(&cmd->t_se_count)) {
3657 if (!atomic_dec_and_test(&cmd->t_se_count))
3658 goto out_busy;
3659 }
3660
3661 if (atomic_read(&cmd->transport_dev_active)) {
3662 atomic_set(&cmd->transport_dev_active, 0);
3663 transport_all_task_dev_remove_state(cmd);
3664 free_tasks = 1;
3665 }
3666 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3667
3668 if (free_tasks != 0)
3669 transport_free_dev_tasks(cmd);
3670
3671 transport_free_pages(cmd);
3672 transport_release_cmd(cmd);
3673 return;
3674 out_busy:
3675 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3676 }
3677
3678 /*
3679 * transport_generic_map_mem_to_cmd - Use fabric-alloced pages instead of
3680 * allocating in the core.
3681 * @cmd: Associated se_cmd descriptor
3682 * @mem: SGL style memory for TCM WRITE / READ
3683 * @sg_mem_num: Number of SGL elements
3684 * @mem_bidi_in: SGL style memory for TCM BIDI READ
3685 * @sg_mem_bidi_num: Number of BIDI READ SGL elements
3686 *
3687 * Return: nonzero return cmd was rejected for -ENOMEM or inproper usage
3688 * of parameters.
3689 */
3690 int transport_generic_map_mem_to_cmd(
3691 struct se_cmd *cmd,
3692 struct scatterlist *sgl,
3693 u32 sgl_count,
3694 struct scatterlist *sgl_bidi,
3695 u32 sgl_bidi_count)
3696 {
3697 if (!sgl || !sgl_count)
3698 return 0;
3699
3700 if ((cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) ||
3701 (cmd->se_cmd_flags & SCF_SCSI_CONTROL_SG_IO_CDB)) {
3702
3703 cmd->t_data_sg = sgl;
3704 cmd->t_data_nents = sgl_count;
3705
3706 if (sgl_bidi && sgl_bidi_count) {
3707 cmd->t_bidi_data_sg = sgl_bidi;
3708 cmd->t_bidi_data_nents = sgl_bidi_count;
3709 }
3710 cmd->se_cmd_flags |= SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC;
3711 }
3712
3713 return 0;
3714 }
3715 EXPORT_SYMBOL(transport_generic_map_mem_to_cmd);
3716
3717 static int transport_new_cmd_obj(struct se_cmd *cmd)
3718 {
3719 struct se_device *dev = cmd->se_dev;
3720 int set_counts = 1, rc, task_cdbs;
3721
3722 /*
3723 * Setup any BIDI READ tasks and memory from
3724 * cmd->t_mem_bidi_list so the READ struct se_tasks
3725 * are queued first for the non pSCSI passthrough case.
3726 */
3727 if (cmd->t_bidi_data_sg &&
3728 (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV)) {
3729 rc = transport_allocate_tasks(cmd,
3730 cmd->t_task_lba,
3731 DMA_FROM_DEVICE,
3732 cmd->t_bidi_data_sg,
3733 cmd->t_bidi_data_nents);
3734 if (rc <= 0) {
3735 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3736 cmd->scsi_sense_reason =
3737 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3738 return -EINVAL;
3739 }
3740 atomic_inc(&cmd->t_fe_count);
3741 atomic_inc(&cmd->t_se_count);
3742 set_counts = 0;
3743 }
3744 /*
3745 * Setup the tasks and memory from cmd->t_mem_list
3746 * Note for BIDI transfers this will contain the WRITE payload
3747 */
3748 task_cdbs = transport_allocate_tasks(cmd,
3749 cmd->t_task_lba,
3750 cmd->data_direction,
3751 cmd->t_data_sg,
3752 cmd->t_data_nents);
3753 if (task_cdbs <= 0) {
3754 cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
3755 cmd->scsi_sense_reason =
3756 TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
3757 return -EINVAL;
3758 }
3759
3760 if (set_counts) {
3761 atomic_inc(&cmd->t_fe_count);
3762 atomic_inc(&cmd->t_se_count);
3763 }
3764
3765 cmd->t_task_list_num = task_cdbs;
3766
3767 atomic_set(&cmd->t_task_cdbs_left, task_cdbs);
3768 atomic_set(&cmd->t_task_cdbs_ex_left, task_cdbs);
3769 atomic_set(&cmd->t_task_cdbs_timeout_left, task_cdbs);
3770 return 0;
3771 }
3772
3773 void *transport_kmap_first_data_page(struct se_cmd *cmd)
3774 {
3775 struct scatterlist *sg = cmd->t_data_sg;
3776
3777 BUG_ON(!sg);
3778 /*
3779 * We need to take into account a possible offset here for fabrics like
3780 * tcm_loop who may be using a contig buffer from the SCSI midlayer for
3781 * control CDBs passed as SGLs via transport_generic_map_mem_to_cmd()
3782 */
3783 return kmap(sg_page(sg)) + sg->offset;
3784 }
3785 EXPORT_SYMBOL(transport_kmap_first_data_page);
3786
3787 void transport_kunmap_first_data_page(struct se_cmd *cmd)
3788 {
3789 kunmap(sg_page(cmd->t_data_sg));
3790 }
3791 EXPORT_SYMBOL(transport_kunmap_first_data_page);
3792
3793 static int
3794 transport_generic_get_mem(struct se_cmd *cmd)
3795 {
3796 u32 length = cmd->data_length;
3797 unsigned int nents;
3798 struct page *page;
3799 int i = 0;
3800
3801 nents = DIV_ROUND_UP(length, PAGE_SIZE);
3802 cmd->t_data_sg = kmalloc(sizeof(struct scatterlist) * nents, GFP_KERNEL);
3803 if (!cmd->t_data_sg)
3804 return -ENOMEM;
3805
3806 cmd->t_data_nents = nents;
3807 sg_init_table(cmd->t_data_sg, nents);
3808
3809 while (length) {
3810 u32 page_len = min_t(u32, length, PAGE_SIZE);
3811 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3812 if (!page)
3813 goto out;
3814
3815 sg_set_page(&cmd->t_data_sg[i], page, page_len, 0);
3816 length -= page_len;
3817 i++;
3818 }
3819 return 0;
3820
3821 out:
3822 while (i >= 0) {
3823 __free_page(sg_page(&cmd->t_data_sg[i]));
3824 i--;
3825 }
3826 kfree(cmd->t_data_sg);
3827 cmd->t_data_sg = NULL;
3828 return -ENOMEM;
3829 }
3830
3831 /* Reduce sectors if they are too long for the device */
3832 static inline sector_t transport_limit_task_sectors(
3833 struct se_device *dev,
3834 unsigned long long lba,
3835 sector_t sectors)
3836 {
3837 sectors = min_t(sector_t, sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
3838
3839 if (dev->transport->get_device_type(dev) == TYPE_DISK)
3840 if ((lba + sectors) > transport_dev_end_lba(dev))
3841 sectors = ((transport_dev_end_lba(dev) - lba) + 1);
3842
3843 return sectors;
3844 }
3845
3846
3847 /*
3848 * This function can be used by HW target mode drivers to create a linked
3849 * scatterlist from all contiguously allocated struct se_task->task_sg[].
3850 * This is intended to be called during the completion path by TCM Core
3851 * when struct target_core_fabric_ops->check_task_sg_chaining is enabled.
3852 */
3853 void transport_do_task_sg_chain(struct se_cmd *cmd)
3854 {
3855 struct scatterlist *sg_first = NULL;
3856 struct scatterlist *sg_prev = NULL;
3857 int sg_prev_nents = 0;
3858 struct scatterlist *sg;
3859 struct se_task *task;
3860 u32 chained_nents = 0;
3861 int i;
3862
3863 BUG_ON(!cmd->se_tfo->task_sg_chaining);
3864
3865 /*
3866 * Walk the struct se_task list and setup scatterlist chains
3867 * for each contiguously allocated struct se_task->task_sg[].
3868 */
3869 list_for_each_entry(task, &cmd->t_task_list, t_list) {
3870 if (!task->task_sg)
3871 continue;
3872
3873 if (!sg_first) {
3874 sg_first = task->task_sg;
3875 chained_nents = task->task_sg_nents;
3876 } else {
3877 sg_chain(sg_prev, sg_prev_nents, task->task_sg);
3878 chained_nents += task->task_sg_nents;
3879 }
3880 /*
3881 * For the padded tasks, use the extra SGL vector allocated
3882 * in transport_allocate_data_tasks() for the sg_prev_nents
3883 * offset into sg_chain() above.
3884 *
3885 * We do not need the padding for the last task (or a single
3886 * task), but in that case we will never use the sg_prev_nents
3887 * value below which would be incorrect.
3888 */
3889 sg_prev_nents = (task->task_sg_nents + 1);
3890 sg_prev = task->task_sg;
3891 }
3892 /*
3893 * Setup the starting pointer and total t_tasks_sg_linked_no including
3894 * padding SGs for linking and to mark the end.
3895 */
3896 cmd->t_tasks_sg_chained = sg_first;
3897 cmd->t_tasks_sg_chained_no = chained_nents;
3898
3899 pr_debug("Setup cmd: %p cmd->t_tasks_sg_chained: %p and"
3900 " t_tasks_sg_chained_no: %u\n", cmd, cmd->t_tasks_sg_chained,
3901 cmd->t_tasks_sg_chained_no);
3902
3903 for_each_sg(cmd->t_tasks_sg_chained, sg,
3904 cmd->t_tasks_sg_chained_no, i) {
3905
3906 pr_debug("SG[%d]: %p page: %p length: %d offset: %d\n",
3907 i, sg, sg_page(sg), sg->length, sg->offset);
3908 if (sg_is_chain(sg))
3909 pr_debug("SG: %p sg_is_chain=1\n", sg);
3910 if (sg_is_last(sg))
3911 pr_debug("SG: %p sg_is_last=1\n", sg);
3912 }
3913 }
3914 EXPORT_SYMBOL(transport_do_task_sg_chain);
3915
3916 /*
3917 * Break up cmd into chunks transport can handle
3918 */
3919 static int transport_allocate_data_tasks(
3920 struct se_cmd *cmd,
3921 unsigned long long lba,
3922 enum dma_data_direction data_direction,
3923 struct scatterlist *sgl,
3924 unsigned int sgl_nents)
3925 {
3926 struct se_task *task;
3927 struct se_device *dev = cmd->se_dev;
3928 unsigned long flags;
3929 int task_count, i;
3930 sector_t sectors, dev_max_sectors = dev->se_sub_dev->se_dev_attrib.max_sectors;
3931 u32 sector_size = dev->se_sub_dev->se_dev_attrib.block_size;
3932 struct scatterlist *sg;
3933 struct scatterlist *cmd_sg;
3934
3935 WARN_ON(cmd->data_length % sector_size);
3936 sectors = DIV_ROUND_UP(cmd->data_length, sector_size);
3937 task_count = DIV_ROUND_UP_SECTOR_T(sectors, dev_max_sectors);
3938
3939 cmd_sg = sgl;
3940 for (i = 0; i < task_count; i++) {
3941 unsigned int task_size, task_sg_nents_padded;
3942 int count;
3943
3944 task = transport_generic_get_task(cmd, data_direction);
3945 if (!task)
3946 return -ENOMEM;
3947
3948 task->task_lba = lba;
3949 task->task_sectors = min(sectors, dev_max_sectors);
3950 task->task_size = task->task_sectors * sector_size;
3951
3952 /*
3953 * This now assumes that passed sg_ents are in PAGE_SIZE chunks
3954 * in order to calculate the number per task SGL entries
3955 */
3956 task->task_sg_nents = DIV_ROUND_UP(task->task_size, PAGE_SIZE);
3957 /*
3958 * Check if the fabric module driver is requesting that all
3959 * struct se_task->task_sg[] be chained together.. If so,
3960 * then allocate an extra padding SG entry for linking and
3961 * marking the end of the chained SGL for every task except
3962 * the last one for (task_count > 1) operation, or skipping
3963 * the extra padding for the (task_count == 1) case.
3964 */
3965 if (cmd->se_tfo->task_sg_chaining && (i < (task_count - 1))) {
3966 task_sg_nents_padded = (task->task_sg_nents + 1);
3967 } else
3968 task_sg_nents_padded = task->task_sg_nents;
3969
3970 task->task_sg = kmalloc(sizeof(struct scatterlist) *
3971 task_sg_nents_padded, GFP_KERNEL);
3972 if (!task->task_sg) {
3973 cmd->se_dev->transport->free_task(task);
3974 return -ENOMEM;
3975 }
3976
3977 sg_init_table(task->task_sg, task_sg_nents_padded);
3978
3979 task_size = task->task_size;
3980
3981 /* Build new sgl, only up to task_size */
3982 for_each_sg(task->task_sg, sg, task->task_sg_nents, count) {
3983 if (cmd_sg->length > task_size)
3984 break;
3985
3986 *sg = *cmd_sg;
3987 task_size -= cmd_sg->length;
3988 cmd_sg = sg_next(cmd_sg);
3989 }
3990
3991 lba += task->task_sectors;
3992 sectors -= task->task_sectors;
3993
3994 spin_lock_irqsave(&cmd->t_state_lock, flags);
3995 list_add_tail(&task->t_list, &cmd->t_task_list);
3996 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
3997 }
3998
3999 return task_count;
4000 }
4001
4002 static int
4003 transport_allocate_control_task(struct se_cmd *cmd)
4004 {
4005 struct se_task *task;
4006 unsigned long flags;
4007
4008 task = transport_generic_get_task(cmd, cmd->data_direction);
4009 if (!task)
4010 return -ENOMEM;
4011
4012 task->task_sg = kmalloc(sizeof(struct scatterlist) * cmd->t_data_nents,
4013 GFP_KERNEL);
4014 if (!task->task_sg) {
4015 cmd->se_dev->transport->free_task(task);
4016 return -ENOMEM;
4017 }
4018
4019 memcpy(task->task_sg, cmd->t_data_sg,
4020 sizeof(struct scatterlist) * cmd->t_data_nents);
4021 task->task_size = cmd->data_length;
4022 task->task_sg_nents = cmd->t_data_nents;
4023
4024 spin_lock_irqsave(&cmd->t_state_lock, flags);
4025 list_add_tail(&task->t_list, &cmd->t_task_list);
4026 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4027
4028 /* Success! Return number of tasks allocated */
4029 return 1;
4030 }
4031
4032 static u32 transport_allocate_tasks(
4033 struct se_cmd *cmd,
4034 unsigned long long lba,
4035 enum dma_data_direction data_direction,
4036 struct scatterlist *sgl,
4037 unsigned int sgl_nents)
4038 {
4039 if (cmd->se_cmd_flags & SCF_SCSI_DATA_SG_IO_CDB) {
4040 if (transport_cmd_get_valid_sectors(cmd) < 0)
4041 return -EINVAL;
4042
4043 return transport_allocate_data_tasks(cmd, lba, data_direction,
4044 sgl, sgl_nents);
4045 } else
4046 return transport_allocate_control_task(cmd);
4047
4048 }
4049
4050
4051 /* transport_generic_new_cmd(): Called from transport_processing_thread()
4052 *
4053 * Allocate storage transport resources from a set of values predefined
4054 * by transport_generic_cmd_sequencer() from the iSCSI Target RX process.
4055 * Any non zero return here is treated as an "out of resource' op here.
4056 */
4057 /*
4058 * Generate struct se_task(s) and/or their payloads for this CDB.
4059 */
4060 int transport_generic_new_cmd(struct se_cmd *cmd)
4061 {
4062 int ret = 0;
4063
4064 /*
4065 * Determine is the TCM fabric module has already allocated physical
4066 * memory, and is directly calling transport_generic_map_mem_to_cmd()
4067 * beforehand.
4068 */
4069 if (!(cmd->se_cmd_flags & SCF_PASSTHROUGH_SG_TO_MEM_NOALLOC) &&
4070 cmd->data_length) {
4071 ret = transport_generic_get_mem(cmd);
4072 if (ret < 0)
4073 return ret;
4074 }
4075 /*
4076 * Call transport_new_cmd_obj() to invoke transport_allocate_tasks() for
4077 * control or data CDB types, and perform the map to backend subsystem
4078 * code from SGL memory allocated here by transport_generic_get_mem(), or
4079 * via pre-existing SGL memory setup explictly by fabric module code with
4080 * transport_generic_map_mem_to_cmd().
4081 */
4082 ret = transport_new_cmd_obj(cmd);
4083 if (ret < 0)
4084 return ret;
4085 /*
4086 * For WRITEs, let the fabric know its buffer is ready..
4087 * This WRITE struct se_cmd (and all of its associated struct se_task's)
4088 * will be added to the struct se_device execution queue after its WRITE
4089 * data has arrived. (ie: It gets handled by the transport processing
4090 * thread a second time)
4091 */
4092 if (cmd->data_direction == DMA_TO_DEVICE) {
4093 transport_add_tasks_to_state_queue(cmd);
4094 return transport_generic_write_pending(cmd);
4095 }
4096 /*
4097 * Everything else but a WRITE, add the struct se_cmd's struct se_task's
4098 * to the execution queue.
4099 */
4100 transport_execute_tasks(cmd);
4101 return 0;
4102 }
4103 EXPORT_SYMBOL(transport_generic_new_cmd);
4104
4105 /* transport_generic_process_write():
4106 *
4107 *
4108 */
4109 void transport_generic_process_write(struct se_cmd *cmd)
4110 {
4111 transport_execute_tasks(cmd);
4112 }
4113 EXPORT_SYMBOL(transport_generic_process_write);
4114
4115 static void transport_write_pending_qf(struct se_cmd *cmd)
4116 {
4117 if (cmd->se_tfo->write_pending(cmd) == -EAGAIN) {
4118 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n",
4119 cmd);
4120 transport_handle_queue_full(cmd, cmd->se_dev);
4121 }
4122 }
4123
4124 static int transport_generic_write_pending(struct se_cmd *cmd)
4125 {
4126 unsigned long flags;
4127 int ret;
4128
4129 spin_lock_irqsave(&cmd->t_state_lock, flags);
4130 cmd->t_state = TRANSPORT_WRITE_PENDING;
4131 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4132
4133 /*
4134 * Clear the se_cmd for WRITE_PENDING status in order to set
4135 * cmd->t_transport_active=0 so that transport_generic_handle_data
4136 * can be called from HW target mode interrupt code. This is safe
4137 * to be called with transport_off=1 before the cmd->se_tfo->write_pending
4138 * because the se_cmd->se_lun pointer is not being cleared.
4139 */
4140 transport_cmd_check_stop(cmd, 1, 0);
4141
4142 /*
4143 * Call the fabric write_pending function here to let the
4144 * frontend know that WRITE buffers are ready.
4145 */
4146 ret = cmd->se_tfo->write_pending(cmd);
4147 if (ret == -EAGAIN)
4148 goto queue_full;
4149 else if (ret < 0)
4150 return ret;
4151
4152 return PYX_TRANSPORT_WRITE_PENDING;
4153
4154 queue_full:
4155 pr_debug("Handling write_pending QUEUE__FULL: se_cmd: %p\n", cmd);
4156 cmd->t_state = TRANSPORT_COMPLETE_QF_WP;
4157 transport_handle_queue_full(cmd, cmd->se_dev);
4158 return ret;
4159 }
4160
4161 /**
4162 * transport_release_cmd - free a command
4163 * @cmd: command to free
4164 *
4165 * This routine unconditionally frees a command, and reference counting
4166 * or list removal must be done in the caller.
4167 */
4168 void transport_release_cmd(struct se_cmd *cmd)
4169 {
4170 BUG_ON(!cmd->se_tfo);
4171
4172 if (cmd->se_tmr_req)
4173 core_tmr_release_req(cmd->se_tmr_req);
4174 if (cmd->t_task_cdb != cmd->__t_task_cdb)
4175 kfree(cmd->t_task_cdb);
4176 cmd->se_tfo->release_cmd(cmd);
4177 }
4178 EXPORT_SYMBOL(transport_release_cmd);
4179
4180 void transport_generic_free_cmd(struct se_cmd *cmd, int wait_for_tasks)
4181 {
4182 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD)) {
4183 if (wait_for_tasks && cmd->se_tmr_req)
4184 transport_wait_for_tasks(cmd);
4185
4186 transport_release_cmd(cmd);
4187 } else {
4188 if (wait_for_tasks)
4189 transport_wait_for_tasks(cmd);
4190
4191 core_dec_lacl_count(cmd->se_sess->se_node_acl, cmd);
4192
4193 if (cmd->se_lun)
4194 transport_lun_remove_cmd(cmd);
4195
4196 transport_free_dev_tasks(cmd);
4197
4198 transport_put_cmd(cmd);
4199 }
4200 }
4201 EXPORT_SYMBOL(transport_generic_free_cmd);
4202
4203 /* transport_lun_wait_for_tasks():
4204 *
4205 * Called from ConfigFS context to stop the passed struct se_cmd to allow
4206 * an struct se_lun to be successfully shutdown.
4207 */
4208 static int transport_lun_wait_for_tasks(struct se_cmd *cmd, struct se_lun *lun)
4209 {
4210 unsigned long flags;
4211 int ret;
4212 /*
4213 * If the frontend has already requested this struct se_cmd to
4214 * be stopped, we can safely ignore this struct se_cmd.
4215 */
4216 spin_lock_irqsave(&cmd->t_state_lock, flags);
4217 if (atomic_read(&cmd->t_transport_stop)) {
4218 atomic_set(&cmd->transport_lun_stop, 0);
4219 pr_debug("ConfigFS ITT[0x%08x] - t_transport_stop =="
4220 " TRUE, skipping\n", cmd->se_tfo->get_task_tag(cmd));
4221 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4222 transport_cmd_check_stop(cmd, 1, 0);
4223 return -EPERM;
4224 }
4225 atomic_set(&cmd->transport_lun_fe_stop, 1);
4226 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4227
4228 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4229
4230 ret = transport_stop_tasks_for_cmd(cmd);
4231
4232 pr_debug("ConfigFS: cmd: %p t_tasks: %d stop tasks ret:"
4233 " %d\n", cmd, cmd->t_task_list_num, ret);
4234 if (!ret) {
4235 pr_debug("ConfigFS: ITT[0x%08x] - stopping cmd....\n",
4236 cmd->se_tfo->get_task_tag(cmd));
4237 wait_for_completion(&cmd->transport_lun_stop_comp);
4238 pr_debug("ConfigFS: ITT[0x%08x] - stopped cmd....\n",
4239 cmd->se_tfo->get_task_tag(cmd));
4240 }
4241 transport_remove_cmd_from_queue(cmd, &cmd->se_dev->dev_queue_obj);
4242
4243 return 0;
4244 }
4245
4246 static void __transport_clear_lun_from_sessions(struct se_lun *lun)
4247 {
4248 struct se_cmd *cmd = NULL;
4249 unsigned long lun_flags, cmd_flags;
4250 /*
4251 * Do exception processing and return CHECK_CONDITION status to the
4252 * Initiator Port.
4253 */
4254 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4255 while (!list_empty(&lun->lun_cmd_list)) {
4256 cmd = list_first_entry(&lun->lun_cmd_list,
4257 struct se_cmd, se_lun_node);
4258 list_del(&cmd->se_lun_node);
4259
4260 atomic_set(&cmd->transport_lun_active, 0);
4261 /*
4262 * This will notify iscsi_target_transport.c:
4263 * transport_cmd_check_stop() that a LUN shutdown is in
4264 * progress for the iscsi_cmd_t.
4265 */
4266 spin_lock(&cmd->t_state_lock);
4267 pr_debug("SE_LUN[%d] - Setting cmd->transport"
4268 "_lun_stop for ITT: 0x%08x\n",
4269 cmd->se_lun->unpacked_lun,
4270 cmd->se_tfo->get_task_tag(cmd));
4271 atomic_set(&cmd->transport_lun_stop, 1);
4272 spin_unlock(&cmd->t_state_lock);
4273
4274 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4275
4276 if (!cmd->se_lun) {
4277 pr_err("ITT: 0x%08x, [i,t]_state: %u/%u\n",
4278 cmd->se_tfo->get_task_tag(cmd),
4279 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state);
4280 BUG();
4281 }
4282 /*
4283 * If the Storage engine still owns the iscsi_cmd_t, determine
4284 * and/or stop its context.
4285 */
4286 pr_debug("SE_LUN[%d] - ITT: 0x%08x before transport"
4287 "_lun_wait_for_tasks()\n", cmd->se_lun->unpacked_lun,
4288 cmd->se_tfo->get_task_tag(cmd));
4289
4290 if (transport_lun_wait_for_tasks(cmd, cmd->se_lun) < 0) {
4291 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4292 continue;
4293 }
4294
4295 pr_debug("SE_LUN[%d] - ITT: 0x%08x after transport_lun"
4296 "_wait_for_tasks(): SUCCESS\n",
4297 cmd->se_lun->unpacked_lun,
4298 cmd->se_tfo->get_task_tag(cmd));
4299
4300 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4301 if (!atomic_read(&cmd->transport_dev_active)) {
4302 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4303 goto check_cond;
4304 }
4305 atomic_set(&cmd->transport_dev_active, 0);
4306 transport_all_task_dev_remove_state(cmd);
4307 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4308
4309 transport_free_dev_tasks(cmd);
4310 /*
4311 * The Storage engine stopped this struct se_cmd before it was
4312 * send to the fabric frontend for delivery back to the
4313 * Initiator Node. Return this SCSI CDB back with an
4314 * CHECK_CONDITION status.
4315 */
4316 check_cond:
4317 transport_send_check_condition_and_sense(cmd,
4318 TCM_NON_EXISTENT_LUN, 0);
4319 /*
4320 * If the fabric frontend is waiting for this iscsi_cmd_t to
4321 * be released, notify the waiting thread now that LU has
4322 * finished accessing it.
4323 */
4324 spin_lock_irqsave(&cmd->t_state_lock, cmd_flags);
4325 if (atomic_read(&cmd->transport_lun_fe_stop)) {
4326 pr_debug("SE_LUN[%d] - Detected FE stop for"
4327 " struct se_cmd: %p ITT: 0x%08x\n",
4328 lun->unpacked_lun,
4329 cmd, cmd->se_tfo->get_task_tag(cmd));
4330
4331 spin_unlock_irqrestore(&cmd->t_state_lock,
4332 cmd_flags);
4333 transport_cmd_check_stop(cmd, 1, 0);
4334 complete(&cmd->transport_lun_fe_stop_comp);
4335 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4336 continue;
4337 }
4338 pr_debug("SE_LUN[%d] - ITT: 0x%08x finished processing\n",
4339 lun->unpacked_lun, cmd->se_tfo->get_task_tag(cmd));
4340
4341 spin_unlock_irqrestore(&cmd->t_state_lock, cmd_flags);
4342 spin_lock_irqsave(&lun->lun_cmd_lock, lun_flags);
4343 }
4344 spin_unlock_irqrestore(&lun->lun_cmd_lock, lun_flags);
4345 }
4346
4347 static int transport_clear_lun_thread(void *p)
4348 {
4349 struct se_lun *lun = (struct se_lun *)p;
4350
4351 __transport_clear_lun_from_sessions(lun);
4352 complete(&lun->lun_shutdown_comp);
4353
4354 return 0;
4355 }
4356
4357 int transport_clear_lun_from_sessions(struct se_lun *lun)
4358 {
4359 struct task_struct *kt;
4360
4361 kt = kthread_run(transport_clear_lun_thread, lun,
4362 "tcm_cl_%u", lun->unpacked_lun);
4363 if (IS_ERR(kt)) {
4364 pr_err("Unable to start clear_lun thread\n");
4365 return PTR_ERR(kt);
4366 }
4367 wait_for_completion(&lun->lun_shutdown_comp);
4368
4369 return 0;
4370 }
4371
4372 /**
4373 * transport_wait_for_tasks - wait for completion to occur
4374 * @cmd: command to wait
4375 *
4376 * Called from frontend fabric context to wait for storage engine
4377 * to pause and/or release frontend generated struct se_cmd.
4378 */
4379 void transport_wait_for_tasks(struct se_cmd *cmd)
4380 {
4381 unsigned long flags;
4382
4383 spin_lock_irqsave(&cmd->t_state_lock, flags);
4384 if (!(cmd->se_cmd_flags & SCF_SE_LUN_CMD) && !(cmd->se_tmr_req)) {
4385 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4386 return;
4387 }
4388 /*
4389 * Only perform a possible wait_for_tasks if SCF_SUPPORTED_SAM_OPCODE
4390 * has been set in transport_set_supported_SAM_opcode().
4391 */
4392 if (!(cmd->se_cmd_flags & SCF_SUPPORTED_SAM_OPCODE) && !cmd->se_tmr_req) {
4393 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4394 return;
4395 }
4396 /*
4397 * If we are already stopped due to an external event (ie: LUN shutdown)
4398 * sleep until the connection can have the passed struct se_cmd back.
4399 * The cmd->transport_lun_stopped_sem will be upped by
4400 * transport_clear_lun_from_sessions() once the ConfigFS context caller
4401 * has completed its operation on the struct se_cmd.
4402 */
4403 if (atomic_read(&cmd->transport_lun_stop)) {
4404
4405 pr_debug("wait_for_tasks: Stopping"
4406 " wait_for_completion(&cmd->t_tasktransport_lun_fe"
4407 "_stop_comp); for ITT: 0x%08x\n",
4408 cmd->se_tfo->get_task_tag(cmd));
4409 /*
4410 * There is a special case for WRITES where a FE exception +
4411 * LUN shutdown means ConfigFS context is still sleeping on
4412 * transport_lun_stop_comp in transport_lun_wait_for_tasks().
4413 * We go ahead and up transport_lun_stop_comp just to be sure
4414 * here.
4415 */
4416 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4417 complete(&cmd->transport_lun_stop_comp);
4418 wait_for_completion(&cmd->transport_lun_fe_stop_comp);
4419 spin_lock_irqsave(&cmd->t_state_lock, flags);
4420
4421 transport_all_task_dev_remove_state(cmd);
4422 /*
4423 * At this point, the frontend who was the originator of this
4424 * struct se_cmd, now owns the structure and can be released through
4425 * normal means below.
4426 */
4427 pr_debug("wait_for_tasks: Stopped"
4428 " wait_for_completion(&cmd->t_tasktransport_lun_fe_"
4429 "stop_comp); for ITT: 0x%08x\n",
4430 cmd->se_tfo->get_task_tag(cmd));
4431
4432 atomic_set(&cmd->transport_lun_stop, 0);
4433 }
4434 if (!atomic_read(&cmd->t_transport_active) ||
4435 atomic_read(&cmd->t_transport_aborted)) {
4436 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4437 return;
4438 }
4439
4440 atomic_set(&cmd->t_transport_stop, 1);
4441
4442 pr_debug("wait_for_tasks: Stopping %p ITT: 0x%08x"
4443 " i_state: %d, t_state/def_t_state: %d/%d, t_transport_stop"
4444 " = TRUE\n", cmd, cmd->se_tfo->get_task_tag(cmd),
4445 cmd->se_tfo->get_cmd_state(cmd), cmd->t_state,
4446 cmd->deferred_t_state);
4447
4448 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4449
4450 wake_up_interruptible(&cmd->se_dev->dev_queue_obj.thread_wq);
4451
4452 wait_for_completion(&cmd->t_transport_stop_comp);
4453
4454 spin_lock_irqsave(&cmd->t_state_lock, flags);
4455 atomic_set(&cmd->t_transport_active, 0);
4456 atomic_set(&cmd->t_transport_stop, 0);
4457
4458 pr_debug("wait_for_tasks: Stopped wait_for_compltion("
4459 "&cmd->t_transport_stop_comp) for ITT: 0x%08x\n",
4460 cmd->se_tfo->get_task_tag(cmd));
4461
4462 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4463 }
4464 EXPORT_SYMBOL(transport_wait_for_tasks);
4465
4466 static int transport_get_sense_codes(
4467 struct se_cmd *cmd,
4468 u8 *asc,
4469 u8 *ascq)
4470 {
4471 *asc = cmd->scsi_asc;
4472 *ascq = cmd->scsi_ascq;
4473
4474 return 0;
4475 }
4476
4477 static int transport_set_sense_codes(
4478 struct se_cmd *cmd,
4479 u8 asc,
4480 u8 ascq)
4481 {
4482 cmd->scsi_asc = asc;
4483 cmd->scsi_ascq = ascq;
4484
4485 return 0;
4486 }
4487
4488 int transport_send_check_condition_and_sense(
4489 struct se_cmd *cmd,
4490 u8 reason,
4491 int from_transport)
4492 {
4493 unsigned char *buffer = cmd->sense_buffer;
4494 unsigned long flags;
4495 int offset;
4496 u8 asc = 0, ascq = 0;
4497
4498 spin_lock_irqsave(&cmd->t_state_lock, flags);
4499 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4500 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4501 return 0;
4502 }
4503 cmd->se_cmd_flags |= SCF_SENT_CHECK_CONDITION;
4504 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4505
4506 if (!reason && from_transport)
4507 goto after_reason;
4508
4509 if (!from_transport)
4510 cmd->se_cmd_flags |= SCF_EMULATED_TASK_SENSE;
4511 /*
4512 * Data Segment and SenseLength of the fabric response PDU.
4513 *
4514 * TRANSPORT_SENSE_BUFFER is now set to SCSI_SENSE_BUFFERSIZE
4515 * from include/scsi/scsi_cmnd.h
4516 */
4517 offset = cmd->se_tfo->set_fabric_sense_len(cmd,
4518 TRANSPORT_SENSE_BUFFER);
4519 /*
4520 * Actual SENSE DATA, see SPC-3 7.23.2 SPC_SENSE_KEY_OFFSET uses
4521 * SENSE KEY values from include/scsi/scsi.h
4522 */
4523 switch (reason) {
4524 case TCM_NON_EXISTENT_LUN:
4525 /* CURRENT ERROR */
4526 buffer[offset] = 0x70;
4527 /* ILLEGAL REQUEST */
4528 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4529 /* LOGICAL UNIT NOT SUPPORTED */
4530 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x25;
4531 break;
4532 case TCM_UNSUPPORTED_SCSI_OPCODE:
4533 case TCM_SECTOR_COUNT_TOO_MANY:
4534 /* CURRENT ERROR */
4535 buffer[offset] = 0x70;
4536 /* ILLEGAL REQUEST */
4537 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4538 /* INVALID COMMAND OPERATION CODE */
4539 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x20;
4540 break;
4541 case TCM_UNKNOWN_MODE_PAGE:
4542 /* CURRENT ERROR */
4543 buffer[offset] = 0x70;
4544 /* ILLEGAL REQUEST */
4545 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4546 /* INVALID FIELD IN CDB */
4547 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4548 break;
4549 case TCM_CHECK_CONDITION_ABORT_CMD:
4550 /* CURRENT ERROR */
4551 buffer[offset] = 0x70;
4552 /* ABORTED COMMAND */
4553 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4554 /* BUS DEVICE RESET FUNCTION OCCURRED */
4555 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x29;
4556 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x03;
4557 break;
4558 case TCM_INCORRECT_AMOUNT_OF_DATA:
4559 /* CURRENT ERROR */
4560 buffer[offset] = 0x70;
4561 /* ABORTED COMMAND */
4562 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4563 /* WRITE ERROR */
4564 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4565 /* NOT ENOUGH UNSOLICITED DATA */
4566 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0d;
4567 break;
4568 case TCM_INVALID_CDB_FIELD:
4569 /* CURRENT ERROR */
4570 buffer[offset] = 0x70;
4571 /* ABORTED COMMAND */
4572 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4573 /* INVALID FIELD IN CDB */
4574 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x24;
4575 break;
4576 case TCM_INVALID_PARAMETER_LIST:
4577 /* CURRENT ERROR */
4578 buffer[offset] = 0x70;
4579 /* ABORTED COMMAND */
4580 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4581 /* INVALID FIELD IN PARAMETER LIST */
4582 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x26;
4583 break;
4584 case TCM_UNEXPECTED_UNSOLICITED_DATA:
4585 /* CURRENT ERROR */
4586 buffer[offset] = 0x70;
4587 /* ABORTED COMMAND */
4588 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4589 /* WRITE ERROR */
4590 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x0c;
4591 /* UNEXPECTED_UNSOLICITED_DATA */
4592 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x0c;
4593 break;
4594 case TCM_SERVICE_CRC_ERROR:
4595 /* CURRENT ERROR */
4596 buffer[offset] = 0x70;
4597 /* ABORTED COMMAND */
4598 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4599 /* PROTOCOL SERVICE CRC ERROR */
4600 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x47;
4601 /* N/A */
4602 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x05;
4603 break;
4604 case TCM_SNACK_REJECTED:
4605 /* CURRENT ERROR */
4606 buffer[offset] = 0x70;
4607 /* ABORTED COMMAND */
4608 buffer[offset+SPC_SENSE_KEY_OFFSET] = ABORTED_COMMAND;
4609 /* READ ERROR */
4610 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x11;
4611 /* FAILED RETRANSMISSION REQUEST */
4612 buffer[offset+SPC_ASCQ_KEY_OFFSET] = 0x13;
4613 break;
4614 case TCM_WRITE_PROTECTED:
4615 /* CURRENT ERROR */
4616 buffer[offset] = 0x70;
4617 /* DATA PROTECT */
4618 buffer[offset+SPC_SENSE_KEY_OFFSET] = DATA_PROTECT;
4619 /* WRITE PROTECTED */
4620 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x27;
4621 break;
4622 case TCM_CHECK_CONDITION_UNIT_ATTENTION:
4623 /* CURRENT ERROR */
4624 buffer[offset] = 0x70;
4625 /* UNIT ATTENTION */
4626 buffer[offset+SPC_SENSE_KEY_OFFSET] = UNIT_ATTENTION;
4627 core_scsi3_ua_for_check_condition(cmd, &asc, &ascq);
4628 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4629 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4630 break;
4631 case TCM_CHECK_CONDITION_NOT_READY:
4632 /* CURRENT ERROR */
4633 buffer[offset] = 0x70;
4634 /* Not Ready */
4635 buffer[offset+SPC_SENSE_KEY_OFFSET] = NOT_READY;
4636 transport_get_sense_codes(cmd, &asc, &ascq);
4637 buffer[offset+SPC_ASC_KEY_OFFSET] = asc;
4638 buffer[offset+SPC_ASCQ_KEY_OFFSET] = ascq;
4639 break;
4640 case TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE:
4641 default:
4642 /* CURRENT ERROR */
4643 buffer[offset] = 0x70;
4644 /* ILLEGAL REQUEST */
4645 buffer[offset+SPC_SENSE_KEY_OFFSET] = ILLEGAL_REQUEST;
4646 /* LOGICAL UNIT COMMUNICATION FAILURE */
4647 buffer[offset+SPC_ASC_KEY_OFFSET] = 0x80;
4648 break;
4649 }
4650 /*
4651 * This code uses linux/include/scsi/scsi.h SAM status codes!
4652 */
4653 cmd->scsi_status = SAM_STAT_CHECK_CONDITION;
4654 /*
4655 * Automatically padded, this value is encoded in the fabric's
4656 * data_length response PDU containing the SCSI defined sense data.
4657 */
4658 cmd->scsi_sense_length = TRANSPORT_SENSE_BUFFER + offset;
4659
4660 after_reason:
4661 return cmd->se_tfo->queue_status(cmd);
4662 }
4663 EXPORT_SYMBOL(transport_send_check_condition_and_sense);
4664
4665 int transport_check_aborted_status(struct se_cmd *cmd, int send_status)
4666 {
4667 int ret = 0;
4668
4669 if (atomic_read(&cmd->t_transport_aborted) != 0) {
4670 if (!send_status ||
4671 (cmd->se_cmd_flags & SCF_SENT_DELAYED_TAS))
4672 return 1;
4673 #if 0
4674 pr_debug("Sending delayed SAM_STAT_TASK_ABORTED"
4675 " status for CDB: 0x%02x ITT: 0x%08x\n",
4676 cmd->t_task_cdb[0],
4677 cmd->se_tfo->get_task_tag(cmd));
4678 #endif
4679 cmd->se_cmd_flags |= SCF_SENT_DELAYED_TAS;
4680 cmd->se_tfo->queue_status(cmd);
4681 ret = 1;
4682 }
4683 return ret;
4684 }
4685 EXPORT_SYMBOL(transport_check_aborted_status);
4686
4687 void transport_send_task_abort(struct se_cmd *cmd)
4688 {
4689 unsigned long flags;
4690
4691 spin_lock_irqsave(&cmd->t_state_lock, flags);
4692 if (cmd->se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
4693 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4694 return;
4695 }
4696 spin_unlock_irqrestore(&cmd->t_state_lock, flags);
4697
4698 /*
4699 * If there are still expected incoming fabric WRITEs, we wait
4700 * until until they have completed before sending a TASK_ABORTED
4701 * response. This response with TASK_ABORTED status will be
4702 * queued back to fabric module by transport_check_aborted_status().
4703 */
4704 if (cmd->data_direction == DMA_TO_DEVICE) {
4705 if (cmd->se_tfo->write_pending_status(cmd) != 0) {
4706 atomic_inc(&cmd->t_transport_aborted);
4707 smp_mb__after_atomic_inc();
4708 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4709 transport_new_cmd_failure(cmd);
4710 return;
4711 }
4712 }
4713 cmd->scsi_status = SAM_STAT_TASK_ABORTED;
4714 #if 0
4715 pr_debug("Setting SAM_STAT_TASK_ABORTED status for CDB: 0x%02x,"
4716 " ITT: 0x%08x\n", cmd->t_task_cdb[0],
4717 cmd->se_tfo->get_task_tag(cmd));
4718 #endif
4719 cmd->se_tfo->queue_status(cmd);
4720 }
4721
4722 /* transport_generic_do_tmr():
4723 *
4724 *
4725 */
4726 int transport_generic_do_tmr(struct se_cmd *cmd)
4727 {
4728 struct se_device *dev = cmd->se_dev;
4729 struct se_tmr_req *tmr = cmd->se_tmr_req;
4730 int ret;
4731
4732 switch (tmr->function) {
4733 case TMR_ABORT_TASK:
4734 tmr->response = TMR_FUNCTION_REJECTED;
4735 break;
4736 case TMR_ABORT_TASK_SET:
4737 case TMR_CLEAR_ACA:
4738 case TMR_CLEAR_TASK_SET:
4739 tmr->response = TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
4740 break;
4741 case TMR_LUN_RESET:
4742 ret = core_tmr_lun_reset(dev, tmr, NULL, NULL);
4743 tmr->response = (!ret) ? TMR_FUNCTION_COMPLETE :
4744 TMR_FUNCTION_REJECTED;
4745 break;
4746 case TMR_TARGET_WARM_RESET:
4747 tmr->response = TMR_FUNCTION_REJECTED;
4748 break;
4749 case TMR_TARGET_COLD_RESET:
4750 tmr->response = TMR_FUNCTION_REJECTED;
4751 break;
4752 default:
4753 pr_err("Uknown TMR function: 0x%02x.\n",
4754 tmr->function);
4755 tmr->response = TMR_FUNCTION_REJECTED;
4756 break;
4757 }
4758
4759 cmd->t_state = TRANSPORT_ISTATE_PROCESSING;
4760 cmd->se_tfo->queue_tm_rsp(cmd);
4761
4762 transport_cmd_check_stop(cmd, 2, 0);
4763 return 0;
4764 }
4765
4766 /* transport_processing_thread():
4767 *
4768 *
4769 */
4770 static int transport_processing_thread(void *param)
4771 {
4772 int ret;
4773 struct se_cmd *cmd;
4774 struct se_device *dev = (struct se_device *) param;
4775
4776 set_user_nice(current, -20);
4777
4778 while (!kthread_should_stop()) {
4779 ret = wait_event_interruptible(dev->dev_queue_obj.thread_wq,
4780 atomic_read(&dev->dev_queue_obj.queue_cnt) ||
4781 kthread_should_stop());
4782 if (ret < 0)
4783 goto out;
4784
4785 get_cmd:
4786 __transport_execute_tasks(dev);
4787
4788 cmd = transport_get_cmd_from_queue(&dev->dev_queue_obj);
4789 if (!cmd)
4790 continue;
4791
4792 switch (cmd->t_state) {
4793 case TRANSPORT_NEW_CMD:
4794 BUG();
4795 break;
4796 case TRANSPORT_NEW_CMD_MAP:
4797 if (!cmd->se_tfo->new_cmd_map) {
4798 pr_err("cmd->se_tfo->new_cmd_map is"
4799 " NULL for TRANSPORT_NEW_CMD_MAP\n");
4800 BUG();
4801 }
4802 ret = cmd->se_tfo->new_cmd_map(cmd);
4803 if (ret < 0) {
4804 cmd->transport_error_status = ret;
4805 transport_generic_request_failure(cmd, NULL,
4806 0, (cmd->data_direction !=
4807 DMA_TO_DEVICE));
4808 break;
4809 }
4810 ret = transport_generic_new_cmd(cmd);
4811 if (ret == -EAGAIN)
4812 break;
4813 else if (ret < 0) {
4814 cmd->transport_error_status = ret;
4815 transport_generic_request_failure(cmd, NULL,
4816 0, (cmd->data_direction !=
4817 DMA_TO_DEVICE));
4818 }
4819 break;
4820 case TRANSPORT_PROCESS_WRITE:
4821 transport_generic_process_write(cmd);
4822 break;
4823 case TRANSPORT_COMPLETE_OK:
4824 transport_stop_all_task_timers(cmd);
4825 transport_generic_complete_ok(cmd);
4826 break;
4827 case TRANSPORT_REMOVE:
4828 transport_put_cmd(cmd);
4829 break;
4830 case TRANSPORT_FREE_CMD_INTR:
4831 transport_generic_free_cmd(cmd, 0);
4832 break;
4833 case TRANSPORT_PROCESS_TMR:
4834 transport_generic_do_tmr(cmd);
4835 break;
4836 case TRANSPORT_COMPLETE_FAILURE:
4837 transport_generic_request_failure(cmd, NULL, 1, 1);
4838 break;
4839 case TRANSPORT_COMPLETE_TIMEOUT:
4840 transport_stop_all_task_timers(cmd);
4841 transport_generic_request_timeout(cmd);
4842 break;
4843 case TRANSPORT_COMPLETE_QF_WP:
4844 transport_write_pending_qf(cmd);
4845 break;
4846 case TRANSPORT_COMPLETE_QF_OK:
4847 transport_complete_qf(cmd);
4848 break;
4849 default:
4850 pr_err("Unknown t_state: %d deferred_t_state:"
4851 " %d for ITT: 0x%08x i_state: %d on SE LUN:"
4852 " %u\n", cmd->t_state, cmd->deferred_t_state,
4853 cmd->se_tfo->get_task_tag(cmd),
4854 cmd->se_tfo->get_cmd_state(cmd),
4855 cmd->se_lun->unpacked_lun);
4856 BUG();
4857 }
4858
4859 goto get_cmd;
4860 }
4861
4862 out:
4863 WARN_ON(!list_empty(&dev->state_task_list));
4864 WARN_ON(!list_empty(&dev->dev_queue_obj.qobj_list));
4865 dev->process_thread = NULL;
4866 return 0;
4867 }
This page took 0.125038 seconds and 4 git commands to generate.