11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c66ac9dbSNicholas Bellinger /*******************************************************************************
3c66ac9dbSNicholas Bellinger  * Filename:  target_core_device.c (based on iscsi_target_device.c)
4c66ac9dbSNicholas Bellinger  *
5e3d6f909SAndy Grover  * This file contains the TCM Virtual Device and Disk Transport
6c66ac9dbSNicholas Bellinger  * agnostic related functions.
7c66ac9dbSNicholas Bellinger  *
84c76251eSNicholas Bellinger  * (c) Copyright 2003-2013 Datera, Inc.
9c66ac9dbSNicholas Bellinger  *
10c66ac9dbSNicholas Bellinger  * Nicholas A. Bellinger <nab@kernel.org>
11c66ac9dbSNicholas Bellinger  *
12c66ac9dbSNicholas Bellinger  ******************************************************************************/
13c66ac9dbSNicholas Bellinger 
14c66ac9dbSNicholas Bellinger #include <linux/net.h>
15c66ac9dbSNicholas Bellinger #include <linux/string.h>
16c66ac9dbSNicholas Bellinger #include <linux/delay.h>
17c66ac9dbSNicholas Bellinger #include <linux/timer.h>
18c66ac9dbSNicholas Bellinger #include <linux/slab.h>
19c66ac9dbSNicholas Bellinger #include <linux/spinlock.h>
20c66ac9dbSNicholas Bellinger #include <linux/kthread.h>
21c66ac9dbSNicholas Bellinger #include <linux/in.h>
22c53181afSPaul Gortmaker #include <linux/export.h>
238dcf07beSBart Van Assche #include <linux/t10-pi.h>
247bfea53bSAndy Grover #include <asm/unaligned.h>
25c66ac9dbSNicholas Bellinger #include <net/sock.h>
26c66ac9dbSNicholas Bellinger #include <net/tcp.h>
27ba929992SBart Van Assche #include <scsi/scsi_common.h>
28ba929992SBart Van Assche #include <scsi/scsi_proto.h>
29c66ac9dbSNicholas Bellinger 
30c66ac9dbSNicholas Bellinger #include <target/target_core_base.h>
31c4795fb2SChristoph Hellwig #include <target/target_core_backend.h>
32c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
33c66ac9dbSNicholas Bellinger 
34e26d99aeSChristoph Hellwig #include "target_core_internal.h"
35c66ac9dbSNicholas Bellinger #include "target_core_alua.h"
36c66ac9dbSNicholas Bellinger #include "target_core_pr.h"
37c66ac9dbSNicholas Bellinger #include "target_core_ua.h"
38c66ac9dbSNicholas Bellinger 
39c82ff239SColin Ian King static DEFINE_MUTEX(device_mutex);
40c82ff239SColin Ian King static LIST_HEAD(device_list);
410a5eee64SMike Christie static DEFINE_IDR(devices_idr);
42d9ea32bfSNicholas Bellinger 
43e3d6f909SAndy Grover static struct se_hba *lun0_hba;
44e3d6f909SAndy Grover /* not static, needed by tpg.c */
45e3d6f909SAndy Grover struct se_device *g_lun0_dev;
46e3d6f909SAndy Grover 
47de103c93SChristoph Hellwig sense_reason_t
48a36840d8SSudhakar Panneerselvam transport_lookup_cmd_lun(struct se_cmd *se_cmd)
49c66ac9dbSNicholas Bellinger {
50c66ac9dbSNicholas Bellinger 	struct se_lun *se_lun = NULL;
51e3d6f909SAndy Grover 	struct se_session *se_sess = se_cmd->se_sess;
5229a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
5329a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
548fa3a867SNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
55c66ac9dbSNicholas Bellinger 
5629a05deeSNicholas Bellinger 	rcu_read_lock();
57a36840d8SSudhakar Panneerselvam 	deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
5829a05deeSNicholas Bellinger 	if (deve) {
5929a05deeSNicholas Bellinger 		atomic_long_inc(&deve->total_cmds);
60c66ac9dbSNicholas Bellinger 
615951146dSAndy Grover 		if (se_cmd->data_direction == DMA_TO_DEVICE)
6229a05deeSNicholas Bellinger 			atomic_long_add(se_cmd->data_length,
6329a05deeSNicholas Bellinger 					&deve->write_bytes);
645951146dSAndy Grover 		else if (se_cmd->data_direction == DMA_FROM_DEVICE)
6529a05deeSNicholas Bellinger 			atomic_long_add(se_cmd->data_length,
6629a05deeSNicholas Bellinger 					&deve->read_bytes);
675951146dSAndy Grover 
68a2b5d6f9SMike Christie 		if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
69a2b5d6f9SMike Christie 		    deve->lun_access_ro) {
70a2b5d6f9SMike Christie 			pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
71a2b5d6f9SMike Christie 				" Access for 0x%08llx\n",
72a2b5d6f9SMike Christie 				se_cmd->se_tfo->fabric_name,
73a2b5d6f9SMike Christie 				se_cmd->orig_fe_lun);
74a2b5d6f9SMike Christie 			rcu_read_unlock();
75a2b5d6f9SMike Christie 			return TCM_WRITE_PROTECTED;
76a2b5d6f9SMike Christie 		}
77a2b5d6f9SMike Christie 
7829a05deeSNicholas Bellinger 		se_lun = rcu_dereference(deve->se_lun);
79bd4e2d29SNicholas Bellinger 
80bd4e2d29SNicholas Bellinger 		if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
81bd4e2d29SNicholas Bellinger 			se_lun = NULL;
82bd4e2d29SNicholas Bellinger 			goto out_unlock;
83bd4e2d29SNicholas Bellinger 		}
84bd4e2d29SNicholas Bellinger 
8563f74794SBart Van Assche 		se_cmd->se_lun = se_lun;
865951146dSAndy Grover 		se_cmd->pr_res_key = deve->pr_res_key;
875951146dSAndy Grover 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
885277797dSNicholas Bellinger 		se_cmd->lun_ref_active = true;
895951146dSAndy Grover 	}
90bd4e2d29SNicholas Bellinger out_unlock:
9129a05deeSNicholas Bellinger 	rcu_read_unlock();
925951146dSAndy Grover 
935951146dSAndy Grover 	if (!se_lun) {
94c66ac9dbSNicholas Bellinger 		/*
95c66ac9dbSNicholas Bellinger 		 * Use the se_portal_group->tpg_virt_lun0 to allow for
96c66ac9dbSNicholas Bellinger 		 * REPORT_LUNS, et al to be returned when no active
97c66ac9dbSNicholas Bellinger 		 * MappedLUN=0 exists for this Initiator Port.
98c66ac9dbSNicholas Bellinger 		 */
99a36840d8SSudhakar Panneerselvam 		if (se_cmd->orig_fe_lun != 0) {
1006708bb27SAndy Grover 			pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
1015482d56bSLance Digby 				" Access for 0x%08llx from %s\n",
10230c7ca93SDavid Disseldorp 				se_cmd->se_tfo->fabric_name,
103a36840d8SSudhakar Panneerselvam 				se_cmd->orig_fe_lun,
1045482d56bSLance Digby 				nacl->initiatorname);
105de103c93SChristoph Hellwig 			return TCM_NON_EXISTENT_LUN;
106c66ac9dbSNicholas Bellinger 		}
1075951146dSAndy Grover 
1088fa3a867SNicholas Bellinger 		/*
1098fa3a867SNicholas Bellinger 		 * Force WRITE PROTECT for virtual LUN 0
1108fa3a867SNicholas Bellinger 		 */
1118fa3a867SNicholas Bellinger 		if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
112a2b5d6f9SMike Christie 		    (se_cmd->data_direction != DMA_NONE))
113a2b5d6f9SMike Christie 			return TCM_WRITE_PROTECTED;
114a2b5d6f9SMike Christie 
115a2b5d6f9SMike Christie 		se_lun = se_sess->se_tpg->tpg_virt_lun0;
116a2b5d6f9SMike Christie 		if (!percpu_ref_tryget_live(&se_lun->lun_ref))
117a2b5d6f9SMike Christie 			return TCM_NON_EXISTENT_LUN;
118a2b5d6f9SMike Christie 
119a2b5d6f9SMike Christie 		se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
120a2b5d6f9SMike Christie 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
121a2b5d6f9SMike Christie 		se_cmd->lun_ref_active = true;
122c66ac9dbSNicholas Bellinger 	}
1234cc987eaSNicholas Bellinger 	/*
1244cc987eaSNicholas Bellinger 	 * RCU reference protected by percpu se_lun->lun_ref taken above that
1254cc987eaSNicholas Bellinger 	 * must drop to zero (including initial reference) before this se_lun
1264cc987eaSNicholas Bellinger 	 * pointer can be kfree_rcu() by the final se_lun->lun_group put via
1274cc987eaSNicholas Bellinger 	 * target_core_fabric_configfs.c:target_fabric_port_release
1284cc987eaSNicholas Bellinger 	 */
1294cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1304cc987eaSNicholas Bellinger 	atomic_long_inc(&se_cmd->se_dev->num_cmds);
131c66ac9dbSNicholas Bellinger 
132c66ac9dbSNicholas Bellinger 	if (se_cmd->data_direction == DMA_TO_DEVICE)
1334cc987eaSNicholas Bellinger 		atomic_long_add(se_cmd->data_length,
1344cc987eaSNicholas Bellinger 				&se_cmd->se_dev->write_bytes);
135c66ac9dbSNicholas Bellinger 	else if (se_cmd->data_direction == DMA_FROM_DEVICE)
1364cc987eaSNicholas Bellinger 		atomic_long_add(se_cmd->data_length,
1374cc987eaSNicholas Bellinger 				&se_cmd->se_dev->read_bytes);
138c66ac9dbSNicholas Bellinger 
1398fa3a867SNicholas Bellinger 	return ret;
140c66ac9dbSNicholas Bellinger }
1415951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_cmd_lun);
142c66ac9dbSNicholas Bellinger 
143a36840d8SSudhakar Panneerselvam int transport_lookup_tmr_lun(struct se_cmd *se_cmd)
144c66ac9dbSNicholas Bellinger {
145c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
146c66ac9dbSNicholas Bellinger 	struct se_lun *se_lun = NULL;
147e3d6f909SAndy Grover 	struct se_session *se_sess = se_cmd->se_sess;
14829a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
149c66ac9dbSNicholas Bellinger 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1505e1be919SRoland Dreier 	unsigned long flags;
151c66ac9dbSNicholas Bellinger 
15229a05deeSNicholas Bellinger 	rcu_read_lock();
153a36840d8SSudhakar Panneerselvam 	deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
15429a05deeSNicholas Bellinger 	if (deve) {
15529a05deeSNicholas Bellinger 		se_lun = rcu_dereference(deve->se_lun);
156eeb64d23SNicholas Bellinger 
157eeb64d23SNicholas Bellinger 		if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
158eeb64d23SNicholas Bellinger 			se_lun = NULL;
159eeb64d23SNicholas Bellinger 			goto out_unlock;
160eeb64d23SNicholas Bellinger 		}
161eeb64d23SNicholas Bellinger 
16263f74794SBart Van Assche 		se_cmd->se_lun = se_lun;
163c66ac9dbSNicholas Bellinger 		se_cmd->pr_res_key = deve->pr_res_key;
164eeb64d23SNicholas Bellinger 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
165eeb64d23SNicholas Bellinger 		se_cmd->lun_ref_active = true;
166c66ac9dbSNicholas Bellinger 	}
167eeb64d23SNicholas Bellinger out_unlock:
16829a05deeSNicholas Bellinger 	rcu_read_unlock();
169c66ac9dbSNicholas Bellinger 
170c66ac9dbSNicholas Bellinger 	if (!se_lun) {
1716708bb27SAndy Grover 		pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
1725482d56bSLance Digby 			" Access for 0x%08llx for %s\n",
17330c7ca93SDavid Disseldorp 			se_cmd->se_tfo->fabric_name,
174a36840d8SSudhakar Panneerselvam 			se_cmd->orig_fe_lun,
1755482d56bSLance Digby 			nacl->initiatorname);
176e3d6f909SAndy Grover 		return -ENODEV;
177c66ac9dbSNicholas Bellinger 	}
1784cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1794cc987eaSNicholas Bellinger 	se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1805951146dSAndy Grover 
1815e1be919SRoland Dreier 	spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
1825951146dSAndy Grover 	list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
1835e1be919SRoland Dreier 	spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
184c66ac9dbSNicholas Bellinger 
185c66ac9dbSNicholas Bellinger 	return 0;
186c66ac9dbSNicholas Bellinger }
1875951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_tmr_lun);
188c66ac9dbSNicholas Bellinger 
18929a05deeSNicholas Bellinger bool target_lun_is_rdonly(struct se_cmd *cmd)
19029a05deeSNicholas Bellinger {
19129a05deeSNicholas Bellinger 	struct se_session *se_sess = cmd->se_sess;
19229a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
19329a05deeSNicholas Bellinger 	bool ret;
19429a05deeSNicholas Bellinger 
19529a05deeSNicholas Bellinger 	rcu_read_lock();
19629a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
19703a68b44SAndy Grover 	ret = deve && deve->lun_access_ro;
19829a05deeSNicholas Bellinger 	rcu_read_unlock();
19929a05deeSNicholas Bellinger 
20029a05deeSNicholas Bellinger 	return ret;
20129a05deeSNicholas Bellinger }
20229a05deeSNicholas Bellinger EXPORT_SYMBOL(target_lun_is_rdonly);
20329a05deeSNicholas Bellinger 
204c66ac9dbSNicholas Bellinger /*
205c66ac9dbSNicholas Bellinger  * This function is called from core_scsi3_emulate_pro_register_and_move()
20629a05deeSNicholas Bellinger  * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
207c66ac9dbSNicholas Bellinger  * when a matching rtpi is found.
208c66ac9dbSNicholas Bellinger  */
209c66ac9dbSNicholas Bellinger struct se_dev_entry *core_get_se_deve_from_rtpi(
210c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
211c66ac9dbSNicholas Bellinger 	u16 rtpi)
212c66ac9dbSNicholas Bellinger {
213c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
214c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
215c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
216c66ac9dbSNicholas Bellinger 
21729a05deeSNicholas Bellinger 	rcu_read_lock();
21829a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
21929a05deeSNicholas Bellinger 		lun = rcu_dereference(deve->se_lun);
2206708bb27SAndy Grover 		if (!lun) {
2216708bb27SAndy Grover 			pr_err("%s device entries device pointer is"
222c66ac9dbSNicholas Bellinger 				" NULL, but Initiator has access.\n",
22330c7ca93SDavid Disseldorp 				tpg->se_tpg_tfo->fabric_name);
224c66ac9dbSNicholas Bellinger 			continue;
225c66ac9dbSNicholas Bellinger 		}
22629a05deeSNicholas Bellinger 		if (lun->lun_rtpi != rtpi)
227c66ac9dbSNicholas Bellinger 			continue;
228c66ac9dbSNicholas Bellinger 
22929a05deeSNicholas Bellinger 		kref_get(&deve->pr_kref);
23029a05deeSNicholas Bellinger 		rcu_read_unlock();
231c66ac9dbSNicholas Bellinger 
232c66ac9dbSNicholas Bellinger 		return deve;
233c66ac9dbSNicholas Bellinger 	}
23429a05deeSNicholas Bellinger 	rcu_read_unlock();
235c66ac9dbSNicholas Bellinger 
236c66ac9dbSNicholas Bellinger 	return NULL;
237c66ac9dbSNicholas Bellinger }
238c66ac9dbSNicholas Bellinger 
23929a05deeSNicholas Bellinger void core_free_device_list_for_node(
240c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
241c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg)
242c66ac9dbSNicholas Bellinger {
243c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
244c66ac9dbSNicholas Bellinger 
24529a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
24629a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
24729a05deeSNicholas Bellinger 		struct se_lun *lun = rcu_dereference_check(deve->se_lun,
24829a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
24929a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
250c66ac9dbSNicholas Bellinger 	}
25129a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
252c66ac9dbSNicholas Bellinger }
253c66ac9dbSNicholas Bellinger 
254c66ac9dbSNicholas Bellinger void core_update_device_list_access(
255f2d30680SHannes Reinecke 	u64 mapped_lun,
25603a68b44SAndy Grover 	bool lun_access_ro,
257c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
258c66ac9dbSNicholas Bellinger {
259c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
260c66ac9dbSNicholas Bellinger 
26129a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
26229a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, mapped_lun);
26303a68b44SAndy Grover 	if (deve)
26403a68b44SAndy Grover 		deve->lun_access_ro = lun_access_ro;
26529a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
266c66ac9dbSNicholas Bellinger }
267c66ac9dbSNicholas Bellinger 
26829a05deeSNicholas Bellinger /*
26929a05deeSNicholas Bellinger  * Called with rcu_read_lock or nacl->device_list_lock held.
270c66ac9dbSNicholas Bellinger  */
271f2d30680SHannes Reinecke struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
27229a05deeSNicholas Bellinger {
27329a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
27429a05deeSNicholas Bellinger 
27529a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
27629a05deeSNicholas Bellinger 		if (deve->mapped_lun == mapped_lun)
27729a05deeSNicholas Bellinger 			return deve;
27829a05deeSNicholas Bellinger 
27929a05deeSNicholas Bellinger 	return NULL;
28029a05deeSNicholas Bellinger }
28129a05deeSNicholas Bellinger EXPORT_SYMBOL(target_nacl_find_deve);
28229a05deeSNicholas Bellinger 
28329a05deeSNicholas Bellinger void target_pr_kref_release(struct kref *kref)
28429a05deeSNicholas Bellinger {
28529a05deeSNicholas Bellinger 	struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
28629a05deeSNicholas Bellinger 						 pr_kref);
28729a05deeSNicholas Bellinger 	complete(&deve->pr_comp);
288c66ac9dbSNicholas Bellinger }
289c66ac9dbSNicholas Bellinger 
2907c0d0d51SHannes Reinecke static void
2917c0d0d51SHannes Reinecke target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
2927c0d0d51SHannes Reinecke 			     bool skip_new)
2937c0d0d51SHannes Reinecke {
2947c0d0d51SHannes Reinecke 	struct se_dev_entry *tmp;
2957c0d0d51SHannes Reinecke 
2967c0d0d51SHannes Reinecke 	rcu_read_lock();
2977c0d0d51SHannes Reinecke 	hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
2987c0d0d51SHannes Reinecke 		if (skip_new && tmp == new)
2997c0d0d51SHannes Reinecke 			continue;
3007c0d0d51SHannes Reinecke 		core_scsi3_ua_allocate(tmp, 0x3F,
3017c0d0d51SHannes Reinecke 				       ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
3027c0d0d51SHannes Reinecke 	}
3037c0d0d51SHannes Reinecke 	rcu_read_unlock();
3047c0d0d51SHannes Reinecke }
3057c0d0d51SHannes Reinecke 
306e80ac6c4SAndy Grover int core_enable_device_list_for_node(
307c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
308c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl,
309f2d30680SHannes Reinecke 	u64 mapped_lun,
31003a68b44SAndy Grover 	bool lun_access_ro,
311c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
312e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
313c66ac9dbSNicholas Bellinger {
31429a05deeSNicholas Bellinger 	struct se_dev_entry *orig, *new;
315c66ac9dbSNicholas Bellinger 
31629a05deeSNicholas Bellinger 	new = kzalloc(sizeof(*new), GFP_KERNEL);
31729a05deeSNicholas Bellinger 	if (!new) {
31829a05deeSNicholas Bellinger 		pr_err("Unable to allocate se_dev_entry memory\n");
31929a05deeSNicholas Bellinger 		return -ENOMEM;
32029a05deeSNicholas Bellinger 	}
321e80ac6c4SAndy Grover 
32229a05deeSNicholas Bellinger 	spin_lock_init(&new->ua_lock);
32329a05deeSNicholas Bellinger 	INIT_LIST_HEAD(&new->ua_list);
324adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&new->lun_link);
325e80ac6c4SAndy Grover 
32629a05deeSNicholas Bellinger 	new->mapped_lun = mapped_lun;
32729a05deeSNicholas Bellinger 	kref_init(&new->pr_kref);
32829a05deeSNicholas Bellinger 	init_completion(&new->pr_comp);
32929a05deeSNicholas Bellinger 
33003a68b44SAndy Grover 	new->lun_access_ro = lun_access_ro;
33129a05deeSNicholas Bellinger 	new->creation_time = get_jiffies_64();
33229a05deeSNicholas Bellinger 	new->attach_count++;
33329a05deeSNicholas Bellinger 
33429a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
33529a05deeSNicholas Bellinger 	orig = target_nacl_find_deve(nacl, mapped_lun);
33629a05deeSNicholas Bellinger 	if (orig && orig->se_lun) {
33729a05deeSNicholas Bellinger 		struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
33829a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
33929a05deeSNicholas Bellinger 
34029a05deeSNicholas Bellinger 		if (orig_lun != lun) {
34129a05deeSNicholas Bellinger 			pr_err("Existing orig->se_lun doesn't match new lun"
34229a05deeSNicholas Bellinger 			       " for dynamic -> explicit NodeACL conversion:"
34329a05deeSNicholas Bellinger 				" %s\n", nacl->initiatorname);
34429a05deeSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
34529a05deeSNicholas Bellinger 			kfree(new);
346e3d6f909SAndy Grover 			return -EINVAL;
347c66ac9dbSNicholas Bellinger 		}
348391e2a6dSNicholas Bellinger 		if (orig->se_lun_acl != NULL) {
349391e2a6dSNicholas Bellinger 			pr_warn_ratelimited("Detected existing explicit"
350391e2a6dSNicholas Bellinger 				" se_lun_acl->se_lun_group reference for %s"
351391e2a6dSNicholas Bellinger 				" mapped_lun: %llu, failing\n",
352391e2a6dSNicholas Bellinger 				 nacl->initiatorname, mapped_lun);
353391e2a6dSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
354391e2a6dSNicholas Bellinger 			kfree(new);
355391e2a6dSNicholas Bellinger 			return -EINVAL;
356391e2a6dSNicholas Bellinger 		}
357c66ac9dbSNicholas Bellinger 
35829a05deeSNicholas Bellinger 		rcu_assign_pointer(new->se_lun, lun);
35929a05deeSNicholas Bellinger 		rcu_assign_pointer(new->se_lun_acl, lun_acl);
36029a05deeSNicholas Bellinger 		hlist_del_rcu(&orig->link);
36129a05deeSNicholas Bellinger 		hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
36229a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
363c66ac9dbSNicholas Bellinger 
3641adff1b3SNicholas Bellinger 		spin_lock(&lun->lun_deve_lock);
365adf653f9SChristoph Hellwig 		list_del(&orig->lun_link);
366adf653f9SChristoph Hellwig 		list_add_tail(&new->lun_link, &lun->lun_deve_list);
3671adff1b3SNicholas Bellinger 		spin_unlock(&lun->lun_deve_lock);
36829a05deeSNicholas Bellinger 
36929a05deeSNicholas Bellinger 		kref_put(&orig->pr_kref, target_pr_kref_release);
37029a05deeSNicholas Bellinger 		wait_for_completion(&orig->pr_comp);
37129a05deeSNicholas Bellinger 
3727c0d0d51SHannes Reinecke 		target_luns_data_has_changed(nacl, new, true);
37329a05deeSNicholas Bellinger 		kfree_rcu(orig, rcu_head);
374c66ac9dbSNicholas Bellinger 		return 0;
375c66ac9dbSNicholas Bellinger 	}
376e80ac6c4SAndy Grover 
37729a05deeSNicholas Bellinger 	rcu_assign_pointer(new->se_lun, lun);
37829a05deeSNicholas Bellinger 	rcu_assign_pointer(new->se_lun_acl, lun_acl);
37929a05deeSNicholas Bellinger 	hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
38029a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
381c66ac9dbSNicholas Bellinger 
3821adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
383adf653f9SChristoph Hellwig 	list_add_tail(&new->lun_link, &lun->lun_deve_list);
3841adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
385c66ac9dbSNicholas Bellinger 
3867c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, new, true);
387c66ac9dbSNicholas Bellinger 	return 0;
388c66ac9dbSNicholas Bellinger }
389c66ac9dbSNicholas Bellinger 
39029a05deeSNicholas Bellinger void core_disable_device_list_for_node(
391e80ac6c4SAndy Grover 	struct se_lun *lun,
39229a05deeSNicholas Bellinger 	struct se_dev_entry *orig,
393e80ac6c4SAndy Grover 	struct se_node_acl *nacl,
394e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
395e80ac6c4SAndy Grover {
396e80ac6c4SAndy Grover 	/*
3974cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
3984cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
3994cc987eaSNicholas Bellinger 	 */
4004cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
401618baaf7SBart Van Assche 
402618baaf7SBart Van Assche 	lockdep_assert_held(&nacl->lun_entry_mutex);
403618baaf7SBart Van Assche 
404e80ac6c4SAndy Grover 	/*
405e80ac6c4SAndy Grover 	 * If the MappedLUN entry is being disabled, the entry in
406adf653f9SChristoph Hellwig 	 * lun->lun_deve_list must be removed now before clearing the
407e80ac6c4SAndy Grover 	 * struct se_dev_entry pointers below as logic in
408e80ac6c4SAndy Grover 	 * core_alua_do_transition_tg_pt() depends on these being present.
409e80ac6c4SAndy Grover 	 *
410e80ac6c4SAndy Grover 	 * deve->se_lun_acl will be NULL for demo-mode created LUNs
411e80ac6c4SAndy Grover 	 * that have not been explicitly converted to MappedLUNs ->
412adf653f9SChristoph Hellwig 	 * struct se_lun_acl, but we remove deve->lun_link from
413adf653f9SChristoph Hellwig 	 * lun->lun_deve_list. This also means that active UAs and
414e80ac6c4SAndy Grover 	 * NodeACL context specific PR metadata for demo-mode
415e80ac6c4SAndy Grover 	 * MappedLUN *deve will be released below..
416e80ac6c4SAndy Grover 	 */
4171adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
418adf653f9SChristoph Hellwig 	list_del(&orig->lun_link);
4191adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
420c66ac9dbSNicholas Bellinger 	/*
421c66ac9dbSNicholas Bellinger 	 * Disable struct se_dev_entry LUN ACL mapping
422c66ac9dbSNicholas Bellinger 	 */
42329a05deeSNicholas Bellinger 	core_scsi3_ua_release_all(orig);
424c66ac9dbSNicholas Bellinger 
42529a05deeSNicholas Bellinger 	hlist_del_rcu(&orig->link);
42680bfdfa9SNicholas Bellinger 	clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
42703a68b44SAndy Grover 	orig->lun_access_ro = false;
42829a05deeSNicholas Bellinger 	orig->creation_time = 0;
42929a05deeSNicholas Bellinger 	orig->attach_count--;
43029a05deeSNicholas Bellinger 	/*
43129a05deeSNicholas Bellinger 	 * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
43229a05deeSNicholas Bellinger 	 * or REGISTER_AND_MOVE PR operation to complete.
43329a05deeSNicholas Bellinger 	 */
43429a05deeSNicholas Bellinger 	kref_put(&orig->pr_kref, target_pr_kref_release);
43529a05deeSNicholas Bellinger 	wait_for_completion(&orig->pr_comp);
43629a05deeSNicholas Bellinger 
4373ccd6e83SNicholas Bellinger 	rcu_assign_pointer(orig->se_lun, NULL);
4383ccd6e83SNicholas Bellinger 	rcu_assign_pointer(orig->se_lun_acl, NULL);
4393ccd6e83SNicholas Bellinger 
44029a05deeSNicholas Bellinger 	kfree_rcu(orig, rcu_head);
441c66ac9dbSNicholas Bellinger 
4424cc987eaSNicholas Bellinger 	core_scsi3_free_pr_reg_from_nacl(dev, nacl);
4437c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, NULL, false);
444c66ac9dbSNicholas Bellinger }
445c66ac9dbSNicholas Bellinger 
446c66ac9dbSNicholas Bellinger /*      core_clear_lun_from_tpg():
447c66ac9dbSNicholas Bellinger  *
448c66ac9dbSNicholas Bellinger  *
449c66ac9dbSNicholas Bellinger  */
450c66ac9dbSNicholas Bellinger void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
451c66ac9dbSNicholas Bellinger {
452c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
453c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
454c66ac9dbSNicholas Bellinger 
455403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
456c66ac9dbSNicholas Bellinger 	list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
457c66ac9dbSNicholas Bellinger 
45829a05deeSNicholas Bellinger 		mutex_lock(&nacl->lun_entry_mutex);
45929a05deeSNicholas Bellinger 		hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
46029a05deeSNicholas Bellinger 			struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
46129a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
46229a05deeSNicholas Bellinger 
46329a05deeSNicholas Bellinger 			if (lun != tmp_lun)
464c66ac9dbSNicholas Bellinger 				continue;
465c66ac9dbSNicholas Bellinger 
46629a05deeSNicholas Bellinger 			core_disable_device_list_for_node(lun, deve, nacl, tpg);
467c66ac9dbSNicholas Bellinger 		}
46829a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
469c66ac9dbSNicholas Bellinger 	}
470403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
471c66ac9dbSNicholas Bellinger }
472c66ac9dbSNicholas Bellinger 
473adf653f9SChristoph Hellwig int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
474c66ac9dbSNicholas Bellinger {
475adf653f9SChristoph Hellwig 	struct se_lun *tmp;
476c66ac9dbSNicholas Bellinger 
477c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
478adf653f9SChristoph Hellwig 	if (dev->export_count == 0x0000ffff) {
4796708bb27SAndy Grover 		pr_warn("Reached dev->dev_port_count =="
480c66ac9dbSNicholas Bellinger 				" 0x0000ffff\n");
481c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
482adf653f9SChristoph Hellwig 		return -ENOSPC;
483c66ac9dbSNicholas Bellinger 	}
484c66ac9dbSNicholas Bellinger again:
485c66ac9dbSNicholas Bellinger 	/*
48635d1efe8SMasanari Iida 	 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
487c66ac9dbSNicholas Bellinger 	 * Here is the table from spc4r17 section 7.7.3.8.
488c66ac9dbSNicholas Bellinger 	 *
489c66ac9dbSNicholas Bellinger 	 *    Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
490c66ac9dbSNicholas Bellinger 	 *
491c66ac9dbSNicholas Bellinger 	 * Code      Description
492c66ac9dbSNicholas Bellinger 	 * 0h        Reserved
493c66ac9dbSNicholas Bellinger 	 * 1h        Relative port 1, historically known as port A
494c66ac9dbSNicholas Bellinger 	 * 2h        Relative port 2, historically known as port B
495c66ac9dbSNicholas Bellinger 	 * 3h to FFFFh    Relative port 3 through 65 535
496c66ac9dbSNicholas Bellinger 	 */
497adf653f9SChristoph Hellwig 	lun->lun_rtpi = dev->dev_rpti_counter++;
498adf653f9SChristoph Hellwig 	if (!lun->lun_rtpi)
499c66ac9dbSNicholas Bellinger 		goto again;
500c66ac9dbSNicholas Bellinger 
501adf653f9SChristoph Hellwig 	list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
502c66ac9dbSNicholas Bellinger 		/*
50335d1efe8SMasanari Iida 		 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
504c66ac9dbSNicholas Bellinger 		 * for 16-bit wrap..
505c66ac9dbSNicholas Bellinger 		 */
506adf653f9SChristoph Hellwig 		if (lun->lun_rtpi == tmp->lun_rtpi)
507c66ac9dbSNicholas Bellinger 			goto again;
508c66ac9dbSNicholas Bellinger 	}
509c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
510c66ac9dbSNicholas Bellinger 
511c66ac9dbSNicholas Bellinger 	return 0;
512c66ac9dbSNicholas Bellinger }
513c66ac9dbSNicholas Bellinger 
5140fd97ccfSChristoph Hellwig static void se_release_vpd_for_dev(struct se_device *dev)
515c66ac9dbSNicholas Bellinger {
516c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd, *vpd_tmp;
517c66ac9dbSNicholas Bellinger 
5180fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_wwn.t10_vpd_lock);
519c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(vpd, vpd_tmp,
5200fd97ccfSChristoph Hellwig 			&dev->t10_wwn.t10_vpd_list, vpd_list) {
521c66ac9dbSNicholas Bellinger 		list_del(&vpd->vpd_list);
522c66ac9dbSNicholas Bellinger 		kfree(vpd);
523c66ac9dbSNicholas Bellinger 	}
5240fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_wwn.t10_vpd_lock);
525c66ac9dbSNicholas Bellinger }
526c66ac9dbSNicholas Bellinger 
527c8045372SRoland Dreier static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
528525a48a2SNicholas Bellinger {
5293e03989bSRoland Dreier 	u32 aligned_max_sectors;
5303e03989bSRoland Dreier 	u32 alignment;
531525a48a2SNicholas Bellinger 	/*
532525a48a2SNicholas Bellinger 	 * Limit max_sectors to a PAGE_SIZE aligned value for modern
533525a48a2SNicholas Bellinger 	 * transport_allocate_data_tasks() operation.
534525a48a2SNicholas Bellinger 	 */
5353e03989bSRoland Dreier 	alignment = max(1ul, PAGE_SIZE / block_size);
5363e03989bSRoland Dreier 	aligned_max_sectors = rounddown(max_sectors, alignment);
537525a48a2SNicholas Bellinger 
5383e03989bSRoland Dreier 	if (max_sectors != aligned_max_sectors)
5393e03989bSRoland Dreier 		pr_info("Rounding down aligned max_sectors from %u to %u\n",
5403e03989bSRoland Dreier 			max_sectors, aligned_max_sectors);
5413e03989bSRoland Dreier 
5423e03989bSRoland Dreier 	return aligned_max_sectors;
543525a48a2SNicholas Bellinger }
544525a48a2SNicholas Bellinger 
5456bb82612SNicholas Bellinger int core_dev_add_lun(
546c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
547c66ac9dbSNicholas Bellinger 	struct se_device *dev,
5486bb82612SNicholas Bellinger 	struct se_lun *lun)
549c66ac9dbSNicholas Bellinger {
5508d9efe53SSebastian Andrzej Siewior 	int rc;
551c66ac9dbSNicholas Bellinger 
55203a68b44SAndy Grover 	rc = core_tpg_add_lun(tpg, lun, false, dev);
5538d9efe53SSebastian Andrzej Siewior 	if (rc < 0)
5546bb82612SNicholas Bellinger 		return rc;
555c66ac9dbSNicholas Bellinger 
556f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
55730c7ca93SDavid Disseldorp 		" CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
5582af7973aSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
55930c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
560c66ac9dbSNicholas Bellinger 	/*
561c66ac9dbSNicholas Bellinger 	 * Update LUN maps for dynamically added initiators when
562c66ac9dbSNicholas Bellinger 	 * generate_node_acl is enabled.
563c66ac9dbSNicholas Bellinger 	 */
564e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
565c66ac9dbSNicholas Bellinger 		struct se_node_acl *acl;
566403edd78SNicholas Bellinger 
567403edd78SNicholas Bellinger 		mutex_lock(&tpg->acl_node_mutex);
568c66ac9dbSNicholas Bellinger 		list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
569052605c6SNicholas Bellinger 			if (acl->dynamic_node_acl &&
570052605c6SNicholas Bellinger 			    (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
571052605c6SNicholas Bellinger 			     !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
572df9766caSNicholas Bellinger 				core_tpg_add_node_to_devs(acl, tpg, lun);
573c66ac9dbSNicholas Bellinger 			}
574c66ac9dbSNicholas Bellinger 		}
575403edd78SNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
576c66ac9dbSNicholas Bellinger 	}
577c66ac9dbSNicholas Bellinger 
5786bb82612SNicholas Bellinger 	return 0;
579c66ac9dbSNicholas Bellinger }
580c66ac9dbSNicholas Bellinger 
581c66ac9dbSNicholas Bellinger /*      core_dev_del_lun():
582c66ac9dbSNicholas Bellinger  *
583c66ac9dbSNicholas Bellinger  *
584c66ac9dbSNicholas Bellinger  */
585cd9d7cbaSAndy Grover void core_dev_del_lun(
586c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
587cd9d7cbaSAndy Grover 	struct se_lun *lun)
588c66ac9dbSNicholas Bellinger {
589f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
59030c7ca93SDavid Disseldorp 		" device object\n", tpg->se_tpg_tfo->fabric_name,
591cd9d7cbaSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
59230c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name);
593c66ac9dbSNicholas Bellinger 
594cd9d7cbaSAndy Grover 	core_tpg_remove_lun(tpg, lun);
595c66ac9dbSNicholas Bellinger }
596c66ac9dbSNicholas Bellinger 
597c66ac9dbSNicholas Bellinger struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
598c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
599fcf29481SNicholas Bellinger 	struct se_node_acl *nacl,
600f2d30680SHannes Reinecke 	u64 mapped_lun,
601c66ac9dbSNicholas Bellinger 	int *ret)
602c66ac9dbSNicholas Bellinger {
603c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl;
604c66ac9dbSNicholas Bellinger 
605fcf29481SNicholas Bellinger 	if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
6066708bb27SAndy Grover 		pr_err("%s InitiatorName exceeds maximum size.\n",
60730c7ca93SDavid Disseldorp 			tpg->se_tpg_tfo->fabric_name);
608c66ac9dbSNicholas Bellinger 		*ret = -EOVERFLOW;
609c66ac9dbSNicholas Bellinger 		return NULL;
610c66ac9dbSNicholas Bellinger 	}
611c66ac9dbSNicholas Bellinger 	lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
6126708bb27SAndy Grover 	if (!lacl) {
6136708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct se_lun_acl.\n");
614c66ac9dbSNicholas Bellinger 		*ret = -ENOMEM;
615c66ac9dbSNicholas Bellinger 		return NULL;
616c66ac9dbSNicholas Bellinger 	}
617c66ac9dbSNicholas Bellinger 
618c66ac9dbSNicholas Bellinger 	lacl->mapped_lun = mapped_lun;
619c66ac9dbSNicholas Bellinger 	lacl->se_lun_nacl = nacl;
620c66ac9dbSNicholas Bellinger 
621c66ac9dbSNicholas Bellinger 	return lacl;
622c66ac9dbSNicholas Bellinger }
623c66ac9dbSNicholas Bellinger 
624c66ac9dbSNicholas Bellinger int core_dev_add_initiator_node_lun_acl(
625c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
626c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl,
6276bb82612SNicholas Bellinger 	struct se_lun *lun,
62803a68b44SAndy Grover 	bool lun_access_ro)
629c66ac9dbSNicholas Bellinger {
6306bb82612SNicholas Bellinger 	struct se_node_acl *nacl = lacl->se_lun_nacl;
6314cc987eaSNicholas Bellinger 	/*
6324cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
6334cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
6344cc987eaSNicholas Bellinger 	 */
6354cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
636c66ac9dbSNicholas Bellinger 
6376708bb27SAndy Grover 	if (!nacl)
638c66ac9dbSNicholas Bellinger 		return -EINVAL;
639c66ac9dbSNicholas Bellinger 
64003a68b44SAndy Grover 	if (lun->lun_access_ro)
64103a68b44SAndy Grover 		lun_access_ro = true;
642c66ac9dbSNicholas Bellinger 
643c66ac9dbSNicholas Bellinger 	lacl->se_lun = lun;
644c66ac9dbSNicholas Bellinger 
645e80ac6c4SAndy Grover 	if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
64603a68b44SAndy Grover 			lun_access_ro, nacl, tpg) < 0)
647c66ac9dbSNicholas Bellinger 		return -EINVAL;
648c66ac9dbSNicholas Bellinger 
649f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
65030c7ca93SDavid Disseldorp 		" InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
6516bb82612SNicholas Bellinger 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
65203a68b44SAndy Grover 		lun_access_ro ? "RO" : "RW",
653b6a54b8dSChris Zankel 		nacl->initiatorname);
654c66ac9dbSNicholas Bellinger 	/*
655c66ac9dbSNicholas Bellinger 	 * Check to see if there are any existing persistent reservation APTPL
656c66ac9dbSNicholas Bellinger 	 * pre-registrations that need to be enabled for this LUN ACL..
657c66ac9dbSNicholas Bellinger 	 */
6584cc987eaSNicholas Bellinger 	core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
659e2480563SNicholas Bellinger 					    lacl->mapped_lun);
660c66ac9dbSNicholas Bellinger 	return 0;
661c66ac9dbSNicholas Bellinger }
662c66ac9dbSNicholas Bellinger 
663c66ac9dbSNicholas Bellinger int core_dev_del_initiator_node_lun_acl(
664c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
665c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
666c66ac9dbSNicholas Bellinger {
667adf653f9SChristoph Hellwig 	struct se_portal_group *tpg = lun->lun_tpg;
668c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
66929a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
670c66ac9dbSNicholas Bellinger 
671c66ac9dbSNicholas Bellinger 	nacl = lacl->se_lun_nacl;
6726708bb27SAndy Grover 	if (!nacl)
673c66ac9dbSNicholas Bellinger 		return -EINVAL;
674c66ac9dbSNicholas Bellinger 
67529a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
67629a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
67729a05deeSNicholas Bellinger 	if (deve)
67829a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
67929a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
680c66ac9dbSNicholas Bellinger 
681f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
682f2d30680SHannes Reinecke 		" InitiatorNode: %s Mapped LUN: %llu\n",
68330c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
684e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
685b6a54b8dSChris Zankel 		nacl->initiatorname, lacl->mapped_lun);
686c66ac9dbSNicholas Bellinger 
687c66ac9dbSNicholas Bellinger 	return 0;
688c66ac9dbSNicholas Bellinger }
689c66ac9dbSNicholas Bellinger 
690c66ac9dbSNicholas Bellinger void core_dev_free_initiator_node_lun_acl(
691c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
692c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
693c66ac9dbSNicholas Bellinger {
6946708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
69530c7ca93SDavid Disseldorp 		" Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
696e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
69730c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
698b6a54b8dSChris Zankel 		lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
699c66ac9dbSNicholas Bellinger 
700c66ac9dbSNicholas Bellinger 	kfree(lacl);
701c66ac9dbSNicholas Bellinger }
702c66ac9dbSNicholas Bellinger 
7030fd97ccfSChristoph Hellwig static void scsi_dump_inquiry(struct se_device *dev)
7040fd97ccfSChristoph Hellwig {
7050fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
706b2da4abfSDavid Disseldorp 	int device_type = dev->transport->get_device_type(dev);
707b2da4abfSDavid Disseldorp 
7080fd97ccfSChristoph Hellwig 	/*
7090fd97ccfSChristoph Hellwig 	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
7100fd97ccfSChristoph Hellwig 	 */
711b2da4abfSDavid Disseldorp 	pr_debug("  Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
712b2da4abfSDavid Disseldorp 		wwn->vendor);
713b2da4abfSDavid Disseldorp 	pr_debug("  Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
714b2da4abfSDavid Disseldorp 		wwn->model);
715b2da4abfSDavid Disseldorp 	pr_debug("  Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
716b2da4abfSDavid Disseldorp 		wwn->revision);
7170fd97ccfSChristoph Hellwig 	pr_debug("  Type:   %s ", scsi_device_type(device_type));
7180fd97ccfSChristoph Hellwig }
7190fd97ccfSChristoph Hellwig 
7200fd97ccfSChristoph Hellwig struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
7210fd97ccfSChristoph Hellwig {
7220fd97ccfSChristoph Hellwig 	struct se_device *dev;
7234863e525SNicholas Bellinger 	struct se_lun *xcopy_lun;
7241526d9f1SMike Christie 	int i;
7250fd97ccfSChristoph Hellwig 
7260a06d430SChristoph Hellwig 	dev = hba->backend->ops->alloc_device(hba, name);
7270fd97ccfSChristoph Hellwig 	if (!dev)
7280fd97ccfSChristoph Hellwig 		return NULL;
7290fd97ccfSChristoph Hellwig 
7301526d9f1SMike Christie 	dev->queues = kcalloc(nr_cpu_ids, sizeof(*dev->queues), GFP_KERNEL);
7311526d9f1SMike Christie 	if (!dev->queues) {
7321526d9f1SMike Christie 		dev->transport->free_device(dev);
7331526d9f1SMike Christie 		return NULL;
7341526d9f1SMike Christie 	}
7351526d9f1SMike Christie 
7361526d9f1SMike Christie 	dev->queue_cnt = nr_cpu_ids;
7371526d9f1SMike Christie 	for (i = 0; i < dev->queue_cnt; i++) {
738eb44ce8cSMike Christie 		struct se_device_queue *q;
739eb44ce8cSMike Christie 
740eb44ce8cSMike Christie 		q = &dev->queues[i];
741eb44ce8cSMike Christie 		INIT_LIST_HEAD(&q->state_list);
742eb44ce8cSMike Christie 		spin_lock_init(&q->lock);
743eb44ce8cSMike Christie 
744eb44ce8cSMike Christie 		init_llist_head(&q->sq.cmd_list);
745eb44ce8cSMike Christie 		INIT_WORK(&q->sq.work, target_queued_submit_work);
7461526d9f1SMike Christie 	}
7471526d9f1SMike Christie 
7480fd97ccfSChristoph Hellwig 	dev->se_hba = hba;
7490a06d430SChristoph Hellwig 	dev->transport = hba->backend->ops;
75069088a04SBodo Stroesser 	dev->transport_flags = dev->transport->transport_flags_default;
751fe052a18SSagi Grimberg 	dev->prot_length = sizeof(struct t10_pi_tuple);
7524cc987eaSNicholas Bellinger 	dev->hba_index = hba->hba_index;
7530fd97ccfSChristoph Hellwig 
7540fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_sep_list);
7550fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_tmr_list);
7560fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->delayed_cmd_list);
7570fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->qf_cmd_list);
7580fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->delayed_cmd_lock);
7590fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->dev_reservation_lock);
7600fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_port_lock);
7610fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_tmr_lock);
7620fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->qf_cmd_lock);
76368ff9b9bSNicholas Bellinger 	sema_init(&dev->caw_sem, 1);
7640fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
7650fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
7660fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.registration_list);
7670fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
7680fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.registration_lock);
7690fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
7700fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
7710fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
772c66094bfSHannes Reinecke 	INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
773c66094bfSHannes Reinecke 	spin_lock_init(&dev->t10_alua.lba_map_lock);
7740fd97ccfSChristoph Hellwig 
775ed1227e0SMike Christie 	INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
776ed1227e0SMike Christie 
7770fd97ccfSChristoph Hellwig 	dev->t10_wwn.t10_dev = dev;
7782469f1e0SSergey Samoylenko 	/*
7792469f1e0SSergey Samoylenko 	 * Use OpenFabrics IEEE Company ID: 00 14 05
7802469f1e0SSergey Samoylenko 	 */
7812469f1e0SSergey Samoylenko 	dev->t10_wwn.company_id = 0x001405;
7822469f1e0SSergey Samoylenko 
7830fd97ccfSChristoph Hellwig 	dev->t10_alua.t10_dev = dev;
7840fd97ccfSChristoph Hellwig 
7850fd97ccfSChristoph Hellwig 	dev->dev_attrib.da_dev = dev;
786adfa9570STregaron Bayly 	dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
787814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_dpo = 1;
788814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_write = 1;
789814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_read = 1;
7900fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
7911bf630fdSDavid Disseldorp 	dev->dev_attrib.emulate_ua_intlck_ctrl = TARGET_UA_INTLCK_CTRL_CLEAR;
7920fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
7930fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
7940fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
7950123a9ecSNicholas Bellinger 	dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
796d397a445SNicholas Bellinger 	dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
797b49d6f78SDavid Disseldorp 	dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
7982ed22c9cSNicholas Bellinger 	dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
7990fd97ccfSChristoph Hellwig 	dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
80092404e60SNicholas Bellinger 	dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
8010fd97ccfSChristoph Hellwig 	dev->dev_attrib.is_nonrot = DA_IS_NONROT;
8020fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
8030fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
8040fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_block_desc_count =
8050fd97ccfSChristoph Hellwig 		DA_MAX_UNMAP_BLOCK_DESC_COUNT;
8060fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
8070fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity_alignment =
8080fd97ccfSChristoph Hellwig 				DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
809e6f41633SJamie Pocas 	dev->dev_attrib.unmap_zeroes_data =
810e6f41633SJamie Pocas 				DA_UNMAP_ZEROES_DATA_DEFAULT;
811773cbaf7SNicholas Bellinger 	dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
8120fd97ccfSChristoph Hellwig 
8134863e525SNicholas Bellinger 	xcopy_lun = &dev->xcopy_lun;
8144cc987eaSNicholas Bellinger 	rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
815bd4e2d29SNicholas Bellinger 	init_completion(&xcopy_lun->lun_shutdown_comp);
816adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
817adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
818adf653f9SChristoph Hellwig 	mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
819adf653f9SChristoph Hellwig 	xcopy_lun->lun_tpg = &xcopy_pt_tpg;
8204863e525SNicholas Bellinger 
8213beeabd5SDavid Disseldorp 	/* Preload the default INQUIRY const values */
8223beeabd5SDavid Disseldorp 	strlcpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
8233beeabd5SDavid Disseldorp 	strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
8243beeabd5SDavid Disseldorp 		sizeof(dev->t10_wwn.model));
8253beeabd5SDavid Disseldorp 	strlcpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
8263beeabd5SDavid Disseldorp 		sizeof(dev->t10_wwn.revision));
8273beeabd5SDavid Disseldorp 
8280fd97ccfSChristoph Hellwig 	return dev;
8290fd97ccfSChristoph Hellwig }
8300fd97ccfSChristoph Hellwig 
8318a9ebe71SMike Christie /*
832cf0fbf89SChristoph Hellwig  * Check if the underlying struct block_device supports discard and if yes
833cf0fbf89SChristoph Hellwig  * configure the UNMAP parameters.
8348a9ebe71SMike Christie  */
8358a9ebe71SMike Christie bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
836817e8b51SChristoph Hellwig 				       struct block_device *bdev)
8378a9ebe71SMike Christie {
838817e8b51SChristoph Hellwig 	int block_size = bdev_logical_block_size(bdev);
839ea263c7fSMike Christie 
84070200574SChristoph Hellwig 	if (!bdev_max_discard_sectors(bdev))
8418a9ebe71SMike Christie 		return false;
8428a9ebe71SMike Christie 
843ea263c7fSMike Christie 	attrib->max_unmap_lba_count =
844cf0fbf89SChristoph Hellwig 		bdev_max_discard_sectors(bdev) >> (ilog2(block_size) - 9);
8458a9ebe71SMike Christie 	/*
8468a9ebe71SMike Christie 	 * Currently hardcoded to 1 in Linux/SCSI code..
8478a9ebe71SMike Christie 	 */
8488a9ebe71SMike Christie 	attrib->max_unmap_block_desc_count = 1;
8497b47ef52SChristoph Hellwig 	attrib->unmap_granularity = bdev_discard_granularity(bdev) / block_size;
850968786b9SChristoph Hellwig 	attrib->unmap_granularity_alignment =
851968786b9SChristoph Hellwig 		bdev_discard_alignment(bdev) / block_size;
8528a9ebe71SMike Christie 	return true;
8538a9ebe71SMike Christie }
8548a9ebe71SMike Christie EXPORT_SYMBOL(target_configure_unmap_from_queue);
8558a9ebe71SMike Christie 
8568a9ebe71SMike Christie /*
8578a9ebe71SMike Christie  * Convert from blocksize advertised to the initiator to the 512 byte
8588a9ebe71SMike Christie  * units unconditionally used by the Linux block layer.
8598a9ebe71SMike Christie  */
8608a9ebe71SMike Christie sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
8618a9ebe71SMike Christie {
8628a9ebe71SMike Christie 	switch (dev->dev_attrib.block_size) {
8638a9ebe71SMike Christie 	case 4096:
8648a9ebe71SMike Christie 		return lb << 3;
8658a9ebe71SMike Christie 	case 2048:
8668a9ebe71SMike Christie 		return lb << 2;
8678a9ebe71SMike Christie 	case 1024:
8688a9ebe71SMike Christie 		return lb << 1;
8698a9ebe71SMike Christie 	default:
8708a9ebe71SMike Christie 		return lb;
8718a9ebe71SMike Christie 	}
8728a9ebe71SMike Christie }
8738a9ebe71SMike Christie EXPORT_SYMBOL(target_to_linux_sector);
8748a9ebe71SMike Christie 
875b1943fd4SMike Christie struct devices_idr_iter {
87636d4cb46SBart Van Assche 	struct config_item *prev_item;
877b1943fd4SMike Christie 	int (*fn)(struct se_device *dev, void *data);
878b1943fd4SMike Christie 	void *data;
879b1943fd4SMike Christie };
880b1943fd4SMike Christie 
881b1943fd4SMike Christie static int target_devices_idr_iter(int id, void *p, void *data)
88236d4cb46SBart Van Assche 	 __must_hold(&device_mutex)
883b1943fd4SMike Christie {
884b1943fd4SMike Christie 	struct devices_idr_iter *iter = data;
885b1943fd4SMike Christie 	struct se_device *dev = p;
88636d4cb46SBart Van Assche 	int ret;
88736d4cb46SBart Van Assche 
88836d4cb46SBart Van Assche 	config_item_put(iter->prev_item);
88936d4cb46SBart Van Assche 	iter->prev_item = NULL;
890b1943fd4SMike Christie 
891b1943fd4SMike Christie 	/*
892b1943fd4SMike Christie 	 * We add the device early to the idr, so it can be used
893b1943fd4SMike Christie 	 * by backend modules during configuration. We do not want
894b1943fd4SMike Christie 	 * to allow other callers to access partially setup devices,
895b1943fd4SMike Christie 	 * so we skip them here.
896b1943fd4SMike Christie 	 */
897cb0f32e1SMike Christie 	if (!target_dev_configured(dev))
898b1943fd4SMike Christie 		return 0;
899b1943fd4SMike Christie 
90036d4cb46SBart Van Assche 	iter->prev_item = config_item_get_unless_zero(&dev->dev_group.cg_item);
90136d4cb46SBart Van Assche 	if (!iter->prev_item)
90236d4cb46SBart Van Assche 		return 0;
90336d4cb46SBart Van Assche 	mutex_unlock(&device_mutex);
90436d4cb46SBart Van Assche 
90536d4cb46SBart Van Assche 	ret = iter->fn(dev, iter->data);
90636d4cb46SBart Van Assche 
90736d4cb46SBart Van Assche 	mutex_lock(&device_mutex);
90836d4cb46SBart Van Assche 	return ret;
909b1943fd4SMike Christie }
910b1943fd4SMike Christie 
911b1943fd4SMike Christie /**
912b1943fd4SMike Christie  * target_for_each_device - iterate over configured devices
913b1943fd4SMike Christie  * @fn: iterator function
914b1943fd4SMike Christie  * @data: pointer to data that will be passed to fn
915b1943fd4SMike Christie  *
916b1943fd4SMike Christie  * fn must return 0 to continue looping over devices. non-zero will break
917b1943fd4SMike Christie  * from the loop and return that value to the caller.
918b1943fd4SMike Christie  */
919b1943fd4SMike Christie int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
920b1943fd4SMike Christie 			   void *data)
921b1943fd4SMike Christie {
92236d4cb46SBart Van Assche 	struct devices_idr_iter iter = { .fn = fn, .data = data };
923b1943fd4SMike Christie 	int ret;
924b1943fd4SMike Christie 
925be50f538SMike Christie 	mutex_lock(&device_mutex);
926b1943fd4SMike Christie 	ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
927be50f538SMike Christie 	mutex_unlock(&device_mutex);
92836d4cb46SBart Van Assche 	config_item_put(iter.prev_item);
929b1943fd4SMike Christie 	return ret;
930b1943fd4SMike Christie }
931b1943fd4SMike Christie 
9320fd97ccfSChristoph Hellwig int target_configure_device(struct se_device *dev)
9330fd97ccfSChristoph Hellwig {
9340fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
9350a5eee64SMike Christie 	int ret, id;
9360fd97ccfSChristoph Hellwig 
937cb0f32e1SMike Christie 	if (target_dev_configured(dev)) {
9380fd97ccfSChristoph Hellwig 		pr_err("se_dev->se_dev_ptr already set for storage"
9390fd97ccfSChristoph Hellwig 				" object\n");
9400fd97ccfSChristoph Hellwig 		return -EEXIST;
9410fd97ccfSChristoph Hellwig 	}
9420fd97ccfSChristoph Hellwig 
9430a5eee64SMike Christie 	/*
9440a5eee64SMike Christie 	 * Add early so modules like tcmu can use during its
9450a5eee64SMike Christie 	 * configuration.
9460a5eee64SMike Christie 	 */
947be50f538SMike Christie 	mutex_lock(&device_mutex);
9480a5eee64SMike Christie 	/*
9490a5eee64SMike Christie 	 * Use cyclic to try and avoid collisions with devices
9500a5eee64SMike Christie 	 * that were recently removed.
9510a5eee64SMike Christie 	 */
9520a5eee64SMike Christie 	id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
953be50f538SMike Christie 	mutex_unlock(&device_mutex);
9540a5eee64SMike Christie 	if (id < 0) {
9550a5eee64SMike Christie 		ret = -ENOMEM;
9560a5eee64SMike Christie 		goto out;
9570a5eee64SMike Christie 	}
9580a5eee64SMike Christie 	dev->dev_index = id;
9590a5eee64SMike Christie 
9600fd97ccfSChristoph Hellwig 	ret = dev->transport->configure_device(dev);
9610fd97ccfSChristoph Hellwig 	if (ret)
9620a5eee64SMike Christie 		goto out_free_index;
963*6b206a5aSMike Christie 
964*6b206a5aSMike Christie 	if (dev->transport->configure_unmap &&
965*6b206a5aSMike Christie 	    dev->transport->configure_unmap(dev)) {
966*6b206a5aSMike Christie 		pr_debug("Discard support available, but disabled by default.\n");
967*6b206a5aSMike Christie 	}
968*6b206a5aSMike Christie 
9690fd97ccfSChristoph Hellwig 	/*
9700fd97ccfSChristoph Hellwig 	 * XXX: there is not much point to have two different values here..
9710fd97ccfSChristoph Hellwig 	 */
9720fd97ccfSChristoph Hellwig 	dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
9730fd97ccfSChristoph Hellwig 	dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
9740fd97ccfSChristoph Hellwig 
9750fd97ccfSChristoph Hellwig 	/*
9760fd97ccfSChristoph Hellwig 	 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
9770fd97ccfSChristoph Hellwig 	 */
9780fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_max_sectors =
9790fd97ccfSChristoph Hellwig 		se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
9800fd97ccfSChristoph Hellwig 					 dev->dev_attrib.hw_block_size);
981046ba642SNicholas Bellinger 	dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
9820fd97ccfSChristoph Hellwig 
9830fd97ccfSChristoph Hellwig 	dev->creation_time = get_jiffies_64();
9840fd97ccfSChristoph Hellwig 
9850fd97ccfSChristoph Hellwig 	ret = core_setup_alua(dev);
9860fd97ccfSChristoph Hellwig 	if (ret)
987c82b59e7Stangwenji 		goto out_destroy_device;
9880fd97ccfSChristoph Hellwig 
9890fd97ccfSChristoph Hellwig 	/*
9900fd97ccfSChristoph Hellwig 	 * Setup work_queue for QUEUE_FULL
9910fd97ccfSChristoph Hellwig 	 */
9920fd97ccfSChristoph Hellwig 	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
9930fd97ccfSChristoph Hellwig 
9940fd97ccfSChristoph Hellwig 	scsi_dump_inquiry(dev);
9950fd97ccfSChristoph Hellwig 
9960fd97ccfSChristoph Hellwig 	spin_lock(&hba->device_lock);
9970fd97ccfSChristoph Hellwig 	hba->dev_count++;
9980fd97ccfSChristoph Hellwig 	spin_unlock(&hba->device_lock);
999d9ea32bfSNicholas Bellinger 
10005f7da044SNicholas Bellinger 	dev->dev_flags |= DF_CONFIGURED;
10015f7da044SNicholas Bellinger 
10020fd97ccfSChristoph Hellwig 	return 0;
10030fd97ccfSChristoph Hellwig 
1004c82b59e7Stangwenji out_destroy_device:
1005c82b59e7Stangwenji 	dev->transport->destroy_device(dev);
10060a5eee64SMike Christie out_free_index:
1007be50f538SMike Christie 	mutex_lock(&device_mutex);
10080a5eee64SMike Christie 	idr_remove(&devices_idr, dev->dev_index);
1009be50f538SMike Christie 	mutex_unlock(&device_mutex);
10100fd97ccfSChristoph Hellwig out:
10110fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
10120fd97ccfSChristoph Hellwig 	return ret;
10130fd97ccfSChristoph Hellwig }
10140fd97ccfSChristoph Hellwig 
10150fd97ccfSChristoph Hellwig void target_free_device(struct se_device *dev)
10160fd97ccfSChristoph Hellwig {
10170fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
10180fd97ccfSChristoph Hellwig 
10190fd97ccfSChristoph Hellwig 	WARN_ON(!list_empty(&dev->dev_sep_list));
10200fd97ccfSChristoph Hellwig 
1021cb0f32e1SMike Christie 	if (target_dev_configured(dev)) {
102292634706SMike Christie 		dev->transport->destroy_device(dev);
102392634706SMike Christie 
1024be50f538SMike Christie 		mutex_lock(&device_mutex);
10250a5eee64SMike Christie 		idr_remove(&devices_idr, dev->dev_index);
1026be50f538SMike Christie 		mutex_unlock(&device_mutex);
1027d9ea32bfSNicholas Bellinger 
10280fd97ccfSChristoph Hellwig 		spin_lock(&hba->device_lock);
10290fd97ccfSChristoph Hellwig 		hba->dev_count--;
10300fd97ccfSChristoph Hellwig 		spin_unlock(&hba->device_lock);
10310fd97ccfSChristoph Hellwig 	}
10320fd97ccfSChristoph Hellwig 
10330fd97ccfSChristoph Hellwig 	core_alua_free_lu_gp_mem(dev);
1034229d4f11SHannes Reinecke 	core_alua_set_lba_map(dev, NULL, 0, 0);
10350fd97ccfSChristoph Hellwig 	core_scsi3_free_all_registrations(dev);
10360fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
10370fd97ccfSChristoph Hellwig 
10382ed22c9cSNicholas Bellinger 	if (dev->transport->free_prot)
10392ed22c9cSNicholas Bellinger 		dev->transport->free_prot(dev);
10402ed22c9cSNicholas Bellinger 
10411526d9f1SMike Christie 	kfree(dev->queues);
10420fd97ccfSChristoph Hellwig 	dev->transport->free_device(dev);
10430fd97ccfSChristoph Hellwig }
10440fd97ccfSChristoph Hellwig 
1045c66ac9dbSNicholas Bellinger int core_dev_setup_virtual_lun0(void)
1046c66ac9dbSNicholas Bellinger {
1047c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
1048c66ac9dbSNicholas Bellinger 	struct se_device *dev;
10491b5ad814SKonstantin Shelekhin 	char buf[] = "rd_pages=8,rd_nullio=1,rd_dummy=1";
1050c66ac9dbSNicholas Bellinger 	int ret;
1051c66ac9dbSNicholas Bellinger 
10526708bb27SAndy Grover 	hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
1053c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
1054c66ac9dbSNicholas Bellinger 		return PTR_ERR(hba);
1055c66ac9dbSNicholas Bellinger 
10560fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, "virt_lun0");
10570fd97ccfSChristoph Hellwig 	if (!dev) {
1058c66ac9dbSNicholas Bellinger 		ret = -ENOMEM;
10590fd97ccfSChristoph Hellwig 		goto out_free_hba;
1060c66ac9dbSNicholas Bellinger 	}
1061c66ac9dbSNicholas Bellinger 
10620a06d430SChristoph Hellwig 	hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
1063c66ac9dbSNicholas Bellinger 
10640fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
10650fd97ccfSChristoph Hellwig 	if (ret)
10660fd97ccfSChristoph Hellwig 		goto out_free_se_dev;
10670fd97ccfSChristoph Hellwig 
10680fd97ccfSChristoph Hellwig 	lun0_hba = hba;
1069e3d6f909SAndy Grover 	g_lun0_dev = dev;
1070c66ac9dbSNicholas Bellinger 	return 0;
10710fd97ccfSChristoph Hellwig 
10720fd97ccfSChristoph Hellwig out_free_se_dev:
10730fd97ccfSChristoph Hellwig 	target_free_device(dev);
10740fd97ccfSChristoph Hellwig out_free_hba:
10750fd97ccfSChristoph Hellwig 	core_delete_hba(hba);
1076c66ac9dbSNicholas Bellinger 	return ret;
1077c66ac9dbSNicholas Bellinger }
1078c66ac9dbSNicholas Bellinger 
1079c66ac9dbSNicholas Bellinger 
1080c66ac9dbSNicholas Bellinger void core_dev_release_virtual_lun0(void)
1081c66ac9dbSNicholas Bellinger {
1082e3d6f909SAndy Grover 	struct se_hba *hba = lun0_hba;
1083c66ac9dbSNicholas Bellinger 
10846708bb27SAndy Grover 	if (!hba)
1085c66ac9dbSNicholas Bellinger 		return;
1086c66ac9dbSNicholas Bellinger 
1087e3d6f909SAndy Grover 	if (g_lun0_dev)
10880fd97ccfSChristoph Hellwig 		target_free_device(g_lun0_dev);
1089c66ac9dbSNicholas Bellinger 	core_delete_hba(hba);
1090c66ac9dbSNicholas Bellinger }
10917bfea53bSAndy Grover 
10927bfea53bSAndy Grover /*
10937bfea53bSAndy Grover  * Common CDB parsing for kernel and user passthrough.
10947bfea53bSAndy Grover  */
10957bfea53bSAndy Grover sense_reason_t
10967bfea53bSAndy Grover passthrough_parse_cdb(struct se_cmd *cmd,
10977bfea53bSAndy Grover 	sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
10987bfea53bSAndy Grover {
10997bfea53bSAndy Grover 	unsigned char *cdb = cmd->t_task_cdb;
11004ec5bf0eSBryant G. Ly 	struct se_device *dev = cmd->se_dev;
11014ec5bf0eSBryant G. Ly 	unsigned int size;
11027bfea53bSAndy Grover 
11037bfea53bSAndy Grover 	/*
11047bfea53bSAndy Grover 	 * For REPORT LUNS we always need to emulate the response, for everything
11057bfea53bSAndy Grover 	 * else, pass it up.
11067bfea53bSAndy Grover 	 */
11077bfea53bSAndy Grover 	if (cdb[0] == REPORT_LUNS) {
11087bfea53bSAndy Grover 		cmd->execute_cmd = spc_emulate_report_luns;
11097bfea53bSAndy Grover 		return TCM_NO_SENSE;
11107bfea53bSAndy Grover 	}
11117bfea53bSAndy Grover 
11124ec5bf0eSBryant G. Ly 	/*
1113b49d6f78SDavid Disseldorp 	 * With emulate_pr disabled, all reservation requests should fail,
1114b49d6f78SDavid Disseldorp 	 * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
1115b49d6f78SDavid Disseldorp 	 */
1116b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr &&
1117b49d6f78SDavid Disseldorp 	    ((cdb[0] == PERSISTENT_RESERVE_IN) ||
1118b49d6f78SDavid Disseldorp 	     (cdb[0] == PERSISTENT_RESERVE_OUT) ||
1119b49d6f78SDavid Disseldorp 	     (cdb[0] == RELEASE || cdb[0] == RELEASE_10) ||
1120b49d6f78SDavid Disseldorp 	     (cdb[0] == RESERVE || cdb[0] == RESERVE_10))) {
1121b49d6f78SDavid Disseldorp 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1122b49d6f78SDavid Disseldorp 	}
1123b49d6f78SDavid Disseldorp 
1124b49d6f78SDavid Disseldorp 	/*
11254ec5bf0eSBryant G. Ly 	 * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
11264ec5bf0eSBryant G. Ly 	 * emulate the response, since tcmu does not have the information
11274ec5bf0eSBryant G. Ly 	 * required to process these commands.
11284ec5bf0eSBryant G. Ly 	 */
112969088a04SBodo Stroesser 	if (!(dev->transport_flags &
11304ec5bf0eSBryant G. Ly 	      TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
11314ec5bf0eSBryant G. Ly 		if (cdb[0] == PERSISTENT_RESERVE_IN) {
11324ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi3_emulate_pr_in;
1133a85d667eSBart Van Assche 			size = get_unaligned_be16(&cdb[7]);
11344ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11354ec5bf0eSBryant G. Ly 		}
11364ec5bf0eSBryant G. Ly 		if (cdb[0] == PERSISTENT_RESERVE_OUT) {
11374ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi3_emulate_pr_out;
1138388fe699STang Wenji 			size = get_unaligned_be32(&cdb[5]);
11394ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11404ec5bf0eSBryant G. Ly 		}
11414ec5bf0eSBryant G. Ly 
11424ec5bf0eSBryant G. Ly 		if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
11434ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi2_reservation_release;
11444ec5bf0eSBryant G. Ly 			if (cdb[0] == RELEASE_10)
1145a85d667eSBart Van Assche 				size = get_unaligned_be16(&cdb[7]);
11464ec5bf0eSBryant G. Ly 			else
11474ec5bf0eSBryant G. Ly 				size = cmd->data_length;
11484ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11494ec5bf0eSBryant G. Ly 		}
11504ec5bf0eSBryant G. Ly 		if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
11514ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi2_reservation_reserve;
11524ec5bf0eSBryant G. Ly 			if (cdb[0] == RESERVE_10)
1153a85d667eSBart Van Assche 				size = get_unaligned_be16(&cdb[7]);
11544ec5bf0eSBryant G. Ly 			else
11554ec5bf0eSBryant G. Ly 				size = cmd->data_length;
11564ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11574ec5bf0eSBryant G. Ly 		}
11584ec5bf0eSBryant G. Ly 	}
11594ec5bf0eSBryant G. Ly 
11607bfea53bSAndy Grover 	/* Set DATA_CDB flag for ops that should have it */
11617bfea53bSAndy Grover 	switch (cdb[0]) {
11627bfea53bSAndy Grover 	case READ_6:
11637bfea53bSAndy Grover 	case READ_10:
11647bfea53bSAndy Grover 	case READ_12:
11657bfea53bSAndy Grover 	case READ_16:
11667bfea53bSAndy Grover 	case WRITE_6:
11677bfea53bSAndy Grover 	case WRITE_10:
11687bfea53bSAndy Grover 	case WRITE_12:
11697bfea53bSAndy Grover 	case WRITE_16:
11707bfea53bSAndy Grover 	case WRITE_VERIFY:
11717bfea53bSAndy Grover 	case WRITE_VERIFY_12:
11723e182db7SBart Van Assche 	case WRITE_VERIFY_16:
11737bfea53bSAndy Grover 	case COMPARE_AND_WRITE:
11747bfea53bSAndy Grover 	case XDWRITEREAD_10:
11757bfea53bSAndy Grover 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
11767bfea53bSAndy Grover 		break;
11777bfea53bSAndy Grover 	case VARIABLE_LENGTH_CMD:
11787bfea53bSAndy Grover 		switch (get_unaligned_be16(&cdb[8])) {
11797bfea53bSAndy Grover 		case READ_32:
11807bfea53bSAndy Grover 		case WRITE_32:
1181e5dc9a70SDamien Le Moal 		case WRITE_VERIFY_32:
11827bfea53bSAndy Grover 		case XDWRITEREAD_32:
11837bfea53bSAndy Grover 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
11847bfea53bSAndy Grover 			break;
11857bfea53bSAndy Grover 		}
11867bfea53bSAndy Grover 	}
11877bfea53bSAndy Grover 
11887bfea53bSAndy Grover 	cmd->execute_cmd = exec_cmd;
11897bfea53bSAndy Grover 
11907bfea53bSAndy Grover 	return TCM_NO_SENSE;
11917bfea53bSAndy Grover }
11927bfea53bSAndy Grover EXPORT_SYMBOL(passthrough_parse_cdb);
1193