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
transport_lookup_cmd_lun(struct se_cmd * se_cmd)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 
78ef4f7e4bSDmitry Bogdanov 		se_lun = 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 
transport_lookup_tmr_lun(struct se_cmd * se_cmd)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;
150c66ac9dbSNicholas Bellinger 
15129a05deeSNicholas Bellinger 	rcu_read_lock();
152a36840d8SSudhakar Panneerselvam 	deve = target_nacl_find_deve(nacl, se_cmd->orig_fe_lun);
15329a05deeSNicholas Bellinger 	if (deve) {
154ef4f7e4bSDmitry Bogdanov 		se_lun = deve->se_lun;
155eeb64d23SNicholas Bellinger 
156eeb64d23SNicholas Bellinger 		if (!percpu_ref_tryget_live(&se_lun->lun_ref)) {
157eeb64d23SNicholas Bellinger 			se_lun = NULL;
158eeb64d23SNicholas Bellinger 			goto out_unlock;
159eeb64d23SNicholas Bellinger 		}
160eeb64d23SNicholas Bellinger 
16163f74794SBart Van Assche 		se_cmd->se_lun = se_lun;
162c66ac9dbSNicholas Bellinger 		se_cmd->pr_res_key = deve->pr_res_key;
163eeb64d23SNicholas Bellinger 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
164eeb64d23SNicholas Bellinger 		se_cmd->lun_ref_active = true;
165c66ac9dbSNicholas Bellinger 	}
166eeb64d23SNicholas Bellinger out_unlock:
16729a05deeSNicholas Bellinger 	rcu_read_unlock();
168c66ac9dbSNicholas Bellinger 
169c66ac9dbSNicholas Bellinger 	if (!se_lun) {
1706708bb27SAndy Grover 		pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
1715482d56bSLance Digby 			" Access for 0x%08llx for %s\n",
17230c7ca93SDavid Disseldorp 			se_cmd->se_tfo->fabric_name,
173a36840d8SSudhakar Panneerselvam 			se_cmd->orig_fe_lun,
1745482d56bSLance Digby 			nacl->initiatorname);
175e3d6f909SAndy Grover 		return -ENODEV;
176c66ac9dbSNicholas Bellinger 	}
1774cc987eaSNicholas Bellinger 	se_cmd->se_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1784cc987eaSNicholas Bellinger 	se_tmr->tmr_dev = rcu_dereference_raw(se_lun->lun_se_dev);
1795951146dSAndy Grover 
180c66ac9dbSNicholas Bellinger 	return 0;
181c66ac9dbSNicholas Bellinger }
1825951146dSAndy Grover EXPORT_SYMBOL(transport_lookup_tmr_lun);
183c66ac9dbSNicholas Bellinger 
target_lun_is_rdonly(struct se_cmd * cmd)18429a05deeSNicholas Bellinger bool target_lun_is_rdonly(struct se_cmd *cmd)
18529a05deeSNicholas Bellinger {
18629a05deeSNicholas Bellinger 	struct se_session *se_sess = cmd->se_sess;
18729a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
18829a05deeSNicholas Bellinger 	bool ret;
18929a05deeSNicholas Bellinger 
19029a05deeSNicholas Bellinger 	rcu_read_lock();
19129a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(se_sess->se_node_acl, cmd->orig_fe_lun);
19203a68b44SAndy Grover 	ret = deve && deve->lun_access_ro;
19329a05deeSNicholas Bellinger 	rcu_read_unlock();
19429a05deeSNicholas Bellinger 
19529a05deeSNicholas Bellinger 	return ret;
19629a05deeSNicholas Bellinger }
19729a05deeSNicholas Bellinger EXPORT_SYMBOL(target_lun_is_rdonly);
19829a05deeSNicholas Bellinger 
199c66ac9dbSNicholas Bellinger /*
200c66ac9dbSNicholas Bellinger  * This function is called from core_scsi3_emulate_pro_register_and_move()
20129a05deeSNicholas Bellinger  * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_kref
202c66ac9dbSNicholas Bellinger  * when a matching rtpi is found.
203c66ac9dbSNicholas Bellinger  */
core_get_se_deve_from_rtpi(struct se_node_acl * nacl,u16 rtpi)204c66ac9dbSNicholas Bellinger struct se_dev_entry *core_get_se_deve_from_rtpi(
205c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
206c66ac9dbSNicholas Bellinger 	u16 rtpi)
207c66ac9dbSNicholas Bellinger {
208c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
209c66ac9dbSNicholas Bellinger 	struct se_lun *lun;
210c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg = nacl->se_tpg;
211c66ac9dbSNicholas Bellinger 
21229a05deeSNicholas Bellinger 	rcu_read_lock();
21329a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
214ef4f7e4bSDmitry Bogdanov 		lun = deve->se_lun;
2156708bb27SAndy Grover 		if (!lun) {
2166708bb27SAndy Grover 			pr_err("%s device entries device pointer is"
217c66ac9dbSNicholas Bellinger 				" NULL, but Initiator has access.\n",
21830c7ca93SDavid Disseldorp 				tpg->se_tpg_tfo->fabric_name);
219c66ac9dbSNicholas Bellinger 			continue;
220c66ac9dbSNicholas Bellinger 		}
221b9e063adSRoman Bolshakov 		if (lun->lun_tpg->tpg_rtpi != rtpi)
222c66ac9dbSNicholas Bellinger 			continue;
223c66ac9dbSNicholas Bellinger 
22429a05deeSNicholas Bellinger 		kref_get(&deve->pr_kref);
22529a05deeSNicholas Bellinger 		rcu_read_unlock();
226c66ac9dbSNicholas Bellinger 
227c66ac9dbSNicholas Bellinger 		return deve;
228c66ac9dbSNicholas Bellinger 	}
22929a05deeSNicholas Bellinger 	rcu_read_unlock();
230c66ac9dbSNicholas Bellinger 
231c66ac9dbSNicholas Bellinger 	return NULL;
232c66ac9dbSNicholas Bellinger }
233c66ac9dbSNicholas Bellinger 
core_free_device_list_for_node(struct se_node_acl * nacl,struct se_portal_group * tpg)23429a05deeSNicholas Bellinger void core_free_device_list_for_node(
235c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
236c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg)
237c66ac9dbSNicholas Bellinger {
238c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
239c66ac9dbSNicholas Bellinger 
24029a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
241ef4f7e4bSDmitry Bogdanov 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
242ef4f7e4bSDmitry Bogdanov 		core_disable_device_list_for_node(deve->se_lun, deve, nacl, tpg);
24329a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
244c66ac9dbSNicholas Bellinger }
245c66ac9dbSNicholas Bellinger 
core_update_device_list_access(u64 mapped_lun,bool lun_access_ro,struct se_node_acl * nacl)246c66ac9dbSNicholas Bellinger void core_update_device_list_access(
247f2d30680SHannes Reinecke 	u64 mapped_lun,
24803a68b44SAndy Grover 	bool lun_access_ro,
249c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl)
250c66ac9dbSNicholas Bellinger {
251c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
252c66ac9dbSNicholas Bellinger 
25329a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
25429a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, mapped_lun);
25503a68b44SAndy Grover 	if (deve)
25603a68b44SAndy Grover 		deve->lun_access_ro = lun_access_ro;
25729a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
258c66ac9dbSNicholas Bellinger }
259c66ac9dbSNicholas Bellinger 
26029a05deeSNicholas Bellinger /*
26129a05deeSNicholas Bellinger  * Called with rcu_read_lock or nacl->device_list_lock held.
262c66ac9dbSNicholas Bellinger  */
target_nacl_find_deve(struct se_node_acl * nacl,u64 mapped_lun)263f2d30680SHannes Reinecke struct se_dev_entry *target_nacl_find_deve(struct se_node_acl *nacl, u64 mapped_lun)
26429a05deeSNicholas Bellinger {
26529a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
26629a05deeSNicholas Bellinger 
26729a05deeSNicholas Bellinger 	hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link)
26829a05deeSNicholas Bellinger 		if (deve->mapped_lun == mapped_lun)
26929a05deeSNicholas Bellinger 			return deve;
27029a05deeSNicholas Bellinger 
27129a05deeSNicholas Bellinger 	return NULL;
27229a05deeSNicholas Bellinger }
27329a05deeSNicholas Bellinger EXPORT_SYMBOL(target_nacl_find_deve);
27429a05deeSNicholas Bellinger 
target_pr_kref_release(struct kref * kref)27529a05deeSNicholas Bellinger void target_pr_kref_release(struct kref *kref)
27629a05deeSNicholas Bellinger {
27729a05deeSNicholas Bellinger 	struct se_dev_entry *deve = container_of(kref, struct se_dev_entry,
27829a05deeSNicholas Bellinger 						 pr_kref);
27929a05deeSNicholas Bellinger 	complete(&deve->pr_comp);
280c66ac9dbSNicholas Bellinger }
281c66ac9dbSNicholas Bellinger 
2826290e23fSDmitry Bogdanov /*
2836290e23fSDmitry Bogdanov  * Establish UA condition on SCSI device - all LUNs
2846290e23fSDmitry Bogdanov  */
target_dev_ua_allocate(struct se_device * dev,u8 asc,u8 ascq)2856290e23fSDmitry Bogdanov void target_dev_ua_allocate(struct se_device *dev, u8 asc, u8 ascq)
2866290e23fSDmitry Bogdanov {
2876290e23fSDmitry Bogdanov 	struct se_dev_entry *se_deve;
2886290e23fSDmitry Bogdanov 	struct se_lun *lun;
2896290e23fSDmitry Bogdanov 
2906290e23fSDmitry Bogdanov 	spin_lock(&dev->se_port_lock);
2916290e23fSDmitry Bogdanov 	list_for_each_entry(lun, &dev->dev_sep_list, lun_dev_link) {
2926290e23fSDmitry Bogdanov 
2936290e23fSDmitry Bogdanov 		spin_lock(&lun->lun_deve_lock);
2946290e23fSDmitry Bogdanov 		list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
2956290e23fSDmitry Bogdanov 			core_scsi3_ua_allocate(se_deve, asc, ascq);
2966290e23fSDmitry Bogdanov 		spin_unlock(&lun->lun_deve_lock);
2976290e23fSDmitry Bogdanov 	}
2986290e23fSDmitry Bogdanov 	spin_unlock(&dev->se_port_lock);
2996290e23fSDmitry Bogdanov }
3006290e23fSDmitry Bogdanov 
3017c0d0d51SHannes Reinecke static void
target_luns_data_has_changed(struct se_node_acl * nacl,struct se_dev_entry * new,bool skip_new)3027c0d0d51SHannes Reinecke target_luns_data_has_changed(struct se_node_acl *nacl, struct se_dev_entry *new,
3037c0d0d51SHannes Reinecke 			     bool skip_new)
3047c0d0d51SHannes Reinecke {
3057c0d0d51SHannes Reinecke 	struct se_dev_entry *tmp;
3067c0d0d51SHannes Reinecke 
3077c0d0d51SHannes Reinecke 	rcu_read_lock();
3087c0d0d51SHannes Reinecke 	hlist_for_each_entry_rcu(tmp, &nacl->lun_entry_hlist, link) {
3097c0d0d51SHannes Reinecke 		if (skip_new && tmp == new)
3107c0d0d51SHannes Reinecke 			continue;
3117c0d0d51SHannes Reinecke 		core_scsi3_ua_allocate(tmp, 0x3F,
3127c0d0d51SHannes Reinecke 				       ASCQ_3FH_REPORTED_LUNS_DATA_HAS_CHANGED);
3137c0d0d51SHannes Reinecke 	}
3147c0d0d51SHannes Reinecke 	rcu_read_unlock();
3157c0d0d51SHannes Reinecke }
3167c0d0d51SHannes Reinecke 
core_enable_device_list_for_node(struct se_lun * lun,struct se_lun_acl * lun_acl,u64 mapped_lun,bool lun_access_ro,struct se_node_acl * nacl,struct se_portal_group * tpg)317e80ac6c4SAndy Grover int core_enable_device_list_for_node(
318c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
319c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lun_acl,
320f2d30680SHannes Reinecke 	u64 mapped_lun,
32103a68b44SAndy Grover 	bool lun_access_ro,
322c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl,
323e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
324c66ac9dbSNicholas Bellinger {
32529a05deeSNicholas Bellinger 	struct se_dev_entry *orig, *new;
326c66ac9dbSNicholas Bellinger 
32729a05deeSNicholas Bellinger 	new = kzalloc(sizeof(*new), GFP_KERNEL);
32829a05deeSNicholas Bellinger 	if (!new) {
32929a05deeSNicholas Bellinger 		pr_err("Unable to allocate se_dev_entry memory\n");
33029a05deeSNicholas Bellinger 		return -ENOMEM;
33129a05deeSNicholas Bellinger 	}
332e80ac6c4SAndy Grover 
33329a05deeSNicholas Bellinger 	spin_lock_init(&new->ua_lock);
33429a05deeSNicholas Bellinger 	INIT_LIST_HEAD(&new->ua_list);
335adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&new->lun_link);
336e80ac6c4SAndy Grover 
33729a05deeSNicholas Bellinger 	new->mapped_lun = mapped_lun;
33829a05deeSNicholas Bellinger 	kref_init(&new->pr_kref);
33929a05deeSNicholas Bellinger 	init_completion(&new->pr_comp);
34029a05deeSNicholas Bellinger 
34103a68b44SAndy Grover 	new->lun_access_ro = lun_access_ro;
34229a05deeSNicholas Bellinger 	new->creation_time = get_jiffies_64();
34329a05deeSNicholas Bellinger 	new->attach_count++;
34429a05deeSNicholas Bellinger 
34529a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
34629a05deeSNicholas Bellinger 	orig = target_nacl_find_deve(nacl, mapped_lun);
34729a05deeSNicholas Bellinger 	if (orig && orig->se_lun) {
348ef4f7e4bSDmitry Bogdanov 		struct se_lun *orig_lun = orig->se_lun;
34929a05deeSNicholas Bellinger 
35029a05deeSNicholas Bellinger 		if (orig_lun != lun) {
35129a05deeSNicholas Bellinger 			pr_err("Existing orig->se_lun doesn't match new lun"
35229a05deeSNicholas Bellinger 			       " for dynamic -> explicit NodeACL conversion:"
35329a05deeSNicholas Bellinger 				" %s\n", nacl->initiatorname);
35429a05deeSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
35529a05deeSNicholas Bellinger 			kfree(new);
356e3d6f909SAndy Grover 			return -EINVAL;
357c66ac9dbSNicholas Bellinger 		}
358391e2a6dSNicholas Bellinger 		if (orig->se_lun_acl != NULL) {
359391e2a6dSNicholas Bellinger 			pr_warn_ratelimited("Detected existing explicit"
360391e2a6dSNicholas Bellinger 				" se_lun_acl->se_lun_group reference for %s"
361391e2a6dSNicholas Bellinger 				" mapped_lun: %llu, failing\n",
362391e2a6dSNicholas Bellinger 				 nacl->initiatorname, mapped_lun);
363391e2a6dSNicholas Bellinger 			mutex_unlock(&nacl->lun_entry_mutex);
364391e2a6dSNicholas Bellinger 			kfree(new);
365391e2a6dSNicholas Bellinger 			return -EINVAL;
366391e2a6dSNicholas Bellinger 		}
367c66ac9dbSNicholas Bellinger 
368ef4f7e4bSDmitry Bogdanov 		new->se_lun = lun;
369ef4f7e4bSDmitry Bogdanov 		new->se_lun_acl = lun_acl;
37029a05deeSNicholas Bellinger 		hlist_del_rcu(&orig->link);
37129a05deeSNicholas Bellinger 		hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
37229a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
373c66ac9dbSNicholas Bellinger 
3741adff1b3SNicholas Bellinger 		spin_lock(&lun->lun_deve_lock);
375adf653f9SChristoph Hellwig 		list_del(&orig->lun_link);
376adf653f9SChristoph Hellwig 		list_add_tail(&new->lun_link, &lun->lun_deve_list);
3771adff1b3SNicholas Bellinger 		spin_unlock(&lun->lun_deve_lock);
37829a05deeSNicholas Bellinger 
37929a05deeSNicholas Bellinger 		kref_put(&orig->pr_kref, target_pr_kref_release);
38029a05deeSNicholas Bellinger 		wait_for_completion(&orig->pr_comp);
38129a05deeSNicholas Bellinger 
3827c0d0d51SHannes Reinecke 		target_luns_data_has_changed(nacl, new, true);
38329a05deeSNicholas Bellinger 		kfree_rcu(orig, rcu_head);
384c66ac9dbSNicholas Bellinger 		return 0;
385c66ac9dbSNicholas Bellinger 	}
386e80ac6c4SAndy Grover 
387ef4f7e4bSDmitry Bogdanov 	new->se_lun = lun;
388ef4f7e4bSDmitry Bogdanov 	new->se_lun_acl = lun_acl;
38929a05deeSNicholas Bellinger 	hlist_add_head_rcu(&new->link, &nacl->lun_entry_hlist);
39029a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
391c66ac9dbSNicholas Bellinger 
3921adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
393adf653f9SChristoph Hellwig 	list_add_tail(&new->lun_link, &lun->lun_deve_list);
3941adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
395c66ac9dbSNicholas Bellinger 
3967c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, new, true);
397c66ac9dbSNicholas Bellinger 	return 0;
398c66ac9dbSNicholas Bellinger }
399c66ac9dbSNicholas Bellinger 
core_disable_device_list_for_node(struct se_lun * lun,struct se_dev_entry * orig,struct se_node_acl * nacl,struct se_portal_group * tpg)40029a05deeSNicholas Bellinger void core_disable_device_list_for_node(
401e80ac6c4SAndy Grover 	struct se_lun *lun,
40229a05deeSNicholas Bellinger 	struct se_dev_entry *orig,
403e80ac6c4SAndy Grover 	struct se_node_acl *nacl,
404e80ac6c4SAndy Grover 	struct se_portal_group *tpg)
405e80ac6c4SAndy Grover {
406e80ac6c4SAndy Grover 	/*
4074cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
4084cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
4094cc987eaSNicholas Bellinger 	 */
4104cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
411618baaf7SBart Van Assche 
412618baaf7SBart Van Assche 	lockdep_assert_held(&nacl->lun_entry_mutex);
413618baaf7SBart Van Assche 
414e80ac6c4SAndy Grover 	/*
415e80ac6c4SAndy Grover 	 * If the MappedLUN entry is being disabled, the entry in
416adf653f9SChristoph Hellwig 	 * lun->lun_deve_list must be removed now before clearing the
417e80ac6c4SAndy Grover 	 * struct se_dev_entry pointers below as logic in
418e80ac6c4SAndy Grover 	 * core_alua_do_transition_tg_pt() depends on these being present.
419e80ac6c4SAndy Grover 	 *
420e80ac6c4SAndy Grover 	 * deve->se_lun_acl will be NULL for demo-mode created LUNs
421e80ac6c4SAndy Grover 	 * that have not been explicitly converted to MappedLUNs ->
422adf653f9SChristoph Hellwig 	 * struct se_lun_acl, but we remove deve->lun_link from
423adf653f9SChristoph Hellwig 	 * lun->lun_deve_list. This also means that active UAs and
424e80ac6c4SAndy Grover 	 * NodeACL context specific PR metadata for demo-mode
425e80ac6c4SAndy Grover 	 * MappedLUN *deve will be released below..
426e80ac6c4SAndy Grover 	 */
4271adff1b3SNicholas Bellinger 	spin_lock(&lun->lun_deve_lock);
428adf653f9SChristoph Hellwig 	list_del(&orig->lun_link);
4291adff1b3SNicholas Bellinger 	spin_unlock(&lun->lun_deve_lock);
430c66ac9dbSNicholas Bellinger 	/*
431c66ac9dbSNicholas Bellinger 	 * Disable struct se_dev_entry LUN ACL mapping
432c66ac9dbSNicholas Bellinger 	 */
43329a05deeSNicholas Bellinger 	core_scsi3_ua_release_all(orig);
434c66ac9dbSNicholas Bellinger 
43529a05deeSNicholas Bellinger 	hlist_del_rcu(&orig->link);
43680bfdfa9SNicholas Bellinger 	clear_bit(DEF_PR_REG_ACTIVE, &orig->deve_flags);
43703a68b44SAndy Grover 	orig->lun_access_ro = false;
43829a05deeSNicholas Bellinger 	orig->creation_time = 0;
43929a05deeSNicholas Bellinger 	orig->attach_count--;
44029a05deeSNicholas Bellinger 	/*
44129a05deeSNicholas Bellinger 	 * Before firing off RCU callback, wait for any in process SPEC_I_PT=1
44229a05deeSNicholas Bellinger 	 * or REGISTER_AND_MOVE PR operation to complete.
44329a05deeSNicholas Bellinger 	 */
44429a05deeSNicholas Bellinger 	kref_put(&orig->pr_kref, target_pr_kref_release);
44529a05deeSNicholas Bellinger 	wait_for_completion(&orig->pr_comp);
44629a05deeSNicholas Bellinger 
44729a05deeSNicholas Bellinger 	kfree_rcu(orig, rcu_head);
448c66ac9dbSNicholas Bellinger 
4494cc987eaSNicholas Bellinger 	core_scsi3_free_pr_reg_from_nacl(dev, nacl);
4507c0d0d51SHannes Reinecke 	target_luns_data_has_changed(nacl, NULL, false);
451c66ac9dbSNicholas Bellinger }
452c66ac9dbSNicholas Bellinger 
453c66ac9dbSNicholas Bellinger /*      core_clear_lun_from_tpg():
454c66ac9dbSNicholas Bellinger  *
455c66ac9dbSNicholas Bellinger  *
456c66ac9dbSNicholas Bellinger  */
core_clear_lun_from_tpg(struct se_lun * lun,struct se_portal_group * tpg)457c66ac9dbSNicholas Bellinger void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
458c66ac9dbSNicholas Bellinger {
459c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
460c66ac9dbSNicholas Bellinger 	struct se_dev_entry *deve;
461c66ac9dbSNicholas Bellinger 
462403edd78SNicholas Bellinger 	mutex_lock(&tpg->acl_node_mutex);
463c66ac9dbSNicholas Bellinger 	list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
464c66ac9dbSNicholas Bellinger 
46529a05deeSNicholas Bellinger 		mutex_lock(&nacl->lun_entry_mutex);
46629a05deeSNicholas Bellinger 		hlist_for_each_entry_rcu(deve, &nacl->lun_entry_hlist, link) {
467ef4f7e4bSDmitry Bogdanov 			if (lun != deve->se_lun)
468c66ac9dbSNicholas Bellinger 				continue;
469c66ac9dbSNicholas Bellinger 
47029a05deeSNicholas Bellinger 			core_disable_device_list_for_node(lun, deve, nacl, tpg);
471c66ac9dbSNicholas Bellinger 		}
47229a05deeSNicholas Bellinger 		mutex_unlock(&nacl->lun_entry_mutex);
473c66ac9dbSNicholas Bellinger 	}
474403edd78SNicholas Bellinger 	mutex_unlock(&tpg->acl_node_mutex);
475c66ac9dbSNicholas Bellinger }
476c66ac9dbSNicholas Bellinger 
se_release_vpd_for_dev(struct se_device * dev)4770fd97ccfSChristoph Hellwig static void se_release_vpd_for_dev(struct se_device *dev)
478c66ac9dbSNicholas Bellinger {
479c66ac9dbSNicholas Bellinger 	struct t10_vpd *vpd, *vpd_tmp;
480c66ac9dbSNicholas Bellinger 
4810fd97ccfSChristoph Hellwig 	spin_lock(&dev->t10_wwn.t10_vpd_lock);
482c66ac9dbSNicholas Bellinger 	list_for_each_entry_safe(vpd, vpd_tmp,
4830fd97ccfSChristoph Hellwig 			&dev->t10_wwn.t10_vpd_list, vpd_list) {
484c66ac9dbSNicholas Bellinger 		list_del(&vpd->vpd_list);
485c66ac9dbSNicholas Bellinger 		kfree(vpd);
486c66ac9dbSNicholas Bellinger 	}
4870fd97ccfSChristoph Hellwig 	spin_unlock(&dev->t10_wwn.t10_vpd_lock);
488c66ac9dbSNicholas Bellinger }
489c66ac9dbSNicholas Bellinger 
se_dev_align_max_sectors(u32 max_sectors,u32 block_size)490c8045372SRoland Dreier static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
491525a48a2SNicholas Bellinger {
4923e03989bSRoland Dreier 	u32 aligned_max_sectors;
4933e03989bSRoland Dreier 	u32 alignment;
494525a48a2SNicholas Bellinger 	/*
495525a48a2SNicholas Bellinger 	 * Limit max_sectors to a PAGE_SIZE aligned value for modern
496525a48a2SNicholas Bellinger 	 * transport_allocate_data_tasks() operation.
497525a48a2SNicholas Bellinger 	 */
4983e03989bSRoland Dreier 	alignment = max(1ul, PAGE_SIZE / block_size);
4993e03989bSRoland Dreier 	aligned_max_sectors = rounddown(max_sectors, alignment);
500525a48a2SNicholas Bellinger 
5013e03989bSRoland Dreier 	if (max_sectors != aligned_max_sectors)
5023e03989bSRoland Dreier 		pr_info("Rounding down aligned max_sectors from %u to %u\n",
5033e03989bSRoland Dreier 			max_sectors, aligned_max_sectors);
5043e03989bSRoland Dreier 
5053e03989bSRoland Dreier 	return aligned_max_sectors;
506525a48a2SNicholas Bellinger }
507525a48a2SNicholas Bellinger 
core_dev_add_lun(struct se_portal_group * tpg,struct se_device * dev,struct se_lun * lun)5086bb82612SNicholas Bellinger int core_dev_add_lun(
509c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
510c66ac9dbSNicholas Bellinger 	struct se_device *dev,
5116bb82612SNicholas Bellinger 	struct se_lun *lun)
512c66ac9dbSNicholas Bellinger {
5138d9efe53SSebastian Andrzej Siewior 	int rc;
514c66ac9dbSNicholas Bellinger 
51503a68b44SAndy Grover 	rc = core_tpg_add_lun(tpg, lun, false, dev);
5168d9efe53SSebastian Andrzej Siewior 	if (rc < 0)
5176bb82612SNicholas Bellinger 		return rc;
518c66ac9dbSNicholas Bellinger 
519f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Activated %s Logical Unit from"
52030c7ca93SDavid Disseldorp 		" CORE HBA: %u\n", tpg->se_tpg_tfo->fabric_name,
5212af7973aSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
52230c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name, dev->se_hba->hba_id);
523c66ac9dbSNicholas Bellinger 	/*
524c66ac9dbSNicholas Bellinger 	 * Update LUN maps for dynamically added initiators when
525c66ac9dbSNicholas Bellinger 	 * generate_node_acl is enabled.
526c66ac9dbSNicholas Bellinger 	 */
527e3d6f909SAndy Grover 	if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
528c66ac9dbSNicholas Bellinger 		struct se_node_acl *acl;
529403edd78SNicholas Bellinger 
530403edd78SNicholas Bellinger 		mutex_lock(&tpg->acl_node_mutex);
531c66ac9dbSNicholas Bellinger 		list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
532052605c6SNicholas Bellinger 			if (acl->dynamic_node_acl &&
533052605c6SNicholas Bellinger 			    (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
534052605c6SNicholas Bellinger 			     !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
535df9766caSNicholas Bellinger 				core_tpg_add_node_to_devs(acl, tpg, lun);
536c66ac9dbSNicholas Bellinger 			}
537c66ac9dbSNicholas Bellinger 		}
538403edd78SNicholas Bellinger 		mutex_unlock(&tpg->acl_node_mutex);
539c66ac9dbSNicholas Bellinger 	}
540c66ac9dbSNicholas Bellinger 
5416bb82612SNicholas Bellinger 	return 0;
542c66ac9dbSNicholas Bellinger }
543c66ac9dbSNicholas Bellinger 
544c66ac9dbSNicholas Bellinger /*      core_dev_del_lun():
545c66ac9dbSNicholas Bellinger  *
546c66ac9dbSNicholas Bellinger  *
547c66ac9dbSNicholas Bellinger  */
core_dev_del_lun(struct se_portal_group * tpg,struct se_lun * lun)548cd9d7cbaSAndy Grover void core_dev_del_lun(
549c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
550cd9d7cbaSAndy Grover 	struct se_lun *lun)
551c66ac9dbSNicholas Bellinger {
552f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%u]_LUN[%llu] - Deactivating %s Logical Unit from"
55330c7ca93SDavid Disseldorp 		" device object\n", tpg->se_tpg_tfo->fabric_name,
554cd9d7cbaSAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
55530c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name);
556c66ac9dbSNicholas Bellinger 
557cd9d7cbaSAndy Grover 	core_tpg_remove_lun(tpg, lun);
558c66ac9dbSNicholas Bellinger }
559c66ac9dbSNicholas Bellinger 
core_dev_init_initiator_node_lun_acl(struct se_portal_group * tpg,struct se_node_acl * nacl,u64 mapped_lun,int * ret)560c66ac9dbSNicholas Bellinger struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
561c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
562fcf29481SNicholas Bellinger 	struct se_node_acl *nacl,
563f2d30680SHannes Reinecke 	u64 mapped_lun,
564c66ac9dbSNicholas Bellinger 	int *ret)
565c66ac9dbSNicholas Bellinger {
566c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl;
567c66ac9dbSNicholas Bellinger 
568fcf29481SNicholas Bellinger 	if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
5696708bb27SAndy Grover 		pr_err("%s InitiatorName exceeds maximum size.\n",
57030c7ca93SDavid Disseldorp 			tpg->se_tpg_tfo->fabric_name);
571c66ac9dbSNicholas Bellinger 		*ret = -EOVERFLOW;
572c66ac9dbSNicholas Bellinger 		return NULL;
573c66ac9dbSNicholas Bellinger 	}
574c66ac9dbSNicholas Bellinger 	lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
5756708bb27SAndy Grover 	if (!lacl) {
5766708bb27SAndy Grover 		pr_err("Unable to allocate memory for struct se_lun_acl.\n");
577c66ac9dbSNicholas Bellinger 		*ret = -ENOMEM;
578c66ac9dbSNicholas Bellinger 		return NULL;
579c66ac9dbSNicholas Bellinger 	}
580c66ac9dbSNicholas Bellinger 
581c66ac9dbSNicholas Bellinger 	lacl->mapped_lun = mapped_lun;
582c66ac9dbSNicholas Bellinger 	lacl->se_lun_nacl = nacl;
583c66ac9dbSNicholas Bellinger 
584c66ac9dbSNicholas Bellinger 	return lacl;
585c66ac9dbSNicholas Bellinger }
586c66ac9dbSNicholas Bellinger 
core_dev_add_initiator_node_lun_acl(struct se_portal_group * tpg,struct se_lun_acl * lacl,struct se_lun * lun,bool lun_access_ro)587c66ac9dbSNicholas Bellinger int core_dev_add_initiator_node_lun_acl(
588c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
589c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl,
5906bb82612SNicholas Bellinger 	struct se_lun *lun,
59103a68b44SAndy Grover 	bool lun_access_ro)
592c66ac9dbSNicholas Bellinger {
5936bb82612SNicholas Bellinger 	struct se_node_acl *nacl = lacl->se_lun_nacl;
5944cc987eaSNicholas Bellinger 	/*
5954cc987eaSNicholas Bellinger 	 * rcu_dereference_raw protected by se_lun->lun_group symlink
5964cc987eaSNicholas Bellinger 	 * reference to se_device->dev_group.
5974cc987eaSNicholas Bellinger 	 */
5984cc987eaSNicholas Bellinger 	struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
599c66ac9dbSNicholas Bellinger 
6006708bb27SAndy Grover 	if (!nacl)
601c66ac9dbSNicholas Bellinger 		return -EINVAL;
602c66ac9dbSNicholas Bellinger 
60303a68b44SAndy Grover 	if (lun->lun_access_ro)
60403a68b44SAndy Grover 		lun_access_ro = true;
605c66ac9dbSNicholas Bellinger 
606c66ac9dbSNicholas Bellinger 	lacl->se_lun = lun;
607c66ac9dbSNicholas Bellinger 
608e80ac6c4SAndy Grover 	if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
60903a68b44SAndy Grover 			lun_access_ro, nacl, tpg) < 0)
610c66ac9dbSNicholas Bellinger 		return -EINVAL;
611c66ac9dbSNicholas Bellinger 
612f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu->%llu] - Added %s ACL for "
61330c7ca93SDavid Disseldorp 		" InitiatorNode: %s\n", tpg->se_tpg_tfo->fabric_name,
6146bb82612SNicholas Bellinger 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun, lacl->mapped_lun,
61503a68b44SAndy Grover 		lun_access_ro ? "RO" : "RW",
616b6a54b8dSChris Zankel 		nacl->initiatorname);
617c66ac9dbSNicholas Bellinger 	/*
618c66ac9dbSNicholas Bellinger 	 * Check to see if there are any existing persistent reservation APTPL
619c66ac9dbSNicholas Bellinger 	 * pre-registrations that need to be enabled for this LUN ACL..
620c66ac9dbSNicholas Bellinger 	 */
6214cc987eaSNicholas Bellinger 	core_scsi3_check_aptpl_registration(dev, tpg, lun, nacl,
622e2480563SNicholas Bellinger 					    lacl->mapped_lun);
623c66ac9dbSNicholas Bellinger 	return 0;
624c66ac9dbSNicholas Bellinger }
625c66ac9dbSNicholas Bellinger 
core_dev_del_initiator_node_lun_acl(struct se_lun * lun,struct se_lun_acl * lacl)626c66ac9dbSNicholas Bellinger int core_dev_del_initiator_node_lun_acl(
627c66ac9dbSNicholas Bellinger 	struct se_lun *lun,
628c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
629c66ac9dbSNicholas Bellinger {
630adf653f9SChristoph Hellwig 	struct se_portal_group *tpg = lun->lun_tpg;
631c66ac9dbSNicholas Bellinger 	struct se_node_acl *nacl;
63229a05deeSNicholas Bellinger 	struct se_dev_entry *deve;
633c66ac9dbSNicholas Bellinger 
634c66ac9dbSNicholas Bellinger 	nacl = lacl->se_lun_nacl;
6356708bb27SAndy Grover 	if (!nacl)
636c66ac9dbSNicholas Bellinger 		return -EINVAL;
637c66ac9dbSNicholas Bellinger 
63829a05deeSNicholas Bellinger 	mutex_lock(&nacl->lun_entry_mutex);
63929a05deeSNicholas Bellinger 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
64029a05deeSNicholas Bellinger 	if (deve)
64129a05deeSNicholas Bellinger 		core_disable_device_list_for_node(lun, deve, nacl, tpg);
64229a05deeSNicholas Bellinger 	mutex_unlock(&nacl->lun_entry_mutex);
643c66ac9dbSNicholas Bellinger 
644f2d30680SHannes Reinecke 	pr_debug("%s_TPG[%hu]_LUN[%llu] - Removed ACL for"
645f2d30680SHannes Reinecke 		" InitiatorNode: %s Mapped LUN: %llu\n",
64630c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
647e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
648b6a54b8dSChris Zankel 		nacl->initiatorname, lacl->mapped_lun);
649c66ac9dbSNicholas Bellinger 
650c66ac9dbSNicholas Bellinger 	return 0;
651c66ac9dbSNicholas Bellinger }
652c66ac9dbSNicholas Bellinger 
core_dev_free_initiator_node_lun_acl(struct se_portal_group * tpg,struct se_lun_acl * lacl)653c66ac9dbSNicholas Bellinger void core_dev_free_initiator_node_lun_acl(
654c66ac9dbSNicholas Bellinger 	struct se_portal_group *tpg,
655c66ac9dbSNicholas Bellinger 	struct se_lun_acl *lacl)
656c66ac9dbSNicholas Bellinger {
6576708bb27SAndy Grover 	pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
65830c7ca93SDavid Disseldorp 		" Mapped LUN: %llu\n", tpg->se_tpg_tfo->fabric_name,
659e3d6f909SAndy Grover 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
66030c7ca93SDavid Disseldorp 		tpg->se_tpg_tfo->fabric_name,
661b6a54b8dSChris Zankel 		lacl->se_lun_nacl->initiatorname, lacl->mapped_lun);
662c66ac9dbSNicholas Bellinger 
663c66ac9dbSNicholas Bellinger 	kfree(lacl);
664c66ac9dbSNicholas Bellinger }
665c66ac9dbSNicholas Bellinger 
scsi_dump_inquiry(struct se_device * dev)6660fd97ccfSChristoph Hellwig static void scsi_dump_inquiry(struct se_device *dev)
6670fd97ccfSChristoph Hellwig {
6680fd97ccfSChristoph Hellwig 	struct t10_wwn *wwn = &dev->t10_wwn;
669b2da4abfSDavid Disseldorp 	int device_type = dev->transport->get_device_type(dev);
670b2da4abfSDavid Disseldorp 
6710fd97ccfSChristoph Hellwig 	/*
6720fd97ccfSChristoph Hellwig 	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
6730fd97ccfSChristoph Hellwig 	 */
674b2da4abfSDavid Disseldorp 	pr_debug("  Vendor: %-" __stringify(INQUIRY_VENDOR_LEN) "s\n",
675b2da4abfSDavid Disseldorp 		wwn->vendor);
676b2da4abfSDavid Disseldorp 	pr_debug("  Model: %-" __stringify(INQUIRY_MODEL_LEN) "s\n",
677b2da4abfSDavid Disseldorp 		wwn->model);
678b2da4abfSDavid Disseldorp 	pr_debug("  Revision: %-" __stringify(INQUIRY_REVISION_LEN) "s\n",
679b2da4abfSDavid Disseldorp 		wwn->revision);
6800fd97ccfSChristoph Hellwig 	pr_debug("  Type:   %s ", scsi_device_type(device_type));
6810fd97ccfSChristoph Hellwig }
6820fd97ccfSChristoph Hellwig 
target_alloc_device(struct se_hba * hba,const char * name)6830fd97ccfSChristoph Hellwig struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
6840fd97ccfSChristoph Hellwig {
6850fd97ccfSChristoph Hellwig 	struct se_device *dev;
6864863e525SNicholas Bellinger 	struct se_lun *xcopy_lun;
6871526d9f1SMike Christie 	int i;
6880fd97ccfSChristoph Hellwig 
6890a06d430SChristoph Hellwig 	dev = hba->backend->ops->alloc_device(hba, name);
6900fd97ccfSChristoph Hellwig 	if (!dev)
6910fd97ccfSChristoph Hellwig 		return NULL;
6920fd97ccfSChristoph Hellwig 
6931526d9f1SMike Christie 	dev->queues = kcalloc(nr_cpu_ids, sizeof(*dev->queues), GFP_KERNEL);
6941526d9f1SMike Christie 	if (!dev->queues) {
6951526d9f1SMike Christie 		dev->transport->free_device(dev);
6961526d9f1SMike Christie 		return NULL;
6971526d9f1SMike Christie 	}
6981526d9f1SMike Christie 
6991526d9f1SMike Christie 	dev->queue_cnt = nr_cpu_ids;
7001526d9f1SMike Christie 	for (i = 0; i < dev->queue_cnt; i++) {
701eb44ce8cSMike Christie 		struct se_device_queue *q;
702eb44ce8cSMike Christie 
703eb44ce8cSMike Christie 		q = &dev->queues[i];
704eb44ce8cSMike Christie 		INIT_LIST_HEAD(&q->state_list);
705eb44ce8cSMike Christie 		spin_lock_init(&q->lock);
706eb44ce8cSMike Christie 
707eb44ce8cSMike Christie 		init_llist_head(&q->sq.cmd_list);
708eb44ce8cSMike Christie 		INIT_WORK(&q->sq.work, target_queued_submit_work);
7091526d9f1SMike Christie 	}
7101526d9f1SMike Christie 
7110fd97ccfSChristoph Hellwig 	dev->se_hba = hba;
7120a06d430SChristoph Hellwig 	dev->transport = hba->backend->ops;
71369088a04SBodo Stroesser 	dev->transport_flags = dev->transport->transport_flags_default;
714fe052a18SSagi Grimberg 	dev->prot_length = sizeof(struct t10_pi_tuple);
7154cc987eaSNicholas Bellinger 	dev->hba_index = hba->hba_index;
7160fd97ccfSChristoph Hellwig 
7170fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_sep_list);
7180fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->dev_tmr_list);
7190fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->delayed_cmd_list);
7200fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->qf_cmd_list);
7210fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->delayed_cmd_lock);
7220fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->dev_reservation_lock);
7230fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_port_lock);
7240fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->se_tmr_lock);
7250fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->qf_cmd_lock);
72668ff9b9bSNicholas Bellinger 	sema_init(&dev->caw_sem, 1);
7270fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
7280fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
7290fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.registration_list);
7300fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
7310fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.registration_lock);
7320fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
7330fd97ccfSChristoph Hellwig 	INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
7340fd97ccfSChristoph Hellwig 	spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
735c66094bfSHannes Reinecke 	INIT_LIST_HEAD(&dev->t10_alua.lba_map_list);
736c66094bfSHannes Reinecke 	spin_lock_init(&dev->t10_alua.lba_map_lock);
7370fd97ccfSChristoph Hellwig 
738ed1227e0SMike Christie 	INIT_WORK(&dev->delayed_cmd_work, target_do_delayed_work);
739673db054SMike Christie 	mutex_init(&dev->lun_reset_mutex);
740ed1227e0SMike Christie 
7410fd97ccfSChristoph Hellwig 	dev->t10_wwn.t10_dev = dev;
7422469f1e0SSergey Samoylenko 	/*
7432469f1e0SSergey Samoylenko 	 * Use OpenFabrics IEEE Company ID: 00 14 05
7442469f1e0SSergey Samoylenko 	 */
7452469f1e0SSergey Samoylenko 	dev->t10_wwn.company_id = 0x001405;
7462469f1e0SSergey Samoylenko 
7470fd97ccfSChristoph Hellwig 	dev->t10_alua.t10_dev = dev;
7480fd97ccfSChristoph Hellwig 
7490fd97ccfSChristoph Hellwig 	dev->dev_attrib.da_dev = dev;
750adfa9570STregaron Bayly 	dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
751814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_dpo = 1;
752814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_write = 1;
753814e5b45SChristoph Hellwig 	dev->dev_attrib.emulate_fua_read = 1;
7540fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
7551bf630fdSDavid Disseldorp 	dev->dev_attrib.emulate_ua_intlck_ctrl = TARGET_UA_INTLCK_CTRL_CLEAR;
7560fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
7570fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
7580fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
7590123a9ecSNicholas Bellinger 	dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
760d397a445SNicholas Bellinger 	dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
761b49d6f78SDavid Disseldorp 	dev->dev_attrib.emulate_pr = DA_EMULATE_PR;
762bd217b8cSDmitry Bogdanov 	dev->dev_attrib.emulate_rsoc = DA_EMULATE_RSOC;
7632ed22c9cSNicholas Bellinger 	dev->dev_attrib.pi_prot_type = TARGET_DIF_TYPE0_PROT;
7640fd97ccfSChristoph Hellwig 	dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
76592404e60SNicholas Bellinger 	dev->dev_attrib.force_pr_aptpl = DA_FORCE_PR_APTPL;
7660fd97ccfSChristoph Hellwig 	dev->dev_attrib.is_nonrot = DA_IS_NONROT;
7670fd97ccfSChristoph Hellwig 	dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
7680fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
7690fd97ccfSChristoph Hellwig 	dev->dev_attrib.max_unmap_block_desc_count =
7700fd97ccfSChristoph Hellwig 		DA_MAX_UNMAP_BLOCK_DESC_COUNT;
7710fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
7720fd97ccfSChristoph Hellwig 	dev->dev_attrib.unmap_granularity_alignment =
7730fd97ccfSChristoph Hellwig 				DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
774e6f41633SJamie Pocas 	dev->dev_attrib.unmap_zeroes_data =
775e6f41633SJamie Pocas 				DA_UNMAP_ZEROES_DATA_DEFAULT;
776773cbaf7SNicholas Bellinger 	dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
7770fd97ccfSChristoph Hellwig 
7784863e525SNicholas Bellinger 	xcopy_lun = &dev->xcopy_lun;
7794cc987eaSNicholas Bellinger 	rcu_assign_pointer(xcopy_lun->lun_se_dev, dev);
780bd4e2d29SNicholas Bellinger 	init_completion(&xcopy_lun->lun_shutdown_comp);
781adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_deve_list);
782adf653f9SChristoph Hellwig 	INIT_LIST_HEAD(&xcopy_lun->lun_dev_link);
783adf653f9SChristoph Hellwig 	mutex_init(&xcopy_lun->lun_tg_pt_md_mutex);
784adf653f9SChristoph Hellwig 	xcopy_lun->lun_tpg = &xcopy_pt_tpg;
7854863e525SNicholas Bellinger 
7863beeabd5SDavid Disseldorp 	/* Preload the default INQUIRY const values */
7870871237aSAzeem Shaikh 	strscpy(dev->t10_wwn.vendor, "LIO-ORG", sizeof(dev->t10_wwn.vendor));
7880871237aSAzeem Shaikh 	strscpy(dev->t10_wwn.model, dev->transport->inquiry_prod,
7893beeabd5SDavid Disseldorp 		sizeof(dev->t10_wwn.model));
7900871237aSAzeem Shaikh 	strscpy(dev->t10_wwn.revision, dev->transport->inquiry_rev,
7913beeabd5SDavid Disseldorp 		sizeof(dev->t10_wwn.revision));
7923beeabd5SDavid Disseldorp 
7930fd97ccfSChristoph Hellwig 	return dev;
7940fd97ccfSChristoph Hellwig }
7950fd97ccfSChristoph Hellwig 
7968a9ebe71SMike Christie /*
797cf0fbf89SChristoph Hellwig  * Check if the underlying struct block_device supports discard and if yes
798cf0fbf89SChristoph Hellwig  * configure the UNMAP parameters.
7998a9ebe71SMike Christie  */
target_configure_unmap_from_queue(struct se_dev_attrib * attrib,struct block_device * bdev)8008a9ebe71SMike Christie bool target_configure_unmap_from_queue(struct se_dev_attrib *attrib,
801817e8b51SChristoph Hellwig 				       struct block_device *bdev)
8028a9ebe71SMike Christie {
803817e8b51SChristoph Hellwig 	int block_size = bdev_logical_block_size(bdev);
804ea263c7fSMike Christie 
80570200574SChristoph Hellwig 	if (!bdev_max_discard_sectors(bdev))
8068a9ebe71SMike Christie 		return false;
8078a9ebe71SMike Christie 
808ea263c7fSMike Christie 	attrib->max_unmap_lba_count =
809cf0fbf89SChristoph Hellwig 		bdev_max_discard_sectors(bdev) >> (ilog2(block_size) - 9);
8108a9ebe71SMike Christie 	/*
8118a9ebe71SMike Christie 	 * Currently hardcoded to 1 in Linux/SCSI code..
8128a9ebe71SMike Christie 	 */
8138a9ebe71SMike Christie 	attrib->max_unmap_block_desc_count = 1;
8147b47ef52SChristoph Hellwig 	attrib->unmap_granularity = bdev_discard_granularity(bdev) / block_size;
815968786b9SChristoph Hellwig 	attrib->unmap_granularity_alignment =
816968786b9SChristoph Hellwig 		bdev_discard_alignment(bdev) / block_size;
8178a9ebe71SMike Christie 	return true;
8188a9ebe71SMike Christie }
8198a9ebe71SMike Christie EXPORT_SYMBOL(target_configure_unmap_from_queue);
8208a9ebe71SMike Christie 
8218a9ebe71SMike Christie /*
8228a9ebe71SMike Christie  * Convert from blocksize advertised to the initiator to the 512 byte
8238a9ebe71SMike Christie  * units unconditionally used by the Linux block layer.
8248a9ebe71SMike Christie  */
target_to_linux_sector(struct se_device * dev,sector_t lb)8258a9ebe71SMike Christie sector_t target_to_linux_sector(struct se_device *dev, sector_t lb)
8268a9ebe71SMike Christie {
8278a9ebe71SMike Christie 	switch (dev->dev_attrib.block_size) {
8288a9ebe71SMike Christie 	case 4096:
8298a9ebe71SMike Christie 		return lb << 3;
8308a9ebe71SMike Christie 	case 2048:
8318a9ebe71SMike Christie 		return lb << 2;
8328a9ebe71SMike Christie 	case 1024:
8338a9ebe71SMike Christie 		return lb << 1;
8348a9ebe71SMike Christie 	default:
8358a9ebe71SMike Christie 		return lb;
8368a9ebe71SMike Christie 	}
8378a9ebe71SMike Christie }
8388a9ebe71SMike Christie EXPORT_SYMBOL(target_to_linux_sector);
8398a9ebe71SMike Christie 
840b1943fd4SMike Christie struct devices_idr_iter {
841b1943fd4SMike Christie 	int (*fn)(struct se_device *dev, void *data);
842b1943fd4SMike Christie 	void *data;
843b1943fd4SMike Christie };
844b1943fd4SMike Christie 
target_devices_idr_iter(int id,void * p,void * data)845b1943fd4SMike Christie static int target_devices_idr_iter(int id, void *p, void *data)
84636d4cb46SBart Van Assche 	 __must_hold(&device_mutex)
847b1943fd4SMike Christie {
848b1943fd4SMike Christie 	struct devices_idr_iter *iter = data;
849b1943fd4SMike Christie 	struct se_device *dev = p;
850*a154f5f6SJunxiao Bi 	struct config_item *item;
85136d4cb46SBart Van Assche 	int ret;
85236d4cb46SBart Van Assche 
853b1943fd4SMike Christie 	/*
854b1943fd4SMike Christie 	 * We add the device early to the idr, so it can be used
855b1943fd4SMike Christie 	 * by backend modules during configuration. We do not want
856b1943fd4SMike Christie 	 * to allow other callers to access partially setup devices,
857b1943fd4SMike Christie 	 * so we skip them here.
858b1943fd4SMike Christie 	 */
859cb0f32e1SMike Christie 	if (!target_dev_configured(dev))
860b1943fd4SMike Christie 		return 0;
861b1943fd4SMike Christie 
862*a154f5f6SJunxiao Bi 	item = config_item_get_unless_zero(&dev->dev_group.cg_item);
863*a154f5f6SJunxiao Bi 	if (!item)
86436d4cb46SBart Van Assche 		return 0;
86536d4cb46SBart Van Assche 	mutex_unlock(&device_mutex);
86636d4cb46SBart Van Assche 
86736d4cb46SBart Van Assche 	ret = iter->fn(dev, iter->data);
868*a154f5f6SJunxiao Bi 	config_item_put(item);
86936d4cb46SBart Van Assche 
87036d4cb46SBart Van Assche 	mutex_lock(&device_mutex);
87136d4cb46SBart Van Assche 	return ret;
872b1943fd4SMike Christie }
873b1943fd4SMike Christie 
874b1943fd4SMike Christie /**
875b1943fd4SMike Christie  * target_for_each_device - iterate over configured devices
876b1943fd4SMike Christie  * @fn: iterator function
877b1943fd4SMike Christie  * @data: pointer to data that will be passed to fn
878b1943fd4SMike Christie  *
879b1943fd4SMike Christie  * fn must return 0 to continue looping over devices. non-zero will break
880b1943fd4SMike Christie  * from the loop and return that value to the caller.
881b1943fd4SMike Christie  */
target_for_each_device(int (* fn)(struct se_device * dev,void * data),void * data)882b1943fd4SMike Christie int target_for_each_device(int (*fn)(struct se_device *dev, void *data),
883b1943fd4SMike Christie 			   void *data)
884b1943fd4SMike Christie {
88536d4cb46SBart Van Assche 	struct devices_idr_iter iter = { .fn = fn, .data = data };
886b1943fd4SMike Christie 	int ret;
887b1943fd4SMike Christie 
888be50f538SMike Christie 	mutex_lock(&device_mutex);
889b1943fd4SMike Christie 	ret = idr_for_each(&devices_idr, target_devices_idr_iter, &iter);
890be50f538SMike Christie 	mutex_unlock(&device_mutex);
891b1943fd4SMike Christie 	return ret;
892b1943fd4SMike Christie }
893b1943fd4SMike Christie 
target_configure_device(struct se_device * dev)8940fd97ccfSChristoph Hellwig int target_configure_device(struct se_device *dev)
8950fd97ccfSChristoph Hellwig {
8960fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
8970a5eee64SMike Christie 	int ret, id;
8980fd97ccfSChristoph Hellwig 
899cb0f32e1SMike Christie 	if (target_dev_configured(dev)) {
9000fd97ccfSChristoph Hellwig 		pr_err("se_dev->se_dev_ptr already set for storage"
9010fd97ccfSChristoph Hellwig 				" object\n");
9020fd97ccfSChristoph Hellwig 		return -EEXIST;
9030fd97ccfSChristoph Hellwig 	}
9040fd97ccfSChristoph Hellwig 
9050a5eee64SMike Christie 	/*
9060a5eee64SMike Christie 	 * Add early so modules like tcmu can use during its
9070a5eee64SMike Christie 	 * configuration.
9080a5eee64SMike Christie 	 */
909be50f538SMike Christie 	mutex_lock(&device_mutex);
9100a5eee64SMike Christie 	/*
9110a5eee64SMike Christie 	 * Use cyclic to try and avoid collisions with devices
9120a5eee64SMike Christie 	 * that were recently removed.
9130a5eee64SMike Christie 	 */
9140a5eee64SMike Christie 	id = idr_alloc_cyclic(&devices_idr, dev, 0, INT_MAX, GFP_KERNEL);
915be50f538SMike Christie 	mutex_unlock(&device_mutex);
9160a5eee64SMike Christie 	if (id < 0) {
9170a5eee64SMike Christie 		ret = -ENOMEM;
9180a5eee64SMike Christie 		goto out;
9190a5eee64SMike Christie 	}
9200a5eee64SMike Christie 	dev->dev_index = id;
9210a5eee64SMike Christie 
9220fd97ccfSChristoph Hellwig 	ret = dev->transport->configure_device(dev);
9230fd97ccfSChristoph Hellwig 	if (ret)
9240a5eee64SMike Christie 		goto out_free_index;
9256b206a5aSMike Christie 
9266b206a5aSMike Christie 	if (dev->transport->configure_unmap &&
9276b206a5aSMike Christie 	    dev->transport->configure_unmap(dev)) {
9286b206a5aSMike Christie 		pr_debug("Discard support available, but disabled by default.\n");
9296b206a5aSMike Christie 	}
9306b206a5aSMike Christie 
9310fd97ccfSChristoph Hellwig 	/*
9320fd97ccfSChristoph Hellwig 	 * XXX: there is not much point to have two different values here..
9330fd97ccfSChristoph Hellwig 	 */
9340fd97ccfSChristoph Hellwig 	dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
9350fd97ccfSChristoph Hellwig 	dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
9360fd97ccfSChristoph Hellwig 
9370fd97ccfSChristoph Hellwig 	/*
9380fd97ccfSChristoph Hellwig 	 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
9390fd97ccfSChristoph Hellwig 	 */
9400fd97ccfSChristoph Hellwig 	dev->dev_attrib.hw_max_sectors =
9410fd97ccfSChristoph Hellwig 		se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
9420fd97ccfSChristoph Hellwig 					 dev->dev_attrib.hw_block_size);
943046ba642SNicholas Bellinger 	dev->dev_attrib.optimal_sectors = dev->dev_attrib.hw_max_sectors;
9440fd97ccfSChristoph Hellwig 
9450fd97ccfSChristoph Hellwig 	dev->creation_time = get_jiffies_64();
9460fd97ccfSChristoph Hellwig 
9470fd97ccfSChristoph Hellwig 	ret = core_setup_alua(dev);
9480fd97ccfSChristoph Hellwig 	if (ret)
949c82b59e7Stangwenji 		goto out_destroy_device;
9500fd97ccfSChristoph Hellwig 
9510fd97ccfSChristoph Hellwig 	/*
9520fd97ccfSChristoph Hellwig 	 * Setup work_queue for QUEUE_FULL
9530fd97ccfSChristoph Hellwig 	 */
9540fd97ccfSChristoph Hellwig 	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
9550fd97ccfSChristoph Hellwig 
9560fd97ccfSChristoph Hellwig 	scsi_dump_inquiry(dev);
9570fd97ccfSChristoph Hellwig 
9580fd97ccfSChristoph Hellwig 	spin_lock(&hba->device_lock);
9590fd97ccfSChristoph Hellwig 	hba->dev_count++;
9600fd97ccfSChristoph Hellwig 	spin_unlock(&hba->device_lock);
961d9ea32bfSNicholas Bellinger 
9625f7da044SNicholas Bellinger 	dev->dev_flags |= DF_CONFIGURED;
9635f7da044SNicholas Bellinger 
9640fd97ccfSChristoph Hellwig 	return 0;
9650fd97ccfSChristoph Hellwig 
966c82b59e7Stangwenji out_destroy_device:
967c82b59e7Stangwenji 	dev->transport->destroy_device(dev);
9680a5eee64SMike Christie out_free_index:
969be50f538SMike Christie 	mutex_lock(&device_mutex);
9700a5eee64SMike Christie 	idr_remove(&devices_idr, dev->dev_index);
971be50f538SMike Christie 	mutex_unlock(&device_mutex);
9720fd97ccfSChristoph Hellwig out:
9730fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
9740fd97ccfSChristoph Hellwig 	return ret;
9750fd97ccfSChristoph Hellwig }
9760fd97ccfSChristoph Hellwig 
target_free_device(struct se_device * dev)9770fd97ccfSChristoph Hellwig void target_free_device(struct se_device *dev)
9780fd97ccfSChristoph Hellwig {
9790fd97ccfSChristoph Hellwig 	struct se_hba *hba = dev->se_hba;
9800fd97ccfSChristoph Hellwig 
9810fd97ccfSChristoph Hellwig 	WARN_ON(!list_empty(&dev->dev_sep_list));
9820fd97ccfSChristoph Hellwig 
983cb0f32e1SMike Christie 	if (target_dev_configured(dev)) {
98492634706SMike Christie 		dev->transport->destroy_device(dev);
98592634706SMike Christie 
986be50f538SMike Christie 		mutex_lock(&device_mutex);
9870a5eee64SMike Christie 		idr_remove(&devices_idr, dev->dev_index);
988be50f538SMike Christie 		mutex_unlock(&device_mutex);
989d9ea32bfSNicholas Bellinger 
9900fd97ccfSChristoph Hellwig 		spin_lock(&hba->device_lock);
9910fd97ccfSChristoph Hellwig 		hba->dev_count--;
9920fd97ccfSChristoph Hellwig 		spin_unlock(&hba->device_lock);
9930fd97ccfSChristoph Hellwig 	}
9940fd97ccfSChristoph Hellwig 
9950fd97ccfSChristoph Hellwig 	core_alua_free_lu_gp_mem(dev);
996229d4f11SHannes Reinecke 	core_alua_set_lba_map(dev, NULL, 0, 0);
9970fd97ccfSChristoph Hellwig 	core_scsi3_free_all_registrations(dev);
9980fd97ccfSChristoph Hellwig 	se_release_vpd_for_dev(dev);
9990fd97ccfSChristoph Hellwig 
10002ed22c9cSNicholas Bellinger 	if (dev->transport->free_prot)
10012ed22c9cSNicholas Bellinger 		dev->transport->free_prot(dev);
10022ed22c9cSNicholas Bellinger 
10031526d9f1SMike Christie 	kfree(dev->queues);
10040fd97ccfSChristoph Hellwig 	dev->transport->free_device(dev);
10050fd97ccfSChristoph Hellwig }
10060fd97ccfSChristoph Hellwig 
core_dev_setup_virtual_lun0(void)1007c66ac9dbSNicholas Bellinger int core_dev_setup_virtual_lun0(void)
1008c66ac9dbSNicholas Bellinger {
1009c66ac9dbSNicholas Bellinger 	struct se_hba *hba;
1010c66ac9dbSNicholas Bellinger 	struct se_device *dev;
10111b5ad814SKonstantin Shelekhin 	char buf[] = "rd_pages=8,rd_nullio=1,rd_dummy=1";
1012c66ac9dbSNicholas Bellinger 	int ret;
1013c66ac9dbSNicholas Bellinger 
10146708bb27SAndy Grover 	hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
1015c66ac9dbSNicholas Bellinger 	if (IS_ERR(hba))
1016c66ac9dbSNicholas Bellinger 		return PTR_ERR(hba);
1017c66ac9dbSNicholas Bellinger 
10180fd97ccfSChristoph Hellwig 	dev = target_alloc_device(hba, "virt_lun0");
10190fd97ccfSChristoph Hellwig 	if (!dev) {
1020c66ac9dbSNicholas Bellinger 		ret = -ENOMEM;
10210fd97ccfSChristoph Hellwig 		goto out_free_hba;
1022c66ac9dbSNicholas Bellinger 	}
1023c66ac9dbSNicholas Bellinger 
10240a06d430SChristoph Hellwig 	hba->backend->ops->set_configfs_dev_params(dev, buf, sizeof(buf));
1025c66ac9dbSNicholas Bellinger 
10260fd97ccfSChristoph Hellwig 	ret = target_configure_device(dev);
10270fd97ccfSChristoph Hellwig 	if (ret)
10280fd97ccfSChristoph Hellwig 		goto out_free_se_dev;
10290fd97ccfSChristoph Hellwig 
10300fd97ccfSChristoph Hellwig 	lun0_hba = hba;
1031e3d6f909SAndy Grover 	g_lun0_dev = dev;
1032c66ac9dbSNicholas Bellinger 	return 0;
10330fd97ccfSChristoph Hellwig 
10340fd97ccfSChristoph Hellwig out_free_se_dev:
10350fd97ccfSChristoph Hellwig 	target_free_device(dev);
10360fd97ccfSChristoph Hellwig out_free_hba:
10370fd97ccfSChristoph Hellwig 	core_delete_hba(hba);
1038c66ac9dbSNicholas Bellinger 	return ret;
1039c66ac9dbSNicholas Bellinger }
1040c66ac9dbSNicholas Bellinger 
1041c66ac9dbSNicholas Bellinger 
core_dev_release_virtual_lun0(void)1042c66ac9dbSNicholas Bellinger void core_dev_release_virtual_lun0(void)
1043c66ac9dbSNicholas Bellinger {
1044e3d6f909SAndy Grover 	struct se_hba *hba = lun0_hba;
1045c66ac9dbSNicholas Bellinger 
10466708bb27SAndy Grover 	if (!hba)
1047c66ac9dbSNicholas Bellinger 		return;
1048c66ac9dbSNicholas Bellinger 
1049e3d6f909SAndy Grover 	if (g_lun0_dev)
10500fd97ccfSChristoph Hellwig 		target_free_device(g_lun0_dev);
1051c66ac9dbSNicholas Bellinger 	core_delete_hba(hba);
1052c66ac9dbSNicholas Bellinger }
10537bfea53bSAndy Grover 
10547bfea53bSAndy Grover /*
10557bfea53bSAndy Grover  * Common CDB parsing for kernel and user passthrough.
10567bfea53bSAndy Grover  */
10577bfea53bSAndy Grover sense_reason_t
passthrough_parse_cdb(struct se_cmd * cmd,sense_reason_t (* exec_cmd)(struct se_cmd * cmd))10587bfea53bSAndy Grover passthrough_parse_cdb(struct se_cmd *cmd,
10597bfea53bSAndy Grover 	sense_reason_t (*exec_cmd)(struct se_cmd *cmd))
10607bfea53bSAndy Grover {
10617bfea53bSAndy Grover 	unsigned char *cdb = cmd->t_task_cdb;
10624ec5bf0eSBryant G. Ly 	struct se_device *dev = cmd->se_dev;
10634ec5bf0eSBryant G. Ly 	unsigned int size;
10647bfea53bSAndy Grover 
10657bfea53bSAndy Grover 	/*
10667bfea53bSAndy Grover 	 * For REPORT LUNS we always need to emulate the response, for everything
10677bfea53bSAndy Grover 	 * else, pass it up.
10687bfea53bSAndy Grover 	 */
10697bfea53bSAndy Grover 	if (cdb[0] == REPORT_LUNS) {
10707bfea53bSAndy Grover 		cmd->execute_cmd = spc_emulate_report_luns;
10717bfea53bSAndy Grover 		return TCM_NO_SENSE;
10727bfea53bSAndy Grover 	}
10737bfea53bSAndy Grover 
10744ec5bf0eSBryant G. Ly 	/*
1075b49d6f78SDavid Disseldorp 	 * With emulate_pr disabled, all reservation requests should fail,
1076b49d6f78SDavid Disseldorp 	 * regardless of whether or not TRANSPORT_FLAG_PASSTHROUGH_PGR is set.
1077b49d6f78SDavid Disseldorp 	 */
1078b49d6f78SDavid Disseldorp 	if (!dev->dev_attrib.emulate_pr &&
1079b49d6f78SDavid Disseldorp 	    ((cdb[0] == PERSISTENT_RESERVE_IN) ||
1080b49d6f78SDavid Disseldorp 	     (cdb[0] == PERSISTENT_RESERVE_OUT) ||
1081b49d6f78SDavid Disseldorp 	     (cdb[0] == RELEASE || cdb[0] == RELEASE_10) ||
1082b49d6f78SDavid Disseldorp 	     (cdb[0] == RESERVE || cdb[0] == RESERVE_10))) {
1083b49d6f78SDavid Disseldorp 		return TCM_UNSUPPORTED_SCSI_OPCODE;
1084b49d6f78SDavid Disseldorp 	}
1085b49d6f78SDavid Disseldorp 
1086b49d6f78SDavid Disseldorp 	/*
10874ec5bf0eSBryant G. Ly 	 * For PERSISTENT RESERVE IN/OUT, RELEASE, and RESERVE we need to
10884ec5bf0eSBryant G. Ly 	 * emulate the response, since tcmu does not have the information
10894ec5bf0eSBryant G. Ly 	 * required to process these commands.
10904ec5bf0eSBryant G. Ly 	 */
109169088a04SBodo Stroesser 	if (!(dev->transport_flags &
10924ec5bf0eSBryant G. Ly 	      TRANSPORT_FLAG_PASSTHROUGH_PGR)) {
10934ec5bf0eSBryant G. Ly 		if (cdb[0] == PERSISTENT_RESERVE_IN) {
10944ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi3_emulate_pr_in;
1095a85d667eSBart Van Assche 			size = get_unaligned_be16(&cdb[7]);
10964ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
10974ec5bf0eSBryant G. Ly 		}
10984ec5bf0eSBryant G. Ly 		if (cdb[0] == PERSISTENT_RESERVE_OUT) {
10994ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi3_emulate_pr_out;
1100388fe699STang Wenji 			size = get_unaligned_be32(&cdb[5]);
11014ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11024ec5bf0eSBryant G. Ly 		}
11034ec5bf0eSBryant G. Ly 
11044ec5bf0eSBryant G. Ly 		if (cdb[0] == RELEASE || cdb[0] == RELEASE_10) {
11054ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi2_reservation_release;
11064ec5bf0eSBryant G. Ly 			if (cdb[0] == RELEASE_10)
1107a85d667eSBart Van Assche 				size = get_unaligned_be16(&cdb[7]);
11084ec5bf0eSBryant G. Ly 			else
11094ec5bf0eSBryant G. Ly 				size = cmd->data_length;
11104ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11114ec5bf0eSBryant G. Ly 		}
11124ec5bf0eSBryant G. Ly 		if (cdb[0] == RESERVE || cdb[0] == RESERVE_10) {
11134ec5bf0eSBryant G. Ly 			cmd->execute_cmd = target_scsi2_reservation_reserve;
11144ec5bf0eSBryant G. Ly 			if (cdb[0] == RESERVE_10)
1115a85d667eSBart Van Assche 				size = get_unaligned_be16(&cdb[7]);
11164ec5bf0eSBryant G. Ly 			else
11174ec5bf0eSBryant G. Ly 				size = cmd->data_length;
11184ec5bf0eSBryant G. Ly 			return target_cmd_size_check(cmd, size);
11194ec5bf0eSBryant G. Ly 		}
11204ec5bf0eSBryant G. Ly 	}
11214ec5bf0eSBryant G. Ly 
11227bfea53bSAndy Grover 	/* Set DATA_CDB flag for ops that should have it */
11237bfea53bSAndy Grover 	switch (cdb[0]) {
11247bfea53bSAndy Grover 	case READ_6:
11257bfea53bSAndy Grover 	case READ_10:
11267bfea53bSAndy Grover 	case READ_12:
11277bfea53bSAndy Grover 	case READ_16:
11287bfea53bSAndy Grover 	case WRITE_6:
11297bfea53bSAndy Grover 	case WRITE_10:
11307bfea53bSAndy Grover 	case WRITE_12:
11317bfea53bSAndy Grover 	case WRITE_16:
11327bfea53bSAndy Grover 	case WRITE_VERIFY:
11337bfea53bSAndy Grover 	case WRITE_VERIFY_12:
11343e182db7SBart Van Assche 	case WRITE_VERIFY_16:
11357bfea53bSAndy Grover 	case COMPARE_AND_WRITE:
11367bfea53bSAndy Grover 	case XDWRITEREAD_10:
11377bfea53bSAndy Grover 		cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
11387bfea53bSAndy Grover 		break;
11397bfea53bSAndy Grover 	case VARIABLE_LENGTH_CMD:
11407bfea53bSAndy Grover 		switch (get_unaligned_be16(&cdb[8])) {
11417bfea53bSAndy Grover 		case READ_32:
11427bfea53bSAndy Grover 		case WRITE_32:
1143e5dc9a70SDamien Le Moal 		case WRITE_VERIFY_32:
11447bfea53bSAndy Grover 		case XDWRITEREAD_32:
11457bfea53bSAndy Grover 			cmd->se_cmd_flags |= SCF_SCSI_DATA_CDB;
11467bfea53bSAndy Grover 			break;
11477bfea53bSAndy Grover 		}
11487bfea53bSAndy Grover 	}
11497bfea53bSAndy Grover 
11507bfea53bSAndy Grover 	cmd->execute_cmd = exec_cmd;
11517bfea53bSAndy Grover 
11527bfea53bSAndy Grover 	return TCM_NO_SENSE;
11537bfea53bSAndy Grover }
11547bfea53bSAndy Grover EXPORT_SYMBOL(passthrough_parse_cdb);
1155