1 /*******************************************************************************
2  * Filename:  target_core_device.c (based on iscsi_target_device.c)
3  *
4  * This file contains the TCM Virtual Device and Disk Transport
5  * agnostic related functions.
6  *
7  * (c) Copyright 2003-2013 Datera, Inc.
8  *
9  * Nicholas A. Bellinger <nab@kernel.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24  *
25  ******************************************************************************/
26 
27 #include <linux/net.h>
28 #include <linux/string.h>
29 #include <linux/delay.h>
30 #include <linux/timer.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/kthread.h>
34 #include <linux/in.h>
35 #include <linux/export.h>
36 #include <net/sock.h>
37 #include <net/tcp.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_device.h>
40 
41 #include <target/target_core_base.h>
42 #include <target/target_core_backend.h>
43 #include <target/target_core_fabric.h>
44 
45 #include "target_core_internal.h"
46 #include "target_core_alua.h"
47 #include "target_core_pr.h"
48 #include "target_core_ua.h"
49 
50 DEFINE_MUTEX(g_device_mutex);
51 LIST_HEAD(g_device_list);
52 
53 static struct se_hba *lun0_hba;
54 /* not static, needed by tpg.c */
55 struct se_device *g_lun0_dev;
56 
57 sense_reason_t
58 transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
59 {
60 	struct se_lun *se_lun = NULL;
61 	struct se_session *se_sess = se_cmd->se_sess;
62 	struct se_device *dev;
63 	unsigned long flags;
64 
65 	if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
66 		return TCM_NON_EXISTENT_LUN;
67 
68 	spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
69 	se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
70 	if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
71 		struct se_dev_entry *deve = se_cmd->se_deve;
72 
73 		deve->total_cmds++;
74 
75 		if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
76 		    (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
77 			pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
78 				" Access for 0x%08x\n",
79 				se_cmd->se_tfo->get_fabric_name(),
80 				unpacked_lun);
81 			spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
82 			return TCM_WRITE_PROTECTED;
83 		}
84 
85 		if (se_cmd->data_direction == DMA_TO_DEVICE)
86 			deve->write_bytes += se_cmd->data_length;
87 		else if (se_cmd->data_direction == DMA_FROM_DEVICE)
88 			deve->read_bytes += se_cmd->data_length;
89 
90 		se_lun = deve->se_lun;
91 		se_cmd->se_lun = deve->se_lun;
92 		se_cmd->pr_res_key = deve->pr_res_key;
93 		se_cmd->orig_fe_lun = unpacked_lun;
94 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
95 	}
96 	spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
97 
98 	if (!se_lun) {
99 		/*
100 		 * Use the se_portal_group->tpg_virt_lun0 to allow for
101 		 * REPORT_LUNS, et al to be returned when no active
102 		 * MappedLUN=0 exists for this Initiator Port.
103 		 */
104 		if (unpacked_lun != 0) {
105 			pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
106 				" Access for 0x%08x\n",
107 				se_cmd->se_tfo->get_fabric_name(),
108 				unpacked_lun);
109 			return TCM_NON_EXISTENT_LUN;
110 		}
111 		/*
112 		 * Force WRITE PROTECT for virtual LUN 0
113 		 */
114 		if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
115 		    (se_cmd->data_direction != DMA_NONE))
116 			return TCM_WRITE_PROTECTED;
117 
118 		se_lun = &se_sess->se_tpg->tpg_virt_lun0;
119 		se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
120 		se_cmd->orig_fe_lun = 0;
121 		se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
122 	}
123 
124 	/* Directly associate cmd with se_dev */
125 	se_cmd->se_dev = se_lun->lun_se_dev;
126 
127 	/* TODO: get rid of this and use atomics for stats */
128 	dev = se_lun->lun_se_dev;
129 	spin_lock_irqsave(&dev->stats_lock, flags);
130 	dev->num_cmds++;
131 	if (se_cmd->data_direction == DMA_TO_DEVICE)
132 		dev->write_bytes += se_cmd->data_length;
133 	else if (se_cmd->data_direction == DMA_FROM_DEVICE)
134 		dev->read_bytes += se_cmd->data_length;
135 	spin_unlock_irqrestore(&dev->stats_lock, flags);
136 
137 	spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
138 	list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
139 	spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
140 
141 	return 0;
142 }
143 EXPORT_SYMBOL(transport_lookup_cmd_lun);
144 
145 int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
146 {
147 	struct se_dev_entry *deve;
148 	struct se_lun *se_lun = NULL;
149 	struct se_session *se_sess = se_cmd->se_sess;
150 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
151 	unsigned long flags;
152 
153 	if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
154 		return -ENODEV;
155 
156 	spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
157 	se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
158 	deve = se_cmd->se_deve;
159 
160 	if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
161 		se_tmr->tmr_lun = deve->se_lun;
162 		se_cmd->se_lun = deve->se_lun;
163 		se_lun = deve->se_lun;
164 		se_cmd->pr_res_key = deve->pr_res_key;
165 		se_cmd->orig_fe_lun = unpacked_lun;
166 	}
167 	spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
168 
169 	if (!se_lun) {
170 		pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
171 			" Access for 0x%08x\n",
172 			se_cmd->se_tfo->get_fabric_name(),
173 			unpacked_lun);
174 		return -ENODEV;
175 	}
176 
177 	/* Directly associate cmd with se_dev */
178 	se_cmd->se_dev = se_lun->lun_se_dev;
179 	se_tmr->tmr_dev = se_lun->lun_se_dev;
180 
181 	spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
182 	list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
183 	spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL(transport_lookup_tmr_lun);
188 
189 /*
190  * This function is called from core_scsi3_emulate_pro_register_and_move()
191  * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
192  * when a matching rtpi is found.
193  */
194 struct se_dev_entry *core_get_se_deve_from_rtpi(
195 	struct se_node_acl *nacl,
196 	u16 rtpi)
197 {
198 	struct se_dev_entry *deve;
199 	struct se_lun *lun;
200 	struct se_port *port;
201 	struct se_portal_group *tpg = nacl->se_tpg;
202 	u32 i;
203 
204 	spin_lock_irq(&nacl->device_list_lock);
205 	for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
206 		deve = nacl->device_list[i];
207 
208 		if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
209 			continue;
210 
211 		lun = deve->se_lun;
212 		if (!lun) {
213 			pr_err("%s device entries device pointer is"
214 				" NULL, but Initiator has access.\n",
215 				tpg->se_tpg_tfo->get_fabric_name());
216 			continue;
217 		}
218 		port = lun->lun_sep;
219 		if (!port) {
220 			pr_err("%s device entries device pointer is"
221 				" NULL, but Initiator has access.\n",
222 				tpg->se_tpg_tfo->get_fabric_name());
223 			continue;
224 		}
225 		if (port->sep_rtpi != rtpi)
226 			continue;
227 
228 		atomic_inc(&deve->pr_ref_count);
229 		smp_mb__after_atomic_inc();
230 		spin_unlock_irq(&nacl->device_list_lock);
231 
232 		return deve;
233 	}
234 	spin_unlock_irq(&nacl->device_list_lock);
235 
236 	return NULL;
237 }
238 
239 int core_free_device_list_for_node(
240 	struct se_node_acl *nacl,
241 	struct se_portal_group *tpg)
242 {
243 	struct se_dev_entry *deve;
244 	struct se_lun *lun;
245 	u32 i;
246 
247 	if (!nacl->device_list)
248 		return 0;
249 
250 	spin_lock_irq(&nacl->device_list_lock);
251 	for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
252 		deve = nacl->device_list[i];
253 
254 		if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
255 			continue;
256 
257 		if (!deve->se_lun) {
258 			pr_err("%s device entries device pointer is"
259 				" NULL, but Initiator has access.\n",
260 				tpg->se_tpg_tfo->get_fabric_name());
261 			continue;
262 		}
263 		lun = deve->se_lun;
264 
265 		spin_unlock_irq(&nacl->device_list_lock);
266 		core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
267 			TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
268 		spin_lock_irq(&nacl->device_list_lock);
269 	}
270 	spin_unlock_irq(&nacl->device_list_lock);
271 
272 	array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
273 	nacl->device_list = NULL;
274 
275 	return 0;
276 }
277 
278 void core_update_device_list_access(
279 	u32 mapped_lun,
280 	u32 lun_access,
281 	struct se_node_acl *nacl)
282 {
283 	struct se_dev_entry *deve;
284 
285 	spin_lock_irq(&nacl->device_list_lock);
286 	deve = nacl->device_list[mapped_lun];
287 	if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
288 		deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
289 		deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
290 	} else {
291 		deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
292 		deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
293 	}
294 	spin_unlock_irq(&nacl->device_list_lock);
295 }
296 
297 /*      core_enable_device_list_for_node():
298  *
299  *
300  */
301 int core_enable_device_list_for_node(
302 	struct se_lun *lun,
303 	struct se_lun_acl *lun_acl,
304 	u32 mapped_lun,
305 	u32 lun_access,
306 	struct se_node_acl *nacl,
307 	struct se_portal_group *tpg)
308 {
309 	struct se_port *port = lun->lun_sep;
310 	struct se_dev_entry *deve;
311 
312 	spin_lock_irq(&nacl->device_list_lock);
313 
314 	deve = nacl->device_list[mapped_lun];
315 
316 	/*
317 	 * Check if the call is handling demo mode -> explict LUN ACL
318 	 * transition.  This transition must be for the same struct se_lun
319 	 * + mapped_lun that was setup in demo mode..
320 	 */
321 	if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
322 		if (deve->se_lun_acl != NULL) {
323 			pr_err("struct se_dev_entry->se_lun_acl"
324 			       " already set for demo mode -> explict"
325 			       " LUN ACL transition\n");
326 			spin_unlock_irq(&nacl->device_list_lock);
327 			return -EINVAL;
328 		}
329 		if (deve->se_lun != lun) {
330 			pr_err("struct se_dev_entry->se_lun does"
331 			       " match passed struct se_lun for demo mode"
332 			       " -> explict LUN ACL transition\n");
333 			spin_unlock_irq(&nacl->device_list_lock);
334 			return -EINVAL;
335 		}
336 		deve->se_lun_acl = lun_acl;
337 
338 		if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
339 			deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
340 			deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
341 		} else {
342 			deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
343 			deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
344 		}
345 
346 		spin_unlock_irq(&nacl->device_list_lock);
347 		return 0;
348 	}
349 
350 	deve->se_lun = lun;
351 	deve->se_lun_acl = lun_acl;
352 	deve->mapped_lun = mapped_lun;
353 	deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
354 
355 	if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
356 		deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
357 		deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
358 	} else {
359 		deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
360 		deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
361 	}
362 
363 	deve->creation_time = get_jiffies_64();
364 	deve->attach_count++;
365 	spin_unlock_irq(&nacl->device_list_lock);
366 
367 	spin_lock_bh(&port->sep_alua_lock);
368 	list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
369 	spin_unlock_bh(&port->sep_alua_lock);
370 
371 	return 0;
372 }
373 
374 /*      core_disable_device_list_for_node():
375  *
376  *
377  */
378 int core_disable_device_list_for_node(
379 	struct se_lun *lun,
380 	struct se_lun_acl *lun_acl,
381 	u32 mapped_lun,
382 	u32 lun_access,
383 	struct se_node_acl *nacl,
384 	struct se_portal_group *tpg)
385 {
386 	struct se_port *port = lun->lun_sep;
387 	struct se_dev_entry *deve = nacl->device_list[mapped_lun];
388 
389 	/*
390 	 * If the MappedLUN entry is being disabled, the entry in
391 	 * port->sep_alua_list must be removed now before clearing the
392 	 * struct se_dev_entry pointers below as logic in
393 	 * core_alua_do_transition_tg_pt() depends on these being present.
394 	 *
395 	 * deve->se_lun_acl will be NULL for demo-mode created LUNs
396 	 * that have not been explicitly converted to MappedLUNs ->
397 	 * struct se_lun_acl, but we remove deve->alua_port_list from
398 	 * port->sep_alua_list. This also means that active UAs and
399 	 * NodeACL context specific PR metadata for demo-mode
400 	 * MappedLUN *deve will be released below..
401 	 */
402 	spin_lock_bh(&port->sep_alua_lock);
403 	list_del(&deve->alua_port_list);
404 	spin_unlock_bh(&port->sep_alua_lock);
405 	/*
406 	 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
407 	 * PR operation to complete.
408 	 */
409 	while (atomic_read(&deve->pr_ref_count) != 0)
410 		cpu_relax();
411 
412 	spin_lock_irq(&nacl->device_list_lock);
413 	/*
414 	 * Disable struct se_dev_entry LUN ACL mapping
415 	 */
416 	core_scsi3_ua_release_all(deve);
417 	deve->se_lun = NULL;
418 	deve->se_lun_acl = NULL;
419 	deve->lun_flags = 0;
420 	deve->creation_time = 0;
421 	deve->attach_count--;
422 	spin_unlock_irq(&nacl->device_list_lock);
423 
424 	core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
425 	return 0;
426 }
427 
428 /*      core_clear_lun_from_tpg():
429  *
430  *
431  */
432 void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
433 {
434 	struct se_node_acl *nacl;
435 	struct se_dev_entry *deve;
436 	u32 i;
437 
438 	spin_lock_irq(&tpg->acl_node_lock);
439 	list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
440 		spin_unlock_irq(&tpg->acl_node_lock);
441 
442 		spin_lock_irq(&nacl->device_list_lock);
443 		for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
444 			deve = nacl->device_list[i];
445 			if (lun != deve->se_lun)
446 				continue;
447 			spin_unlock_irq(&nacl->device_list_lock);
448 
449 			core_disable_device_list_for_node(lun, NULL,
450 				deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
451 				nacl, tpg);
452 
453 			spin_lock_irq(&nacl->device_list_lock);
454 		}
455 		spin_unlock_irq(&nacl->device_list_lock);
456 
457 		spin_lock_irq(&tpg->acl_node_lock);
458 	}
459 	spin_unlock_irq(&tpg->acl_node_lock);
460 }
461 
462 static struct se_port *core_alloc_port(struct se_device *dev)
463 {
464 	struct se_port *port, *port_tmp;
465 
466 	port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
467 	if (!port) {
468 		pr_err("Unable to allocate struct se_port\n");
469 		return ERR_PTR(-ENOMEM);
470 	}
471 	INIT_LIST_HEAD(&port->sep_alua_list);
472 	INIT_LIST_HEAD(&port->sep_list);
473 	atomic_set(&port->sep_tg_pt_secondary_offline, 0);
474 	spin_lock_init(&port->sep_alua_lock);
475 	mutex_init(&port->sep_tg_pt_md_mutex);
476 
477 	spin_lock(&dev->se_port_lock);
478 	if (dev->dev_port_count == 0x0000ffff) {
479 		pr_warn("Reached dev->dev_port_count =="
480 				" 0x0000ffff\n");
481 		spin_unlock(&dev->se_port_lock);
482 		return ERR_PTR(-ENOSPC);
483 	}
484 again:
485 	/*
486 	 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
487 	 * Here is the table from spc4r17 section 7.7.3.8.
488 	 *
489 	 *    Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
490 	 *
491 	 * Code      Description
492 	 * 0h        Reserved
493 	 * 1h        Relative port 1, historically known as port A
494 	 * 2h        Relative port 2, historically known as port B
495 	 * 3h to FFFFh    Relative port 3 through 65 535
496 	 */
497 	port->sep_rtpi = dev->dev_rpti_counter++;
498 	if (!port->sep_rtpi)
499 		goto again;
500 
501 	list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
502 		/*
503 		 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
504 		 * for 16-bit wrap..
505 		 */
506 		if (port->sep_rtpi == port_tmp->sep_rtpi)
507 			goto again;
508 	}
509 	spin_unlock(&dev->se_port_lock);
510 
511 	return port;
512 }
513 
514 static void core_export_port(
515 	struct se_device *dev,
516 	struct se_portal_group *tpg,
517 	struct se_port *port,
518 	struct se_lun *lun)
519 {
520 	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
521 
522 	spin_lock(&dev->se_port_lock);
523 	spin_lock(&lun->lun_sep_lock);
524 	port->sep_tpg = tpg;
525 	port->sep_lun = lun;
526 	lun->lun_sep = port;
527 	spin_unlock(&lun->lun_sep_lock);
528 
529 	list_add_tail(&port->sep_list, &dev->dev_sep_list);
530 	spin_unlock(&dev->se_port_lock);
531 
532 	if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
533 	    !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
534 		tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
535 		if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
536 			pr_err("Unable to allocate t10_alua_tg_pt"
537 					"_gp_member_t\n");
538 			return;
539 		}
540 		spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
541 		__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
542 			dev->t10_alua.default_tg_pt_gp);
543 		spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
544 		pr_debug("%s/%s: Adding to default ALUA Target Port"
545 			" Group: alua/default_tg_pt_gp\n",
546 			dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
547 	}
548 
549 	dev->dev_port_count++;
550 	port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
551 }
552 
553 /*
554  *	Called with struct se_device->se_port_lock spinlock held.
555  */
556 static void core_release_port(struct se_device *dev, struct se_port *port)
557 	__releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
558 {
559 	/*
560 	 * Wait for any port reference for PR ALL_TG_PT=1 operation
561 	 * to complete in __core_scsi3_alloc_registration()
562 	 */
563 	spin_unlock(&dev->se_port_lock);
564 	if (atomic_read(&port->sep_tg_pt_ref_cnt))
565 		cpu_relax();
566 	spin_lock(&dev->se_port_lock);
567 
568 	core_alua_free_tg_pt_gp_mem(port);
569 
570 	list_del(&port->sep_list);
571 	dev->dev_port_count--;
572 	kfree(port);
573 }
574 
575 int core_dev_export(
576 	struct se_device *dev,
577 	struct se_portal_group *tpg,
578 	struct se_lun *lun)
579 {
580 	struct se_hba *hba = dev->se_hba;
581 	struct se_port *port;
582 
583 	port = core_alloc_port(dev);
584 	if (IS_ERR(port))
585 		return PTR_ERR(port);
586 
587 	lun->lun_se_dev = dev;
588 
589 	spin_lock(&hba->device_lock);
590 	dev->export_count++;
591 	spin_unlock(&hba->device_lock);
592 
593 	core_export_port(dev, tpg, port, lun);
594 	return 0;
595 }
596 
597 void core_dev_unexport(
598 	struct se_device *dev,
599 	struct se_portal_group *tpg,
600 	struct se_lun *lun)
601 {
602 	struct se_hba *hba = dev->se_hba;
603 	struct se_port *port = lun->lun_sep;
604 
605 	spin_lock(&lun->lun_sep_lock);
606 	if (lun->lun_se_dev == NULL) {
607 		spin_unlock(&lun->lun_sep_lock);
608 		return;
609 	}
610 	spin_unlock(&lun->lun_sep_lock);
611 
612 	spin_lock(&dev->se_port_lock);
613 	core_release_port(dev, port);
614 	spin_unlock(&dev->se_port_lock);
615 
616 	spin_lock(&hba->device_lock);
617 	dev->export_count--;
618 	spin_unlock(&hba->device_lock);
619 
620 	lun->lun_se_dev = NULL;
621 }
622 
623 static void se_release_vpd_for_dev(struct se_device *dev)
624 {
625 	struct t10_vpd *vpd, *vpd_tmp;
626 
627 	spin_lock(&dev->t10_wwn.t10_vpd_lock);
628 	list_for_each_entry_safe(vpd, vpd_tmp,
629 			&dev->t10_wwn.t10_vpd_list, vpd_list) {
630 		list_del(&vpd->vpd_list);
631 		kfree(vpd);
632 	}
633 	spin_unlock(&dev->t10_wwn.t10_vpd_lock);
634 }
635 
636 static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
637 {
638 	u32 aligned_max_sectors;
639 	u32 alignment;
640 	/*
641 	 * Limit max_sectors to a PAGE_SIZE aligned value for modern
642 	 * transport_allocate_data_tasks() operation.
643 	 */
644 	alignment = max(1ul, PAGE_SIZE / block_size);
645 	aligned_max_sectors = rounddown(max_sectors, alignment);
646 
647 	if (max_sectors != aligned_max_sectors)
648 		pr_info("Rounding down aligned max_sectors from %u to %u\n",
649 			max_sectors, aligned_max_sectors);
650 
651 	return aligned_max_sectors;
652 }
653 
654 int se_dev_set_max_unmap_lba_count(
655 	struct se_device *dev,
656 	u32 max_unmap_lba_count)
657 {
658 	dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
659 	pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
660 			dev, dev->dev_attrib.max_unmap_lba_count);
661 	return 0;
662 }
663 
664 int se_dev_set_max_unmap_block_desc_count(
665 	struct se_device *dev,
666 	u32 max_unmap_block_desc_count)
667 {
668 	dev->dev_attrib.max_unmap_block_desc_count =
669 		max_unmap_block_desc_count;
670 	pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
671 			dev, dev->dev_attrib.max_unmap_block_desc_count);
672 	return 0;
673 }
674 
675 int se_dev_set_unmap_granularity(
676 	struct se_device *dev,
677 	u32 unmap_granularity)
678 {
679 	dev->dev_attrib.unmap_granularity = unmap_granularity;
680 	pr_debug("dev[%p]: Set unmap_granularity: %u\n",
681 			dev, dev->dev_attrib.unmap_granularity);
682 	return 0;
683 }
684 
685 int se_dev_set_unmap_granularity_alignment(
686 	struct se_device *dev,
687 	u32 unmap_granularity_alignment)
688 {
689 	dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
690 	pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
691 			dev, dev->dev_attrib.unmap_granularity_alignment);
692 	return 0;
693 }
694 
695 int se_dev_set_max_write_same_len(
696 	struct se_device *dev,
697 	u32 max_write_same_len)
698 {
699 	dev->dev_attrib.max_write_same_len = max_write_same_len;
700 	pr_debug("dev[%p]: Set max_write_same_len: %u\n",
701 			dev, dev->dev_attrib.max_write_same_len);
702 	return 0;
703 }
704 
705 static void dev_set_t10_wwn_model_alias(struct se_device *dev)
706 {
707 	const char *configname;
708 
709 	configname = config_item_name(&dev->dev_group.cg_item);
710 	if (strlen(configname) >= 16) {
711 		pr_warn("dev[%p]: Backstore name '%s' is too long for "
712 			"INQUIRY_MODEL, truncating to 16 bytes\n", dev,
713 			configname);
714 	}
715 	snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
716 }
717 
718 int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
719 {
720 	if (dev->export_count) {
721 		pr_err("dev[%p]: Unable to change model alias"
722 			" while export_count is %d\n",
723 			dev, dev->export_count);
724 			return -EINVAL;
725 	}
726 
727 	if (flag != 0 && flag != 1) {
728 		pr_err("Illegal value %d\n", flag);
729 		return -EINVAL;
730 	}
731 
732 	if (flag) {
733 		dev_set_t10_wwn_model_alias(dev);
734 	} else {
735 		strncpy(&dev->t10_wwn.model[0],
736 			dev->transport->inquiry_prod, 16);
737 	}
738 	dev->dev_attrib.emulate_model_alias = flag;
739 
740 	return 0;
741 }
742 
743 int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
744 {
745 	if (flag != 0 && flag != 1) {
746 		pr_err("Illegal value %d\n", flag);
747 		return -EINVAL;
748 	}
749 
750 	if (flag) {
751 		pr_err("dpo_emulated not supported\n");
752 		return -EINVAL;
753 	}
754 
755 	return 0;
756 }
757 
758 int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
759 {
760 	if (flag != 0 && flag != 1) {
761 		pr_err("Illegal value %d\n", flag);
762 		return -EINVAL;
763 	}
764 
765 	if (flag &&
766 	    dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
767 		pr_err("emulate_fua_write not supported for pSCSI\n");
768 		return -EINVAL;
769 	}
770 	dev->dev_attrib.emulate_fua_write = flag;
771 	pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
772 			dev, dev->dev_attrib.emulate_fua_write);
773 	return 0;
774 }
775 
776 int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
777 {
778 	if (flag != 0 && flag != 1) {
779 		pr_err("Illegal value %d\n", flag);
780 		return -EINVAL;
781 	}
782 
783 	if (flag) {
784 		pr_err("ua read emulated not supported\n");
785 		return -EINVAL;
786 	}
787 
788 	return 0;
789 }
790 
791 int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
792 {
793 	if (flag != 0 && flag != 1) {
794 		pr_err("Illegal value %d\n", flag);
795 		return -EINVAL;
796 	}
797 	if (flag &&
798 	    dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
799 		pr_err("emulate_write_cache not supported for pSCSI\n");
800 		return -EINVAL;
801 	}
802 	if (dev->transport->get_write_cache) {
803 		pr_warn("emulate_write_cache cannot be changed when underlying"
804 			" HW reports WriteCacheEnabled, ignoring request\n");
805 		return 0;
806 	}
807 
808 	dev->dev_attrib.emulate_write_cache = flag;
809 	pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
810 			dev, dev->dev_attrib.emulate_write_cache);
811 	return 0;
812 }
813 
814 int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
815 {
816 	if ((flag != 0) && (flag != 1) && (flag != 2)) {
817 		pr_err("Illegal value %d\n", flag);
818 		return -EINVAL;
819 	}
820 
821 	if (dev->export_count) {
822 		pr_err("dev[%p]: Unable to change SE Device"
823 			" UA_INTRLCK_CTRL while export_count is %d\n",
824 			dev, dev->export_count);
825 		return -EINVAL;
826 	}
827 	dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
828 	pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
829 		dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
830 
831 	return 0;
832 }
833 
834 int se_dev_set_emulate_tas(struct se_device *dev, int flag)
835 {
836 	if ((flag != 0) && (flag != 1)) {
837 		pr_err("Illegal value %d\n", flag);
838 		return -EINVAL;
839 	}
840 
841 	if (dev->export_count) {
842 		pr_err("dev[%p]: Unable to change SE Device TAS while"
843 			" export_count is %d\n",
844 			dev, dev->export_count);
845 		return -EINVAL;
846 	}
847 	dev->dev_attrib.emulate_tas = flag;
848 	pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
849 		dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
850 
851 	return 0;
852 }
853 
854 int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
855 {
856 	if ((flag != 0) && (flag != 1)) {
857 		pr_err("Illegal value %d\n", flag);
858 		return -EINVAL;
859 	}
860 	/*
861 	 * We expect this value to be non-zero when generic Block Layer
862 	 * Discard supported is detected iblock_create_virtdevice().
863 	 */
864 	if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
865 		pr_err("Generic Block Discard not supported\n");
866 		return -ENOSYS;
867 	}
868 
869 	dev->dev_attrib.emulate_tpu = flag;
870 	pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
871 				dev, flag);
872 	return 0;
873 }
874 
875 int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
876 {
877 	if ((flag != 0) && (flag != 1)) {
878 		pr_err("Illegal value %d\n", flag);
879 		return -EINVAL;
880 	}
881 	/*
882 	 * We expect this value to be non-zero when generic Block Layer
883 	 * Discard supported is detected iblock_create_virtdevice().
884 	 */
885 	if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
886 		pr_err("Generic Block Discard not supported\n");
887 		return -ENOSYS;
888 	}
889 
890 	dev->dev_attrib.emulate_tpws = flag;
891 	pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
892 				dev, flag);
893 	return 0;
894 }
895 
896 int se_dev_set_emulate_caw(struct se_device *dev, int flag)
897 {
898 	if (flag != 0 && flag != 1) {
899 		pr_err("Illegal value %d\n", flag);
900 		return -EINVAL;
901 	}
902 	dev->dev_attrib.emulate_caw = flag;
903 	pr_debug("dev[%p]: SE Device CompareAndWrite (AtomicTestandSet): %d\n",
904 		 dev, flag);
905 
906 	return 0;
907 }
908 
909 int se_dev_set_emulate_3pc(struct se_device *dev, int flag)
910 {
911 	if (flag != 0 && flag != 1) {
912 		pr_err("Illegal value %d\n", flag);
913 		return -EINVAL;
914 	}
915 	dev->dev_attrib.emulate_3pc = flag;
916 	pr_debug("dev[%p]: SE Device 3rd Party Copy (EXTENDED_COPY): %d\n",
917 		dev, flag);
918 
919 	return 0;
920 }
921 
922 int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
923 {
924 	if ((flag != 0) && (flag != 1)) {
925 		pr_err("Illegal value %d\n", flag);
926 		return -EINVAL;
927 	}
928 	dev->dev_attrib.enforce_pr_isids = flag;
929 	pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
930 		(dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
931 	return 0;
932 }
933 
934 int se_dev_set_is_nonrot(struct se_device *dev, int flag)
935 {
936 	if ((flag != 0) && (flag != 1)) {
937 		printk(KERN_ERR "Illegal value %d\n", flag);
938 		return -EINVAL;
939 	}
940 	dev->dev_attrib.is_nonrot = flag;
941 	pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
942 	       dev, flag);
943 	return 0;
944 }
945 
946 int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
947 {
948 	if (flag != 0) {
949 		printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
950 			" reordering not implemented\n", dev);
951 		return -ENOSYS;
952 	}
953 	dev->dev_attrib.emulate_rest_reord = flag;
954 	pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
955 	return 0;
956 }
957 
958 /*
959  * Note, this can only be called on unexported SE Device Object.
960  */
961 int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
962 {
963 	if (dev->export_count) {
964 		pr_err("dev[%p]: Unable to change SE Device TCQ while"
965 			" export_count is %d\n",
966 			dev, dev->export_count);
967 		return -EINVAL;
968 	}
969 	if (!queue_depth) {
970 		pr_err("dev[%p]: Illegal ZERO value for queue"
971 			"_depth\n", dev);
972 		return -EINVAL;
973 	}
974 
975 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
976 		if (queue_depth > dev->dev_attrib.hw_queue_depth) {
977 			pr_err("dev[%p]: Passed queue_depth: %u"
978 				" exceeds TCM/SE_Device TCQ: %u\n",
979 				dev, queue_depth,
980 				dev->dev_attrib.hw_queue_depth);
981 			return -EINVAL;
982 		}
983 	} else {
984 		if (queue_depth > dev->dev_attrib.queue_depth) {
985 			if (queue_depth > dev->dev_attrib.hw_queue_depth) {
986 				pr_err("dev[%p]: Passed queue_depth:"
987 					" %u exceeds TCM/SE_Device MAX"
988 					" TCQ: %u\n", dev, queue_depth,
989 					dev->dev_attrib.hw_queue_depth);
990 				return -EINVAL;
991 			}
992 		}
993 	}
994 
995 	dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
996 	pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
997 			dev, queue_depth);
998 	return 0;
999 }
1000 
1001 int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
1002 {
1003 	int block_size = dev->dev_attrib.block_size;
1004 
1005 	if (dev->export_count) {
1006 		pr_err("dev[%p]: Unable to change SE Device"
1007 			" fabric_max_sectors while export_count is %d\n",
1008 			dev, dev->export_count);
1009 		return -EINVAL;
1010 	}
1011 	if (!fabric_max_sectors) {
1012 		pr_err("dev[%p]: Illegal ZERO value for"
1013 			" fabric_max_sectors\n", dev);
1014 		return -EINVAL;
1015 	}
1016 	if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
1017 		pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
1018 			" DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
1019 				DA_STATUS_MAX_SECTORS_MIN);
1020 		return -EINVAL;
1021 	}
1022 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1023 		if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
1024 			pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1025 				" greater than TCM/SE_Device max_sectors:"
1026 				" %u\n", dev, fabric_max_sectors,
1027 				dev->dev_attrib.hw_max_sectors);
1028 			 return -EINVAL;
1029 		}
1030 	} else {
1031 		if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
1032 			pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1033 				" greater than DA_STATUS_MAX_SECTORS_MAX:"
1034 				" %u\n", dev, fabric_max_sectors,
1035 				DA_STATUS_MAX_SECTORS_MAX);
1036 			return -EINVAL;
1037 		}
1038 	}
1039 	/*
1040 	 * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
1041 	 */
1042 	if (!block_size) {
1043 		block_size = 512;
1044 		pr_warn("Defaulting to 512 for zero block_size\n");
1045 	}
1046 	fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
1047 						      block_size);
1048 
1049 	dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
1050 	pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
1051 			dev, fabric_max_sectors);
1052 	return 0;
1053 }
1054 
1055 int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1056 {
1057 	if (dev->export_count) {
1058 		pr_err("dev[%p]: Unable to change SE Device"
1059 			" optimal_sectors while export_count is %d\n",
1060 			dev, dev->export_count);
1061 		return -EINVAL;
1062 	}
1063 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1064 		pr_err("dev[%p]: Passed optimal_sectors cannot be"
1065 				" changed for TCM/pSCSI\n", dev);
1066 		return -EINVAL;
1067 	}
1068 	if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
1069 		pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
1070 			" greater than fabric_max_sectors: %u\n", dev,
1071 			optimal_sectors, dev->dev_attrib.fabric_max_sectors);
1072 		return -EINVAL;
1073 	}
1074 
1075 	dev->dev_attrib.optimal_sectors = optimal_sectors;
1076 	pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
1077 			dev, optimal_sectors);
1078 	return 0;
1079 }
1080 
1081 int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1082 {
1083 	if (dev->export_count) {
1084 		pr_err("dev[%p]: Unable to change SE Device block_size"
1085 			" while export_count is %d\n",
1086 			dev, dev->export_count);
1087 		return -EINVAL;
1088 	}
1089 
1090 	if ((block_size != 512) &&
1091 	    (block_size != 1024) &&
1092 	    (block_size != 2048) &&
1093 	    (block_size != 4096)) {
1094 		pr_err("dev[%p]: Illegal value for block_device: %u"
1095 			" for SE device, must be 512, 1024, 2048 or 4096\n",
1096 			dev, block_size);
1097 		return -EINVAL;
1098 	}
1099 
1100 	if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1101 		pr_err("dev[%p]: Not allowed to change block_size for"
1102 			" Physical Device, use for Linux/SCSI to change"
1103 			" block_size for underlying hardware\n", dev);
1104 		return -EINVAL;
1105 	}
1106 
1107 	dev->dev_attrib.block_size = block_size;
1108 	pr_debug("dev[%p]: SE Device block_size changed to %u\n",
1109 			dev, block_size);
1110 	return 0;
1111 }
1112 
1113 struct se_lun *core_dev_add_lun(
1114 	struct se_portal_group *tpg,
1115 	struct se_device *dev,
1116 	u32 lun)
1117 {
1118 	struct se_lun *lun_p;
1119 	int rc;
1120 
1121 	lun_p = core_tpg_pre_addlun(tpg, lun);
1122 	if (IS_ERR(lun_p))
1123 		return lun_p;
1124 
1125 	rc = core_tpg_post_addlun(tpg, lun_p,
1126 				TRANSPORT_LUNFLAGS_READ_WRITE, dev);
1127 	if (rc < 0)
1128 		return ERR_PTR(rc);
1129 
1130 	pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
1131 		" CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1132 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
1133 		tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
1134 	/*
1135 	 * Update LUN maps for dynamically added initiators when
1136 	 * generate_node_acl is enabled.
1137 	 */
1138 	if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
1139 		struct se_node_acl *acl;
1140 		spin_lock_irq(&tpg->acl_node_lock);
1141 		list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
1142 			if (acl->dynamic_node_acl &&
1143 			    (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
1144 			     !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
1145 				spin_unlock_irq(&tpg->acl_node_lock);
1146 				core_tpg_add_node_to_devs(acl, tpg);
1147 				spin_lock_irq(&tpg->acl_node_lock);
1148 			}
1149 		}
1150 		spin_unlock_irq(&tpg->acl_node_lock);
1151 	}
1152 
1153 	return lun_p;
1154 }
1155 
1156 /*      core_dev_del_lun():
1157  *
1158  *
1159  */
1160 int core_dev_del_lun(
1161 	struct se_portal_group *tpg,
1162 	u32 unpacked_lun)
1163 {
1164 	struct se_lun *lun;
1165 
1166 	lun = core_tpg_pre_dellun(tpg, unpacked_lun);
1167 	if (IS_ERR(lun))
1168 		return PTR_ERR(lun);
1169 
1170 	core_tpg_post_dellun(tpg, lun);
1171 
1172 	pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
1173 		" device object\n", tpg->se_tpg_tfo->get_fabric_name(),
1174 		tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
1175 		tpg->se_tpg_tfo->get_fabric_name());
1176 
1177 	return 0;
1178 }
1179 
1180 struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
1181 {
1182 	struct se_lun *lun;
1183 
1184 	spin_lock(&tpg->tpg_lun_lock);
1185 	if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
1186 		pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
1187 			"_PER_TPG-1: %u for Target Portal Group: %hu\n",
1188 			tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1189 			TRANSPORT_MAX_LUNS_PER_TPG-1,
1190 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
1191 		spin_unlock(&tpg->tpg_lun_lock);
1192 		return NULL;
1193 	}
1194 	lun = tpg->tpg_lun_list[unpacked_lun];
1195 
1196 	if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
1197 		pr_err("%s Logical Unit Number: %u is not free on"
1198 			" Target Portal Group: %hu, ignoring request.\n",
1199 			tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1200 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
1201 		spin_unlock(&tpg->tpg_lun_lock);
1202 		return NULL;
1203 	}
1204 	spin_unlock(&tpg->tpg_lun_lock);
1205 
1206 	return lun;
1207 }
1208 
1209 /*      core_dev_get_lun():
1210  *
1211  *
1212  */
1213 static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
1214 {
1215 	struct se_lun *lun;
1216 
1217 	spin_lock(&tpg->tpg_lun_lock);
1218 	if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
1219 		pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
1220 			"_TPG-1: %u for Target Portal Group: %hu\n",
1221 			tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1222 			TRANSPORT_MAX_LUNS_PER_TPG-1,
1223 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
1224 		spin_unlock(&tpg->tpg_lun_lock);
1225 		return NULL;
1226 	}
1227 	lun = tpg->tpg_lun_list[unpacked_lun];
1228 
1229 	if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
1230 		pr_err("%s Logical Unit Number: %u is not active on"
1231 			" Target Portal Group: %hu, ignoring request.\n",
1232 			tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1233 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
1234 		spin_unlock(&tpg->tpg_lun_lock);
1235 		return NULL;
1236 	}
1237 	spin_unlock(&tpg->tpg_lun_lock);
1238 
1239 	return lun;
1240 }
1241 
1242 struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1243 	struct se_portal_group *tpg,
1244 	struct se_node_acl *nacl,
1245 	u32 mapped_lun,
1246 	int *ret)
1247 {
1248 	struct se_lun_acl *lacl;
1249 
1250 	if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
1251 		pr_err("%s InitiatorName exceeds maximum size.\n",
1252 			tpg->se_tpg_tfo->get_fabric_name());
1253 		*ret = -EOVERFLOW;
1254 		return NULL;
1255 	}
1256 	lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
1257 	if (!lacl) {
1258 		pr_err("Unable to allocate memory for struct se_lun_acl.\n");
1259 		*ret = -ENOMEM;
1260 		return NULL;
1261 	}
1262 
1263 	INIT_LIST_HEAD(&lacl->lacl_list);
1264 	lacl->mapped_lun = mapped_lun;
1265 	lacl->se_lun_nacl = nacl;
1266 	snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
1267 		 nacl->initiatorname);
1268 
1269 	return lacl;
1270 }
1271 
1272 int core_dev_add_initiator_node_lun_acl(
1273 	struct se_portal_group *tpg,
1274 	struct se_lun_acl *lacl,
1275 	u32 unpacked_lun,
1276 	u32 lun_access)
1277 {
1278 	struct se_lun *lun;
1279 	struct se_node_acl *nacl;
1280 
1281 	lun = core_dev_get_lun(tpg, unpacked_lun);
1282 	if (!lun) {
1283 		pr_err("%s Logical Unit Number: %u is not active on"
1284 			" Target Portal Group: %hu, ignoring request.\n",
1285 			tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1286 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
1287 		return -EINVAL;
1288 	}
1289 
1290 	nacl = lacl->se_lun_nacl;
1291 	if (!nacl)
1292 		return -EINVAL;
1293 
1294 	if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
1295 	    (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
1296 		lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1297 
1298 	lacl->se_lun = lun;
1299 
1300 	if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
1301 			lun_access, nacl, tpg) < 0)
1302 		return -EINVAL;
1303 
1304 	spin_lock(&lun->lun_acl_lock);
1305 	list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1306 	atomic_inc(&lun->lun_acl_count);
1307 	smp_mb__after_atomic_inc();
1308 	spin_unlock(&lun->lun_acl_lock);
1309 
1310 	pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
1311 		" InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
1312 		tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
1313 		(lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
1314 		lacl->initiatorname);
1315 	/*
1316 	 * Check to see if there are any existing persistent reservation APTPL
1317 	 * pre-registrations that need to be enabled for this LUN ACL..
1318 	 */
1319 	core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
1320 	return 0;
1321 }
1322 
1323 /*      core_dev_del_initiator_node_lun_acl():
1324  *
1325  *
1326  */
1327 int core_dev_del_initiator_node_lun_acl(
1328 	struct se_portal_group *tpg,
1329 	struct se_lun *lun,
1330 	struct se_lun_acl *lacl)
1331 {
1332 	struct se_node_acl *nacl;
1333 
1334 	nacl = lacl->se_lun_nacl;
1335 	if (!nacl)
1336 		return -EINVAL;
1337 
1338 	spin_lock(&lun->lun_acl_lock);
1339 	list_del(&lacl->lacl_list);
1340 	atomic_dec(&lun->lun_acl_count);
1341 	smp_mb__after_atomic_dec();
1342 	spin_unlock(&lun->lun_acl_lock);
1343 
1344 	core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
1345 		TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
1346 
1347 	lacl->se_lun = NULL;
1348 
1349 	pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
1350 		" InitiatorNode: %s Mapped LUN: %u\n",
1351 		tpg->se_tpg_tfo->get_fabric_name(),
1352 		tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
1353 		lacl->initiatorname, lacl->mapped_lun);
1354 
1355 	return 0;
1356 }
1357 
1358 void core_dev_free_initiator_node_lun_acl(
1359 	struct se_portal_group *tpg,
1360 	struct se_lun_acl *lacl)
1361 {
1362 	pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
1363 		" Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1364 		tpg->se_tpg_tfo->tpg_get_tag(tpg),
1365 		tpg->se_tpg_tfo->get_fabric_name(),
1366 		lacl->initiatorname, lacl->mapped_lun);
1367 
1368 	kfree(lacl);
1369 }
1370 
1371 static void scsi_dump_inquiry(struct se_device *dev)
1372 {
1373 	struct t10_wwn *wwn = &dev->t10_wwn;
1374 	char buf[17];
1375 	int i, device_type;
1376 	/*
1377 	 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1378 	 */
1379 	for (i = 0; i < 8; i++)
1380 		if (wwn->vendor[i] >= 0x20)
1381 			buf[i] = wwn->vendor[i];
1382 		else
1383 			buf[i] = ' ';
1384 	buf[i] = '\0';
1385 	pr_debug("  Vendor: %s\n", buf);
1386 
1387 	for (i = 0; i < 16; i++)
1388 		if (wwn->model[i] >= 0x20)
1389 			buf[i] = wwn->model[i];
1390 		else
1391 			buf[i] = ' ';
1392 	buf[i] = '\0';
1393 	pr_debug("  Model: %s\n", buf);
1394 
1395 	for (i = 0; i < 4; i++)
1396 		if (wwn->revision[i] >= 0x20)
1397 			buf[i] = wwn->revision[i];
1398 		else
1399 			buf[i] = ' ';
1400 	buf[i] = '\0';
1401 	pr_debug("  Revision: %s\n", buf);
1402 
1403 	device_type = dev->transport->get_device_type(dev);
1404 	pr_debug("  Type:   %s ", scsi_device_type(device_type));
1405 }
1406 
1407 struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
1408 {
1409 	struct se_device *dev;
1410 
1411 	dev = hba->transport->alloc_device(hba, name);
1412 	if (!dev)
1413 		return NULL;
1414 
1415 	dev->dev_link_magic = SE_DEV_LINK_MAGIC;
1416 	dev->se_hba = hba;
1417 	dev->transport = hba->transport;
1418 
1419 	INIT_LIST_HEAD(&dev->dev_list);
1420 	INIT_LIST_HEAD(&dev->dev_sep_list);
1421 	INIT_LIST_HEAD(&dev->dev_tmr_list);
1422 	INIT_LIST_HEAD(&dev->delayed_cmd_list);
1423 	INIT_LIST_HEAD(&dev->state_list);
1424 	INIT_LIST_HEAD(&dev->qf_cmd_list);
1425 	INIT_LIST_HEAD(&dev->g_dev_node);
1426 	spin_lock_init(&dev->stats_lock);
1427 	spin_lock_init(&dev->execute_task_lock);
1428 	spin_lock_init(&dev->delayed_cmd_lock);
1429 	spin_lock_init(&dev->dev_reservation_lock);
1430 	spin_lock_init(&dev->se_port_lock);
1431 	spin_lock_init(&dev->se_tmr_lock);
1432 	spin_lock_init(&dev->qf_cmd_lock);
1433 	sema_init(&dev->caw_sem, 1);
1434 	atomic_set(&dev->dev_ordered_id, 0);
1435 	INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
1436 	spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
1437 	INIT_LIST_HEAD(&dev->t10_pr.registration_list);
1438 	INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
1439 	spin_lock_init(&dev->t10_pr.registration_lock);
1440 	spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
1441 	INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
1442 	spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
1443 
1444 	dev->t10_wwn.t10_dev = dev;
1445 	dev->t10_alua.t10_dev = dev;
1446 
1447 	dev->dev_attrib.da_dev = dev;
1448 	dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
1449 	dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
1450 	dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
1451 	dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
1452 	dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
1453 	dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
1454 	dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
1455 	dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
1456 	dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
1457 	dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
1458 	dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
1459 	dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
1460 	dev->dev_attrib.is_nonrot = DA_IS_NONROT;
1461 	dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
1462 	dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
1463 	dev->dev_attrib.max_unmap_block_desc_count =
1464 		DA_MAX_UNMAP_BLOCK_DESC_COUNT;
1465 	dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
1466 	dev->dev_attrib.unmap_granularity_alignment =
1467 				DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
1468 	dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
1469 	dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
1470 	dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
1471 
1472 	return dev;
1473 }
1474 
1475 int target_configure_device(struct se_device *dev)
1476 {
1477 	struct se_hba *hba = dev->se_hba;
1478 	int ret;
1479 
1480 	if (dev->dev_flags & DF_CONFIGURED) {
1481 		pr_err("se_dev->se_dev_ptr already set for storage"
1482 				" object\n");
1483 		return -EEXIST;
1484 	}
1485 
1486 	ret = dev->transport->configure_device(dev);
1487 	if (ret)
1488 		goto out;
1489 	dev->dev_flags |= DF_CONFIGURED;
1490 
1491 	/*
1492 	 * XXX: there is not much point to have two different values here..
1493 	 */
1494 	dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
1495 	dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
1496 
1497 	/*
1498 	 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
1499 	 */
1500 	dev->dev_attrib.hw_max_sectors =
1501 		se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
1502 					 dev->dev_attrib.hw_block_size);
1503 
1504 	dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1505 	dev->creation_time = get_jiffies_64();
1506 
1507 	ret = core_setup_alua(dev);
1508 	if (ret)
1509 		goto out;
1510 
1511 	/*
1512 	 * Startup the struct se_device processing thread
1513 	 */
1514 	dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
1515 				      dev->transport->name);
1516 	if (!dev->tmr_wq) {
1517 		pr_err("Unable to create tmr workqueue for %s\n",
1518 			dev->transport->name);
1519 		ret = -ENOMEM;
1520 		goto out_free_alua;
1521 	}
1522 
1523 	/*
1524 	 * Setup work_queue for QUEUE_FULL
1525 	 */
1526 	INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1527 
1528 	/*
1529 	 * Preload the initial INQUIRY const values if we are doing
1530 	 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1531 	 * passthrough because this is being provided by the backend LLD.
1532 	 */
1533 	if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1534 		strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1535 		strncpy(&dev->t10_wwn.model[0],
1536 			dev->transport->inquiry_prod, 16);
1537 		strncpy(&dev->t10_wwn.revision[0],
1538 			dev->transport->inquiry_rev, 4);
1539 	}
1540 
1541 	scsi_dump_inquiry(dev);
1542 
1543 	spin_lock(&hba->device_lock);
1544 	hba->dev_count++;
1545 	spin_unlock(&hba->device_lock);
1546 
1547 	mutex_lock(&g_device_mutex);
1548 	list_add_tail(&dev->g_dev_node, &g_device_list);
1549 	mutex_unlock(&g_device_mutex);
1550 
1551 	return 0;
1552 
1553 out_free_alua:
1554 	core_alua_free_lu_gp_mem(dev);
1555 out:
1556 	se_release_vpd_for_dev(dev);
1557 	return ret;
1558 }
1559 
1560 void target_free_device(struct se_device *dev)
1561 {
1562 	struct se_hba *hba = dev->se_hba;
1563 
1564 	WARN_ON(!list_empty(&dev->dev_sep_list));
1565 
1566 	if (dev->dev_flags & DF_CONFIGURED) {
1567 		destroy_workqueue(dev->tmr_wq);
1568 
1569 		mutex_lock(&g_device_mutex);
1570 		list_del(&dev->g_dev_node);
1571 		mutex_unlock(&g_device_mutex);
1572 
1573 		spin_lock(&hba->device_lock);
1574 		hba->dev_count--;
1575 		spin_unlock(&hba->device_lock);
1576 	}
1577 
1578 	core_alua_free_lu_gp_mem(dev);
1579 	core_scsi3_free_all_registrations(dev);
1580 	se_release_vpd_for_dev(dev);
1581 
1582 	dev->transport->free_device(dev);
1583 }
1584 
1585 int core_dev_setup_virtual_lun0(void)
1586 {
1587 	struct se_hba *hba;
1588 	struct se_device *dev;
1589 	char buf[] = "rd_pages=8,rd_nullio=1";
1590 	int ret;
1591 
1592 	hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
1593 	if (IS_ERR(hba))
1594 		return PTR_ERR(hba);
1595 
1596 	dev = target_alloc_device(hba, "virt_lun0");
1597 	if (!dev) {
1598 		ret = -ENOMEM;
1599 		goto out_free_hba;
1600 	}
1601 
1602 	hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
1603 
1604 	ret = target_configure_device(dev);
1605 	if (ret)
1606 		goto out_free_se_dev;
1607 
1608 	lun0_hba = hba;
1609 	g_lun0_dev = dev;
1610 	return 0;
1611 
1612 out_free_se_dev:
1613 	target_free_device(dev);
1614 out_free_hba:
1615 	core_delete_hba(hba);
1616 	return ret;
1617 }
1618 
1619 
1620 void core_dev_release_virtual_lun0(void)
1621 {
1622 	struct se_hba *hba = lun0_hba;
1623 
1624 	if (!hba)
1625 		return;
1626 
1627 	if (g_lun0_dev)
1628 		target_free_device(g_lun0_dev);
1629 	core_delete_hba(hba);
1630 }
1631