blob: d90dbb0f1a69753a6654413587d9da31a32bdbaa [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_device.c (based on iscsi_target_device.c)
3 *
Andy Grovere3d6f902011-07-19 08:55:10 +00004 * This file contains the TCM Virtual Device and Disk Transport
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08005 * agnostic related functions.
6 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07007 * (c) Copyright 2003-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08008 *
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>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080033#include <linux/kthread.h>
34#include <linux/in.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040035#include <linux/export.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <net/sock.h>
37#include <net/tcp.h>
38#include <scsi/scsi.h>
Nicholas Bellinger1078da12011-05-19 20:19:13 -070039#include <scsi/scsi_device.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040
41#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050042#include <target/target_core_backend.h>
43#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044
Christoph Hellwige26d99a2011-11-14 12:30:30 -050045#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047#include "target_core_pr.h"
48#include "target_core_ua.h"
49
Nicholas Bellingerd9ea32b2013-08-22 11:33:37 -070050DEFINE_MUTEX(g_device_mutex);
51LIST_HEAD(g_device_list);
52
Andy Grovere3d6f902011-07-19 08:55:10 +000053static struct se_hba *lun0_hba;
Andy Grovere3d6f902011-07-19 08:55:10 +000054/* not static, needed by tpg.c */
55struct se_device *g_lun0_dev;
56
Christoph Hellwigde103c92012-11-06 12:24:09 -080057sense_reason_t
58transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080059{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060 struct se_lun *se_lun = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +000061 struct se_session *se_sess = se_cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +000062 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080064
Christoph Hellwigde103c92012-11-06 12:24:09 -080065 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
66 return TCM_NON_EXISTENT_LUN;
Fubo Chend8144952011-02-13 15:13:42 -080067
Roland Dreier78faae32011-07-20 09:09:10 +000068 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
Jörn Engelf2083242012-03-15 15:05:40 -040069 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
Andy Grover5951146d2011-07-19 10:26:37 +000070 if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
71 struct se_dev_entry *deve = se_cmd->se_deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080072
Andy Grover5951146d2011-07-19 10:26:37 +000073 deve->total_cmds++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080074
Andy Grover5951146d2011-07-19 10:26:37 +000075 if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
76 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
Andy Grover6708bb22011-06-08 10:36:43 -070077 pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078 " Access for 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +000079 se_cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080 unpacked_lun);
Roland Dreier78faae32011-07-20 09:09:10 +000081 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Christoph Hellwigde103c92012-11-06 12:24:09 -080082 return TCM_WRITE_PROTECTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080083 }
Andy Grover5951146d2011-07-19 10:26:37 +000084
85 if (se_cmd->data_direction == DMA_TO_DEVICE)
86 deve->write_bytes += se_cmd->data_length;
87 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
88 deve->read_bytes += se_cmd->data_length;
89
Andy Grover5951146d2011-07-19 10:26:37 +000090 se_lun = deve->se_lun;
91 se_cmd->se_lun = deve->se_lun;
92 se_cmd->pr_res_key = deve->pr_res_key;
93 se_cmd->orig_fe_lun = unpacked_lun;
Andy Grover5951146d2011-07-19 10:26:37 +000094 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
95 }
Roland Dreier78faae32011-07-20 09:09:10 +000096 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +000097
98 if (!se_lun) {
99 /*
100 * Use the se_portal_group->tpg_virt_lun0 to allow for
101 * REPORT_LUNS, et al to be returned when no active
102 * MappedLUN=0 exists for this Initiator Port.
103 */
104 if (unpacked_lun != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700105 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
Andy Grover5951146d2011-07-19 10:26:37 +0000106 " Access for 0x%08x\n",
107 se_cmd->se_tfo->get_fabric_name(),
108 unpacked_lun);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800109 return TCM_NON_EXISTENT_LUN;
Andy Grover5951146d2011-07-19 10:26:37 +0000110 }
111 /*
112 * Force WRITE PROTECT for virtual LUN 0
113 */
114 if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
Christoph Hellwigde103c92012-11-06 12:24:09 -0800115 (se_cmd->data_direction != DMA_NONE))
116 return TCM_WRITE_PROTECTED;
Andy Grover5951146d2011-07-19 10:26:37 +0000117
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;
Andy Grover5951146d2011-07-19 10:26:37 +0000121 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800122 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800123
Andy Grover5951146d2011-07-19 10:26:37 +0000124 /* 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;
Roland Dreier78faae32011-07-20 09:09:10 +0000129 spin_lock_irqsave(&dev->stats_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800130 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;
Roland Dreier78faae32011-07-20 09:09:10 +0000135 spin_unlock_irqrestore(&dev->stats_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800137 spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000138 list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800139 spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
140
141 return 0;
142}
Andy Grover5951146d2011-07-19 10:26:37 +0000143EXPORT_SYMBOL(transport_lookup_cmd_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800144
Andy Grover5951146d2011-07-19 10:26:37 +0000145int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800146{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147 struct se_dev_entry *deve;
148 struct se_lun *se_lun = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +0000149 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800150 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
Roland Dreier5e1be912011-07-20 09:28:56 +0000151 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800152
Christoph Hellwigde103c92012-11-06 12:24:09 -0800153 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
Andy Grovere3d6f902011-07-19 08:55:10 +0000154 return -ENODEV;
Fubo Chend8144952011-02-13 15:13:42 -0800155
Roland Dreier5e1be912011-07-20 09:28:56 +0000156 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
Jörn Engelf2083242012-03-15 15:05:40 -0400157 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
Andy Grover5951146d2011-07-19 10:26:37 +0000158 deve = se_cmd->se_deve;
159
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800160 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
Andy Grover5951146d2011-07-19 10:26:37 +0000161 se_tmr->tmr_lun = deve->se_lun;
162 se_cmd->se_lun = deve->se_lun;
163 se_lun = deve->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164 se_cmd->pr_res_key = deve->pr_res_key;
165 se_cmd->orig_fe_lun = unpacked_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800166 }
Roland Dreier5e1be912011-07-20 09:28:56 +0000167 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168
169 if (!se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700170 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800171 " Access for 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000172 se_cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800173 unpacked_lun);
Andy Grovere3d6f902011-07-19 08:55:10 +0000174 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800175 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176
Andy Grover5951146d2011-07-19 10:26:37 +0000177 /* 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
Roland Dreier5e1be912011-07-20 09:28:56 +0000181 spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000182 list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
Roland Dreier5e1be912011-07-20 09:28:56 +0000183 spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800184
185 return 0;
186}
Andy Grover5951146d2011-07-19 10:26:37 +0000187EXPORT_SYMBOL(transport_lookup_tmr_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800188
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++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400206 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800207
208 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
209 continue;
210
211 lun = deve->se_lun;
Andy Grover6708bb22011-06-08 10:36:43 -0700212 if (!lun) {
213 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000215 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800216 continue;
217 }
218 port = lun->lun_sep;
Andy Grover6708bb22011-06-08 10:36:43 -0700219 if (!port) {
220 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800221 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000222 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800223 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++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400252 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800253
254 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
255 continue;
256
257 if (!deve->se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700258 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800259 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000260 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800261 continue;
262 }
263 lun = deve->se_lun;
264
265 spin_unlock_irq(&nacl->device_list_lock);
Andy Grovere80ac6c2012-07-12 17:34:58 -0700266 core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
267 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800268 spin_lock_irq(&nacl->device_list_lock);
269 }
270 spin_unlock_irq(&nacl->device_list_lock);
271
Jörn Engelf2083242012-03-15 15:05:40 -0400272 array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800273 nacl->device_list = NULL;
274
275 return 0;
276}
277
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800278void core_update_device_list_access(
279 u32 mapped_lun,
280 u32 lun_access,
281 struct se_node_acl *nacl)
282{
283 struct se_dev_entry *deve;
284
285 spin_lock_irq(&nacl->device_list_lock);
Jörn Engelf2083242012-03-15 15:05:40 -0400286 deve = nacl->device_list[mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800287 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
288 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
289 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
290 } else {
291 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
292 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
293 }
294 spin_unlock_irq(&nacl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295}
296
Andy Grovere80ac6c2012-07-12 17:34:58 -0700297/* core_enable_device_list_for_node():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800298 *
299 *
300 */
Andy Grovere80ac6c2012-07-12 17:34:58 -0700301int core_enable_device_list_for_node(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302 struct se_lun *lun,
303 struct se_lun_acl *lun_acl,
304 u32 mapped_lun,
305 u32 lun_access,
306 struct se_node_acl *nacl,
Andy Grovere80ac6c2012-07-12 17:34:58 -0700307 struct se_portal_group *tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308{
309 struct se_port *port = lun->lun_sep;
Andy Grovere80ac6c2012-07-12 17:34:58 -0700310 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800311
312 spin_lock_irq(&nacl->device_list_lock);
Andy Grovere80ac6c2012-07-12 17:34:58 -0700313
314 deve = nacl->device_list[mapped_lun];
315
316 /*
317 * Check if the call is handling demo mode -> explict LUN ACL
318 * transition. This transition must be for the same struct se_lun
319 * + mapped_lun that was setup in demo mode..
320 */
321 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
322 if (deve->se_lun_acl != NULL) {
323 pr_err("struct se_dev_entry->se_lun_acl"
324 " already set for demo mode -> explict"
325 " LUN ACL transition\n");
326 spin_unlock_irq(&nacl->device_list_lock);
327 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 }
Andy Grovere80ac6c2012-07-12 17:34:58 -0700329 if (deve->se_lun != lun) {
330 pr_err("struct se_dev_entry->se_lun does"
331 " match passed struct se_lun for demo mode"
332 " -> explict LUN ACL transition\n");
333 spin_unlock_irq(&nacl->device_list_lock);
334 return -EINVAL;
335 }
336 deve->se_lun_acl = lun_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800337
338 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
339 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
340 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
341 } else {
342 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
343 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
344 }
345
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800346 spin_unlock_irq(&nacl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347 return 0;
348 }
Andy Grovere80ac6c2012-07-12 17:34:58 -0700349
350 deve->se_lun = lun;
351 deve->se_lun_acl = lun_acl;
352 deve->mapped_lun = mapped_lun;
353 deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
354
355 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
356 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
357 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
358 } else {
359 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
360 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
361 }
362
363 deve->creation_time = get_jiffies_64();
364 deve->attach_count++;
365 spin_unlock_irq(&nacl->device_list_lock);
366
367 spin_lock_bh(&port->sep_alua_lock);
368 list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
369 spin_unlock_bh(&port->sep_alua_lock);
370
371 return 0;
372}
373
374/* core_disable_device_list_for_node():
375 *
376 *
377 */
378int core_disable_device_list_for_node(
379 struct se_lun *lun,
380 struct se_lun_acl *lun_acl,
381 u32 mapped_lun,
382 u32 lun_access,
383 struct se_node_acl *nacl,
384 struct se_portal_group *tpg)
385{
386 struct se_port *port = lun->lun_sep;
Nicholas Bellinger77d4c742012-07-14 15:11:41 -0700387 struct se_dev_entry *deve = nacl->device_list[mapped_lun];
Andy Grovere80ac6c2012-07-12 17:34:58 -0700388
389 /*
390 * If the MappedLUN entry is being disabled, the entry in
391 * port->sep_alua_list must be removed now before clearing the
392 * struct se_dev_entry pointers below as logic in
393 * core_alua_do_transition_tg_pt() depends on these being present.
394 *
395 * deve->se_lun_acl will be NULL for demo-mode created LUNs
396 * that have not been explicitly converted to MappedLUNs ->
397 * struct se_lun_acl, but we remove deve->alua_port_list from
398 * port->sep_alua_list. This also means that active UAs and
399 * NodeACL context specific PR metadata for demo-mode
400 * MappedLUN *deve will be released below..
401 */
402 spin_lock_bh(&port->sep_alua_lock);
403 list_del(&deve->alua_port_list);
404 spin_unlock_bh(&port->sep_alua_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800405 /*
406 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
407 * PR operation to complete.
408 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 while (atomic_read(&deve->pr_ref_count) != 0)
410 cpu_relax();
Nicholas Bellinger77d4c742012-07-14 15:11:41 -0700411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 spin_lock_irq(&nacl->device_list_lock);
413 /*
414 * Disable struct se_dev_entry LUN ACL mapping
415 */
416 core_scsi3_ua_release_all(deve);
417 deve->se_lun = NULL;
418 deve->se_lun_acl = NULL;
419 deve->lun_flags = 0;
420 deve->creation_time = 0;
421 deve->attach_count--;
422 spin_unlock_irq(&nacl->device_list_lock);
423
424 core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
425 return 0;
426}
427
428/* core_clear_lun_from_tpg():
429 *
430 *
431 */
432void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
433{
434 struct se_node_acl *nacl;
435 struct se_dev_entry *deve;
436 u32 i;
437
Roland Dreier28638882011-08-16 09:40:01 -0700438 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800439 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
Roland Dreier28638882011-08-16 09:40:01 -0700440 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441
442 spin_lock_irq(&nacl->device_list_lock);
443 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400444 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800445 if (lun != deve->se_lun)
446 continue;
447 spin_unlock_irq(&nacl->device_list_lock);
448
Andy Grovere80ac6c2012-07-12 17:34:58 -0700449 core_disable_device_list_for_node(lun, NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800450 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
Andy Grovere80ac6c2012-07-12 17:34:58 -0700451 nacl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800452
453 spin_lock_irq(&nacl->device_list_lock);
454 }
455 spin_unlock_irq(&nacl->device_list_lock);
456
Roland Dreier28638882011-08-16 09:40:01 -0700457 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800458 }
Roland Dreier28638882011-08-16 09:40:01 -0700459 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460}
461
462static struct se_port *core_alloc_port(struct se_device *dev)
463{
464 struct se_port *port, *port_tmp;
465
466 port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700467 if (!port) {
468 pr_err("Unable to allocate struct se_port\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000469 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470 }
471 INIT_LIST_HEAD(&port->sep_alua_list);
472 INIT_LIST_HEAD(&port->sep_list);
473 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
474 spin_lock_init(&port->sep_alua_lock);
475 mutex_init(&port->sep_tg_pt_md_mutex);
476
477 spin_lock(&dev->se_port_lock);
478 if (dev->dev_port_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -0700479 pr_warn("Reached dev->dev_port_count =="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480 " 0x0000ffff\n");
481 spin_unlock(&dev->se_port_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000482 return ERR_PTR(-ENOSPC);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800483 }
484again:
485 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900486 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800487 * Here is the table from spc4r17 section 7.7.3.8.
488 *
489 * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
490 *
491 * Code Description
492 * 0h Reserved
493 * 1h Relative port 1, historically known as port A
494 * 2h Relative port 2, historically known as port B
495 * 3h to FFFFh Relative port 3 through 65 535
496 */
497 port->sep_rtpi = dev->dev_rpti_counter++;
Andy Grover6708bb22011-06-08 10:36:43 -0700498 if (!port->sep_rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800499 goto again;
500
501 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
502 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900503 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800504 * for 16-bit wrap..
505 */
506 if (port->sep_rtpi == port_tmp->sep_rtpi)
507 goto again;
508 }
509 spin_unlock(&dev->se_port_lock);
510
511 return port;
512}
513
514static void core_export_port(
515 struct se_device *dev,
516 struct se_portal_group *tpg,
517 struct se_port *port,
518 struct se_lun *lun)
519{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800520 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
521
522 spin_lock(&dev->se_port_lock);
523 spin_lock(&lun->lun_sep_lock);
524 port->sep_tpg = tpg;
525 port->sep_lun = lun;
526 lun->lun_sep = port;
527 spin_unlock(&lun->lun_sep_lock);
528
529 list_add_tail(&port->sep_list, &dev->dev_sep_list);
530 spin_unlock(&dev->se_port_lock);
531
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400532 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
533 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
535 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
Andy Grover6708bb22011-06-08 10:36:43 -0700536 pr_err("Unable to allocate t10_alua_tg_pt"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800537 "_gp_member_t\n");
538 return;
539 }
540 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
541 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400542 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800543 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700544 pr_debug("%s/%s: Adding to default ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545 " Group: alua/default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000546 dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 }
548
549 dev->dev_port_count++;
Masanari Iida35d1efe2012-08-16 22:43:13 +0900550 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800551}
552
553/*
554 * Called with struct se_device->se_port_lock spinlock held.
555 */
556static void core_release_port(struct se_device *dev, struct se_port *port)
Dan Carpenter5dd7ed22011-03-14 04:06:01 -0700557 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558{
559 /*
560 * Wait for any port reference for PR ALL_TG_PT=1 operation
561 * to complete in __core_scsi3_alloc_registration()
562 */
563 spin_unlock(&dev->se_port_lock);
564 if (atomic_read(&port->sep_tg_pt_ref_cnt))
565 cpu_relax();
566 spin_lock(&dev->se_port_lock);
567
568 core_alua_free_tg_pt_gp_mem(port);
569
570 list_del(&port->sep_list);
571 dev->dev_port_count--;
572 kfree(port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573}
574
575int core_dev_export(
576 struct se_device *dev,
577 struct se_portal_group *tpg,
578 struct se_lun *lun)
579{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400580 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800581 struct se_port *port;
582
583 port = core_alloc_port(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000584 if (IS_ERR(port))
585 return PTR_ERR(port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800586
587 lun->lun_se_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400589 spin_lock(&hba->device_lock);
590 dev->export_count++;
591 spin_unlock(&hba->device_lock);
592
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800593 core_export_port(dev, tpg, port, lun);
594 return 0;
595}
596
597void core_dev_unexport(
598 struct se_device *dev,
599 struct se_portal_group *tpg,
600 struct se_lun *lun)
601{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400602 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800603 struct se_port *port = lun->lun_sep;
604
605 spin_lock(&lun->lun_sep_lock);
606 if (lun->lun_se_dev == NULL) {
607 spin_unlock(&lun->lun_sep_lock);
608 return;
609 }
610 spin_unlock(&lun->lun_sep_lock);
611
612 spin_lock(&dev->se_port_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800613 core_release_port(dev, port);
614 spin_unlock(&dev->se_port_lock);
615
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400616 spin_lock(&hba->device_lock);
617 dev->export_count--;
618 spin_unlock(&hba->device_lock);
619
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800620 lun->lun_se_dev = NULL;
621}
622
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400623static void se_release_vpd_for_dev(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800624{
625 struct t10_vpd *vpd, *vpd_tmp;
626
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400627 spin_lock(&dev->t10_wwn.t10_vpd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800628 list_for_each_entry_safe(vpd, vpd_tmp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400629 &dev->t10_wwn.t10_vpd_list, vpd_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 list_del(&vpd->vpd_list);
631 kfree(vpd);
632 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400633 spin_unlock(&dev->t10_wwn.t10_vpd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800634}
635
Roland Dreierc8045372012-07-16 15:17:12 -0700636static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700637{
Roland Dreier3e039892012-10-31 09:16:45 -0700638 u32 aligned_max_sectors;
639 u32 alignment;
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700640 /*
641 * Limit max_sectors to a PAGE_SIZE aligned value for modern
642 * transport_allocate_data_tasks() operation.
643 */
Roland Dreier3e039892012-10-31 09:16:45 -0700644 alignment = max(1ul, PAGE_SIZE / block_size);
645 aligned_max_sectors = rounddown(max_sectors, alignment);
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700646
Roland Dreier3e039892012-10-31 09:16:45 -0700647 if (max_sectors != aligned_max_sectors)
648 pr_info("Rounding down aligned max_sectors from %u to %u\n",
649 max_sectors, aligned_max_sectors);
650
651 return aligned_max_sectors;
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700652}
653
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800654int se_dev_set_max_unmap_lba_count(
655 struct se_device *dev,
656 u32 max_unmap_lba_count)
657{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400658 dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
Andy Grover6708bb22011-06-08 10:36:43 -0700659 pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400660 dev, dev->dev_attrib.max_unmap_lba_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800661 return 0;
662}
663
664int se_dev_set_max_unmap_block_desc_count(
665 struct se_device *dev,
666 u32 max_unmap_block_desc_count)
667{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400668 dev->dev_attrib.max_unmap_block_desc_count =
Andy Grovere3d6f902011-07-19 08:55:10 +0000669 max_unmap_block_desc_count;
Andy Grover6708bb22011-06-08 10:36:43 -0700670 pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400671 dev, dev->dev_attrib.max_unmap_block_desc_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800672 return 0;
673}
674
675int se_dev_set_unmap_granularity(
676 struct se_device *dev,
677 u32 unmap_granularity)
678{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400679 dev->dev_attrib.unmap_granularity = unmap_granularity;
Andy Grover6708bb22011-06-08 10:36:43 -0700680 pr_debug("dev[%p]: Set unmap_granularity: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400681 dev, dev->dev_attrib.unmap_granularity);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682 return 0;
683}
684
685int se_dev_set_unmap_granularity_alignment(
686 struct se_device *dev,
687 u32 unmap_granularity_alignment)
688{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400689 dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
Andy Grover6708bb22011-06-08 10:36:43 -0700690 pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400691 dev, dev->dev_attrib.unmap_granularity_alignment);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800692 return 0;
693}
694
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800695int se_dev_set_max_write_same_len(
696 struct se_device *dev,
697 u32 max_write_same_len)
698{
699 dev->dev_attrib.max_write_same_len = max_write_same_len;
700 pr_debug("dev[%p]: Set max_write_same_len: %u\n",
701 dev, dev->dev_attrib.max_write_same_len);
702 return 0;
703}
704
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700705static void dev_set_t10_wwn_model_alias(struct se_device *dev)
706{
707 const char *configname;
708
709 configname = config_item_name(&dev->dev_group.cg_item);
710 if (strlen(configname) >= 16) {
711 pr_warn("dev[%p]: Backstore name '%s' is too long for "
712 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
713 configname);
714 }
715 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
716}
717
718int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
719{
720 if (dev->export_count) {
721 pr_err("dev[%p]: Unable to change model alias"
722 " while export_count is %d\n",
723 dev, dev->export_count);
724 return -EINVAL;
725 }
726
727 if (flag != 0 && flag != 1) {
728 pr_err("Illegal value %d\n", flag);
729 return -EINVAL;
730 }
731
732 if (flag) {
733 dev_set_t10_wwn_model_alias(dev);
734 } else {
735 strncpy(&dev->t10_wwn.model[0],
736 dev->transport->inquiry_prod, 16);
737 }
738 dev->dev_attrib.emulate_model_alias = flag;
739
740 return 0;
741}
742
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800743int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
744{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400745 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700746 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000747 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400749
Andy Groverc6388302011-11-30 12:11:50 -0800750 if (flag) {
751 pr_err("dpo_emulated not supported\n");
752 return -EINVAL;
753 }
754
755 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800756}
757
758int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
759{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400760 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700761 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000762 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800763 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400764
Nicholas Bellingerfd30e932012-08-26 13:35:58 -0700765 if (flag &&
766 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
767 pr_err("emulate_fua_write not supported for pSCSI\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000768 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800769 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400770 dev->dev_attrib.emulate_fua_write = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700771 pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400772 dev, dev->dev_attrib.emulate_fua_write);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800773 return 0;
774}
775
776int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
777{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400778 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700779 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000780 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800781 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400782
Andy Groverc6388302011-11-30 12:11:50 -0800783 if (flag) {
784 pr_err("ua read emulated not supported\n");
785 return -EINVAL;
786 }
787
788 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800789}
790
791int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
792{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400793 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700794 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000795 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800796 }
Nicholas Bellingerfd30e932012-08-26 13:35:58 -0700797 if (flag &&
798 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
799 pr_err("emulate_write_cache not supported for pSCSI\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000800 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801 }
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800802 if (dev->transport->get_write_cache) {
803 pr_warn("emulate_write_cache cannot be changed when underlying"
804 " HW reports WriteCacheEnabled, ignoring request\n");
805 return 0;
806 }
807
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400808 dev->dev_attrib.emulate_write_cache = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700809 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400810 dev, dev->dev_attrib.emulate_write_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800811 return 0;
812}
813
814int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
815{
816 if ((flag != 0) && (flag != 1) && (flag != 2)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700817 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000818 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800819 }
820
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400821 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700822 pr_err("dev[%p]: Unable to change SE Device"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400823 " UA_INTRLCK_CTRL while export_count is %d\n",
824 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +0000825 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800826 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400827 dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700828 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400829 dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800830
831 return 0;
832}
833
834int se_dev_set_emulate_tas(struct se_device *dev, int flag)
835{
836 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700837 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000838 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800839 }
840
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400841 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700842 pr_err("dev[%p]: Unable to change SE Device TAS while"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400843 " export_count is %d\n",
844 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +0000845 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800846 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400847 dev->dev_attrib.emulate_tas = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700848 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400849 dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800850
851 return 0;
852}
853
854int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
855{
856 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700857 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000858 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800859 }
860 /*
861 * We expect this value to be non-zero when generic Block Layer
862 * Discard supported is detected iblock_create_virtdevice().
863 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400864 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700865 pr_err("Generic Block Discard not supported\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800866 return -ENOSYS;
867 }
868
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400869 dev->dev_attrib.emulate_tpu = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700870 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800871 dev, flag);
872 return 0;
873}
874
875int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
876{
877 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700878 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000879 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800880 }
881 /*
882 * We expect this value to be non-zero when generic Block Layer
883 * Discard supported is detected iblock_create_virtdevice().
884 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400885 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700886 pr_err("Generic Block Discard not supported\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800887 return -ENOSYS;
888 }
889
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400890 dev->dev_attrib.emulate_tpws = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700891 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800892 dev, flag);
893 return 0;
894}
895
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -0700896int se_dev_set_emulate_caw(struct se_device *dev, int flag)
897{
898 if (flag != 0 && flag != 1) {
899 pr_err("Illegal value %d\n", flag);
900 return -EINVAL;
901 }
902 dev->dev_attrib.emulate_caw = flag;
903 pr_debug("dev[%p]: SE Device CompareAndWrite (AtomicTestandSet): %d\n",
904 dev, flag);
905
906 return 0;
907}
908
Nicholas Bellingerd397a442013-08-22 14:17:20 -0700909int se_dev_set_emulate_3pc(struct se_device *dev, int flag)
910{
911 if (flag != 0 && flag != 1) {
912 pr_err("Illegal value %d\n", flag);
913 return -EINVAL;
914 }
915 dev->dev_attrib.emulate_3pc = flag;
916 pr_debug("dev[%p]: SE Device 3rd Party Copy (EXTENDED_COPY): %d\n",
917 dev, flag);
918
919 return 0;
920}
921
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800922int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
923{
924 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700925 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000926 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800927 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400928 dev->dev_attrib.enforce_pr_isids = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700929 pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400930 (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800931 return 0;
932}
933
Roland Dreiere22a7f02011-07-05 13:34:52 -0700934int se_dev_set_is_nonrot(struct se_device *dev, int flag)
935{
936 if ((flag != 0) && (flag != 1)) {
937 printk(KERN_ERR "Illegal value %d\n", flag);
938 return -EINVAL;
939 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400940 dev->dev_attrib.is_nonrot = flag;
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700941 pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
Roland Dreiere22a7f02011-07-05 13:34:52 -0700942 dev, flag);
943 return 0;
944}
945
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700946int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
947{
948 if (flag != 0) {
949 printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
950 " reordering not implemented\n", dev);
951 return -ENOSYS;
952 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400953 dev->dev_attrib.emulate_rest_reord = flag;
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700954 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
955 return 0;
956}
957
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958/*
959 * Note, this can only be called on unexported SE Device Object.
960 */
961int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
962{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400963 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700964 pr_err("dev[%p]: Unable to change SE Device TCQ while"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400965 " export_count is %d\n",
966 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +0000967 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800968 }
Andy Grover6708bb22011-06-08 10:36:43 -0700969 if (!queue_depth) {
970 pr_err("dev[%p]: Illegal ZERO value for queue"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800971 "_depth\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000972 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800973 }
974
Andy Grovere3d6f902011-07-19 08:55:10 +0000975 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400976 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -0700977 pr_err("dev[%p]: Passed queue_depth: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800978 " exceeds TCM/SE_Device TCQ: %u\n",
979 dev, queue_depth,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400980 dev->dev_attrib.hw_queue_depth);
Andy Grovere3d6f902011-07-19 08:55:10 +0000981 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800982 }
983 } else {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400984 if (queue_depth > dev->dev_attrib.queue_depth) {
985 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -0700986 pr_err("dev[%p]: Passed queue_depth:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800987 " %u exceeds TCM/SE_Device MAX"
988 " TCQ: %u\n", dev, queue_depth,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400989 dev->dev_attrib.hw_queue_depth);
Andy Grovere3d6f902011-07-19 08:55:10 +0000990 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800991 }
992 }
993 }
994
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400995 dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
Andy Grover6708bb22011-06-08 10:36:43 -0700996 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800997 dev, queue_depth);
998 return 0;
999}
1000
Roland Dreier015487b2012-02-13 16:18:17 -08001001int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
1002{
Nicholas Bellinger7a3cf6c2013-01-31 15:05:48 -08001003 int block_size = dev->dev_attrib.block_size;
1004
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001005 if (dev->export_count) {
Roland Dreier015487b2012-02-13 16:18:17 -08001006 pr_err("dev[%p]: Unable to change SE Device"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001007 " fabric_max_sectors while export_count is %d\n",
1008 dev, dev->export_count);
Roland Dreier015487b2012-02-13 16:18:17 -08001009 return -EINVAL;
1010 }
1011 if (!fabric_max_sectors) {
1012 pr_err("dev[%p]: Illegal ZERO value for"
1013 " fabric_max_sectors\n", dev);
1014 return -EINVAL;
1015 }
1016 if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
1017 pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
1018 " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
1019 DA_STATUS_MAX_SECTORS_MIN);
1020 return -EINVAL;
1021 }
1022 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001023 if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
Roland Dreier015487b2012-02-13 16:18:17 -08001024 pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1025 " greater than TCM/SE_Device max_sectors:"
1026 " %u\n", dev, fabric_max_sectors,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001027 dev->dev_attrib.hw_max_sectors);
Roland Dreier015487b2012-02-13 16:18:17 -08001028 return -EINVAL;
1029 }
1030 } else {
1031 if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
1032 pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1033 " greater than DA_STATUS_MAX_SECTORS_MAX:"
1034 " %u\n", dev, fabric_max_sectors,
1035 DA_STATUS_MAX_SECTORS_MAX);
1036 return -EINVAL;
1037 }
1038 }
1039 /*
1040 * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
1041 */
Nicholas Bellinger7a3cf6c2013-01-31 15:05:48 -08001042 if (!block_size) {
1043 block_size = 512;
1044 pr_warn("Defaulting to 512 for zero block_size\n");
1045 }
Roland Dreier015487b2012-02-13 16:18:17 -08001046 fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
Nicholas Bellinger7a3cf6c2013-01-31 15:05:48 -08001047 block_size);
Roland Dreier015487b2012-02-13 16:18:17 -08001048
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001049 dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
Roland Dreier015487b2012-02-13 16:18:17 -08001050 pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
1051 dev, fabric_max_sectors);
1052 return 0;
1053}
1054
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001055int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1056{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001057 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001058 pr_err("dev[%p]: Unable to change SE Device"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001059 " optimal_sectors while export_count is %d\n",
1060 dev, dev->export_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001061 return -EINVAL;
1062 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001063 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Andy Grover6708bb22011-06-08 10:36:43 -07001064 pr_err("dev[%p]: Passed optimal_sectors cannot be"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001065 " changed for TCM/pSCSI\n", dev);
1066 return -EINVAL;
1067 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001068 if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
Andy Grover6708bb22011-06-08 10:36:43 -07001069 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
Roland Dreier015487b2012-02-13 16:18:17 -08001070 " greater than fabric_max_sectors: %u\n", dev,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001071 optimal_sectors, dev->dev_attrib.fabric_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072 return -EINVAL;
1073 }
1074
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001075 dev->dev_attrib.optimal_sectors = optimal_sectors;
Andy Grover6708bb22011-06-08 10:36:43 -07001076 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001077 dev, optimal_sectors);
1078 return 0;
1079}
1080
1081int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1082{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001083 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001084 pr_err("dev[%p]: Unable to change SE Device block_size"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001085 " while export_count is %d\n",
1086 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +00001087 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001088 }
1089
1090 if ((block_size != 512) &&
1091 (block_size != 1024) &&
1092 (block_size != 2048) &&
1093 (block_size != 4096)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001094 pr_err("dev[%p]: Illegal value for block_device: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095 " for SE device, must be 512, 1024, 2048 or 4096\n",
1096 dev, block_size);
Andy Grovere3d6f902011-07-19 08:55:10 +00001097 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001098 }
1099
Andy Grovere3d6f902011-07-19 08:55:10 +00001100 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Andy Grover6708bb22011-06-08 10:36:43 -07001101 pr_err("dev[%p]: Not allowed to change block_size for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001102 " Physical Device, use for Linux/SCSI to change"
1103 " block_size for underlying hardware\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00001104 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 }
1106
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001107 dev->dev_attrib.block_size = block_size;
Andy Grover6708bb22011-06-08 10:36:43 -07001108 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001109 dev, block_size);
1110 return 0;
1111}
1112
1113struct se_lun *core_dev_add_lun(
1114 struct se_portal_group *tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001115 struct se_device *dev,
1116 u32 lun)
1117{
1118 struct se_lun *lun_p;
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001119 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001120
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121 lun_p = core_tpg_pre_addlun(tpg, lun);
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001122 if (IS_ERR(lun_p))
1123 return lun_p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124
Nicholas Bellinger58d92612012-03-20 21:26:48 -07001125 rc = core_tpg_post_addlun(tpg, lun_p,
1126 TRANSPORT_LUNFLAGS_READ_WRITE, dev);
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001127 if (rc < 0)
1128 return ERR_PTR(rc);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129
Andy Grover6708bb22011-06-08 10:36:43 -07001130 pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
Andy Grovere3d6f902011-07-19 08:55:10 +00001131 " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1132 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
Andy Grover2dca6732012-07-12 17:34:55 -07001133 tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134 /*
1135 * Update LUN maps for dynamically added initiators when
1136 * generate_node_acl is enabled.
1137 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001138 if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 struct se_node_acl *acl;
Roland Dreier28638882011-08-16 09:40:01 -07001140 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001141 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
Nicholas Bellinger052605c2011-07-26 17:48:43 -07001142 if (acl->dynamic_node_acl &&
1143 (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
1144 !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
Roland Dreier28638882011-08-16 09:40:01 -07001145 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 core_tpg_add_node_to_devs(acl, tpg);
Roland Dreier28638882011-08-16 09:40:01 -07001147 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148 }
1149 }
Roland Dreier28638882011-08-16 09:40:01 -07001150 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001151 }
1152
1153 return lun_p;
1154}
1155
1156/* core_dev_del_lun():
1157 *
1158 *
1159 */
1160int core_dev_del_lun(
1161 struct se_portal_group *tpg,
1162 u32 unpacked_lun)
1163{
1164 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001165
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001166 lun = core_tpg_pre_dellun(tpg, unpacked_lun);
1167 if (IS_ERR(lun))
1168 return PTR_ERR(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169
1170 core_tpg_post_dellun(tpg, lun);
1171
Andy Grover6708bb22011-06-08 10:36:43 -07001172 pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
Andy Grovere3d6f902011-07-19 08:55:10 +00001173 " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
1174 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
1175 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001176
1177 return 0;
1178}
1179
1180struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
1181{
1182 struct se_lun *lun;
1183
1184 spin_lock(&tpg->tpg_lun_lock);
1185 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001186 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187 "_PER_TPG-1: %u for Target Portal Group: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001188 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +00001190 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 spin_unlock(&tpg->tpg_lun_lock);
1192 return NULL;
1193 }
Jörn Engel4a5a75f2012-03-15 15:05:12 -04001194 lun = tpg->tpg_lun_list[unpacked_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195
1196 if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001197 pr_err("%s Logical Unit Number: %u is not free on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001199 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1200 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 spin_unlock(&tpg->tpg_lun_lock);
1202 return NULL;
1203 }
1204 spin_unlock(&tpg->tpg_lun_lock);
1205
1206 return lun;
1207}
1208
1209/* core_dev_get_lun():
1210 *
1211 *
1212 */
1213static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
1214{
1215 struct se_lun *lun;
1216
1217 spin_lock(&tpg->tpg_lun_lock);
1218 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001219 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001220 "_TPG-1: %u for Target Portal Group: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001221 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +00001223 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 spin_unlock(&tpg->tpg_lun_lock);
1225 return NULL;
1226 }
Jörn Engel4a5a75f2012-03-15 15:05:12 -04001227 lun = tpg->tpg_lun_list[unpacked_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001228
1229 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001230 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001232 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1233 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001234 spin_unlock(&tpg->tpg_lun_lock);
1235 return NULL;
1236 }
1237 spin_unlock(&tpg->tpg_lun_lock);
1238
1239 return lun;
1240}
1241
1242struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1243 struct se_portal_group *tpg,
Nicholas Bellingerfcf29482013-02-18 18:00:33 -08001244 struct se_node_acl *nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245 u32 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246 int *ret)
1247{
1248 struct se_lun_acl *lacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249
Nicholas Bellingerfcf29482013-02-18 18:00:33 -08001250 if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001251 pr_err("%s InitiatorName exceeds maximum size.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001252 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253 *ret = -EOVERFLOW;
1254 return NULL;
1255 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001257 if (!lacl) {
1258 pr_err("Unable to allocate memory for struct se_lun_acl.\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259 *ret = -ENOMEM;
1260 return NULL;
1261 }
1262
1263 INIT_LIST_HEAD(&lacl->lacl_list);
1264 lacl->mapped_lun = mapped_lun;
1265 lacl->se_lun_nacl = nacl;
Nicholas Bellingerfcf29482013-02-18 18:00:33 -08001266 snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
1267 nacl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268
1269 return lacl;
1270}
1271
1272int core_dev_add_initiator_node_lun_acl(
1273 struct se_portal_group *tpg,
1274 struct se_lun_acl *lacl,
1275 u32 unpacked_lun,
1276 u32 lun_access)
1277{
1278 struct se_lun *lun;
1279 struct se_node_acl *nacl;
1280
1281 lun = core_dev_get_lun(tpg, unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -07001282 if (!lun) {
1283 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001285 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1286 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 return -EINVAL;
1288 }
1289
1290 nacl = lacl->se_lun_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07001291 if (!nacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 return -EINVAL;
1293
1294 if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
1295 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
1296 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1297
1298 lacl->se_lun = lun;
1299
Andy Grovere80ac6c2012-07-12 17:34:58 -07001300 if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
1301 lun_access, nacl, tpg) < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001302 return -EINVAL;
1303
1304 spin_lock(&lun->lun_acl_lock);
1305 list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1306 atomic_inc(&lun->lun_acl_count);
1307 smp_mb__after_atomic_inc();
1308 spin_unlock(&lun->lun_acl_lock);
1309
Andy Grover6708bb22011-06-08 10:36:43 -07001310 pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
Andy Grovere3d6f902011-07-19 08:55:10 +00001311 " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
1312 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
1314 lacl->initiatorname);
1315 /*
1316 * Check to see if there are any existing persistent reservation APTPL
1317 * pre-registrations that need to be enabled for this LUN ACL..
1318 */
1319 core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
1320 return 0;
1321}
1322
1323/* core_dev_del_initiator_node_lun_acl():
1324 *
1325 *
1326 */
1327int core_dev_del_initiator_node_lun_acl(
1328 struct se_portal_group *tpg,
1329 struct se_lun *lun,
1330 struct se_lun_acl *lacl)
1331{
1332 struct se_node_acl *nacl;
1333
1334 nacl = lacl->se_lun_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07001335 if (!nacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001336 return -EINVAL;
1337
1338 spin_lock(&lun->lun_acl_lock);
1339 list_del(&lacl->lacl_list);
1340 atomic_dec(&lun->lun_acl_count);
1341 smp_mb__after_atomic_dec();
1342 spin_unlock(&lun->lun_acl_lock);
1343
Andy Grovere80ac6c2012-07-12 17:34:58 -07001344 core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
1345 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001346
1347 lacl->se_lun = NULL;
1348
Andy Grover6708bb22011-06-08 10:36:43 -07001349 pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001350 " InitiatorNode: %s Mapped LUN: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001351 tpg->se_tpg_tfo->get_fabric_name(),
1352 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001353 lacl->initiatorname, lacl->mapped_lun);
1354
1355 return 0;
1356}
1357
1358void core_dev_free_initiator_node_lun_acl(
1359 struct se_portal_group *tpg,
1360 struct se_lun_acl *lacl)
1361{
Andy Grover6708bb22011-06-08 10:36:43 -07001362 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
Andy Grovere3d6f902011-07-19 08:55:10 +00001363 " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1364 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1365 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001366 lacl->initiatorname, lacl->mapped_lun);
1367
1368 kfree(lacl);
1369}
1370
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001371static void scsi_dump_inquiry(struct se_device *dev)
1372{
1373 struct t10_wwn *wwn = &dev->t10_wwn;
1374 char buf[17];
1375 int i, device_type;
1376 /*
1377 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1378 */
1379 for (i = 0; i < 8; i++)
1380 if (wwn->vendor[i] >= 0x20)
1381 buf[i] = wwn->vendor[i];
1382 else
1383 buf[i] = ' ';
1384 buf[i] = '\0';
1385 pr_debug(" Vendor: %s\n", buf);
1386
1387 for (i = 0; i < 16; i++)
1388 if (wwn->model[i] >= 0x20)
1389 buf[i] = wwn->model[i];
1390 else
1391 buf[i] = ' ';
1392 buf[i] = '\0';
1393 pr_debug(" Model: %s\n", buf);
1394
1395 for (i = 0; i < 4; i++)
1396 if (wwn->revision[i] >= 0x20)
1397 buf[i] = wwn->revision[i];
1398 else
1399 buf[i] = ' ';
1400 buf[i] = '\0';
1401 pr_debug(" Revision: %s\n", buf);
1402
1403 device_type = dev->transport->get_device_type(dev);
1404 pr_debug(" Type: %s ", scsi_device_type(device_type));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001405}
1406
1407struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
1408{
1409 struct se_device *dev;
1410
1411 dev = hba->transport->alloc_device(hba, name);
1412 if (!dev)
1413 return NULL;
1414
Nicholas Bellinger0ff87542012-12-04 23:43:57 -08001415 dev->dev_link_magic = SE_DEV_LINK_MAGIC;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001416 dev->se_hba = hba;
1417 dev->transport = hba->transport;
1418
1419 INIT_LIST_HEAD(&dev->dev_list);
1420 INIT_LIST_HEAD(&dev->dev_sep_list);
1421 INIT_LIST_HEAD(&dev->dev_tmr_list);
1422 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1423 INIT_LIST_HEAD(&dev->state_list);
1424 INIT_LIST_HEAD(&dev->qf_cmd_list);
Nicholas Bellingerd9ea32b2013-08-22 11:33:37 -07001425 INIT_LIST_HEAD(&dev->g_dev_node);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001426 spin_lock_init(&dev->stats_lock);
1427 spin_lock_init(&dev->execute_task_lock);
1428 spin_lock_init(&dev->delayed_cmd_lock);
1429 spin_lock_init(&dev->dev_reservation_lock);
1430 spin_lock_init(&dev->se_port_lock);
1431 spin_lock_init(&dev->se_tmr_lock);
1432 spin_lock_init(&dev->qf_cmd_lock);
Nicholas Bellinger68ff9b92013-08-19 15:20:28 -07001433 sema_init(&dev->caw_sem, 1);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001434 atomic_set(&dev->dev_ordered_id, 0);
1435 INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
1436 spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
1437 INIT_LIST_HEAD(&dev->t10_pr.registration_list);
1438 INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
1439 spin_lock_init(&dev->t10_pr.registration_lock);
1440 spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
1441 INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
1442 spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
1443
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001444 dev->t10_wwn.t10_dev = dev;
1445 dev->t10_alua.t10_dev = dev;
1446
1447 dev->dev_attrib.da_dev = dev;
Tregaron Baylyadfa9572013-01-31 15:30:24 -07001448 dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001449 dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
1450 dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
1451 dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
1452 dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
1453 dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
1454 dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
1455 dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
1456 dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
Nicholas Bellinger0123a9e2013-08-20 14:24:09 -07001457 dev->dev_attrib.emulate_caw = DA_EMULATE_CAW;
Nicholas Bellingerd397a442013-08-22 14:17:20 -07001458 dev->dev_attrib.emulate_3pc = DA_EMULATE_3PC;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001459 dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
1460 dev->dev_attrib.is_nonrot = DA_IS_NONROT;
1461 dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
1462 dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
1463 dev->dev_attrib.max_unmap_block_desc_count =
1464 DA_MAX_UNMAP_BLOCK_DESC_COUNT;
1465 dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
1466 dev->dev_attrib.unmap_granularity_alignment =
1467 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -08001468 dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001469 dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
1470 dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
1471
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001472 return dev;
1473}
1474
1475int target_configure_device(struct se_device *dev)
1476{
1477 struct se_hba *hba = dev->se_hba;
1478 int ret;
1479
1480 if (dev->dev_flags & DF_CONFIGURED) {
1481 pr_err("se_dev->se_dev_ptr already set for storage"
1482 " object\n");
1483 return -EEXIST;
1484 }
1485
1486 ret = dev->transport->configure_device(dev);
1487 if (ret)
1488 goto out;
1489 dev->dev_flags |= DF_CONFIGURED;
1490
1491 /*
1492 * XXX: there is not much point to have two different values here..
1493 */
1494 dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
1495 dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
1496
1497 /*
1498 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
1499 */
1500 dev->dev_attrib.hw_max_sectors =
1501 se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
1502 dev->dev_attrib.hw_block_size);
1503
1504 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1505 dev->creation_time = get_jiffies_64();
1506
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001507 ret = core_setup_alua(dev);
1508 if (ret)
1509 goto out;
1510
1511 /*
1512 * Startup the struct se_device processing thread
1513 */
1514 dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
1515 dev->transport->name);
1516 if (!dev->tmr_wq) {
1517 pr_err("Unable to create tmr workqueue for %s\n",
1518 dev->transport->name);
1519 ret = -ENOMEM;
1520 goto out_free_alua;
1521 }
1522
1523 /*
1524 * Setup work_queue for QUEUE_FULL
1525 */
1526 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1527
1528 /*
1529 * Preload the initial INQUIRY const values if we are doing
1530 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1531 * passthrough because this is being provided by the backend LLD.
1532 */
1533 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1534 strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1535 strncpy(&dev->t10_wwn.model[0],
1536 dev->transport->inquiry_prod, 16);
1537 strncpy(&dev->t10_wwn.revision[0],
1538 dev->transport->inquiry_rev, 4);
1539 }
1540
1541 scsi_dump_inquiry(dev);
1542
1543 spin_lock(&hba->device_lock);
1544 hba->dev_count++;
1545 spin_unlock(&hba->device_lock);
Nicholas Bellingerd9ea32b2013-08-22 11:33:37 -07001546
1547 mutex_lock(&g_device_mutex);
1548 list_add_tail(&dev->g_dev_node, &g_device_list);
1549 mutex_unlock(&g_device_mutex);
1550
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001551 return 0;
1552
1553out_free_alua:
1554 core_alua_free_lu_gp_mem(dev);
1555out:
1556 se_release_vpd_for_dev(dev);
1557 return ret;
1558}
1559
1560void target_free_device(struct se_device *dev)
1561{
1562 struct se_hba *hba = dev->se_hba;
1563
1564 WARN_ON(!list_empty(&dev->dev_sep_list));
1565
1566 if (dev->dev_flags & DF_CONFIGURED) {
1567 destroy_workqueue(dev->tmr_wq);
1568
Nicholas Bellingerd9ea32b2013-08-22 11:33:37 -07001569 mutex_lock(&g_device_mutex);
1570 list_del(&dev->g_dev_node);
1571 mutex_unlock(&g_device_mutex);
1572
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001573 spin_lock(&hba->device_lock);
1574 hba->dev_count--;
1575 spin_unlock(&hba->device_lock);
1576 }
1577
1578 core_alua_free_lu_gp_mem(dev);
1579 core_scsi3_free_all_registrations(dev);
1580 se_release_vpd_for_dev(dev);
1581
1582 dev->transport->free_device(dev);
1583}
1584
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001585int core_dev_setup_virtual_lun0(void)
1586{
1587 struct se_hba *hba;
1588 struct se_device *dev;
Andy Groverdb5d1c32013-05-28 16:55:20 -07001589 char buf[] = "rd_pages=8,rd_nullio=1";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 int ret;
1591
Andy Grover6708bb22011-06-08 10:36:43 -07001592 hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001593 if (IS_ERR(hba))
1594 return PTR_ERR(hba);
1595
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001596 dev = target_alloc_device(hba, "virt_lun0");
1597 if (!dev) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598 ret = -ENOMEM;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001599 goto out_free_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001601
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001602 hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001603
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001604 ret = target_configure_device(dev);
1605 if (ret)
1606 goto out_free_se_dev;
1607
1608 lun0_hba = hba;
Andy Grovere3d6f902011-07-19 08:55:10 +00001609 g_lun0_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001610 return 0;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001611
1612out_free_se_dev:
1613 target_free_device(dev);
1614out_free_hba:
1615 core_delete_hba(hba);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616 return ret;
1617}
1618
1619
1620void core_dev_release_virtual_lun0(void)
1621{
Andy Grovere3d6f902011-07-19 08:55:10 +00001622 struct se_hba *hba = lun0_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001623
Andy Grover6708bb22011-06-08 10:36:43 -07001624 if (!hba)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625 return;
1626
Andy Grovere3d6f902011-07-19 08:55:10 +00001627 if (g_lun0_dev)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001628 target_free_device(g_lun0_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 core_delete_hba(hba);
1630}