1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2016 Intel Corporation
6 * Scott Bauer <scott.bauer@intel.com>
7 * Rafael Antognolli <rafael.antognolli@intel.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ":OPAL: " fmt
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/blkdev.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>
24 #include "opal_proto.h"
26 #define IO_BUFFER_LENGTH 2048
29 /* Number of bytes needed by cmd_finalize. */
30 #define CMD_FINALIZE_BYTES_NEEDED 7
33 int (*fn)(struct opal_dev *dev, void *data);
36 typedef int (cont_fn)(struct opal_dev *dev);
38 enum opal_atom_width {
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.
52 struct opal_resp_tok {
55 enum opal_response_token type;
56 enum opal_atom_width width;
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.
73 struct opal_resp_tok toks[MAX_TOKS];
80 sec_send_recv *send_recv;
82 struct mutex dev_lock;
93 struct parsed_resp parsed;
97 struct list_head unlk_lst;
101 static const u8 opaluid[][OPAL_UID_LENGTH] = {
104 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
106 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
108 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
109 [OPAL_LOCKINGSP_UID] =
110 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
111 [OPAL_ENTERPRISE_LOCKINGSP_UID] =
112 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
114 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
116 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
118 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
120 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
122 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
124 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
125 [OPAL_ENTERPRISE_BANDMASTER0_UID] =
126 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
127 [OPAL_ENTERPRISE_ERASEMASTER_UID] =
128 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
132 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01 },
133 [OPAL_LOCKINGRANGE_GLOBAL] =
134 { 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
135 [OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
136 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
137 [OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
138 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
140 { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
142 { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
143 [OPAL_AUTHORITY_TABLE] =
144 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
146 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
147 [OPAL_LOCKING_INFO_TABLE] =
148 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
149 [OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
150 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
152 { 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00 },
154 /* C_PIN_TABLE object ID's */
156 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
158 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
159 [OPAL_C_PIN_ADMIN1] =
160 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
162 /* half UID's (only first 4 bytes used) */
163 [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
164 { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
165 [OPAL_HALF_UID_BOOLEAN_ACE] =
166 { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
168 /* special value for omitted optional parameter */
170 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
174 * TCG Storage SSC Methods.
175 * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
176 * Section: 6.3 Assigned UIDs
178 static const u8 opalmethod[][OPAL_METHOD_LENGTH] = {
180 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
181 [OPAL_STARTSESSION] =
182 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
184 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
186 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
188 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
190 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
192 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
193 [OPAL_EAUTHENTICATE] =
194 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
196 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
198 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
200 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
202 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
204 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
205 [OPAL_AUTHENTICATE] =
206 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
208 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
210 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
213 static int end_opal_session_error(struct opal_dev *dev);
214 static int opal_discovery0_step(struct opal_dev *dev);
216 struct opal_suspend_data {
217 struct opal_lock_unlock unlk;
219 struct list_head node;
224 * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
225 * Section: 5.1.5 Method Status Codes
227 static const char * const opal_errors[] = {
235 "No Sessions Available",
236 "Uniqueness Conflict",
237 "Insufficient Space",
244 "Transaction Failure",
246 "Authority Locked Out",
249 static const char *opal_error_to_human(int error)
254 if (error >= ARRAY_SIZE(opal_errors) || error < 0)
255 return "Unknown Error";
257 return opal_errors[error];
260 static void print_buffer(const u8 *ptr, u32 length)
263 print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
268 static bool check_tper(const void *data)
270 const struct d0_tper_features *tper = data;
271 u8 flags = tper->supported_features;
273 if (!(flags & TPER_SYNC_SUPPORTED)) {
274 pr_debug("TPer sync not supported. flags = %d\n",
275 tper->supported_features);
282 static bool check_lcksuppt(const void *data)
284 const struct d0_locking_features *lfeat = data;
285 u8 sup_feat = lfeat->supported_features;
287 return !!(sup_feat & LOCKING_SUPPORTED_MASK);
290 static bool check_lckenabled(const void *data)
292 const struct d0_locking_features *lfeat = data;
293 u8 sup_feat = lfeat->supported_features;
295 return !!(sup_feat & LOCKING_ENABLED_MASK);
298 static bool check_locked(const void *data)
300 const struct d0_locking_features *lfeat = data;
301 u8 sup_feat = lfeat->supported_features;
303 return !!(sup_feat & LOCKED_MASK);
306 static bool check_mbrenabled(const void *data)
308 const struct d0_locking_features *lfeat = data;
309 u8 sup_feat = lfeat->supported_features;
311 return !!(sup_feat & MBR_ENABLED_MASK);
314 static bool check_mbrdone(const void *data)
316 const struct d0_locking_features *lfeat = data;
317 u8 sup_feat = lfeat->supported_features;
319 return !!(sup_feat & MBR_DONE_MASK);
322 static bool check_sum(const void *data)
324 const struct d0_single_user_mode *sum = data;
325 u32 nlo = be32_to_cpu(sum->num_locking_objects);
328 pr_debug("Need at least one locking object.\n");
332 pr_debug("Number of locking objects: %d\n", nlo);
337 static u16 get_comid_v100(const void *data)
339 const struct d0_opal_v100 *v100 = data;
341 return be16_to_cpu(v100->baseComID);
344 static u16 get_comid_v200(const void *data)
346 const struct d0_opal_v200 *v200 = data;
348 return be16_to_cpu(v200->baseComID);
351 static int opal_send_cmd(struct opal_dev *dev)
353 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
354 dev->cmd, IO_BUFFER_LENGTH,
358 static int opal_recv_cmd(struct opal_dev *dev)
360 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
361 dev->resp, IO_BUFFER_LENGTH,
365 static int opal_recv_check(struct opal_dev *dev)
367 size_t buflen = IO_BUFFER_LENGTH;
368 void *buffer = dev->resp;
369 struct opal_header *hdr = buffer;
373 pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
374 hdr->cp.outstandingData,
375 hdr->cp.minTransfer);
377 if (hdr->cp.outstandingData == 0 ||
378 hdr->cp.minTransfer != 0)
381 memset(buffer, 0, buflen);
382 ret = opal_recv_cmd(dev);
388 static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
392 ret = opal_send_cmd(dev);
395 ret = opal_recv_cmd(dev);
398 ret = opal_recv_check(dev);
404 static void check_geometry(struct opal_dev *dev, const void *data)
406 const struct d0_geometry_features *geo = data;
408 dev->align = be64_to_cpu(geo->alignment_granularity);
409 dev->lowest_lba = be64_to_cpu(geo->lowest_aligned_lba);
412 static int execute_step(struct opal_dev *dev,
413 const struct opal_step *step, size_t stepIndex)
415 int error = step->fn(dev, step->data);
418 pr_debug("Step %zu (%pS) failed with error %d: %s\n",
419 stepIndex, step->fn, error,
420 opal_error_to_human(error));
426 static int execute_steps(struct opal_dev *dev,
427 const struct opal_step *steps, size_t n_steps)
432 /* first do a discovery0 */
433 error = opal_discovery0_step(dev);
437 for (state = 0; state < n_steps; state++) {
438 error = execute_step(dev, &steps[state], state);
447 * For each OPAL command the first step in steps starts some sort of
448 * session. If an error occurred in the initial discovery0 or if an
449 * error occurred in the first step (and thus stopping the loop with
450 * state == 0) then there was an error before or during the attempt to
451 * start a session. Therefore we shouldn't attempt to terminate a
452 * session, as one has not yet been created.
455 end_opal_session_error(dev);
460 static int opal_discovery0_end(struct opal_dev *dev)
462 bool found_com_id = false, supported = true, single_user = false;
463 const struct d0_header *hdr = (struct d0_header *)dev->resp;
464 const u8 *epos = dev->resp, *cpos = dev->resp;
466 u32 hlen = be32_to_cpu(hdr->length);
468 print_buffer(dev->resp, hlen);
469 dev->flags &= OPAL_FL_SUPPORTED;
471 if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
472 pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
473 sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
477 epos += hlen; /* end of buffer */
478 cpos += sizeof(*hdr); /* current position on buffer */
480 while (cpos < epos && supported) {
481 const struct d0_features *body =
482 (const struct d0_features *)cpos;
484 switch (be16_to_cpu(body->code)) {
486 supported = check_tper(body->features);
489 single_user = check_sum(body->features);
492 check_geometry(dev, body);
495 if (check_lcksuppt(body->features))
496 dev->flags |= OPAL_FL_LOCKING_SUPPORTED;
497 if (check_lckenabled(body->features))
498 dev->flags |= OPAL_FL_LOCKING_ENABLED;
499 if (check_locked(body->features))
500 dev->flags |= OPAL_FL_LOCKED;
501 if (check_mbrenabled(body->features))
502 dev->flags |= OPAL_FL_MBR_ENABLED;
503 if (check_mbrdone(body->features))
504 dev->flags |= OPAL_FL_MBR_DONE;
508 /* some ignored properties */
509 pr_debug("Found OPAL feature description: %d\n",
510 be16_to_cpu(body->code));
513 comid = get_comid_v100(body->features);
517 comid = get_comid_v200(body->features);
520 case 0xbfff ... 0xffff:
521 /* vendor specific, just ignore */
524 pr_debug("OPAL Unknown feature: %d\n",
525 be16_to_cpu(body->code));
528 cpos += body->length + 4;
532 pr_debug("This device is not Opal enabled. Not Supported!\n");
537 pr_debug("Device doesn't support single user mode\n");
541 pr_debug("Could not find OPAL comid for device. Returning early\n");
550 static int opal_discovery0(struct opal_dev *dev, void *data)
554 memset(dev->resp, 0, IO_BUFFER_LENGTH);
555 dev->comid = OPAL_DISCOVERY_COMID;
556 ret = opal_recv_cmd(dev);
560 return opal_discovery0_end(dev);
563 static int opal_discovery0_step(struct opal_dev *dev)
565 const struct opal_step discovery0_step = {
569 return execute_step(dev, &discovery0_step, 0);
572 static size_t remaining_size(struct opal_dev *cmd)
574 return IO_BUFFER_LENGTH - cmd->pos;
577 static bool can_add(int *err, struct opal_dev *cmd, size_t len)
582 if (remaining_size(cmd) < len) {
583 pr_debug("Error adding %zu bytes: end of buffer.\n", len);
591 static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
593 if (!can_add(err, cmd, 1))
596 cmd->cmd[cmd->pos++] = tok;
599 static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
600 bool has_sign, int len)
605 atom = SHORT_ATOM_ID;
606 atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
607 atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
608 atom |= len & SHORT_ATOM_LEN_MASK;
610 add_token_u8(&err, cmd, atom);
613 static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
614 bool has_sign, int len)
618 header0 = MEDIUM_ATOM_ID;
619 header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
620 header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
621 header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
623 cmd->cmd[cmd->pos++] = header0;
624 cmd->cmd[cmd->pos++] = len;
627 static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
632 if (!(number & ~TINY_ATOM_DATA_MASK)) {
633 add_token_u8(err, cmd, number);
638 len = DIV_ROUND_UP(msb, 8);
640 if (!can_add(err, cmd, len + 1)) {
641 pr_debug("Error adding u64: end of buffer.\n");
644 add_short_atom_header(cmd, false, false, len);
646 add_token_u8(err, cmd, number >> (len * 8));
649 static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len)
651 size_t header_len = 1;
652 bool is_short_atom = true;
654 if (len & ~SHORT_ATOM_LEN_MASK) {
656 is_short_atom = false;
659 if (!can_add(err, cmd, header_len + len)) {
660 pr_debug("Error adding bytestring: end of buffer.\n");
665 add_short_atom_header(cmd, true, false, len);
667 add_medium_atom_header(cmd, true, false, len);
669 return &cmd->cmd[cmd->pos];
672 static void add_token_bytestring(int *err, struct opal_dev *cmd,
673 const u8 *bytestring, size_t len)
677 start = add_bytestring_header(err, cmd, len);
680 memcpy(start, bytestring, len);
684 static int build_locking_range(u8 *buffer, size_t length, u8 lr)
686 if (length > OPAL_UID_LENGTH) {
687 pr_debug("Can't build locking range. Length OOB\n");
691 memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
696 buffer[5] = LOCKING_RANGE_NON_GLOBAL;
702 static int build_locking_user(u8 *buffer, size_t length, u8 lr)
704 if (length > OPAL_UID_LENGTH) {
705 pr_debug("Can't build locking range user. Length OOB\n");
709 memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
716 static void set_comid(struct opal_dev *cmd, u16 comid)
718 struct opal_header *hdr = (struct opal_header *)cmd->cmd;
720 hdr->cp.extendedComID[0] = comid >> 8;
721 hdr->cp.extendedComID[1] = comid;
722 hdr->cp.extendedComID[2] = 0;
723 hdr->cp.extendedComID[3] = 0;
726 static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
728 struct opal_header *hdr;
732 * Close the parameter list opened from cmd_start.
733 * The number of bytes added must be equal to
734 * CMD_FINALIZE_BYTES_NEEDED.
736 add_token_u8(&err, cmd, OPAL_ENDLIST);
738 add_token_u8(&err, cmd, OPAL_ENDOFDATA);
739 add_token_u8(&err, cmd, OPAL_STARTLIST);
740 add_token_u8(&err, cmd, 0);
741 add_token_u8(&err, cmd, 0);
742 add_token_u8(&err, cmd, 0);
743 add_token_u8(&err, cmd, OPAL_ENDLIST);
746 pr_debug("Error finalizing command.\n");
750 hdr = (struct opal_header *) cmd->cmd;
752 hdr->pkt.tsn = cpu_to_be32(tsn);
753 hdr->pkt.hsn = cpu_to_be32(hsn);
755 hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
756 while (cmd->pos % 4) {
757 if (cmd->pos >= IO_BUFFER_LENGTH) {
758 pr_debug("Error: Buffer overrun\n");
761 cmd->cmd[cmd->pos++] = 0;
763 hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
765 hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
770 static const struct opal_resp_tok *response_get_token(
771 const struct parsed_resp *resp,
774 const struct opal_resp_tok *tok;
777 pr_debug("Response is NULL\n");
778 return ERR_PTR(-EINVAL);
781 if (n >= resp->num) {
782 pr_debug("Token number doesn't exist: %d, resp: %d\n",
784 return ERR_PTR(-EINVAL);
787 tok = &resp->toks[n];
789 pr_debug("Token length must be non-zero\n");
790 return ERR_PTR(-EINVAL);
796 static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
801 tok->width = OPAL_WIDTH_TINY;
803 if (pos[0] & TINY_ATOM_SIGNED) {
804 tok->type = OPAL_DTA_TOKENID_SINT;
806 tok->type = OPAL_DTA_TOKENID_UINT;
807 tok->stored.u = pos[0] & 0x3f;
813 static ssize_t response_parse_short(struct opal_resp_tok *tok,
817 tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
818 tok->width = OPAL_WIDTH_SHORT;
820 if (pos[0] & SHORT_ATOM_BYTESTRING) {
821 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
822 } else if (pos[0] & SHORT_ATOM_SIGNED) {
823 tok->type = OPAL_DTA_TOKENID_SINT;
828 tok->type = OPAL_DTA_TOKENID_UINT;
830 pr_debug("uint64 with more than 8 bytes\n");
833 for (i = tok->len - 1; i > 0; i--) {
834 u_integer |= ((u64)pos[i] << (8 * b));
837 tok->stored.u = u_integer;
843 static ssize_t response_parse_medium(struct opal_resp_tok *tok,
847 tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
848 tok->width = OPAL_WIDTH_MEDIUM;
850 if (pos[0] & MEDIUM_ATOM_BYTESTRING)
851 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
852 else if (pos[0] & MEDIUM_ATOM_SIGNED)
853 tok->type = OPAL_DTA_TOKENID_SINT;
855 tok->type = OPAL_DTA_TOKENID_UINT;
860 static ssize_t response_parse_long(struct opal_resp_tok *tok,
864 tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
865 tok->width = OPAL_WIDTH_LONG;
867 if (pos[0] & LONG_ATOM_BYTESTRING)
868 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
869 else if (pos[0] & LONG_ATOM_SIGNED)
870 tok->type = OPAL_DTA_TOKENID_SINT;
872 tok->type = OPAL_DTA_TOKENID_UINT;
877 static ssize_t response_parse_token(struct opal_resp_tok *tok,
882 tok->type = OPAL_DTA_TOKENID_TOKEN;
883 tok->width = OPAL_WIDTH_TOKEN;
888 static int response_parse(const u8 *buf, size_t length,
889 struct parsed_resp *resp)
891 const struct opal_header *hdr;
892 struct opal_resp_tok *iter;
895 ssize_t token_length;
897 u32 clen, plen, slen;
905 hdr = (struct opal_header *)buf;
909 clen = be32_to_cpu(hdr->cp.length);
910 plen = be32_to_cpu(hdr->pkt.length);
911 slen = be32_to_cpu(hdr->subpkt.length);
912 pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
915 if (clen == 0 || plen == 0 || slen == 0 ||
916 slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
917 pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
919 print_buffer(pos, sizeof(*hdr));
923 if (pos > buf + length)
928 print_buffer(pos, total);
930 if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
931 token_length = response_parse_tiny(iter, pos);
932 else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
933 token_length = response_parse_short(iter, pos);
934 else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
935 token_length = response_parse_medium(iter, pos);
936 else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
937 token_length = response_parse_long(iter, pos);
939 token_length = response_parse_token(iter, pos);
941 if (token_length < 0)
945 total -= token_length;
950 resp->num = num_entries;
955 static size_t response_get_string(const struct parsed_resp *resp, int n,
959 const struct opal_resp_tok *tok;
962 tok = response_get_token(resp, n);
966 if (tok->type != OPAL_DTA_TOKENID_BYTESTRING) {
967 pr_debug("Token is not a byte string!\n");
971 switch (tok->width) {
972 case OPAL_WIDTH_TINY:
973 case OPAL_WIDTH_SHORT:
976 case OPAL_WIDTH_MEDIUM:
979 case OPAL_WIDTH_LONG:
983 pr_debug("Token has invalid width!\n");
987 *store = tok->pos + skip;
989 return tok->len - skip;
992 static u64 response_get_u64(const struct parsed_resp *resp, int n)
994 const struct opal_resp_tok *tok;
996 tok = response_get_token(resp, n);
1000 if (tok->type != OPAL_DTA_TOKENID_UINT) {
1001 pr_debug("Token is not unsigned int: %d\n", tok->type);
1005 if (tok->width != OPAL_WIDTH_TINY && tok->width != OPAL_WIDTH_SHORT) {
1006 pr_debug("Atom is not short or tiny: %d\n", tok->width);
1010 return tok->stored.u;
1013 static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
1015 if (IS_ERR(token) ||
1016 token->type != OPAL_DTA_TOKENID_TOKEN ||
1017 token->pos[0] != match)
1022 static u8 response_status(const struct parsed_resp *resp)
1024 const struct opal_resp_tok *tok;
1026 tok = response_get_token(resp, 0);
1027 if (response_token_matches(tok, OPAL_ENDOFSESSION))
1031 return DTAERROR_NO_METHOD_STATUS;
1033 tok = response_get_token(resp, resp->num - 5);
1034 if (!response_token_matches(tok, OPAL_STARTLIST))
1035 return DTAERROR_NO_METHOD_STATUS;
1037 tok = response_get_token(resp, resp->num - 1);
1038 if (!response_token_matches(tok, OPAL_ENDLIST))
1039 return DTAERROR_NO_METHOD_STATUS;
1041 return response_get_u64(resp, resp->num - 4);
1044 /* Parses and checks for errors */
1045 static int parse_and_check_status(struct opal_dev *dev)
1049 print_buffer(dev->cmd, dev->pos);
1051 error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
1053 pr_debug("Couldn't parse response.\n");
1057 return response_status(&dev->parsed);
1060 static void clear_opal_cmd(struct opal_dev *dev)
1062 dev->pos = sizeof(struct opal_header);
1063 memset(dev->cmd, 0, IO_BUFFER_LENGTH);
1066 static int cmd_start(struct opal_dev *dev, const u8 *uid, const u8 *method)
1070 clear_opal_cmd(dev);
1071 set_comid(dev, dev->comid);
1073 add_token_u8(&err, dev, OPAL_CALL);
1074 add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
1075 add_token_bytestring(&err, dev, method, OPAL_METHOD_LENGTH);
1078 * Every method call is followed by its parameters enclosed within
1079 * OPAL_STARTLIST and OPAL_ENDLIST tokens. We automatically open the
1080 * parameter list here and close it later in cmd_finalize.
1082 add_token_u8(&err, dev, OPAL_STARTLIST);
1087 static int start_opal_session_cont(struct opal_dev *dev)
1092 error = parse_and_check_status(dev);
1096 hsn = response_get_u64(&dev->parsed, 4);
1097 tsn = response_get_u64(&dev->parsed, 5);
1099 if (hsn != GENERIC_HOST_SESSION_NUM || tsn < FIRST_TPER_SESSION_NUM) {
1100 pr_debug("Couldn't authenticate session\n");
1110 static void add_suspend_info(struct opal_dev *dev,
1111 struct opal_suspend_data *sus)
1113 struct opal_suspend_data *iter;
1115 list_for_each_entry(iter, &dev->unlk_lst, node) {
1116 if (iter->lr == sus->lr) {
1117 list_del(&iter->node);
1122 list_add_tail(&sus->node, &dev->unlk_lst);
1125 static int end_session_cont(struct opal_dev *dev)
1130 return parse_and_check_status(dev);
1133 static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
1137 ret = cmd_finalize(dev, dev->hsn, dev->tsn);
1139 pr_debug("Error finalizing command buffer: %d\n", ret);
1143 print_buffer(dev->cmd, dev->pos);
1145 return opal_send_recv(dev, cont);
1149 * request @column from table @table on device @dev. On success, the column
1150 * data will be available in dev->resp->tok[4]
1152 static int generic_get_column(struct opal_dev *dev, const u8 *table,
1157 err = cmd_start(dev, table, opalmethod[OPAL_GET]);
1159 add_token_u8(&err, dev, OPAL_STARTLIST);
1161 add_token_u8(&err, dev, OPAL_STARTNAME);
1162 add_token_u8(&err, dev, OPAL_STARTCOLUMN);
1163 add_token_u64(&err, dev, column);
1164 add_token_u8(&err, dev, OPAL_ENDNAME);
1166 add_token_u8(&err, dev, OPAL_STARTNAME);
1167 add_token_u8(&err, dev, OPAL_ENDCOLUMN);
1168 add_token_u64(&err, dev, column);
1169 add_token_u8(&err, dev, OPAL_ENDNAME);
1171 add_token_u8(&err, dev, OPAL_ENDLIST);
1176 return finalize_and_send(dev, parse_and_check_status);
1180 * see TCG SAS 5.3.2.3 for a description of the available columns
1182 * the result is provided in dev->resp->tok[4]
1184 static int generic_get_table_info(struct opal_dev *dev, const u8 *table_uid,
1187 u8 uid[OPAL_UID_LENGTH];
1188 const unsigned int half = OPAL_UID_LENGTH_HALF;
1190 /* sed-opal UIDs can be split in two halves:
1191 * first: actual table index
1192 * second: relative index in the table
1193 * so we have to get the first half of the OPAL_TABLE_TABLE and use the
1194 * first part of the target table as relative index into that table
1196 memcpy(uid, opaluid[OPAL_TABLE_TABLE], half);
1197 memcpy(uid + half, table_uid, half);
1199 return generic_get_column(dev, uid, column);
1202 static int gen_key(struct opal_dev *dev, void *data)
1204 u8 uid[OPAL_UID_LENGTH];
1207 memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
1208 kfree(dev->prev_data);
1209 dev->prev_data = NULL;
1211 err = cmd_start(dev, uid, opalmethod[OPAL_GENKEY]);
1214 pr_debug("Error building gen key command\n");
1219 return finalize_and_send(dev, parse_and_check_status);
1222 static int get_active_key_cont(struct opal_dev *dev)
1224 const char *activekey;
1228 error = parse_and_check_status(dev);
1232 keylen = response_get_string(&dev->parsed, 4, &activekey);
1234 pr_debug("%s: Couldn't extract the Activekey from the response\n",
1236 return OPAL_INVAL_PARAM;
1239 dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
1241 if (!dev->prev_data)
1244 dev->prev_d_len = keylen;
1249 static int get_active_key(struct opal_dev *dev, void *data)
1251 u8 uid[OPAL_UID_LENGTH];
1255 err = build_locking_range(uid, sizeof(uid), *lr);
1259 err = generic_get_column(dev, uid, OPAL_ACTIVEKEY);
1263 return get_active_key_cont(dev);
1266 static int generic_table_write_data(struct opal_dev *dev, const u64 data,
1267 u64 offset, u64 size, const u8 *uid)
1269 const u8 __user *src = (u8 __user *)(uintptr_t)data;
1275 /* do we fit in the available space? */
1276 err = generic_get_table_info(dev, uid, OPAL_TABLE_ROWS);
1278 pr_debug("Couldn't get the table size\n");
1282 len = response_get_u64(&dev->parsed, 4);
1283 if (size > len || offset > len - size) {
1284 pr_debug("Does not fit in the table (%llu vs. %llu)\n",
1285 offset + size, len);
1289 /* do the actual transmission(s) */
1290 while (off < size) {
1291 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1292 add_token_u8(&err, dev, OPAL_STARTNAME);
1293 add_token_u8(&err, dev, OPAL_WHERE);
1294 add_token_u64(&err, dev, offset + off);
1295 add_token_u8(&err, dev, OPAL_ENDNAME);
1297 add_token_u8(&err, dev, OPAL_STARTNAME);
1298 add_token_u8(&err, dev, OPAL_VALUES);
1301 * The bytestring header is either 1 or 2 bytes, so assume 2.
1302 * There also needs to be enough space to accommodate the
1303 * trailing OPAL_ENDNAME (1 byte) and tokens added by
1306 len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
1307 (size_t)(size - off));
1308 pr_debug("Write bytes %zu+%llu/%llu\n", off, len, size);
1310 dst = add_bytestring_header(&err, dev, len);
1314 if (copy_from_user(dst, src + off, len)) {
1321 add_token_u8(&err, dev, OPAL_ENDNAME);
1325 err = finalize_and_send(dev, parse_and_check_status);
1335 static int generic_lr_enable_disable(struct opal_dev *dev,
1336 u8 *uid, bool rle, bool wle,
1341 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1343 add_token_u8(&err, dev, OPAL_STARTNAME);
1344 add_token_u8(&err, dev, OPAL_VALUES);
1345 add_token_u8(&err, dev, OPAL_STARTLIST);
1347 add_token_u8(&err, dev, OPAL_STARTNAME);
1348 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1349 add_token_u8(&err, dev, rle);
1350 add_token_u8(&err, dev, OPAL_ENDNAME);
1352 add_token_u8(&err, dev, OPAL_STARTNAME);
1353 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1354 add_token_u8(&err, dev, wle);
1355 add_token_u8(&err, dev, OPAL_ENDNAME);
1357 add_token_u8(&err, dev, OPAL_STARTNAME);
1358 add_token_u8(&err, dev, OPAL_READLOCKED);
1359 add_token_u8(&err, dev, rl);
1360 add_token_u8(&err, dev, OPAL_ENDNAME);
1362 add_token_u8(&err, dev, OPAL_STARTNAME);
1363 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1364 add_token_u8(&err, dev, wl);
1365 add_token_u8(&err, dev, OPAL_ENDNAME);
1367 add_token_u8(&err, dev, OPAL_ENDLIST);
1368 add_token_u8(&err, dev, OPAL_ENDNAME);
1373 static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
1374 struct opal_user_lr_setup *setup)
1378 err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
1381 pr_debug("Failed to create enable global lr command\n");
1386 static int setup_locking_range(struct opal_dev *dev, void *data)
1388 u8 uid[OPAL_UID_LENGTH];
1389 struct opal_user_lr_setup *setup = data;
1393 lr = setup->session.opal_key.lr;
1394 err = build_locking_range(uid, sizeof(uid), lr);
1399 err = enable_global_lr(dev, uid, setup);
1401 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1403 add_token_u8(&err, dev, OPAL_STARTNAME);
1404 add_token_u8(&err, dev, OPAL_VALUES);
1405 add_token_u8(&err, dev, OPAL_STARTLIST);
1407 add_token_u8(&err, dev, OPAL_STARTNAME);
1408 add_token_u8(&err, dev, OPAL_RANGESTART);
1409 add_token_u64(&err, dev, setup->range_start);
1410 add_token_u8(&err, dev, OPAL_ENDNAME);
1412 add_token_u8(&err, dev, OPAL_STARTNAME);
1413 add_token_u8(&err, dev, OPAL_RANGELENGTH);
1414 add_token_u64(&err, dev, setup->range_length);
1415 add_token_u8(&err, dev, OPAL_ENDNAME);
1417 add_token_u8(&err, dev, OPAL_STARTNAME);
1418 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1419 add_token_u64(&err, dev, !!setup->RLE);
1420 add_token_u8(&err, dev, OPAL_ENDNAME);
1422 add_token_u8(&err, dev, OPAL_STARTNAME);
1423 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1424 add_token_u64(&err, dev, !!setup->WLE);
1425 add_token_u8(&err, dev, OPAL_ENDNAME);
1427 add_token_u8(&err, dev, OPAL_ENDLIST);
1428 add_token_u8(&err, dev, OPAL_ENDNAME);
1431 pr_debug("Error building Setup Locking range command.\n");
1435 return finalize_and_send(dev, parse_and_check_status);
1438 static int start_generic_opal_session(struct opal_dev *dev,
1440 enum opal_uid sp_type,
1447 if (key == NULL && auth != OPAL_ANYBODY_UID)
1448 return OPAL_INVAL_PARAM;
1450 hsn = GENERIC_HOST_SESSION_NUM;
1451 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1452 opalmethod[OPAL_STARTSESSION]);
1454 add_token_u64(&err, dev, hsn);
1455 add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
1456 add_token_u8(&err, dev, 1);
1459 case OPAL_ANYBODY_UID:
1461 case OPAL_ADMIN1_UID:
1464 add_token_u8(&err, dev, OPAL_STARTNAME);
1465 add_token_u8(&err, dev, 0); /* HostChallenge */
1466 add_token_bytestring(&err, dev, key, key_len);
1467 add_token_u8(&err, dev, OPAL_ENDNAME);
1468 add_token_u8(&err, dev, OPAL_STARTNAME);
1469 add_token_u8(&err, dev, 3); /* HostSignAuth */
1470 add_token_bytestring(&err, dev, opaluid[auth],
1472 add_token_u8(&err, dev, OPAL_ENDNAME);
1475 pr_debug("Cannot start Admin SP session with auth %d\n", auth);
1476 return OPAL_INVAL_PARAM;
1480 pr_debug("Error building start adminsp session command.\n");
1484 return finalize_and_send(dev, start_opal_session_cont);
1487 static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
1489 return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
1490 OPAL_ADMINSP_UID, NULL, 0);
1493 static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
1496 const u8 *key = dev->prev_data;
1499 const struct opal_key *okey = data;
1501 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1506 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1508 key, dev->prev_d_len);
1510 dev->prev_data = NULL;
1516 static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
1518 struct opal_key *key = data;
1520 return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
1522 key->key, key->key_len);
1525 static int start_PSID_opal_session(struct opal_dev *dev, void *data)
1527 const struct opal_key *okey = data;
1529 return start_generic_opal_session(dev, OPAL_PSID_UID,
1535 static int start_auth_opal_session(struct opal_dev *dev, void *data)
1537 struct opal_session_info *session = data;
1538 u8 lk_ul_user[OPAL_UID_LENGTH];
1539 size_t keylen = session->opal_key.key_len;
1542 u8 *key = session->opal_key.key;
1543 u32 hsn = GENERIC_HOST_SESSION_NUM;
1546 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1547 session->opal_key.lr);
1548 else if (session->who != OPAL_ADMIN1 && !session->sum)
1549 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1552 memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
1557 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1558 opalmethod[OPAL_STARTSESSION]);
1560 add_token_u64(&err, dev, hsn);
1561 add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
1563 add_token_u8(&err, dev, 1);
1564 add_token_u8(&err, dev, OPAL_STARTNAME);
1565 add_token_u8(&err, dev, 0);
1566 add_token_bytestring(&err, dev, key, keylen);
1567 add_token_u8(&err, dev, OPAL_ENDNAME);
1568 add_token_u8(&err, dev, OPAL_STARTNAME);
1569 add_token_u8(&err, dev, 3);
1570 add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
1571 add_token_u8(&err, dev, OPAL_ENDNAME);
1574 pr_debug("Error building STARTSESSION command.\n");
1578 return finalize_and_send(dev, start_opal_session_cont);
1581 static int revert_tper(struct opal_dev *dev, void *data)
1585 err = cmd_start(dev, opaluid[OPAL_ADMINSP_UID],
1586 opalmethod[OPAL_REVERT]);
1588 pr_debug("Error building REVERT TPER command.\n");
1592 return finalize_and_send(dev, parse_and_check_status);
1595 static int internal_activate_user(struct opal_dev *dev, void *data)
1597 struct opal_session_info *session = data;
1598 u8 uid[OPAL_UID_LENGTH];
1601 memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1602 uid[7] = session->who;
1604 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1605 add_token_u8(&err, dev, OPAL_STARTNAME);
1606 add_token_u8(&err, dev, OPAL_VALUES);
1607 add_token_u8(&err, dev, OPAL_STARTLIST);
1608 add_token_u8(&err, dev, OPAL_STARTNAME);
1609 add_token_u8(&err, dev, 5); /* Enabled */
1610 add_token_u8(&err, dev, OPAL_TRUE);
1611 add_token_u8(&err, dev, OPAL_ENDNAME);
1612 add_token_u8(&err, dev, OPAL_ENDLIST);
1613 add_token_u8(&err, dev, OPAL_ENDNAME);
1616 pr_debug("Error building Activate UserN command.\n");
1620 return finalize_and_send(dev, parse_and_check_status);
1623 static int erase_locking_range(struct opal_dev *dev, void *data)
1625 struct opal_session_info *session = data;
1626 u8 uid[OPAL_UID_LENGTH];
1629 if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
1632 err = cmd_start(dev, uid, opalmethod[OPAL_ERASE]);
1635 pr_debug("Error building Erase Locking Range Command.\n");
1639 return finalize_and_send(dev, parse_and_check_status);
1642 static int set_mbr_done(struct opal_dev *dev, void *data)
1644 u8 *mbr_done_tf = data;
1647 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1648 opalmethod[OPAL_SET]);
1650 add_token_u8(&err, dev, OPAL_STARTNAME);
1651 add_token_u8(&err, dev, OPAL_VALUES);
1652 add_token_u8(&err, dev, OPAL_STARTLIST);
1653 add_token_u8(&err, dev, OPAL_STARTNAME);
1654 add_token_u8(&err, dev, OPAL_MBRDONE);
1655 add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
1656 add_token_u8(&err, dev, OPAL_ENDNAME);
1657 add_token_u8(&err, dev, OPAL_ENDLIST);
1658 add_token_u8(&err, dev, OPAL_ENDNAME);
1661 pr_debug("Error Building set MBR Done command\n");
1665 return finalize_and_send(dev, parse_and_check_status);
1668 static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
1670 u8 *mbr_en_dis = data;
1673 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1674 opalmethod[OPAL_SET]);
1676 add_token_u8(&err, dev, OPAL_STARTNAME);
1677 add_token_u8(&err, dev, OPAL_VALUES);
1678 add_token_u8(&err, dev, OPAL_STARTLIST);
1679 add_token_u8(&err, dev, OPAL_STARTNAME);
1680 add_token_u8(&err, dev, OPAL_MBRENABLE);
1681 add_token_u8(&err, dev, *mbr_en_dis);
1682 add_token_u8(&err, dev, OPAL_ENDNAME);
1683 add_token_u8(&err, dev, OPAL_ENDLIST);
1684 add_token_u8(&err, dev, OPAL_ENDNAME);
1687 pr_debug("Error Building set MBR done command\n");
1691 return finalize_and_send(dev, parse_and_check_status);
1694 static int write_shadow_mbr(struct opal_dev *dev, void *data)
1696 struct opal_shadow_mbr *shadow = data;
1698 return generic_table_write_data(dev, shadow->data, shadow->offset,
1699 shadow->size, opaluid[OPAL_MBR]);
1702 static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
1703 struct opal_dev *dev)
1707 err = cmd_start(dev, cpin_uid, opalmethod[OPAL_SET]);
1709 add_token_u8(&err, dev, OPAL_STARTNAME);
1710 add_token_u8(&err, dev, OPAL_VALUES);
1711 add_token_u8(&err, dev, OPAL_STARTLIST);
1712 add_token_u8(&err, dev, OPAL_STARTNAME);
1713 add_token_u8(&err, dev, OPAL_PIN);
1714 add_token_bytestring(&err, dev, key, key_len);
1715 add_token_u8(&err, dev, OPAL_ENDNAME);
1716 add_token_u8(&err, dev, OPAL_ENDLIST);
1717 add_token_u8(&err, dev, OPAL_ENDNAME);
1722 static int set_new_pw(struct opal_dev *dev, void *data)
1724 u8 cpin_uid[OPAL_UID_LENGTH];
1725 struct opal_session_info *usr = data;
1727 memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
1729 if (usr->who != OPAL_ADMIN1) {
1732 cpin_uid[7] = usr->opal_key.lr + 1;
1734 cpin_uid[7] = usr->who;
1737 if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
1739 pr_debug("Error building set password command.\n");
1743 return finalize_and_send(dev, parse_and_check_status);
1746 static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
1748 u8 cpin_uid[OPAL_UID_LENGTH];
1749 struct opal_key *key = data;
1751 memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
1753 if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
1754 pr_debug("Error building Set SID cpin\n");
1757 return finalize_and_send(dev, parse_and_check_status);
1760 static int add_user_to_lr(struct opal_dev *dev, void *data)
1762 u8 lr_buffer[OPAL_UID_LENGTH];
1763 u8 user_uid[OPAL_UID_LENGTH];
1764 struct opal_lock_unlock *lkul = data;
1767 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
1770 if (lkul->l_state == OPAL_RW)
1771 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
1774 lr_buffer[7] = lkul->session.opal_key.lr;
1776 memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1778 user_uid[7] = lkul->session.who;
1780 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1782 add_token_u8(&err, dev, OPAL_STARTNAME);
1783 add_token_u8(&err, dev, OPAL_VALUES);
1785 add_token_u8(&err, dev, OPAL_STARTLIST);
1786 add_token_u8(&err, dev, OPAL_STARTNAME);
1787 add_token_u8(&err, dev, 3);
1789 add_token_u8(&err, dev, OPAL_STARTLIST);
1792 add_token_u8(&err, dev, OPAL_STARTNAME);
1793 add_token_bytestring(&err, dev,
1794 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1796 add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1797 add_token_u8(&err, dev, OPAL_ENDNAME);
1800 add_token_u8(&err, dev, OPAL_STARTNAME);
1801 add_token_bytestring(&err, dev,
1802 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1804 add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1805 add_token_u8(&err, dev, OPAL_ENDNAME);
1808 add_token_u8(&err, dev, OPAL_STARTNAME);
1809 add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
1811 add_token_u8(&err, dev, 1);
1812 add_token_u8(&err, dev, OPAL_ENDNAME);
1815 add_token_u8(&err, dev, OPAL_ENDLIST);
1816 add_token_u8(&err, dev, OPAL_ENDNAME);
1817 add_token_u8(&err, dev, OPAL_ENDLIST);
1818 add_token_u8(&err, dev, OPAL_ENDNAME);
1821 pr_debug("Error building add user to locking range command.\n");
1825 return finalize_and_send(dev, parse_and_check_status);
1828 static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
1830 u8 lr_buffer[OPAL_UID_LENGTH];
1831 struct opal_lock_unlock *lkul = data;
1832 u8 read_locked = 1, write_locked = 1;
1835 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1836 lkul->session.opal_key.lr) < 0)
1839 switch (lkul->l_state) {
1849 /* vars are initialized to locked */
1852 pr_debug("Tried to set an invalid locking state... returning to uland\n");
1853 return OPAL_INVAL_PARAM;
1856 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1858 add_token_u8(&err, dev, OPAL_STARTNAME);
1859 add_token_u8(&err, dev, OPAL_VALUES);
1860 add_token_u8(&err, dev, OPAL_STARTLIST);
1862 add_token_u8(&err, dev, OPAL_STARTNAME);
1863 add_token_u8(&err, dev, OPAL_READLOCKED);
1864 add_token_u8(&err, dev, read_locked);
1865 add_token_u8(&err, dev, OPAL_ENDNAME);
1867 add_token_u8(&err, dev, OPAL_STARTNAME);
1868 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1869 add_token_u8(&err, dev, write_locked);
1870 add_token_u8(&err, dev, OPAL_ENDNAME);
1872 add_token_u8(&err, dev, OPAL_ENDLIST);
1873 add_token_u8(&err, dev, OPAL_ENDNAME);
1876 pr_debug("Error building SET command.\n");
1880 return finalize_and_send(dev, parse_and_check_status);
1884 static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
1886 u8 lr_buffer[OPAL_UID_LENGTH];
1887 u8 read_locked = 1, write_locked = 1;
1888 struct opal_lock_unlock *lkul = data;
1891 clear_opal_cmd(dev);
1892 set_comid(dev, dev->comid);
1894 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1895 lkul->session.opal_key.lr) < 0)
1898 switch (lkul->l_state) {
1908 /* vars are initialized to locked */
1911 pr_debug("Tried to set an invalid locking state.\n");
1912 return OPAL_INVAL_PARAM;
1914 ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
1915 read_locked, write_locked);
1918 pr_debug("Error building SET command.\n");
1922 return finalize_and_send(dev, parse_and_check_status);
1925 static int activate_lsp(struct opal_dev *dev, void *data)
1927 struct opal_lr_act *opal_act = data;
1928 u8 user_lr[OPAL_UID_LENGTH];
1931 err = cmd_start(dev, opaluid[OPAL_LOCKINGSP_UID],
1932 opalmethod[OPAL_ACTIVATE]);
1934 if (opal_act->sum) {
1935 err = build_locking_range(user_lr, sizeof(user_lr),
1940 add_token_u8(&err, dev, OPAL_STARTNAME);
1941 add_token_u64(&err, dev, OPAL_SUM_SET_LIST);
1943 add_token_u8(&err, dev, OPAL_STARTLIST);
1944 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1945 for (i = 1; i < opal_act->num_lrs; i++) {
1946 user_lr[7] = opal_act->lr[i];
1947 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1949 add_token_u8(&err, dev, OPAL_ENDLIST);
1950 add_token_u8(&err, dev, OPAL_ENDNAME);
1954 pr_debug("Error building Activate LockingSP command.\n");
1958 return finalize_and_send(dev, parse_and_check_status);
1961 /* Determine if we're in the Manufactured Inactive or Active state */
1962 static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
1967 err = generic_get_column(dev, opaluid[OPAL_LOCKINGSP_UID],
1972 lc_status = response_get_u64(&dev->parsed, 4);
1973 /* 0x08 is Manufactured Inactive */
1974 /* 0x09 is Manufactured */
1975 if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
1976 pr_debug("Couldn't determine the status of the Lifecycle state\n");
1983 static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
1985 const char *msid_pin;
1989 err = generic_get_column(dev, opaluid[OPAL_C_PIN_MSID], OPAL_PIN);
1993 strlen = response_get_string(&dev->parsed, 4, &msid_pin);
1995 pr_debug("Couldn't extract MSID_CPIN from response\n");
1996 return OPAL_INVAL_PARAM;
1999 dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
2000 if (!dev->prev_data)
2003 dev->prev_d_len = strlen;
2008 static int write_table_data(struct opal_dev *dev, void *data)
2010 struct opal_read_write_table *write_tbl = data;
2012 return generic_table_write_data(dev, write_tbl->data, write_tbl->offset,
2013 write_tbl->size, write_tbl->table_uid);
2016 static int read_table_data_cont(struct opal_dev *dev)
2019 const char *data_read;
2021 err = parse_and_check_status(dev);
2025 dev->prev_d_len = response_get_string(&dev->parsed, 1, &data_read);
2026 dev->prev_data = (void *)data_read;
2027 if (!dev->prev_data) {
2028 pr_debug("%s: Couldn't read data from the table.\n", __func__);
2029 return OPAL_INVAL_PARAM;
2036 * IO_BUFFER_LENGTH = 2048
2037 * sizeof(header) = 56
2038 * No. of Token Bytes in the Response = 11
2039 * MAX size of data that can be carried in response buffer
2040 * at a time is : 2048 - (56 + 11) = 1981 = 0x7BD.
2042 #define OPAL_MAX_READ_TABLE (0x7BD)
2044 static int read_table_data(struct opal_dev *dev, void *data)
2046 struct opal_read_write_table *read_tbl = data;
2048 size_t off = 0, max_read_size = OPAL_MAX_READ_TABLE;
2050 u64 offset = read_tbl->offset, read_size = read_tbl->size - 1;
2053 err = generic_get_table_info(dev, read_tbl->table_uid, OPAL_TABLE_ROWS);
2055 pr_debug("Couldn't get the table size\n");
2059 table_len = response_get_u64(&dev->parsed, 4);
2061 /* Check if the user is trying to read from the table limits */
2062 if (read_size > table_len || offset > table_len - read_size) {
2063 pr_debug("Read size exceeds the Table size limits (%llu vs. %llu)\n",
2064 offset + read_size, table_len);
2068 while (off < read_size) {
2069 err = cmd_start(dev, read_tbl->table_uid, opalmethod[OPAL_GET]);
2071 add_token_u8(&err, dev, OPAL_STARTLIST);
2072 add_token_u8(&err, dev, OPAL_STARTNAME);
2073 add_token_u8(&err, dev, OPAL_STARTROW);
2074 add_token_u64(&err, dev, offset + off); /* start row value */
2075 add_token_u8(&err, dev, OPAL_ENDNAME);
2077 add_token_u8(&err, dev, OPAL_STARTNAME);
2078 add_token_u8(&err, dev, OPAL_ENDROW);
2080 len = min(max_read_size, (size_t)(read_size - off));
2081 add_token_u64(&err, dev, offset + off + len); /* end row value
2083 add_token_u8(&err, dev, OPAL_ENDNAME);
2084 add_token_u8(&err, dev, OPAL_ENDLIST);
2087 pr_debug("Error building read table data command.\n");
2091 err = finalize_and_send(dev, read_table_data_cont);
2095 /* len+1: This includes the NULL terminator at the end*/
2096 if (dev->prev_d_len > len + 1) {
2101 dst = (u8 __user *)(uintptr_t)read_tbl->data;
2102 if (copy_to_user(dst + off, dev->prev_data, dev->prev_d_len)) {
2103 pr_debug("Error copying data to userspace\n");
2107 dev->prev_data = NULL;
2115 static int end_opal_session(struct opal_dev *dev, void *data)
2119 clear_opal_cmd(dev);
2120 set_comid(dev, dev->comid);
2121 add_token_u8(&err, dev, OPAL_ENDOFSESSION);
2126 return finalize_and_send(dev, end_session_cont);
2129 static int end_opal_session_error(struct opal_dev *dev)
2131 const struct opal_step error_end_session = {
2135 return execute_step(dev, &error_end_session, 0);
2138 static inline void setup_opal_dev(struct opal_dev *dev)
2142 dev->prev_data = NULL;
2145 static int check_opal_support(struct opal_dev *dev)
2149 mutex_lock(&dev->dev_lock);
2150 setup_opal_dev(dev);
2151 ret = opal_discovery0_step(dev);
2153 dev->flags |= OPAL_FL_SUPPORTED;
2154 mutex_unlock(&dev->dev_lock);
2159 static void clean_opal_dev(struct opal_dev *dev)
2162 struct opal_suspend_data *suspend, *next;
2164 mutex_lock(&dev->dev_lock);
2165 list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
2166 list_del(&suspend->node);
2169 mutex_unlock(&dev->dev_lock);
2172 void free_opal_dev(struct opal_dev *dev)
2177 clean_opal_dev(dev);
2182 EXPORT_SYMBOL(free_opal_dev);
2184 struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
2186 struct opal_dev *dev;
2188 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
2193 * Presumably DMA-able buffers must be cache-aligned. Kmalloc makes
2194 * sure the allocated buffer is DMA-safe in that regard.
2196 dev->cmd = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2200 dev->resp = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2204 INIT_LIST_HEAD(&dev->unlk_lst);
2205 mutex_init(&dev->dev_lock);
2208 dev->send_recv = send_recv;
2209 if (check_opal_support(dev) != 0) {
2210 pr_debug("Opal is not supported on this device\n");
2227 EXPORT_SYMBOL(init_opal_dev);
2229 static int opal_secure_erase_locking_range(struct opal_dev *dev,
2230 struct opal_session_info *opal_session)
2232 const struct opal_step erase_steps[] = {
2233 { start_auth_opal_session, opal_session },
2234 { get_active_key, &opal_session->opal_key.lr },
2236 { end_opal_session, }
2240 mutex_lock(&dev->dev_lock);
2241 setup_opal_dev(dev);
2242 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2243 mutex_unlock(&dev->dev_lock);
2248 static int opal_erase_locking_range(struct opal_dev *dev,
2249 struct opal_session_info *opal_session)
2251 const struct opal_step erase_steps[] = {
2252 { start_auth_opal_session, opal_session },
2253 { erase_locking_range, opal_session },
2254 { end_opal_session, }
2258 mutex_lock(&dev->dev_lock);
2259 setup_opal_dev(dev);
2260 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2261 mutex_unlock(&dev->dev_lock);
2266 static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
2267 struct opal_mbr_data *opal_mbr)
2269 u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
2270 OPAL_TRUE : OPAL_FALSE;
2272 const struct opal_step mbr_steps[] = {
2273 { start_admin1LSP_opal_session, &opal_mbr->key },
2274 { set_mbr_done, &enable_disable },
2275 { end_opal_session, },
2276 { start_admin1LSP_opal_session, &opal_mbr->key },
2277 { set_mbr_enable_disable, &enable_disable },
2278 { end_opal_session, }
2282 if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
2283 opal_mbr->enable_disable != OPAL_MBR_DISABLE)
2286 mutex_lock(&dev->dev_lock);
2287 setup_opal_dev(dev);
2288 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2289 mutex_unlock(&dev->dev_lock);
2294 static int opal_set_mbr_done(struct opal_dev *dev,
2295 struct opal_mbr_done *mbr_done)
2297 u8 mbr_done_tf = mbr_done->done_flag == OPAL_MBR_DONE ?
2298 OPAL_TRUE : OPAL_FALSE;
2300 const struct opal_step mbr_steps[] = {
2301 { start_admin1LSP_opal_session, &mbr_done->key },
2302 { set_mbr_done, &mbr_done_tf },
2303 { end_opal_session, }
2307 if (mbr_done->done_flag != OPAL_MBR_DONE &&
2308 mbr_done->done_flag != OPAL_MBR_NOT_DONE)
2311 mutex_lock(&dev->dev_lock);
2312 setup_opal_dev(dev);
2313 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2314 mutex_unlock(&dev->dev_lock);
2319 static int opal_write_shadow_mbr(struct opal_dev *dev,
2320 struct opal_shadow_mbr *info)
2322 const struct opal_step mbr_steps[] = {
2323 { start_admin1LSP_opal_session, &info->key },
2324 { write_shadow_mbr, info },
2325 { end_opal_session, }
2329 if (info->size == 0)
2332 mutex_lock(&dev->dev_lock);
2333 setup_opal_dev(dev);
2334 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2335 mutex_unlock(&dev->dev_lock);
2340 static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
2342 struct opal_suspend_data *suspend;
2344 suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
2348 suspend->unlk = *lk_unlk;
2349 suspend->lr = lk_unlk->session.opal_key.lr;
2351 mutex_lock(&dev->dev_lock);
2352 setup_opal_dev(dev);
2353 add_suspend_info(dev, suspend);
2354 mutex_unlock(&dev->dev_lock);
2359 static int opal_add_user_to_lr(struct opal_dev *dev,
2360 struct opal_lock_unlock *lk_unlk)
2362 const struct opal_step steps[] = {
2363 { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
2364 { add_user_to_lr, lk_unlk },
2365 { end_opal_session, }
2369 if (lk_unlk->l_state != OPAL_RO &&
2370 lk_unlk->l_state != OPAL_RW) {
2371 pr_debug("Locking state was not RO or RW\n");
2375 if (lk_unlk->session.who < OPAL_USER1 ||
2376 lk_unlk->session.who > OPAL_USER9) {
2377 pr_debug("Authority was not within the range of users: %d\n",
2378 lk_unlk->session.who);
2382 if (lk_unlk->session.sum) {
2383 pr_debug("%s not supported in sum. Use setup locking range\n",
2388 mutex_lock(&dev->dev_lock);
2389 setup_opal_dev(dev);
2390 ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
2391 mutex_unlock(&dev->dev_lock);
2396 static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal, bool psid)
2398 /* controller will terminate session */
2399 const struct opal_step revert_steps[] = {
2400 { start_SIDASP_opal_session, opal },
2403 const struct opal_step psid_revert_steps[] = {
2404 { start_PSID_opal_session, opal },
2410 mutex_lock(&dev->dev_lock);
2411 setup_opal_dev(dev);
2413 ret = execute_steps(dev, psid_revert_steps,
2414 ARRAY_SIZE(psid_revert_steps));
2416 ret = execute_steps(dev, revert_steps,
2417 ARRAY_SIZE(revert_steps));
2418 mutex_unlock(&dev->dev_lock);
2421 * If we successfully reverted lets clean
2422 * any saved locking ranges.
2425 clean_opal_dev(dev);
2430 static int __opal_lock_unlock(struct opal_dev *dev,
2431 struct opal_lock_unlock *lk_unlk)
2433 const struct opal_step unlock_steps[] = {
2434 { start_auth_opal_session, &lk_unlk->session },
2435 { lock_unlock_locking_range, lk_unlk },
2436 { end_opal_session, }
2438 const struct opal_step unlock_sum_steps[] = {
2439 { start_auth_opal_session, &lk_unlk->session },
2440 { lock_unlock_locking_range_sum, lk_unlk },
2441 { end_opal_session, }
2444 if (lk_unlk->session.sum)
2445 return execute_steps(dev, unlock_sum_steps,
2446 ARRAY_SIZE(unlock_sum_steps));
2448 return execute_steps(dev, unlock_steps,
2449 ARRAY_SIZE(unlock_steps));
2452 static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
2454 u8 mbr_done_tf = OPAL_TRUE;
2455 const struct opal_step mbrdone_step[] = {
2456 { start_admin1LSP_opal_session, key },
2457 { set_mbr_done, &mbr_done_tf },
2458 { end_opal_session, }
2461 return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
2464 static void opal_lock_check_for_saved_key(struct opal_dev *dev,
2465 struct opal_lock_unlock *lk_unlk)
2467 struct opal_suspend_data *iter;
2469 if (lk_unlk->l_state != OPAL_LK ||
2470 lk_unlk->session.opal_key.key_len > 0)
2474 * Usually when closing a crypto device (eg: dm-crypt with LUKS) the
2475 * volume key is not required, as it requires root privileges anyway,
2476 * and root can deny access to a disk in many ways regardless.
2477 * Requiring the volume key to lock the device is a peculiarity of the
2478 * OPAL specification. Given we might already have saved the key if
2479 * the user requested it via the 'IOC_OPAL_SAVE' ioctl, we can use
2480 * that key to lock the device if no key was provided here, the
2481 * locking range matches and the appropriate flag was passed with
2483 * This allows integrating OPAL with tools and libraries that are used
2484 * to the common behaviour and do not ask for the volume key when
2487 setup_opal_dev(dev);
2488 list_for_each_entry(iter, &dev->unlk_lst, node) {
2489 if ((iter->unlk.flags & OPAL_SAVE_FOR_LOCK) &&
2490 iter->lr == lk_unlk->session.opal_key.lr &&
2491 iter->unlk.session.opal_key.key_len > 0) {
2492 lk_unlk->session.opal_key.key_len =
2493 iter->unlk.session.opal_key.key_len;
2494 memcpy(lk_unlk->session.opal_key.key,
2495 iter->unlk.session.opal_key.key,
2496 iter->unlk.session.opal_key.key_len);
2502 static int opal_lock_unlock(struct opal_dev *dev,
2503 struct opal_lock_unlock *lk_unlk)
2507 if (lk_unlk->session.who > OPAL_USER9)
2510 mutex_lock(&dev->dev_lock);
2511 opal_lock_check_for_saved_key(dev, lk_unlk);
2512 ret = __opal_lock_unlock(dev, lk_unlk);
2513 mutex_unlock(&dev->dev_lock);
2518 static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
2520 const struct opal_step owner_steps[] = {
2521 { start_anybodyASP_opal_session, },
2522 { get_msid_cpin_pin, },
2523 { end_opal_session, },
2524 { start_SIDASP_opal_session, opal },
2525 { set_sid_cpin_pin, opal },
2526 { end_opal_session, }
2533 mutex_lock(&dev->dev_lock);
2534 setup_opal_dev(dev);
2535 ret = execute_steps(dev, owner_steps, ARRAY_SIZE(owner_steps));
2536 mutex_unlock(&dev->dev_lock);
2541 static int opal_activate_lsp(struct opal_dev *dev,
2542 struct opal_lr_act *opal_lr_act)
2544 const struct opal_step active_steps[] = {
2545 { start_SIDASP_opal_session, &opal_lr_act->key },
2546 { get_lsp_lifecycle, },
2547 { activate_lsp, opal_lr_act },
2548 { end_opal_session, }
2552 if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
2555 mutex_lock(&dev->dev_lock);
2556 setup_opal_dev(dev);
2557 ret = execute_steps(dev, active_steps, ARRAY_SIZE(active_steps));
2558 mutex_unlock(&dev->dev_lock);
2563 static int opal_setup_locking_range(struct opal_dev *dev,
2564 struct opal_user_lr_setup *opal_lrs)
2566 const struct opal_step lr_steps[] = {
2567 { start_auth_opal_session, &opal_lrs->session },
2568 { setup_locking_range, opal_lrs },
2569 { end_opal_session, }
2573 mutex_lock(&dev->dev_lock);
2574 setup_opal_dev(dev);
2575 ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
2576 mutex_unlock(&dev->dev_lock);
2581 static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
2583 const struct opal_step pw_steps[] = {
2584 { start_auth_opal_session, &opal_pw->session },
2585 { set_new_pw, &opal_pw->new_user_pw },
2586 { end_opal_session, }
2590 if (opal_pw->session.who > OPAL_USER9 ||
2591 opal_pw->new_user_pw.who > OPAL_USER9)
2594 mutex_lock(&dev->dev_lock);
2595 setup_opal_dev(dev);
2596 ret = execute_steps(dev, pw_steps, ARRAY_SIZE(pw_steps));
2597 mutex_unlock(&dev->dev_lock);
2602 static int opal_activate_user(struct opal_dev *dev,
2603 struct opal_session_info *opal_session)
2605 const struct opal_step act_steps[] = {
2606 { start_admin1LSP_opal_session, &opal_session->opal_key },
2607 { internal_activate_user, opal_session },
2608 { end_opal_session, }
2612 /* We can't activate Admin1 it's active as manufactured */
2613 if (opal_session->who < OPAL_USER1 ||
2614 opal_session->who > OPAL_USER9) {
2615 pr_debug("Who was not a valid user: %d\n", opal_session->who);
2619 mutex_lock(&dev->dev_lock);
2620 setup_opal_dev(dev);
2621 ret = execute_steps(dev, act_steps, ARRAY_SIZE(act_steps));
2622 mutex_unlock(&dev->dev_lock);
2627 bool opal_unlock_from_suspend(struct opal_dev *dev)
2629 struct opal_suspend_data *suspend;
2630 bool was_failure = false;
2636 if (!(dev->flags & OPAL_FL_SUPPORTED))
2639 mutex_lock(&dev->dev_lock);
2640 setup_opal_dev(dev);
2642 list_for_each_entry(suspend, &dev->unlk_lst, node) {
2646 ret = __opal_lock_unlock(dev, &suspend->unlk);
2648 pr_debug("Failed to unlock LR %hhu with sum %d\n",
2649 suspend->unlk.session.opal_key.lr,
2650 suspend->unlk.session.sum);
2654 if (dev->flags & OPAL_FL_MBR_ENABLED) {
2655 ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
2657 pr_debug("Failed to set MBR Done in S3 resume\n");
2660 mutex_unlock(&dev->dev_lock);
2664 EXPORT_SYMBOL(opal_unlock_from_suspend);
2666 static int opal_read_table(struct opal_dev *dev,
2667 struct opal_read_write_table *rw_tbl)
2669 const struct opal_step read_table_steps[] = {
2670 { start_admin1LSP_opal_session, &rw_tbl->key },
2671 { read_table_data, rw_tbl },
2672 { end_opal_session, }
2679 return execute_steps(dev, read_table_steps,
2680 ARRAY_SIZE(read_table_steps));
2683 static int opal_write_table(struct opal_dev *dev,
2684 struct opal_read_write_table *rw_tbl)
2686 const struct opal_step write_table_steps[] = {
2687 { start_admin1LSP_opal_session, &rw_tbl->key },
2688 { write_table_data, rw_tbl },
2689 { end_opal_session, }
2696 return execute_steps(dev, write_table_steps,
2697 ARRAY_SIZE(write_table_steps));
2700 static int opal_generic_read_write_table(struct opal_dev *dev,
2701 struct opal_read_write_table *rw_tbl)
2705 mutex_lock(&dev->dev_lock);
2706 setup_opal_dev(dev);
2708 bit_set = fls64(rw_tbl->flags) - 1;
2710 case OPAL_READ_TABLE:
2711 ret = opal_read_table(dev, rw_tbl);
2713 case OPAL_WRITE_TABLE:
2714 ret = opal_write_table(dev, rw_tbl);
2717 pr_debug("Invalid bit set in the flag (%016llx).\n",
2723 mutex_unlock(&dev->dev_lock);
2728 static int opal_get_status(struct opal_dev *dev, void __user *data)
2730 struct opal_status sts = {0};
2733 * check_opal_support() error is not fatal,
2734 * !dev->supported is a valid condition
2736 if (!check_opal_support(dev))
2737 sts.flags = dev->flags;
2738 if (copy_to_user(data, &sts, sizeof(sts))) {
2739 pr_debug("Error copying status to userspace\n");
2745 int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
2750 if (!capable(CAP_SYS_ADMIN))
2754 if (!(dev->flags & OPAL_FL_SUPPORTED))
2758 p = memdup_user(arg, _IOC_SIZE(cmd));
2765 ret = opal_save(dev, p);
2767 case IOC_OPAL_LOCK_UNLOCK:
2768 ret = opal_lock_unlock(dev, p);
2770 case IOC_OPAL_TAKE_OWNERSHIP:
2771 ret = opal_take_ownership(dev, p);
2773 case IOC_OPAL_ACTIVATE_LSP:
2774 ret = opal_activate_lsp(dev, p);
2776 case IOC_OPAL_SET_PW:
2777 ret = opal_set_new_pw(dev, p);
2779 case IOC_OPAL_ACTIVATE_USR:
2780 ret = opal_activate_user(dev, p);
2782 case IOC_OPAL_REVERT_TPR:
2783 ret = opal_reverttper(dev, p, false);
2785 case IOC_OPAL_LR_SETUP:
2786 ret = opal_setup_locking_range(dev, p);
2788 case IOC_OPAL_ADD_USR_TO_LR:
2789 ret = opal_add_user_to_lr(dev, p);
2791 case IOC_OPAL_ENABLE_DISABLE_MBR:
2792 ret = opal_enable_disable_shadow_mbr(dev, p);
2794 case IOC_OPAL_MBR_DONE:
2795 ret = opal_set_mbr_done(dev, p);
2797 case IOC_OPAL_WRITE_SHADOW_MBR:
2798 ret = opal_write_shadow_mbr(dev, p);
2800 case IOC_OPAL_ERASE_LR:
2801 ret = opal_erase_locking_range(dev, p);
2803 case IOC_OPAL_SECURE_ERASE_LR:
2804 ret = opal_secure_erase_locking_range(dev, p);
2806 case IOC_OPAL_PSID_REVERT_TPR:
2807 ret = opal_reverttper(dev, p, true);
2809 case IOC_OPAL_GENERIC_TABLE_RW:
2810 ret = opal_generic_read_write_table(dev, p);
2812 case IOC_OPAL_GET_STATUS:
2813 ret = opal_get_status(dev, arg);
2823 EXPORT_SYMBOL_GPL(sed_ioctl);