1 // SPDX-License-Identifier: GPL-2.0 2 3 #define pr_fmt(fmt) "papr-scm: " fmt 4 5 #include <linux/of.h> 6 #include <linux/kernel.h> 7 #include <linux/module.h> 8 #include <linux/ioport.h> 9 #include <linux/slab.h> 10 #include <linux/ndctl.h> 11 #include <linux/sched.h> 12 #include <linux/libnvdimm.h> 13 #include <linux/platform_device.h> 14 #include <linux/delay.h> 15 #include <linux/seq_buf.h> 16 #include <linux/nd.h> 17 18 #include <asm/plpar_wrappers.h> 19 #include <asm/papr_pdsm.h> 20 #include <asm/mce.h> 21 22 #define BIND_ANY_ADDR (~0ul) 23 24 #define PAPR_SCM_DIMM_CMD_MASK \ 25 ((1ul << ND_CMD_GET_CONFIG_SIZE) | \ 26 (1ul << ND_CMD_GET_CONFIG_DATA) | \ 27 (1ul << ND_CMD_SET_CONFIG_DATA) | \ 28 (1ul << ND_CMD_CALL)) 29 30 /* DIMM health bitmap bitmap indicators */ 31 /* SCM device is unable to persist memory contents */ 32 #define PAPR_PMEM_UNARMED (1ULL << (63 - 0)) 33 /* SCM device failed to persist memory contents */ 34 #define PAPR_PMEM_SHUTDOWN_DIRTY (1ULL << (63 - 1)) 35 /* SCM device contents are persisted from previous IPL */ 36 #define PAPR_PMEM_SHUTDOWN_CLEAN (1ULL << (63 - 2)) 37 /* SCM device contents are not persisted from previous IPL */ 38 #define PAPR_PMEM_EMPTY (1ULL << (63 - 3)) 39 /* SCM device memory life remaining is critically low */ 40 #define PAPR_PMEM_HEALTH_CRITICAL (1ULL << (63 - 4)) 41 /* SCM device will be garded off next IPL due to failure */ 42 #define PAPR_PMEM_HEALTH_FATAL (1ULL << (63 - 5)) 43 /* SCM contents cannot persist due to current platform health status */ 44 #define PAPR_PMEM_HEALTH_UNHEALTHY (1ULL << (63 - 6)) 45 /* SCM device is unable to persist memory contents in certain conditions */ 46 #define PAPR_PMEM_HEALTH_NON_CRITICAL (1ULL << (63 - 7)) 47 /* SCM device is encrypted */ 48 #define PAPR_PMEM_ENCRYPTED (1ULL << (63 - 8)) 49 /* SCM device has been scrubbed and locked */ 50 #define PAPR_PMEM_SCRUBBED_AND_LOCKED (1ULL << (63 - 9)) 51 52 /* Bits status indicators for health bitmap indicating unarmed dimm */ 53 #define PAPR_PMEM_UNARMED_MASK (PAPR_PMEM_UNARMED | \ 54 PAPR_PMEM_HEALTH_UNHEALTHY) 55 56 /* Bits status indicators for health bitmap indicating unflushed dimm */ 57 #define PAPR_PMEM_BAD_SHUTDOWN_MASK (PAPR_PMEM_SHUTDOWN_DIRTY) 58 59 /* Bits status indicators for health bitmap indicating unrestored dimm */ 60 #define PAPR_PMEM_BAD_RESTORE_MASK (PAPR_PMEM_EMPTY) 61 62 /* Bit status indicators for smart event notification */ 63 #define PAPR_PMEM_SMART_EVENT_MASK (PAPR_PMEM_HEALTH_CRITICAL | \ 64 PAPR_PMEM_HEALTH_FATAL | \ 65 PAPR_PMEM_HEALTH_UNHEALTHY) 66 67 /* private struct associated with each region */ 68 struct papr_scm_priv { 69 struct platform_device *pdev; 70 struct device_node *dn; 71 uint32_t drc_index; 72 uint64_t blocks; 73 uint64_t block_size; 74 int metadata_size; 75 bool is_volatile; 76 77 uint64_t bound_addr; 78 79 struct nvdimm_bus_descriptor bus_desc; 80 struct nvdimm_bus *bus; 81 struct nvdimm *nvdimm; 82 struct resource res; 83 struct nd_region *region; 84 struct nd_interleave_set nd_set; 85 struct list_head region_list; 86 87 /* Protect dimm health data from concurrent read/writes */ 88 struct mutex health_mutex; 89 90 /* Last time the health information of the dimm was updated */ 91 unsigned long lasthealth_jiffies; 92 93 /* Health information for the dimm */ 94 u64 health_bitmap; 95 }; 96 97 LIST_HEAD(papr_nd_regions); 98 DEFINE_MUTEX(papr_ndr_lock); 99 100 static int drc_pmem_bind(struct papr_scm_priv *p) 101 { 102 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 103 uint64_t saved = 0; 104 uint64_t token; 105 int64_t rc; 106 107 /* 108 * When the hypervisor cannot map all the requested memory in a single 109 * hcall it returns H_BUSY and we call again with the token until 110 * we get H_SUCCESS. Aborting the retry loop before getting H_SUCCESS 111 * leave the system in an undefined state, so we wait. 112 */ 113 token = 0; 114 115 do { 116 rc = plpar_hcall(H_SCM_BIND_MEM, ret, p->drc_index, 0, 117 p->blocks, BIND_ANY_ADDR, token); 118 token = ret[0]; 119 if (!saved) 120 saved = ret[1]; 121 cond_resched(); 122 } while (rc == H_BUSY); 123 124 if (rc) 125 return rc; 126 127 p->bound_addr = saved; 128 dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", 129 p->drc_index, (unsigned long)saved); 130 return rc; 131 } 132 133 static void drc_pmem_unbind(struct papr_scm_priv *p) 134 { 135 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 136 uint64_t token = 0; 137 int64_t rc; 138 139 dev_dbg(&p->pdev->dev, "unbind drc 0x%x\n", p->drc_index); 140 141 /* NB: unbind has the same retry requirements as drc_pmem_bind() */ 142 do { 143 144 /* Unbind of all SCM resources associated with drcIndex */ 145 rc = plpar_hcall(H_SCM_UNBIND_ALL, ret, H_UNBIND_SCOPE_DRC, 146 p->drc_index, token); 147 token = ret[0]; 148 149 /* Check if we are stalled for some time */ 150 if (H_IS_LONG_BUSY(rc)) { 151 msleep(get_longbusy_msecs(rc)); 152 rc = H_BUSY; 153 } else if (rc == H_BUSY) { 154 cond_resched(); 155 } 156 157 } while (rc == H_BUSY); 158 159 if (rc) 160 dev_err(&p->pdev->dev, "unbind error: %lld\n", rc); 161 else 162 dev_dbg(&p->pdev->dev, "unbind drc 0x%x complete\n", 163 p->drc_index); 164 165 return; 166 } 167 168 static int drc_pmem_query_n_bind(struct papr_scm_priv *p) 169 { 170 unsigned long start_addr; 171 unsigned long end_addr; 172 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 173 int64_t rc; 174 175 176 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret, 177 p->drc_index, 0); 178 if (rc) 179 goto err_out; 180 start_addr = ret[0]; 181 182 /* Make sure the full region is bound. */ 183 rc = plpar_hcall(H_SCM_QUERY_BLOCK_MEM_BINDING, ret, 184 p->drc_index, p->blocks - 1); 185 if (rc) 186 goto err_out; 187 end_addr = ret[0]; 188 189 if ((end_addr - start_addr) != ((p->blocks - 1) * p->block_size)) 190 goto err_out; 191 192 p->bound_addr = start_addr; 193 dev_dbg(&p->pdev->dev, "bound drc 0x%x to 0x%lx\n", p->drc_index, start_addr); 194 return rc; 195 196 err_out: 197 dev_info(&p->pdev->dev, 198 "Failed to query, trying an unbind followed by bind"); 199 drc_pmem_unbind(p); 200 return drc_pmem_bind(p); 201 } 202 203 /* 204 * Issue hcall to retrieve dimm health info and populate papr_scm_priv with the 205 * health information. 206 */ 207 static int __drc_pmem_query_health(struct papr_scm_priv *p) 208 { 209 unsigned long ret[PLPAR_HCALL_BUFSIZE]; 210 long rc; 211 212 /* issue the hcall */ 213 rc = plpar_hcall(H_SCM_HEALTH, ret, p->drc_index); 214 if (rc != H_SUCCESS) { 215 dev_err(&p->pdev->dev, 216 "Failed to query health information, Err:%ld\n", rc); 217 return -ENXIO; 218 } 219 220 p->lasthealth_jiffies = jiffies; 221 p->health_bitmap = ret[0] & ret[1]; 222 223 dev_dbg(&p->pdev->dev, 224 "Queried dimm health info. Bitmap:0x%016lx Mask:0x%016lx\n", 225 ret[0], ret[1]); 226 227 return 0; 228 } 229 230 /* Min interval in seconds for assuming stable dimm health */ 231 #define MIN_HEALTH_QUERY_INTERVAL 60 232 233 /* Query cached health info and if needed call drc_pmem_query_health */ 234 static int drc_pmem_query_health(struct papr_scm_priv *p) 235 { 236 unsigned long cache_timeout; 237 int rc; 238 239 /* Protect concurrent modifications to papr_scm_priv */ 240 rc = mutex_lock_interruptible(&p->health_mutex); 241 if (rc) 242 return rc; 243 244 /* Jiffies offset for which the health data is assumed to be same */ 245 cache_timeout = p->lasthealth_jiffies + 246 msecs_to_jiffies(MIN_HEALTH_QUERY_INTERVAL * 1000); 247 248 /* Fetch new health info is its older than MIN_HEALTH_QUERY_INTERVAL */ 249 if (time_after(jiffies, cache_timeout)) 250 rc = __drc_pmem_query_health(p); 251 else 252 /* Assume cached health data is valid */ 253 rc = 0; 254 255 mutex_unlock(&p->health_mutex); 256 return rc; 257 } 258 259 static int papr_scm_meta_get(struct papr_scm_priv *p, 260 struct nd_cmd_get_config_data_hdr *hdr) 261 { 262 unsigned long data[PLPAR_HCALL_BUFSIZE]; 263 unsigned long offset, data_offset; 264 int len, read; 265 int64_t ret; 266 267 if ((hdr->in_offset + hdr->in_length) > p->metadata_size) 268 return -EINVAL; 269 270 for (len = hdr->in_length; len; len -= read) { 271 272 data_offset = hdr->in_length - len; 273 offset = hdr->in_offset + data_offset; 274 275 if (len >= 8) 276 read = 8; 277 else if (len >= 4) 278 read = 4; 279 else if (len >= 2) 280 read = 2; 281 else 282 read = 1; 283 284 ret = plpar_hcall(H_SCM_READ_METADATA, data, p->drc_index, 285 offset, read); 286 287 if (ret == H_PARAMETER) /* bad DRC index */ 288 return -ENODEV; 289 if (ret) 290 return -EINVAL; /* other invalid parameter */ 291 292 switch (read) { 293 case 8: 294 *(uint64_t *)(hdr->out_buf + data_offset) = be64_to_cpu(data[0]); 295 break; 296 case 4: 297 *(uint32_t *)(hdr->out_buf + data_offset) = be32_to_cpu(data[0] & 0xffffffff); 298 break; 299 300 case 2: 301 *(uint16_t *)(hdr->out_buf + data_offset) = be16_to_cpu(data[0] & 0xffff); 302 break; 303 304 case 1: 305 *(uint8_t *)(hdr->out_buf + data_offset) = (data[0] & 0xff); 306 break; 307 } 308 } 309 return 0; 310 } 311 312 static int papr_scm_meta_set(struct papr_scm_priv *p, 313 struct nd_cmd_set_config_hdr *hdr) 314 { 315 unsigned long offset, data_offset; 316 int len, wrote; 317 unsigned long data; 318 __be64 data_be; 319 int64_t ret; 320 321 if ((hdr->in_offset + hdr->in_length) > p->metadata_size) 322 return -EINVAL; 323 324 for (len = hdr->in_length; len; len -= wrote) { 325 326 data_offset = hdr->in_length - len; 327 offset = hdr->in_offset + data_offset; 328 329 if (len >= 8) { 330 data = *(uint64_t *)(hdr->in_buf + data_offset); 331 data_be = cpu_to_be64(data); 332 wrote = 8; 333 } else if (len >= 4) { 334 data = *(uint32_t *)(hdr->in_buf + data_offset); 335 data &= 0xffffffff; 336 data_be = cpu_to_be32(data); 337 wrote = 4; 338 } else if (len >= 2) { 339 data = *(uint16_t *)(hdr->in_buf + data_offset); 340 data &= 0xffff; 341 data_be = cpu_to_be16(data); 342 wrote = 2; 343 } else { 344 data_be = *(uint8_t *)(hdr->in_buf + data_offset); 345 data_be &= 0xff; 346 wrote = 1; 347 } 348 349 ret = plpar_hcall_norets(H_SCM_WRITE_METADATA, p->drc_index, 350 offset, data_be, wrote); 351 if (ret == H_PARAMETER) /* bad DRC index */ 352 return -ENODEV; 353 if (ret) 354 return -EINVAL; /* other invalid parameter */ 355 } 356 357 return 0; 358 } 359 360 /* 361 * Do a sanity checks on the inputs args to dimm-control function and return 362 * '0' if valid. Validation of PDSM payloads happens later in 363 * papr_scm_service_pdsm. 364 */ 365 static int is_cmd_valid(struct nvdimm *nvdimm, unsigned int cmd, void *buf, 366 unsigned int buf_len) 367 { 368 unsigned long cmd_mask = PAPR_SCM_DIMM_CMD_MASK; 369 struct nd_cmd_pkg *nd_cmd; 370 struct papr_scm_priv *p; 371 enum papr_pdsm pdsm; 372 373 /* Only dimm-specific calls are supported atm */ 374 if (!nvdimm) 375 return -EINVAL; 376 377 /* get the provider data from struct nvdimm */ 378 p = nvdimm_provider_data(nvdimm); 379 380 if (!test_bit(cmd, &cmd_mask)) { 381 dev_dbg(&p->pdev->dev, "Unsupported cmd=%u\n", cmd); 382 return -EINVAL; 383 } 384 385 /* For CMD_CALL verify pdsm request */ 386 if (cmd == ND_CMD_CALL) { 387 /* Verify the envelope and envelop size */ 388 if (!buf || 389 buf_len < (sizeof(struct nd_cmd_pkg) + ND_PDSM_HDR_SIZE)) { 390 dev_dbg(&p->pdev->dev, "Invalid pkg size=%u\n", 391 buf_len); 392 return -EINVAL; 393 } 394 395 /* Verify that the nd_cmd_pkg.nd_family is correct */ 396 nd_cmd = (struct nd_cmd_pkg *)buf; 397 398 if (nd_cmd->nd_family != NVDIMM_FAMILY_PAPR) { 399 dev_dbg(&p->pdev->dev, "Invalid pkg family=0x%llx\n", 400 nd_cmd->nd_family); 401 return -EINVAL; 402 } 403 404 pdsm = (enum papr_pdsm)nd_cmd->nd_command; 405 406 /* Verify if the pdsm command is valid */ 407 if (pdsm <= PAPR_PDSM_MIN || pdsm >= PAPR_PDSM_MAX) { 408 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid PDSM\n", 409 pdsm); 410 return -EINVAL; 411 } 412 413 /* Have enough space to hold returned 'nd_pkg_pdsm' header */ 414 if (nd_cmd->nd_size_out < ND_PDSM_HDR_SIZE) { 415 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid payload\n", 416 pdsm); 417 return -EINVAL; 418 } 419 } 420 421 /* Let the command be further processed */ 422 return 0; 423 } 424 425 /* Fetch the DIMM health info and populate it in provided package. */ 426 static int papr_pdsm_health(struct papr_scm_priv *p, 427 union nd_pdsm_payload *payload) 428 { 429 int rc; 430 431 /* Ensure dimm health mutex is taken preventing concurrent access */ 432 rc = mutex_lock_interruptible(&p->health_mutex); 433 if (rc) 434 goto out; 435 436 /* Always fetch upto date dimm health data ignoring cached values */ 437 rc = __drc_pmem_query_health(p); 438 if (rc) { 439 mutex_unlock(&p->health_mutex); 440 goto out; 441 } 442 443 /* update health struct with various flags derived from health bitmap */ 444 payload->health = (struct nd_papr_pdsm_health) { 445 .extension_flags = 0, 446 .dimm_unarmed = !!(p->health_bitmap & PAPR_PMEM_UNARMED_MASK), 447 .dimm_bad_shutdown = !!(p->health_bitmap & PAPR_PMEM_BAD_SHUTDOWN_MASK), 448 .dimm_bad_restore = !!(p->health_bitmap & PAPR_PMEM_BAD_RESTORE_MASK), 449 .dimm_scrubbed = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED), 450 .dimm_locked = !!(p->health_bitmap & PAPR_PMEM_SCRUBBED_AND_LOCKED), 451 .dimm_encrypted = !!(p->health_bitmap & PAPR_PMEM_ENCRYPTED), 452 .dimm_health = PAPR_PDSM_DIMM_HEALTHY, 453 }; 454 455 /* Update field dimm_health based on health_bitmap flags */ 456 if (p->health_bitmap & PAPR_PMEM_HEALTH_FATAL) 457 payload->health.dimm_health = PAPR_PDSM_DIMM_FATAL; 458 else if (p->health_bitmap & PAPR_PMEM_HEALTH_CRITICAL) 459 payload->health.dimm_health = PAPR_PDSM_DIMM_CRITICAL; 460 else if (p->health_bitmap & PAPR_PMEM_HEALTH_UNHEALTHY) 461 payload->health.dimm_health = PAPR_PDSM_DIMM_UNHEALTHY; 462 463 /* struct populated hence can release the mutex now */ 464 mutex_unlock(&p->health_mutex); 465 rc = sizeof(struct nd_papr_pdsm_health); 466 467 out: 468 return rc; 469 } 470 471 /* 472 * 'struct pdsm_cmd_desc' 473 * Identifies supported PDSMs' expected length of in/out payloads 474 * and pdsm service function. 475 * 476 * size_in : Size of input payload if any in the PDSM request. 477 * size_out : Size of output payload if any in the PDSM request. 478 * service : Service function for the PDSM request. Return semantics: 479 * rc < 0 : Error servicing PDSM and rc indicates the error. 480 * rc >=0 : Serviced successfully and 'rc' indicate number of 481 * bytes written to payload. 482 */ 483 struct pdsm_cmd_desc { 484 u32 size_in; 485 u32 size_out; 486 int (*service)(struct papr_scm_priv *dimm, 487 union nd_pdsm_payload *payload); 488 }; 489 490 /* Holds all supported PDSMs' command descriptors */ 491 static const struct pdsm_cmd_desc __pdsm_cmd_descriptors[] = { 492 [PAPR_PDSM_MIN] = { 493 .size_in = 0, 494 .size_out = 0, 495 .service = NULL, 496 }, 497 /* New PDSM command descriptors to be added below */ 498 499 [PAPR_PDSM_HEALTH] = { 500 .size_in = 0, 501 .size_out = sizeof(struct nd_papr_pdsm_health), 502 .service = papr_pdsm_health, 503 }, 504 /* Empty */ 505 [PAPR_PDSM_MAX] = { 506 .size_in = 0, 507 .size_out = 0, 508 .service = NULL, 509 }, 510 }; 511 512 /* Given a valid pdsm cmd return its command descriptor else return NULL */ 513 static inline const struct pdsm_cmd_desc *pdsm_cmd_desc(enum papr_pdsm cmd) 514 { 515 if (cmd >= 0 || cmd < ARRAY_SIZE(__pdsm_cmd_descriptors)) 516 return &__pdsm_cmd_descriptors[cmd]; 517 518 return NULL; 519 } 520 521 /* 522 * For a given pdsm request call an appropriate service function. 523 * Returns errors if any while handling the pdsm command package. 524 */ 525 static int papr_scm_service_pdsm(struct papr_scm_priv *p, 526 struct nd_cmd_pkg *pkg) 527 { 528 /* Get the PDSM header and PDSM command */ 529 struct nd_pkg_pdsm *pdsm_pkg = (struct nd_pkg_pdsm *)pkg->nd_payload; 530 enum papr_pdsm pdsm = (enum papr_pdsm)pkg->nd_command; 531 const struct pdsm_cmd_desc *pdsc; 532 int rc; 533 534 /* Fetch corresponding pdsm descriptor for validation and servicing */ 535 pdsc = pdsm_cmd_desc(pdsm); 536 537 /* Validate pdsm descriptor */ 538 /* Ensure that reserved fields are 0 */ 539 if (pdsm_pkg->reserved[0] || pdsm_pkg->reserved[1]) { 540 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Invalid reserved field\n", 541 pdsm); 542 return -EINVAL; 543 } 544 545 /* If pdsm expects some input, then ensure that the size_in matches */ 546 if (pdsc->size_in && 547 pkg->nd_size_in != (pdsc->size_in + ND_PDSM_HDR_SIZE)) { 548 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_in=%d\n", 549 pdsm, pkg->nd_size_in); 550 return -EINVAL; 551 } 552 553 /* If pdsm wants to return data, then ensure that size_out matches */ 554 if (pdsc->size_out && 555 pkg->nd_size_out != (pdsc->size_out + ND_PDSM_HDR_SIZE)) { 556 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Mismatched size_out=%d\n", 557 pdsm, pkg->nd_size_out); 558 return -EINVAL; 559 } 560 561 /* Service the pdsm */ 562 if (pdsc->service) { 563 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Servicing..\n", pdsm); 564 565 rc = pdsc->service(p, &pdsm_pkg->payload); 566 567 if (rc < 0) { 568 /* error encountered while servicing pdsm */ 569 pdsm_pkg->cmd_status = rc; 570 pkg->nd_fw_size = ND_PDSM_HDR_SIZE; 571 } else { 572 /* pdsm serviced and 'rc' bytes written to payload */ 573 pdsm_pkg->cmd_status = 0; 574 pkg->nd_fw_size = ND_PDSM_HDR_SIZE + rc; 575 } 576 } else { 577 dev_dbg(&p->pdev->dev, "PDSM[0x%x]: Unsupported PDSM request\n", 578 pdsm); 579 pdsm_pkg->cmd_status = -ENOENT; 580 pkg->nd_fw_size = ND_PDSM_HDR_SIZE; 581 } 582 583 return pdsm_pkg->cmd_status; 584 } 585 586 static int papr_scm_ndctl(struct nvdimm_bus_descriptor *nd_desc, 587 struct nvdimm *nvdimm, unsigned int cmd, void *buf, 588 unsigned int buf_len, int *cmd_rc) 589 { 590 struct nd_cmd_get_config_size *get_size_hdr; 591 struct nd_cmd_pkg *call_pkg = NULL; 592 struct papr_scm_priv *p; 593 int rc; 594 595 rc = is_cmd_valid(nvdimm, cmd, buf, buf_len); 596 if (rc) { 597 pr_debug("Invalid cmd=0x%x. Err=%d\n", cmd, rc); 598 return rc; 599 } 600 601 /* Use a local variable in case cmd_rc pointer is NULL */ 602 if (!cmd_rc) 603 cmd_rc = &rc; 604 605 p = nvdimm_provider_data(nvdimm); 606 607 switch (cmd) { 608 case ND_CMD_GET_CONFIG_SIZE: 609 get_size_hdr = buf; 610 611 get_size_hdr->status = 0; 612 get_size_hdr->max_xfer = 8; 613 get_size_hdr->config_size = p->metadata_size; 614 *cmd_rc = 0; 615 break; 616 617 case ND_CMD_GET_CONFIG_DATA: 618 *cmd_rc = papr_scm_meta_get(p, buf); 619 break; 620 621 case ND_CMD_SET_CONFIG_DATA: 622 *cmd_rc = papr_scm_meta_set(p, buf); 623 break; 624 625 case ND_CMD_CALL: 626 call_pkg = (struct nd_cmd_pkg *)buf; 627 *cmd_rc = papr_scm_service_pdsm(p, call_pkg); 628 break; 629 630 default: 631 dev_dbg(&p->pdev->dev, "Unknown command = %d\n", cmd); 632 return -EINVAL; 633 } 634 635 dev_dbg(&p->pdev->dev, "returned with cmd_rc = %d\n", *cmd_rc); 636 637 return 0; 638 } 639 640 static ssize_t flags_show(struct device *dev, 641 struct device_attribute *attr, char *buf) 642 { 643 struct nvdimm *dimm = to_nvdimm(dev); 644 struct papr_scm_priv *p = nvdimm_provider_data(dimm); 645 struct seq_buf s; 646 u64 health; 647 int rc; 648 649 rc = drc_pmem_query_health(p); 650 if (rc) 651 return rc; 652 653 /* Copy health_bitmap locally, check masks & update out buffer */ 654 health = READ_ONCE(p->health_bitmap); 655 656 seq_buf_init(&s, buf, PAGE_SIZE); 657 if (health & PAPR_PMEM_UNARMED_MASK) 658 seq_buf_printf(&s, "not_armed "); 659 660 if (health & PAPR_PMEM_BAD_SHUTDOWN_MASK) 661 seq_buf_printf(&s, "flush_fail "); 662 663 if (health & PAPR_PMEM_BAD_RESTORE_MASK) 664 seq_buf_printf(&s, "restore_fail "); 665 666 if (health & PAPR_PMEM_ENCRYPTED) 667 seq_buf_printf(&s, "encrypted "); 668 669 if (health & PAPR_PMEM_SMART_EVENT_MASK) 670 seq_buf_printf(&s, "smart_notify "); 671 672 if (health & PAPR_PMEM_SCRUBBED_AND_LOCKED) 673 seq_buf_printf(&s, "scrubbed locked "); 674 675 if (seq_buf_used(&s)) 676 seq_buf_printf(&s, "\n"); 677 678 return seq_buf_used(&s); 679 } 680 DEVICE_ATTR_RO(flags); 681 682 /* papr_scm specific dimm attributes */ 683 static struct attribute *papr_nd_attributes[] = { 684 &dev_attr_flags.attr, 685 NULL, 686 }; 687 688 static struct attribute_group papr_nd_attribute_group = { 689 .name = "papr", 690 .attrs = papr_nd_attributes, 691 }; 692 693 static const struct attribute_group *papr_nd_attr_groups[] = { 694 &papr_nd_attribute_group, 695 NULL, 696 }; 697 698 static int papr_scm_nvdimm_init(struct papr_scm_priv *p) 699 { 700 struct device *dev = &p->pdev->dev; 701 struct nd_mapping_desc mapping; 702 struct nd_region_desc ndr_desc; 703 unsigned long dimm_flags; 704 int target_nid, online_nid; 705 706 p->bus_desc.ndctl = papr_scm_ndctl; 707 p->bus_desc.module = THIS_MODULE; 708 p->bus_desc.of_node = p->pdev->dev.of_node; 709 p->bus_desc.provider_name = kstrdup(p->pdev->name, GFP_KERNEL); 710 711 if (!p->bus_desc.provider_name) 712 return -ENOMEM; 713 714 p->bus = nvdimm_bus_register(NULL, &p->bus_desc); 715 if (!p->bus) { 716 dev_err(dev, "Error creating nvdimm bus %pOF\n", p->dn); 717 kfree(p->bus_desc.provider_name); 718 return -ENXIO; 719 } 720 721 dimm_flags = 0; 722 set_bit(NDD_LABELING, &dimm_flags); 723 724 p->nvdimm = nvdimm_create(p->bus, p, papr_nd_attr_groups, 725 dimm_flags, PAPR_SCM_DIMM_CMD_MASK, 0, NULL); 726 if (!p->nvdimm) { 727 dev_err(dev, "Error creating DIMM object for %pOF\n", p->dn); 728 goto err; 729 } 730 731 if (nvdimm_bus_check_dimm_count(p->bus, 1)) 732 goto err; 733 734 /* now add the region */ 735 736 memset(&mapping, 0, sizeof(mapping)); 737 mapping.nvdimm = p->nvdimm; 738 mapping.start = 0; 739 mapping.size = p->blocks * p->block_size; // XXX: potential overflow? 740 741 memset(&ndr_desc, 0, sizeof(ndr_desc)); 742 target_nid = dev_to_node(&p->pdev->dev); 743 online_nid = numa_map_to_online_node(target_nid); 744 ndr_desc.numa_node = online_nid; 745 ndr_desc.target_node = target_nid; 746 ndr_desc.res = &p->res; 747 ndr_desc.of_node = p->dn; 748 ndr_desc.provider_data = p; 749 ndr_desc.mapping = &mapping; 750 ndr_desc.num_mappings = 1; 751 ndr_desc.nd_set = &p->nd_set; 752 753 if (p->is_volatile) 754 p->region = nvdimm_volatile_region_create(p->bus, &ndr_desc); 755 else { 756 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc.flags); 757 p->region = nvdimm_pmem_region_create(p->bus, &ndr_desc); 758 } 759 if (!p->region) { 760 dev_err(dev, "Error registering region %pR from %pOF\n", 761 ndr_desc.res, p->dn); 762 goto err; 763 } 764 if (target_nid != online_nid) 765 dev_info(dev, "Region registered with target node %d and online node %d", 766 target_nid, online_nid); 767 768 mutex_lock(&papr_ndr_lock); 769 list_add_tail(&p->region_list, &papr_nd_regions); 770 mutex_unlock(&papr_ndr_lock); 771 772 return 0; 773 774 err: nvdimm_bus_unregister(p->bus); 775 kfree(p->bus_desc.provider_name); 776 return -ENXIO; 777 } 778 779 static void papr_scm_add_badblock(struct nd_region *region, 780 struct nvdimm_bus *bus, u64 phys_addr) 781 { 782 u64 aligned_addr = ALIGN_DOWN(phys_addr, L1_CACHE_BYTES); 783 784 if (nvdimm_bus_add_badrange(bus, aligned_addr, L1_CACHE_BYTES)) { 785 pr_err("Bad block registration for 0x%llx failed\n", phys_addr); 786 return; 787 } 788 789 pr_debug("Add memory range (0x%llx - 0x%llx) as bad range\n", 790 aligned_addr, aligned_addr + L1_CACHE_BYTES); 791 792 nvdimm_region_notify(region, NVDIMM_REVALIDATE_POISON); 793 } 794 795 static int handle_mce_ue(struct notifier_block *nb, unsigned long val, 796 void *data) 797 { 798 struct machine_check_event *evt = data; 799 struct papr_scm_priv *p; 800 u64 phys_addr; 801 bool found = false; 802 803 if (evt->error_type != MCE_ERROR_TYPE_UE) 804 return NOTIFY_DONE; 805 806 if (list_empty(&papr_nd_regions)) 807 return NOTIFY_DONE; 808 809 /* 810 * The physical address obtained here is PAGE_SIZE aligned, so get the 811 * exact address from the effective address 812 */ 813 phys_addr = evt->u.ue_error.physical_address + 814 (evt->u.ue_error.effective_address & ~PAGE_MASK); 815 816 if (!evt->u.ue_error.physical_address_provided || 817 !is_zone_device_page(pfn_to_page(phys_addr >> PAGE_SHIFT))) 818 return NOTIFY_DONE; 819 820 /* mce notifier is called from a process context, so mutex is safe */ 821 mutex_lock(&papr_ndr_lock); 822 list_for_each_entry(p, &papr_nd_regions, region_list) { 823 if (phys_addr >= p->res.start && phys_addr <= p->res.end) { 824 found = true; 825 break; 826 } 827 } 828 829 if (found) 830 papr_scm_add_badblock(p->region, p->bus, phys_addr); 831 832 mutex_unlock(&papr_ndr_lock); 833 834 return found ? NOTIFY_OK : NOTIFY_DONE; 835 } 836 837 static struct notifier_block mce_ue_nb = { 838 .notifier_call = handle_mce_ue 839 }; 840 841 static int papr_scm_probe(struct platform_device *pdev) 842 { 843 struct device_node *dn = pdev->dev.of_node; 844 u32 drc_index, metadata_size; 845 u64 blocks, block_size; 846 struct papr_scm_priv *p; 847 const char *uuid_str; 848 u64 uuid[2]; 849 int rc; 850 851 /* check we have all the required DT properties */ 852 if (of_property_read_u32(dn, "ibm,my-drc-index", &drc_index)) { 853 dev_err(&pdev->dev, "%pOF: missing drc-index!\n", dn); 854 return -ENODEV; 855 } 856 857 if (of_property_read_u64(dn, "ibm,block-size", &block_size)) { 858 dev_err(&pdev->dev, "%pOF: missing block-size!\n", dn); 859 return -ENODEV; 860 } 861 862 if (of_property_read_u64(dn, "ibm,number-of-blocks", &blocks)) { 863 dev_err(&pdev->dev, "%pOF: missing number-of-blocks!\n", dn); 864 return -ENODEV; 865 } 866 867 if (of_property_read_string(dn, "ibm,unit-guid", &uuid_str)) { 868 dev_err(&pdev->dev, "%pOF: missing unit-guid!\n", dn); 869 return -ENODEV; 870 } 871 872 873 p = kzalloc(sizeof(*p), GFP_KERNEL); 874 if (!p) 875 return -ENOMEM; 876 877 /* Initialize the dimm mutex */ 878 mutex_init(&p->health_mutex); 879 880 /* optional DT properties */ 881 of_property_read_u32(dn, "ibm,metadata-size", &metadata_size); 882 883 p->dn = dn; 884 p->drc_index = drc_index; 885 p->block_size = block_size; 886 p->blocks = blocks; 887 p->is_volatile = !of_property_read_bool(dn, "ibm,cache-flush-required"); 888 889 /* We just need to ensure that set cookies are unique across */ 890 uuid_parse(uuid_str, (uuid_t *) uuid); 891 /* 892 * cookie1 and cookie2 are not really little endian 893 * we store a little endian representation of the 894 * uuid str so that we can compare this with the label 895 * area cookie irrespective of the endian config with which 896 * the kernel is built. 897 */ 898 p->nd_set.cookie1 = cpu_to_le64(uuid[0]); 899 p->nd_set.cookie2 = cpu_to_le64(uuid[1]); 900 901 /* might be zero */ 902 p->metadata_size = metadata_size; 903 p->pdev = pdev; 904 905 /* request the hypervisor to bind this region to somewhere in memory */ 906 rc = drc_pmem_bind(p); 907 908 /* If phyp says drc memory still bound then force unbound and retry */ 909 if (rc == H_OVERLAP) 910 rc = drc_pmem_query_n_bind(p); 911 912 if (rc != H_SUCCESS) { 913 dev_err(&p->pdev->dev, "bind err: %d\n", rc); 914 rc = -ENXIO; 915 goto err; 916 } 917 918 /* setup the resource for the newly bound range */ 919 p->res.start = p->bound_addr; 920 p->res.end = p->bound_addr + p->blocks * p->block_size - 1; 921 p->res.name = pdev->name; 922 p->res.flags = IORESOURCE_MEM; 923 924 rc = papr_scm_nvdimm_init(p); 925 if (rc) 926 goto err2; 927 928 platform_set_drvdata(pdev, p); 929 930 return 0; 931 932 err2: drc_pmem_unbind(p); 933 err: kfree(p); 934 return rc; 935 } 936 937 static int papr_scm_remove(struct platform_device *pdev) 938 { 939 struct papr_scm_priv *p = platform_get_drvdata(pdev); 940 941 mutex_lock(&papr_ndr_lock); 942 list_del(&p->region_list); 943 mutex_unlock(&papr_ndr_lock); 944 945 nvdimm_bus_unregister(p->bus); 946 drc_pmem_unbind(p); 947 kfree(p->bus_desc.provider_name); 948 kfree(p); 949 950 return 0; 951 } 952 953 static const struct of_device_id papr_scm_match[] = { 954 { .compatible = "ibm,pmemory" }, 955 { .compatible = "ibm,pmemory-v2" }, 956 { }, 957 }; 958 959 static struct platform_driver papr_scm_driver = { 960 .probe = papr_scm_probe, 961 .remove = papr_scm_remove, 962 .driver = { 963 .name = "papr_scm", 964 .of_match_table = papr_scm_match, 965 }, 966 }; 967 968 static int __init papr_scm_init(void) 969 { 970 int ret; 971 972 ret = platform_driver_register(&papr_scm_driver); 973 if (!ret) 974 mce_register_notifier(&mce_ue_nb); 975 976 return ret; 977 } 978 module_init(papr_scm_init); 979 980 static void __exit papr_scm_exit(void) 981 { 982 mce_unregister_notifier(&mce_ue_nb); 983 platform_driver_unregister(&papr_scm_driver); 984 } 985 module_exit(papr_scm_exit); 986 987 MODULE_DEVICE_TABLE(of, papr_scm_match); 988 MODULE_LICENSE("GPL"); 989 MODULE_AUTHOR("IBM Corporation"); 990