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