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 
52c82ff239SColin Ian King static DEFINE_MUTEX(device_mutex);
53c82ff239SColin Ian King static LIST_HEAD(device_list);
540a5eee64SMike Christie static DEFINE_IDR(devices_idr);
55d9ea32bfSNicholas Bellinger 
56e3d6f909SAndy Grover static struct se_hba *lun0_hba;
57e3d6f909SAndy Grover /* not static, needed by tpg.c */
58e3d6f909SAndy Grover struct se_device *g_lun0_dev;
59e3d6f909SAndy Grover 
60de103c93SChristoph Hellwig sense_reason_t
61f2d30680SHannes Reinecke transport_lookup_cmd_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
62c66ac9dbSNicholas Bellinger {
63c66ac9dbSNicholas Bellinger 	struct se_lun *se_lun = NULL;
64e3d6f909SAndy Grover 	struct se_session *se_sess = se_cmd->se_sess;
6529a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
6629a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
678fa3a867SNicholas Bellinger 	sense_reason_t ret = TCM_NO_SENSE;
68c66ac9dbSNicholas Bellinger 
6929a05deeSNicholas Bellinger 	rcu_read_lock();
7029a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, unpacked_lun);
7129a05deeSNicholas Bellinger 	if (deve) {
7229a05deeSNicholas Bellinger 		atomic_long_inc(&deve->total_cmds);
73c66ac9dbSNicholas Bellinger 
745951146dSAndy Grover 		if (se_cmd->data_direction == DMA_TO_DEVICE)
7529a05deeSNicholas Bellinger 			atomic_long_add(se_cmd->data_length,
7629a05deeSNicholas Bellinger 					&deve->write_bytes);
775951146dSAndy Grover 		else if (se_cmd->data_direction == DMA_FROM_DEVICE)
7829a05deeSNicholas Bellinger 			atomic_long_add(se_cmd->data_length,
7929a05deeSNicholas Bellinger 					&deve->read_bytes);
805951146dSAndy Grover 
8129a05deeSNicholas Bellinger 		se_lun = rcu_dereference(deve->se_lun);
82bd4e2d29SNicholas Bellinger 
83bd4e2d29SNicholas Bellinger 		if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
84bd4e2d29SNicholas Bellinger 			se_lun = NULL;
85bd4e2d29SNicholas Bellinger 			goto out_unlock;
86bd4e2d29SNicholas Bellinger 		}
87bd4e2d29SNicholas Bellinger 
8829a05deeSNicholas Bellinger 		se_cmd->se_lun = rcu_dereference(deve->se_lun);
895951146dSAndy Grover 		se_cmd->pr_res_key = deve->pr_res_key;
905951146dSAndy Grover 		se_cmd->orig_fe_lun = unpacked_lun;
915951146dSAndy Grover 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
925277797dSNicholas Bellinger 		se_cmd->lun_ref_active = true;
938fa3a867SNicholas Bellinger 
948fa3a867SNicholas Bellinger 		if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
9503a68b44SAndy Grover 		    deve->lun_access_ro) {
968fa3a867SNicholas Bellinger 			pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
978fa3a867SNicholas Bellinger 				" Access for 0x%08llx\n",
9830c7ca93SDavid Disseldorp 				se_cmd->se_tfo->fabric_name,
998fa3a867SNicholas Bellinger 				unpacked_lun);
1008fa3a867SNicholas Bellinger 			rcu_read_unlock();
1018fa3a867SNicholas Bellinger 			ret = TCM_WRITE_PROTECTED;
1028fa3a867SNicholas Bellinger 			goto ref_dev;
1038fa3a867SNicholas Bellinger 		}
1045951146dSAndy Grover 	}
105bd4e2d29SNicholas Bellinger out_unlock:
10629a05deeSNicholas Bellinger 	rcu_read_unlock();
1075951146dSAndy Grover 
1085951146dSAndy Grover 	if (!se_lun) {
109c66ac9dbSNicholas Bellinger 		/*
110c66ac9dbSNicholas Bellinger 		 * Use the se_portal_group->tpg_virt_lun0 to allow for
111c66ac9dbSNicholas Bellinger 		 * REPORT_LUNS, et al to be returned when no active
112c66ac9dbSNicholas Bellinger 		 * MappedLUN=0 exists for this Initiator Port.
113c66ac9dbSNicholas Bellinger 		 */
114c66ac9dbSNicholas Bellinger 		if (unpacked_lun != 0) {
1156708bb27SAndy Grover 			pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
116f2d30680SHannes Reinecke 				" Access for 0x%08llx\n",
11730c7ca93SDavid Disseldorp 				se_cmd->se_tfo->fabric_name,
118c66ac9dbSNicholas Bellinger 				unpacked_lun);
119de103c93SChristoph Hellwig 			return TCM_NON_EXISTENT_LUN;
120c66ac9dbSNicholas Bellinger 		}
1215951146dSAndy Grover 
122adf653f9SChristoph Hellwig 		se_lun = se_sess->se_tpg->tpg_virt_lun0;
123adf653f9SChristoph Hellwig 		se_cmd->se_lun = se_sess->se_tpg->tpg_virt_lun0;
124c66ac9dbSNicholas Bellinger 		se_cmd->orig_fe_lun = 0;
125c66ac9dbSNicholas Bellinger 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
1265277797dSNicholas Bellinger 
1275277797dSNicholas Bellinger 		percpu_ref_get(&se_lun->lun_ref);
1285277797dSNicholas Bellinger 		se_cmd->lun_ref_active = true;
1298fa3a867SNicholas Bellinger 
1308fa3a867SNicholas Bellinger 		/*
1318fa3a867SNicholas Bellinger 		 * Force WRITE PROTECT for virtual LUN 0
1328fa3a867SNicholas Bellinger 		 */
1338fa3a867SNicholas Bellinger 		if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
1348fa3a867SNicholas Bellinger 		    (se_cmd->data_direction != DMA_NONE)) {
1358fa3a867SNicholas Bellinger 			ret = TCM_WRITE_PROTECTED;
1368fa3a867SNicholas Bellinger 			goto ref_dev;
1378fa3a867SNicholas Bellinger 		}
138c66ac9dbSNicholas Bellinger 	}
1394cc987eaSNicholas Bellinger 	/*
1404cc987eaSNicholas Bellinger 	 * RCU reference protected by percpu se_lun->lun_ref taken above that
1414cc987eaSNicholas Bellinger 	 * must drop to zero (including initial reference) before this se_lun
1424cc987eaSNicholas Bellinger 	 * pointer can be kfree_rcu() by the final se_lun->lun_group put via
1434cc987eaSNicholas Bellinger 	 * target_core_fabric_configfs.c:target_fabric_port_release
1444cc987eaSNicholas Bellinger 	 */
1458fa3a867SNicholas Bellinger ref_dev:
1464cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1474cc987eaSNicholas Bellinger 	atomic_long_inc(&se_cmd->se_dev->num_cmds);
148c66ac9dbSNicholas Bellinger 
149c66ac9dbSNicholas Bellinger 	if (se_cmd->data_direction == DMA_TO_DEVICE)
1504cc987eaSNicholas Bellinger 		atomic_long_add(se_cmd->data_length,
1514cc987eaSNicholas Bellinger 				&se_cmd->se_dev->write_bytes);
152c66ac9dbSNicholas Bellinger 	else if (se_cmd->data_direction == DMA_FROM_DEVICE)
1534cc987eaSNicholas Bellinger 		atomic_long_add(se_cmd->data_length,
1544cc987eaSNicholas Bellinger 				&se_cmd->se_dev->read_bytes);
155c66ac9dbSNicholas Bellinger 
1568fa3a867SNicholas Bellinger 	return ret;
157c66ac9dbSNicholas Bellinger }
1585951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_cmd_lun);
159c66ac9dbSNicholas Bellinger 
160f2d30680SHannes Reinecke int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u64 unpacked_lun)
161c66ac9dbSNicholas Bellinger {
162c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
163c66ac9dbSNicholas Bellinger 	struct se_lun *se_lun = NULL;
164e3d6f909SAndy Grover 	struct se_session *se_sess = se_cmd->se_sess;
16529a05deeSNicholas Bellinger 	struct se_node_acl *nacl = se_sess->se_node_acl;
166c66ac9dbSNicholas Bellinger 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
1675e1be919SRoland Dreier 	unsigned long flags;
168c66ac9dbSNicholas Bellinger 
16929a05deeSNicholas Bellinger 	rcu_read_lock();
17029a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, unpacked_lun);
17129a05deeSNicholas Bellinger 	if (deve) {
17229a05deeSNicholas Bellinger 		se_lun = rcu_dereference(deve->se_lun);
173eeb64d23SNicholas Bellinger 
174eeb64d23SNicholas Bellinger 		if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
175eeb64d23SNicholas Bellinger 			se_lun = NULL;
176eeb64d23SNicholas Bellinger 			goto out_unlock;
177eeb64d23SNicholas Bellinger 		}
178eeb64d23SNicholas Bellinger 
179eeb64d23SNicholas Bellinger 		se_cmd->se_lun = rcu_dereference(deve->se_lun);
180c66ac9dbSNicholas Bellinger 		se_cmd->pr_res_key = deve->pr_res_key;
181c66ac9dbSNicholas Bellinger 		se_cmd->orig_fe_lun = unpacked_lun;
182eeb64d23SNicholas Bellinger 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
183eeb64d23SNicholas Bellinger 		se_cmd->lun_ref_active = true;
184c66ac9dbSNicholas Bellinger 	}
185eeb64d23SNicholas Bellinger out_unlock:
18629a05deeSNicholas Bellinger 	rcu_read_unlock();
187c66ac9dbSNicholas Bellinger 
188c66ac9dbSNicholas Bellinger 	if (!se_lun) {
1896708bb27SAndy Grover 		pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
190f2d30680SHannes Reinecke 			" Access for 0x%08llx\n",
19130c7ca93SDavid Disseldorp 			se_cmd->se_tfo->fabric_name,
192c66ac9dbSNicholas Bellinger 			unpacked_lun);
193e3d6f909SAndy Grover 		return -ENODEV;
194c66ac9dbSNicholas Bellinger 	}
1954cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1964cc987eaSNicholas Bellinger 	se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1975951146dSAndy Grover 
1985e1be919SRoland Dreier 	spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
1995951146dSAndy Grover 	list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
2005e1be919SRoland Dreier 	spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
201c66ac9dbSNicholas Bellinger 
202c66ac9dbSNicholas Bellinger 	return 0;
203c66ac9dbSNicholas Bellinger }
2045951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_tmr_lun);
205c66ac9dbSNicholas Bellinger 
20629a05deeSNicholas Bellinger bool target_lun_is_rdonly(struct se_cmd *cmd)
20729a05deeSNicholas Bellinger {
20829a05deeSNicholas Bellinger 	struct se_session *se_sess = cmd->se_sess;
20929a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
21029a05deeSNicholas Bellinger 	bool ret;
21129a05deeSNicholas Bellinger 
21229a05deeSNicholas Bellinger 	rcu_read_lock();
21329a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
21403a68b44SAndy Grover 	ret = deve && deve->lun_access_ro;
21529a05deeSNicholas Bellinger 	rcu_read_unlock();
21629a05deeSNicholas Bellinger 
21729a05deeSNicholas Bellinger 	return ret;
21829a05deeSNicholas Bellinger }
21929a05deeSNicholas Bellinger EXPORT_SYMBOL(target_lun_is_rdonly);
22029a05deeSNicholas Bellinger 
221c66ac9dbSNicholas Bellinger /*
222c66ac9dbSNicholas Bellinger  * This function is called from core_scsi3_emulate_pro_register_and_move()
22329a05deeSNicholas Bellinger  * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
224c66ac9dbSNicholas Bellinger  * when a matching rtpi is found.
225c66ac9dbSNicholas Bellinger  */
226c66ac9dbSNicholas Bellinger struct se_dev_entry *core_get_se_deve_from_rtpi(
227c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
228c66ac9dbSNicholas Bellinger 	u16 rtpi)
229c66ac9dbSNicholas Bellinger {
230c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
231c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
232c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
233c66ac9dbSNicholas Bellinger 
23429a05deeSNicholas Bellinger 	rcu_read_lock();
23529a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
23629a05deeSNicholas Bellinger 		lun = rcu_dereference(deve->se_lun);
2376708bb27SAndy Grover 		if (!lun) {
2386708bb27SAndy Grover 			pr_err("%s device entries device pointer is"
239c66ac9dbSNicholas Bellinger 				" NULL, but Initiator has access.\n",
24030c7ca93SDavid Disseldorp 				tpg->se_tpg_tfo->fabric_name);
241c66ac9dbSNicholas Bellinger 			continue;
242c66ac9dbSNicholas Bellinger 		}
24329a05deeSNicholas Bellinger 		if (lun->lun_rtpi != rtpi)
244c66ac9dbSNicholas Bellinger 			continue;
245c66ac9dbSNicholas Bellinger 
24629a05deeSNicholas Bellinger 		kref_get(&deve->pr_kref);
24729a05deeSNicholas Bellinger 		rcu_read_unlock();
248c66ac9dbSNicholas Bellinger 
249c66ac9dbSNicholas Bellinger 		return deve;
250c66ac9dbSNicholas Bellinger 	}
25129a05deeSNicholas Bellinger 	rcu_read_unlock();
252c66ac9dbSNicholas Bellinger 
253c66ac9dbSNicholas Bellinger 	return NULL;
254c66ac9dbSNicholas Bellinger }
255c66ac9dbSNicholas Bellinger 
25629a05deeSNicholas Bellinger void core_free_device_list_for_node(
257c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
258c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg)
259c66ac9dbSNicholas Bellinger {
260c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
261c66ac9dbSNicholas Bellinger 
26229a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
26329a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
26429a05deeSNicholas Bellinger 		struct se_lun *lun = rcu_dereference_check(deve->se_lun,
26529a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
26629a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
267c66ac9dbSNicholas Bellinger 	}
26829a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
269c66ac9dbSNicholas Bellinger }
270c66ac9dbSNicholas Bellinger 
271c66ac9dbSNicholas Bellinger void core_update_device_list_access(
272f2d30680SHannes Reinecke 	u64 mapped_lun,
27303a68b44SAndy Grover 	bool lun_access_ro,
274c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
275c66ac9dbSNicholas Bellinger {
276c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
277c66ac9dbSNicholas Bellinger 
27829a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
27929a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, mapped_lun);
28003a68b44SAndy Grover 	if (deve)
28103a68b44SAndy Grover 		deve->lun_access_ro = lun_access_ro;
28229a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
283c66ac9dbSNicholas Bellinger }
284c66ac9dbSNicholas Bellinger 
28529a05deeSNicholas Bellinger /*
28629a05deeSNicholas Bellinger  * Called with rcu_read_lock or nacl->device_list_lock held.
287c66ac9dbSNicholas Bellinger  */
288f2d30680SHannes Reinecke struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
28929a05deeSNicholas Bellinger {
29029a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
29129a05deeSNicholas Bellinger 
29229a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
29329a05deeSNicholas Bellinger 		if (deve->mapped_lun == mapped_lun)
29429a05deeSNicholas Bellinger 			return deve;
29529a05deeSNicholas Bellinger 
29629a05deeSNicholas Bellinger 	return NULL;
29729a05deeSNicholas Bellinger }
29829a05deeSNicholas Bellinger EXPORT_SYMBOL(target_nacl_find_deve);
29929a05deeSNicholas Bellinger 
30029a05deeSNicholas Bellinger void target_pr_kref_release(struct kref *kref)
30129a05deeSNicholas Bellinger {
30229a05deeSNicholas Bellinger 	struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
30329a05deeSNicholas Bellinger 						 pr_kref);
30429a05deeSNicholas Bellinger 	complete(&deve->pr_comp);
305c66ac9dbSNicholas Bellinger }
306c66ac9dbSNicholas Bellinger 
3077c0d0d51SHannes Reinecke static void
3087c0d0d51SHannes Reinecke target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
3097c0d0d51SHannes Reinecke 			     bool skip_new)
3107c0d0d51SHannes Reinecke {
3117c0d0d51SHannes Reinecke 	struct se_dev_entry *tmp;
3127c0d0d51SHannes Reinecke 
3137c0d0d51SHannes Reinecke 	rcu_read_lock();
3147c0d0d51SHannes Reinecke 	hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
3157c0d0d51SHannes Reinecke 		if (skip_new && tmp == new)
3167c0d0d51SHannes Reinecke 			continue;
3177c0d0d51SHannes Reinecke 		core_scsi3_ua_allocate(tmp, 0x3F,
3187c0d0d51SHannes Reinecke 				       ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
3197c0d0d51SHannes Reinecke 	}
3207c0d0d51SHannes Reinecke 	rcu_read_unlock();
3217c0d0d51SHannes Reinecke }
3227c0d0d51SHannes Reinecke 
323e80ac6c4SAndy Grover int core_enable_device_list_for_node(
324c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
325c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl,
326f2d30680SHannes Reinecke 	u64 mapped_lun,
32703a68b44SAndy Grover 	bool lun_access_ro,
328c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
329e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
330c66ac9dbSNicholas Bellinger {
33129a05deeSNicholas Bellinger 	struct se_dev_entry *orig, *new;
332c66ac9dbSNicholas Bellinger 
33329a05deeSNicholas Bellinger 	new = kzalloc(sizeof(*new), GFP_KERNEL);
33429a05deeSNicholas Bellinger 	if (!new) {
33529a05deeSNicholas Bellinger 		pr_err("Unable to allocate se_dev_entry memory\n");
33629a05deeSNicholas Bellinger 		return -ENOMEM;
33729a05deeSNicholas Bellinger 	}
338e80ac6c4SAndy Grover 
33929a05deeSNicholas Bellinger 	spin_lock_init(&new->ua_lock);
34029a05deeSNicholas Bellinger 	INIT_LIST_HEAD(&new->ua_list);
341adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&new->lun_link);
342e80ac6c4SAndy Grover 
34329a05deeSNicholas Bellinger 	new->mapped_lun = mapped_lun;
34429a05deeSNicholas Bellinger 	kref_init(&new->pr_kref);
34529a05deeSNicholas Bellinger 	init_completion(&new->pr_comp);
34629a05deeSNicholas Bellinger 
34703a68b44SAndy Grover 	new->lun_access_ro = lun_access_ro;
34829a05deeSNicholas Bellinger 	new->creation_time = get_jiffies_64();
34929a05deeSNicholas Bellinger 	new->attach_count++;
35029a05deeSNicholas Bellinger 
35129a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
35229a05deeSNicholas Bellinger 	orig = target_nacl_find_deve(nacl, mapped_lun);
35329a05deeSNicholas Bellinger 	if (orig && orig->se_lun) {
35429a05deeSNicholas Bellinger 		struct se_lun *orig_lun = rcu_dereference_check(orig->se_lun,
35529a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
35629a05deeSNicholas Bellinger 
35729a05deeSNicholas Bellinger 		if (orig_lun != lun) {
35829a05deeSNicholas Bellinger 			pr_err("Existing orig->se_lun doesn't match new lun"
35929a05deeSNicholas Bellinger 			       " for dynamic -> explicit NodeACL conversion:"
36029a05deeSNicholas Bellinger 				" %s\n", nacl->initiatorname);
36129a05deeSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
36229a05deeSNicholas Bellinger 			kfree(new);
363e3d6f909SAndy Grover 			return -EINVAL;
364c66ac9dbSNicholas Bellinger 		}
365391e2a6dSNicholas Bellinger 		if (orig->se_lun_acl != NULL) {
366391e2a6dSNicholas Bellinger 			pr_warn_ratelimited("Detected existing explicit"
367391e2a6dSNicholas Bellinger 				" se_lun_acl->se_lun_group reference for %s"
368391e2a6dSNicholas Bellinger 				" mapped_lun: %llu, failing\n",
369391e2a6dSNicholas Bellinger 				 nacl->initiatorname, mapped_lun);
370391e2a6dSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
371391e2a6dSNicholas Bellinger 			kfree(new);
372391e2a6dSNicholas Bellinger 			return -EINVAL;
373391e2a6dSNicholas Bellinger 		}
374c66ac9dbSNicholas Bellinger 
37529a05deeSNicholas Bellinger 		rcu_assign_pointer(new->se_lun, lun);
37629a05deeSNicholas Bellinger 		rcu_assign_pointer(new->se_lun_acl, lun_acl);
37729a05deeSNicholas Bellinger 		hlist_del_rcu(&orig->link);
37829a05deeSNicholas Bellinger 		hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
37929a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
380c66ac9dbSNicholas Bellinger 
3811adff1b3SNicholas Bellinger 		spin_lock(&lun->lun_deve_lock);
382adf653f9SChristoph Hellwig 		list_del(&orig->lun_link);
383adf653f9SChristoph Hellwig 		list_add_tail(&new->lun_link, &lun->lun_deve_list);
3841adff1b3SNicholas Bellinger 		spin_unlock(&lun->lun_deve_lock);
38529a05deeSNicholas Bellinger 
38629a05deeSNicholas Bellinger 		kref_put(&orig->pr_kref, target_pr_kref_release);
38729a05deeSNicholas Bellinger 		wait_for_completion(&orig->pr_comp);
38829a05deeSNicholas Bellinger 
3897c0d0d51SHannes Reinecke 		target_luns_data_has_changed(nacl, new, true);
39029a05deeSNicholas Bellinger 		kfree_rcu(orig, rcu_head);
391c66ac9dbSNicholas Bellinger 		return 0;
392c66ac9dbSNicholas Bellinger 	}
393e80ac6c4SAndy Grover 
39429a05deeSNicholas Bellinger 	rcu_assign_pointer(new->se_lun, lun);
39529a05deeSNicholas Bellinger 	rcu_assign_pointer(new->se_lun_acl, lun_acl);
39629a05deeSNicholas Bellinger 	hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
39729a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
398c66ac9dbSNicholas Bellinger 
3991adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
400adf653f9SChristoph Hellwig 	list_add_tail(&new->lun_link, &lun->lun_deve_list);
4011adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
402c66ac9dbSNicholas Bellinger 
4037c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, new, true);
404c66ac9dbSNicholas Bellinger 	return 0;
405c66ac9dbSNicholas Bellinger }
406c66ac9dbSNicholas Bellinger 
40729a05deeSNicholas Bellinger void core_disable_device_list_for_node(
408e80ac6c4SAndy Grover 	struct se_lun *lun,
40929a05deeSNicholas Bellinger 	struct se_dev_entry *orig,
410e80ac6c4SAndy Grover 	struct se_node_acl *nacl,
411e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
412e80ac6c4SAndy Grover {
413e80ac6c4SAndy Grover 	/*
4144cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
4154cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
4164cc987eaSNicholas Bellinger 	 */
4174cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
418618baaf7SBart Van Assche 
419618baaf7SBart Van Assche 	lockdep_assert_held(&nacl->lun_entry_mutex);
420618baaf7SBart Van Assche 
421e80ac6c4SAndy Grover 	/*
422e80ac6c4SAndy Grover 	 * If the MappedLUN entry is being disabled, the entry in
423adf653f9SChristoph Hellwig 	 * lun->lun_deve_list must be removed now before clearing the
424e80ac6c4SAndy Grover 	 * struct se_dev_entry pointers below as logic in
425e80ac6c4SAndy Grover 	 * core_alua_do_transition_tg_pt() depends on these being present.
426e80ac6c4SAndy Grover 	 *
427e80ac6c4SAndy Grover 	 * deve->se_lun_acl will be NULL for demo-mode created LUNs
428e80ac6c4SAndy Grover 	 * that have not been explicitly converted to MappedLUNs ->
429adf653f9SChristoph Hellwig 	 * struct se_lun_acl, but we remove deve->lun_link from
430adf653f9SChristoph Hellwig 	 * lun->lun_deve_list. This also means that active UAs and
431e80ac6c4SAndy Grover 	 * NodeACL context specific PR metadata for demo-mode
432e80ac6c4SAndy Grover 	 * MappedLUN *deve will be released below..
433e80ac6c4SAndy Grover 	 */
4341adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
435adf653f9SChristoph Hellwig 	list_del(&orig->lun_link);
4361adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
437c66ac9dbSNicholas Bellinger 	/*
438c66ac9dbSNicholas Bellinger 	 * Disable struct se_dev_entry LUN ACL mapping
439c66ac9dbSNicholas Bellinger 	 */
44029a05deeSNicholas Bellinger 	core_scsi3_ua_release_all(orig);
441c66ac9dbSNicholas Bellinger 
44229a05deeSNicholas Bellinger 	hlist_del_rcu(&orig->link);
44380bfdfa9SNicholas Bellinger 	clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
44403a68b44SAndy Grover 	orig->lun_access_ro = false;
44529a05deeSNicholas Bellinger 	orig->creation_time = 0;
44629a05deeSNicholas Bellinger 	orig->attach_count--;
44729a05deeSNicholas Bellinger 	/*
44829a05deeSNicholas Bellinger 	 * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
44929a05deeSNicholas Bellinger 	 * or REGISTER_AND_MOVE PR operation to complete.
45029a05deeSNicholas Bellinger 	 */
45129a05deeSNicholas Bellinger 	kref_put(&orig->pr_kref, target_pr_kref_release);
45229a05deeSNicholas Bellinger 	wait_for_completion(&orig->pr_comp);
45329a05deeSNicholas Bellinger 
4543ccd6e83SNicholas Bellinger 	rcu_assign_pointer(orig->se_lun, NULL);
4553ccd6e83SNicholas Bellinger 	rcu_assign_pointer(orig->se_lun_acl, NULL);
4563ccd6e83SNicholas Bellinger 
45729a05deeSNicholas Bellinger 	kfree_rcu(orig, rcu_head);
458c66ac9dbSNicholas Bellinger 
4594cc987eaSNicholas Bellinger 	core_scsi3_free_pr_reg_from_nacl(dev, nacl);
4607c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, NULL, false);
461c66ac9dbSNicholas Bellinger }
462c66ac9dbSNicholas Bellinger 
463c66ac9dbSNicholas Bellinger /*      core_clear_lun_from_tpg():
464c66ac9dbSNicholas Bellinger  *
465c66ac9dbSNicholas Bellinger  *
466c66ac9dbSNicholas Bellinger  */
467c66ac9dbSNicholas Bellinger void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
468c66ac9dbSNicholas Bellinger {
469c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
470c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
471c66ac9dbSNicholas Bellinger 
472403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
473c66ac9dbSNicholas Bellinger 	list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
474c66ac9dbSNicholas Bellinger 
47529a05deeSNicholas Bellinger 		mutex_lock(&nacl->lun_entry_mutex);
47629a05deeSNicholas Bellinger 		hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
47729a05deeSNicholas Bellinger 			struct se_lun *tmp_lun = rcu_dereference_check(deve->se_lun,
47829a05deeSNicholas Bellinger 					lockdep_is_held(&nacl->lun_entry_mutex));
47929a05deeSNicholas Bellinger 
48029a05deeSNicholas Bellinger 			if (lun != tmp_lun)
481c66ac9dbSNicholas Bellinger 				continue;
482c66ac9dbSNicholas Bellinger 
48329a05deeSNicholas Bellinger 			core_disable_device_list_for_node(lun, deve, nacl, tpg);
484c66ac9dbSNicholas Bellinger 		}
48529a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
486c66ac9dbSNicholas Bellinger 	}
487403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
488c66ac9dbSNicholas Bellinger }
489c66ac9dbSNicholas Bellinger 
490adf653f9SChristoph Hellwig int core_alloc_rtpi(struct se_lun *lun, struct se_device *dev)
491c66ac9dbSNicholas Bellinger {
492adf653f9SChristoph Hellwig 	struct se_lun *tmp;
493c66ac9dbSNicholas Bellinger 
494c66ac9dbSNicholas Bellinger 	spin_lock(&dev->se_port_lock);
495adf653f9SChristoph Hellwig 	if (dev->export_count == 0x0000ffff) {
4966708bb27SAndy Grover 		pr_warn("Reached dev->dev_port_count =="
497c66ac9dbSNicholas Bellinger 				" 0x0000ffff\n");
498c66ac9dbSNicholas Bellinger 		spin_unlock(&dev->se_port_lock);
499adf653f9SChristoph Hellwig 		return -ENOSPC;
500c66ac9dbSNicholas Bellinger 	}
501c66ac9dbSNicholas Bellinger again:
502c66ac9dbSNicholas Bellinger 	/*
50335d1efe8SMasanari Iida 	 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
504c66ac9dbSNicholas Bellinger 	 * Here is the table from spc4r17 section 7.7.3.8.
505c66ac9dbSNicholas Bellinger 	 *
506c66ac9dbSNicholas Bellinger 	 *    Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
507c66ac9dbSNicholas Bellinger 	 *
508c66ac9dbSNicholas Bellinger 	 * Code      Description
509c66ac9dbSNicholas Bellinger 	 * 0h        Reserved
510c66ac9dbSNicholas Bellinger 	 * 1h        Relative port 1, historically known as port A
511c66ac9dbSNicholas Bellinger 	 * 2h        Relative port 2, historically known as port B
512c66ac9dbSNicholas Bellinger 	 * 3h to FFFFh    Relative port 3 through 65 535
513c66ac9dbSNicholas Bellinger 	 */
514adf653f9SChristoph Hellwig 	lun->lun_rtpi = dev->dev_rpti_counter++;
515adf653f9SChristoph Hellwig 	if (!lun->lun_rtpi)
516c66ac9dbSNicholas Bellinger 		goto again;
517c66ac9dbSNicholas Bellinger 
518adf653f9SChristoph Hellwig 	list_for_each_entry(tmp, &dev->dev_sep_list, lun_dev_link) {
519c66ac9dbSNicholas Bellinger 		/*
52035d1efe8SMasanari Iida 		 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
521c66ac9dbSNicholas Bellinger 		 * for 16-bit wrap..
522c66ac9dbSNicholas Bellinger 		 */
523adf653f9SChristoph Hellwig 		if (lun->lun_rtpi == tmp->lun_rtpi)
524c66ac9dbSNicholas Bellinger 			goto again;
525c66ac9dbSNicholas Bellinger 	}
526c66ac9dbSNicholas Bellinger 	spin_unlock(&dev->se_port_lock);
527c66ac9dbSNicholas Bellinger 
528c66ac9dbSNicholas Bellinger 	return 0;
529c66ac9dbSNicholas Bellinger }
530c66ac9dbSNicholas Bellinger 
5310fd97ccfSChristoph Hellwig static void se_release_vpd_for_dev(struct se_device *dev)
532c66ac9dbSNicholas Bellinger {
533c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd, *vpd_tmp;
534c66ac9dbSNicholas Bellinger 
5350fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_wwn.t10_vpd_lock);
536c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(vpd, vpd_tmp,
5370fd97ccfSChristoph Hellwig 			&dev->t10_wwn.t10_vpd_list, vpd_list) {
538c66ac9dbSNicholas Bellinger 		list_del(&vpd->vpd_list);
539c66ac9dbSNicholas Bellinger 		kfree(vpd);
540c66ac9dbSNicholas Bellinger 	}
5410fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_wwn.t10_vpd_lock);
542c66ac9dbSNicholas Bellinger }
543c66ac9dbSNicholas Bellinger 
544c8045372SRoland Dreier static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
545525a48a2SNicholas Bellinger {
5463e03989bSRoland Dreier 	u32 aligned_max_sectors;
5473e03989bSRoland Dreier 	u32 alignment;
548525a48a2SNicholas Bellinger 	/*
549525a48a2SNicholas Bellinger 	 * Limit max_sectors to a PAGE_SIZE aligned value for modern
550525a48a2SNicholas Bellinger 	 * transport_allocate_data_tasks() operation.
551525a48a2SNicholas Bellinger 	 */
5523e03989bSRoland Dreier 	alignment = max(1ul, PAGE_SIZE / block_size);
5533e03989bSRoland Dreier 	aligned_max_sectors = rounddown(max_sectors, alignment);
554525a48a2SNicholas Bellinger 
5553e03989bSRoland Dreier 	if (max_sectors != aligned_max_sectors)
5563e03989bSRoland Dreier 		pr_info("Rounding down aligned max_sectors from %u to %u\n",
5573e03989bSRoland Dreier 			max_sectors, aligned_max_sectors);
5583e03989bSRoland Dreier 
5593e03989bSRoland Dreier 	return aligned_max_sectors;
560525a48a2SNicholas Bellinger }
561525a48a2SNicholas Bellinger 
5626bb82612SNicholas Bellinger int core_dev_add_lun(
563c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
564c66ac9dbSNicholas Bellinger 	struct se_device *dev,
5656bb82612SNicholas Bellinger 	struct se_lun *lun)
566c66ac9dbSNicholas Bellinger {
5678d9efe53SSebastian Andrzej Siewior 	int rc;
568c66ac9dbSNicholas Bellinger 
56903a68b44SAndy Grover 	rc = core_tpg_add_lun(tpg, lun, false, dev);
5708d9efe53SSebastian Andrzej Siewior 	if (rc < 0)
5716bb82612SNicholas Bellinger 		return rc;
572c66ac9dbSNicholas Bellinger 
573f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
57430c7ca93SDavid Disseldorp 		" CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
5752af7973aSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
57630c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
577c66ac9dbSNicholas Bellinger 	/*
578c66ac9dbSNicholas Bellinger 	 * Update LUN maps for dynamically added initiators when
579c66ac9dbSNicholas Bellinger 	 * generate_node_acl is enabled.
580c66ac9dbSNicholas Bellinger 	 */
581e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
582c66ac9dbSNicholas Bellinger 		struct se_node_acl *acl;
583403edd78SNicholas Bellinger 
584403edd78SNicholas Bellinger 		mutex_lock(&tpg->acl_node_mutex);
585c66ac9dbSNicholas Bellinger 		list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
586052605c6SNicholas Bellinger 			if (acl->dynamic_node_acl &&
587052605c6SNicholas Bellinger 			    (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
588052605c6SNicholas Bellinger 			     !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
589df9766caSNicholas Bellinger 				core_tpg_add_node_to_devs(acl, tpg, lun);
590c66ac9dbSNicholas Bellinger 			}
591c66ac9dbSNicholas Bellinger 		}
592403edd78SNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
593c66ac9dbSNicholas Bellinger 	}
594c66ac9dbSNicholas Bellinger 
5956bb82612SNicholas Bellinger 	return 0;
596c66ac9dbSNicholas Bellinger }
597c66ac9dbSNicholas Bellinger 
598c66ac9dbSNicholas Bellinger /*      core_dev_del_lun():
599c66ac9dbSNicholas Bellinger  *
600c66ac9dbSNicholas Bellinger  *
601c66ac9dbSNicholas Bellinger  */
602cd9d7cbaSAndy Grover void core_dev_del_lun(
603c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
604cd9d7cbaSAndy Grover 	struct se_lun *lun)
605c66ac9dbSNicholas Bellinger {
606f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
60730c7ca93SDavid Disseldorp 		" device object\n", tpg->se_tpg_tfo->fabric_name,
608cd9d7cbaSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
60930c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name);
610c66ac9dbSNicholas Bellinger 
611cd9d7cbaSAndy Grover 	core_tpg_remove_lun(tpg, lun);
612c66ac9dbSNicholas Bellinger }
613c66ac9dbSNicholas Bellinger 
614c66ac9dbSNicholas Bellinger struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
615c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
616fcf29481SNicholas Bellinger 	struct se_node_acl *nacl,
617f2d30680SHannes Reinecke 	u64 mapped_lun,
618c66ac9dbSNicholas Bellinger 	int *ret)
619c66ac9dbSNicholas Bellinger {
620c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl;
621c66ac9dbSNicholas Bellinger 
622fcf29481SNicholas Bellinger 	if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
6236708bb27SAndy Grover 		pr_err("%s InitiatorName exceeds maximum size.\n",
62430c7ca93SDavid Disseldorp 			tpg->se_tpg_tfo->fabric_name);
625c66ac9dbSNicholas Bellinger 		*ret = -EOVERFLOW;
626c66ac9dbSNicholas Bellinger 		return NULL;
627c66ac9dbSNicholas Bellinger 	}
628c66ac9dbSNicholas Bellinger 	lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
6296708bb27SAndy Grover 	if (!lacl) {
6306708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct se_lun_acl.\n");
631c66ac9dbSNicholas Bellinger 		*ret = -ENOMEM;
632c66ac9dbSNicholas Bellinger 		return NULL;
633c66ac9dbSNicholas Bellinger 	}
634c66ac9dbSNicholas Bellinger 
635c66ac9dbSNicholas Bellinger 	lacl->mapped_lun = mapped_lun;
636c66ac9dbSNicholas Bellinger 	lacl->se_lun_nacl = nacl;
637c66ac9dbSNicholas Bellinger 
638c66ac9dbSNicholas Bellinger 	return lacl;
639c66ac9dbSNicholas Bellinger }
640c66ac9dbSNicholas Bellinger 
641c66ac9dbSNicholas Bellinger int core_dev_add_initiator_node_lun_acl(
642c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
643c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl,
6446bb82612SNicholas Bellinger 	struct se_lun *lun,
64503a68b44SAndy Grover 	bool lun_access_ro)
646c66ac9dbSNicholas Bellinger {
6476bb82612SNicholas Bellinger 	struct se_node_acl *nacl = lacl->se_lun_nacl;
6484cc987eaSNicholas Bellinger 	/*
6494cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
6504cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
6514cc987eaSNicholas Bellinger 	 */
6524cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
653c66ac9dbSNicholas Bellinger 
6546708bb27SAndy Grover 	if (!nacl)
655c66ac9dbSNicholas Bellinger 		return -EINVAL;
656c66ac9dbSNicholas Bellinger 
65703a68b44SAndy Grover 	if (lun->lun_access_ro)
65803a68b44SAndy Grover 		lun_access_ro = true;
659c66ac9dbSNicholas Bellinger 
660c66ac9dbSNicholas Bellinger 	lacl->se_lun = lun;
661c66ac9dbSNicholas Bellinger 
662e80ac6c4SAndy Grover 	if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
66303a68b44SAndy Grover 			lun_access_ro, nacl, tpg) < 0)
664c66ac9dbSNicholas Bellinger 		return -EINVAL;
665c66ac9dbSNicholas Bellinger 
666f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
66730c7ca93SDavid Disseldorp 		" InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
6686bb82612SNicholas Bellinger 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
66903a68b44SAndy Grover 		lun_access_ro ? "RO" : "RW",
670b6a54b8dSChris Zankel 		nacl->initiatorname);
671c66ac9dbSNicholas Bellinger 	/*
672c66ac9dbSNicholas Bellinger 	 * Check to see if there are any existing persistent reservation APTPL
673c66ac9dbSNicholas Bellinger 	 * pre-registrations that need to be enabled for this LUN ACL..
674c66ac9dbSNicholas Bellinger 	 */
6754cc987eaSNicholas Bellinger 	core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
676e2480563SNicholas Bellinger 					    lacl->mapped_lun);
677c66ac9dbSNicholas Bellinger 	return 0;
678c66ac9dbSNicholas Bellinger }
679c66ac9dbSNicholas Bellinger 
680c66ac9dbSNicholas Bellinger int core_dev_del_initiator_node_lun_acl(
681c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
682c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
683c66ac9dbSNicholas Bellinger {
684adf653f9SChristoph Hellwig 	struct se_portal_group *tpg = lun->lun_tpg;
685c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
68629a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
687c66ac9dbSNicholas Bellinger 
688c66ac9dbSNicholas Bellinger 	nacl = lacl->se_lun_nacl;
6896708bb27SAndy Grover 	if (!nacl)
690c66ac9dbSNicholas Bellinger 		return -EINVAL;
691c66ac9dbSNicholas Bellinger 
69229a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
69329a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
69429a05deeSNicholas Bellinger 	if (deve)
69529a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
69629a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
697c66ac9dbSNicholas Bellinger 
698f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
699f2d30680SHannes Reinecke 		" InitiatorNode: %s Mapped LUN: %llu\n",
70030c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
701e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
702b6a54b8dSChris Zankel 		nacl->initiatorname, lacl->mapped_lun);
703c66ac9dbSNicholas Bellinger 
704c66ac9dbSNicholas Bellinger 	return 0;
705c66ac9dbSNicholas Bellinger }
706c66ac9dbSNicholas Bellinger 
707c66ac9dbSNicholas Bellinger void core_dev_free_initiator_node_lun_acl(
708c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
709c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
710c66ac9dbSNicholas Bellinger {
7116708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
71230c7ca93SDavid Disseldorp 		" Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
713e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
71430c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
715b6a54b8dSChris Zankel 		lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
716c66ac9dbSNicholas Bellinger 
717c66ac9dbSNicholas Bellinger 	kfree(lacl);
718c66ac9dbSNicholas Bellinger }
719c66ac9dbSNicholas Bellinger 
7200fd97ccfSChristoph Hellwig static void scsi_dump_inquiry(struct se_device *dev)
7210fd97ccfSChristoph Hellwig {
7220fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
723b2da4abfSDavid Disseldorp 	int device_type = dev->transport->get_device_type(dev);
724b2da4abfSDavid Disseldorp 
7250fd97ccfSChristoph Hellwig 	/*
7260fd97ccfSChristoph Hellwig 	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
7270fd97ccfSChristoph Hellwig 	 */
728b2da4abfSDavid Disseldorp 	pr_debug("  Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
729b2da4abfSDavid Disseldorp 		wwn->vendor);
730b2da4abfSDavid Disseldorp 	pr_debug("  Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
731b2da4abfSDavid Disseldorp 		wwn->model);
732b2da4abfSDavid Disseldorp 	pr_debug("  Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
733b2da4abfSDavid Disseldorp 		wwn->revision);
7340fd97ccfSChristoph Hellwig 	pr_debug("  Type:   %s ", scsi_device_type(device_type));
7350fd97ccfSChristoph Hellwig }
7360fd97ccfSChristoph Hellwig 
7370fd97ccfSChristoph Hellwig struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
7380fd97ccfSChristoph Hellwig {
7390fd97ccfSChristoph Hellwig 	struct se_device *dev;
7404863e525SNicholas Bellinger 	struct se_lun *xcopy_lun;
7410fd97ccfSChristoph Hellwig 
7420a06d430SChristoph Hellwig 	dev = hba->backend->ops->alloc_device(hba, name);
7430fd97ccfSChristoph Hellwig 	if (!dev)
7440fd97ccfSChristoph Hellwig 		return NULL;
7450fd97ccfSChristoph Hellwig 
7460fd97ccfSChristoph Hellwig 	dev->se_hba = hba;
7470a06d430SChristoph Hellwig 	dev->transport = hba->backend->ops;
748fe052a18SSagi Grimberg 	dev->prot_length = sizeof(struct t10_pi_tuple);
7494cc987eaSNicholas Bellinger 	dev->hba_index = hba->hba_index;
7500fd97ccfSChristoph Hellwig 
7510fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_sep_list);
7520fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_tmr_list);
7530fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->delayed_cmd_list);
7540fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->state_list);
7550fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->qf_cmd_list);
7560fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->execute_task_lock);
7570fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->delayed_cmd_lock);
7580fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->dev_reservation_lock);
7590fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_port_lock);
7600fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_tmr_lock);
7610fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->qf_cmd_lock);
76268ff9b9bSNicholas Bellinger 	sema_init(&dev->caw_sem, 1);
7630fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
7640fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
7650fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.registration_list);
7660fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
7670fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.registration_lock);
7680fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
7690fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
7700fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
771c66094bfSHannes Reinecke 	INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
772c66094bfSHannes Reinecke 	spin_lock_init(&dev->t10_alua.lba_map_lock);
7730fd97ccfSChristoph Hellwig 
7740fd97ccfSChristoph Hellwig 	dev->t10_wwn.t10_dev = dev;
7750fd97ccfSChristoph Hellwig 	dev->t10_alua.t10_dev = dev;
7760fd97ccfSChristoph Hellwig 
7770fd97ccfSChristoph Hellwig 	dev->dev_attrib.da_dev = dev;
778adfa9570STregaron Bayly 	dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
779814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_dpo = 1;
780814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_write = 1;
781814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_read = 1;
7820fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
7830fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
7840fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
7850fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
7860fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
7870123a9ecSNicholas Bellinger 	dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
788d397a445SNicholas Bellinger 	dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
789b49d6f78SDavid Disseldorp 	dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
7902ed22c9cSNicholas Bellinger 	dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
7910fd97ccfSChristoph Hellwig 	dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
79292404e60SNicholas Bellinger 	dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
7930fd97ccfSChristoph Hellwig 	dev->dev_attrib.is_nonrot = DA_IS_NONROT;
7940fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
7950fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
7960fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_block_desc_count =
7970fd97ccfSChristoph Hellwig 		DA_MAX_UNMAP_BLOCK_DESC_COUNT;
7980fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
7990fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity_alignment =
8000fd97ccfSChristoph Hellwig 				DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
801e6f41633SJamie Pocas 	dev->dev_attrib.unmap_zeroes_data =
802e6f41633SJamie Pocas 				DA_UNMAP_ZEROES_DATA_DEFAULT;
803773cbaf7SNicholas Bellinger 	dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
8040fd97ccfSChristoph Hellwig 
8054863e525SNicholas Bellinger 	xcopy_lun = &dev->xcopy_lun;
8064cc987eaSNicholas Bellinger 	rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
807bd4e2d29SNicholas Bellinger 	init_completion(&xcopy_lun->lun_shutdown_comp);
808adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
809adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
810adf653f9SChristoph Hellwig 	mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
811adf653f9SChristoph Hellwig 	xcopy_lun->lun_tpg = &xcopy_pt_tpg;
8124863e525SNicholas Bellinger 
8133beeabd5SDavid Disseldorp 	/* Preload the default INQUIRY const values */
8143beeabd5SDavid Disseldorp 	strlcpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
8153beeabd5SDavid Disseldorp 	strlcpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
8163beeabd5SDavid Disseldorp 		sizeof(dev->t10_wwn.model));
8173beeabd5SDavid Disseldorp 	strlcpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
8183beeabd5SDavid Disseldorp 		sizeof(dev->t10_wwn.revision));
8193beeabd5SDavid Disseldorp 
8200fd97ccfSChristoph Hellwig 	return dev;
8210fd97ccfSChristoph Hellwig }
8220fd97ccfSChristoph Hellwig 
8238a9ebe71SMike Christie /*
8248a9ebe71SMike Christie  * Check if the underlying struct block_device request_queue supports
8258a9ebe71SMike Christie  * the QUEUE_FLAG_DISCARD bit for UNMAP/WRITE_SAME in SCSI + TRIM
8268a9ebe71SMike Christie  * in ATA and we need to set TPE=1
8278a9ebe71SMike Christie  */
8288a9ebe71SMike Christie bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
829ea263c7fSMike Christie 				       struct request_queue *q)
8308a9ebe71SMike Christie {
831ea263c7fSMike Christie 	int block_size = queue_logical_block_size(q);
832ea263c7fSMike Christie 
8338a9ebe71SMike Christie 	if (!blk_queue_discard(q))
8348a9ebe71SMike Christie 		return false;
8358a9ebe71SMike Christie 
836ea263c7fSMike Christie 	attrib->max_unmap_lba_count =
837ea263c7fSMike Christie 		q->limits.max_discard_sectors >> (ilog2(block_size) - 9);
8388a9ebe71SMike Christie 	/*
8398a9ebe71SMike Christie 	 * Currently hardcoded to 1 in Linux/SCSI code..
8408a9ebe71SMike Christie 	 */
8418a9ebe71SMike Christie 	attrib->max_unmap_block_desc_count = 1;
8428a9ebe71SMike Christie 	attrib->unmap_granularity = q->limits.discard_granularity / block_size;
8438a9ebe71SMike Christie 	attrib->unmap_granularity_alignment = q->limits.discard_alignment /
8448a9ebe71SMike Christie 								block_size;
8452237498fSNicholas Bellinger 	attrib->unmap_zeroes_data = (q->limits.max_write_zeroes_sectors);
8468a9ebe71SMike Christie 	return true;
8478a9ebe71SMike Christie }
8488a9ebe71SMike Christie EXPORT_SYMBOL(target_configure_unmap_from_queue);
8498a9ebe71SMike Christie 
8508a9ebe71SMike Christie /*
8518a9ebe71SMike Christie  * Convert from blocksize advertised to the initiator to the 512 byte
8528a9ebe71SMike Christie  * units unconditionally used by the Linux block layer.
8538a9ebe71SMike Christie  */
8548a9ebe71SMike Christie sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
8558a9ebe71SMike Christie {
8568a9ebe71SMike Christie 	switch (dev->dev_attrib.block_size) {
8578a9ebe71SMike Christie 	case 4096:
8588a9ebe71SMike Christie 		return lb << 3;
8598a9ebe71SMike Christie 	case 2048:
8608a9ebe71SMike Christie 		return lb << 2;
8618a9ebe71SMike Christie 	case 1024:
8628a9ebe71SMike Christie 		return lb << 1;
8638a9ebe71SMike Christie 	default:
8648a9ebe71SMike Christie 		return lb;
8658a9ebe71SMike Christie 	}
8668a9ebe71SMike Christie }
8678a9ebe71SMike Christie EXPORT_SYMBOL(target_to_linux_sector);
8688a9ebe71SMike Christie 
869b1943fd4SMike Christie struct devices_idr_iter {
87036d4cb46SBart Van Assche 	struct config_item *prev_item;
871b1943fd4SMike Christie 	int (*fn)(struct se_device *dev, void *data);
872b1943fd4SMike Christie 	void *data;
873b1943fd4SMike Christie };
874b1943fd4SMike Christie 
875b1943fd4SMike Christie static int target_devices_idr_iter(int id, void *p, void *data)
87636d4cb46SBart Van Assche 	 __must_hold(&device_mutex)
877b1943fd4SMike Christie {
878b1943fd4SMike Christie 	struct devices_idr_iter *iter = data;
879b1943fd4SMike Christie 	struct se_device *dev = p;
88036d4cb46SBart Van Assche 	int ret;
88136d4cb46SBart Van Assche 
88236d4cb46SBart Van Assche 	config_item_put(iter->prev_item);
88336d4cb46SBart Van Assche 	iter->prev_item = NULL;
884b1943fd4SMike Christie 
885b1943fd4SMike Christie 	/*
886b1943fd4SMike Christie 	 * We add the device early to the idr, so it can be used
887b1943fd4SMike Christie 	 * by backend modules during configuration. We do not want
888b1943fd4SMike Christie 	 * to allow other callers to access partially setup devices,
889b1943fd4SMike Christie 	 * so we skip them here.
890b1943fd4SMike Christie 	 */
891cb0f32e1SMike Christie 	if (!target_dev_configured(dev))
892b1943fd4SMike Christie 		return 0;
893b1943fd4SMike Christie 
89436d4cb46SBart Van Assche 	iter->prev_item = config_item_get_unless_zero(&dev->dev_group.cg_item);
89536d4cb46SBart Van Assche 	if (!iter->prev_item)
89636d4cb46SBart Van Assche 		return 0;
89736d4cb46SBart Van Assche 	mutex_unlock(&device_mutex);
89836d4cb46SBart Van Assche 
89936d4cb46SBart Van Assche 	ret = iter->fn(dev, iter->data);
90036d4cb46SBart Van Assche 
90136d4cb46SBart Van Assche 	mutex_lock(&device_mutex);
90236d4cb46SBart Van Assche 	return ret;
903b1943fd4SMike Christie }
904b1943fd4SMike Christie 
905b1943fd4SMike Christie /**
906b1943fd4SMike Christie  * target_for_each_device - iterate over configured devices
907b1943fd4SMike Christie  * @fn: iterator function
908b1943fd4SMike Christie  * @data: pointer to data that will be passed to fn
909b1943fd4SMike Christie  *
910b1943fd4SMike Christie  * fn must return 0 to continue looping over devices. non-zero will break
911b1943fd4SMike Christie  * from the loop and return that value to the caller.
912b1943fd4SMike Christie  */
913b1943fd4SMike Christie int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
914b1943fd4SMike Christie 			   void *data)
915b1943fd4SMike Christie {
91636d4cb46SBart Van Assche 	struct devices_idr_iter iter = { .fn = fn, .data = data };
917b1943fd4SMike Christie 	int ret;
918b1943fd4SMike Christie 
919be50f538SMike Christie 	mutex_lock(&device_mutex);
920b1943fd4SMike Christie 	ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
921be50f538SMike Christie 	mutex_unlock(&device_mutex);
92236d4cb46SBart Van Assche 	config_item_put(iter.prev_item);
923b1943fd4SMike Christie 	return ret;
924b1943fd4SMike Christie }
925b1943fd4SMike Christie 
9260fd97ccfSChristoph Hellwig int target_configure_device(struct se_device *dev)
9270fd97ccfSChristoph Hellwig {
9280fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
9290a5eee64SMike Christie 	int ret, id;
9300fd97ccfSChristoph Hellwig 
931cb0f32e1SMike Christie 	if (target_dev_configured(dev)) {
9320fd97ccfSChristoph Hellwig 		pr_err("se_dev->se_dev_ptr already set for storage"
9330fd97ccfSChristoph Hellwig 				" object\n");
9340fd97ccfSChristoph Hellwig 		return -EEXIST;
9350fd97ccfSChristoph Hellwig 	}
9360fd97ccfSChristoph Hellwig 
9370a5eee64SMike Christie 	/*
9380a5eee64SMike Christie 	 * Add early so modules like tcmu can use during its
9390a5eee64SMike Christie 	 * configuration.
9400a5eee64SMike Christie 	 */
941be50f538SMike Christie 	mutex_lock(&device_mutex);
9420a5eee64SMike Christie 	/*
9430a5eee64SMike Christie 	 * Use cyclic to try and avoid collisions with devices
9440a5eee64SMike Christie 	 * that were recently removed.
9450a5eee64SMike Christie 	 */
9460a5eee64SMike Christie 	id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
947be50f538SMike Christie 	mutex_unlock(&device_mutex);
9480a5eee64SMike Christie 	if (id < 0) {
9490a5eee64SMike Christie 		ret = -ENOMEM;
9500a5eee64SMike Christie 		goto out;
9510a5eee64SMike Christie 	}
9520a5eee64SMike Christie 	dev->dev_index = id;
9530a5eee64SMike Christie 
9540fd97ccfSChristoph Hellwig 	ret = dev->transport->configure_device(dev);
9550fd97ccfSChristoph Hellwig 	if (ret)
9560a5eee64SMike Christie 		goto out_free_index;
9570fd97ccfSChristoph Hellwig 	/*
9580fd97ccfSChristoph Hellwig 	 * XXX: there is not much point to have two different values here..
9590fd97ccfSChristoph Hellwig 	 */
9600fd97ccfSChristoph Hellwig 	dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
9610fd97ccfSChristoph Hellwig 	dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
9620fd97ccfSChristoph Hellwig 
9630fd97ccfSChristoph Hellwig 	/*
9640fd97ccfSChristoph Hellwig 	 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
9650fd97ccfSChristoph Hellwig 	 */
9660fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_max_sectors =
9670fd97ccfSChristoph Hellwig 		se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
9680fd97ccfSChristoph Hellwig 					 dev->dev_attrib.hw_block_size);
969046ba642SNicholas Bellinger 	dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
9700fd97ccfSChristoph Hellwig 
9710fd97ccfSChristoph Hellwig 	dev->creation_time = get_jiffies_64();
9720fd97ccfSChristoph Hellwig 
9730fd97ccfSChristoph Hellwig 	ret = core_setup_alua(dev);
9740fd97ccfSChristoph Hellwig 	if (ret)
975c82b59e7Stangwenji 		goto out_destroy_device;
9760fd97ccfSChristoph Hellwig 
9770fd97ccfSChristoph Hellwig 	/*
9780fd97ccfSChristoph Hellwig 	 * Setup work_queue for QUEUE_FULL
9790fd97ccfSChristoph Hellwig 	 */
9800fd97ccfSChristoph Hellwig 	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
9810fd97ccfSChristoph Hellwig 
9820fd97ccfSChristoph Hellwig 	scsi_dump_inquiry(dev);
9830fd97ccfSChristoph Hellwig 
9840fd97ccfSChristoph Hellwig 	spin_lock(&hba->device_lock);
9850fd97ccfSChristoph Hellwig 	hba->dev_count++;
9860fd97ccfSChristoph Hellwig 	spin_unlock(&hba->device_lock);
987d9ea32bfSNicholas Bellinger 
9885f7da044SNicholas Bellinger 	dev->dev_flags |= DF_CONFIGURED;
9895f7da044SNicholas Bellinger 
9900fd97ccfSChristoph Hellwig 	return 0;
9910fd97ccfSChristoph Hellwig 
992c82b59e7Stangwenji out_destroy_device:
993c82b59e7Stangwenji 	dev->transport->destroy_device(dev);
9940a5eee64SMike Christie out_free_index:
995be50f538SMike Christie 	mutex_lock(&device_mutex);
9960a5eee64SMike Christie 	idr_remove(&devices_idr, dev->dev_index);
997be50f538SMike Christie 	mutex_unlock(&device_mutex);
9980fd97ccfSChristoph Hellwig out:
9990fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
10000fd97ccfSChristoph Hellwig 	return ret;
10010fd97ccfSChristoph Hellwig }
10020fd97ccfSChristoph Hellwig 
10030fd97ccfSChristoph Hellwig void target_free_device(struct se_device *dev)
10040fd97ccfSChristoph Hellwig {
10050fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
10060fd97ccfSChristoph Hellwig 
10070fd97ccfSChristoph Hellwig 	WARN_ON(!list_empty(&dev->dev_sep_list));
10080fd97ccfSChristoph Hellwig 
1009cb0f32e1SMike Christie 	if (target_dev_configured(dev)) {
101092634706SMike Christie 		dev->transport->destroy_device(dev);
101192634706SMike Christie 
1012be50f538SMike Christie 		mutex_lock(&device_mutex);
10130a5eee64SMike Christie 		idr_remove(&devices_idr, dev->dev_index);
1014be50f538SMike Christie 		mutex_unlock(&device_mutex);
1015d9ea32bfSNicholas Bellinger 
10160fd97ccfSChristoph Hellwig 		spin_lock(&hba->device_lock);
10170fd97ccfSChristoph Hellwig 		hba->dev_count--;
10180fd97ccfSChristoph Hellwig 		spin_unlock(&hba->device_lock);
10190fd97ccfSChristoph Hellwig 	}
10200fd97ccfSChristoph Hellwig 
10210fd97ccfSChristoph Hellwig 	core_alua_free_lu_gp_mem(dev);
1022229d4f11SHannes Reinecke 	core_alua_set_lba_map(dev, NULL, 0, 0);
10230fd97ccfSChristoph Hellwig 	core_scsi3_free_all_registrations(dev);
10240fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
10250fd97ccfSChristoph Hellwig 
10262ed22c9cSNicholas Bellinger 	if (dev->transport->free_prot)
10272ed22c9cSNicholas Bellinger 		dev->transport->free_prot(dev);
10282ed22c9cSNicholas Bellinger 
10290fd97ccfSChristoph Hellwig 	dev->transport->free_device(dev);
10300fd97ccfSChristoph Hellwig }
10310fd97ccfSChristoph Hellwig 
1032c66ac9dbSNicholas Bellinger int core_dev_setup_virtual_lun0(void)
1033c66ac9dbSNicholas Bellinger {
1034c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
1035c66ac9dbSNicholas Bellinger 	struct se_device *dev;
1036db5d1c3cSAndy Grover 	char buf[] = "rd_pages=8,rd_nullio=1";
1037c66ac9dbSNicholas Bellinger 	int ret;
1038c66ac9dbSNicholas Bellinger 
10396708bb27SAndy Grover 	hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
1040c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
1041c66ac9dbSNicholas Bellinger 		return PTR_ERR(hba);
1042c66ac9dbSNicholas Bellinger 
10430fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, "virt_lun0");
10440fd97ccfSChristoph Hellwig 	if (!dev) {
1045c66ac9dbSNicholas Bellinger 		ret = -ENOMEM;
10460fd97ccfSChristoph Hellwig 		goto out_free_hba;
1047c66ac9dbSNicholas Bellinger 	}
1048c66ac9dbSNicholas Bellinger 
10490a06d430SChristoph Hellwig 	hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
1050c66ac9dbSNicholas Bellinger 
10510fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
10520fd97ccfSChristoph Hellwig 	if (ret)
10530fd97ccfSChristoph Hellwig 		goto out_free_se_dev;
10540fd97ccfSChristoph Hellwig 
10550fd97ccfSChristoph Hellwig 	lun0_hba = hba;
1056e3d6f909SAndy Grover 	g_lun0_dev = dev;
1057c66ac9dbSNicholas Bellinger 	return 0;
10580fd97ccfSChristoph Hellwig 
10590fd97ccfSChristoph Hellwig out_free_se_dev:
10600fd97ccfSChristoph Hellwig 	target_free_device(dev);
10610fd97ccfSChristoph Hellwig out_free_hba:
10620fd97ccfSChristoph Hellwig 	core_delete_hba(hba);
1063c66ac9dbSNicholas Bellinger 	return ret;
1064c66ac9dbSNicholas Bellinger }
1065c66ac9dbSNicholas Bellinger 
1066c66ac9dbSNicholas Bellinger 
1067c66ac9dbSNicholas Bellinger void core_dev_release_virtual_lun0(void)
1068c66ac9dbSNicholas Bellinger {
1069e3d6f909SAndy Grover 	struct se_hba *hba = lun0_hba;
1070c66ac9dbSNicholas Bellinger 
10716708bb27SAndy Grover 	if (!hba)
1072c66ac9dbSNicholas Bellinger 		return;
1073c66ac9dbSNicholas Bellinger 
1074e3d6f909SAndy Grover 	if (g_lun0_dev)
10750fd97ccfSChristoph Hellwig 		target_free_device(g_lun0_dev);
1076c66ac9dbSNicholas Bellinger 	core_delete_hba(hba);
1077c66ac9dbSNicholas Bellinger }
10787bfea53bSAndy Grover 
10797bfea53bSAndy Grover /*
10807bfea53bSAndy Grover  * Common CDB parsing for kernel and user passthrough.
10817bfea53bSAndy Grover  */
10827bfea53bSAndy Grover sense_reason_t
10837bfea53bSAndy Grover passthrough_parse_cdb(struct se_cmd *cmd,
10847bfea53bSAndy Grover 	sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
10857bfea53bSAndy Grover {
10867bfea53bSAndy Grover 	unsigned char *cdb = cmd->t_task_cdb;
10874ec5bf0eSBryant G. Ly 	struct se_device *dev = cmd->se_dev;
10884ec5bf0eSBryant G. Ly 	unsigned int size;
10897bfea53bSAndy Grover 
10907bfea53bSAndy Grover 	/*
10917bfea53bSAndy Grover 	 * Clear a lun set in the cdb if the initiator talking to use spoke
10927bfea53bSAndy Grover 	 * and old standards version, as we can't assume the underlying device
10937bfea53bSAndy Grover 	 * won't choke up on it.
10947bfea53bSAndy Grover 	 */
10957bfea53bSAndy Grover 	switch (cdb[0]) {
10967bfea53bSAndy Grover 	case READ_10: /* SBC - RDProtect */
10977bfea53bSAndy Grover 	case READ_12: /* SBC - RDProtect */
10987bfea53bSAndy Grover 	case READ_16: /* SBC - RDProtect */
10997bfea53bSAndy Grover 	case SEND_DIAGNOSTIC: /* SPC - SELF-TEST Code */
11007bfea53bSAndy Grover 	case VERIFY: /* SBC - VRProtect */
11017bfea53bSAndy Grover 	case VERIFY_16: /* SBC - VRProtect */
11027bfea53bSAndy Grover 	case WRITE_VERIFY: /* SBC - VRProtect */
11037bfea53bSAndy Grover 	case WRITE_VERIFY_12: /* SBC - VRProtect */
11047bfea53bSAndy Grover 	case MAINTENANCE_IN: /* SPC - Parameter Data Format for SA RTPG */
11057bfea53bSAndy Grover 		break;
11067bfea53bSAndy Grover 	default:
11077bfea53bSAndy Grover 		cdb[1] &= 0x1f; /* clear logical unit number */
11087bfea53bSAndy Grover 		break;
11097bfea53bSAndy Grover 	}
11107bfea53bSAndy Grover 
11117bfea53bSAndy Grover 	/*
11127bfea53bSAndy Grover 	 * For REPORT LUNS we always need to emulate the response, for everything
11137bfea53bSAndy Grover 	 * else, pass it up.
11147bfea53bSAndy Grover 	 */
11157bfea53bSAndy Grover 	if (cdb[0] == REPORT_LUNS) {
11167bfea53bSAndy Grover 		cmd->execute_cmd = spc_emulate_report_luns;
11177bfea53bSAndy Grover 		return TCM_NO_SENSE;
11187bfea53bSAndy Grover 	}
11197bfea53bSAndy Grover 
11204ec5bf0eSBryant G. Ly 	/*
1121b49d6f78SDavid Disseldorp 	 * With emulate_pr disabled, all reservation requests should fail,
1122b49d6f78SDavid Disseldorp 	 * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
1123b49d6f78SDavid Disseldorp 	 */
1124b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr &&
1125b49d6f78SDavid Disseldorp 	    ((cdb[0] == PERSISTENT_RESERVE_IN) ||
1126b49d6f78SDavid Disseldorp 	     (cdb[0] == PERSISTENT_RESERVE_OUT) ||
1127b49d6f78SDavid Disseldorp 	     (cdb[0] == RELEASE || cdb[0] == RELEASE_10) ||
1128b49d6f78SDavid Disseldorp 	     (cdb[0] == RESERVE || cdb[0] == RESERVE_10))) {
1129b49d6f78SDavid Disseldorp 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1130b49d6f78SDavid Disseldorp 	}
1131b49d6f78SDavid Disseldorp 
1132b49d6f78SDavid Disseldorp 	/*
11334ec5bf0eSBryant G. Ly 	 * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
11344ec5bf0eSBryant G. Ly 	 * emulate the response, since tcmu does not have the information
11354ec5bf0eSBryant G. Ly 	 * required to process these commands.
11364ec5bf0eSBryant G. Ly 	 */
11374ec5bf0eSBryant G. Ly 	if (!(dev->transport->transport_flags &
11384ec5bf0eSBryant G. Ly 	      TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
11394ec5bf0eSBryant G. Ly 		if (cdb[0] == PERSISTENT_RESERVE_IN) {
11404ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi3_emulate_pr_in;
1141a85d667eSBart Van Assche 			size = get_unaligned_be16(&cdb[7]);
11424ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11434ec5bf0eSBryant G. Ly 		}
11444ec5bf0eSBryant G. Ly 		if (cdb[0] == PERSISTENT_RESERVE_OUT) {
11454ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi3_emulate_pr_out;
1146388fe699STang Wenji 			size = get_unaligned_be32(&cdb[5]);
11474ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11484ec5bf0eSBryant G. Ly 		}
11494ec5bf0eSBryant G. Ly 
11504ec5bf0eSBryant G. Ly 		if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
11514ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi2_reservation_release;
11524ec5bf0eSBryant G. Ly 			if (cdb[0] == RELEASE_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 		if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
11594ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi2_reservation_reserve;
11604ec5bf0eSBryant G. Ly 			if (cdb[0] == RESERVE_10)
1161a85d667eSBart Van Assche 				size = get_unaligned_be16(&cdb[7]);
11624ec5bf0eSBryant G. Ly 			else
11634ec5bf0eSBryant G. Ly 				size = cmd->data_length;
11644ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11654ec5bf0eSBryant G. Ly 		}
11664ec5bf0eSBryant G. Ly 	}
11674ec5bf0eSBryant G. Ly 
11687bfea53bSAndy Grover 	/* Set DATA_CDB flag for ops that should have it */
11697bfea53bSAndy Grover 	switch (cdb[0]) {
11707bfea53bSAndy Grover 	case READ_6:
11717bfea53bSAndy Grover 	case READ_10:
11727bfea53bSAndy Grover 	case READ_12:
11737bfea53bSAndy Grover 	case READ_16:
11747bfea53bSAndy Grover 	case WRITE_6:
11757bfea53bSAndy Grover 	case WRITE_10:
11767bfea53bSAndy Grover 	case WRITE_12:
11777bfea53bSAndy Grover 	case WRITE_16:
11787bfea53bSAndy Grover 	case WRITE_VERIFY:
11797bfea53bSAndy Grover 	case WRITE_VERIFY_12:
11803e182db7SBart Van Assche 	case WRITE_VERIFY_16:
11817bfea53bSAndy Grover 	case COMPARE_AND_WRITE:
11827bfea53bSAndy Grover 	case XDWRITEREAD_10:
11837bfea53bSAndy Grover 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
11847bfea53bSAndy Grover 		break;
11857bfea53bSAndy Grover 	case VARIABLE_LENGTH_CMD:
11867bfea53bSAndy Grover 		switch (get_unaligned_be16(&cdb[8])) {
11877bfea53bSAndy Grover 		case READ_32:
11887bfea53bSAndy Grover 		case WRITE_32:
1189e5dc9a70SDamien Le Moal 		case WRITE_VERIFY_32:
11907bfea53bSAndy Grover 		case XDWRITEREAD_32:
11917bfea53bSAndy Grover 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
11927bfea53bSAndy Grover 			break;
11937bfea53bSAndy Grover 		}
11947bfea53bSAndy Grover 	}
11957bfea53bSAndy Grover 
11967bfea53bSAndy Grover 	cmd->execute_cmd = exec_cmd;
11977bfea53bSAndy Grover 
11987bfea53bSAndy Grover 	return TCM_NO_SENSE;
11997bfea53bSAndy Grover }
12007bfea53bSAndy Grover EXPORT_SYMBOL(passthrough_parse_cdb);
1201