1c66ac9dbSNicholas Bellinger /*******************************************************************************
2c66ac9dbSNicholas Bellinger  * Filename:  target_core_device.c (based on iscsi_target_device.c)
3c66ac9dbSNicholas Bellinger  *
4e3d6f909SAndy Grover  * This file contains the TCM Virtual Device and Disk Transport
5c66ac9dbSNicholas Bellinger  * agnostic related functions.
6c66ac9dbSNicholas Bellinger  *
74c76251eSNicholas Bellinger  * (c) Copyright 2003-2013 Datera, Inc.
8c66ac9dbSNicholas Bellinger  *
9c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
10c66ac9dbSNicholas Bellinger  *
11c66ac9dbSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
12c66ac9dbSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
13c66ac9dbSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
14c66ac9dbSNicholas Bellinger  * (at your option) any later version.
15c66ac9dbSNicholas Bellinger  *
16c66ac9dbSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
17c66ac9dbSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18c66ac9dbSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19c66ac9dbSNicholas Bellinger  * GNU General Public License for more details.
20c66ac9dbSNicholas Bellinger  *
21c66ac9dbSNicholas Bellinger  * You should have received a copy of the GNU General Public License
22c66ac9dbSNicholas Bellinger  * along with this program; if not, write to the Free Software
23c66ac9dbSNicholas Bellinger  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24c66ac9dbSNicholas Bellinger  *
25c66ac9dbSNicholas Bellinger  ******************************************************************************/
26c66ac9dbSNicholas Bellinger 
27c66ac9dbSNicholas Bellinger #include <linux/net.h>
28c66ac9dbSNicholas Bellinger #include <linux/string.h>
29c66ac9dbSNicholas Bellinger #include <linux/delay.h>
30c66ac9dbSNicholas Bellinger #include <linux/timer.h>
31c66ac9dbSNicholas Bellinger #include <linux/slab.h>
32c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
33c66ac9dbSNicholas Bellinger #include <linux/kthread.h>
34c66ac9dbSNicholas Bellinger #include <linux/in.h>
35c53181afSPaul Gortmaker #include <linux/export.h>
368dcf07beSBart Van Assche #include <linux/t10-pi.h>
377bfea53bSAndy Grover #include <asm/unaligned.h>
38c66ac9dbSNicholas Bellinger #include <net/sock.h>
39c66ac9dbSNicholas Bellinger #include <net/tcp.h>
40ba929992SBart Van Assche #include <scsi/scsi_common.h>
41ba929992SBart Van Assche #include <scsi/scsi_proto.h>
42c66ac9dbSNicholas Bellinger 
43c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
44c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
45c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
46c66ac9dbSNicholas Bellinger 
47e26d99aeSChristoph Hellwig #include "target_core_internal.h"
48c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
49c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
50c66ac9dbSNicholas Bellinger #include "target_core_ua.h"
51c66ac9dbSNicholas Bellinger 
52d9ea32bfSNicholas Bellinger DEFINE_MUTEX(g_device_mutex);
53d9ea32bfSNicholas Bellinger LIST_HEAD(g_device_list);
54d9ea32bfSNicholas Bellinger 
55e3d6f909SAndy Grover static struct se_hba *lun0_hba;
56e3d6f909SAndy Grover /* not static, needed by tpg.c */
57e3d6f909SAndy Grover struct se_device *g_lun0_dev;
58e3d6f909SAndy Grover 
59de103c93SChristoph Hellwig sense_reason_t
60f2d30680SHannes Reinecke transport_lookup_cmd_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
61c66ac9dbSNicholas Bellinger {
62c66ac9dbSNicholas Bellinger 	struct se_lun *se_lun = NULL;
63e3d6f909SAndy Grover 	struct se_session *se_sess = se_cmd->se_sess;
6429a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
6529a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
668fa3a867SNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
67c66ac9dbSNicholas Bellinger 
6829a05deeSNicholas Bellinger 	rcu_read_lock();
6929a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, unpacked_lun);
7029a05deeSNicholas Bellinger 	if (deve) {
7129a05deeSNicholas Bellinger 		atomic_long_inc(&deve->total_cmds);
72c66ac9dbSNicholas Bellinger 
735951146dSAndy Grover 		if (se_cmd->data_direction == DMA_TO_DEVICE)
7429a05deeSNicholas Bellinger 			atomic_long_add(se_cmd->data_length,
7529a05deeSNicholas Bellinger 					&deve->write_bytes);
765951146dSAndy Grover 		else if (se_cmd->data_direction == DMA_FROM_DEVICE)
7729a05deeSNicholas Bellinger 			atomic_long_add(se_cmd->data_length,
7829a05deeSNicholas Bellinger 					&deve->read_bytes);
795951146dSAndy Grover 
8029a05deeSNicholas Bellinger 		se_lun = rcu_dereference(deve->se_lun);
8129a05deeSNicholas Bellinger 		se_cmd->se_lun = rcu_dereference(deve->se_lun);
825951146dSAndy Grover 		se_cmd->pr_res_key = deve->pr_res_key;
835951146dSAndy Grover 		se_cmd->orig_fe_lun = unpacked_lun;
845951146dSAndy Grover 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
855277797dSNicholas Bellinger 
865277797dSNicholas Bellinger 		percpu_ref_get(&se_lun->lun_ref);
875277797dSNicholas Bellinger 		se_cmd->lun_ref_active = true;
888fa3a867SNicholas Bellinger 
898fa3a867SNicholas Bellinger 		if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
9003a68b44SAndy Grover 		    deve->lun_access_ro) {
918fa3a867SNicholas Bellinger 			pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
928fa3a867SNicholas Bellinger 				" Access for 0x%08llx\n",
938fa3a867SNicholas Bellinger 				se_cmd->se_tfo->get_fabric_name(),
948fa3a867SNicholas Bellinger 				unpacked_lun);
958fa3a867SNicholas Bellinger 			rcu_read_unlock();
968fa3a867SNicholas Bellinger 			ret = TCM_WRITE_PROTECTED;
978fa3a867SNicholas Bellinger 			goto ref_dev;
988fa3a867SNicholas Bellinger 		}
995951146dSAndy Grover 	}
10029a05deeSNicholas Bellinger 	rcu_read_unlock();
1015951146dSAndy Grover 
1025951146dSAndy Grover 	if (!se_lun) {
103c66ac9dbSNicholas Bellinger 		/*
104c66ac9dbSNicholas Bellinger 		 * Use the se_portal_group->tpg_virt_lun0 to allow for
105c66ac9dbSNicholas Bellinger 		 * REPORT_LUNS, et al to be returned when no active
106c66ac9dbSNicholas Bellinger 		 * MappedLUN=0 exists for this Initiator Port.
107c66ac9dbSNicholas Bellinger 		 */
108c66ac9dbSNicholas Bellinger 		if (unpacked_lun != 0) {
1096708bb27SAndy Grover 			pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
110f2d30680SHannes Reinecke 				" Access for 0x%08llx\n",
111e3d6f909SAndy Grover 				se_cmd->se_tfo->get_fabric_name(),
112c66ac9dbSNicholas Bellinger 				unpacked_lun);
113de103c93SChristoph Hellwig 			return TCM_NON_EXISTENT_LUN;
114c66ac9dbSNicholas Bellinger 		}
1155951146dSAndy Grover 
116adf653f9SChristoph Hellwig 		se_lun = se_sess->se_tpg->tpg_virt_lun0;
117adf653f9SChristoph Hellwig 		se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
118c66ac9dbSNicholas Bellinger 		se_cmd->orig_fe_lun = 0;
119c66ac9dbSNicholas Bellinger 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
1205277797dSNicholas Bellinger 
1215277797dSNicholas Bellinger 		percpu_ref_get(&se_lun->lun_ref);
1225277797dSNicholas Bellinger 		se_cmd->lun_ref_active = true;
1238fa3a867SNicholas Bellinger 
1248fa3a867SNicholas Bellinger 		/*
1258fa3a867SNicholas Bellinger 		 * Force WRITE PROTECT for virtual LUN 0
1268fa3a867SNicholas Bellinger 		 */
1278fa3a867SNicholas Bellinger 		if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
1288fa3a867SNicholas Bellinger 		    (se_cmd->data_direction != DMA_NONE)) {
1298fa3a867SNicholas Bellinger 			ret = TCM_WRITE_PROTECTED;
1308fa3a867SNicholas Bellinger 			goto ref_dev;
1318fa3a867SNicholas Bellinger 		}
132c66ac9dbSNicholas Bellinger 	}
1334cc987eaSNicholas Bellinger 	/*
1344cc987eaSNicholas Bellinger 	 * RCU reference protected by percpu se_lun->lun_ref taken above that
1354cc987eaSNicholas Bellinger 	 * must drop to zero (including initial reference) before this se_lun
1364cc987eaSNicholas Bellinger 	 * pointer can be kfree_rcu() by the final se_lun->lun_group put via
1374cc987eaSNicholas Bellinger 	 * target_core_fabric_configfs.c:target_fabric_port_release
1384cc987eaSNicholas Bellinger 	 */
1398fa3a867SNicholas Bellinger ref_dev:
1404cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1414cc987eaSNicholas Bellinger 	atomic_long_inc(&se_cmd->se_dev->num_cmds);
142c66ac9dbSNicholas Bellinger 
143c66ac9dbSNicholas Bellinger 	if (se_cmd->data_direction == DMA_TO_DEVICE)
1444cc987eaSNicholas Bellinger 		atomic_long_add(se_cmd->data_length,
1454cc987eaSNicholas Bellinger 				&se_cmd->se_dev->write_bytes);
146c66ac9dbSNicholas Bellinger 	else if (se_cmd->data_direction == DMA_FROM_DEVICE)
1474cc987eaSNicholas Bellinger 		atomic_long_add(se_cmd->data_length,
1484cc987eaSNicholas Bellinger 				&se_cmd->se_dev->read_bytes);
149c66ac9dbSNicholas Bellinger 
1508fa3a867SNicholas Bellinger 	return ret;
151c66ac9dbSNicholas Bellinger }
1525951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_cmd_lun);
153c66ac9dbSNicholas Bellinger 
154f2d30680SHannes Reinecke int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
155c66ac9dbSNicholas Bellinger {
156c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
157c66ac9dbSNicholas Bellinger 	struct se_lun *se_lun = NULL;
158e3d6f909SAndy Grover 	struct se_session *se_sess = se_cmd->se_sess;
15929a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
160c66ac9dbSNicholas Bellinger 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1615e1be919SRoland Dreier 	unsigned long flags;
162c66ac9dbSNicholas Bellinger 
16329a05deeSNicholas Bellinger 	rcu_read_lock();
16429a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, unpacked_lun);
16529a05deeSNicholas Bellinger 	if (deve) {
16629a05deeSNicholas Bellinger 		se_tmr->tmr_lun = rcu_dereference(deve->se_lun);
16729a05deeSNicholas Bellinger 		se_cmd->se_lun = rcu_dereference(deve->se_lun);
16829a05deeSNicholas Bellinger 		se_lun = rcu_dereference(deve->se_lun);
169c66ac9dbSNicholas Bellinger 		se_cmd->pr_res_key = deve->pr_res_key;
170c66ac9dbSNicholas Bellinger 		se_cmd->orig_fe_lun = unpacked_lun;
171c66ac9dbSNicholas Bellinger 	}
17229a05deeSNicholas Bellinger 	rcu_read_unlock();
173c66ac9dbSNicholas Bellinger 
174c66ac9dbSNicholas Bellinger 	if (!se_lun) {
1756708bb27SAndy Grover 		pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
176f2d30680SHannes Reinecke 			" Access for 0x%08llx\n",
177e3d6f909SAndy Grover 			se_cmd->se_tfo->get_fabric_name(),
178c66ac9dbSNicholas Bellinger 			unpacked_lun);
179e3d6f909SAndy Grover 		return -ENODEV;
180c66ac9dbSNicholas Bellinger 	}
1814cc987eaSNicholas Bellinger 	/*
1824cc987eaSNicholas Bellinger 	 * XXX: Add percpu se_lun->lun_ref reference count for TMR
1834cc987eaSNicholas Bellinger 	 */
1844cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1854cc987eaSNicholas Bellinger 	se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1865951146dSAndy Grover 
1875e1be919SRoland Dreier 	spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
1885951146dSAndy Grover 	list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
1895e1be919SRoland Dreier 	spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
190c66ac9dbSNicholas Bellinger 
191c66ac9dbSNicholas Bellinger 	return 0;
192c66ac9dbSNicholas Bellinger }
1935951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_tmr_lun);
194c66ac9dbSNicholas Bellinger 
19529a05deeSNicholas Bellinger bool target_lun_is_rdonly(struct se_cmd *cmd)
19629a05deeSNicholas Bellinger {
19729a05deeSNicholas Bellinger 	struct se_session *se_sess = cmd->se_sess;
19829a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
19929a05deeSNicholas Bellinger 	bool ret;
20029a05deeSNicholas Bellinger 
20129a05deeSNicholas Bellinger 	rcu_read_lock();
20229a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
20303a68b44SAndy Grover 	ret = deve && deve->lun_access_ro;
20429a05deeSNicholas Bellinger 	rcu_read_unlock();
20529a05deeSNicholas Bellinger 
20629a05deeSNicholas Bellinger 	return ret;
20729a05deeSNicholas Bellinger }
20829a05deeSNicholas Bellinger EXPORT_SYMBOL(target_lun_is_rdonly);
20929a05deeSNicholas Bellinger 
210c66ac9dbSNicholas Bellinger /*
211c66ac9dbSNicholas Bellinger  * This function is called from core_scsi3_emulate_pro_register_and_move()
21229a05deeSNicholas Bellinger  * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
213c66ac9dbSNicholas Bellinger  * when a matching rtpi is found.
214c66ac9dbSNicholas Bellinger  */
215c66ac9dbSNicholas Bellinger struct se_dev_entry *core_get_se_deve_from_rtpi(
216c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
217c66ac9dbSNicholas Bellinger 	u16 rtpi)
218c66ac9dbSNicholas Bellinger {
219c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
220c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
221c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
222c66ac9dbSNicholas Bellinger 
22329a05deeSNicholas Bellinger 	rcu_read_lock();
22429a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
22529a05deeSNicholas Bellinger 		lun = rcu_dereference(deve->se_lun);
2266708bb27SAndy Grover 		if (!lun) {
2276708bb27SAndy Grover 			pr_err("%s device entries device pointer is"
228c66ac9dbSNicholas Bellinger 				" NULL, but Initiator has access.\n",
229e3d6f909SAndy Grover 				tpg->se_tpg_tfo->get_fabric_name());
230c66ac9dbSNicholas Bellinger 			continue;
231c66ac9dbSNicholas Bellinger 		}
23229a05deeSNicholas Bellinger 		if (lun->lun_rtpi != rtpi)
233c66ac9dbSNicholas Bellinger 			continue;
234c66ac9dbSNicholas Bellinger 
23529a05deeSNicholas Bellinger 		kref_get(&deve->pr_kref);
23629a05deeSNicholas Bellinger 		rcu_read_unlock();
237c66ac9dbSNicholas Bellinger 
238c66ac9dbSNicholas Bellinger 		return deve;
239c66ac9dbSNicholas Bellinger 	}
24029a05deeSNicholas Bellinger 	rcu_read_unlock();
241c66ac9dbSNicholas Bellinger 
242c66ac9dbSNicholas Bellinger 	return NULL;
243c66ac9dbSNicholas Bellinger }
244c66ac9dbSNicholas Bellinger 
24529a05deeSNicholas Bellinger void core_free_device_list_for_node(
246c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
247c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg)
248c66ac9dbSNicholas Bellinger {
249c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
250c66ac9dbSNicholas Bellinger 
25129a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
25229a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
25329a05deeSNicholas Bellinger 		struct se_lun *lun = rcu_dereference_check(deve->se_lun,
25429a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
25529a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
256c66ac9dbSNicholas Bellinger 	}
25729a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
258c66ac9dbSNicholas Bellinger }
259c66ac9dbSNicholas Bellinger 
260c66ac9dbSNicholas Bellinger void core_update_device_list_access(
261f2d30680SHannes Reinecke 	u64 mapped_lun,
26203a68b44SAndy Grover 	bool lun_access_ro,
263c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
264c66ac9dbSNicholas Bellinger {
265c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
266c66ac9dbSNicholas Bellinger 
26729a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
26829a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, mapped_lun);
26903a68b44SAndy Grover 	if (deve)
27003a68b44SAndy Grover 		deve->lun_access_ro = lun_access_ro;
27129a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
272c66ac9dbSNicholas Bellinger }
273c66ac9dbSNicholas Bellinger 
27429a05deeSNicholas Bellinger /*
27529a05deeSNicholas Bellinger  * Called with rcu_read_lock or nacl->device_list_lock held.
276c66ac9dbSNicholas Bellinger  */
277f2d30680SHannes Reinecke struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
27829a05deeSNicholas Bellinger {
27929a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
28029a05deeSNicholas Bellinger 
28129a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
28229a05deeSNicholas Bellinger 		if (deve->mapped_lun == mapped_lun)
28329a05deeSNicholas Bellinger 			return deve;
28429a05deeSNicholas Bellinger 
28529a05deeSNicholas Bellinger 	return NULL;
28629a05deeSNicholas Bellinger }
28729a05deeSNicholas Bellinger EXPORT_SYMBOL(target_nacl_find_deve);
28829a05deeSNicholas Bellinger 
28929a05deeSNicholas Bellinger void target_pr_kref_release(struct kref *kref)
29029a05deeSNicholas Bellinger {
29129a05deeSNicholas Bellinger 	struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
29229a05deeSNicholas Bellinger 						 pr_kref);
29329a05deeSNicholas Bellinger 	complete(&deve->pr_comp);
294c66ac9dbSNicholas Bellinger }
295c66ac9dbSNicholas Bellinger 
2967c0d0d51SHannes Reinecke static void
2977c0d0d51SHannes Reinecke target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
2987c0d0d51SHannes Reinecke 			     bool skip_new)
2997c0d0d51SHannes Reinecke {
3007c0d0d51SHannes Reinecke 	struct se_dev_entry *tmp;
3017c0d0d51SHannes Reinecke 
3027c0d0d51SHannes Reinecke 	rcu_read_lock();
3037c0d0d51SHannes Reinecke 	hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
3047c0d0d51SHannes Reinecke 		if (skip_new && tmp == new)
3057c0d0d51SHannes Reinecke 			continue;
3067c0d0d51SHannes Reinecke 		core_scsi3_ua_allocate(tmp, 0x3F,
3077c0d0d51SHannes Reinecke 				       ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
3087c0d0d51SHannes Reinecke 	}
3097c0d0d51SHannes Reinecke 	rcu_read_unlock();
3107c0d0d51SHannes Reinecke }
3117c0d0d51SHannes Reinecke 
312e80ac6c4SAndy Grover int core_enable_device_list_for_node(
313c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
314c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl,
315f2d30680SHannes Reinecke 	u64 mapped_lun,
31603a68b44SAndy Grover 	bool lun_access_ro,
317c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
318e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
319c66ac9dbSNicholas Bellinger {
32029a05deeSNicholas Bellinger 	struct se_dev_entry *orig, *new;
321c66ac9dbSNicholas Bellinger 
32229a05deeSNicholas Bellinger 	new = kzalloc(sizeof(*new), GFP_KERNEL);
32329a05deeSNicholas Bellinger 	if (!new) {
32429a05deeSNicholas Bellinger 		pr_err("Unable to allocate se_dev_entry memory\n");
32529a05deeSNicholas Bellinger 		return -ENOMEM;
32629a05deeSNicholas Bellinger 	}
327e80ac6c4SAndy Grover 
32829a05deeSNicholas Bellinger 	atomic_set(&new->ua_count, 0);
32929a05deeSNicholas Bellinger 	spin_lock_init(&new->ua_lock);
33029a05deeSNicholas Bellinger 	INIT_LIST_HEAD(&new->ua_list);
331adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&new->lun_link);
332e80ac6c4SAndy Grover 
33329a05deeSNicholas Bellinger 	new->mapped_lun = mapped_lun;
33429a05deeSNicholas Bellinger 	kref_init(&new->pr_kref);
33529a05deeSNicholas Bellinger 	init_completion(&new->pr_comp);
33629a05deeSNicholas Bellinger 
33703a68b44SAndy Grover 	new->lun_access_ro = lun_access_ro;
33829a05deeSNicholas Bellinger 	new->creation_time = get_jiffies_64();
33929a05deeSNicholas Bellinger 	new->attach_count++;
34029a05deeSNicholas Bellinger 
34129a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
34229a05deeSNicholas Bellinger 	orig = target_nacl_find_deve(nacl, mapped_lun);
34329a05deeSNicholas Bellinger 	if (orig && orig->se_lun) {
34429a05deeSNicholas Bellinger 		struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
34529a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
34629a05deeSNicholas Bellinger 
34729a05deeSNicholas Bellinger 		if (orig_lun != lun) {
34829a05deeSNicholas Bellinger 			pr_err("Existing orig->se_lun doesn't match new lun"
34929a05deeSNicholas Bellinger 			       " for dynamic -> explicit NodeACL conversion:"
35029a05deeSNicholas Bellinger 				" %s\n", nacl->initiatorname);
35129a05deeSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
35229a05deeSNicholas Bellinger 			kfree(new);
353e3d6f909SAndy Grover 			return -EINVAL;
354c66ac9dbSNicholas Bellinger 		}
355391e2a6dSNicholas Bellinger 		if (orig->se_lun_acl != NULL) {
356391e2a6dSNicholas Bellinger 			pr_warn_ratelimited("Detected existing explicit"
357391e2a6dSNicholas Bellinger 				" se_lun_acl->se_lun_group reference for %s"
358391e2a6dSNicholas Bellinger 				" mapped_lun: %llu, failing\n",
359391e2a6dSNicholas Bellinger 				 nacl->initiatorname, mapped_lun);
360391e2a6dSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
361391e2a6dSNicholas Bellinger 			kfree(new);
362391e2a6dSNicholas Bellinger 			return -EINVAL;
363391e2a6dSNicholas Bellinger 		}
364c66ac9dbSNicholas Bellinger 
36529a05deeSNicholas Bellinger 		rcu_assign_pointer(new->se_lun, lun);
36629a05deeSNicholas Bellinger 		rcu_assign_pointer(new->se_lun_acl, lun_acl);
36729a05deeSNicholas Bellinger 		hlist_del_rcu(&orig->link);
36829a05deeSNicholas Bellinger 		hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
36929a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
370c66ac9dbSNicholas Bellinger 
3711adff1b3SNicholas Bellinger 		spin_lock(&lun->lun_deve_lock);
372adf653f9SChristoph Hellwig 		list_del(&orig->lun_link);
373adf653f9SChristoph Hellwig 		list_add_tail(&new->lun_link, &lun->lun_deve_list);
3741adff1b3SNicholas Bellinger 		spin_unlock(&lun->lun_deve_lock);
37529a05deeSNicholas Bellinger 
37629a05deeSNicholas Bellinger 		kref_put(&orig->pr_kref, target_pr_kref_release);
37729a05deeSNicholas Bellinger 		wait_for_completion(&orig->pr_comp);
37829a05deeSNicholas Bellinger 
3797c0d0d51SHannes Reinecke 		target_luns_data_has_changed(nacl, new, true);
38029a05deeSNicholas Bellinger 		kfree_rcu(orig, rcu_head);
381c66ac9dbSNicholas Bellinger 		return 0;
382c66ac9dbSNicholas Bellinger 	}
383e80ac6c4SAndy Grover 
38429a05deeSNicholas Bellinger 	rcu_assign_pointer(new->se_lun, lun);
38529a05deeSNicholas Bellinger 	rcu_assign_pointer(new->se_lun_acl, lun_acl);
38629a05deeSNicholas Bellinger 	hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
38729a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
388c66ac9dbSNicholas Bellinger 
3891adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
390adf653f9SChristoph Hellwig 	list_add_tail(&new->lun_link, &lun->lun_deve_list);
3911adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
392c66ac9dbSNicholas Bellinger 
3937c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, new, true);
394c66ac9dbSNicholas Bellinger 	return 0;
395c66ac9dbSNicholas Bellinger }
396c66ac9dbSNicholas Bellinger 
397c66ac9dbSNicholas Bellinger /*
39829a05deeSNicholas Bellinger  *	Called with se_node_acl->lun_entry_mutex held.
399c66ac9dbSNicholas Bellinger  */
40029a05deeSNicholas Bellinger void core_disable_device_list_for_node(
401e80ac6c4SAndy Grover 	struct se_lun *lun,
40229a05deeSNicholas Bellinger 	struct se_dev_entry *orig,
403e80ac6c4SAndy Grover 	struct se_node_acl *nacl,
404e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
405e80ac6c4SAndy Grover {
406e80ac6c4SAndy Grover 	/*
4074cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
4084cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
4094cc987eaSNicholas Bellinger 	 */
4104cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
411e80ac6c4SAndy Grover 	/*
412e80ac6c4SAndy Grover 	 * If the MappedLUN entry is being disabled, the entry in
413adf653f9SChristoph Hellwig 	 * lun->lun_deve_list must be removed now before clearing the
414e80ac6c4SAndy Grover 	 * struct se_dev_entry pointers below as logic in
415e80ac6c4SAndy Grover 	 * core_alua_do_transition_tg_pt() depends on these being present.
416e80ac6c4SAndy Grover 	 *
417e80ac6c4SAndy Grover 	 * deve->se_lun_acl will be NULL for demo-mode created LUNs
418e80ac6c4SAndy Grover 	 * that have not been explicitly converted to MappedLUNs ->
419adf653f9SChristoph Hellwig 	 * struct se_lun_acl, but we remove deve->lun_link from
420adf653f9SChristoph Hellwig 	 * lun->lun_deve_list. This also means that active UAs and
421e80ac6c4SAndy Grover 	 * NodeACL context specific PR metadata for demo-mode
422e80ac6c4SAndy Grover 	 * MappedLUN *deve will be released below..
423e80ac6c4SAndy Grover 	 */
4241adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
425adf653f9SChristoph Hellwig 	list_del(&orig->lun_link);
4261adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
427c66ac9dbSNicholas Bellinger 	/*
428c66ac9dbSNicholas Bellinger 	 * Disable struct se_dev_entry LUN ACL mapping
429c66ac9dbSNicholas Bellinger 	 */
43029a05deeSNicholas Bellinger 	core_scsi3_ua_release_all(orig);
431c66ac9dbSNicholas Bellinger 
43229a05deeSNicholas Bellinger 	hlist_del_rcu(&orig->link);
43380bfdfa9SNicholas Bellinger 	clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
43403a68b44SAndy Grover 	orig->lun_access_ro = false;
43529a05deeSNicholas Bellinger 	orig->creation_time = 0;
43629a05deeSNicholas Bellinger 	orig->attach_count--;
43729a05deeSNicholas Bellinger 	/*
43829a05deeSNicholas Bellinger 	 * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
43929a05deeSNicholas Bellinger 	 * or REGISTER_AND_MOVE PR operation to complete.
44029a05deeSNicholas Bellinger 	 */
44129a05deeSNicholas Bellinger 	kref_put(&orig->pr_kref, target_pr_kref_release);
44229a05deeSNicholas Bellinger 	wait_for_completion(&orig->pr_comp);
44329a05deeSNicholas Bellinger 
4443ccd6e83SNicholas Bellinger 	rcu_assign_pointer(orig->se_lun, NULL);
4453ccd6e83SNicholas Bellinger 	rcu_assign_pointer(orig->se_lun_acl, NULL);
4463ccd6e83SNicholas Bellinger 
44729a05deeSNicholas Bellinger 	kfree_rcu(orig, rcu_head);
448c66ac9dbSNicholas Bellinger 
4494cc987eaSNicholas Bellinger 	core_scsi3_free_pr_reg_from_nacl(dev, nacl);
4507c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, NULL, false);
451c66ac9dbSNicholas Bellinger }
452c66ac9dbSNicholas Bellinger 
453c66ac9dbSNicholas Bellinger /*      core_clear_lun_from_tpg():
454c66ac9dbSNicholas Bellinger  *
455c66ac9dbSNicholas Bellinger  *
456c66ac9dbSNicholas Bellinger  */
457c66ac9dbSNicholas Bellinger void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
458c66ac9dbSNicholas Bellinger {
459c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
460c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
461c66ac9dbSNicholas Bellinger 
462403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
463c66ac9dbSNicholas Bellinger 	list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
464c66ac9dbSNicholas Bellinger 
46529a05deeSNicholas Bellinger 		mutex_lock(&nacl->lun_entry_mutex);
46629a05deeSNicholas Bellinger 		hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
46729a05deeSNicholas Bellinger 			struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
46829a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
46929a05deeSNicholas Bellinger 
47029a05deeSNicholas Bellinger 			if (lun != tmp_lun)
471c66ac9dbSNicholas Bellinger 				continue;
472c66ac9dbSNicholas Bellinger 
47329a05deeSNicholas Bellinger 			core_disable_device_list_for_node(lun, deve, nacl, tpg);
474c66ac9dbSNicholas Bellinger 		}
47529a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
476c66ac9dbSNicholas Bellinger 	}
477403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
478c66ac9dbSNicholas Bellinger }
479c66ac9dbSNicholas Bellinger 
480adf653f9SChristoph Hellwig int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
481c66ac9dbSNicholas Bellinger {
482adf653f9SChristoph Hellwig 	struct se_lun *tmp;
483c66ac9dbSNicholas Bellinger 
484c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
485adf653f9SChristoph Hellwig 	if (dev->export_count == 0x0000ffff) {
4866708bb27SAndy Grover 		pr_warn("Reached dev->dev_port_count =="
487c66ac9dbSNicholas Bellinger 				" 0x0000ffff\n");
488c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
489adf653f9SChristoph Hellwig 		return -ENOSPC;
490c66ac9dbSNicholas Bellinger 	}
491c66ac9dbSNicholas Bellinger again:
492c66ac9dbSNicholas Bellinger 	/*
49335d1efe8SMasanari Iida 	 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
494c66ac9dbSNicholas Bellinger 	 * Here is the table from spc4r17 section 7.7.3.8.
495c66ac9dbSNicholas Bellinger 	 *
496c66ac9dbSNicholas Bellinger 	 *    Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
497c66ac9dbSNicholas Bellinger 	 *
498c66ac9dbSNicholas Bellinger 	 * Code      Description
499c66ac9dbSNicholas Bellinger 	 * 0h        Reserved
500c66ac9dbSNicholas Bellinger 	 * 1h        Relative port 1, historically known as port A
501c66ac9dbSNicholas Bellinger 	 * 2h        Relative port 2, historically known as port B
502c66ac9dbSNicholas Bellinger 	 * 3h to FFFFh    Relative port 3 through 65 535
503c66ac9dbSNicholas Bellinger 	 */
504adf653f9SChristoph Hellwig 	lun->lun_rtpi = dev->dev_rpti_counter++;
505adf653f9SChristoph Hellwig 	if (!lun->lun_rtpi)
506c66ac9dbSNicholas Bellinger 		goto again;
507c66ac9dbSNicholas Bellinger 
508adf653f9SChristoph Hellwig 	list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
509c66ac9dbSNicholas Bellinger 		/*
51035d1efe8SMasanari Iida 		 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
511c66ac9dbSNicholas Bellinger 		 * for 16-bit wrap..
512c66ac9dbSNicholas Bellinger 		 */
513adf653f9SChristoph Hellwig 		if (lun->lun_rtpi == tmp->lun_rtpi)
514c66ac9dbSNicholas Bellinger 			goto again;
515c66ac9dbSNicholas Bellinger 	}
516c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
517c66ac9dbSNicholas Bellinger 
518c66ac9dbSNicholas Bellinger 	return 0;
519c66ac9dbSNicholas Bellinger }
520c66ac9dbSNicholas Bellinger 
5210fd97ccfSChristoph Hellwig static void se_release_vpd_for_dev(struct se_device *dev)
522c66ac9dbSNicholas Bellinger {
523c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd, *vpd_tmp;
524c66ac9dbSNicholas Bellinger 
5250fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_wwn.t10_vpd_lock);
526c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(vpd, vpd_tmp,
5270fd97ccfSChristoph Hellwig 			&dev->t10_wwn.t10_vpd_list, vpd_list) {
528c66ac9dbSNicholas Bellinger 		list_del(&vpd->vpd_list);
529c66ac9dbSNicholas Bellinger 		kfree(vpd);
530c66ac9dbSNicholas Bellinger 	}
5310fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_wwn.t10_vpd_lock);
532c66ac9dbSNicholas Bellinger }
533c66ac9dbSNicholas Bellinger 
534c8045372SRoland Dreier static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
535525a48a2SNicholas Bellinger {
5363e03989bSRoland Dreier 	u32 aligned_max_sectors;
5373e03989bSRoland Dreier 	u32 alignment;
538525a48a2SNicholas Bellinger 	/*
539525a48a2SNicholas Bellinger 	 * Limit max_sectors to a PAGE_SIZE aligned value for modern
540525a48a2SNicholas Bellinger 	 * transport_allocate_data_tasks() operation.
541525a48a2SNicholas Bellinger 	 */
5423e03989bSRoland Dreier 	alignment = max(1ul, PAGE_SIZE / block_size);
5433e03989bSRoland Dreier 	aligned_max_sectors = rounddown(max_sectors, alignment);
544525a48a2SNicholas Bellinger 
5453e03989bSRoland Dreier 	if (max_sectors != aligned_max_sectors)
5463e03989bSRoland Dreier 		pr_info("Rounding down aligned max_sectors from %u to %u\n",
5473e03989bSRoland Dreier 			max_sectors, aligned_max_sectors);
5483e03989bSRoland Dreier 
5493e03989bSRoland Dreier 	return aligned_max_sectors;
550525a48a2SNicholas Bellinger }
551525a48a2SNicholas Bellinger 
5526bb82612SNicholas Bellinger int core_dev_add_lun(
553c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
554c66ac9dbSNicholas Bellinger 	struct se_device *dev,
5556bb82612SNicholas Bellinger 	struct se_lun *lun)
556c66ac9dbSNicholas Bellinger {
5578d9efe53SSebastian Andrzej Siewior 	int rc;
558c66ac9dbSNicholas Bellinger 
55903a68b44SAndy Grover 	rc = core_tpg_add_lun(tpg, lun, false, dev);
5608d9efe53SSebastian Andrzej Siewior 	if (rc < 0)
5616bb82612SNicholas Bellinger 		return rc;
562c66ac9dbSNicholas Bellinger 
563f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
564e3d6f909SAndy Grover 		" CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
5652af7973aSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
5662dca673bSAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
567c66ac9dbSNicholas Bellinger 	/*
568c66ac9dbSNicholas Bellinger 	 * Update LUN maps for dynamically added initiators when
569c66ac9dbSNicholas Bellinger 	 * generate_node_acl is enabled.
570c66ac9dbSNicholas Bellinger 	 */
571e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
572c66ac9dbSNicholas Bellinger 		struct se_node_acl *acl;
573403edd78SNicholas Bellinger 
574403edd78SNicholas Bellinger 		mutex_lock(&tpg->acl_node_mutex);
575c66ac9dbSNicholas Bellinger 		list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
576052605c6SNicholas Bellinger 			if (acl->dynamic_node_acl &&
577052605c6SNicholas Bellinger 			    (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
578052605c6SNicholas Bellinger 			     !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
579df9766caSNicholas Bellinger 				core_tpg_add_node_to_devs(acl, tpg, lun);
580c66ac9dbSNicholas Bellinger 			}
581c66ac9dbSNicholas Bellinger 		}
582403edd78SNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
583c66ac9dbSNicholas Bellinger 	}
584c66ac9dbSNicholas Bellinger 
5856bb82612SNicholas Bellinger 	return 0;
586c66ac9dbSNicholas Bellinger }
587c66ac9dbSNicholas Bellinger 
588c66ac9dbSNicholas Bellinger /*      core_dev_del_lun():
589c66ac9dbSNicholas Bellinger  *
590c66ac9dbSNicholas Bellinger  *
591c66ac9dbSNicholas Bellinger  */
592cd9d7cbaSAndy Grover void core_dev_del_lun(
593c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
594cd9d7cbaSAndy Grover 	struct se_lun *lun)
595c66ac9dbSNicholas Bellinger {
596f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
597e3d6f909SAndy Grover 		" device object\n", tpg->se_tpg_tfo->get_fabric_name(),
598cd9d7cbaSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
599e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name());
600c66ac9dbSNicholas Bellinger 
601cd9d7cbaSAndy Grover 	core_tpg_remove_lun(tpg, lun);
602c66ac9dbSNicholas Bellinger }
603c66ac9dbSNicholas Bellinger 
604c66ac9dbSNicholas Bellinger struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
605c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
606fcf29481SNicholas Bellinger 	struct se_node_acl *nacl,
607f2d30680SHannes Reinecke 	u64 mapped_lun,
608c66ac9dbSNicholas Bellinger 	int *ret)
609c66ac9dbSNicholas Bellinger {
610c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl;
611c66ac9dbSNicholas Bellinger 
612fcf29481SNicholas Bellinger 	if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
6136708bb27SAndy Grover 		pr_err("%s InitiatorName exceeds maximum size.\n",
614e3d6f909SAndy Grover 			tpg->se_tpg_tfo->get_fabric_name());
615c66ac9dbSNicholas Bellinger 		*ret = -EOVERFLOW;
616c66ac9dbSNicholas Bellinger 		return NULL;
617c66ac9dbSNicholas Bellinger 	}
618c66ac9dbSNicholas Bellinger 	lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
6196708bb27SAndy Grover 	if (!lacl) {
6206708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct se_lun_acl.\n");
621c66ac9dbSNicholas Bellinger 		*ret = -ENOMEM;
622c66ac9dbSNicholas Bellinger 		return NULL;
623c66ac9dbSNicholas Bellinger 	}
624c66ac9dbSNicholas Bellinger 
625c66ac9dbSNicholas Bellinger 	lacl->mapped_lun = mapped_lun;
626c66ac9dbSNicholas Bellinger 	lacl->se_lun_nacl = nacl;
627c66ac9dbSNicholas Bellinger 
628c66ac9dbSNicholas Bellinger 	return lacl;
629c66ac9dbSNicholas Bellinger }
630c66ac9dbSNicholas Bellinger 
631c66ac9dbSNicholas Bellinger int core_dev_add_initiator_node_lun_acl(
632c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
633c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl,
6346bb82612SNicholas Bellinger 	struct se_lun *lun,
63503a68b44SAndy Grover 	bool lun_access_ro)
636c66ac9dbSNicholas Bellinger {
6376bb82612SNicholas Bellinger 	struct se_node_acl *nacl = lacl->se_lun_nacl;
6384cc987eaSNicholas Bellinger 	/*
6394cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
6404cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
6414cc987eaSNicholas Bellinger 	 */
6424cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
643c66ac9dbSNicholas Bellinger 
6446708bb27SAndy Grover 	if (!nacl)
645c66ac9dbSNicholas Bellinger 		return -EINVAL;
646c66ac9dbSNicholas Bellinger 
64703a68b44SAndy Grover 	if (lun->lun_access_ro)
64803a68b44SAndy Grover 		lun_access_ro = true;
649c66ac9dbSNicholas Bellinger 
650c66ac9dbSNicholas Bellinger 	lacl->se_lun = lun;
651c66ac9dbSNicholas Bellinger 
652e80ac6c4SAndy Grover 	if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
65303a68b44SAndy Grover 			lun_access_ro, nacl, tpg) < 0)
654c66ac9dbSNicholas Bellinger 		return -EINVAL;
655c66ac9dbSNicholas Bellinger 
656f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
657e3d6f909SAndy Grover 		" InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
6586bb82612SNicholas Bellinger 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
65903a68b44SAndy Grover 		lun_access_ro ? "RO" : "RW",
660b6a54b8dSChris Zankel 		nacl->initiatorname);
661c66ac9dbSNicholas Bellinger 	/*
662c66ac9dbSNicholas Bellinger 	 * Check to see if there are any existing persistent reservation APTPL
663c66ac9dbSNicholas Bellinger 	 * pre-registrations that need to be enabled for this LUN ACL..
664c66ac9dbSNicholas Bellinger 	 */
6654cc987eaSNicholas Bellinger 	core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
666e2480563SNicholas Bellinger 					    lacl->mapped_lun);
667c66ac9dbSNicholas Bellinger 	return 0;
668c66ac9dbSNicholas Bellinger }
669c66ac9dbSNicholas Bellinger 
670c66ac9dbSNicholas Bellinger int core_dev_del_initiator_node_lun_acl(
671c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
672c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
673c66ac9dbSNicholas Bellinger {
674adf653f9SChristoph Hellwig 	struct se_portal_group *tpg = lun->lun_tpg;
675c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
67629a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
677c66ac9dbSNicholas Bellinger 
678c66ac9dbSNicholas Bellinger 	nacl = lacl->se_lun_nacl;
6796708bb27SAndy Grover 	if (!nacl)
680c66ac9dbSNicholas Bellinger 		return -EINVAL;
681c66ac9dbSNicholas Bellinger 
68229a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
68329a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
68429a05deeSNicholas Bellinger 	if (deve)
68529a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
68629a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
687c66ac9dbSNicholas Bellinger 
688f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
689f2d30680SHannes Reinecke 		" InitiatorNode: %s Mapped LUN: %llu\n",
690e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(),
691e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
692b6a54b8dSChris Zankel 		nacl->initiatorname, lacl->mapped_lun);
693c66ac9dbSNicholas Bellinger 
694c66ac9dbSNicholas Bellinger 	return 0;
695c66ac9dbSNicholas Bellinger }
696c66ac9dbSNicholas Bellinger 
697c66ac9dbSNicholas Bellinger void core_dev_free_initiator_node_lun_acl(
698c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
699c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
700c66ac9dbSNicholas Bellinger {
7016708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
702f2d30680SHannes Reinecke 		" Mapped LUN: %llu\n", tpg->se_tpg_tfo->get_fabric_name(),
703e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
704e3d6f909SAndy Grover 		tpg->se_tpg_tfo->get_fabric_name(),
705b6a54b8dSChris Zankel 		lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
706c66ac9dbSNicholas Bellinger 
707c66ac9dbSNicholas Bellinger 	kfree(lacl);
708c66ac9dbSNicholas Bellinger }
709c66ac9dbSNicholas Bellinger 
7100fd97ccfSChristoph Hellwig static void scsi_dump_inquiry(struct se_device *dev)
7110fd97ccfSChristoph Hellwig {
7120fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
7130fd97ccfSChristoph Hellwig 	char buf[17];
7140fd97ccfSChristoph Hellwig 	int i, device_type;
7150fd97ccfSChristoph Hellwig 	/*
7160fd97ccfSChristoph Hellwig 	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
7170fd97ccfSChristoph Hellwig 	 */
7180fd97ccfSChristoph Hellwig 	for (i = 0; i < 8; i++)
7190fd97ccfSChristoph Hellwig 		if (wwn->vendor[i] >= 0x20)
7200fd97ccfSChristoph Hellwig 			buf[i] = wwn->vendor[i];
7210fd97ccfSChristoph Hellwig 		else
7220fd97ccfSChristoph Hellwig 			buf[i] = ' ';
7230fd97ccfSChristoph Hellwig 	buf[i] = '\0';
7240fd97ccfSChristoph Hellwig 	pr_debug("  Vendor: %s\n", buf);
7250fd97ccfSChristoph Hellwig 
7260fd97ccfSChristoph Hellwig 	for (i = 0; i < 16; i++)
7270fd97ccfSChristoph Hellwig 		if (wwn->model[i] >= 0x20)
7280fd97ccfSChristoph Hellwig 			buf[i] = wwn->model[i];
7290fd97ccfSChristoph Hellwig 		else
7300fd97ccfSChristoph Hellwig 			buf[i] = ' ';
7310fd97ccfSChristoph Hellwig 	buf[i] = '\0';
7320fd97ccfSChristoph Hellwig 	pr_debug("  Model: %s\n", buf);
7330fd97ccfSChristoph Hellwig 
7340fd97ccfSChristoph Hellwig 	for (i = 0; i < 4; i++)
7350fd97ccfSChristoph Hellwig 		if (wwn->revision[i] >= 0x20)
7360fd97ccfSChristoph Hellwig 			buf[i] = wwn->revision[i];
7370fd97ccfSChristoph Hellwig 		else
7380fd97ccfSChristoph Hellwig 			buf[i] = ' ';
7390fd97ccfSChristoph Hellwig 	buf[i] = '\0';
7400fd97ccfSChristoph Hellwig 	pr_debug("  Revision: %s\n", buf);
7410fd97ccfSChristoph Hellwig 
7420fd97ccfSChristoph Hellwig 	device_type = dev->transport->get_device_type(dev);
7430fd97ccfSChristoph Hellwig 	pr_debug("  Type:   %s ", scsi_device_type(device_type));
7440fd97ccfSChristoph Hellwig }
7450fd97ccfSChristoph Hellwig 
7460fd97ccfSChristoph Hellwig struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
7470fd97ccfSChristoph Hellwig {
7480fd97ccfSChristoph Hellwig 	struct se_device *dev;
7494863e525SNicholas Bellinger 	struct se_lun *xcopy_lun;
7500fd97ccfSChristoph Hellwig 
7510a06d430SChristoph Hellwig 	dev = hba->backend->ops->alloc_device(hba, name);
7520fd97ccfSChristoph Hellwig 	if (!dev)
7530fd97ccfSChristoph Hellwig 		return NULL;
7540fd97ccfSChristoph Hellwig 
7550ff87549SNicholas Bellinger 	dev->dev_link_magic = SE_DEV_LINK_MAGIC;
7560fd97ccfSChristoph Hellwig 	dev->se_hba = hba;
7570a06d430SChristoph Hellwig 	dev->transport = hba->backend->ops;
758fe052a18SSagi Grimberg 	dev->prot_length = sizeof(struct t10_pi_tuple);
7594cc987eaSNicholas Bellinger 	dev->hba_index = hba->hba_index;
7600fd97ccfSChristoph Hellwig 
7610fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_list);
7620fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_sep_list);
7630fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_tmr_list);
7640fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->delayed_cmd_list);
7650fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->state_list);
7660fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->qf_cmd_list);
767d9ea32bfSNicholas Bellinger 	INIT_LIST_HEAD(&dev->g_dev_node);
7680fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->execute_task_lock);
7690fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->delayed_cmd_lock);
7700fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->dev_reservation_lock);
7710fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_port_lock);
7720fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_tmr_lock);
7730fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->qf_cmd_lock);
77468ff9b9bSNicholas Bellinger 	sema_init(&dev->caw_sem, 1);
7750fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
7760fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
7770fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.registration_list);
7780fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
7790fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.registration_lock);
7800fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
7810fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
7820fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
783c66094bfSHannes Reinecke 	INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
784c66094bfSHannes Reinecke 	spin_lock_init(&dev->t10_alua.lba_map_lock);
7850fd97ccfSChristoph Hellwig 
7860fd97ccfSChristoph Hellwig 	dev->t10_wwn.t10_dev = dev;
7870fd97ccfSChristoph Hellwig 	dev->t10_alua.t10_dev = dev;
7880fd97ccfSChristoph Hellwig 
7890fd97ccfSChristoph Hellwig 	dev->dev_attrib.da_dev = dev;
790adfa9570STregaron Bayly 	dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
791814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_dpo = 1;
792814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_write = 1;
793814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_read = 1;
7940fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
7950fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
7960fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
7970fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
7980fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
7990123a9ecSNicholas Bellinger 	dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
800d397a445SNicholas Bellinger 	dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
8012ed22c9cSNicholas Bellinger 	dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
8020fd97ccfSChristoph Hellwig 	dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
80392404e60SNicholas Bellinger 	dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
8040fd97ccfSChristoph Hellwig 	dev->dev_attrib.is_nonrot = DA_IS_NONROT;
8050fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
8060fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
8070fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_block_desc_count =
8080fd97ccfSChristoph Hellwig 		DA_MAX_UNMAP_BLOCK_DESC_COUNT;
8090fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
8100fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity_alignment =
8110fd97ccfSChristoph Hellwig 				DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
812e6f41633SJamie Pocas 	dev->dev_attrib.unmap_zeroes_data =
813e6f41633SJamie Pocas 				DA_UNMAP_ZEROES_DATA_DEFAULT;
814773cbaf7SNicholas Bellinger 	dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
8150fd97ccfSChristoph Hellwig 
8164863e525SNicholas Bellinger 	xcopy_lun = &dev->xcopy_lun;
8174cc987eaSNicholas Bellinger 	rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
8184863e525SNicholas Bellinger 	init_completion(&xcopy_lun->lun_ref_comp);
819adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
820adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
821adf653f9SChristoph Hellwig 	mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
822adf653f9SChristoph Hellwig 	xcopy_lun->lun_tpg = &xcopy_pt_tpg;
8234863e525SNicholas Bellinger 
8240fd97ccfSChristoph Hellwig 	return dev;
8250fd97ccfSChristoph Hellwig }
8260fd97ccfSChristoph Hellwig 
8278a9ebe71SMike Christie /*
8288a9ebe71SMike Christie  * Check if the underlying struct block_device request_queue supports
8298a9ebe71SMike Christie  * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
8308a9ebe71SMike Christie  * in ATA and we need to set TPE=1
8318a9ebe71SMike Christie  */
8328a9ebe71SMike Christie bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
833ea263c7fSMike Christie 				       struct request_queue *q)
8348a9ebe71SMike Christie {
835ea263c7fSMike Christie 	int block_size = queue_logical_block_size(q);
836ea263c7fSMike Christie 
8378a9ebe71SMike Christie 	if (!blk_queue_discard(q))
8388a9ebe71SMike Christie 		return false;
8398a9ebe71SMike Christie 
840ea263c7fSMike Christie 	attrib->max_unmap_lba_count =
841ea263c7fSMike Christie 		q->limits.max_discard_sectors >> (ilog2(block_size) - 9);
8428a9ebe71SMike Christie 	/*
8438a9ebe71SMike Christie 	 * Currently hardcoded to 1 in Linux/SCSI code..
8448a9ebe71SMike Christie 	 */
8458a9ebe71SMike Christie 	attrib->max_unmap_block_desc_count = 1;
8468a9ebe71SMike Christie 	attrib->unmap_granularity = q->limits.discard_granularity / block_size;
8478a9ebe71SMike Christie 	attrib->unmap_granularity_alignment = q->limits.discard_alignment /
8488a9ebe71SMike Christie 								block_size;
8498a9ebe71SMike Christie 	attrib->unmap_zeroes_data = q->limits.discard_zeroes_data;
8508a9ebe71SMike Christie 	return true;
8518a9ebe71SMike Christie }
8528a9ebe71SMike Christie EXPORT_SYMBOL(target_configure_unmap_from_queue);
8538a9ebe71SMike Christie 
8548a9ebe71SMike Christie /*
8558a9ebe71SMike Christie  * Convert from blocksize advertised to the initiator to the 512 byte
8568a9ebe71SMike Christie  * units unconditionally used by the Linux block layer.
8578a9ebe71SMike Christie  */
8588a9ebe71SMike Christie sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
8598a9ebe71SMike Christie {
8608a9ebe71SMike Christie 	switch (dev->dev_attrib.block_size) {
8618a9ebe71SMike Christie 	case 4096:
8628a9ebe71SMike Christie 		return lb << 3;
8638a9ebe71SMike Christie 	case 2048:
8648a9ebe71SMike Christie 		return lb << 2;
8658a9ebe71SMike Christie 	case 1024:
8668a9ebe71SMike Christie 		return lb << 1;
8678a9ebe71SMike Christie 	default:
8688a9ebe71SMike Christie 		return lb;
8698a9ebe71SMike Christie 	}
8708a9ebe71SMike Christie }
8718a9ebe71SMike Christie EXPORT_SYMBOL(target_to_linux_sector);
8728a9ebe71SMike Christie 
8730fd97ccfSChristoph Hellwig int target_configure_device(struct se_device *dev)
8740fd97ccfSChristoph Hellwig {
8750fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
8760fd97ccfSChristoph Hellwig 	int ret;
8770fd97ccfSChristoph Hellwig 
8780fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_CONFIGURED) {
8790fd97ccfSChristoph Hellwig 		pr_err("se_dev->se_dev_ptr already set for storage"
8800fd97ccfSChristoph Hellwig 				" object\n");
8810fd97ccfSChristoph Hellwig 		return -EEXIST;
8820fd97ccfSChristoph Hellwig 	}
8830fd97ccfSChristoph Hellwig 
8840fd97ccfSChristoph Hellwig 	ret = dev->transport->configure_device(dev);
8850fd97ccfSChristoph Hellwig 	if (ret)
8860fd97ccfSChristoph Hellwig 		goto out;
8870fd97ccfSChristoph Hellwig 	/*
8880fd97ccfSChristoph Hellwig 	 * XXX: there is not much point to have two different values here..
8890fd97ccfSChristoph Hellwig 	 */
8900fd97ccfSChristoph Hellwig 	dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
8910fd97ccfSChristoph Hellwig 	dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
8920fd97ccfSChristoph Hellwig 
8930fd97ccfSChristoph Hellwig 	/*
8940fd97ccfSChristoph Hellwig 	 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
8950fd97ccfSChristoph Hellwig 	 */
8960fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_max_sectors =
8970fd97ccfSChristoph Hellwig 		se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
8980fd97ccfSChristoph Hellwig 					 dev->dev_attrib.hw_block_size);
899046ba642SNicholas Bellinger 	dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
9000fd97ccfSChristoph Hellwig 
9010fd97ccfSChristoph Hellwig 	dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
9020fd97ccfSChristoph Hellwig 	dev->creation_time = get_jiffies_64();
9030fd97ccfSChristoph Hellwig 
9040fd97ccfSChristoph Hellwig 	ret = core_setup_alua(dev);
9050fd97ccfSChristoph Hellwig 	if (ret)
9060fd97ccfSChristoph Hellwig 		goto out;
9070fd97ccfSChristoph Hellwig 
9080fd97ccfSChristoph Hellwig 	/*
9090fd97ccfSChristoph Hellwig 	 * Startup the struct se_device processing thread
9100fd97ccfSChristoph Hellwig 	 */
9110fd97ccfSChristoph Hellwig 	dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
9120fd97ccfSChristoph Hellwig 				      dev->transport->name);
9130fd97ccfSChristoph Hellwig 	if (!dev->tmr_wq) {
9140fd97ccfSChristoph Hellwig 		pr_err("Unable to create tmr workqueue for %s\n",
9150fd97ccfSChristoph Hellwig 			dev->transport->name);
9160fd97ccfSChristoph Hellwig 		ret = -ENOMEM;
9170fd97ccfSChristoph Hellwig 		goto out_free_alua;
9180fd97ccfSChristoph Hellwig 	}
9190fd97ccfSChristoph Hellwig 
9200fd97ccfSChristoph Hellwig 	/*
9210fd97ccfSChristoph Hellwig 	 * Setup work_queue for QUEUE_FULL
9220fd97ccfSChristoph Hellwig 	 */
9230fd97ccfSChristoph Hellwig 	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
9240fd97ccfSChristoph Hellwig 
9250fd97ccfSChristoph Hellwig 	/*
9260fd97ccfSChristoph Hellwig 	 * Preload the initial INQUIRY const values if we are doing
9270fd97ccfSChristoph Hellwig 	 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
9280fd97ccfSChristoph Hellwig 	 * passthrough because this is being provided by the backend LLD.
9290fd97ccfSChristoph Hellwig 	 */
930a3541703SAndy Grover 	if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)) {
9310fd97ccfSChristoph Hellwig 		strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
9320fd97ccfSChristoph Hellwig 		strncpy(&dev->t10_wwn.model[0],
9330fd97ccfSChristoph Hellwig 			dev->transport->inquiry_prod, 16);
9340fd97ccfSChristoph Hellwig 		strncpy(&dev->t10_wwn.revision[0],
9350fd97ccfSChristoph Hellwig 			dev->transport->inquiry_rev, 4);
9360fd97ccfSChristoph Hellwig 	}
9370fd97ccfSChristoph Hellwig 
9380fd97ccfSChristoph Hellwig 	scsi_dump_inquiry(dev);
9390fd97ccfSChristoph Hellwig 
9400fd97ccfSChristoph Hellwig 	spin_lock(&hba->device_lock);
9410fd97ccfSChristoph Hellwig 	hba->dev_count++;
9420fd97ccfSChristoph Hellwig 	spin_unlock(&hba->device_lock);
943d9ea32bfSNicholas Bellinger 
944d9ea32bfSNicholas Bellinger 	mutex_lock(&g_device_mutex);
945d9ea32bfSNicholas Bellinger 	list_add_tail(&dev->g_dev_node, &g_device_list);
946d9ea32bfSNicholas Bellinger 	mutex_unlock(&g_device_mutex);
947d9ea32bfSNicholas Bellinger 
9485f7da044SNicholas Bellinger 	dev->dev_flags |= DF_CONFIGURED;
9495f7da044SNicholas Bellinger 
9500fd97ccfSChristoph Hellwig 	return 0;
9510fd97ccfSChristoph Hellwig 
9520fd97ccfSChristoph Hellwig out_free_alua:
9530fd97ccfSChristoph Hellwig 	core_alua_free_lu_gp_mem(dev);
9540fd97ccfSChristoph Hellwig out:
9550fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
9560fd97ccfSChristoph Hellwig 	return ret;
9570fd97ccfSChristoph Hellwig }
9580fd97ccfSChristoph Hellwig 
9590fd97ccfSChristoph Hellwig void target_free_device(struct se_device *dev)
9600fd97ccfSChristoph Hellwig {
9610fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
9620fd97ccfSChristoph Hellwig 
9630fd97ccfSChristoph Hellwig 	WARN_ON(!list_empty(&dev->dev_sep_list));
9640fd97ccfSChristoph Hellwig 
9650fd97ccfSChristoph Hellwig 	if (dev->dev_flags & DF_CONFIGURED) {
9660fd97ccfSChristoph Hellwig 		destroy_workqueue(dev->tmr_wq);
9670fd97ccfSChristoph Hellwig 
968d9ea32bfSNicholas Bellinger 		mutex_lock(&g_device_mutex);
969d9ea32bfSNicholas Bellinger 		list_del(&dev->g_dev_node);
970d9ea32bfSNicholas Bellinger 		mutex_unlock(&g_device_mutex);
971d9ea32bfSNicholas Bellinger 
9720fd97ccfSChristoph Hellwig 		spin_lock(&hba->device_lock);
9730fd97ccfSChristoph Hellwig 		hba->dev_count--;
9740fd97ccfSChristoph Hellwig 		spin_unlock(&hba->device_lock);
9750fd97ccfSChristoph Hellwig 	}
9760fd97ccfSChristoph Hellwig 
9770fd97ccfSChristoph Hellwig 	core_alua_free_lu_gp_mem(dev);
978229d4f11SHannes Reinecke 	core_alua_set_lba_map(dev, NULL, 0, 0);
9790fd97ccfSChristoph Hellwig 	core_scsi3_free_all_registrations(dev);
9800fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
9810fd97ccfSChristoph Hellwig 
9822ed22c9cSNicholas Bellinger 	if (dev->transport->free_prot)
9832ed22c9cSNicholas Bellinger 		dev->transport->free_prot(dev);
9842ed22c9cSNicholas Bellinger 
9850fd97ccfSChristoph Hellwig 	dev->transport->free_device(dev);
9860fd97ccfSChristoph Hellwig }
9870fd97ccfSChristoph Hellwig 
988c66ac9dbSNicholas Bellinger int core_dev_setup_virtual_lun0(void)
989c66ac9dbSNicholas Bellinger {
990c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
991c66ac9dbSNicholas Bellinger 	struct se_device *dev;
992db5d1c3cSAndy Grover 	char buf[] = "rd_pages=8,rd_nullio=1";
993c66ac9dbSNicholas Bellinger 	int ret;
994c66ac9dbSNicholas Bellinger 
9956708bb27SAndy Grover 	hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
996c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
997c66ac9dbSNicholas Bellinger 		return PTR_ERR(hba);
998c66ac9dbSNicholas Bellinger 
9990fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, "virt_lun0");
10000fd97ccfSChristoph Hellwig 	if (!dev) {
1001c66ac9dbSNicholas Bellinger 		ret = -ENOMEM;
10020fd97ccfSChristoph Hellwig 		goto out_free_hba;
1003c66ac9dbSNicholas Bellinger 	}
1004c66ac9dbSNicholas Bellinger 
10050a06d430SChristoph Hellwig 	hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
1006c66ac9dbSNicholas Bellinger 
10070fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
10080fd97ccfSChristoph Hellwig 	if (ret)
10090fd97ccfSChristoph Hellwig 		goto out_free_se_dev;
10100fd97ccfSChristoph Hellwig 
10110fd97ccfSChristoph Hellwig 	lun0_hba = hba;
1012e3d6f909SAndy Grover 	g_lun0_dev = dev;
1013c66ac9dbSNicholas Bellinger 	return 0;
10140fd97ccfSChristoph Hellwig 
10150fd97ccfSChristoph Hellwig out_free_se_dev:
10160fd97ccfSChristoph Hellwig 	target_free_device(dev);
10170fd97ccfSChristoph Hellwig out_free_hba:
10180fd97ccfSChristoph Hellwig 	core_delete_hba(hba);
1019c66ac9dbSNicholas Bellinger 	return ret;
1020c66ac9dbSNicholas Bellinger }
1021c66ac9dbSNicholas Bellinger 
1022c66ac9dbSNicholas Bellinger 
1023c66ac9dbSNicholas Bellinger void core_dev_release_virtual_lun0(void)
1024c66ac9dbSNicholas Bellinger {
1025e3d6f909SAndy Grover 	struct se_hba *hba = lun0_hba;
1026c66ac9dbSNicholas Bellinger 
10276708bb27SAndy Grover 	if (!hba)
1028c66ac9dbSNicholas Bellinger 		return;
1029c66ac9dbSNicholas Bellinger 
1030e3d6f909SAndy Grover 	if (g_lun0_dev)
10310fd97ccfSChristoph Hellwig 		target_free_device(g_lun0_dev);
1032c66ac9dbSNicholas Bellinger 	core_delete_hba(hba);
1033c66ac9dbSNicholas Bellinger }
10347bfea53bSAndy Grover 
10357bfea53bSAndy Grover /*
10367bfea53bSAndy Grover  * Common CDB parsing for kernel and user passthrough.
10377bfea53bSAndy Grover  */
10387bfea53bSAndy Grover sense_reason_t
10397bfea53bSAndy Grover passthrough_parse_cdb(struct se_cmd *cmd,
10407bfea53bSAndy Grover 	sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
10417bfea53bSAndy Grover {
10427bfea53bSAndy Grover 	unsigned char *cdb = cmd->t_task_cdb;
10437bfea53bSAndy Grover 
10447bfea53bSAndy Grover 	/*
10457bfea53bSAndy Grover 	 * Clear a lun set in the cdb if the initiator talking to use spoke
10467bfea53bSAndy Grover 	 * and old standards version, as we can't assume the underlying device
10477bfea53bSAndy Grover 	 * won't choke up on it.
10487bfea53bSAndy Grover 	 */
10497bfea53bSAndy Grover 	switch (cdb[0]) {
10507bfea53bSAndy Grover 	case READ_10: /* SBC - RDProtect */
10517bfea53bSAndy Grover 	case READ_12: /* SBC - RDProtect */
10527bfea53bSAndy Grover 	case READ_16: /* SBC - RDProtect */
10537bfea53bSAndy Grover 	case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
10547bfea53bSAndy Grover 	case VERIFY: /* SBC - VRProtect */
10557bfea53bSAndy Grover 	case VERIFY_16: /* SBC - VRProtect */
10567bfea53bSAndy Grover 	case WRITE_VERIFY: /* SBC - VRProtect */
10577bfea53bSAndy Grover 	case WRITE_VERIFY_12: /* SBC - VRProtect */
10587bfea53bSAndy Grover 	case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
10597bfea53bSAndy Grover 		break;
10607bfea53bSAndy Grover 	default:
10617bfea53bSAndy Grover 		cdb[1] &= 0x1f; /* clear logical unit number */
10627bfea53bSAndy Grover 		break;
10637bfea53bSAndy Grover 	}
10647bfea53bSAndy Grover 
10657bfea53bSAndy Grover 	/*
10667bfea53bSAndy Grover 	 * For REPORT LUNS we always need to emulate the response, for everything
10677bfea53bSAndy Grover 	 * else, pass it up.
10687bfea53bSAndy Grover 	 */
10697bfea53bSAndy Grover 	if (cdb[0] == REPORT_LUNS) {
10707bfea53bSAndy Grover 		cmd->execute_cmd = spc_emulate_report_luns;
10717bfea53bSAndy Grover 		return TCM_NO_SENSE;
10727bfea53bSAndy Grover 	}
10737bfea53bSAndy Grover 
10747bfea53bSAndy Grover 	/* Set DATA_CDB flag for ops that should have it */
10757bfea53bSAndy Grover 	switch (cdb[0]) {
10767bfea53bSAndy Grover 	case READ_6:
10777bfea53bSAndy Grover 	case READ_10:
10787bfea53bSAndy Grover 	case READ_12:
10797bfea53bSAndy Grover 	case READ_16:
10807bfea53bSAndy Grover 	case WRITE_6:
10817bfea53bSAndy Grover 	case WRITE_10:
10827bfea53bSAndy Grover 	case WRITE_12:
10837bfea53bSAndy Grover 	case WRITE_16:
10847bfea53bSAndy Grover 	case WRITE_VERIFY:
10857bfea53bSAndy Grover 	case WRITE_VERIFY_12:
10867bfea53bSAndy Grover 	case 0x8e: /* WRITE_VERIFY_16 */
10877bfea53bSAndy Grover 	case COMPARE_AND_WRITE:
10887bfea53bSAndy Grover 	case XDWRITEREAD_10:
10897bfea53bSAndy Grover 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
10907bfea53bSAndy Grover 		break;
10917bfea53bSAndy Grover 	case VARIABLE_LENGTH_CMD:
10927bfea53bSAndy Grover 		switch (get_unaligned_be16(&cdb[8])) {
10937bfea53bSAndy Grover 		case READ_32:
10947bfea53bSAndy Grover 		case WRITE_32:
10957bfea53bSAndy Grover 		case 0x0c: /* WRITE_VERIFY_32 */
10967bfea53bSAndy Grover 		case XDWRITEREAD_32:
10977bfea53bSAndy Grover 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
10987bfea53bSAndy Grover 			break;
10997bfea53bSAndy Grover 		}
11007bfea53bSAndy Grover 	}
11017bfea53bSAndy Grover 
11027bfea53bSAndy Grover 	cmd->execute_cmd = exec_cmd;
11037bfea53bSAndy Grover 
11047bfea53bSAndy Grover 	return TCM_NO_SENSE;
11057bfea53bSAndy Grover }
11067bfea53bSAndy Grover EXPORT_SYMBOL(passthrough_parse_cdb);
1107