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/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>
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];
81 sec_send_recv *send_recv;
83 struct mutex dev_lock;
91 u8 cmd[IO_BUFFER_LENGTH];
92 u8 resp[IO_BUFFER_LENGTH];
94 struct parsed_resp parsed;
98 struct list_head unlk_lst;
102 static const u8 opaluid[][OPAL_UID_LENGTH] = {
105 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
107 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
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 },
115 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
117 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
119 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
121 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
123 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
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 },
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 },
141 { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
143 { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
144 [OPAL_AUTHORITY_TABLE] =
145 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
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 },
153 /* C_PIN_TABLE object ID's */
155 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
157 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
158 [OPAL_C_PIN_ADMIN1] =
159 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
161 /* half UID's (only first 4 bytes used) */
162 [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
163 { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
164 [OPAL_HALF_UID_BOOLEAN_ACE] =
165 { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
167 /* special value for omitted optional parameter */
169 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
173 * TCG Storage SSC Methods.
174 * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
175 * Section: 6.3 Assigned UIDs
177 static const u8 opalmethod[][OPAL_METHOD_LENGTH] = {
179 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
180 [OPAL_STARTSESSION] =
181 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
183 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
185 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
187 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
189 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
191 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
192 [OPAL_EAUTHENTICATE] =
193 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
195 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
197 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
199 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
201 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
203 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
204 [OPAL_AUTHENTICATE] =
205 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
207 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
209 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
212 static int end_opal_session_error(struct opal_dev *dev);
213 static int opal_discovery0_step(struct opal_dev *dev);
215 struct opal_suspend_data {
216 struct opal_lock_unlock unlk;
218 struct list_head node;
223 * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
224 * Section: 5.1.5 Method Status Codes
226 static const char * const opal_errors[] = {
234 "No Sessions Available",
235 "Uniqueness Conflict",
236 "Insufficient Space",
243 "Transaction Failure",
245 "Authority Locked Out",
248 static const char *opal_error_to_human(int error)
253 if (error >= ARRAY_SIZE(opal_errors) || error < 0)
254 return "Unknown Error";
256 return opal_errors[error];
259 static void print_buffer(const u8 *ptr, u32 length)
262 print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
267 static bool check_tper(const void *data)
269 const struct d0_tper_features *tper = data;
270 u8 flags = tper->supported_features;
272 if (!(flags & TPER_SYNC_SUPPORTED)) {
273 pr_debug("TPer sync not supported. flags = %d\n",
274 tper->supported_features);
281 static bool check_mbrenabled(const void *data)
283 const struct d0_locking_features *lfeat = data;
284 u8 sup_feat = lfeat->supported_features;
286 return !!(sup_feat & MBR_ENABLED_MASK);
289 static bool check_sum(const void *data)
291 const struct d0_single_user_mode *sum = data;
292 u32 nlo = be32_to_cpu(sum->num_locking_objects);
295 pr_debug("Need at least one locking object.\n");
299 pr_debug("Number of locking objects: %d\n", nlo);
304 static u16 get_comid_v100(const void *data)
306 const struct d0_opal_v100 *v100 = data;
308 return be16_to_cpu(v100->baseComID);
311 static u16 get_comid_v200(const void *data)
313 const struct d0_opal_v200 *v200 = data;
315 return be16_to_cpu(v200->baseComID);
318 static int opal_send_cmd(struct opal_dev *dev)
320 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
321 dev->cmd, IO_BUFFER_LENGTH,
325 static int opal_recv_cmd(struct opal_dev *dev)
327 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
328 dev->resp, IO_BUFFER_LENGTH,
332 static int opal_recv_check(struct opal_dev *dev)
334 size_t buflen = IO_BUFFER_LENGTH;
335 void *buffer = dev->resp;
336 struct opal_header *hdr = buffer;
340 pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
341 hdr->cp.outstandingData,
342 hdr->cp.minTransfer);
344 if (hdr->cp.outstandingData == 0 ||
345 hdr->cp.minTransfer != 0)
348 memset(buffer, 0, buflen);
349 ret = opal_recv_cmd(dev);
355 static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
359 ret = opal_send_cmd(dev);
362 ret = opal_recv_cmd(dev);
365 ret = opal_recv_check(dev);
371 static void check_geometry(struct opal_dev *dev, const void *data)
373 const struct d0_geometry_features *geo = data;
375 dev->align = geo->alignment_granularity;
376 dev->lowest_lba = geo->lowest_aligned_lba;
379 static int execute_step(struct opal_dev *dev,
380 const struct opal_step *step, size_t stepIndex)
382 int error = step->fn(dev, step->data);
385 pr_debug("Step %zu (%pS) failed with error %d: %s\n",
386 stepIndex, step->fn, error,
387 opal_error_to_human(error));
393 static int execute_steps(struct opal_dev *dev,
394 const struct opal_step *steps, size_t n_steps)
399 /* first do a discovery0 */
400 error = opal_discovery0_step(dev);
404 for (state = 0; state < n_steps; state++) {
405 error = execute_step(dev, &steps[state], state);
414 * For each OPAL command the first step in steps starts some sort of
415 * session. If an error occurred in the initial discovery0 or if an
416 * error occurred in the first step (and thus stopping the loop with
417 * state == 0) then there was an error before or during the attempt to
418 * start a session. Therefore we shouldn't attempt to terminate a
419 * session, as one has not yet been created.
422 end_opal_session_error(dev);
427 static int opal_discovery0_end(struct opal_dev *dev)
429 bool found_com_id = false, supported = true, single_user = false;
430 const struct d0_header *hdr = (struct d0_header *)dev->resp;
431 const u8 *epos = dev->resp, *cpos = dev->resp;
433 u32 hlen = be32_to_cpu(hdr->length);
435 print_buffer(dev->resp, hlen);
436 dev->mbr_enabled = false;
438 if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
439 pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
440 sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
444 epos += hlen; /* end of buffer */
445 cpos += sizeof(*hdr); /* current position on buffer */
447 while (cpos < epos && supported) {
448 const struct d0_features *body =
449 (const struct d0_features *)cpos;
451 switch (be16_to_cpu(body->code)) {
453 supported = check_tper(body->features);
456 single_user = check_sum(body->features);
459 check_geometry(dev, body);
462 dev->mbr_enabled = check_mbrenabled(body->features);
466 /* some ignored properties */
467 pr_debug("Found OPAL feature description: %d\n",
468 be16_to_cpu(body->code));
471 comid = get_comid_v100(body->features);
475 comid = get_comid_v200(body->features);
478 case 0xbfff ... 0xffff:
479 /* vendor specific, just ignore */
482 pr_debug("OPAL Unknown feature: %d\n",
483 be16_to_cpu(body->code));
486 cpos += body->length + 4;
490 pr_debug("This device is not Opal enabled. Not Supported!\n");
495 pr_debug("Device doesn't support single user mode\n");
499 pr_debug("Could not find OPAL comid for device. Returning early\n");
508 static int opal_discovery0(struct opal_dev *dev, void *data)
512 memset(dev->resp, 0, IO_BUFFER_LENGTH);
513 dev->comid = OPAL_DISCOVERY_COMID;
514 ret = opal_recv_cmd(dev);
518 return opal_discovery0_end(dev);
521 static int opal_discovery0_step(struct opal_dev *dev)
523 const struct opal_step discovery0_step = {
527 return execute_step(dev, &discovery0_step, 0);
530 static size_t remaining_size(struct opal_dev *cmd)
532 return IO_BUFFER_LENGTH - cmd->pos;
535 static bool can_add(int *err, struct opal_dev *cmd, size_t len)
540 if (remaining_size(cmd) < len) {
541 pr_debug("Error adding %zu bytes: end of buffer.\n", len);
549 static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
551 if (!can_add(err, cmd, 1))
554 cmd->cmd[cmd->pos++] = tok;
557 static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
558 bool has_sign, int len)
563 atom = SHORT_ATOM_ID;
564 atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
565 atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
566 atom |= len & SHORT_ATOM_LEN_MASK;
568 add_token_u8(&err, cmd, atom);
571 static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
572 bool has_sign, int len)
576 header0 = MEDIUM_ATOM_ID;
577 header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
578 header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
579 header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
581 cmd->cmd[cmd->pos++] = header0;
582 cmd->cmd[cmd->pos++] = len;
585 static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
590 if (!(number & ~TINY_ATOM_DATA_MASK)) {
591 add_token_u8(err, cmd, number);
596 len = DIV_ROUND_UP(msb, 8);
598 if (!can_add(err, cmd, len + 1)) {
599 pr_debug("Error adding u64: end of buffer.\n");
602 add_short_atom_header(cmd, false, false, len);
604 add_token_u8(err, cmd, number >> (len * 8));
607 static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len)
609 size_t header_len = 1;
610 bool is_short_atom = true;
612 if (len & ~SHORT_ATOM_LEN_MASK) {
614 is_short_atom = false;
617 if (!can_add(err, cmd, header_len + len)) {
618 pr_debug("Error adding bytestring: end of buffer.\n");
623 add_short_atom_header(cmd, true, false, len);
625 add_medium_atom_header(cmd, true, false, len);
627 return &cmd->cmd[cmd->pos];
630 static void add_token_bytestring(int *err, struct opal_dev *cmd,
631 const u8 *bytestring, size_t len)
635 start = add_bytestring_header(err, cmd, len);
638 memcpy(start, bytestring, len);
642 static int build_locking_range(u8 *buffer, size_t length, u8 lr)
644 if (length > OPAL_UID_LENGTH) {
645 pr_debug("Can't build locking range. Length OOB\n");
649 memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
654 buffer[5] = LOCKING_RANGE_NON_GLOBAL;
660 static int build_locking_user(u8 *buffer, size_t length, u8 lr)
662 if (length > OPAL_UID_LENGTH) {
663 pr_debug("Can't build locking range user. Length OOB\n");
667 memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
674 static void set_comid(struct opal_dev *cmd, u16 comid)
676 struct opal_header *hdr = (struct opal_header *)cmd->cmd;
678 hdr->cp.extendedComID[0] = comid >> 8;
679 hdr->cp.extendedComID[1] = comid;
680 hdr->cp.extendedComID[2] = 0;
681 hdr->cp.extendedComID[3] = 0;
684 static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
686 struct opal_header *hdr;
690 * Close the parameter list opened from cmd_start.
691 * The number of bytes added must be equal to
692 * CMD_FINALIZE_BYTES_NEEDED.
694 add_token_u8(&err, cmd, OPAL_ENDLIST);
696 add_token_u8(&err, cmd, OPAL_ENDOFDATA);
697 add_token_u8(&err, cmd, OPAL_STARTLIST);
698 add_token_u8(&err, cmd, 0);
699 add_token_u8(&err, cmd, 0);
700 add_token_u8(&err, cmd, 0);
701 add_token_u8(&err, cmd, OPAL_ENDLIST);
704 pr_debug("Error finalizing command.\n");
708 hdr = (struct opal_header *) cmd->cmd;
710 hdr->pkt.tsn = cpu_to_be32(tsn);
711 hdr->pkt.hsn = cpu_to_be32(hsn);
713 hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
714 while (cmd->pos % 4) {
715 if (cmd->pos >= IO_BUFFER_LENGTH) {
716 pr_debug("Error: Buffer overrun\n");
719 cmd->cmd[cmd->pos++] = 0;
721 hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
723 hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
728 static const struct opal_resp_tok *response_get_token(
729 const struct parsed_resp *resp,
732 const struct opal_resp_tok *tok;
735 pr_debug("Response is NULL\n");
736 return ERR_PTR(-EINVAL);
739 if (n >= resp->num) {
740 pr_debug("Token number doesn't exist: %d, resp: %d\n",
742 return ERR_PTR(-EINVAL);
745 tok = &resp->toks[n];
747 pr_debug("Token length must be non-zero\n");
748 return ERR_PTR(-EINVAL);
754 static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
759 tok->width = OPAL_WIDTH_TINY;
761 if (pos[0] & TINY_ATOM_SIGNED) {
762 tok->type = OPAL_DTA_TOKENID_SINT;
764 tok->type = OPAL_DTA_TOKENID_UINT;
765 tok->stored.u = pos[0] & 0x3f;
771 static ssize_t response_parse_short(struct opal_resp_tok *tok,
775 tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
776 tok->width = OPAL_WIDTH_SHORT;
778 if (pos[0] & SHORT_ATOM_BYTESTRING) {
779 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
780 } else if (pos[0] & SHORT_ATOM_SIGNED) {
781 tok->type = OPAL_DTA_TOKENID_SINT;
786 tok->type = OPAL_DTA_TOKENID_UINT;
788 pr_debug("uint64 with more than 8 bytes\n");
791 for (i = tok->len - 1; i > 0; i--) {
792 u_integer |= ((u64)pos[i] << (8 * b));
795 tok->stored.u = u_integer;
801 static ssize_t response_parse_medium(struct opal_resp_tok *tok,
805 tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
806 tok->width = OPAL_WIDTH_MEDIUM;
808 if (pos[0] & MEDIUM_ATOM_BYTESTRING)
809 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
810 else if (pos[0] & MEDIUM_ATOM_SIGNED)
811 tok->type = OPAL_DTA_TOKENID_SINT;
813 tok->type = OPAL_DTA_TOKENID_UINT;
818 static ssize_t response_parse_long(struct opal_resp_tok *tok,
822 tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
823 tok->width = OPAL_WIDTH_LONG;
825 if (pos[0] & LONG_ATOM_BYTESTRING)
826 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
827 else if (pos[0] & LONG_ATOM_SIGNED)
828 tok->type = OPAL_DTA_TOKENID_SINT;
830 tok->type = OPAL_DTA_TOKENID_UINT;
835 static ssize_t response_parse_token(struct opal_resp_tok *tok,
840 tok->type = OPAL_DTA_TOKENID_TOKEN;
841 tok->width = OPAL_WIDTH_TOKEN;
846 static int response_parse(const u8 *buf, size_t length,
847 struct parsed_resp *resp)
849 const struct opal_header *hdr;
850 struct opal_resp_tok *iter;
853 ssize_t token_length;
855 u32 clen, plen, slen;
863 hdr = (struct opal_header *)buf;
867 clen = be32_to_cpu(hdr->cp.length);
868 plen = be32_to_cpu(hdr->pkt.length);
869 slen = be32_to_cpu(hdr->subpkt.length);
870 pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
873 if (clen == 0 || plen == 0 || slen == 0 ||
874 slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
875 pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
877 print_buffer(pos, sizeof(*hdr));
881 if (pos > buf + length)
886 print_buffer(pos, total);
888 if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
889 token_length = response_parse_tiny(iter, pos);
890 else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
891 token_length = response_parse_short(iter, pos);
892 else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
893 token_length = response_parse_medium(iter, pos);
894 else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
895 token_length = response_parse_long(iter, pos);
897 token_length = response_parse_token(iter, pos);
899 if (token_length < 0)
903 total -= token_length;
908 if (num_entries == 0) {
909 pr_debug("Couldn't parse response.\n");
912 resp->num = num_entries;
917 static size_t response_get_string(const struct parsed_resp *resp, int n,
921 const struct opal_resp_tok *tok;
924 tok = response_get_token(resp, n);
928 if (tok->type != OPAL_DTA_TOKENID_BYTESTRING) {
929 pr_debug("Token is not a byte string!\n");
933 switch (tok->width) {
934 case OPAL_WIDTH_TINY:
935 case OPAL_WIDTH_SHORT:
938 case OPAL_WIDTH_MEDIUM:
941 case OPAL_WIDTH_LONG:
945 pr_debug("Token has invalid width!\n");
949 *store = tok->pos + skip;
951 return tok->len - skip;
954 static u64 response_get_u64(const struct parsed_resp *resp, int n)
956 const struct opal_resp_tok *tok;
958 tok = response_get_token(resp, n);
962 if (tok->type != OPAL_DTA_TOKENID_UINT) {
963 pr_debug("Token is not unsigned int: %d\n", tok->type);
967 if (tok->width != OPAL_WIDTH_TINY && tok->width != OPAL_WIDTH_SHORT) {
968 pr_debug("Atom is not short or tiny: %d\n", tok->width);
972 return tok->stored.u;
975 static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
978 token->type != OPAL_DTA_TOKENID_TOKEN ||
979 token->pos[0] != match)
984 static u8 response_status(const struct parsed_resp *resp)
986 const struct opal_resp_tok *tok;
988 tok = response_get_token(resp, 0);
989 if (response_token_matches(tok, OPAL_ENDOFSESSION))
993 return DTAERROR_NO_METHOD_STATUS;
995 tok = response_get_token(resp, resp->num - 5);
996 if (!response_token_matches(tok, OPAL_STARTLIST))
997 return DTAERROR_NO_METHOD_STATUS;
999 tok = response_get_token(resp, resp->num - 1);
1000 if (!response_token_matches(tok, OPAL_ENDLIST))
1001 return DTAERROR_NO_METHOD_STATUS;
1003 return response_get_u64(resp, resp->num - 4);
1006 /* Parses and checks for errors */
1007 static int parse_and_check_status(struct opal_dev *dev)
1011 print_buffer(dev->cmd, dev->pos);
1013 error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
1015 pr_debug("Couldn't parse response.\n");
1019 return response_status(&dev->parsed);
1022 static void clear_opal_cmd(struct opal_dev *dev)
1024 dev->pos = sizeof(struct opal_header);
1025 memset(dev->cmd, 0, IO_BUFFER_LENGTH);
1028 static int cmd_start(struct opal_dev *dev, const u8 *uid, const u8 *method)
1032 clear_opal_cmd(dev);
1033 set_comid(dev, dev->comid);
1035 add_token_u8(&err, dev, OPAL_CALL);
1036 add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
1037 add_token_bytestring(&err, dev, method, OPAL_METHOD_LENGTH);
1040 * Every method call is followed by its parameters enclosed within
1041 * OPAL_STARTLIST and OPAL_ENDLIST tokens. We automatically open the
1042 * parameter list here and close it later in cmd_finalize.
1044 add_token_u8(&err, dev, OPAL_STARTLIST);
1049 static int start_opal_session_cont(struct opal_dev *dev)
1054 error = parse_and_check_status(dev);
1058 hsn = response_get_u64(&dev->parsed, 4);
1059 tsn = response_get_u64(&dev->parsed, 5);
1061 if (hsn == 0 && tsn == 0) {
1062 pr_debug("Couldn't authenticate session\n");
1072 static void add_suspend_info(struct opal_dev *dev,
1073 struct opal_suspend_data *sus)
1075 struct opal_suspend_data *iter;
1077 list_for_each_entry(iter, &dev->unlk_lst, node) {
1078 if (iter->lr == sus->lr) {
1079 list_del(&iter->node);
1084 list_add_tail(&sus->node, &dev->unlk_lst);
1087 static int end_session_cont(struct opal_dev *dev)
1092 return parse_and_check_status(dev);
1095 static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
1099 ret = cmd_finalize(dev, dev->hsn, dev->tsn);
1101 pr_debug("Error finalizing command buffer: %d\n", ret);
1105 print_buffer(dev->cmd, dev->pos);
1107 return opal_send_recv(dev, cont);
1111 * request @column from table @table on device @dev. On success, the column
1112 * data will be available in dev->resp->tok[4]
1114 static int generic_get_column(struct opal_dev *dev, const u8 *table,
1119 err = cmd_start(dev, table, opalmethod[OPAL_GET]);
1121 add_token_u8(&err, dev, OPAL_STARTLIST);
1123 add_token_u8(&err, dev, OPAL_STARTNAME);
1124 add_token_u8(&err, dev, OPAL_STARTCOLUMN);
1125 add_token_u64(&err, dev, column);
1126 add_token_u8(&err, dev, OPAL_ENDNAME);
1128 add_token_u8(&err, dev, OPAL_STARTNAME);
1129 add_token_u8(&err, dev, OPAL_ENDCOLUMN);
1130 add_token_u64(&err, dev, column);
1131 add_token_u8(&err, dev, OPAL_ENDNAME);
1133 add_token_u8(&err, dev, OPAL_ENDLIST);
1138 return finalize_and_send(dev, parse_and_check_status);
1142 * see TCG SAS 5.3.2.3 for a description of the available columns
1144 * the result is provided in dev->resp->tok[4]
1146 static int generic_get_table_info(struct opal_dev *dev, enum opal_uid table,
1149 u8 uid[OPAL_UID_LENGTH];
1150 const unsigned int half = OPAL_UID_LENGTH/2;
1152 /* sed-opal UIDs can be split in two halves:
1153 * first: actual table index
1154 * second: relative index in the table
1155 * so we have to get the first half of the OPAL_TABLE_TABLE and use the
1156 * first part of the target table as relative index into that table
1158 memcpy(uid, opaluid[OPAL_TABLE_TABLE], half);
1159 memcpy(uid+half, opaluid[table], half);
1161 return generic_get_column(dev, uid, column);
1164 static int gen_key(struct opal_dev *dev, void *data)
1166 u8 uid[OPAL_UID_LENGTH];
1169 memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
1170 kfree(dev->prev_data);
1171 dev->prev_data = NULL;
1173 err = cmd_start(dev, uid, opalmethod[OPAL_GENKEY]);
1176 pr_debug("Error building gen key command\n");
1181 return finalize_and_send(dev, parse_and_check_status);
1184 static int get_active_key_cont(struct opal_dev *dev)
1186 const char *activekey;
1190 error = parse_and_check_status(dev);
1194 keylen = response_get_string(&dev->parsed, 4, &activekey);
1196 pr_debug("%s: Couldn't extract the Activekey from the response\n",
1198 return OPAL_INVAL_PARAM;
1201 dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
1203 if (!dev->prev_data)
1206 dev->prev_d_len = keylen;
1211 static int get_active_key(struct opal_dev *dev, void *data)
1213 u8 uid[OPAL_UID_LENGTH];
1217 err = build_locking_range(uid, sizeof(uid), *lr);
1221 err = generic_get_column(dev, uid, OPAL_ACTIVEKEY);
1225 return get_active_key_cont(dev);
1228 static int generic_lr_enable_disable(struct opal_dev *dev,
1229 u8 *uid, bool rle, bool wle,
1234 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1236 add_token_u8(&err, dev, OPAL_STARTNAME);
1237 add_token_u8(&err, dev, OPAL_VALUES);
1238 add_token_u8(&err, dev, OPAL_STARTLIST);
1240 add_token_u8(&err, dev, OPAL_STARTNAME);
1241 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1242 add_token_u8(&err, dev, rle);
1243 add_token_u8(&err, dev, OPAL_ENDNAME);
1245 add_token_u8(&err, dev, OPAL_STARTNAME);
1246 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1247 add_token_u8(&err, dev, wle);
1248 add_token_u8(&err, dev, OPAL_ENDNAME);
1250 add_token_u8(&err, dev, OPAL_STARTNAME);
1251 add_token_u8(&err, dev, OPAL_READLOCKED);
1252 add_token_u8(&err, dev, rl);
1253 add_token_u8(&err, dev, OPAL_ENDNAME);
1255 add_token_u8(&err, dev, OPAL_STARTNAME);
1256 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1257 add_token_u8(&err, dev, wl);
1258 add_token_u8(&err, dev, OPAL_ENDNAME);
1260 add_token_u8(&err, dev, OPAL_ENDLIST);
1261 add_token_u8(&err, dev, OPAL_ENDNAME);
1266 static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
1267 struct opal_user_lr_setup *setup)
1271 err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
1274 pr_debug("Failed to create enable global lr command\n");
1279 static int setup_locking_range(struct opal_dev *dev, void *data)
1281 u8 uid[OPAL_UID_LENGTH];
1282 struct opal_user_lr_setup *setup = data;
1286 lr = setup->session.opal_key.lr;
1287 err = build_locking_range(uid, sizeof(uid), lr);
1292 err = enable_global_lr(dev, uid, setup);
1294 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1296 add_token_u8(&err, dev, OPAL_STARTNAME);
1297 add_token_u8(&err, dev, OPAL_VALUES);
1298 add_token_u8(&err, dev, OPAL_STARTLIST);
1300 add_token_u8(&err, dev, OPAL_STARTNAME);
1301 add_token_u8(&err, dev, OPAL_RANGESTART);
1302 add_token_u64(&err, dev, setup->range_start);
1303 add_token_u8(&err, dev, OPAL_ENDNAME);
1305 add_token_u8(&err, dev, OPAL_STARTNAME);
1306 add_token_u8(&err, dev, OPAL_RANGELENGTH);
1307 add_token_u64(&err, dev, setup->range_length);
1308 add_token_u8(&err, dev, OPAL_ENDNAME);
1310 add_token_u8(&err, dev, OPAL_STARTNAME);
1311 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1312 add_token_u64(&err, dev, !!setup->RLE);
1313 add_token_u8(&err, dev, OPAL_ENDNAME);
1315 add_token_u8(&err, dev, OPAL_STARTNAME);
1316 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1317 add_token_u64(&err, dev, !!setup->WLE);
1318 add_token_u8(&err, dev, OPAL_ENDNAME);
1320 add_token_u8(&err, dev, OPAL_ENDLIST);
1321 add_token_u8(&err, dev, OPAL_ENDNAME);
1324 pr_debug("Error building Setup Locking range command.\n");
1328 return finalize_and_send(dev, parse_and_check_status);
1331 static int start_generic_opal_session(struct opal_dev *dev,
1333 enum opal_uid sp_type,
1340 if (key == NULL && auth != OPAL_ANYBODY_UID)
1341 return OPAL_INVAL_PARAM;
1343 hsn = GENERIC_HOST_SESSION_NUM;
1344 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1345 opalmethod[OPAL_STARTSESSION]);
1347 add_token_u64(&err, dev, hsn);
1348 add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
1349 add_token_u8(&err, dev, 1);
1352 case OPAL_ANYBODY_UID:
1354 case OPAL_ADMIN1_UID:
1357 add_token_u8(&err, dev, OPAL_STARTNAME);
1358 add_token_u8(&err, dev, 0); /* HostChallenge */
1359 add_token_bytestring(&err, dev, key, key_len);
1360 add_token_u8(&err, dev, OPAL_ENDNAME);
1361 add_token_u8(&err, dev, OPAL_STARTNAME);
1362 add_token_u8(&err, dev, 3); /* HostSignAuth */
1363 add_token_bytestring(&err, dev, opaluid[auth],
1365 add_token_u8(&err, dev, OPAL_ENDNAME);
1368 pr_debug("Cannot start Admin SP session with auth %d\n", auth);
1369 return OPAL_INVAL_PARAM;
1373 pr_debug("Error building start adminsp session command.\n");
1377 return finalize_and_send(dev, start_opal_session_cont);
1380 static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
1382 return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
1383 OPAL_ADMINSP_UID, NULL, 0);
1386 static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
1389 const u8 *key = dev->prev_data;
1392 const struct opal_key *okey = data;
1394 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1399 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1401 key, dev->prev_d_len);
1403 dev->prev_data = NULL;
1409 static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
1411 struct opal_key *key = data;
1413 return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
1415 key->key, key->key_len);
1418 static int start_PSID_opal_session(struct opal_dev *dev, void *data)
1420 const struct opal_key *okey = data;
1422 return start_generic_opal_session(dev, OPAL_PSID_UID,
1428 static int start_auth_opal_session(struct opal_dev *dev, void *data)
1430 struct opal_session_info *session = data;
1431 u8 lk_ul_user[OPAL_UID_LENGTH];
1432 size_t keylen = session->opal_key.key_len;
1435 u8 *key = session->opal_key.key;
1436 u32 hsn = GENERIC_HOST_SESSION_NUM;
1439 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1440 session->opal_key.lr);
1441 else if (session->who != OPAL_ADMIN1 && !session->sum)
1442 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1445 memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
1450 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1451 opalmethod[OPAL_STARTSESSION]);
1453 add_token_u64(&err, dev, hsn);
1454 add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
1456 add_token_u8(&err, dev, 1);
1457 add_token_u8(&err, dev, OPAL_STARTNAME);
1458 add_token_u8(&err, dev, 0);
1459 add_token_bytestring(&err, dev, key, keylen);
1460 add_token_u8(&err, dev, OPAL_ENDNAME);
1461 add_token_u8(&err, dev, OPAL_STARTNAME);
1462 add_token_u8(&err, dev, 3);
1463 add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
1464 add_token_u8(&err, dev, OPAL_ENDNAME);
1467 pr_debug("Error building STARTSESSION command.\n");
1471 return finalize_and_send(dev, start_opal_session_cont);
1474 static int revert_tper(struct opal_dev *dev, void *data)
1478 err = cmd_start(dev, opaluid[OPAL_ADMINSP_UID],
1479 opalmethod[OPAL_REVERT]);
1481 pr_debug("Error building REVERT TPER command.\n");
1485 return finalize_and_send(dev, parse_and_check_status);
1488 static int internal_activate_user(struct opal_dev *dev, void *data)
1490 struct opal_session_info *session = data;
1491 u8 uid[OPAL_UID_LENGTH];
1494 memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1495 uid[7] = session->who;
1497 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1498 add_token_u8(&err, dev, OPAL_STARTNAME);
1499 add_token_u8(&err, dev, OPAL_VALUES);
1500 add_token_u8(&err, dev, OPAL_STARTLIST);
1501 add_token_u8(&err, dev, OPAL_STARTNAME);
1502 add_token_u8(&err, dev, 5); /* Enabled */
1503 add_token_u8(&err, dev, OPAL_TRUE);
1504 add_token_u8(&err, dev, OPAL_ENDNAME);
1505 add_token_u8(&err, dev, OPAL_ENDLIST);
1506 add_token_u8(&err, dev, OPAL_ENDNAME);
1509 pr_debug("Error building Activate UserN command.\n");
1513 return finalize_and_send(dev, parse_and_check_status);
1516 static int erase_locking_range(struct opal_dev *dev, void *data)
1518 struct opal_session_info *session = data;
1519 u8 uid[OPAL_UID_LENGTH];
1522 if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
1525 err = cmd_start(dev, uid, opalmethod[OPAL_ERASE]);
1528 pr_debug("Error building Erase Locking Range Command.\n");
1532 return finalize_and_send(dev, parse_and_check_status);
1535 static int set_mbr_done(struct opal_dev *dev, void *data)
1537 u8 *mbr_done_tf = data;
1540 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1541 opalmethod[OPAL_SET]);
1543 add_token_u8(&err, dev, OPAL_STARTNAME);
1544 add_token_u8(&err, dev, OPAL_VALUES);
1545 add_token_u8(&err, dev, OPAL_STARTLIST);
1546 add_token_u8(&err, dev, OPAL_STARTNAME);
1547 add_token_u8(&err, dev, OPAL_MBRDONE);
1548 add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
1549 add_token_u8(&err, dev, OPAL_ENDNAME);
1550 add_token_u8(&err, dev, OPAL_ENDLIST);
1551 add_token_u8(&err, dev, OPAL_ENDNAME);
1554 pr_debug("Error Building set MBR Done command\n");
1558 return finalize_and_send(dev, parse_and_check_status);
1561 static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
1563 u8 *mbr_en_dis = data;
1566 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1567 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, OPAL_MBRENABLE);
1574 add_token_u8(&err, dev, *mbr_en_dis);
1575 add_token_u8(&err, dev, OPAL_ENDNAME);
1576 add_token_u8(&err, dev, OPAL_ENDLIST);
1577 add_token_u8(&err, dev, OPAL_ENDNAME);
1580 pr_debug("Error Building set MBR done command\n");
1584 return finalize_and_send(dev, parse_and_check_status);
1587 static int write_shadow_mbr(struct opal_dev *dev, void *data)
1589 struct opal_shadow_mbr *shadow = data;
1590 const u8 __user *src;
1596 /* do we fit in the available shadow mbr space? */
1597 err = generic_get_table_info(dev, OPAL_MBR, OPAL_TABLE_ROWS);
1599 pr_debug("MBR: could not get shadow size\n");
1603 len = response_get_u64(&dev->parsed, 4);
1604 if (shadow->size > len || shadow->offset > len - shadow->size) {
1605 pr_debug("MBR: does not fit in shadow (%llu vs. %llu)\n",
1606 shadow->offset + shadow->size, len);
1610 /* do the actual transmission(s) */
1611 src = (u8 __user *)(uintptr_t)shadow->data;
1612 while (off < shadow->size) {
1613 err = cmd_start(dev, opaluid[OPAL_MBR], opalmethod[OPAL_SET]);
1614 add_token_u8(&err, dev, OPAL_STARTNAME);
1615 add_token_u8(&err, dev, OPAL_WHERE);
1616 add_token_u64(&err, dev, shadow->offset + off);
1617 add_token_u8(&err, dev, OPAL_ENDNAME);
1619 add_token_u8(&err, dev, OPAL_STARTNAME);
1620 add_token_u8(&err, dev, OPAL_VALUES);
1623 * The bytestring header is either 1 or 2 bytes, so assume 2.
1624 * There also needs to be enough space to accommodate the
1625 * trailing OPAL_ENDNAME (1 byte) and tokens added by
1628 len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
1629 (size_t)(shadow->size - off));
1630 pr_debug("MBR: write bytes %zu+%llu/%llu\n",
1631 off, len, shadow->size);
1633 dst = add_bytestring_header(&err, dev, len);
1636 if (copy_from_user(dst, src + off, len))
1640 add_token_u8(&err, dev, OPAL_ENDNAME);
1644 err = finalize_and_send(dev, parse_and_check_status);
1654 static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
1655 struct opal_dev *dev)
1659 err = cmd_start(dev, cpin_uid, opalmethod[OPAL_SET]);
1661 add_token_u8(&err, dev, OPAL_STARTNAME);
1662 add_token_u8(&err, dev, OPAL_VALUES);
1663 add_token_u8(&err, dev, OPAL_STARTLIST);
1664 add_token_u8(&err, dev, OPAL_STARTNAME);
1665 add_token_u8(&err, dev, OPAL_PIN);
1666 add_token_bytestring(&err, dev, key, key_len);
1667 add_token_u8(&err, dev, OPAL_ENDNAME);
1668 add_token_u8(&err, dev, OPAL_ENDLIST);
1669 add_token_u8(&err, dev, OPAL_ENDNAME);
1674 static int set_new_pw(struct opal_dev *dev, void *data)
1676 u8 cpin_uid[OPAL_UID_LENGTH];
1677 struct opal_session_info *usr = data;
1679 memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
1681 if (usr->who != OPAL_ADMIN1) {
1684 cpin_uid[7] = usr->opal_key.lr + 1;
1686 cpin_uid[7] = usr->who;
1689 if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
1691 pr_debug("Error building set password command.\n");
1695 return finalize_and_send(dev, parse_and_check_status);
1698 static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
1700 u8 cpin_uid[OPAL_UID_LENGTH];
1701 struct opal_key *key = data;
1703 memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
1705 if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
1706 pr_debug("Error building Set SID cpin\n");
1709 return finalize_and_send(dev, parse_and_check_status);
1712 static int add_user_to_lr(struct opal_dev *dev, void *data)
1714 u8 lr_buffer[OPAL_UID_LENGTH];
1715 u8 user_uid[OPAL_UID_LENGTH];
1716 struct opal_lock_unlock *lkul = data;
1719 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
1722 if (lkul->l_state == OPAL_RW)
1723 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
1726 lr_buffer[7] = lkul->session.opal_key.lr;
1728 memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1730 user_uid[7] = lkul->session.who;
1732 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1734 add_token_u8(&err, dev, OPAL_STARTNAME);
1735 add_token_u8(&err, dev, OPAL_VALUES);
1737 add_token_u8(&err, dev, OPAL_STARTLIST);
1738 add_token_u8(&err, dev, OPAL_STARTNAME);
1739 add_token_u8(&err, dev, 3);
1741 add_token_u8(&err, dev, OPAL_STARTLIST);
1744 add_token_u8(&err, dev, OPAL_STARTNAME);
1745 add_token_bytestring(&err, dev,
1746 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1748 add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1749 add_token_u8(&err, dev, OPAL_ENDNAME);
1752 add_token_u8(&err, dev, OPAL_STARTNAME);
1753 add_token_bytestring(&err, dev,
1754 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1756 add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1757 add_token_u8(&err, dev, OPAL_ENDNAME);
1760 add_token_u8(&err, dev, OPAL_STARTNAME);
1761 add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
1763 add_token_u8(&err, dev, 1);
1764 add_token_u8(&err, dev, OPAL_ENDNAME);
1767 add_token_u8(&err, dev, OPAL_ENDLIST);
1768 add_token_u8(&err, dev, OPAL_ENDNAME);
1769 add_token_u8(&err, dev, OPAL_ENDLIST);
1770 add_token_u8(&err, dev, OPAL_ENDNAME);
1773 pr_debug("Error building add user to locking range command.\n");
1777 return finalize_and_send(dev, parse_and_check_status);
1780 static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
1782 u8 lr_buffer[OPAL_UID_LENGTH];
1783 struct opal_lock_unlock *lkul = data;
1784 u8 read_locked = 1, write_locked = 1;
1787 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1788 lkul->session.opal_key.lr) < 0)
1791 switch (lkul->l_state) {
1801 /* vars are initialized to locked */
1804 pr_debug("Tried to set an invalid locking state... returning to uland\n");
1805 return OPAL_INVAL_PARAM;
1808 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1810 add_token_u8(&err, dev, OPAL_STARTNAME);
1811 add_token_u8(&err, dev, OPAL_VALUES);
1812 add_token_u8(&err, dev, OPAL_STARTLIST);
1814 add_token_u8(&err, dev, OPAL_STARTNAME);
1815 add_token_u8(&err, dev, OPAL_READLOCKED);
1816 add_token_u8(&err, dev, read_locked);
1817 add_token_u8(&err, dev, OPAL_ENDNAME);
1819 add_token_u8(&err, dev, OPAL_STARTNAME);
1820 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1821 add_token_u8(&err, dev, write_locked);
1822 add_token_u8(&err, dev, OPAL_ENDNAME);
1824 add_token_u8(&err, dev, OPAL_ENDLIST);
1825 add_token_u8(&err, dev, OPAL_ENDNAME);
1828 pr_debug("Error building SET command.\n");
1832 return finalize_and_send(dev, parse_and_check_status);
1836 static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
1838 u8 lr_buffer[OPAL_UID_LENGTH];
1839 u8 read_locked = 1, write_locked = 1;
1840 struct opal_lock_unlock *lkul = data;
1843 clear_opal_cmd(dev);
1844 set_comid(dev, dev->comid);
1846 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1847 lkul->session.opal_key.lr) < 0)
1850 switch (lkul->l_state) {
1860 /* vars are initialized to locked */
1863 pr_debug("Tried to set an invalid locking state.\n");
1864 return OPAL_INVAL_PARAM;
1866 ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
1867 read_locked, write_locked);
1870 pr_debug("Error building SET command.\n");
1874 return finalize_and_send(dev, parse_and_check_status);
1877 static int activate_lsp(struct opal_dev *dev, void *data)
1879 struct opal_lr_act *opal_act = data;
1880 u8 user_lr[OPAL_UID_LENGTH];
1884 err = cmd_start(dev, opaluid[OPAL_LOCKINGSP_UID],
1885 opalmethod[OPAL_ACTIVATE]);
1887 if (opal_act->sum) {
1888 err = build_locking_range(user_lr, sizeof(user_lr),
1893 add_token_u8(&err, dev, OPAL_STARTNAME);
1894 add_token_u8(&err, dev, uint_3);
1895 add_token_u8(&err, dev, 6);
1896 add_token_u8(&err, dev, 0);
1897 add_token_u8(&err, dev, 0);
1899 add_token_u8(&err, dev, OPAL_STARTLIST);
1900 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1901 for (i = 1; i < opal_act->num_lrs; i++) {
1902 user_lr[7] = opal_act->lr[i];
1903 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1905 add_token_u8(&err, dev, OPAL_ENDLIST);
1906 add_token_u8(&err, dev, OPAL_ENDNAME);
1910 pr_debug("Error building Activate LockingSP command.\n");
1914 return finalize_and_send(dev, parse_and_check_status);
1917 /* Determine if we're in the Manufactured Inactive or Active state */
1918 static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
1923 err = generic_get_column(dev, opaluid[OPAL_LOCKINGSP_UID],
1928 lc_status = response_get_u64(&dev->parsed, 4);
1929 /* 0x08 is Manufactured Inactive */
1930 /* 0x09 is Manufactured */
1931 if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
1932 pr_debug("Couldn't determine the status of the Lifecycle state\n");
1939 static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
1941 const char *msid_pin;
1945 err = generic_get_column(dev, opaluid[OPAL_C_PIN_MSID], OPAL_PIN);
1949 strlen = response_get_string(&dev->parsed, 4, &msid_pin);
1951 pr_debug("Couldn't extract MSID_CPIN from response\n");
1952 return OPAL_INVAL_PARAM;
1955 dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
1956 if (!dev->prev_data)
1959 dev->prev_d_len = strlen;
1964 static int end_opal_session(struct opal_dev *dev, void *data)
1968 clear_opal_cmd(dev);
1969 set_comid(dev, dev->comid);
1970 add_token_u8(&err, dev, OPAL_ENDOFSESSION);
1975 return finalize_and_send(dev, end_session_cont);
1978 static int end_opal_session_error(struct opal_dev *dev)
1980 const struct opal_step error_end_session = {
1984 return execute_step(dev, &error_end_session, 0);
1987 static inline void setup_opal_dev(struct opal_dev *dev)
1991 dev->prev_data = NULL;
1994 static int check_opal_support(struct opal_dev *dev)
1998 mutex_lock(&dev->dev_lock);
1999 setup_opal_dev(dev);
2000 ret = opal_discovery0_step(dev);
2001 dev->supported = !ret;
2002 mutex_unlock(&dev->dev_lock);
2007 static void clean_opal_dev(struct opal_dev *dev)
2010 struct opal_suspend_data *suspend, *next;
2012 mutex_lock(&dev->dev_lock);
2013 list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
2014 list_del(&suspend->node);
2017 mutex_unlock(&dev->dev_lock);
2020 void free_opal_dev(struct opal_dev *dev)
2025 clean_opal_dev(dev);
2028 EXPORT_SYMBOL(free_opal_dev);
2030 struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
2032 struct opal_dev *dev;
2034 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
2038 INIT_LIST_HEAD(&dev->unlk_lst);
2039 mutex_init(&dev->dev_lock);
2041 dev->send_recv = send_recv;
2042 if (check_opal_support(dev) != 0) {
2043 pr_debug("Opal is not supported on this device\n");
2050 EXPORT_SYMBOL(init_opal_dev);
2052 static int opal_secure_erase_locking_range(struct opal_dev *dev,
2053 struct opal_session_info *opal_session)
2055 const struct opal_step erase_steps[] = {
2056 { start_auth_opal_session, opal_session },
2057 { get_active_key, &opal_session->opal_key.lr },
2059 { end_opal_session, }
2063 mutex_lock(&dev->dev_lock);
2064 setup_opal_dev(dev);
2065 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2066 mutex_unlock(&dev->dev_lock);
2071 static int opal_erase_locking_range(struct opal_dev *dev,
2072 struct opal_session_info *opal_session)
2074 const struct opal_step erase_steps[] = {
2075 { start_auth_opal_session, opal_session },
2076 { erase_locking_range, opal_session },
2077 { end_opal_session, }
2081 mutex_lock(&dev->dev_lock);
2082 setup_opal_dev(dev);
2083 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2084 mutex_unlock(&dev->dev_lock);
2089 static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
2090 struct opal_mbr_data *opal_mbr)
2092 u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
2093 OPAL_TRUE : OPAL_FALSE;
2095 const struct opal_step mbr_steps[] = {
2096 { start_admin1LSP_opal_session, &opal_mbr->key },
2097 { set_mbr_done, &enable_disable },
2098 { end_opal_session, },
2099 { start_admin1LSP_opal_session, &opal_mbr->key },
2100 { set_mbr_enable_disable, &enable_disable },
2101 { end_opal_session, }
2105 if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
2106 opal_mbr->enable_disable != OPAL_MBR_DISABLE)
2109 mutex_lock(&dev->dev_lock);
2110 setup_opal_dev(dev);
2111 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2112 mutex_unlock(&dev->dev_lock);
2117 static int opal_set_mbr_done(struct opal_dev *dev,
2118 struct opal_mbr_done *mbr_done)
2120 u8 mbr_done_tf = mbr_done->done_flag == OPAL_MBR_DONE ?
2121 OPAL_TRUE : OPAL_FALSE;
2123 const struct opal_step mbr_steps[] = {
2124 { start_admin1LSP_opal_session, &mbr_done->key },
2125 { set_mbr_done, &mbr_done_tf },
2126 { end_opal_session, }
2130 if (mbr_done->done_flag != OPAL_MBR_DONE &&
2131 mbr_done->done_flag != OPAL_MBR_NOT_DONE)
2134 mutex_lock(&dev->dev_lock);
2135 setup_opal_dev(dev);
2136 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2137 mutex_unlock(&dev->dev_lock);
2142 static int opal_write_shadow_mbr(struct opal_dev *dev,
2143 struct opal_shadow_mbr *info)
2145 const struct opal_step mbr_steps[] = {
2146 { start_admin1LSP_opal_session, &info->key },
2147 { write_shadow_mbr, info },
2148 { end_opal_session, }
2152 if (info->size == 0)
2155 mutex_lock(&dev->dev_lock);
2156 setup_opal_dev(dev);
2157 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2158 mutex_unlock(&dev->dev_lock);
2163 static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
2165 struct opal_suspend_data *suspend;
2167 suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
2171 suspend->unlk = *lk_unlk;
2172 suspend->lr = lk_unlk->session.opal_key.lr;
2174 mutex_lock(&dev->dev_lock);
2175 setup_opal_dev(dev);
2176 add_suspend_info(dev, suspend);
2177 mutex_unlock(&dev->dev_lock);
2182 static int opal_add_user_to_lr(struct opal_dev *dev,
2183 struct opal_lock_unlock *lk_unlk)
2185 const struct opal_step steps[] = {
2186 { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
2187 { add_user_to_lr, lk_unlk },
2188 { end_opal_session, }
2192 if (lk_unlk->l_state != OPAL_RO &&
2193 lk_unlk->l_state != OPAL_RW) {
2194 pr_debug("Locking state was not RO or RW\n");
2198 if (lk_unlk->session.who < OPAL_USER1 ||
2199 lk_unlk->session.who > OPAL_USER9) {
2200 pr_debug("Authority was not within the range of users: %d\n",
2201 lk_unlk->session.who);
2205 if (lk_unlk->session.sum) {
2206 pr_debug("%s not supported in sum. Use setup locking range\n",
2211 mutex_lock(&dev->dev_lock);
2212 setup_opal_dev(dev);
2213 ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
2214 mutex_unlock(&dev->dev_lock);
2219 static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal, bool psid)
2221 /* controller will terminate session */
2222 const struct opal_step revert_steps[] = {
2223 { start_SIDASP_opal_session, opal },
2226 const struct opal_step psid_revert_steps[] = {
2227 { start_PSID_opal_session, opal },
2233 mutex_lock(&dev->dev_lock);
2234 setup_opal_dev(dev);
2236 ret = execute_steps(dev, psid_revert_steps,
2237 ARRAY_SIZE(psid_revert_steps));
2239 ret = execute_steps(dev, revert_steps,
2240 ARRAY_SIZE(revert_steps));
2241 mutex_unlock(&dev->dev_lock);
2244 * If we successfully reverted lets clean
2245 * any saved locking ranges.
2248 clean_opal_dev(dev);
2253 static int __opal_lock_unlock(struct opal_dev *dev,
2254 struct opal_lock_unlock *lk_unlk)
2256 const struct opal_step unlock_steps[] = {
2257 { start_auth_opal_session, &lk_unlk->session },
2258 { lock_unlock_locking_range, lk_unlk },
2259 { end_opal_session, }
2261 const struct opal_step unlock_sum_steps[] = {
2262 { start_auth_opal_session, &lk_unlk->session },
2263 { lock_unlock_locking_range_sum, lk_unlk },
2264 { end_opal_session, }
2267 if (lk_unlk->session.sum)
2268 return execute_steps(dev, unlock_sum_steps,
2269 ARRAY_SIZE(unlock_sum_steps));
2271 return execute_steps(dev, unlock_steps,
2272 ARRAY_SIZE(unlock_steps));
2275 static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
2277 u8 mbr_done_tf = OPAL_TRUE;
2278 const struct opal_step mbrdone_step[] = {
2279 { start_admin1LSP_opal_session, key },
2280 { set_mbr_done, &mbr_done_tf },
2281 { end_opal_session, }
2284 return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
2287 static int opal_lock_unlock(struct opal_dev *dev,
2288 struct opal_lock_unlock *lk_unlk)
2292 if (lk_unlk->session.who > OPAL_USER9)
2295 mutex_lock(&dev->dev_lock);
2296 ret = __opal_lock_unlock(dev, lk_unlk);
2297 mutex_unlock(&dev->dev_lock);
2302 static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
2304 const struct opal_step owner_steps[] = {
2305 { start_anybodyASP_opal_session, },
2306 { get_msid_cpin_pin, },
2307 { end_opal_session, },
2308 { start_SIDASP_opal_session, opal },
2309 { set_sid_cpin_pin, opal },
2310 { end_opal_session, }
2317 mutex_lock(&dev->dev_lock);
2318 setup_opal_dev(dev);
2319 ret = execute_steps(dev, owner_steps, ARRAY_SIZE(owner_steps));
2320 mutex_unlock(&dev->dev_lock);
2325 static int opal_activate_lsp(struct opal_dev *dev,
2326 struct opal_lr_act *opal_lr_act)
2328 const struct opal_step active_steps[] = {
2329 { start_SIDASP_opal_session, &opal_lr_act->key },
2330 { get_lsp_lifecycle, },
2331 { activate_lsp, opal_lr_act },
2332 { end_opal_session, }
2336 if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
2339 mutex_lock(&dev->dev_lock);
2340 setup_opal_dev(dev);
2341 ret = execute_steps(dev, active_steps, ARRAY_SIZE(active_steps));
2342 mutex_unlock(&dev->dev_lock);
2347 static int opal_setup_locking_range(struct opal_dev *dev,
2348 struct opal_user_lr_setup *opal_lrs)
2350 const struct opal_step lr_steps[] = {
2351 { start_auth_opal_session, &opal_lrs->session },
2352 { setup_locking_range, opal_lrs },
2353 { end_opal_session, }
2357 mutex_lock(&dev->dev_lock);
2358 setup_opal_dev(dev);
2359 ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
2360 mutex_unlock(&dev->dev_lock);
2365 static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
2367 const struct opal_step pw_steps[] = {
2368 { start_auth_opal_session, &opal_pw->session },
2369 { set_new_pw, &opal_pw->new_user_pw },
2370 { end_opal_session, }
2374 if (opal_pw->session.who > OPAL_USER9 ||
2375 opal_pw->new_user_pw.who > OPAL_USER9)
2378 mutex_lock(&dev->dev_lock);
2379 setup_opal_dev(dev);
2380 ret = execute_steps(dev, pw_steps, ARRAY_SIZE(pw_steps));
2381 mutex_unlock(&dev->dev_lock);
2386 static int opal_activate_user(struct opal_dev *dev,
2387 struct opal_session_info *opal_session)
2389 const struct opal_step act_steps[] = {
2390 { start_admin1LSP_opal_session, &opal_session->opal_key },
2391 { internal_activate_user, opal_session },
2392 { end_opal_session, }
2396 /* We can't activate Admin1 it's active as manufactured */
2397 if (opal_session->who < OPAL_USER1 ||
2398 opal_session->who > OPAL_USER9) {
2399 pr_debug("Who was not a valid user: %d\n", opal_session->who);
2403 mutex_lock(&dev->dev_lock);
2404 setup_opal_dev(dev);
2405 ret = execute_steps(dev, act_steps, ARRAY_SIZE(act_steps));
2406 mutex_unlock(&dev->dev_lock);
2411 bool opal_unlock_from_suspend(struct opal_dev *dev)
2413 struct opal_suspend_data *suspend;
2414 bool was_failure = false;
2420 if (!dev->supported)
2423 mutex_lock(&dev->dev_lock);
2424 setup_opal_dev(dev);
2426 list_for_each_entry(suspend, &dev->unlk_lst, node) {
2430 ret = __opal_lock_unlock(dev, &suspend->unlk);
2432 pr_debug("Failed to unlock LR %hhu with sum %d\n",
2433 suspend->unlk.session.opal_key.lr,
2434 suspend->unlk.session.sum);
2438 if (dev->mbr_enabled) {
2439 ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
2441 pr_debug("Failed to set MBR Done in S3 resume\n");
2444 mutex_unlock(&dev->dev_lock);
2448 EXPORT_SYMBOL(opal_unlock_from_suspend);
2450 int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
2455 if (!capable(CAP_SYS_ADMIN))
2459 if (!dev->supported)
2462 p = memdup_user(arg, _IOC_SIZE(cmd));
2468 ret = opal_save(dev, p);
2470 case IOC_OPAL_LOCK_UNLOCK:
2471 ret = opal_lock_unlock(dev, p);
2473 case IOC_OPAL_TAKE_OWNERSHIP:
2474 ret = opal_take_ownership(dev, p);
2476 case IOC_OPAL_ACTIVATE_LSP:
2477 ret = opal_activate_lsp(dev, p);
2479 case IOC_OPAL_SET_PW:
2480 ret = opal_set_new_pw(dev, p);
2482 case IOC_OPAL_ACTIVATE_USR:
2483 ret = opal_activate_user(dev, p);
2485 case IOC_OPAL_REVERT_TPR:
2486 ret = opal_reverttper(dev, p, false);
2488 case IOC_OPAL_LR_SETUP:
2489 ret = opal_setup_locking_range(dev, p);
2491 case IOC_OPAL_ADD_USR_TO_LR:
2492 ret = opal_add_user_to_lr(dev, p);
2494 case IOC_OPAL_ENABLE_DISABLE_MBR:
2495 ret = opal_enable_disable_shadow_mbr(dev, p);
2497 case IOC_OPAL_MBR_DONE:
2498 ret = opal_set_mbr_done(dev, p);
2500 case IOC_OPAL_WRITE_SHADOW_MBR:
2501 ret = opal_write_shadow_mbr(dev, p);
2503 case IOC_OPAL_ERASE_LR:
2504 ret = opal_erase_locking_range(dev, p);
2506 case IOC_OPAL_SECURE_ERASE_LR:
2507 ret = opal_secure_erase_locking_range(dev, p);
2509 case IOC_OPAL_PSID_REVERT_TPR:
2510 ret = opal_reverttper(dev, p, true);
2519 EXPORT_SYMBOL_GPL(sed_ioctl);