Commit | Line | Data |
---|---|---|
c66ac9db NB |
1 | /******************************************************************************* |
2 | * Filename: target_core_ua.c | |
3 | * | |
4 | * This file contains logic for SPC-3 Unit Attention emulation | |
5 | * | |
4c76251e | 6 | * (c) Copyright 2009-2013 Datera, Inc. |
c66ac9db NB |
7 | * |
8 | * Nicholas A. Bellinger <nab@kernel.org> | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | * | |
24 | ******************************************************************************/ | |
25 | ||
c66ac9db NB |
26 | #include <linux/slab.h> |
27 | #include <linux/spinlock.h> | |
ba929992 | 28 | #include <scsi/scsi_proto.h> |
c66ac9db NB |
29 | |
30 | #include <target/target_core_base.h> | |
c4795fb2 | 31 | #include <target/target_core_fabric.h> |
c66ac9db NB |
32 | #include <target/target_core_configfs.h> |
33 | ||
e26d99ae | 34 | #include "target_core_internal.h" |
c66ac9db | 35 | #include "target_core_alua.h" |
c66ac9db NB |
36 | #include "target_core_pr.h" |
37 | #include "target_core_ua.h" | |
38 | ||
de103c93 CH |
39 | sense_reason_t |
40 | target_scsi3_ua_check(struct se_cmd *cmd) | |
c66ac9db NB |
41 | { |
42 | struct se_dev_entry *deve; | |
43 | struct se_session *sess = cmd->se_sess; | |
44 | struct se_node_acl *nacl; | |
45 | ||
6708bb27 | 46 | if (!sess) |
c66ac9db NB |
47 | return 0; |
48 | ||
49 | nacl = sess->se_node_acl; | |
6708bb27 | 50 | if (!nacl) |
c66ac9db NB |
51 | return 0; |
52 | ||
f2083241 | 53 | deve = nacl->device_list[cmd->orig_fe_lun]; |
6708bb27 | 54 | if (!atomic_read(&deve->ua_count)) |
c66ac9db NB |
55 | return 0; |
56 | /* | |
57 | * From sam4r14, section 5.14 Unit attention condition: | |
58 | * | |
59 | * a) if an INQUIRY command enters the enabled command state, the | |
60 | * device server shall process the INQUIRY command and shall neither | |
61 | * report nor clear any unit attention condition; | |
62 | * b) if a REPORT LUNS command enters the enabled command state, the | |
63 | * device server shall process the REPORT LUNS command and shall not | |
64 | * report any unit attention condition; | |
65 | * e) if a REQUEST SENSE command enters the enabled command state while | |
66 | * a unit attention condition exists for the SCSI initiator port | |
67 | * associated with the I_T nexus on which the REQUEST SENSE command | |
68 | * was received, then the device server shall process the command | |
69 | * and either: | |
70 | */ | |
de103c93 | 71 | switch (cmd->t_task_cdb[0]) { |
c66ac9db NB |
72 | case INQUIRY: |
73 | case REPORT_LUNS: | |
74 | case REQUEST_SENSE: | |
75 | return 0; | |
76 | default: | |
de103c93 | 77 | return TCM_CHECK_CONDITION_UNIT_ATTENTION; |
c66ac9db | 78 | } |
c66ac9db NB |
79 | } |
80 | ||
81 | int core_scsi3_ua_allocate( | |
82 | struct se_node_acl *nacl, | |
83 | u32 unpacked_lun, | |
84 | u8 asc, | |
85 | u8 ascq) | |
86 | { | |
87 | struct se_dev_entry *deve; | |
88 | struct se_ua *ua, *ua_p, *ua_tmp; | |
89 | /* | |
90 | * PASSTHROUGH OPS | |
91 | */ | |
6708bb27 | 92 | if (!nacl) |
e3d6f909 | 93 | return -EINVAL; |
c66ac9db NB |
94 | |
95 | ua = kmem_cache_zalloc(se_ua_cache, GFP_ATOMIC); | |
6708bb27 AG |
96 | if (!ua) { |
97 | pr_err("Unable to allocate struct se_ua\n"); | |
e3d6f909 | 98 | return -ENOMEM; |
c66ac9db | 99 | } |
c66ac9db NB |
100 | INIT_LIST_HEAD(&ua->ua_nacl_list); |
101 | ||
102 | ua->ua_nacl = nacl; | |
103 | ua->ua_asc = asc; | |
104 | ua->ua_ascq = ascq; | |
105 | ||
106 | spin_lock_irq(&nacl->device_list_lock); | |
f2083241 | 107 | deve = nacl->device_list[unpacked_lun]; |
c66ac9db NB |
108 | |
109 | spin_lock(&deve->ua_lock); | |
110 | list_for_each_entry_safe(ua_p, ua_tmp, &deve->ua_list, ua_nacl_list) { | |
111 | /* | |
112 | * Do not report the same UNIT ATTENTION twice.. | |
113 | */ | |
114 | if ((ua_p->ua_asc == asc) && (ua_p->ua_ascq == ascq)) { | |
115 | spin_unlock(&deve->ua_lock); | |
116 | spin_unlock_irq(&nacl->device_list_lock); | |
117 | kmem_cache_free(se_ua_cache, ua); | |
118 | return 0; | |
119 | } | |
120 | /* | |
121 | * Attach the highest priority Unit Attention to | |
122 | * the head of the list following sam4r14, | |
123 | * Section 5.14 Unit Attention Condition: | |
124 | * | |
125 | * POWER ON, RESET, OR BUS DEVICE RESET OCCURRED highest | |
126 | * POWER ON OCCURRED or | |
127 | * DEVICE INTERNAL RESET | |
128 | * SCSI BUS RESET OCCURRED or | |
129 | * MICROCODE HAS BEEN CHANGED or | |
130 | * protocol specific | |
131 | * BUS DEVICE RESET FUNCTION OCCURRED | |
132 | * I_T NEXUS LOSS OCCURRED | |
133 | * COMMANDS CLEARED BY POWER LOSS NOTIFICATION | |
134 | * all others Lowest | |
135 | * | |
136 | * Each of the ASCQ codes listed above are defined in | |
137 | * the 29h ASC family, see spc4r17 Table D.1 | |
138 | */ | |
139 | if (ua_p->ua_asc == 0x29) { | |
140 | if ((asc == 0x29) && (ascq > ua_p->ua_ascq)) | |
141 | list_add(&ua->ua_nacl_list, | |
142 | &deve->ua_list); | |
143 | else | |
144 | list_add_tail(&ua->ua_nacl_list, | |
145 | &deve->ua_list); | |
146 | } else if (ua_p->ua_asc == 0x2a) { | |
147 | /* | |
148 | * Incoming Family 29h ASCQ codes will override | |
149 | * Family 2AHh ASCQ codes for Unit Attention condition. | |
150 | */ | |
151 | if ((asc == 0x29) || (ascq > ua_p->ua_asc)) | |
152 | list_add(&ua->ua_nacl_list, | |
153 | &deve->ua_list); | |
154 | else | |
155 | list_add_tail(&ua->ua_nacl_list, | |
156 | &deve->ua_list); | |
157 | } else | |
158 | list_add_tail(&ua->ua_nacl_list, | |
159 | &deve->ua_list); | |
160 | spin_unlock(&deve->ua_lock); | |
161 | spin_unlock_irq(&nacl->device_list_lock); | |
162 | ||
33940d09 | 163 | atomic_inc_mb(&deve->ua_count); |
c66ac9db NB |
164 | return 0; |
165 | } | |
166 | list_add_tail(&ua->ua_nacl_list, &deve->ua_list); | |
167 | spin_unlock(&deve->ua_lock); | |
168 | spin_unlock_irq(&nacl->device_list_lock); | |
169 | ||
6708bb27 | 170 | pr_debug("[%s]: Allocated UNIT ATTENTION, mapped LUN: %u, ASC:" |
c66ac9db | 171 | " 0x%02x, ASCQ: 0x%02x\n", |
e3d6f909 | 172 | nacl->se_tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun, |
c66ac9db NB |
173 | asc, ascq); |
174 | ||
33940d09 | 175 | atomic_inc_mb(&deve->ua_count); |
c66ac9db NB |
176 | return 0; |
177 | } | |
178 | ||
179 | void core_scsi3_ua_release_all( | |
180 | struct se_dev_entry *deve) | |
181 | { | |
182 | struct se_ua *ua, *ua_p; | |
183 | ||
184 | spin_lock(&deve->ua_lock); | |
185 | list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) { | |
186 | list_del(&ua->ua_nacl_list); | |
187 | kmem_cache_free(se_ua_cache, ua); | |
188 | ||
33940d09 | 189 | atomic_dec_mb(&deve->ua_count); |
c66ac9db NB |
190 | } |
191 | spin_unlock(&deve->ua_lock); | |
192 | } | |
193 | ||
194 | void core_scsi3_ua_for_check_condition( | |
195 | struct se_cmd *cmd, | |
196 | u8 *asc, | |
197 | u8 *ascq) | |
198 | { | |
5951146d | 199 | struct se_device *dev = cmd->se_dev; |
c66ac9db NB |
200 | struct se_dev_entry *deve; |
201 | struct se_session *sess = cmd->se_sess; | |
202 | struct se_node_acl *nacl; | |
203 | struct se_ua *ua = NULL, *ua_p; | |
204 | int head = 1; | |
205 | ||
6708bb27 | 206 | if (!sess) |
c66ac9db NB |
207 | return; |
208 | ||
209 | nacl = sess->se_node_acl; | |
6708bb27 | 210 | if (!nacl) |
c66ac9db NB |
211 | return; |
212 | ||
213 | spin_lock_irq(&nacl->device_list_lock); | |
f2083241 | 214 | deve = nacl->device_list[cmd->orig_fe_lun]; |
6708bb27 | 215 | if (!atomic_read(&deve->ua_count)) { |
c66ac9db NB |
216 | spin_unlock_irq(&nacl->device_list_lock); |
217 | return; | |
218 | } | |
219 | /* | |
220 | * The highest priority Unit Attentions are placed at the head of the | |
221 | * struct se_dev_entry->ua_list, and will be returned in CHECK_CONDITION + | |
222 | * sense data for the received CDB. | |
223 | */ | |
224 | spin_lock(&deve->ua_lock); | |
225 | list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) { | |
226 | /* | |
227 | * For ua_intlck_ctrl code not equal to 00b, only report the | |
228 | * highest priority UNIT_ATTENTION and ASC/ASCQ without | |
229 | * clearing it. | |
230 | */ | |
0fd97ccf | 231 | if (dev->dev_attrib.emulate_ua_intlck_ctrl != 0) { |
c66ac9db NB |
232 | *asc = ua->ua_asc; |
233 | *ascq = ua->ua_ascq; | |
234 | break; | |
235 | } | |
236 | /* | |
237 | * Otherwise for the default 00b, release the UNIT ATTENTION | |
25985edc | 238 | * condition. Return the ASC/ASCQ of the highest priority UA |
c66ac9db NB |
239 | * (head of the list) in the outgoing CHECK_CONDITION + sense. |
240 | */ | |
241 | if (head) { | |
242 | *asc = ua->ua_asc; | |
243 | *ascq = ua->ua_ascq; | |
244 | head = 0; | |
245 | } | |
246 | list_del(&ua->ua_nacl_list); | |
247 | kmem_cache_free(se_ua_cache, ua); | |
248 | ||
33940d09 | 249 | atomic_dec_mb(&deve->ua_count); |
c66ac9db NB |
250 | } |
251 | spin_unlock(&deve->ua_lock); | |
252 | spin_unlock_irq(&nacl->device_list_lock); | |
253 | ||
6708bb27 | 254 | pr_debug("[%s]: %s UNIT ATTENTION condition with" |
c66ac9db NB |
255 | " INTLCK_CTRL: %d, mapped LUN: %u, got CDB: 0x%02x" |
256 | " reported ASC: 0x%02x, ASCQ: 0x%02x\n", | |
e3d6f909 | 257 | nacl->se_tpg->se_tpg_tfo->get_fabric_name(), |
0fd97ccf CH |
258 | (dev->dev_attrib.emulate_ua_intlck_ctrl != 0) ? "Reporting" : |
259 | "Releasing", dev->dev_attrib.emulate_ua_intlck_ctrl, | |
a1d8b49a | 260 | cmd->orig_fe_lun, cmd->t_task_cdb[0], *asc, *ascq); |
c66ac9db NB |
261 | } |
262 | ||
263 | int core_scsi3_ua_clear_for_request_sense( | |
264 | struct se_cmd *cmd, | |
265 | u8 *asc, | |
266 | u8 *ascq) | |
267 | { | |
268 | struct se_dev_entry *deve; | |
269 | struct se_session *sess = cmd->se_sess; | |
270 | struct se_node_acl *nacl; | |
271 | struct se_ua *ua = NULL, *ua_p; | |
272 | int head = 1; | |
273 | ||
6708bb27 | 274 | if (!sess) |
e3d6f909 | 275 | return -EINVAL; |
c66ac9db NB |
276 | |
277 | nacl = sess->se_node_acl; | |
6708bb27 | 278 | if (!nacl) |
e3d6f909 | 279 | return -EINVAL; |
c66ac9db NB |
280 | |
281 | spin_lock_irq(&nacl->device_list_lock); | |
f2083241 | 282 | deve = nacl->device_list[cmd->orig_fe_lun]; |
6708bb27 | 283 | if (!atomic_read(&deve->ua_count)) { |
c66ac9db | 284 | spin_unlock_irq(&nacl->device_list_lock); |
e3d6f909 | 285 | return -EPERM; |
c66ac9db NB |
286 | } |
287 | /* | |
288 | * The highest priority Unit Attentions are placed at the head of the | |
289 | * struct se_dev_entry->ua_list. The First (and hence highest priority) | |
290 | * ASC/ASCQ will be returned in REQUEST_SENSE payload data for the | |
291 | * matching struct se_lun. | |
292 | * | |
293 | * Once the returning ASC/ASCQ values are set, we go ahead and | |
25985edc | 294 | * release all of the Unit Attention conditions for the associated |
c66ac9db NB |
295 | * struct se_lun. |
296 | */ | |
297 | spin_lock(&deve->ua_lock); | |
298 | list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) { | |
299 | if (head) { | |
300 | *asc = ua->ua_asc; | |
301 | *ascq = ua->ua_ascq; | |
302 | head = 0; | |
303 | } | |
304 | list_del(&ua->ua_nacl_list); | |
305 | kmem_cache_free(se_ua_cache, ua); | |
306 | ||
33940d09 | 307 | atomic_dec_mb(&deve->ua_count); |
c66ac9db NB |
308 | } |
309 | spin_unlock(&deve->ua_lock); | |
310 | spin_unlock_irq(&nacl->device_list_lock); | |
311 | ||
6708bb27 | 312 | pr_debug("[%s]: Released UNIT ATTENTION condition, mapped" |
c66ac9db | 313 | " LUN: %u, got REQUEST_SENSE reported ASC: 0x%02x," |
e3d6f909 | 314 | " ASCQ: 0x%02x\n", nacl->se_tpg->se_tpg_tfo->get_fabric_name(), |
c66ac9db NB |
315 | cmd->orig_fe_lun, *asc, *ascq); |
316 | ||
e3d6f909 | 317 | return (head) ? -EPERM : 0; |
c66ac9db | 318 | } |