Blame view

drivers/target/target_core_ua.c 9.09 KB
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*******************************************************************************
   * Filename: target_core_ua.c
   *
   * This file contains logic for SPC-3 Unit Attention emulation
   *
   * Copyright (c) 2009,2010 Rising Tide Systems
   * Copyright (c) 2009,2010 Linux-iSCSI.org
   *
   * Nicholas A. Bellinger <nab@kernel.org>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   *
   ******************************************************************************/
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <scsi/scsi.h>
  #include <scsi/scsi_cmnd.h>
  
  #include <target/target_core_base.h>
  #include <target/target_core_device.h>
  #include <target/target_core_transport.h>
  #include <target/target_core_fabric_ops.h>
  #include <target/target_core_configfs.h>
  
  #include "target_core_alua.h"
  #include "target_core_hba.h"
  #include "target_core_pr.h"
  #include "target_core_ua.h"
  
  int core_scsi3_ua_check(
  	struct se_cmd *cmd,
  	unsigned char *cdb)
  {
  	struct se_dev_entry *deve;
  	struct se_session *sess = cmd->se_sess;
  	struct se_node_acl *nacl;
6708bb27b   Andy Grover   target: Follow up...
49
  	if (!sess)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
50
51
52
  		return 0;
  
  	nacl = sess->se_node_acl;
6708bb27b   Andy Grover   target: Follow up...
53
  	if (!nacl)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
54
55
56
  		return 0;
  
  	deve = &nacl->device_list[cmd->orig_fe_lun];
6708bb27b   Andy Grover   target: Follow up...
57
  	if (!atomic_read(&deve->ua_count))
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  		return 0;
  	/*
  	 * 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:
  	 */
  	switch (cdb[0]) {
  	case INQUIRY:
  	case REPORT_LUNS:
  	case REQUEST_SENSE:
  		return 0;
  	default:
e3d6f909e   Andy Grover   target: Core clea...
80
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
81
  	}
e3d6f909e   Andy Grover   target: Core clea...
82
  	return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
83
84
85
86
87
88
89
90
91
92
93
94
95
  }
  
  int core_scsi3_ua_allocate(
  	struct se_node_acl *nacl,
  	u32 unpacked_lun,
  	u8 asc,
  	u8 ascq)
  {
  	struct se_dev_entry *deve;
  	struct se_ua *ua, *ua_p, *ua_tmp;
  	/*
  	 * PASSTHROUGH OPS
  	 */
6708bb27b   Andy Grover   target: Follow up...
96
  	if (!nacl)
e3d6f909e   Andy Grover   target: Core clea...
97
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
98
99
  
  	ua = kmem_cache_zalloc(se_ua_cache, GFP_ATOMIC);
6708bb27b   Andy Grover   target: Follow up...
100
101
102
  	if (!ua) {
  		pr_err("Unable to allocate struct se_ua
  ");
e3d6f909e   Andy Grover   target: Core clea...
103
  		return -ENOMEM;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  	}
  	INIT_LIST_HEAD(&ua->ua_dev_list);
  	INIT_LIST_HEAD(&ua->ua_nacl_list);
  
  	ua->ua_nacl = nacl;
  	ua->ua_asc = asc;
  	ua->ua_ascq = ascq;
  
  	spin_lock_irq(&nacl->device_list_lock);
  	deve = &nacl->device_list[unpacked_lun];
  
  	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);
  			spin_unlock_irq(&nacl->device_list_lock);
  			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);
  		spin_unlock_irq(&nacl->device_list_lock);
  
  		atomic_inc(&deve->ua_count);
  		smp_mb__after_atomic_inc();
  		return 0;
  	}
  	list_add_tail(&ua->ua_nacl_list, &deve->ua_list);
  	spin_unlock(&deve->ua_lock);
  	spin_unlock_irq(&nacl->device_list_lock);
6708bb27b   Andy Grover   target: Follow up...
176
  	pr_debug("[%s]: Allocated UNIT ATTENTION, mapped LUN: %u, ASC:"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
177
178
  		" 0x%02x, ASCQ: 0x%02x
  ",
e3d6f909e   Andy Grover   target: Core clea...
179
  		nacl->se_tpg->se_tpg_tfo->get_fabric_name(), unpacked_lun,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  		asc, ascq);
  
  	atomic_inc(&deve->ua_count);
  	smp_mb__after_atomic_inc();
  	return 0;
  }
  
  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);
  
  		atomic_dec(&deve->ua_count);
  		smp_mb__after_atomic_dec();
  	}
  	spin_unlock(&deve->ua_lock);
  }
  
  void core_scsi3_ua_for_check_condition(
  	struct se_cmd *cmd,
  	u8 *asc,
  	u8 *ascq)
  {
5951146de   Andy Grover   target: More core...
208
  	struct se_device *dev = cmd->se_dev;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
209
210
211
212
213
  	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...
214
  	if (!sess)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
215
216
217
  		return;
  
  	nacl = sess->se_node_acl;
6708bb27b   Andy Grover   target: Follow up...
218
  	if (!nacl)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
219
220
221
222
  		return;
  
  	spin_lock_irq(&nacl->device_list_lock);
  	deve = &nacl->device_list[cmd->orig_fe_lun];
6708bb27b   Andy Grover   target: Follow up...
223
  	if (!atomic_read(&deve->ua_count)) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  		spin_unlock_irq(&nacl->device_list_lock);
  		return;
  	}
  	/*
  	 * 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.
  		 */
e3d6f909e   Andy Grover   target: Core clea...
239
  		if (dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl != 0) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
240
241
242
243
244
245
  			*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...
246
  		 * condition.  Return the ASC/ASCQ of the highest priority UA
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  		 * (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);
  
  		atomic_dec(&deve->ua_count);
  		smp_mb__after_atomic_dec();
  	}
  	spin_unlock(&deve->ua_lock);
  	spin_unlock_irq(&nacl->device_list_lock);
6708bb27b   Andy Grover   target: Follow up...
262
  	pr_debug("[%s]: %s UNIT ATTENTION condition with"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
263
264
265
  		" INTLCK_CTRL: %d, mapped LUN: %u, got CDB: 0x%02x"
  		" reported ASC: 0x%02x, ASCQ: 0x%02x
  ",
e3d6f909e   Andy Grover   target: Core clea...
266
267
268
  		nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
  		(dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl != 0) ? "Reporting" :
  		"Releasing", dev->se_sub_dev->se_dev_attrib.emulate_ua_intlck_ctrl,
a1d8b49ab   Andy Grover   target: Updates f...
269
  		cmd->orig_fe_lun, cmd->t_task_cdb[0], *asc, *ascq);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
270
271
272
273
274
275
276
277
278
279
280
281
  }
  
  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...
282
  	if (!sess)
e3d6f909e   Andy Grover   target: Core clea...
283
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
284
285
  
  	nacl = sess->se_node_acl;
6708bb27b   Andy Grover   target: Follow up...
286
  	if (!nacl)
e3d6f909e   Andy Grover   target: Core clea...
287
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
288
289
290
  
  	spin_lock_irq(&nacl->device_list_lock);
  	deve = &nacl->device_list[cmd->orig_fe_lun];
6708bb27b   Andy Grover   target: Follow up...
291
  	if (!atomic_read(&deve->ua_count)) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
292
  		spin_unlock_irq(&nacl->device_list_lock);
e3d6f909e   Andy Grover   target: Core clea...
293
  		return -EPERM;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
294
295
296
297
298
299
300
301
  	}
  	/*
  	 * 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...
302
  	 * release all of the Unit Attention conditions for the associated
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
  	 * 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);
  
  		atomic_dec(&deve->ua_count);
  		smp_mb__after_atomic_dec();
  	}
  	spin_unlock(&deve->ua_lock);
  	spin_unlock_irq(&nacl->device_list_lock);
6708bb27b   Andy Grover   target: Follow up...
320
  	pr_debug("[%s]: Released UNIT ATTENTION condition, mapped"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
321
  		" LUN: %u, got REQUEST_SENSE reported ASC: 0x%02x,"
e3d6f909e   Andy Grover   target: Core clea...
322
323
  		" ASCQ: 0x%02x
  ", nacl->se_tpg->se_tpg_tfo->get_fabric_name(),
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
324
  		cmd->orig_fe_lun, *asc, *ascq);
e3d6f909e   Andy Grover   target: Core clea...
325
  	return (head) ? -EPERM : 0;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
326
  }