target: Minimize SCSI header #include directives
[linux/fpc-iii.git] / drivers / target / target_core_ua.c
blobe44cc94b12cb1b10ae3670f053fc0456255ac515
1 /*******************************************************************************
2 * Filename: target_core_ua.c
4 * This file contains logic for SPC-3 Unit Attention emulation
6 * (c) Copyright 2009-2013 Datera, Inc.
8 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
24 ******************************************************************************/
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <scsi/scsi_proto.h>
30 #include <target/target_core_base.h>
31 #include <target/target_core_fabric.h>
32 #include <target/target_core_configfs.h>
34 #include "target_core_internal.h"
35 #include "target_core_alua.h"
36 #include "target_core_pr.h"
37 #include "target_core_ua.h"
39 sense_reason_t
40 target_scsi3_ua_check(struct se_cmd *cmd)
42 struct se_dev_entry *deve;
43 struct se_session *sess = cmd->se_sess;
44 struct se_node_acl *nacl;
46 if (!sess)
47 return 0;
49 nacl = sess->se_node_acl;
50 if (!nacl)
51 return 0;
53 deve = nacl->device_list[cmd->orig_fe_lun];
54 if (!atomic_read(&deve->ua_count))
55 return 0;
57 * From sam4r14, section 5.14 Unit attention condition:
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:
71 switch (cmd->t_task_cdb[0]) {
72 case INQUIRY:
73 case REPORT_LUNS:
74 case REQUEST_SENSE:
75 return 0;
76 default:
77 return TCM_CHECK_CONDITION_UNIT_ATTENTION;
81 int core_scsi3_ua_allocate(
82 struct se_node_acl *nacl,
83 u32 unpacked_lun,
84 u8 asc,
85 u8 ascq)
87 struct se_dev_entry *deve;
88 struct se_ua *ua, *ua_p, *ua_tmp;
90 * PASSTHROUGH OPS
92 if (!nacl)
93 return -EINVAL;
95 ua = kmem_cache_zalloc(se_ua_cache, GFP_ATOMIC);
96 if (!ua) {
97 pr_err("Unable to allocate struct se_ua\n");
98 return -ENOMEM;
100 INIT_LIST_HEAD(&ua->ua_nacl_list);
102 ua->ua_nacl = nacl;
103 ua->ua_asc = asc;
104 ua->ua_ascq = ascq;
106 spin_lock_irq(&nacl->device_list_lock);
107 deve = nacl->device_list[unpacked_lun];
109 spin_lock(&deve->ua_lock);
110 list_for_each_entry_safe(ua_p, ua_tmp, &deve->ua_list, ua_nacl_list) {
112 * Do not report the same UNIT ATTENTION twice..
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;
121 * Attach the highest priority Unit Attention to
122 * the head of the list following sam4r14,
123 * Section 5.14 Unit Attention Condition:
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
136 * Each of the ASCQ codes listed above are defined in
137 * the 29h ASC family, see spc4r17 Table D.1
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) {
148 * Incoming Family 29h ASCQ codes will override
149 * Family 2AHh ASCQ codes for Unit Attention condition.
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);
163 atomic_inc_mb(&deve->ua_count);
164 return 0;
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);
170 pr_debug("[%s]: Allocated UNIT ATTENTION, mapped LUN: %u, ASC:"
171 " 0x%02x, ASCQ: 0x%02x\n",
172 nacl->se_tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
173 asc, ascq);
175 atomic_inc_mb(&deve->ua_count);
176 return 0;
179 void core_scsi3_ua_release_all(
180 struct se_dev_entry *deve)
182 struct se_ua *ua, *ua_p;
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);
189 atomic_dec_mb(&deve->ua_count);
191 spin_unlock(&deve->ua_lock);
194 void core_scsi3_ua_for_check_condition(
195 struct se_cmd *cmd,
196 u8 *asc,
197 u8 *ascq)
199 struct se_device *dev = cmd->se_dev;
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;
206 if (!sess)
207 return;
209 nacl = sess->se_node_acl;
210 if (!nacl)
211 return;
213 spin_lock_irq(&nacl->device_list_lock);
214 deve = nacl->device_list[cmd->orig_fe_lun];
215 if (!atomic_read(&deve->ua_count)) {
216 spin_unlock_irq(&nacl->device_list_lock);
217 return;
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.
224 spin_lock(&deve->ua_lock);
225 list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
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.
231 if (dev->dev_attrib.emulate_ua_intlck_ctrl != 0) {
232 *asc = ua->ua_asc;
233 *ascq = ua->ua_ascq;
234 break;
237 * Otherwise for the default 00b, release the UNIT ATTENTION
238 * condition. Return the ASC/ASCQ of the highest priority UA
239 * (head of the list) in the outgoing CHECK_CONDITION + sense.
241 if (head) {
242 *asc = ua->ua_asc;
243 *ascq = ua->ua_ascq;
244 head = 0;
246 list_del(&ua->ua_nacl_list);
247 kmem_cache_free(se_ua_cache, ua);
249 atomic_dec_mb(&deve->ua_count);
251 spin_unlock(&deve->ua_lock);
252 spin_unlock_irq(&nacl->device_list_lock);
254 pr_debug("[%s]: %s UNIT ATTENTION condition with"
255 " INTLCK_CTRL: %d, mapped LUN: %u, got CDB: 0x%02x"
256 " reported ASC: 0x%02x, ASCQ: 0x%02x\n",
257 nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
258 (dev->dev_attrib.emulate_ua_intlck_ctrl != 0) ? "Reporting" :
259 "Releasing", dev->dev_attrib.emulate_ua_intlck_ctrl,
260 cmd->orig_fe_lun, cmd->t_task_cdb[0], *asc, *ascq);
263 int core_scsi3_ua_clear_for_request_sense(
264 struct se_cmd *cmd,
265 u8 *asc,
266 u8 *ascq)
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;
274 if (!sess)
275 return -EINVAL;
277 nacl = sess->se_node_acl;
278 if (!nacl)
279 return -EINVAL;
281 spin_lock_irq(&nacl->device_list_lock);
282 deve = nacl->device_list[cmd->orig_fe_lun];
283 if (!atomic_read(&deve->ua_count)) {
284 spin_unlock_irq(&nacl->device_list_lock);
285 return -EPERM;
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.
293 * Once the returning ASC/ASCQ values are set, we go ahead and
294 * release all of the Unit Attention conditions for the associated
295 * struct se_lun.
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;
304 list_del(&ua->ua_nacl_list);
305 kmem_cache_free(se_ua_cache, ua);
307 atomic_dec_mb(&deve->ua_count);
309 spin_unlock(&deve->ua_lock);
310 spin_unlock_irq(&nacl->device_list_lock);
312 pr_debug("[%s]: Released UNIT ATTENTION condition, mapped"
313 " LUN: %u, got REQUEST_SENSE reported ASC: 0x%02x,"
314 " ASCQ: 0x%02x\n", nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
315 cmd->orig_fe_lun, *asc, *ascq);
317 return (head) ? -EPERM : 0;