target: Remove unnecessary *cdb transport_get_lun_for_cmd parameter
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / target / target_core_device.c
1 /*******************************************************************************
2  * Filename:  target_core_device.c (based on iscsi_target_device.c)
3  *
4  * This file contains the iSCSI Virtual Device and Disk Transport
5  * agnostic related functions.
6  *
7  * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
8  * Copyright (c) 2005-2006 SBE, Inc.  All Rights Reserved.
9  * Copyright (c) 2007-2010 Rising Tide Systems
10  * Copyright (c) 2008-2010 Linux-iSCSI.org
11  *
12  * Nicholas A. Bellinger <nab@kernel.org>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  *
28  ******************************************************************************/
29
30 #include <linux/net.h>
31 #include <linux/string.h>
32 #include <linux/delay.h>
33 #include <linux/timer.h>
34 #include <linux/slab.h>
35 #include <linux/spinlock.h>
36 #include <linux/kthread.h>
37 #include <linux/in.h>
38 #include <net/sock.h>
39 #include <net/tcp.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_device.h>
42
43 #include <target/target_core_base.h>
44 #include <target/target_core_device.h>
45 #include <target/target_core_tpg.h>
46 #include <target/target_core_transport.h>
47 #include <target/target_core_fabric_ops.h>
48
49 #include "target_core_alua.h"
50 #include "target_core_hba.h"
51 #include "target_core_pr.h"
52 #include "target_core_ua.h"
53
54 static void se_dev_start(struct se_device *dev);
55 static void se_dev_stop(struct se_device *dev);
56
57 int transport_get_lun_for_cmd(
58         struct se_cmd *se_cmd,
59         u32 unpacked_lun)
60 {
61         struct se_dev_entry *deve;
62         struct se_lun *se_lun = NULL;
63         struct se_session *se_sess = SE_SESS(se_cmd);
64         unsigned long flags;
65         int read_only = 0;
66
67         if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
68                 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
69                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
70                 return -1;
71         }
72
73         spin_lock_irq(&SE_NODE_ACL(se_sess)->device_list_lock);
74         deve = se_cmd->se_deve =
75                         &SE_NODE_ACL(se_sess)->device_list[unpacked_lun];
76         if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
77                 if (se_cmd) {
78                         deve->total_cmds++;
79                         deve->total_bytes += se_cmd->data_length;
80
81                         if (se_cmd->data_direction == DMA_TO_DEVICE) {
82                                 if (deve->lun_flags &
83                                                 TRANSPORT_LUNFLAGS_READ_ONLY) {
84                                         read_only = 1;
85                                         goto out;
86                                 }
87                                 deve->write_bytes += se_cmd->data_length;
88                         } else if (se_cmd->data_direction ==
89                                    DMA_FROM_DEVICE) {
90                                 deve->read_bytes += se_cmd->data_length;
91                         }
92                 }
93                 deve->deve_cmds++;
94
95                 se_lun = se_cmd->se_lun = deve->se_lun;
96                 se_cmd->pr_res_key = deve->pr_res_key;
97                 se_cmd->orig_fe_lun = unpacked_lun;
98                 se_cmd->se_orig_obj_ptr = SE_LUN(se_cmd)->lun_se_dev;
99                 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
100         }
101 out:
102         spin_unlock_irq(&SE_NODE_ACL(se_sess)->device_list_lock);
103
104         if (!se_lun) {
105                 if (read_only) {
106                         se_cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
107                         se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
108                         printk("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
109                                 " Access for 0x%08x\n",
110                                 CMD_TFO(se_cmd)->get_fabric_name(),
111                                 unpacked_lun);
112                         return -1;
113                 } else {
114                         /*
115                          * Use the se_portal_group->tpg_virt_lun0 to allow for
116                          * REPORT_LUNS, et al to be returned when no active
117                          * MappedLUN=0 exists for this Initiator Port.
118                          */
119                         if (unpacked_lun != 0) {
120                                 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
121                                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
122                                 printk("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
123                                         " Access for 0x%08x\n",
124                                         CMD_TFO(se_cmd)->get_fabric_name(),
125                                         unpacked_lun);
126                                 return -1;
127                         }
128                         /*
129                          * Force WRITE PROTECT for virtual LUN 0
130                          */
131                         if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
132                             (se_cmd->data_direction != DMA_NONE)) {
133                                 se_cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
134                                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
135                                 return -1;
136                         }
137 #if 0
138                         printk("TARGET_CORE[%s]: Using virtual LUN0! :-)\n",
139                                 CMD_TFO(se_cmd)->get_fabric_name());
140 #endif
141                         se_lun = se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
142                         se_cmd->orig_fe_lun = 0;
143                         se_cmd->se_orig_obj_ptr = SE_LUN(se_cmd)->lun_se_dev;
144                         se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
145                 }
146         }
147         /*
148          * Determine if the struct se_lun is online.
149          */
150 /* #warning FIXME: Check for LUN_RESET + UNIT Attention */
151         if (se_dev_check_online(se_lun->lun_se_dev) != 0) {
152                 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
153                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
154                 return -1;
155         }
156
157         {
158         struct se_device *dev = se_lun->lun_se_dev;
159         spin_lock_irq(&dev->stats_lock);
160         dev->num_cmds++;
161         if (se_cmd->data_direction == DMA_TO_DEVICE)
162                 dev->write_bytes += se_cmd->data_length;
163         else if (se_cmd->data_direction == DMA_FROM_DEVICE)
164                 dev->read_bytes += se_cmd->data_length;
165         spin_unlock_irq(&dev->stats_lock);
166         }
167
168         /*
169          * Add the iscsi_cmd_t to the struct se_lun's cmd list.  This list is used
170          * for tracking state of struct se_cmds during LUN shutdown events.
171          */
172         spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
173         list_add_tail(&se_cmd->se_lun_list, &se_lun->lun_cmd_list);
174         atomic_set(&T_TASK(se_cmd)->transport_lun_active, 1);
175 #if 0
176         printk(KERN_INFO "Adding ITT: 0x%08x to LUN LIST[%d]\n",
177                 CMD_TFO(se_cmd)->get_task_tag(se_cmd), se_lun->unpacked_lun);
178 #endif
179         spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
180
181         return 0;
182 }
183 EXPORT_SYMBOL(transport_get_lun_for_cmd);
184
185 int transport_get_lun_for_tmr(
186         struct se_cmd *se_cmd,
187         u32 unpacked_lun)
188 {
189         struct se_device *dev = NULL;
190         struct se_dev_entry *deve;
191         struct se_lun *se_lun = NULL;
192         struct se_session *se_sess = SE_SESS(se_cmd);
193         struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
194
195         if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
196                 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
197                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
198                 return -1;
199         }
200
201         spin_lock_irq(&SE_NODE_ACL(se_sess)->device_list_lock);
202         deve = se_cmd->se_deve =
203                         &SE_NODE_ACL(se_sess)->device_list[unpacked_lun];
204         if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
205                 se_lun = se_cmd->se_lun = se_tmr->tmr_lun = deve->se_lun;
206                 dev = se_lun->lun_se_dev;
207                 se_cmd->pr_res_key = deve->pr_res_key;
208                 se_cmd->orig_fe_lun = unpacked_lun;
209                 se_cmd->se_orig_obj_ptr = SE_LUN(se_cmd)->lun_se_dev;
210 /*              se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD; */
211         }
212         spin_unlock_irq(&SE_NODE_ACL(se_sess)->device_list_lock);
213
214         if (!se_lun) {
215                 printk(KERN_INFO "TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
216                         " Access for 0x%08x\n",
217                         CMD_TFO(se_cmd)->get_fabric_name(),
218                         unpacked_lun);
219                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
220                 return -1;
221         }
222         /*
223          * Determine if the struct se_lun is online.
224          */
225 /* #warning FIXME: Check for LUN_RESET + UNIT Attention */
226         if (se_dev_check_online(se_lun->lun_se_dev) != 0) {
227                 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
228                 return -1;
229         }
230         se_tmr->tmr_dev = dev;
231
232         spin_lock(&dev->se_tmr_lock);
233         list_add_tail(&se_tmr->tmr_list, &dev->dev_tmr_list);
234         spin_unlock(&dev->se_tmr_lock);
235
236         return 0;
237 }
238 EXPORT_SYMBOL(transport_get_lun_for_tmr);
239
240 /*
241  * This function is called from core_scsi3_emulate_pro_register_and_move()
242  * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
243  * when a matching rtpi is found.
244  */
245 struct se_dev_entry *core_get_se_deve_from_rtpi(
246         struct se_node_acl *nacl,
247         u16 rtpi)
248 {
249         struct se_dev_entry *deve;
250         struct se_lun *lun;
251         struct se_port *port;
252         struct se_portal_group *tpg = nacl->se_tpg;
253         u32 i;
254
255         spin_lock_irq(&nacl->device_list_lock);
256         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
257                 deve = &nacl->device_list[i];
258
259                 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
260                         continue;
261
262                 lun = deve->se_lun;
263                 if (!(lun)) {
264                         printk(KERN_ERR "%s device entries device pointer is"
265                                 " NULL, but Initiator has access.\n",
266                                 TPG_TFO(tpg)->get_fabric_name());
267                         continue;
268                 }
269                 port = lun->lun_sep;
270                 if (!(port)) {
271                         printk(KERN_ERR "%s device entries device pointer is"
272                                 " NULL, but Initiator has access.\n",
273                                 TPG_TFO(tpg)->get_fabric_name());
274                         continue;
275                 }
276                 if (port->sep_rtpi != rtpi)
277                         continue;
278
279                 atomic_inc(&deve->pr_ref_count);
280                 smp_mb__after_atomic_inc();
281                 spin_unlock_irq(&nacl->device_list_lock);
282
283                 return deve;
284         }
285         spin_unlock_irq(&nacl->device_list_lock);
286
287         return NULL;
288 }
289
290 int core_free_device_list_for_node(
291         struct se_node_acl *nacl,
292         struct se_portal_group *tpg)
293 {
294         struct se_dev_entry *deve;
295         struct se_lun *lun;
296         u32 i;
297
298         if (!nacl->device_list)
299                 return 0;
300
301         spin_lock_irq(&nacl->device_list_lock);
302         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
303                 deve = &nacl->device_list[i];
304
305                 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
306                         continue;
307
308                 if (!deve->se_lun) {
309                         printk(KERN_ERR "%s device entries device pointer is"
310                                 " NULL, but Initiator has access.\n",
311                                 TPG_TFO(tpg)->get_fabric_name());
312                         continue;
313                 }
314                 lun = deve->se_lun;
315
316                 spin_unlock_irq(&nacl->device_list_lock);
317                 core_update_device_list_for_node(lun, NULL, deve->mapped_lun,
318                         TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
319                 spin_lock_irq(&nacl->device_list_lock);
320         }
321         spin_unlock_irq(&nacl->device_list_lock);
322
323         kfree(nacl->device_list);
324         nacl->device_list = NULL;
325
326         return 0;
327 }
328
329 void core_dec_lacl_count(struct se_node_acl *se_nacl, struct se_cmd *se_cmd)
330 {
331         struct se_dev_entry *deve;
332
333         spin_lock_irq(&se_nacl->device_list_lock);
334         deve = &se_nacl->device_list[se_cmd->orig_fe_lun];
335         deve->deve_cmds--;
336         spin_unlock_irq(&se_nacl->device_list_lock);
337
338         return;
339 }
340
341 void core_update_device_list_access(
342         u32 mapped_lun,
343         u32 lun_access,
344         struct se_node_acl *nacl)
345 {
346         struct se_dev_entry *deve;
347
348         spin_lock_irq(&nacl->device_list_lock);
349         deve = &nacl->device_list[mapped_lun];
350         if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
351                 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
352                 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
353         } else {
354                 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
355                 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
356         }
357         spin_unlock_irq(&nacl->device_list_lock);
358
359         return;
360 }
361
362 /*      core_update_device_list_for_node():
363  *
364  *
365  */
366 int core_update_device_list_for_node(
367         struct se_lun *lun,
368         struct se_lun_acl *lun_acl,
369         u32 mapped_lun,
370         u32 lun_access,
371         struct se_node_acl *nacl,
372         struct se_portal_group *tpg,
373         int enable)
374 {
375         struct se_port *port = lun->lun_sep;
376         struct se_dev_entry *deve = &nacl->device_list[mapped_lun];
377         int trans = 0;
378         /*
379          * If the MappedLUN entry is being disabled, the entry in
380          * port->sep_alua_list must be removed now before clearing the
381          * struct se_dev_entry pointers below as logic in
382          * core_alua_do_transition_tg_pt() depends on these being present.
383          */
384         if (!(enable)) {
385                 /*
386                  * deve->se_lun_acl will be NULL for demo-mode created LUNs
387                  * that have not been explicitly concerted to MappedLUNs ->
388                  * struct se_lun_acl, but we remove deve->alua_port_list from
389                  * port->sep_alua_list. This also means that active UAs and
390                  * NodeACL context specific PR metadata for demo-mode
391                  * MappedLUN *deve will be released below..
392                  */
393                 spin_lock_bh(&port->sep_alua_lock);
394                 list_del(&deve->alua_port_list);
395                 spin_unlock_bh(&port->sep_alua_lock);
396         }
397
398         spin_lock_irq(&nacl->device_list_lock);
399         if (enable) {
400                 /*
401                  * Check if the call is handling demo mode -> explict LUN ACL
402                  * transition.  This transition must be for the same struct se_lun
403                  * + mapped_lun that was setup in demo mode..
404                  */
405                 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
406                         if (deve->se_lun_acl != NULL) {
407                                 printk(KERN_ERR "struct se_dev_entry->se_lun_acl"
408                                         " already set for demo mode -> explict"
409                                         " LUN ACL transition\n");
410                                 spin_unlock_irq(&nacl->device_list_lock);
411                                 return -1;
412                         }
413                         if (deve->se_lun != lun) {
414                                 printk(KERN_ERR "struct se_dev_entry->se_lun does"
415                                         " match passed struct se_lun for demo mode"
416                                         " -> explict LUN ACL transition\n");
417                                 spin_unlock_irq(&nacl->device_list_lock);
418                                 return -1;
419                         }
420                         deve->se_lun_acl = lun_acl;
421                         trans = 1;
422                 } else {
423                         deve->se_lun = lun;
424                         deve->se_lun_acl = lun_acl;
425                         deve->mapped_lun = mapped_lun;
426                         deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
427                 }
428
429                 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
430                         deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
431                         deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
432                 } else {
433                         deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
434                         deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
435                 }
436
437                 if (trans) {
438                         spin_unlock_irq(&nacl->device_list_lock);
439                         return 0;
440                 }
441                 deve->creation_time = get_jiffies_64();
442                 deve->attach_count++;
443                 spin_unlock_irq(&nacl->device_list_lock);
444
445                 spin_lock_bh(&port->sep_alua_lock);
446                 list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
447                 spin_unlock_bh(&port->sep_alua_lock);
448
449                 return 0;
450         }
451         /*
452          * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
453          * PR operation to complete.
454          */
455         spin_unlock_irq(&nacl->device_list_lock);
456         while (atomic_read(&deve->pr_ref_count) != 0)
457                 cpu_relax();
458         spin_lock_irq(&nacl->device_list_lock);
459         /*
460          * Disable struct se_dev_entry LUN ACL mapping
461          */
462         core_scsi3_ua_release_all(deve);
463         deve->se_lun = NULL;
464         deve->se_lun_acl = NULL;
465         deve->lun_flags = 0;
466         deve->creation_time = 0;
467         deve->attach_count--;
468         spin_unlock_irq(&nacl->device_list_lock);
469
470         core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
471         return 0;
472 }
473
474 /*      core_clear_lun_from_tpg():
475  *
476  *
477  */
478 void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
479 {
480         struct se_node_acl *nacl;
481         struct se_dev_entry *deve;
482         u32 i;
483
484         spin_lock_bh(&tpg->acl_node_lock);
485         list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
486                 spin_unlock_bh(&tpg->acl_node_lock);
487
488                 spin_lock_irq(&nacl->device_list_lock);
489                 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
490                         deve = &nacl->device_list[i];
491                         if (lun != deve->se_lun)
492                                 continue;
493                         spin_unlock_irq(&nacl->device_list_lock);
494
495                         core_update_device_list_for_node(lun, NULL,
496                                 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
497                                 nacl, tpg, 0);
498
499                         spin_lock_irq(&nacl->device_list_lock);
500                 }
501                 spin_unlock_irq(&nacl->device_list_lock);
502
503                 spin_lock_bh(&tpg->acl_node_lock);
504         }
505         spin_unlock_bh(&tpg->acl_node_lock);
506
507         return;
508 }
509
510 static struct se_port *core_alloc_port(struct se_device *dev)
511 {
512         struct se_port *port, *port_tmp;
513
514         port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
515         if (!(port)) {
516                 printk(KERN_ERR "Unable to allocate struct se_port\n");
517                 return NULL;
518         }
519         INIT_LIST_HEAD(&port->sep_alua_list);
520         INIT_LIST_HEAD(&port->sep_list);
521         atomic_set(&port->sep_tg_pt_secondary_offline, 0);
522         spin_lock_init(&port->sep_alua_lock);
523         mutex_init(&port->sep_tg_pt_md_mutex);
524
525         spin_lock(&dev->se_port_lock);
526         if (dev->dev_port_count == 0x0000ffff) {
527                 printk(KERN_WARNING "Reached dev->dev_port_count =="
528                                 " 0x0000ffff\n");
529                 spin_unlock(&dev->se_port_lock);
530                 return NULL;
531         }
532 again:
533         /*
534          * Allocate the next RELATIVE TARGET PORT IDENTIFER for this struct se_device
535          * Here is the table from spc4r17 section 7.7.3.8.
536          *
537          *    Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
538          *
539          * Code      Description
540          * 0h        Reserved
541          * 1h        Relative port 1, historically known as port A
542          * 2h        Relative port 2, historically known as port B
543          * 3h to FFFFh    Relative port 3 through 65 535
544          */
545         port->sep_rtpi = dev->dev_rpti_counter++;
546         if (!(port->sep_rtpi))
547                 goto again;
548
549         list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
550                 /*
551                  * Make sure RELATIVE TARGET PORT IDENTIFER is unique
552                  * for 16-bit wrap..
553                  */
554                 if (port->sep_rtpi == port_tmp->sep_rtpi)
555                         goto again;
556         }
557         spin_unlock(&dev->se_port_lock);
558
559         return port;
560 }
561
562 static void core_export_port(
563         struct se_device *dev,
564         struct se_portal_group *tpg,
565         struct se_port *port,
566         struct se_lun *lun)
567 {
568         struct se_subsystem_dev *su_dev = SU_DEV(dev);
569         struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
570
571         spin_lock(&dev->se_port_lock);
572         spin_lock(&lun->lun_sep_lock);
573         port->sep_tpg = tpg;
574         port->sep_lun = lun;
575         lun->lun_sep = port;
576         spin_unlock(&lun->lun_sep_lock);
577
578         list_add_tail(&port->sep_list, &dev->dev_sep_list);
579         spin_unlock(&dev->se_port_lock);
580
581         if (T10_ALUA(su_dev)->alua_type == SPC3_ALUA_EMULATED) {
582                 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
583                 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
584                         printk(KERN_ERR "Unable to allocate t10_alua_tg_pt"
585                                         "_gp_member_t\n");
586                         return;
587                 }
588                 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
589                 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
590                         T10_ALUA(su_dev)->default_tg_pt_gp);
591                 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
592                 printk(KERN_INFO "%s/%s: Adding to default ALUA Target Port"
593                         " Group: alua/default_tg_pt_gp\n",
594                         TRANSPORT(dev)->name, TPG_TFO(tpg)->get_fabric_name());
595         }
596
597         dev->dev_port_count++;
598         port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFER */
599 }
600
601 /*
602  *      Called with struct se_device->se_port_lock spinlock held.
603  */
604 static void core_release_port(struct se_device *dev, struct se_port *port)
605         __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
606 {
607         /*
608          * Wait for any port reference for PR ALL_TG_PT=1 operation
609          * to complete in __core_scsi3_alloc_registration()
610          */
611         spin_unlock(&dev->se_port_lock);
612         if (atomic_read(&port->sep_tg_pt_ref_cnt))
613                 cpu_relax();
614         spin_lock(&dev->se_port_lock);
615
616         core_alua_free_tg_pt_gp_mem(port);
617
618         list_del(&port->sep_list);
619         dev->dev_port_count--;
620         kfree(port);
621
622         return;
623 }
624
625 int core_dev_export(
626         struct se_device *dev,
627         struct se_portal_group *tpg,
628         struct se_lun *lun)
629 {
630         struct se_port *port;
631
632         port = core_alloc_port(dev);
633         if (!(port))
634                 return -1;
635
636         lun->lun_se_dev = dev;
637         se_dev_start(dev);
638
639         atomic_inc(&dev->dev_export_obj.obj_access_count);
640         core_export_port(dev, tpg, port, lun);
641         return 0;
642 }
643
644 void core_dev_unexport(
645         struct se_device *dev,
646         struct se_portal_group *tpg,
647         struct se_lun *lun)
648 {
649         struct se_port *port = lun->lun_sep;
650
651         spin_lock(&lun->lun_sep_lock);
652         if (lun->lun_se_dev == NULL) {
653                 spin_unlock(&lun->lun_sep_lock);
654                 return;
655         }
656         spin_unlock(&lun->lun_sep_lock);
657
658         spin_lock(&dev->se_port_lock);
659         atomic_dec(&dev->dev_export_obj.obj_access_count);
660         core_release_port(dev, port);
661         spin_unlock(&dev->se_port_lock);
662
663         se_dev_stop(dev);
664         lun->lun_se_dev = NULL;
665 }
666
667 int transport_core_report_lun_response(struct se_cmd *se_cmd)
668 {
669         struct se_dev_entry *deve;
670         struct se_lun *se_lun;
671         struct se_session *se_sess = SE_SESS(se_cmd);
672         struct se_task *se_task;
673         unsigned char *buf = (unsigned char *)T_TASK(se_cmd)->t_task_buf;
674         u32 cdb_offset = 0, lun_count = 0, offset = 8, i;
675
676         list_for_each_entry(se_task, &T_TASK(se_cmd)->t_task_list, t_list)
677                 break;
678
679         if (!(se_task)) {
680                 printk(KERN_ERR "Unable to locate struct se_task for struct se_cmd\n");
681                 return PYX_TRANSPORT_LU_COMM_FAILURE;
682         }
683
684         /*
685          * If no struct se_session pointer is present, this struct se_cmd is
686          * coming via a target_core_mod PASSTHROUGH op, and not through
687          * a $FABRIC_MOD.  In that case, report LUN=0 only.
688          */
689         if (!(se_sess)) {
690                 int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
691                 lun_count = 1;
692                 goto done;
693         }
694
695         spin_lock_irq(&SE_NODE_ACL(se_sess)->device_list_lock);
696         for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
697                 deve = &SE_NODE_ACL(se_sess)->device_list[i];
698                 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
699                         continue;
700                 se_lun = deve->se_lun;
701                 /*
702                  * We determine the correct LUN LIST LENGTH even once we
703                  * have reached the initial allocation length.
704                  * See SPC2-R20 7.19.
705                  */
706                 lun_count++;
707                 if ((cdb_offset + 8) >= se_cmd->data_length)
708                         continue;
709
710                 int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
711                 offset += 8;
712                 cdb_offset += 8;
713         }
714         spin_unlock_irq(&SE_NODE_ACL(se_sess)->device_list_lock);
715
716         /*
717          * See SPC3 r07, page 159.
718          */
719 done:
720         lun_count *= 8;
721         buf[0] = ((lun_count >> 24) & 0xff);
722         buf[1] = ((lun_count >> 16) & 0xff);
723         buf[2] = ((lun_count >> 8) & 0xff);
724         buf[3] = (lun_count & 0xff);
725
726         return PYX_TRANSPORT_SENT_TO_TRANSPORT;
727 }
728
729 /*      se_release_device_for_hba():
730  *
731  *
732  */
733 void se_release_device_for_hba(struct se_device *dev)
734 {
735         struct se_hba *hba = dev->se_hba;
736
737         if ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) ||
738             (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) ||
739             (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) ||
740             (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_ACTIVATED) ||
741             (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_DEACTIVATED))
742                 se_dev_stop(dev);
743
744         if (dev->dev_ptr) {
745                 kthread_stop(dev->process_thread);
746                 if (dev->transport->free_device)
747                         dev->transport->free_device(dev->dev_ptr);
748         }
749
750         spin_lock(&hba->device_lock);
751         list_del(&dev->dev_list);
752         hba->dev_count--;
753         spin_unlock(&hba->device_lock);
754
755         core_scsi3_free_all_registrations(dev);
756         se_release_vpd_for_dev(dev);
757
758         kfree(dev->dev_status_queue_obj);
759         kfree(dev->dev_queue_obj);
760         kfree(dev);
761
762         return;
763 }
764
765 void se_release_vpd_for_dev(struct se_device *dev)
766 {
767         struct t10_vpd *vpd, *vpd_tmp;
768
769         spin_lock(&DEV_T10_WWN(dev)->t10_vpd_lock);
770         list_for_each_entry_safe(vpd, vpd_tmp,
771                         &DEV_T10_WWN(dev)->t10_vpd_list, vpd_list) {
772                 list_del(&vpd->vpd_list);
773                 kfree(vpd);
774         }
775         spin_unlock(&DEV_T10_WWN(dev)->t10_vpd_lock);
776
777         return;
778 }
779
780 /*      se_free_virtual_device():
781  *
782  *      Used for IBLOCK, RAMDISK, and FILEIO Transport Drivers.
783  */
784 int se_free_virtual_device(struct se_device *dev, struct se_hba *hba)
785 {
786         if (!list_empty(&dev->dev_sep_list))
787                 dump_stack();
788
789         core_alua_free_lu_gp_mem(dev);
790         se_release_device_for_hba(dev);
791
792         return 0;
793 }
794
795 static void se_dev_start(struct se_device *dev)
796 {
797         struct se_hba *hba = dev->se_hba;
798
799         spin_lock(&hba->device_lock);
800         atomic_inc(&dev->dev_obj.obj_access_count);
801         if (atomic_read(&dev->dev_obj.obj_access_count) == 1) {
802                 if (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) {
803                         dev->dev_status &= ~TRANSPORT_DEVICE_DEACTIVATED;
804                         dev->dev_status |= TRANSPORT_DEVICE_ACTIVATED;
805                 } else if (dev->dev_status &
806                            TRANSPORT_DEVICE_OFFLINE_DEACTIVATED) {
807                         dev->dev_status &=
808                                 ~TRANSPORT_DEVICE_OFFLINE_DEACTIVATED;
809                         dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_ACTIVATED;
810                 }
811         }
812         spin_unlock(&hba->device_lock);
813 }
814
815 static void se_dev_stop(struct se_device *dev)
816 {
817         struct se_hba *hba = dev->se_hba;
818
819         spin_lock(&hba->device_lock);
820         atomic_dec(&dev->dev_obj.obj_access_count);
821         if (atomic_read(&dev->dev_obj.obj_access_count) == 0) {
822                 if (dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) {
823                         dev->dev_status &= ~TRANSPORT_DEVICE_ACTIVATED;
824                         dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
825                 } else if (dev->dev_status &
826                            TRANSPORT_DEVICE_OFFLINE_ACTIVATED) {
827                         dev->dev_status &= ~TRANSPORT_DEVICE_OFFLINE_ACTIVATED;
828                         dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_DEACTIVATED;
829                 }
830         }
831         spin_unlock(&hba->device_lock);
832 }
833
834 int se_dev_check_online(struct se_device *dev)
835 {
836         int ret;
837
838         spin_lock_irq(&dev->dev_status_lock);
839         ret = ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) ||
840                (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED)) ? 0 : 1;
841         spin_unlock_irq(&dev->dev_status_lock);
842
843         return ret;
844 }
845
846 int se_dev_check_shutdown(struct se_device *dev)
847 {
848         int ret;
849
850         spin_lock_irq(&dev->dev_status_lock);
851         ret = (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN);
852         spin_unlock_irq(&dev->dev_status_lock);
853
854         return ret;
855 }
856
857 void se_dev_set_default_attribs(
858         struct se_device *dev,
859         struct se_dev_limits *dev_limits)
860 {
861         struct queue_limits *limits = &dev_limits->limits;
862
863         DEV_ATTRIB(dev)->emulate_dpo = DA_EMULATE_DPO;
864         DEV_ATTRIB(dev)->emulate_fua_write = DA_EMULATE_FUA_WRITE;
865         DEV_ATTRIB(dev)->emulate_fua_read = DA_EMULATE_FUA_READ;
866         DEV_ATTRIB(dev)->emulate_write_cache = DA_EMULATE_WRITE_CACHE;
867         DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
868         DEV_ATTRIB(dev)->emulate_tas = DA_EMULATE_TAS;
869         DEV_ATTRIB(dev)->emulate_tpu = DA_EMULATE_TPU;
870         DEV_ATTRIB(dev)->emulate_tpws = DA_EMULATE_TPWS;
871         DEV_ATTRIB(dev)->emulate_reservations = DA_EMULATE_RESERVATIONS;
872         DEV_ATTRIB(dev)->emulate_alua = DA_EMULATE_ALUA;
873         DEV_ATTRIB(dev)->enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
874         /*
875          * The TPU=1 and TPWS=1 settings will be set in TCM/IBLOCK
876          * iblock_create_virtdevice() from struct queue_limits values
877          * if blk_queue_discard()==1
878          */
879         DEV_ATTRIB(dev)->max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
880         DEV_ATTRIB(dev)->max_unmap_block_desc_count =
881                                 DA_MAX_UNMAP_BLOCK_DESC_COUNT;
882         DEV_ATTRIB(dev)->unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
883         DEV_ATTRIB(dev)->unmap_granularity_alignment =
884                                 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
885         /*
886          * block_size is based on subsystem plugin dependent requirements.
887          */
888         DEV_ATTRIB(dev)->hw_block_size = limits->logical_block_size;
889         DEV_ATTRIB(dev)->block_size = limits->logical_block_size;
890         /*
891          * max_sectors is based on subsystem plugin dependent requirements.
892          */
893         DEV_ATTRIB(dev)->hw_max_sectors = limits->max_hw_sectors;
894         DEV_ATTRIB(dev)->max_sectors = limits->max_sectors;
895         /*
896          * Set optimal_sectors from max_sectors, which can be lowered via
897          * configfs.
898          */
899         DEV_ATTRIB(dev)->optimal_sectors = limits->max_sectors;
900         /*
901          * queue_depth is based on subsystem plugin dependent requirements.
902          */
903         DEV_ATTRIB(dev)->hw_queue_depth = dev_limits->hw_queue_depth;
904         DEV_ATTRIB(dev)->queue_depth = dev_limits->queue_depth;
905 }
906
907 int se_dev_set_task_timeout(struct se_device *dev, u32 task_timeout)
908 {
909         if (task_timeout > DA_TASK_TIMEOUT_MAX) {
910                 printk(KERN_ERR "dev[%p]: Passed task_timeout: %u larger then"
911                         " DA_TASK_TIMEOUT_MAX\n", dev, task_timeout);
912                 return -1;
913         } else {
914                 DEV_ATTRIB(dev)->task_timeout = task_timeout;
915                 printk(KERN_INFO "dev[%p]: Set SE Device task_timeout: %u\n",
916                         dev, task_timeout);
917         }
918
919         return 0;
920 }
921
922 int se_dev_set_max_unmap_lba_count(
923         struct se_device *dev,
924         u32 max_unmap_lba_count)
925 {
926         DEV_ATTRIB(dev)->max_unmap_lba_count = max_unmap_lba_count;
927         printk(KERN_INFO "dev[%p]: Set max_unmap_lba_count: %u\n",
928                         dev, DEV_ATTRIB(dev)->max_unmap_lba_count);
929         return 0;
930 }
931
932 int se_dev_set_max_unmap_block_desc_count(
933         struct se_device *dev,
934         u32 max_unmap_block_desc_count)
935 {
936         DEV_ATTRIB(dev)->max_unmap_block_desc_count = max_unmap_block_desc_count;
937         printk(KERN_INFO "dev[%p]: Set max_unmap_block_desc_count: %u\n",
938                         dev, DEV_ATTRIB(dev)->max_unmap_block_desc_count);
939         return 0;
940 }
941
942 int se_dev_set_unmap_granularity(
943         struct se_device *dev,
944         u32 unmap_granularity)
945 {
946         DEV_ATTRIB(dev)->unmap_granularity = unmap_granularity;
947         printk(KERN_INFO "dev[%p]: Set unmap_granularity: %u\n",
948                         dev, DEV_ATTRIB(dev)->unmap_granularity);
949         return 0;
950 }
951
952 int se_dev_set_unmap_granularity_alignment(
953         struct se_device *dev,
954         u32 unmap_granularity_alignment)
955 {
956         DEV_ATTRIB(dev)->unmap_granularity_alignment = unmap_granularity_alignment;
957         printk(KERN_INFO "dev[%p]: Set unmap_granularity_alignment: %u\n",
958                         dev, DEV_ATTRIB(dev)->unmap_granularity_alignment);
959         return 0;
960 }
961
962 int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
963 {
964         if ((flag != 0) && (flag != 1)) {
965                 printk(KERN_ERR "Illegal value %d\n", flag);
966                 return -1;
967         }
968         if (TRANSPORT(dev)->dpo_emulated == NULL) {
969                 printk(KERN_ERR "TRANSPORT(dev)->dpo_emulated is NULL\n");
970                 return -1;
971         }
972         if (TRANSPORT(dev)->dpo_emulated(dev) == 0) {
973                 printk(KERN_ERR "TRANSPORT(dev)->dpo_emulated not supported\n");
974                 return -1;
975         }
976         DEV_ATTRIB(dev)->emulate_dpo = flag;
977         printk(KERN_INFO "dev[%p]: SE Device Page Out (DPO) Emulation"
978                         " bit: %d\n", dev, DEV_ATTRIB(dev)->emulate_dpo);
979         return 0;
980 }
981
982 int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
983 {
984         if ((flag != 0) && (flag != 1)) {
985                 printk(KERN_ERR "Illegal value %d\n", flag);
986                 return -1;
987         }
988         if (TRANSPORT(dev)->fua_write_emulated == NULL) {
989                 printk(KERN_ERR "TRANSPORT(dev)->fua_write_emulated is NULL\n");
990                 return -1;
991         }
992         if (TRANSPORT(dev)->fua_write_emulated(dev) == 0) {
993                 printk(KERN_ERR "TRANSPORT(dev)->fua_write_emulated not supported\n");
994                 return -1;
995         }
996         DEV_ATTRIB(dev)->emulate_fua_write = flag;
997         printk(KERN_INFO "dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
998                         dev, DEV_ATTRIB(dev)->emulate_fua_write);
999         return 0;
1000 }
1001
1002 int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
1003 {
1004         if ((flag != 0) && (flag != 1)) {
1005                 printk(KERN_ERR "Illegal value %d\n", flag);
1006                 return -1;
1007         }
1008         if (TRANSPORT(dev)->fua_read_emulated == NULL) {
1009                 printk(KERN_ERR "TRANSPORT(dev)->fua_read_emulated is NULL\n");
1010                 return -1;
1011         }
1012         if (TRANSPORT(dev)->fua_read_emulated(dev) == 0) {
1013                 printk(KERN_ERR "TRANSPORT(dev)->fua_read_emulated not supported\n");
1014                 return -1;
1015         }
1016         DEV_ATTRIB(dev)->emulate_fua_read = flag;
1017         printk(KERN_INFO "dev[%p]: SE Device Forced Unit Access READs: %d\n",
1018                         dev, DEV_ATTRIB(dev)->emulate_fua_read);
1019         return 0;
1020 }
1021
1022 int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
1023 {
1024         if ((flag != 0) && (flag != 1)) {
1025                 printk(KERN_ERR "Illegal value %d\n", flag);
1026                 return -1;
1027         }
1028         if (TRANSPORT(dev)->write_cache_emulated == NULL) {
1029                 printk(KERN_ERR "TRANSPORT(dev)->write_cache_emulated is NULL\n");
1030                 return -1;
1031         }
1032         if (TRANSPORT(dev)->write_cache_emulated(dev) == 0) {
1033                 printk(KERN_ERR "TRANSPORT(dev)->write_cache_emulated not supported\n");
1034                 return -1;
1035         }
1036         DEV_ATTRIB(dev)->emulate_write_cache = flag;
1037         printk(KERN_INFO "dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
1038                         dev, DEV_ATTRIB(dev)->emulate_write_cache);
1039         return 0;
1040 }
1041
1042 int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
1043 {
1044         if ((flag != 0) && (flag != 1) && (flag != 2)) {
1045                 printk(KERN_ERR "Illegal value %d\n", flag);
1046                 return -1;
1047         }
1048
1049         if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1050                 printk(KERN_ERR "dev[%p]: Unable to change SE Device"
1051                         " UA_INTRLCK_CTRL while dev_export_obj: %d count"
1052                         " exists\n", dev,
1053                         atomic_read(&dev->dev_export_obj.obj_access_count));
1054                 return -1;
1055         }
1056         DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl = flag;
1057         printk(KERN_INFO "dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
1058                 dev, DEV_ATTRIB(dev)->emulate_ua_intlck_ctrl);
1059
1060         return 0;
1061 }
1062
1063 int se_dev_set_emulate_tas(struct se_device *dev, int flag)
1064 {
1065         if ((flag != 0) && (flag != 1)) {
1066                 printk(KERN_ERR "Illegal value %d\n", flag);
1067                 return -1;
1068         }
1069
1070         if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1071                 printk(KERN_ERR "dev[%p]: Unable to change SE Device TAS while"
1072                         " dev_export_obj: %d count exists\n", dev,
1073                         atomic_read(&dev->dev_export_obj.obj_access_count));
1074                 return -1;
1075         }
1076         DEV_ATTRIB(dev)->emulate_tas = flag;
1077         printk(KERN_INFO "dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
1078                 dev, (DEV_ATTRIB(dev)->emulate_tas) ? "Enabled" : "Disabled");
1079
1080         return 0;
1081 }
1082
1083 int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
1084 {
1085         if ((flag != 0) && (flag != 1)) {
1086                 printk(KERN_ERR "Illegal value %d\n", flag);
1087                 return -1;
1088         }
1089         /*
1090          * We expect this value to be non-zero when generic Block Layer
1091          * Discard supported is detected iblock_create_virtdevice().
1092          */
1093         if (!(DEV_ATTRIB(dev)->max_unmap_block_desc_count)) {
1094                 printk(KERN_ERR "Generic Block Discard not supported\n");
1095                 return -ENOSYS;
1096         }
1097
1098         DEV_ATTRIB(dev)->emulate_tpu = flag;
1099         printk(KERN_INFO "dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
1100                                 dev, flag);
1101         return 0;
1102 }
1103
1104 int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
1105 {
1106         if ((flag != 0) && (flag != 1)) {
1107                 printk(KERN_ERR "Illegal value %d\n", flag);
1108                 return -1;
1109         }
1110         /*
1111          * We expect this value to be non-zero when generic Block Layer
1112          * Discard supported is detected iblock_create_virtdevice().
1113          */
1114         if (!(DEV_ATTRIB(dev)->max_unmap_block_desc_count)) {
1115                 printk(KERN_ERR "Generic Block Discard not supported\n");
1116                 return -ENOSYS;
1117         }
1118
1119         DEV_ATTRIB(dev)->emulate_tpws = flag;
1120         printk(KERN_INFO "dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
1121                                 dev, flag);
1122         return 0;
1123 }
1124
1125 int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
1126 {
1127         if ((flag != 0) && (flag != 1)) {
1128                 printk(KERN_ERR "Illegal value %d\n", flag);
1129                 return -1;
1130         }
1131         DEV_ATTRIB(dev)->enforce_pr_isids = flag;
1132         printk(KERN_INFO "dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
1133                 (DEV_ATTRIB(dev)->enforce_pr_isids) ? "Enabled" : "Disabled");
1134         return 0;
1135 }
1136
1137 /*
1138  * Note, this can only be called on unexported SE Device Object.
1139  */
1140 int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
1141 {
1142         u32 orig_queue_depth = dev->queue_depth;
1143
1144         if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1145                 printk(KERN_ERR "dev[%p]: Unable to change SE Device TCQ while"
1146                         " dev_export_obj: %d count exists\n", dev,
1147                         atomic_read(&dev->dev_export_obj.obj_access_count));
1148                 return -1;
1149         }
1150         if (!(queue_depth)) {
1151                 printk(KERN_ERR "dev[%p]: Illegal ZERO value for queue"
1152                         "_depth\n", dev);
1153                 return -1;
1154         }
1155
1156         if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1157                 if (queue_depth > DEV_ATTRIB(dev)->hw_queue_depth) {
1158                         printk(KERN_ERR "dev[%p]: Passed queue_depth: %u"
1159                                 " exceeds TCM/SE_Device TCQ: %u\n",
1160                                 dev, queue_depth,
1161                                 DEV_ATTRIB(dev)->hw_queue_depth);
1162                         return -1;
1163                 }
1164         } else {
1165                 if (queue_depth > DEV_ATTRIB(dev)->queue_depth) {
1166                         if (queue_depth > DEV_ATTRIB(dev)->hw_queue_depth) {
1167                                 printk(KERN_ERR "dev[%p]: Passed queue_depth:"
1168                                         " %u exceeds TCM/SE_Device MAX"
1169                                         " TCQ: %u\n", dev, queue_depth,
1170                                         DEV_ATTRIB(dev)->hw_queue_depth);
1171                                 return -1;
1172                         }
1173                 }
1174         }
1175
1176         DEV_ATTRIB(dev)->queue_depth = dev->queue_depth = queue_depth;
1177         if (queue_depth > orig_queue_depth)
1178                 atomic_add(queue_depth - orig_queue_depth, &dev->depth_left);
1179         else if (queue_depth < orig_queue_depth)
1180                 atomic_sub(orig_queue_depth - queue_depth, &dev->depth_left);
1181
1182         printk(KERN_INFO "dev[%p]: SE Device TCQ Depth changed to: %u\n",
1183                         dev, queue_depth);
1184         return 0;
1185 }
1186
1187 int se_dev_set_max_sectors(struct se_device *dev, u32 max_sectors)
1188 {
1189         int force = 0; /* Force setting for VDEVS */
1190
1191         if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1192                 printk(KERN_ERR "dev[%p]: Unable to change SE Device"
1193                         " max_sectors while dev_export_obj: %d count exists\n",
1194                         dev, atomic_read(&dev->dev_export_obj.obj_access_count));
1195                 return -1;
1196         }
1197         if (!(max_sectors)) {
1198                 printk(KERN_ERR "dev[%p]: Illegal ZERO value for"
1199                         " max_sectors\n", dev);
1200                 return -1;
1201         }
1202         if (max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
1203                 printk(KERN_ERR "dev[%p]: Passed max_sectors: %u less than"
1204                         " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, max_sectors,
1205                                 DA_STATUS_MAX_SECTORS_MIN);
1206                 return -1;
1207         }
1208         if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1209                 if (max_sectors > DEV_ATTRIB(dev)->hw_max_sectors) {
1210                         printk(KERN_ERR "dev[%p]: Passed max_sectors: %u"
1211                                 " greater than TCM/SE_Device max_sectors:"
1212                                 " %u\n", dev, max_sectors,
1213                                 DEV_ATTRIB(dev)->hw_max_sectors);
1214                          return -1;
1215                 }
1216         } else {
1217                 if (!(force) && (max_sectors >
1218                                  DEV_ATTRIB(dev)->hw_max_sectors)) {
1219                         printk(KERN_ERR "dev[%p]: Passed max_sectors: %u"
1220                                 " greater than TCM/SE_Device max_sectors"
1221                                 ": %u, use force=1 to override.\n", dev,
1222                                 max_sectors, DEV_ATTRIB(dev)->hw_max_sectors);
1223                         return -1;
1224                 }
1225                 if (max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
1226                         printk(KERN_ERR "dev[%p]: Passed max_sectors: %u"
1227                                 " greater than DA_STATUS_MAX_SECTORS_MAX:"
1228                                 " %u\n", dev, max_sectors,
1229                                 DA_STATUS_MAX_SECTORS_MAX);
1230                         return -1;
1231                 }
1232         }
1233
1234         DEV_ATTRIB(dev)->max_sectors = max_sectors;
1235         printk("dev[%p]: SE Device max_sectors changed to %u\n",
1236                         dev, max_sectors);
1237         return 0;
1238 }
1239
1240 int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1241 {
1242         if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1243                 printk(KERN_ERR "dev[%p]: Unable to change SE Device"
1244                         " optimal_sectors while dev_export_obj: %d count exists\n",
1245                         dev, atomic_read(&dev->dev_export_obj.obj_access_count));
1246                 return -EINVAL;
1247         }
1248         if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1249                 printk(KERN_ERR "dev[%p]: Passed optimal_sectors cannot be"
1250                                 " changed for TCM/pSCSI\n", dev);
1251                 return -EINVAL;
1252         }
1253         if (optimal_sectors > DEV_ATTRIB(dev)->max_sectors) {
1254                 printk(KERN_ERR "dev[%p]: Passed optimal_sectors %u cannot be"
1255                         " greater than max_sectors: %u\n", dev,
1256                         optimal_sectors, DEV_ATTRIB(dev)->max_sectors);
1257                 return -EINVAL;
1258         }
1259
1260         DEV_ATTRIB(dev)->optimal_sectors = optimal_sectors;
1261         printk(KERN_INFO "dev[%p]: SE Device optimal_sectors changed to %u\n",
1262                         dev, optimal_sectors);
1263         return 0;
1264 }
1265
1266 int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1267 {
1268         if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
1269                 printk(KERN_ERR "dev[%p]: Unable to change SE Device block_size"
1270                         " while dev_export_obj: %d count exists\n", dev,
1271                         atomic_read(&dev->dev_export_obj.obj_access_count));
1272                 return -1;
1273         }
1274
1275         if ((block_size != 512) &&
1276             (block_size != 1024) &&
1277             (block_size != 2048) &&
1278             (block_size != 4096)) {
1279                 printk(KERN_ERR "dev[%p]: Illegal value for block_device: %u"
1280                         " for SE device, must be 512, 1024, 2048 or 4096\n",
1281                         dev, block_size);
1282                 return -1;
1283         }
1284
1285         if (TRANSPORT(dev)->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1286                 printk(KERN_ERR "dev[%p]: Not allowed to change block_size for"
1287                         " Physical Device, use for Linux/SCSI to change"
1288                         " block_size for underlying hardware\n", dev);
1289                 return -1;
1290         }
1291
1292         DEV_ATTRIB(dev)->block_size = block_size;
1293         printk(KERN_INFO "dev[%p]: SE Device block_size changed to %u\n",
1294                         dev, block_size);
1295         return 0;
1296 }
1297
1298 struct se_lun *core_dev_add_lun(
1299         struct se_portal_group *tpg,
1300         struct se_hba *hba,
1301         struct se_device *dev,
1302         u32 lun)
1303 {
1304         struct se_lun *lun_p;
1305         u32 lun_access = 0;
1306
1307         if (atomic_read(&dev->dev_access_obj.obj_access_count) != 0) {
1308                 printk(KERN_ERR "Unable to export struct se_device while dev_access_obj: %d\n",
1309                         atomic_read(&dev->dev_access_obj.obj_access_count));
1310                 return NULL;
1311         }
1312
1313         lun_p = core_tpg_pre_addlun(tpg, lun);
1314         if ((IS_ERR(lun_p)) || !(lun_p))
1315                 return NULL;
1316
1317         if (dev->dev_flags & DF_READ_ONLY)
1318                 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1319         else
1320                 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
1321
1322         if (core_tpg_post_addlun(tpg, lun_p, lun_access, dev) < 0)
1323                 return NULL;
1324
1325         printk(KERN_INFO "%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
1326                 " CORE HBA: %u\n", TPG_TFO(tpg)->get_fabric_name(),
1327                 TPG_TFO(tpg)->tpg_get_tag(tpg), lun_p->unpacked_lun,
1328                 TPG_TFO(tpg)->get_fabric_name(), hba->hba_id);
1329         /*
1330          * Update LUN maps for dynamically added initiators when
1331          * generate_node_acl is enabled.
1332          */
1333         if (TPG_TFO(tpg)->tpg_check_demo_mode(tpg)) {
1334                 struct se_node_acl *acl;
1335                 spin_lock_bh(&tpg->acl_node_lock);
1336                 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
1337                         if (acl->dynamic_node_acl) {
1338                                 spin_unlock_bh(&tpg->acl_node_lock);
1339                                 core_tpg_add_node_to_devs(acl, tpg);
1340                                 spin_lock_bh(&tpg->acl_node_lock);
1341                         }
1342                 }
1343                 spin_unlock_bh(&tpg->acl_node_lock);
1344         }
1345
1346         return lun_p;
1347 }
1348
1349 /*      core_dev_del_lun():
1350  *
1351  *
1352  */
1353 int core_dev_del_lun(
1354         struct se_portal_group *tpg,
1355         u32 unpacked_lun)
1356 {
1357         struct se_lun *lun;
1358         int ret = 0;
1359
1360         lun = core_tpg_pre_dellun(tpg, unpacked_lun, &ret);
1361         if (!(lun))
1362                 return ret;
1363
1364         core_tpg_post_dellun(tpg, lun);
1365
1366         printk(KERN_INFO "%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
1367                 " device object\n", TPG_TFO(tpg)->get_fabric_name(),
1368                 TPG_TFO(tpg)->tpg_get_tag(tpg), unpacked_lun,
1369                 TPG_TFO(tpg)->get_fabric_name());
1370
1371         return 0;
1372 }
1373
1374 struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
1375 {
1376         struct se_lun *lun;
1377
1378         spin_lock(&tpg->tpg_lun_lock);
1379         if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
1380                 printk(KERN_ERR "%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
1381                         "_PER_TPG-1: %u for Target Portal Group: %hu\n",
1382                         TPG_TFO(tpg)->get_fabric_name(), unpacked_lun,
1383                         TRANSPORT_MAX_LUNS_PER_TPG-1,
1384                         TPG_TFO(tpg)->tpg_get_tag(tpg));
1385                 spin_unlock(&tpg->tpg_lun_lock);
1386                 return NULL;
1387         }
1388         lun = &tpg->tpg_lun_list[unpacked_lun];
1389
1390         if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
1391                 printk(KERN_ERR "%s Logical Unit Number: %u is not free on"
1392                         " Target Portal Group: %hu, ignoring request.\n",
1393                         TPG_TFO(tpg)->get_fabric_name(), unpacked_lun,
1394                         TPG_TFO(tpg)->tpg_get_tag(tpg));
1395                 spin_unlock(&tpg->tpg_lun_lock);
1396                 return NULL;
1397         }
1398         spin_unlock(&tpg->tpg_lun_lock);
1399
1400         return lun;
1401 }
1402
1403 /*      core_dev_get_lun():
1404  *
1405  *
1406  */
1407 static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
1408 {
1409         struct se_lun *lun;
1410
1411         spin_lock(&tpg->tpg_lun_lock);
1412         if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
1413                 printk(KERN_ERR "%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
1414                         "_TPG-1: %u for Target Portal Group: %hu\n",
1415                         TPG_TFO(tpg)->get_fabric_name(), unpacked_lun,
1416                         TRANSPORT_MAX_LUNS_PER_TPG-1,
1417                         TPG_TFO(tpg)->tpg_get_tag(tpg));
1418                 spin_unlock(&tpg->tpg_lun_lock);
1419                 return NULL;
1420         }
1421         lun = &tpg->tpg_lun_list[unpacked_lun];
1422
1423         if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
1424                 printk(KERN_ERR "%s Logical Unit Number: %u is not active on"
1425                         " Target Portal Group: %hu, ignoring request.\n",
1426                         TPG_TFO(tpg)->get_fabric_name(), unpacked_lun,
1427                         TPG_TFO(tpg)->tpg_get_tag(tpg));
1428                 spin_unlock(&tpg->tpg_lun_lock);
1429                 return NULL;
1430         }
1431         spin_unlock(&tpg->tpg_lun_lock);
1432
1433         return lun;
1434 }
1435
1436 struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1437         struct se_portal_group *tpg,
1438         u32 mapped_lun,
1439         char *initiatorname,
1440         int *ret)
1441 {
1442         struct se_lun_acl *lacl;
1443         struct se_node_acl *nacl;
1444
1445         if (strlen(initiatorname) >= TRANSPORT_IQN_LEN) {
1446                 printk(KERN_ERR "%s InitiatorName exceeds maximum size.\n",
1447                         TPG_TFO(tpg)->get_fabric_name());
1448                 *ret = -EOVERFLOW;
1449                 return NULL;
1450         }
1451         nacl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
1452         if (!(nacl)) {
1453                 *ret = -EINVAL;
1454                 return NULL;
1455         }
1456         lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
1457         if (!(lacl)) {
1458                 printk(KERN_ERR "Unable to allocate memory for struct se_lun_acl.\n");
1459                 *ret = -ENOMEM;
1460                 return NULL;
1461         }
1462
1463         INIT_LIST_HEAD(&lacl->lacl_list);
1464         lacl->mapped_lun = mapped_lun;
1465         lacl->se_lun_nacl = nacl;
1466         snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
1467
1468         return lacl;
1469 }
1470
1471 int core_dev_add_initiator_node_lun_acl(
1472         struct se_portal_group *tpg,
1473         struct se_lun_acl *lacl,
1474         u32 unpacked_lun,
1475         u32 lun_access)
1476 {
1477         struct se_lun *lun;
1478         struct se_node_acl *nacl;
1479
1480         lun = core_dev_get_lun(tpg, unpacked_lun);
1481         if (!(lun)) {
1482                 printk(KERN_ERR "%s Logical Unit Number: %u is not active on"
1483                         " Target Portal Group: %hu, ignoring request.\n",
1484                         TPG_TFO(tpg)->get_fabric_name(), unpacked_lun,
1485                         TPG_TFO(tpg)->tpg_get_tag(tpg));
1486                 return -EINVAL;
1487         }
1488
1489         nacl = lacl->se_lun_nacl;
1490         if (!(nacl))
1491                 return -EINVAL;
1492
1493         if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
1494             (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
1495                 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1496
1497         lacl->se_lun = lun;
1498
1499         if (core_update_device_list_for_node(lun, lacl, lacl->mapped_lun,
1500                         lun_access, nacl, tpg, 1) < 0)
1501                 return -EINVAL;
1502
1503         spin_lock(&lun->lun_acl_lock);
1504         list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1505         atomic_inc(&lun->lun_acl_count);
1506         smp_mb__after_atomic_inc();
1507         spin_unlock(&lun->lun_acl_lock);
1508
1509         printk(KERN_INFO "%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
1510                 " InitiatorNode: %s\n", TPG_TFO(tpg)->get_fabric_name(),
1511                 TPG_TFO(tpg)->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
1512                 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
1513                 lacl->initiatorname);
1514         /*
1515          * Check to see if there are any existing persistent reservation APTPL
1516          * pre-registrations that need to be enabled for this LUN ACL..
1517          */
1518         core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
1519         return 0;
1520 }
1521
1522 /*      core_dev_del_initiator_node_lun_acl():
1523  *
1524  *
1525  */
1526 int core_dev_del_initiator_node_lun_acl(
1527         struct se_portal_group *tpg,
1528         struct se_lun *lun,
1529         struct se_lun_acl *lacl)
1530 {
1531         struct se_node_acl *nacl;
1532
1533         nacl = lacl->se_lun_nacl;
1534         if (!(nacl))
1535                 return -EINVAL;
1536
1537         spin_lock(&lun->lun_acl_lock);
1538         list_del(&lacl->lacl_list);
1539         atomic_dec(&lun->lun_acl_count);
1540         smp_mb__after_atomic_dec();
1541         spin_unlock(&lun->lun_acl_lock);
1542
1543         core_update_device_list_for_node(lun, NULL, lacl->mapped_lun,
1544                 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
1545
1546         lacl->se_lun = NULL;
1547
1548         printk(KERN_INFO "%s_TPG[%hu]_LUN[%u] - Removed ACL for"
1549                 " InitiatorNode: %s Mapped LUN: %u\n",
1550                 TPG_TFO(tpg)->get_fabric_name(),
1551                 TPG_TFO(tpg)->tpg_get_tag(tpg), lun->unpacked_lun,
1552                 lacl->initiatorname, lacl->mapped_lun);
1553
1554         return 0;
1555 }
1556
1557 void core_dev_free_initiator_node_lun_acl(
1558         struct se_portal_group *tpg,
1559         struct se_lun_acl *lacl)
1560 {
1561         printk("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
1562                 " Mapped LUN: %u\n", TPG_TFO(tpg)->get_fabric_name(),
1563                 TPG_TFO(tpg)->tpg_get_tag(tpg),
1564                 TPG_TFO(tpg)->get_fabric_name(),
1565                 lacl->initiatorname, lacl->mapped_lun);
1566
1567         kfree(lacl);
1568 }
1569
1570 int core_dev_setup_virtual_lun0(void)
1571 {
1572         struct se_hba *hba;
1573         struct se_device *dev;
1574         struct se_subsystem_dev *se_dev = NULL;
1575         struct se_subsystem_api *t;
1576         char buf[16];
1577         int ret;
1578
1579         hba = core_alloc_hba("rd_dr", 0, HBA_FLAGS_INTERNAL_USE);
1580         if (IS_ERR(hba))
1581                 return PTR_ERR(hba);
1582
1583         se_global->g_lun0_hba = hba;
1584         t = hba->transport;
1585
1586         se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
1587         if (!(se_dev)) {
1588                 printk(KERN_ERR "Unable to allocate memory for"
1589                                 " struct se_subsystem_dev\n");
1590                 ret = -ENOMEM;
1591                 goto out;
1592         }
1593         INIT_LIST_HEAD(&se_dev->g_se_dev_list);
1594         INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
1595         spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
1596         INIT_LIST_HEAD(&se_dev->t10_reservation.registration_list);
1597         INIT_LIST_HEAD(&se_dev->t10_reservation.aptpl_reg_list);
1598         spin_lock_init(&se_dev->t10_reservation.registration_lock);
1599         spin_lock_init(&se_dev->t10_reservation.aptpl_reg_lock);
1600         INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
1601         spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
1602         spin_lock_init(&se_dev->se_dev_lock);
1603         se_dev->t10_reservation.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
1604         se_dev->t10_wwn.t10_sub_dev = se_dev;
1605         se_dev->t10_alua.t10_sub_dev = se_dev;
1606         se_dev->se_dev_attrib.da_sub_dev = se_dev;
1607         se_dev->se_dev_hba = hba;
1608
1609         se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, "virt_lun0");
1610         if (!(se_dev->se_dev_su_ptr)) {
1611                 printk(KERN_ERR "Unable to locate subsystem dependent pointer"
1612                         " from allocate_virtdevice()\n");
1613                 ret = -ENOMEM;
1614                 goto out;
1615         }
1616         se_global->g_lun0_su_dev = se_dev;
1617
1618         memset(buf, 0, 16);
1619         sprintf(buf, "rd_pages=8");
1620         t->set_configfs_dev_params(hba, se_dev, buf, sizeof(buf));
1621
1622         dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
1623         if (!(dev) || IS_ERR(dev)) {
1624                 ret = -ENOMEM;
1625                 goto out;
1626         }
1627         se_dev->se_dev_ptr = dev;
1628         se_global->g_lun0_dev = dev;
1629
1630         return 0;
1631 out:
1632         se_global->g_lun0_su_dev = NULL;
1633         kfree(se_dev);
1634         if (se_global->g_lun0_hba) {
1635                 core_delete_hba(se_global->g_lun0_hba);
1636                 se_global->g_lun0_hba = NULL;
1637         }
1638         return ret;
1639 }
1640
1641
1642 void core_dev_release_virtual_lun0(void)
1643 {
1644         struct se_hba *hba = se_global->g_lun0_hba;
1645         struct se_subsystem_dev *su_dev = se_global->g_lun0_su_dev;
1646
1647         if (!(hba))
1648                 return;
1649
1650         if (se_global->g_lun0_dev)
1651                 se_free_virtual_device(se_global->g_lun0_dev, hba);
1652
1653         kfree(su_dev);
1654         core_delete_hba(hba);
1655 }