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