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