1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ 3 #include <linux/security.h> 4 #include <linux/debugfs.h> 5 #include <linux/ktime.h> 6 #include <linux/mutex.h> 7 #include <linux/unaligned.h> 8 #include <cxlpci.h> 9 #include <cxlmem.h> 10 #include <cxl.h> 11 12 #include "core.h" 13 #include "trace.h" 14 15 static bool cxl_raw_allow_all; 16 17 /** 18 * DOC: cxl mbox 19 * 20 * Core implementation of the CXL 2.0 Type-3 Memory Device Mailbox. The 21 * implementation is used by the cxl_pci driver to initialize the device 22 * and implement the cxl_mem.h IOCTL UAPI. It also implements the 23 * backend of the cxl_pmem_ctl() transport for LIBNVDIMM. 24 */ 25 26 #define cxl_for_each_cmd(cmd) \ 27 for ((cmd) = &cxl_mem_commands[0]; \ 28 ((cmd) - cxl_mem_commands) < ARRAY_SIZE(cxl_mem_commands); (cmd)++) 29 30 #define CXL_CMD(_id, sin, sout, _flags) \ 31 [CXL_MEM_COMMAND_ID_##_id] = { \ 32 .info = { \ 33 .id = CXL_MEM_COMMAND_ID_##_id, \ 34 .size_in = sin, \ 35 .size_out = sout, \ 36 }, \ 37 .opcode = CXL_MBOX_OP_##_id, \ 38 .flags = _flags, \ 39 } 40 41 #define CXL_VARIABLE_PAYLOAD ~0U 42 /* 43 * This table defines the supported mailbox commands for the driver. This table 44 * is made up of a UAPI structure. Non-negative values as parameters in the 45 * table will be validated against the user's input. For example, if size_in is 46 * 0, and the user passed in 1, it is an error. 47 */ 48 static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = { 49 CXL_CMD(IDENTIFY, 0, 0x43, CXL_CMD_FLAG_FORCE_ENABLE), 50 #ifdef CONFIG_CXL_MEM_RAW_COMMANDS 51 CXL_CMD(RAW, CXL_VARIABLE_PAYLOAD, CXL_VARIABLE_PAYLOAD, 0), 52 #endif 53 CXL_CMD(GET_SUPPORTED_LOGS, 0, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 54 CXL_CMD(GET_FW_INFO, 0, 0x50, 0), 55 CXL_CMD(GET_PARTITION_INFO, 0, 0x20, 0), 56 CXL_CMD(GET_LSA, 0x8, CXL_VARIABLE_PAYLOAD, 0), 57 CXL_CMD(GET_HEALTH_INFO, 0, 0x12, 0), 58 CXL_CMD(GET_LOG, 0x18, CXL_VARIABLE_PAYLOAD, CXL_CMD_FLAG_FORCE_ENABLE), 59 CXL_CMD(GET_LOG_CAPS, 0x10, 0x4, 0), 60 CXL_CMD(CLEAR_LOG, 0x10, 0, 0), 61 CXL_CMD(GET_SUP_LOG_SUBLIST, 0x2, CXL_VARIABLE_PAYLOAD, 0), 62 CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0), 63 CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0), 64 CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0), 65 CXL_CMD(SET_ALERT_CONFIG, 0xc, 0, 0), 66 CXL_CMD(GET_SHUTDOWN_STATE, 0, 0x1, 0), 67 CXL_CMD(SET_SHUTDOWN_STATE, 0x1, 0, 0), 68 CXL_CMD(GET_SCAN_MEDIA_CAPS, 0x10, 0x4, 0), 69 CXL_CMD(GET_TIMESTAMP, 0, 0x8, 0), 70 }; 71 72 /* 73 * Commands that RAW doesn't permit. The rationale for each: 74 * 75 * CXL_MBOX_OP_ACTIVATE_FW: Firmware activation requires adjustment / 76 * coordination of transaction timeout values at the root bridge level. 77 * 78 * CXL_MBOX_OP_SET_PARTITION_INFO: The device memory map may change live 79 * and needs to be coordinated with HDM updates. 80 * 81 * CXL_MBOX_OP_SET_LSA: The label storage area may be cached by the 82 * driver and any writes from userspace invalidates those contents. 83 * 84 * CXL_MBOX_OP_SET_SHUTDOWN_STATE: Set shutdown state assumes no writes 85 * to the device after it is marked clean, userspace can not make that 86 * assertion. 87 * 88 * CXL_MBOX_OP_[GET_]SCAN_MEDIA: The kernel provides a native error list that 89 * is kept up to date with patrol notifications and error management. 90 * 91 * CXL_MBOX_OP_[GET_,INJECT_,CLEAR_]POISON: These commands require kernel 92 * driver orchestration for safety. 93 * 94 * CXL_MBOX_OP_[GET_SUPPORTED_FEATURES,GET_FEATURE,SET_FEATURE]: Features are 95 * accessed through the fwctl ABI, which applies scope-based access control. 96 * The RAW path would bypass those checks, so it is not permitted here. 97 */ 98 static u16 cxl_disabled_raw_commands[] = { 99 CXL_MBOX_OP_ACTIVATE_FW, 100 CXL_MBOX_OP_SET_PARTITION_INFO, 101 CXL_MBOX_OP_SET_LSA, 102 CXL_MBOX_OP_SET_SHUTDOWN_STATE, 103 CXL_MBOX_OP_SCAN_MEDIA, 104 CXL_MBOX_OP_GET_SCAN_MEDIA, 105 CXL_MBOX_OP_GET_POISON, 106 CXL_MBOX_OP_INJECT_POISON, 107 CXL_MBOX_OP_CLEAR_POISON, 108 CXL_MBOX_OP_GET_SUPPORTED_FEATURES, 109 CXL_MBOX_OP_GET_FEATURE, 110 CXL_MBOX_OP_SET_FEATURE, 111 }; 112 113 /* 114 * Command sets that RAW doesn't permit. All opcodes in this set are 115 * disabled because they pass plain text security payloads over the 116 * user/kernel boundary. This functionality is intended to be wrapped 117 * behind the keys ABI which allows for encrypted payloads in the UAPI 118 */ 119 static u8 security_command_sets[] = { 120 0x44, /* Sanitize */ 121 0x45, /* Persistent Memory Data-at-rest Security */ 122 0x46, /* Security Passthrough */ 123 }; 124 125 static bool cxl_is_security_command(u16 opcode) 126 { 127 int i; 128 129 for (i = 0; i < ARRAY_SIZE(security_command_sets); i++) 130 if (security_command_sets[i] == (opcode >> 8)) 131 return true; 132 return false; 133 } 134 135 static void cxl_set_security_cmd_enabled(struct cxl_security_state *security, 136 u16 opcode) 137 { 138 switch (opcode) { 139 case CXL_MBOX_OP_SANITIZE: 140 set_bit(CXL_SEC_ENABLED_SANITIZE, security->enabled_cmds); 141 break; 142 case CXL_MBOX_OP_SECURE_ERASE: 143 set_bit(CXL_SEC_ENABLED_SECURE_ERASE, 144 security->enabled_cmds); 145 break; 146 case CXL_MBOX_OP_GET_SECURITY_STATE: 147 set_bit(CXL_SEC_ENABLED_GET_SECURITY_STATE, 148 security->enabled_cmds); 149 break; 150 case CXL_MBOX_OP_SET_PASSPHRASE: 151 set_bit(CXL_SEC_ENABLED_SET_PASSPHRASE, 152 security->enabled_cmds); 153 break; 154 case CXL_MBOX_OP_DISABLE_PASSPHRASE: 155 set_bit(CXL_SEC_ENABLED_DISABLE_PASSPHRASE, 156 security->enabled_cmds); 157 break; 158 case CXL_MBOX_OP_UNLOCK: 159 set_bit(CXL_SEC_ENABLED_UNLOCK, security->enabled_cmds); 160 break; 161 case CXL_MBOX_OP_FREEZE_SECURITY: 162 set_bit(CXL_SEC_ENABLED_FREEZE_SECURITY, 163 security->enabled_cmds); 164 break; 165 case CXL_MBOX_OP_PASSPHRASE_SECURE_ERASE: 166 set_bit(CXL_SEC_ENABLED_PASSPHRASE_SECURE_ERASE, 167 security->enabled_cmds); 168 break; 169 default: 170 break; 171 } 172 } 173 174 static bool cxl_is_dcd_command(u16 opcode) 175 { 176 #define CXL_MBOX_OP_DCD_CMDS 0x48 177 178 return (opcode >> 8) == CXL_MBOX_OP_DCD_CMDS; 179 } 180 181 static void cxl_set_dcd_cmd_enabled(u16 opcode, unsigned long *cmd_mask) 182 { 183 switch (opcode) { 184 case CXL_MBOX_OP_GET_DC_CONFIG: 185 set_bit(CXL_DCD_ENABLED_GET_CONFIG, cmd_mask); 186 break; 187 case CXL_MBOX_OP_GET_DC_EXTENT_LIST: 188 set_bit(CXL_DCD_ENABLED_GET_EXTENT_LIST, cmd_mask); 189 break; 190 case CXL_MBOX_OP_ADD_DC_RESPONSE: 191 set_bit(CXL_DCD_ENABLED_ADD_RESPONSE, cmd_mask); 192 break; 193 case CXL_MBOX_OP_RELEASE_DC: 194 set_bit(CXL_DCD_ENABLED_RELEASE, cmd_mask); 195 break; 196 default: 197 break; 198 } 199 } 200 201 static bool cxl_is_poison_command(u16 opcode) 202 { 203 #define CXL_MBOX_OP_POISON_CMDS 0x43 204 205 if ((opcode >> 8) == CXL_MBOX_OP_POISON_CMDS) 206 return true; 207 208 return false; 209 } 210 211 static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison, 212 u16 opcode) 213 { 214 switch (opcode) { 215 case CXL_MBOX_OP_GET_POISON: 216 set_bit(CXL_POISON_ENABLED_LIST, poison->enabled_cmds); 217 break; 218 case CXL_MBOX_OP_INJECT_POISON: 219 set_bit(CXL_POISON_ENABLED_INJECT, poison->enabled_cmds); 220 break; 221 case CXL_MBOX_OP_CLEAR_POISON: 222 set_bit(CXL_POISON_ENABLED_CLEAR, poison->enabled_cmds); 223 break; 224 case CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS: 225 set_bit(CXL_POISON_ENABLED_SCAN_CAPS, poison->enabled_cmds); 226 break; 227 case CXL_MBOX_OP_SCAN_MEDIA: 228 set_bit(CXL_POISON_ENABLED_SCAN_MEDIA, poison->enabled_cmds); 229 break; 230 case CXL_MBOX_OP_GET_SCAN_MEDIA: 231 set_bit(CXL_POISON_ENABLED_SCAN_RESULTS, poison->enabled_cmds); 232 break; 233 default: 234 break; 235 } 236 } 237 238 static struct cxl_mem_command *cxl_mem_find_command(u16 opcode) 239 { 240 struct cxl_mem_command *c; 241 242 cxl_for_each_cmd(c) 243 if (c->opcode == opcode) 244 return c; 245 246 return NULL; 247 } 248 249 static const char *cxl_mem_opcode_to_name(u16 opcode) 250 { 251 struct cxl_mem_command *c; 252 253 c = cxl_mem_find_command(opcode); 254 if (!c) 255 return NULL; 256 257 return cxl_command_names[c->info.id].name; 258 } 259 260 /** 261 * cxl_internal_send_cmd() - Kernel internal interface to send a mailbox command 262 * @cxl_mbox: CXL mailbox context 263 * @mbox_cmd: initialized command to execute 264 * 265 * Context: Any context. 266 * Return: 267 * * %>=0 - Number of bytes returned in @out. 268 * * %-E2BIG - Payload is too large for hardware. 269 * * %-EBUSY - Couldn't acquire exclusive mailbox access. 270 * * %-EFAULT - Hardware error occurred. 271 * * %-ENXIO - Command completed, but device reported an error. 272 * * %-EIO - Unexpected output size. 273 * 274 * Mailbox commands may execute successfully yet the device itself reported an 275 * error. While this distinction can be useful for commands from userspace, the 276 * kernel will only be able to use results when both are successful. 277 */ 278 int cxl_internal_send_cmd(struct cxl_mailbox *cxl_mbox, 279 struct cxl_mbox_cmd *mbox_cmd) 280 { 281 size_t out_size, min_out; 282 int rc; 283 284 if (mbox_cmd->size_in > cxl_mbox->payload_size || 285 mbox_cmd->size_out > cxl_mbox->payload_size) 286 return -E2BIG; 287 288 out_size = mbox_cmd->size_out; 289 min_out = mbox_cmd->min_out; 290 rc = cxl_mbox->mbox_send(cxl_mbox, mbox_cmd); 291 /* 292 * EIO is reserved for a payload size mismatch and mbox_send() 293 * may not return this error. 294 */ 295 if (WARN_ONCE(rc == -EIO, "Bad return code: -EIO")) 296 return -ENXIO; 297 if (rc) 298 return rc; 299 300 if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS && 301 mbox_cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND) 302 return cxl_mbox_cmd_rc2errno(mbox_cmd); 303 304 if (!out_size) 305 return 0; 306 307 /* 308 * Variable sized output needs to at least satisfy the caller's 309 * minimum if not the fully requested size. 310 */ 311 if (min_out == 0) 312 min_out = out_size; 313 314 if (mbox_cmd->size_out < min_out) 315 return -EIO; 316 return 0; 317 } 318 EXPORT_SYMBOL_NS_GPL(cxl_internal_send_cmd, "CXL"); 319 320 static bool cxl_mem_raw_command_allowed(u16 opcode) 321 { 322 int i; 323 324 if (!IS_ENABLED(CONFIG_CXL_MEM_RAW_COMMANDS)) 325 return false; 326 327 if (security_locked_down(LOCKDOWN_PCI_ACCESS)) 328 return false; 329 330 if (cxl_raw_allow_all) 331 return true; 332 333 if (cxl_is_security_command(opcode)) 334 return false; 335 336 for (i = 0; i < ARRAY_SIZE(cxl_disabled_raw_commands); i++) 337 if (cxl_disabled_raw_commands[i] == opcode) 338 return false; 339 340 return true; 341 } 342 343 /** 344 * cxl_payload_from_user_allowed() - Check contents of in_payload. 345 * @opcode: The mailbox command opcode. 346 * @payload_in: Pointer to the input payload passed in from user space. 347 * @in_size: Size of @payload_in in bytes. 348 * 349 * Return: 350 * * true - payload_in passes check for @opcode. 351 * * false - payload_in contains invalid or unsupported values. 352 * 353 * The driver may inspect payload contents before sending a mailbox 354 * command from user space to the device. The intent is to reject 355 * commands with input payloads that are known to be unsafe. This 356 * check is not intended to replace the users careful selection of 357 * mailbox command parameters and makes no guarantee that the user 358 * command will succeed, nor that it is appropriate. 359 * 360 * The specific checks are determined by the opcode. 361 */ 362 static bool cxl_payload_from_user_allowed(u16 opcode, void *payload_in, 363 size_t in_size) 364 { 365 switch (opcode) { 366 case CXL_MBOX_OP_SET_PARTITION_INFO: { 367 struct cxl_mbox_set_partition_info *pi = payload_in; 368 369 if (in_size < sizeof(*pi)) 370 return false; 371 if (pi->flags & CXL_SET_PARTITION_IMMEDIATE_FLAG) 372 return false; 373 break; 374 } 375 case CXL_MBOX_OP_CLEAR_LOG: { 376 const uuid_t *uuid = (uuid_t *)payload_in; 377 378 if (in_size < sizeof(uuid_t)) 379 return false; 380 /* 381 * Restrict the ‘Clear log’ action to only apply to 382 * Vendor debug logs. 383 */ 384 return uuid_equal(uuid, &DEFINE_CXL_VENDOR_DEBUG_UUID); 385 } 386 default: 387 break; 388 } 389 return true; 390 } 391 392 static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox_cmd, 393 struct cxl_mailbox *cxl_mbox, u16 opcode, 394 size_t in_size, size_t out_size, u64 in_payload) 395 { 396 *mbox_cmd = (struct cxl_mbox_cmd) { 397 .opcode = opcode, 398 .size_in = in_size, 399 }; 400 401 if (in_size) { 402 mbox_cmd->payload_in = vmemdup_user(u64_to_user_ptr(in_payload), 403 in_size); 404 if (IS_ERR(mbox_cmd->payload_in)) 405 return PTR_ERR(mbox_cmd->payload_in); 406 407 if (!cxl_payload_from_user_allowed(opcode, mbox_cmd->payload_in, 408 in_size)) { 409 dev_dbg(cxl_mbox->host, "%s: input payload not allowed\n", 410 cxl_mem_opcode_to_name(opcode)); 411 kvfree(mbox_cmd->payload_in); 412 return -EBUSY; 413 } 414 } 415 416 mbox_cmd->size_out = min_t(size_t, out_size, cxl_mbox->payload_size); 417 418 if (mbox_cmd->size_out) { 419 mbox_cmd->payload_out = kvzalloc(mbox_cmd->size_out, GFP_KERNEL); 420 if (!mbox_cmd->payload_out) { 421 kvfree(mbox_cmd->payload_in); 422 return -ENOMEM; 423 } 424 } 425 return 0; 426 } 427 428 static void cxl_mbox_cmd_dtor(struct cxl_mbox_cmd *mbox) 429 { 430 kvfree(mbox->payload_in); 431 kvfree(mbox->payload_out); 432 } 433 434 static int cxl_to_mem_cmd_raw(struct cxl_mem_command *mem_cmd, 435 const struct cxl_send_command *send_cmd, 436 struct cxl_mailbox *cxl_mbox) 437 { 438 if (send_cmd->raw.rsvd) 439 return -EINVAL; 440 441 /* 442 * Unlike supported commands, the output size of RAW commands 443 * gets passed along without further checking, so it must be 444 * validated here. 445 */ 446 if (send_cmd->out.size > cxl_mbox->payload_size) 447 return -EINVAL; 448 449 if (!cxl_mem_raw_command_allowed(send_cmd->raw.opcode)) 450 return -EPERM; 451 452 dev_WARN_ONCE(cxl_mbox->host, true, "raw command path used\n"); 453 454 *mem_cmd = (struct cxl_mem_command) { 455 .info = { 456 .id = CXL_MEM_COMMAND_ID_RAW, 457 .size_in = send_cmd->in.size, 458 .size_out = send_cmd->out.size, 459 }, 460 .opcode = send_cmd->raw.opcode 461 }; 462 463 return 0; 464 } 465 466 static int cxl_to_mem_cmd(struct cxl_mem_command *mem_cmd, 467 const struct cxl_send_command *send_cmd, 468 struct cxl_mailbox *cxl_mbox) 469 { 470 struct cxl_mem_command *c = &cxl_mem_commands[send_cmd->id]; 471 const struct cxl_command_info *info = &c->info; 472 473 if (send_cmd->flags & ~CXL_MEM_COMMAND_FLAG_MASK) 474 return -EINVAL; 475 476 if (send_cmd->rsvd) 477 return -EINVAL; 478 479 if (send_cmd->in.rsvd || send_cmd->out.rsvd) 480 return -EINVAL; 481 482 /* Check that the command is enabled for hardware */ 483 if (!test_bit(info->id, cxl_mbox->enabled_cmds)) 484 return -ENOTTY; 485 486 /* Check that the command is not claimed for exclusive kernel use */ 487 if (test_bit(info->id, cxl_mbox->exclusive_cmds)) 488 return -EBUSY; 489 490 /* Check the input buffer is the expected size */ 491 if ((info->size_in != CXL_VARIABLE_PAYLOAD) && 492 (info->size_in != send_cmd->in.size)) 493 return -ENOMEM; 494 495 /* Check the output buffer is at least large enough */ 496 if ((info->size_out != CXL_VARIABLE_PAYLOAD) && 497 (send_cmd->out.size < info->size_out)) 498 return -ENOMEM; 499 500 *mem_cmd = (struct cxl_mem_command) { 501 .info = { 502 .id = info->id, 503 .flags = info->flags, 504 .size_in = send_cmd->in.size, 505 .size_out = send_cmd->out.size, 506 }, 507 .opcode = c->opcode 508 }; 509 510 return 0; 511 } 512 513 /** 514 * cxl_validate_cmd_from_user() - Check fields for CXL_MEM_SEND_COMMAND. 515 * @mbox_cmd: Sanitized and populated &struct cxl_mbox_cmd. 516 * @cxl_mbox: CXL mailbox context 517 * @send_cmd: &struct cxl_send_command copied in from userspace. 518 * 519 * Return: 520 * * %0 - @out_cmd is ready to send. 521 * * %-ENOTTY - Invalid command specified. 522 * * %-EINVAL - Reserved fields or invalid values were used. 523 * * %-ENOMEM - Input or output buffer wasn't sized properly. 524 * * %-EPERM - Attempted to use a protected command. 525 * * %-EBUSY - Kernel has claimed exclusive access to this opcode 526 * 527 * The result of this command is a fully validated command in @mbox_cmd that is 528 * safe to send to the hardware. 529 */ 530 static int cxl_validate_cmd_from_user(struct cxl_mbox_cmd *mbox_cmd, 531 struct cxl_mailbox *cxl_mbox, 532 const struct cxl_send_command *send_cmd) 533 { 534 struct cxl_mem_command mem_cmd; 535 int rc; 536 537 if (send_cmd->id == 0 || send_cmd->id >= CXL_MEM_COMMAND_ID_MAX) 538 return -ENOTTY; 539 540 /* 541 * The user can never specify an input payload larger than what hardware 542 * supports, but output can be arbitrarily large (simply write out as 543 * much data as the hardware provides). 544 */ 545 if (send_cmd->in.size > cxl_mbox->payload_size) 546 return -EINVAL; 547 548 /* Sanitize and construct a cxl_mem_command */ 549 if (send_cmd->id == CXL_MEM_COMMAND_ID_RAW) 550 rc = cxl_to_mem_cmd_raw(&mem_cmd, send_cmd, cxl_mbox); 551 else 552 rc = cxl_to_mem_cmd(&mem_cmd, send_cmd, cxl_mbox); 553 554 if (rc) 555 return rc; 556 557 /* Sanitize and construct a cxl_mbox_cmd */ 558 return cxl_mbox_cmd_ctor(mbox_cmd, cxl_mbox, mem_cmd.opcode, 559 mem_cmd.info.size_in, mem_cmd.info.size_out, 560 send_cmd->in.payload); 561 } 562 563 int cxl_query_cmd(struct cxl_mailbox *cxl_mbox, 564 struct cxl_mem_query_commands __user *q) 565 { 566 struct device *dev = cxl_mbox->host; 567 struct cxl_mem_command *cmd; 568 u32 n_commands; 569 int j = 0; 570 571 dev_dbg(dev, "Query IOCTL\n"); 572 573 if (get_user(n_commands, &q->n_commands)) 574 return -EFAULT; 575 576 /* returns the total number if 0 elements are requested. */ 577 if (n_commands == 0) 578 return put_user(ARRAY_SIZE(cxl_mem_commands), &q->n_commands); 579 580 /* 581 * otherwise, return min(n_commands, total commands) cxl_command_info 582 * structures. 583 */ 584 cxl_for_each_cmd(cmd) { 585 struct cxl_command_info info = cmd->info; 586 587 if (test_bit(info.id, cxl_mbox->enabled_cmds)) 588 info.flags |= CXL_MEM_COMMAND_FLAG_ENABLED; 589 if (test_bit(info.id, cxl_mbox->exclusive_cmds)) 590 info.flags |= CXL_MEM_COMMAND_FLAG_EXCLUSIVE; 591 592 if (copy_to_user(&q->commands[j++], &info, sizeof(info))) 593 return -EFAULT; 594 595 if (j == n_commands) 596 break; 597 } 598 599 return 0; 600 } 601 602 /** 603 * handle_mailbox_cmd_from_user() - Dispatch a mailbox command for userspace. 604 * @cxl_mbox: The mailbox context for the operation. 605 * @mbox_cmd: The validated mailbox command. 606 * @out_payload: Pointer to userspace's output payload. 607 * @size_out: (Input) Max payload size to copy out. 608 * (Output) Payload size hardware generated. 609 * @retval: Hardware generated return code from the operation. 610 * 611 * Return: 612 * * %0 - Mailbox transaction succeeded. This implies the mailbox 613 * protocol completed successfully not that the operation itself 614 * was successful. 615 * * %-ENOMEM - Couldn't allocate a bounce buffer. 616 * * %-EFAULT - Something happened with copy_to/from_user. 617 * * %-EINTR - Mailbox acquisition interrupted. 618 * * %-EXXX - Transaction level failures. 619 * 620 * Dispatches a mailbox command on behalf of a userspace request. 621 * The output payload is copied to userspace. 622 * 623 * See cxl_send_cmd(). 624 */ 625 static int handle_mailbox_cmd_from_user(struct cxl_mailbox *cxl_mbox, 626 struct cxl_mbox_cmd *mbox_cmd, 627 u64 out_payload, s32 *size_out, 628 u32 *retval) 629 { 630 struct device *dev = cxl_mbox->host; 631 int rc; 632 633 dev_dbg(dev, 634 "Submitting %s command for user\n" 635 "\topcode: %x\n" 636 "\tsize: %zx\n", 637 cxl_mem_opcode_to_name(mbox_cmd->opcode), 638 mbox_cmd->opcode, mbox_cmd->size_in); 639 640 rc = cxl_mbox->mbox_send(cxl_mbox, mbox_cmd); 641 if (rc) 642 goto out; 643 644 /* 645 * @size_out contains the max size that's allowed to be written back out 646 * to userspace. While the payload may have written more output than 647 * this it will have to be ignored. 648 */ 649 if (mbox_cmd->size_out) { 650 dev_WARN_ONCE(dev, mbox_cmd->size_out > *size_out, 651 "Invalid return size\n"); 652 if (copy_to_user(u64_to_user_ptr(out_payload), 653 mbox_cmd->payload_out, mbox_cmd->size_out)) { 654 rc = -EFAULT; 655 goto out; 656 } 657 } 658 659 *size_out = mbox_cmd->size_out; 660 *retval = mbox_cmd->return_code; 661 662 out: 663 cxl_mbox_cmd_dtor(mbox_cmd); 664 return rc; 665 } 666 667 int cxl_send_cmd(struct cxl_mailbox *cxl_mbox, struct cxl_send_command __user *s) 668 { 669 struct device *dev = cxl_mbox->host; 670 struct cxl_send_command send; 671 struct cxl_mbox_cmd mbox_cmd; 672 int rc; 673 674 dev_dbg(dev, "Send IOCTL\n"); 675 676 if (copy_from_user(&send, s, sizeof(send))) 677 return -EFAULT; 678 679 rc = cxl_validate_cmd_from_user(&mbox_cmd, cxl_mbox, &send); 680 if (rc) 681 return rc; 682 683 rc = handle_mailbox_cmd_from_user(cxl_mbox, &mbox_cmd, send.out.payload, 684 &send.out.size, &send.retval); 685 if (rc) 686 return rc; 687 688 if (copy_to_user(s, &send, sizeof(send))) 689 return -EFAULT; 690 691 return 0; 692 } 693 694 static int cxl_xfer_log(struct cxl_memdev_state *mds, uuid_t *uuid, 695 u32 *size, u8 *out) 696 { 697 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 698 u32 remaining = *size; 699 u32 offset = 0; 700 701 while (remaining) { 702 u32 xfer_size = min_t(u32, remaining, cxl_mbox->payload_size); 703 struct cxl_mbox_cmd mbox_cmd; 704 struct cxl_mbox_get_log log; 705 int rc; 706 707 log = (struct cxl_mbox_get_log) { 708 .uuid = *uuid, 709 .offset = cpu_to_le32(offset), 710 .length = cpu_to_le32(xfer_size), 711 }; 712 713 mbox_cmd = (struct cxl_mbox_cmd) { 714 .opcode = CXL_MBOX_OP_GET_LOG, 715 .size_in = sizeof(log), 716 .payload_in = &log, 717 .size_out = xfer_size, 718 .payload_out = out, 719 }; 720 721 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 722 723 /* 724 * The output payload length that indicates the number 725 * of valid bytes can be smaller than the Log buffer 726 * size. 727 */ 728 if (rc == -EIO && mbox_cmd.size_out < xfer_size) { 729 offset += mbox_cmd.size_out; 730 break; 731 } 732 733 if (rc < 0) 734 return rc; 735 736 out += xfer_size; 737 remaining -= xfer_size; 738 offset += xfer_size; 739 } 740 741 *size = offset; 742 743 return 0; 744 } 745 746 static int check_features_opcodes(u16 opcode, int *ro_cmds, int *wr_cmds) 747 { 748 switch (opcode) { 749 case CXL_MBOX_OP_GET_SUPPORTED_FEATURES: 750 case CXL_MBOX_OP_GET_FEATURE: 751 (*ro_cmds)++; 752 return 1; 753 case CXL_MBOX_OP_SET_FEATURE: 754 (*wr_cmds)++; 755 return 1; 756 default: 757 return 0; 758 } 759 } 760 761 /* 'Get Supported Features' and 'Get Feature' */ 762 #define MAX_FEATURES_READ_CMDS 2 763 static void set_features_cap(struct cxl_mailbox *cxl_mbox, 764 int ro_cmds, int wr_cmds) 765 { 766 /* Setting up Features capability while walking the CEL */ 767 if (ro_cmds == MAX_FEATURES_READ_CMDS) { 768 if (wr_cmds) 769 cxl_mbox->feat_cap = CXL_FEATURES_RW; 770 else 771 cxl_mbox->feat_cap = CXL_FEATURES_RO; 772 } 773 } 774 775 /** 776 * cxl_walk_cel() - Walk through the Command Effects Log. 777 * @mds: The driver data for the operation 778 * @size: Length of the Command Effects Log. 779 * @cel: CEL 780 * 781 * Iterate over each entry in the CEL and determine if the driver supports the 782 * command. If so, the command is enabled for the device and can be used later. 783 */ 784 static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel) 785 { 786 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 787 struct cxl_cel_entry *cel_entry; 788 const int cel_entries = size / sizeof(*cel_entry); 789 DECLARE_BITMAP(dcd_cmds, CXL_DCD_ENABLED_MAX) = {}; 790 struct device *dev = mds->cxlds.dev; 791 int i, ro_cmds = 0, wr_cmds = 0; 792 793 cel_entry = (struct cxl_cel_entry *) cel; 794 795 for (i = 0; i < cel_entries; i++) { 796 u16 opcode = le16_to_cpu(cel_entry[i].opcode); 797 struct cxl_mem_command *cmd = cxl_mem_find_command(opcode); 798 int enabled = 0; 799 800 if (cmd) { 801 set_bit(cmd->info.id, cxl_mbox->enabled_cmds); 802 enabled++; 803 } 804 805 enabled += check_features_opcodes(opcode, &ro_cmds, 806 &wr_cmds); 807 808 if (cxl_is_poison_command(opcode)) { 809 cxl_set_poison_cmd_enabled(&mds->poison, opcode); 810 enabled++; 811 } 812 813 if (cxl_is_security_command(opcode)) { 814 cxl_set_security_cmd_enabled(&mds->security, opcode); 815 enabled++; 816 } 817 818 if (cxl_is_dcd_command(opcode)) { 819 cxl_set_dcd_cmd_enabled(opcode, dcd_cmds); 820 enabled++; 821 } 822 823 dev_dbg(dev, "Opcode 0x%04x %s\n", opcode, 824 enabled ? "enabled" : "unsupported by driver"); 825 } 826 827 set_features_cap(cxl_mbox, ro_cmds, wr_cmds); 828 /* 829 * Disabled until event handling implemented. 830 */ 831 if (bitmap_full(dcd_cmds, CXL_DCD_ENABLED_MAX)) 832 dev_dbg(dev, "Device supports DCD; capability disabled\n"); 833 mds->dcd_supported = false; 834 } 835 836 static struct cxl_mbox_get_supported_logs *cxl_get_gsl(struct cxl_memdev_state *mds) 837 { 838 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 839 struct cxl_mbox_get_supported_logs *ret; 840 struct cxl_mbox_cmd mbox_cmd; 841 int rc; 842 843 ret = kvmalloc(cxl_mbox->payload_size, GFP_KERNEL); 844 if (!ret) 845 return ERR_PTR(-ENOMEM); 846 847 mbox_cmd = (struct cxl_mbox_cmd) { 848 .opcode = CXL_MBOX_OP_GET_SUPPORTED_LOGS, 849 .size_out = cxl_mbox->payload_size, 850 .payload_out = ret, 851 /* At least the record number field must be valid */ 852 .min_out = 2, 853 }; 854 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 855 if (rc < 0) { 856 kvfree(ret); 857 return ERR_PTR(rc); 858 } 859 860 861 return ret; 862 } 863 864 enum { 865 CEL_UUID, 866 VENDOR_DEBUG_UUID, 867 }; 868 869 /* See CXL 2.0 Table 170. Get Log Input Payload */ 870 static const uuid_t log_uuid[] = { 871 [CEL_UUID] = DEFINE_CXL_CEL_UUID, 872 [VENDOR_DEBUG_UUID] = DEFINE_CXL_VENDOR_DEBUG_UUID, 873 }; 874 875 /** 876 * cxl_enumerate_cmds() - Enumerate commands for a device. 877 * @mds: The driver data for the operation 878 * 879 * Returns 0 if enumerate completed successfully. 880 * 881 * CXL devices have optional support for certain commands. This function will 882 * determine the set of supported commands for the hardware and update the 883 * enabled_cmds bitmap in the @mds. 884 */ 885 int cxl_enumerate_cmds(struct cxl_memdev_state *mds) 886 { 887 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 888 struct cxl_mbox_get_supported_logs *gsl; 889 struct device *dev = mds->cxlds.dev; 890 struct cxl_mem_command *cmd; 891 int i, rc; 892 893 gsl = cxl_get_gsl(mds); 894 if (IS_ERR(gsl)) 895 return PTR_ERR(gsl); 896 897 rc = -ENOENT; 898 for (i = 0; i < le16_to_cpu(gsl->entries); i++) { 899 u32 size = le32_to_cpu(gsl->entry[i].size); 900 uuid_t uuid = gsl->entry[i].uuid; 901 u8 *log; 902 903 dev_dbg(dev, "Found LOG type %pU of size %d", &uuid, size); 904 905 if (!uuid_equal(&uuid, &log_uuid[CEL_UUID])) 906 continue; 907 908 log = kvmalloc(size, GFP_KERNEL); 909 if (!log) { 910 rc = -ENOMEM; 911 goto out; 912 } 913 914 rc = cxl_xfer_log(mds, &uuid, &size, log); 915 if (rc) { 916 kvfree(log); 917 goto out; 918 } 919 920 cxl_walk_cel(mds, size, log); 921 kvfree(log); 922 923 /* In case CEL was bogus, enable some default commands. */ 924 cxl_for_each_cmd(cmd) 925 if (cmd->flags & CXL_CMD_FLAG_FORCE_ENABLE) 926 set_bit(cmd->info.id, cxl_mbox->enabled_cmds); 927 928 /* Found the required CEL */ 929 rc = 0; 930 } 931 out: 932 kvfree(gsl); 933 return rc; 934 } 935 EXPORT_SYMBOL_NS_GPL(cxl_enumerate_cmds, "CXL"); 936 937 void cxl_event_trace_record(struct cxl_memdev *cxlmd, 938 enum cxl_event_log_type type, 939 enum cxl_event_type event_type, 940 const uuid_t *uuid, union cxl_event *evt) 941 { 942 if (event_type == CXL_CPER_EVENT_MEM_MODULE) { 943 trace_cxl_memory_module(cxlmd, type, &evt->mem_module); 944 return; 945 } 946 if (event_type == CXL_CPER_EVENT_GENERIC) { 947 trace_cxl_generic_event(cxlmd, type, uuid, &evt->generic); 948 return; 949 } 950 if (event_type == CXL_CPER_EVENT_MEM_SPARING) { 951 trace_cxl_memory_sparing(cxlmd, type, &evt->mem_sparing); 952 return; 953 } 954 955 if (trace_cxl_general_media_enabled() || trace_cxl_dram_enabled()) { 956 u64 dpa, hpa = ULLONG_MAX, hpa_alias = ULLONG_MAX; 957 struct cxl_region *cxlr; 958 959 /* 960 * These trace points are annotated with HPA and region 961 * translations. Take topology mutation locks and lookup 962 * { HPA, REGION } from { DPA, MEMDEV } in the event record. 963 */ 964 guard(device)(&cxlmd->dev); 965 guard(rwsem_read)(&cxl_rwsem.region); 966 guard(rwsem_read)(&cxl_rwsem.dpa); 967 968 dpa = le64_to_cpu(evt->media_hdr.phys_addr) & CXL_DPA_MASK; 969 cxlr = cxl_dpa_to_region(cxlmd, dpa); 970 if (cxlr) { 971 u64 cache_size = cxlr->params.cache_size; 972 973 hpa = cxl_dpa_to_hpa(cxlr, cxlmd, dpa); 974 if (cache_size) 975 hpa_alias = hpa - cache_size; 976 } 977 978 if (event_type == CXL_CPER_EVENT_GEN_MEDIA) { 979 if (cxl_store_rec_gen_media((struct cxl_memdev *)cxlmd, evt)) 980 dev_dbg(&cxlmd->dev, "CXL store rec_gen_media failed\n"); 981 982 if (evt->gen_media.media_hdr.descriptor & 983 CXL_GMER_EVT_DESC_THRESHOLD_EVENT) 984 WARN_ON_ONCE((evt->gen_media.media_hdr.type == 985 CXL_GMER_MEM_EVT_TYPE_AP_CME_COUNTER_EXPIRE) && 986 !get_unaligned_le24(evt->gen_media.cme_count)); 987 else 988 WARN_ON_ONCE(evt->gen_media.media_hdr.type == 989 CXL_GMER_MEM_EVT_TYPE_AP_CME_COUNTER_EXPIRE); 990 991 trace_cxl_general_media(cxlmd, type, cxlr, hpa, 992 hpa_alias, &evt->gen_media); 993 } else if (event_type == CXL_CPER_EVENT_DRAM) { 994 if (cxl_store_rec_dram((struct cxl_memdev *)cxlmd, evt)) 995 dev_dbg(&cxlmd->dev, "CXL store rec_dram failed\n"); 996 997 if (evt->dram.media_hdr.descriptor & 998 CXL_GMER_EVT_DESC_THRESHOLD_EVENT) 999 WARN_ON_ONCE((evt->dram.media_hdr.type == 1000 CXL_DER_MEM_EVT_TYPE_AP_CME_COUNTER_EXPIRE) && 1001 !get_unaligned_le24(evt->dram.cvme_count)); 1002 else 1003 WARN_ON_ONCE(evt->dram.media_hdr.type == 1004 CXL_DER_MEM_EVT_TYPE_AP_CME_COUNTER_EXPIRE); 1005 1006 trace_cxl_dram(cxlmd, type, cxlr, hpa, hpa_alias, 1007 &evt->dram); 1008 } 1009 } 1010 } 1011 EXPORT_SYMBOL_NS_GPL(cxl_event_trace_record, "CXL"); 1012 1013 static void __cxl_event_trace_record(struct cxl_memdev *cxlmd, 1014 enum cxl_event_log_type type, 1015 struct cxl_event_record_raw *record) 1016 { 1017 enum cxl_event_type ev_type = CXL_CPER_EVENT_GENERIC; 1018 const uuid_t *uuid = &record->id; 1019 1020 if (uuid_equal(uuid, &CXL_EVENT_GEN_MEDIA_UUID)) 1021 ev_type = CXL_CPER_EVENT_GEN_MEDIA; 1022 else if (uuid_equal(uuid, &CXL_EVENT_DRAM_UUID)) 1023 ev_type = CXL_CPER_EVENT_DRAM; 1024 else if (uuid_equal(uuid, &CXL_EVENT_MEM_MODULE_UUID)) 1025 ev_type = CXL_CPER_EVENT_MEM_MODULE; 1026 else if (uuid_equal(uuid, &CXL_EVENT_MEM_SPARING_UUID)) 1027 ev_type = CXL_CPER_EVENT_MEM_SPARING; 1028 1029 cxl_event_trace_record(cxlmd, type, ev_type, uuid, &record->event); 1030 } 1031 1032 static int cxl_clear_event_record(struct cxl_memdev_state *mds, 1033 enum cxl_event_log_type log, 1034 struct cxl_get_event_payload *get_pl) 1035 { 1036 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1037 struct cxl_mbox_clear_event_payload *payload; 1038 u16 total = le16_to_cpu(get_pl->record_count); 1039 u8 max_handles = CXL_CLEAR_EVENT_MAX_HANDLES; 1040 size_t pl_size = struct_size(payload, handles, max_handles); 1041 struct cxl_mbox_cmd mbox_cmd; 1042 u16 cnt; 1043 int rc = 0; 1044 int i; 1045 1046 /* Payload size may limit the max handles */ 1047 if (pl_size > cxl_mbox->payload_size) { 1048 max_handles = (cxl_mbox->payload_size - sizeof(*payload)) / 1049 sizeof(__le16); 1050 pl_size = struct_size(payload, handles, max_handles); 1051 } 1052 1053 payload = kvzalloc(pl_size, GFP_KERNEL); 1054 if (!payload) 1055 return -ENOMEM; 1056 1057 *payload = (struct cxl_mbox_clear_event_payload) { 1058 .event_log = log, 1059 }; 1060 1061 mbox_cmd = (struct cxl_mbox_cmd) { 1062 .opcode = CXL_MBOX_OP_CLEAR_EVENT_RECORD, 1063 .payload_in = payload, 1064 .size_in = pl_size, 1065 }; 1066 1067 /* 1068 * Clear Event Records uses u8 for the handle cnt while Get Event 1069 * Record can return up to 0xffff records. 1070 */ 1071 i = 0; 1072 for (cnt = 0; cnt < total; cnt++) { 1073 struct cxl_event_record_raw *raw = &get_pl->records[cnt]; 1074 struct cxl_event_generic *gen = &raw->event.generic; 1075 1076 payload->handles[i++] = gen->hdr.handle; 1077 dev_dbg(mds->cxlds.dev, "Event log '%d': Clearing %u\n", log, 1078 le16_to_cpu(payload->handles[i - 1])); 1079 1080 if (i == max_handles) { 1081 payload->nr_recs = i; 1082 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1083 if (rc) 1084 goto free_pl; 1085 i = 0; 1086 } 1087 } 1088 1089 /* Clear what is left if any */ 1090 if (i) { 1091 payload->nr_recs = i; 1092 mbox_cmd.size_in = struct_size(payload, handles, i); 1093 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1094 if (rc) 1095 goto free_pl; 1096 } 1097 1098 free_pl: 1099 kvfree(payload); 1100 return rc; 1101 } 1102 1103 static void cxl_mem_get_records_log(struct cxl_memdev_state *mds, 1104 enum cxl_event_log_type type) 1105 { 1106 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1107 struct cxl_memdev *cxlmd = mds->cxlds.cxlmd; 1108 struct device *dev = mds->cxlds.dev; 1109 struct cxl_get_event_payload *payload; 1110 u8 log_type = type; 1111 u16 nr_rec; 1112 1113 mutex_lock(&mds->event.log_lock); 1114 payload = mds->event.buf; 1115 1116 do { 1117 int rc, i; 1118 struct cxl_mbox_cmd mbox_cmd = (struct cxl_mbox_cmd) { 1119 .opcode = CXL_MBOX_OP_GET_EVENT_RECORD, 1120 .payload_in = &log_type, 1121 .size_in = sizeof(log_type), 1122 .payload_out = payload, 1123 .size_out = cxl_mbox->payload_size, 1124 .min_out = struct_size(payload, records, 0), 1125 }; 1126 1127 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1128 if (rc) { 1129 dev_err_ratelimited(dev, 1130 "Event log '%d': Failed to query event records : %d", 1131 type, rc); 1132 break; 1133 } 1134 1135 nr_rec = le16_to_cpu(payload->record_count); 1136 if (!nr_rec) 1137 break; 1138 1139 for (i = 0; i < nr_rec; i++) 1140 __cxl_event_trace_record(cxlmd, type, 1141 &payload->records[i]); 1142 1143 if (payload->flags & CXL_GET_EVENT_FLAG_OVERFLOW) 1144 trace_cxl_overflow(cxlmd, type, payload); 1145 1146 rc = cxl_clear_event_record(mds, type, payload); 1147 if (rc) { 1148 dev_err_ratelimited(dev, 1149 "Event log '%d': Failed to clear events : %d", 1150 type, rc); 1151 break; 1152 } 1153 } while (nr_rec); 1154 1155 mutex_unlock(&mds->event.log_lock); 1156 } 1157 1158 /** 1159 * cxl_mem_get_event_records - Get Event Records from the device 1160 * @mds: The driver data for the operation 1161 * @status: Event Status register value identifying which events are available. 1162 * 1163 * Retrieve all event records available on the device, report them as trace 1164 * events, and clear them. 1165 * 1166 * See CXL rev 3.0 @8.2.9.2.2 Get Event Records 1167 * See CXL rev 3.0 @8.2.9.2.3 Clear Event Records 1168 */ 1169 void cxl_mem_get_event_records(struct cxl_memdev_state *mds, u32 status) 1170 { 1171 dev_dbg(mds->cxlds.dev, "Reading event logs: %x\n", status); 1172 1173 if (status & CXLDEV_EVENT_STATUS_FATAL) 1174 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FATAL); 1175 if (status & CXLDEV_EVENT_STATUS_FAIL) 1176 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_FAIL); 1177 if (status & CXLDEV_EVENT_STATUS_WARN) 1178 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_WARN); 1179 if (status & CXLDEV_EVENT_STATUS_INFO) 1180 cxl_mem_get_records_log(mds, CXL_EVENT_TYPE_INFO); 1181 } 1182 EXPORT_SYMBOL_NS_GPL(cxl_mem_get_event_records, "CXL"); 1183 1184 /** 1185 * cxl_mem_get_partition_info - Get partition info 1186 * @mds: The driver data for the operation 1187 * 1188 * Retrieve the current partition info for the device specified. The active 1189 * values are the current capacity in bytes. If not 0, the 'next' values are 1190 * the pending values, in bytes, which take affect on next cold reset. 1191 * 1192 * Return: 0 if no error: or the result of the mailbox command. 1193 * 1194 * See CXL @8.2.9.5.2.1 Get Partition Info 1195 */ 1196 int cxl_mem_get_partition_info(struct cxl_memdev_state *mds) 1197 { 1198 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1199 struct cxl_mbox_get_partition_info pi; 1200 struct cxl_mbox_cmd mbox_cmd; 1201 int rc; 1202 1203 mbox_cmd = (struct cxl_mbox_cmd) { 1204 .opcode = CXL_MBOX_OP_GET_PARTITION_INFO, 1205 .size_out = sizeof(pi), 1206 .payload_out = &pi, 1207 }; 1208 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1209 if (rc) 1210 return rc; 1211 1212 mds->active_volatile_bytes = 1213 le64_to_cpu(pi.active_volatile_cap) * CXL_CAPACITY_MULTIPLIER; 1214 mds->active_persistent_bytes = 1215 le64_to_cpu(pi.active_persistent_cap) * CXL_CAPACITY_MULTIPLIER; 1216 1217 return 0; 1218 } 1219 1220 /** 1221 * cxl_dev_state_identify() - Send the IDENTIFY command to the device. 1222 * @mds: The driver data for the operation 1223 * 1224 * Return: 0 if identify was executed successfully or media not ready. 1225 * 1226 * This will dispatch the identify command to the device and on success populate 1227 * structures to be exported to sysfs. 1228 */ 1229 int cxl_dev_state_identify(struct cxl_memdev_state *mds) 1230 { 1231 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1232 /* See CXL 2.0 Table 175 Identify Memory Device Output Payload */ 1233 struct cxl_mbox_identify id; 1234 struct cxl_mbox_cmd mbox_cmd; 1235 u32 val; 1236 int rc; 1237 1238 if (!mds->cxlds.media_ready) 1239 return 0; 1240 1241 mbox_cmd = (struct cxl_mbox_cmd) { 1242 .opcode = CXL_MBOX_OP_IDENTIFY, 1243 .size_out = sizeof(id), 1244 .payload_out = &id, 1245 }; 1246 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1247 if (rc < 0) 1248 return rc; 1249 1250 mds->total_bytes = 1251 le64_to_cpu(id.total_capacity) * CXL_CAPACITY_MULTIPLIER; 1252 mds->volatile_only_bytes = 1253 le64_to_cpu(id.volatile_capacity) * CXL_CAPACITY_MULTIPLIER; 1254 mds->persistent_only_bytes = 1255 le64_to_cpu(id.persistent_capacity) * CXL_CAPACITY_MULTIPLIER; 1256 mds->partition_align_bytes = 1257 le64_to_cpu(id.partition_align) * CXL_CAPACITY_MULTIPLIER; 1258 1259 mds->lsa_size = le32_to_cpu(id.lsa_size); 1260 memcpy(mds->firmware_version, id.fw_revision, 1261 sizeof(id.fw_revision)); 1262 1263 if (test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds)) { 1264 val = get_unaligned_le24(id.poison_list_max_mer); 1265 mds->poison.max_errors = min_t(u32, val, CXL_POISON_LIST_MAX); 1266 } 1267 1268 return 0; 1269 } 1270 EXPORT_SYMBOL_NS_GPL(cxl_dev_state_identify, "CXL"); 1271 1272 static int __cxl_mem_sanitize(struct cxl_memdev_state *mds, u16 cmd) 1273 { 1274 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1275 int rc; 1276 u32 sec_out = 0; 1277 struct cxl_get_security_output { 1278 __le32 flags; 1279 } out; 1280 struct cxl_mbox_cmd sec_cmd = { 1281 .opcode = CXL_MBOX_OP_GET_SECURITY_STATE, 1282 .payload_out = &out, 1283 .size_out = sizeof(out), 1284 }; 1285 struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd }; 1286 1287 if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE) 1288 return -EINVAL; 1289 1290 rc = cxl_internal_send_cmd(cxl_mbox, &sec_cmd); 1291 if (rc < 0) { 1292 dev_err(cxl_mbox->host, "Failed to get security state : %d", rc); 1293 return rc; 1294 } 1295 1296 /* 1297 * Prior to using these commands, any security applied to 1298 * the user data areas of the device shall be DISABLED (or 1299 * UNLOCKED for secure erase case). 1300 */ 1301 sec_out = le32_to_cpu(out.flags); 1302 if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET) 1303 return -EINVAL; 1304 1305 if (cmd == CXL_MBOX_OP_SECURE_ERASE && 1306 sec_out & CXL_PMEM_SEC_STATE_LOCKED) 1307 return -EINVAL; 1308 1309 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1310 if (rc < 0) { 1311 dev_err(cxl_mbox->host, "Failed to sanitize device : %d", rc); 1312 return rc; 1313 } 1314 1315 return 0; 1316 } 1317 1318 1319 /** 1320 * cxl_mem_sanitize() - Send a sanitization command to the device. 1321 * @cxlmd: The device for the operation 1322 * @cmd: The specific sanitization command opcode 1323 * 1324 * Return: 0 if the command was executed successfully, regardless of 1325 * whether or not the actual security operation is done in the background, 1326 * such as for the Sanitize case. 1327 * Error return values can be the result of the mailbox command, -EINVAL 1328 * when security requirements are not met or invalid contexts, or -EBUSY 1329 * if the sanitize operation is already in flight. 1330 * 1331 * See CXL 3.0 @8.2.9.8.5.1 Sanitize and @8.2.9.8.5.2 Secure Erase. 1332 */ 1333 int cxl_mem_sanitize(struct cxl_memdev *cxlmd, u16 cmd) 1334 { 1335 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 1336 struct cxl_port *endpoint; 1337 1338 /* synchronize with cxl_mem_probe() and decoder write operations */ 1339 guard(device)(&cxlmd->dev); 1340 endpoint = cxlmd->endpoint; 1341 guard(rwsem_read)(&cxl_rwsem.region); 1342 /* 1343 * Require an endpoint to be safe otherwise the driver can not 1344 * be sure that the device is unmapped. 1345 */ 1346 if (cxlmd->dev.driver && cxl_num_decoders_committed(endpoint) == 0) 1347 return __cxl_mem_sanitize(mds, cmd); 1348 1349 return -EBUSY; 1350 } 1351 1352 /* 1353 * A partition unavailable for Dynamic Capacity reports zeros for decode_length, 1354 * length, and block_size. 1355 */ 1356 static bool cxl_dc_partition_unavailable(struct cxl_dc_partition *dev_part) 1357 { 1358 return !le64_to_cpu(dev_part->decode_length) && 1359 !le64_to_cpu(dev_part->length) && 1360 !le64_to_cpu(dev_part->block_size); 1361 } 1362 1363 static int cxl_dc_check(struct device *dev, struct cxl_dc_partition_info *part, 1364 struct cxl_dc_partition *dev_part) 1365 { 1366 u64 decode_len = le64_to_cpu(dev_part->decode_length); 1367 u64 blk_size = le64_to_cpu(dev_part->block_size); 1368 u64 len = le64_to_cpu(dev_part->length); 1369 u32 handle = le32_to_cpu(dev_part->dsmad_handle); 1370 u64 size; 1371 1372 /* 1373 * Not an error; leave the entry empty. A partially zeroed partition 1374 * is rejected by the checks below. CXL r4.0 Table 8-347. 1375 */ 1376 if (cxl_dc_partition_unavailable(dev_part)) { 1377 *part = (struct cxl_dc_partition_info) { }; 1378 dev_dbg(dev, "Partition 0 unavailable for DC\n"); 1379 return 0; 1380 } 1381 1382 /* The CDAT DSMAD handle this refers to is 8 bits */ 1383 if (handle > U8_MAX) { 1384 dev_warn(dev, "DSMAD handle 0x%x exceeds the 8 bit CDAT DSMAD handle\n", 1385 handle); 1386 return -EINVAL; 1387 } 1388 1389 /* 1390 * Decode length is a count of 256M units, so a device reporting 2^36 1391 * or more wraps. 1392 */ 1393 if (check_mul_overflow(decode_len, (u64)CXL_CAPACITY_MULTIPLIER, &size)) { 1394 dev_err(dev, "DC partition 0 decode length %#llx overflows\n", 1395 decode_len); 1396 return -EINVAL; 1397 } 1398 1399 *part = (struct cxl_dc_partition_info) { 1400 .start = le64_to_cpu(dev_part->base), 1401 .size = size, 1402 .handle = handle, 1403 }; 1404 1405 /* 1406 * Block size is a power of 2 and a multiple of 40h. is_power_of_2() 1407 * takes an unsigned long, which truncates blk_size on 32 bit. 1408 */ 1409 if (blk_size == 0 || (blk_size & (blk_size - 1)) || 1410 blk_size % CXL_DCD_BLOCK_LINE_SIZE) { 1411 dev_err(dev, "DC partition 0 invalid block size %#llx\n", blk_size); 1412 return -EINVAL; 1413 } 1414 1415 if (part->size == 0) { 1416 dev_err(dev, "DC partition 0 zero decode length\n"); 1417 return -EINVAL; 1418 } 1419 1420 if (len == 0) { 1421 dev_err(dev, "DC partition 0 zero length\n"); 1422 return -EINVAL; 1423 } 1424 1425 if (len > part->size) { 1426 dev_err(dev, "DC partition 0 length %#llx exceeds decode length %#llx\n", 1427 len, part->size); 1428 return -EINVAL; 1429 } 1430 1431 if (!IS_ALIGNED(len, blk_size)) { 1432 dev_err(dev, "DC partition 0 length %#llx not a multiple of block size %#llx\n", 1433 len, blk_size); 1434 return -EINVAL; 1435 } 1436 1437 if (!IS_ALIGNED(part->start, SZ_256M)) { 1438 dev_err(dev, "DC partition 0 base %#llx not aligned to 256M\n", 1439 part->start); 1440 return -EINVAL; 1441 } 1442 1443 dev_dbg(dev, "DC partition 0 start %#llx size %#llx blk_size: %#llx\n", 1444 part->start, part->size, blk_size); 1445 1446 return 0; 1447 } 1448 1449 /* Returns the number of partitions in dc_resp or -ERRNO */ 1450 static int cxl_get_dc_config(struct cxl_mailbox *mbox, u8 start_partition, 1451 u8 partition_count, 1452 struct cxl_mbox_get_dc_config_out *dc_resp, 1453 size_t dc_resp_size) 1454 { 1455 struct cxl_mbox_get_dc_config_in get_dc = (struct cxl_mbox_get_dc_config_in) { 1456 .partition_count = partition_count, 1457 .start_partition_index = start_partition, 1458 }; 1459 struct cxl_mbox_cmd mbox_cmd = (struct cxl_mbox_cmd) { 1460 .opcode = CXL_MBOX_OP_GET_DC_CONFIG, 1461 .payload_in = &get_dc, 1462 .size_in = sizeof(get_dc), 1463 .size_out = dc_resp_size, 1464 .payload_out = dc_resp, 1465 /* The device must return at least the fixed header */ 1466 .min_out = sizeof(*dc_resp), 1467 }; 1468 size_t expected_sz; 1469 int rc; 1470 1471 rc = cxl_internal_send_cmd(mbox, &mbox_cmd); 1472 if (rc < 0) 1473 return rc; 1474 1475 if (dc_resp->partitions_returned > partition_count) { 1476 dev_err(mbox->host, "Device returned %u partitions, requested %u\n", 1477 dc_resp->partitions_returned, partition_count); 1478 return -EIO; 1479 } 1480 1481 /* The trailing extent/tag counts (CXL r4.0 Table 8-346) are not read */ 1482 expected_sz = struct_size(dc_resp, partition, 1483 dc_resp->partitions_returned); 1484 1485 if (mbox_cmd.size_out < expected_sz) { 1486 dev_err(mbox->host, 1487 "Payload size %zu less than expected %zu for %u partitions\n", 1488 mbox_cmd.size_out, expected_sz, 1489 dc_resp->partitions_returned); 1490 return -EIO; 1491 } 1492 1493 dev_dbg(mbox->host, "Read %d/%d DC partitions\n", 1494 dc_resp->partitions_returned, dc_resp->avail_partition_count); 1495 return dc_resp->partitions_returned; 1496 } 1497 1498 /** 1499 * cxl_dev_dc_identify() - Reads the dynamic capacity information from the 1500 * device. 1501 * @mbox: Mailbox to query 1502 * @dc_info: The dynamic partition information to return 1503 * 1504 * Read every partition the device reports, but validate only the first: 1505 * Linux maps partition 0 and nothing else, so a defect in capacity the 1506 * driver never touches is not a reason to refuse the device dynamic 1507 * capacity. The remaining partitions are read but not recorded. 1508 * 1509 * Return: 0 if identify was executed successfully, -ERRNO on error. 1510 * On error, @dc_info is left unchanged. 1511 */ 1512 int cxl_dev_dc_identify(struct cxl_mailbox *mbox, 1513 struct cxl_dc_partition_info *dc_info) 1514 { 1515 struct cxl_dc_partition_info partitions[CXL_MAX_DC_PARTITIONS] = { }; 1516 struct device *dev = mbox->host; 1517 u8 avail_partitions = 0; 1518 u8 num_partitions; 1519 u8 partition_count; 1520 size_t dc_resp_size; 1521 1522 /* 1523 * Bound requested number of partitions by mailbox payload size. The 1524 * 256 byte spec minimum, verified in cxl_pci_setup_mailbox(), keeps 1525 * the subtraction below from underflowing. 1526 */ 1527 partition_count = min(CXL_MAX_DC_PARTITIONS, 1528 (mbox->payload_size - 1529 sizeof(struct cxl_mbox_get_dc_config_out) - 1530 sizeof(struct cxl_mbox_get_dc_config_tail)) / 1531 sizeof(struct cxl_dc_partition)); 1532 dc_resp_size = struct_size_t(struct cxl_mbox_get_dc_config_out, 1533 partition, partition_count) + 1534 sizeof(struct cxl_mbox_get_dc_config_tail); 1535 1536 struct cxl_mbox_get_dc_config_out *dc_resp __free(kfree) = 1537 kmalloc(dc_resp_size, GFP_KERNEL); 1538 if (!dc_resp) 1539 return -ENOMEM; 1540 1541 num_partitions = 0; 1542 do { 1543 int returned, rc; 1544 1545 returned = cxl_get_dc_config(mbox, num_partitions, 1546 partition_count, dc_resp, 1547 dc_resp_size); 1548 if (returned < 0) { 1549 dev_err(dev, "Failed to get DC config: %d\n", returned); 1550 return returned; 1551 } 1552 1553 if (returned == 0) { 1554 dev_err(dev, 1555 "Device reported %u partitions available but returned none at index %u\n", 1556 dc_resp->avail_partition_count, num_partitions); 1557 return -EIO; 1558 } 1559 1560 /* A DCD reports at least one partition (CXL r4.0 Table 8-346) */ 1561 if (dc_resp->avail_partition_count == 0) { 1562 dev_err(dev, "Device reported no DC partitions\n"); 1563 return -EIO; 1564 } 1565 1566 avail_partitions = min(dc_resp->avail_partition_count, 1567 CXL_MAX_DC_PARTITIONS); 1568 1569 /* 1570 * Only partition 0 is mapped, so it is the only one checked. 1571 * The rest are read to complete the enumeration and dropped. 1572 */ 1573 if (num_partitions == 0) { 1574 rc = cxl_dc_check(dev, &partitions[0], 1575 &dc_resp->partition[0]); 1576 if (rc) 1577 return rc; 1578 } 1579 1580 num_partitions += returned; 1581 1582 } while (num_partitions < avail_partitions); 1583 1584 /* Linux only supports the 1st partition; nothing to do if it is unavailable */ 1585 if (partitions[0].size == 0) 1586 return -ENODEV; 1587 1588 *dc_info = partitions[0]; 1589 dev_dbg(dev, "Returning partition 0 %#llx size %#llx\n", 1590 dc_info->start, dc_info->size); 1591 1592 return 0; 1593 } 1594 EXPORT_SYMBOL_NS_GPL(cxl_dev_dc_identify, "CXL"); 1595 1596 int cxl_get_dirty_count(struct cxl_memdev_state *mds, u32 *count) 1597 { 1598 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1599 struct cxl_mbox_get_health_info_out hi; 1600 struct cxl_mbox_cmd mbox_cmd; 1601 int rc; 1602 1603 mbox_cmd = (struct cxl_mbox_cmd) { 1604 .opcode = CXL_MBOX_OP_GET_HEALTH_INFO, 1605 .size_out = sizeof(hi), 1606 .payload_out = &hi, 1607 }; 1608 1609 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1610 if (!rc) 1611 *count = le32_to_cpu(hi.dirty_shutdown_cnt); 1612 1613 return rc; 1614 } 1615 EXPORT_SYMBOL_NS_GPL(cxl_get_dirty_count, "CXL"); 1616 1617 int cxl_arm_dirty_shutdown(struct cxl_memdev_state *mds) 1618 { 1619 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1620 struct cxl_mbox_cmd mbox_cmd; 1621 struct cxl_mbox_set_shutdown_state_in in = { 1622 .state = 1 1623 }; 1624 1625 mbox_cmd = (struct cxl_mbox_cmd) { 1626 .opcode = CXL_MBOX_OP_SET_SHUTDOWN_STATE, 1627 .size_in = sizeof(in), 1628 .payload_in = &in, 1629 }; 1630 1631 return cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1632 } 1633 EXPORT_SYMBOL_NS_GPL(cxl_arm_dirty_shutdown, "CXL"); 1634 1635 int cxl_set_timestamp(struct cxl_memdev_state *mds) 1636 { 1637 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1638 struct cxl_mbox_cmd mbox_cmd; 1639 struct cxl_mbox_set_timestamp_in pi; 1640 int rc; 1641 1642 pi.timestamp = cpu_to_le64(ktime_get_real_ns()); 1643 mbox_cmd = (struct cxl_mbox_cmd) { 1644 .opcode = CXL_MBOX_OP_SET_TIMESTAMP, 1645 .size_in = sizeof(pi), 1646 .payload_in = &pi, 1647 }; 1648 1649 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1650 /* 1651 * Command is optional. Devices may have another way of providing 1652 * a timestamp, or may return all 0s in timestamp fields. 1653 * Don't report an error if this command isn't supported 1654 */ 1655 if (rc && (mbox_cmd.return_code != CXL_MBOX_CMD_RC_UNSUPPORTED)) 1656 return rc; 1657 1658 return 0; 1659 } 1660 EXPORT_SYMBOL_NS_GPL(cxl_set_timestamp, "CXL"); 1661 1662 int cxl_mem_get_poison(struct cxl_memdev *cxlmd, u64 offset, u64 len, 1663 struct cxl_region *cxlr) 1664 { 1665 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 1666 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 1667 struct cxl_mbox_poison_out *po; 1668 struct cxl_mbox_poison_in pi; 1669 int nr_records = 0; 1670 int rc; 1671 1672 if (!len) 1673 return 0; 1674 1675 ACQUIRE(mutex_intr, lock)(&mds->poison.mutex); 1676 if ((rc = ACQUIRE_ERR(mutex_intr, &lock))) 1677 return rc; 1678 1679 po = mds->poison.list_out; 1680 pi.offset = cpu_to_le64(offset); 1681 pi.length = cpu_to_le64(len / CXL_POISON_LEN_MULT); 1682 1683 do { 1684 struct cxl_mbox_cmd mbox_cmd = (struct cxl_mbox_cmd){ 1685 .opcode = CXL_MBOX_OP_GET_POISON, 1686 .size_in = sizeof(pi), 1687 .payload_in = &pi, 1688 .size_out = cxl_mbox->payload_size, 1689 .payload_out = po, 1690 .min_out = struct_size(po, record, 0), 1691 }; 1692 1693 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1694 if (rc) 1695 break; 1696 1697 if (!le16_to_cpu(po->count)) { 1698 dev_dbg(&cxlmd->dev, "Poison empty payload!\n"); 1699 break; 1700 } 1701 1702 for (int i = 0; i < le16_to_cpu(po->count); i++) 1703 trace_cxl_poison(cxlmd, cxlr, &po->record[i], 1704 po->flags, po->overflow_ts, 1705 CXL_POISON_TRACE_LIST); 1706 1707 /* Protect against an uncleared _FLAG_MORE */ 1708 nr_records = nr_records + le16_to_cpu(po->count); 1709 if (nr_records >= mds->poison.max_errors) { 1710 dev_dbg(&cxlmd->dev, "Max Error Records reached: %d\n", 1711 nr_records); 1712 break; 1713 } 1714 } while (po->flags & CXL_POISON_FLAG_MORE); 1715 1716 return rc; 1717 } 1718 EXPORT_SYMBOL_NS_GPL(cxl_mem_get_poison, "CXL"); 1719 1720 static void free_poison_buf(void *buf) 1721 { 1722 kvfree(buf); 1723 } 1724 1725 /* Get Poison List output buffer is protected by mds->poison.lock */ 1726 static int cxl_poison_alloc_buf(struct cxl_memdev_state *mds) 1727 { 1728 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1729 1730 mds->poison.list_out = kvmalloc(cxl_mbox->payload_size, GFP_KERNEL); 1731 if (!mds->poison.list_out) 1732 return -ENOMEM; 1733 1734 return devm_add_action_or_reset(mds->cxlds.dev, free_poison_buf, 1735 mds->poison.list_out); 1736 } 1737 1738 int cxl_poison_state_init(struct cxl_memdev_state *mds) 1739 { 1740 int rc; 1741 1742 if (!test_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds)) 1743 return 0; 1744 1745 rc = cxl_poison_alloc_buf(mds); 1746 if (rc) { 1747 clear_bit(CXL_POISON_ENABLED_LIST, mds->poison.enabled_cmds); 1748 return rc; 1749 } 1750 1751 mutex_init(&mds->poison.mutex); 1752 return 0; 1753 } 1754 EXPORT_SYMBOL_NS_GPL(cxl_poison_state_init, "CXL"); 1755 1756 int cxl_mailbox_init(struct cxl_mailbox *cxl_mbox, struct device *host) 1757 { 1758 if (!cxl_mbox || !host) 1759 return -EINVAL; 1760 1761 cxl_mbox->host = host; 1762 mutex_init(&cxl_mbox->mbox_mutex); 1763 mutex_init(&cxl_mbox->feat_mutex); 1764 rcuwait_init(&cxl_mbox->mbox_wait); 1765 1766 return 0; 1767 } 1768 EXPORT_SYMBOL_NS_GPL(cxl_mailbox_init, "CXL"); 1769 1770 struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev, u64 serial, 1771 u16 dvsec) 1772 { 1773 struct cxl_memdev_state *mds; 1774 1775 mds = devm_cxl_dev_state_create(dev, CXL_DEVTYPE_CLASSMEM, serial, 1776 dvsec, struct cxl_memdev_state, cxlds, 1777 true); 1778 if (!mds) { 1779 dev_err(dev, "No memory available\n"); 1780 return ERR_PTR(-ENOMEM); 1781 } 1782 1783 mutex_init(&mds->event.log_lock); 1784 1785 return mds; 1786 } 1787 EXPORT_SYMBOL_NS_GPL(cxl_memdev_state_create, "CXL"); 1788 1789 void __init cxl_mbox_init(void) 1790 { 1791 struct dentry *mbox_debugfs; 1792 1793 mbox_debugfs = cxl_debugfs_create_dir("mbox"); 1794 debugfs_create_bool("raw_allow_all", 0600, mbox_debugfs, 1795 &cxl_raw_allow_all); 1796 } 1797