blob: 0c5992f0d9469cd1c34f63ef22296aa7519498e8 [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 *
7 * Copyright (c) 2003, 2004, 2005 PyX Technologies, Inc.
8 * Copyright (c) 2005-2006 SBE, Inc. All Rights Reserved.
9 * Copyright (c) 2007-2010 Rising Tide Systems
10 * Copyright (c) 2008-2010 Linux-iSCSI.org
11 *
12 * Nicholas A. Bellinger <nab@kernel.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 *
28 ******************************************************************************/
29
30#include <linux/net.h>
31#include <linux/string.h>
32#include <linux/delay.h>
33#include <linux/timer.h>
34#include <linux/slab.h>
35#include <linux/spinlock.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080036#include <linux/kthread.h>
37#include <linux/in.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040038#include <linux/export.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039#include <net/sock.h>
40#include <net/tcp.h>
41#include <scsi/scsi.h>
Nicholas Bellinger1078da12011-05-19 20:19:13 -070042#include <scsi/scsi_device.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080043
44#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050045#include <target/target_core_backend.h>
46#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
Christoph Hellwige26d99a2011-11-14 12:30:30 -050048#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080049#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080050#include "target_core_pr.h"
51#include "target_core_ua.h"
52
53static void se_dev_start(struct se_device *dev);
54static void se_dev_stop(struct se_device *dev);
55
Andy Grovere3d6f902011-07-19 08:55:10 +000056static struct se_hba *lun0_hba;
57static struct se_subsystem_dev *lun0_su_dev;
58/* not static, needed by tpg.c */
59struct se_device *g_lun0_dev;
60
Andy Grover5951146d2011-07-19 10:26:37 +000061int transport_lookup_cmd_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080062{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063 struct se_lun *se_lun = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +000064 struct se_session *se_sess = se_cmd->se_sess;
Andy Grover5951146d2011-07-19 10:26:37 +000065 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080066 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080067
Fubo Chend8144952011-02-13 15:13:42 -080068 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
69 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
70 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grovere3d6f902011-07-19 08:55:10 +000071 return -ENODEV;
Fubo Chend8144952011-02-13 15:13:42 -080072 }
73
Roland Dreier78faae32011-07-20 09:09:10 +000074 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +000075 se_cmd->se_deve = &se_sess->se_node_acl->device_list[unpacked_lun];
76 if (se_cmd->se_deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
77 struct se_dev_entry *deve = se_cmd->se_deve;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080078
Andy Grover5951146d2011-07-19 10:26:37 +000079 deve->total_cmds++;
80 deve->total_bytes += se_cmd->data_length;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080081
Andy Grover5951146d2011-07-19 10:26:37 +000082 if ((se_cmd->data_direction == DMA_TO_DEVICE) &&
83 (deve->lun_flags & TRANSPORT_LUNFLAGS_READ_ONLY)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080084 se_cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
85 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grover6708bb22011-06-08 10:36:43 -070086 pr_err("TARGET_CORE[%s]: Detected WRITE_PROTECTED LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080087 " Access for 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +000088 se_cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080089 unpacked_lun);
Roland Dreier78faae32011-07-20 09:09:10 +000090 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Andy Grovere3d6f902011-07-19 08:55:10 +000091 return -EACCES;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080092 }
Andy Grover5951146d2011-07-19 10:26:37 +000093
94 if (se_cmd->data_direction == DMA_TO_DEVICE)
95 deve->write_bytes += se_cmd->data_length;
96 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
97 deve->read_bytes += se_cmd->data_length;
98
99 deve->deve_cmds++;
100
101 se_lun = deve->se_lun;
102 se_cmd->se_lun = deve->se_lun;
103 se_cmd->pr_res_key = deve->pr_res_key;
104 se_cmd->orig_fe_lun = unpacked_lun;
Andy Grover5951146d2011-07-19 10:26:37 +0000105 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
106 }
Roland Dreier78faae32011-07-20 09:09:10 +0000107 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000108
109 if (!se_lun) {
110 /*
111 * Use the se_portal_group->tpg_virt_lun0 to allow for
112 * REPORT_LUNS, et al to be returned when no active
113 * MappedLUN=0 exists for this Initiator Port.
114 */
115 if (unpacked_lun != 0) {
116 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
117 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grover6708bb22011-06-08 10:36:43 -0700118 pr_err("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
Andy Grover5951146d2011-07-19 10:26:37 +0000119 " Access for 0x%08x\n",
120 se_cmd->se_tfo->get_fabric_name(),
121 unpacked_lun);
122 return -ENODEV;
123 }
124 /*
125 * Force WRITE PROTECT for virtual LUN 0
126 */
127 if ((se_cmd->data_direction != DMA_FROM_DEVICE) &&
128 (se_cmd->data_direction != DMA_NONE)) {
129 se_cmd->scsi_sense_reason = TCM_WRITE_PROTECTED;
130 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
131 return -EACCES;
132 }
133
134 se_lun = &se_sess->se_tpg->tpg_virt_lun0;
135 se_cmd->se_lun = &se_sess->se_tpg->tpg_virt_lun0;
136 se_cmd->orig_fe_lun = 0;
Andy Grover5951146d2011-07-19 10:26:37 +0000137 se_cmd->se_cmd_flags |= SCF_SE_LUN_CMD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800138 }
139 /*
140 * Determine if the struct se_lun is online.
Andy Grover5951146d2011-07-19 10:26:37 +0000141 * FIXME: Check for LUN_RESET + UNIT Attention
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800143 if (se_dev_check_online(se_lun->lun_se_dev) != 0) {
144 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
145 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grovere3d6f902011-07-19 08:55:10 +0000146 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147 }
148
Andy Grover5951146d2011-07-19 10:26:37 +0000149 /* Directly associate cmd with se_dev */
150 se_cmd->se_dev = se_lun->lun_se_dev;
151
152 /* TODO: get rid of this and use atomics for stats */
153 dev = se_lun->lun_se_dev;
Roland Dreier78faae32011-07-20 09:09:10 +0000154 spin_lock_irqsave(&dev->stats_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800155 dev->num_cmds++;
156 if (se_cmd->data_direction == DMA_TO_DEVICE)
157 dev->write_bytes += se_cmd->data_length;
158 else if (se_cmd->data_direction == DMA_FROM_DEVICE)
159 dev->read_bytes += se_cmd->data_length;
Roland Dreier78faae32011-07-20 09:09:10 +0000160 spin_unlock_irqrestore(&dev->stats_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800161
162 /*
163 * Add the iscsi_cmd_t to the struct se_lun's cmd list. This list is used
164 * for tracking state of struct se_cmds during LUN shutdown events.
165 */
166 spin_lock_irqsave(&se_lun->lun_cmd_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000167 list_add_tail(&se_cmd->se_lun_node, &se_lun->lun_cmd_list);
Andy Grovera1d8b492011-05-02 17:12:10 -0700168 atomic_set(&se_cmd->transport_lun_active, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800169 spin_unlock_irqrestore(&se_lun->lun_cmd_lock, flags);
170
171 return 0;
172}
Andy Grover5951146d2011-07-19 10:26:37 +0000173EXPORT_SYMBOL(transport_lookup_cmd_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800174
Andy Grover5951146d2011-07-19 10:26:37 +0000175int transport_lookup_tmr_lun(struct se_cmd *se_cmd, u32 unpacked_lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800177 struct se_dev_entry *deve;
178 struct se_lun *se_lun = NULL;
Andy Grovere3d6f902011-07-19 08:55:10 +0000179 struct se_session *se_sess = se_cmd->se_sess;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800180 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
Roland Dreier5e1be912011-07-20 09:28:56 +0000181 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800182
Fubo Chend8144952011-02-13 15:13:42 -0800183 if (unpacked_lun >= TRANSPORT_MAX_LUNS_PER_TPG) {
184 se_cmd->scsi_sense_reason = TCM_NON_EXISTENT_LUN;
185 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grovere3d6f902011-07-19 08:55:10 +0000186 return -ENODEV;
Fubo Chend8144952011-02-13 15:13:42 -0800187 }
188
Roland Dreier5e1be912011-07-20 09:28:56 +0000189 spin_lock_irqsave(&se_sess->se_node_acl->device_list_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000190 se_cmd->se_deve = &se_sess->se_node_acl->device_list[unpacked_lun];
191 deve = se_cmd->se_deve;
192
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800193 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
Andy Grover5951146d2011-07-19 10:26:37 +0000194 se_tmr->tmr_lun = deve->se_lun;
195 se_cmd->se_lun = deve->se_lun;
196 se_lun = deve->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800197 se_cmd->pr_res_key = deve->pr_res_key;
198 se_cmd->orig_fe_lun = unpacked_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800199 }
Roland Dreier5e1be912011-07-20 09:28:56 +0000200 spin_unlock_irqrestore(&se_sess->se_node_acl->device_list_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800201
202 if (!se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700203 pr_debug("TARGET_CORE[%s]: Detected NON_EXISTENT_LUN"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 " Access for 0x%08x\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000205 se_cmd->se_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800206 unpacked_lun);
207 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grovere3d6f902011-07-19 08:55:10 +0000208 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800209 }
210 /*
211 * Determine if the struct se_lun is online.
Andy Grover5951146d2011-07-19 10:26:37 +0000212 * FIXME: Check for LUN_RESET + UNIT Attention
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800213 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800214 if (se_dev_check_online(se_lun->lun_se_dev) != 0) {
215 se_cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
Andy Grovere3d6f902011-07-19 08:55:10 +0000216 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800217 }
218
Andy Grover5951146d2011-07-19 10:26:37 +0000219 /* Directly associate cmd with se_dev */
220 se_cmd->se_dev = se_lun->lun_se_dev;
221 se_tmr->tmr_dev = se_lun->lun_se_dev;
222
Roland Dreier5e1be912011-07-20 09:28:56 +0000223 spin_lock_irqsave(&se_tmr->tmr_dev->se_tmr_lock, flags);
Andy Grover5951146d2011-07-19 10:26:37 +0000224 list_add_tail(&se_tmr->tmr_list, &se_tmr->tmr_dev->dev_tmr_list);
Roland Dreier5e1be912011-07-20 09:28:56 +0000225 spin_unlock_irqrestore(&se_tmr->tmr_dev->se_tmr_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800226
227 return 0;
228}
Andy Grover5951146d2011-07-19 10:26:37 +0000229EXPORT_SYMBOL(transport_lookup_tmr_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800230
231/*
232 * This function is called from core_scsi3_emulate_pro_register_and_move()
233 * and core_scsi3_decode_spec_i_port(), and will increment &deve->pr_ref_count
234 * when a matching rtpi is found.
235 */
236struct se_dev_entry *core_get_se_deve_from_rtpi(
237 struct se_node_acl *nacl,
238 u16 rtpi)
239{
240 struct se_dev_entry *deve;
241 struct se_lun *lun;
242 struct se_port *port;
243 struct se_portal_group *tpg = nacl->se_tpg;
244 u32 i;
245
246 spin_lock_irq(&nacl->device_list_lock);
247 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
248 deve = &nacl->device_list[i];
249
250 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
251 continue;
252
253 lun = deve->se_lun;
Andy Grover6708bb22011-06-08 10:36:43 -0700254 if (!lun) {
255 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 port = lun->lun_sep;
Andy Grover6708bb22011-06-08 10:36:43 -0700261 if (!port) {
262 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800263 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000264 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800265 continue;
266 }
267 if (port->sep_rtpi != rtpi)
268 continue;
269
270 atomic_inc(&deve->pr_ref_count);
271 smp_mb__after_atomic_inc();
272 spin_unlock_irq(&nacl->device_list_lock);
273
274 return deve;
275 }
276 spin_unlock_irq(&nacl->device_list_lock);
277
278 return NULL;
279}
280
281int core_free_device_list_for_node(
282 struct se_node_acl *nacl,
283 struct se_portal_group *tpg)
284{
285 struct se_dev_entry *deve;
286 struct se_lun *lun;
287 u32 i;
288
289 if (!nacl->device_list)
290 return 0;
291
292 spin_lock_irq(&nacl->device_list_lock);
293 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
294 deve = &nacl->device_list[i];
295
296 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
297 continue;
298
299 if (!deve->se_lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700300 pr_err("%s device entries device pointer is"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 " NULL, but Initiator has access.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000302 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 continue;
304 }
305 lun = deve->se_lun;
306
307 spin_unlock_irq(&nacl->device_list_lock);
308 core_update_device_list_for_node(lun, NULL, deve->mapped_lun,
309 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
310 spin_lock_irq(&nacl->device_list_lock);
311 }
312 spin_unlock_irq(&nacl->device_list_lock);
313
314 kfree(nacl->device_list);
315 nacl->device_list = NULL;
316
317 return 0;
318}
319
320void core_dec_lacl_count(struct se_node_acl *se_nacl, struct se_cmd *se_cmd)
321{
322 struct se_dev_entry *deve;
323
324 spin_lock_irq(&se_nacl->device_list_lock);
325 deve = &se_nacl->device_list[se_cmd->orig_fe_lun];
326 deve->deve_cmds--;
327 spin_unlock_irq(&se_nacl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328}
329
330void core_update_device_list_access(
331 u32 mapped_lun,
332 u32 lun_access,
333 struct se_node_acl *nacl)
334{
335 struct se_dev_entry *deve;
336
337 spin_lock_irq(&nacl->device_list_lock);
338 deve = &nacl->device_list[mapped_lun];
339 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
340 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
341 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
342 } else {
343 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
344 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
345 }
346 spin_unlock_irq(&nacl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347}
348
349/* core_update_device_list_for_node():
350 *
351 *
352 */
353int core_update_device_list_for_node(
354 struct se_lun *lun,
355 struct se_lun_acl *lun_acl,
356 u32 mapped_lun,
357 u32 lun_access,
358 struct se_node_acl *nacl,
359 struct se_portal_group *tpg,
360 int enable)
361{
362 struct se_port *port = lun->lun_sep;
363 struct se_dev_entry *deve = &nacl->device_list[mapped_lun];
364 int trans = 0;
365 /*
366 * If the MappedLUN entry is being disabled, the entry in
367 * port->sep_alua_list must be removed now before clearing the
368 * struct se_dev_entry pointers below as logic in
369 * core_alua_do_transition_tg_pt() depends on these being present.
370 */
Andy Grover6708bb22011-06-08 10:36:43 -0700371 if (!enable) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 /*
373 * deve->se_lun_acl will be NULL for demo-mode created LUNs
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 * that have not been explicitly concerted to MappedLUNs ->
Nicholas Bellinger29fe6092011-02-09 15:34:43 -0800375 * struct se_lun_acl, but we remove deve->alua_port_list from
376 * port->sep_alua_list. This also means that active UAs and
377 * NodeACL context specific PR metadata for demo-mode
378 * MappedLUN *deve will be released below..
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800379 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800380 spin_lock_bh(&port->sep_alua_lock);
381 list_del(&deve->alua_port_list);
382 spin_unlock_bh(&port->sep_alua_lock);
383 }
384
385 spin_lock_irq(&nacl->device_list_lock);
386 if (enable) {
387 /*
388 * Check if the call is handling demo mode -> explict LUN ACL
389 * transition. This transition must be for the same struct se_lun
390 * + mapped_lun that was setup in demo mode..
391 */
392 if (deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS) {
393 if (deve->se_lun_acl != NULL) {
Andy Grover6708bb22011-06-08 10:36:43 -0700394 pr_err("struct se_dev_entry->se_lun_acl"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 " already set for demo mode -> explict"
396 " LUN ACL transition\n");
Fubo Chen85dc98d2011-02-09 15:34:48 -0800397 spin_unlock_irq(&nacl->device_list_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000398 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 }
400 if (deve->se_lun != lun) {
Andy Grover6708bb22011-06-08 10:36:43 -0700401 pr_err("struct se_dev_entry->se_lun does"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800402 " match passed struct se_lun for demo mode"
403 " -> explict LUN ACL transition\n");
Fubo Chen85dc98d2011-02-09 15:34:48 -0800404 spin_unlock_irq(&nacl->device_list_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000405 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 }
407 deve->se_lun_acl = lun_acl;
408 trans = 1;
409 } else {
410 deve->se_lun = lun;
411 deve->se_lun_acl = lun_acl;
412 deve->mapped_lun = mapped_lun;
413 deve->lun_flags |= TRANSPORT_LUNFLAGS_INITIATOR_ACCESS;
414 }
415
416 if (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) {
417 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_ONLY;
418 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_WRITE;
419 } else {
420 deve->lun_flags &= ~TRANSPORT_LUNFLAGS_READ_WRITE;
421 deve->lun_flags |= TRANSPORT_LUNFLAGS_READ_ONLY;
422 }
423
424 if (trans) {
425 spin_unlock_irq(&nacl->device_list_lock);
426 return 0;
427 }
428 deve->creation_time = get_jiffies_64();
429 deve->attach_count++;
430 spin_unlock_irq(&nacl->device_list_lock);
431
432 spin_lock_bh(&port->sep_alua_lock);
433 list_add_tail(&deve->alua_port_list, &port->sep_alua_list);
434 spin_unlock_bh(&port->sep_alua_lock);
435
436 return 0;
437 }
438 /*
439 * Wait for any in process SPEC_I_PT=1 or REGISTER_AND_MOVE
440 * PR operation to complete.
441 */
442 spin_unlock_irq(&nacl->device_list_lock);
443 while (atomic_read(&deve->pr_ref_count) != 0)
444 cpu_relax();
445 spin_lock_irq(&nacl->device_list_lock);
446 /*
447 * Disable struct se_dev_entry LUN ACL mapping
448 */
449 core_scsi3_ua_release_all(deve);
450 deve->se_lun = NULL;
451 deve->se_lun_acl = NULL;
452 deve->lun_flags = 0;
453 deve->creation_time = 0;
454 deve->attach_count--;
455 spin_unlock_irq(&nacl->device_list_lock);
456
457 core_scsi3_free_pr_reg_from_nacl(lun->lun_se_dev, nacl);
458 return 0;
459}
460
461/* core_clear_lun_from_tpg():
462 *
463 *
464 */
465void core_clear_lun_from_tpg(struct se_lun *lun, struct se_portal_group *tpg)
466{
467 struct se_node_acl *nacl;
468 struct se_dev_entry *deve;
469 u32 i;
470
Roland Dreier28638882011-08-16 09:40:01 -0700471 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800472 list_for_each_entry(nacl, &tpg->acl_node_list, acl_list) {
Roland Dreier28638882011-08-16 09:40:01 -0700473 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800474
475 spin_lock_irq(&nacl->device_list_lock);
476 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
477 deve = &nacl->device_list[i];
478 if (lun != deve->se_lun)
479 continue;
480 spin_unlock_irq(&nacl->device_list_lock);
481
482 core_update_device_list_for_node(lun, NULL,
483 deve->mapped_lun, TRANSPORT_LUNFLAGS_NO_ACCESS,
484 nacl, tpg, 0);
485
486 spin_lock_irq(&nacl->device_list_lock);
487 }
488 spin_unlock_irq(&nacl->device_list_lock);
489
Roland Dreier28638882011-08-16 09:40:01 -0700490 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800491 }
Roland Dreier28638882011-08-16 09:40:01 -0700492 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800493}
494
495static struct se_port *core_alloc_port(struct se_device *dev)
496{
497 struct se_port *port, *port_tmp;
498
499 port = kzalloc(sizeof(struct se_port), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700500 if (!port) {
501 pr_err("Unable to allocate struct se_port\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000502 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800503 }
504 INIT_LIST_HEAD(&port->sep_alua_list);
505 INIT_LIST_HEAD(&port->sep_list);
506 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
507 spin_lock_init(&port->sep_alua_lock);
508 mutex_init(&port->sep_tg_pt_md_mutex);
509
510 spin_lock(&dev->se_port_lock);
511 if (dev->dev_port_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -0700512 pr_warn("Reached dev->dev_port_count =="
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800513 " 0x0000ffff\n");
514 spin_unlock(&dev->se_port_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +0000515 return ERR_PTR(-ENOSPC);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800516 }
517again:
518 /*
519 * Allocate the next RELATIVE TARGET PORT IDENTIFER for this struct se_device
520 * Here is the table from spc4r17 section 7.7.3.8.
521 *
522 * Table 473 -- RELATIVE TARGET PORT IDENTIFIER field
523 *
524 * Code Description
525 * 0h Reserved
526 * 1h Relative port 1, historically known as port A
527 * 2h Relative port 2, historically known as port B
528 * 3h to FFFFh Relative port 3 through 65 535
529 */
530 port->sep_rtpi = dev->dev_rpti_counter++;
Andy Grover6708bb22011-06-08 10:36:43 -0700531 if (!port->sep_rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800532 goto again;
533
534 list_for_each_entry(port_tmp, &dev->dev_sep_list, sep_list) {
535 /*
536 * Make sure RELATIVE TARGET PORT IDENTIFER is unique
537 * for 16-bit wrap..
538 */
539 if (port->sep_rtpi == port_tmp->sep_rtpi)
540 goto again;
541 }
542 spin_unlock(&dev->se_port_lock);
543
544 return port;
545}
546
547static void core_export_port(
548 struct se_device *dev,
549 struct se_portal_group *tpg,
550 struct se_port *port,
551 struct se_lun *lun)
552{
Andy Grovere3d6f902011-07-19 08:55:10 +0000553 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem = NULL;
555
556 spin_lock(&dev->se_port_lock);
557 spin_lock(&lun->lun_sep_lock);
558 port->sep_tpg = tpg;
559 port->sep_lun = lun;
560 lun->lun_sep = port;
561 spin_unlock(&lun->lun_sep_lock);
562
563 list_add_tail(&port->sep_list, &dev->dev_sep_list);
564 spin_unlock(&dev->se_port_lock);
565
Andy Grovere3d6f902011-07-19 08:55:10 +0000566 if (su_dev->t10_alua.alua_type == SPC3_ALUA_EMULATED) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800567 tg_pt_gp_mem = core_alua_allocate_tg_pt_gp_mem(port);
568 if (IS_ERR(tg_pt_gp_mem) || !tg_pt_gp_mem) {
Andy Grover6708bb22011-06-08 10:36:43 -0700569 pr_err("Unable to allocate t10_alua_tg_pt"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800570 "_gp_member_t\n");
571 return;
572 }
573 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
574 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +0000575 su_dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700577 pr_debug("%s/%s: Adding to default ALUA Target Port"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800578 " Group: alua/default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000579 dev->transport->name, tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 }
581
582 dev->dev_port_count++;
583 port->sep_index = port->sep_rtpi; /* RELATIVE TARGET PORT IDENTIFER */
584}
585
586/*
587 * Called with struct se_device->se_port_lock spinlock held.
588 */
589static void core_release_port(struct se_device *dev, struct se_port *port)
Dan Carpenter5dd7ed22011-03-14 04:06:01 -0700590 __releases(&dev->se_port_lock) __acquires(&dev->se_port_lock)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591{
592 /*
593 * Wait for any port reference for PR ALL_TG_PT=1 operation
594 * to complete in __core_scsi3_alloc_registration()
595 */
596 spin_unlock(&dev->se_port_lock);
597 if (atomic_read(&port->sep_tg_pt_ref_cnt))
598 cpu_relax();
599 spin_lock(&dev->se_port_lock);
600
601 core_alua_free_tg_pt_gp_mem(port);
602
603 list_del(&port->sep_list);
604 dev->dev_port_count--;
605 kfree(port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606}
607
608int core_dev_export(
609 struct se_device *dev,
610 struct se_portal_group *tpg,
611 struct se_lun *lun)
612{
613 struct se_port *port;
614
615 port = core_alloc_port(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +0000616 if (IS_ERR(port))
617 return PTR_ERR(port);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618
619 lun->lun_se_dev = dev;
620 se_dev_start(dev);
621
622 atomic_inc(&dev->dev_export_obj.obj_access_count);
623 core_export_port(dev, tpg, port, lun);
624 return 0;
625}
626
627void core_dev_unexport(
628 struct se_device *dev,
629 struct se_portal_group *tpg,
630 struct se_lun *lun)
631{
632 struct se_port *port = lun->lun_sep;
633
634 spin_lock(&lun->lun_sep_lock);
635 if (lun->lun_se_dev == NULL) {
636 spin_unlock(&lun->lun_sep_lock);
637 return;
638 }
639 spin_unlock(&lun->lun_sep_lock);
640
641 spin_lock(&dev->se_port_lock);
642 atomic_dec(&dev->dev_export_obj.obj_access_count);
643 core_release_port(dev, port);
644 spin_unlock(&dev->se_port_lock);
645
646 se_dev_stop(dev);
647 lun->lun_se_dev = NULL;
648}
649
Christoph Hellwige76a35d2011-11-03 17:50:42 -0400650int target_report_luns(struct se_task *se_task)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651{
Christoph Hellwige76a35d2011-11-03 17:50:42 -0400652 struct se_cmd *se_cmd = se_task->task_se_cmd;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800653 struct se_dev_entry *deve;
654 struct se_lun *se_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +0000655 struct se_session *se_sess = se_cmd->se_sess;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000656 unsigned char *buf;
Nicholas Bellinger1078da12011-05-19 20:19:13 -0700657 u32 cdb_offset = 0, lun_count = 0, offset = 8, i;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658
Andy Grover05d1c7c2011-07-20 19:13:28 +0000659 buf = transport_kmap_first_data_page(se_cmd);
660
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800661 /*
662 * If no struct se_session pointer is present, this struct se_cmd is
663 * coming via a target_core_mod PASSTHROUGH op, and not through
664 * a $FABRIC_MOD. In that case, report LUN=0 only.
665 */
Andy Grover6708bb22011-06-08 10:36:43 -0700666 if (!se_sess) {
Nicholas Bellinger1078da12011-05-19 20:19:13 -0700667 int_to_scsilun(0, (struct scsi_lun *)&buf[offset]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800668 lun_count = 1;
669 goto done;
670 }
671
Andy Grovere3d6f902011-07-19 08:55:10 +0000672 spin_lock_irq(&se_sess->se_node_acl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800673 for (i = 0; i < TRANSPORT_MAX_LUNS_PER_TPG; i++) {
Andy Grovere3d6f902011-07-19 08:55:10 +0000674 deve = &se_sess->se_node_acl->device_list[i];
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800675 if (!(deve->lun_flags & TRANSPORT_LUNFLAGS_INITIATOR_ACCESS))
676 continue;
677 se_lun = deve->se_lun;
678 /*
679 * We determine the correct LUN LIST LENGTH even once we
680 * have reached the initial allocation length.
681 * See SPC2-R20 7.19.
682 */
683 lun_count++;
684 if ((cdb_offset + 8) >= se_cmd->data_length)
685 continue;
686
Nicholas Bellinger1078da12011-05-19 20:19:13 -0700687 int_to_scsilun(deve->mapped_lun, (struct scsi_lun *)&buf[offset]);
688 offset += 8;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800689 cdb_offset += 8;
690 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000691 spin_unlock_irq(&se_sess->se_node_acl->device_list_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800692
693 /*
694 * See SPC3 r07, page 159.
695 */
696done:
Andy Grover05d1c7c2011-07-20 19:13:28 +0000697 transport_kunmap_first_data_page(se_cmd);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800698 lun_count *= 8;
699 buf[0] = ((lun_count >> 24) & 0xff);
700 buf[1] = ((lun_count >> 16) & 0xff);
701 buf[2] = ((lun_count >> 8) & 0xff);
702 buf[3] = (lun_count & 0xff);
703
Christoph Hellwigd29a5b62011-11-03 17:50:44 -0400704 se_task->task_scsi_status = GOOD;
705 transport_complete_task(se_task, 1);
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700706 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800707}
708
709/* se_release_device_for_hba():
710 *
711 *
712 */
713void se_release_device_for_hba(struct se_device *dev)
714{
715 struct se_hba *hba = dev->se_hba;
716
717 if ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) ||
718 (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) ||
719 (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN) ||
720 (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_ACTIVATED) ||
721 (dev->dev_status & TRANSPORT_DEVICE_OFFLINE_DEACTIVATED))
722 se_dev_stop(dev);
723
724 if (dev->dev_ptr) {
725 kthread_stop(dev->process_thread);
726 if (dev->transport->free_device)
727 dev->transport->free_device(dev->dev_ptr);
728 }
729
730 spin_lock(&hba->device_lock);
731 list_del(&dev->dev_list);
732 hba->dev_count--;
733 spin_unlock(&hba->device_lock);
734
735 core_scsi3_free_all_registrations(dev);
736 se_release_vpd_for_dev(dev);
737
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800738 kfree(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800739}
740
741void se_release_vpd_for_dev(struct se_device *dev)
742{
743 struct t10_vpd *vpd, *vpd_tmp;
744
Andy Grovere3d6f902011-07-19 08:55:10 +0000745 spin_lock(&dev->se_sub_dev->t10_wwn.t10_vpd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800746 list_for_each_entry_safe(vpd, vpd_tmp,
Andy Grovere3d6f902011-07-19 08:55:10 +0000747 &dev->se_sub_dev->t10_wwn.t10_vpd_list, vpd_list) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748 list_del(&vpd->vpd_list);
749 kfree(vpd);
750 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000751 spin_unlock(&dev->se_sub_dev->t10_wwn.t10_vpd_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800752}
753
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754/* se_free_virtual_device():
755 *
756 * Used for IBLOCK, RAMDISK, and FILEIO Transport Drivers.
757 */
758int se_free_virtual_device(struct se_device *dev, struct se_hba *hba)
759{
Fubo Chen05aea6e2011-03-14 04:06:00 -0700760 if (!list_empty(&dev->dev_sep_list))
761 dump_stack();
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800762
763 core_alua_free_lu_gp_mem(dev);
764 se_release_device_for_hba(dev);
765
766 return 0;
767}
768
769static void se_dev_start(struct se_device *dev)
770{
771 struct se_hba *hba = dev->se_hba;
772
773 spin_lock(&hba->device_lock);
774 atomic_inc(&dev->dev_obj.obj_access_count);
775 if (atomic_read(&dev->dev_obj.obj_access_count) == 1) {
776 if (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED) {
777 dev->dev_status &= ~TRANSPORT_DEVICE_DEACTIVATED;
778 dev->dev_status |= TRANSPORT_DEVICE_ACTIVATED;
779 } else if (dev->dev_status &
780 TRANSPORT_DEVICE_OFFLINE_DEACTIVATED) {
781 dev->dev_status &=
782 ~TRANSPORT_DEVICE_OFFLINE_DEACTIVATED;
783 dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_ACTIVATED;
784 }
785 }
786 spin_unlock(&hba->device_lock);
787}
788
789static void se_dev_stop(struct se_device *dev)
790{
791 struct se_hba *hba = dev->se_hba;
792
793 spin_lock(&hba->device_lock);
794 atomic_dec(&dev->dev_obj.obj_access_count);
795 if (atomic_read(&dev->dev_obj.obj_access_count) == 0) {
796 if (dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) {
797 dev->dev_status &= ~TRANSPORT_DEVICE_ACTIVATED;
798 dev->dev_status |= TRANSPORT_DEVICE_DEACTIVATED;
799 } else if (dev->dev_status &
800 TRANSPORT_DEVICE_OFFLINE_ACTIVATED) {
801 dev->dev_status &= ~TRANSPORT_DEVICE_OFFLINE_ACTIVATED;
802 dev->dev_status |= TRANSPORT_DEVICE_OFFLINE_DEACTIVATED;
803 }
804 }
805 spin_unlock(&hba->device_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800806}
807
808int se_dev_check_online(struct se_device *dev)
809{
Roland Dreier56e34ee2011-06-13 20:55:06 -0700810 unsigned long flags;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800811 int ret;
812
Roland Dreier56e34ee2011-06-13 20:55:06 -0700813 spin_lock_irqsave(&dev->dev_status_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800814 ret = ((dev->dev_status & TRANSPORT_DEVICE_ACTIVATED) ||
815 (dev->dev_status & TRANSPORT_DEVICE_DEACTIVATED)) ? 0 : 1;
Roland Dreier56e34ee2011-06-13 20:55:06 -0700816 spin_unlock_irqrestore(&dev->dev_status_lock, flags);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800817
818 return ret;
819}
820
821int se_dev_check_shutdown(struct se_device *dev)
822{
823 int ret;
824
825 spin_lock_irq(&dev->dev_status_lock);
826 ret = (dev->dev_status & TRANSPORT_DEVICE_SHUTDOWN);
827 spin_unlock_irq(&dev->dev_status_lock);
828
829 return ret;
830}
831
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700832u32 se_dev_align_max_sectors(u32 max_sectors, u32 block_size)
833{
834 u32 tmp, aligned_max_sectors;
835 /*
836 * Limit max_sectors to a PAGE_SIZE aligned value for modern
837 * transport_allocate_data_tasks() operation.
838 */
839 tmp = rounddown((max_sectors * block_size), PAGE_SIZE);
840 aligned_max_sectors = (tmp / block_size);
841 if (max_sectors != aligned_max_sectors) {
842 printk(KERN_INFO "Rounding down aligned max_sectors from %u"
843 " to %u\n", max_sectors, aligned_max_sectors);
844 return aligned_max_sectors;
845 }
846
847 return max_sectors;
848}
849
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800850void se_dev_set_default_attribs(
851 struct se_device *dev,
852 struct se_dev_limits *dev_limits)
853{
854 struct queue_limits *limits = &dev_limits->limits;
855
Andy Grovere3d6f902011-07-19 08:55:10 +0000856 dev->se_sub_dev->se_dev_attrib.emulate_dpo = DA_EMULATE_DPO;
857 dev->se_sub_dev->se_dev_attrib.emulate_fua_write = DA_EMULATE_FUA_WRITE;
858 dev->se_sub_dev->se_dev_attrib.emulate_fua_read = DA_EMULATE_FUA_READ;
859 dev->se_sub_dev->se_dev_attrib.emulate_write_cache = DA_EMULATE_WRITE_CACHE;
860 dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl = DA_EMULATE_UA_INTLLCK_CTRL;
861 dev->se_sub_dev->se_dev_attrib.emulate_tas = DA_EMULATE_TAS;
862 dev->se_sub_dev->se_dev_attrib.emulate_tpu = DA_EMULATE_TPU;
863 dev->se_sub_dev->se_dev_attrib.emulate_tpws = DA_EMULATE_TPWS;
864 dev->se_sub_dev->se_dev_attrib.emulate_reservations = DA_EMULATE_RESERVATIONS;
865 dev->se_sub_dev->se_dev_attrib.emulate_alua = DA_EMULATE_ALUA;
866 dev->se_sub_dev->se_dev_attrib.enforce_pr_isids = DA_ENFORCE_PR_ISIDS;
Roland Dreiere22a7f02011-07-05 13:34:52 -0700867 dev->se_sub_dev->se_dev_attrib.is_nonrot = DA_IS_NONROT;
Nicholas Bellinger5de619a2011-07-17 02:57:58 -0700868 dev->se_sub_dev->se_dev_attrib.emulate_rest_reord = DA_EMULATE_REST_REORD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800869 /*
870 * The TPU=1 and TPWS=1 settings will be set in TCM/IBLOCK
871 * iblock_create_virtdevice() from struct queue_limits values
872 * if blk_queue_discard()==1
873 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000874 dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count = DA_MAX_UNMAP_LBA_COUNT;
875 dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count =
876 DA_MAX_UNMAP_BLOCK_DESC_COUNT;
877 dev->se_sub_dev->se_dev_attrib.unmap_granularity = DA_UNMAP_GRANULARITY_DEFAULT;
878 dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment =
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879 DA_UNMAP_GRANULARITY_ALIGNMENT_DEFAULT;
880 /*
881 * block_size is based on subsystem plugin dependent requirements.
882 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000883 dev->se_sub_dev->se_dev_attrib.hw_block_size = limits->logical_block_size;
884 dev->se_sub_dev->se_dev_attrib.block_size = limits->logical_block_size;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800885 /*
886 * max_sectors is based on subsystem plugin dependent requirements.
887 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000888 dev->se_sub_dev->se_dev_attrib.hw_max_sectors = limits->max_hw_sectors;
Nicholas Bellinger525a48a2011-08-13 02:11:38 -0700889 /*
890 * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
891 */
892 limits->max_sectors = se_dev_align_max_sectors(limits->max_sectors,
893 limits->logical_block_size);
Andy Grovere3d6f902011-07-19 08:55:10 +0000894 dev->se_sub_dev->se_dev_attrib.max_sectors = limits->max_sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800895 /*
896 * Set optimal_sectors from max_sectors, which can be lowered via
897 * configfs.
898 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000899 dev->se_sub_dev->se_dev_attrib.optimal_sectors = limits->max_sectors;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800900 /*
901 * queue_depth is based on subsystem plugin dependent requirements.
902 */
Andy Grovere3d6f902011-07-19 08:55:10 +0000903 dev->se_sub_dev->se_dev_attrib.hw_queue_depth = dev_limits->hw_queue_depth;
904 dev->se_sub_dev->se_dev_attrib.queue_depth = dev_limits->queue_depth;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800905}
906
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907int se_dev_set_max_unmap_lba_count(
908 struct se_device *dev,
909 u32 max_unmap_lba_count)
910{
Andy Grovere3d6f902011-07-19 08:55:10 +0000911 dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count = max_unmap_lba_count;
Andy Grover6708bb22011-06-08 10:36:43 -0700912 pr_debug("dev[%p]: Set max_unmap_lba_count: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000913 dev, dev->se_sub_dev->se_dev_attrib.max_unmap_lba_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800914 return 0;
915}
916
917int se_dev_set_max_unmap_block_desc_count(
918 struct se_device *dev,
919 u32 max_unmap_block_desc_count)
920{
Andy Grovere3d6f902011-07-19 08:55:10 +0000921 dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count =
922 max_unmap_block_desc_count;
Andy Grover6708bb22011-06-08 10:36:43 -0700923 pr_debug("dev[%p]: Set max_unmap_block_desc_count: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000924 dev, dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800925 return 0;
926}
927
928int se_dev_set_unmap_granularity(
929 struct se_device *dev,
930 u32 unmap_granularity)
931{
Andy Grovere3d6f902011-07-19 08:55:10 +0000932 dev->se_sub_dev->se_dev_attrib.unmap_granularity = unmap_granularity;
Andy Grover6708bb22011-06-08 10:36:43 -0700933 pr_debug("dev[%p]: Set unmap_granularity: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000934 dev, dev->se_sub_dev->se_dev_attrib.unmap_granularity);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800935 return 0;
936}
937
938int se_dev_set_unmap_granularity_alignment(
939 struct se_device *dev,
940 u32 unmap_granularity_alignment)
941{
Andy Grovere3d6f902011-07-19 08:55:10 +0000942 dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment = unmap_granularity_alignment;
Andy Grover6708bb22011-06-08 10:36:43 -0700943 pr_debug("dev[%p]: Set unmap_granularity_alignment: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000944 dev, dev->se_sub_dev->se_dev_attrib.unmap_granularity_alignment);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800945 return 0;
946}
947
948int se_dev_set_emulate_dpo(struct se_device *dev, int flag)
949{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400950 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700951 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000952 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400954
Andy Groverc6388302011-11-30 12:11:50 -0800955 if (flag) {
956 pr_err("dpo_emulated not supported\n");
957 return -EINVAL;
958 }
959
960 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800961}
962
963int se_dev_set_emulate_fua_write(struct se_device *dev, int flag)
964{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400965 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700966 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000967 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800968 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400969
Andy Groverc6388302011-11-30 12:11:50 -0800970 if (flag && dev->transport->fua_write_emulated == 0) {
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400971 pr_err("fua_write_emulated not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +0000972 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800973 }
Andy Grovere3d6f902011-07-19 08:55:10 +0000974 dev->se_sub_dev->se_dev_attrib.emulate_fua_write = flag;
Andy Grover6708bb22011-06-08 10:36:43 -0700975 pr_debug("dev[%p]: SE Device Forced Unit Access WRITEs: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +0000976 dev, dev->se_sub_dev->se_dev_attrib.emulate_fua_write);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800977 return 0;
978}
979
980int se_dev_set_emulate_fua_read(struct se_device *dev, int flag)
981{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400982 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700983 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000984 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800985 }
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400986
Andy Groverc6388302011-11-30 12:11:50 -0800987 if (flag) {
988 pr_err("ua read emulated not supported\n");
989 return -EINVAL;
990 }
991
992 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800993}
994
995int se_dev_set_emulate_write_cache(struct se_device *dev, int flag)
996{
Christoph Hellwigf55918f2011-10-14 07:30:17 -0400997 if (flag != 0 && flag != 1) {
Andy Grover6708bb22011-06-08 10:36:43 -0700998 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +0000999 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000 }
Andy Groverc6388302011-11-30 12:11:50 -08001001 if (flag && dev->transport->write_cache_emulated == 0) {
Christoph Hellwigf55918f2011-10-14 07:30:17 -04001002 pr_err("write_cache_emulated not supported\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001003 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001005 dev->se_sub_dev->se_dev_attrib.emulate_write_cache = flag;
Andy Grover6708bb22011-06-08 10:36:43 -07001006 pr_debug("dev[%p]: SE Device WRITE_CACHE_EMULATION flag: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001007 dev, dev->se_sub_dev->se_dev_attrib.emulate_write_cache);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001008 return 0;
1009}
1010
1011int se_dev_set_emulate_ua_intlck_ctrl(struct se_device *dev, int flag)
1012{
1013 if ((flag != 0) && (flag != 1) && (flag != 2)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001014 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +00001015 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001016 }
1017
1018 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001019 pr_err("dev[%p]: Unable to change SE Device"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020 " UA_INTRLCK_CTRL while dev_export_obj: %d count"
1021 " exists\n", dev,
1022 atomic_read(&dev->dev_export_obj.obj_access_count));
Andy Grovere3d6f902011-07-19 08:55:10 +00001023 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001024 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001025 dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl = flag;
Andy Grover6708bb22011-06-08 10:36:43 -07001026 pr_debug("dev[%p]: SE Device UA_INTRLCK_CTRL flag: %d\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001027 dev, dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001028
1029 return 0;
1030}
1031
1032int se_dev_set_emulate_tas(struct se_device *dev, int flag)
1033{
1034 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001035 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +00001036 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037 }
1038
1039 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001040 pr_err("dev[%p]: Unable to change SE Device TAS while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041 " dev_export_obj: %d count exists\n", dev,
1042 atomic_read(&dev->dev_export_obj.obj_access_count));
Andy Grovere3d6f902011-07-19 08:55:10 +00001043 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001044 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001045 dev->se_sub_dev->se_dev_attrib.emulate_tas = flag;
Andy Grover6708bb22011-06-08 10:36:43 -07001046 pr_debug("dev[%p]: SE Device TASK_ABORTED status bit: %s\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001047 dev, (dev->se_sub_dev->se_dev_attrib.emulate_tas) ? "Enabled" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001048
1049 return 0;
1050}
1051
1052int se_dev_set_emulate_tpu(struct se_device *dev, int flag)
1053{
1054 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001055 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +00001056 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001057 }
1058 /*
1059 * We expect this value to be non-zero when generic Block Layer
1060 * Discard supported is detected iblock_create_virtdevice().
1061 */
Andy Groverc6388302011-11-30 12:11:50 -08001062 if (flag && !dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001063 pr_err("Generic Block Discard not supported\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001064 return -ENOSYS;
1065 }
1066
Andy Grovere3d6f902011-07-19 08:55:10 +00001067 dev->se_sub_dev->se_dev_attrib.emulate_tpu = flag;
Andy Grover6708bb22011-06-08 10:36:43 -07001068 pr_debug("dev[%p]: SE Device Thin Provisioning UNMAP bit: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001069 dev, flag);
1070 return 0;
1071}
1072
1073int se_dev_set_emulate_tpws(struct se_device *dev, int flag)
1074{
1075 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001076 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +00001077 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001078 }
1079 /*
1080 * We expect this value to be non-zero when generic Block Layer
1081 * Discard supported is detected iblock_create_virtdevice().
1082 */
Andy Groverc6388302011-11-30 12:11:50 -08001083 if (flag && !dev->se_sub_dev->se_dev_attrib.max_unmap_block_desc_count) {
Andy Grover6708bb22011-06-08 10:36:43 -07001084 pr_err("Generic Block Discard not supported\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001085 return -ENOSYS;
1086 }
1087
Andy Grovere3d6f902011-07-19 08:55:10 +00001088 dev->se_sub_dev->se_dev_attrib.emulate_tpws = flag;
Andy Grover6708bb22011-06-08 10:36:43 -07001089 pr_debug("dev[%p]: SE Device Thin Provisioning WRITE_SAME: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001090 dev, flag);
1091 return 0;
1092}
1093
1094int se_dev_set_enforce_pr_isids(struct se_device *dev, int flag)
1095{
1096 if ((flag != 0) && (flag != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001097 pr_err("Illegal value %d\n", flag);
Andy Grovere3d6f902011-07-19 08:55:10 +00001098 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001099 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001100 dev->se_sub_dev->se_dev_attrib.enforce_pr_isids = flag;
Andy Grover6708bb22011-06-08 10:36:43 -07001101 pr_debug("dev[%p]: SE Device enforce_pr_isids bit: %s\n", dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001102 (dev->se_sub_dev->se_dev_attrib.enforce_pr_isids) ? "Enabled" : "Disabled");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001103 return 0;
1104}
1105
Roland Dreiere22a7f02011-07-05 13:34:52 -07001106int se_dev_set_is_nonrot(struct se_device *dev, int flag)
1107{
1108 if ((flag != 0) && (flag != 1)) {
1109 printk(KERN_ERR "Illegal value %d\n", flag);
1110 return -EINVAL;
1111 }
1112 dev->se_sub_dev->se_dev_attrib.is_nonrot = flag;
Nicholas Bellinger5de619a2011-07-17 02:57:58 -07001113 pr_debug("dev[%p]: SE Device is_nonrot bit: %d\n",
Roland Dreiere22a7f02011-07-05 13:34:52 -07001114 dev, flag);
1115 return 0;
1116}
1117
Nicholas Bellinger5de619a2011-07-17 02:57:58 -07001118int se_dev_set_emulate_rest_reord(struct se_device *dev, int flag)
1119{
1120 if (flag != 0) {
1121 printk(KERN_ERR "dev[%p]: SE Device emulatation of restricted"
1122 " reordering not implemented\n", dev);
1123 return -ENOSYS;
1124 }
1125 dev->se_sub_dev->se_dev_attrib.emulate_rest_reord = flag;
1126 pr_debug("dev[%p]: SE Device emulate_rest_reord: %d\n", dev, flag);
1127 return 0;
1128}
1129
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130/*
1131 * Note, this can only be called on unexported SE Device Object.
1132 */
1133int se_dev_set_queue_depth(struct se_device *dev, u32 queue_depth)
1134{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001135 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001136 pr_err("dev[%p]: Unable to change SE Device TCQ while"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001137 " dev_export_obj: %d count exists\n", dev,
1138 atomic_read(&dev->dev_export_obj.obj_access_count));
Andy Grovere3d6f902011-07-19 08:55:10 +00001139 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140 }
Andy Grover6708bb22011-06-08 10:36:43 -07001141 if (!queue_depth) {
1142 pr_err("dev[%p]: Illegal ZERO value for queue"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143 "_depth\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00001144 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 }
1146
Andy Grovere3d6f902011-07-19 08:55:10 +00001147 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1148 if (queue_depth > dev->se_sub_dev->se_dev_attrib.hw_queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -07001149 pr_err("dev[%p]: Passed queue_depth: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001150 " exceeds TCM/SE_Device TCQ: %u\n",
1151 dev, queue_depth,
Andy Grovere3d6f902011-07-19 08:55:10 +00001152 dev->se_sub_dev->se_dev_attrib.hw_queue_depth);
1153 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 }
1155 } else {
Andy Grovere3d6f902011-07-19 08:55:10 +00001156 if (queue_depth > dev->se_sub_dev->se_dev_attrib.queue_depth) {
1157 if (queue_depth > dev->se_sub_dev->se_dev_attrib.hw_queue_depth) {
Andy Grover6708bb22011-06-08 10:36:43 -07001158 pr_err("dev[%p]: Passed queue_depth:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001159 " %u exceeds TCM/SE_Device MAX"
1160 " TCQ: %u\n", dev, queue_depth,
Andy Grovere3d6f902011-07-19 08:55:10 +00001161 dev->se_sub_dev->se_dev_attrib.hw_queue_depth);
1162 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001163 }
1164 }
1165 }
1166
Andy Grovere3d6f902011-07-19 08:55:10 +00001167 dev->se_sub_dev->se_dev_attrib.queue_depth = dev->queue_depth = queue_depth;
Andy Grover6708bb22011-06-08 10:36:43 -07001168 pr_debug("dev[%p]: SE Device TCQ Depth changed to: %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001169 dev, queue_depth);
1170 return 0;
1171}
1172
1173int se_dev_set_max_sectors(struct se_device *dev, u32 max_sectors)
1174{
1175 int force = 0; /* Force setting for VDEVS */
1176
1177 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001178 pr_err("dev[%p]: Unable to change SE Device"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 " max_sectors while dev_export_obj: %d count exists\n",
1180 dev, atomic_read(&dev->dev_export_obj.obj_access_count));
Andy Grovere3d6f902011-07-19 08:55:10 +00001181 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001182 }
Andy Grover6708bb22011-06-08 10:36:43 -07001183 if (!max_sectors) {
1184 pr_err("dev[%p]: Illegal ZERO value for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001185 " max_sectors\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00001186 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187 }
1188 if (max_sectors < DA_STATUS_MAX_SECTORS_MIN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001189 pr_err("dev[%p]: Passed max_sectors: %u less than"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190 " DA_STATUS_MAX_SECTORS_MIN: %u\n", dev, max_sectors,
1191 DA_STATUS_MAX_SECTORS_MIN);
Andy Grovere3d6f902011-07-19 08:55:10 +00001192 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001194 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
1195 if (max_sectors > dev->se_sub_dev->se_dev_attrib.hw_max_sectors) {
Andy Grover6708bb22011-06-08 10:36:43 -07001196 pr_err("dev[%p]: Passed max_sectors: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197 " greater than TCM/SE_Device max_sectors:"
1198 " %u\n", dev, max_sectors,
Andy Grovere3d6f902011-07-19 08:55:10 +00001199 dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
1200 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 }
1202 } else {
Andy Grover6708bb22011-06-08 10:36:43 -07001203 if (!force && (max_sectors >
Andy Grovere3d6f902011-07-19 08:55:10 +00001204 dev->se_sub_dev->se_dev_attrib.hw_max_sectors)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001205 pr_err("dev[%p]: Passed max_sectors: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001206 " greater than TCM/SE_Device max_sectors"
1207 ": %u, use force=1 to override.\n", dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001208 max_sectors, dev->se_sub_dev->se_dev_attrib.hw_max_sectors);
1209 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210 }
1211 if (max_sectors > DA_STATUS_MAX_SECTORS_MAX) {
Andy Grover6708bb22011-06-08 10:36:43 -07001212 pr_err("dev[%p]: Passed max_sectors: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 " greater than DA_STATUS_MAX_SECTORS_MAX:"
1214 " %u\n", dev, max_sectors,
1215 DA_STATUS_MAX_SECTORS_MAX);
Andy Grovere3d6f902011-07-19 08:55:10 +00001216 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001217 }
1218 }
Nicholas Bellinger525a48a2011-08-13 02:11:38 -07001219 /*
1220 * Align max_sectors down to PAGE_SIZE to follow transport_allocate_data_tasks()
1221 */
1222 max_sectors = se_dev_align_max_sectors(max_sectors,
1223 dev->se_sub_dev->se_dev_attrib.block_size);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224
Andy Grovere3d6f902011-07-19 08:55:10 +00001225 dev->se_sub_dev->se_dev_attrib.max_sectors = max_sectors;
Andy Grover6708bb22011-06-08 10:36:43 -07001226 pr_debug("dev[%p]: SE Device max_sectors changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 dev, max_sectors);
1228 return 0;
1229}
1230
1231int se_dev_set_optimal_sectors(struct se_device *dev, u32 optimal_sectors)
1232{
1233 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001234 pr_err("dev[%p]: Unable to change SE Device"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001235 " optimal_sectors while dev_export_obj: %d count exists\n",
1236 dev, atomic_read(&dev->dev_export_obj.obj_access_count));
1237 return -EINVAL;
1238 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001239 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Andy Grover6708bb22011-06-08 10:36:43 -07001240 pr_err("dev[%p]: Passed optimal_sectors cannot be"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001241 " changed for TCM/pSCSI\n", dev);
1242 return -EINVAL;
1243 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001244 if (optimal_sectors > dev->se_sub_dev->se_dev_attrib.max_sectors) {
Andy Grover6708bb22011-06-08 10:36:43 -07001245 pr_err("dev[%p]: Passed optimal_sectors %u cannot be"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246 " greater than max_sectors: %u\n", dev,
Andy Grovere3d6f902011-07-19 08:55:10 +00001247 optimal_sectors, dev->se_sub_dev->se_dev_attrib.max_sectors);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248 return -EINVAL;
1249 }
1250
Andy Grovere3d6f902011-07-19 08:55:10 +00001251 dev->se_sub_dev->se_dev_attrib.optimal_sectors = optimal_sectors;
Andy Grover6708bb22011-06-08 10:36:43 -07001252 pr_debug("dev[%p]: SE Device optimal_sectors changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253 dev, optimal_sectors);
1254 return 0;
1255}
1256
1257int se_dev_set_block_size(struct se_device *dev, u32 block_size)
1258{
1259 if (atomic_read(&dev->dev_export_obj.obj_access_count)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001260 pr_err("dev[%p]: Unable to change SE Device block_size"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 " while dev_export_obj: %d count exists\n", dev,
1262 atomic_read(&dev->dev_export_obj.obj_access_count));
Andy Grovere3d6f902011-07-19 08:55:10 +00001263 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001264 }
1265
1266 if ((block_size != 512) &&
1267 (block_size != 1024) &&
1268 (block_size != 2048) &&
1269 (block_size != 4096)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001270 pr_err("dev[%p]: Illegal value for block_device: %u"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001271 " for SE device, must be 512, 1024, 2048 or 4096\n",
1272 dev, block_size);
Andy Grovere3d6f902011-07-19 08:55:10 +00001273 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 }
1275
Andy Grovere3d6f902011-07-19 08:55:10 +00001276 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) {
Andy Grover6708bb22011-06-08 10:36:43 -07001277 pr_err("dev[%p]: Not allowed to change block_size for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 " Physical Device, use for Linux/SCSI to change"
1279 " block_size for underlying hardware\n", dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00001280 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001281 }
1282
Andy Grovere3d6f902011-07-19 08:55:10 +00001283 dev->se_sub_dev->se_dev_attrib.block_size = block_size;
Andy Grover6708bb22011-06-08 10:36:43 -07001284 pr_debug("dev[%p]: SE Device block_size changed to %u\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001285 dev, block_size);
1286 return 0;
1287}
1288
1289struct se_lun *core_dev_add_lun(
1290 struct se_portal_group *tpg,
1291 struct se_hba *hba,
1292 struct se_device *dev,
1293 u32 lun)
1294{
1295 struct se_lun *lun_p;
1296 u32 lun_access = 0;
1297
1298 if (atomic_read(&dev->dev_access_obj.obj_access_count) != 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001299 pr_err("Unable to export struct se_device while dev_access_obj: %d\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300 atomic_read(&dev->dev_access_obj.obj_access_count));
1301 return NULL;
1302 }
1303
1304 lun_p = core_tpg_pre_addlun(tpg, lun);
Andy Grover6708bb22011-06-08 10:36:43 -07001305 if ((IS_ERR(lun_p)) || !lun_p)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 return NULL;
1307
1308 if (dev->dev_flags & DF_READ_ONLY)
1309 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1310 else
1311 lun_access = TRANSPORT_LUNFLAGS_READ_WRITE;
1312
1313 if (core_tpg_post_addlun(tpg, lun_p, lun_access, dev) < 0)
1314 return NULL;
1315
Andy Grover6708bb22011-06-08 10:36:43 -07001316 pr_debug("%s_TPG[%u]_LUN[%u] - Activated %s Logical Unit from"
Andy Grovere3d6f902011-07-19 08:55:10 +00001317 " CORE HBA: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1318 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun_p->unpacked_lun,
1319 tpg->se_tpg_tfo->get_fabric_name(), hba->hba_id);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320 /*
1321 * Update LUN maps for dynamically added initiators when
1322 * generate_node_acl is enabled.
1323 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001324 if (tpg->se_tpg_tfo->tpg_check_demo_mode(tpg)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001325 struct se_node_acl *acl;
Roland Dreier28638882011-08-16 09:40:01 -07001326 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327 list_for_each_entry(acl, &tpg->acl_node_list, acl_list) {
Nicholas Bellinger052605c2011-07-26 17:48:43 -07001328 if (acl->dynamic_node_acl &&
1329 (!tpg->se_tpg_tfo->tpg_check_demo_mode_login_only ||
1330 !tpg->se_tpg_tfo->tpg_check_demo_mode_login_only(tpg))) {
Roland Dreier28638882011-08-16 09:40:01 -07001331 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001332 core_tpg_add_node_to_devs(acl, tpg);
Roland Dreier28638882011-08-16 09:40:01 -07001333 spin_lock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001334 }
1335 }
Roland Dreier28638882011-08-16 09:40:01 -07001336 spin_unlock_irq(&tpg->acl_node_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001337 }
1338
1339 return lun_p;
1340}
1341
1342/* core_dev_del_lun():
1343 *
1344 *
1345 */
1346int core_dev_del_lun(
1347 struct se_portal_group *tpg,
1348 u32 unpacked_lun)
1349{
1350 struct se_lun *lun;
1351 int ret = 0;
1352
1353 lun = core_tpg_pre_dellun(tpg, unpacked_lun, &ret);
Andy Grover6708bb22011-06-08 10:36:43 -07001354 if (!lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001355 return ret;
1356
1357 core_tpg_post_dellun(tpg, lun);
1358
Andy Grover6708bb22011-06-08 10:36:43 -07001359 pr_debug("%s_TPG[%u]_LUN[%u] - Deactivated %s Logical Unit from"
Andy Grovere3d6f902011-07-19 08:55:10 +00001360 " device object\n", tpg->se_tpg_tfo->get_fabric_name(),
1361 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun,
1362 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001363
1364 return 0;
1365}
1366
1367struct se_lun *core_get_lun_from_tpg(struct se_portal_group *tpg, u32 unpacked_lun)
1368{
1369 struct se_lun *lun;
1370
1371 spin_lock(&tpg->tpg_lun_lock);
1372 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001373 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001374 "_PER_TPG-1: %u for Target Portal Group: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001375 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001376 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +00001377 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001378 spin_unlock(&tpg->tpg_lun_lock);
1379 return NULL;
1380 }
1381 lun = &tpg->tpg_lun_list[unpacked_lun];
1382
1383 if (lun->lun_status != TRANSPORT_LUN_STATUS_FREE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001384 pr_err("%s Logical Unit Number: %u is not free on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001385 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001386 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1387 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001388 spin_unlock(&tpg->tpg_lun_lock);
1389 return NULL;
1390 }
1391 spin_unlock(&tpg->tpg_lun_lock);
1392
1393 return lun;
1394}
1395
1396/* core_dev_get_lun():
1397 *
1398 *
1399 */
1400static struct se_lun *core_dev_get_lun(struct se_portal_group *tpg, u32 unpacked_lun)
1401{
1402 struct se_lun *lun;
1403
1404 spin_lock(&tpg->tpg_lun_lock);
1405 if (unpacked_lun > (TRANSPORT_MAX_LUNS_PER_TPG-1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001406 pr_err("%s LUN: %u exceeds TRANSPORT_MAX_LUNS_PER"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001407 "_TPG-1: %u for Target Portal Group: %hu\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001408 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409 TRANSPORT_MAX_LUNS_PER_TPG-1,
Andy Grovere3d6f902011-07-19 08:55:10 +00001410 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001411 spin_unlock(&tpg->tpg_lun_lock);
1412 return NULL;
1413 }
1414 lun = &tpg->tpg_lun_list[unpacked_lun];
1415
1416 if (lun->lun_status != TRANSPORT_LUN_STATUS_ACTIVE) {
Andy Grover6708bb22011-06-08 10:36:43 -07001417 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001418 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001419 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1420 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001421 spin_unlock(&tpg->tpg_lun_lock);
1422 return NULL;
1423 }
1424 spin_unlock(&tpg->tpg_lun_lock);
1425
1426 return lun;
1427}
1428
1429struct se_lun_acl *core_dev_init_initiator_node_lun_acl(
1430 struct se_portal_group *tpg,
1431 u32 mapped_lun,
1432 char *initiatorname,
1433 int *ret)
1434{
1435 struct se_lun_acl *lacl;
1436 struct se_node_acl *nacl;
1437
Dan Carpenter60d645a2011-06-15 10:03:05 -07001438 if (strlen(initiatorname) >= TRANSPORT_IQN_LEN) {
Andy Grover6708bb22011-06-08 10:36:43 -07001439 pr_err("%s InitiatorName exceeds maximum size.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001440 tpg->se_tpg_tfo->get_fabric_name());
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 *ret = -EOVERFLOW;
1442 return NULL;
1443 }
1444 nacl = core_tpg_get_initiator_node_acl(tpg, initiatorname);
Andy Grover6708bb22011-06-08 10:36:43 -07001445 if (!nacl) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001446 *ret = -EINVAL;
1447 return NULL;
1448 }
1449 lacl = kzalloc(sizeof(struct se_lun_acl), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001450 if (!lacl) {
1451 pr_err("Unable to allocate memory for struct se_lun_acl.\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452 *ret = -ENOMEM;
1453 return NULL;
1454 }
1455
1456 INIT_LIST_HEAD(&lacl->lacl_list);
1457 lacl->mapped_lun = mapped_lun;
1458 lacl->se_lun_nacl = nacl;
1459 snprintf(lacl->initiatorname, TRANSPORT_IQN_LEN, "%s", initiatorname);
1460
1461 return lacl;
1462}
1463
1464int core_dev_add_initiator_node_lun_acl(
1465 struct se_portal_group *tpg,
1466 struct se_lun_acl *lacl,
1467 u32 unpacked_lun,
1468 u32 lun_access)
1469{
1470 struct se_lun *lun;
1471 struct se_node_acl *nacl;
1472
1473 lun = core_dev_get_lun(tpg, unpacked_lun);
Andy Grover6708bb22011-06-08 10:36:43 -07001474 if (!lun) {
1475 pr_err("%s Logical Unit Number: %u is not active on"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 " Target Portal Group: %hu, ignoring request.\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001477 tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
1478 tpg->se_tpg_tfo->tpg_get_tag(tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 return -EINVAL;
1480 }
1481
1482 nacl = lacl->se_lun_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07001483 if (!nacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484 return -EINVAL;
1485
1486 if ((lun->lun_access & TRANSPORT_LUNFLAGS_READ_ONLY) &&
1487 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE))
1488 lun_access = TRANSPORT_LUNFLAGS_READ_ONLY;
1489
1490 lacl->se_lun = lun;
1491
1492 if (core_update_device_list_for_node(lun, lacl, lacl->mapped_lun,
1493 lun_access, nacl, tpg, 1) < 0)
1494 return -EINVAL;
1495
1496 spin_lock(&lun->lun_acl_lock);
1497 list_add_tail(&lacl->lacl_list, &lun->lun_acl_list);
1498 atomic_inc(&lun->lun_acl_count);
1499 smp_mb__after_atomic_inc();
1500 spin_unlock(&lun->lun_acl_lock);
1501
Andy Grover6708bb22011-06-08 10:36:43 -07001502 pr_debug("%s_TPG[%hu]_LUN[%u->%u] - Added %s ACL for "
Andy Grovere3d6f902011-07-19 08:55:10 +00001503 " InitiatorNode: %s\n", tpg->se_tpg_tfo->get_fabric_name(),
1504 tpg->se_tpg_tfo->tpg_get_tag(tpg), unpacked_lun, lacl->mapped_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001505 (lun_access & TRANSPORT_LUNFLAGS_READ_WRITE) ? "RW" : "RO",
1506 lacl->initiatorname);
1507 /*
1508 * Check to see if there are any existing persistent reservation APTPL
1509 * pre-registrations that need to be enabled for this LUN ACL..
1510 */
1511 core_scsi3_check_aptpl_registration(lun->lun_se_dev, tpg, lun, lacl);
1512 return 0;
1513}
1514
1515/* core_dev_del_initiator_node_lun_acl():
1516 *
1517 *
1518 */
1519int core_dev_del_initiator_node_lun_acl(
1520 struct se_portal_group *tpg,
1521 struct se_lun *lun,
1522 struct se_lun_acl *lacl)
1523{
1524 struct se_node_acl *nacl;
1525
1526 nacl = lacl->se_lun_nacl;
Andy Grover6708bb22011-06-08 10:36:43 -07001527 if (!nacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001528 return -EINVAL;
1529
1530 spin_lock(&lun->lun_acl_lock);
1531 list_del(&lacl->lacl_list);
1532 atomic_dec(&lun->lun_acl_count);
1533 smp_mb__after_atomic_dec();
1534 spin_unlock(&lun->lun_acl_lock);
1535
1536 core_update_device_list_for_node(lun, NULL, lacl->mapped_lun,
1537 TRANSPORT_LUNFLAGS_NO_ACCESS, nacl, tpg, 0);
1538
1539 lacl->se_lun = NULL;
1540
Andy Grover6708bb22011-06-08 10:36:43 -07001541 pr_debug("%s_TPG[%hu]_LUN[%u] - Removed ACL for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001542 " InitiatorNode: %s Mapped LUN: %u\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001543 tpg->se_tpg_tfo->get_fabric_name(),
1544 tpg->se_tpg_tfo->tpg_get_tag(tpg), lun->unpacked_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001545 lacl->initiatorname, lacl->mapped_lun);
1546
1547 return 0;
1548}
1549
1550void core_dev_free_initiator_node_lun_acl(
1551 struct se_portal_group *tpg,
1552 struct se_lun_acl *lacl)
1553{
Andy Grover6708bb22011-06-08 10:36:43 -07001554 pr_debug("%s_TPG[%hu] - Freeing ACL for %s InitiatorNode: %s"
Andy Grovere3d6f902011-07-19 08:55:10 +00001555 " Mapped LUN: %u\n", tpg->se_tpg_tfo->get_fabric_name(),
1556 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1557 tpg->se_tpg_tfo->get_fabric_name(),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001558 lacl->initiatorname, lacl->mapped_lun);
1559
1560 kfree(lacl);
1561}
1562
1563int core_dev_setup_virtual_lun0(void)
1564{
1565 struct se_hba *hba;
1566 struct se_device *dev;
1567 struct se_subsystem_dev *se_dev = NULL;
1568 struct se_subsystem_api *t;
1569 char buf[16];
1570 int ret;
1571
Andy Grover6708bb22011-06-08 10:36:43 -07001572 hba = core_alloc_hba("rd_mcp", 0, HBA_FLAGS_INTERNAL_USE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573 if (IS_ERR(hba))
1574 return PTR_ERR(hba);
1575
Andy Grovere3d6f902011-07-19 08:55:10 +00001576 lun0_hba = hba;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001577 t = hba->transport;
1578
1579 se_dev = kzalloc(sizeof(struct se_subsystem_dev), GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001580 if (!se_dev) {
1581 pr_err("Unable to allocate memory for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 " struct se_subsystem_dev\n");
1583 ret = -ENOMEM;
1584 goto out;
1585 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001586 INIT_LIST_HEAD(&se_dev->t10_wwn.t10_vpd_list);
1587 spin_lock_init(&se_dev->t10_wwn.t10_vpd_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001588 INIT_LIST_HEAD(&se_dev->t10_pr.registration_list);
1589 INIT_LIST_HEAD(&se_dev->t10_pr.aptpl_reg_list);
1590 spin_lock_init(&se_dev->t10_pr.registration_lock);
1591 spin_lock_init(&se_dev->t10_pr.aptpl_reg_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001592 INIT_LIST_HEAD(&se_dev->t10_alua.tg_pt_gps_list);
1593 spin_lock_init(&se_dev->t10_alua.tg_pt_gps_lock);
1594 spin_lock_init(&se_dev->se_dev_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001595 se_dev->t10_pr.pr_aptpl_buf_len = PR_APTPL_BUF_LEN;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001596 se_dev->t10_wwn.t10_sub_dev = se_dev;
1597 se_dev->t10_alua.t10_sub_dev = se_dev;
1598 se_dev->se_dev_attrib.da_sub_dev = se_dev;
1599 se_dev->se_dev_hba = hba;
1600
1601 se_dev->se_dev_su_ptr = t->allocate_virtdevice(hba, "virt_lun0");
Andy Grover6708bb22011-06-08 10:36:43 -07001602 if (!se_dev->se_dev_su_ptr) {
1603 pr_err("Unable to locate subsystem dependent pointer"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001604 " from allocate_virtdevice()\n");
1605 ret = -ENOMEM;
1606 goto out;
1607 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001608 lun0_su_dev = se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001609
1610 memset(buf, 0, 16);
1611 sprintf(buf, "rd_pages=8");
1612 t->set_configfs_dev_params(hba, se_dev, buf, sizeof(buf));
1613
1614 dev = t->create_virtdevice(hba, se_dev, se_dev->se_dev_su_ptr);
Andy Grovere3d6f902011-07-19 08:55:10 +00001615 if (IS_ERR(dev)) {
1616 ret = PTR_ERR(dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001617 goto out;
1618 }
1619 se_dev->se_dev_ptr = dev;
Andy Grovere3d6f902011-07-19 08:55:10 +00001620 g_lun0_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621
1622 return 0;
1623out:
Andy Grovere3d6f902011-07-19 08:55:10 +00001624 lun0_su_dev = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001625 kfree(se_dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00001626 if (lun0_hba) {
1627 core_delete_hba(lun0_hba);
1628 lun0_hba = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629 }
1630 return ret;
1631}
1632
1633
1634void core_dev_release_virtual_lun0(void)
1635{
Andy Grovere3d6f902011-07-19 08:55:10 +00001636 struct se_hba *hba = lun0_hba;
1637 struct se_subsystem_dev *su_dev = lun0_su_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638
Andy Grover6708bb22011-06-08 10:36:43 -07001639 if (!hba)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001640 return;
1641
Andy Grovere3d6f902011-07-19 08:55:10 +00001642 if (g_lun0_dev)
1643 se_free_virtual_device(g_lun0_dev, hba);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001644
1645 kfree(su_dev);
1646 core_delete_hba(hba);
1647}