Blame view

drivers/target/target_core_ua.c 8.27 KB
1a59d1b8e   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2
3
4
5
6
  /*******************************************************************************
   * Filename: target_core_ua.c
   *
   * This file contains logic for SPC-3 Unit Attention emulation
   *
4c76251e8   Nicholas Bellinger   target: Update co...
7
   * (c) Copyright 2009-2013 Datera, Inc.
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
8
9
10
   *
   * Nicholas A. Bellinger <nab@kernel.org>
   *
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
11
   ******************************************************************************/
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
12
13
  #include <linux/slab.h>
  #include <linux/spinlock.h>
ba9299925   Bart Van Assche   target: Minimize ...
14
  #include <scsi/scsi_proto.h>
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
15
16
  
  #include <target/target_core_base.h>
c4795fb20   Christoph Hellwig   target: header re...
17
  #include <target/target_core_fabric.h>
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
18

e26d99aed   Christoph Hellwig   target: reshuffle...
19
  #include "target_core_internal.h"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
20
  #include "target_core_alua.h"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
21
22
  #include "target_core_pr.h"
  #include "target_core_ua.h"
de103c93a   Christoph Hellwig   target: pass sens...
23
24
  sense_reason_t
  target_scsi3_ua_check(struct se_cmd *cmd)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
25
26
27
28
  {
  	struct se_dev_entry *deve;
  	struct se_session *sess = cmd->se_sess;
  	struct se_node_acl *nacl;
6708bb27b   Andy Grover   target: Follow up...
29
  	if (!sess)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
30
31
32
  		return 0;
  
  	nacl = sess->se_node_acl;
6708bb27b   Andy Grover   target: Follow up...
33
  	if (!nacl)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
34
  		return 0;
29a05deeb   Nicholas Bellinger   target: Convert s...
35
36
37
38
  	rcu_read_lock();
  	deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
  	if (!deve) {
  		rcu_read_unlock();
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
39
  		return 0;
29a05deeb   Nicholas Bellinger   target: Convert s...
40
  	}
e936a38ac   Bart Van Assche   scsi: target: Rem...
41
  	if (list_empty_careful(&deve->ua_list)) {
29a05deeb   Nicholas Bellinger   target: Convert s...
42
43
44
45
  		rcu_read_unlock();
  		return 0;
  	}
  	rcu_read_unlock();
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  	/*
  	 * From sam4r14, section 5.14 Unit attention condition:
  	 *
  	 * a) if an INQUIRY command enters the enabled command state, the
  	 *    device server shall process the INQUIRY command and shall neither
  	 *    report nor clear any unit attention condition;
  	 * b) if a REPORT LUNS command enters the enabled command state, the
  	 *    device server shall process the REPORT LUNS command and shall not
  	 *    report any unit attention condition;
  	 * e) if a REQUEST SENSE command enters the enabled command state while
  	 *    a unit attention condition exists for the SCSI initiator port
  	 *    associated with the I_T nexus on which the REQUEST SENSE command
  	 *    was received, then the device server shall process the command
  	 *    and either:
  	 */
de103c93a   Christoph Hellwig   target: pass sens...
61
  	switch (cmd->t_task_cdb[0]) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
62
63
64
65
66
  	case INQUIRY:
  	case REPORT_LUNS:
  	case REQUEST_SENSE:
  		return 0;
  	default:
de103c93a   Christoph Hellwig   target: pass sens...
67
  		return TCM_CHECK_CONDITION_UNIT_ATTENTION;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
68
  	}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
69
70
71
  }
  
  int core_scsi3_ua_allocate(
c51c8e7bc   Hannes Reinecke   target: use 'se_d...
72
  	struct se_dev_entry *deve,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
73
74
75
  	u8 asc,
  	u8 ascq)
  {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
76
  	struct se_ua *ua, *ua_p, *ua_tmp;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
77
78
  
  	ua = kmem_cache_zalloc(se_ua_cache, GFP_ATOMIC);
6708bb27b   Andy Grover   target: Follow up...
79
80
81
  	if (!ua) {
  		pr_err("Unable to allocate struct se_ua
  ");
e3d6f909e   Andy Grover   target: Core clea...
82
  		return -ENOMEM;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
83
  	}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
84
  	INIT_LIST_HEAD(&ua->ua_nacl_list);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
85
86
  	ua->ua_asc = asc;
  	ua->ua_ascq = ascq;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
87
88
89
90
91
92
93
  	spin_lock(&deve->ua_lock);
  	list_for_each_entry_safe(ua_p, ua_tmp, &deve->ua_list, ua_nacl_list) {
  		/*
  		 * Do not report the same UNIT ATTENTION twice..
  		 */
  		if ((ua_p->ua_asc == asc) && (ua_p->ua_ascq == ascq)) {
  			spin_unlock(&deve->ua_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  			kmem_cache_free(se_ua_cache, ua);
  			return 0;
  		}
  		/*
  		 * Attach the highest priority Unit Attention to
  		 * the head of the list following sam4r14,
  		 * Section 5.14 Unit Attention Condition:
  		 *
  		 * POWER ON, RESET, OR BUS DEVICE RESET OCCURRED highest
  		 * POWER ON OCCURRED or
  		 * DEVICE INTERNAL RESET
  		 * SCSI BUS RESET OCCURRED or
  		 * MICROCODE HAS BEEN CHANGED or
  		 * protocol specific
  		 * BUS DEVICE RESET FUNCTION OCCURRED
  		 * I_T NEXUS LOSS OCCURRED
  		 * COMMANDS CLEARED BY POWER LOSS NOTIFICATION
  		 * all others                                    Lowest
  		 *
  		 * Each of the ASCQ codes listed above are defined in
  		 * the 29h ASC family, see spc4r17 Table D.1
  		 */
  		if (ua_p->ua_asc == 0x29) {
  			if ((asc == 0x29) && (ascq > ua_p->ua_ascq))
  				list_add(&ua->ua_nacl_list,
  						&deve->ua_list);
  			else
  				list_add_tail(&ua->ua_nacl_list,
  						&deve->ua_list);
  		} else if (ua_p->ua_asc == 0x2a) {
  			/*
  			 * Incoming Family 29h ASCQ codes will override
  			 * Family 2AHh ASCQ codes for Unit Attention condition.
  			 */
  			if ((asc == 0x29) || (ascq > ua_p->ua_asc))
  				list_add(&ua->ua_nacl_list,
  					&deve->ua_list);
  			else
  				list_add_tail(&ua->ua_nacl_list,
  						&deve->ua_list);
  		} else
  			list_add_tail(&ua->ua_nacl_list,
  				&deve->ua_list);
  		spin_unlock(&deve->ua_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
138

c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
139
140
141
142
  		return 0;
  	}
  	list_add_tail(&ua->ua_nacl_list, &deve->ua_list);
  	spin_unlock(&deve->ua_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
143

c51c8e7bc   Hannes Reinecke   target: use 'se_d...
144
145
146
  	pr_debug("Allocated UNIT ATTENTION, mapped LUN: %llu, ASC:"
  		" 0x%02x, ASCQ: 0x%02x
  ", deve->mapped_lun,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
147
  		asc, ascq);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
148
149
  	return 0;
  }
c51c8e7bc   Hannes Reinecke   target: use 'se_d...
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  void target_ua_allocate_lun(struct se_node_acl *nacl,
  			    u32 unpacked_lun, u8 asc, u8 ascq)
  {
  	struct se_dev_entry *deve;
  
  	if (!nacl)
  		return;
  
  	rcu_read_lock();
  	deve = target_nacl_find_deve(nacl, unpacked_lun);
  	if (!deve) {
  		rcu_read_unlock();
  		return;
  	}
  
  	core_scsi3_ua_allocate(deve, asc, ascq);
  	rcu_read_unlock();
  }
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
168
169
170
171
172
173
174
175
176
  void core_scsi3_ua_release_all(
  	struct se_dev_entry *deve)
  {
  	struct se_ua *ua, *ua_p;
  
  	spin_lock(&deve->ua_lock);
  	list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  		list_del(&ua->ua_nacl_list);
  		kmem_cache_free(se_ua_cache, ua);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
177
178
179
  	}
  	spin_unlock(&deve->ua_lock);
  }
325c1e8b2   Bart Van Assche   scsi: target: Fix...
180
181
182
183
184
185
186
  /*
   * Dequeue a unit attention from the unit attention list. This function
   * returns true if the dequeuing succeeded and if *@key, *@asc and *@ascq have
   * been set.
   */
  bool core_scsi3_ua_for_check_condition(struct se_cmd *cmd, u8 *key, u8 *asc,
  				       u8 *ascq)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
187
  {
5951146de   Andy Grover   target: More core...
188
  	struct se_device *dev = cmd->se_dev;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
189
190
191
192
193
  	struct se_dev_entry *deve;
  	struct se_session *sess = cmd->se_sess;
  	struct se_node_acl *nacl;
  	struct se_ua *ua = NULL, *ua_p;
  	int head = 1;
1bf630fdd   David Disseldorp   scsi: target: use...
194
195
  	bool dev_ua_intlck_clear = (dev->dev_attrib.emulate_ua_intlck_ctrl
  						== TARGET_UA_INTLCK_CTRL_CLEAR);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
196

325c1e8b2   Bart Van Assche   scsi: target: Fix...
197
198
  	if (WARN_ON_ONCE(!sess))
  		return false;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
199
200
  
  	nacl = sess->se_node_acl;
325c1e8b2   Bart Van Assche   scsi: target: Fix...
201
202
  	if (WARN_ON_ONCE(!nacl))
  		return false;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
203

29a05deeb   Nicholas Bellinger   target: Convert s...
204
205
206
207
  	rcu_read_lock();
  	deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
  	if (!deve) {
  		rcu_read_unlock();
325c1e8b2   Bart Van Assche   scsi: target: Fix...
208
209
210
211
  		*key = ILLEGAL_REQUEST;
  		*asc = 0x25; /* LOGICAL UNIT NOT SUPPORTED */
  		*ascq = 0;
  		return true;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
212
  	}
325c1e8b2   Bart Van Assche   scsi: target: Fix...
213
  	*key = UNIT_ATTENTION;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
214
215
216
217
218
219
220
221
222
223
224
225
  	/*
  	 * The highest priority Unit Attentions are placed at the head of the
  	 * struct se_dev_entry->ua_list, and will be returned in CHECK_CONDITION +
  	 * sense data for the received CDB.
  	 */
  	spin_lock(&deve->ua_lock);
  	list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  		/*
  		 * For ua_intlck_ctrl code not equal to 00b, only report the
  		 * highest priority UNIT_ATTENTION and ASC/ASCQ without
  		 * clearing it.
  		 */
1bf630fdd   David Disseldorp   scsi: target: use...
226
  		if (!dev_ua_intlck_clear) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
227
228
229
230
231
232
  			*asc = ua->ua_asc;
  			*ascq = ua->ua_ascq;
  			break;
  		}
  		/*
  		 * Otherwise for the default 00b, release the UNIT ATTENTION
25985edce   Lucas De Marchi   Fix common misspe...
233
  		 * condition.  Return the ASC/ASCQ of the highest priority UA
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
234
235
236
237
238
239
240
241
242
  		 * (head of the list) in the outgoing CHECK_CONDITION + sense.
  		 */
  		if (head) {
  			*asc = ua->ua_asc;
  			*ascq = ua->ua_ascq;
  			head = 0;
  		}
  		list_del(&ua->ua_nacl_list);
  		kmem_cache_free(se_ua_cache, ua);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
243
244
  	}
  	spin_unlock(&deve->ua_lock);
29a05deeb   Nicholas Bellinger   target: Convert s...
245
  	rcu_read_unlock();
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
246

6708bb27b   Andy Grover   target: Follow up...
247
  	pr_debug("[%s]: %s UNIT ATTENTION condition with"
f2d306802   Hannes Reinecke   target: use 64-bi...
248
  		" INTLCK_CTRL: %d, mapped LUN: %llu, got CDB: 0x%02x"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
249
250
  		" reported ASC: 0x%02x, ASCQ: 0x%02x
  ",
30c7ca935   David Disseldorp   scsi: target: dro...
251
  		nacl->se_tpg->se_tpg_tfo->fabric_name,
1bf630fdd   David Disseldorp   scsi: target: use...
252
253
  		dev_ua_intlck_clear ? "Releasing" : "Reporting",
  		dev->dev_attrib.emulate_ua_intlck_ctrl,
a1d8b49ab   Andy Grover   target: Updates f...
254
  		cmd->orig_fe_lun, cmd->t_task_cdb[0], *asc, *ascq);
325c1e8b2   Bart Van Assche   scsi: target: Fix...
255
256
  
  	return head == 0;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
257
258
259
260
261
262
263
264
265
266
267
268
  }
  
  int core_scsi3_ua_clear_for_request_sense(
  	struct se_cmd *cmd,
  	u8 *asc,
  	u8 *ascq)
  {
  	struct se_dev_entry *deve;
  	struct se_session *sess = cmd->se_sess;
  	struct se_node_acl *nacl;
  	struct se_ua *ua = NULL, *ua_p;
  	int head = 1;
6708bb27b   Andy Grover   target: Follow up...
269
  	if (!sess)
e3d6f909e   Andy Grover   target: Core clea...
270
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
271
272
  
  	nacl = sess->se_node_acl;
6708bb27b   Andy Grover   target: Follow up...
273
  	if (!nacl)
e3d6f909e   Andy Grover   target: Core clea...
274
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
275

29a05deeb   Nicholas Bellinger   target: Convert s...
276
277
278
279
280
281
  	rcu_read_lock();
  	deve = target_nacl_find_deve(nacl, cmd->orig_fe_lun);
  	if (!deve) {
  		rcu_read_unlock();
  		return -EINVAL;
  	}
e936a38ac   Bart Van Assche   scsi: target: Rem...
282
  	if (list_empty_careful(&deve->ua_list)) {
29a05deeb   Nicholas Bellinger   target: Convert s...
283
  		rcu_read_unlock();
e3d6f909e   Andy Grover   target: Core clea...
284
  		return -EPERM;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
285
286
287
288
289
290
291
292
  	}
  	/*
  	 * The highest priority Unit Attentions are placed at the head of the
  	 * struct se_dev_entry->ua_list.  The First (and hence highest priority)
  	 * ASC/ASCQ will be returned in REQUEST_SENSE payload data for the
  	 * matching struct se_lun.
  	 *
  	 * Once the returning ASC/ASCQ values are set, we go ahead and
25985edce   Lucas De Marchi   Fix common misspe...
293
  	 * release all of the Unit Attention conditions for the associated
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
294
295
296
297
298
299
300
301
302
303
304
  	 * struct se_lun.
  	 */
  	spin_lock(&deve->ua_lock);
  	list_for_each_entry_safe(ua, ua_p, &deve->ua_list, ua_nacl_list) {
  		if (head) {
  			*asc = ua->ua_asc;
  			*ascq = ua->ua_ascq;
  			head = 0;
  		}
  		list_del(&ua->ua_nacl_list);
  		kmem_cache_free(se_ua_cache, ua);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
305
306
  	}
  	spin_unlock(&deve->ua_lock);
29a05deeb   Nicholas Bellinger   target: Convert s...
307
  	rcu_read_unlock();
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
308

6708bb27b   Andy Grover   target: Follow up...
309
  	pr_debug("[%s]: Released UNIT ATTENTION condition, mapped"
f2d306802   Hannes Reinecke   target: use 64-bi...
310
  		" LUN: %llu, got REQUEST_SENSE reported ASC: 0x%02x,"
30c7ca935   David Disseldorp   scsi: target: dro...
311
312
  		" ASCQ: 0x%02x
  ", nacl->se_tpg->se_tpg_tfo->fabric_name,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
313
  		cmd->orig_fe_lun, *asc, *ascq);
e3d6f909e   Andy Grover   target: Core clea...
314
  	return (head) ? -EPERM : 0;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
315
  }