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