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
30 int (*fn)(struct opal_dev *dev, void *data);
33 typedef int (cont_fn)(struct opal_dev *dev);
35 enum opal_atom_width {
44 * On the parsed response, we don't store again the toks that are already
45 * stored in the response buffer. Instead, for each token, we just store a
46 * pointer to the position in the buffer where the token starts, and the size
47 * of the token in bytes.
49 struct opal_resp_tok {
52 enum opal_response_token type;
53 enum opal_atom_width width;
61 * From the response header it's not possible to know how many tokens there are
62 * on the payload. So we hardcode that the maximum will be MAX_TOKS, and later
63 * if we start dealing with messages that have more than that, we can increase
64 * this number. This is done to avoid having to make two passes through the
65 * response, the first one counting how many tokens we have and the second one
66 * actually storing the positions.
70 struct opal_resp_tok toks[MAX_TOKS];
78 sec_send_recv *send_recv;
80 struct mutex dev_lock;
88 u8 cmd[IO_BUFFER_LENGTH];
89 u8 resp[IO_BUFFER_LENGTH];
91 struct parsed_resp parsed;
95 struct list_head unlk_lst;
99 static const u8 opaluid[][OPAL_UID_LENGTH] = {
102 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
104 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
106 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
107 [OPAL_LOCKINGSP_UID] =
108 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
109 [OPAL_ENTERPRISE_LOCKINGSP_UID] =
110 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
112 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
114 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
116 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
118 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
120 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
122 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
123 [OPAL_ENTERPRISE_BANDMASTER0_UID] =
124 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
125 [OPAL_ENTERPRISE_ERASEMASTER_UID] =
126 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
130 [OPAL_LOCKINGRANGE_GLOBAL] =
131 { 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
132 [OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
133 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
134 [OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
135 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
137 { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
139 { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
140 [OPAL_AUTHORITY_TABLE] =
141 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
143 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
144 [OPAL_LOCKING_INFO_TABLE] =
145 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
146 [OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
147 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
149 /* C_PIN_TABLE object ID's */
152 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
154 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
155 [OPAL_C_PIN_ADMIN1] =
156 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
158 /* half UID's (only first 4 bytes used) */
160 [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
161 { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
162 [OPAL_HALF_UID_BOOLEAN_ACE] =
163 { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
165 /* special value for omitted optional parameter */
167 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
171 * TCG Storage SSC Methods.
172 * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
173 * Section: 6.3 Assigned UIDs
175 static const u8 opalmethod[][OPAL_METHOD_LENGTH] = {
177 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
178 [OPAL_STARTSESSION] =
179 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
181 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
183 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
185 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
187 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
189 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
190 [OPAL_EAUTHENTICATE] =
191 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
193 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
195 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
197 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
199 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
201 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
202 [OPAL_AUTHENTICATE] =
203 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
205 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
207 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
210 static int end_opal_session_error(struct opal_dev *dev);
211 static int opal_discovery0_step(struct opal_dev *dev);
213 struct opal_suspend_data {
214 struct opal_lock_unlock unlk;
216 struct list_head node;
221 * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
222 * Section: 5.1.5 Method Status Codes
224 static const char * const opal_errors[] = {
232 "No Sessions Available",
233 "Uniqueness Conflict",
234 "Insufficient Space",
241 "Transaction Failure",
243 "Authority Locked Out",
246 static const char *opal_error_to_human(int error)
251 if (error >= ARRAY_SIZE(opal_errors) || error < 0)
252 return "Unknown Error";
254 return opal_errors[error];
257 static void print_buffer(const u8 *ptr, u32 length)
260 print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
265 static bool check_tper(const void *data)
267 const struct d0_tper_features *tper = data;
268 u8 flags = tper->supported_features;
270 if (!(flags & TPER_SYNC_SUPPORTED)) {
271 pr_debug("TPer sync not supported. flags = %d\n",
272 tper->supported_features);
279 static bool check_mbrenabled(const void *data)
281 const struct d0_locking_features *lfeat = data;
282 u8 sup_feat = lfeat->supported_features;
284 return !!(sup_feat & MBR_ENABLED_MASK);
287 static bool check_sum(const void *data)
289 const struct d0_single_user_mode *sum = data;
290 u32 nlo = be32_to_cpu(sum->num_locking_objects);
293 pr_debug("Need at least one locking object.\n");
297 pr_debug("Number of locking objects: %d\n", nlo);
302 static u16 get_comid_v100(const void *data)
304 const struct d0_opal_v100 *v100 = data;
306 return be16_to_cpu(v100->baseComID);
309 static u16 get_comid_v200(const void *data)
311 const struct d0_opal_v200 *v200 = data;
313 return be16_to_cpu(v200->baseComID);
316 static int opal_send_cmd(struct opal_dev *dev)
318 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
319 dev->cmd, IO_BUFFER_LENGTH,
323 static int opal_recv_cmd(struct opal_dev *dev)
325 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
326 dev->resp, IO_BUFFER_LENGTH,
330 static int opal_recv_check(struct opal_dev *dev)
332 size_t buflen = IO_BUFFER_LENGTH;
333 void *buffer = dev->resp;
334 struct opal_header *hdr = buffer;
338 pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
339 hdr->cp.outstandingData,
340 hdr->cp.minTransfer);
342 if (hdr->cp.outstandingData == 0 ||
343 hdr->cp.minTransfer != 0)
346 memset(buffer, 0, buflen);
347 ret = opal_recv_cmd(dev);
353 static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
357 ret = opal_send_cmd(dev);
360 ret = opal_recv_cmd(dev);
363 ret = opal_recv_check(dev);
369 static void check_geometry(struct opal_dev *dev, const void *data)
371 const struct d0_geometry_features *geo = data;
373 dev->align = geo->alignment_granularity;
374 dev->lowest_lba = geo->lowest_aligned_lba;
377 static int execute_step(struct opal_dev *dev,
378 const struct opal_step *step, size_t stepIndex)
380 int error = step->fn(dev, step->data);
383 pr_debug("Step %zu (%pS) failed with error %d: %s\n",
384 stepIndex, step->fn, error,
385 opal_error_to_human(error));
391 static int execute_steps(struct opal_dev *dev,
392 const struct opal_step *steps, size_t n_steps)
397 /* first do a discovery0 */
398 error = opal_discovery0_step(dev);
402 for (state = 0; state < n_steps; state++) {
403 error = execute_step(dev, &steps[state], state);
412 * For each OPAL command the first step in steps starts some sort of
413 * session. If an error occurred in the initial discovery0 or if an
414 * error occurred in the first step (and thus stopping the loop with
415 * state == 0) then there was an error before or during the attempt to
416 * start a session. Therefore we shouldn't attempt to terminate a
417 * session, as one has not yet been created.
420 end_opal_session_error(dev);
425 static int opal_discovery0_end(struct opal_dev *dev)
427 bool found_com_id = false, supported = true, single_user = false;
428 const struct d0_header *hdr = (struct d0_header *)dev->resp;
429 const u8 *epos = dev->resp, *cpos = dev->resp;
431 u32 hlen = be32_to_cpu(hdr->length);
433 print_buffer(dev->resp, hlen);
434 dev->mbr_enabled = false;
436 if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
437 pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
438 sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
442 epos += hlen; /* end of buffer */
443 cpos += sizeof(*hdr); /* current position on buffer */
445 while (cpos < epos && supported) {
446 const struct d0_features *body =
447 (const struct d0_features *)cpos;
449 switch (be16_to_cpu(body->code)) {
451 supported = check_tper(body->features);
454 single_user = check_sum(body->features);
457 check_geometry(dev, body);
460 dev->mbr_enabled = check_mbrenabled(body->features);
464 /* some ignored properties */
465 pr_debug("Found OPAL feature description: %d\n",
466 be16_to_cpu(body->code));
469 comid = get_comid_v100(body->features);
473 comid = get_comid_v200(body->features);
476 case 0xbfff ... 0xffff:
477 /* vendor specific, just ignore */
480 pr_debug("OPAL Unknown feature: %d\n",
481 be16_to_cpu(body->code));
484 cpos += body->length + 4;
488 pr_debug("This device is not Opal enabled. Not Supported!\n");
493 pr_debug("Device doesn't support single user mode\n");
497 pr_debug("Could not find OPAL comid for device. Returning early\n");
506 static int opal_discovery0(struct opal_dev *dev, void *data)
510 memset(dev->resp, 0, IO_BUFFER_LENGTH);
511 dev->comid = OPAL_DISCOVERY_COMID;
512 ret = opal_recv_cmd(dev);
515 return opal_discovery0_end(dev);
518 static int opal_discovery0_step(struct opal_dev *dev)
520 const struct opal_step discovery0_step = {
523 return execute_step(dev, &discovery0_step, 0);
526 static bool can_add(int *err, struct opal_dev *cmd, size_t len)
531 if (len > IO_BUFFER_LENGTH || cmd->pos > IO_BUFFER_LENGTH - len) {
532 pr_debug("Error adding %zu bytes: end of buffer.\n", len);
540 static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
542 if (!can_add(err, cmd, 1))
544 cmd->cmd[cmd->pos++] = tok;
547 static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
548 bool has_sign, int len)
553 atom = SHORT_ATOM_ID;
554 atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
555 atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
556 atom |= len & SHORT_ATOM_LEN_MASK;
558 add_token_u8(&err, cmd, atom);
561 static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
562 bool has_sign, int len)
566 header0 = MEDIUM_ATOM_ID;
567 header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
568 header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
569 header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
570 cmd->cmd[cmd->pos++] = header0;
571 cmd->cmd[cmd->pos++] = len;
574 static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
579 if (!(number & ~TINY_ATOM_DATA_MASK)) {
580 add_token_u8(err, cmd, number);
585 len = DIV_ROUND_UP(msb, 8);
587 if (!can_add(err, cmd, len + 1)) {
588 pr_debug("Error adding u64: end of buffer.\n");
591 add_short_atom_header(cmd, false, false, len);
593 add_token_u8(err, cmd, number >> (len * 8));
596 static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len)
598 size_t header_len = 1;
599 bool is_short_atom = true;
601 if (len & ~SHORT_ATOM_LEN_MASK) {
603 is_short_atom = false;
606 if (!can_add(err, cmd, header_len + len)) {
607 pr_debug("Error adding bytestring: end of buffer.\n");
612 add_short_atom_header(cmd, true, false, len);
614 add_medium_atom_header(cmd, true, false, len);
616 return &cmd->cmd[cmd->pos];
619 static void add_token_bytestring(int *err, struct opal_dev *cmd,
620 const u8 *bytestring, size_t len)
624 start = add_bytestring_header(err, cmd, len);
627 memcpy(start, bytestring, len);
631 static int build_locking_range(u8 *buffer, size_t length, u8 lr)
633 if (length > OPAL_UID_LENGTH) {
634 pr_debug("Can't build locking range. Length OOB\n");
638 memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
642 buffer[5] = LOCKING_RANGE_NON_GLOBAL;
648 static int build_locking_user(u8 *buffer, size_t length, u8 lr)
650 if (length > OPAL_UID_LENGTH) {
651 pr_debug("Can't build locking range user. Length OOB\n");
655 memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
662 static void set_comid(struct opal_dev *cmd, u16 comid)
664 struct opal_header *hdr = (struct opal_header *)cmd->cmd;
666 hdr->cp.extendedComID[0] = comid >> 8;
667 hdr->cp.extendedComID[1] = comid;
668 hdr->cp.extendedComID[2] = 0;
669 hdr->cp.extendedComID[3] = 0;
672 static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
674 struct opal_header *hdr;
677 /* close the parameter list opened from cmd_start */
678 add_token_u8(&err, cmd, OPAL_ENDLIST);
680 add_token_u8(&err, cmd, OPAL_ENDOFDATA);
681 add_token_u8(&err, cmd, OPAL_STARTLIST);
682 add_token_u8(&err, cmd, 0);
683 add_token_u8(&err, cmd, 0);
684 add_token_u8(&err, cmd, 0);
685 add_token_u8(&err, cmd, OPAL_ENDLIST);
688 pr_debug("Error finalizing command.\n");
692 hdr = (struct opal_header *) cmd->cmd;
694 hdr->pkt.tsn = cpu_to_be32(tsn);
695 hdr->pkt.hsn = cpu_to_be32(hsn);
697 hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
698 while (cmd->pos % 4) {
699 if (cmd->pos >= IO_BUFFER_LENGTH) {
700 pr_debug("Error: Buffer overrun\n");
703 cmd->cmd[cmd->pos++] = 0;
705 hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
707 hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
712 static const struct opal_resp_tok *response_get_token(
713 const struct parsed_resp *resp,
716 const struct opal_resp_tok *tok;
719 pr_debug("Response is NULL\n");
720 return ERR_PTR(-EINVAL);
723 if (n >= resp->num) {
724 pr_debug("Token number doesn't exist: %d, resp: %d\n",
726 return ERR_PTR(-EINVAL);
729 tok = &resp->toks[n];
731 pr_debug("Token length must be non-zero\n");
732 return ERR_PTR(-EINVAL);
738 static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
743 tok->width = OPAL_WIDTH_TINY;
745 if (pos[0] & TINY_ATOM_SIGNED) {
746 tok->type = OPAL_DTA_TOKENID_SINT;
748 tok->type = OPAL_DTA_TOKENID_UINT;
749 tok->stored.u = pos[0] & 0x3f;
755 static ssize_t response_parse_short(struct opal_resp_tok *tok,
759 tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
760 tok->width = OPAL_WIDTH_SHORT;
762 if (pos[0] & SHORT_ATOM_BYTESTRING) {
763 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
764 } else if (pos[0] & SHORT_ATOM_SIGNED) {
765 tok->type = OPAL_DTA_TOKENID_SINT;
770 tok->type = OPAL_DTA_TOKENID_UINT;
772 pr_debug("uint64 with more than 8 bytes\n");
775 for (i = tok->len - 1; i > 0; i--) {
776 u_integer |= ((u64)pos[i] << (8 * b));
779 tok->stored.u = u_integer;
785 static ssize_t response_parse_medium(struct opal_resp_tok *tok,
789 tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
790 tok->width = OPAL_WIDTH_MEDIUM;
792 if (pos[0] & MEDIUM_ATOM_BYTESTRING)
793 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
794 else if (pos[0] & MEDIUM_ATOM_SIGNED)
795 tok->type = OPAL_DTA_TOKENID_SINT;
797 tok->type = OPAL_DTA_TOKENID_UINT;
802 static ssize_t response_parse_long(struct opal_resp_tok *tok,
806 tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
807 tok->width = OPAL_WIDTH_LONG;
809 if (pos[0] & LONG_ATOM_BYTESTRING)
810 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
811 else if (pos[0] & LONG_ATOM_SIGNED)
812 tok->type = OPAL_DTA_TOKENID_SINT;
814 tok->type = OPAL_DTA_TOKENID_UINT;
819 static ssize_t response_parse_token(struct opal_resp_tok *tok,
824 tok->type = OPAL_DTA_TOKENID_TOKEN;
825 tok->width = OPAL_WIDTH_TOKEN;
830 static int response_parse(const u8 *buf, size_t length,
831 struct parsed_resp *resp)
833 const struct opal_header *hdr;
834 struct opal_resp_tok *iter;
837 ssize_t token_length;
839 u32 clen, plen, slen;
847 hdr = (struct opal_header *)buf;
851 clen = be32_to_cpu(hdr->cp.length);
852 plen = be32_to_cpu(hdr->pkt.length);
853 slen = be32_to_cpu(hdr->subpkt.length);
854 pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
857 if (clen == 0 || plen == 0 || slen == 0 ||
858 slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
859 pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
861 print_buffer(pos, sizeof(*hdr));
865 if (pos > buf + length)
870 print_buffer(pos, total);
872 if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
873 token_length = response_parse_tiny(iter, pos);
874 else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
875 token_length = response_parse_short(iter, pos);
876 else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
877 token_length = response_parse_medium(iter, pos);
878 else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
879 token_length = response_parse_long(iter, pos);
881 token_length = response_parse_token(iter, pos);
883 if (token_length < 0)
887 total -= token_length;
892 if (num_entries == 0) {
893 pr_debug("Couldn't parse response.\n");
896 resp->num = num_entries;
901 static size_t response_get_string(const struct parsed_resp *resp, int n,
905 const struct opal_resp_tok *tok;
908 tok = response_get_token(resp, n);
912 if (tok->type != OPAL_DTA_TOKENID_BYTESTRING) {
913 pr_debug("Token is not a byte string!\n");
917 switch (tok->width) {
918 case OPAL_WIDTH_TINY:
919 case OPAL_WIDTH_SHORT:
922 case OPAL_WIDTH_MEDIUM:
925 case OPAL_WIDTH_LONG:
929 pr_debug("Token has invalid width!\n");
933 *store = tok->pos + skip;
934 return tok->len - skip;
937 static u64 response_get_u64(const struct parsed_resp *resp, int n)
939 const struct opal_resp_tok *tok;
941 tok = response_get_token(resp, n);
945 if (tok->type != OPAL_DTA_TOKENID_UINT) {
946 pr_debug("Token is not unsigned int: %d\n", tok->type);
950 if (tok->width != OPAL_WIDTH_TINY && tok->width != OPAL_WIDTH_SHORT) {
951 pr_debug("Atom is not short or tiny: %d\n", tok->width);
955 return tok->stored.u;
958 static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
961 token->type != OPAL_DTA_TOKENID_TOKEN ||
962 token->pos[0] != match)
967 static u8 response_status(const struct parsed_resp *resp)
969 const struct opal_resp_tok *tok;
971 tok = response_get_token(resp, 0);
972 if (response_token_matches(tok, OPAL_ENDOFSESSION))
976 return DTAERROR_NO_METHOD_STATUS;
978 tok = response_get_token(resp, resp->num - 5);
979 if (!response_token_matches(tok, OPAL_STARTLIST))
980 return DTAERROR_NO_METHOD_STATUS;
982 tok = response_get_token(resp, resp->num - 1);
983 if (!response_token_matches(tok, OPAL_ENDLIST))
984 return DTAERROR_NO_METHOD_STATUS;
986 return response_get_u64(resp, resp->num - 4);
989 /* Parses and checks for errors */
990 static int parse_and_check_status(struct opal_dev *dev)
994 print_buffer(dev->cmd, dev->pos);
996 error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
998 pr_debug("Couldn't parse response.\n");
1002 return response_status(&dev->parsed);
1005 static void clear_opal_cmd(struct opal_dev *dev)
1007 dev->pos = sizeof(struct opal_header);
1008 memset(dev->cmd, 0, IO_BUFFER_LENGTH);
1011 static int cmd_start(struct opal_dev *dev, const u8 *uid, const u8 *method)
1015 clear_opal_cmd(dev);
1016 set_comid(dev, dev->comid);
1018 add_token_u8(&err, dev, OPAL_CALL);
1019 add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
1020 add_token_bytestring(&err, dev, method, OPAL_METHOD_LENGTH);
1023 * Every method call is followed by its parameters enclosed within
1024 * OPAL_STARTLIST and OPAL_ENDLIST tokens. We automatically open the
1025 * parameter list here and close it later in cmd_finalize.
1027 add_token_u8(&err, dev, OPAL_STARTLIST);
1032 static int start_opal_session_cont(struct opal_dev *dev)
1037 error = parse_and_check_status(dev);
1041 hsn = response_get_u64(&dev->parsed, 4);
1042 tsn = response_get_u64(&dev->parsed, 5);
1044 if (hsn == 0 && tsn == 0) {
1045 pr_debug("Couldn't authenticate session\n");
1054 static void add_suspend_info(struct opal_dev *dev,
1055 struct opal_suspend_data *sus)
1057 struct opal_suspend_data *iter;
1059 list_for_each_entry(iter, &dev->unlk_lst, node) {
1060 if (iter->lr == sus->lr) {
1061 list_del(&iter->node);
1066 list_add_tail(&sus->node, &dev->unlk_lst);
1069 static int end_session_cont(struct opal_dev *dev)
1073 return parse_and_check_status(dev);
1076 static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
1080 ret = cmd_finalize(dev, dev->hsn, dev->tsn);
1082 pr_debug("Error finalizing command buffer: %d\n", ret);
1086 print_buffer(dev->cmd, dev->pos);
1088 return opal_send_recv(dev, cont);
1092 * request @column from table @table on device @dev. On success, the column
1093 * data will be available in dev->resp->tok[4]
1095 static int generic_get_column(struct opal_dev *dev, const u8 *table,
1100 err = cmd_start(dev, table, opalmethod[OPAL_GET]);
1102 add_token_u8(&err, dev, OPAL_STARTLIST);
1104 add_token_u8(&err, dev, OPAL_STARTNAME);
1105 add_token_u8(&err, dev, OPAL_STARTCOLUMN);
1106 add_token_u64(&err, dev, column);
1107 add_token_u8(&err, dev, OPAL_ENDNAME);
1109 add_token_u8(&err, dev, OPAL_STARTNAME);
1110 add_token_u8(&err, dev, OPAL_ENDCOLUMN);
1111 add_token_u64(&err, dev, column);
1112 add_token_u8(&err, dev, OPAL_ENDNAME);
1114 add_token_u8(&err, dev, OPAL_ENDLIST);
1119 return finalize_and_send(dev, parse_and_check_status);
1122 static int gen_key(struct opal_dev *dev, void *data)
1124 u8 uid[OPAL_UID_LENGTH];
1127 memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
1128 kfree(dev->prev_data);
1129 dev->prev_data = NULL;
1131 err = cmd_start(dev, uid, opalmethod[OPAL_GENKEY]);
1134 pr_debug("Error building gen key command\n");
1138 return finalize_and_send(dev, parse_and_check_status);
1141 static int get_active_key_cont(struct opal_dev *dev)
1143 const char *activekey;
1147 error = parse_and_check_status(dev);
1150 keylen = response_get_string(&dev->parsed, 4, &activekey);
1152 pr_debug("%s: Couldn't extract the Activekey from the response\n",
1154 return OPAL_INVAL_PARAM;
1156 dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
1158 if (!dev->prev_data)
1161 dev->prev_d_len = keylen;
1166 static int get_active_key(struct opal_dev *dev, void *data)
1168 u8 uid[OPAL_UID_LENGTH];
1172 err = build_locking_range(uid, sizeof(uid), *lr);
1176 err = generic_get_column(dev, uid, OPAL_ACTIVEKEY);
1180 return get_active_key_cont(dev);
1183 static int generic_lr_enable_disable(struct opal_dev *dev,
1184 u8 *uid, bool rle, bool wle,
1189 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1191 add_token_u8(&err, dev, OPAL_STARTNAME);
1192 add_token_u8(&err, dev, OPAL_VALUES);
1193 add_token_u8(&err, dev, OPAL_STARTLIST);
1195 add_token_u8(&err, dev, OPAL_STARTNAME);
1196 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1197 add_token_u8(&err, dev, rle);
1198 add_token_u8(&err, dev, OPAL_ENDNAME);
1200 add_token_u8(&err, dev, OPAL_STARTNAME);
1201 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1202 add_token_u8(&err, dev, wle);
1203 add_token_u8(&err, dev, OPAL_ENDNAME);
1205 add_token_u8(&err, dev, OPAL_STARTNAME);
1206 add_token_u8(&err, dev, OPAL_READLOCKED);
1207 add_token_u8(&err, dev, rl);
1208 add_token_u8(&err, dev, OPAL_ENDNAME);
1210 add_token_u8(&err, dev, OPAL_STARTNAME);
1211 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1212 add_token_u8(&err, dev, wl);
1213 add_token_u8(&err, dev, OPAL_ENDNAME);
1215 add_token_u8(&err, dev, OPAL_ENDLIST);
1216 add_token_u8(&err, dev, OPAL_ENDNAME);
1220 static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
1221 struct opal_user_lr_setup *setup)
1225 err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
1228 pr_debug("Failed to create enable global lr command\n");
1232 static int setup_locking_range(struct opal_dev *dev, void *data)
1234 u8 uid[OPAL_UID_LENGTH];
1235 struct opal_user_lr_setup *setup = data;
1239 lr = setup->session.opal_key.lr;
1240 err = build_locking_range(uid, sizeof(uid), lr);
1245 err = enable_global_lr(dev, uid, setup);
1247 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1249 add_token_u8(&err, dev, OPAL_STARTNAME);
1250 add_token_u8(&err, dev, OPAL_VALUES);
1251 add_token_u8(&err, dev, OPAL_STARTLIST);
1253 add_token_u8(&err, dev, OPAL_STARTNAME);
1254 add_token_u8(&err, dev, OPAL_RANGESTART);
1255 add_token_u64(&err, dev, setup->range_start);
1256 add_token_u8(&err, dev, OPAL_ENDNAME);
1258 add_token_u8(&err, dev, OPAL_STARTNAME);
1259 add_token_u8(&err, dev, OPAL_RANGELENGTH);
1260 add_token_u64(&err, dev, setup->range_length);
1261 add_token_u8(&err, dev, OPAL_ENDNAME);
1263 add_token_u8(&err, dev, OPAL_STARTNAME);
1264 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1265 add_token_u64(&err, dev, !!setup->RLE);
1266 add_token_u8(&err, dev, OPAL_ENDNAME);
1268 add_token_u8(&err, dev, OPAL_STARTNAME);
1269 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1270 add_token_u64(&err, dev, !!setup->WLE);
1271 add_token_u8(&err, dev, OPAL_ENDNAME);
1273 add_token_u8(&err, dev, OPAL_ENDLIST);
1274 add_token_u8(&err, dev, OPAL_ENDNAME);
1277 pr_debug("Error building Setup Locking range command.\n");
1282 return finalize_and_send(dev, parse_and_check_status);
1285 static int start_generic_opal_session(struct opal_dev *dev,
1287 enum opal_uid sp_type,
1294 if (key == NULL && auth != OPAL_ANYBODY_UID)
1295 return OPAL_INVAL_PARAM;
1297 hsn = GENERIC_HOST_SESSION_NUM;
1298 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1299 opalmethod[OPAL_STARTSESSION]);
1301 add_token_u64(&err, dev, hsn);
1302 add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
1303 add_token_u8(&err, dev, 1);
1306 case OPAL_ANYBODY_UID:
1308 case OPAL_ADMIN1_UID:
1310 add_token_u8(&err, dev, OPAL_STARTNAME);
1311 add_token_u8(&err, dev, 0); /* HostChallenge */
1312 add_token_bytestring(&err, dev, key, key_len);
1313 add_token_u8(&err, dev, OPAL_ENDNAME);
1314 add_token_u8(&err, dev, OPAL_STARTNAME);
1315 add_token_u8(&err, dev, 3); /* HostSignAuth */
1316 add_token_bytestring(&err, dev, opaluid[auth],
1318 add_token_u8(&err, dev, OPAL_ENDNAME);
1321 pr_debug("Cannot start Admin SP session with auth %d\n", auth);
1322 return OPAL_INVAL_PARAM;
1326 pr_debug("Error building start adminsp session command.\n");
1330 return finalize_and_send(dev, start_opal_session_cont);
1333 static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
1335 return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
1336 OPAL_ADMINSP_UID, NULL, 0);
1339 static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
1342 const u8 *key = dev->prev_data;
1345 const struct opal_key *okey = data;
1347 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1352 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1354 key, dev->prev_d_len);
1356 dev->prev_data = NULL;
1361 static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
1363 struct opal_key *key = data;
1365 return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
1367 key->key, key->key_len);
1370 static int start_auth_opal_session(struct opal_dev *dev, void *data)
1372 struct opal_session_info *session = data;
1373 u8 lk_ul_user[OPAL_UID_LENGTH];
1374 size_t keylen = session->opal_key.key_len;
1377 u8 *key = session->opal_key.key;
1378 u32 hsn = GENERIC_HOST_SESSION_NUM;
1381 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1382 session->opal_key.lr);
1383 else if (session->who != OPAL_ADMIN1 && !session->sum)
1384 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1387 memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
1392 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1393 opalmethod[OPAL_STARTSESSION]);
1395 add_token_u64(&err, dev, hsn);
1396 add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
1398 add_token_u8(&err, dev, 1);
1399 add_token_u8(&err, dev, OPAL_STARTNAME);
1400 add_token_u8(&err, dev, 0);
1401 add_token_bytestring(&err, dev, key, keylen);
1402 add_token_u8(&err, dev, OPAL_ENDNAME);
1403 add_token_u8(&err, dev, OPAL_STARTNAME);
1404 add_token_u8(&err, dev, 3);
1405 add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
1406 add_token_u8(&err, dev, OPAL_ENDNAME);
1409 pr_debug("Error building STARTSESSION command.\n");
1413 return finalize_and_send(dev, start_opal_session_cont);
1416 static int revert_tper(struct opal_dev *dev, void *data)
1420 err = cmd_start(dev, opaluid[OPAL_ADMINSP_UID],
1421 opalmethod[OPAL_REVERT]);
1423 pr_debug("Error building REVERT TPER command.\n");
1427 return finalize_and_send(dev, parse_and_check_status);
1430 static int internal_activate_user(struct opal_dev *dev, void *data)
1432 struct opal_session_info *session = data;
1433 u8 uid[OPAL_UID_LENGTH];
1436 memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1437 uid[7] = session->who;
1439 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1440 add_token_u8(&err, dev, OPAL_STARTNAME);
1441 add_token_u8(&err, dev, OPAL_VALUES);
1442 add_token_u8(&err, dev, OPAL_STARTLIST);
1443 add_token_u8(&err, dev, OPAL_STARTNAME);
1444 add_token_u8(&err, dev, 5); /* Enabled */
1445 add_token_u8(&err, dev, OPAL_TRUE);
1446 add_token_u8(&err, dev, OPAL_ENDNAME);
1447 add_token_u8(&err, dev, OPAL_ENDLIST);
1448 add_token_u8(&err, dev, OPAL_ENDNAME);
1451 pr_debug("Error building Activate UserN command.\n");
1455 return finalize_and_send(dev, parse_and_check_status);
1458 static int erase_locking_range(struct opal_dev *dev, void *data)
1460 struct opal_session_info *session = data;
1461 u8 uid[OPAL_UID_LENGTH];
1464 if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
1467 err = cmd_start(dev, uid, opalmethod[OPAL_ERASE]);
1470 pr_debug("Error building Erase Locking Range Command.\n");
1473 return finalize_and_send(dev, parse_and_check_status);
1476 static int set_mbr_done(struct opal_dev *dev, void *data)
1478 u8 *mbr_done_tf = data;
1481 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1482 opalmethod[OPAL_SET]);
1484 add_token_u8(&err, dev, OPAL_STARTNAME);
1485 add_token_u8(&err, dev, OPAL_VALUES);
1486 add_token_u8(&err, dev, OPAL_STARTLIST);
1487 add_token_u8(&err, dev, OPAL_STARTNAME);
1488 add_token_u8(&err, dev, OPAL_MBRDONE);
1489 add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
1490 add_token_u8(&err, dev, OPAL_ENDNAME);
1491 add_token_u8(&err, dev, OPAL_ENDLIST);
1492 add_token_u8(&err, dev, OPAL_ENDNAME);
1495 pr_debug("Error Building set MBR Done command\n");
1499 return finalize_and_send(dev, parse_and_check_status);
1502 static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
1504 u8 *mbr_en_dis = data;
1507 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1508 opalmethod[OPAL_SET]);
1510 add_token_u8(&err, dev, OPAL_STARTNAME);
1511 add_token_u8(&err, dev, OPAL_VALUES);
1512 add_token_u8(&err, dev, OPAL_STARTLIST);
1513 add_token_u8(&err, dev, OPAL_STARTNAME);
1514 add_token_u8(&err, dev, OPAL_MBRENABLE);
1515 add_token_u8(&err, dev, *mbr_en_dis);
1516 add_token_u8(&err, dev, OPAL_ENDNAME);
1517 add_token_u8(&err, dev, OPAL_ENDLIST);
1518 add_token_u8(&err, dev, OPAL_ENDNAME);
1521 pr_debug("Error Building set MBR done command\n");
1525 return finalize_and_send(dev, parse_and_check_status);
1528 static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
1529 struct opal_dev *dev)
1533 err = cmd_start(dev, cpin_uid, opalmethod[OPAL_SET]);
1535 add_token_u8(&err, dev, OPAL_STARTNAME);
1536 add_token_u8(&err, dev, OPAL_VALUES);
1537 add_token_u8(&err, dev, OPAL_STARTLIST);
1538 add_token_u8(&err, dev, OPAL_STARTNAME);
1539 add_token_u8(&err, dev, OPAL_PIN);
1540 add_token_bytestring(&err, dev, key, key_len);
1541 add_token_u8(&err, dev, OPAL_ENDNAME);
1542 add_token_u8(&err, dev, OPAL_ENDLIST);
1543 add_token_u8(&err, dev, OPAL_ENDNAME);
1548 static int set_new_pw(struct opal_dev *dev, void *data)
1550 u8 cpin_uid[OPAL_UID_LENGTH];
1551 struct opal_session_info *usr = data;
1553 memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
1555 if (usr->who != OPAL_ADMIN1) {
1558 cpin_uid[7] = usr->opal_key.lr + 1;
1560 cpin_uid[7] = usr->who;
1563 if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
1565 pr_debug("Error building set password command.\n");
1569 return finalize_and_send(dev, parse_and_check_status);
1572 static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
1574 u8 cpin_uid[OPAL_UID_LENGTH];
1575 struct opal_key *key = data;
1577 memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
1579 if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
1580 pr_debug("Error building Set SID cpin\n");
1583 return finalize_and_send(dev, parse_and_check_status);
1586 static int add_user_to_lr(struct opal_dev *dev, void *data)
1588 u8 lr_buffer[OPAL_UID_LENGTH];
1589 u8 user_uid[OPAL_UID_LENGTH];
1590 struct opal_lock_unlock *lkul = data;
1593 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_RDLOCKED],
1596 if (lkul->l_state == OPAL_RW)
1597 memcpy(lr_buffer, opaluid[OPAL_LOCKINGRANGE_ACE_WRLOCKED],
1600 lr_buffer[7] = lkul->session.opal_key.lr;
1602 memcpy(user_uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1604 user_uid[7] = lkul->session.who;
1606 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1608 add_token_u8(&err, dev, OPAL_STARTNAME);
1609 add_token_u8(&err, dev, OPAL_VALUES);
1611 add_token_u8(&err, dev, OPAL_STARTLIST);
1612 add_token_u8(&err, dev, OPAL_STARTNAME);
1613 add_token_u8(&err, dev, 3);
1615 add_token_u8(&err, dev, OPAL_STARTLIST);
1618 add_token_u8(&err, dev, OPAL_STARTNAME);
1619 add_token_bytestring(&err, dev,
1620 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1622 add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1623 add_token_u8(&err, dev, OPAL_ENDNAME);
1626 add_token_u8(&err, dev, OPAL_STARTNAME);
1627 add_token_bytestring(&err, dev,
1628 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
1630 add_token_bytestring(&err, dev, user_uid, OPAL_UID_LENGTH);
1631 add_token_u8(&err, dev, OPAL_ENDNAME);
1634 add_token_u8(&err, dev, OPAL_STARTNAME);
1635 add_token_bytestring(&err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
1637 add_token_u8(&err, dev, 1);
1638 add_token_u8(&err, dev, OPAL_ENDNAME);
1641 add_token_u8(&err, dev, OPAL_ENDLIST);
1642 add_token_u8(&err, dev, OPAL_ENDNAME);
1643 add_token_u8(&err, dev, OPAL_ENDLIST);
1644 add_token_u8(&err, dev, OPAL_ENDNAME);
1647 pr_debug("Error building add user to locking range command.\n");
1651 return finalize_and_send(dev, parse_and_check_status);
1654 static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
1656 u8 lr_buffer[OPAL_UID_LENGTH];
1657 struct opal_lock_unlock *lkul = data;
1658 u8 read_locked = 1, write_locked = 1;
1661 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1662 lkul->session.opal_key.lr) < 0)
1665 switch (lkul->l_state) {
1675 /* vars are initialized to locked */
1678 pr_debug("Tried to set an invalid locking state... returning to uland\n");
1679 return OPAL_INVAL_PARAM;
1682 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
1684 add_token_u8(&err, dev, OPAL_STARTNAME);
1685 add_token_u8(&err, dev, OPAL_VALUES);
1686 add_token_u8(&err, dev, OPAL_STARTLIST);
1688 add_token_u8(&err, dev, OPAL_STARTNAME);
1689 add_token_u8(&err, dev, OPAL_READLOCKED);
1690 add_token_u8(&err, dev, read_locked);
1691 add_token_u8(&err, dev, OPAL_ENDNAME);
1693 add_token_u8(&err, dev, OPAL_STARTNAME);
1694 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1695 add_token_u8(&err, dev, write_locked);
1696 add_token_u8(&err, dev, OPAL_ENDNAME);
1698 add_token_u8(&err, dev, OPAL_ENDLIST);
1699 add_token_u8(&err, dev, OPAL_ENDNAME);
1702 pr_debug("Error building SET command.\n");
1705 return finalize_and_send(dev, parse_and_check_status);
1709 static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
1711 u8 lr_buffer[OPAL_UID_LENGTH];
1712 u8 read_locked = 1, write_locked = 1;
1713 struct opal_lock_unlock *lkul = data;
1716 clear_opal_cmd(dev);
1717 set_comid(dev, dev->comid);
1719 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
1720 lkul->session.opal_key.lr) < 0)
1723 switch (lkul->l_state) {
1733 /* vars are initialized to locked */
1736 pr_debug("Tried to set an invalid locking state.\n");
1737 return OPAL_INVAL_PARAM;
1739 ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
1740 read_locked, write_locked);
1743 pr_debug("Error building SET command.\n");
1746 return finalize_and_send(dev, parse_and_check_status);
1749 static int activate_lsp(struct opal_dev *dev, void *data)
1751 struct opal_lr_act *opal_act = data;
1752 u8 user_lr[OPAL_UID_LENGTH];
1756 err = cmd_start(dev, opaluid[OPAL_LOCKINGSP_UID],
1757 opalmethod[OPAL_ACTIVATE]);
1759 if (opal_act->sum) {
1760 err = build_locking_range(user_lr, sizeof(user_lr),
1765 add_token_u8(&err, dev, OPAL_STARTNAME);
1766 add_token_u8(&err, dev, uint_3);
1767 add_token_u8(&err, dev, 6);
1768 add_token_u8(&err, dev, 0);
1769 add_token_u8(&err, dev, 0);
1771 add_token_u8(&err, dev, OPAL_STARTLIST);
1772 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1773 for (i = 1; i < opal_act->num_lrs; i++) {
1774 user_lr[7] = opal_act->lr[i];
1775 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
1777 add_token_u8(&err, dev, OPAL_ENDLIST);
1778 add_token_u8(&err, dev, OPAL_ENDNAME);
1782 pr_debug("Error building Activate LockingSP command.\n");
1786 return finalize_and_send(dev, parse_and_check_status);
1789 /* Determine if we're in the Manufactured Inactive or Active state */
1790 static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
1795 err = generic_get_column(dev, opaluid[OPAL_LOCKINGSP_UID],
1800 lc_status = response_get_u64(&dev->parsed, 4);
1801 /* 0x08 is Manufactured Inactive */
1802 /* 0x09 is Manufactured */
1803 if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
1804 pr_debug("Couldn't determine the status of the Lifecycle state\n");
1811 static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
1813 const char *msid_pin;
1817 err = generic_get_column(dev, opaluid[OPAL_C_PIN_MSID], OPAL_PIN);
1821 strlen = response_get_string(&dev->parsed, 4, &msid_pin);
1823 pr_debug("Couldn't extract MSID_CPIN from response\n");
1824 return OPAL_INVAL_PARAM;
1827 dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
1828 if (!dev->prev_data)
1831 dev->prev_d_len = strlen;
1836 static int end_opal_session(struct opal_dev *dev, void *data)
1840 clear_opal_cmd(dev);
1841 set_comid(dev, dev->comid);
1842 add_token_u8(&err, dev, OPAL_ENDOFSESSION);
1846 return finalize_and_send(dev, end_session_cont);
1849 static int end_opal_session_error(struct opal_dev *dev)
1851 const struct opal_step error_end_session = {
1854 return execute_step(dev, &error_end_session, 0);
1857 static inline void setup_opal_dev(struct opal_dev *dev)
1861 dev->prev_data = NULL;
1864 static int check_opal_support(struct opal_dev *dev)
1868 mutex_lock(&dev->dev_lock);
1869 setup_opal_dev(dev);
1870 ret = opal_discovery0_step(dev);
1871 dev->supported = !ret;
1872 mutex_unlock(&dev->dev_lock);
1876 static void clean_opal_dev(struct opal_dev *dev)
1879 struct opal_suspend_data *suspend, *next;
1881 mutex_lock(&dev->dev_lock);
1882 list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
1883 list_del(&suspend->node);
1886 mutex_unlock(&dev->dev_lock);
1889 void free_opal_dev(struct opal_dev *dev)
1893 clean_opal_dev(dev);
1896 EXPORT_SYMBOL(free_opal_dev);
1898 struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
1900 struct opal_dev *dev;
1902 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
1906 INIT_LIST_HEAD(&dev->unlk_lst);
1907 mutex_init(&dev->dev_lock);
1909 dev->send_recv = send_recv;
1910 if (check_opal_support(dev) != 0) {
1911 pr_debug("Opal is not supported on this device\n");
1917 EXPORT_SYMBOL(init_opal_dev);
1919 static int opal_secure_erase_locking_range(struct opal_dev *dev,
1920 struct opal_session_info *opal_session)
1922 const struct opal_step erase_steps[] = {
1923 { start_auth_opal_session, opal_session },
1924 { get_active_key, &opal_session->opal_key.lr },
1926 { end_opal_session, }
1930 mutex_lock(&dev->dev_lock);
1931 setup_opal_dev(dev);
1932 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
1933 mutex_unlock(&dev->dev_lock);
1937 static int opal_erase_locking_range(struct opal_dev *dev,
1938 struct opal_session_info *opal_session)
1940 const struct opal_step erase_steps[] = {
1941 { start_auth_opal_session, opal_session },
1942 { erase_locking_range, opal_session },
1943 { end_opal_session, }
1947 mutex_lock(&dev->dev_lock);
1948 setup_opal_dev(dev);
1949 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
1950 mutex_unlock(&dev->dev_lock);
1954 static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
1955 struct opal_mbr_data *opal_mbr)
1957 u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
1958 OPAL_TRUE : OPAL_FALSE;
1960 const struct opal_step mbr_steps[] = {
1961 { start_admin1LSP_opal_session, &opal_mbr->key },
1962 { set_mbr_done, &enable_disable },
1963 { end_opal_session, },
1964 { start_admin1LSP_opal_session, &opal_mbr->key },
1965 { set_mbr_enable_disable, &enable_disable },
1966 { end_opal_session, }
1970 if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
1971 opal_mbr->enable_disable != OPAL_MBR_DISABLE)
1974 mutex_lock(&dev->dev_lock);
1975 setup_opal_dev(dev);
1976 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
1977 mutex_unlock(&dev->dev_lock);
1981 static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
1983 struct opal_suspend_data *suspend;
1985 suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
1989 suspend->unlk = *lk_unlk;
1990 suspend->lr = lk_unlk->session.opal_key.lr;
1992 mutex_lock(&dev->dev_lock);
1993 setup_opal_dev(dev);
1994 add_suspend_info(dev, suspend);
1995 mutex_unlock(&dev->dev_lock);
1999 static int opal_add_user_to_lr(struct opal_dev *dev,
2000 struct opal_lock_unlock *lk_unlk)
2002 const struct opal_step steps[] = {
2003 { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
2004 { add_user_to_lr, lk_unlk },
2005 { end_opal_session, }
2009 if (lk_unlk->l_state != OPAL_RO &&
2010 lk_unlk->l_state != OPAL_RW) {
2011 pr_debug("Locking state was not RO or RW\n");
2014 if (lk_unlk->session.who < OPAL_USER1 ||
2015 lk_unlk->session.who > OPAL_USER9) {
2016 pr_debug("Authority was not within the range of users: %d\n",
2017 lk_unlk->session.who);
2020 if (lk_unlk->session.sum) {
2021 pr_debug("%s not supported in sum. Use setup locking range\n",
2026 mutex_lock(&dev->dev_lock);
2027 setup_opal_dev(dev);
2028 ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
2029 mutex_unlock(&dev->dev_lock);
2033 static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal)
2035 const struct opal_step revert_steps[] = {
2036 { start_SIDASP_opal_session, opal },
2037 { revert_tper, } /* controller will terminate session */
2041 mutex_lock(&dev->dev_lock);
2042 setup_opal_dev(dev);
2043 ret = execute_steps(dev, revert_steps, ARRAY_SIZE(revert_steps));
2044 mutex_unlock(&dev->dev_lock);
2047 * If we successfully reverted lets clean
2048 * any saved locking ranges.
2051 clean_opal_dev(dev);
2056 static int __opal_lock_unlock(struct opal_dev *dev,
2057 struct opal_lock_unlock *lk_unlk)
2059 const struct opal_step unlock_steps[] = {
2060 { start_auth_opal_session, &lk_unlk->session },
2061 { lock_unlock_locking_range, lk_unlk },
2062 { end_opal_session, }
2064 const struct opal_step unlock_sum_steps[] = {
2065 { start_auth_opal_session, &lk_unlk->session },
2066 { lock_unlock_locking_range_sum, lk_unlk },
2067 { end_opal_session, }
2070 if (lk_unlk->session.sum)
2071 return execute_steps(dev, unlock_sum_steps,
2072 ARRAY_SIZE(unlock_sum_steps));
2074 return execute_steps(dev, unlock_steps,
2075 ARRAY_SIZE(unlock_steps));
2078 static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
2080 u8 mbr_done_tf = OPAL_TRUE;
2081 const struct opal_step mbrdone_step[] = {
2082 { start_admin1LSP_opal_session, key },
2083 { set_mbr_done, &mbr_done_tf },
2084 { end_opal_session, }
2087 return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
2090 static int opal_lock_unlock(struct opal_dev *dev,
2091 struct opal_lock_unlock *lk_unlk)
2095 if (lk_unlk->session.who < OPAL_ADMIN1 ||
2096 lk_unlk->session.who > OPAL_USER9)
2099 mutex_lock(&dev->dev_lock);
2100 ret = __opal_lock_unlock(dev, lk_unlk);
2101 mutex_unlock(&dev->dev_lock);
2105 static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
2107 const struct opal_step owner_steps[] = {
2108 { start_anybodyASP_opal_session, },
2109 { get_msid_cpin_pin, },
2110 { end_opal_session, },
2111 { start_SIDASP_opal_session, opal },
2112 { set_sid_cpin_pin, opal },
2113 { end_opal_session, }
2120 mutex_lock(&dev->dev_lock);
2121 setup_opal_dev(dev);
2122 ret = execute_steps(dev, owner_steps, ARRAY_SIZE(owner_steps));
2123 mutex_unlock(&dev->dev_lock);
2127 static int opal_activate_lsp(struct opal_dev *dev,
2128 struct opal_lr_act *opal_lr_act)
2130 const struct opal_step active_steps[] = {
2131 { start_SIDASP_opal_session, &opal_lr_act->key },
2132 { get_lsp_lifecycle, },
2133 { activate_lsp, opal_lr_act },
2134 { end_opal_session, }
2138 if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
2141 mutex_lock(&dev->dev_lock);
2142 setup_opal_dev(dev);
2143 ret = execute_steps(dev, active_steps, ARRAY_SIZE(active_steps));
2144 mutex_unlock(&dev->dev_lock);
2148 static int opal_setup_locking_range(struct opal_dev *dev,
2149 struct opal_user_lr_setup *opal_lrs)
2151 const struct opal_step lr_steps[] = {
2152 { start_auth_opal_session, &opal_lrs->session },
2153 { setup_locking_range, opal_lrs },
2154 { end_opal_session, }
2158 mutex_lock(&dev->dev_lock);
2159 setup_opal_dev(dev);
2160 ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
2161 mutex_unlock(&dev->dev_lock);
2165 static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
2167 const struct opal_step pw_steps[] = {
2168 { start_auth_opal_session, &opal_pw->session },
2169 { set_new_pw, &opal_pw->new_user_pw },
2170 { end_opal_session, }
2174 if (opal_pw->session.who < OPAL_ADMIN1 ||
2175 opal_pw->session.who > OPAL_USER9 ||
2176 opal_pw->new_user_pw.who < OPAL_ADMIN1 ||
2177 opal_pw->new_user_pw.who > OPAL_USER9)
2180 mutex_lock(&dev->dev_lock);
2181 setup_opal_dev(dev);
2182 ret = execute_steps(dev, pw_steps, ARRAY_SIZE(pw_steps));
2183 mutex_unlock(&dev->dev_lock);
2187 static int opal_activate_user(struct opal_dev *dev,
2188 struct opal_session_info *opal_session)
2190 const struct opal_step act_steps[] = {
2191 { start_admin1LSP_opal_session, &opal_session->opal_key },
2192 { internal_activate_user, opal_session },
2193 { end_opal_session, }
2197 /* We can't activate Admin1 it's active as manufactured */
2198 if (opal_session->who < OPAL_USER1 ||
2199 opal_session->who > OPAL_USER9) {
2200 pr_debug("Who was not a valid user: %d\n", opal_session->who);
2204 mutex_lock(&dev->dev_lock);
2205 setup_opal_dev(dev);
2206 ret = execute_steps(dev, act_steps, ARRAY_SIZE(act_steps));
2207 mutex_unlock(&dev->dev_lock);
2211 bool opal_unlock_from_suspend(struct opal_dev *dev)
2213 struct opal_suspend_data *suspend;
2214 bool was_failure = false;
2219 if (!dev->supported)
2222 mutex_lock(&dev->dev_lock);
2223 setup_opal_dev(dev);
2225 list_for_each_entry(suspend, &dev->unlk_lst, node) {
2229 ret = __opal_lock_unlock(dev, &suspend->unlk);
2231 pr_debug("Failed to unlock LR %hhu with sum %d\n",
2232 suspend->unlk.session.opal_key.lr,
2233 suspend->unlk.session.sum);
2236 if (dev->mbr_enabled) {
2237 ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
2239 pr_debug("Failed to set MBR Done in S3 resume\n");
2242 mutex_unlock(&dev->dev_lock);
2245 EXPORT_SYMBOL(opal_unlock_from_suspend);
2247 int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
2252 if (!capable(CAP_SYS_ADMIN))
2256 if (!dev->supported)
2259 p = memdup_user(arg, _IOC_SIZE(cmd));
2265 ret = opal_save(dev, p);
2267 case IOC_OPAL_LOCK_UNLOCK:
2268 ret = opal_lock_unlock(dev, p);
2270 case IOC_OPAL_TAKE_OWNERSHIP:
2271 ret = opal_take_ownership(dev, p);
2273 case IOC_OPAL_ACTIVATE_LSP:
2274 ret = opal_activate_lsp(dev, p);
2276 case IOC_OPAL_SET_PW:
2277 ret = opal_set_new_pw(dev, p);
2279 case IOC_OPAL_ACTIVATE_USR:
2280 ret = opal_activate_user(dev, p);
2282 case IOC_OPAL_REVERT_TPR:
2283 ret = opal_reverttper(dev, p);
2285 case IOC_OPAL_LR_SETUP:
2286 ret = opal_setup_locking_range(dev, p);
2288 case IOC_OPAL_ADD_USR_TO_LR:
2289 ret = opal_add_user_to_lr(dev, p);
2291 case IOC_OPAL_ENABLE_DISABLE_MBR:
2292 ret = opal_enable_disable_shadow_mbr(dev, p);
2294 case IOC_OPAL_ERASE_LR:
2295 ret = opal_erase_locking_range(dev, p);
2297 case IOC_OPAL_SECURE_ERASE_LR:
2298 ret = opal_secure_erase_locking_range(dev, p);
2307 EXPORT_SYMBOL_GPL(sed_ioctl);