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