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