GNU Linux-libre 5.10.217-gnu1
[releases.git] / block / sed-opal.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2016 Intel Corporation
4  *
5  * Authors:
6  *    Scott  Bauer      <scott.bauer@intel.com>
7  *    Rafael Antognolli <rafael.antognolli@intel.com>
8  */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ":OPAL: " fmt
11
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/genhd.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <uapi/linux/sed-opal.h>
20 #include <linux/sed-opal.h>
21 #include <linux/string.h>
22 #include <linux/kdev_t.h>
23
24 #include "opal_proto.h"
25
26 #define IO_BUFFER_LENGTH 2048
27 #define MAX_TOKS 64
28
29 /* Number of bytes needed by cmd_finalize. */
30 #define CMD_FINALIZE_BYTES_NEEDED 7
31
32 struct opal_step {
33         int (*fn)(struct opal_dev *dev, void *data);
34         void *data;
35 };
36 typedef int (cont_fn)(struct opal_dev *dev);
37
38 enum opal_atom_width {
39         OPAL_WIDTH_TINY,
40         OPAL_WIDTH_SHORT,
41         OPAL_WIDTH_MEDIUM,
42         OPAL_WIDTH_LONG,
43         OPAL_WIDTH_TOKEN
44 };
45
46 /*
47  * On the parsed response, we don't store again the toks that are already
48  * stored in the response buffer. Instead, for each token, we just store a
49  * pointer to the position in the buffer where the token starts, and the size
50  * of the token in bytes.
51  */
52 struct opal_resp_tok {
53         const u8 *pos;
54         size_t len;
55         enum opal_response_token type;
56         enum opal_atom_width width;
57         union {
58                 u64 u;
59                 s64 s;
60         } stored;
61 };
62
63 /*
64  * From the response header it's not possible to know how many tokens there are
65  * on the payload. So we hardcode that the maximum will be MAX_TOKS, and later
66  * if we start dealing with messages that have more than that, we can increase
67  * this number. This is done to avoid having to make two passes through the
68  * response, the first one counting how many tokens we have and the second one
69  * actually storing the positions.
70  */
71 struct parsed_resp {
72         int num;
73         struct opal_resp_tok toks[MAX_TOKS];
74 };
75
76 struct opal_dev {
77         bool supported;
78         bool mbr_enabled;
79
80         void *data;
81         sec_send_recv *send_recv;
82
83         struct mutex dev_lock;
84         u16 comid;
85         u32 hsn;
86         u32 tsn;
87         u64 align;
88         u64 lowest_lba;
89
90         size_t pos;
91         u8 *cmd;
92         u8 *resp;
93
94         struct parsed_resp parsed;
95         size_t prev_d_len;
96         void *prev_data;
97
98         struct list_head unlk_lst;
99 };
100
101
102 static const u8 opaluid[][OPAL_UID_LENGTH] = {
103         /* users */
104         [OPAL_SMUID_UID] =
105                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
106         [OPAL_THISSP_UID] =
107                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
108         [OPAL_ADMINSP_UID] =
109                 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
110         [OPAL_LOCKINGSP_UID] =
111                 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
112         [OPAL_ENTERPRISE_LOCKINGSP_UID] =
113                 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
114         [OPAL_ANYBODY_UID] =
115                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
116         [OPAL_SID_UID] =
117                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
118         [OPAL_ADMIN1_UID] =
119                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
120         [OPAL_USER1_UID] =
121                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
122         [OPAL_USER2_UID] =
123                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
124         [OPAL_PSID_UID] =
125                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
126         [OPAL_ENTERPRISE_BANDMASTER0_UID] =
127                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
128         [OPAL_ENTERPRISE_ERASEMASTER_UID] =
129                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
130
131         /* tables */
132         [OPAL_TABLE_TABLE] =
133                 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01 },
134         [OPAL_LOCKINGRANGE_GLOBAL] =
135                 { 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
136         [OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
137                 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
138         [OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
139                 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
140         [OPAL_MBRCONTROL] =
141                 { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
142         [OPAL_MBR] =
143                 { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
144         [OPAL_AUTHORITY_TABLE] =
145                 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
146         [OPAL_C_PIN_TABLE] =
147                 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
148         [OPAL_LOCKING_INFO_TABLE] =
149                 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
150         [OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
151                 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
152         [OPAL_DATASTORE] =
153                 { 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00 },
154
155         /* C_PIN_TABLE object ID's */
156         [OPAL_C_PIN_MSID] =
157                 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
158         [OPAL_C_PIN_SID] =
159                 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
160         [OPAL_C_PIN_ADMIN1] =
161                 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
162
163         /* half UID's (only first 4 bytes used) */
164         [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
165                 { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
166         [OPAL_HALF_UID_BOOLEAN_ACE] =
167                 { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
168
169         /* special value for omitted optional parameter */
170         [OPAL_UID_HEXFF] =
171                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
172 };
173
174 /*
175  * TCG Storage SSC Methods.
176  * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
177  * Section: 6.3 Assigned UIDs
178  */
179 static const u8 opalmethod[][OPAL_METHOD_LENGTH] = {
180         [OPAL_PROPERTIES] =
181                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
182         [OPAL_STARTSESSION] =
183                 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
184         [OPAL_REVERT] =
185                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
186         [OPAL_ACTIVATE] =
187                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
188         [OPAL_EGET] =
189                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
190         [OPAL_ESET] =
191                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
192         [OPAL_NEXT] =
193                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
194         [OPAL_EAUTHENTICATE] =
195                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
196         [OPAL_GETACL] =
197                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
198         [OPAL_GENKEY] =
199                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
200         [OPAL_REVERTSP] =
201                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
202         [OPAL_GET] =
203                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
204         [OPAL_SET] =
205                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
206         [OPAL_AUTHENTICATE] =
207                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
208         [OPAL_RANDOM] =
209                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
210         [OPAL_ERASE] =
211                 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
212 };
213
214 static int end_opal_session_error(struct opal_dev *dev);
215 static int opal_discovery0_step(struct opal_dev *dev);
216
217 struct opal_suspend_data {
218         struct opal_lock_unlock unlk;
219         u8 lr;
220         struct list_head node;
221 };
222
223 /*
224  * Derived from:
225  * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
226  * Section: 5.1.5 Method Status Codes
227  */
228 static const char * const opal_errors[] = {
229         "Success",
230         "Not Authorized",
231         "Unknown Error",
232         "SP Busy",
233         "SP Failed",
234         "SP Disabled",
235         "SP Frozen",
236         "No Sessions Available",
237         "Uniqueness Conflict",
238         "Insufficient Space",
239         "Insufficient Rows",
240         "Invalid Function",
241         "Invalid Parameter",
242         "Invalid Reference",
243         "Unknown Error",
244         "TPER Malfunction",
245         "Transaction Failure",
246         "Response Overflow",
247         "Authority Locked Out",
248 };
249
250 static const char *opal_error_to_human(int error)
251 {
252         if (error == 0x3f)
253                 return "Failed";
254
255         if (error >= ARRAY_SIZE(opal_errors) || error < 0)
256                 return "Unknown Error";
257
258         return opal_errors[error];
259 }
260
261 static void print_buffer(const u8 *ptr, u32 length)
262 {
263 #ifdef DEBUG
264         print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
265         pr_debug("\n");
266 #endif
267 }
268
269 static bool check_tper(const void *data)
270 {
271         const struct d0_tper_features *tper = data;
272         u8 flags = tper->supported_features;
273
274         if (!(flags & TPER_SYNC_SUPPORTED)) {
275                 pr_debug("TPer sync not supported. flags = %d\n",
276                          tper->supported_features);
277                 return false;
278         }
279
280         return true;
281 }
282
283 static bool check_mbrenabled(const void *data)
284 {
285         const struct d0_locking_features *lfeat = data;
286         u8 sup_feat = lfeat->supported_features;
287
288         return !!(sup_feat & MBR_ENABLED_MASK);
289 }
290
291 static bool check_sum(const void *data)
292 {
293         const struct d0_single_user_mode *sum = data;
294         u32 nlo = be32_to_cpu(sum->num_locking_objects);
295
296         if (nlo == 0) {
297                 pr_debug("Need at least one locking object.\n");
298                 return false;
299         }
300
301         pr_debug("Number of locking objects: %d\n", nlo);
302
303         return true;
304 }
305
306 static u16 get_comid_v100(const void *data)
307 {
308         const struct d0_opal_v100 *v100 = data;
309
310         return be16_to_cpu(v100->baseComID);
311 }
312
313 static u16 get_comid_v200(const void *data)
314 {
315         const struct d0_opal_v200 *v200 = data;
316
317         return be16_to_cpu(v200->baseComID);
318 }
319
320 static int opal_send_cmd(struct opal_dev *dev)
321 {
322         return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
323                               dev->cmd, IO_BUFFER_LENGTH,
324                               true);
325 }
326
327 static int opal_recv_cmd(struct opal_dev *dev)
328 {
329         return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
330                               dev->resp, IO_BUFFER_LENGTH,
331                               false);
332 }
333
334 static int opal_recv_check(struct opal_dev *dev)
335 {
336         size_t buflen = IO_BUFFER_LENGTH;
337         void *buffer = dev->resp;
338         struct opal_header *hdr = buffer;
339         int ret;
340
341         do {
342                 pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
343                          hdr->cp.outstandingData,
344                          hdr->cp.minTransfer);
345
346                 if (hdr->cp.outstandingData == 0 ||
347                     hdr->cp.minTransfer != 0)
348                         return 0;
349
350                 memset(buffer, 0, buflen);
351                 ret = opal_recv_cmd(dev);
352         } while (!ret);
353
354         return ret;
355 }
356
357 static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
358 {
359         int ret;
360
361         ret = opal_send_cmd(dev);
362         if (ret)
363                 return ret;
364         ret = opal_recv_cmd(dev);
365         if (ret)
366                 return ret;
367         ret = opal_recv_check(dev);
368         if (ret)
369                 return ret;
370         return cont(dev);
371 }
372
373 static void check_geometry(struct opal_dev *dev, const void *data)
374 {
375         const struct d0_geometry_features *geo = data;
376
377         dev->align = be64_to_cpu(geo->alignment_granularity);
378         dev->lowest_lba = be64_to_cpu(geo->lowest_aligned_lba);
379 }
380
381 static int execute_step(struct opal_dev *dev,
382                         const struct opal_step *step, size_t stepIndex)
383 {
384         int error = step->fn(dev, step->data);
385
386         if (error) {
387                 pr_debug("Step %zu (%pS) failed with error %d: %s\n",
388                          stepIndex, step->fn, error,
389                          opal_error_to_human(error));
390         }
391
392         return error;
393 }
394
395 static int execute_steps(struct opal_dev *dev,
396                          const struct opal_step *steps, size_t n_steps)
397 {
398         size_t state = 0;
399         int error;
400
401         /* first do a discovery0 */
402         error = opal_discovery0_step(dev);
403         if (error)
404                 return error;
405
406         for (state = 0; state < n_steps; state++) {
407                 error = execute_step(dev, &steps[state], state);
408                 if (error)
409                         goto out_error;
410         }
411
412         return 0;
413
414 out_error:
415         /*
416          * For each OPAL command the first step in steps starts some sort of
417          * session. If an error occurred in the initial discovery0 or if an
418          * error occurred in the first step (and thus stopping the loop with
419          * state == 0) then there was an error before or during the attempt to
420          * start a session. Therefore we shouldn't attempt to terminate a
421          * session, as one has not yet been created.
422          */
423         if (state > 0)
424                 end_opal_session_error(dev);
425
426         return error;
427 }
428
429 static int opal_discovery0_end(struct opal_dev *dev)
430 {
431         bool found_com_id = false, supported = true, single_user = false;
432         const struct d0_header *hdr = (struct d0_header *)dev->resp;
433         const u8 *epos = dev->resp, *cpos = dev->resp;
434         u16 comid = 0;
435         u32 hlen = be32_to_cpu(hdr->length);
436
437         print_buffer(dev->resp, hlen);
438         dev->mbr_enabled = false;
439
440         if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
441                 pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
442                          sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
443                 return -EFAULT;
444         }
445
446         epos += hlen; /* end of buffer */
447         cpos += sizeof(*hdr); /* current position on buffer */
448
449         while (cpos < epos && supported) {
450                 const struct d0_features *body =
451                         (const struct d0_features *)cpos;
452
453                 switch (be16_to_cpu(body->code)) {
454                 case FC_TPER:
455                         supported = check_tper(body->features);
456                         break;
457                 case FC_SINGLEUSER:
458                         single_user = check_sum(body->features);
459                         break;
460                 case FC_GEOMETRY:
461                         check_geometry(dev, body);
462                         break;
463                 case FC_LOCKING:
464                         dev->mbr_enabled = check_mbrenabled(body->features);
465                         break;
466                 case FC_ENTERPRISE:
467                 case FC_DATASTORE:
468                         /* some ignored properties */
469                         pr_debug("Found OPAL feature description: %d\n",
470                                  be16_to_cpu(body->code));
471                         break;
472                 case FC_OPALV100:
473                         comid = get_comid_v100(body->features);
474                         found_com_id = true;
475                         break;
476                 case FC_OPALV200:
477                         comid = get_comid_v200(body->features);
478                         found_com_id = true;
479                         break;
480                 case 0xbfff ... 0xffff:
481                         /* vendor specific, just ignore */
482                         break;
483                 default:
484                         pr_debug("OPAL Unknown feature: %d\n",
485                                  be16_to_cpu(body->code));
486
487                 }
488                 cpos += body->length + 4;
489         }
490
491         if (!supported) {
492                 pr_debug("This device is not Opal enabled. Not Supported!\n");
493                 return -EOPNOTSUPP;
494         }
495
496         if (!single_user)
497                 pr_debug("Device doesn't support single user mode\n");
498
499
500         if (!found_com_id) {
501                 pr_debug("Could not find OPAL comid for device. Returning early\n");
502                 return -EOPNOTSUPP;
503         }
504
505         dev->comid = comid;
506
507         return 0;
508 }
509
510 static int opal_discovery0(struct opal_dev *dev, void *data)
511 {
512         int ret;
513
514         memset(dev->resp, 0, IO_BUFFER_LENGTH);
515         dev->comid = OPAL_DISCOVERY_COMID;
516         ret = opal_recv_cmd(dev);
517         if (ret)
518                 return ret;
519
520         return opal_discovery0_end(dev);
521 }
522
523 static int opal_discovery0_step(struct opal_dev *dev)
524 {
525         const struct opal_step discovery0_step = {
526                 opal_discovery0,
527         };
528
529         return execute_step(dev, &discovery0_step, 0);
530 }
531
532 static size_t remaining_size(struct opal_dev *cmd)
533 {
534         return IO_BUFFER_LENGTH - cmd->pos;
535 }
536
537 static bool can_add(int *err, struct opal_dev *cmd, size_t len)
538 {
539         if (*err)
540                 return false;
541
542         if (remaining_size(cmd) < len) {
543                 pr_debug("Error adding %zu bytes: end of buffer.\n", len);
544                 *err = -ERANGE;
545                 return false;
546         }
547
548         return true;
549 }
550
551 static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
552 {
553         if (!can_add(err, cmd, 1))
554                 return;
555
556         cmd->cmd[cmd->pos++] = tok;
557 }
558
559 static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
560                                   bool has_sign, int len)
561 {
562         u8 atom;
563         int err = 0;
564
565         atom = SHORT_ATOM_ID;
566         atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
567         atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
568         atom |= len & SHORT_ATOM_LEN_MASK;
569
570         add_token_u8(&err, cmd, atom);
571 }
572
573 static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
574                                    bool has_sign, int len)
575 {
576         u8 header0;
577
578         header0 = MEDIUM_ATOM_ID;
579         header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
580         header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
581         header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
582
583         cmd->cmd[cmd->pos++] = header0;
584         cmd->cmd[cmd->pos++] = len;
585 }
586
587 static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
588 {
589         size_t len;
590         int msb;
591
592         if (!(number & ~TINY_ATOM_DATA_MASK)) {
593                 add_token_u8(err, cmd, number);
594                 return;
595         }
596
597         msb = fls64(number);
598         len = DIV_ROUND_UP(msb, 8);
599
600         if (!can_add(err, cmd, len + 1)) {
601                 pr_debug("Error adding u64: end of buffer.\n");
602                 return;
603         }
604         add_short_atom_header(cmd, false, false, len);
605         while (len--)
606                 add_token_u8(err, cmd, number >> (len * 8));
607 }
608
609 static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len)
610 {
611         size_t header_len = 1;
612         bool is_short_atom = true;
613
614         if (len & ~SHORT_ATOM_LEN_MASK) {
615                 header_len = 2;
616                 is_short_atom = false;
617         }
618
619         if (!can_add(err, cmd, header_len + len)) {
620                 pr_debug("Error adding bytestring: end of buffer.\n");
621                 return NULL;
622         }
623
624         if (is_short_atom)
625                 add_short_atom_header(cmd, true, false, len);
626         else
627                 add_medium_atom_header(cmd, true, false, len);
628
629         return &cmd->cmd[cmd->pos];
630 }
631
632 static void add_token_bytestring(int *err, struct opal_dev *cmd,
633                                  const u8 *bytestring, size_t len)
634 {
635         u8 *start;
636
637         start = add_bytestring_header(err, cmd, len);
638         if (!start)
639                 return;
640         memcpy(start, bytestring, len);
641         cmd->pos += len;
642 }
643
644 static int build_locking_range(u8 *buffer, size_t length, u8 lr)
645 {
646         if (length > OPAL_UID_LENGTH) {
647                 pr_debug("Can't build locking range. Length OOB\n");
648                 return -ERANGE;
649         }
650
651         memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
652
653         if (lr == 0)
654                 return 0;
655
656         buffer[5] = LOCKING_RANGE_NON_GLOBAL;
657         buffer[7] = lr;
658
659         return 0;
660 }
661
662 static int build_locking_user(u8 *buffer, size_t length, u8 lr)
663 {
664         if (length > OPAL_UID_LENGTH) {
665                 pr_debug("Can't build locking range user. Length OOB\n");
666                 return -ERANGE;
667         }
668
669         memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
670
671         buffer[7] = lr + 1;
672
673         return 0;
674 }
675
676 static void set_comid(struct opal_dev *cmd, u16 comid)
677 {
678         struct opal_header *hdr = (struct opal_header *)cmd->cmd;
679
680         hdr->cp.extendedComID[0] = comid >> 8;
681         hdr->cp.extendedComID[1] = comid;
682         hdr->cp.extendedComID[2] = 0;
683         hdr->cp.extendedComID[3] = 0;
684 }
685
686 static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
687 {
688         struct opal_header *hdr;
689         int err = 0;
690
691         /*
692          * Close the parameter list opened from cmd_start.
693          * The number of bytes added must be equal to
694          * CMD_FINALIZE_BYTES_NEEDED.
695          */
696         add_token_u8(&err, cmd, OPAL_ENDLIST);
697
698         add_token_u8(&err, cmd, OPAL_ENDOFDATA);
699         add_token_u8(&err, cmd, OPAL_STARTLIST);
700         add_token_u8(&err, cmd, 0);
701         add_token_u8(&err, cmd, 0);
702         add_token_u8(&err, cmd, 0);
703         add_token_u8(&err, cmd, OPAL_ENDLIST);
704
705         if (err) {
706                 pr_debug("Error finalizing command.\n");
707                 return -EFAULT;
708         }
709
710         hdr = (struct opal_header *) cmd->cmd;
711
712         hdr->pkt.tsn = cpu_to_be32(tsn);
713         hdr->pkt.hsn = cpu_to_be32(hsn);
714
715         hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
716         while (cmd->pos % 4) {
717                 if (cmd->pos >= IO_BUFFER_LENGTH) {
718                         pr_debug("Error: Buffer overrun\n");
719                         return -ERANGE;
720                 }
721                 cmd->cmd[cmd->pos++] = 0;
722         }
723         hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
724                                       sizeof(hdr->pkt));
725         hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
726
727         return 0;
728 }
729
730 static const struct opal_resp_tok *response_get_token(
731                                 const struct parsed_resp *resp,
732                                 int n)
733 {
734         const struct opal_resp_tok *tok;
735
736         if (!resp) {
737                 pr_debug("Response is NULL\n");
738                 return ERR_PTR(-EINVAL);
739         }
740
741         if (n >= resp->num) {
742                 pr_debug("Token number doesn't exist: %d, resp: %d\n",
743                          n, resp->num);
744                 return ERR_PTR(-EINVAL);
745         }
746
747         tok = &resp->toks[n];
748         if (tok->len == 0) {
749                 pr_debug("Token length must be non-zero\n");
750                 return ERR_PTR(-EINVAL);
751         }
752
753         return tok;
754 }
755
756 static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
757                                    const u8 *pos)
758 {
759         tok->pos = pos;
760         tok->len = 1;
761         tok->width = OPAL_WIDTH_TINY;
762
763         if (pos[0] & TINY_ATOM_SIGNED) {
764                 tok->type = OPAL_DTA_TOKENID_SINT;
765         } else {
766                 tok->type = OPAL_DTA_TOKENID_UINT;
767                 tok->stored.u = pos[0] & 0x3f;
768         }
769
770         return tok->len;
771 }
772
773 static ssize_t response_parse_short(struct opal_resp_tok *tok,
774                                     const u8 *pos)
775 {
776         tok->pos = pos;
777         tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
778         tok->width = OPAL_WIDTH_SHORT;
779
780         if (pos[0] & SHORT_ATOM_BYTESTRING) {
781                 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
782         } else if (pos[0] & SHORT_ATOM_SIGNED) {
783                 tok->type = OPAL_DTA_TOKENID_SINT;
784         } else {
785                 u64 u_integer = 0;
786                 ssize_t i, b = 0;
787
788                 tok->type = OPAL_DTA_TOKENID_UINT;
789                 if (tok->len > 9) {
790                         pr_debug("uint64 with more than 8 bytes\n");
791                         return -EINVAL;
792                 }
793                 for (i = tok->len - 1; i > 0; i--) {
794                         u_integer |= ((u64)pos[i] << (8 * b));
795                         b++;
796                 }
797                 tok->stored.u = u_integer;
798         }
799
800         return tok->len;
801 }
802
803 static ssize_t response_parse_medium(struct opal_resp_tok *tok,
804                                      const u8 *pos)
805 {
806         tok->pos = pos;
807         tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
808         tok->width = OPAL_WIDTH_MEDIUM;
809
810         if (pos[0] & MEDIUM_ATOM_BYTESTRING)
811                 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
812         else if (pos[0] & MEDIUM_ATOM_SIGNED)
813                 tok->type = OPAL_DTA_TOKENID_SINT;
814         else
815                 tok->type = OPAL_DTA_TOKENID_UINT;
816
817         return tok->len;
818 }
819
820 static ssize_t response_parse_long(struct opal_resp_tok *tok,
821                                    const u8 *pos)
822 {
823         tok->pos = pos;
824         tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
825         tok->width = OPAL_WIDTH_LONG;
826
827         if (pos[0] & LONG_ATOM_BYTESTRING)
828                 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
829         else if (pos[0] & LONG_ATOM_SIGNED)
830                 tok->type = OPAL_DTA_TOKENID_SINT;
831         else
832                 tok->type = OPAL_DTA_TOKENID_UINT;
833
834         return tok->len;
835 }
836
837 static ssize_t response_parse_token(struct opal_resp_tok *tok,
838                                     const u8 *pos)
839 {
840         tok->pos = pos;
841         tok->len = 1;
842         tok->type = OPAL_DTA_TOKENID_TOKEN;
843         tok->width = OPAL_WIDTH_TOKEN;
844
845         return tok->len;
846 }
847
848 static int response_parse(const u8 *buf, size_t length,
849                           struct parsed_resp *resp)
850 {
851         const struct opal_header *hdr;
852         struct opal_resp_tok *iter;
853         int num_entries = 0;
854         int total;
855         ssize_t token_length;
856         const u8 *pos;
857         u32 clen, plen, slen;
858
859         if (!buf)
860                 return -EFAULT;
861
862         if (!resp)
863                 return -EFAULT;
864
865         hdr = (struct opal_header *)buf;
866         pos = buf;
867         pos += sizeof(*hdr);
868
869         clen = be32_to_cpu(hdr->cp.length);
870         plen = be32_to_cpu(hdr->pkt.length);
871         slen = be32_to_cpu(hdr->subpkt.length);
872         pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
873                  clen, plen, slen);
874
875         if (clen == 0 || plen == 0 || slen == 0 ||
876             slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
877                 pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
878                          clen, plen, slen);
879                 print_buffer(pos, sizeof(*hdr));
880                 return -EINVAL;
881         }
882
883         if (pos > buf + length)
884                 return -EFAULT;
885
886         iter = resp->toks;
887         total = slen;
888         print_buffer(pos, total);
889         while (total > 0) {
890                 if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
891                         token_length = response_parse_tiny(iter, pos);
892                 else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
893                         token_length = response_parse_short(iter, pos);
894                 else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
895                         token_length = response_parse_medium(iter, pos);
896                 else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
897                         token_length = response_parse_long(iter, pos);
898                 else if (pos[0] == EMPTY_ATOM_BYTE) /* empty atom */
899                         token_length = 1;
900                 else /* TOKEN */
901                         token_length = response_parse_token(iter, pos);
902
903                 if (token_length < 0)
904                         return token_length;
905
906                 if (pos[0] != EMPTY_ATOM_BYTE)
907                         num_entries++;
908
909                 pos += token_length;
910                 total -= token_length;
911                 iter++;
912         }
913
914         resp->num = num_entries;
915
916         return 0;
917 }
918
919 static size_t response_get_string(const struct parsed_resp *resp, int n,
920                                   const char **store)
921 {
922         u8 skip;
923         const struct opal_resp_tok *tok;
924
925         *store = NULL;
926         tok = response_get_token(resp, n);
927         if (IS_ERR(tok))
928                 return 0;
929
930         if (tok->type != OPAL_DTA_TOKENID_BYTESTRING) {
931                 pr_debug("Token is not a byte string!\n");
932                 return 0;
933         }
934
935         switch (tok->width) {
936         case OPAL_WIDTH_TINY:
937         case OPAL_WIDTH_SHORT:
938                 skip = 1;
939                 break;
940         case OPAL_WIDTH_MEDIUM:
941                 skip = 2;
942                 break;
943         case OPAL_WIDTH_LONG:
944                 skip = 4;
945                 break;
946         default:
947                 pr_debug("Token has invalid width!\n");
948                 return 0;
949         }
950
951         *store = tok->pos + skip;
952
953         return tok->len - skip;
954 }
955
956 static u64 response_get_u64(const struct parsed_resp *resp, int n)
957 {
958         const struct opal_resp_tok *tok;
959
960         tok = response_get_token(resp, n);
961         if (IS_ERR(tok))
962                 return 0;
963
964         if (tok->type != OPAL_DTA_TOKENID_UINT) {
965                 pr_debug("Token is not unsigned int: %d\n", tok->type);
966                 return 0;
967         }
968
969         if (tok->width != OPAL_WIDTH_TINY && tok->width != OPAL_WIDTH_SHORT) {
970                 pr_debug("Atom is not short or tiny: %d\n", tok->width);
971                 return 0;
972         }
973
974         return tok->stored.u;
975 }
976
977 static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
978 {
979         if (IS_ERR(token) ||
980             token->type != OPAL_DTA_TOKENID_TOKEN ||
981             token->pos[0] != match)
982                 return false;
983         return true;
984 }
985
986 static u8 response_status(const struct parsed_resp *resp)
987 {
988         const struct opal_resp_tok *tok;
989
990         tok = response_get_token(resp, 0);
991         if (response_token_matches(tok, OPAL_ENDOFSESSION))
992                 return 0;
993
994         if (resp->num < 5)
995                 return DTAERROR_NO_METHOD_STATUS;
996
997         tok = response_get_token(resp, resp->num - 5);
998         if (!response_token_matches(tok, OPAL_STARTLIST))
999                 return DTAERROR_NO_METHOD_STATUS;
1000
1001         tok = response_get_token(resp, resp->num - 1);
1002         if (!response_token_matches(tok, OPAL_ENDLIST))
1003                 return DTAERROR_NO_METHOD_STATUS;
1004
1005         return response_get_u64(resp, resp->num - 4);
1006 }
1007
1008 /* Parses and checks for errors */
1009 static int parse_and_check_status(struct opal_dev *dev)
1010 {
1011         int error;
1012
1013         print_buffer(dev->cmd, dev->pos);
1014
1015         error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
1016         if (error) {
1017                 pr_debug("Couldn't parse response.\n");
1018                 return error;
1019         }
1020
1021         return response_status(&dev->parsed);
1022 }
1023
1024 static void clear_opal_cmd(struct opal_dev *dev)
1025 {
1026         dev->pos = sizeof(struct opal_header);
1027         memset(dev->cmd, 0, IO_BUFFER_LENGTH);
1028 }
1029
1030 static int cmd_start(struct opal_dev *dev, const u8 *uid, const u8 *method)
1031 {
1032         int err = 0;
1033
1034         clear_opal_cmd(dev);
1035         set_comid(dev, dev->comid);
1036
1037         add_token_u8(&err, dev, OPAL_CALL);
1038         add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
1039         add_token_bytestring(&err, dev, method, OPAL_METHOD_LENGTH);
1040
1041         /*
1042          * Every method call is followed by its parameters enclosed within
1043          * OPAL_STARTLIST and OPAL_ENDLIST tokens. We automatically open the
1044          * parameter list here and close it later in cmd_finalize.
1045          */
1046         add_token_u8(&err, dev, OPAL_STARTLIST);
1047
1048         return err;
1049 }
1050
1051 static int start_opal_session_cont(struct opal_dev *dev)
1052 {
1053         u32 hsn, tsn;
1054         int error = 0;
1055
1056         error = parse_and_check_status(dev);
1057         if (error)
1058                 return error;
1059
1060         hsn = response_get_u64(&dev->parsed, 4);
1061         tsn = response_get_u64(&dev->parsed, 5);
1062
1063         if (hsn != GENERIC_HOST_SESSION_NUM || tsn < FIRST_TPER_SESSION_NUM) {
1064                 pr_debug("Couldn't authenticate session\n");
1065                 return -EPERM;
1066         }
1067
1068         dev->hsn = hsn;
1069         dev->tsn = tsn;
1070
1071         return 0;
1072 }
1073
1074 static void add_suspend_info(struct opal_dev *dev,
1075                              struct opal_suspend_data *sus)
1076 {
1077         struct opal_suspend_data *iter;
1078
1079         list_for_each_entry(iter, &dev->unlk_lst, node) {
1080                 if (iter->lr == sus->lr) {
1081                         list_del(&iter->node);
1082                         kfree(iter);
1083                         break;
1084                 }
1085         }
1086         list_add_tail(&sus->node, &dev->unlk_lst);
1087 }
1088
1089 static int end_session_cont(struct opal_dev *dev)
1090 {
1091         dev->hsn = 0;
1092         dev->tsn = 0;
1093
1094         return parse_and_check_status(dev);
1095 }
1096
1097 static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
1098 {
1099         int ret;
1100
1101         ret = cmd_finalize(dev, dev->hsn, dev->tsn);
1102         if (ret) {
1103                 pr_debug("Error finalizing command buffer: %d\n", ret);
1104                 return ret;
1105         }
1106
1107         print_buffer(dev->cmd, dev->pos);
1108
1109         return opal_send_recv(dev, cont);
1110 }
1111
1112 /*
1113  * request @column from table @table on device @dev. On success, the column
1114  * data will be available in dev->resp->tok[4]
1115  */
1116 static int generic_get_column(struct opal_dev *dev, const u8 *table,
1117                               u64 column)
1118 {
1119         int err;
1120
1121         err = cmd_start(dev, table, opalmethod[OPAL_GET]);
1122
1123         add_token_u8(&err, dev, OPAL_STARTLIST);
1124
1125         add_token_u8(&err, dev, OPAL_STARTNAME);
1126         add_token_u8(&err, dev, OPAL_STARTCOLUMN);
1127         add_token_u64(&err, dev, column);
1128         add_token_u8(&err, dev, OPAL_ENDNAME);
1129
1130         add_token_u8(&err, dev, OPAL_STARTNAME);
1131         add_token_u8(&err, dev, OPAL_ENDCOLUMN);
1132         add_token_u64(&err, dev, column);
1133         add_token_u8(&err, dev, OPAL_ENDNAME);
1134
1135         add_token_u8(&err, dev, OPAL_ENDLIST);
1136
1137         if (err)
1138                 return err;
1139
1140         return finalize_and_send(dev, parse_and_check_status);
1141 }
1142
1143 /*
1144  * see TCG SAS 5.3.2.3 for a description of the available columns
1145  *
1146  * the result is provided in dev->resp->tok[4]
1147  */
1148 static int generic_get_table_info(struct opal_dev *dev, const u8 *table_uid,
1149                                   u64 column)
1150 {
1151         u8 uid[OPAL_UID_LENGTH];
1152         const unsigned int half = OPAL_UID_LENGTH_HALF;
1153
1154         /* sed-opal UIDs can be split in two halves:
1155          *  first:  actual table index
1156          *  second: relative index in the table
1157          * so we have to get the first half of the OPAL_TABLE_TABLE and use the
1158          * first part of the target table as relative index into that table
1159          */
1160         memcpy(uid, opaluid[OPAL_TABLE_TABLE], half);
1161         memcpy(uid + half, table_uid, half);
1162
1163         return generic_get_column(dev, uid, column);
1164 }
1165
1166 static int gen_key(struct opal_dev *dev, void *data)
1167 {
1168         u8 uid[OPAL_UID_LENGTH];
1169         int err;
1170
1171         memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
1172         kfree(dev->prev_data);
1173         dev->prev_data = NULL;
1174
1175         err = cmd_start(dev, uid, opalmethod[OPAL_GENKEY]);
1176
1177         if (err) {
1178                 pr_debug("Error building gen key command\n");
1179                 return err;
1180
1181         }
1182
1183         return finalize_and_send(dev, parse_and_check_status);
1184 }
1185
1186 static int get_active_key_cont(struct opal_dev *dev)
1187 {
1188         const char *activekey;
1189         size_t keylen;
1190         int error = 0;
1191
1192         error = parse_and_check_status(dev);
1193         if (error)
1194                 return error;
1195
1196         keylen = response_get_string(&dev->parsed, 4, &activekey);
1197         if (!activekey) {
1198                 pr_debug("%s: Couldn't extract the Activekey from the response\n",
1199                          __func__);
1200                 return OPAL_INVAL_PARAM;
1201         }
1202
1203         dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
1204
1205         if (!dev->prev_data)
1206                 return -ENOMEM;
1207
1208         dev->prev_d_len = keylen;
1209
1210         return 0;
1211 }
1212
1213 static int get_active_key(struct opal_dev *dev, void *data)
1214 {
1215         u8 uid[OPAL_UID_LENGTH];
1216         int err;
1217         u8 *lr = data;
1218
1219         err = build_locking_range(uid, sizeof(uid), *lr);
1220         if (err)
1221                 return err;
1222
1223         err = generic_get_column(dev, uid, OPAL_ACTIVEKEY);
1224         if (err)
1225                 return err;
1226
1227         return get_active_key_cont(dev);
1228 }
1229
1230 static int generic_table_write_data(struct opal_dev *dev, const u64 data,
1231                                     u64 offset, u64 size, const u8 *uid)
1232 {
1233         const u8 __user *src = (u8 __user *)(uintptr_t)data;
1234         u8 *dst;
1235         u64 len;
1236         size_t off = 0;
1237         int err;
1238
1239         /* do we fit in the available space? */
1240         err = generic_get_table_info(dev, uid, OPAL_TABLE_ROWS);
1241         if (err) {
1242                 pr_debug("Couldn't get the table size\n");
1243                 return err;
1244         }
1245
1246         len = response_get_u64(&dev->parsed, 4);
1247         if (size > len || offset > len - size) {
1248                 pr_debug("Does not fit in the table (%llu vs. %llu)\n",
1249                           offset + size, len);
1250                 return -ENOSPC;
1251         }
1252
1253         /* do the actual transmission(s) */
1254         while (off < size) {
1255                 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1256                 add_token_u8(&err, dev, OPAL_STARTNAME);
1257                 add_token_u8(&err, dev, OPAL_WHERE);
1258                 add_token_u64(&err, dev, offset + off);
1259                 add_token_u8(&err, dev, OPAL_ENDNAME);
1260
1261                 add_token_u8(&err, dev, OPAL_STARTNAME);
1262                 add_token_u8(&err, dev, OPAL_VALUES);
1263
1264                 /*
1265                  * The bytestring header is either 1 or 2 bytes, so assume 2.
1266                  * There also needs to be enough space to accommodate the
1267                  * trailing OPAL_ENDNAME (1 byte) and tokens added by
1268                  * cmd_finalize.
1269                  */
1270                 len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
1271                           (size_t)(size - off));
1272                 pr_debug("Write bytes %zu+%llu/%llu\n", off, len, size);
1273
1274                 dst = add_bytestring_header(&err, dev, len);
1275                 if (!dst)
1276                         break;
1277
1278                 if (copy_from_user(dst, src + off, len)) {
1279                         err = -EFAULT;
1280                         break;
1281                 }
1282
1283                 dev->pos += len;
1284
1285                 add_token_u8(&err, dev, OPAL_ENDNAME);
1286                 if (err)
1287                         break;
1288
1289                 err = finalize_and_send(dev, parse_and_check_status);
1290                 if (err)
1291                         break;
1292
1293                 off += len;
1294         }
1295
1296         return err;
1297 }
1298
1299 static int generic_lr_enable_disable(struct opal_dev *dev,
1300                                      u8 *uid, bool rle, bool wle,
1301                                      bool rl, bool wl)
1302 {
1303         int err;
1304
1305         err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1306
1307         add_token_u8(&err, dev, OPAL_STARTNAME);
1308         add_token_u8(&err, dev, OPAL_VALUES);
1309         add_token_u8(&err, dev, OPAL_STARTLIST);
1310
1311         add_token_u8(&err, dev, OPAL_STARTNAME);
1312         add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1313         add_token_u8(&err, dev, rle);
1314         add_token_u8(&err, dev, OPAL_ENDNAME);
1315
1316         add_token_u8(&err, dev, OPAL_STARTNAME);
1317         add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1318         add_token_u8(&err, dev, wle);
1319         add_token_u8(&err, dev, OPAL_ENDNAME);
1320
1321         add_token_u8(&err, dev, OPAL_STARTNAME);
1322         add_token_u8(&err, dev, OPAL_READLOCKED);
1323         add_token_u8(&err, dev, rl);
1324         add_token_u8(&err, dev, OPAL_ENDNAME);
1325
1326         add_token_u8(&err, dev, OPAL_STARTNAME);
1327         add_token_u8(&err, dev, OPAL_WRITELOCKED);
1328         add_token_u8(&err, dev, wl);
1329         add_token_u8(&err, dev, OPAL_ENDNAME);
1330
1331         add_token_u8(&err, dev, OPAL_ENDLIST);
1332         add_token_u8(&err, dev, OPAL_ENDNAME);
1333
1334         return err;
1335 }
1336
1337 static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
1338                                    struct opal_user_lr_setup *setup)
1339 {
1340         int err;
1341
1342         err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
1343                                         0, 0);
1344         if (err)
1345                 pr_debug("Failed to create enable global lr command\n");
1346
1347         return err;
1348 }
1349
1350 static int setup_locking_range(struct opal_dev *dev, void *data)
1351 {
1352         u8 uid[OPAL_UID_LENGTH];
1353         struct opal_user_lr_setup *setup = data;
1354         u8 lr;
1355         int err;
1356
1357         lr = setup->session.opal_key.lr;
1358         err = build_locking_range(uid, sizeof(uid), lr);
1359         if (err)
1360                 return err;
1361
1362         if (lr == 0)
1363                 err = enable_global_lr(dev, uid, setup);
1364         else {
1365                 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1366
1367                 add_token_u8(&err, dev, OPAL_STARTNAME);
1368                 add_token_u8(&err, dev, OPAL_VALUES);
1369                 add_token_u8(&err, dev, OPAL_STARTLIST);
1370
1371                 add_token_u8(&err, dev, OPAL_STARTNAME);
1372                 add_token_u8(&err, dev, OPAL_RANGESTART);
1373                 add_token_u64(&err, dev, setup->range_start);
1374                 add_token_u8(&err, dev, OPAL_ENDNAME);
1375
1376                 add_token_u8(&err, dev, OPAL_STARTNAME);
1377                 add_token_u8(&err, dev, OPAL_RANGELENGTH);
1378                 add_token_u64(&err, dev, setup->range_length);
1379                 add_token_u8(&err, dev, OPAL_ENDNAME);
1380
1381                 add_token_u8(&err, dev, OPAL_STARTNAME);
1382                 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1383                 add_token_u64(&err, dev, !!setup->RLE);
1384                 add_token_u8(&err, dev, OPAL_ENDNAME);
1385
1386                 add_token_u8(&err, dev, OPAL_STARTNAME);
1387                 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1388                 add_token_u64(&err, dev, !!setup->WLE);
1389                 add_token_u8(&err, dev, OPAL_ENDNAME);
1390
1391                 add_token_u8(&err, dev, OPAL_ENDLIST);
1392                 add_token_u8(&err, dev, OPAL_ENDNAME);
1393         }
1394         if (err) {
1395                 pr_debug("Error building Setup Locking range command.\n");
1396                 return err;
1397         }
1398
1399         return finalize_and_send(dev, parse_and_check_status);
1400 }
1401
1402 static int start_generic_opal_session(struct opal_dev *dev,
1403                                       enum opal_uid auth,
1404                                       enum opal_uid sp_type,
1405                                       const char *key,
1406                                       u8 key_len)
1407 {
1408         u32 hsn;
1409         int err;
1410
1411         if (key == NULL && auth != OPAL_ANYBODY_UID)
1412                 return OPAL_INVAL_PARAM;
1413
1414         hsn = GENERIC_HOST_SESSION_NUM;
1415         err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1416                         opalmethod[OPAL_STARTSESSION]);
1417
1418         add_token_u64(&err, dev, hsn);
1419         add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
1420         add_token_u8(&err, dev, 1);
1421
1422         switch (auth) {
1423         case OPAL_ANYBODY_UID:
1424                 break;
1425         case OPAL_ADMIN1_UID:
1426         case OPAL_SID_UID:
1427         case OPAL_PSID_UID:
1428                 add_token_u8(&err, dev, OPAL_STARTNAME);
1429                 add_token_u8(&err, dev, 0); /* HostChallenge */
1430                 add_token_bytestring(&err, dev, key, key_len);
1431                 add_token_u8(&err, dev, OPAL_ENDNAME);
1432                 add_token_u8(&err, dev, OPAL_STARTNAME);
1433                 add_token_u8(&err, dev, 3); /* HostSignAuth */
1434                 add_token_bytestring(&err, dev, opaluid[auth],
1435                                      OPAL_UID_LENGTH);
1436                 add_token_u8(&err, dev, OPAL_ENDNAME);
1437                 break;
1438         default:
1439                 pr_debug("Cannot start Admin SP session with auth %d\n", auth);
1440                 return OPAL_INVAL_PARAM;
1441         }
1442
1443         if (err) {
1444                 pr_debug("Error building start adminsp session command.\n");
1445                 return err;
1446         }
1447
1448         return finalize_and_send(dev, start_opal_session_cont);
1449 }
1450
1451 static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
1452 {
1453         return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
1454                                           OPAL_ADMINSP_UID, NULL, 0);
1455 }
1456
1457 static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
1458 {
1459         int ret;
1460         const u8 *key = dev->prev_data;
1461
1462         if (!key) {
1463                 const struct opal_key *okey = data;
1464
1465                 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1466                                                  OPAL_ADMINSP_UID,
1467                                                  okey->key,
1468                                                  okey->key_len);
1469         } else {
1470                 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1471                                                  OPAL_ADMINSP_UID,
1472                                                  key, dev->prev_d_len);
1473                 kfree(key);
1474                 dev->prev_data = NULL;
1475         }
1476
1477         return ret;
1478 }
1479
1480 static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
1481 {
1482         struct opal_key *key = data;
1483
1484         return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
1485                                           OPAL_LOCKINGSP_UID,
1486                                           key->key, key->key_len);
1487 }
1488
1489 static int start_PSID_opal_session(struct opal_dev *dev, void *data)
1490 {
1491         const struct opal_key *okey = data;
1492
1493         return start_generic_opal_session(dev, OPAL_PSID_UID,
1494                                           OPAL_ADMINSP_UID,
1495                                           okey->key,
1496                                           okey->key_len);
1497 }
1498
1499 static int start_auth_opal_session(struct opal_dev *dev, void *data)
1500 {
1501         struct opal_session_info *session = data;
1502         u8 lk_ul_user[OPAL_UID_LENGTH];
1503         size_t keylen = session->opal_key.key_len;
1504         int err = 0;
1505
1506         u8 *key = session->opal_key.key;
1507         u32 hsn = GENERIC_HOST_SESSION_NUM;
1508
1509         if (session->sum)
1510                 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1511                                          session->opal_key.lr);
1512         else if (session->who != OPAL_ADMIN1 && !session->sum)
1513                 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1514                                          session->who - 1);
1515         else
1516                 memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
1517
1518         if (err)
1519                 return err;
1520
1521         err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1522                         opalmethod[OPAL_STARTSESSION]);
1523
1524         add_token_u64(&err, dev, hsn);
1525         add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
1526                              OPAL_UID_LENGTH);
1527         add_token_u8(&err, dev, 1);
1528         add_token_u8(&err, dev, OPAL_STARTNAME);
1529         add_token_u8(&err, dev, 0);
1530         add_token_bytestring(&err, dev, key, keylen);
1531         add_token_u8(&err, dev, OPAL_ENDNAME);
1532         add_token_u8(&err, dev, OPAL_STARTNAME);
1533         add_token_u8(&err, dev, 3);
1534         add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
1535         add_token_u8(&err, dev, OPAL_ENDNAME);
1536
1537         if (err) {
1538                 pr_debug("Error building STARTSESSION command.\n");
1539                 return err;
1540         }
1541
1542         return finalize_and_send(dev, start_opal_session_cont);
1543 }
1544
1545 static int revert_tper(struct opal_dev *dev, void *data)
1546 {
1547         int err;
1548
1549         err = cmd_start(dev, opaluid[OPAL_ADMINSP_UID],
1550                         opalmethod[OPAL_REVERT]);
1551         if (err) {
1552                 pr_debug("Error building REVERT TPER command.\n");
1553                 return err;
1554         }
1555
1556         return finalize_and_send(dev, parse_and_check_status);
1557 }
1558
1559 static int internal_activate_user(struct opal_dev *dev, void *data)
1560 {
1561         struct opal_session_info *session = data;
1562         u8 uid[OPAL_UID_LENGTH];
1563         int err;
1564
1565         memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1566         uid[7] = session->who;
1567
1568         err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1569         add_token_u8(&err, dev, OPAL_STARTNAME);
1570         add_token_u8(&err, dev, OPAL_VALUES);
1571         add_token_u8(&err, dev, OPAL_STARTLIST);
1572         add_token_u8(&err, dev, OPAL_STARTNAME);
1573         add_token_u8(&err, dev, 5); /* Enabled */
1574         add_token_u8(&err, dev, OPAL_TRUE);
1575         add_token_u8(&err, dev, OPAL_ENDNAME);
1576         add_token_u8(&err, dev, OPAL_ENDLIST);
1577         add_token_u8(&err, dev, OPAL_ENDNAME);
1578
1579         if (err) {
1580                 pr_debug("Error building Activate UserN command.\n");
1581                 return err;
1582         }
1583
1584         return finalize_and_send(dev, parse_and_check_status);
1585 }
1586
1587 static int erase_locking_range(struct opal_dev *dev, void *data)
1588 {
1589         struct opal_session_info *session = data;
1590         u8 uid[OPAL_UID_LENGTH];
1591         int err;
1592
1593         if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
1594                 return -ERANGE;
1595
1596         err = cmd_start(dev, uid, opalmethod[OPAL_ERASE]);
1597
1598         if (err) {
1599                 pr_debug("Error building Erase Locking Range Command.\n");
1600                 return err;
1601         }
1602
1603         return finalize_and_send(dev, parse_and_check_status);
1604 }
1605
1606 static int set_mbr_done(struct opal_dev *dev, void *data)
1607 {
1608         u8 *mbr_done_tf = data;
1609         int err;
1610
1611         err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1612                         opalmethod[OPAL_SET]);
1613
1614         add_token_u8(&err, dev, OPAL_STARTNAME);
1615         add_token_u8(&err, dev, OPAL_VALUES);
1616         add_token_u8(&err, dev, OPAL_STARTLIST);
1617         add_token_u8(&err, dev, OPAL_STARTNAME);
1618         add_token_u8(&err, dev, OPAL_MBRDONE);
1619         add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
1620         add_token_u8(&err, dev, OPAL_ENDNAME);
1621         add_token_u8(&err, dev, OPAL_ENDLIST);
1622         add_token_u8(&err, dev, OPAL_ENDNAME);
1623
1624         if (err) {
1625                 pr_debug("Error Building set MBR Done command\n");
1626                 return err;
1627         }
1628
1629         return finalize_and_send(dev, parse_and_check_status);
1630 }
1631
1632 static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
1633 {
1634         u8 *mbr_en_dis = data;
1635         int err;
1636
1637         err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1638                         opalmethod[OPAL_SET]);
1639
1640         add_token_u8(&err, dev, OPAL_STARTNAME);
1641         add_token_u8(&err, dev, OPAL_VALUES);
1642         add_token_u8(&err, dev, OPAL_STARTLIST);
1643         add_token_u8(&err, dev, OPAL_STARTNAME);
1644         add_token_u8(&err, dev, OPAL_MBRENABLE);
1645         add_token_u8(&err, dev, *mbr_en_dis);
1646         add_token_u8(&err, dev, OPAL_ENDNAME);
1647         add_token_u8(&err, dev, OPAL_ENDLIST);
1648         add_token_u8(&err, dev, OPAL_ENDNAME);
1649
1650         if (err) {
1651                 pr_debug("Error Building set MBR done command\n");
1652                 return err;
1653         }
1654
1655         return finalize_and_send(dev, parse_and_check_status);
1656 }
1657
1658 static int write_shadow_mbr(struct opal_dev *dev, void *data)
1659 {
1660         struct opal_shadow_mbr *shadow = data;
1661
1662         return generic_table_write_data(dev, shadow->data, shadow->offset,
1663                                         shadow->size, opaluid[OPAL_MBR]);
1664 }
1665
1666 static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
1667                           struct opal_dev *dev)
1668 {
1669         int err;
1670
1671         err = cmd_start(dev, cpin_uid, opalmethod[OPAL_SET]);
1672
1673         add_token_u8(&err, dev, OPAL_STARTNAME);
1674         add_token_u8(&err, dev, OPAL_VALUES);
1675         add_token_u8(&err, dev, OPAL_STARTLIST);
1676         add_token_u8(&err, dev, OPAL_STARTNAME);
1677         add_token_u8(&err, dev, OPAL_PIN);
1678         add_token_bytestring(&err, dev, key, key_len);
1679         add_token_u8(&err, dev, OPAL_ENDNAME);
1680         add_token_u8(&err, dev, OPAL_ENDLIST);
1681         add_token_u8(&err, dev, OPAL_ENDNAME);
1682
1683         return err;
1684 }
1685
1686 static int set_new_pw(struct opal_dev *dev, void *data)
1687 {
1688         u8 cpin_uid[OPAL_UID_LENGTH];
1689         struct opal_session_info *usr = data;
1690
1691         memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
1692
1693         if (usr->who != OPAL_ADMIN1) {
1694                 cpin_uid[5] = 0x03;
1695                 if (usr->sum)
1696                         cpin_uid[7] = usr->opal_key.lr + 1;
1697                 else
1698                         cpin_uid[7] = usr->who;
1699         }
1700
1701         if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
1702                            cpin_uid, dev)) {
1703                 pr_debug("Error building set password command.\n");
1704                 return -ERANGE;
1705         }
1706
1707         return finalize_and_send(dev, parse_and_check_status);
1708 }
1709
1710 static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
1711 {
1712         u8 cpin_uid[OPAL_UID_LENGTH];
1713         struct opal_key *key = data;
1714
1715         memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
1716
1717         if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
1718                 pr_debug("Error building Set SID cpin\n");
1719                 return -ERANGE;
1720         }
1721         return finalize_and_send(dev, parse_and_check_status);
1722 }
1723
1724 static int add_user_to_lr(struct opal_dev *dev, void *data)
1725 {
1726         u8 lr_buffer[OPAL_UID_LENGTH];
1727         u8 user_uid[OPAL_UID_LENGTH];
1728         struct opal_lock_unlock *lkul = data;
1729         int err;
1730
1731         memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
1732                OPAL_UID_LENGTH);
1733
1734         if (lkul->l_state == OPAL_RW)
1735                 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
1736                        OPAL_UID_LENGTH);
1737
1738         lr_buffer[7] = lkul->session.opal_key.lr;
1739
1740         memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1741
1742         user_uid[7] = lkul->session.who;
1743
1744         err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1745
1746         add_token_u8(&err, dev, OPAL_STARTNAME);
1747         add_token_u8(&err, dev, OPAL_VALUES);
1748
1749         add_token_u8(&err, dev, OPAL_STARTLIST);
1750         add_token_u8(&err, dev, OPAL_STARTNAME);
1751         add_token_u8(&err, dev, 3);
1752
1753         add_token_u8(&err, dev, OPAL_STARTLIST);
1754
1755
1756         add_token_u8(&err, dev, OPAL_STARTNAME);
1757         add_token_bytestring(&err, dev,
1758                              opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1759                              OPAL_UID_LENGTH/2);
1760         add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1761         add_token_u8(&err, dev, OPAL_ENDNAME);
1762
1763
1764         add_token_u8(&err, dev, OPAL_STARTNAME);
1765         add_token_bytestring(&err, dev,
1766                              opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1767                              OPAL_UID_LENGTH/2);
1768         add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1769         add_token_u8(&err, dev, OPAL_ENDNAME);
1770
1771
1772         add_token_u8(&err, dev, OPAL_STARTNAME);
1773         add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
1774                              OPAL_UID_LENGTH/2);
1775         add_token_u8(&err, dev, 1);
1776         add_token_u8(&err, dev, OPAL_ENDNAME);
1777
1778
1779         add_token_u8(&err, dev, OPAL_ENDLIST);
1780         add_token_u8(&err, dev, OPAL_ENDNAME);
1781         add_token_u8(&err, dev, OPAL_ENDLIST);
1782         add_token_u8(&err, dev, OPAL_ENDNAME);
1783
1784         if (err) {
1785                 pr_debug("Error building add user to locking range command.\n");
1786                 return err;
1787         }
1788
1789         return finalize_and_send(dev, parse_and_check_status);
1790 }
1791
1792 static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
1793 {
1794         u8 lr_buffer[OPAL_UID_LENGTH];
1795         struct opal_lock_unlock *lkul = data;
1796         u8 read_locked = 1, write_locked = 1;
1797         int err = 0;
1798
1799         if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1800                                 lkul->session.opal_key.lr) < 0)
1801                 return -ERANGE;
1802
1803         switch (lkul->l_state) {
1804         case OPAL_RO:
1805                 read_locked = 0;
1806                 write_locked = 1;
1807                 break;
1808         case OPAL_RW:
1809                 read_locked = 0;
1810                 write_locked = 0;
1811                 break;
1812         case OPAL_LK:
1813                 /* vars are initialized to locked */
1814                 break;
1815         default:
1816                 pr_debug("Tried to set an invalid locking state... returning to uland\n");
1817                 return OPAL_INVAL_PARAM;
1818         }
1819
1820         err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1821
1822         add_token_u8(&err, dev, OPAL_STARTNAME);
1823         add_token_u8(&err, dev, OPAL_VALUES);
1824         add_token_u8(&err, dev, OPAL_STARTLIST);
1825
1826         add_token_u8(&err, dev, OPAL_STARTNAME);
1827         add_token_u8(&err, dev, OPAL_READLOCKED);
1828         add_token_u8(&err, dev, read_locked);
1829         add_token_u8(&err, dev, OPAL_ENDNAME);
1830
1831         add_token_u8(&err, dev, OPAL_STARTNAME);
1832         add_token_u8(&err, dev, OPAL_WRITELOCKED);
1833         add_token_u8(&err, dev, write_locked);
1834         add_token_u8(&err, dev, OPAL_ENDNAME);
1835
1836         add_token_u8(&err, dev, OPAL_ENDLIST);
1837         add_token_u8(&err, dev, OPAL_ENDNAME);
1838
1839         if (err) {
1840                 pr_debug("Error building SET command.\n");
1841                 return err;
1842         }
1843
1844         return finalize_and_send(dev, parse_and_check_status);
1845 }
1846
1847
1848 static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
1849 {
1850         u8 lr_buffer[OPAL_UID_LENGTH];
1851         u8 read_locked = 1, write_locked = 1;
1852         struct opal_lock_unlock *lkul = data;
1853         int ret;
1854
1855         clear_opal_cmd(dev);
1856         set_comid(dev, dev->comid);
1857
1858         if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1859                                 lkul->session.opal_key.lr) < 0)
1860                 return -ERANGE;
1861
1862         switch (lkul->l_state) {
1863         case OPAL_RO:
1864                 read_locked = 0;
1865                 write_locked = 1;
1866                 break;
1867         case OPAL_RW:
1868                 read_locked = 0;
1869                 write_locked = 0;
1870                 break;
1871         case OPAL_LK:
1872                 /* vars are initialized to locked */
1873                 break;
1874         default:
1875                 pr_debug("Tried to set an invalid locking state.\n");
1876                 return OPAL_INVAL_PARAM;
1877         }
1878         ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
1879                                         read_locked, write_locked);
1880
1881         if (ret < 0) {
1882                 pr_debug("Error building SET command.\n");
1883                 return ret;
1884         }
1885
1886         return finalize_and_send(dev, parse_and_check_status);
1887 }
1888
1889 static int activate_lsp(struct opal_dev *dev, void *data)
1890 {
1891         struct opal_lr_act *opal_act = data;
1892         u8 user_lr[OPAL_UID_LENGTH];
1893         int err, i;
1894
1895         err = cmd_start(dev, opaluid[OPAL_LOCKINGSP_UID],
1896                         opalmethod[OPAL_ACTIVATE]);
1897
1898         if (opal_act->sum) {
1899                 err = build_locking_range(user_lr, sizeof(user_lr),
1900                                           opal_act->lr[0]);
1901                 if (err)
1902                         return err;
1903
1904                 add_token_u8(&err, dev, OPAL_STARTNAME);
1905                 add_token_u64(&err, dev, OPAL_SUM_SET_LIST);
1906
1907                 add_token_u8(&err, dev, OPAL_STARTLIST);
1908                 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1909                 for (i = 1; i < opal_act->num_lrs; i++) {
1910                         user_lr[7] = opal_act->lr[i];
1911                         add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1912                 }
1913                 add_token_u8(&err, dev, OPAL_ENDLIST);
1914                 add_token_u8(&err, dev, OPAL_ENDNAME);
1915         }
1916
1917         if (err) {
1918                 pr_debug("Error building Activate LockingSP command.\n");
1919                 return err;
1920         }
1921
1922         return finalize_and_send(dev, parse_and_check_status);
1923 }
1924
1925 /* Determine if we're in the Manufactured Inactive or Active state */
1926 static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
1927 {
1928         u8 lc_status;
1929         int err;
1930
1931         err = generic_get_column(dev, opaluid[OPAL_LOCKINGSP_UID],
1932                                  OPAL_LIFECYCLE);
1933         if (err)
1934                 return err;
1935
1936         lc_status = response_get_u64(&dev->parsed, 4);
1937         /* 0x08 is Manufactured Inactive */
1938         /* 0x09 is Manufactured */
1939         if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
1940                 pr_debug("Couldn't determine the status of the Lifecycle state\n");
1941                 return -ENODEV;
1942         }
1943
1944         return 0;
1945 }
1946
1947 static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
1948 {
1949         const char *msid_pin;
1950         size_t strlen;
1951         int err;
1952
1953         err = generic_get_column(dev, opaluid[OPAL_C_PIN_MSID], OPAL_PIN);
1954         if (err)
1955                 return err;
1956
1957         strlen = response_get_string(&dev->parsed, 4, &msid_pin);
1958         if (!msid_pin) {
1959                 pr_debug("Couldn't extract MSID_CPIN from response\n");
1960                 return OPAL_INVAL_PARAM;
1961         }
1962
1963         dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
1964         if (!dev->prev_data)
1965                 return -ENOMEM;
1966
1967         dev->prev_d_len = strlen;
1968
1969         return 0;
1970 }
1971
1972 static int write_table_data(struct opal_dev *dev, void *data)
1973 {
1974         struct opal_read_write_table *write_tbl = data;
1975
1976         return generic_table_write_data(dev, write_tbl->data, write_tbl->offset,
1977                                         write_tbl->size, write_tbl->table_uid);
1978 }
1979
1980 static int read_table_data_cont(struct opal_dev *dev)
1981 {
1982         int err;
1983         const char *data_read;
1984
1985         err = parse_and_check_status(dev);
1986         if (err)
1987                 return err;
1988
1989         dev->prev_d_len = response_get_string(&dev->parsed, 1, &data_read);
1990         dev->prev_data = (void *)data_read;
1991         if (!dev->prev_data) {
1992                 pr_debug("%s: Couldn't read data from the table.\n", __func__);
1993                 return OPAL_INVAL_PARAM;
1994         }
1995
1996         return 0;
1997 }
1998
1999 /*
2000  * IO_BUFFER_LENGTH = 2048
2001  * sizeof(header) = 56
2002  * No. of Token Bytes in the Response = 11
2003  * MAX size of data that can be carried in response buffer
2004  * at a time is : 2048 - (56 + 11) = 1981 = 0x7BD.
2005  */
2006 #define OPAL_MAX_READ_TABLE (0x7BD)
2007
2008 static int read_table_data(struct opal_dev *dev, void *data)
2009 {
2010         struct opal_read_write_table *read_tbl = data;
2011         int err;
2012         size_t off = 0, max_read_size = OPAL_MAX_READ_TABLE;
2013         u64 table_len, len;
2014         u64 offset = read_tbl->offset, read_size = read_tbl->size - 1;
2015         u8 __user *dst;
2016
2017         err = generic_get_table_info(dev, read_tbl->table_uid, OPAL_TABLE_ROWS);
2018         if (err) {
2019                 pr_debug("Couldn't get the table size\n");
2020                 return err;
2021         }
2022
2023         table_len = response_get_u64(&dev->parsed, 4);
2024
2025         /* Check if the user is trying to read from the table limits */
2026         if (read_size > table_len || offset > table_len - read_size) {
2027                 pr_debug("Read size exceeds the Table size limits (%llu vs. %llu)\n",
2028                           offset + read_size, table_len);
2029                 return -EINVAL;
2030         }
2031
2032         while (off < read_size) {
2033                 err = cmd_start(dev, read_tbl->table_uid, opalmethod[OPAL_GET]);
2034
2035                 add_token_u8(&err, dev, OPAL_STARTLIST);
2036                 add_token_u8(&err, dev, OPAL_STARTNAME);
2037                 add_token_u8(&err, dev, OPAL_STARTROW);
2038                 add_token_u64(&err, dev, offset + off); /* start row value */
2039                 add_token_u8(&err, dev, OPAL_ENDNAME);
2040
2041                 add_token_u8(&err, dev, OPAL_STARTNAME);
2042                 add_token_u8(&err, dev, OPAL_ENDROW);
2043
2044                 len = min(max_read_size, (size_t)(read_size - off));
2045                 add_token_u64(&err, dev, offset + off + len); /* end row value
2046                                                                */
2047                 add_token_u8(&err, dev, OPAL_ENDNAME);
2048                 add_token_u8(&err, dev, OPAL_ENDLIST);
2049
2050                 if (err) {
2051                         pr_debug("Error building read table data command.\n");
2052                         break;
2053                 }
2054
2055                 err = finalize_and_send(dev, read_table_data_cont);
2056                 if (err)
2057                         break;
2058
2059                 /* len+1: This includes the NULL terminator at the end*/
2060                 if (dev->prev_d_len > len + 1) {
2061                         err = -EOVERFLOW;
2062                         break;
2063                 }
2064
2065                 dst = (u8 __user *)(uintptr_t)read_tbl->data;
2066                 if (copy_to_user(dst + off, dev->prev_data, dev->prev_d_len)) {
2067                         pr_debug("Error copying data to userspace\n");
2068                         err = -EFAULT;
2069                         break;
2070                 }
2071                 dev->prev_data = NULL;
2072
2073                 off += len;
2074         }
2075
2076         return err;
2077 }
2078
2079 static int end_opal_session(struct opal_dev *dev, void *data)
2080 {
2081         int err = 0;
2082
2083         clear_opal_cmd(dev);
2084         set_comid(dev, dev->comid);
2085         add_token_u8(&err, dev, OPAL_ENDOFSESSION);
2086
2087         if (err < 0)
2088                 return err;
2089
2090         return finalize_and_send(dev, end_session_cont);
2091 }
2092
2093 static int end_opal_session_error(struct opal_dev *dev)
2094 {
2095         const struct opal_step error_end_session = {
2096                 end_opal_session,
2097         };
2098
2099         return execute_step(dev, &error_end_session, 0);
2100 }
2101
2102 static inline void setup_opal_dev(struct opal_dev *dev)
2103 {
2104         dev->tsn = 0;
2105         dev->hsn = 0;
2106         dev->prev_data = NULL;
2107 }
2108
2109 static int check_opal_support(struct opal_dev *dev)
2110 {
2111         int ret;
2112
2113         mutex_lock(&dev->dev_lock);
2114         setup_opal_dev(dev);
2115         ret = opal_discovery0_step(dev);
2116         dev->supported = !ret;
2117         mutex_unlock(&dev->dev_lock);
2118
2119         return ret;
2120 }
2121
2122 static void clean_opal_dev(struct opal_dev *dev)
2123 {
2124
2125         struct opal_suspend_data *suspend, *next;
2126
2127         mutex_lock(&dev->dev_lock);
2128         list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
2129                 list_del(&suspend->node);
2130                 kfree(suspend);
2131         }
2132         mutex_unlock(&dev->dev_lock);
2133 }
2134
2135 void free_opal_dev(struct opal_dev *dev)
2136 {
2137         if (!dev)
2138                 return;
2139
2140         clean_opal_dev(dev);
2141         kfree(dev->resp);
2142         kfree(dev->cmd);
2143         kfree(dev);
2144 }
2145 EXPORT_SYMBOL(free_opal_dev);
2146
2147 struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
2148 {
2149         struct opal_dev *dev;
2150
2151         dev = kmalloc(sizeof(*dev), GFP_KERNEL);
2152         if (!dev)
2153                 return NULL;
2154
2155         /*
2156          * Presumably DMA-able buffers must be cache-aligned. Kmalloc makes
2157          * sure the allocated buffer is DMA-safe in that regard.
2158          */
2159         dev->cmd = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2160         if (!dev->cmd)
2161                 goto err_free_dev;
2162
2163         dev->resp = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2164         if (!dev->resp)
2165                 goto err_free_cmd;
2166
2167         INIT_LIST_HEAD(&dev->unlk_lst);
2168         mutex_init(&dev->dev_lock);
2169         dev->data = data;
2170         dev->send_recv = send_recv;
2171         if (check_opal_support(dev) != 0) {
2172                 pr_debug("Opal is not supported on this device\n");
2173                 goto err_free_resp;
2174         }
2175
2176         return dev;
2177
2178 err_free_resp:
2179         kfree(dev->resp);
2180
2181 err_free_cmd:
2182         kfree(dev->cmd);
2183
2184 err_free_dev:
2185         kfree(dev);
2186
2187         return NULL;
2188 }
2189 EXPORT_SYMBOL(init_opal_dev);
2190
2191 static int opal_secure_erase_locking_range(struct opal_dev *dev,
2192                                            struct opal_session_info *opal_session)
2193 {
2194         const struct opal_step erase_steps[] = {
2195                 { start_auth_opal_session, opal_session },
2196                 { get_active_key, &opal_session->opal_key.lr },
2197                 { gen_key, },
2198                 { end_opal_session, }
2199         };
2200         int ret;
2201
2202         mutex_lock(&dev->dev_lock);
2203         setup_opal_dev(dev);
2204         ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2205         mutex_unlock(&dev->dev_lock);
2206
2207         return ret;
2208 }
2209
2210 static int opal_erase_locking_range(struct opal_dev *dev,
2211                                     struct opal_session_info *opal_session)
2212 {
2213         const struct opal_step erase_steps[] = {
2214                 { start_auth_opal_session, opal_session },
2215                 { erase_locking_range, opal_session },
2216                 { end_opal_session, }
2217         };
2218         int ret;
2219
2220         mutex_lock(&dev->dev_lock);
2221         setup_opal_dev(dev);
2222         ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2223         mutex_unlock(&dev->dev_lock);
2224
2225         return ret;
2226 }
2227
2228 static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
2229                                           struct opal_mbr_data *opal_mbr)
2230 {
2231         u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
2232                 OPAL_TRUE : OPAL_FALSE;
2233
2234         const struct opal_step mbr_steps[] = {
2235                 { start_admin1LSP_opal_session, &opal_mbr->key },
2236                 { set_mbr_done, &enable_disable },
2237                 { end_opal_session, },
2238                 { start_admin1LSP_opal_session, &opal_mbr->key },
2239                 { set_mbr_enable_disable, &enable_disable },
2240                 { end_opal_session, }
2241         };
2242         int ret;
2243
2244         if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
2245             opal_mbr->enable_disable != OPAL_MBR_DISABLE)
2246                 return -EINVAL;
2247
2248         mutex_lock(&dev->dev_lock);
2249         setup_opal_dev(dev);
2250         ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2251         mutex_unlock(&dev->dev_lock);
2252
2253         return ret;
2254 }
2255
2256 static int opal_set_mbr_done(struct opal_dev *dev,
2257                              struct opal_mbr_done *mbr_done)
2258 {
2259         u8 mbr_done_tf = mbr_done->done_flag == OPAL_MBR_DONE ?
2260                 OPAL_TRUE : OPAL_FALSE;
2261
2262         const struct opal_step mbr_steps[] = {
2263                 { start_admin1LSP_opal_session, &mbr_done->key },
2264                 { set_mbr_done, &mbr_done_tf },
2265                 { end_opal_session, }
2266         };
2267         int ret;
2268
2269         if (mbr_done->done_flag != OPAL_MBR_DONE &&
2270             mbr_done->done_flag != OPAL_MBR_NOT_DONE)
2271                 return -EINVAL;
2272
2273         mutex_lock(&dev->dev_lock);
2274         setup_opal_dev(dev);
2275         ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2276         mutex_unlock(&dev->dev_lock);
2277
2278         return ret;
2279 }
2280
2281 static int opal_write_shadow_mbr(struct opal_dev *dev,
2282                                  struct opal_shadow_mbr *info)
2283 {
2284         const struct opal_step mbr_steps[] = {
2285                 { start_admin1LSP_opal_session, &info->key },
2286                 { write_shadow_mbr, info },
2287                 { end_opal_session, }
2288         };
2289         int ret;
2290
2291         if (info->size == 0)
2292                 return 0;
2293
2294         mutex_lock(&dev->dev_lock);
2295         setup_opal_dev(dev);
2296         ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2297         mutex_unlock(&dev->dev_lock);
2298
2299         return ret;
2300 }
2301
2302 static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
2303 {
2304         struct opal_suspend_data *suspend;
2305
2306         suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
2307         if (!suspend)
2308                 return -ENOMEM;
2309
2310         suspend->unlk = *lk_unlk;
2311         suspend->lr = lk_unlk->session.opal_key.lr;
2312
2313         mutex_lock(&dev->dev_lock);
2314         setup_opal_dev(dev);
2315         add_suspend_info(dev, suspend);
2316         mutex_unlock(&dev->dev_lock);
2317
2318         return 0;
2319 }
2320
2321 static int opal_add_user_to_lr(struct opal_dev *dev,
2322                                struct opal_lock_unlock *lk_unlk)
2323 {
2324         const struct opal_step steps[] = {
2325                 { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
2326                 { add_user_to_lr, lk_unlk },
2327                 { end_opal_session, }
2328         };
2329         int ret;
2330
2331         if (lk_unlk->l_state != OPAL_RO &&
2332             lk_unlk->l_state != OPAL_RW) {
2333                 pr_debug("Locking state was not RO or RW\n");
2334                 return -EINVAL;
2335         }
2336
2337         if (lk_unlk->session.who < OPAL_USER1 ||
2338             lk_unlk->session.who > OPAL_USER9) {
2339                 pr_debug("Authority was not within the range of users: %d\n",
2340                          lk_unlk->session.who);
2341                 return -EINVAL;
2342         }
2343
2344         if (lk_unlk->session.sum) {
2345                 pr_debug("%s not supported in sum. Use setup locking range\n",
2346                          __func__);
2347                 return -EINVAL;
2348         }
2349
2350         mutex_lock(&dev->dev_lock);
2351         setup_opal_dev(dev);
2352         ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
2353         mutex_unlock(&dev->dev_lock);
2354
2355         return ret;
2356 }
2357
2358 static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal, bool psid)
2359 {
2360         /* controller will terminate session */
2361         const struct opal_step revert_steps[] = {
2362                 { start_SIDASP_opal_session, opal },
2363                 { revert_tper, }
2364         };
2365         const struct opal_step psid_revert_steps[] = {
2366                 { start_PSID_opal_session, opal },
2367                 { revert_tper, }
2368         };
2369
2370         int ret;
2371
2372         mutex_lock(&dev->dev_lock);
2373         setup_opal_dev(dev);
2374         if (psid)
2375                 ret = execute_steps(dev, psid_revert_steps,
2376                                     ARRAY_SIZE(psid_revert_steps));
2377         else
2378                 ret = execute_steps(dev, revert_steps,
2379                                     ARRAY_SIZE(revert_steps));
2380         mutex_unlock(&dev->dev_lock);
2381
2382         /*
2383          * If we successfully reverted lets clean
2384          * any saved locking ranges.
2385          */
2386         if (!ret)
2387                 clean_opal_dev(dev);
2388
2389         return ret;
2390 }
2391
2392 static int __opal_lock_unlock(struct opal_dev *dev,
2393                               struct opal_lock_unlock *lk_unlk)
2394 {
2395         const struct opal_step unlock_steps[] = {
2396                 { start_auth_opal_session, &lk_unlk->session },
2397                 { lock_unlock_locking_range, lk_unlk },
2398                 { end_opal_session, }
2399         };
2400         const struct opal_step unlock_sum_steps[] = {
2401                 { start_auth_opal_session, &lk_unlk->session },
2402                 { lock_unlock_locking_range_sum, lk_unlk },
2403                 { end_opal_session, }
2404         };
2405
2406         if (lk_unlk->session.sum)
2407                 return execute_steps(dev, unlock_sum_steps,
2408                                      ARRAY_SIZE(unlock_sum_steps));
2409         else
2410                 return execute_steps(dev, unlock_steps,
2411                                      ARRAY_SIZE(unlock_steps));
2412 }
2413
2414 static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
2415 {
2416         u8 mbr_done_tf = OPAL_TRUE;
2417         const struct opal_step mbrdone_step[] = {
2418                 { start_admin1LSP_opal_session, key },
2419                 { set_mbr_done, &mbr_done_tf },
2420                 { end_opal_session, }
2421         };
2422
2423         return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
2424 }
2425
2426 static int opal_lock_unlock(struct opal_dev *dev,
2427                             struct opal_lock_unlock *lk_unlk)
2428 {
2429         int ret;
2430
2431         if (lk_unlk->session.who > OPAL_USER9)
2432                 return -EINVAL;
2433
2434         mutex_lock(&dev->dev_lock);
2435         ret = __opal_lock_unlock(dev, lk_unlk);
2436         mutex_unlock(&dev->dev_lock);
2437
2438         return ret;
2439 }
2440
2441 static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
2442 {
2443         const struct opal_step owner_steps[] = {
2444                 { start_anybodyASP_opal_session, },
2445                 { get_msid_cpin_pin, },
2446                 { end_opal_session, },
2447                 { start_SIDASP_opal_session, opal },
2448                 { set_sid_cpin_pin, opal },
2449                 { end_opal_session, }
2450         };
2451         int ret;
2452
2453         if (!dev)
2454                 return -ENODEV;
2455
2456         mutex_lock(&dev->dev_lock);
2457         setup_opal_dev(dev);
2458         ret = execute_steps(dev, owner_steps, ARRAY_SIZE(owner_steps));
2459         mutex_unlock(&dev->dev_lock);
2460
2461         return ret;
2462 }
2463
2464 static int opal_activate_lsp(struct opal_dev *dev,
2465                              struct opal_lr_act *opal_lr_act)
2466 {
2467         const struct opal_step active_steps[] = {
2468                 { start_SIDASP_opal_session, &opal_lr_act->key },
2469                 { get_lsp_lifecycle, },
2470                 { activate_lsp, opal_lr_act },
2471                 { end_opal_session, }
2472         };
2473         int ret;
2474
2475         if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
2476                 return -EINVAL;
2477
2478         mutex_lock(&dev->dev_lock);
2479         setup_opal_dev(dev);
2480         ret = execute_steps(dev, active_steps, ARRAY_SIZE(active_steps));
2481         mutex_unlock(&dev->dev_lock);
2482
2483         return ret;
2484 }
2485
2486 static int opal_setup_locking_range(struct opal_dev *dev,
2487                                     struct opal_user_lr_setup *opal_lrs)
2488 {
2489         const struct opal_step lr_steps[] = {
2490                 { start_auth_opal_session, &opal_lrs->session },
2491                 { setup_locking_range, opal_lrs },
2492                 { end_opal_session, }
2493         };
2494         int ret;
2495
2496         mutex_lock(&dev->dev_lock);
2497         setup_opal_dev(dev);
2498         ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
2499         mutex_unlock(&dev->dev_lock);
2500
2501         return ret;
2502 }
2503
2504 static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
2505 {
2506         const struct opal_step pw_steps[] = {
2507                 { start_auth_opal_session, &opal_pw->session },
2508                 { set_new_pw, &opal_pw->new_user_pw },
2509                 { end_opal_session, }
2510         };
2511         int ret;
2512
2513         if (opal_pw->session.who > OPAL_USER9  ||
2514             opal_pw->new_user_pw.who > OPAL_USER9)
2515                 return -EINVAL;
2516
2517         mutex_lock(&dev->dev_lock);
2518         setup_opal_dev(dev);
2519         ret = execute_steps(dev, pw_steps, ARRAY_SIZE(pw_steps));
2520         mutex_unlock(&dev->dev_lock);
2521
2522         return ret;
2523 }
2524
2525 static int opal_activate_user(struct opal_dev *dev,
2526                               struct opal_session_info *opal_session)
2527 {
2528         const struct opal_step act_steps[] = {
2529                 { start_admin1LSP_opal_session, &opal_session->opal_key },
2530                 { internal_activate_user, opal_session },
2531                 { end_opal_session, }
2532         };
2533         int ret;
2534
2535         /* We can't activate Admin1 it's active as manufactured */
2536         if (opal_session->who < OPAL_USER1 ||
2537             opal_session->who > OPAL_USER9) {
2538                 pr_debug("Who was not a valid user: %d\n", opal_session->who);
2539                 return -EINVAL;
2540         }
2541
2542         mutex_lock(&dev->dev_lock);
2543         setup_opal_dev(dev);
2544         ret = execute_steps(dev, act_steps, ARRAY_SIZE(act_steps));
2545         mutex_unlock(&dev->dev_lock);
2546
2547         return ret;
2548 }
2549
2550 bool opal_unlock_from_suspend(struct opal_dev *dev)
2551 {
2552         struct opal_suspend_data *suspend;
2553         bool was_failure = false;
2554         int ret = 0;
2555
2556         if (!dev)
2557                 return false;
2558
2559         if (!dev->supported)
2560                 return false;
2561
2562         mutex_lock(&dev->dev_lock);
2563         setup_opal_dev(dev);
2564
2565         list_for_each_entry(suspend, &dev->unlk_lst, node) {
2566                 dev->tsn = 0;
2567                 dev->hsn = 0;
2568
2569                 ret = __opal_lock_unlock(dev, &suspend->unlk);
2570                 if (ret) {
2571                         pr_debug("Failed to unlock LR %hhu with sum %d\n",
2572                                  suspend->unlk.session.opal_key.lr,
2573                                  suspend->unlk.session.sum);
2574                         was_failure = true;
2575                 }
2576
2577                 if (dev->mbr_enabled) {
2578                         ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
2579                         if (ret)
2580                                 pr_debug("Failed to set MBR Done in S3 resume\n");
2581                 }
2582         }
2583         mutex_unlock(&dev->dev_lock);
2584
2585         return was_failure;
2586 }
2587 EXPORT_SYMBOL(opal_unlock_from_suspend);
2588
2589 static int opal_read_table(struct opal_dev *dev,
2590                            struct opal_read_write_table *rw_tbl)
2591 {
2592         const struct opal_step read_table_steps[] = {
2593                 { start_admin1LSP_opal_session, &rw_tbl->key },
2594                 { read_table_data, rw_tbl },
2595                 { end_opal_session, }
2596         };
2597         int ret = 0;
2598
2599         if (!rw_tbl->size)
2600                 return ret;
2601
2602         return execute_steps(dev, read_table_steps,
2603                              ARRAY_SIZE(read_table_steps));
2604 }
2605
2606 static int opal_write_table(struct opal_dev *dev,
2607                             struct opal_read_write_table *rw_tbl)
2608 {
2609         const struct opal_step write_table_steps[] = {
2610                 { start_admin1LSP_opal_session, &rw_tbl->key },
2611                 { write_table_data, rw_tbl },
2612                 { end_opal_session, }
2613         };
2614         int ret = 0;
2615
2616         if (!rw_tbl->size)
2617                 return ret;
2618
2619         return execute_steps(dev, write_table_steps,
2620                              ARRAY_SIZE(write_table_steps));
2621 }
2622
2623 static int opal_generic_read_write_table(struct opal_dev *dev,
2624                                          struct opal_read_write_table *rw_tbl)
2625 {
2626         int ret, bit_set;
2627
2628         mutex_lock(&dev->dev_lock);
2629         setup_opal_dev(dev);
2630
2631         bit_set = fls64(rw_tbl->flags) - 1;
2632         switch (bit_set) {
2633         case OPAL_READ_TABLE:
2634                 ret = opal_read_table(dev, rw_tbl);
2635                 break;
2636         case OPAL_WRITE_TABLE:
2637                 ret = opal_write_table(dev, rw_tbl);
2638                 break;
2639         default:
2640                 pr_debug("Invalid bit set in the flag (%016llx).\n",
2641                          rw_tbl->flags);
2642                 ret = -EINVAL;
2643                 break;
2644         }
2645
2646         mutex_unlock(&dev->dev_lock);
2647
2648         return ret;
2649 }
2650
2651 int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
2652 {
2653         void *p;
2654         int ret = -ENOTTY;
2655
2656         if (!capable(CAP_SYS_ADMIN))
2657                 return -EACCES;
2658         if (!dev)
2659                 return -ENOTSUPP;
2660         if (!dev->supported)
2661                 return -ENOTSUPP;
2662
2663         p = memdup_user(arg, _IOC_SIZE(cmd));
2664         if (IS_ERR(p))
2665                 return PTR_ERR(p);
2666
2667         switch (cmd) {
2668         case IOC_OPAL_SAVE:
2669                 ret = opal_save(dev, p);
2670                 break;
2671         case IOC_OPAL_LOCK_UNLOCK:
2672                 ret = opal_lock_unlock(dev, p);
2673                 break;
2674         case IOC_OPAL_TAKE_OWNERSHIP:
2675                 ret = opal_take_ownership(dev, p);
2676                 break;
2677         case IOC_OPAL_ACTIVATE_LSP:
2678                 ret = opal_activate_lsp(dev, p);
2679                 break;
2680         case IOC_OPAL_SET_PW:
2681                 ret = opal_set_new_pw(dev, p);
2682                 break;
2683         case IOC_OPAL_ACTIVATE_USR:
2684                 ret = opal_activate_user(dev, p);
2685                 break;
2686         case IOC_OPAL_REVERT_TPR:
2687                 ret = opal_reverttper(dev, p, false);
2688                 break;
2689         case IOC_OPAL_LR_SETUP:
2690                 ret = opal_setup_locking_range(dev, p);
2691                 break;
2692         case IOC_OPAL_ADD_USR_TO_LR:
2693                 ret = opal_add_user_to_lr(dev, p);
2694                 break;
2695         case IOC_OPAL_ENABLE_DISABLE_MBR:
2696                 ret = opal_enable_disable_shadow_mbr(dev, p);
2697                 break;
2698         case IOC_OPAL_MBR_DONE:
2699                 ret = opal_set_mbr_done(dev, p);
2700                 break;
2701         case IOC_OPAL_WRITE_SHADOW_MBR:
2702                 ret = opal_write_shadow_mbr(dev, p);
2703                 break;
2704         case IOC_OPAL_ERASE_LR:
2705                 ret = opal_erase_locking_range(dev, p);
2706                 break;
2707         case IOC_OPAL_SECURE_ERASE_LR:
2708                 ret = opal_secure_erase_locking_range(dev, p);
2709                 break;
2710         case IOC_OPAL_PSID_REVERT_TPR:
2711                 ret = opal_reverttper(dev, p, true);
2712                 break;
2713         case IOC_OPAL_GENERIC_TABLE_RW:
2714                 ret = opal_generic_read_write_table(dev, p);
2715                 break;
2716         default:
2717                 break;
2718         }
2719
2720         kfree(p);
2721         return ret;
2722 }
2723 EXPORT_SYMBOL_GPL(sed_ioctl);