1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2016 Intel Corporation
6 * Scott Bauer <scott.bauer@intel.com>
7 * Rafael Antognolli <rafael.antognolli@intel.com>
10 #define pr_fmt(fmt) KBUILD_MODNAME ":OPAL: " fmt
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/blkdev.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <uapi/linux/sed-opal.h>
20 #include <linux/sed-opal.h>
21 #include <linux/string.h>
22 #include <linux/kdev_t.h>
23 #include <linux/key.h>
24 #include <linux/key-type.h>
25 #include <keys/user-type.h>
27 #include "opal_proto.h"
29 #define IO_BUFFER_LENGTH 2048
32 /* Number of bytes needed by cmd_finalize. */
33 #define CMD_FINALIZE_BYTES_NEEDED 7
35 static struct key *sed_opal_keyring;
38 int (*fn)(struct opal_dev *dev, void *data);
41 typedef int (cont_fn)(struct opal_dev *dev);
43 enum opal_atom_width {
52 * On the parsed response, we don't store again the toks that are already
53 * stored in the response buffer. Instead, for each token, we just store a
54 * pointer to the position in the buffer where the token starts, and the size
55 * of the token in bytes.
57 struct opal_resp_tok {
60 enum opal_response_token type;
61 enum opal_atom_width width;
69 * From the response header it's not possible to know how many tokens there are
70 * on the payload. So we hardcode that the maximum will be MAX_TOKS, and later
71 * if we start dealing with messages that have more than that, we can increase
72 * this number. This is done to avoid having to make two passes through the
73 * response, the first one counting how many tokens we have and the second one
74 * actually storing the positions.
78 struct opal_resp_tok toks[MAX_TOKS];
85 sec_send_recv *send_recv;
87 struct mutex dev_lock;
91 u64 align; /* alignment granularity */
93 u32 logical_block_size;
94 u8 align_required; /* ALIGN: 0 or 1 */
100 struct parsed_resp parsed;
104 struct list_head unlk_lst;
108 static const u8 opaluid[][OPAL_UID_LENGTH] = {
111 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff },
113 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
115 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x01 },
116 [OPAL_LOCKINGSP_UID] =
117 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x00, 0x00, 0x02 },
118 [OPAL_ENTERPRISE_LOCKINGSP_UID] =
119 { 0x00, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00, 0x01 },
121 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x01 },
123 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x06 },
125 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x01 },
127 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x01 },
129 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x02 },
131 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0xff, 0x01 },
132 [OPAL_ENTERPRISE_BANDMASTER0_UID] =
133 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x80, 0x01 },
134 [OPAL_ENTERPRISE_ERASEMASTER_UID] =
135 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x84, 0x01 },
139 { 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01 },
140 [OPAL_LOCKINGRANGE_GLOBAL] =
141 { 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x01 },
142 [OPAL_LOCKINGRANGE_ACE_START_TO_KEY] =
143 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xD0, 0x01 },
144 [OPAL_LOCKINGRANGE_ACE_RDLOCKED] =
145 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE0, 0x01 },
146 [OPAL_LOCKINGRANGE_ACE_WRLOCKED] =
147 { 0x00, 0x00, 0x00, 0x08, 0x00, 0x03, 0xE8, 0x01 },
149 { 0x00, 0x00, 0x08, 0x03, 0x00, 0x00, 0x00, 0x01 },
151 { 0x00, 0x00, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00 },
152 [OPAL_AUTHORITY_TABLE] =
153 { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00},
155 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00},
156 [OPAL_LOCKING_INFO_TABLE] =
157 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01 },
158 [OPAL_ENTERPRISE_LOCKING_INFO_TABLE] =
159 { 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00 },
161 { 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00 },
163 /* C_PIN_TABLE object ID's */
165 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x84, 0x02},
167 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x01},
168 [OPAL_C_PIN_ADMIN1] =
169 { 0x00, 0x00, 0x00, 0x0B, 0x00, 0x01, 0x00, 0x01},
171 /* half UID's (only first 4 bytes used) */
172 [OPAL_HALF_UID_AUTHORITY_OBJ_REF] =
173 { 0x00, 0x00, 0x0C, 0x05, 0xff, 0xff, 0xff, 0xff },
174 [OPAL_HALF_UID_BOOLEAN_ACE] =
175 { 0x00, 0x00, 0x04, 0x0E, 0xff, 0xff, 0xff, 0xff },
177 /* special value for omitted optional parameter */
179 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
183 * TCG Storage SSC Methods.
184 * Derived from: TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
185 * Section: 6.3 Assigned UIDs
187 static const u8 opalmethod[][OPAL_METHOD_LENGTH] = {
189 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x01 },
190 [OPAL_STARTSESSION] =
191 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x02 },
193 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x02 },
195 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x02, 0x03 },
197 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x06 },
199 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07 },
201 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x08 },
202 [OPAL_EAUTHENTICATE] =
203 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0c },
205 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x0d },
207 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x10 },
209 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x11 },
211 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x16 },
213 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x17 },
214 [OPAL_AUTHENTICATE] =
215 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x1c },
217 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x01 },
219 { 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x08, 0x03 },
222 static int end_opal_session_error(struct opal_dev *dev);
223 static int opal_discovery0_step(struct opal_dev *dev);
225 struct opal_suspend_data {
226 struct opal_lock_unlock unlk;
228 struct list_head node;
233 * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
234 * Section: 5.1.5 Method Status Codes
236 static const char * const opal_errors[] = {
244 "No Sessions Available",
245 "Uniqueness Conflict",
246 "Insufficient Space",
253 "Transaction Failure",
255 "Authority Locked Out",
258 static const char *opal_error_to_human(int error)
263 if (error >= ARRAY_SIZE(opal_errors) || error < 0)
264 return "Unknown Error";
266 return opal_errors[error];
269 static void print_buffer(const u8 *ptr, u32 length)
272 print_hex_dump_bytes("OPAL: ", DUMP_PREFIX_OFFSET, ptr, length);
278 * Allocate/update a SED Opal key and add it to the SED Opal keyring.
280 static int update_sed_opal_key(const char *desc, u_char *key_data, int keylen)
284 if (!sed_opal_keyring)
287 kr = key_create_or_update(make_key_ref(sed_opal_keyring, true), "user",
288 desc, (const void *)key_data, keylen,
289 KEY_USR_VIEW | KEY_USR_SEARCH | KEY_USR_WRITE,
290 KEY_ALLOC_NOT_IN_QUOTA | KEY_ALLOC_BUILT_IN |
291 KEY_ALLOC_BYPASS_RESTRICTION);
293 pr_err("Error adding SED key (%ld)\n", PTR_ERR(kr));
301 * Read a SED Opal key from the SED Opal keyring.
303 static int read_sed_opal_key(const char *key_name, u_char *buffer, int buflen)
309 if (!sed_opal_keyring)
312 kref = keyring_search(make_key_ref(sed_opal_keyring, true),
313 &key_type_user, key_name, true);
318 key = key_ref_to_ptr(kref);
319 down_read(&key->sem);
320 ret = key_validate(key);
322 if (buflen > key->datalen)
323 buflen = key->datalen;
325 ret = key->type->read(key, (char *)buffer, buflen);
334 static int opal_get_key(struct opal_dev *dev, struct opal_key *key)
338 switch (key->key_type) {
340 /* the key is ready to use */
343 /* the key is in the keyring */
344 ret = read_sed_opal_key(OPAL_AUTH_KEY, key->key, OPAL_KEY_MAX);
351 key->key_type = OPAL_INCLUDED;
361 /* must have a PEK by now or it's an error */
362 if (key->key_type != OPAL_INCLUDED || key->key_len == 0) {
368 pr_debug("Error getting password: %d\n", ret);
372 static bool check_tper(const void *data)
374 const struct d0_tper_features *tper = data;
375 u8 flags = tper->supported_features;
377 if (!(flags & TPER_SYNC_SUPPORTED)) {
378 pr_debug("TPer sync not supported. flags = %d\n",
379 tper->supported_features);
386 static bool check_lcksuppt(const void *data)
388 const struct d0_locking_features *lfeat = data;
389 u8 sup_feat = lfeat->supported_features;
391 return !!(sup_feat & LOCKING_SUPPORTED_MASK);
394 static bool check_lckenabled(const void *data)
396 const struct d0_locking_features *lfeat = data;
397 u8 sup_feat = lfeat->supported_features;
399 return !!(sup_feat & LOCKING_ENABLED_MASK);
402 static bool check_locked(const void *data)
404 const struct d0_locking_features *lfeat = data;
405 u8 sup_feat = lfeat->supported_features;
407 return !!(sup_feat & LOCKED_MASK);
410 static bool check_mbrenabled(const void *data)
412 const struct d0_locking_features *lfeat = data;
413 u8 sup_feat = lfeat->supported_features;
415 return !!(sup_feat & MBR_ENABLED_MASK);
418 static bool check_mbrdone(const void *data)
420 const struct d0_locking_features *lfeat = data;
421 u8 sup_feat = lfeat->supported_features;
423 return !!(sup_feat & MBR_DONE_MASK);
426 static bool check_sum(const void *data)
428 const struct d0_single_user_mode *sum = data;
429 u32 nlo = be32_to_cpu(sum->num_locking_objects);
432 pr_debug("Need at least one locking object.\n");
436 pr_debug("Number of locking objects: %d\n", nlo);
441 static u16 get_comid_v100(const void *data)
443 const struct d0_opal_v100 *v100 = data;
445 return be16_to_cpu(v100->baseComID);
448 static u16 get_comid_v200(const void *data)
450 const struct d0_opal_v200 *v200 = data;
452 return be16_to_cpu(v200->baseComID);
455 static int opal_send_cmd(struct opal_dev *dev)
457 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
458 dev->cmd, IO_BUFFER_LENGTH,
462 static int opal_recv_cmd(struct opal_dev *dev)
464 return dev->send_recv(dev->data, dev->comid, TCG_SECP_01,
465 dev->resp, IO_BUFFER_LENGTH,
469 static int opal_recv_check(struct opal_dev *dev)
471 size_t buflen = IO_BUFFER_LENGTH;
472 void *buffer = dev->resp;
473 struct opal_header *hdr = buffer;
477 pr_debug("Sent OPAL command: outstanding=%d, minTransfer=%d\n",
478 hdr->cp.outstandingData,
479 hdr->cp.minTransfer);
481 if (hdr->cp.outstandingData == 0 ||
482 hdr->cp.minTransfer != 0)
485 memset(buffer, 0, buflen);
486 ret = opal_recv_cmd(dev);
492 static int opal_send_recv(struct opal_dev *dev, cont_fn *cont)
496 ret = opal_send_cmd(dev);
499 ret = opal_recv_cmd(dev);
502 ret = opal_recv_check(dev);
508 static void check_geometry(struct opal_dev *dev, const void *data)
510 const struct d0_geometry_features *geo = data;
512 dev->align = be64_to_cpu(geo->alignment_granularity);
513 dev->lowest_lba = be64_to_cpu(geo->lowest_aligned_lba);
514 dev->logical_block_size = be32_to_cpu(geo->logical_block_size);
515 dev->align_required = geo->reserved01 & 1;
518 static int execute_step(struct opal_dev *dev,
519 const struct opal_step *step, size_t stepIndex)
521 int error = step->fn(dev, step->data);
524 pr_debug("Step %zu (%pS) failed with error %d: %s\n",
525 stepIndex, step->fn, error,
526 opal_error_to_human(error));
532 static int execute_steps(struct opal_dev *dev,
533 const struct opal_step *steps, size_t n_steps)
538 /* first do a discovery0 */
539 error = opal_discovery0_step(dev);
543 for (state = 0; state < n_steps; state++) {
544 error = execute_step(dev, &steps[state], state);
553 * For each OPAL command the first step in steps starts some sort of
554 * session. If an error occurred in the initial discovery0 or if an
555 * error occurred in the first step (and thus stopping the loop with
556 * state == 0) then there was an error before or during the attempt to
557 * start a session. Therefore we shouldn't attempt to terminate a
558 * session, as one has not yet been created.
561 end_opal_session_error(dev);
566 static int opal_discovery0_end(struct opal_dev *dev, void *data)
568 struct opal_discovery *discv_out = data; /* may be NULL */
571 bool found_com_id = false, supported = true, single_user = false;
572 const struct d0_header *hdr = (struct d0_header *)dev->resp;
573 const u8 *epos = dev->resp, *cpos = dev->resp;
575 u32 hlen = be32_to_cpu(hdr->length);
577 print_buffer(dev->resp, hlen);
578 dev->flags &= OPAL_FL_SUPPORTED;
580 if (hlen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
581 pr_debug("Discovery length overflows buffer (%zu+%u)/%u\n",
582 sizeof(*hdr), hlen, IO_BUFFER_LENGTH);
587 buf_out = (u8 __user *)(uintptr_t)discv_out->data;
588 len_out = min_t(u64, discv_out->size, hlen);
589 if (buf_out && copy_to_user(buf_out, dev->resp, len_out))
592 discv_out->size = hlen; /* actual size of data */
595 epos += hlen; /* end of buffer */
596 cpos += sizeof(*hdr); /* current position on buffer */
598 while (cpos < epos && supported) {
599 const struct d0_features *body =
600 (const struct d0_features *)cpos;
602 switch (be16_to_cpu(body->code)) {
604 supported = check_tper(body->features);
607 single_user = check_sum(body->features);
609 dev->flags |= OPAL_FL_SUM_SUPPORTED;
612 check_geometry(dev, body);
615 if (check_lcksuppt(body->features))
616 dev->flags |= OPAL_FL_LOCKING_SUPPORTED;
617 if (check_lckenabled(body->features))
618 dev->flags |= OPAL_FL_LOCKING_ENABLED;
619 if (check_locked(body->features))
620 dev->flags |= OPAL_FL_LOCKED;
621 if (check_mbrenabled(body->features))
622 dev->flags |= OPAL_FL_MBR_ENABLED;
623 if (check_mbrdone(body->features))
624 dev->flags |= OPAL_FL_MBR_DONE;
628 /* some ignored properties */
629 pr_debug("Found OPAL feature description: %d\n",
630 be16_to_cpu(body->code));
633 comid = get_comid_v100(body->features);
637 comid = get_comid_v200(body->features);
640 case 0xbfff ... 0xffff:
641 /* vendor specific, just ignore */
644 pr_debug("OPAL Unknown feature: %d\n",
645 be16_to_cpu(body->code));
648 cpos += body->length + 4;
652 pr_debug("This device is not Opal enabled. Not Supported!\n");
657 pr_debug("Device doesn't support single user mode\n");
661 pr_debug("Could not find OPAL comid for device. Returning early\n");
670 static int opal_discovery0(struct opal_dev *dev, void *data)
674 memset(dev->resp, 0, IO_BUFFER_LENGTH);
675 dev->comid = OPAL_DISCOVERY_COMID;
676 ret = opal_recv_cmd(dev);
680 return opal_discovery0_end(dev, data);
683 static int opal_discovery0_step(struct opal_dev *dev)
685 const struct opal_step discovery0_step = {
686 opal_discovery0, NULL
689 return execute_step(dev, &discovery0_step, 0);
692 static size_t remaining_size(struct opal_dev *cmd)
694 return IO_BUFFER_LENGTH - cmd->pos;
697 static bool can_add(int *err, struct opal_dev *cmd, size_t len)
702 if (remaining_size(cmd) < len) {
703 pr_debug("Error adding %zu bytes: end of buffer.\n", len);
711 static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok)
713 if (!can_add(err, cmd, 1))
716 cmd->cmd[cmd->pos++] = tok;
719 static void add_short_atom_header(struct opal_dev *cmd, bool bytestring,
720 bool has_sign, int len)
725 atom = SHORT_ATOM_ID;
726 atom |= bytestring ? SHORT_ATOM_BYTESTRING : 0;
727 atom |= has_sign ? SHORT_ATOM_SIGNED : 0;
728 atom |= len & SHORT_ATOM_LEN_MASK;
730 add_token_u8(&err, cmd, atom);
733 static void add_medium_atom_header(struct opal_dev *cmd, bool bytestring,
734 bool has_sign, int len)
738 header0 = MEDIUM_ATOM_ID;
739 header0 |= bytestring ? MEDIUM_ATOM_BYTESTRING : 0;
740 header0 |= has_sign ? MEDIUM_ATOM_SIGNED : 0;
741 header0 |= (len >> 8) & MEDIUM_ATOM_LEN_MASK;
743 cmd->cmd[cmd->pos++] = header0;
744 cmd->cmd[cmd->pos++] = len;
747 static void add_token_u64(int *err, struct opal_dev *cmd, u64 number)
752 if (!(number & ~TINY_ATOM_DATA_MASK)) {
753 add_token_u8(err, cmd, number);
758 len = DIV_ROUND_UP(msb, 8);
760 if (!can_add(err, cmd, len + 1)) {
761 pr_debug("Error adding u64: end of buffer.\n");
764 add_short_atom_header(cmd, false, false, len);
766 add_token_u8(err, cmd, number >> (len * 8));
769 static u8 *add_bytestring_header(int *err, struct opal_dev *cmd, size_t len)
771 size_t header_len = 1;
772 bool is_short_atom = true;
774 if (len & ~SHORT_ATOM_LEN_MASK) {
776 is_short_atom = false;
779 if (!can_add(err, cmd, header_len + len)) {
780 pr_debug("Error adding bytestring: end of buffer.\n");
785 add_short_atom_header(cmd, true, false, len);
787 add_medium_atom_header(cmd, true, false, len);
789 return &cmd->cmd[cmd->pos];
792 static void add_token_bytestring(int *err, struct opal_dev *cmd,
793 const u8 *bytestring, size_t len)
797 start = add_bytestring_header(err, cmd, len);
800 memcpy(start, bytestring, len);
804 static int build_locking_range(u8 *buffer, size_t length, u8 lr)
806 if (length > OPAL_UID_LENGTH) {
807 pr_debug("Can't build locking range. Length OOB\n");
811 memcpy(buffer, opaluid[OPAL_LOCKINGRANGE_GLOBAL], OPAL_UID_LENGTH);
816 buffer[5] = LOCKING_RANGE_NON_GLOBAL;
822 static int build_locking_user(u8 *buffer, size_t length, u8 lr)
824 if (length > OPAL_UID_LENGTH) {
825 pr_debug("Can't build locking range user. Length OOB\n");
829 memcpy(buffer, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
836 static void set_comid(struct opal_dev *cmd, u16 comid)
838 struct opal_header *hdr = (struct opal_header *)cmd->cmd;
840 hdr->cp.extendedComID[0] = comid >> 8;
841 hdr->cp.extendedComID[1] = comid;
842 hdr->cp.extendedComID[2] = 0;
843 hdr->cp.extendedComID[3] = 0;
846 static int cmd_finalize(struct opal_dev *cmd, u32 hsn, u32 tsn)
848 struct opal_header *hdr;
852 * Close the parameter list opened from cmd_start.
853 * The number of bytes added must be equal to
854 * CMD_FINALIZE_BYTES_NEEDED.
856 add_token_u8(&err, cmd, OPAL_ENDLIST);
858 add_token_u8(&err, cmd, OPAL_ENDOFDATA);
859 add_token_u8(&err, cmd, OPAL_STARTLIST);
860 add_token_u8(&err, cmd, 0);
861 add_token_u8(&err, cmd, 0);
862 add_token_u8(&err, cmd, 0);
863 add_token_u8(&err, cmd, OPAL_ENDLIST);
866 pr_debug("Error finalizing command.\n");
870 hdr = (struct opal_header *) cmd->cmd;
872 hdr->pkt.tsn = cpu_to_be32(tsn);
873 hdr->pkt.hsn = cpu_to_be32(hsn);
875 hdr->subpkt.length = cpu_to_be32(cmd->pos - sizeof(*hdr));
876 while (cmd->pos % 4) {
877 if (cmd->pos >= IO_BUFFER_LENGTH) {
878 pr_debug("Error: Buffer overrun\n");
881 cmd->cmd[cmd->pos++] = 0;
883 hdr->pkt.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp) -
885 hdr->cp.length = cpu_to_be32(cmd->pos - sizeof(hdr->cp));
890 static const struct opal_resp_tok *response_get_token(
891 const struct parsed_resp *resp,
894 const struct opal_resp_tok *tok;
897 pr_debug("Response is NULL\n");
898 return ERR_PTR(-EINVAL);
901 if (n >= resp->num) {
902 pr_debug("Token number doesn't exist: %d, resp: %d\n",
904 return ERR_PTR(-EINVAL);
907 tok = &resp->toks[n];
909 pr_debug("Token length must be non-zero\n");
910 return ERR_PTR(-EINVAL);
916 static ssize_t response_parse_tiny(struct opal_resp_tok *tok,
921 tok->width = OPAL_WIDTH_TINY;
923 if (pos[0] & TINY_ATOM_SIGNED) {
924 tok->type = OPAL_DTA_TOKENID_SINT;
926 tok->type = OPAL_DTA_TOKENID_UINT;
927 tok->stored.u = pos[0] & 0x3f;
933 static ssize_t response_parse_short(struct opal_resp_tok *tok,
937 tok->len = (pos[0] & SHORT_ATOM_LEN_MASK) + 1;
938 tok->width = OPAL_WIDTH_SHORT;
940 if (pos[0] & SHORT_ATOM_BYTESTRING) {
941 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
942 } else if (pos[0] & SHORT_ATOM_SIGNED) {
943 tok->type = OPAL_DTA_TOKENID_SINT;
948 tok->type = OPAL_DTA_TOKENID_UINT;
950 pr_debug("uint64 with more than 8 bytes\n");
953 for (i = tok->len - 1; i > 0; i--) {
954 u_integer |= ((u64)pos[i] << (8 * b));
957 tok->stored.u = u_integer;
963 static ssize_t response_parse_medium(struct opal_resp_tok *tok,
967 tok->len = (((pos[0] & MEDIUM_ATOM_LEN_MASK) << 8) | pos[1]) + 2;
968 tok->width = OPAL_WIDTH_MEDIUM;
970 if (pos[0] & MEDIUM_ATOM_BYTESTRING)
971 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
972 else if (pos[0] & MEDIUM_ATOM_SIGNED)
973 tok->type = OPAL_DTA_TOKENID_SINT;
975 tok->type = OPAL_DTA_TOKENID_UINT;
980 static ssize_t response_parse_long(struct opal_resp_tok *tok,
984 tok->len = ((pos[1] << 16) | (pos[2] << 8) | pos[3]) + 4;
985 tok->width = OPAL_WIDTH_LONG;
987 if (pos[0] & LONG_ATOM_BYTESTRING)
988 tok->type = OPAL_DTA_TOKENID_BYTESTRING;
989 else if (pos[0] & LONG_ATOM_SIGNED)
990 tok->type = OPAL_DTA_TOKENID_SINT;
992 tok->type = OPAL_DTA_TOKENID_UINT;
997 static ssize_t response_parse_token(struct opal_resp_tok *tok,
1002 tok->type = OPAL_DTA_TOKENID_TOKEN;
1003 tok->width = OPAL_WIDTH_TOKEN;
1008 static int response_parse(const u8 *buf, size_t length,
1009 struct parsed_resp *resp)
1011 const struct opal_header *hdr;
1012 struct opal_resp_tok *iter;
1013 int num_entries = 0;
1015 ssize_t token_length;
1017 u32 clen, plen, slen;
1025 hdr = (struct opal_header *)buf;
1027 pos += sizeof(*hdr);
1029 clen = be32_to_cpu(hdr->cp.length);
1030 plen = be32_to_cpu(hdr->pkt.length);
1031 slen = be32_to_cpu(hdr->subpkt.length);
1032 pr_debug("Response size: cp: %u, pkt: %u, subpkt: %u\n",
1035 if (clen == 0 || plen == 0 || slen == 0 ||
1036 slen > IO_BUFFER_LENGTH - sizeof(*hdr)) {
1037 pr_debug("Bad header length. cp: %u, pkt: %u, subpkt: %u\n",
1039 print_buffer(pos, sizeof(*hdr));
1043 if (pos > buf + length)
1048 print_buffer(pos, total);
1050 if (pos[0] <= TINY_ATOM_BYTE) /* tiny atom */
1051 token_length = response_parse_tiny(iter, pos);
1052 else if (pos[0] <= SHORT_ATOM_BYTE) /* short atom */
1053 token_length = response_parse_short(iter, pos);
1054 else if (pos[0] <= MEDIUM_ATOM_BYTE) /* medium atom */
1055 token_length = response_parse_medium(iter, pos);
1056 else if (pos[0] <= LONG_ATOM_BYTE) /* long atom */
1057 token_length = response_parse_long(iter, pos);
1059 token_length = response_parse_token(iter, pos);
1061 if (token_length < 0)
1062 return token_length;
1064 pos += token_length;
1065 total -= token_length;
1070 resp->num = num_entries;
1075 static size_t response_get_string(const struct parsed_resp *resp, int n,
1079 const struct opal_resp_tok *tok;
1082 tok = response_get_token(resp, n);
1086 if (tok->type != OPAL_DTA_TOKENID_BYTESTRING) {
1087 pr_debug("Token is not a byte string!\n");
1091 switch (tok->width) {
1092 case OPAL_WIDTH_TINY:
1093 case OPAL_WIDTH_SHORT:
1096 case OPAL_WIDTH_MEDIUM:
1099 case OPAL_WIDTH_LONG:
1103 pr_debug("Token has invalid width!\n");
1107 *store = tok->pos + skip;
1109 return tok->len - skip;
1112 static u64 response_get_u64(const struct parsed_resp *resp, int n)
1114 const struct opal_resp_tok *tok;
1116 tok = response_get_token(resp, n);
1120 if (tok->type != OPAL_DTA_TOKENID_UINT) {
1121 pr_debug("Token is not unsigned int: %d\n", tok->type);
1125 if (tok->width != OPAL_WIDTH_TINY && tok->width != OPAL_WIDTH_SHORT) {
1126 pr_debug("Atom is not short or tiny: %d\n", tok->width);
1130 return tok->stored.u;
1133 static bool response_token_matches(const struct opal_resp_tok *token, u8 match)
1135 if (IS_ERR(token) ||
1136 token->type != OPAL_DTA_TOKENID_TOKEN ||
1137 token->pos[0] != match)
1142 static u8 response_status(const struct parsed_resp *resp)
1144 const struct opal_resp_tok *tok;
1146 tok = response_get_token(resp, 0);
1147 if (response_token_matches(tok, OPAL_ENDOFSESSION))
1151 return DTAERROR_NO_METHOD_STATUS;
1153 tok = response_get_token(resp, resp->num - 5);
1154 if (!response_token_matches(tok, OPAL_STARTLIST))
1155 return DTAERROR_NO_METHOD_STATUS;
1157 tok = response_get_token(resp, resp->num - 1);
1158 if (!response_token_matches(tok, OPAL_ENDLIST))
1159 return DTAERROR_NO_METHOD_STATUS;
1161 return response_get_u64(resp, resp->num - 4);
1164 /* Parses and checks for errors */
1165 static int parse_and_check_status(struct opal_dev *dev)
1169 print_buffer(dev->cmd, dev->pos);
1171 error = response_parse(dev->resp, IO_BUFFER_LENGTH, &dev->parsed);
1173 pr_debug("Couldn't parse response.\n");
1177 return response_status(&dev->parsed);
1180 static void clear_opal_cmd(struct opal_dev *dev)
1182 dev->pos = sizeof(struct opal_header);
1183 memset(dev->cmd, 0, IO_BUFFER_LENGTH);
1186 static int cmd_start(struct opal_dev *dev, const u8 *uid, const u8 *method)
1190 clear_opal_cmd(dev);
1191 set_comid(dev, dev->comid);
1193 add_token_u8(&err, dev, OPAL_CALL);
1194 add_token_bytestring(&err, dev, uid, OPAL_UID_LENGTH);
1195 add_token_bytestring(&err, dev, method, OPAL_METHOD_LENGTH);
1198 * Every method call is followed by its parameters enclosed within
1199 * OPAL_STARTLIST and OPAL_ENDLIST tokens. We automatically open the
1200 * parameter list here and close it later in cmd_finalize.
1202 add_token_u8(&err, dev, OPAL_STARTLIST);
1207 static int start_opal_session_cont(struct opal_dev *dev)
1212 error = parse_and_check_status(dev);
1216 hsn = response_get_u64(&dev->parsed, 4);
1217 tsn = response_get_u64(&dev->parsed, 5);
1219 if (hsn != GENERIC_HOST_SESSION_NUM || tsn < FIRST_TPER_SESSION_NUM) {
1220 pr_debug("Couldn't authenticate session\n");
1230 static void add_suspend_info(struct opal_dev *dev,
1231 struct opal_suspend_data *sus)
1233 struct opal_suspend_data *iter;
1235 list_for_each_entry(iter, &dev->unlk_lst, node) {
1236 if (iter->lr == sus->lr) {
1237 list_del(&iter->node);
1242 list_add_tail(&sus->node, &dev->unlk_lst);
1245 static int end_session_cont(struct opal_dev *dev)
1250 return parse_and_check_status(dev);
1253 static int finalize_and_send(struct opal_dev *dev, cont_fn cont)
1257 ret = cmd_finalize(dev, dev->hsn, dev->tsn);
1259 pr_debug("Error finalizing command buffer: %d\n", ret);
1263 print_buffer(dev->cmd, dev->pos);
1265 return opal_send_recv(dev, cont);
1268 static int generic_get_columns(struct opal_dev *dev, const u8 *table,
1269 u64 start_column, u64 end_column)
1273 err = cmd_start(dev, table, opalmethod[OPAL_GET]);
1275 add_token_u8(&err, dev, OPAL_STARTLIST);
1277 add_token_u8(&err, dev, OPAL_STARTNAME);
1278 add_token_u8(&err, dev, OPAL_STARTCOLUMN);
1279 add_token_u64(&err, dev, start_column);
1280 add_token_u8(&err, dev, OPAL_ENDNAME);
1282 add_token_u8(&err, dev, OPAL_STARTNAME);
1283 add_token_u8(&err, dev, OPAL_ENDCOLUMN);
1284 add_token_u64(&err, dev, end_column);
1285 add_token_u8(&err, dev, OPAL_ENDNAME);
1287 add_token_u8(&err, dev, OPAL_ENDLIST);
1292 return finalize_and_send(dev, parse_and_check_status);
1296 * request @column from table @table on device @dev. On success, the column
1297 * data will be available in dev->resp->tok[4]
1299 static int generic_get_column(struct opal_dev *dev, const u8 *table,
1302 return generic_get_columns(dev, table, column, column);
1306 * see TCG SAS 5.3.2.3 for a description of the available columns
1308 * the result is provided in dev->resp->tok[4]
1310 static int generic_get_table_info(struct opal_dev *dev, const u8 *table_uid,
1313 u8 uid[OPAL_UID_LENGTH];
1314 const unsigned int half = OPAL_UID_LENGTH_HALF;
1316 /* sed-opal UIDs can be split in two halves:
1317 * first: actual table index
1318 * second: relative index in the table
1319 * so we have to get the first half of the OPAL_TABLE_TABLE and use the
1320 * first part of the target table as relative index into that table
1322 memcpy(uid, opaluid[OPAL_TABLE_TABLE], half);
1323 memcpy(uid + half, table_uid, half);
1325 return generic_get_column(dev, uid, column);
1328 static int gen_key(struct opal_dev *dev, void *data)
1330 u8 uid[OPAL_UID_LENGTH];
1333 memcpy(uid, dev->prev_data, min(sizeof(uid), dev->prev_d_len));
1334 kfree(dev->prev_data);
1335 dev->prev_data = NULL;
1337 err = cmd_start(dev, uid, opalmethod[OPAL_GENKEY]);
1340 pr_debug("Error building gen key command\n");
1345 return finalize_and_send(dev, parse_and_check_status);
1348 static int get_active_key_cont(struct opal_dev *dev)
1350 const char *activekey;
1354 error = parse_and_check_status(dev);
1358 keylen = response_get_string(&dev->parsed, 4, &activekey);
1360 pr_debug("%s: Couldn't extract the Activekey from the response\n",
1362 return OPAL_INVAL_PARAM;
1365 dev->prev_data = kmemdup(activekey, keylen, GFP_KERNEL);
1367 if (!dev->prev_data)
1370 dev->prev_d_len = keylen;
1375 static int get_active_key(struct opal_dev *dev, void *data)
1377 u8 uid[OPAL_UID_LENGTH];
1381 err = build_locking_range(uid, sizeof(uid), *lr);
1385 err = generic_get_column(dev, uid, OPAL_ACTIVEKEY);
1389 return get_active_key_cont(dev);
1392 static int generic_table_write_data(struct opal_dev *dev, const u64 data,
1393 u64 offset, u64 size, const u8 *uid)
1395 const u8 __user *src = (u8 __user *)(uintptr_t)data;
1401 /* do we fit in the available space? */
1402 err = generic_get_table_info(dev, uid, OPAL_TABLE_ROWS);
1404 pr_debug("Couldn't get the table size\n");
1408 len = response_get_u64(&dev->parsed, 4);
1409 if (size > len || offset > len - size) {
1410 pr_debug("Does not fit in the table (%llu vs. %llu)\n",
1411 offset + size, len);
1415 /* do the actual transmission(s) */
1416 while (off < size) {
1417 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1418 add_token_u8(&err, dev, OPAL_STARTNAME);
1419 add_token_u8(&err, dev, OPAL_WHERE);
1420 add_token_u64(&err, dev, offset + off);
1421 add_token_u8(&err, dev, OPAL_ENDNAME);
1423 add_token_u8(&err, dev, OPAL_STARTNAME);
1424 add_token_u8(&err, dev, OPAL_VALUES);
1427 * The bytestring header is either 1 or 2 bytes, so assume 2.
1428 * There also needs to be enough space to accommodate the
1429 * trailing OPAL_ENDNAME (1 byte) and tokens added by
1432 len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
1433 (size_t)(size - off));
1434 pr_debug("Write bytes %zu+%llu/%llu\n", off, len, size);
1436 dst = add_bytestring_header(&err, dev, len);
1440 if (copy_from_user(dst, src + off, len)) {
1447 add_token_u8(&err, dev, OPAL_ENDNAME);
1451 err = finalize_and_send(dev, parse_and_check_status);
1461 static int generic_lr_enable_disable(struct opal_dev *dev,
1462 u8 *uid, bool rle, bool wle,
1467 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1469 add_token_u8(&err, dev, OPAL_STARTNAME);
1470 add_token_u8(&err, dev, OPAL_VALUES);
1471 add_token_u8(&err, dev, OPAL_STARTLIST);
1473 add_token_u8(&err, dev, OPAL_STARTNAME);
1474 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1475 add_token_u8(&err, dev, rle);
1476 add_token_u8(&err, dev, OPAL_ENDNAME);
1478 add_token_u8(&err, dev, OPAL_STARTNAME);
1479 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1480 add_token_u8(&err, dev, wle);
1481 add_token_u8(&err, dev, OPAL_ENDNAME);
1483 add_token_u8(&err, dev, OPAL_STARTNAME);
1484 add_token_u8(&err, dev, OPAL_READLOCKED);
1485 add_token_u8(&err, dev, rl);
1486 add_token_u8(&err, dev, OPAL_ENDNAME);
1488 add_token_u8(&err, dev, OPAL_STARTNAME);
1489 add_token_u8(&err, dev, OPAL_WRITELOCKED);
1490 add_token_u8(&err, dev, wl);
1491 add_token_u8(&err, dev, OPAL_ENDNAME);
1493 add_token_u8(&err, dev, OPAL_ENDLIST);
1494 add_token_u8(&err, dev, OPAL_ENDNAME);
1499 static inline int enable_global_lr(struct opal_dev *dev, u8 *uid,
1500 struct opal_user_lr_setup *setup)
1504 err = generic_lr_enable_disable(dev, uid, !!setup->RLE, !!setup->WLE,
1507 pr_debug("Failed to create enable global lr command\n");
1512 static int setup_locking_range(struct opal_dev *dev, void *data)
1514 u8 uid[OPAL_UID_LENGTH];
1515 struct opal_user_lr_setup *setup = data;
1519 lr = setup->session.opal_key.lr;
1520 err = build_locking_range(uid, sizeof(uid), lr);
1525 err = enable_global_lr(dev, uid, setup);
1527 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1529 add_token_u8(&err, dev, OPAL_STARTNAME);
1530 add_token_u8(&err, dev, OPAL_VALUES);
1531 add_token_u8(&err, dev, OPAL_STARTLIST);
1533 add_token_u8(&err, dev, OPAL_STARTNAME);
1534 add_token_u8(&err, dev, OPAL_RANGESTART);
1535 add_token_u64(&err, dev, setup->range_start);
1536 add_token_u8(&err, dev, OPAL_ENDNAME);
1538 add_token_u8(&err, dev, OPAL_STARTNAME);
1539 add_token_u8(&err, dev, OPAL_RANGELENGTH);
1540 add_token_u64(&err, dev, setup->range_length);
1541 add_token_u8(&err, dev, OPAL_ENDNAME);
1543 add_token_u8(&err, dev, OPAL_STARTNAME);
1544 add_token_u8(&err, dev, OPAL_READLOCKENABLED);
1545 add_token_u64(&err, dev, !!setup->RLE);
1546 add_token_u8(&err, dev, OPAL_ENDNAME);
1548 add_token_u8(&err, dev, OPAL_STARTNAME);
1549 add_token_u8(&err, dev, OPAL_WRITELOCKENABLED);
1550 add_token_u64(&err, dev, !!setup->WLE);
1551 add_token_u8(&err, dev, OPAL_ENDNAME);
1553 add_token_u8(&err, dev, OPAL_ENDLIST);
1554 add_token_u8(&err, dev, OPAL_ENDNAME);
1557 pr_debug("Error building Setup Locking range command.\n");
1561 return finalize_and_send(dev, parse_and_check_status);
1564 static int response_get_column(const struct parsed_resp *resp,
1569 const struct opal_resp_tok *tok;
1573 tok = response_get_token(resp, n);
1575 return PTR_ERR(tok);
1577 if (!response_token_matches(tok, OPAL_STARTNAME)) {
1578 pr_debug("Unexpected response token type %d.\n", n);
1579 return OPAL_INVAL_PARAM;
1583 if (response_get_u64(resp, n) != column) {
1584 pr_debug("Token %d does not match expected column %u.\n",
1586 return OPAL_INVAL_PARAM;
1590 val = response_get_u64(resp, n);
1593 tok = response_get_token(resp, n);
1595 return PTR_ERR(tok);
1597 if (!response_token_matches(tok, OPAL_ENDNAME)) {
1598 pr_debug("Unexpected response token type %d.\n", n);
1599 return OPAL_INVAL_PARAM;
1609 static int locking_range_status(struct opal_dev *dev, void *data)
1611 u8 lr_buffer[OPAL_UID_LENGTH];
1613 bool rlocked, wlocked;
1615 struct opal_lr_status *lrst = data;
1617 err = build_locking_range(lr_buffer, sizeof(lr_buffer),
1618 lrst->session.opal_key.lr);
1622 err = generic_get_columns(dev, lr_buffer, OPAL_RANGESTART,
1625 pr_debug("Couldn't get lr %u table columns %d to %d.\n",
1626 lrst->session.opal_key.lr, OPAL_RANGESTART,
1632 err = response_get_column(&dev->parsed, &tok_n, OPAL_RANGESTART,
1633 &lrst->range_start);
1638 err = response_get_column(&dev->parsed, &tok_n, OPAL_RANGELENGTH,
1639 &lrst->range_length);
1644 err = response_get_column(&dev->parsed, &tok_n, OPAL_READLOCKENABLED,
1652 err = response_get_column(&dev->parsed, &tok_n, OPAL_WRITELOCKENABLED,
1660 err = response_get_column(&dev->parsed, &tok_n, OPAL_READLOCKED, &resp);
1667 err = response_get_column(&dev->parsed, &tok_n, OPAL_WRITELOCKED, &resp);
1673 /* opal_lock_state can not map 'read locked' only state. */
1674 lrst->l_state = OPAL_RW;
1675 if (rlocked && wlocked)
1676 lrst->l_state = OPAL_LK;
1678 lrst->l_state = OPAL_RO;
1680 pr_debug("Can not report read locked only state.\n");
1687 static int start_generic_opal_session(struct opal_dev *dev,
1689 enum opal_uid sp_type,
1696 if (key == NULL && auth != OPAL_ANYBODY_UID)
1697 return OPAL_INVAL_PARAM;
1699 hsn = GENERIC_HOST_SESSION_NUM;
1700 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1701 opalmethod[OPAL_STARTSESSION]);
1703 add_token_u64(&err, dev, hsn);
1704 add_token_bytestring(&err, dev, opaluid[sp_type], OPAL_UID_LENGTH);
1705 add_token_u8(&err, dev, 1);
1708 case OPAL_ANYBODY_UID:
1710 case OPAL_ADMIN1_UID:
1713 add_token_u8(&err, dev, OPAL_STARTNAME);
1714 add_token_u8(&err, dev, 0); /* HostChallenge */
1715 add_token_bytestring(&err, dev, key, key_len);
1716 add_token_u8(&err, dev, OPAL_ENDNAME);
1717 add_token_u8(&err, dev, OPAL_STARTNAME);
1718 add_token_u8(&err, dev, 3); /* HostSignAuth */
1719 add_token_bytestring(&err, dev, opaluid[auth],
1721 add_token_u8(&err, dev, OPAL_ENDNAME);
1724 pr_debug("Cannot start Admin SP session with auth %d\n", auth);
1725 return OPAL_INVAL_PARAM;
1729 pr_debug("Error building start adminsp session command.\n");
1733 return finalize_and_send(dev, start_opal_session_cont);
1736 static int start_anybodyASP_opal_session(struct opal_dev *dev, void *data)
1738 return start_generic_opal_session(dev, OPAL_ANYBODY_UID,
1739 OPAL_ADMINSP_UID, NULL, 0);
1742 static int start_SIDASP_opal_session(struct opal_dev *dev, void *data)
1745 const u8 *key = dev->prev_data;
1748 const struct opal_key *okey = data;
1750 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1755 ret = start_generic_opal_session(dev, OPAL_SID_UID,
1757 key, dev->prev_d_len);
1759 dev->prev_data = NULL;
1765 static int start_admin1LSP_opal_session(struct opal_dev *dev, void *data)
1767 struct opal_key *key = data;
1769 return start_generic_opal_session(dev, OPAL_ADMIN1_UID,
1771 key->key, key->key_len);
1774 static int start_PSID_opal_session(struct opal_dev *dev, void *data)
1776 const struct opal_key *okey = data;
1778 return start_generic_opal_session(dev, OPAL_PSID_UID,
1784 static int start_auth_opal_session(struct opal_dev *dev, void *data)
1786 struct opal_session_info *session = data;
1787 u8 lk_ul_user[OPAL_UID_LENGTH];
1788 size_t keylen = session->opal_key.key_len;
1791 u8 *key = session->opal_key.key;
1792 u32 hsn = GENERIC_HOST_SESSION_NUM;
1795 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1796 session->opal_key.lr);
1797 else if (session->who != OPAL_ADMIN1 && !session->sum)
1798 err = build_locking_user(lk_ul_user, sizeof(lk_ul_user),
1801 memcpy(lk_ul_user, opaluid[OPAL_ADMIN1_UID], OPAL_UID_LENGTH);
1806 err = cmd_start(dev, opaluid[OPAL_SMUID_UID],
1807 opalmethod[OPAL_STARTSESSION]);
1809 add_token_u64(&err, dev, hsn);
1810 add_token_bytestring(&err, dev, opaluid[OPAL_LOCKINGSP_UID],
1812 add_token_u8(&err, dev, 1);
1813 add_token_u8(&err, dev, OPAL_STARTNAME);
1814 add_token_u8(&err, dev, 0);
1815 add_token_bytestring(&err, dev, key, keylen);
1816 add_token_u8(&err, dev, OPAL_ENDNAME);
1817 add_token_u8(&err, dev, OPAL_STARTNAME);
1818 add_token_u8(&err, dev, 3);
1819 add_token_bytestring(&err, dev, lk_ul_user, OPAL_UID_LENGTH);
1820 add_token_u8(&err, dev, OPAL_ENDNAME);
1823 pr_debug("Error building STARTSESSION command.\n");
1827 return finalize_and_send(dev, start_opal_session_cont);
1830 static int revert_tper(struct opal_dev *dev, void *data)
1834 err = cmd_start(dev, opaluid[OPAL_ADMINSP_UID],
1835 opalmethod[OPAL_REVERT]);
1837 pr_debug("Error building REVERT TPER command.\n");
1841 return finalize_and_send(dev, parse_and_check_status);
1844 static int internal_activate_user(struct opal_dev *dev, void *data)
1846 struct opal_session_info *session = data;
1847 u8 uid[OPAL_UID_LENGTH];
1850 memcpy(uid, opaluid[OPAL_USER1_UID], OPAL_UID_LENGTH);
1851 uid[7] = session->who;
1853 err = cmd_start(dev, uid, opalmethod[OPAL_SET]);
1854 add_token_u8(&err, dev, OPAL_STARTNAME);
1855 add_token_u8(&err, dev, OPAL_VALUES);
1856 add_token_u8(&err, dev, OPAL_STARTLIST);
1857 add_token_u8(&err, dev, OPAL_STARTNAME);
1858 add_token_u8(&err, dev, 5); /* Enabled */
1859 add_token_u8(&err, dev, OPAL_TRUE);
1860 add_token_u8(&err, dev, OPAL_ENDNAME);
1861 add_token_u8(&err, dev, OPAL_ENDLIST);
1862 add_token_u8(&err, dev, OPAL_ENDNAME);
1865 pr_debug("Error building Activate UserN command.\n");
1869 return finalize_and_send(dev, parse_and_check_status);
1872 static int revert_lsp(struct opal_dev *dev, void *data)
1874 struct opal_revert_lsp *rev = data;
1877 err = cmd_start(dev, opaluid[OPAL_THISSP_UID],
1878 opalmethod[OPAL_REVERTSP]);
1879 add_token_u8(&err, dev, OPAL_STARTNAME);
1880 add_token_u64(&err, dev, OPAL_KEEP_GLOBAL_RANGE_KEY);
1881 add_token_u8(&err, dev, (rev->options & OPAL_PRESERVE) ?
1882 OPAL_TRUE : OPAL_FALSE);
1883 add_token_u8(&err, dev, OPAL_ENDNAME);
1885 pr_debug("Error building REVERT SP command.\n");
1889 return finalize_and_send(dev, parse_and_check_status);
1892 static int erase_locking_range(struct opal_dev *dev, void *data)
1894 struct opal_session_info *session = data;
1895 u8 uid[OPAL_UID_LENGTH];
1898 if (build_locking_range(uid, sizeof(uid), session->opal_key.lr) < 0)
1901 err = cmd_start(dev, uid, opalmethod[OPAL_ERASE]);
1904 pr_debug("Error building Erase Locking Range Command.\n");
1908 return finalize_and_send(dev, parse_and_check_status);
1911 static int set_mbr_done(struct opal_dev *dev, void *data)
1913 u8 *mbr_done_tf = data;
1916 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1917 opalmethod[OPAL_SET]);
1919 add_token_u8(&err, dev, OPAL_STARTNAME);
1920 add_token_u8(&err, dev, OPAL_VALUES);
1921 add_token_u8(&err, dev, OPAL_STARTLIST);
1922 add_token_u8(&err, dev, OPAL_STARTNAME);
1923 add_token_u8(&err, dev, OPAL_MBRDONE);
1924 add_token_u8(&err, dev, *mbr_done_tf); /* Done T or F */
1925 add_token_u8(&err, dev, OPAL_ENDNAME);
1926 add_token_u8(&err, dev, OPAL_ENDLIST);
1927 add_token_u8(&err, dev, OPAL_ENDNAME);
1930 pr_debug("Error Building set MBR Done command\n");
1934 return finalize_and_send(dev, parse_and_check_status);
1937 static int set_mbr_enable_disable(struct opal_dev *dev, void *data)
1939 u8 *mbr_en_dis = data;
1942 err = cmd_start(dev, opaluid[OPAL_MBRCONTROL],
1943 opalmethod[OPAL_SET]);
1945 add_token_u8(&err, dev, OPAL_STARTNAME);
1946 add_token_u8(&err, dev, OPAL_VALUES);
1947 add_token_u8(&err, dev, OPAL_STARTLIST);
1948 add_token_u8(&err, dev, OPAL_STARTNAME);
1949 add_token_u8(&err, dev, OPAL_MBRENABLE);
1950 add_token_u8(&err, dev, *mbr_en_dis);
1951 add_token_u8(&err, dev, OPAL_ENDNAME);
1952 add_token_u8(&err, dev, OPAL_ENDLIST);
1953 add_token_u8(&err, dev, OPAL_ENDNAME);
1956 pr_debug("Error Building set MBR done command\n");
1960 return finalize_and_send(dev, parse_and_check_status);
1963 static int write_shadow_mbr(struct opal_dev *dev, void *data)
1965 struct opal_shadow_mbr *shadow = data;
1967 return generic_table_write_data(dev, shadow->data, shadow->offset,
1968 shadow->size, opaluid[OPAL_MBR]);
1971 static int generic_pw_cmd(u8 *key, size_t key_len, u8 *cpin_uid,
1972 struct opal_dev *dev)
1976 err = cmd_start(dev, cpin_uid, opalmethod[OPAL_SET]);
1978 add_token_u8(&err, dev, OPAL_STARTNAME);
1979 add_token_u8(&err, dev, OPAL_VALUES);
1980 add_token_u8(&err, dev, OPAL_STARTLIST);
1981 add_token_u8(&err, dev, OPAL_STARTNAME);
1982 add_token_u8(&err, dev, OPAL_PIN);
1983 add_token_bytestring(&err, dev, key, key_len);
1984 add_token_u8(&err, dev, OPAL_ENDNAME);
1985 add_token_u8(&err, dev, OPAL_ENDLIST);
1986 add_token_u8(&err, dev, OPAL_ENDNAME);
1991 static int set_new_pw(struct opal_dev *dev, void *data)
1993 u8 cpin_uid[OPAL_UID_LENGTH];
1994 struct opal_session_info *usr = data;
1996 memcpy(cpin_uid, opaluid[OPAL_C_PIN_ADMIN1], OPAL_UID_LENGTH);
1998 if (usr->who != OPAL_ADMIN1) {
2001 cpin_uid[7] = usr->opal_key.lr + 1;
2003 cpin_uid[7] = usr->who;
2006 if (generic_pw_cmd(usr->opal_key.key, usr->opal_key.key_len,
2008 pr_debug("Error building set password command.\n");
2012 return finalize_and_send(dev, parse_and_check_status);
2015 static int set_sid_cpin_pin(struct opal_dev *dev, void *data)
2017 u8 cpin_uid[OPAL_UID_LENGTH];
2018 struct opal_key *key = data;
2020 memcpy(cpin_uid, opaluid[OPAL_C_PIN_SID], OPAL_UID_LENGTH);
2022 if (generic_pw_cmd(key->key, key->key_len, cpin_uid, dev)) {
2023 pr_debug("Error building Set SID cpin\n");
2026 return finalize_and_send(dev, parse_and_check_status);
2029 static void add_authority_object_ref(int *err,
2030 struct opal_dev *dev,
2034 add_token_u8(err, dev, OPAL_STARTNAME);
2035 add_token_bytestring(err, dev,
2036 opaluid[OPAL_HALF_UID_AUTHORITY_OBJ_REF],
2038 add_token_bytestring(err, dev, uid, uid_len);
2039 add_token_u8(err, dev, OPAL_ENDNAME);
2042 static void add_boolean_object_ref(int *err,
2043 struct opal_dev *dev,
2046 add_token_u8(err, dev, OPAL_STARTNAME);
2047 add_token_bytestring(err, dev, opaluid[OPAL_HALF_UID_BOOLEAN_ACE],
2049 add_token_u8(err, dev, boolean_op);
2050 add_token_u8(err, dev, OPAL_ENDNAME);
2053 static int set_lr_boolean_ace(struct opal_dev *dev,
2054 unsigned int opal_uid,
2059 u8 lr_buffer[OPAL_UID_LENGTH];
2060 u8 user_uid[OPAL_UID_LENGTH];
2064 memcpy(lr_buffer, opaluid[opal_uid], OPAL_UID_LENGTH);
2067 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
2069 add_token_u8(&err, dev, OPAL_STARTNAME);
2070 add_token_u8(&err, dev, OPAL_VALUES);
2072 add_token_u8(&err, dev, OPAL_STARTLIST);
2073 add_token_u8(&err, dev, OPAL_STARTNAME);
2074 add_token_u8(&err, dev, 3);
2076 add_token_u8(&err, dev, OPAL_STARTLIST);
2078 for (u = 0; u < users_len; u++) {
2079 if (users[u] == OPAL_ADMIN1)
2080 memcpy(user_uid, opaluid[OPAL_ADMIN1_UID],
2083 memcpy(user_uid, opaluid[OPAL_USER1_UID],
2085 user_uid[7] = users[u];
2088 add_authority_object_ref(&err, dev, user_uid, sizeof(user_uid));
2091 * Add boolean operator in postfix only with
2092 * two or more authorities being added in ACE
2096 add_boolean_object_ref(&err, dev, OPAL_BOOLEAN_OR);
2099 add_token_u8(&err, dev, OPAL_ENDLIST);
2100 add_token_u8(&err, dev, OPAL_ENDNAME);
2101 add_token_u8(&err, dev, OPAL_ENDLIST);
2102 add_token_u8(&err, dev, OPAL_ENDNAME);
2107 static int add_user_to_lr(struct opal_dev *dev, void *data)
2110 struct opal_lock_unlock *lkul = data;
2111 const u8 users[] = {
2115 err = set_lr_boolean_ace(dev,
2116 lkul->l_state == OPAL_RW ?
2117 OPAL_LOCKINGRANGE_ACE_WRLOCKED :
2118 OPAL_LOCKINGRANGE_ACE_RDLOCKED,
2119 lkul->session.opal_key.lr, users,
2122 pr_debug("Error building add user to locking range command.\n");
2126 return finalize_and_send(dev, parse_and_check_status);
2129 static int add_user_to_lr_ace(struct opal_dev *dev, void *data)
2132 struct opal_lock_unlock *lkul = data;
2133 const u8 users[] = {
2138 err = set_lr_boolean_ace(dev, OPAL_LOCKINGRANGE_ACE_START_TO_KEY,
2139 lkul->session.opal_key.lr, users,
2143 pr_debug("Error building add user to locking ranges ACEs.\n");
2147 return finalize_and_send(dev, parse_and_check_status);
2150 static int lock_unlock_locking_range(struct opal_dev *dev, void *data)
2152 u8 lr_buffer[OPAL_UID_LENGTH];
2153 struct opal_lock_unlock *lkul = data;
2154 u8 read_locked = 1, write_locked = 1;
2157 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
2158 lkul->session.opal_key.lr) < 0)
2161 switch (lkul->l_state) {
2171 /* vars are initialized to locked */
2174 pr_debug("Tried to set an invalid locking state... returning to uland\n");
2175 return OPAL_INVAL_PARAM;
2178 err = cmd_start(dev, lr_buffer, opalmethod[OPAL_SET]);
2180 add_token_u8(&err, dev, OPAL_STARTNAME);
2181 add_token_u8(&err, dev, OPAL_VALUES);
2182 add_token_u8(&err, dev, OPAL_STARTLIST);
2184 add_token_u8(&err, dev, OPAL_STARTNAME);
2185 add_token_u8(&err, dev, OPAL_READLOCKED);
2186 add_token_u8(&err, dev, read_locked);
2187 add_token_u8(&err, dev, OPAL_ENDNAME);
2189 add_token_u8(&err, dev, OPAL_STARTNAME);
2190 add_token_u8(&err, dev, OPAL_WRITELOCKED);
2191 add_token_u8(&err, dev, write_locked);
2192 add_token_u8(&err, dev, OPAL_ENDNAME);
2194 add_token_u8(&err, dev, OPAL_ENDLIST);
2195 add_token_u8(&err, dev, OPAL_ENDNAME);
2198 pr_debug("Error building SET command.\n");
2202 return finalize_and_send(dev, parse_and_check_status);
2206 static int lock_unlock_locking_range_sum(struct opal_dev *dev, void *data)
2208 u8 lr_buffer[OPAL_UID_LENGTH];
2209 u8 read_locked = 1, write_locked = 1;
2210 struct opal_lock_unlock *lkul = data;
2213 clear_opal_cmd(dev);
2214 set_comid(dev, dev->comid);
2216 if (build_locking_range(lr_buffer, sizeof(lr_buffer),
2217 lkul->session.opal_key.lr) < 0)
2220 switch (lkul->l_state) {
2230 /* vars are initialized to locked */
2233 pr_debug("Tried to set an invalid locking state.\n");
2234 return OPAL_INVAL_PARAM;
2236 ret = generic_lr_enable_disable(dev, lr_buffer, 1, 1,
2237 read_locked, write_locked);
2240 pr_debug("Error building SET command.\n");
2244 return finalize_and_send(dev, parse_and_check_status);
2247 static int activate_lsp(struct opal_dev *dev, void *data)
2249 struct opal_lr_act *opal_act = data;
2250 u8 user_lr[OPAL_UID_LENGTH];
2253 err = cmd_start(dev, opaluid[OPAL_LOCKINGSP_UID],
2254 opalmethod[OPAL_ACTIVATE]);
2256 if (opal_act->sum) {
2257 err = build_locking_range(user_lr, sizeof(user_lr),
2262 add_token_u8(&err, dev, OPAL_STARTNAME);
2263 add_token_u64(&err, dev, OPAL_SUM_SET_LIST);
2265 add_token_u8(&err, dev, OPAL_STARTLIST);
2266 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
2267 for (i = 1; i < opal_act->num_lrs; i++) {
2268 user_lr[7] = opal_act->lr[i];
2269 add_token_bytestring(&err, dev, user_lr, OPAL_UID_LENGTH);
2271 add_token_u8(&err, dev, OPAL_ENDLIST);
2272 add_token_u8(&err, dev, OPAL_ENDNAME);
2276 pr_debug("Error building Activate LockingSP command.\n");
2280 return finalize_and_send(dev, parse_and_check_status);
2283 /* Determine if we're in the Manufactured Inactive or Active state */
2284 static int get_lsp_lifecycle(struct opal_dev *dev, void *data)
2289 err = generic_get_column(dev, opaluid[OPAL_LOCKINGSP_UID],
2294 lc_status = response_get_u64(&dev->parsed, 4);
2295 /* 0x08 is Manufactured Inactive */
2296 /* 0x09 is Manufactured */
2297 if (lc_status != OPAL_MANUFACTURED_INACTIVE) {
2298 pr_debug("Couldn't determine the status of the Lifecycle state\n");
2305 static int get_msid_cpin_pin(struct opal_dev *dev, void *data)
2307 const char *msid_pin;
2311 err = generic_get_column(dev, opaluid[OPAL_C_PIN_MSID], OPAL_PIN);
2315 strlen = response_get_string(&dev->parsed, 4, &msid_pin);
2317 pr_debug("Couldn't extract MSID_CPIN from response\n");
2318 return OPAL_INVAL_PARAM;
2321 dev->prev_data = kmemdup(msid_pin, strlen, GFP_KERNEL);
2322 if (!dev->prev_data)
2325 dev->prev_d_len = strlen;
2330 static int write_table_data(struct opal_dev *dev, void *data)
2332 struct opal_read_write_table *write_tbl = data;
2334 return generic_table_write_data(dev, write_tbl->data, write_tbl->offset,
2335 write_tbl->size, write_tbl->table_uid);
2338 static int read_table_data_cont(struct opal_dev *dev)
2341 const char *data_read;
2343 err = parse_and_check_status(dev);
2347 dev->prev_d_len = response_get_string(&dev->parsed, 1, &data_read);
2348 dev->prev_data = (void *)data_read;
2349 if (!dev->prev_data) {
2350 pr_debug("%s: Couldn't read data from the table.\n", __func__);
2351 return OPAL_INVAL_PARAM;
2358 * IO_BUFFER_LENGTH = 2048
2359 * sizeof(header) = 56
2360 * No. of Token Bytes in the Response = 11
2361 * MAX size of data that can be carried in response buffer
2362 * at a time is : 2048 - (56 + 11) = 1981 = 0x7BD.
2364 #define OPAL_MAX_READ_TABLE (0x7BD)
2366 static int read_table_data(struct opal_dev *dev, void *data)
2368 struct opal_read_write_table *read_tbl = data;
2370 size_t off = 0, max_read_size = OPAL_MAX_READ_TABLE;
2372 u64 offset = read_tbl->offset, read_size = read_tbl->size - 1;
2375 err = generic_get_table_info(dev, read_tbl->table_uid, OPAL_TABLE_ROWS);
2377 pr_debug("Couldn't get the table size\n");
2381 table_len = response_get_u64(&dev->parsed, 4);
2383 /* Check if the user is trying to read from the table limits */
2384 if (read_size > table_len || offset > table_len - read_size) {
2385 pr_debug("Read size exceeds the Table size limits (%llu vs. %llu)\n",
2386 offset + read_size, table_len);
2390 while (off < read_size) {
2391 err = cmd_start(dev, read_tbl->table_uid, opalmethod[OPAL_GET]);
2393 add_token_u8(&err, dev, OPAL_STARTLIST);
2394 add_token_u8(&err, dev, OPAL_STARTNAME);
2395 add_token_u8(&err, dev, OPAL_STARTROW);
2396 add_token_u64(&err, dev, offset + off); /* start row value */
2397 add_token_u8(&err, dev, OPAL_ENDNAME);
2399 add_token_u8(&err, dev, OPAL_STARTNAME);
2400 add_token_u8(&err, dev, OPAL_ENDROW);
2402 len = min(max_read_size, (size_t)(read_size - off));
2403 add_token_u64(&err, dev, offset + off + len); /* end row value
2405 add_token_u8(&err, dev, OPAL_ENDNAME);
2406 add_token_u8(&err, dev, OPAL_ENDLIST);
2409 pr_debug("Error building read table data command.\n");
2413 err = finalize_and_send(dev, read_table_data_cont);
2417 /* len+1: This includes the NULL terminator at the end*/
2418 if (dev->prev_d_len > len + 1) {
2423 dst = (u8 __user *)(uintptr_t)read_tbl->data;
2424 if (copy_to_user(dst + off, dev->prev_data, dev->prev_d_len)) {
2425 pr_debug("Error copying data to userspace\n");
2429 dev->prev_data = NULL;
2437 static int end_opal_session(struct opal_dev *dev, void *data)
2441 clear_opal_cmd(dev);
2442 set_comid(dev, dev->comid);
2443 add_token_u8(&err, dev, OPAL_ENDOFSESSION);
2448 return finalize_and_send(dev, end_session_cont);
2451 static int end_opal_session_error(struct opal_dev *dev)
2453 const struct opal_step error_end_session = {
2457 return execute_step(dev, &error_end_session, 0);
2460 static inline void setup_opal_dev(struct opal_dev *dev)
2464 dev->prev_data = NULL;
2467 static int check_opal_support(struct opal_dev *dev)
2471 mutex_lock(&dev->dev_lock);
2472 setup_opal_dev(dev);
2473 ret = opal_discovery0_step(dev);
2475 dev->flags |= OPAL_FL_SUPPORTED;
2476 mutex_unlock(&dev->dev_lock);
2481 static void clean_opal_dev(struct opal_dev *dev)
2484 struct opal_suspend_data *suspend, *next;
2486 mutex_lock(&dev->dev_lock);
2487 list_for_each_entry_safe(suspend, next, &dev->unlk_lst, node) {
2488 list_del(&suspend->node);
2491 mutex_unlock(&dev->dev_lock);
2494 void free_opal_dev(struct opal_dev *dev)
2499 clean_opal_dev(dev);
2504 EXPORT_SYMBOL(free_opal_dev);
2506 struct opal_dev *init_opal_dev(void *data, sec_send_recv *send_recv)
2508 struct opal_dev *dev;
2510 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
2515 * Presumably DMA-able buffers must be cache-aligned. Kmalloc makes
2516 * sure the allocated buffer is DMA-safe in that regard.
2518 dev->cmd = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2522 dev->resp = kmalloc(IO_BUFFER_LENGTH, GFP_KERNEL);
2526 INIT_LIST_HEAD(&dev->unlk_lst);
2527 mutex_init(&dev->dev_lock);
2530 dev->send_recv = send_recv;
2531 if (check_opal_support(dev) != 0) {
2532 pr_debug("Opal is not supported on this device\n");
2549 EXPORT_SYMBOL(init_opal_dev);
2551 static int opal_secure_erase_locking_range(struct opal_dev *dev,
2552 struct opal_session_info *opal_session)
2554 const struct opal_step erase_steps[] = {
2555 { start_auth_opal_session, opal_session },
2556 { get_active_key, &opal_session->opal_key.lr },
2558 { end_opal_session, }
2562 ret = opal_get_key(dev, &opal_session->opal_key);
2565 mutex_lock(&dev->dev_lock);
2566 setup_opal_dev(dev);
2567 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2568 mutex_unlock(&dev->dev_lock);
2573 static int opal_get_discv(struct opal_dev *dev, struct opal_discovery *discv)
2575 const struct opal_step discovery0_step = {
2576 opal_discovery0, discv
2580 mutex_lock(&dev->dev_lock);
2581 setup_opal_dev(dev);
2582 ret = execute_step(dev, &discovery0_step, 0);
2583 mutex_unlock(&dev->dev_lock);
2586 return discv->size; /* modified to actual length of data */
2589 static int opal_revertlsp(struct opal_dev *dev, struct opal_revert_lsp *rev)
2591 /* controller will terminate session */
2592 const struct opal_step steps[] = {
2593 { start_admin1LSP_opal_session, &rev->key },
2598 ret = opal_get_key(dev, &rev->key);
2601 mutex_lock(&dev->dev_lock);
2602 setup_opal_dev(dev);
2603 ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
2604 mutex_unlock(&dev->dev_lock);
2609 static int opal_erase_locking_range(struct opal_dev *dev,
2610 struct opal_session_info *opal_session)
2612 const struct opal_step erase_steps[] = {
2613 { start_auth_opal_session, opal_session },
2614 { erase_locking_range, opal_session },
2615 { end_opal_session, }
2619 ret = opal_get_key(dev, &opal_session->opal_key);
2622 mutex_lock(&dev->dev_lock);
2623 setup_opal_dev(dev);
2624 ret = execute_steps(dev, erase_steps, ARRAY_SIZE(erase_steps));
2625 mutex_unlock(&dev->dev_lock);
2630 static int opal_enable_disable_shadow_mbr(struct opal_dev *dev,
2631 struct opal_mbr_data *opal_mbr)
2633 u8 enable_disable = opal_mbr->enable_disable == OPAL_MBR_ENABLE ?
2634 OPAL_TRUE : OPAL_FALSE;
2636 const struct opal_step mbr_steps[] = {
2637 { start_admin1LSP_opal_session, &opal_mbr->key },
2638 { set_mbr_done, &enable_disable },
2639 { end_opal_session, },
2640 { start_admin1LSP_opal_session, &opal_mbr->key },
2641 { set_mbr_enable_disable, &enable_disable },
2642 { end_opal_session, }
2646 if (opal_mbr->enable_disable != OPAL_MBR_ENABLE &&
2647 opal_mbr->enable_disable != OPAL_MBR_DISABLE)
2650 ret = opal_get_key(dev, &opal_mbr->key);
2653 mutex_lock(&dev->dev_lock);
2654 setup_opal_dev(dev);
2655 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2656 mutex_unlock(&dev->dev_lock);
2661 static int opal_set_mbr_done(struct opal_dev *dev,
2662 struct opal_mbr_done *mbr_done)
2664 u8 mbr_done_tf = mbr_done->done_flag == OPAL_MBR_DONE ?
2665 OPAL_TRUE : OPAL_FALSE;
2667 const struct opal_step mbr_steps[] = {
2668 { start_admin1LSP_opal_session, &mbr_done->key },
2669 { set_mbr_done, &mbr_done_tf },
2670 { end_opal_session, }
2674 if (mbr_done->done_flag != OPAL_MBR_DONE &&
2675 mbr_done->done_flag != OPAL_MBR_NOT_DONE)
2678 ret = opal_get_key(dev, &mbr_done->key);
2681 mutex_lock(&dev->dev_lock);
2682 setup_opal_dev(dev);
2683 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2684 mutex_unlock(&dev->dev_lock);
2689 static int opal_write_shadow_mbr(struct opal_dev *dev,
2690 struct opal_shadow_mbr *info)
2692 const struct opal_step mbr_steps[] = {
2693 { start_admin1LSP_opal_session, &info->key },
2694 { write_shadow_mbr, info },
2695 { end_opal_session, }
2699 if (info->size == 0)
2702 ret = opal_get_key(dev, &info->key);
2705 mutex_lock(&dev->dev_lock);
2706 setup_opal_dev(dev);
2707 ret = execute_steps(dev, mbr_steps, ARRAY_SIZE(mbr_steps));
2708 mutex_unlock(&dev->dev_lock);
2713 static int opal_save(struct opal_dev *dev, struct opal_lock_unlock *lk_unlk)
2715 struct opal_suspend_data *suspend;
2717 suspend = kzalloc(sizeof(*suspend), GFP_KERNEL);
2721 suspend->unlk = *lk_unlk;
2722 suspend->lr = lk_unlk->session.opal_key.lr;
2724 mutex_lock(&dev->dev_lock);
2725 setup_opal_dev(dev);
2726 add_suspend_info(dev, suspend);
2727 mutex_unlock(&dev->dev_lock);
2732 static int opal_add_user_to_lr(struct opal_dev *dev,
2733 struct opal_lock_unlock *lk_unlk)
2735 const struct opal_step steps[] = {
2736 { start_admin1LSP_opal_session, &lk_unlk->session.opal_key },
2737 { add_user_to_lr, lk_unlk },
2738 { add_user_to_lr_ace, lk_unlk },
2739 { end_opal_session, }
2743 if (lk_unlk->l_state != OPAL_RO &&
2744 lk_unlk->l_state != OPAL_RW) {
2745 pr_debug("Locking state was not RO or RW\n");
2749 if (lk_unlk->session.who < OPAL_USER1 ||
2750 lk_unlk->session.who > OPAL_USER9) {
2751 pr_debug("Authority was not within the range of users: %d\n",
2752 lk_unlk->session.who);
2756 if (lk_unlk->session.sum) {
2757 pr_debug("%s not supported in sum. Use setup locking range\n",
2762 ret = opal_get_key(dev, &lk_unlk->session.opal_key);
2765 mutex_lock(&dev->dev_lock);
2766 setup_opal_dev(dev);
2767 ret = execute_steps(dev, steps, ARRAY_SIZE(steps));
2768 mutex_unlock(&dev->dev_lock);
2773 static int opal_reverttper(struct opal_dev *dev, struct opal_key *opal, bool psid)
2775 /* controller will terminate session */
2776 const struct opal_step revert_steps[] = {
2777 { start_SIDASP_opal_session, opal },
2780 const struct opal_step psid_revert_steps[] = {
2781 { start_PSID_opal_session, opal },
2787 ret = opal_get_key(dev, opal);
2791 mutex_lock(&dev->dev_lock);
2792 setup_opal_dev(dev);
2794 ret = execute_steps(dev, psid_revert_steps,
2795 ARRAY_SIZE(psid_revert_steps));
2797 ret = execute_steps(dev, revert_steps,
2798 ARRAY_SIZE(revert_steps));
2799 mutex_unlock(&dev->dev_lock);
2802 * If we successfully reverted lets clean
2803 * any saved locking ranges.
2806 clean_opal_dev(dev);
2811 static int __opal_lock_unlock(struct opal_dev *dev,
2812 struct opal_lock_unlock *lk_unlk)
2814 const struct opal_step unlock_steps[] = {
2815 { start_auth_opal_session, &lk_unlk->session },
2816 { lock_unlock_locking_range, lk_unlk },
2817 { end_opal_session, }
2819 const struct opal_step unlock_sum_steps[] = {
2820 { start_auth_opal_session, &lk_unlk->session },
2821 { lock_unlock_locking_range_sum, lk_unlk },
2822 { end_opal_session, }
2825 if (lk_unlk->session.sum)
2826 return execute_steps(dev, unlock_sum_steps,
2827 ARRAY_SIZE(unlock_sum_steps));
2829 return execute_steps(dev, unlock_steps,
2830 ARRAY_SIZE(unlock_steps));
2833 static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
2835 u8 mbr_done_tf = OPAL_TRUE;
2836 const struct opal_step mbrdone_step[] = {
2837 { start_admin1LSP_opal_session, key },
2838 { set_mbr_done, &mbr_done_tf },
2839 { end_opal_session, }
2842 return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
2845 static void opal_lock_check_for_saved_key(struct opal_dev *dev,
2846 struct opal_lock_unlock *lk_unlk)
2848 struct opal_suspend_data *iter;
2850 if (lk_unlk->l_state != OPAL_LK ||
2851 lk_unlk->session.opal_key.key_len > 0)
2855 * Usually when closing a crypto device (eg: dm-crypt with LUKS) the
2856 * volume key is not required, as it requires root privileges anyway,
2857 * and root can deny access to a disk in many ways regardless.
2858 * Requiring the volume key to lock the device is a peculiarity of the
2859 * OPAL specification. Given we might already have saved the key if
2860 * the user requested it via the 'IOC_OPAL_SAVE' ioctl, we can use
2861 * that key to lock the device if no key was provided here, the
2862 * locking range matches and the appropriate flag was passed with
2864 * This allows integrating OPAL with tools and libraries that are used
2865 * to the common behaviour and do not ask for the volume key when
2868 setup_opal_dev(dev);
2869 list_for_each_entry(iter, &dev->unlk_lst, node) {
2870 if ((iter->unlk.flags & OPAL_SAVE_FOR_LOCK) &&
2871 iter->lr == lk_unlk->session.opal_key.lr &&
2872 iter->unlk.session.opal_key.key_len > 0) {
2873 lk_unlk->session.opal_key.key_len =
2874 iter->unlk.session.opal_key.key_len;
2875 memcpy(lk_unlk->session.opal_key.key,
2876 iter->unlk.session.opal_key.key,
2877 iter->unlk.session.opal_key.key_len);
2883 static int opal_lock_unlock(struct opal_dev *dev,
2884 struct opal_lock_unlock *lk_unlk)
2888 if (lk_unlk->session.who > OPAL_USER9)
2891 ret = opal_get_key(dev, &lk_unlk->session.opal_key);
2894 mutex_lock(&dev->dev_lock);
2895 opal_lock_check_for_saved_key(dev, lk_unlk);
2896 ret = __opal_lock_unlock(dev, lk_unlk);
2897 mutex_unlock(&dev->dev_lock);
2902 static int opal_take_ownership(struct opal_dev *dev, struct opal_key *opal)
2904 const struct opal_step owner_steps[] = {
2905 { start_anybodyASP_opal_session, },
2906 { get_msid_cpin_pin, },
2907 { end_opal_session, },
2908 { start_SIDASP_opal_session, opal },
2909 { set_sid_cpin_pin, opal },
2910 { end_opal_session, }
2917 ret = opal_get_key(dev, opal);
2920 mutex_lock(&dev->dev_lock);
2921 setup_opal_dev(dev);
2922 ret = execute_steps(dev, owner_steps, ARRAY_SIZE(owner_steps));
2923 mutex_unlock(&dev->dev_lock);
2928 static int opal_activate_lsp(struct opal_dev *dev,
2929 struct opal_lr_act *opal_lr_act)
2931 const struct opal_step active_steps[] = {
2932 { start_SIDASP_opal_session, &opal_lr_act->key },
2933 { get_lsp_lifecycle, },
2934 { activate_lsp, opal_lr_act },
2935 { end_opal_session, }
2939 if (!opal_lr_act->num_lrs || opal_lr_act->num_lrs > OPAL_MAX_LRS)
2942 ret = opal_get_key(dev, &opal_lr_act->key);
2945 mutex_lock(&dev->dev_lock);
2946 setup_opal_dev(dev);
2947 ret = execute_steps(dev, active_steps, ARRAY_SIZE(active_steps));
2948 mutex_unlock(&dev->dev_lock);
2953 static int opal_setup_locking_range(struct opal_dev *dev,
2954 struct opal_user_lr_setup *opal_lrs)
2956 const struct opal_step lr_steps[] = {
2957 { start_auth_opal_session, &opal_lrs->session },
2958 { setup_locking_range, opal_lrs },
2959 { end_opal_session, }
2963 ret = opal_get_key(dev, &opal_lrs->session.opal_key);
2966 mutex_lock(&dev->dev_lock);
2967 setup_opal_dev(dev);
2968 ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
2969 mutex_unlock(&dev->dev_lock);
2974 static int opal_locking_range_status(struct opal_dev *dev,
2975 struct opal_lr_status *opal_lrst,
2978 const struct opal_step lr_steps[] = {
2979 { start_auth_opal_session, &opal_lrst->session },
2980 { locking_range_status, opal_lrst },
2981 { end_opal_session, }
2985 mutex_lock(&dev->dev_lock);
2986 setup_opal_dev(dev);
2987 ret = execute_steps(dev, lr_steps, ARRAY_SIZE(lr_steps));
2988 mutex_unlock(&dev->dev_lock);
2990 /* skip session info when copying back to uspace */
2991 if (!ret && copy_to_user(data + offsetof(struct opal_lr_status, range_start),
2992 (void *)opal_lrst + offsetof(struct opal_lr_status, range_start),
2993 sizeof(*opal_lrst) - offsetof(struct opal_lr_status, range_start))) {
2994 pr_debug("Error copying status to userspace\n");
3001 static int opal_set_new_pw(struct opal_dev *dev, struct opal_new_pw *opal_pw)
3003 const struct opal_step pw_steps[] = {
3004 { start_auth_opal_session, &opal_pw->session },
3005 { set_new_pw, &opal_pw->new_user_pw },
3006 { end_opal_session, }
3010 if (opal_pw->session.who > OPAL_USER9 ||
3011 opal_pw->new_user_pw.who > OPAL_USER9)
3014 mutex_lock(&dev->dev_lock);
3015 setup_opal_dev(dev);
3016 ret = execute_steps(dev, pw_steps, ARRAY_SIZE(pw_steps));
3017 mutex_unlock(&dev->dev_lock);
3022 /* update keyring with new password */
3023 ret = update_sed_opal_key(OPAL_AUTH_KEY,
3024 opal_pw->new_user_pw.opal_key.key,
3025 opal_pw->new_user_pw.opal_key.key_len);
3030 static int opal_activate_user(struct opal_dev *dev,
3031 struct opal_session_info *opal_session)
3033 const struct opal_step act_steps[] = {
3034 { start_admin1LSP_opal_session, &opal_session->opal_key },
3035 { internal_activate_user, opal_session },
3036 { end_opal_session, }
3040 /* We can't activate Admin1 it's active as manufactured */
3041 if (opal_session->who < OPAL_USER1 ||
3042 opal_session->who > OPAL_USER9) {
3043 pr_debug("Who was not a valid user: %d\n", opal_session->who);
3047 ret = opal_get_key(dev, &opal_session->opal_key);
3050 mutex_lock(&dev->dev_lock);
3051 setup_opal_dev(dev);
3052 ret = execute_steps(dev, act_steps, ARRAY_SIZE(act_steps));
3053 mutex_unlock(&dev->dev_lock);
3058 bool opal_unlock_from_suspend(struct opal_dev *dev)
3060 struct opal_suspend_data *suspend;
3061 bool was_failure = false;
3067 if (!(dev->flags & OPAL_FL_SUPPORTED))
3070 mutex_lock(&dev->dev_lock);
3071 setup_opal_dev(dev);
3073 list_for_each_entry(suspend, &dev->unlk_lst, node) {
3077 ret = __opal_lock_unlock(dev, &suspend->unlk);
3079 pr_debug("Failed to unlock LR %hhu with sum %d\n",
3080 suspend->unlk.session.opal_key.lr,
3081 suspend->unlk.session.sum);
3085 if (dev->flags & OPAL_FL_MBR_ENABLED) {
3086 ret = __opal_set_mbr_done(dev, &suspend->unlk.session.opal_key);
3088 pr_debug("Failed to set MBR Done in S3 resume\n");
3091 mutex_unlock(&dev->dev_lock);
3095 EXPORT_SYMBOL(opal_unlock_from_suspend);
3097 static int opal_read_table(struct opal_dev *dev,
3098 struct opal_read_write_table *rw_tbl)
3100 const struct opal_step read_table_steps[] = {
3101 { start_admin1LSP_opal_session, &rw_tbl->key },
3102 { read_table_data, rw_tbl },
3103 { end_opal_session, }
3110 return execute_steps(dev, read_table_steps,
3111 ARRAY_SIZE(read_table_steps));
3114 static int opal_write_table(struct opal_dev *dev,
3115 struct opal_read_write_table *rw_tbl)
3117 const struct opal_step write_table_steps[] = {
3118 { start_admin1LSP_opal_session, &rw_tbl->key },
3119 { write_table_data, rw_tbl },
3120 { end_opal_session, }
3127 return execute_steps(dev, write_table_steps,
3128 ARRAY_SIZE(write_table_steps));
3131 static int opal_generic_read_write_table(struct opal_dev *dev,
3132 struct opal_read_write_table *rw_tbl)
3136 ret = opal_get_key(dev, &rw_tbl->key);
3139 mutex_lock(&dev->dev_lock);
3140 setup_opal_dev(dev);
3142 bit_set = fls64(rw_tbl->flags) - 1;
3144 case OPAL_READ_TABLE:
3145 ret = opal_read_table(dev, rw_tbl);
3147 case OPAL_WRITE_TABLE:
3148 ret = opal_write_table(dev, rw_tbl);
3151 pr_debug("Invalid bit set in the flag (%016llx).\n",
3157 mutex_unlock(&dev->dev_lock);
3162 static int opal_get_status(struct opal_dev *dev, void __user *data)
3164 struct opal_status sts = {0};
3167 * check_opal_support() error is not fatal,
3168 * !dev->supported is a valid condition
3170 if (!check_opal_support(dev))
3171 sts.flags = dev->flags;
3172 if (copy_to_user(data, &sts, sizeof(sts))) {
3173 pr_debug("Error copying status to userspace\n");
3179 static int opal_get_geometry(struct opal_dev *dev, void __user *data)
3181 struct opal_geometry geo = {0};
3183 if (check_opal_support(dev))
3186 geo.align = dev->align_required;
3187 geo.logical_block_size = dev->logical_block_size;
3188 geo.alignment_granularity = dev->align;
3189 geo.lowest_aligned_lba = dev->lowest_lba;
3191 if (copy_to_user(data, &geo, sizeof(geo))) {
3192 pr_debug("Error copying geometry data to userspace\n");
3199 int sed_ioctl(struct opal_dev *dev, unsigned int cmd, void __user *arg)
3204 if (!capable(CAP_SYS_ADMIN))
3208 if (!(dev->flags & OPAL_FL_SUPPORTED))
3212 p = memdup_user(arg, _IOC_SIZE(cmd));
3219 ret = opal_save(dev, p);
3221 case IOC_OPAL_LOCK_UNLOCK:
3222 ret = opal_lock_unlock(dev, p);
3224 case IOC_OPAL_TAKE_OWNERSHIP:
3225 ret = opal_take_ownership(dev, p);
3227 case IOC_OPAL_ACTIVATE_LSP:
3228 ret = opal_activate_lsp(dev, p);
3230 case IOC_OPAL_SET_PW:
3231 ret = opal_set_new_pw(dev, p);
3233 case IOC_OPAL_ACTIVATE_USR:
3234 ret = opal_activate_user(dev, p);
3236 case IOC_OPAL_REVERT_TPR:
3237 ret = opal_reverttper(dev, p, false);
3239 case IOC_OPAL_LR_SETUP:
3240 ret = opal_setup_locking_range(dev, p);
3242 case IOC_OPAL_ADD_USR_TO_LR:
3243 ret = opal_add_user_to_lr(dev, p);
3245 case IOC_OPAL_ENABLE_DISABLE_MBR:
3246 ret = opal_enable_disable_shadow_mbr(dev, p);
3248 case IOC_OPAL_MBR_DONE:
3249 ret = opal_set_mbr_done(dev, p);
3251 case IOC_OPAL_WRITE_SHADOW_MBR:
3252 ret = opal_write_shadow_mbr(dev, p);
3254 case IOC_OPAL_ERASE_LR:
3255 ret = opal_erase_locking_range(dev, p);
3257 case IOC_OPAL_SECURE_ERASE_LR:
3258 ret = opal_secure_erase_locking_range(dev, p);
3260 case IOC_OPAL_PSID_REVERT_TPR:
3261 ret = opal_reverttper(dev, p, true);
3263 case IOC_OPAL_GENERIC_TABLE_RW:
3264 ret = opal_generic_read_write_table(dev, p);
3266 case IOC_OPAL_GET_STATUS:
3267 ret = opal_get_status(dev, arg);
3269 case IOC_OPAL_GET_LR_STATUS:
3270 ret = opal_locking_range_status(dev, p, arg);
3272 case IOC_OPAL_GET_GEOMETRY:
3273 ret = opal_get_geometry(dev, arg);
3275 case IOC_OPAL_REVERT_LSP:
3276 ret = opal_revertlsp(dev, p);
3278 case IOC_OPAL_DISCOVERY:
3279 ret = opal_get_discv(dev, p);
3290 EXPORT_SYMBOL_GPL(sed_ioctl);
3292 static int __init sed_opal_init(void)
3296 kr = keyring_alloc(".sed_opal",
3297 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
3298 (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_VIEW |
3299 KEY_USR_READ | KEY_USR_SEARCH | KEY_USR_WRITE,
3300 KEY_ALLOC_NOT_IN_QUOTA,
3305 sed_opal_keyring = kr;
3309 late_initcall(sed_opal_init);