Blame view

block/sed-opal.c 62.5 KB
8c16567d8   Christoph Hellwig   block: switch all...
1
  // SPDX-License-Identifier: GPL-2.0
455a7b238   Scott Bauer   block: Add Sed-op...
2
3
4
5
6
7
  /*
   * Copyright © 2016 Intel Corporation
   *
   * Authors:
   *    Scott  Bauer      <scott.bauer@intel.com>
   *    Rafael Antognolli <rafael.antognolli@intel.com>
455a7b238   Scott Bauer   block: Add Sed-op...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   */
  
  #define pr_fmt(fmt) KBUILD_MODNAME ":OPAL: " fmt
  
  #include <linux/delay.h>
  #include <linux/device.h>
  #include <linux/kernel.h>
  #include <linux/list.h>
  #include <linux/genhd.h>
  #include <linux/slab.h>
  #include <linux/uaccess.h>
  #include <uapi/linux/sed-opal.h>
  #include <linux/sed-opal.h>
  #include <linux/string.h>
  #include <linux/kdev_t.h>
  
  #include "opal_proto.h"
4f1244c82   Christoph Hellwig   block/sed-opal: a...
25
26
  #define IO_BUFFER_LENGTH 2048
  #define MAX_TOKS 64
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
27
28
  /* Number of bytes needed by cmd_finalize. */
  #define CMD_FINALIZE_BYTES_NEEDED 7
eed64951f   Jon Derrick   block/sed: Embed ...
29
30
31
32
33
  struct opal_step {
  	int (*fn)(struct opal_dev *dev, void *data);
  	void *data;
  };
  typedef int (cont_fn)(struct opal_dev *dev);
4f1244c82   Christoph Hellwig   block/sed-opal: a...
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  
  enum opal_atom_width {
  	OPAL_WIDTH_TINY,
  	OPAL_WIDTH_SHORT,
  	OPAL_WIDTH_MEDIUM,
  	OPAL_WIDTH_LONG,
  	OPAL_WIDTH_TOKEN
  };
  
  /*
   * On the parsed response, we don't store again the toks that are already
   * stored in the response buffer. Instead, for each token, we just store a
   * pointer to the position in the buffer where the token starts, and the size
   * of the token in bytes.
   */
  struct opal_resp_tok {
  	const u8 *pos;
  	size_t len;
  	enum opal_response_token type;
  	enum opal_atom_width width;
  	union {
  		u64 u;
  		s64 s;
  	} stored;
  };
  
  /*
   * From the response header it's not possible to know how many tokens there are
   * on the payload. So we hardcode that the maximum will be MAX_TOKS, and later
   * if we start dealing with messages that have more than that, we can increase
   * this number. This is done to avoid having to make two passes through the
   * response, the first one counting how many tokens we have and the second one
   * actually storing the positions.
   */
  struct parsed_resp {
  	int num;
  	struct opal_resp_tok toks[MAX_TOKS];
  };
  
  struct opal_dev {
  	bool supported;
dbec491b1   Scott Bauer   block: sed-opal: ...
75
  	bool mbr_enabled;
4f1244c82   Christoph Hellwig   block/sed-opal: a...
76
77
78
  
  	void *data;
  	sec_send_recv *send_recv;
4f1244c82   Christoph Hellwig   block/sed-opal: a...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  	struct mutex dev_lock;
  	u16 comid;
  	u32 hsn;
  	u32 tsn;
  	u64 align;
  	u64 lowest_lba;
  
  	size_t pos;
  	u8 cmd[IO_BUFFER_LENGTH];
  	u8 resp[IO_BUFFER_LENGTH];
  
  	struct parsed_resp parsed;
  	size_t prev_d_len;
  	void *prev_data;
  
  	struct list_head unlk_lst;
  };
455a7b238   Scott Bauer   block: Add Sed-op...
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
  static const u8 opaluid[][OPAL_UID_LENGTH] = {
  	/* users */
  	[OPAL_SMUID_UID] =
  		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
  	[OPAL_THISSP_UID] =
  		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  	[OPAL_ADMINSP_UID] =
  		{ 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
  	[OPAL_LOCKINGSP_UID] =
  		{ 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
  	[OPAL_ENTERPRISE_LOCKINGSP_UID] =
  		{ 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
  	[OPAL_ANYBODY_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
  	[OPAL_SID_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
  	[OPAL_ADMIN1_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
  	[OPAL_USER1_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
  	[OPAL_USER2_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
  	[OPAL_PSID_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
  	[OPAL_ENTERPRISE_BANDMASTER0_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
  	[OPAL_ENTERPRISE_ERASEMASTER_UID] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
  
  	/* tables */
dc3010256   Randy Dunlap   block: sed-opal: ...
126
  	[OPAL_TABLE_TABLE] =
ff91064ea   Jonas Rabenstein   block: sed-opal: ...
127
  		{ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01 },
455a7b238   Scott Bauer   block: Add Sed-op...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  	[OPAL_LOCKINGRANGE_GLOBAL] =
  		{ 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
  	[OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
  		{ 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
  	[OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
  		{ 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
  	[OPAL_MBRCONTROL] =
  		{ 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
  	[OPAL_MBR] =
  		{ 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
  	[OPAL_AUTHORITY_TABLE] =
  		{ 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
  	[OPAL_C_PIN_TABLE] =
  		{ 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
  	[OPAL_LOCKING_INFO_TABLE] =
  		{ 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
  	[OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
  		{ 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
62c441c6a   Revanth Rajashekar   block: sed-opal: ...
146
147
  	[OPAL_DATASTORE] =
  		{ 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00 },
455a7b238   Scott Bauer   block: Add Sed-op...
148
149
  
  	/* C_PIN_TABLE object ID's */
1e815b33c   David Kozub   block: sed-opal: ...
150
  	[OPAL_C_PIN_MSID] =
455a7b238   Scott Bauer   block: Add Sed-op...
151
152
153
154
155
156
157
  		{ 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
  	[OPAL_C_PIN_SID] =
  		{ 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
  	[OPAL_C_PIN_ADMIN1] =
  		{ 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
  
  	/* half UID's (only first 4 bytes used) */
455a7b238   Scott Bauer   block: Add Sed-op...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  	[OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
  		{ 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
  	[OPAL_HALF_UID_BOOLEAN_ACE] =
  		{ 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
  
  	/* special value for omitted optional parameter */
  	[OPAL_UID_HEXFF] =
  		{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  };
  
  /*
   * TCG Storage SSC Methods.
   * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
   * Section: 6.3 Assigned UIDs
   */
1b6b75b01   Jonas Rabenstein   block: sed-opal: ...
173
  static const u8 opalmethod[][OPAL_METHOD_LENGTH] = {
455a7b238   Scott Bauer   block: Add Sed-op...
174
175
176
177
178
179
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
  	[OPAL_PROPERTIES] =
  		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
  	[OPAL_STARTSESSION] =
  		{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
  	[OPAL_REVERT] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
  	[OPAL_ACTIVATE] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
  	[OPAL_EGET] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
  	[OPAL_ESET] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
  	[OPAL_NEXT] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
  	[OPAL_EAUTHENTICATE] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
  	[OPAL_GETACL] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
  	[OPAL_GENKEY] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
  	[OPAL_REVERTSP] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
  	[OPAL_GET] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
  	[OPAL_SET] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
  	[OPAL_AUTHENTICATE] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
  	[OPAL_RANDOM] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
  	[OPAL_ERASE] =
  		{ 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
  };
455a7b238   Scott Bauer   block: Add Sed-op...
207
  static int end_opal_session_error(struct opal_dev *dev);
0af2648ec   David Kozub   block: sed-opal: ...
208
  static int opal_discovery0_step(struct opal_dev *dev);
455a7b238   Scott Bauer   block: Add Sed-op...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  
  struct opal_suspend_data {
  	struct opal_lock_unlock unlk;
  	u8 lr;
  	struct list_head node;
  };
  
  /*
   * Derived from:
   * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
   * Section: 5.1.5 Method Status Codes
   */
  static const char * const opal_errors[] = {
  	"Success",
  	"Not Authorized",
  	"Unknown Error",
  	"SP Busy",
  	"SP Failed",
  	"SP Disabled",
  	"SP Frozen",
  	"No Sessions Available",
  	"Uniqueness Conflict",
  	"Insufficient Space",
  	"Insufficient Rows",
  	"Invalid Function",
  	"Invalid Parameter",
  	"Invalid Reference",
  	"Unknown Error",
  	"TPER Malfunction",
  	"Transaction Failure",
  	"Response Overflow",
  	"Authority Locked Out",
  };
  
  static const char *opal_error_to_human(int error)
  {
  	if (error == 0x3f)
  		return "Failed";
  
  	if (error >= ARRAY_SIZE(opal_errors) || error < 0)
  		return "Unknown Error";
  
  	return opal_errors[error];
  }
  
  static void print_buffer(const u8 *ptr, u32 length)
  {
  #ifdef DEBUG
  	print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
  	pr_debug("
  ");
  #endif
  }
  
  static bool check_tper(const void *data)
  {
  	const struct d0_tper_features *tper = data;
  	u8 flags = tper->supported_features;
  
  	if (!(flags & TPER_SYNC_SUPPORTED)) {
591c59d18   Scott Bauer   block: sed-opal: ...
269
270
271
  		pr_debug("TPer sync not supported. flags = %d
  ",
  			 tper->supported_features);
455a7b238   Scott Bauer   block: Add Sed-op...
272
273
274
275
276
  		return false;
  	}
  
  	return true;
  }
dbec491b1   Scott Bauer   block: sed-opal: ...
277
278
279
280
281
282
283
  static bool check_mbrenabled(const void *data)
  {
  	const struct d0_locking_features *lfeat = data;
  	u8 sup_feat = lfeat->supported_features;
  
  	return !!(sup_feat & MBR_ENABLED_MASK);
  }
455a7b238   Scott Bauer   block: Add Sed-op...
284
285
286
287
288
289
  static bool check_sum(const void *data)
  {
  	const struct d0_single_user_mode *sum = data;
  	u32 nlo = be32_to_cpu(sum->num_locking_objects);
  
  	if (nlo == 0) {
591c59d18   Scott Bauer   block: sed-opal: ...
290
291
  		pr_debug("Need at least one locking object.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  		return false;
  	}
  
  	pr_debug("Number of locking objects: %d
  ", nlo);
  
  	return true;
  }
  
  static u16 get_comid_v100(const void *data)
  {
  	const struct d0_opal_v100 *v100 = data;
  
  	return be16_to_cpu(v100->baseComID);
  }
  
  static u16 get_comid_v200(const void *data)
  {
  	const struct d0_opal_v200 *v200 = data;
  
  	return be16_to_cpu(v200->baseComID);
  }
  
  static int opal_send_cmd(struct opal_dev *dev)
  {
4f1244c82   Christoph Hellwig   block/sed-opal: a...
317
  	return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
455a7b238   Scott Bauer   block: Add Sed-op...
318
319
320
321
322
323
  			      dev->cmd, IO_BUFFER_LENGTH,
  			      true);
  }
  
  static int opal_recv_cmd(struct opal_dev *dev)
  {
4f1244c82   Christoph Hellwig   block/sed-opal: a...
324
  	return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
455a7b238   Scott Bauer   block: Add Sed-op...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  			      dev->resp, IO_BUFFER_LENGTH,
  			      false);
  }
  
  static int opal_recv_check(struct opal_dev *dev)
  {
  	size_t buflen = IO_BUFFER_LENGTH;
  	void *buffer = dev->resp;
  	struct opal_header *hdr = buffer;
  	int ret;
  
  	do {
  		pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d
  ",
  			 hdr->cp.outstandingData,
  			 hdr->cp.minTransfer);
  
  		if (hdr->cp.outstandingData == 0 ||
  		    hdr->cp.minTransfer != 0)
  			return 0;
  
  		memset(buffer, 0, buflen);
  		ret = opal_recv_cmd(dev);
  	} while (!ret);
  
  	return ret;
  }
  
  static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
  {
  	int ret;
  
  	ret = opal_send_cmd(dev);
  	if (ret)
  		return ret;
  	ret = opal_recv_cmd(dev);
  	if (ret)
  		return ret;
  	ret = opal_recv_check(dev);
  	if (ret)
  		return ret;
  	return cont(dev);
  }
  
  static void check_geometry(struct opal_dev *dev, const void *data)
  {
  	const struct d0_geometry_features *geo = data;
a9eb49c96   Randy Dunlap   block: sed-opal: ...
372
373
  	dev->align = be64_to_cpu(geo->alignment_granularity);
  	dev->lowest_lba = be64_to_cpu(geo->lowest_aligned_lba);
455a7b238   Scott Bauer   block: Add Sed-op...
374
  }
0af2648ec   David Kozub   block: sed-opal: ...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  static int execute_step(struct opal_dev *dev,
  			const struct opal_step *step, size_t stepIndex)
  {
  	int error = step->fn(dev, step->data);
  
  	if (error) {
  		pr_debug("Step %zu (%pS) failed with error %d: %s
  ",
  			 stepIndex, step->fn, error,
  			 opal_error_to_human(error));
  	}
  
  	return error;
  }
a80f36cc6   David Kozub   block: sed-opal: ...
389
390
  static int execute_steps(struct opal_dev *dev,
  			 const struct opal_step *steps, size_t n_steps)
455a7b238   Scott Bauer   block: Add Sed-op...
391
  {
0af2648ec   David Kozub   block: sed-opal: ...
392
393
  	size_t state = 0;
  	int error;
455a7b238   Scott Bauer   block: Add Sed-op...
394

0af2648ec   David Kozub   block: sed-opal: ...
395
396
397
398
  	/* first do a discovery0 */
  	error = opal_discovery0_step(dev);
  	if (error)
  		return error;
455a7b238   Scott Bauer   block: Add Sed-op...
399

0af2648ec   David Kozub   block: sed-opal: ...
400
401
  	for (state = 0; state < n_steps; state++) {
  		error = execute_step(dev, &steps[state], state);
3db87236c   David Kozub   block: sed-opal: ...
402
403
404
  		if (error)
  			goto out_error;
  	}
2d19020b0   Scott Bauer   block/sed-opal: P...
405

3db87236c   David Kozub   block: sed-opal: ...
406
407
408
409
  	return 0;
  
  out_error:
  	/*
0af2648ec   David Kozub   block: sed-opal: ...
410
411
412
413
414
415
  	 * For each OPAL command the first step in steps starts some sort of
  	 * session. If an error occurred in the initial discovery0 or if an
  	 * error occurred in the first step (and thus stopping the loop with
  	 * state == 0) then there was an error before or during the attempt to
  	 * start a session. Therefore we shouldn't attempt to terminate a
  	 * session, as one has not yet been created.
3db87236c   David Kozub   block: sed-opal: ...
416
  	 */
0af2648ec   David Kozub   block: sed-opal: ...
417
  	if (state > 0)
3db87236c   David Kozub   block: sed-opal: ...
418
  		end_opal_session_error(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
419
420
421
422
423
424
425
426
427
428
  
  	return error;
  }
  
  static int opal_discovery0_end(struct opal_dev *dev)
  {
  	bool found_com_id = false, supported = true, single_user = false;
  	const struct d0_header *hdr = (struct d0_header *)dev->resp;
  	const u8 *epos = dev->resp, *cpos = dev->resp;
  	u16 comid = 0;
77039b963   Jon Derrick   block/sed: Check ...
429
  	u32 hlen = be32_to_cpu(hdr->length);
455a7b238   Scott Bauer   block: Add Sed-op...
430

77039b963   Jon Derrick   block/sed: Check ...
431
  	print_buffer(dev->resp, hlen);
dbec491b1   Scott Bauer   block: sed-opal: ...
432
  	dev->mbr_enabled = false;
455a7b238   Scott Bauer   block: Add Sed-op...
433

77039b963   Jon Derrick   block/sed: Check ...
434
  	if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
591c59d18   Scott Bauer   block: sed-opal: ...
435
436
437
  		pr_debug("Discovery length overflows buffer (%zu+%u)/%u
  ",
  			 sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
77039b963   Jon Derrick   block/sed: Check ...
438
439
440
441
  		return -EFAULT;
  	}
  
  	epos += hlen; /* end of buffer */
455a7b238   Scott Bauer   block: Add Sed-op...
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
  	cpos += sizeof(*hdr); /* current position on buffer */
  
  	while (cpos < epos && supported) {
  		const struct d0_features *body =
  			(const struct d0_features *)cpos;
  
  		switch (be16_to_cpu(body->code)) {
  		case FC_TPER:
  			supported = check_tper(body->features);
  			break;
  		case FC_SINGLEUSER:
  			single_user = check_sum(body->features);
  			break;
  		case FC_GEOMETRY:
  			check_geometry(dev, body);
  			break;
  		case FC_LOCKING:
dbec491b1   Scott Bauer   block: sed-opal: ...
459
460
  			dev->mbr_enabled = check_mbrenabled(body->features);
  			break;
455a7b238   Scott Bauer   block: Add Sed-op...
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
  		case FC_ENTERPRISE:
  		case FC_DATASTORE:
  			/* some ignored properties */
  			pr_debug("Found OPAL feature description: %d
  ",
  				 be16_to_cpu(body->code));
  			break;
  		case FC_OPALV100:
  			comid = get_comid_v100(body->features);
  			found_com_id = true;
  			break;
  		case FC_OPALV200:
  			comid = get_comid_v200(body->features);
  			found_com_id = true;
  			break;
  		case 0xbfff ... 0xffff:
  			/* vendor specific, just ignore */
  			break;
  		default:
  			pr_debug("OPAL Unknown feature: %d
  ",
  				 be16_to_cpu(body->code));
  
  		}
  		cpos += body->length + 4;
  	}
  
  	if (!supported) {
f5b37b7c2   Christoph Hellwig   block/sed-opal: t...
489
490
  		pr_debug("This device is not Opal enabled. Not Supported!
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
491
492
493
494
  		return -EOPNOTSUPP;
  	}
  
  	if (!single_user)
f5b37b7c2   Christoph Hellwig   block/sed-opal: t...
495
496
  		pr_debug("Device doesn't support single user mode
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
497
498
499
  
  
  	if (!found_com_id) {
f5b37b7c2   Christoph Hellwig   block/sed-opal: t...
500
501
  		pr_debug("Could not find OPAL comid for device. Returning early
  ");
ed7158bae   Ingo Molnar   treewide/trivial:...
502
  		return -EOPNOTSUPP;
455a7b238   Scott Bauer   block: Add Sed-op...
503
504
505
506
507
508
  	}
  
  	dev->comid = comid;
  
  	return 0;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
509
  static int opal_discovery0(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
510
511
512
513
514
515
516
517
  {
  	int ret;
  
  	memset(dev->resp, 0, IO_BUFFER_LENGTH);
  	dev->comid = OPAL_DISCOVERY_COMID;
  	ret = opal_recv_cmd(dev);
  	if (ret)
  		return ret;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
518

455a7b238   Scott Bauer   block: Add Sed-op...
519
520
  	return opal_discovery0_end(dev);
  }
0af2648ec   David Kozub   block: sed-opal: ...
521
522
523
524
525
  static int opal_discovery0_step(struct opal_dev *dev)
  {
  	const struct opal_step discovery0_step = {
  		opal_discovery0,
  	};
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
526

0af2648ec   David Kozub   block: sed-opal: ...
527
528
  	return execute_step(dev, &discovery0_step, 0);
  }
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
529
530
531
532
  static size_t remaining_size(struct opal_dev *cmd)
  {
  	return IO_BUFFER_LENGTH - cmd->pos;
  }
e2821a50b   Jonas Rabenstein   block: sed-opal: ...
533
  static bool can_add(int *err, struct opal_dev *cmd, size_t len)
455a7b238   Scott Bauer   block: Add Sed-op...
534
535
  {
  	if (*err)
e2821a50b   Jonas Rabenstein   block: sed-opal: ...
536
  		return false;
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
537
  	if (remaining_size(cmd) < len) {
e2821a50b   Jonas Rabenstein   block: sed-opal: ...
538
539
  		pr_debug("Error adding %zu bytes: end of buffer.
  ", len);
455a7b238   Scott Bauer   block: Add Sed-op...
540
  		*err = -ERANGE;
e2821a50b   Jonas Rabenstein   block: sed-opal: ...
541
  		return false;
455a7b238   Scott Bauer   block: Add Sed-op...
542
  	}
e2821a50b   Jonas Rabenstein   block: sed-opal: ...
543
544
545
546
547
548
549
550
  
  	return true;
  }
  
  static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
  {
  	if (!can_add(err, cmd, 1))
  		return;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
551

455a7b238   Scott Bauer   block: Add Sed-op...
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  	cmd->cmd[cmd->pos++] = tok;
  }
  
  static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
  				  bool has_sign, int len)
  {
  	u8 atom;
  	int err = 0;
  
  	atom = SHORT_ATOM_ID;
  	atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
  	atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
  	atom |= len & SHORT_ATOM_LEN_MASK;
  
  	add_token_u8(&err, cmd, atom);
  }
  
  static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
  				   bool has_sign, int len)
  {
  	u8 header0;
  
  	header0 = MEDIUM_ATOM_ID;
  	header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
  	header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
  	header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
578

455a7b238   Scott Bauer   block: Add Sed-op...
579
580
581
582
583
584
  	cmd->cmd[cmd->pos++] = header0;
  	cmd->cmd[cmd->pos++] = len;
  }
  
  static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
  {
455a7b238   Scott Bauer   block: Add Sed-op...
585
586
  	size_t len;
  	int msb;
455a7b238   Scott Bauer   block: Add Sed-op...
587
588
589
590
591
  
  	if (!(number & ~TINY_ATOM_DATA_MASK)) {
  		add_token_u8(err, cmd, number);
  		return;
  	}
5f990d316   Jonas Rabenstein   block: sed-opal: ...
592
593
  	msb = fls64(number);
  	len = DIV_ROUND_UP(msb, 8);
455a7b238   Scott Bauer   block: Add Sed-op...
594

e2821a50b   Jonas Rabenstein   block: sed-opal: ...
595
  	if (!can_add(err, cmd, len + 1)) {
591c59d18   Scott Bauer   block: sed-opal: ...
596
597
  		pr_debug("Error adding u64: end of buffer.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
598
599
600
  		return;
  	}
  	add_short_atom_header(cmd, false, false, len);
5f990d316   Jonas Rabenstein   block: sed-opal: ...
601
602
  	while (len--)
  		add_token_u8(err, cmd, number >> (len * 8));
455a7b238   Scott Bauer   block: Add Sed-op...
603
  }
285599590   Jonas Rabenstein   block: sed-opal: ...
604
  static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len)
455a7b238   Scott Bauer   block: Add Sed-op...
605
606
607
  {
  	size_t header_len = 1;
  	bool is_short_atom = true;
455a7b238   Scott Bauer   block: Add Sed-op...
608
609
610
611
  	if (len & ~SHORT_ATOM_LEN_MASK) {
  		header_len = 2;
  		is_short_atom = false;
  	}
e2821a50b   Jonas Rabenstein   block: sed-opal: ...
612
  	if (!can_add(err, cmd, header_len + len)) {
591c59d18   Scott Bauer   block: sed-opal: ...
613
614
  		pr_debug("Error adding bytestring: end of buffer.
  ");
285599590   Jonas Rabenstein   block: sed-opal: ...
615
  		return NULL;
455a7b238   Scott Bauer   block: Add Sed-op...
616
617
618
619
620
621
  	}
  
  	if (is_short_atom)
  		add_short_atom_header(cmd, true, false, len);
  	else
  		add_medium_atom_header(cmd, true, false, len);
285599590   Jonas Rabenstein   block: sed-opal: ...
622
623
  	return &cmd->cmd[cmd->pos];
  }
455a7b238   Scott Bauer   block: Add Sed-op...
624

285599590   Jonas Rabenstein   block: sed-opal: ...
625
626
627
628
629
630
631
632
633
634
  static void add_token_bytestring(int *err, struct opal_dev *cmd,
  				 const u8 *bytestring, size_t len)
  {
  	u8 *start;
  
  	start = add_bytestring_header(err, cmd, len);
  	if (!start)
  		return;
  	memcpy(start, bytestring, len);
  	cmd->pos += len;
455a7b238   Scott Bauer   block: Add Sed-op...
635
636
637
638
639
  }
  
  static int build_locking_range(u8 *buffer, size_t length, u8 lr)
  {
  	if (length > OPAL_UID_LENGTH) {
591c59d18   Scott Bauer   block: sed-opal: ...
640
641
  		pr_debug("Can't build locking range. Length OOB
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
642
643
644
645
646
647
648
  		return -ERANGE;
  	}
  
  	memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
  
  	if (lr == 0)
  		return 0;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
649

455a7b238   Scott Bauer   block: Add Sed-op...
650
651
652
653
654
655
656
657
658
  	buffer[5] = LOCKING_RANGE_NON_GLOBAL;
  	buffer[7] = lr;
  
  	return 0;
  }
  
  static int build_locking_user(u8 *buffer, size_t length, u8 lr)
  {
  	if (length > OPAL_UID_LENGTH) {
1e815b33c   David Kozub   block: sed-opal: ...
659
660
  		pr_debug("Can't build locking range user. Length OOB
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
  		return -ERANGE;
  	}
  
  	memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
  
  	buffer[7] = lr + 1;
  
  	return 0;
  }
  
  static void set_comid(struct opal_dev *cmd, u16 comid)
  {
  	struct opal_header *hdr = (struct opal_header *)cmd->cmd;
  
  	hdr->cp.extendedComID[0] = comid >> 8;
  	hdr->cp.extendedComID[1] = comid;
  	hdr->cp.extendedComID[2] = 0;
  	hdr->cp.extendedComID[3] = 0;
  }
  
  static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
  {
  	struct opal_header *hdr;
  	int err = 0;
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
685
686
687
688
689
  	/*
  	 * Close the parameter list opened from cmd_start.
  	 * The number of bytes added must be equal to
  	 * CMD_FINALIZE_BYTES_NEEDED.
  	 */
78d584ca3   David Kozub   block: sed-opal: ...
690
  	add_token_u8(&err, cmd, OPAL_ENDLIST);
455a7b238   Scott Bauer   block: Add Sed-op...
691
692
693
694
695
696
697
698
  	add_token_u8(&err, cmd, OPAL_ENDOFDATA);
  	add_token_u8(&err, cmd, OPAL_STARTLIST);
  	add_token_u8(&err, cmd, 0);
  	add_token_u8(&err, cmd, 0);
  	add_token_u8(&err, cmd, 0);
  	add_token_u8(&err, cmd, OPAL_ENDLIST);
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
699
700
  		pr_debug("Error finalizing command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
701
702
703
704
705
706
707
708
709
710
711
  		return -EFAULT;
  	}
  
  	hdr = (struct opal_header *) cmd->cmd;
  
  	hdr->pkt.tsn = cpu_to_be32(tsn);
  	hdr->pkt.hsn = cpu_to_be32(hsn);
  
  	hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
  	while (cmd->pos % 4) {
  		if (cmd->pos >= IO_BUFFER_LENGTH) {
591c59d18   Scott Bauer   block: sed-opal: ...
712
713
  			pr_debug("Error: Buffer overrun
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
714
715
716
717
718
719
720
721
722
723
  			return -ERANGE;
  		}
  		cmd->cmd[cmd->pos++] = 0;
  	}
  	hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
  				      sizeof(hdr->pkt));
  	hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
  
  	return 0;
  }
cccb92417   Jon Derrick   block/sed: Add he...
724
725
726
  static const struct opal_resp_tok *response_get_token(
  				const struct parsed_resp *resp,
  				int n)
455a7b238   Scott Bauer   block: Add Sed-op...
727
728
  {
  	const struct opal_resp_tok *tok;
7d9b62ae2   David Kozub   block: sed-opal: ...
729
730
731
732
733
  	if (!resp) {
  		pr_debug("Response is NULL
  ");
  		return ERR_PTR(-EINVAL);
  	}
455a7b238   Scott Bauer   block: Add Sed-op...
734
  	if (n >= resp->num) {
591c59d18   Scott Bauer   block: sed-opal: ...
735
736
737
  		pr_debug("Token number doesn't exist: %d, resp: %d
  ",
  			 n, resp->num);
cccb92417   Jon Derrick   block/sed: Add he...
738
  		return ERR_PTR(-EINVAL);
455a7b238   Scott Bauer   block: Add Sed-op...
739
740
741
742
  	}
  
  	tok = &resp->toks[n];
  	if (tok->len == 0) {
591c59d18   Scott Bauer   block: sed-opal: ...
743
744
  		pr_debug("Token length must be non-zero
  ");
cccb92417   Jon Derrick   block/sed: Add he...
745
  		return ERR_PTR(-EINVAL);
455a7b238   Scott Bauer   block: Add Sed-op...
746
  	}
cccb92417   Jon Derrick   block/sed: Add he...
747
  	return tok;
455a7b238   Scott Bauer   block: Add Sed-op...
748
  }
aedb6e241   Jon Derrick   block/sed: Use ss...
749
750
  static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
  				   const u8 *pos)
455a7b238   Scott Bauer   block: Add Sed-op...
751
752
753
754
755
756
757
758
759
760
761
762
763
764
  {
  	tok->pos = pos;
  	tok->len = 1;
  	tok->width = OPAL_WIDTH_TINY;
  
  	if (pos[0] & TINY_ATOM_SIGNED) {
  		tok->type = OPAL_DTA_TOKENID_SINT;
  	} else {
  		tok->type = OPAL_DTA_TOKENID_UINT;
  		tok->stored.u = pos[0] & 0x3f;
  	}
  
  	return tok->len;
  }
aedb6e241   Jon Derrick   block/sed: Use ss...
765
766
  static ssize_t response_parse_short(struct opal_resp_tok *tok,
  				    const u8 *pos)
455a7b238   Scott Bauer   block: Add Sed-op...
767
768
769
770
771
772
773
774
775
776
777
  {
  	tok->pos = pos;
  	tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
  	tok->width = OPAL_WIDTH_SHORT;
  
  	if (pos[0] & SHORT_ATOM_BYTESTRING) {
  		tok->type = OPAL_DTA_TOKENID_BYTESTRING;
  	} else if (pos[0] & SHORT_ATOM_SIGNED) {
  		tok->type = OPAL_DTA_TOKENID_SINT;
  	} else {
  		u64 u_integer = 0;
aedb6e241   Jon Derrick   block/sed: Use ss...
778
  		ssize_t i, b = 0;
455a7b238   Scott Bauer   block: Add Sed-op...
779
780
781
  
  		tok->type = OPAL_DTA_TOKENID_UINT;
  		if (tok->len > 9) {
591c59d18   Scott Bauer   block: sed-opal: ...
782
783
  			pr_debug("uint64 with more than 8 bytes
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
784
785
786
787
788
789
790
791
792
793
794
  			return -EINVAL;
  		}
  		for (i = tok->len - 1; i > 0; i--) {
  			u_integer |= ((u64)pos[i] << (8 * b));
  			b++;
  		}
  		tok->stored.u = u_integer;
  	}
  
  	return tok->len;
  }
aedb6e241   Jon Derrick   block/sed: Use ss...
795
796
  static ssize_t response_parse_medium(struct opal_resp_tok *tok,
  				     const u8 *pos)
455a7b238   Scott Bauer   block: Add Sed-op...
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  {
  	tok->pos = pos;
  	tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
  	tok->width = OPAL_WIDTH_MEDIUM;
  
  	if (pos[0] & MEDIUM_ATOM_BYTESTRING)
  		tok->type = OPAL_DTA_TOKENID_BYTESTRING;
  	else if (pos[0] & MEDIUM_ATOM_SIGNED)
  		tok->type = OPAL_DTA_TOKENID_SINT;
  	else
  		tok->type = OPAL_DTA_TOKENID_UINT;
  
  	return tok->len;
  }
aedb6e241   Jon Derrick   block/sed: Use ss...
811
812
  static ssize_t response_parse_long(struct opal_resp_tok *tok,
  				   const u8 *pos)
455a7b238   Scott Bauer   block: Add Sed-op...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  {
  	tok->pos = pos;
  	tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
  	tok->width = OPAL_WIDTH_LONG;
  
  	if (pos[0] & LONG_ATOM_BYTESTRING)
  		tok->type = OPAL_DTA_TOKENID_BYTESTRING;
  	else if (pos[0] & LONG_ATOM_SIGNED)
  		tok->type = OPAL_DTA_TOKENID_SINT;
  	else
  		tok->type = OPAL_DTA_TOKENID_UINT;
  
  	return tok->len;
  }
aedb6e241   Jon Derrick   block/sed: Use ss...
827
828
  static ssize_t response_parse_token(struct opal_resp_tok *tok,
  				    const u8 *pos)
455a7b238   Scott Bauer   block: Add Sed-op...
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
  {
  	tok->pos = pos;
  	tok->len = 1;
  	tok->type = OPAL_DTA_TOKENID_TOKEN;
  	tok->width = OPAL_WIDTH_TOKEN;
  
  	return tok->len;
  }
  
  static int response_parse(const u8 *buf, size_t length,
  			  struct parsed_resp *resp)
  {
  	const struct opal_header *hdr;
  	struct opal_resp_tok *iter;
  	int num_entries = 0;
  	int total;
aedb6e241   Jon Derrick   block/sed: Use ss...
845
  	ssize_t token_length;
455a7b238   Scott Bauer   block: Add Sed-op...
846
  	const u8 *pos;
77039b963   Jon Derrick   block/sed: Check ...
847
  	u32 clen, plen, slen;
455a7b238   Scott Bauer   block: Add Sed-op...
848
849
850
851
852
853
854
855
856
857
  
  	if (!buf)
  		return -EFAULT;
  
  	if (!resp)
  		return -EFAULT;
  
  	hdr = (struct opal_header *)buf;
  	pos = buf;
  	pos += sizeof(*hdr);
77039b963   Jon Derrick   block/sed: Check ...
858
859
860
861
862
863
864
865
866
  	clen = be32_to_cpu(hdr->cp.length);
  	plen = be32_to_cpu(hdr->pkt.length);
  	slen = be32_to_cpu(hdr->subpkt.length);
  	pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u
  ",
  		 clen, plen, slen);
  
  	if (clen == 0 || plen == 0 || slen == 0 ||
  	    slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
591c59d18   Scott Bauer   block: sed-opal: ...
867
868
869
  		pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u
  ",
  			 clen, plen, slen);
455a7b238   Scott Bauer   block: Add Sed-op...
870
871
872
873
874
875
876
877
  		print_buffer(pos, sizeof(*hdr));
  		return -EINVAL;
  	}
  
  	if (pos > buf + length)
  		return -EFAULT;
  
  	iter = resp->toks;
77039b963   Jon Derrick   block/sed: Check ...
878
  	total = slen;
455a7b238   Scott Bauer   block: Add Sed-op...
879
880
881
882
883
884
885
886
887
888
889
890
  	print_buffer(pos, total);
  	while (total > 0) {
  		if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
  			token_length = response_parse_tiny(iter, pos);
  		else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
  			token_length = response_parse_short(iter, pos);
  		else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
  			token_length = response_parse_medium(iter, pos);
  		else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
  			token_length = response_parse_long(iter, pos);
  		else /* TOKEN */
  			token_length = response_parse_token(iter, pos);
aedb6e241   Jon Derrick   block/sed: Use ss...
891
892
  		if (token_length < 0)
  			return token_length;
455a7b238   Scott Bauer   block: Add Sed-op...
893
894
895
896
897
898
  
  		pos += token_length;
  		total -= token_length;
  		iter++;
  		num_entries++;
  	}
455a7b238   Scott Bauer   block: Add Sed-op...
899
900
901
902
903
904
905
906
  	resp->num = num_entries;
  
  	return 0;
  }
  
  static size_t response_get_string(const struct parsed_resp *resp, int n,
  				  const char **store)
  {
d15e1175a   Jonas Rabenstein   block: sed-opal: ...
907
  	u8 skip;
b68f09ecd   David Kozub   block: sed-opal: ...
908
  	const struct opal_resp_tok *tok;
d15e1175a   Jonas Rabenstein   block: sed-opal: ...
909

455a7b238   Scott Bauer   block: Add Sed-op...
910
  	*store = NULL;
b68f09ecd   David Kozub   block: sed-opal: ...
911
912
  	tok = response_get_token(resp, n);
  	if (IS_ERR(tok))
455a7b238   Scott Bauer   block: Add Sed-op...
913
  		return 0;
455a7b238   Scott Bauer   block: Add Sed-op...
914

b68f09ecd   David Kozub   block: sed-opal: ...
915
  	if (tok->type != OPAL_DTA_TOKENID_BYTESTRING) {
591c59d18   Scott Bauer   block: sed-opal: ...
916
917
  		pr_debug("Token is not a byte string!
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
918
919
  		return 0;
  	}
b68f09ecd   David Kozub   block: sed-opal: ...
920
  	switch (tok->width) {
d15e1175a   Jonas Rabenstein   block: sed-opal: ...
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
  	case OPAL_WIDTH_TINY:
  	case OPAL_WIDTH_SHORT:
  		skip = 1;
  		break;
  	case OPAL_WIDTH_MEDIUM:
  		skip = 2;
  		break;
  	case OPAL_WIDTH_LONG:
  		skip = 4;
  		break;
  	default:
  		pr_debug("Token has invalid width!
  ");
  		return 0;
  	}
b68f09ecd   David Kozub   block: sed-opal: ...
936
  	*store = tok->pos + skip;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
937

b68f09ecd   David Kozub   block: sed-opal: ...
938
  	return tok->len - skip;
455a7b238   Scott Bauer   block: Add Sed-op...
939
940
941
942
  }
  
  static u64 response_get_u64(const struct parsed_resp *resp, int n)
  {
b68f09ecd   David Kozub   block: sed-opal: ...
943
  	const struct opal_resp_tok *tok;
455a7b238   Scott Bauer   block: Add Sed-op...
944

b68f09ecd   David Kozub   block: sed-opal: ...
945
946
  	tok = response_get_token(resp, n);
  	if (IS_ERR(tok))
455a7b238   Scott Bauer   block: Add Sed-op...
947
  		return 0;
455a7b238   Scott Bauer   block: Add Sed-op...
948

b68f09ecd   David Kozub   block: sed-opal: ...
949
950
951
  	if (tok->type != OPAL_DTA_TOKENID_UINT) {
  		pr_debug("Token is not unsigned int: %d
  ", tok->type);
455a7b238   Scott Bauer   block: Add Sed-op...
952
953
  		return 0;
  	}
b68f09ecd   David Kozub   block: sed-opal: ...
954
955
956
  	if (tok->width != OPAL_WIDTH_TINY && tok->width != OPAL_WIDTH_SHORT) {
  		pr_debug("Atom is not short or tiny: %d
  ", tok->width);
455a7b238   Scott Bauer   block: Add Sed-op...
957
958
  		return 0;
  	}
b68f09ecd   David Kozub   block: sed-opal: ...
959
  	return tok->stored.u;
455a7b238   Scott Bauer   block: Add Sed-op...
960
  }
cccb92417   Jon Derrick   block/sed: Add he...
961
962
963
964
965
966
967
968
  static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
  {
  	if (IS_ERR(token) ||
  	    token->type != OPAL_DTA_TOKENID_TOKEN ||
  	    token->pos[0] != match)
  		return false;
  	return true;
  }
455a7b238   Scott Bauer   block: Add Sed-op...
969
970
  static u8 response_status(const struct parsed_resp *resp)
  {
cccb92417   Jon Derrick   block/sed: Add he...
971
972
973
974
  	const struct opal_resp_tok *tok;
  
  	tok = response_get_token(resp, 0);
  	if (response_token_matches(tok, OPAL_ENDOFSESSION))
455a7b238   Scott Bauer   block: Add Sed-op...
975
  		return 0;
455a7b238   Scott Bauer   block: Add Sed-op...
976
977
978
  
  	if (resp->num < 5)
  		return DTAERROR_NO_METHOD_STATUS;
cccb92417   Jon Derrick   block/sed: Add he...
979
980
981
982
983
984
  	tok = response_get_token(resp, resp->num - 5);
  	if (!response_token_matches(tok, OPAL_STARTLIST))
  		return DTAERROR_NO_METHOD_STATUS;
  
  	tok = response_get_token(resp, resp->num - 1);
  	if (!response_token_matches(tok, OPAL_ENDLIST))
455a7b238   Scott Bauer   block: Add Sed-op...
985
986
987
988
989
990
991
992
993
994
995
996
997
998
  		return DTAERROR_NO_METHOD_STATUS;
  
  	return response_get_u64(resp, resp->num - 4);
  }
  
  /* Parses and checks for errors */
  static int parse_and_check_status(struct opal_dev *dev)
  {
  	int error;
  
  	print_buffer(dev->cmd, dev->pos);
  
  	error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
  	if (error) {
591c59d18   Scott Bauer   block: sed-opal: ...
999
1000
  		pr_debug("Couldn't parse response.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
  		return error;
  	}
  
  	return response_status(&dev->parsed);
  }
  
  static void clear_opal_cmd(struct opal_dev *dev)
  {
  	dev->pos = sizeof(struct opal_header);
  	memset(dev->cmd, 0, IO_BUFFER_LENGTH);
  }
e8b292245   David Kozub   block: sed-opal: ...
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
  static int cmd_start(struct opal_dev *dev, const u8 *uid, const u8 *method)
  {
  	int err = 0;
  
  	clear_opal_cmd(dev);
  	set_comid(dev, dev->comid);
  
  	add_token_u8(&err, dev, OPAL_CALL);
  	add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
  	add_token_bytestring(&err, dev, method, OPAL_METHOD_LENGTH);
  
  	/*
  	 * Every method call is followed by its parameters enclosed within
  	 * OPAL_STARTLIST and OPAL_ENDLIST tokens. We automatically open the
  	 * parameter list here and close it later in cmd_finalize.
  	 */
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  
  	return err;
  }
455a7b238   Scott Bauer   block: Add Sed-op...
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  static int start_opal_session_cont(struct opal_dev *dev)
  {
  	u32 hsn, tsn;
  	int error = 0;
  
  	error = parse_and_check_status(dev);
  	if (error)
  		return error;
  
  	hsn = response_get_u64(&dev->parsed, 4);
  	tsn = response_get_u64(&dev->parsed, 5);
88d6041d0   Revanth Rajashekar   block: sed-opal: ...
1043
  	if (hsn != GENERIC_HOST_SESSION_NUM || tsn < FIRST_TPER_SESSION_NUM) {
591c59d18   Scott Bauer   block: sed-opal: ...
1044
1045
  		pr_debug("Couldn't authenticate session
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1046
1047
1048
1049
1050
  		return -EPERM;
  	}
  
  	dev->hsn = hsn;
  	dev->tsn = tsn;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1051

455a7b238   Scott Bauer   block: Add Sed-op...
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
  	return 0;
  }
  
  static void add_suspend_info(struct opal_dev *dev,
  			     struct opal_suspend_data *sus)
  {
  	struct opal_suspend_data *iter;
  
  	list_for_each_entry(iter, &dev->unlk_lst, node) {
  		if (iter->lr == sus->lr) {
  			list_del(&iter->node);
  			kfree(iter);
  			break;
  		}
  	}
  	list_add_tail(&sus->node, &dev->unlk_lst);
  }
  
  static int end_session_cont(struct opal_dev *dev)
  {
  	dev->hsn = 0;
  	dev->tsn = 0;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1074

455a7b238   Scott Bauer   block: Add Sed-op...
1075
1076
1077
1078
1079
1080
1081
1082
1083
  	return parse_and_check_status(dev);
  }
  
  static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
  {
  	int ret;
  
  	ret = cmd_finalize(dev, dev->hsn, dev->tsn);
  	if (ret) {
591c59d18   Scott Bauer   block: sed-opal: ...
1084
1085
  		pr_debug("Error finalizing command buffer: %d
  ", ret);
455a7b238   Scott Bauer   block: Add Sed-op...
1086
1087
1088
1089
1090
1091
1092
  		return ret;
  	}
  
  	print_buffer(dev->cmd, dev->pos);
  
  	return opal_send_recv(dev, cont);
  }
3fff234b8   David Kozub   block: sed-opal: ...
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
  /*
   * request @column from table @table on device @dev. On success, the column
   * data will be available in dev->resp->tok[4]
   */
  static int generic_get_column(struct opal_dev *dev, const u8 *table,
  			      u64 column)
  {
  	int err;
  
  	err = cmd_start(dev, table, opalmethod[OPAL_GET]);
  
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_STARTCOLUMN);
  	add_token_u64(&err, dev, column);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_ENDCOLUMN);
  	add_token_u64(&err, dev, column);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  
  	if (err)
  		return err;
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
ff91064ea   Jonas Rabenstein   block: sed-opal: ...
1123
1124
1125
1126
1127
  /*
   * see TCG SAS 5.3.2.3 for a description of the available columns
   *
   * the result is provided in dev->resp->tok[4]
   */
3495ea1b5   Revanth Rajashekar   block: sed-opal: ...
1128
  static int generic_get_table_info(struct opal_dev *dev, const u8 *table_uid,
ff91064ea   Jonas Rabenstein   block: sed-opal: ...
1129
1130
1131
  				  u64 column)
  {
  	u8 uid[OPAL_UID_LENGTH];
3495ea1b5   Revanth Rajashekar   block: sed-opal: ...
1132
  	const unsigned int half = OPAL_UID_LENGTH_HALF;
ff91064ea   Jonas Rabenstein   block: sed-opal: ...
1133
1134
1135
1136
1137
1138
1139
1140
  
  	/* sed-opal UIDs can be split in two halves:
  	 *  first:  actual table index
  	 *  second: relative index in the table
  	 * so we have to get the first half of the OPAL_TABLE_TABLE and use the
  	 * first part of the target table as relative index into that table
  	 */
  	memcpy(uid, opaluid[OPAL_TABLE_TABLE], half);
3495ea1b5   Revanth Rajashekar   block: sed-opal: ...
1141
  	memcpy(uid + half, table_uid, half);
ff91064ea   Jonas Rabenstein   block: sed-opal: ...
1142
1143
1144
  
  	return generic_get_column(dev, uid, column);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1145
  static int gen_key(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1146
  {
455a7b238   Scott Bauer   block: Add Sed-op...
1147
  	u8 uid[OPAL_UID_LENGTH];
e8b292245   David Kozub   block: sed-opal: ...
1148
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1149
1150
  
  	memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
455a7b238   Scott Bauer   block: Add Sed-op...
1151
1152
  	kfree(dev->prev_data);
  	dev->prev_data = NULL;
e8b292245   David Kozub   block: sed-opal: ...
1153
  	err = cmd_start(dev, uid, opalmethod[OPAL_GENKEY]);
455a7b238   Scott Bauer   block: Add Sed-op...
1154
1155
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1156
1157
  		pr_debug("Error building gen key command
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1158
1159
1160
  		return err;
  
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1161

455a7b238   Scott Bauer   block: Add Sed-op...
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
  	return finalize_and_send(dev, parse_and_check_status);
  }
  
  static int get_active_key_cont(struct opal_dev *dev)
  {
  	const char *activekey;
  	size_t keylen;
  	int error = 0;
  
  	error = parse_and_check_status(dev);
  	if (error)
  		return error;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1174

455a7b238   Scott Bauer   block: Add Sed-op...
1175
1176
  	keylen = response_get_string(&dev->parsed, 4, &activekey);
  	if (!activekey) {
591c59d18   Scott Bauer   block: sed-opal: ...
1177
1178
1179
  		pr_debug("%s: Couldn't extract the Activekey from the response
  ",
  			 __func__);
455a7b238   Scott Bauer   block: Add Sed-op...
1180
1181
  		return OPAL_INVAL_PARAM;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1182

455a7b238   Scott Bauer   block: Add Sed-op...
1183
1184
1185
1186
1187
1188
1189
1190
1191
  	dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
  
  	if (!dev->prev_data)
  		return -ENOMEM;
  
  	dev->prev_d_len = keylen;
  
  	return 0;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1192
  static int get_active_key(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1193
1194
  {
  	u8 uid[OPAL_UID_LENGTH];
e8b292245   David Kozub   block: sed-opal: ...
1195
  	int err;
eed64951f   Jon Derrick   block/sed: Embed ...
1196
  	u8 *lr = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1197

455a7b238   Scott Bauer   block: Add Sed-op...
1198
1199
1200
  	err = build_locking_range(uid, sizeof(uid), *lr);
  	if (err)
  		return err;
3fff234b8   David Kozub   block: sed-opal: ...
1201
1202
  	err = generic_get_column(dev, uid, OPAL_ACTIVEKEY);
  	if (err)
455a7b238   Scott Bauer   block: Add Sed-op...
1203
  		return err;
455a7b238   Scott Bauer   block: Add Sed-op...
1204

3fff234b8   David Kozub   block: sed-opal: ...
1205
  	return get_active_key_cont(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
1206
  }
3495ea1b5   Revanth Rajashekar   block: sed-opal: ...
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
  static int generic_table_write_data(struct opal_dev *dev, const u64 data,
  				    u64 offset, u64 size, const u8 *uid)
  {
  	const u8 __user *src = (u8 __user *)(uintptr_t)data;
  	u8 *dst;
  	u64 len;
  	size_t off = 0;
  	int err;
  
  	/* do we fit in the available space? */
  	err = generic_get_table_info(dev, uid, OPAL_TABLE_ROWS);
  	if (err) {
  		pr_debug("Couldn't get the table size
  ");
  		return err;
  	}
  
  	len = response_get_u64(&dev->parsed, 4);
  	if (size > len || offset > len - size) {
  		pr_debug("Does not fit in the table (%llu vs. %llu)
  ",
  			  offset + size, len);
  		return -ENOSPC;
  	}
  
  	/* do the actual transmission(s) */
  	while (off < size) {
  		err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, OPAL_WHERE);
  		add_token_u64(&err, dev, offset + off);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, OPAL_VALUES);
  
  		/*
  		 * The bytestring header is either 1 or 2 bytes, so assume 2.
  		 * There also needs to be enough space to accommodate the
  		 * trailing OPAL_ENDNAME (1 byte) and tokens added by
  		 * cmd_finalize.
  		 */
  		len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
  			  (size_t)(size - off));
  		pr_debug("Write bytes %zu+%llu/%llu
  ", off, len, size);
  
  		dst = add_bytestring_header(&err, dev, len);
  		if (!dst)
  			break;
  
  		if (copy_from_user(dst, src + off, len)) {
  			err = -EFAULT;
  			break;
  		}
  
  		dev->pos += len;
  
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  		if (err)
  			break;
  
  		err = finalize_and_send(dev, parse_and_check_status);
  		if (err)
  			break;
  
  		off += len;
  	}
  
  	return err;
  }
455a7b238   Scott Bauer   block: Add Sed-op...
1278
1279
1280
1281
  static int generic_lr_enable_disable(struct opal_dev *dev,
  				     u8 *uid, bool rle, bool wle,
  				     bool rl, bool wl)
  {
e8b292245   David Kozub   block: sed-opal: ...
1282
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1283

e8b292245   David Kozub   block: sed-opal: ...
1284
  	err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1285

455a7b238   Scott Bauer   block: Add Sed-op...
1286
1287
1288
1289
1290
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1291
  	add_token_u8(&err, dev, OPAL_READLOCKENABLED);
455a7b238   Scott Bauer   block: Add Sed-op...
1292
1293
1294
1295
  	add_token_u8(&err, dev, rle);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1296
  	add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
455a7b238   Scott Bauer   block: Add Sed-op...
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
  	add_token_u8(&err, dev, wle);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_READLOCKED);
  	add_token_u8(&err, dev, rl);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_WRITELOCKED);
  	add_token_u8(&err, dev, wl);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1312

455a7b238   Scott Bauer   block: Add Sed-op...
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
  	return err;
  }
  
  static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
  				   struct opal_user_lr_setup *setup)
  {
  	int err;
  
  	err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
  					0, 0);
  	if (err)
591c59d18   Scott Bauer   block: sed-opal: ...
1324
1325
  		pr_debug("Failed to create enable global lr command
  ");
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1326

455a7b238   Scott Bauer   block: Add Sed-op...
1327
1328
  	return err;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1329
  static int setup_locking_range(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1330
1331
  {
  	u8 uid[OPAL_UID_LENGTH];
eed64951f   Jon Derrick   block/sed: Embed ...
1332
  	struct opal_user_lr_setup *setup = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1333
  	u8 lr;
e8b292245   David Kozub   block: sed-opal: ...
1334
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1335

455a7b238   Scott Bauer   block: Add Sed-op...
1336
1337
1338
1339
1340
1341
1342
1343
  	lr = setup->session.opal_key.lr;
  	err = build_locking_range(uid, sizeof(uid), lr);
  	if (err)
  		return err;
  
  	if (lr == 0)
  		err = enable_global_lr(dev, uid, setup);
  	else {
e8b292245   David Kozub   block: sed-opal: ...
1344
  		err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1345

455a7b238   Scott Bauer   block: Add Sed-op...
1346
1347
1348
1349
1350
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, OPAL_VALUES);
  		add_token_u8(&err, dev, OPAL_STARTLIST);
  
  		add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1351
  		add_token_u8(&err, dev, OPAL_RANGESTART);
455a7b238   Scott Bauer   block: Add Sed-op...
1352
1353
1354
1355
  		add_token_u64(&err, dev, setup->range_start);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  
  		add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1356
  		add_token_u8(&err, dev, OPAL_RANGELENGTH);
455a7b238   Scott Bauer   block: Add Sed-op...
1357
1358
1359
1360
  		add_token_u64(&err, dev, setup->range_length);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  
  		add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1361
  		add_token_u8(&err, dev, OPAL_READLOCKENABLED);
455a7b238   Scott Bauer   block: Add Sed-op...
1362
1363
1364
1365
  		add_token_u64(&err, dev, !!setup->RLE);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  
  		add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1366
  		add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
455a7b238   Scott Bauer   block: Add Sed-op...
1367
1368
1369
1370
1371
  		add_token_u64(&err, dev, !!setup->WLE);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  
  		add_token_u8(&err, dev, OPAL_ENDLIST);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1372
1373
  	}
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1374
1375
  		pr_debug("Error building Setup Locking range command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1376
  		return err;
455a7b238   Scott Bauer   block: Add Sed-op...
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
  
  static int start_generic_opal_session(struct opal_dev *dev,
  				      enum opal_uid auth,
  				      enum opal_uid sp_type,
  				      const char *key,
  				      u8 key_len)
  {
  	u32 hsn;
e8b292245   David Kozub   block: sed-opal: ...
1389
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1390

591c59d18   Scott Bauer   block: sed-opal: ...
1391
  	if (key == NULL && auth != OPAL_ANYBODY_UID)
455a7b238   Scott Bauer   block: Add Sed-op...
1392
  		return OPAL_INVAL_PARAM;
455a7b238   Scott Bauer   block: Add Sed-op...
1393

455a7b238   Scott Bauer   block: Add Sed-op...
1394
  	hsn = GENERIC_HOST_SESSION_NUM;
e8b292245   David Kozub   block: sed-opal: ...
1395
1396
  	err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
  			opalmethod[OPAL_STARTSESSION]);
455a7b238   Scott Bauer   block: Add Sed-op...
1397

455a7b238   Scott Bauer   block: Add Sed-op...
1398
1399
1400
1401
1402
1403
  	add_token_u64(&err, dev, hsn);
  	add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
  	add_token_u8(&err, dev, 1);
  
  	switch (auth) {
  	case OPAL_ANYBODY_UID:
455a7b238   Scott Bauer   block: Add Sed-op...
1404
1405
1406
  		break;
  	case OPAL_ADMIN1_UID:
  	case OPAL_SID_UID:
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
1407
  	case OPAL_PSID_UID:
455a7b238   Scott Bauer   block: Add Sed-op...
1408
1409
1410
1411
1412
1413
1414
1415
1416
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, 0); /* HostChallenge */
  		add_token_bytestring(&err, dev, key, key_len);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, 3); /* HostSignAuth */
  		add_token_bytestring(&err, dev, opaluid[auth],
  				     OPAL_UID_LENGTH);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1417
1418
  		break;
  	default:
591c59d18   Scott Bauer   block: sed-opal: ...
1419
1420
  		pr_debug("Cannot start Admin SP session with auth %d
  ", auth);
455a7b238   Scott Bauer   block: Add Sed-op...
1421
1422
1423
1424
  		return OPAL_INVAL_PARAM;
  	}
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1425
1426
  		pr_debug("Error building start adminsp session command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1427
1428
1429
1430
1431
  		return err;
  	}
  
  	return finalize_and_send(dev, start_opal_session_cont);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1432
  static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1433
1434
1435
1436
  {
  	return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
  					  OPAL_ADMINSP_UID, NULL, 0);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1437
  static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1438
1439
1440
  {
  	int ret;
  	const u8 *key = dev->prev_data;
455a7b238   Scott Bauer   block: Add Sed-op...
1441
1442
  
  	if (!key) {
eed64951f   Jon Derrick   block/sed: Embed ...
1443
  		const struct opal_key *okey = data;
1e815b33c   David Kozub   block: sed-opal: ...
1444

455a7b238   Scott Bauer   block: Add Sed-op...
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
  		ret = start_generic_opal_session(dev, OPAL_SID_UID,
  						 OPAL_ADMINSP_UID,
  						 okey->key,
  						 okey->key_len);
  	} else {
  		ret = start_generic_opal_session(dev, OPAL_SID_UID,
  						 OPAL_ADMINSP_UID,
  						 key, dev->prev_d_len);
  		kfree(key);
  		dev->prev_data = NULL;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1456

455a7b238   Scott Bauer   block: Add Sed-op...
1457
1458
  	return ret;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1459
  static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1460
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1461
  	struct opal_key *key = data;
1e815b33c   David Kozub   block: sed-opal: ...
1462

455a7b238   Scott Bauer   block: Add Sed-op...
1463
1464
1465
1466
  	return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
  					  OPAL_LOCKINGSP_UID,
  					  key->key, key->key_len);
  }
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
1467
1468
1469
1470
1471
1472
1473
1474
1475
  static int start_PSID_opal_session(struct opal_dev *dev, void *data)
  {
  	const struct opal_key *okey = data;
  
  	return start_generic_opal_session(dev, OPAL_PSID_UID,
  					  OPAL_ADMINSP_UID,
  					  okey->key,
  					  okey->key_len);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1476
  static int start_auth_opal_session(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1477
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1478
  	struct opal_session_info *session = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1479
  	u8 lk_ul_user[OPAL_UID_LENGTH];
eed64951f   Jon Derrick   block/sed: Embed ...
1480
  	size_t keylen = session->opal_key.key_len;
455a7b238   Scott Bauer   block: Add Sed-op...
1481
  	int err = 0;
455a7b238   Scott Bauer   block: Add Sed-op...
1482
1483
  	u8 *key = session->opal_key.key;
  	u32 hsn = GENERIC_HOST_SESSION_NUM;
e8b292245   David Kozub   block: sed-opal: ...
1484
  	if (session->sum)
455a7b238   Scott Bauer   block: Add Sed-op...
1485
1486
  		err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
  					 session->opal_key.lr);
e8b292245   David Kozub   block: sed-opal: ...
1487
  	else if (session->who != OPAL_ADMIN1 && !session->sum)
455a7b238   Scott Bauer   block: Add Sed-op...
1488
1489
  		err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
  					 session->who - 1);
e8b292245   David Kozub   block: sed-opal: ...
1490
  	else
455a7b238   Scott Bauer   block: Add Sed-op...
1491
  		memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
e8b292245   David Kozub   block: sed-opal: ...
1492
1493
1494
1495
1496
  	if (err)
  		return err;
  
  	err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
  			opalmethod[OPAL_STARTSESSION]);
455a7b238   Scott Bauer   block: Add Sed-op...
1497

455a7b238   Scott Bauer   block: Add Sed-op...
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
  	add_token_u64(&err, dev, hsn);
  	add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
  			     OPAL_UID_LENGTH);
  	add_token_u8(&err, dev, 1);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, 0);
  	add_token_bytestring(&err, dev, key, keylen);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, 3);
  	add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1510
1511
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1512
1513
  		pr_debug("Error building STARTSESSION command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1514
1515
1516
1517
1518
  		return err;
  	}
  
  	return finalize_and_send(dev, start_opal_session_cont);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1519
  static int revert_tper(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1520
  {
e8b292245   David Kozub   block: sed-opal: ...
1521
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1522

e8b292245   David Kozub   block: sed-opal: ...
1523
1524
  	err = cmd_start(dev, opaluid[OPAL_ADMINSP_UID],
  			opalmethod[OPAL_REVERT]);
455a7b238   Scott Bauer   block: Add Sed-op...
1525
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1526
1527
  		pr_debug("Error building REVERT TPER command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1528
1529
1530
1531
1532
  		return err;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1533
  static int internal_activate_user(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1534
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1535
  	struct opal_session_info *session = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1536
  	u8 uid[OPAL_UID_LENGTH];
e8b292245   David Kozub   block: sed-opal: ...
1537
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1538
1539
1540
  
  	memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
  	uid[7] = session->who;
e8b292245   David Kozub   block: sed-opal: ...
1541
  	err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1542
1543
1544
1545
1546
1547
1548
1549
1550
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, 5); /* Enabled */
  	add_token_u8(&err, dev, OPAL_TRUE);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1551
1552
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1553
1554
  		pr_debug("Error building Activate UserN command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1555
1556
1557
1558
1559
  		return err;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1560
  static int erase_locking_range(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1561
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1562
  	struct opal_session_info *session = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1563
  	u8 uid[OPAL_UID_LENGTH];
e8b292245   David Kozub   block: sed-opal: ...
1564
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1565
1566
1567
  
  	if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
  		return -ERANGE;
e8b292245   David Kozub   block: sed-opal: ...
1568
  	err = cmd_start(dev, uid, opalmethod[OPAL_ERASE]);
455a7b238   Scott Bauer   block: Add Sed-op...
1569
1570
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1571
1572
  		pr_debug("Error building Erase Locking Range Command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1573
1574
  		return err;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1575

455a7b238   Scott Bauer   block: Add Sed-op...
1576
1577
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1578
  static int set_mbr_done(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1579
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1580
  	u8 *mbr_done_tf = data;
e8b292245   David Kozub   block: sed-opal: ...
1581
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1582

e8b292245   David Kozub   block: sed-opal: ...
1583
1584
  	err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
  			opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1585

455a7b238   Scott Bauer   block: Add Sed-op...
1586
1587
1588
1589
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1590
  	add_token_u8(&err, dev, OPAL_MBRDONE);
eed64951f   Jon Derrick   block/sed: Embed ...
1591
  	add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
455a7b238   Scott Bauer   block: Add Sed-op...
1592
1593
1594
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1595
1596
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1597
1598
  		pr_debug("Error Building set MBR Done command
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1599
1600
1601
1602
1603
  		return err;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1604
  static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1605
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1606
  	u8 *mbr_en_dis = data;
e8b292245   David Kozub   block: sed-opal: ...
1607
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1608

e8b292245   David Kozub   block: sed-opal: ...
1609
1610
  	err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
  			opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1611

455a7b238   Scott Bauer   block: Add Sed-op...
1612
1613
1614
1615
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1616
  	add_token_u8(&err, dev, OPAL_MBRENABLE);
eed64951f   Jon Derrick   block/sed: Embed ...
1617
  	add_token_u8(&err, dev, *mbr_en_dis);
455a7b238   Scott Bauer   block: Add Sed-op...
1618
1619
1620
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1621
1622
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1623
1624
  		pr_debug("Error Building set MBR done command
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1625
1626
1627
1628
1629
  		return err;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
1630
1631
1632
  static int write_shadow_mbr(struct opal_dev *dev, void *data)
  {
  	struct opal_shadow_mbr *shadow = data;
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
1633

3495ea1b5   Revanth Rajashekar   block: sed-opal: ...
1634
1635
  	return generic_table_write_data(dev, shadow->data, shadow->offset,
  					shadow->size, opaluid[OPAL_MBR]);
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
1636
  }
455a7b238   Scott Bauer   block: Add Sed-op...
1637
1638
1639
  static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
  			  struct opal_dev *dev)
  {
e8b292245   David Kozub   block: sed-opal: ...
1640
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1641

e8b292245   David Kozub   block: sed-opal: ...
1642
  	err = cmd_start(dev, cpin_uid, opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1643

455a7b238   Scott Bauer   block: Add Sed-op...
1644
1645
1646
1647
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
372be4084   David Kozub   block: sed-opal: ...
1648
  	add_token_u8(&err, dev, OPAL_PIN);
455a7b238   Scott Bauer   block: Add Sed-op...
1649
1650
1651
1652
  	add_token_bytestring(&err, dev, key, key_len);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1653
1654
1655
  
  	return err;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1656
  static int set_new_pw(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1657
1658
  {
  	u8 cpin_uid[OPAL_UID_LENGTH];
eed64951f   Jon Derrick   block/sed: Embed ...
1659
  	struct opal_session_info *usr = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
  
  	memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
  
  	if (usr->who != OPAL_ADMIN1) {
  		cpin_uid[5] = 0x03;
  		if (usr->sum)
  			cpin_uid[7] = usr->opal_key.lr + 1;
  		else
  			cpin_uid[7] = usr->who;
  	}
  
  	if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
  			   cpin_uid, dev)) {
591c59d18   Scott Bauer   block: sed-opal: ...
1673
1674
  		pr_debug("Error building set password command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1675
1676
1677
1678
1679
  		return -ERANGE;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1680
  static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1681
1682
  {
  	u8 cpin_uid[OPAL_UID_LENGTH];
eed64951f   Jon Derrick   block/sed: Embed ...
1683
  	struct opal_key *key = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1684
1685
1686
1687
  
  	memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
  
  	if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
591c59d18   Scott Bauer   block: sed-opal: ...
1688
1689
  		pr_debug("Error building Set SID cpin
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1690
1691
1692
1693
  		return -ERANGE;
  	}
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1694
  static int add_user_to_lr(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1695
1696
1697
  {
  	u8 lr_buffer[OPAL_UID_LENGTH];
  	u8 user_uid[OPAL_UID_LENGTH];
eed64951f   Jon Derrick   block/sed: Embed ...
1698
  	struct opal_lock_unlock *lkul = data;
e8b292245   David Kozub   block: sed-opal: ...
1699
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1700

455a7b238   Scott Bauer   block: Add Sed-op...
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
  	memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
  	       OPAL_UID_LENGTH);
  
  	if (lkul->l_state == OPAL_RW)
  		memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
  		       OPAL_UID_LENGTH);
  
  	lr_buffer[7] = lkul->session.opal_key.lr;
  
  	memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
  
  	user_uid[7] = lkul->session.who;
e8b292245   David Kozub   block: sed-opal: ...
1713
  	err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1714

455a7b238   Scott Bauer   block: Add Sed-op...
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, 3);
  
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_bytestring(&err, dev,
  			     opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
  			     OPAL_UID_LENGTH/2);
  	add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_bytestring(&err, dev,
  			     opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
  			     OPAL_UID_LENGTH/2);
  	add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
  			     OPAL_UID_LENGTH/2);
  	add_token_u8(&err, dev, 1);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1752
1753
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1754
1755
  		pr_debug("Error building add user to locking range command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1756
1757
1758
1759
1760
  		return err;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1761
  static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1762
1763
  {
  	u8 lr_buffer[OPAL_UID_LENGTH];
eed64951f   Jon Derrick   block/sed: Embed ...
1764
  	struct opal_lock_unlock *lkul = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1765
1766
  	u8 read_locked = 1, write_locked = 1;
  	int err = 0;
455a7b238   Scott Bauer   block: Add Sed-op...
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
  	if (build_locking_range(lr_buffer, sizeof(lr_buffer),
  				lkul->session.opal_key.lr) < 0)
  		return -ERANGE;
  
  	switch (lkul->l_state) {
  	case OPAL_RO:
  		read_locked = 0;
  		write_locked = 1;
  		break;
  	case OPAL_RW:
  		read_locked = 0;
  		write_locked = 0;
  		break;
  	case OPAL_LK:
1e815b33c   David Kozub   block: sed-opal: ...
1781
  		/* vars are initialized to locked */
455a7b238   Scott Bauer   block: Add Sed-op...
1782
1783
  		break;
  	default:
591c59d18   Scott Bauer   block: sed-opal: ...
1784
1785
  		pr_debug("Tried to set an invalid locking state... returning to uland
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1786
1787
  		return OPAL_INVAL_PARAM;
  	}
e8b292245   David Kozub   block: sed-opal: ...
1788
  	err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
455a7b238   Scott Bauer   block: Add Sed-op...
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_VALUES);
  	add_token_u8(&err, dev, OPAL_STARTLIST);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_READLOCKED);
  	add_token_u8(&err, dev, read_locked);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_STARTNAME);
  	add_token_u8(&err, dev, OPAL_WRITELOCKED);
  	add_token_u8(&err, dev, write_locked);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
  
  	add_token_u8(&err, dev, OPAL_ENDLIST);
  	add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1805
1806
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1807
1808
  		pr_debug("Error building SET command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1809
1810
  		return err;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1811

455a7b238   Scott Bauer   block: Add Sed-op...
1812
1813
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1814
  static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1815
1816
1817
  {
  	u8 lr_buffer[OPAL_UID_LENGTH];
  	u8 read_locked = 1, write_locked = 1;
eed64951f   Jon Derrick   block/sed: Embed ...
1818
  	struct opal_lock_unlock *lkul = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1819
1820
1821
1822
  	int ret;
  
  	clear_opal_cmd(dev);
  	set_comid(dev, dev->comid);
455a7b238   Scott Bauer   block: Add Sed-op...
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
  	if (build_locking_range(lr_buffer, sizeof(lr_buffer),
  				lkul->session.opal_key.lr) < 0)
  		return -ERANGE;
  
  	switch (lkul->l_state) {
  	case OPAL_RO:
  		read_locked = 0;
  		write_locked = 1;
  		break;
  	case OPAL_RW:
  		read_locked = 0;
  		write_locked = 0;
  		break;
  	case OPAL_LK:
1e815b33c   David Kozub   block: sed-opal: ...
1837
  		/* vars are initialized to locked */
455a7b238   Scott Bauer   block: Add Sed-op...
1838
1839
  		break;
  	default:
591c59d18   Scott Bauer   block: sed-opal: ...
1840
1841
  		pr_debug("Tried to set an invalid locking state.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1842
1843
1844
1845
1846
1847
  		return OPAL_INVAL_PARAM;
  	}
  	ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
  					read_locked, write_locked);
  
  	if (ret < 0) {
591c59d18   Scott Bauer   block: sed-opal: ...
1848
1849
  		pr_debug("Error building SET command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1850
1851
  		return ret;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
1852

455a7b238   Scott Bauer   block: Add Sed-op...
1853
1854
  	return finalize_and_send(dev, parse_and_check_status);
  }
eed64951f   Jon Derrick   block/sed: Embed ...
1855
  static int activate_lsp(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1856
  {
eed64951f   Jon Derrick   block/sed: Embed ...
1857
  	struct opal_lr_act *opal_act = data;
455a7b238   Scott Bauer   block: Add Sed-op...
1858
  	u8 user_lr[OPAL_UID_LENGTH];
e8b292245   David Kozub   block: sed-opal: ...
1859
  	int err, i;
455a7b238   Scott Bauer   block: Add Sed-op...
1860

e8b292245   David Kozub   block: sed-opal: ...
1861
1862
  	err = cmd_start(dev, opaluid[OPAL_LOCKINGSP_UID],
  			opalmethod[OPAL_ACTIVATE]);
455a7b238   Scott Bauer   block: Add Sed-op...
1863
1864
1865
1866
1867
1868
  
  	if (opal_act->sum) {
  		err = build_locking_range(user_lr, sizeof(user_lr),
  					  opal_act->lr[0]);
  		if (err)
  			return err;
455a7b238   Scott Bauer   block: Add Sed-op...
1869
  		add_token_u8(&err, dev, OPAL_STARTNAME);
c6da429ea   Revanth Rajashekar   block: sed-opal: ...
1870
  		add_token_u64(&err, dev, OPAL_SUM_SET_LIST);
455a7b238   Scott Bauer   block: Add Sed-op...
1871
1872
1873
1874
1875
1876
1877
1878
1879
  
  		add_token_u8(&err, dev, OPAL_STARTLIST);
  		add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
  		for (i = 1; i < opal_act->num_lrs; i++) {
  			user_lr[7] = opal_act->lr[i];
  			add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
  		}
  		add_token_u8(&err, dev, OPAL_ENDLIST);
  		add_token_u8(&err, dev, OPAL_ENDNAME);
455a7b238   Scott Bauer   block: Add Sed-op...
1880
1881
1882
  	}
  
  	if (err) {
591c59d18   Scott Bauer   block: sed-opal: ...
1883
1884
  		pr_debug("Error building Activate LockingSP command.
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1885
1886
1887
1888
1889
  		return err;
  	}
  
  	return finalize_and_send(dev, parse_and_check_status);
  }
3fff234b8   David Kozub   block: sed-opal: ...
1890
1891
  /* Determine if we're in the Manufactured Inactive or Active state */
  static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1892
1893
  {
  	u8 lc_status;
3fff234b8   David Kozub   block: sed-opal: ...
1894
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1895

3fff234b8   David Kozub   block: sed-opal: ...
1896
1897
1898
1899
  	err = generic_get_column(dev, opaluid[OPAL_LOCKINGSP_UID],
  				 OPAL_LIFECYCLE);
  	if (err)
  		return err;
455a7b238   Scott Bauer   block: Add Sed-op...
1900
1901
  
  	lc_status = response_get_u64(&dev->parsed, 4);
1e815b33c   David Kozub   block: sed-opal: ...
1902
  	/* 0x08 is Manufactured Inactive */
455a7b238   Scott Bauer   block: Add Sed-op...
1903
1904
  	/* 0x09 is Manufactured */
  	if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
591c59d18   Scott Bauer   block: sed-opal: ...
1905
1906
  		pr_debug("Couldn't determine the status of the Lifecycle state
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1907
1908
1909
1910
1911
  		return -ENODEV;
  	}
  
  	return 0;
  }
3fff234b8   David Kozub   block: sed-opal: ...
1912
  static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
1913
1914
1915
  {
  	const char *msid_pin;
  	size_t strlen;
3fff234b8   David Kozub   block: sed-opal: ...
1916
  	int err;
455a7b238   Scott Bauer   block: Add Sed-op...
1917

3fff234b8   David Kozub   block: sed-opal: ...
1918
1919
1920
  	err = generic_get_column(dev, opaluid[OPAL_C_PIN_MSID], OPAL_PIN);
  	if (err)
  		return err;
455a7b238   Scott Bauer   block: Add Sed-op...
1921
1922
1923
  
  	strlen = response_get_string(&dev->parsed, 4, &msid_pin);
  	if (!msid_pin) {
3fff234b8   David Kozub   block: sed-opal: ...
1924
1925
  		pr_debug("Couldn't extract MSID_CPIN from response
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
  		return OPAL_INVAL_PARAM;
  	}
  
  	dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
  	if (!dev->prev_data)
  		return -ENOMEM;
  
  	dev->prev_d_len = strlen;
  
  	return 0;
  }
51f421c85   Revanth Rajashekar   block: sed-opal: ...
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
  static int write_table_data(struct opal_dev *dev, void *data)
  {
  	struct opal_read_write_table *write_tbl = data;
  
  	return generic_table_write_data(dev, write_tbl->data, write_tbl->offset,
  					write_tbl->size, write_tbl->table_uid);
  }
  
  static int read_table_data_cont(struct opal_dev *dev)
  {
  	int err;
  	const char *data_read;
  
  	err = parse_and_check_status(dev);
  	if (err)
  		return err;
  
  	dev->prev_d_len = response_get_string(&dev->parsed, 1, &data_read);
  	dev->prev_data = (void *)data_read;
  	if (!dev->prev_data) {
  		pr_debug("%s: Couldn't read data from the table.
  ", __func__);
  		return OPAL_INVAL_PARAM;
  	}
  
  	return 0;
  }
  
  /*
   * IO_BUFFER_LENGTH = 2048
   * sizeof(header) = 56
   * No. of Token Bytes in the Response = 11
   * MAX size of data that can be carried in response buffer
   * at a time is : 2048 - (56 + 11) = 1981 = 0x7BD.
   */
  #define OPAL_MAX_READ_TABLE (0x7BD)
  
  static int read_table_data(struct opal_dev *dev, void *data)
  {
  	struct opal_read_write_table *read_tbl = data;
  	int err;
  	size_t off = 0, max_read_size = OPAL_MAX_READ_TABLE;
  	u64 table_len, len;
  	u64 offset = read_tbl->offset, read_size = read_tbl->size - 1;
  	u8 __user *dst;
  
  	err = generic_get_table_info(dev, read_tbl->table_uid, OPAL_TABLE_ROWS);
  	if (err) {
  		pr_debug("Couldn't get the table size
  ");
  		return err;
  	}
  
  	table_len = response_get_u64(&dev->parsed, 4);
  
  	/* Check if the user is trying to read from the table limits */
  	if (read_size > table_len || offset > table_len - read_size) {
  		pr_debug("Read size exceeds the Table size limits (%llu vs. %llu)
  ",
  			  offset + read_size, table_len);
  		return -EINVAL;
  	}
  
  	while (off < read_size) {
  		err = cmd_start(dev, read_tbl->table_uid, opalmethod[OPAL_GET]);
  
  		add_token_u8(&err, dev, OPAL_STARTLIST);
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, OPAL_STARTROW);
  		add_token_u64(&err, dev, offset + off); /* start row value */
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  
  		add_token_u8(&err, dev, OPAL_STARTNAME);
  		add_token_u8(&err, dev, OPAL_ENDROW);
  
  		len = min(max_read_size, (size_t)(read_size - off));
  		add_token_u64(&err, dev, offset + off + len); /* end row value
  							       */
  		add_token_u8(&err, dev, OPAL_ENDNAME);
  		add_token_u8(&err, dev, OPAL_ENDLIST);
  
  		if (err) {
  			pr_debug("Error building read table data command.
  ");
  			break;
  		}
  
  		err = finalize_and_send(dev, read_table_data_cont);
  		if (err)
  			break;
  
  		/* len+1: This includes the NULL terminator at the end*/
  		if (dev->prev_d_len > len + 1) {
  			err = -EOVERFLOW;
  			break;
  		}
  
  		dst = (u8 __user *)(uintptr_t)read_tbl->data;
  		if (copy_to_user(dst + off, dev->prev_data, dev->prev_d_len)) {
  			pr_debug("Error copying data to userspace
  ");
  			err = -EFAULT;
  			break;
  		}
  		dev->prev_data = NULL;
  
  		off += len;
  	}
  
  	return err;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
2048
  static int end_opal_session(struct opal_dev *dev, void *data)
455a7b238   Scott Bauer   block: Add Sed-op...
2049
2050
2051
2052
  {
  	int err = 0;
  
  	clear_opal_cmd(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
2053
2054
  	set_comid(dev, dev->comid);
  	add_token_u8(&err, dev, OPAL_ENDOFSESSION);
455a7b238   Scott Bauer   block: Add Sed-op...
2055

eed64951f   Jon Derrick   block/sed: Embed ...
2056
2057
  	if (err < 0)
  		return err;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2058

455a7b238   Scott Bauer   block: Add Sed-op...
2059
2060
2061
2062
2063
  	return finalize_and_send(dev, end_session_cont);
  }
  
  static int end_opal_session_error(struct opal_dev *dev)
  {
0af2648ec   David Kozub   block: sed-opal: ...
2064
2065
  	const struct opal_step error_end_session = {
  		end_opal_session,
455a7b238   Scott Bauer   block: Add Sed-op...
2066
  	};
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2067

0af2648ec   David Kozub   block: sed-opal: ...
2068
  	return execute_step(dev, &error_end_session, 0);
455a7b238   Scott Bauer   block: Add Sed-op...
2069
  }
3db87236c   David Kozub   block: sed-opal: ...
2070
  static inline void setup_opal_dev(struct opal_dev *dev)
455a7b238   Scott Bauer   block: Add Sed-op...
2071
  {
455a7b238   Scott Bauer   block: Add Sed-op...
2072
2073
  	dev->tsn = 0;
  	dev->hsn = 0;
455a7b238   Scott Bauer   block: Add Sed-op...
2074
2075
2076
2077
2078
  	dev->prev_data = NULL;
  }
  
  static int check_opal_support(struct opal_dev *dev)
  {
455a7b238   Scott Bauer   block: Add Sed-op...
2079
2080
2081
  	int ret;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2082
  	setup_opal_dev(dev);
0af2648ec   David Kozub   block: sed-opal: ...
2083
  	ret = opal_discovery0_step(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
2084
2085
  	dev->supported = !ret;
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2086

455a7b238   Scott Bauer   block: Add Sed-op...
2087
2088
  	return ret;
  }
7d6d15789   Scott Bauer   block/sed-opal: I...
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
  static void clean_opal_dev(struct opal_dev *dev)
  {
  
  	struct opal_suspend_data *suspend, *next;
  
  	mutex_lock(&dev->dev_lock);
  	list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
  		list_del(&suspend->node);
  		kfree(suspend);
  	}
  	mutex_unlock(&dev->dev_lock);
  }
  
  void free_opal_dev(struct opal_dev *dev)
  {
  	if (!dev)
  		return;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2106

7d6d15789   Scott Bauer   block/sed-opal: I...
2107
2108
2109
2110
  	clean_opal_dev(dev);
  	kfree(dev);
  }
  EXPORT_SYMBOL(free_opal_dev);
4f1244c82   Christoph Hellwig   block/sed-opal: a...
2111
  struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
455a7b238   Scott Bauer   block: Add Sed-op...
2112
  {
4f1244c82   Christoph Hellwig   block/sed-opal: a...
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
  	struct opal_dev *dev;
  
  	dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  	if (!dev)
  		return NULL;
  
  	INIT_LIST_HEAD(&dev->unlk_lst);
  	mutex_init(&dev->dev_lock);
  	dev->data = data;
  	dev->send_recv = send_recv;
  	if (check_opal_support(dev) != 0) {
f5b37b7c2   Christoph Hellwig   block/sed-opal: t...
2124
2125
  		pr_debug("Opal is not supported on this device
  ");
4f1244c82   Christoph Hellwig   block/sed-opal: a...
2126
2127
2128
  		kfree(dev);
  		return NULL;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2129

4f1244c82   Christoph Hellwig   block/sed-opal: a...
2130
  	return dev;
455a7b238   Scott Bauer   block: Add Sed-op...
2131
2132
2133
2134
2135
2136
  }
  EXPORT_SYMBOL(init_opal_dev);
  
  static int opal_secure_erase_locking_range(struct opal_dev *dev,
  					   struct opal_session_info *opal_session)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2137
  	const struct opal_step erase_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2138
2139
2140
  		{ start_auth_opal_session, opal_session },
  		{ get_active_key, &opal_session->opal_key.lr },
  		{ gen_key, },
3db87236c   David Kozub   block: sed-opal: ...
2141
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2142
2143
2144
2145
  	};
  	int ret;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2146
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2147
  	ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2148
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2149

455a7b238   Scott Bauer   block: Add Sed-op...
2150
2151
2152
2153
2154
2155
  	return ret;
  }
  
  static int opal_erase_locking_range(struct opal_dev *dev,
  				    struct opal_session_info *opal_session)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2156
  	const struct opal_step erase_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2157
2158
  		{ start_auth_opal_session, opal_session },
  		{ erase_locking_range, opal_session },
3db87236c   David Kozub   block: sed-opal: ...
2159
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2160
2161
2162
2163
  	};
  	int ret;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2164
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2165
  	ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2166
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2167

455a7b238   Scott Bauer   block: Add Sed-op...
2168
2169
2170
2171
2172
2173
  	return ret;
  }
  
  static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
  					  struct opal_mbr_data *opal_mbr)
  {
78bf47353   David Kozub   block: sed-opal: ...
2174
2175
  	u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
  		OPAL_TRUE : OPAL_FALSE;
eed64951f   Jon Derrick   block/sed: Embed ...
2176
  	const struct opal_step mbr_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2177
  		{ start_admin1LSP_opal_session, &opal_mbr->key },
78bf47353   David Kozub   block: sed-opal: ...
2178
  		{ set_mbr_done, &enable_disable },
eed64951f   Jon Derrick   block/sed: Embed ...
2179
2180
  		{ end_opal_session, },
  		{ start_admin1LSP_opal_session, &opal_mbr->key },
78bf47353   David Kozub   block: sed-opal: ...
2181
  		{ set_mbr_enable_disable, &enable_disable },
3db87236c   David Kozub   block: sed-opal: ...
2182
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2183
2184
2185
2186
2187
2188
2189
2190
  	};
  	int ret;
  
  	if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
  	    opal_mbr->enable_disable != OPAL_MBR_DISABLE)
  		return -EINVAL;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2191
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2192
  	ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2193
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2194

455a7b238   Scott Bauer   block: Add Sed-op...
2195
2196
  	return ret;
  }
c98884434   Jonas Rabenstein   block: sed-opal: ...
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
  static int opal_set_mbr_done(struct opal_dev *dev,
  			     struct opal_mbr_done *mbr_done)
  {
  	u8 mbr_done_tf = mbr_done->done_flag == OPAL_MBR_DONE ?
  		OPAL_TRUE : OPAL_FALSE;
  
  	const struct opal_step mbr_steps[] = {
  		{ start_admin1LSP_opal_session, &mbr_done->key },
  		{ set_mbr_done, &mbr_done_tf },
  		{ end_opal_session, }
  	};
  	int ret;
  
  	if (mbr_done->done_flag != OPAL_MBR_DONE &&
  	    mbr_done->done_flag != OPAL_MBR_NOT_DONE)
  		return -EINVAL;
  
  	mutex_lock(&dev->dev_lock);
  	setup_opal_dev(dev);
  	ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2218

c98884434   Jonas Rabenstein   block: sed-opal: ...
2219
2220
  	return ret;
  }
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
  static int opal_write_shadow_mbr(struct opal_dev *dev,
  				 struct opal_shadow_mbr *info)
  {
  	const struct opal_step mbr_steps[] = {
  		{ start_admin1LSP_opal_session, &info->key },
  		{ write_shadow_mbr, info },
  		{ end_opal_session, }
  	};
  	int ret;
  
  	if (info->size == 0)
  		return 0;
  
  	mutex_lock(&dev->dev_lock);
  	setup_opal_dev(dev);
  	ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2238

a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
2239
2240
  	return ret;
  }
455a7b238   Scott Bauer   block: Add Sed-op...
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
  static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
  {
  	struct opal_suspend_data *suspend;
  
  	suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
  	if (!suspend)
  		return -ENOMEM;
  
  	suspend->unlk = *lk_unlk;
  	suspend->lr = lk_unlk->session.opal_key.lr;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2253
  	setup_opal_dev(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
2254
2255
  	add_suspend_info(dev, suspend);
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2256

455a7b238   Scott Bauer   block: Add Sed-op...
2257
2258
2259
2260
2261
2262
  	return 0;
  }
  
  static int opal_add_user_to_lr(struct opal_dev *dev,
  			       struct opal_lock_unlock *lk_unlk)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2263
  	const struct opal_step steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2264
2265
  		{ start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
  		{ add_user_to_lr, lk_unlk },
3db87236c   David Kozub   block: sed-opal: ...
2266
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2267
2268
2269
2270
2271
  	};
  	int ret;
  
  	if (lk_unlk->l_state != OPAL_RO &&
  	    lk_unlk->l_state != OPAL_RW) {
591c59d18   Scott Bauer   block: sed-opal: ...
2272
2273
  		pr_debug("Locking state was not RO or RW
  ");
455a7b238   Scott Bauer   block: Add Sed-op...
2274
2275
  		return -EINVAL;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2276

b0bfdfc2b   Jon Derrick   block/sed: Fix op...
2277
  	if (lk_unlk->session.who < OPAL_USER1 ||
455a7b238   Scott Bauer   block: Add Sed-op...
2278
  	    lk_unlk->session.who > OPAL_USER9) {
591c59d18   Scott Bauer   block: sed-opal: ...
2279
2280
2281
  		pr_debug("Authority was not within the range of users: %d
  ",
  			 lk_unlk->session.who);
455a7b238   Scott Bauer   block: Add Sed-op...
2282
2283
  		return -EINVAL;
  	}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2284

455a7b238   Scott Bauer   block: Add Sed-op...
2285
  	if (lk_unlk->session.sum) {
591c59d18   Scott Bauer   block: sed-opal: ...
2286
2287
2288
  		pr_debug("%s not supported in sum. Use setup locking range
  ",
  			 __func__);
455a7b238   Scott Bauer   block: Add Sed-op...
2289
2290
2291
2292
  		return -EINVAL;
  	}
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2293
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2294
  	ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2295
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2296

455a7b238   Scott Bauer   block: Add Sed-op...
2297
2298
  	return ret;
  }
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2299
  static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal, bool psid)
455a7b238   Scott Bauer   block: Add Sed-op...
2300
  {
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2301
  	/* controller will terminate session */
eed64951f   Jon Derrick   block/sed: Embed ...
2302
  	const struct opal_step revert_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2303
  		{ start_SIDASP_opal_session, opal },
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2304
  		{ revert_tper, }
455a7b238   Scott Bauer   block: Add Sed-op...
2305
  	};
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2306
2307
2308
2309
  	const struct opal_step psid_revert_steps[] = {
  		{ start_PSID_opal_session, opal },
  		{ revert_tper, }
  	};
455a7b238   Scott Bauer   block: Add Sed-op...
2310
2311
2312
  	int ret;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2313
  	setup_opal_dev(dev);
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2314
2315
2316
2317
2318
2319
  	if (psid)
  		ret = execute_steps(dev, psid_revert_steps,
  				    ARRAY_SIZE(psid_revert_steps));
  	else
  		ret = execute_steps(dev, revert_steps,
  				    ARRAY_SIZE(revert_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2320
  	mutex_unlock(&dev->dev_lock);
7d6d15789   Scott Bauer   block/sed-opal: I...
2321
2322
2323
2324
2325
2326
2327
  
  	/*
  	 * If we successfully reverted lets clean
  	 * any saved locking ranges.
  	 */
  	if (!ret)
  		clean_opal_dev(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
2328
2329
  	return ret;
  }
eed64951f   Jon Derrick   block/sed: Embed ...
2330
2331
  static int __opal_lock_unlock(struct opal_dev *dev,
  			      struct opal_lock_unlock *lk_unlk)
455a7b238   Scott Bauer   block: Add Sed-op...
2332
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2333
  	const struct opal_step unlock_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2334
2335
  		{ start_auth_opal_session, &lk_unlk->session },
  		{ lock_unlock_locking_range, lk_unlk },
3db87236c   David Kozub   block: sed-opal: ...
2336
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2337
  	};
eed64951f   Jon Derrick   block/sed: Embed ...
2338
  	const struct opal_step unlock_sum_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2339
2340
  		{ start_auth_opal_session, &lk_unlk->session },
  		{ lock_unlock_locking_range_sum, lk_unlk },
3db87236c   David Kozub   block: sed-opal: ...
2341
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2342
  	};
3db87236c   David Kozub   block: sed-opal: ...
2343
  	if (lk_unlk->session.sum)
a80f36cc6   David Kozub   block: sed-opal: ...
2344
2345
  		return execute_steps(dev, unlock_sum_steps,
  				     ARRAY_SIZE(unlock_sum_steps));
3db87236c   David Kozub   block: sed-opal: ...
2346
  	else
a80f36cc6   David Kozub   block: sed-opal: ...
2347
2348
  		return execute_steps(dev, unlock_steps,
  				     ARRAY_SIZE(unlock_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2349
  }
dbec491b1   Scott Bauer   block: sed-opal: ...
2350
2351
  static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
  {
78bf47353   David Kozub   block: sed-opal: ...
2352
  	u8 mbr_done_tf = OPAL_TRUE;
1e815b33c   David Kozub   block: sed-opal: ...
2353
  	const struct opal_step mbrdone_step[] = {
dbec491b1   Scott Bauer   block: sed-opal: ...
2354
2355
  		{ start_admin1LSP_opal_session, key },
  		{ set_mbr_done, &mbr_done_tf },
3db87236c   David Kozub   block: sed-opal: ...
2356
  		{ end_opal_session, }
dbec491b1   Scott Bauer   block: sed-opal: ...
2357
  	};
a80f36cc6   David Kozub   block: sed-opal: ...
2358
  	return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
dbec491b1   Scott Bauer   block: sed-opal: ...
2359
  }
eed64951f   Jon Derrick   block/sed: Embed ...
2360
2361
  static int opal_lock_unlock(struct opal_dev *dev,
  			    struct opal_lock_unlock *lk_unlk)
455a7b238   Scott Bauer   block: Add Sed-op...
2362
  {
455a7b238   Scott Bauer   block: Add Sed-op...
2363
  	int ret;
15ddffcb3   Revanth Rajashekar   block: sed-opal: ...
2364
  	if (lk_unlk->session.who > OPAL_USER9)
455a7b238   Scott Bauer   block: Add Sed-op...
2365
2366
2367
  		return -EINVAL;
  
  	mutex_lock(&dev->dev_lock);
eed64951f   Jon Derrick   block/sed: Embed ...
2368
  	ret = __opal_lock_unlock(dev, lk_unlk);
455a7b238   Scott Bauer   block: Add Sed-op...
2369
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2370

455a7b238   Scott Bauer   block: Add Sed-op...
2371
2372
2373
2374
2375
  	return ret;
  }
  
  static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2376
  	const struct opal_step owner_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2377
2378
2379
2380
2381
  		{ start_anybodyASP_opal_session, },
  		{ get_msid_cpin_pin, },
  		{ end_opal_session, },
  		{ start_SIDASP_opal_session, opal },
  		{ set_sid_cpin_pin, opal },
3db87236c   David Kozub   block: sed-opal: ...
2382
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2383
  	};
455a7b238   Scott Bauer   block: Add Sed-op...
2384
2385
2386
2387
2388
2389
  	int ret;
  
  	if (!dev)
  		return -ENODEV;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2390
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2391
  	ret = execute_steps(dev, owner_steps, ARRAY_SIZE(owner_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2392
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2393

455a7b238   Scott Bauer   block: Add Sed-op...
2394
2395
  	return ret;
  }
1e815b33c   David Kozub   block: sed-opal: ...
2396
2397
  static int opal_activate_lsp(struct opal_dev *dev,
  			     struct opal_lr_act *opal_lr_act)
455a7b238   Scott Bauer   block: Add Sed-op...
2398
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2399
  	const struct opal_step active_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2400
2401
2402
  		{ start_SIDASP_opal_session, &opal_lr_act->key },
  		{ get_lsp_lifecycle, },
  		{ activate_lsp, opal_lr_act },
3db87236c   David Kozub   block: sed-opal: ...
2403
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2404
2405
2406
2407
2408
2409
2410
  	};
  	int ret;
  
  	if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
  		return -EINVAL;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2411
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2412
  	ret = execute_steps(dev, active_steps, ARRAY_SIZE(active_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2413
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2414

455a7b238   Scott Bauer   block: Add Sed-op...
2415
2416
2417
2418
2419
2420
  	return ret;
  }
  
  static int opal_setup_locking_range(struct opal_dev *dev,
  				    struct opal_user_lr_setup *opal_lrs)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2421
  	const struct opal_step lr_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2422
2423
  		{ start_auth_opal_session, &opal_lrs->session },
  		{ setup_locking_range, opal_lrs },
3db87236c   David Kozub   block: sed-opal: ...
2424
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2425
2426
2427
2428
  	};
  	int ret;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2429
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2430
  	ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2431
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2432

455a7b238   Scott Bauer   block: Add Sed-op...
2433
2434
2435
2436
2437
  	return ret;
  }
  
  static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2438
  	const struct opal_step pw_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2439
2440
  		{ start_auth_opal_session, &opal_pw->session },
  		{ set_new_pw, &opal_pw->new_user_pw },
3db87236c   David Kozub   block: sed-opal: ...
2441
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2442
  	};
455a7b238   Scott Bauer   block: Add Sed-op...
2443
  	int ret;
15ddffcb3   Revanth Rajashekar   block: sed-opal: ...
2444
  	if (opal_pw->session.who > OPAL_USER9  ||
455a7b238   Scott Bauer   block: Add Sed-op...
2445
2446
2447
2448
  	    opal_pw->new_user_pw.who > OPAL_USER9)
  		return -EINVAL;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2449
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2450
  	ret = execute_steps(dev, pw_steps, ARRAY_SIZE(pw_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2451
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2452

455a7b238   Scott Bauer   block: Add Sed-op...
2453
2454
2455
2456
2457
2458
  	return ret;
  }
  
  static int opal_activate_user(struct opal_dev *dev,
  			      struct opal_session_info *opal_session)
  {
eed64951f   Jon Derrick   block/sed: Embed ...
2459
  	const struct opal_step act_steps[] = {
eed64951f   Jon Derrick   block/sed: Embed ...
2460
2461
  		{ start_admin1LSP_opal_session, &opal_session->opal_key },
  		{ internal_activate_user, opal_session },
3db87236c   David Kozub   block: sed-opal: ...
2462
  		{ end_opal_session, }
455a7b238   Scott Bauer   block: Add Sed-op...
2463
  	};
455a7b238   Scott Bauer   block: Add Sed-op...
2464
2465
2466
  	int ret;
  
  	/* We can't activate Admin1 it's active as manufactured */
b0bfdfc2b   Jon Derrick   block/sed: Fix op...
2467
  	if (opal_session->who < OPAL_USER1 ||
455a7b238   Scott Bauer   block: Add Sed-op...
2468
  	    opal_session->who > OPAL_USER9) {
591c59d18   Scott Bauer   block: sed-opal: ...
2469
2470
  		pr_debug("Who was not a valid user: %d
  ", opal_session->who);
455a7b238   Scott Bauer   block: Add Sed-op...
2471
2472
2473
2474
  		return -EINVAL;
  	}
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2475
  	setup_opal_dev(dev);
a80f36cc6   David Kozub   block: sed-opal: ...
2476
  	ret = execute_steps(dev, act_steps, ARRAY_SIZE(act_steps));
455a7b238   Scott Bauer   block: Add Sed-op...
2477
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2478

455a7b238   Scott Bauer   block: Add Sed-op...
2479
2480
2481
2482
2483
2484
  	return ret;
  }
  
  bool opal_unlock_from_suspend(struct opal_dev *dev)
  {
  	struct opal_suspend_data *suspend;
455a7b238   Scott Bauer   block: Add Sed-op...
2485
2486
2487
2488
2489
  	bool was_failure = false;
  	int ret = 0;
  
  	if (!dev)
  		return false;
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2490

455a7b238   Scott Bauer   block: Add Sed-op...
2491
2492
2493
2494
  	if (!dev->supported)
  		return false;
  
  	mutex_lock(&dev->dev_lock);
3db87236c   David Kozub   block: sed-opal: ...
2495
  	setup_opal_dev(dev);
455a7b238   Scott Bauer   block: Add Sed-op...
2496
2497
  
  	list_for_each_entry(suspend, &dev->unlk_lst, node) {
455a7b238   Scott Bauer   block: Add Sed-op...
2498
2499
  		dev->tsn = 0;
  		dev->hsn = 0;
eed64951f   Jon Derrick   block/sed: Embed ...
2500
  		ret = __opal_lock_unlock(dev, &suspend->unlk);
455a7b238   Scott Bauer   block: Add Sed-op...
2501
  		if (ret) {
591c59d18   Scott Bauer   block: sed-opal: ...
2502
2503
2504
2505
  			pr_debug("Failed to unlock LR %hhu with sum %d
  ",
  				 suspend->unlk.session.opal_key.lr,
  				 suspend->unlk.session.sum);
455a7b238   Scott Bauer   block: Add Sed-op...
2506
2507
  			was_failure = true;
  		}
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2508

dbec491b1   Scott Bauer   block: sed-opal: ...
2509
2510
2511
2512
2513
2514
  		if (dev->mbr_enabled) {
  			ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
  			if (ret)
  				pr_debug("Failed to set MBR Done in S3 resume
  ");
  		}
455a7b238   Scott Bauer   block: Add Sed-op...
2515
2516
  	}
  	mutex_unlock(&dev->dev_lock);
5cc23ed75   Revanth Rajashekar   block: sed-opal: ...
2517

455a7b238   Scott Bauer   block: Add Sed-op...
2518
2519
2520
  	return was_failure;
  }
  EXPORT_SYMBOL(opal_unlock_from_suspend);
51f421c85   Revanth Rajashekar   block: sed-opal: ...
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
  static int opal_read_table(struct opal_dev *dev,
  			   struct opal_read_write_table *rw_tbl)
  {
  	const struct opal_step read_table_steps[] = {
  		{ start_admin1LSP_opal_session, &rw_tbl->key },
  		{ read_table_data, rw_tbl },
  		{ end_opal_session, }
  	};
  	int ret = 0;
  
  	if (!rw_tbl->size)
  		return ret;
  
  	return execute_steps(dev, read_table_steps,
  			     ARRAY_SIZE(read_table_steps));
  }
  
  static int opal_write_table(struct opal_dev *dev,
  			    struct opal_read_write_table *rw_tbl)
  {
  	const struct opal_step write_table_steps[] = {
  		{ start_admin1LSP_opal_session, &rw_tbl->key },
  		{ write_table_data, rw_tbl },
  		{ end_opal_session, }
  	};
  	int ret = 0;
  
  	if (!rw_tbl->size)
  		return ret;
  
  	return execute_steps(dev, write_table_steps,
  			     ARRAY_SIZE(write_table_steps));
  }
  
  static int opal_generic_read_write_table(struct opal_dev *dev,
  					 struct opal_read_write_table *rw_tbl)
  {
  	int ret, bit_set;
  
  	mutex_lock(&dev->dev_lock);
  	setup_opal_dev(dev);
  
  	bit_set = fls64(rw_tbl->flags) - 1;
  	switch (bit_set) {
  	case OPAL_READ_TABLE:
  		ret = opal_read_table(dev, rw_tbl);
  		break;
  	case OPAL_WRITE_TABLE:
  		ret = opal_write_table(dev, rw_tbl);
  		break;
  	default:
  		pr_debug("Invalid bit set in the flag (%016llx).
  ",
  			 rw_tbl->flags);
  		ret = -EINVAL;
  		break;
  	}
  
  	mutex_unlock(&dev->dev_lock);
  
  	return ret;
  }
e225c20eb   Scott Bauer   Move stack parame...
2583
  int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
455a7b238   Scott Bauer   block: Add Sed-op...
2584
  {
e225c20eb   Scott Bauer   Move stack parame...
2585
2586
  	void *p;
  	int ret = -ENOTTY;
455a7b238   Scott Bauer   block: Add Sed-op...
2587
2588
2589
  
  	if (!capable(CAP_SYS_ADMIN))
  		return -EACCES;
4f1244c82   Christoph Hellwig   block/sed-opal: a...
2590
2591
  	if (!dev)
  		return -ENOTSUPP;
591c59d18   Scott Bauer   block: sed-opal: ...
2592
  	if (!dev->supported)
455a7b238   Scott Bauer   block: Add Sed-op...
2593
  		return -ENOTSUPP;
455a7b238   Scott Bauer   block: Add Sed-op...
2594

eed64951f   Jon Derrick   block/sed: Embed ...
2595
  	p = memdup_user(arg, _IOC_SIZE(cmd));
e225c20eb   Scott Bauer   Move stack parame...
2596
2597
  	if (IS_ERR(p))
  		return PTR_ERR(p);
455a7b238   Scott Bauer   block: Add Sed-op...
2598

e225c20eb   Scott Bauer   Move stack parame...
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
  	switch (cmd) {
  	case IOC_OPAL_SAVE:
  		ret = opal_save(dev, p);
  		break;
  	case IOC_OPAL_LOCK_UNLOCK:
  		ret = opal_lock_unlock(dev, p);
  		break;
  	case IOC_OPAL_TAKE_OWNERSHIP:
  		ret = opal_take_ownership(dev, p);
  		break;
  	case IOC_OPAL_ACTIVATE_LSP:
  		ret = opal_activate_lsp(dev, p);
  		break;
  	case IOC_OPAL_SET_PW:
  		ret = opal_set_new_pw(dev, p);
  		break;
  	case IOC_OPAL_ACTIVATE_USR:
  		ret = opal_activate_user(dev, p);
  		break;
  	case IOC_OPAL_REVERT_TPR:
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2619
  		ret = opal_reverttper(dev, p, false);
e225c20eb   Scott Bauer   Move stack parame...
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
  		break;
  	case IOC_OPAL_LR_SETUP:
  		ret = opal_setup_locking_range(dev, p);
  		break;
  	case IOC_OPAL_ADD_USR_TO_LR:
  		ret = opal_add_user_to_lr(dev, p);
  		break;
  	case IOC_OPAL_ENABLE_DISABLE_MBR:
  		ret = opal_enable_disable_shadow_mbr(dev, p);
  		break;
c98884434   Jonas Rabenstein   block: sed-opal: ...
2630
2631
2632
  	case IOC_OPAL_MBR_DONE:
  		ret = opal_set_mbr_done(dev, p);
  		break;
a9b25b4cf   Jonas Rabenstein   block: sed-opal: ...
2633
2634
2635
  	case IOC_OPAL_WRITE_SHADOW_MBR:
  		ret = opal_write_shadow_mbr(dev, p);
  		break;
e225c20eb   Scott Bauer   Move stack parame...
2636
2637
2638
2639
2640
2641
  	case IOC_OPAL_ERASE_LR:
  		ret = opal_erase_locking_range(dev, p);
  		break;
  	case IOC_OPAL_SECURE_ERASE_LR:
  		ret = opal_secure_erase_locking_range(dev, p);
  		break;
5e4c7cf60   Revanth Rajashekar   block: sed-opal: ...
2642
2643
2644
  	case IOC_OPAL_PSID_REVERT_TPR:
  		ret = opal_reverttper(dev, p, true);
  		break;
51f421c85   Revanth Rajashekar   block: sed-opal: ...
2645
2646
2647
  	case IOC_OPAL_GENERIC_TABLE_RW:
  		ret = opal_generic_read_write_table(dev, p);
  		break;
455a7b238   Scott Bauer   block: Add Sed-op...
2648
  	default:
591c59d18   Scott Bauer   block: sed-opal: ...
2649
  		break;
455a7b238   Scott Bauer   block: Add Sed-op...
2650
  	}
e225c20eb   Scott Bauer   Move stack parame...
2651
2652
2653
  
  	kfree(p);
  	return ret;
455a7b238   Scott Bauer   block: Add Sed-op...
2654
2655
  }
  EXPORT_SYMBOL_GPL(sed_ioctl);