]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/target/target_core_device.c
target: Update copyright ownership/year information to 2013
[mirror_ubuntu-bionic-kernel.git] / drivers / target / target_core_device.c
CommitLineData
c66ac9db
NB
1/*******************************************************************************
2 * Filename: target_core_device.c (based on iscsi_target_device.c)
3 *
e3d6f909 4 * This file contains the TCM Virtual Device and Disk Transport
c66ac9db
NB
5 * agnostic related functions.
6 *
4c76251e 7 * (c) Copyright 2003-2013 Datera, Inc.
c66ac9db
NB
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>
c66ac9db
NB
33#include <linux/kthread.h>
34#include <linux/in.h>
c53181af 35#include <linux/export.h>
c66ac9db
NB
36#include <net/sock.h>
37#include <net/tcp.h>
38#include <scsi/scsi.h>
1078da16 39#include <scsi/scsi_device.h>
c66ac9db
NB
40
41#include <target/target_core_base.h>
c4795fb2
CH
42#include <target/target_core_backend.h>
43#include <target/target_core_fabric.h>
c66ac9db 44
e26d99ae 45#include "target_core_internal.h"
c66ac9db 46#include "target_core_alua.h"
c66ac9db
NB
47#include "target_core_pr.h"
48#include "target_core_ua.h"
49
d9ea32bf
NB
50DEFINE_MUTEX(g_device_mutex);
51LIST_HEAD(g_device_list);
52
e3d6f909 53static struct se_hba *lun0_hba;
e3d6f909
AG
54/* not static, needed by tpg.c */
55struct se_device *g_lun0_dev;
56
de103c93
CH
57sense_reason_t
58transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
c66ac9db 59{
c66ac9db 60 struct se_lun *se_lun = NULL;
e3d6f909 61 struct se_session *se_sess = se_cmd->se_sess;
5951146d 62 struct se_device *dev;
c66ac9db 63 unsigned long flags;
c66ac9db 64
de103c93
CH
65 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
66 return TCM_NON_EXISTENT_LUN;
d8144955 67
78faae37 68 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
f2083241 69 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
5951146d
AG
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++;
5951146d
AG
74
75 if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
76 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
6708bb27 77 pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
5951146d
AG
78 " Access for 0x%08x\n",
79 se_cmd->se_tfo->get_fabric_name(),
80 unpacked_lun);
78faae37 81 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
de103c93 82 return TCM_WRITE_PROTECTED;
c66ac9db 83 }
5951146d
AG
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
5951146d
AG
90 se_lun = deve->se_lun;
91 se_cmd->se_lun = deve->se_lun;
c66ac9db
NB
92 se_cmd->pr_res_key = deve->pr_res_key;
93 se_cmd->orig_fe_lun = unpacked_lun;
c66ac9db
NB
94 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
95 }
78faae37 96 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
c66ac9db
NB
97
98 if (!se_lun) {
5951146d
AG
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) {
6708bb27 105 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
c66ac9db 106 " Access for 0x%08x\n",
e3d6f909 107 se_cmd->se_tfo->get_fabric_name(),
c66ac9db 108 unpacked_lun);
de103c93 109 return TCM_NON_EXISTENT_LUN;
5951146d
AG
110 }
111 /*
112 * Force WRITE PROTECT for virtual LUN 0
113 */
114 if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
de103c93
CH
115 (se_cmd->data_direction != DMA_NONE))
116 return TCM_WRITE_PROTECTED;
5951146d
AG
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;
5951146d 121 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
c66ac9db 122 }
c66ac9db 123
5951146d
AG
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;
78faae37 129 spin_lock_irqsave(&dev->stats_lock, flags);
c66ac9db
NB
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;
78faae37 135 spin_unlock_irqrestore(&dev->stats_lock, flags);
c66ac9db 136
c66ac9db 137 spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
5951146d 138 list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
c66ac9db
NB
139 spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
140
141 return 0;
142}
5951146d 143EXPORT_SYMBOL(transport_lookup_cmd_lun);
c66ac9db 144
5951146d 145int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
c66ac9db 146{
c66ac9db
NB
147 struct se_dev_entry *deve;
148 struct se_lun *se_lun = NULL;
e3d6f909 149 struct se_session *se_sess = se_cmd->se_sess;
c66ac9db 150 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
5e1be919 151 unsigned long flags;
c66ac9db 152
de103c93 153 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
e3d6f909 154 return -ENODEV;
d8144955 155
5e1be919 156 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
f2083241 157 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
5951146d
AG
158 deve = se_cmd->se_deve;
159
c66ac9db 160 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
5951146d
AG
161 se_tmr->tmr_lun = deve->se_lun;
162 se_cmd->se_lun = deve->se_lun;
163 se_lun = deve->se_lun;
c66ac9db
NB
164 se_cmd->pr_res_key = deve->pr_res_key;
165 se_cmd->orig_fe_lun = unpacked_lun;
c66ac9db 166 }
5e1be919 167 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
c66ac9db
NB
168
169 if (!se_lun) {
6708bb27 170 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
c66ac9db 171 " Access for 0x%08x\n",
e3d6f909 172 se_cmd->se_tfo->get_fabric_name(),
c66ac9db 173 unpacked_lun);
e3d6f909 174 return -ENODEV;
c66ac9db 175 }
c66ac9db 176
5951146d
AG
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
5e1be919 181 spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
5951146d 182 list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
5e1be919 183 spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
c66ac9db
NB
184
185 return 0;
186}
5951146d 187EXPORT_SYMBOL(transport_lookup_tmr_lun);
c66ac9db
NB
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 */
194struct 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++) {
f2083241 206 deve = nacl->device_list[i];
c66ac9db
NB
207
208 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
209 continue;
210
211 lun = deve->se_lun;
6708bb27
AG
212 if (!lun) {
213 pr_err("%s device entries device pointer is"
c66ac9db 214 " NULL, but Initiator has access.\n",
e3d6f909 215 tpg->se_tpg_tfo->get_fabric_name());
c66ac9db
NB
216 continue;
217 }
218 port = lun->lun_sep;
6708bb27
AG
219 if (!port) {
220 pr_err("%s device entries device pointer is"
c66ac9db 221 " NULL, but Initiator has access.\n",
e3d6f909 222 tpg->se_tpg_tfo->get_fabric_name());
c66ac9db
NB
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
239int 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++) {
f2083241 252 deve = nacl->device_list[i];
c66ac9db
NB
253
254 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
255 continue;
256
257 if (!deve->se_lun) {
6708bb27 258 pr_err("%s device entries device pointer is"
c66ac9db 259 " NULL, but Initiator has access.\n",
e3d6f909 260 tpg->se_tpg_tfo->get_fabric_name());
c66ac9db
NB
261 continue;
262 }
263 lun = deve->se_lun;
264
265 spin_unlock_irq(&nacl->device_list_lock);
e80ac6c4
AG
266 core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
267 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
c66ac9db
NB
268 spin_lock_irq(&nacl->device_list_lock);
269 }
270 spin_unlock_irq(&nacl->device_list_lock);
271
f2083241 272 array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
c66ac9db
NB
273 nacl->device_list = NULL;
274
275 return 0;
276}
277
c66ac9db
NB
278void 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);
f2083241 286 deve = nacl->device_list[mapped_lun];
c66ac9db
NB
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);
c66ac9db
NB
295}
296
e80ac6c4 297/* core_enable_device_list_for_node():
c66ac9db
NB
298 *
299 *
300 */
e80ac6c4 301int core_enable_device_list_for_node(
c66ac9db
NB
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,
e80ac6c4 307 struct se_portal_group *tpg)
c66ac9db
NB
308{
309 struct se_port *port = lun->lun_sep;
e80ac6c4 310 struct se_dev_entry *deve;
c66ac9db
NB
311
312 spin_lock_irq(&nacl->device_list_lock);
e80ac6c4
AG
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;
c66ac9db 335 }
e80ac6c4 336 deve->se_lun_acl = lun_acl;
c66ac9db
NB
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
c66ac9db 346 spin_unlock_irq(&nacl->device_list_lock);
e80ac6c4
AG
347 return 0;
348 }
c66ac9db 349
e80ac6c4
AG
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;
c66ac9db 354
e80ac6c4
AG
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;
c66ac9db 361 }
e80ac6c4
AG
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 */
378int 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;
77d4c745 387 struct se_dev_entry *deve = nacl->device_list[mapped_lun];
e80ac6c4
AG
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);
c66ac9db
NB
405 /*
406 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
407 * PR operation to complete.
408 */
c66ac9db
NB
409 while (atomic_read(&deve->pr_ref_count) != 0)
410 cpu_relax();
77d4c745 411
c66ac9db
NB
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 */
432void 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
28638887 438 spin_lock_irq(&tpg->acl_node_lock);
c66ac9db 439 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
28638887 440 spin_unlock_irq(&tpg->acl_node_lock);
c66ac9db
NB
441
442 spin_lock_irq(&nacl->device_list_lock);
443 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
f2083241 444 deve = nacl->device_list[i];
c66ac9db
NB
445 if (lun != deve->se_lun)
446 continue;
447 spin_unlock_irq(&nacl->device_list_lock);
448
e80ac6c4 449 core_disable_device_list_for_node(lun, NULL,
c66ac9db 450 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
e80ac6c4 451 nacl, tpg);
c66ac9db
NB
452
453 spin_lock_irq(&nacl->device_list_lock);
454 }
455 spin_unlock_irq(&nacl->device_list_lock);
456
28638887 457 spin_lock_irq(&tpg->acl_node_lock);
c66ac9db 458 }
28638887 459 spin_unlock_irq(&tpg->acl_node_lock);
c66ac9db
NB
460}
461
462static 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);
6708bb27
AG
467 if (!port) {
468 pr_err("Unable to allocate struct se_port\n");
e3d6f909 469 return ERR_PTR(-ENOMEM);
c66ac9db
NB
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) {
6708bb27 479 pr_warn("Reached dev->dev_port_count =="
c66ac9db
NB
480 " 0x0000ffff\n");
481 spin_unlock(&dev->se_port_lock);
e3d6f909 482 return ERR_PTR(-ENOSPC);
c66ac9db
NB
483 }
484again:
485 /*
35d1efe8 486 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
c66ac9db
NB
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++;
6708bb27 498 if (!port->sep_rtpi)
c66ac9db
NB
499 goto again;
500
501 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
502 /*
35d1efe8 503 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
c66ac9db
NB
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
514static 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{
c66ac9db
NB
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
c87fbd56
CH
532 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
533 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
c66ac9db
NB
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) {
6708bb27 536 pr_err("Unable to allocate t10_alua_tg_pt"
c66ac9db
NB
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,
0fd97ccf 542 dev->t10_alua.default_tg_pt_gp);
c66ac9db 543 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
6708bb27 544 pr_debug("%s/%s: Adding to default ALUA Target Port"
c66ac9db 545 " Group: alua/default_tg_pt_gp\n",
e3d6f909 546 dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
c66ac9db
NB
547 }
548
549 dev->dev_port_count++;
35d1efe8 550 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
c66ac9db
NB
551}
552
553/*
554 * Called with struct se_device->se_port_lock spinlock held.
555 */
556static void core_release_port(struct se_device *dev, struct se_port *port)
5dd7ed2e 557 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
c66ac9db
NB
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);
c66ac9db
NB
573}
574
575int core_dev_export(
576 struct se_device *dev,
577 struct se_portal_group *tpg,
578 struct se_lun *lun)
579{
0fd97ccf 580 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
581 struct se_port *port;
582
583 port = core_alloc_port(dev);
e3d6f909
AG
584 if (IS_ERR(port))
585 return PTR_ERR(port);
c66ac9db
NB
586
587 lun->lun_se_dev = dev;
c66ac9db 588
0fd97ccf
CH
589 spin_lock(&hba->device_lock);
590 dev->export_count++;
591 spin_unlock(&hba->device_lock);
592
c66ac9db
NB
593 core_export_port(dev, tpg, port, lun);
594 return 0;
595}
596
597void core_dev_unexport(
598 struct se_device *dev,
599 struct se_portal_group *tpg,
600 struct se_lun *lun)
601{
0fd97ccf 602 struct se_hba *hba = dev->se_hba;
c66ac9db
NB
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);
c66ac9db
NB
613 core_release_port(dev, port);
614 spin_unlock(&dev->se_port_lock);
615
0fd97ccf
CH
616 spin_lock(&hba->device_lock);
617 dev->export_count--;
618 spin_unlock(&hba->device_lock);
619
c66ac9db
NB
620 lun->lun_se_dev = NULL;
621}
622
0fd97ccf 623static void se_release_vpd_for_dev(struct se_device *dev)
c66ac9db
NB
624{
625 struct t10_vpd *vpd, *vpd_tmp;
626
0fd97ccf 627 spin_lock(&dev->t10_wwn.t10_vpd_lock);
c66ac9db 628 list_for_each_entry_safe(vpd, vpd_tmp,
0fd97ccf 629 &dev->t10_wwn.t10_vpd_list, vpd_list) {
c66ac9db
NB
630 list_del(&vpd->vpd_list);
631 kfree(vpd);
632 }
0fd97ccf 633 spin_unlock(&dev->t10_wwn.t10_vpd_lock);
c66ac9db
NB
634}
635
c8045372 636static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
525a48a2 637{
3e03989b
RD
638 u32 aligned_max_sectors;
639 u32 alignment;
525a48a2
NB
640 /*
641 * Limit max_sectors to a PAGE_SIZE aligned value for modern
642 * transport_allocate_data_tasks() operation.
643 */
3e03989b
RD
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);
525a48a2 650
3e03989b 651 return aligned_max_sectors;
525a48a2
NB
652}
653
c66ac9db
NB
654int se_dev_set_max_unmap_lba_count(
655 struct se_device *dev,
656 u32 max_unmap_lba_count)
657{
0fd97ccf 658 dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
6708bb27 659 pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
0fd97ccf 660 dev, dev->dev_attrib.max_unmap_lba_count);
c66ac9db
NB
661 return 0;
662}
663
664int se_dev_set_max_unmap_block_desc_count(
665 struct se_device *dev,
666 u32 max_unmap_block_desc_count)
667{
0fd97ccf 668 dev->dev_attrib.max_unmap_block_desc_count =
e3d6f909 669 max_unmap_block_desc_count;
6708bb27 670 pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
0fd97ccf 671 dev, dev->dev_attrib.max_unmap_block_desc_count);
c66ac9db
NB
672 return 0;
673}
674
675int se_dev_set_unmap_granularity(
676 struct se_device *dev,
677 u32 unmap_granularity)
678{
0fd97ccf 679 dev->dev_attrib.unmap_granularity = unmap_granularity;
6708bb27 680 pr_debug("dev[%p]: Set unmap_granularity: %u\n",
0fd97ccf 681 dev, dev->dev_attrib.unmap_granularity);
c66ac9db
NB
682 return 0;
683}
684
685int se_dev_set_unmap_granularity_alignment(
686 struct se_device *dev,
687 u32 unmap_granularity_alignment)
688{
0fd97ccf 689 dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
6708bb27 690 pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
0fd97ccf 691 dev, dev->dev_attrib.unmap_granularity_alignment);
c66ac9db
NB
692 return 0;
693}
694
773cbaf7
NB
695int 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
adfa9570
TB
705static 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
718int 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
c66ac9db
NB
743int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
744{
f55918fa 745 if (flag != 0 && flag != 1) {
6708bb27 746 pr_err("Illegal value %d\n", flag);
e3d6f909 747 return -EINVAL;
c66ac9db 748 }
f55918fa 749
c638830d
AG
750 if (flag) {
751 pr_err("dpo_emulated not supported\n");
752 return -EINVAL;
753 }
754
755 return 0;
c66ac9db
NB
756}
757
758int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
759{
f55918fa 760 if (flag != 0 && flag != 1) {
6708bb27 761 pr_err("Illegal value %d\n", flag);
e3d6f909 762 return -EINVAL;
c66ac9db 763 }
f55918fa 764
fd30e931
NB
765 if (flag &&
766 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
767 pr_err("emulate_fua_write not supported for pSCSI\n");
e3d6f909 768 return -EINVAL;
c66ac9db 769 }
0fd97ccf 770 dev->dev_attrib.emulate_fua_write = flag;
6708bb27 771 pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
0fd97ccf 772 dev, dev->dev_attrib.emulate_fua_write);
c66ac9db
NB
773 return 0;
774}
775
776int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
777{
f55918fa 778 if (flag != 0 && flag != 1) {
6708bb27 779 pr_err("Illegal value %d\n", flag);
e3d6f909 780 return -EINVAL;
c66ac9db 781 }
f55918fa 782
c638830d
AG
783 if (flag) {
784 pr_err("ua read emulated not supported\n");
785 return -EINVAL;
786 }
787
788 return 0;
c66ac9db
NB
789}
790
791int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
792{
f55918fa 793 if (flag != 0 && flag != 1) {
6708bb27 794 pr_err("Illegal value %d\n", flag);
e3d6f909 795 return -EINVAL;
c66ac9db 796 }
fd30e931
NB
797 if (flag &&
798 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
799 pr_err("emulate_write_cache not supported for pSCSI\n");
e3d6f909 800 return -EINVAL;
c66ac9db 801 }
d0c8b259
NB
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
0fd97ccf 808 dev->dev_attrib.emulate_write_cache = flag;
6708bb27 809 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
0fd97ccf 810 dev, dev->dev_attrib.emulate_write_cache);
c66ac9db
NB
811 return 0;
812}
813
814int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
815{
816 if ((flag != 0) && (flag != 1) && (flag != 2)) {
6708bb27 817 pr_err("Illegal value %d\n", flag);
e3d6f909 818 return -EINVAL;
c66ac9db
NB
819 }
820
0fd97ccf 821 if (dev->export_count) {
6708bb27 822 pr_err("dev[%p]: Unable to change SE Device"
0fd97ccf
CH
823 " UA_INTRLCK_CTRL while export_count is %d\n",
824 dev, dev->export_count);
e3d6f909 825 return -EINVAL;
c66ac9db 826 }
0fd97ccf 827 dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
6708bb27 828 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
0fd97ccf 829 dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
c66ac9db
NB
830
831 return 0;
832}
833
834int se_dev_set_emulate_tas(struct se_device *dev, int flag)
835{
836 if ((flag != 0) && (flag != 1)) {
6708bb27 837 pr_err("Illegal value %d\n", flag);
e3d6f909 838 return -EINVAL;
c66ac9db
NB
839 }
840
0fd97ccf 841 if (dev->export_count) {
6708bb27 842 pr_err("dev[%p]: Unable to change SE Device TAS while"
0fd97ccf
CH
843 " export_count is %d\n",
844 dev, dev->export_count);
e3d6f909 845 return -EINVAL;
c66ac9db 846 }
0fd97ccf 847 dev->dev_attrib.emulate_tas = flag;
6708bb27 848 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
0fd97ccf 849 dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
c66ac9db
NB
850
851 return 0;
852}
853
854int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
855{
856 if ((flag != 0) && (flag != 1)) {
6708bb27 857 pr_err("Illegal value %d\n", flag);
e3d6f909 858 return -EINVAL;
c66ac9db
NB
859 }
860 /*
861 * We expect this value to be non-zero when generic Block Layer
862 * Discard supported is detected iblock_create_virtdevice().
863 */
0fd97ccf 864 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
6708bb27 865 pr_err("Generic Block Discard not supported\n");
c66ac9db
NB
866 return -ENOSYS;
867 }
868
0fd97ccf 869 dev->dev_attrib.emulate_tpu = flag;
6708bb27 870 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
c66ac9db
NB
871 dev, flag);
872 return 0;
873}
874
875int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
876{
877 if ((flag != 0) && (flag != 1)) {
6708bb27 878 pr_err("Illegal value %d\n", flag);
e3d6f909 879 return -EINVAL;
c66ac9db
NB
880 }
881 /*
882 * We expect this value to be non-zero when generic Block Layer
883 * Discard supported is detected iblock_create_virtdevice().
884 */
0fd97ccf 885 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
6708bb27 886 pr_err("Generic Block Discard not supported\n");
c66ac9db
NB
887 return -ENOSYS;
888 }
889
0fd97ccf 890 dev->dev_attrib.emulate_tpws = flag;
6708bb27 891 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
c66ac9db
NB
892 dev, flag);
893 return 0;
894}
895
0123a9ec
NB
896int 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
d397a445
NB
909int 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
c66ac9db
NB
922int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
923{
924 if ((flag != 0) && (flag != 1)) {
6708bb27 925 pr_err("Illegal value %d\n", flag);
e3d6f909 926 return -EINVAL;
c66ac9db 927 }
0fd97ccf 928 dev->dev_attrib.enforce_pr_isids = flag;
6708bb27 929 pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
0fd97ccf 930 (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
c66ac9db
NB
931 return 0;
932}
933
e22a7f07
RD
934int 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 }
0fd97ccf 940 dev->dev_attrib.is_nonrot = flag;
5de619a3 941 pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
e22a7f07
RD
942 dev, flag);
943 return 0;
944}
945
5de619a3
NB
946int 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 }
0fd97ccf 953 dev->dev_attrib.emulate_rest_reord = flag;
5de619a3
NB
954 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
955 return 0;
956}
957
c66ac9db
NB
958/*
959 * Note, this can only be called on unexported SE Device Object.
960 */
961int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
962{
0fd97ccf 963 if (dev->export_count) {
6708bb27 964 pr_err("dev[%p]: Unable to change SE Device TCQ while"
0fd97ccf
CH
965 " export_count is %d\n",
966 dev, dev->export_count);
e3d6f909 967 return -EINVAL;
c66ac9db 968 }
6708bb27
AG
969 if (!queue_depth) {
970 pr_err("dev[%p]: Illegal ZERO value for queue"
c66ac9db 971 "_depth\n", dev);
e3d6f909 972 return -EINVAL;
c66ac9db
NB
973 }
974
e3d6f909 975 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
0fd97ccf 976 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
6708bb27 977 pr_err("dev[%p]: Passed queue_depth: %u"
c66ac9db
NB
978 " exceeds TCM/SE_Device TCQ: %u\n",
979 dev, queue_depth,
0fd97ccf 980 dev->dev_attrib.hw_queue_depth);
e3d6f909 981 return -EINVAL;
c66ac9db
NB
982 }
983 } else {
0fd97ccf
CH
984 if (queue_depth > dev->dev_attrib.queue_depth) {
985 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
6708bb27 986 pr_err("dev[%p]: Passed queue_depth:"
c66ac9db
NB
987 " %u exceeds TCM/SE_Device MAX"
988 " TCQ: %u\n", dev, queue_depth,
0fd97ccf 989 dev->dev_attrib.hw_queue_depth);
e3d6f909 990 return -EINVAL;
c66ac9db
NB
991 }
992 }
993 }
994
0fd97ccf 995 dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
6708bb27 996 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
c66ac9db
NB
997 dev, queue_depth);
998 return 0;
999}
1000
015487b8
RD
1001int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
1002{
7a3cf6ca
NB
1003 int block_size = dev->dev_attrib.block_size;
1004
0fd97ccf 1005 if (dev->export_count) {
015487b8 1006 pr_err("dev[%p]: Unable to change SE Device"
0fd97ccf
CH
1007 " fabric_max_sectors while export_count is %d\n",
1008 dev, dev->export_count);
015487b8
RD
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) {
0fd97ccf 1023 if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
015487b8
RD
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,
0fd97ccf 1027 dev->dev_attrib.hw_max_sectors);
015487b8
RD
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 */
7a3cf6ca
NB
1042 if (!block_size) {
1043 block_size = 512;
1044 pr_warn("Defaulting to 512 for zero block_size\n");
1045 }
015487b8 1046 fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
7a3cf6ca 1047 block_size);
015487b8 1048
0fd97ccf 1049 dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
015487b8
RD
1050 pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
1051 dev, fabric_max_sectors);
1052 return 0;
1053}
1054
c66ac9db
NB
1055int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1056{
0fd97ccf 1057 if (dev->export_count) {
6708bb27 1058 pr_err("dev[%p]: Unable to change SE Device"
0fd97ccf
CH
1059 " optimal_sectors while export_count is %d\n",
1060 dev, dev->export_count);
c66ac9db
NB
1061 return -EINVAL;
1062 }
e3d6f909 1063 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
6708bb27 1064 pr_err("dev[%p]: Passed optimal_sectors cannot be"
c66ac9db
NB
1065 " changed for TCM/pSCSI\n", dev);
1066 return -EINVAL;
1067 }
0fd97ccf 1068 if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
6708bb27 1069 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
015487b8 1070 " greater than fabric_max_sectors: %u\n", dev,
0fd97ccf 1071 optimal_sectors, dev->dev_attrib.fabric_max_sectors);
c66ac9db
NB
1072 return -EINVAL;
1073 }
1074
0fd97ccf 1075 dev->dev_attrib.optimal_sectors = optimal_sectors;
6708bb27 1076 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
c66ac9db
NB
1077 dev, optimal_sectors);
1078 return 0;
1079}
1080
1081int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1082{
0fd97ccf 1083 if (dev->export_count) {
6708bb27 1084 pr_err("dev[%p]: Unable to change SE Device block_size"
0fd97ccf
CH
1085 " while export_count is %d\n",
1086 dev, dev->export_count);
e3d6f909 1087 return -EINVAL;
c66ac9db
NB
1088 }
1089
1090 if ((block_size != 512) &&
1091 (block_size != 1024) &&
1092 (block_size != 2048) &&
1093 (block_size != 4096)) {
6708bb27 1094 pr_err("dev[%p]: Illegal value for block_device: %u"
c66ac9db
NB
1095 " for SE device, must be 512, 1024, 2048 or 4096\n",
1096 dev, block_size);
e3d6f909 1097 return -EINVAL;
c66ac9db
NB
1098 }
1099
e3d6f909 1100 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
6708bb27 1101 pr_err("dev[%p]: Not allowed to change block_size for"
c66ac9db
NB
1102 " Physical Device, use for Linux/SCSI to change"
1103 " block_size for underlying hardware\n", dev);
e3d6f909 1104 return -EINVAL;
c66ac9db
NB
1105 }
1106
0fd97ccf 1107 dev->dev_attrib.block_size = block_size;
6708bb27 1108 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
c66ac9db
NB
1109 dev, block_size);
1110 return 0;
1111}
1112
1113struct se_lun *core_dev_add_lun(
1114 struct se_portal_group *tpg,
c66ac9db
NB
1115 struct se_device *dev,
1116 u32 lun)
1117{
1118 struct se_lun *lun_p;
8d9efe53 1119 int rc;
c66ac9db 1120
c66ac9db 1121 lun_p = core_tpg_pre_addlun(tpg, lun);
8d9efe53
SAS
1122 if (IS_ERR(lun_p))
1123 return lun_p;
c66ac9db 1124
58d92618
NB
1125 rc = core_tpg_post_addlun(tpg, lun_p,
1126 TRANSPORT_LUNFLAGS_READ_WRITE, dev);
8d9efe53
SAS
1127 if (rc < 0)
1128 return ERR_PTR(rc);
c66ac9db 1129
6708bb27 1130 pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
e3d6f909
AG
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,
2dca673b 1133 tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
c66ac9db
NB
1134 /*
1135 * Update LUN maps for dynamically added initiators when
1136 * generate_node_acl is enabled.
1137 */
e3d6f909 1138 if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
c66ac9db 1139 struct se_node_acl *acl;
28638887 1140 spin_lock_irq(&tpg->acl_node_lock);
c66ac9db 1141 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
052605c6
NB
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))) {
28638887 1145 spin_unlock_irq(&tpg->acl_node_lock);
c66ac9db 1146 core_tpg_add_node_to_devs(acl, tpg);
28638887 1147 spin_lock_irq(&tpg->acl_node_lock);
c66ac9db
NB
1148 }
1149 }
28638887 1150 spin_unlock_irq(&tpg->acl_node_lock);
c66ac9db
NB
1151 }
1152
1153 return lun_p;
1154}
1155
1156/* core_dev_del_lun():
1157 *
1158 *
1159 */
1160int core_dev_del_lun(
1161 struct se_portal_group *tpg,
1162 u32 unpacked_lun)
1163{
1164 struct se_lun *lun;
c66ac9db 1165
8d9efe53
SAS
1166 lun = core_tpg_pre_dellun(tpg, unpacked_lun);
1167 if (IS_ERR(lun))
1168 return PTR_ERR(lun);
c66ac9db
NB
1169
1170 core_tpg_post_dellun(tpg, lun);
1171
6708bb27 1172 pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
e3d6f909
AG
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());
c66ac9db
NB
1176
1177 return 0;
1178}
1179
1180struct 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)) {
6708bb27 1186 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
c66ac9db 1187 "_PER_TPG-1: %u for Target Portal Group: %hu\n",
e3d6f909 1188 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
c66ac9db 1189 TRANSPORT_MAX_LUNS_PER_TPG-1,
e3d6f909 1190 tpg->se_tpg_tfo->tpg_get_tag(tpg));
c66ac9db
NB
1191 spin_unlock(&tpg->tpg_lun_lock);
1192 return NULL;
1193 }
4a5a75f3 1194 lun = tpg->tpg_lun_list[unpacked_lun];
c66ac9db
NB
1195
1196 if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
6708bb27 1197 pr_err("%s Logical Unit Number: %u is not free on"
c66ac9db 1198 " Target Portal Group: %hu, ignoring request.\n",
e3d6f909
AG
1199 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1200 tpg->se_tpg_tfo->tpg_get_tag(tpg));
c66ac9db
NB
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 */
1213static 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)) {
6708bb27 1219 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
c66ac9db 1220 "_TPG-1: %u for Target Portal Group: %hu\n",
e3d6f909 1221 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
c66ac9db 1222 TRANSPORT_MAX_LUNS_PER_TPG-1,
e3d6f909 1223 tpg->se_tpg_tfo->tpg_get_tag(tpg));
c66ac9db
NB
1224 spin_unlock(&tpg->tpg_lun_lock);
1225 return NULL;
1226 }
4a5a75f3 1227 lun = tpg->tpg_lun_list[unpacked_lun];
c66ac9db
NB
1228
1229 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
6708bb27 1230 pr_err("%s Logical Unit Number: %u is not active on"
c66ac9db 1231 " Target Portal Group: %hu, ignoring request.\n",
e3d6f909
AG
1232 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1233 tpg->se_tpg_tfo->tpg_get_tag(tpg));
c66ac9db
NB
1234 spin_unlock(&tpg->tpg_lun_lock);
1235 return NULL;
1236 }
1237 spin_unlock(&tpg->tpg_lun_lock);
1238
1239 return lun;
1240}
1241
1242struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1243 struct se_portal_group *tpg,
fcf29481 1244 struct se_node_acl *nacl,
c66ac9db 1245 u32 mapped_lun,
c66ac9db
NB
1246 int *ret)
1247{
1248 struct se_lun_acl *lacl;
c66ac9db 1249
fcf29481 1250 if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
6708bb27 1251 pr_err("%s InitiatorName exceeds maximum size.\n",
e3d6f909 1252 tpg->se_tpg_tfo->get_fabric_name());
c66ac9db
NB
1253 *ret = -EOVERFLOW;
1254 return NULL;
1255 }
c66ac9db 1256 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
6708bb27
AG
1257 if (!lacl) {
1258 pr_err("Unable to allocate memory for struct se_lun_acl.\n");
c66ac9db
NB
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;
fcf29481
NB
1266 snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
1267 nacl->initiatorname);
c66ac9db
NB
1268
1269 return lacl;
1270}
1271
1272int 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);
6708bb27
AG
1282 if (!lun) {
1283 pr_err("%s Logical Unit Number: %u is not active on"
c66ac9db 1284 " Target Portal Group: %hu, ignoring request.\n",
e3d6f909
AG
1285 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1286 tpg->se_tpg_tfo->tpg_get_tag(tpg));
c66ac9db
NB
1287 return -EINVAL;
1288 }
1289
1290 nacl = lacl->se_lun_nacl;
6708bb27 1291 if (!nacl)
c66ac9db
NB
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
e80ac6c4
AG
1300 if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
1301 lun_access, nacl, tpg) < 0)
c66ac9db
NB
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
6708bb27 1310 pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
e3d6f909
AG
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,
c66ac9db
NB
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 */
1327int 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;
6708bb27 1335 if (!nacl)
c66ac9db
NB
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
e80ac6c4
AG
1344 core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
1345 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
c66ac9db
NB
1346
1347 lacl->se_lun = NULL;
1348
6708bb27 1349 pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
c66ac9db 1350 " InitiatorNode: %s Mapped LUN: %u\n",
e3d6f909
AG
1351 tpg->se_tpg_tfo->get_fabric_name(),
1352 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
c66ac9db
NB
1353 lacl->initiatorname, lacl->mapped_lun);
1354
1355 return 0;
1356}
1357
1358void core_dev_free_initiator_node_lun_acl(
1359 struct se_portal_group *tpg,
1360 struct se_lun_acl *lacl)
1361{
6708bb27 1362 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
e3d6f909
AG
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(),
c66ac9db
NB
1366 lacl->initiatorname, lacl->mapped_lun);
1367
1368 kfree(lacl);
1369}
1370
0fd97ccf
CH
1371static 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));
0fd97ccf
CH
1405}
1406
1407struct 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
0ff87549 1415 dev->dev_link_magic = SE_DEV_LINK_MAGIC;
0fd97ccf
CH
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);
d9ea32bf 1425 INIT_LIST_HEAD(&dev->g_dev_node);
0fd97ccf
CH
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);
68ff9b9b 1433 sema_init(&dev->caw_sem, 1);
0fd97ccf
CH
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
0fd97ccf
CH
1444 dev->t10_wwn.t10_dev = dev;
1445 dev->t10_alua.t10_dev = dev;
1446
1447 dev->dev_attrib.da_dev = dev;
adfa9570 1448 dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
0fd97ccf
CH
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;
0123a9ec 1457 dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
d397a445 1458 dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
0fd97ccf
CH
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;
773cbaf7 1468 dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
0fd97ccf
CH
1469 dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
1470 dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
1471
0fd97ccf
CH
1472 return dev;
1473}
1474
1475int 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
0fd97ccf
CH
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);
d9ea32bf
NB
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
0fd97ccf
CH
1551 return 0;
1552
1553out_free_alua:
1554 core_alua_free_lu_gp_mem(dev);
1555out:
1556 se_release_vpd_for_dev(dev);
1557 return ret;
1558}
1559
1560void 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
d9ea32bf
NB
1569 mutex_lock(&g_device_mutex);
1570 list_del(&dev->g_dev_node);
1571 mutex_unlock(&g_device_mutex);
1572
0fd97ccf
CH
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
c66ac9db
NB
1585int core_dev_setup_virtual_lun0(void)
1586{
1587 struct se_hba *hba;
1588 struct se_device *dev;
db5d1c3c 1589 char buf[] = "rd_pages=8,rd_nullio=1";
c66ac9db
NB
1590 int ret;
1591
6708bb27 1592 hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
c66ac9db
NB
1593 if (IS_ERR(hba))
1594 return PTR_ERR(hba);
1595
0fd97ccf
CH
1596 dev = target_alloc_device(hba, "virt_lun0");
1597 if (!dev) {
c66ac9db 1598 ret = -ENOMEM;
0fd97ccf 1599 goto out_free_hba;
c66ac9db 1600 }
c66ac9db 1601
0fd97ccf 1602 hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
c66ac9db 1603
0fd97ccf
CH
1604 ret = target_configure_device(dev);
1605 if (ret)
1606 goto out_free_se_dev;
c66ac9db 1607
0fd97ccf
CH
1608 lun0_hba = hba;
1609 g_lun0_dev = dev;
c66ac9db 1610 return 0;
0fd97ccf
CH
1611
1612out_free_se_dev:
1613 target_free_device(dev);
1614out_free_hba:
1615 core_delete_hba(hba);
c66ac9db
NB
1616 return ret;
1617}
1618
1619
1620void core_dev_release_virtual_lun0(void)
1621{
e3d6f909 1622 struct se_hba *hba = lun0_hba;
c66ac9db 1623
6708bb27 1624 if (!hba)
c66ac9db
NB
1625 return;
1626
e3d6f909 1627 if (g_lun0_dev)
0fd97ccf 1628 target_free_device(g_lun0_dev);
c66ac9db
NB
1629 core_delete_hba(hba);
1630}