1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) IBM Corporation 2017 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERGCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/device.h> 16 #include <linux/errno.h> 17 #include <linux/fs.h> 18 #include <linux/fsi.h> 19 #include <linux/fsi-sbefifo.h> 20 #include <linux/idr.h> 21 #include <linux/kernel.h> 22 #include <linux/miscdevice.h> 23 #include <linux/module.h> 24 #include <linux/mutex.h> 25 #include <linux/of.h> 26 #include <linux/of_device.h> 27 #include <linux/of_platform.h> 28 #include <linux/sched.h> 29 #include <linux/slab.h> 30 #include <linux/uaccess.h> 31 #include <linux/delay.h> 32 #include <linux/uio.h> 33 #include <linux/vmalloc.h> 34 35 /* 36 * The SBEFIFO is a pipe-like FSI device for communicating with 37 * the self boot engine on POWER processors. 38 */ 39 40 #define DEVICE_NAME "sbefifo" 41 #define FSI_ENGID_SBE 0x22 42 43 /* 44 * Register layout 45 */ 46 47 /* Register banks */ 48 #define SBEFIFO_UP 0x00 /* FSI -> Host */ 49 #define SBEFIFO_DOWN 0x40 /* Host -> FSI */ 50 51 /* Per-bank registers */ 52 #define SBEFIFO_FIFO 0x00 /* The FIFO itself */ 53 #define SBEFIFO_STS 0x04 /* Status register */ 54 #define SBEFIFO_STS_PARITY_ERR 0x20000000 55 #define SBEFIFO_STS_RESET_REQ 0x02000000 56 #define SBEFIFO_STS_GOT_EOT 0x00800000 57 #define SBEFIFO_STS_MAX_XFER_LIMIT 0x00400000 58 #define SBEFIFO_STS_FULL 0x00200000 59 #define SBEFIFO_STS_EMPTY 0x00100000 60 #define SBEFIFO_STS_ECNT_MASK 0x000f0000 61 #define SBEFIFO_STS_ECNT_SHIFT 16 62 #define SBEFIFO_STS_VALID_MASK 0x0000ff00 63 #define SBEFIFO_STS_VALID_SHIFT 8 64 #define SBEFIFO_STS_EOT_MASK 0x000000ff 65 #define SBEFIFO_STS_EOT_SHIFT 0 66 #define SBEFIFO_EOT_RAISE 0x08 /* (Up only) Set End Of Transfer */ 67 #define SBEFIFO_REQ_RESET 0x0C /* (Up only) Reset Request */ 68 #define SBEFIFO_PERFORM_RESET 0x10 /* (Down only) Perform Reset */ 69 #define SBEFIFO_EOT_ACK 0x14 /* (Down only) Acknowledge EOT */ 70 #define SBEFIFO_DOWN_MAX 0x18 /* (Down only) Max transfer */ 71 72 /* CFAM GP Mailbox SelfBoot Message register */ 73 #define CFAM_GP_MBOX_SBM_ADDR 0x2824 /* Converted 0x2809 */ 74 75 #define CFAM_SBM_SBE_BOOTED 0x80000000 76 #define CFAM_SBM_SBE_ASYNC_FFDC 0x40000000 77 #define CFAM_SBM_SBE_STATE_MASK 0x00f00000 78 #define CFAM_SBM_SBE_STATE_SHIFT 20 79 80 enum sbe_state 81 { 82 SBE_STATE_UNKNOWN = 0x0, // Unkown, initial state 83 SBE_STATE_IPLING = 0x1, // IPL'ing - autonomous mode (transient) 84 SBE_STATE_ISTEP = 0x2, // ISTEP - Running IPL by steps (transient) 85 SBE_STATE_MPIPL = 0x3, // MPIPL 86 SBE_STATE_RUNTIME = 0x4, // SBE Runtime 87 SBE_STATE_DMT = 0x5, // Dead Man Timer State (transient) 88 SBE_STATE_DUMP = 0x6, // Dumping 89 SBE_STATE_FAILURE = 0x7, // Internal SBE failure 90 SBE_STATE_QUIESCE = 0x8, // Final state - needs SBE reset to get out 91 }; 92 93 /* FIFO depth */ 94 #define SBEFIFO_FIFO_DEPTH 8 95 96 /* Helpers */ 97 #define sbefifo_empty(sts) ((sts) & SBEFIFO_STS_EMPTY) 98 #define sbefifo_full(sts) ((sts) & SBEFIFO_STS_FULL) 99 #define sbefifo_parity_err(sts) ((sts) & SBEFIFO_STS_PARITY_ERR) 100 #define sbefifo_populated(sts) (((sts) & SBEFIFO_STS_ECNT_MASK) >> SBEFIFO_STS_ECNT_SHIFT) 101 #define sbefifo_vacant(sts) (SBEFIFO_FIFO_DEPTH - sbefifo_populated(sts)) 102 #define sbefifo_eot_set(sts) (((sts) & SBEFIFO_STS_EOT_MASK) >> SBEFIFO_STS_EOT_SHIFT) 103 104 /* Reset request timeout in ms */ 105 #define SBEFIFO_RESET_TIMEOUT 10000 106 107 /* Timeouts for commands in ms */ 108 #define SBEFIFO_TIMEOUT_START_CMD 10000 109 #define SBEFIFO_TIMEOUT_IN_CMD 1000 110 #define SBEFIFO_TIMEOUT_START_RSP 10000 111 #define SBEFIFO_TIMEOUT_IN_RSP 1000 112 113 /* Other constants */ 114 #define SBEFIFO_MAX_CMD_LEN PAGE_SIZE 115 #define SBEFIFO_RESET_MAGIC 0x52534554 /* "RSET" */ 116 117 struct sbefifo { 118 uint32_t magic; 119 #define SBEFIFO_MAGIC 0x53424546 /* "SBEF" */ 120 struct fsi_device *fsi_dev; 121 struct miscdevice mdev; 122 struct mutex lock; 123 char name[32]; 124 int idx; 125 bool broken; 126 bool async_ffdc; 127 }; 128 129 struct sbefifo_user { 130 struct sbefifo *sbefifo; 131 struct mutex file_lock; 132 void *pending_cmd; 133 size_t pending_len; 134 }; 135 136 static DEFINE_IDA(sbefifo_ida); 137 static DEFINE_MUTEX(sbefifo_ffdc_mutex); 138 139 140 static void sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc, 141 size_t ffdc_sz, bool internal) 142 { 143 int pack = 0; 144 #define FFDC_LSIZE 60 145 static char ffdc_line[FFDC_LSIZE]; 146 char *p = ffdc_line; 147 148 mutex_lock(&sbefifo_ffdc_mutex); 149 while (ffdc_sz) { 150 u32 w0, w1, w2, i; 151 if (ffdc_sz < 3) { 152 dev_err(dev, "SBE invalid FFDC package size %zd\n", ffdc_sz); 153 return; 154 } 155 w0 = be32_to_cpu(*(ffdc++)); 156 w1 = be32_to_cpu(*(ffdc++)); 157 w2 = be32_to_cpu(*(ffdc++)); 158 ffdc_sz -= 3; 159 if ((w0 >> 16) != 0xFFDC) { 160 dev_err(dev, "SBE invalid FFDC package signature %08x %08x %08x\n", 161 w0, w1, w2); 162 break; 163 } 164 w0 &= 0xffff; 165 if (w0 > ffdc_sz) { 166 dev_err(dev, "SBE FFDC package len %d words but only %zd remaining\n", 167 w0, ffdc_sz); 168 w0 = ffdc_sz; 169 break; 170 } 171 if (internal) { 172 dev_warn(dev, "+---- SBE FFDC package %d for async err -----+\n", 173 pack++); 174 } else { 175 dev_warn(dev, "+---- SBE FFDC package %d for cmd %02x:%02x -----+\n", 176 pack++, (w1 >> 8) & 0xff, w1 & 0xff); 177 } 178 dev_warn(dev, "| Response code: %08x |\n", w2); 179 dev_warn(dev, "|-------------------------------------------|\n"); 180 for (i = 0; i < w0; i++) { 181 if ((i & 3) == 0) { 182 p = ffdc_line; 183 p += sprintf(p, "| %04x:", i << 4); 184 } 185 p += sprintf(p, " %08x", be32_to_cpu(*(ffdc++))); 186 ffdc_sz--; 187 if ((i & 3) == 3 || i == (w0 - 1)) { 188 while ((i & 3) < 3) { 189 p += sprintf(p, " "); 190 i++; 191 } 192 dev_warn(dev, "%s |\n", ffdc_line); 193 } 194 } 195 dev_warn(dev, "+-------------------------------------------+\n"); 196 } 197 } 198 199 int sbefifo_parse_status(struct device *dev, u16 cmd, __be32 *response, 200 size_t resp_len, size_t *data_len) 201 { 202 u32 dh, s0, s1; 203 size_t ffdc_sz; 204 205 if (resp_len < 3) { 206 pr_debug("sbefifo: cmd %04x, response too small: %zd\n", 207 cmd, resp_len); 208 return -ENXIO; 209 } 210 dh = be32_to_cpu(response[resp_len - 1]); 211 if (dh > resp_len || dh < 3) { 212 dev_err(dev, "SBE cmd %02x:%02x status offset out of range: %d/%zd\n", 213 cmd >> 8, cmd & 0xff, dh, resp_len); 214 return -ENXIO; 215 } 216 s0 = be32_to_cpu(response[resp_len - dh]); 217 s1 = be32_to_cpu(response[resp_len - dh + 1]); 218 if (((s0 >> 16) != 0xC0DE) || ((s0 & 0xffff) != cmd)) { 219 dev_err(dev, "SBE cmd %02x:%02x, status signature invalid: 0x%08x 0x%08x\n", 220 cmd >> 8, cmd & 0xff, s0, s1); 221 return -ENXIO; 222 } 223 if (s1 != 0) { 224 ffdc_sz = dh - 3; 225 dev_warn(dev, "SBE error cmd %02x:%02x status=%04x:%04x\n", 226 cmd >> 8, cmd & 0xff, s1 >> 16, s1 & 0xffff); 227 if (ffdc_sz) 228 sbefifo_dump_ffdc(dev, &response[resp_len - dh + 2], 229 ffdc_sz, false); 230 } 231 if (data_len) 232 *data_len = resp_len - dh; 233 234 /* 235 * Primary status don't have the top bit set, so can't be confused with 236 * Linux negative error codes, so return the status word whole. 237 */ 238 return s1; 239 } 240 EXPORT_SYMBOL_GPL(sbefifo_parse_status); 241 242 static int sbefifo_regr(struct sbefifo *sbefifo, int reg, u32 *word) 243 { 244 __be32 raw_word; 245 int rc; 246 247 rc = fsi_device_read(sbefifo->fsi_dev, reg, &raw_word, 248 sizeof(raw_word)); 249 if (rc) 250 return rc; 251 252 *word = be32_to_cpu(raw_word); 253 254 return 0; 255 } 256 257 static int sbefifo_regw(struct sbefifo *sbefifo, int reg, u32 word) 258 { 259 __be32 raw_word = cpu_to_be32(word); 260 261 return fsi_device_write(sbefifo->fsi_dev, reg, &raw_word, 262 sizeof(raw_word)); 263 } 264 265 static int sbefifo_check_sbe_state(struct sbefifo *sbefifo) 266 { 267 __be32 raw_word; 268 u32 sbm; 269 int rc; 270 271 rc = fsi_slave_read(sbefifo->fsi_dev->slave, CFAM_GP_MBOX_SBM_ADDR, 272 &raw_word, sizeof(raw_word)); 273 if (rc) 274 return rc; 275 sbm = be32_to_cpu(raw_word); 276 277 /* SBE booted at all ? */ 278 if (!(sbm & CFAM_SBM_SBE_BOOTED)) 279 return -ESHUTDOWN; 280 281 /* Check its state */ 282 switch ((sbm & CFAM_SBM_SBE_STATE_MASK) >> CFAM_SBM_SBE_STATE_SHIFT) { 283 case SBE_STATE_UNKNOWN: 284 return -ESHUTDOWN; 285 case SBE_STATE_IPLING: 286 case SBE_STATE_ISTEP: 287 case SBE_STATE_MPIPL: 288 case SBE_STATE_DMT: 289 return -EBUSY; 290 case SBE_STATE_RUNTIME: 291 case SBE_STATE_DUMP: /* Not sure about that one */ 292 break; 293 case SBE_STATE_FAILURE: 294 case SBE_STATE_QUIESCE: 295 return -ESHUTDOWN; 296 } 297 298 /* Is there async FFDC available ? Remember it */ 299 if (sbm & CFAM_SBM_SBE_ASYNC_FFDC) 300 sbefifo->async_ffdc = true; 301 302 return 0; 303 } 304 305 /* Don't flip endianness of data to/from FIFO, just pass through. */ 306 static int sbefifo_down_read(struct sbefifo *sbefifo, __be32 *word) 307 { 308 return fsi_device_read(sbefifo->fsi_dev, SBEFIFO_DOWN, word, 309 sizeof(*word)); 310 } 311 312 static int sbefifo_up_write(struct sbefifo *sbefifo, __be32 word) 313 { 314 return fsi_device_write(sbefifo->fsi_dev, SBEFIFO_UP, &word, 315 sizeof(word)); 316 } 317 318 static int sbefifo_request_reset(struct sbefifo *sbefifo) 319 { 320 struct device *dev = &sbefifo->fsi_dev->dev; 321 u32 status, timeout; 322 int rc; 323 324 dev_dbg(dev, "Requesting FIFO reset\n"); 325 326 /* Mark broken first, will be cleared if reset succeeds */ 327 sbefifo->broken = true; 328 329 /* Send reset request */ 330 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1); 331 if (rc) { 332 dev_err(dev, "Sending reset request failed, rc=%d\n", rc); 333 return rc; 334 } 335 336 /* Wait for it to complete */ 337 for (timeout = 0; timeout < SBEFIFO_RESET_TIMEOUT; timeout++) { 338 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &status); 339 if (rc) { 340 dev_err(dev, "Failed to read UP fifo status during reset" 341 " , rc=%d\n", rc); 342 return rc; 343 } 344 345 if (!(status & SBEFIFO_STS_RESET_REQ)) { 346 dev_dbg(dev, "FIFO reset done\n"); 347 sbefifo->broken = false; 348 return 0; 349 } 350 351 msleep(1); 352 } 353 dev_err(dev, "FIFO reset timed out\n"); 354 355 return -ETIMEDOUT; 356 } 357 358 static int sbefifo_cleanup_hw(struct sbefifo *sbefifo) 359 { 360 struct device *dev = &sbefifo->fsi_dev->dev; 361 u32 up_status, down_status; 362 bool need_reset = false; 363 int rc; 364 365 rc = sbefifo_check_sbe_state(sbefifo); 366 if (rc) { 367 dev_dbg(dev, "SBE state=%d\n", rc); 368 return rc; 369 } 370 371 /* If broken, we don't need to look at status, go straight to reset */ 372 if (sbefifo->broken) 373 goto do_reset; 374 375 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status); 376 if (rc) { 377 dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc); 378 379 /* Will try reset again on next attempt at using it */ 380 sbefifo->broken = true; 381 return rc; 382 } 383 384 rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status); 385 if (rc) { 386 dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc); 387 388 /* Will try reset again on next attempt at using it */ 389 sbefifo->broken = true; 390 return rc; 391 } 392 393 /* The FIFO already contains a reset request from the SBE ? */ 394 if (down_status & SBEFIFO_STS_RESET_REQ) { 395 dev_info(dev, "Cleanup: FIFO reset request set, resetting\n"); 396 rc = sbefifo_regw(sbefifo, SBEFIFO_UP, SBEFIFO_PERFORM_RESET); 397 if (rc) { 398 sbefifo->broken = true; 399 dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc); 400 return rc; 401 } 402 sbefifo->broken = false; 403 return 0; 404 } 405 406 /* Parity error on either FIFO ? */ 407 if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR) 408 need_reset = true; 409 410 /* Either FIFO not empty ? */ 411 if (!((up_status & down_status) & SBEFIFO_STS_EMPTY)) 412 need_reset = true; 413 414 if (!need_reset) 415 return 0; 416 417 dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n", 418 up_status, down_status); 419 420 do_reset: 421 422 /* Mark broken, will be cleared if/when reset succeeds */ 423 return sbefifo_request_reset(sbefifo); 424 } 425 426 static int sbefifo_wait(struct sbefifo *sbefifo, bool up, 427 u32 *status, unsigned long timeout) 428 { 429 struct device *dev = &sbefifo->fsi_dev->dev; 430 unsigned long end_time; 431 bool ready = false; 432 u32 addr, sts = 0; 433 int rc; 434 435 dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down"); 436 437 addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS; 438 439 end_time = jiffies + timeout; 440 while (!time_after(jiffies, end_time)) { 441 cond_resched(); 442 rc = sbefifo_regr(sbefifo, addr, &sts); 443 if (rc < 0) { 444 dev_err(dev, "FSI error %d reading status register\n", rc); 445 return rc; 446 } 447 if (!up && sbefifo_parity_err(sts)) { 448 dev_err(dev, "Parity error in DOWN FIFO\n"); 449 return -ENXIO; 450 } 451 ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts)); 452 if (ready) 453 break; 454 } 455 if (!ready) { 456 dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts); 457 return -ETIMEDOUT; 458 } 459 dev_vdbg(dev, "End of wait status: %08x\n", sts); 460 461 *status = sts; 462 463 return 0; 464 } 465 466 static int sbefifo_send_command(struct sbefifo *sbefifo, 467 const __be32 *command, size_t cmd_len) 468 { 469 struct device *dev = &sbefifo->fsi_dev->dev; 470 size_t len, chunk, vacant = 0, remaining = cmd_len; 471 unsigned long timeout; 472 u32 status; 473 int rc; 474 475 dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n", 476 cmd_len, be32_to_cpu(command[1])); 477 478 /* As long as there's something to send */ 479 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD); 480 while (remaining) { 481 /* Wait for room in the FIFO */ 482 rc = sbefifo_wait(sbefifo, true, &status, timeout); 483 if (rc < 0) 484 return rc; 485 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD); 486 487 vacant = sbefifo_vacant(status); 488 len = chunk = min(vacant, remaining); 489 490 dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n", 491 status, vacant, chunk); 492 493 /* Write as much as we can */ 494 while (len--) { 495 rc = sbefifo_up_write(sbefifo, *(command++)); 496 if (rc) { 497 dev_err(dev, "FSI error %d writing UP FIFO\n", rc); 498 return rc; 499 } 500 } 501 remaining -= chunk; 502 vacant -= chunk; 503 } 504 505 /* If there's no room left, wait for some to write EOT */ 506 if (!vacant) { 507 rc = sbefifo_wait(sbefifo, true, &status, timeout); 508 if (rc) 509 return rc; 510 } 511 512 /* Send an EOT */ 513 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0); 514 if (rc) 515 dev_err(dev, "FSI error %d writing EOT\n", rc); 516 return rc; 517 } 518 519 static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response) 520 { 521 struct device *dev = &sbefifo->fsi_dev->dev; 522 u32 status, eot_set; 523 unsigned long timeout; 524 bool overflow = false; 525 __be32 data; 526 size_t len; 527 int rc; 528 529 dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response)); 530 531 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP); 532 for (;;) { 533 /* Grab FIFO status (this will handle parity errors) */ 534 rc = sbefifo_wait(sbefifo, false, &status, timeout); 535 if (rc < 0) 536 return rc; 537 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP); 538 539 /* Decode status */ 540 len = sbefifo_populated(status); 541 eot_set = sbefifo_eot_set(status); 542 543 dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set); 544 545 /* Go through the chunk */ 546 while(len--) { 547 /* Read the data */ 548 rc = sbefifo_down_read(sbefifo, &data); 549 if (rc < 0) 550 return rc; 551 552 /* Was it an EOT ? */ 553 if (eot_set & 0x80) { 554 /* 555 * There should be nothing else in the FIFO, 556 * if there is, mark broken, this will force 557 * a reset on next use, but don't fail the 558 * command. 559 */ 560 if (len) { 561 dev_warn(dev, "FIFO read hit" 562 " EOT with still %zd data\n", 563 len); 564 sbefifo->broken = true; 565 } 566 567 /* We are done */ 568 rc = sbefifo_regw(sbefifo, 569 SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0); 570 571 /* 572 * If that write fail, still complete the request but mark 573 * the fifo as broken for subsequent reset (not much else 574 * we can do here). 575 */ 576 if (rc) { 577 dev_err(dev, "FSI error %d ack'ing EOT\n", rc); 578 sbefifo->broken = true; 579 } 580 581 /* Tell whether we overflowed */ 582 return overflow ? -EOVERFLOW : 0; 583 } 584 585 /* Store it if there is room */ 586 if (iov_iter_count(response) >= sizeof(__be32)) { 587 if (copy_to_iter(&data, sizeof(__be32), response) < sizeof(__be32)) 588 return -EFAULT; 589 } else { 590 dev_vdbg(dev, "Response overflowed !\n"); 591 592 overflow = true; 593 } 594 595 /* Next EOT bit */ 596 eot_set <<= 1; 597 } 598 } 599 /* Shouldn't happen */ 600 return -EIO; 601 } 602 603 static int sbefifo_do_command(struct sbefifo *sbefifo, 604 const __be32 *command, size_t cmd_len, 605 struct iov_iter *response) 606 { 607 /* Try sending the command */ 608 int rc = sbefifo_send_command(sbefifo, command, cmd_len); 609 if (rc) 610 return rc; 611 612 /* Now, get the response */ 613 return sbefifo_read_response(sbefifo, response); 614 } 615 616 static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo) 617 { 618 struct device *dev = &sbefifo->fsi_dev->dev; 619 struct iov_iter ffdc_iter; 620 struct kvec ffdc_iov; 621 __be32 *ffdc; 622 size_t ffdc_sz; 623 __be32 cmd[2]; 624 int rc; 625 626 sbefifo->async_ffdc = false; 627 ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE); 628 if (!ffdc) { 629 dev_err(dev, "Failed to allocate SBE FFDC buffer\n"); 630 return; 631 } 632 ffdc_iov.iov_base = ffdc; 633 ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE; 634 iov_iter_kvec(&ffdc_iter, WRITE | ITER_KVEC, &ffdc_iov, 1, SBEFIFO_MAX_FFDC_SIZE); 635 cmd[0] = cpu_to_be32(2); 636 cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC); 637 rc = sbefifo_do_command(sbefifo, cmd, 2, &ffdc_iter); 638 if (rc != 0) { 639 dev_err(dev, "Error %d retrieving SBE FFDC\n", rc); 640 goto bail; 641 } 642 ffdc_sz = SBEFIFO_MAX_FFDC_SIZE - iov_iter_count(&ffdc_iter); 643 ffdc_sz /= sizeof(__be32); 644 rc = sbefifo_parse_status(dev, SBEFIFO_CMD_GET_SBE_FFDC, ffdc, 645 ffdc_sz, &ffdc_sz); 646 if (rc != 0) { 647 dev_err(dev, "Error %d decoding SBE FFDC\n", rc); 648 goto bail; 649 } 650 if (ffdc_sz > 0) 651 sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true); 652 bail: 653 vfree(ffdc); 654 655 } 656 657 static int __sbefifo_submit(struct sbefifo *sbefifo, 658 const __be32 *command, size_t cmd_len, 659 struct iov_iter *response) 660 { 661 struct device *dev = &sbefifo->fsi_dev->dev; 662 int rc; 663 664 if (cmd_len < 2 || be32_to_cpu(command[0]) != cmd_len) { 665 dev_vdbg(dev, "Invalid command len %zd (header: %d)\n", 666 cmd_len, be32_to_cpu(command[0])); 667 return -EINVAL; 668 } 669 670 /* First ensure the HW is in a clean state */ 671 rc = sbefifo_cleanup_hw(sbefifo); 672 if (rc) 673 return rc; 674 675 /* Look for async FFDC first if any */ 676 if (sbefifo->async_ffdc) 677 sbefifo_collect_async_ffdc(sbefifo); 678 679 rc = sbefifo_do_command(sbefifo, command, cmd_len, response); 680 if (rc != 0 && rc != -EOVERFLOW) 681 goto fail; 682 return rc; 683 fail: 684 /* 685 * On failure, attempt a reset. Ignore the result, it will mark 686 * the fifo broken if the reset fails 687 */ 688 sbefifo_request_reset(sbefifo); 689 690 /* Return original error */ 691 return rc; 692 } 693 694 /** 695 * sbefifo_submit() - Submit and SBE fifo command and receive response 696 * @dev: The sbefifo device 697 * @command: The raw command data 698 * @cmd_len: The command size (in 32-bit words) 699 * @response: The output response buffer 700 * @resp_len: In: Response buffer size, Out: Response size 701 * 702 * This will perform the entire operation. If the reponse buffer 703 * overflows, returns -EOVERFLOW 704 */ 705 int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len, 706 __be32 *response, size_t *resp_len) 707 { 708 struct sbefifo *sbefifo = dev_get_drvdata(dev); 709 struct iov_iter resp_iter; 710 struct kvec resp_iov; 711 size_t rbytes; 712 int rc; 713 714 if (!dev || !sbefifo) 715 return -ENODEV; 716 if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC)) 717 return -ENODEV; 718 if (!resp_len || !command || !response || cmd_len > SBEFIFO_MAX_CMD_LEN) 719 return -EINVAL; 720 721 /* Prepare iov iterator */ 722 rbytes = (*resp_len) * sizeof(__be32); 723 resp_iov.iov_base = response; 724 resp_iov.iov_len = rbytes; 725 iov_iter_kvec(&resp_iter, WRITE | ITER_KVEC, &resp_iov, 1, rbytes); 726 727 /* Perform the command */ 728 mutex_lock(&sbefifo->lock); 729 rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter); 730 mutex_unlock(&sbefifo->lock); 731 732 /* Extract the response length */ 733 rbytes -= iov_iter_count(&resp_iter); 734 *resp_len = rbytes / sizeof(__be32); 735 736 return rc; 737 } 738 EXPORT_SYMBOL_GPL(sbefifo_submit); 739 740 /* 741 * Char device interface 742 */ 743 static int sbefifo_user_open(struct inode *inode, struct file *file) 744 { 745 struct sbefifo *sbefifo = container_of(file->private_data, 746 struct sbefifo, mdev); 747 struct sbefifo_user *user; 748 749 user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL); 750 if (!user) 751 return -ENOMEM; 752 753 file->private_data = user; 754 user->sbefifo = sbefifo; 755 user->pending_cmd = (void *)__get_free_page(GFP_KERNEL); 756 if (!user->pending_cmd) { 757 kfree(user); 758 return -ENOMEM; 759 } 760 mutex_init(&user->file_lock); 761 762 return 0; 763 } 764 765 static ssize_t sbefifo_user_read(struct file *file, char __user *buf, 766 size_t len, loff_t *offset) 767 { 768 struct sbefifo_user *user = file->private_data; 769 struct sbefifo *sbefifo; 770 struct iov_iter resp_iter; 771 struct iovec resp_iov; 772 size_t cmd_len; 773 int rc; 774 775 if (!user) 776 return -EINVAL; 777 sbefifo = user->sbefifo; 778 if (len & 3) 779 return -EINVAL; 780 781 mutex_lock(&user->file_lock); 782 783 /* Cronus relies on -EAGAIN after a short read */ 784 if (user->pending_len == 0) { 785 rc = -EAGAIN; 786 goto bail; 787 } 788 if (user->pending_len < 8) { 789 rc = -EINVAL; 790 goto bail; 791 } 792 cmd_len = user->pending_len >> 2; 793 794 /* Prepare iov iterator */ 795 resp_iov.iov_base = buf; 796 resp_iov.iov_len = len; 797 iov_iter_init(&resp_iter, WRITE, &resp_iov, 1, len); 798 799 /* Perform the command */ 800 mutex_lock(&sbefifo->lock); 801 rc = __sbefifo_submit(sbefifo, user->pending_cmd, cmd_len, &resp_iter); 802 mutex_unlock(&sbefifo->lock); 803 if (rc < 0) 804 goto bail; 805 806 /* Extract the response length */ 807 rc = len - iov_iter_count(&resp_iter); 808 bail: 809 user->pending_len = 0; 810 mutex_unlock(&user->file_lock); 811 return rc; 812 } 813 814 static ssize_t sbefifo_user_write(struct file *file, const char __user *buf, 815 size_t len, loff_t *offset) 816 { 817 struct sbefifo_user *user = file->private_data; 818 struct sbefifo *sbefifo; 819 int rc = len; 820 821 if (!user) 822 return -EINVAL; 823 sbefifo = user->sbefifo; 824 if (len > SBEFIFO_MAX_CMD_LEN) 825 return -EINVAL; 826 if (len & 3) 827 return -EINVAL; 828 829 mutex_lock(&user->file_lock); 830 831 /* Copy the command into the staging buffer */ 832 if (copy_from_user(user->pending_cmd, buf, len)) { 833 rc = -EFAULT; 834 goto bail; 835 } 836 837 /* Check for the magic reset command */ 838 if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) == 839 SBEFIFO_RESET_MAGIC) { 840 841 /* Clear out any pending command */ 842 user->pending_len = 0; 843 844 /* Trigger reset request */ 845 mutex_lock(&sbefifo->lock); 846 rc = sbefifo_request_reset(user->sbefifo); 847 mutex_unlock(&sbefifo->lock); 848 if (rc == 0) 849 rc = 4; 850 goto bail; 851 } 852 853 /* Update the staging buffer size */ 854 user->pending_len = len; 855 bail: 856 mutex_unlock(&user->file_lock); 857 858 /* And that's it, we'll issue the command on a read */ 859 return rc; 860 } 861 862 static int sbefifo_user_release(struct inode *inode, struct file *file) 863 { 864 struct sbefifo_user *user = file->private_data; 865 866 if (!user) 867 return -EINVAL; 868 869 free_page((unsigned long)user->pending_cmd); 870 kfree(user); 871 872 return 0; 873 } 874 875 static const struct file_operations sbefifo_fops = { 876 .owner = THIS_MODULE, 877 .open = sbefifo_user_open, 878 .read = sbefifo_user_read, 879 .write = sbefifo_user_write, 880 .release = sbefifo_user_release, 881 }; 882 883 /* 884 * Probe/remove 885 */ 886 887 static int sbefifo_probe(struct device *dev) 888 { 889 struct fsi_device *fsi_dev = to_fsi_dev(dev); 890 struct sbefifo *sbefifo; 891 struct device_node *np; 892 struct platform_device *child; 893 char child_name[32]; 894 int rc, child_idx = 0; 895 896 dev_dbg(dev, "Found sbefifo device\n"); 897 898 sbefifo = devm_kzalloc(dev, sizeof(*sbefifo), GFP_KERNEL); 899 if (!sbefifo) 900 return -ENOMEM; 901 sbefifo->magic = SBEFIFO_MAGIC; 902 sbefifo->fsi_dev = fsi_dev; 903 mutex_init(&sbefifo->lock); 904 905 /* 906 * Try cleaning up the FIFO. If this fails, we still register the 907 * driver and will try cleaning things up again on the next access. 908 */ 909 rc = sbefifo_cleanup_hw(sbefifo); 910 if (rc && rc != -ESHUTDOWN) 911 dev_err(dev, "Initial HW cleanup failed, will retry later\n"); 912 913 sbefifo->idx = ida_simple_get(&sbefifo_ida, 1, INT_MAX, GFP_KERNEL); 914 snprintf(sbefifo->name, sizeof(sbefifo->name), "sbefifo%d", 915 sbefifo->idx); 916 917 dev_set_drvdata(dev, sbefifo); 918 919 /* Create misc chardev for userspace access */ 920 sbefifo->mdev.minor = MISC_DYNAMIC_MINOR; 921 sbefifo->mdev.fops = &sbefifo_fops; 922 sbefifo->mdev.name = sbefifo->name; 923 sbefifo->mdev.parent = dev; 924 rc = misc_register(&sbefifo->mdev); 925 if (rc) { 926 dev_err(dev, "Failed to register miscdevice: %d\n", rc); 927 ida_simple_remove(&sbefifo_ida, sbefifo->idx); 928 return rc; 929 } 930 931 /* Create platform devs for dts child nodes (occ, etc) */ 932 for_each_available_child_of_node(dev->of_node, np) { 933 snprintf(child_name, sizeof(child_name), "%s-dev%d", 934 sbefifo->name, child_idx++); 935 child = of_platform_device_create(np, child_name, dev); 936 if (!child) 937 dev_warn(dev, "failed to create child %s dev\n", 938 child_name); 939 } 940 941 return 0; 942 } 943 944 static int sbefifo_unregister_child(struct device *dev, void *data) 945 { 946 struct platform_device *child = to_platform_device(dev); 947 948 of_device_unregister(child); 949 if (dev->of_node) 950 of_node_clear_flag(dev->of_node, OF_POPULATED); 951 952 return 0; 953 } 954 955 static int sbefifo_remove(struct device *dev) 956 { 957 struct sbefifo *sbefifo = dev_get_drvdata(dev); 958 959 dev_dbg(dev, "Removing sbefifo device...\n"); 960 961 misc_deregister(&sbefifo->mdev); 962 device_for_each_child(dev, NULL, sbefifo_unregister_child); 963 964 ida_simple_remove(&sbefifo_ida, sbefifo->idx); 965 966 return 0; 967 } 968 969 static struct fsi_device_id sbefifo_ids[] = { 970 { 971 .engine_type = FSI_ENGID_SBE, 972 .version = FSI_VERSION_ANY, 973 }, 974 { 0 } 975 }; 976 977 static struct fsi_driver sbefifo_drv = { 978 .id_table = sbefifo_ids, 979 .drv = { 980 .name = DEVICE_NAME, 981 .bus = &fsi_bus_type, 982 .probe = sbefifo_probe, 983 .remove = sbefifo_remove, 984 } 985 }; 986 987 static int sbefifo_init(void) 988 { 989 return fsi_driver_register(&sbefifo_drv); 990 } 991 992 static void sbefifo_exit(void) 993 { 994 fsi_driver_unregister(&sbefifo_drv); 995 996 ida_destroy(&sbefifo_ida); 997 } 998 999 module_init(sbefifo_init); 1000 module_exit(sbefifo_exit); 1001 MODULE_LICENSE("GPL"); 1002 MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>"); 1003 MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>"); 1004 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>"); 1005 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); 1006 MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine"); 1007