mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / target / target_core_device.c
blob4711e437479eb7279bd6e3ff1a84008bf84a8303
1 /*******************************************************************************
2 * Filename: target_core_device.c (based on iscsi_target_device.c)
4 * This file contains the TCM Virtual Device and Disk Transport
5 * agnostic related functions.
7 * (c) Copyright 2003-2013 Datera, Inc.
9 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
25 ******************************************************************************/
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>
41 #include <target/target_core_base.h>
42 #include <target/target_core_backend.h>
43 #include <target/target_core_fabric.h>
45 #include "target_core_internal.h"
46 #include "target_core_alua.h"
47 #include "target_core_pr.h"
48 #include "target_core_ua.h"
50 DEFINE_MUTEX(g_device_mutex);
51 LIST_HEAD(g_device_list);
53 static struct se_hba *lun0_hba;
54 /* not static, needed by tpg.c */
55 struct se_device *g_lun0_dev;
57 sense_reason_t
58 transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
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;
65 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
66 return TCM_NON_EXISTENT_LUN;
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;
73 deve->total_cmds++;
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;
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;
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;
96 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
98 if (!se_lun) {
100 * Use the se_portal_group->tpg_virt_lun0 to allow for
101 * REPORT_LUNS, et al to be returned when no active
102 * MappedLUN=0 exists for this Initiator Port.
104 if (unpacked_lun != 0) {
105 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
106 " Access for 0x%08x\n",
107 se_cmd->se_tfo->get_fabric_name(),
108 unpacked_lun);
109 return TCM_NON_EXISTENT_LUN;
112 * Force WRITE PROTECT for virtual LUN 0
114 if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
115 (se_cmd->data_direction != DMA_NONE))
116 return TCM_WRITE_PROTECTED;
118 se_lun = &se_sess->se_tpg->tpg_virt_lun0;
119 se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
120 se_cmd->orig_fe_lun = 0;
121 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
124 /* Directly associate cmd with se_dev */
125 se_cmd->se_dev = se_lun->lun_se_dev;
127 /* TODO: get rid of this and use atomics for stats */
128 dev = se_lun->lun_se_dev;
129 spin_lock_irqsave(&dev->stats_lock, flags);
130 dev->num_cmds++;
131 if (se_cmd->data_direction == DMA_TO_DEVICE)
132 dev->write_bytes += se_cmd->data_length;
133 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
134 dev->read_bytes += se_cmd->data_length;
135 spin_unlock_irqrestore(&dev->stats_lock, flags);
137 spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
138 list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
139 spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
141 return 0;
143 EXPORT_SYMBOL(transport_lookup_cmd_lun);
145 int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
147 struct se_dev_entry *deve;
148 struct se_lun *se_lun = NULL;
149 struct se_session *se_sess = se_cmd->se_sess;
150 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
151 unsigned long flags;
153 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
154 return -ENODEV;
156 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
157 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
158 deve = se_cmd->se_deve;
160 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
161 se_tmr->tmr_lun = deve->se_lun;
162 se_cmd->se_lun = deve->se_lun;
163 se_lun = deve->se_lun;
164 se_cmd->pr_res_key = deve->pr_res_key;
165 se_cmd->orig_fe_lun = unpacked_lun;
167 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
169 if (!se_lun) {
170 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
171 " Access for 0x%08x\n",
172 se_cmd->se_tfo->get_fabric_name(),
173 unpacked_lun);
174 return -ENODEV;
177 /* Directly associate cmd with se_dev */
178 se_cmd->se_dev = se_lun->lun_se_dev;
179 se_tmr->tmr_dev = se_lun->lun_se_dev;
181 spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
182 list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
183 spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
185 return 0;
187 EXPORT_SYMBOL(transport_lookup_tmr_lun);
190 * This function is called from core_scsi3_emulate_pro_register_and_move()
191 * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
192 * when a matching rtpi is found.
194 struct se_dev_entry *core_get_se_deve_from_rtpi(
195 struct se_node_acl *nacl,
196 u16 rtpi)
198 struct se_dev_entry *deve;
199 struct se_lun *lun;
200 struct se_port *port;
201 struct se_portal_group *tpg = nacl->se_tpg;
202 u32 i;
204 spin_lock_irq(&nacl->device_list_lock);
205 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
206 deve = nacl->device_list[i];
208 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
209 continue;
211 lun = deve->se_lun;
212 if (!lun) {
213 pr_err("%s device entries device pointer is"
214 " NULL, but Initiator has access.\n",
215 tpg->se_tpg_tfo->get_fabric_name());
216 continue;
218 port = lun->lun_sep;
219 if (!port) {
220 pr_err("%s device entries device pointer is"
221 " NULL, but Initiator has access.\n",
222 tpg->se_tpg_tfo->get_fabric_name());
223 continue;
225 if (port->sep_rtpi != rtpi)
226 continue;
228 atomic_inc(&deve->pr_ref_count);
229 smp_mb__after_atomic_inc();
230 spin_unlock_irq(&nacl->device_list_lock);
232 return deve;
234 spin_unlock_irq(&nacl->device_list_lock);
236 return NULL;
239 int core_free_device_list_for_node(
240 struct se_node_acl *nacl,
241 struct se_portal_group *tpg)
243 struct se_dev_entry *deve;
244 struct se_lun *lun;
245 u32 i;
247 if (!nacl->device_list)
248 return 0;
250 spin_lock_irq(&nacl->device_list_lock);
251 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
252 deve = nacl->device_list[i];
254 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
255 continue;
257 if (!deve->se_lun) {
258 pr_err("%s device entries device pointer is"
259 " NULL, but Initiator has access.\n",
260 tpg->se_tpg_tfo->get_fabric_name());
261 continue;
263 lun = deve->se_lun;
265 spin_unlock_irq(&nacl->device_list_lock);
266 core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
267 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
268 spin_lock_irq(&nacl->device_list_lock);
270 spin_unlock_irq(&nacl->device_list_lock);
272 array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
273 nacl->device_list = NULL;
275 return 0;
278 void core_update_device_list_access(
279 u32 mapped_lun,
280 u32 lun_access,
281 struct se_node_acl *nacl)
283 struct se_dev_entry *deve;
285 spin_lock_irq(&nacl->device_list_lock);
286 deve = nacl->device_list[mapped_lun];
287 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
288 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
289 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
290 } else {
291 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
292 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
294 spin_unlock_irq(&nacl->device_list_lock);
297 /* core_enable_device_list_for_node():
301 int core_enable_device_list_for_node(
302 struct se_lun *lun,
303 struct se_lun_acl *lun_acl,
304 u32 mapped_lun,
305 u32 lun_access,
306 struct se_node_acl *nacl,
307 struct se_portal_group *tpg)
309 struct se_port *port = lun->lun_sep;
310 struct se_dev_entry *deve;
312 spin_lock_irq(&nacl->device_list_lock);
314 deve = nacl->device_list[mapped_lun];
317 * Check if the call is handling demo mode -> explict LUN ACL
318 * transition. This transition must be for the same struct se_lun
319 * + mapped_lun that was setup in demo mode..
321 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
322 if (deve->se_lun_acl != NULL) {
323 pr_err("struct se_dev_entry->se_lun_acl"
324 " already set for demo mode -> explict"
325 " LUN ACL transition\n");
326 spin_unlock_irq(&nacl->device_list_lock);
327 return -EINVAL;
329 if (deve->se_lun != lun) {
330 pr_err("struct se_dev_entry->se_lun does"
331 " match passed struct se_lun for demo mode"
332 " -> explict LUN ACL transition\n");
333 spin_unlock_irq(&nacl->device_list_lock);
334 return -EINVAL;
336 deve->se_lun_acl = lun_acl;
338 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
339 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
340 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
341 } else {
342 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
343 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
346 spin_unlock_irq(&nacl->device_list_lock);
347 return 0;
350 deve->se_lun = lun;
351 deve->se_lun_acl = lun_acl;
352 deve->mapped_lun = mapped_lun;
353 deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
355 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
356 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
357 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
358 } else {
359 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
360 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
363 deve->creation_time = get_jiffies_64();
364 deve->attach_count++;
365 spin_unlock_irq(&nacl->device_list_lock);
367 spin_lock_bh(&port->sep_alua_lock);
368 list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
369 spin_unlock_bh(&port->sep_alua_lock);
371 return 0;
374 /* core_disable_device_list_for_node():
378 int core_disable_device_list_for_node(
379 struct se_lun *lun,
380 struct se_lun_acl *lun_acl,
381 u32 mapped_lun,
382 u32 lun_access,
383 struct se_node_acl *nacl,
384 struct se_portal_group *tpg)
386 struct se_port *port = lun->lun_sep;
387 struct se_dev_entry *deve = nacl->device_list[mapped_lun];
390 * If the MappedLUN entry is being disabled, the entry in
391 * port->sep_alua_list must be removed now before clearing the
392 * struct se_dev_entry pointers below as logic in
393 * core_alua_do_transition_tg_pt() depends on these being present.
395 * deve->se_lun_acl will be NULL for demo-mode created LUNs
396 * that have not been explicitly converted to MappedLUNs ->
397 * struct se_lun_acl, but we remove deve->alua_port_list from
398 * port->sep_alua_list. This also means that active UAs and
399 * NodeACL context specific PR metadata for demo-mode
400 * MappedLUN *deve will be released below..
402 spin_lock_bh(&port->sep_alua_lock);
403 list_del(&deve->alua_port_list);
404 spin_unlock_bh(&port->sep_alua_lock);
406 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
407 * PR operation to complete.
409 while (atomic_read(&deve->pr_ref_count) != 0)
410 cpu_relax();
412 spin_lock_irq(&nacl->device_list_lock);
414 * Disable struct se_dev_entry LUN ACL mapping
416 core_scsi3_ua_release_all(deve);
417 deve->se_lun = NULL;
418 deve->se_lun_acl = NULL;
419 deve->lun_flags = 0;
420 deve->creation_time = 0;
421 deve->attach_count--;
422 spin_unlock_irq(&nacl->device_list_lock);
424 core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
425 return 0;
428 /* core_clear_lun_from_tpg():
432 void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
434 struct se_node_acl *nacl;
435 struct se_dev_entry *deve;
436 u32 i;
438 spin_lock_irq(&tpg->acl_node_lock);
439 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
440 spin_unlock_irq(&tpg->acl_node_lock);
442 spin_lock_irq(&nacl->device_list_lock);
443 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
444 deve = nacl->device_list[i];
445 if (lun != deve->se_lun)
446 continue;
447 spin_unlock_irq(&nacl->device_list_lock);
449 core_disable_device_list_for_node(lun, NULL,
450 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
451 nacl, tpg);
453 spin_lock_irq(&nacl->device_list_lock);
455 spin_unlock_irq(&nacl->device_list_lock);
457 spin_lock_irq(&tpg->acl_node_lock);
459 spin_unlock_irq(&tpg->acl_node_lock);
462 static struct se_port *core_alloc_port(struct se_device *dev)
464 struct se_port *port, *port_tmp;
466 port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
467 if (!port) {
468 pr_err("Unable to allocate struct se_port\n");
469 return ERR_PTR(-ENOMEM);
471 INIT_LIST_HEAD(&port->sep_alua_list);
472 INIT_LIST_HEAD(&port->sep_list);
473 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
474 spin_lock_init(&port->sep_alua_lock);
475 mutex_init(&port->sep_tg_pt_md_mutex);
477 spin_lock(&dev->se_port_lock);
478 if (dev->dev_port_count == 0x0000ffff) {
479 pr_warn("Reached dev->dev_port_count =="
480 " 0x0000ffff\n");
481 spin_unlock(&dev->se_port_lock);
482 return ERR_PTR(-ENOSPC);
484 again:
486 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
487 * Here is the table from spc4r17 section 7.7.3.8.
489 * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
491 * Code Description
492 * 0h Reserved
493 * 1h Relative port 1, historically known as port A
494 * 2h Relative port 2, historically known as port B
495 * 3h to FFFFh Relative port 3 through 65 535
497 port->sep_rtpi = dev->dev_rpti_counter++;
498 if (!port->sep_rtpi)
499 goto again;
501 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
503 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
504 * for 16-bit wrap..
506 if (port->sep_rtpi == port_tmp->sep_rtpi)
507 goto again;
509 spin_unlock(&dev->se_port_lock);
511 return port;
514 static void core_export_port(
515 struct se_device *dev,
516 struct se_portal_group *tpg,
517 struct se_port *port,
518 struct se_lun *lun)
520 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
522 spin_lock(&dev->se_port_lock);
523 spin_lock(&lun->lun_sep_lock);
524 port->sep_tpg = tpg;
525 port->sep_lun = lun;
526 lun->lun_sep = port;
527 spin_unlock(&lun->lun_sep_lock);
529 list_add_tail(&port->sep_list, &dev->dev_sep_list);
530 spin_unlock(&dev->se_port_lock);
532 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
533 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
534 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
535 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
536 pr_err("Unable to allocate t10_alua_tg_pt"
537 "_gp_member_t\n");
538 return;
540 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
541 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
542 dev->t10_alua.default_tg_pt_gp);
543 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
544 pr_debug("%s/%s: Adding to default ALUA Target Port"
545 " Group: alua/default_tg_pt_gp\n",
546 dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
549 dev->dev_port_count++;
550 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
554 * Called with struct se_device->se_port_lock spinlock held.
556 static void core_release_port(struct se_device *dev, struct se_port *port)
557 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
560 * Wait for any port reference for PR ALL_TG_PT=1 operation
561 * to complete in __core_scsi3_alloc_registration()
563 spin_unlock(&dev->se_port_lock);
564 if (atomic_read(&port->sep_tg_pt_ref_cnt))
565 cpu_relax();
566 spin_lock(&dev->se_port_lock);
568 core_alua_free_tg_pt_gp_mem(port);
570 list_del(&port->sep_list);
571 dev->dev_port_count--;
572 kfree(port);
575 int core_dev_export(
576 struct se_device *dev,
577 struct se_portal_group *tpg,
578 struct se_lun *lun)
580 struct se_hba *hba = dev->se_hba;
581 struct se_port *port;
583 port = core_alloc_port(dev);
584 if (IS_ERR(port))
585 return PTR_ERR(port);
587 lun->lun_se_dev = dev;
589 spin_lock(&hba->device_lock);
590 dev->export_count++;
591 spin_unlock(&hba->device_lock);
593 core_export_port(dev, tpg, port, lun);
594 return 0;
597 void core_dev_unexport(
598 struct se_device *dev,
599 struct se_portal_group *tpg,
600 struct se_lun *lun)
602 struct se_hba *hba = dev->se_hba;
603 struct se_port *port = lun->lun_sep;
605 spin_lock(&lun->lun_sep_lock);
606 if (lun->lun_se_dev == NULL) {
607 spin_unlock(&lun->lun_sep_lock);
608 return;
610 spin_unlock(&lun->lun_sep_lock);
612 spin_lock(&dev->se_port_lock);
613 core_release_port(dev, port);
614 spin_unlock(&dev->se_port_lock);
616 spin_lock(&hba->device_lock);
617 dev->export_count--;
618 spin_unlock(&hba->device_lock);
620 lun->lun_sep = NULL;
621 lun->lun_se_dev = NULL;
624 static void se_release_vpd_for_dev(struct se_device *dev)
626 struct t10_vpd *vpd, *vpd_tmp;
628 spin_lock(&dev->t10_wwn.t10_vpd_lock);
629 list_for_each_entry_safe(vpd, vpd_tmp,
630 &dev->t10_wwn.t10_vpd_list, vpd_list) {
631 list_del(&vpd->vpd_list);
632 kfree(vpd);
634 spin_unlock(&dev->t10_wwn.t10_vpd_lock);
637 static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
639 u32 aligned_max_sectors;
640 u32 alignment;
642 * Limit max_sectors to a PAGE_SIZE aligned value for modern
643 * transport_allocate_data_tasks() operation.
645 alignment = max(1ul, PAGE_SIZE / block_size);
646 aligned_max_sectors = rounddown(max_sectors, alignment);
648 if (max_sectors != aligned_max_sectors)
649 pr_info("Rounding down aligned max_sectors from %u to %u\n",
650 max_sectors, aligned_max_sectors);
652 return aligned_max_sectors;
655 int se_dev_set_max_unmap_lba_count(
656 struct se_device *dev,
657 u32 max_unmap_lba_count)
659 dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
660 pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
661 dev, dev->dev_attrib.max_unmap_lba_count);
662 return 0;
665 int se_dev_set_max_unmap_block_desc_count(
666 struct se_device *dev,
667 u32 max_unmap_block_desc_count)
669 dev->dev_attrib.max_unmap_block_desc_count =
670 max_unmap_block_desc_count;
671 pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
672 dev, dev->dev_attrib.max_unmap_block_desc_count);
673 return 0;
676 int se_dev_set_unmap_granularity(
677 struct se_device *dev,
678 u32 unmap_granularity)
680 dev->dev_attrib.unmap_granularity = unmap_granularity;
681 pr_debug("dev[%p]: Set unmap_granularity: %u\n",
682 dev, dev->dev_attrib.unmap_granularity);
683 return 0;
686 int se_dev_set_unmap_granularity_alignment(
687 struct se_device *dev,
688 u32 unmap_granularity_alignment)
690 dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
691 pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
692 dev, dev->dev_attrib.unmap_granularity_alignment);
693 return 0;
696 int se_dev_set_max_write_same_len(
697 struct se_device *dev,
698 u32 max_write_same_len)
700 dev->dev_attrib.max_write_same_len = max_write_same_len;
701 pr_debug("dev[%p]: Set max_write_same_len: %u\n",
702 dev, dev->dev_attrib.max_write_same_len);
703 return 0;
706 static void dev_set_t10_wwn_model_alias(struct se_device *dev)
708 const char *configname;
710 configname = config_item_name(&dev->dev_group.cg_item);
711 if (strlen(configname) >= 16) {
712 pr_warn("dev[%p]: Backstore name '%s' is too long for "
713 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
714 configname);
716 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
719 int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
721 if (dev->export_count) {
722 pr_err("dev[%p]: Unable to change model alias"
723 " while export_count is %d\n",
724 dev, dev->export_count);
725 return -EINVAL;
728 if (flag != 0 && flag != 1) {
729 pr_err("Illegal value %d\n", flag);
730 return -EINVAL;
733 if (flag) {
734 dev_set_t10_wwn_model_alias(dev);
735 } else {
736 strncpy(&dev->t10_wwn.model[0],
737 dev->transport->inquiry_prod, 16);
739 dev->dev_attrib.emulate_model_alias = flag;
741 return 0;
744 int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
746 if (flag != 0 && flag != 1) {
747 pr_err("Illegal value %d\n", flag);
748 return -EINVAL;
751 if (flag) {
752 pr_err("dpo_emulated not supported\n");
753 return -EINVAL;
756 return 0;
759 int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
761 if (flag != 0 && flag != 1) {
762 pr_err("Illegal value %d\n", flag);
763 return -EINVAL;
766 if (flag &&
767 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
768 pr_err("emulate_fua_write not supported for pSCSI\n");
769 return -EINVAL;
771 dev->dev_attrib.emulate_fua_write = flag;
772 pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
773 dev, dev->dev_attrib.emulate_fua_write);
774 return 0;
777 int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
779 if (flag != 0 && flag != 1) {
780 pr_err("Illegal value %d\n", flag);
781 return -EINVAL;
784 if (flag) {
785 pr_err("ua read emulated not supported\n");
786 return -EINVAL;
789 return 0;
792 int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
794 if (flag != 0 && flag != 1) {
795 pr_err("Illegal value %d\n", flag);
796 return -EINVAL;
798 if (flag &&
799 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
800 pr_err("emulate_write_cache not supported for pSCSI\n");
801 return -EINVAL;
803 if (flag &&
804 dev->transport->get_write_cache) {
805 pr_err("emulate_write_cache not supported for this device\n");
806 return -EINVAL;
809 dev->dev_attrib.emulate_write_cache = flag;
810 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
811 dev, dev->dev_attrib.emulate_write_cache);
812 return 0;
815 int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
817 if ((flag != 0) && (flag != 1) && (flag != 2)) {
818 pr_err("Illegal value %d\n", flag);
819 return -EINVAL;
822 if (dev->export_count) {
823 pr_err("dev[%p]: Unable to change SE Device"
824 " UA_INTRLCK_CTRL while export_count is %d\n",
825 dev, dev->export_count);
826 return -EINVAL;
828 dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
829 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
830 dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
832 return 0;
835 int se_dev_set_emulate_tas(struct se_device *dev, int flag)
837 if ((flag != 0) && (flag != 1)) {
838 pr_err("Illegal value %d\n", flag);
839 return -EINVAL;
842 if (dev->export_count) {
843 pr_err("dev[%p]: Unable to change SE Device TAS while"
844 " export_count is %d\n",
845 dev, dev->export_count);
846 return -EINVAL;
848 dev->dev_attrib.emulate_tas = flag;
849 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
850 dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
852 return 0;
855 int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
857 if ((flag != 0) && (flag != 1)) {
858 pr_err("Illegal value %d\n", flag);
859 return -EINVAL;
862 * We expect this value to be non-zero when generic Block Layer
863 * Discard supported is detected iblock_create_virtdevice().
865 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
866 pr_err("Generic Block Discard not supported\n");
867 return -ENOSYS;
870 dev->dev_attrib.emulate_tpu = flag;
871 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
872 dev, flag);
873 return 0;
876 int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
878 if ((flag != 0) && (flag != 1)) {
879 pr_err("Illegal value %d\n", flag);
880 return -EINVAL;
883 * We expect this value to be non-zero when generic Block Layer
884 * Discard supported is detected iblock_create_virtdevice().
886 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
887 pr_err("Generic Block Discard not supported\n");
888 return -ENOSYS;
891 dev->dev_attrib.emulate_tpws = flag;
892 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
893 dev, flag);
894 return 0;
897 int se_dev_set_emulate_caw(struct se_device *dev, int flag)
899 if (flag != 0 && flag != 1) {
900 pr_err("Illegal value %d\n", flag);
901 return -EINVAL;
903 dev->dev_attrib.emulate_caw = flag;
904 pr_debug("dev[%p]: SE Device CompareAndWrite (AtomicTestandSet): %d\n",
905 dev, flag);
907 return 0;
910 int se_dev_set_emulate_3pc(struct se_device *dev, int flag)
912 if (flag != 0 && flag != 1) {
913 pr_err("Illegal value %d\n", flag);
914 return -EINVAL;
916 dev->dev_attrib.emulate_3pc = flag;
917 pr_debug("dev[%p]: SE Device 3rd Party Copy (EXTENDED_COPY): %d\n",
918 dev, flag);
920 return 0;
923 int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
925 if ((flag != 0) && (flag != 1)) {
926 pr_err("Illegal value %d\n", flag);
927 return -EINVAL;
929 dev->dev_attrib.enforce_pr_isids = flag;
930 pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
931 (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
932 return 0;
935 int se_dev_set_is_nonrot(struct se_device *dev, int flag)
937 if ((flag != 0) && (flag != 1)) {
938 printk(KERN_ERR "Illegal value %d\n", flag);
939 return -EINVAL;
941 dev->dev_attrib.is_nonrot = flag;
942 pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
943 dev, flag);
944 return 0;
947 int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
949 if (flag != 0) {
950 printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
951 " reordering not implemented\n", dev);
952 return -ENOSYS;
954 dev->dev_attrib.emulate_rest_reord = flag;
955 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
956 return 0;
960 * Note, this can only be called on unexported SE Device Object.
962 int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
964 if (dev->export_count) {
965 pr_err("dev[%p]: Unable to change SE Device TCQ while"
966 " export_count is %d\n",
967 dev, dev->export_count);
968 return -EINVAL;
970 if (!queue_depth) {
971 pr_err("dev[%p]: Illegal ZERO value for queue"
972 "_depth\n", dev);
973 return -EINVAL;
976 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
977 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
978 pr_err("dev[%p]: Passed queue_depth: %u"
979 " exceeds TCM/SE_Device TCQ: %u\n",
980 dev, queue_depth,
981 dev->dev_attrib.hw_queue_depth);
982 return -EINVAL;
984 } else {
985 if (queue_depth > dev->dev_attrib.queue_depth) {
986 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
987 pr_err("dev[%p]: Passed queue_depth:"
988 " %u exceeds TCM/SE_Device MAX"
989 " TCQ: %u\n", dev, queue_depth,
990 dev->dev_attrib.hw_queue_depth);
991 return -EINVAL;
996 dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
997 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
998 dev, queue_depth);
999 return 0;
1002 int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
1004 int block_size = dev->dev_attrib.block_size;
1006 if (dev->export_count) {
1007 pr_err("dev[%p]: Unable to change SE Device"
1008 " fabric_max_sectors while export_count is %d\n",
1009 dev, dev->export_count);
1010 return -EINVAL;
1012 if (!fabric_max_sectors) {
1013 pr_err("dev[%p]: Illegal ZERO value for"
1014 " fabric_max_sectors\n", dev);
1015 return -EINVAL;
1017 if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
1018 pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
1019 " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
1020 DA_STATUS_MAX_SECTORS_MIN);
1021 return -EINVAL;
1023 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1024 if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
1025 pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1026 " greater than TCM/SE_Device max_sectors:"
1027 " %u\n", dev, fabric_max_sectors,
1028 dev->dev_attrib.hw_max_sectors);
1029 return -EINVAL;
1031 } else {
1032 if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
1033 pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1034 " greater than DA_STATUS_MAX_SECTORS_MAX:"
1035 " %u\n", dev, fabric_max_sectors,
1036 DA_STATUS_MAX_SECTORS_MAX);
1037 return -EINVAL;
1041 * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
1043 if (!block_size) {
1044 block_size = 512;
1045 pr_warn("Defaulting to 512 for zero block_size\n");
1047 fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
1048 block_size);
1050 dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
1051 pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
1052 dev, fabric_max_sectors);
1053 return 0;
1056 int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1058 if (dev->export_count) {
1059 pr_err("dev[%p]: Unable to change SE Device"
1060 " optimal_sectors while export_count is %d\n",
1061 dev, dev->export_count);
1062 return -EINVAL;
1064 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1065 pr_err("dev[%p]: Passed optimal_sectors cannot be"
1066 " changed for TCM/pSCSI\n", dev);
1067 return -EINVAL;
1069 if (optimal_sectors > dev->dev_attrib.hw_max_sectors) {
1070 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1071 " greater than hw_max_sectors: %u\n", dev,
1072 optimal_sectors, dev->dev_attrib.hw_max_sectors);
1073 return -EINVAL;
1076 dev->dev_attrib.optimal_sectors = optimal_sectors;
1077 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1078 dev, optimal_sectors);
1079 return 0;
1082 int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1084 if (dev->export_count) {
1085 pr_err("dev[%p]: Unable to change SE Device block_size"
1086 " while export_count is %d\n",
1087 dev, dev->export_count);
1088 return -EINVAL;
1091 if ((block_size != 512) &&
1092 (block_size != 1024) &&
1093 (block_size != 2048) &&
1094 (block_size != 4096)) {
1095 pr_err("dev[%p]: Illegal value for block_device: %u"
1096 " for SE device, must be 512, 1024, 2048 or 4096\n",
1097 dev, block_size);
1098 return -EINVAL;
1101 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1102 pr_err("dev[%p]: Not allowed to change block_size for"
1103 " Physical Device, use for Linux/SCSI to change"
1104 " block_size for underlying hardware\n", dev);
1105 return -EINVAL;
1108 dev->dev_attrib.block_size = block_size;
1109 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1110 dev, block_size);
1112 if (dev->dev_attrib.max_bytes_per_io)
1113 dev->dev_attrib.hw_max_sectors =
1114 dev->dev_attrib.max_bytes_per_io / block_size;
1116 return 0;
1119 struct se_lun *core_dev_add_lun(
1120 struct se_portal_group *tpg,
1121 struct se_device *dev,
1122 u32 lun)
1124 struct se_lun *lun_p;
1125 int rc;
1127 lun_p = core_tpg_pre_addlun(tpg, lun);
1128 if (IS_ERR(lun_p))
1129 return lun_p;
1131 rc = core_tpg_post_addlun(tpg, lun_p,
1132 TRANSPORT_LUNFLAGS_READ_WRITE, dev);
1133 if (rc < 0)
1134 return ERR_PTR(rc);
1136 pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
1137 " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1138 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
1139 tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
1141 * Update LUN maps for dynamically added initiators when
1142 * generate_node_acl is enabled.
1144 if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
1145 struct se_node_acl *acl;
1146 spin_lock_irq(&tpg->acl_node_lock);
1147 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
1148 if (acl->dynamic_node_acl &&
1149 (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
1150 !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
1151 spin_unlock_irq(&tpg->acl_node_lock);
1152 core_tpg_add_node_to_devs(acl, tpg);
1153 spin_lock_irq(&tpg->acl_node_lock);
1156 spin_unlock_irq(&tpg->acl_node_lock);
1159 return lun_p;
1162 /* core_dev_del_lun():
1166 int core_dev_del_lun(
1167 struct se_portal_group *tpg,
1168 u32 unpacked_lun)
1170 struct se_lun *lun;
1172 lun = core_tpg_pre_dellun(tpg, unpacked_lun);
1173 if (IS_ERR(lun))
1174 return PTR_ERR(lun);
1176 core_tpg_post_dellun(tpg, lun);
1178 pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
1179 " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
1180 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
1181 tpg->se_tpg_tfo->get_fabric_name());
1183 return 0;
1186 struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
1188 struct se_lun *lun;
1190 spin_lock(&tpg->tpg_lun_lock);
1191 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
1192 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
1193 "_PER_TPG-1: %u for Target Portal Group: %hu\n",
1194 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1195 TRANSPORT_MAX_LUNS_PER_TPG-1,
1196 tpg->se_tpg_tfo->tpg_get_tag(tpg));
1197 spin_unlock(&tpg->tpg_lun_lock);
1198 return NULL;
1200 lun = tpg->tpg_lun_list[unpacked_lun];
1202 if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
1203 pr_err("%s Logical Unit Number: %u is not free on"
1204 " Target Portal Group: %hu, ignoring request.\n",
1205 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1206 tpg->se_tpg_tfo->tpg_get_tag(tpg));
1207 spin_unlock(&tpg->tpg_lun_lock);
1208 return NULL;
1210 spin_unlock(&tpg->tpg_lun_lock);
1212 return lun;
1215 /* core_dev_get_lun():
1219 static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
1221 struct se_lun *lun;
1223 spin_lock(&tpg->tpg_lun_lock);
1224 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
1225 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
1226 "_TPG-1: %u for Target Portal Group: %hu\n",
1227 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1228 TRANSPORT_MAX_LUNS_PER_TPG-1,
1229 tpg->se_tpg_tfo->tpg_get_tag(tpg));
1230 spin_unlock(&tpg->tpg_lun_lock);
1231 return NULL;
1233 lun = tpg->tpg_lun_list[unpacked_lun];
1235 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
1236 pr_err("%s Logical Unit Number: %u is not active on"
1237 " Target Portal Group: %hu, ignoring request.\n",
1238 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1239 tpg->se_tpg_tfo->tpg_get_tag(tpg));
1240 spin_unlock(&tpg->tpg_lun_lock);
1241 return NULL;
1243 spin_unlock(&tpg->tpg_lun_lock);
1245 return lun;
1248 struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1249 struct se_portal_group *tpg,
1250 struct se_node_acl *nacl,
1251 u32 mapped_lun,
1252 int *ret)
1254 struct se_lun_acl *lacl;
1256 if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
1257 pr_err("%s InitiatorName exceeds maximum size.\n",
1258 tpg->se_tpg_tfo->get_fabric_name());
1259 *ret = -EOVERFLOW;
1260 return NULL;
1262 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
1263 if (!lacl) {
1264 pr_err("Unable to allocate memory for struct se_lun_acl.\n");
1265 *ret = -ENOMEM;
1266 return NULL;
1269 INIT_LIST_HEAD(&lacl->lacl_list);
1270 lacl->mapped_lun = mapped_lun;
1271 lacl->se_lun_nacl = nacl;
1272 snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
1273 nacl->initiatorname);
1275 return lacl;
1278 int core_dev_add_initiator_node_lun_acl(
1279 struct se_portal_group *tpg,
1280 struct se_lun_acl *lacl,
1281 u32 unpacked_lun,
1282 u32 lun_access)
1284 struct se_lun *lun;
1285 struct se_node_acl *nacl;
1287 lun = core_dev_get_lun(tpg, unpacked_lun);
1288 if (!lun) {
1289 pr_err("%s Logical Unit Number: %u is not active on"
1290 " Target Portal Group: %hu, ignoring request.\n",
1291 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1292 tpg->se_tpg_tfo->tpg_get_tag(tpg));
1293 return -EINVAL;
1296 nacl = lacl->se_lun_nacl;
1297 if (!nacl)
1298 return -EINVAL;
1300 if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
1301 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
1302 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1304 lacl->se_lun = lun;
1306 if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
1307 lun_access, nacl, tpg) < 0)
1308 return -EINVAL;
1310 spin_lock(&lun->lun_acl_lock);
1311 list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1312 atomic_inc(&lun->lun_acl_count);
1313 smp_mb__after_atomic_inc();
1314 spin_unlock(&lun->lun_acl_lock);
1316 pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
1317 " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
1318 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
1319 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
1320 lacl->initiatorname);
1322 * Check to see if there are any existing persistent reservation APTPL
1323 * pre-registrations that need to be enabled for this LUN ACL..
1325 core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, nacl,
1326 lacl->mapped_lun);
1327 return 0;
1330 /* core_dev_del_initiator_node_lun_acl():
1334 int core_dev_del_initiator_node_lun_acl(
1335 struct se_portal_group *tpg,
1336 struct se_lun *lun,
1337 struct se_lun_acl *lacl)
1339 struct se_node_acl *nacl;
1341 nacl = lacl->se_lun_nacl;
1342 if (!nacl)
1343 return -EINVAL;
1345 spin_lock(&lun->lun_acl_lock);
1346 list_del(&lacl->lacl_list);
1347 atomic_dec(&lun->lun_acl_count);
1348 smp_mb__after_atomic_dec();
1349 spin_unlock(&lun->lun_acl_lock);
1351 core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
1352 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
1354 lacl->se_lun = NULL;
1356 pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
1357 " InitiatorNode: %s Mapped LUN: %u\n",
1358 tpg->se_tpg_tfo->get_fabric_name(),
1359 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
1360 lacl->initiatorname, lacl->mapped_lun);
1362 return 0;
1365 void core_dev_free_initiator_node_lun_acl(
1366 struct se_portal_group *tpg,
1367 struct se_lun_acl *lacl)
1369 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
1370 " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1371 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1372 tpg->se_tpg_tfo->get_fabric_name(),
1373 lacl->initiatorname, lacl->mapped_lun);
1375 kfree(lacl);
1378 static void scsi_dump_inquiry(struct se_device *dev)
1380 struct t10_wwn *wwn = &dev->t10_wwn;
1381 char buf[17];
1382 int i, device_type;
1384 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1386 for (i = 0; i < 8; i++)
1387 if (wwn->vendor[i] >= 0x20)
1388 buf[i] = wwn->vendor[i];
1389 else
1390 buf[i] = ' ';
1391 buf[i] = '\0';
1392 pr_debug(" Vendor: %s\n", buf);
1394 for (i = 0; i < 16; i++)
1395 if (wwn->model[i] >= 0x20)
1396 buf[i] = wwn->model[i];
1397 else
1398 buf[i] = ' ';
1399 buf[i] = '\0';
1400 pr_debug(" Model: %s\n", buf);
1402 for (i = 0; i < 4; i++)
1403 if (wwn->revision[i] >= 0x20)
1404 buf[i] = wwn->revision[i];
1405 else
1406 buf[i] = ' ';
1407 buf[i] = '\0';
1408 pr_debug(" Revision: %s\n", buf);
1410 device_type = dev->transport->get_device_type(dev);
1411 pr_debug(" Type: %s ", scsi_device_type(device_type));
1414 struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
1416 struct se_device *dev;
1418 dev = hba->transport->alloc_device(hba, name);
1419 if (!dev)
1420 return NULL;
1422 dev->dev_link_magic = SE_DEV_LINK_MAGIC;
1423 dev->se_hba = hba;
1424 dev->transport = hba->transport;
1426 INIT_LIST_HEAD(&dev->dev_list);
1427 INIT_LIST_HEAD(&dev->dev_sep_list);
1428 INIT_LIST_HEAD(&dev->dev_tmr_list);
1429 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1430 INIT_LIST_HEAD(&dev->state_list);
1431 INIT_LIST_HEAD(&dev->qf_cmd_list);
1432 INIT_LIST_HEAD(&dev->g_dev_node);
1433 spin_lock_init(&dev->stats_lock);
1434 spin_lock_init(&dev->execute_task_lock);
1435 spin_lock_init(&dev->delayed_cmd_lock);
1436 spin_lock_init(&dev->dev_reservation_lock);
1437 spin_lock_init(&dev->se_port_lock);
1438 spin_lock_init(&dev->se_tmr_lock);
1439 spin_lock_init(&dev->qf_cmd_lock);
1440 sema_init(&dev->caw_sem, 1);
1441 atomic_set(&dev->dev_ordered_id, 0);
1442 INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
1443 spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
1444 INIT_LIST_HEAD(&dev->t10_pr.registration_list);
1445 INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
1446 spin_lock_init(&dev->t10_pr.registration_lock);
1447 spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
1448 INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
1449 spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
1451 dev->t10_wwn.t10_dev = dev;
1452 dev->t10_alua.t10_dev = dev;
1454 dev->dev_attrib.da_dev = dev;
1455 dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
1456 dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
1457 dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
1458 dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
1459 dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
1460 dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
1461 dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
1462 dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
1463 dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
1464 dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
1465 dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
1466 dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
1467 dev->dev_attrib.is_nonrot = DA_IS_NONROT;
1468 dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
1469 dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
1470 dev->dev_attrib.max_unmap_block_desc_count =
1471 DA_MAX_UNMAP_BLOCK_DESC_COUNT;
1472 dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
1473 dev->dev_attrib.unmap_granularity_alignment =
1474 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
1475 dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
1476 dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
1478 return dev;
1481 int target_configure_device(struct se_device *dev)
1483 struct se_hba *hba = dev->se_hba;
1484 int ret;
1486 if (dev->dev_flags & DF_CONFIGURED) {
1487 pr_err("se_dev->se_dev_ptr already set for storage"
1488 " object\n");
1489 return -EEXIST;
1492 ret = dev->transport->configure_device(dev);
1493 if (ret)
1494 goto out;
1496 * XXX: there is not much point to have two different values here..
1498 dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
1499 dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
1502 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
1504 dev->dev_attrib.hw_max_sectors =
1505 se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
1506 dev->dev_attrib.hw_block_size);
1507 dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
1509 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1510 dev->creation_time = get_jiffies_64();
1512 ret = core_setup_alua(dev);
1513 if (ret)
1514 goto out;
1517 * Startup the struct se_device processing thread
1519 dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
1520 dev->transport->name);
1521 if (!dev->tmr_wq) {
1522 pr_err("Unable to create tmr workqueue for %s\n",
1523 dev->transport->name);
1524 ret = -ENOMEM;
1525 goto out_free_alua;
1529 * Setup work_queue for QUEUE_FULL
1531 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1534 * Preload the initial INQUIRY const values if we are doing
1535 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1536 * passthrough because this is being provided by the backend LLD.
1538 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1539 strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1540 strncpy(&dev->t10_wwn.model[0],
1541 dev->transport->inquiry_prod, 16);
1542 strncpy(&dev->t10_wwn.revision[0],
1543 dev->transport->inquiry_rev, 4);
1546 scsi_dump_inquiry(dev);
1548 spin_lock(&hba->device_lock);
1549 hba->dev_count++;
1550 spin_unlock(&hba->device_lock);
1552 mutex_lock(&g_device_mutex);
1553 list_add_tail(&dev->g_dev_node, &g_device_list);
1554 mutex_unlock(&g_device_mutex);
1556 dev->dev_flags |= DF_CONFIGURED;
1558 return 0;
1560 out_free_alua:
1561 core_alua_free_lu_gp_mem(dev);
1562 out:
1563 se_release_vpd_for_dev(dev);
1564 return ret;
1567 void target_free_device(struct se_device *dev)
1569 struct se_hba *hba = dev->se_hba;
1571 WARN_ON(!list_empty(&dev->dev_sep_list));
1573 if (dev->dev_flags & DF_CONFIGURED) {
1574 destroy_workqueue(dev->tmr_wq);
1576 mutex_lock(&g_device_mutex);
1577 list_del(&dev->g_dev_node);
1578 mutex_unlock(&g_device_mutex);
1580 spin_lock(&hba->device_lock);
1581 hba->dev_count--;
1582 spin_unlock(&hba->device_lock);
1585 core_alua_free_lu_gp_mem(dev);
1586 core_scsi3_free_all_registrations(dev);
1587 se_release_vpd_for_dev(dev);
1589 dev->transport->free_device(dev);
1592 int core_dev_setup_virtual_lun0(void)
1594 struct se_hba *hba;
1595 struct se_device *dev;
1596 char buf[] = "rd_pages=8,rd_nullio=1";
1597 int ret;
1599 hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
1600 if (IS_ERR(hba))
1601 return PTR_ERR(hba);
1603 dev = target_alloc_device(hba, "virt_lun0");
1604 if (!dev) {
1605 ret = -ENOMEM;
1606 goto out_free_hba;
1609 hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
1611 ret = target_configure_device(dev);
1612 if (ret)
1613 goto out_free_se_dev;
1615 lun0_hba = hba;
1616 g_lun0_dev = dev;
1617 return 0;
1619 out_free_se_dev:
1620 target_free_device(dev);
1621 out_free_hba:
1622 core_delete_hba(hba);
1623 return ret;
1627 void core_dev_release_virtual_lun0(void)
1629 struct se_hba *hba = lun0_hba;
1631 if (!hba)
1632 return;
1634 if (g_lun0_dev)
1635 target_free_device(g_lun0_dev);
1636 core_delete_hba(hba);