1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2020 Intel Corporation. */ 3 4 #include <linux/io-64-nonatomic-lo-hi.h> 5 #include <linux/firmware.h> 6 #include <linux/device.h> 7 #include <linux/slab.h> 8 #include <linux/idr.h> 9 #include <linux/pci.h> 10 #include <cxlmem.h> 11 #include "trace.h" 12 #include "core.h" 13 14 static DECLARE_RWSEM(cxl_memdev_rwsem); 15 16 /* 17 * An entire PCI topology full of devices should be enough for any 18 * config 19 */ 20 #define CXL_MEM_MAX_DEVS 65536 21 22 static int cxl_mem_major; 23 static DEFINE_IDA(cxl_memdev_ida); 24 25 static void cxl_memdev_release(struct device *dev) 26 { 27 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 28 struct device *parent = dev->parent; 29 30 ida_free(&cxl_memdev_ida, cxlmd->id); 31 kfree(cxlmd); 32 put_device(parent); 33 } 34 35 static char *cxl_memdev_devnode(const struct device *dev, umode_t *mode, kuid_t *uid, 36 kgid_t *gid) 37 { 38 return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev)); 39 } 40 41 static ssize_t firmware_version_show(struct device *dev, 42 struct device_attribute *attr, char *buf) 43 { 44 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 45 struct cxl_dev_state *cxlds = cxlmd->cxlds; 46 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); 47 48 if (!mds) 49 return sysfs_emit(buf, "\n"); 50 return sysfs_emit(buf, "%.16s\n", mds->firmware_version); 51 } 52 static DEVICE_ATTR_RO(firmware_version); 53 54 static ssize_t payload_max_show(struct device *dev, 55 struct device_attribute *attr, char *buf) 56 { 57 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 58 struct cxl_dev_state *cxlds = cxlmd->cxlds; 59 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); 60 61 if (!mds) 62 return sysfs_emit(buf, "\n"); 63 return sysfs_emit(buf, "%zu\n", cxlds->cxl_mbox.payload_size); 64 } 65 static DEVICE_ATTR_RO(payload_max); 66 67 static ssize_t label_storage_size_show(struct device *dev, 68 struct device_attribute *attr, char *buf) 69 { 70 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 71 struct cxl_dev_state *cxlds = cxlmd->cxlds; 72 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); 73 74 if (!mds) 75 return sysfs_emit(buf, "\n"); 76 return sysfs_emit(buf, "%zu\n", mds->lsa_size); 77 } 78 static DEVICE_ATTR_RO(label_storage_size); 79 80 static ssize_t ram_size_show(struct device *dev, struct device_attribute *attr, 81 char *buf) 82 { 83 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 84 struct cxl_dev_state *cxlds = cxlmd->cxlds; 85 unsigned long long len = cxl_part_size(cxlds, CXL_PARTMODE_RAM); 86 87 return sysfs_emit(buf, "%#llx\n", len); 88 } 89 90 static struct device_attribute dev_attr_ram_size = 91 __ATTR(size, 0444, ram_size_show, NULL); 92 93 static ssize_t pmem_size_show(struct device *dev, struct device_attribute *attr, 94 char *buf) 95 { 96 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 97 struct cxl_dev_state *cxlds = cxlmd->cxlds; 98 unsigned long long len = cxl_part_size(cxlds, CXL_PARTMODE_PMEM); 99 100 return sysfs_emit(buf, "%#llx\n", len); 101 } 102 103 static struct device_attribute dev_attr_pmem_size = 104 __ATTR(size, 0444, pmem_size_show, NULL); 105 106 static ssize_t serial_show(struct device *dev, struct device_attribute *attr, 107 char *buf) 108 { 109 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 110 struct cxl_dev_state *cxlds = cxlmd->cxlds; 111 112 return sysfs_emit(buf, "%#llx\n", cxlds->serial); 113 } 114 static DEVICE_ATTR_RO(serial); 115 116 static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, 117 char *buf) 118 { 119 return sysfs_emit(buf, "%d\n", dev_to_node(dev)); 120 } 121 static DEVICE_ATTR_RO(numa_node); 122 123 static ssize_t security_state_show(struct device *dev, 124 struct device_attribute *attr, 125 char *buf) 126 { 127 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 128 struct cxl_dev_state *cxlds = cxlmd->cxlds; 129 struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox; 130 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); 131 unsigned long state = mds->security.state; 132 int rc = 0; 133 134 /* sync with latest submission state */ 135 mutex_lock(&cxl_mbox->mbox_mutex); 136 if (mds->security.sanitize_active) 137 rc = sysfs_emit(buf, "sanitize\n"); 138 mutex_unlock(&cxl_mbox->mbox_mutex); 139 if (rc) 140 return rc; 141 142 if (!(state & CXL_PMEM_SEC_STATE_USER_PASS_SET)) 143 return sysfs_emit(buf, "disabled\n"); 144 if (state & CXL_PMEM_SEC_STATE_FROZEN || 145 state & CXL_PMEM_SEC_STATE_MASTER_PLIMIT || 146 state & CXL_PMEM_SEC_STATE_USER_PLIMIT) 147 return sysfs_emit(buf, "frozen\n"); 148 if (state & CXL_PMEM_SEC_STATE_LOCKED) 149 return sysfs_emit(buf, "locked\n"); 150 151 return sysfs_emit(buf, "unlocked\n"); 152 } 153 static struct device_attribute dev_attr_security_state = 154 __ATTR(state, 0444, security_state_show, NULL); 155 156 static ssize_t security_sanitize_store(struct device *dev, 157 struct device_attribute *attr, 158 const char *buf, size_t len) 159 { 160 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 161 bool sanitize; 162 ssize_t rc; 163 164 if (kstrtobool(buf, &sanitize) || !sanitize) 165 return -EINVAL; 166 167 rc = cxl_mem_sanitize(cxlmd, CXL_MBOX_OP_SANITIZE); 168 if (rc) 169 return rc; 170 171 return len; 172 } 173 static struct device_attribute dev_attr_security_sanitize = 174 __ATTR(sanitize, 0200, NULL, security_sanitize_store); 175 176 static ssize_t security_erase_store(struct device *dev, 177 struct device_attribute *attr, 178 const char *buf, size_t len) 179 { 180 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 181 ssize_t rc; 182 bool erase; 183 184 if (kstrtobool(buf, &erase) || !erase) 185 return -EINVAL; 186 187 rc = cxl_mem_sanitize(cxlmd, CXL_MBOX_OP_SECURE_ERASE); 188 if (rc) 189 return rc; 190 191 return len; 192 } 193 static struct device_attribute dev_attr_security_erase = 194 __ATTR(erase, 0200, NULL, security_erase_store); 195 196 bool cxl_memdev_has_poison_cmd(struct cxl_memdev *cxlmd, 197 enum poison_cmd_enabled_bits cmd) 198 { 199 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 200 201 if (!mds) 202 return 0; 203 204 return test_bit(cmd, mds->poison.enabled_cmds); 205 } 206 207 static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd) 208 { 209 struct cxl_dev_state *cxlds = cxlmd->cxlds; 210 u64 offset, length; 211 int rc = 0; 212 213 /* CXL 3.0 Spec 8.2.9.8.4.1 Separate pmem and ram poison requests */ 214 for (int i = 0; i < cxlds->nr_partitions; i++) { 215 const struct resource *res = &cxlds->part[i].res; 216 217 offset = res->start; 218 length = resource_size(res); 219 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL); 220 /* 221 * Invalid Physical Address is not an error for 222 * volatile addresses. Device support is optional. 223 */ 224 if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM) 225 rc = 0; 226 } 227 return rc; 228 } 229 230 int cxl_trigger_poison_list(struct cxl_memdev *cxlmd) 231 { 232 struct cxl_port *port; 233 int rc; 234 235 port = cxlmd->endpoint; 236 if (!port || !is_cxl_endpoint(port)) 237 return -EINVAL; 238 239 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 240 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 241 return rc; 242 243 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 244 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 245 return rc; 246 247 if (cxl_num_decoders_committed(port) == 0) { 248 /* No regions mapped to this memdev */ 249 rc = cxl_get_poison_by_memdev(cxlmd); 250 } else { 251 /* Regions mapped, collect poison by endpoint */ 252 rc = cxl_get_poison_by_endpoint(port); 253 } 254 255 return rc; 256 } 257 EXPORT_SYMBOL_NS_GPL(cxl_trigger_poison_list, "CXL"); 258 259 static int cxl_validate_poison_dpa(struct cxl_memdev *cxlmd, u64 dpa) 260 { 261 struct cxl_dev_state *cxlds = cxlmd->cxlds; 262 263 if (!IS_ENABLED(CONFIG_DEBUG_FS)) 264 return 0; 265 266 if (!resource_size(&cxlds->dpa_res)) { 267 dev_dbg(cxlds->dev, "device has no dpa resource\n"); 268 return -EINVAL; 269 } 270 if (!cxl_resource_contains_addr(&cxlds->dpa_res, dpa)) { 271 dev_dbg(cxlds->dev, "dpa:0x%llx not in resource:%pR\n", 272 dpa, &cxlds->dpa_res); 273 return -EINVAL; 274 } 275 if (!IS_ALIGNED(dpa, 64)) { 276 dev_dbg(cxlds->dev, "dpa:0x%llx is not 64-byte aligned\n", dpa); 277 return -EINVAL; 278 } 279 280 return 0; 281 } 282 283 int cxl_inject_poison_locked(struct cxl_memdev *cxlmd, u64 dpa) 284 { 285 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 286 struct cxl_mbox_inject_poison inject; 287 struct cxl_poison_record record; 288 struct cxl_mbox_cmd mbox_cmd; 289 struct cxl_region *cxlr; 290 int rc; 291 292 if (!IS_ENABLED(CONFIG_DEBUG_FS)) 293 return 0; 294 295 lockdep_assert_held(&cxl_rwsem.dpa); 296 lockdep_assert_held(&cxl_rwsem.region); 297 298 rc = cxl_validate_poison_dpa(cxlmd, dpa); 299 if (rc) 300 return rc; 301 302 inject.address = cpu_to_le64(dpa); 303 mbox_cmd = (struct cxl_mbox_cmd) { 304 .opcode = CXL_MBOX_OP_INJECT_POISON, 305 .size_in = sizeof(inject), 306 .payload_in = &inject, 307 }; 308 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 309 if (rc) 310 return rc; 311 312 cxlr = cxl_dpa_to_region(cxlmd, dpa); 313 if (cxlr) 314 dev_warn_once(cxl_mbox->host, 315 "poison inject dpa:%#llx region: %s\n", dpa, 316 dev_name(&cxlr->dev)); 317 318 record = (struct cxl_poison_record) { 319 .address = cpu_to_le64(dpa), 320 .length = cpu_to_le32(1), 321 }; 322 trace_cxl_poison(cxlmd, cxlr, &record, 0, 0, CXL_POISON_TRACE_INJECT); 323 324 return 0; 325 } 326 327 int cxl_inject_poison(struct cxl_memdev *cxlmd, u64 dpa) 328 { 329 int rc; 330 331 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 332 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 333 return rc; 334 335 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 336 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 337 return rc; 338 339 return cxl_inject_poison_locked(cxlmd, dpa); 340 } 341 EXPORT_SYMBOL_NS_GPL(cxl_inject_poison, "CXL"); 342 343 int cxl_clear_poison_locked(struct cxl_memdev *cxlmd, u64 dpa) 344 { 345 struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox; 346 struct cxl_mbox_clear_poison clear; 347 struct cxl_poison_record record; 348 struct cxl_mbox_cmd mbox_cmd; 349 struct cxl_region *cxlr; 350 int rc; 351 352 if (!IS_ENABLED(CONFIG_DEBUG_FS)) 353 return 0; 354 355 lockdep_assert_held(&cxl_rwsem.dpa); 356 lockdep_assert_held(&cxl_rwsem.region); 357 358 rc = cxl_validate_poison_dpa(cxlmd, dpa); 359 if (rc) 360 return rc; 361 362 /* 363 * In CXL 3.0 Spec 8.2.9.8.4.3, the Clear Poison mailbox command 364 * is defined to accept 64 bytes of write-data, along with the 365 * address to clear. This driver uses zeroes as write-data. 366 */ 367 clear = (struct cxl_mbox_clear_poison) { 368 .address = cpu_to_le64(dpa) 369 }; 370 371 mbox_cmd = (struct cxl_mbox_cmd) { 372 .opcode = CXL_MBOX_OP_CLEAR_POISON, 373 .size_in = sizeof(clear), 374 .payload_in = &clear, 375 }; 376 377 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 378 if (rc) 379 return rc; 380 381 cxlr = cxl_dpa_to_region(cxlmd, dpa); 382 if (cxlr) 383 dev_warn_once(cxl_mbox->host, 384 "poison clear dpa:%#llx region: %s\n", dpa, 385 dev_name(&cxlr->dev)); 386 387 record = (struct cxl_poison_record) { 388 .address = cpu_to_le64(dpa), 389 .length = cpu_to_le32(1), 390 }; 391 trace_cxl_poison(cxlmd, cxlr, &record, 0, 0, CXL_POISON_TRACE_CLEAR); 392 393 return 0; 394 } 395 396 int cxl_clear_poison(struct cxl_memdev *cxlmd, u64 dpa) 397 { 398 int rc; 399 400 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 401 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 402 return rc; 403 404 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 405 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 406 return rc; 407 408 return cxl_clear_poison_locked(cxlmd, dpa); 409 } 410 EXPORT_SYMBOL_NS_GPL(cxl_clear_poison, "CXL"); 411 412 static struct attribute *cxl_memdev_attributes[] = { 413 &dev_attr_serial.attr, 414 &dev_attr_firmware_version.attr, 415 &dev_attr_payload_max.attr, 416 &dev_attr_label_storage_size.attr, 417 &dev_attr_numa_node.attr, 418 NULL, 419 }; 420 421 static struct cxl_dpa_perf *part_perf(struct cxl_dev_state *cxlds, 422 enum cxl_partition_mode mode) 423 { 424 for (int i = 0; i < cxlds->nr_partitions; i++) 425 if (cxlds->part[i].mode == mode) 426 return &cxlds->part[i].perf; 427 return NULL; 428 } 429 430 static ssize_t pmem_qos_class_show(struct device *dev, 431 struct device_attribute *attr, char *buf) 432 { 433 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 434 struct cxl_dev_state *cxlds = cxlmd->cxlds; 435 436 return sysfs_emit(buf, "%d\n", part_perf(cxlds, CXL_PARTMODE_PMEM)->qos_class); 437 } 438 439 static struct device_attribute dev_attr_pmem_qos_class = 440 __ATTR(qos_class, 0444, pmem_qos_class_show, NULL); 441 442 static struct attribute *cxl_memdev_pmem_attributes[] = { 443 &dev_attr_pmem_size.attr, 444 &dev_attr_pmem_qos_class.attr, 445 NULL, 446 }; 447 448 static ssize_t ram_qos_class_show(struct device *dev, 449 struct device_attribute *attr, char *buf) 450 { 451 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 452 struct cxl_dev_state *cxlds = cxlmd->cxlds; 453 454 return sysfs_emit(buf, "%d\n", part_perf(cxlds, CXL_PARTMODE_RAM)->qos_class); 455 } 456 457 static struct device_attribute dev_attr_ram_qos_class = 458 __ATTR(qos_class, 0444, ram_qos_class_show, NULL); 459 460 static struct attribute *cxl_memdev_ram_attributes[] = { 461 &dev_attr_ram_size.attr, 462 &dev_attr_ram_qos_class.attr, 463 NULL, 464 }; 465 466 static struct attribute *cxl_memdev_security_attributes[] = { 467 &dev_attr_security_state.attr, 468 &dev_attr_security_sanitize.attr, 469 &dev_attr_security_erase.attr, 470 NULL, 471 }; 472 473 static umode_t cxl_memdev_visible(struct kobject *kobj, struct attribute *a, 474 int n) 475 { 476 if (!IS_ENABLED(CONFIG_NUMA) && a == &dev_attr_numa_node.attr) 477 return 0; 478 return a->mode; 479 } 480 481 static struct attribute_group cxl_memdev_attribute_group = { 482 .attrs = cxl_memdev_attributes, 483 .is_visible = cxl_memdev_visible, 484 }; 485 486 static umode_t cxl_ram_visible(struct kobject *kobj, struct attribute *a, int n) 487 { 488 struct device *dev = kobj_to_dev(kobj); 489 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 490 struct cxl_dpa_perf *perf = part_perf(cxlmd->cxlds, CXL_PARTMODE_RAM); 491 492 if (a == &dev_attr_ram_qos_class.attr && 493 (!perf || perf->qos_class == CXL_QOS_CLASS_INVALID)) 494 return 0; 495 496 return a->mode; 497 } 498 499 static struct attribute_group cxl_memdev_ram_attribute_group = { 500 .name = "ram", 501 .attrs = cxl_memdev_ram_attributes, 502 .is_visible = cxl_ram_visible, 503 }; 504 505 static umode_t cxl_pmem_visible(struct kobject *kobj, struct attribute *a, int n) 506 { 507 struct device *dev = kobj_to_dev(kobj); 508 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 509 struct cxl_dpa_perf *perf = part_perf(cxlmd->cxlds, CXL_PARTMODE_PMEM); 510 511 if (a == &dev_attr_pmem_qos_class.attr && 512 (!perf || perf->qos_class == CXL_QOS_CLASS_INVALID)) 513 return 0; 514 515 return a->mode; 516 } 517 518 static struct attribute_group cxl_memdev_pmem_attribute_group = { 519 .name = "pmem", 520 .attrs = cxl_memdev_pmem_attributes, 521 .is_visible = cxl_pmem_visible, 522 }; 523 524 static umode_t cxl_memdev_security_visible(struct kobject *kobj, 525 struct attribute *a, int n) 526 { 527 struct device *dev = kobj_to_dev(kobj); 528 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 529 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 530 531 if (a == &dev_attr_security_sanitize.attr && 532 !test_bit(CXL_SEC_ENABLED_SANITIZE, mds->security.enabled_cmds)) 533 return 0; 534 535 if (a == &dev_attr_security_erase.attr && 536 !test_bit(CXL_SEC_ENABLED_SECURE_ERASE, mds->security.enabled_cmds)) 537 return 0; 538 539 return a->mode; 540 } 541 542 static struct attribute_group cxl_memdev_security_attribute_group = { 543 .name = "security", 544 .attrs = cxl_memdev_security_attributes, 545 .is_visible = cxl_memdev_security_visible, 546 }; 547 548 static const struct attribute_group *cxl_memdev_attribute_groups[] = { 549 &cxl_memdev_attribute_group, 550 &cxl_memdev_ram_attribute_group, 551 &cxl_memdev_pmem_attribute_group, 552 &cxl_memdev_security_attribute_group, 553 NULL, 554 }; 555 556 void cxl_memdev_update_perf(struct cxl_memdev *cxlmd) 557 { 558 sysfs_update_group(&cxlmd->dev.kobj, &cxl_memdev_ram_attribute_group); 559 sysfs_update_group(&cxlmd->dev.kobj, &cxl_memdev_pmem_attribute_group); 560 } 561 EXPORT_SYMBOL_NS_GPL(cxl_memdev_update_perf, "CXL"); 562 563 static const struct device_type cxl_class_memdev_type = { 564 .name = "cxl_memdev", 565 .release = cxl_memdev_release, 566 .devnode = cxl_memdev_devnode, 567 .groups = cxl_memdev_attribute_groups, 568 }; 569 570 static const struct device_type cxl_memdev_type = { 571 .name = "cxl_memdev", 572 .release = cxl_memdev_release, 573 .devnode = cxl_memdev_devnode, 574 }; 575 576 bool is_cxl_memdev(const struct device *dev) 577 { 578 return (dev->type == &cxl_class_memdev_type || 579 dev->type == &cxl_memdev_type); 580 } 581 EXPORT_SYMBOL_NS_GPL(is_cxl_memdev, "CXL"); 582 583 static void add_part(struct cxl_dpa_info *info, u64 start, u64 size, 584 enum cxl_partition_mode mode, u8 handle) 585 { 586 int i = info->nr_partitions; 587 588 if (size == 0) 589 return; 590 591 info->part[i].range = (struct range) { 592 .start = start, 593 .end = start + size - 1, 594 }; 595 info->part[i].mode = mode; 596 info->part[i].handle = handle; 597 info->nr_partitions++; 598 } 599 600 int cxl_mem_dpa_fetch(struct cxl_memdev_state *mds, struct cxl_dpa_info *info) 601 { 602 struct cxl_dev_state *cxlds = &mds->cxlds; 603 struct device *dev = cxlds->dev; 604 int rc; 605 606 if (!cxlds->media_ready) { 607 info->size = 0; 608 return 0; 609 } 610 611 info->size = mds->total_bytes; 612 613 if (mds->partition_align_bytes == 0) { 614 add_part(info, 0, mds->volatile_only_bytes, CXL_PARTMODE_RAM, 0); 615 add_part(info, mds->volatile_only_bytes, 616 mds->persistent_only_bytes, CXL_PARTMODE_PMEM, 0); 617 return 0; 618 } 619 620 rc = cxl_mem_get_partition_info(mds); 621 if (rc) { 622 dev_err(dev, "Failed to query partition information\n"); 623 return rc; 624 } 625 626 add_part(info, 0, mds->active_volatile_bytes, CXL_PARTMODE_RAM, 0); 627 add_part(info, mds->active_volatile_bytes, mds->active_persistent_bytes, 628 CXL_PARTMODE_PMEM, 0); 629 630 return 0; 631 } 632 EXPORT_SYMBOL_NS_GPL(cxl_mem_dpa_fetch, "CXL"); 633 634 int cxl_configure_dcd(struct cxl_memdev_state *mds, struct cxl_dpa_info *info) 635 { 636 struct cxl_dc_partition_info dc_info = { }; 637 struct device *dev = mds->cxlds.dev; 638 u64 static_end = 0; 639 u64 dc_end; 640 int rc; 641 642 if (!mds->cxlds.media_ready) 643 return 0; 644 645 rc = cxl_dev_dc_identify(&mds->cxlds.cxl_mbox, &dc_info); 646 if (rc) { 647 dev_warn(dev, 648 "Failed to read Dynamic Capacity config: %d\n", rc); 649 return rc; 650 } 651 652 if (info->nr_partitions) 653 static_end = info->part[info->nr_partitions - 1].range.end + 1; 654 655 if (dc_info.start < static_end) { 656 dev_err(dev, 657 "DC partition 0 base %#llx overlaps static capacity ending at %#llx\n", 658 dc_info.start, static_end); 659 return -EINVAL; 660 } 661 662 /* A gap between static capacity and the DC partition is not supported */ 663 if (dc_info.start > static_end) { 664 dev_warn(dev, 665 "DC partition 0 base %#llx leaves a gap from static capacity ending at %#llx\n", 666 dc_info.start, static_end); 667 return -EOPNOTSUPP; 668 } 669 670 if (check_add_overflow(dc_info.start, dc_info.size, &dc_end)) { 671 dev_err(dev, 672 "DC partition 0 size %#llx overflows DPA space at base %#llx\n", 673 dc_info.size, dc_info.start); 674 return -EINVAL; 675 } 676 677 /* The DPA space must reach the end of the DC partition */ 678 if (dc_end > info->size) 679 info->size = dc_end; 680 dev_dbg(dev, "Adding dynamic ram partition; %#llx size %#llx\n", 681 dc_info.start, dc_info.size); 682 add_part(info, dc_info.start, dc_info.size, CXL_PARTMODE_DYNAMIC_RAM, 683 dc_info.handle); 684 685 return 0; 686 } 687 EXPORT_SYMBOL_NS_GPL(cxl_configure_dcd, "CXL"); 688 689 /** 690 * cxl_set_capacity: initialize dpa by a driver without a mailbox. 691 * 692 * @cxlds: pointer to cxl_dev_state 693 * @capacity: device volatile memory size 694 */ 695 int cxl_set_capacity(struct cxl_dev_state *cxlds, u64 capacity) 696 { 697 struct cxl_dpa_info range_info = { 698 .size = capacity, 699 }; 700 701 add_part(&range_info, 0, capacity, CXL_PARTMODE_RAM, 0); 702 return cxl_dpa_setup(cxlds, &range_info); 703 } 704 EXPORT_SYMBOL_NS_GPL(cxl_set_capacity, "CXL"); 705 706 /** 707 * set_exclusive_cxl_commands() - atomically disable user cxl commands 708 * @mds: The device state to operate on 709 * @cmds: bitmap of commands to mark exclusive 710 * 711 * Grab the cxl_memdev_rwsem in write mode to flush in-flight 712 * invocations of the ioctl path and then disable future execution of 713 * commands with the command ids set in @cmds. 714 */ 715 void set_exclusive_cxl_commands(struct cxl_memdev_state *mds, 716 unsigned long *cmds) 717 { 718 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 719 720 guard(rwsem_write)(&cxl_memdev_rwsem); 721 bitmap_or(cxl_mbox->exclusive_cmds, cxl_mbox->exclusive_cmds, 722 cmds, CXL_MEM_COMMAND_ID_MAX); 723 } 724 EXPORT_SYMBOL_NS_GPL(set_exclusive_cxl_commands, "CXL"); 725 726 /** 727 * clear_exclusive_cxl_commands() - atomically enable user cxl commands 728 * @mds: The device state to modify 729 * @cmds: bitmap of commands to mark available for userspace 730 */ 731 void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds, 732 unsigned long *cmds) 733 { 734 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 735 736 guard(rwsem_write)(&cxl_memdev_rwsem); 737 bitmap_andnot(cxl_mbox->exclusive_cmds, cxl_mbox->exclusive_cmds, 738 cmds, CXL_MEM_COMMAND_ID_MAX); 739 } 740 EXPORT_SYMBOL_NS_GPL(clear_exclusive_cxl_commands, "CXL"); 741 742 static void cxl_memdev_shutdown(struct device *dev) 743 { 744 struct cxl_memdev *cxlmd = to_cxl_memdev(dev); 745 746 guard(rwsem_write)(&cxl_memdev_rwsem); 747 cxlmd->cxlds = NULL; 748 } 749 750 static void cxl_memdev_unregister(void *_cxlmd) 751 { 752 struct cxl_memdev *cxlmd = _cxlmd; 753 struct device *dev = &cxlmd->dev; 754 755 cdev_device_del(&cxlmd->cdev, dev); 756 cxl_memdev_shutdown(dev); 757 put_device(dev); 758 } 759 760 static void detach_memdev(struct work_struct *work) 761 { 762 struct cxl_memdev *cxlmd; 763 764 cxlmd = container_of(work, typeof(*cxlmd), detach_work); 765 766 /* 767 * When the creator of @cxlmd sets ->attach it indicates CXL operation 768 * is required. In that case, @cxlmd detach escalates to parent device 769 * detach. 770 */ 771 if (cxlmd->attach) 772 device_release_driver(cxlmd->dev.parent); 773 else 774 device_release_driver(&cxlmd->dev); 775 put_device(&cxlmd->dev); 776 } 777 778 static struct lock_class_key cxl_memdev_key; 779 780 struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev, 781 enum cxl_devtype type, 782 u64 serial, u16 dvsec, 783 size_t size, bool has_mbox) 784 { 785 struct cxl_dev_state *cxlds = devm_kzalloc(dev, size, GFP_KERNEL); 786 787 if (!cxlds) 788 return NULL; 789 790 cxlds->dev = dev; 791 cxlds->type = type; 792 cxlds->serial = serial; 793 cxlds->cxl_dvsec = dvsec; 794 cxlds->reg_map.host = dev; 795 cxlds->reg_map.resource = CXL_RESOURCE_NONE; 796 797 if (has_mbox) 798 cxlds->cxl_mbox.host = dev; 799 800 return cxlds; 801 } 802 EXPORT_SYMBOL_NS_GPL(_devm_cxl_dev_state_create, "CXL"); 803 804 static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds, 805 const struct file_operations *fops, 806 const struct cxl_memdev_attach *attach) 807 { 808 struct cxl_memdev *cxlmd; 809 struct device *dev; 810 struct cdev *cdev; 811 int rc; 812 813 cxlmd = kzalloc_obj(*cxlmd); 814 if (!cxlmd) 815 return ERR_PTR(-ENOMEM); 816 817 rc = ida_alloc_max(&cxl_memdev_ida, CXL_MEM_MAX_DEVS - 1, GFP_KERNEL); 818 if (rc < 0) 819 goto err; 820 cxlmd->id = rc; 821 cxlmd->depth = -1; 822 cxlmd->attach = attach; 823 cxlmd->endpoint = ERR_PTR(-ENXIO); 824 825 dev = &cxlmd->dev; 826 device_initialize(dev); 827 lockdep_set_class(&dev->mutex, &cxl_memdev_key); 828 dev->parent = get_device(cxlds->dev); 829 dev->bus = &cxl_bus_type; 830 dev->devt = MKDEV(cxl_mem_major, cxlmd->id); 831 if (cxlds->type == CXL_DEVTYPE_DEVMEM) 832 dev->type = &cxl_memdev_type; 833 else 834 dev->type = &cxl_class_memdev_type; 835 device_set_pm_not_required(dev); 836 INIT_WORK(&cxlmd->detach_work, detach_memdev); 837 838 cdev = &cxlmd->cdev; 839 cdev_init(cdev, fops); 840 return cxlmd; 841 842 err: 843 kfree(cxlmd); 844 return ERR_PTR(rc); 845 } 846 847 static long __cxl_memdev_ioctl(struct cxl_memdev *cxlmd, unsigned int cmd, 848 unsigned long arg) 849 { 850 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlmd->cxlds); 851 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 852 853 switch (cmd) { 854 case CXL_MEM_QUERY_COMMANDS: 855 return cxl_query_cmd(cxl_mbox, (void __user *)arg); 856 case CXL_MEM_SEND_COMMAND: 857 return cxl_send_cmd(cxl_mbox, (void __user *)arg); 858 default: 859 return -ENOTTY; 860 } 861 } 862 863 static long cxl_memdev_ioctl(struct file *file, unsigned int cmd, 864 unsigned long arg) 865 { 866 struct cxl_memdev *cxlmd = file->private_data; 867 struct cxl_dev_state *cxlds; 868 869 guard(rwsem_read)(&cxl_memdev_rwsem); 870 cxlds = cxlmd->cxlds; 871 if (cxlds && cxlds->type == CXL_DEVTYPE_CLASSMEM) 872 return __cxl_memdev_ioctl(cxlmd, cmd, arg); 873 874 return -ENXIO; 875 } 876 877 static int cxl_memdev_open(struct inode *inode, struct file *file) 878 { 879 struct cxl_memdev *cxlmd = 880 container_of(inode->i_cdev, typeof(*cxlmd), cdev); 881 882 get_device(&cxlmd->dev); 883 file->private_data = cxlmd; 884 885 return 0; 886 } 887 888 static int cxl_memdev_release_file(struct inode *inode, struct file *file) 889 { 890 struct cxl_memdev *cxlmd = 891 container_of(inode->i_cdev, typeof(*cxlmd), cdev); 892 893 put_device(&cxlmd->dev); 894 895 return 0; 896 } 897 898 /** 899 * cxl_mem_get_fw_info - Get Firmware info 900 * @mds: The device data for the operation 901 * 902 * Retrieve firmware info for the device specified. 903 * 904 * Return: 0 if no error: or the result of the mailbox command. 905 * 906 * See CXL-3.0 8.2.9.3.1 Get FW Info 907 */ 908 static int cxl_mem_get_fw_info(struct cxl_memdev_state *mds) 909 { 910 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 911 struct cxl_mbox_get_fw_info info; 912 struct cxl_mbox_cmd mbox_cmd; 913 int rc; 914 915 mbox_cmd = (struct cxl_mbox_cmd) { 916 .opcode = CXL_MBOX_OP_GET_FW_INFO, 917 .size_out = sizeof(info), 918 .payload_out = &info, 919 }; 920 921 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 922 if (rc < 0) 923 return rc; 924 925 mds->fw.num_slots = info.num_slots; 926 mds->fw.cur_slot = FIELD_GET(CXL_FW_INFO_SLOT_INFO_CUR_MASK, 927 info.slot_info); 928 929 return 0; 930 } 931 932 /** 933 * cxl_mem_activate_fw - Activate Firmware 934 * @mds: The device data for the operation 935 * @slot: slot number to activate 936 * 937 * Activate firmware in a given slot for the device specified. 938 * 939 * Return: 0 if no error: or the result of the mailbox command. 940 * 941 * See CXL-3.0 8.2.9.3.3 Activate FW 942 */ 943 static int cxl_mem_activate_fw(struct cxl_memdev_state *mds, int slot) 944 { 945 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 946 struct cxl_mbox_activate_fw activate; 947 struct cxl_mbox_cmd mbox_cmd; 948 949 if (slot == 0 || slot > mds->fw.num_slots) 950 return -EINVAL; 951 952 mbox_cmd = (struct cxl_mbox_cmd) { 953 .opcode = CXL_MBOX_OP_ACTIVATE_FW, 954 .size_in = sizeof(activate), 955 .payload_in = &activate, 956 }; 957 958 /* Only offline activation supported for now */ 959 activate.action = CXL_FW_ACTIVATE_OFFLINE; 960 activate.slot = slot; 961 962 return cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 963 } 964 965 /** 966 * cxl_mem_abort_fw_xfer - Abort an in-progress FW transfer 967 * @mds: The device data for the operation 968 * 969 * Abort an in-progress firmware transfer for the device specified. 970 * 971 * Return: 0 if no error: or the result of the mailbox command. 972 * 973 * See CXL-3.0 8.2.9.3.2 Transfer FW 974 */ 975 static int cxl_mem_abort_fw_xfer(struct cxl_memdev_state *mds) 976 { 977 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 978 struct cxl_mbox_transfer_fw *transfer; 979 struct cxl_mbox_cmd mbox_cmd; 980 int rc; 981 982 transfer = kzalloc_flex(*transfer, data, 0); 983 if (!transfer) 984 return -ENOMEM; 985 986 /* Set a 1s poll interval and a total wait time of 30s */ 987 mbox_cmd = (struct cxl_mbox_cmd) { 988 .opcode = CXL_MBOX_OP_TRANSFER_FW, 989 .size_in = sizeof(*transfer), 990 .payload_in = transfer, 991 .poll_interval_ms = 1000, 992 .poll_count = 30, 993 }; 994 995 transfer->action = CXL_FW_TRANSFER_ACTION_ABORT; 996 997 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 998 kfree(transfer); 999 return rc; 1000 } 1001 1002 static void cxl_fw_cleanup(struct fw_upload *fwl) 1003 { 1004 struct cxl_memdev_state *mds = fwl->dd_handle; 1005 1006 mds->fw.next_slot = 0; 1007 } 1008 1009 static int cxl_fw_do_cancel(struct fw_upload *fwl) 1010 { 1011 struct cxl_memdev_state *mds = fwl->dd_handle; 1012 struct cxl_dev_state *cxlds = &mds->cxlds; 1013 struct cxl_memdev *cxlmd = cxlds->cxlmd; 1014 int rc; 1015 1016 rc = cxl_mem_abort_fw_xfer(mds); 1017 if (rc < 0) 1018 dev_err(&cxlmd->dev, "Error aborting FW transfer: %d\n", rc); 1019 1020 return FW_UPLOAD_ERR_CANCELED; 1021 } 1022 1023 static enum fw_upload_err cxl_fw_prepare(struct fw_upload *fwl, const u8 *data, 1024 u32 size) 1025 { 1026 struct cxl_memdev_state *mds = fwl->dd_handle; 1027 struct cxl_mbox_transfer_fw *transfer; 1028 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1029 1030 if (!size) 1031 return FW_UPLOAD_ERR_INVALID_SIZE; 1032 1033 mds->fw.oneshot = struct_size(transfer, data, size) <= 1034 cxl_mbox->payload_size; 1035 1036 if (cxl_mem_get_fw_info(mds)) 1037 return FW_UPLOAD_ERR_HW_ERROR; 1038 1039 /* 1040 * So far no state has been changed, hence no other cleanup is 1041 * necessary. Simply return the cancelled status. 1042 */ 1043 if (test_and_clear_bit(CXL_FW_CANCEL, mds->fw.state)) 1044 return FW_UPLOAD_ERR_CANCELED; 1045 1046 return FW_UPLOAD_ERR_NONE; 1047 } 1048 1049 static enum fw_upload_err cxl_fw_write(struct fw_upload *fwl, const u8 *data, 1050 u32 offset, u32 size, u32 *written) 1051 { 1052 struct cxl_memdev_state *mds = fwl->dd_handle; 1053 struct cxl_dev_state *cxlds = &mds->cxlds; 1054 struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox; 1055 struct cxl_memdev *cxlmd = cxlds->cxlmd; 1056 struct cxl_mbox_transfer_fw *transfer; 1057 struct cxl_mbox_cmd mbox_cmd; 1058 u32 cur_size, remaining; 1059 size_t size_in; 1060 int rc; 1061 1062 *written = 0; 1063 1064 /* Offset has to be aligned to 128B (CXL-3.0 8.2.9.3.2 Table 8-57) */ 1065 if (!IS_ALIGNED(offset, CXL_FW_TRANSFER_ALIGNMENT)) { 1066 dev_err(&cxlmd->dev, 1067 "misaligned offset for FW transfer slice (%u)\n", 1068 offset); 1069 return FW_UPLOAD_ERR_RW_ERROR; 1070 } 1071 1072 /* 1073 * Pick transfer size based on mds->payload_size @size must bw 128-byte 1074 * aligned, ->payload_size is a power of 2 starting at 256 bytes, and 1075 * sizeof(*transfer) is 128. These constraints imply that @cur_size 1076 * will always be 128b aligned. 1077 */ 1078 cur_size = min_t(size_t, size, cxl_mbox->payload_size - sizeof(*transfer)); 1079 1080 remaining = size - cur_size; 1081 size_in = struct_size(transfer, data, cur_size); 1082 1083 if (test_and_clear_bit(CXL_FW_CANCEL, mds->fw.state)) 1084 return cxl_fw_do_cancel(fwl); 1085 1086 /* 1087 * Slot numbers are 1-indexed 1088 * cur_slot is the 0-indexed next_slot (i.e. 'cur_slot - 1 + 1') 1089 * Check for rollover using modulo, and 1-index it by adding 1 1090 */ 1091 mds->fw.next_slot = (mds->fw.cur_slot % mds->fw.num_slots) + 1; 1092 1093 /* Do the transfer via mailbox cmd */ 1094 transfer = kzalloc(size_in, GFP_KERNEL); 1095 if (!transfer) 1096 return FW_UPLOAD_ERR_RW_ERROR; 1097 1098 transfer->offset = cpu_to_le32(offset / CXL_FW_TRANSFER_ALIGNMENT); 1099 memcpy(transfer->data, data + offset, cur_size); 1100 if (mds->fw.oneshot) { 1101 transfer->action = CXL_FW_TRANSFER_ACTION_FULL; 1102 transfer->slot = mds->fw.next_slot; 1103 } else { 1104 if (offset == 0) { 1105 transfer->action = CXL_FW_TRANSFER_ACTION_INITIATE; 1106 } else if (remaining == 0) { 1107 transfer->action = CXL_FW_TRANSFER_ACTION_END; 1108 transfer->slot = mds->fw.next_slot; 1109 } else { 1110 transfer->action = CXL_FW_TRANSFER_ACTION_CONTINUE; 1111 } 1112 } 1113 1114 mbox_cmd = (struct cxl_mbox_cmd) { 1115 .opcode = CXL_MBOX_OP_TRANSFER_FW, 1116 .size_in = size_in, 1117 .payload_in = transfer, 1118 .poll_interval_ms = 1000, 1119 .poll_count = 30, 1120 }; 1121 1122 rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd); 1123 if (rc < 0) { 1124 rc = FW_UPLOAD_ERR_RW_ERROR; 1125 goto out_free; 1126 } 1127 1128 *written = cur_size; 1129 1130 /* Activate FW if oneshot or if the last slice was written */ 1131 if (mds->fw.oneshot || remaining == 0) { 1132 dev_dbg(&cxlmd->dev, "Activating firmware slot: %d\n", 1133 mds->fw.next_slot); 1134 rc = cxl_mem_activate_fw(mds, mds->fw.next_slot); 1135 if (rc < 0) { 1136 dev_err(&cxlmd->dev, "Error activating firmware: %d\n", 1137 rc); 1138 rc = FW_UPLOAD_ERR_HW_ERROR; 1139 goto out_free; 1140 } 1141 } 1142 1143 rc = FW_UPLOAD_ERR_NONE; 1144 1145 out_free: 1146 kfree(transfer); 1147 return rc; 1148 } 1149 1150 static enum fw_upload_err cxl_fw_poll_complete(struct fw_upload *fwl) 1151 { 1152 struct cxl_memdev_state *mds = fwl->dd_handle; 1153 1154 /* 1155 * cxl_internal_send_cmd() handles background operations synchronously. 1156 * No need to wait for completions here - any errors would've been 1157 * reported and handled during the ->write() call(s). 1158 * Just check if a cancel request was received, and return success. 1159 */ 1160 if (test_and_clear_bit(CXL_FW_CANCEL, mds->fw.state)) 1161 return cxl_fw_do_cancel(fwl); 1162 1163 return FW_UPLOAD_ERR_NONE; 1164 } 1165 1166 static void cxl_fw_cancel(struct fw_upload *fwl) 1167 { 1168 struct cxl_memdev_state *mds = fwl->dd_handle; 1169 1170 set_bit(CXL_FW_CANCEL, mds->fw.state); 1171 } 1172 1173 static const struct fw_upload_ops cxl_memdev_fw_ops = { 1174 .prepare = cxl_fw_prepare, 1175 .write = cxl_fw_write, 1176 .poll_complete = cxl_fw_poll_complete, 1177 .cancel = cxl_fw_cancel, 1178 .cleanup = cxl_fw_cleanup, 1179 }; 1180 1181 static void cxl_remove_fw_upload(void *fwl) 1182 { 1183 firmware_upload_unregister(fwl); 1184 } 1185 1186 int devm_cxl_setup_fw_upload(struct device *host, struct cxl_memdev_state *mds) 1187 { 1188 struct cxl_dev_state *cxlds = &mds->cxlds; 1189 struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox; 1190 struct device *dev = &cxlds->cxlmd->dev; 1191 struct fw_upload *fwl; 1192 1193 if (!test_bit(CXL_MEM_COMMAND_ID_GET_FW_INFO, cxl_mbox->enabled_cmds)) 1194 return 0; 1195 1196 fwl = firmware_upload_register(THIS_MODULE, dev, dev_name(dev), 1197 &cxl_memdev_fw_ops, mds); 1198 if (IS_ERR(fwl)) 1199 return PTR_ERR(fwl); 1200 return devm_add_action_or_reset(host, cxl_remove_fw_upload, fwl); 1201 } 1202 EXPORT_SYMBOL_NS_GPL(devm_cxl_setup_fw_upload, "CXL"); 1203 1204 static const struct file_operations cxl_memdev_fops = { 1205 .owner = THIS_MODULE, 1206 .unlocked_ioctl = cxl_memdev_ioctl, 1207 .open = cxl_memdev_open, 1208 .release = cxl_memdev_release_file, 1209 .compat_ioctl = compat_ptr_ioctl, 1210 .llseek = noop_llseek, 1211 }; 1212 1213 /* 1214 * Activate ioctl operations, no cxl_memdev_rwsem manipulation needed as this is 1215 * ordered with cdev_add() publishing the device. 1216 */ 1217 static int cxlmd_add(struct cxl_memdev *cxlmd, struct cxl_dev_state *cxlds) 1218 { 1219 int rc; 1220 1221 cxlmd->cxlds = cxlds; 1222 cxlds->cxlmd = cxlmd; 1223 1224 rc = cdev_device_add(&cxlmd->cdev, &cxlmd->dev); 1225 if (rc) { 1226 /* 1227 * The cdev was briefly live, shutdown any ioctl operations that 1228 * saw that state. 1229 */ 1230 cxl_memdev_shutdown(&cxlmd->dev); 1231 return rc; 1232 } 1233 1234 return 0; 1235 } 1236 1237 DEFINE_FREE(put_cxlmd, struct cxl_memdev *, 1238 if (!IS_ERR_OR_NULL(_T)) put_device(&_T->dev)) 1239 1240 static bool cxl_memdev_attach_failed(struct cxl_memdev *cxlmd) 1241 { 1242 /* 1243 * If @attach is provided fail if the driver is not attached upon 1244 * return. Note that failure here could be the result of a race to 1245 * teardown the CXL port topology. I.e. cxl_mem_probe() could have 1246 * succeeded and then cxl_mem unbound before the lock is acquired. 1247 */ 1248 guard(device)(&cxlmd->dev); 1249 return (cxlmd->attach && !cxlmd->dev.driver); 1250 } 1251 1252 static struct cxl_memdev *cxl_memdev_autoremove(struct cxl_memdev *cxlmd) 1253 { 1254 int rc; 1255 1256 if (cxl_memdev_attach_failed(cxlmd)) { 1257 cxl_memdev_unregister(cxlmd); 1258 return ERR_PTR(-ENXIO); 1259 } 1260 1261 rc = devm_add_action_or_reset(cxlmd->cxlds->dev, cxl_memdev_unregister, 1262 cxlmd); 1263 if (rc) 1264 return ERR_PTR(rc); 1265 1266 return cxlmd; 1267 } 1268 1269 /* 1270 * Core helper for devm_cxl_add_memdev() that wants to both create a device and 1271 * assert to the caller that upon return cxl_mem::probe() has been invoked. 1272 */ 1273 struct cxl_memdev *__devm_cxl_add_memdev(struct cxl_dev_state *cxlds, 1274 const struct cxl_memdev_attach *attach) 1275 { 1276 struct device *dev; 1277 int rc; 1278 1279 struct cxl_memdev *cxlmd __free(put_cxlmd) = 1280 cxl_memdev_alloc(cxlds, &cxl_memdev_fops, attach); 1281 if (IS_ERR(cxlmd)) 1282 return cxlmd; 1283 1284 dev = &cxlmd->dev; 1285 rc = dev_set_name(dev, "mem%d", cxlmd->id); 1286 if (rc) 1287 return ERR_PTR(rc); 1288 1289 rc = cxlmd_add(cxlmd, cxlds); 1290 if (rc) 1291 return ERR_PTR(rc); 1292 1293 return cxl_memdev_autoremove(no_free_ptr(cxlmd)); 1294 } 1295 EXPORT_SYMBOL_FOR_MODULES(__devm_cxl_add_memdev, "cxl_mem"); 1296 1297 static void sanitize_teardown_notifier(void *data) 1298 { 1299 struct cxl_memdev_state *mds = data; 1300 struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox; 1301 struct kernfs_node *state; 1302 1303 /* 1304 * Prevent new irq triggered invocations of the workqueue and 1305 * flush inflight invocations. 1306 */ 1307 mutex_lock(&cxl_mbox->mbox_mutex); 1308 state = mds->security.sanitize_node; 1309 mds->security.sanitize_node = NULL; 1310 mutex_unlock(&cxl_mbox->mbox_mutex); 1311 1312 cancel_delayed_work_sync(&mds->security.poll_dwork); 1313 sysfs_put(state); 1314 } 1315 1316 int devm_cxl_sanitize_setup_notifier(struct device *host, 1317 struct cxl_memdev *cxlmd) 1318 { 1319 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1320 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds); 1321 struct kernfs_node *sec; 1322 1323 if (!test_bit(CXL_SEC_ENABLED_SANITIZE, mds->security.enabled_cmds)) 1324 return 0; 1325 1326 /* 1327 * Note, the expectation is that @cxlmd would have failed to be 1328 * created if these sysfs_get_dirent calls fail. 1329 */ 1330 sec = sysfs_get_dirent(cxlmd->dev.kobj.sd, "security"); 1331 if (!sec) 1332 return -ENOENT; 1333 mds->security.sanitize_node = sysfs_get_dirent(sec, "state"); 1334 sysfs_put(sec); 1335 if (!mds->security.sanitize_node) 1336 return -ENOENT; 1337 1338 return devm_add_action_or_reset(host, sanitize_teardown_notifier, mds); 1339 } 1340 EXPORT_SYMBOL_NS_GPL(devm_cxl_sanitize_setup_notifier, "CXL"); 1341 1342 __init int cxl_memdev_init(void) 1343 { 1344 dev_t devt; 1345 int rc; 1346 1347 rc = alloc_chrdev_region(&devt, 0, CXL_MEM_MAX_DEVS, "cxl"); 1348 if (rc) 1349 return rc; 1350 1351 cxl_mem_major = MAJOR(devt); 1352 1353 return 0; 1354 } 1355 1356 void cxl_memdev_exit(void) 1357 { 1358 unregister_chrdev_region(MKDEV(cxl_mem_major, 0), CXL_MEM_MAX_DEVS); 1359 } 1360