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