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