blob: 8f4142fe5f19fdd336bf55bfc761f100ae67ce2c [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 Bellingerfd9a11d2012-11-09 14:51:48 -08007 * (c) Copyright 2003-2012 RisingTide Systems LLC.
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
Andy Grovere3d6f902011-07-19 08:55:10 +000050static struct se_hba *lun0_hba;
Andy Grovere3d6f902011-07-19 08:55:10 +000051/* not static, needed by tpg.c */
52struct se_device *g_lun0_dev;
53
Christoph Hellwigde103c92012-11-06 12:24:09 -080054sense_reason_t
55transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080056{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057 struct se_lun *se_lun = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +000058 struct se_session *se_sess = se_cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +000059 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061
Christoph Hellwigde103c92012-11-06 12:24:09 -080062 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
63 return TCM_NON_EXISTENT_LUN;
Fubo Chend8144952011-02-13 15:13:42 -080064
Roland Dreier78faae32011-07-20 09:09:10 +000065 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
Jörn Engelf2083242012-03-15 15:05:40 -040066 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
Andy Grover5951146d2011-07-19 10:26:37 +000067 if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
68 struct se_dev_entry *deve = se_cmd->se_deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080069
Andy Grover5951146d2011-07-19 10:26:37 +000070 deve->total_cmds++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080071
Andy Grover5951146d2011-07-19 10:26:37 +000072 if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
73 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
Andy Grover6708bb22011-06-08 10:36:43 -070074 pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080075 " Access for 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +000076 se_cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080077 unpacked_lun);
Roland Dreier78faae32011-07-20 09:09:10 +000078 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Christoph Hellwigde103c92012-11-06 12:24:09 -080079 return TCM_WRITE_PROTECTED;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080080 }
Andy Grover5951146d2011-07-19 10:26:37 +000081
82 if (se_cmd->data_direction == DMA_TO_DEVICE)
83 deve->write_bytes += se_cmd->data_length;
84 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
85 deve->read_bytes += se_cmd->data_length;
86
Andy Grover5951146d2011-07-19 10:26:37 +000087 se_lun = deve->se_lun;
88 se_cmd->se_lun = deve->se_lun;
89 se_cmd->pr_res_key = deve->pr_res_key;
90 se_cmd->orig_fe_lun = unpacked_lun;
Andy Grover5951146d2011-07-19 10:26:37 +000091 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
92 }
Roland Dreier78faae32011-07-20 09:09:10 +000093 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +000094
95 if (!se_lun) {
96 /*
97 * Use the se_portal_group->tpg_virt_lun0 to allow for
98 * REPORT_LUNS, et al to be returned when no active
99 * MappedLUN=0 exists for this Initiator Port.
100 */
101 if (unpacked_lun != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700102 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
Andy Grover5951146d2011-07-19 10:26:37 +0000103 " Access for 0x%08x\n",
104 se_cmd->se_tfo->get_fabric_name(),
105 unpacked_lun);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800106 return TCM_NON_EXISTENT_LUN;
Andy Grover5951146d2011-07-19 10:26:37 +0000107 }
108 /*
109 * Force WRITE PROTECT for virtual LUN 0
110 */
111 if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
Christoph Hellwigde103c92012-11-06 12:24:09 -0800112 (se_cmd->data_direction != DMA_NONE))
113 return TCM_WRITE_PROTECTED;
Andy Grover5951146d2011-07-19 10:26:37 +0000114
115 se_lun = &se_sess->se_tpg->tpg_virt_lun0;
116 se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
117 se_cmd->orig_fe_lun = 0;
Andy Grover5951146d2011-07-19 10:26:37 +0000118 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800119 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800120
Andy Grover5951146d2011-07-19 10:26:37 +0000121 /* Directly associate cmd with se_dev */
122 se_cmd->se_dev = se_lun->lun_se_dev;
123
124 /* TODO: get rid of this and use atomics for stats */
125 dev = se_lun->lun_se_dev;
Roland Dreier78faae32011-07-20 09:09:10 +0000126 spin_lock_irqsave(&dev->stats_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800127 dev->num_cmds++;
128 if (se_cmd->data_direction == DMA_TO_DEVICE)
129 dev->write_bytes += se_cmd->data_length;
130 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
131 dev->read_bytes += se_cmd->data_length;
Roland Dreier78faae32011-07-20 09:09:10 +0000132 spin_unlock_irqrestore(&dev->stats_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800133
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800134 spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000135 list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800136 spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
137
138 return 0;
139}
Andy Grover5951146d2011-07-19 10:26:37 +0000140EXPORT_SYMBOL(transport_lookup_cmd_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141
Andy Grover5951146d2011-07-19 10:26:37 +0000142int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800143{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800144 struct se_dev_entry *deve;
145 struct se_lun *se_lun = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +0000146 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
Roland Dreier5e1be912011-07-20 09:28:56 +0000148 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149
Christoph Hellwigde103c92012-11-06 12:24:09 -0800150 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG)
Andy Grovere3d6f902011-07-19 08:55:10 +0000151 return -ENODEV;
Fubo Chend8144952011-02-13 15:13:42 -0800152
Roland Dreier5e1be912011-07-20 09:28:56 +0000153 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
Jörn Engelf2083242012-03-15 15:05:40 -0400154 se_cmd->se_deve = se_sess->se_node_acl->device_list[unpacked_lun];
Andy Grover5951146d2011-07-19 10:26:37 +0000155 deve = se_cmd->se_deve;
156
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800157 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
Andy Grover5951146d2011-07-19 10:26:37 +0000158 se_tmr->tmr_lun = deve->se_lun;
159 se_cmd->se_lun = deve->se_lun;
160 se_lun = deve->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161 se_cmd->pr_res_key = deve->pr_res_key;
162 se_cmd->orig_fe_lun = unpacked_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800163 }
Roland Dreier5e1be912011-07-20 09:28:56 +0000164 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800165
166 if (!se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700167 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168 " Access for 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000169 se_cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800170 unpacked_lun);
Andy Grovere3d6f902011-07-19 08:55:10 +0000171 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800172 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800173
Andy Grover5951146d2011-07-19 10:26:37 +0000174 /* Directly associate cmd with se_dev */
175 se_cmd->se_dev = se_lun->lun_se_dev;
176 se_tmr->tmr_dev = se_lun->lun_se_dev;
177
Roland Dreier5e1be912011-07-20 09:28:56 +0000178 spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000179 list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
Roland Dreier5e1be912011-07-20 09:28:56 +0000180 spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800181
182 return 0;
183}
Andy Grover5951146d2011-07-19 10:26:37 +0000184EXPORT_SYMBOL(transport_lookup_tmr_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800185
186/*
187 * This function is called from core_scsi3_emulate_pro_register_and_move()
188 * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
189 * when a matching rtpi is found.
190 */
191struct se_dev_entry *core_get_se_deve_from_rtpi(
192 struct se_node_acl *nacl,
193 u16 rtpi)
194{
195 struct se_dev_entry *deve;
196 struct se_lun *lun;
197 struct se_port *port;
198 struct se_portal_group *tpg = nacl->se_tpg;
199 u32 i;
200
201 spin_lock_irq(&nacl->device_list_lock);
202 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400203 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204
205 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
206 continue;
207
208 lun = deve->se_lun;
Andy Grover6708bb22011-06-08 10:36:43 -0700209 if (!lun) {
210 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800211 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000212 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213 continue;
214 }
215 port = lun->lun_sep;
Andy Grover6708bb22011-06-08 10:36:43 -0700216 if (!port) {
217 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800218 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000219 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800220 continue;
221 }
222 if (port->sep_rtpi != rtpi)
223 continue;
224
225 atomic_inc(&deve->pr_ref_count);
226 smp_mb__after_atomic_inc();
227 spin_unlock_irq(&nacl->device_list_lock);
228
229 return deve;
230 }
231 spin_unlock_irq(&nacl->device_list_lock);
232
233 return NULL;
234}
235
236int core_free_device_list_for_node(
237 struct se_node_acl *nacl,
238 struct se_portal_group *tpg)
239{
240 struct se_dev_entry *deve;
241 struct se_lun *lun;
242 u32 i;
243
244 if (!nacl->device_list)
245 return 0;
246
247 spin_lock_irq(&nacl->device_list_lock);
248 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400249 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800250
251 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
252 continue;
253
254 if (!deve->se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700255 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800256 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000257 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800258 continue;
259 }
260 lun = deve->se_lun;
261
262 spin_unlock_irq(&nacl->device_list_lock);
Andy Grovere80ac6c2012-07-12 17:34:58 -0700263 core_disable_device_list_for_node(lun, NULL, deve->mapped_lun,
264 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800265 spin_lock_irq(&nacl->device_list_lock);
266 }
267 spin_unlock_irq(&nacl->device_list_lock);
268
Jörn Engelf2083242012-03-15 15:05:40 -0400269 array_free(nacl->device_list, TRANSPORT_MAX_LUNS_PER_TPG);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800270 nacl->device_list = NULL;
271
272 return 0;
273}
274
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800275void core_update_device_list_access(
276 u32 mapped_lun,
277 u32 lun_access,
278 struct se_node_acl *nacl)
279{
280 struct se_dev_entry *deve;
281
282 spin_lock_irq(&nacl->device_list_lock);
Jörn Engelf2083242012-03-15 15:05:40 -0400283 deve = nacl->device_list[mapped_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
285 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
286 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
287 } else {
288 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
289 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
290 }
291 spin_unlock_irq(&nacl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800292}
293
Andy Grovere80ac6c2012-07-12 17:34:58 -0700294/* core_enable_device_list_for_node():
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800295 *
296 *
297 */
Andy Grovere80ac6c2012-07-12 17:34:58 -0700298int core_enable_device_list_for_node(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800299 struct se_lun *lun,
300 struct se_lun_acl *lun_acl,
301 u32 mapped_lun,
302 u32 lun_access,
303 struct se_node_acl *nacl,
Andy Grovere80ac6c2012-07-12 17:34:58 -0700304 struct se_portal_group *tpg)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800305{
306 struct se_port *port = lun->lun_sep;
Andy Grovere80ac6c2012-07-12 17:34:58 -0700307 struct se_dev_entry *deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308
309 spin_lock_irq(&nacl->device_list_lock);
Andy Grovere80ac6c2012-07-12 17:34:58 -0700310
311 deve = nacl->device_list[mapped_lun];
312
313 /*
314 * Check if the call is handling demo mode -> explict LUN ACL
315 * transition. This transition must be for the same struct se_lun
316 * + mapped_lun that was setup in demo mode..
317 */
318 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
319 if (deve->se_lun_acl != NULL) {
320 pr_err("struct se_dev_entry->se_lun_acl"
321 " already set for demo mode -> explict"
322 " LUN ACL transition\n");
323 spin_unlock_irq(&nacl->device_list_lock);
324 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325 }
Andy Grovere80ac6c2012-07-12 17:34:58 -0700326 if (deve->se_lun != lun) {
327 pr_err("struct se_dev_entry->se_lun does"
328 " match passed struct se_lun for demo mode"
329 " -> explict LUN ACL transition\n");
330 spin_unlock_irq(&nacl->device_list_lock);
331 return -EINVAL;
332 }
333 deve->se_lun_acl = lun_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800334
335 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
336 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
337 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
338 } else {
339 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
340 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
341 }
342
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800343 spin_unlock_irq(&nacl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800344 return 0;
345 }
Andy Grovere80ac6c2012-07-12 17:34:58 -0700346
347 deve->se_lun = lun;
348 deve->se_lun_acl = lun_acl;
349 deve->mapped_lun = mapped_lun;
350 deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
351
352 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
353 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
354 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
355 } else {
356 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
357 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
358 }
359
360 deve->creation_time = get_jiffies_64();
361 deve->attach_count++;
362 spin_unlock_irq(&nacl->device_list_lock);
363
364 spin_lock_bh(&port->sep_alua_lock);
365 list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
366 spin_unlock_bh(&port->sep_alua_lock);
367
368 return 0;
369}
370
371/* core_disable_device_list_for_node():
372 *
373 *
374 */
375int core_disable_device_list_for_node(
376 struct se_lun *lun,
377 struct se_lun_acl *lun_acl,
378 u32 mapped_lun,
379 u32 lun_access,
380 struct se_node_acl *nacl,
381 struct se_portal_group *tpg)
382{
383 struct se_port *port = lun->lun_sep;
Nicholas Bellinger77d4c742012-07-14 15:11:41 -0700384 struct se_dev_entry *deve = nacl->device_list[mapped_lun];
Andy Grovere80ac6c2012-07-12 17:34:58 -0700385
386 /*
387 * If the MappedLUN entry is being disabled, the entry in
388 * port->sep_alua_list must be removed now before clearing the
389 * struct se_dev_entry pointers below as logic in
390 * core_alua_do_transition_tg_pt() depends on these being present.
391 *
392 * deve->se_lun_acl will be NULL for demo-mode created LUNs
393 * that have not been explicitly converted to MappedLUNs ->
394 * struct se_lun_acl, but we remove deve->alua_port_list from
395 * port->sep_alua_list. This also means that active UAs and
396 * NodeACL context specific PR metadata for demo-mode
397 * MappedLUN *deve will be released below..
398 */
399 spin_lock_bh(&port->sep_alua_lock);
400 list_del(&deve->alua_port_list);
401 spin_unlock_bh(&port->sep_alua_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 /*
403 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
404 * PR operation to complete.
405 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 while (atomic_read(&deve->pr_ref_count) != 0)
407 cpu_relax();
Nicholas Bellinger77d4c742012-07-14 15:11:41 -0700408
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800409 spin_lock_irq(&nacl->device_list_lock);
410 /*
411 * Disable struct se_dev_entry LUN ACL mapping
412 */
413 core_scsi3_ua_release_all(deve);
414 deve->se_lun = NULL;
415 deve->se_lun_acl = NULL;
416 deve->lun_flags = 0;
417 deve->creation_time = 0;
418 deve->attach_count--;
419 spin_unlock_irq(&nacl->device_list_lock);
420
421 core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
422 return 0;
423}
424
425/* core_clear_lun_from_tpg():
426 *
427 *
428 */
429void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
430{
431 struct se_node_acl *nacl;
432 struct se_dev_entry *deve;
433 u32 i;
434
Roland Dreier28638882011-08-16 09:40:01 -0700435 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800436 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
Roland Dreier28638882011-08-16 09:40:01 -0700437 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800438
439 spin_lock_irq(&nacl->device_list_lock);
440 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Jörn Engelf2083242012-03-15 15:05:40 -0400441 deve = nacl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800442 if (lun != deve->se_lun)
443 continue;
444 spin_unlock_irq(&nacl->device_list_lock);
445
Andy Grovere80ac6c2012-07-12 17:34:58 -0700446 core_disable_device_list_for_node(lun, NULL,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800447 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
Andy Grovere80ac6c2012-07-12 17:34:58 -0700448 nacl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800449
450 spin_lock_irq(&nacl->device_list_lock);
451 }
452 spin_unlock_irq(&nacl->device_list_lock);
453
Roland Dreier28638882011-08-16 09:40:01 -0700454 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800455 }
Roland Dreier28638882011-08-16 09:40:01 -0700456 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800457}
458
459static struct se_port *core_alloc_port(struct se_device *dev)
460{
461 struct se_port *port, *port_tmp;
462
463 port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700464 if (!port) {
465 pr_err("Unable to allocate struct se_port\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000466 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800467 }
468 INIT_LIST_HEAD(&port->sep_alua_list);
469 INIT_LIST_HEAD(&port->sep_list);
470 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
471 spin_lock_init(&port->sep_alua_lock);
472 mutex_init(&port->sep_tg_pt_md_mutex);
473
474 spin_lock(&dev->se_port_lock);
475 if (dev->dev_port_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -0700476 pr_warn("Reached dev->dev_port_count =="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800477 " 0x0000ffff\n");
478 spin_unlock(&dev->se_port_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000479 return ERR_PTR(-ENOSPC);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800480 }
481again:
482 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900483 * Allocate the next RELATIVE TARGET PORT IDENTIFIER for this struct se_device
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800484 * Here is the table from spc4r17 section 7.7.3.8.
485 *
486 * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
487 *
488 * Code Description
489 * 0h Reserved
490 * 1h Relative port 1, historically known as port A
491 * 2h Relative port 2, historically known as port B
492 * 3h to FFFFh Relative port 3 through 65 535
493 */
494 port->sep_rtpi = dev->dev_rpti_counter++;
Andy Grover6708bb22011-06-08 10:36:43 -0700495 if (!port->sep_rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800496 goto again;
497
498 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
499 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900500 * Make sure RELATIVE TARGET PORT IDENTIFIER is unique
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800501 * for 16-bit wrap..
502 */
503 if (port->sep_rtpi == port_tmp->sep_rtpi)
504 goto again;
505 }
506 spin_unlock(&dev->se_port_lock);
507
508 return port;
509}
510
511static void core_export_port(
512 struct se_device *dev,
513 struct se_portal_group *tpg,
514 struct se_port *port,
515 struct se_lun *lun)
516{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800517 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
518
519 spin_lock(&dev->se_port_lock);
520 spin_lock(&lun->lun_sep_lock);
521 port->sep_tpg = tpg;
522 port->sep_lun = lun;
523 lun->lun_sep = port;
524 spin_unlock(&lun->lun_sep_lock);
525
526 list_add_tail(&port->sep_list, &dev->dev_sep_list);
527 spin_unlock(&dev->se_port_lock);
528
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400529 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
530 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800531 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
532 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
Andy Grover6708bb22011-06-08 10:36:43 -0700533 pr_err("Unable to allocate t10_alua_tg_pt"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 "_gp_member_t\n");
535 return;
536 }
537 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
538 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400539 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800540 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700541 pr_debug("%s/%s: Adding to default ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800542 " Group: alua/default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000543 dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800544 }
545
546 dev->dev_port_count++;
Masanari Iida35d1efe2012-08-16 22:43:13 +0900547 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFIER */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800548}
549
550/*
551 * Called with struct se_device->se_port_lock spinlock held.
552 */
553static void core_release_port(struct se_device *dev, struct se_port *port)
Dan Carpenter5dd7ed22011-03-14 04:06:01 -0700554 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800555{
556 /*
557 * Wait for any port reference for PR ALL_TG_PT=1 operation
558 * to complete in __core_scsi3_alloc_registration()
559 */
560 spin_unlock(&dev->se_port_lock);
561 if (atomic_read(&port->sep_tg_pt_ref_cnt))
562 cpu_relax();
563 spin_lock(&dev->se_port_lock);
564
565 core_alua_free_tg_pt_gp_mem(port);
566
567 list_del(&port->sep_list);
568 dev->dev_port_count--;
569 kfree(port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800570}
571
572int core_dev_export(
573 struct se_device *dev,
574 struct se_portal_group *tpg,
575 struct se_lun *lun)
576{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400577 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578 struct se_port *port;
579
580 port = core_alloc_port(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000581 if (IS_ERR(port))
582 return PTR_ERR(port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800583
584 lun->lun_se_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400586 spin_lock(&hba->device_lock);
587 dev->export_count++;
588 spin_unlock(&hba->device_lock);
589
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800590 core_export_port(dev, tpg, port, lun);
591 return 0;
592}
593
594void core_dev_unexport(
595 struct se_device *dev,
596 struct se_portal_group *tpg,
597 struct se_lun *lun)
598{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400599 struct se_hba *hba = dev->se_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800600 struct se_port *port = lun->lun_sep;
601
602 spin_lock(&lun->lun_sep_lock);
603 if (lun->lun_se_dev == NULL) {
604 spin_unlock(&lun->lun_sep_lock);
605 return;
606 }
607 spin_unlock(&lun->lun_sep_lock);
608
609 spin_lock(&dev->se_port_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800610 core_release_port(dev, port);
611 spin_unlock(&dev->se_port_lock);
612
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400613 spin_lock(&hba->device_lock);
614 dev->export_count--;
615 spin_unlock(&hba->device_lock);
616
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617 lun->lun_se_dev = NULL;
618}
619
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400620static void se_release_vpd_for_dev(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621{
622 struct t10_vpd *vpd, *vpd_tmp;
623
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400624 spin_lock(&dev->t10_wwn.t10_vpd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800625 list_for_each_entry_safe(vpd, vpd_tmp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400626 &dev->t10_wwn.t10_vpd_list, vpd_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800627 list_del(&vpd->vpd_list);
628 kfree(vpd);
629 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400630 spin_unlock(&dev->t10_wwn.t10_vpd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800631}
632
Roland Dreierc8045372012-07-16 15:17:12 -0700633static u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700634{
Roland Dreier3e039892012-10-31 09:16:45 -0700635 u32 aligned_max_sectors;
636 u32 alignment;
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700637 /*
638 * Limit max_sectors to a PAGE_SIZE aligned value for modern
639 * transport_allocate_data_tasks() operation.
640 */
Roland Dreier3e039892012-10-31 09:16:45 -0700641 alignment = max(1ul, PAGE_SIZE / block_size);
642 aligned_max_sectors = rounddown(max_sectors, alignment);
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700643
Roland Dreier3e039892012-10-31 09:16:45 -0700644 if (max_sectors != aligned_max_sectors)
645 pr_info("Rounding down aligned max_sectors from %u to %u\n",
646 max_sectors, aligned_max_sectors);
647
648 return aligned_max_sectors;
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700649}
650
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651int se_dev_set_max_unmap_lba_count(
652 struct se_device *dev,
653 u32 max_unmap_lba_count)
654{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400655 dev->dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
Andy Grover6708bb22011-06-08 10:36:43 -0700656 pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400657 dev, dev->dev_attrib.max_unmap_lba_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658 return 0;
659}
660
661int se_dev_set_max_unmap_block_desc_count(
662 struct se_device *dev,
663 u32 max_unmap_block_desc_count)
664{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400665 dev->dev_attrib.max_unmap_block_desc_count =
Andy Grovere3d6f902011-07-19 08:55:10 +0000666 max_unmap_block_desc_count;
Andy Grover6708bb22011-06-08 10:36:43 -0700667 pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400668 dev, dev->dev_attrib.max_unmap_block_desc_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669 return 0;
670}
671
672int se_dev_set_unmap_granularity(
673 struct se_device *dev,
674 u32 unmap_granularity)
675{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400676 dev->dev_attrib.unmap_granularity = unmap_granularity;
Andy Grover6708bb22011-06-08 10:36:43 -0700677 pr_debug("dev[%p]: Set unmap_granularity: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400678 dev, dev->dev_attrib.unmap_granularity);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800679 return 0;
680}
681
682int se_dev_set_unmap_granularity_alignment(
683 struct se_device *dev,
684 u32 unmap_granularity_alignment)
685{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400686 dev->dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
Andy Grover6708bb22011-06-08 10:36:43 -0700687 pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400688 dev, dev->dev_attrib.unmap_granularity_alignment);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689 return 0;
690}
691
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -0800692int se_dev_set_max_write_same_len(
693 struct se_device *dev,
694 u32 max_write_same_len)
695{
696 dev->dev_attrib.max_write_same_len = max_write_same_len;
697 pr_debug("dev[%p]: Set max_write_same_len: %u\n",
698 dev, dev->dev_attrib.max_write_same_len);
699 return 0;
700}
701
Tregaron Baylyadfa9572013-01-31 15:30:24 -0700702static void dev_set_t10_wwn_model_alias(struct se_device *dev)
703{
704 const char *configname;
705
706 configname = config_item_name(&dev->dev_group.cg_item);
707 if (strlen(configname) >= 16) {
708 pr_warn("dev[%p]: Backstore name '%s' is too long for "
709 "INQUIRY_MODEL, truncating to 16 bytes\n", dev,
710 configname);
711 }
712 snprintf(&dev->t10_wwn.model[0], 16, "%s", configname);
713}
714
715int se_dev_set_emulate_model_alias(struct se_device *dev, int flag)
716{
717 if (dev->export_count) {
718 pr_err("dev[%p]: Unable to change model alias"
719 " while export_count is %d\n",
720 dev, dev->export_count);
721 return -EINVAL;
722 }
723
724 if (flag != 0 && flag != 1) {
725 pr_err("Illegal value %d\n", flag);
726 return -EINVAL;
727 }
728
729 if (flag) {
730 dev_set_t10_wwn_model_alias(dev);
731 } else {
732 strncpy(&dev->t10_wwn.model[0],
733 dev->transport->inquiry_prod, 16);
734 }
735 dev->dev_attrib.emulate_model_alias = flag;
736
737 return 0;
738}
739
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800740int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
741{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400742 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700743 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000744 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800745 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400746
Andy Groverc6388302011-11-30 12:11:50 -0800747 if (flag) {
748 pr_err("dpo_emulated not supported\n");
749 return -EINVAL;
750 }
751
752 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800753}
754
755int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
756{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400757 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700758 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000759 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800760 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400761
Nicholas Bellingerfd30e932012-08-26 13:35:58 -0700762 if (flag &&
763 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
764 pr_err("emulate_fua_write not supported for pSCSI\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000765 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800766 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400767 dev->dev_attrib.emulate_fua_write = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700768 pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400769 dev, dev->dev_attrib.emulate_fua_write);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800770 return 0;
771}
772
773int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
774{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400775 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700776 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000777 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800778 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400779
Andy Groverc6388302011-11-30 12:11:50 -0800780 if (flag) {
781 pr_err("ua read emulated not supported\n");
782 return -EINVAL;
783 }
784
785 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800786}
787
788int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
789{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400790 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700791 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000792 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800793 }
Nicholas Bellingerfd30e932012-08-26 13:35:58 -0700794 if (flag &&
795 dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
796 pr_err("emulate_write_cache not supported for pSCSI\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000797 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800798 }
Nicholas Bellingerd0c8b252013-01-29 22:10:06 -0800799 if (dev->transport->get_write_cache) {
800 pr_warn("emulate_write_cache cannot be changed when underlying"
801 " HW reports WriteCacheEnabled, ignoring request\n");
802 return 0;
803 }
804
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400805 dev->dev_attrib.emulate_write_cache = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700806 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400807 dev, dev->dev_attrib.emulate_write_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800808 return 0;
809}
810
811int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
812{
813 if ((flag != 0) && (flag != 1) && (flag != 2)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700814 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000815 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800816 }
817
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400818 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700819 pr_err("dev[%p]: Unable to change SE Device"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400820 " UA_INTRLCK_CTRL while export_count is %d\n",
821 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +0000822 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800823 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400824 dev->dev_attrib.emulate_ua_intlck_ctrl = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700825 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400826 dev, dev->dev_attrib.emulate_ua_intlck_ctrl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800827
828 return 0;
829}
830
831int se_dev_set_emulate_tas(struct se_device *dev, int flag)
832{
833 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700834 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000835 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800836 }
837
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400838 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700839 pr_err("dev[%p]: Unable to change SE Device TAS while"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400840 " export_count is %d\n",
841 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +0000842 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800843 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400844 dev->dev_attrib.emulate_tas = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700845 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400846 dev, (dev->dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847
848 return 0;
849}
850
851int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
852{
853 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700854 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000855 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856 }
857 /*
858 * We expect this value to be non-zero when generic Block Layer
859 * Discard supported is detected iblock_create_virtdevice().
860 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400861 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700862 pr_err("Generic Block Discard not supported\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800863 return -ENOSYS;
864 }
865
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400866 dev->dev_attrib.emulate_tpu = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700867 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800868 dev, flag);
869 return 0;
870}
871
872int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
873{
874 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700875 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000876 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800877 }
878 /*
879 * We expect this value to be non-zero when generic Block Layer
880 * Discard supported is detected iblock_create_virtdevice().
881 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400882 if (flag && !dev->dev_attrib.max_unmap_block_desc_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700883 pr_err("Generic Block Discard not supported\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800884 return -ENOSYS;
885 }
886
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400887 dev->dev_attrib.emulate_tpws = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700888 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889 dev, flag);
890 return 0;
891}
892
893int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
894{
895 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700896 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000897 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800898 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400899 dev->dev_attrib.enforce_pr_isids = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700900 pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400901 (dev->dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800902 return 0;
903}
904
Roland Dreiere22a7f02011-07-05 13:34:52 -0700905int se_dev_set_is_nonrot(struct se_device *dev, int flag)
906{
907 if ((flag != 0) && (flag != 1)) {
908 printk(KERN_ERR "Illegal value %d\n", flag);
909 return -EINVAL;
910 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400911 dev->dev_attrib.is_nonrot = flag;
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700912 pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
Roland Dreiere22a7f02011-07-05 13:34:52 -0700913 dev, flag);
914 return 0;
915}
916
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700917int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
918{
919 if (flag != 0) {
920 printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
921 " reordering not implemented\n", dev);
922 return -ENOSYS;
923 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400924 dev->dev_attrib.emulate_rest_reord = flag;
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700925 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
926 return 0;
927}
928
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800929/*
930 * Note, this can only be called on unexported SE Device Object.
931 */
932int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
933{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400934 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -0700935 pr_err("dev[%p]: Unable to change SE Device TCQ while"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400936 " export_count is %d\n",
937 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +0000938 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800939 }
Andy Grover6708bb22011-06-08 10:36:43 -0700940 if (!queue_depth) {
941 pr_err("dev[%p]: Illegal ZERO value for queue"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800942 "_depth\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000943 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800944 }
945
Andy Grovere3d6f902011-07-19 08:55:10 +0000946 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400947 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -0700948 pr_err("dev[%p]: Passed queue_depth: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949 " exceeds TCM/SE_Device TCQ: %u\n",
950 dev, queue_depth,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400951 dev->dev_attrib.hw_queue_depth);
Andy Grovere3d6f902011-07-19 08:55:10 +0000952 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 }
954 } else {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400955 if (queue_depth > dev->dev_attrib.queue_depth) {
956 if (queue_depth > dev->dev_attrib.hw_queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -0700957 pr_err("dev[%p]: Passed queue_depth:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 " %u exceeds TCM/SE_Device MAX"
959 " TCQ: %u\n", dev, queue_depth,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400960 dev->dev_attrib.hw_queue_depth);
Andy Grovere3d6f902011-07-19 08:55:10 +0000961 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800962 }
963 }
964 }
965
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400966 dev->dev_attrib.queue_depth = dev->queue_depth = queue_depth;
Andy Grover6708bb22011-06-08 10:36:43 -0700967 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800968 dev, queue_depth);
969 return 0;
970}
971
Roland Dreier015487b2012-02-13 16:18:17 -0800972int se_dev_set_fabric_max_sectors(struct se_device *dev, u32 fabric_max_sectors)
973{
Nicholas Bellinger7a3cf6c2013-01-31 15:05:48 -0800974 int block_size = dev->dev_attrib.block_size;
975
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400976 if (dev->export_count) {
Roland Dreier015487b2012-02-13 16:18:17 -0800977 pr_err("dev[%p]: Unable to change SE Device"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400978 " fabric_max_sectors while export_count is %d\n",
979 dev, dev->export_count);
Roland Dreier015487b2012-02-13 16:18:17 -0800980 return -EINVAL;
981 }
982 if (!fabric_max_sectors) {
983 pr_err("dev[%p]: Illegal ZERO value for"
984 " fabric_max_sectors\n", dev);
985 return -EINVAL;
986 }
987 if (fabric_max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
988 pr_err("dev[%p]: Passed fabric_max_sectors: %u less than"
989 " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, fabric_max_sectors,
990 DA_STATUS_MAX_SECTORS_MIN);
991 return -EINVAL;
992 }
993 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400994 if (fabric_max_sectors > dev->dev_attrib.hw_max_sectors) {
Roland Dreier015487b2012-02-13 16:18:17 -0800995 pr_err("dev[%p]: Passed fabric_max_sectors: %u"
996 " greater than TCM/SE_Device max_sectors:"
997 " %u\n", dev, fabric_max_sectors,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400998 dev->dev_attrib.hw_max_sectors);
Roland Dreier015487b2012-02-13 16:18:17 -0800999 return -EINVAL;
1000 }
1001 } else {
1002 if (fabric_max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
1003 pr_err("dev[%p]: Passed fabric_max_sectors: %u"
1004 " greater than DA_STATUS_MAX_SECTORS_MAX:"
1005 " %u\n", dev, fabric_max_sectors,
1006 DA_STATUS_MAX_SECTORS_MAX);
1007 return -EINVAL;
1008 }
1009 }
1010 /*
1011 * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
1012 */
Nicholas Bellinger7a3cf6c2013-01-31 15:05:48 -08001013 if (!block_size) {
1014 block_size = 512;
1015 pr_warn("Defaulting to 512 for zero block_size\n");
1016 }
Roland Dreier015487b2012-02-13 16:18:17 -08001017 fabric_max_sectors = se_dev_align_max_sectors(fabric_max_sectors,
Nicholas Bellinger7a3cf6c2013-01-31 15:05:48 -08001018 block_size);
Roland Dreier015487b2012-02-13 16:18:17 -08001019
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001020 dev->dev_attrib.fabric_max_sectors = fabric_max_sectors;
Roland Dreier015487b2012-02-13 16:18:17 -08001021 pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
1022 dev, fabric_max_sectors);
1023 return 0;
1024}
1025
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1027{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001028 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001029 pr_err("dev[%p]: Unable to change SE Device"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001030 " optimal_sectors while export_count is %d\n",
1031 dev, dev->export_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001032 return -EINVAL;
1033 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001034 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Andy Grover6708bb22011-06-08 10:36:43 -07001035 pr_err("dev[%p]: Passed optimal_sectors cannot be"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001036 " changed for TCM/pSCSI\n", dev);
1037 return -EINVAL;
1038 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001039 if (optimal_sectors > dev->dev_attrib.fabric_max_sectors) {
Andy Grover6708bb22011-06-08 10:36:43 -07001040 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
Roland Dreier015487b2012-02-13 16:18:17 -08001041 " greater than fabric_max_sectors: %u\n", dev,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001042 optimal_sectors, dev->dev_attrib.fabric_max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001043 return -EINVAL;
1044 }
1045
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001046 dev->dev_attrib.optimal_sectors = optimal_sectors;
Andy Grover6708bb22011-06-08 10:36:43 -07001047 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048 dev, optimal_sectors);
1049 return 0;
1050}
1051
1052int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1053{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001054 if (dev->export_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001055 pr_err("dev[%p]: Unable to change SE Device block_size"
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001056 " while export_count is %d\n",
1057 dev, dev->export_count);
Andy Grovere3d6f902011-07-19 08:55:10 +00001058 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001059 }
1060
1061 if ((block_size != 512) &&
1062 (block_size != 1024) &&
1063 (block_size != 2048) &&
1064 (block_size != 4096)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001065 pr_err("dev[%p]: Illegal value for block_device: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001066 " for SE device, must be 512, 1024, 2048 or 4096\n",
1067 dev, block_size);
Andy Grovere3d6f902011-07-19 08:55:10 +00001068 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069 }
1070
Andy Grovere3d6f902011-07-19 08:55:10 +00001071 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Andy Grover6708bb22011-06-08 10:36:43 -07001072 pr_err("dev[%p]: Not allowed to change block_size for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001073 " Physical Device, use for Linux/SCSI to change"
1074 " block_size for underlying hardware\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00001075 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001076 }
1077
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001078 dev->dev_attrib.block_size = block_size;
Andy Grover6708bb22011-06-08 10:36:43 -07001079 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001080 dev, block_size);
1081 return 0;
1082}
1083
1084struct se_lun *core_dev_add_lun(
1085 struct se_portal_group *tpg,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001086 struct se_device *dev,
1087 u32 lun)
1088{
1089 struct se_lun *lun_p;
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001090 int rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001091
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001092 lun_p = core_tpg_pre_addlun(tpg, lun);
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001093 if (IS_ERR(lun_p))
1094 return lun_p;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001095
Nicholas Bellinger58d92612012-03-20 21:26:48 -07001096 rc = core_tpg_post_addlun(tpg, lun_p,
1097 TRANSPORT_LUNFLAGS_READ_WRITE, dev);
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001098 if (rc < 0)
1099 return ERR_PTR(rc);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001100
Andy Grover6708bb22011-06-08 10:36:43 -07001101 pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
Andy Grovere3d6f902011-07-19 08:55:10 +00001102 " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1103 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
Andy Grover2dca6732012-07-12 17:34:55 -07001104 tpg->se_tpg_tfo->get_fabric_name(), dev->se_hba->hba_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001105 /*
1106 * Update LUN maps for dynamically added initiators when
1107 * generate_node_acl is enabled.
1108 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001109 if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 struct se_node_acl *acl;
Roland Dreier28638882011-08-16 09:40:01 -07001111 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001112 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
Nicholas Bellinger052605c2011-07-26 17:48:43 -07001113 if (acl->dynamic_node_acl &&
1114 (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
1115 !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
Roland Dreier28638882011-08-16 09:40:01 -07001116 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001117 core_tpg_add_node_to_devs(acl, tpg);
Roland Dreier28638882011-08-16 09:40:01 -07001118 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001119 }
1120 }
Roland Dreier28638882011-08-16 09:40:01 -07001121 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001122 }
1123
1124 return lun_p;
1125}
1126
1127/* core_dev_del_lun():
1128 *
1129 *
1130 */
1131int core_dev_del_lun(
1132 struct se_portal_group *tpg,
1133 u32 unpacked_lun)
1134{
1135 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001136
Sebastian Andrzej Siewior8d9efe52012-01-11 21:43:38 +01001137 lun = core_tpg_pre_dellun(tpg, unpacked_lun);
1138 if (IS_ERR(lun))
1139 return PTR_ERR(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140
1141 core_tpg_post_dellun(tpg, lun);
1142
Andy Grover6708bb22011-06-08 10:36:43 -07001143 pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
Andy Grovere3d6f902011-07-19 08:55:10 +00001144 " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
1145 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
1146 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001147
1148 return 0;
1149}
1150
1151struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
1152{
1153 struct se_lun *lun;
1154
1155 spin_lock(&tpg->tpg_lun_lock);
1156 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001157 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001158 "_PER_TPG-1: %u for Target Portal Group: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001159 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001160 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +00001161 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 spin_unlock(&tpg->tpg_lun_lock);
1163 return NULL;
1164 }
Jörn Engel4a5a75f2012-03-15 15:05:12 -04001165 lun = tpg->tpg_lun_list[unpacked_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166
1167 if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001168 pr_err("%s Logical Unit Number: %u is not free on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001170 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1171 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172 spin_unlock(&tpg->tpg_lun_lock);
1173 return NULL;
1174 }
1175 spin_unlock(&tpg->tpg_lun_lock);
1176
1177 return lun;
1178}
1179
1180/* core_dev_get_lun():
1181 *
1182 *
1183 */
1184static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
1185{
1186 struct se_lun *lun;
1187
1188 spin_lock(&tpg->tpg_lun_lock);
1189 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001190 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 "_TPG-1: %u for Target Portal Group: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001192 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +00001194 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195 spin_unlock(&tpg->tpg_lun_lock);
1196 return NULL;
1197 }
Jörn Engel4a5a75f2012-03-15 15:05:12 -04001198 lun = tpg->tpg_lun_list[unpacked_lun];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199
1200 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001201 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001203 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1204 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001205 spin_unlock(&tpg->tpg_lun_lock);
1206 return NULL;
1207 }
1208 spin_unlock(&tpg->tpg_lun_lock);
1209
1210 return lun;
1211}
1212
1213struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1214 struct se_portal_group *tpg,
Nicholas Bellingerfcf29482013-02-18 18:00:33 -08001215 struct se_node_acl *nacl,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001216 u32 mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217 int *ret)
1218{
1219 struct se_lun_acl *lacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001220
Nicholas Bellingerfcf29482013-02-18 18:00:33 -08001221 if (strlen(nacl->initiatorname) >= TRANSPORT_IQN_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001222 pr_err("%s InitiatorName exceeds maximum size.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001223 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224 *ret = -EOVERFLOW;
1225 return NULL;
1226 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001228 if (!lacl) {
1229 pr_err("Unable to allocate memory for struct se_lun_acl.\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001230 *ret = -ENOMEM;
1231 return NULL;
1232 }
1233
1234 INIT_LIST_HEAD(&lacl->lacl_list);
1235 lacl->mapped_lun = mapped_lun;
1236 lacl->se_lun_nacl = nacl;
Nicholas Bellingerfcf29482013-02-18 18:00:33 -08001237 snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s",
1238 nacl->initiatorname);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001239
1240 return lacl;
1241}
1242
1243int core_dev_add_initiator_node_lun_acl(
1244 struct se_portal_group *tpg,
1245 struct se_lun_acl *lacl,
1246 u32 unpacked_lun,
1247 u32 lun_access)
1248{
1249 struct se_lun *lun;
1250 struct se_node_acl *nacl;
1251
1252 lun = core_dev_get_lun(tpg, unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -07001253 if (!lun) {
1254 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001256 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1257 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258 return -EINVAL;
1259 }
1260
1261 nacl = lacl->se_lun_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07001262 if (!nacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001263 return -EINVAL;
1264
1265 if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
1266 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
1267 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1268
1269 lacl->se_lun = lun;
1270
Andy Grovere80ac6c2012-07-12 17:34:58 -07001271 if (core_enable_device_list_for_node(lun, lacl, lacl->mapped_lun,
1272 lun_access, nacl, tpg) < 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001273 return -EINVAL;
1274
1275 spin_lock(&lun->lun_acl_lock);
1276 list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1277 atomic_inc(&lun->lun_acl_count);
1278 smp_mb__after_atomic_inc();
1279 spin_unlock(&lun->lun_acl_lock);
1280
Andy Grover6708bb22011-06-08 10:36:43 -07001281 pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
Andy Grovere3d6f902011-07-19 08:55:10 +00001282 " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
1283 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
1285 lacl->initiatorname);
1286 /*
1287 * Check to see if there are any existing persistent reservation APTPL
1288 * pre-registrations that need to be enabled for this LUN ACL..
1289 */
1290 core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
1291 return 0;
1292}
1293
1294/* core_dev_del_initiator_node_lun_acl():
1295 *
1296 *
1297 */
1298int core_dev_del_initiator_node_lun_acl(
1299 struct se_portal_group *tpg,
1300 struct se_lun *lun,
1301 struct se_lun_acl *lacl)
1302{
1303 struct se_node_acl *nacl;
1304
1305 nacl = lacl->se_lun_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07001306 if (!nacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307 return -EINVAL;
1308
1309 spin_lock(&lun->lun_acl_lock);
1310 list_del(&lacl->lacl_list);
1311 atomic_dec(&lun->lun_acl_count);
1312 smp_mb__after_atomic_dec();
1313 spin_unlock(&lun->lun_acl_lock);
1314
Andy Grovere80ac6c2012-07-12 17:34:58 -07001315 core_disable_device_list_for_node(lun, NULL, lacl->mapped_lun,
1316 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001317
1318 lacl->se_lun = NULL;
1319
Andy Grover6708bb22011-06-08 10:36:43 -07001320 pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001321 " InitiatorNode: %s Mapped LUN: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001322 tpg->se_tpg_tfo->get_fabric_name(),
1323 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001324 lacl->initiatorname, lacl->mapped_lun);
1325
1326 return 0;
1327}
1328
1329void core_dev_free_initiator_node_lun_acl(
1330 struct se_portal_group *tpg,
1331 struct se_lun_acl *lacl)
1332{
Andy Grover6708bb22011-06-08 10:36:43 -07001333 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
Andy Grovere3d6f902011-07-19 08:55:10 +00001334 " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1335 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1336 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 lacl->initiatorname, lacl->mapped_lun);
1338
1339 kfree(lacl);
1340}
1341
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001342static void scsi_dump_inquiry(struct se_device *dev)
1343{
1344 struct t10_wwn *wwn = &dev->t10_wwn;
1345 char buf[17];
1346 int i, device_type;
1347 /*
1348 * Print Linux/SCSI style INQUIRY formatting to the kernel ring buffer
1349 */
1350 for (i = 0; i < 8; i++)
1351 if (wwn->vendor[i] >= 0x20)
1352 buf[i] = wwn->vendor[i];
1353 else
1354 buf[i] = ' ';
1355 buf[i] = '\0';
1356 pr_debug(" Vendor: %s\n", buf);
1357
1358 for (i = 0; i < 16; i++)
1359 if (wwn->model[i] >= 0x20)
1360 buf[i] = wwn->model[i];
1361 else
1362 buf[i] = ' ';
1363 buf[i] = '\0';
1364 pr_debug(" Model: %s\n", buf);
1365
1366 for (i = 0; i < 4; i++)
1367 if (wwn->revision[i] >= 0x20)
1368 buf[i] = wwn->revision[i];
1369 else
1370 buf[i] = ' ';
1371 buf[i] = '\0';
1372 pr_debug(" Revision: %s\n", buf);
1373
1374 device_type = dev->transport->get_device_type(dev);
1375 pr_debug(" Type: %s ", scsi_device_type(device_type));
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001376}
1377
1378struct se_device *target_alloc_device(struct se_hba *hba, const char *name)
1379{
1380 struct se_device *dev;
1381
1382 dev = hba->transport->alloc_device(hba, name);
1383 if (!dev)
1384 return NULL;
1385
Nicholas Bellinger0ff87542012-12-04 23:43:57 -08001386 dev->dev_link_magic = SE_DEV_LINK_MAGIC;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001387 dev->se_hba = hba;
1388 dev->transport = hba->transport;
1389
1390 INIT_LIST_HEAD(&dev->dev_list);
1391 INIT_LIST_HEAD(&dev->dev_sep_list);
1392 INIT_LIST_HEAD(&dev->dev_tmr_list);
1393 INIT_LIST_HEAD(&dev->delayed_cmd_list);
1394 INIT_LIST_HEAD(&dev->state_list);
1395 INIT_LIST_HEAD(&dev->qf_cmd_list);
1396 spin_lock_init(&dev->stats_lock);
1397 spin_lock_init(&dev->execute_task_lock);
1398 spin_lock_init(&dev->delayed_cmd_lock);
1399 spin_lock_init(&dev->dev_reservation_lock);
1400 spin_lock_init(&dev->se_port_lock);
1401 spin_lock_init(&dev->se_tmr_lock);
1402 spin_lock_init(&dev->qf_cmd_lock);
1403 atomic_set(&dev->dev_ordered_id, 0);
1404 INIT_LIST_HEAD(&dev->t10_wwn.t10_vpd_list);
1405 spin_lock_init(&dev->t10_wwn.t10_vpd_lock);
1406 INIT_LIST_HEAD(&dev->t10_pr.registration_list);
1407 INIT_LIST_HEAD(&dev->t10_pr.aptpl_reg_list);
1408 spin_lock_init(&dev->t10_pr.registration_lock);
1409 spin_lock_init(&dev->t10_pr.aptpl_reg_lock);
1410 INIT_LIST_HEAD(&dev->t10_alua.tg_pt_gps_list);
1411 spin_lock_init(&dev->t10_alua.tg_pt_gps_lock);
1412
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001413 dev->t10_wwn.t10_dev = dev;
1414 dev->t10_alua.t10_dev = dev;
1415
1416 dev->dev_attrib.da_dev = dev;
Tregaron Baylyadfa9572013-01-31 15:30:24 -07001417 dev->dev_attrib.emulate_model_alias = DA_EMULATE_MODEL_ALIAS;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001418 dev->dev_attrib.emulate_dpo = DA_EMULATE_DPO;
1419 dev->dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
1420 dev->dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
1421 dev->dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
1422 dev->dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
1423 dev->dev_attrib.emulate_tas = DA_EMULATE_TAS;
1424 dev->dev_attrib.emulate_tpu = DA_EMULATE_TPU;
1425 dev->dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001426 dev->dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
1427 dev->dev_attrib.is_nonrot = DA_IS_NONROT;
1428 dev->dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
1429 dev->dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
1430 dev->dev_attrib.max_unmap_block_desc_count =
1431 DA_MAX_UNMAP_BLOCK_DESC_COUNT;
1432 dev->dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
1433 dev->dev_attrib.unmap_granularity_alignment =
1434 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
Nicholas Bellinger773cbaf2012-11-15 11:02:49 -08001435 dev->dev_attrib.max_write_same_len = DA_MAX_WRITE_SAME_LEN;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001436 dev->dev_attrib.fabric_max_sectors = DA_FABRIC_MAX_SECTORS;
1437 dev->dev_attrib.optimal_sectors = DA_FABRIC_MAX_SECTORS;
1438
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001439 return dev;
1440}
1441
1442int target_configure_device(struct se_device *dev)
1443{
1444 struct se_hba *hba = dev->se_hba;
1445 int ret;
1446
1447 if (dev->dev_flags & DF_CONFIGURED) {
1448 pr_err("se_dev->se_dev_ptr already set for storage"
1449 " object\n");
1450 return -EEXIST;
1451 }
1452
1453 ret = dev->transport->configure_device(dev);
1454 if (ret)
1455 goto out;
1456 dev->dev_flags |= DF_CONFIGURED;
1457
1458 /*
1459 * XXX: there is not much point to have two different values here..
1460 */
1461 dev->dev_attrib.block_size = dev->dev_attrib.hw_block_size;
1462 dev->dev_attrib.queue_depth = dev->dev_attrib.hw_queue_depth;
1463
1464 /*
1465 * Align max_hw_sectors down to PAGE_SIZE I/O transfers
1466 */
1467 dev->dev_attrib.hw_max_sectors =
1468 se_dev_align_max_sectors(dev->dev_attrib.hw_max_sectors,
1469 dev->dev_attrib.hw_block_size);
1470
1471 dev->dev_index = scsi_get_new_index(SCSI_DEVICE_INDEX);
1472 dev->creation_time = get_jiffies_64();
1473
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001474 ret = core_setup_alua(dev);
1475 if (ret)
1476 goto out;
1477
1478 /*
1479 * Startup the struct se_device processing thread
1480 */
1481 dev->tmr_wq = alloc_workqueue("tmr-%s", WQ_MEM_RECLAIM | WQ_UNBOUND, 1,
1482 dev->transport->name);
1483 if (!dev->tmr_wq) {
1484 pr_err("Unable to create tmr workqueue for %s\n",
1485 dev->transport->name);
1486 ret = -ENOMEM;
1487 goto out_free_alua;
1488 }
1489
1490 /*
1491 * Setup work_queue for QUEUE_FULL
1492 */
1493 INIT_WORK(&dev->qf_work_queue, target_qf_do_work);
1494
1495 /*
1496 * Preload the initial INQUIRY const values if we are doing
1497 * anything virtual (IBLOCK, FILEIO, RAMDISK), but not for TCM/pSCSI
1498 * passthrough because this is being provided by the backend LLD.
1499 */
1500 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV) {
1501 strncpy(&dev->t10_wwn.vendor[0], "LIO-ORG", 8);
1502 strncpy(&dev->t10_wwn.model[0],
1503 dev->transport->inquiry_prod, 16);
1504 strncpy(&dev->t10_wwn.revision[0],
1505 dev->transport->inquiry_rev, 4);
1506 }
1507
1508 scsi_dump_inquiry(dev);
1509
1510 spin_lock(&hba->device_lock);
1511 hba->dev_count++;
1512 spin_unlock(&hba->device_lock);
1513 return 0;
1514
1515out_free_alua:
1516 core_alua_free_lu_gp_mem(dev);
1517out:
1518 se_release_vpd_for_dev(dev);
1519 return ret;
1520}
1521
1522void target_free_device(struct se_device *dev)
1523{
1524 struct se_hba *hba = dev->se_hba;
1525
1526 WARN_ON(!list_empty(&dev->dev_sep_list));
1527
1528 if (dev->dev_flags & DF_CONFIGURED) {
1529 destroy_workqueue(dev->tmr_wq);
1530
1531 spin_lock(&hba->device_lock);
1532 hba->dev_count--;
1533 spin_unlock(&hba->device_lock);
1534 }
1535
1536 core_alua_free_lu_gp_mem(dev);
1537 core_scsi3_free_all_registrations(dev);
1538 se_release_vpd_for_dev(dev);
1539
1540 dev->transport->free_device(dev);
1541}
1542
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001543int core_dev_setup_virtual_lun0(void)
1544{
1545 struct se_hba *hba;
1546 struct se_device *dev;
Andy Groverdb5d1c32013-05-28 16:55:20 -07001547 char buf[] = "rd_pages=8,rd_nullio=1";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001548 int ret;
1549
Andy Grover6708bb22011-06-08 10:36:43 -07001550 hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001551 if (IS_ERR(hba))
1552 return PTR_ERR(hba);
1553
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001554 dev = target_alloc_device(hba, "virt_lun0");
1555 if (!dev) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001556 ret = -ENOMEM;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001557 goto out_free_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001558 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001559
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001560 hba->transport->set_configfs_dev_params(dev, buf, sizeof(buf));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001562 ret = target_configure_device(dev);
1563 if (ret)
1564 goto out_free_se_dev;
1565
1566 lun0_hba = hba;
Andy Grovere3d6f902011-07-19 08:55:10 +00001567 g_lun0_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001568 return 0;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001569
1570out_free_se_dev:
1571 target_free_device(dev);
1572out_free_hba:
1573 core_delete_hba(hba);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001574 return ret;
1575}
1576
1577
1578void core_dev_release_virtual_lun0(void)
1579{
Andy Grovere3d6f902011-07-19 08:55:10 +00001580 struct se_hba *hba = lun0_hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581
Andy Grover6708bb22011-06-08 10:36:43 -07001582 if (!hba)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 return;
1584
Andy Grovere3d6f902011-07-19 08:55:10 +00001585 if (g_lun0_dev)
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001586 target_free_device(g_lun0_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001587 core_delete_hba(hba);
1588}